Jump to content

Web Based Editor For Admin Area to open read and write over the TPL


Guest storyboo

Recommended Posts

Guest storyboo

What I did was I made a new page inthe admin area and gave it a link under navigation. Then I made sure to included the general files like header.inc.php etc. I am trying to open the index.tpl to a form in the new admin page and then read it...and make changes and upon saving the changes..have the php script I wrote overwrite the contents of the index.tpl with my changes and then save those changes for next time I edit it. I have copied the code from the select skin under general settings to put on the new page, so that I can first choose a skin..then it loads in the text area below and then I can edit.

The PROBLEM:

I think I am not getting the path to the file correct. Or I may be missing something in my code. I FINALLY got the tpl to read to the text area...so now I can see my tpl file in the text area now and I can make changes. However, I had to delete all the other skins except the killer that I use and that way I was able to use the form to find file=" http://www.mystore.com/skins/Killer/styleT...lobal/index.tpl.

Also, I made sure to chmodd the files to writable readable. The file is displayed in my little text area BUT when I hit save to save changes, I get this error:

Warning: fopen(skins//styleTemplates/content/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!

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

The code for the form or extra adminpage I made:

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

<?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;

}

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,scr

eenY=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. &lt;a href=&quot;page.php&quot;&gt;Page&lt;/a&gt;<br>

would be written as &lt;a href=&quot;http://www.yoursite.com/page.php&quot;&gt;Page&lt;/a&gt;)</td>

</tr>

<td>

<tr>

<td align="center" colspan="2">

<textarea rows="20" name="store_template" cols="55">

<?php

$fn = "http://www.mysite.com/skins/Killer/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 here is the file for the file that the form calls to perform functions

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

<?php

$fn =("skins/".$config['skinDir']."/styleTemplates/content/index.tpl");

$store_template = stripslashes($_POST['content']);

$fp = fopen($fn,"r+") 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";

?>

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

NOTE: I also tried making the snippet above look like this as an alternative:

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

<?php

$fn =("http://www.mysite.com/skins/Killer/styleTemplates/content/index.tpl");

$store_template = stripslashes($_POST['content']);

$fp = fopen($fn,"r+") 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";

?>

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

HOWEVER THIS MADE A WARNING THAT THE HTTP wrapper does not support writeable connections in blahblahblah-------------

I REALLY want to be able to take the store admin select skin feature and then be able to add the functionality of editing the chosen skin file's TPL from admin. Have it open, read it, allow me to change it and save it. Then to be able to do it again anytime. What am I doing wrong?

Link to comment
Share on other sites

Your skin chooser section has form element tags not contained within form tags. The line <select class="textbox" name="config[skinDir]"> is outside a form construct.

You should also try to prepend your file locations with $glob['rootDir']. So:

$fn =("skins/".$config['skinDir']."/styleTemplates/content/index.tpl");

should be

$fn =($glob['rootDir']."skins/".$config['skinDir']."/styleTemplates/content/index.tpl");

If these suggestions get you further, let us know. Your mileage may vary.

Link to comment
Share on other sites

Guest storyboo

Hi!

I had tried that very line of code for the path ...I tried again however..cause your method was a bit different. A new error popped up unfortunately:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/marketmo/public_html/admin/settings/getfile.php on line 2

The line of code in question is:

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

$fn = ("$glob['rootDir']."skins/".$config['skinDir']."/styleTemplates/content/index.tpl");

I dont understand....shouldnt this work? Am I forgetting to include something to the php file? Hmmm The mystery deepens :) :)

Link to comment
Share on other sites

Guest storyboo

I'm a dumbass lol. Ok It did work but since I copy and paste your string directly..I just noticed that you put /content/index.tpl and it should be global/ not content/ LOL. Ok That makes the error go away :)

So now, I can read, write...but when I hit update settings button..it just loads back the original file...with no changes

Should I use something other than r+? Or it my form submit button missing a feature? Oh god..I just spent 4 hours without seeing that /content/ ...should have been /global/ ...god dang it...I'm a freaking n00b!

