Page 1 of 1

How to build web based text editor

Posted: Wed Jul 30, 2008 11:23 am
by storyboo
Ok Here is a little background on the topic. I have a shopping cart installed that I am working with. It comes bundled with three templates. The contents of each template folder is the same. The store administration currently allows the administrator (me) to select which skin to use via a drop down. But in order to edit that file...I must go into the server and edit the file with filemanager.

The Goal:

To take the selected skin from that drop down and have the html contents open into a text area which can will show me the current contents, allow me to edit it via the admin, and save the changes thus overwriting what was already there.

The Problem:

I THINK* I have the fread and fopen and fwrite commands right. However, because one of the directories in the file path is going to be dynamic (becuase there are three skins) I get a warning saying no such file or directory.

Another Problem:

I tried a test file that had a fixed path..not dynamic..just to test the functionality of my html form text area and it pulled the contents!!!! YAY!!! Success right? Well, I deleted the html code and typed the word testing. and hit update......upon refresh..the file was empty. When I looked into the server at that exact file and opened it..the old contents were still there....like its not overwriting. Now the contents arent opening anymore.


The CODE for FORM:----------------------Please note I had to include some things to create the administration panel to wrap around the file contents-------This File is named stm.php------------------------------------------------------------------------------------------------------------------------


<?php
include("../../includes/ini.inc.php");
include("../../includes/global.inc.php");
require_once("../../classes/db.inc.php");
$db = new db();
include_once("../../includes/functions.inc.php");
$config = fetchDbConfig("config");
include_once("../../language/".$config['defaultLang']."/lang.inc.php");
$enableSSl = 1;
include_once("../../includes/sslSwitch.inc.php");
include("../includes/auth.inc.php");
include("../includes/functions.inc.php");

if(permission("settings","read")==FALSE){
header("Location: ".$GLOBALS['rootRel']."admin/401.php");
exit;
}

if(isset($_POST['config'])){
$config = fetchDbConfig("config");
$msg = writeDbConf($_POST['config'],"config", $config, "config");
}
$config = fetchDbConfig("config");

$jsScript = jsGeoLocation("siteCountry", "siteCounty", "-- ".$lang['admin']['na']." --");

include("../includes/header.inc.php");
?>
<p class="pageTitle"><?php echo $lang['admin']['settings']['store_settings']; ?></p>
<?php
if(isset($msg)){
echo stripslashes($msg);
} else {
?>
<p class="copyText"><?php echo $lang['admin']['settings']['edit_below']; ?></p>
<?php } ?>
<table>
<tr>
<td width="30%" class="tdText"><strong><?php echo $lang['admin']['settings']['store_skin'];?></strong></td>
<td align="left">
<?php
$path = $GLOBALS['rootDir']."/skins";
if ($dir = opendir($path)) {
?>
<select class="textbox" name="config[skinDir]">
<?php

while (false !== ($file = readdir($dir))) {

if(!eregi($file,array(".",".."))){
?>
<option value="<?php echo $file; ?>" <?php if($file==$config['skinDir']) { echo "selected='selected'"; } ?>><?php echo $file; ?></option>
<?php
}
}
?>
</select>

<?php } ?> </td>
</tr><tr><td>


<form action="getfile.php" method="POST">
<table border="0" cellpadding="7" cellspacing="0" align="center" class="generaltable">
<tr>
<td align="center" class="fieldname" colspan="2">Custom Template</td>
</tr>
<tr>
<td align="center" colspan="2">
Paste your custom template's HTML code below, including all HTML tags<br>
and any <a href="includes/sitecodes.htm" target="_blank" onClick="PopUp=window.open('includes/sitecodes.htm',
'NewWin', 'resizable=yes,scrollbars=yes,width=400,height=400,left=0,top=0,screenX=0,screenY=0');
PopUp.focus(); return false;">site include codes</a> as needed. Use absolute references for all of your<br>
images and links, instead of relative ones. (ie. <a href="page.php">Page</a><br>
would be written as <a href="http://www.yoursite.com/page.php">Page</a>)</td>
</tr>
<td>
<tr>
<td align="center" colspan="2">
<textarea rows="20" name="store_template" cols="55">
<?php
$fn = "skins/".$config['skinDir']."/styleTemplates/global/index.tpl";
print htmlspecialchars(implode("",file($fn)));
?>
</textarea></td>
</tr>
<tr>
<tr>
<td width="30%" class="tdText">&nbsp;</td>
<td align="left">
<input name="submit" type="submit" class="submit" id="submit" value="Update Settings" /></td>
</tr>
</tr>
</table>
</form>
</td></tr></table>

<?php include("../includes/footer.inc.php"); ?>

-------------------------------------------------------------------------------------------------------
And now for the getfile.php that controls the form
-------------------------------------------------------------------------------------------------------


<?php
$fn = "skins/".$config['skinDir']."/styleTemplates/global/index.tpl";
$store_template = stripslashes($_POST['content']);
$fp = fopen($fn,"w") or die ("Error opening file in write mode!");
fputs($fp,$store_template);
fclose($fp) or die ("Error closing file!");
echo "<meta http-equiv=\"refresh\" content=\"0; url=stm.php\" />\n";
?>

------------------------------------------------------------------------------------------------

