Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

Help!! What's wrong with this code? (to easy-edit languages)


Guest

Recommended Posts

Posted

Hi,

 

I'm trying to make the define_languages section in the admin safer for editing text, so the pages can't be broken by accidentally deleting a quote mark in one of the define statements.

I couldn't find a contribution that does this (correct me if there is one!); but if I can get my idea to work I will post it as a contribution.

I am trying to make the text contents of each define statement appear in a textarea for editing. I have got this bit to work using this function (in admin/includes/functions/general.php):

 

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

function tep_safedit_extract($filecontents) { /* open function */

 

/* Need to find all occurrences (one by one) of the string "define('" */

 

$startsearch = 0;

 

// while there are still more defines to find

while (strpos($filecontents, "define('", $startsearch) != FALSE) {

 

// find position of next occurrence of define

$definepos = strpos($filecontents, "define('", $startsearch);

 

// now find the position of the comma and opening quote

$commapos = strpos($filecontents, "', '" , $definepos);

 

// extract the key - the string between the first set of commas

$key = substr($filecontents, $definepos+8, (($commapos) - ($definepos+8)));

 

// find the closing quote ');

$closepos = strpos($filecontents, "');", $commapos);

 

// then extract the editable text string - between the second opening and closing quotes

$text = substr($filecontents, $commapos+4, ($closepos - ($commapos+4)));

 

 

/* Build array of key-value pairs. */

$editarray[$key] = $text;

 

// (this makes script start looking for next define after end of previous one)

$startsearch = $closepos;

 

};

 

return $editarray;

 

}; /* end function */

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

 

 

This function is called from admin/define_languages.php, and then draws a 2-column table, the left column has the text key, e.g. HEADING_TITLE and the right column contains a textarea populated by the text value, e.g. New Address Book Entry.

To do this I replaced the line

 

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

"echo tep_draw_textarea_field('file_contents', 'soft', '80', '20', $contents, (($file_writeable) ? '' : 'readonly'));"

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

 

with the following:

 

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

$editarray = tep_safedit_extract($contents);

 

// $editarray is returned from function

 

foreach ($editarray as $key => $value) {

print $key . "</td><td class='main'>" . tep_draw_textarea_field($key, 'soft', '60', '3', $value, (($file_writeable) ? '' : 'readonly')) . "</td></tr><tr><td class='main'>";

 

$i++;

 

};

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

 

 

This bit works, but it doesn't work when you edit any of the text and click the save button, i.e. case 'save'. Simply nothing happens; your changes are not 'remembered'.

 

I replaced the following code:

 

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

if (file_exists($file)) {

 

if (file_exists('bak' . $file)) {

@unlink('bak' . $file);

}

 

@rename($file, 'bak' . $file);

 

$new_file = fopen($file, 'w');

 

$file_contents = stripslashes($HTTP_POST_VARS['file_contents']);

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

 

With this:

 

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

if (file_exists($file)) {

 

// get original file from server

 

$file_array = file($file);

$contents = implode('', $file_array);

 

$file_writeable = true;

if (!is_writeable($file)) {

$file_writeable = false;

$messageStack->reset();

$messageStack->add(sprintf(ERROR_FILE_NOT_WRITEABLE, $file), 'error');

echo $messageStack->output();

}

 

if (file_exists('bak' . $file)) {

@unlink('bak' . $file);

}

 

@rename($file, 'bak' . $file);

 

$new_file = fopen($file, 'w');

 

 

/* reconstruct an array to submit to tep_safedit_replace(); using keys from the original file (extracted using tep_safedit_extract(); again).

Then loop through $editarray and replace old values with new values (new values obtained from $_POST array).

This creates a new array called $newarray to be submitted to tep_safedit_replace().

*/

 

$editarray = tep_safedit_extract($contents);

 

foreach ($editarray as $key => $value) {

 

$newarray[$key] = $_POST[$key];

 

};

 

 

// function replaces original text strings with new

$finalcontent = tep_safedit_replace($contents,$newarray);

 

// $finalcontent is returned from function

$file_contents = stripslashes($finalcontent);

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

 

 

This calls upon another function tep_safedit_replace(); which is defined in admin/includes/functions/general.php:

 

 

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

function tep_safedit_replace($contents, $newarray) { /* open function. Variables passed to function are $contents (original file) and $editarray (the array from function tep_safedit_extract) */

 

/* Need to loop through editarray looking for each $key in turn */

foreach ($newarray as $key => $value) {

 

// Look for each key in $contents

$keypos = strpos($contents, $key);

 

// now find the position of the comma and opening quote

$commapos = strpos($contents, "', '" , $keypos);

 

// find the closing quote ');

$closepos = strpos($contents, "');", $commapos);

 

/* then need to replace editable text string - between the second opening and closing quotes - with the value of the key */

$finalcontent = $finalcontent . substr_replace($contents, $value, $commapos+4, ($closepos - ($commapos+4)));

 

}

 

return $finalcontent;

 

};

/* end function */

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

 

I really can't see what's wrong with this code, I have been staring at it for 2 days and getting nowhere, so if anyone can help please, PLEASE do!!

 

Maybe I have the syntax wrong for creating a new array using the $_POST array or something?

 

thanks from Louise

Posted
 	 $editarray = tep_safedit_extract($contents);



	 foreach ($editarray as $key => $value) {

	 

   $newarray[$key] = $_POST[$key];



	 };

I don't have time to play with this right now, but have you tried adding an echo statement here to see what is being printed? For example, adding
echo ':' . $key . '{' . $_POST[$key] . '}';

into the loop might give you useful information. Also, where do you actually write the content to a file?

 

Later,

Matt

 

P.S. How does this handle comments in the file? What happens to them?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...