LOL...going to bed now :)

thank you from the bottom of my heart bsmither :) Now we just need to get the file contents to save. Anyone got a thought? BTW....I will offer this mod for free to everyone :)

Link to comment
Share on other sites

Guest storyboo

sigh... lol

ok..I was messing with the form action on my last post..but didnt realize...thus it was action=stm.php and it should have been action=getfile.php like my original post

---so now the error is back

Parse error: syntax error, unexpected T_STRING in /home/marketmo/public_html/admin/settings/getfile.php on line 2

and line 2 is

$fn = ("$glob[rootDir]."skins".[skinDir]."/styleTemplates/global/index.tpl");

please note my $fn in the file that actually contains the form (getfile.php is seperate form) is set to:

$fn = "http://www.mysite.com/skins/Killer/styleTemplates/global/index.tpl";---this prints out the contents for me to read....but if i make it relative...no go...I get warning that function.file does not exist.

Again still it boils down to the path of the file both in the getfile.php and the stm.php---noting that stm.php is the admin page fill displayed in store settings......and the getfile.php is what is done upon submitting the form.

Remember i want the form to find the current index.tpl that was configured in settings....and allows me to update the currenty configured one with my new tpl coding

Link to comment
Share on other sites

Guest storyboo

Just want to thank you bsmither :)

I finally got it working :) I will submit the mod to the forums as a free mod :) Before doing so ...I want to make the cart.tpl overwritable as well...plus allow myself the ability to upload file to the images folder so that I can completely control everything through admin :) Rather than server side.

The reason for the mod: I am a web site designer and I have to install websites left and right. I finally updated my store www.storybooksites.com so that when a client purchases, they can immediately download their package of graphics and the html code for the index.tpl page. I'm hoping that now..some of my clients will be able to copy and page and upload the graphics and let me get some real work done lol. I am foreve being asked if I can add their site counters or top 100 banner ads to their site :) This is going to be a TREMENDOUS help.

Just a recap.....what I did was I combined the two different files into one file...placing the php code for the action of the form below the form itself...also after HOURS of playing with slashes and dot and parenthesis combos to get the path just right I was finally able to get it :) Yes your suggestions got me far :) Thank you for helping on my first php script :) I understand a lot better now about php coding...although im a totally html geek--php has never been a likingof mine. I feel much more comfortable now.

Here is the perfect final version for anyone who is interested:

<?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;

}

include("../includes/header.inc.php");

?>

<p class="pageTitle"><?php echo $lang['admin']['settings']['store_settings']; ?></p>

<p class="copyText"><?php echo $lang['admin']['settings']['edit_below']; ?></p>



<form action="stm.php" method="POST">

<table border="0" cellpadding="7" cellspacing="0" align="center">

<tr>

<td align="center" colspan="2">Custom Template</td>

</tr>

<tr>

<td align="center" colspan="2">

<textarea rows="25" name="store_template" cols="65">

<?php

$fn = $glob['rootDir']."/skins/".$config['skinDir']."/styleTemplates/global/index.tpl";

print htmlspecialchars(implode("",file($fn)));

?> 

</textarea></td>

</tr>

<tr>

<td align="left"> 

<input name="submit" type="submit" class="submit" id="submit" value="Update Settings" /></td>

</tr>

</table>

</form>

<?php

if (isset($_POST['store_template']))

{

// Get the code from the form

	   $store_template = $_POST['store_template'];



$fn = $glob['rootDir']."/skins/".$config['skinDir']."/styleTemplates/global/index.tpl";

$store_template = stripslashes($_POST['store_template']);

$fh = fopen($fn,"w+") or die ("Error opening file in write mode!");

fputs($fh,$store_template) or die ("Error putting file!");

fclose($fh) or die ("Error closing file!");

echo "<meta http-equiv=\"refresh\" content=\"0; url=stm.php\" />\n";

}

?> 

<?php

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

Thank you bsmither :) :)

Link to comment
Share on other sites