These files are both in the same folder. So how do I specify the path correctly..and get this thing to open, read out the file contents inside the html text area, allow me to make changes to that html data, and to when I hit save..have it overwrite the file so that when the page refreshes..I see my changes and the file in the server has been changed as well.

I have spent 14 hours doing this!!! Its my first php script :) PLEASE HELP!

Re: How to build web based text editor

Posted: Wed Jul 30, 2008 3:17 pm
by storyboo
Ok...I tweaked a bit and this is the new code:

For the admin page of the store that contains the form called stm.php:
------------------------------------------------------------------------

<?php
include("../../includes/ini.inc.php");
include("../../includes/global.inc.php");
require_once("../../classes/db.inc.php");
$db = new db();
include_once("../../includes/functions.inc.php");
$config = fetchDbConfig("config");
include_once("../../language/".$config['defaultLang']."/lang.inc.php");
$enableSSl = 1;
include_once("../../includes/sslSwitch.inc.php");
include("../includes/auth.inc.php");
include("../includes/functions.inc.php");

if(permission("settings","read")==FALSE){
header("Location: ".$GLOBALS['rootRel']."admin/401.php");
exit;
}

if(isset($_POST['config'])){
$config = fetchDbConfig("config");
$msg = writeDbConf($_POST['config'],"config", $config, "config");
}
$config = fetchDbConfig("config");

$jsScript = jsGeoLocation("siteCountry", "siteCounty", "-- ".$lang['admin']['na']." --");

include("../includes/header.inc.php");
?>
<p class="pageTitle"><?php echo $lang['admin']['settings']['store_settings']; ?></p>
<?php
if(isset($msg)){
echo stripslashes($msg);
} else {
?>
<p class="copyText"><?php echo $lang['admin']['settings']['edit_below']; ?></p>
<?php } ?>
<table>
<tr>
<td width="30%" class="tdText"><strong><?php echo $lang['admin']['settings']['store_skin'];?></strong></td>
<td align="left">
<?php
$path = $GLOBALS['rootDir']."/skins";
if ($dir = opendir($path)) {
?>
<select class="textbox" name="config[skinDir]">
<?php

while (false !== ($file = readdir($dir))) {

if(!eregi($file,array(".",".."))){
?>
<option value="<?php echo $file; ?>" <?php if($file==$config['skinDir']) { echo "selected='selected'"; } ?>><?php echo $file; ?></option>
<?php
}
}
?>
</select>

<?php } ?> </td>
</tr><tr><td>


<form action="getfile.php" method="POST">
<table border="0" cellpadding="7" cellspacing="0" align="center" class="generaltable">
<tr>
<td align="center" class="fieldname" colspan="2">Custom Template</td>
</tr>
<tr>
<td align="center" colspan="2">
Paste your custom template's HTML code below, including all HTML tags<br>
and any <a href="includes/sitecodes.htm" target="_blank" onClick="PopUp=window.open('includes/sitecodes.htm',
'NewWin', 'resizable=yes,scrollbars=yes,width=400,height=400,left=0,top=0,screenX=0,screenY=0');
PopUp.focus(); return false;">site include codes</a> as needed. Use absolute references for all of your<br>
images and links, instead of relative ones. (ie. <a href="page.php">Page</a><br>
would be written as <a href="http://www.yoursite.com/page.php">Page</a>)</td>
</tr>
<td>
<tr>
<td align="center" colspan="2">
<textarea rows="20" name="store_template" cols="55">
<?php
$fn = "http://www.marketmoms.com/skins/Killer/ ... /index.tpl";
print htmlspecialchars(implode("",file($fn)));
?>
</textarea></td>
</tr>
<tr>
<tr>
<td width="30%" class="tdText">&nbsp;</td>
<td align="left">
<input name="submit" type="submit" class="submit" id="submit" value="Update Settings" /></td>
</tr>
</tr>
</table>
</form>
</td></tr></table>

<?php include("../includes/footer.inc.php"); ?>


-----------------------------------------------------------------
And for the php file that the form uses called getfile.php:
-----------------------------------------------------------------

<?php
$fn = "$Home_Path/skins/Killer/styleTemplates/global/index.tpl";
$store_template = stripslashes($_POST['content']);
$fp = fopen($fn,"w") or die ("Error opening file in write mode!");
fputs($fp,$store_template);
fclose($fp) or die ("Error closing file!");
echo "<meta http-equiv=\"refresh\" content=\"0; url=stm.php\" />\n";
?>


BTW--I have version 5 of PHP but I had to have my host htaccess the file to application the version 5

NOW I GET THIS ERROR
-------------------------------------------------------------------------

Warning: fopen(/skins/Killer/styleTemplates/global/index.tpl) [function.fopen]: failed to open stream: No such file or directory in /home/marketmo/public_html/admin/settings/getfile.php on line 4
Error opening file in write mode!

--------------------------------------------------------------------------

What I did was I deleted the other two skins in the skin folder so that just the Killer Skin remains so that I could have a direct path..not a relative one. It would be best for me to have it so I can choose a skin and edit it...but..for the sake of testing I deleted the classic and legend skins. Now I have one index.tpl
Now I just learned this last night and I have no clue what I am doing so any help..holding my hand would be greatly appreciated. What is wrong with the code? :banghead: LOL