I'm glad to see you got this working. Congratulations!

You are certainly helping to make CubeCart accessible to those with less technical skills and this is to be highly commended.

There's one thing that I would like to suggest though. Although many of your current clients will want to edit templates for the 'active' skin, others may want to edit a different set of templates before enabling them. Your mod will be far more useful when you allow the client to select which set of templates they want to edit.

And just to point out one other thing -- any time we set world-writeable permissions on a file, it opens us up to being hacked from another account on the server. Although this is more complicated, a far safer way to access and edit files on the server is to go through an FTP connection, which can be done with PHP. I'm not sure what server variations may cause a problem with this, so I'm not sure if it's feasible on all servers. I do know, however, that I've used other software that does it and it works great on my server.

Some applications that allow this type of editing store their templates in the database. The downside is that it slows the applications. CubeCart's caching mechanism may help with it though.

Just some thoughts to consider.

I'm glad to see someone took the time to create such a mod.

Link to comment
Share on other sites

Guest storyboo

I'm glad to see you got this working. Congratulations!

You are certainly helping to make CubeCart accessible to those with less technical skills and this is to be highly commended.

There's one thing that I would like to suggest though. Although many of your current clients will want to edit templates for the 'active' skin, others may want to edit a different set of templates before enabling them. Your mod will be far more useful when you allow the client to select which set of templates they want to edit.

And just to point out one other thing -- any time we set world-writeable permissions on a file, it opens us up to being hacked from another account on the server. Although this is more complicated, a far safer way to access and edit files on the server is to go through an FTP connection, which can be done with PHP. I'm not sure what server variations may cause a problem with this, so I'm not sure if it's feasible on all servers. I do know, however, that I've used other software that does it and it works great on my server.

Some applications that allow this type of editing store their templates in the database. The downside is that it slows the applications. CubeCart's caching mechanism may help with it though.

Just some thoughts to consider.

I'm glad to see someone took the time to create such a mod.

Thanks! I had asked one of the more popular modders to make this mod for me..but I was told it was impossible :) hehe.

I have written a code..I just have to rename some of the commands....but Im not sure how to append it the the existing code...Here is some php to use ftp... just not sure how to involve it just yet.

<?php

}



// Update theme if needed

if (file_exists("glob[rootDir]."skins".[skinDir]."/styleTemplates/global/index.tpl") AND ($set_master_key == "yes" OR $show_custom == "Yes"))

{



if ($Submit == "Customize" AND $file)

{

$file = "glob[rootDir]."skins".[skinDir]."/styleTemplates/global/index.tpl";

$templateval = str_replace("_", " ", substr($file, 0, -4));

echo "<p align=\"center\" class=\"highlighttext\">Please Note: You are editing the $templateval template. If you save these<br>

changes by clicking &quot;Update&quot; below, you will override any custom<br>template you have loaded in 

the past. This change is not reversable!</p>";

}

else

$file = "glob[rootDir]."skins".[skinDir]."/styleTemplates/global/index.tpl";

if (version_compare(phpversion(), "4.3.0", ">="))

{

$contents = file_get_contents($file);

}

else

{

if (!empty($ftp_site))

{

$file = str_replace($Home_Path, $ftp_path, $file);

$conn_id = @ftp_connect($ftp_site);

$login_result = @ftp_login($conn_id, $ftp_user, $ftp_pass);

@ftp_site($conn_id, "CHMOD " .$chmod_update ." " .$file);

}

$handle = @fopen ($file, "a+");

if (!$handle)

die ("Could not upload file");

$contents = @fread ($handle, filesize ($file));

@fclose($handle);

if (!empty($ftp_site))

{

@ftp_site($conn_id, "CHMOD " .$chmod_file ." " .$cust_pg);

@ftp_close($conn_id);

}

if (!$contents)

echo "<p align=\"center\"><b>Please ask your administrator to check the template file permissions.</b></p>";

}

?>



<a name="customize"></a>

How can I alter my code to use this ftp access php script?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...