<?php

highlight_file
(__FILE__);
        exit;

/*
  $Id: textmaster.php,v 1.1 2008/01/13 
  textmaster Originally Created by: Jack_mcs
  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2003 osCommerce

  Released under the GNU General Public License
*/
 
  
require('includes/application_top.php');
  require(
DIR_WS_LANGUAGES $language '/' FILENAME_TEXTMASTER);
  require(
DIR_WS_FUNCTIONS FILENAME_TEXTMASTER);

  
$backupDir DIR_FS_ADMIN 'backups_textmaster/';
  
$currentEdit = isset($_POST['tinymce_status']) ? $_POST['tinymce_status'] : EDITOR_FOR_NONE;
  
$editorStatus EDITOR_FOR_ALL;
  
$enableTinyMCE false;
  
$foundDefine false;
  
$fp = array();
  
$newDir false;
  
$initialDir '/includes/languages/espanol/';       //DIR_WS_INCLUDES; -use this instead to allow include files to be edited
  
$pathDir DIR_FS_CATALOG $initialDir;
  
$restoreEnable 'checked disabled';  //control the restore button
  
$selectedDir DEF_SELECT_DIRECTORY;
  
$selectedFile DEF_SELECT_FILE;
  
$textArray = array();
  
$textString '';
 
  if (isset(
$_POST['current_directory']) && $_POST['selected_directory'] != DEF_SELECT_DIRECTORY)
  {
    
$selectedDir $_POST['selected_directory'];  
    
$pathDir unserialize(str_replace("\\"""$_POST['pathDir']));
    
$d explode('/'$pathDir);

    
/************************ PARENT SELECTED IN DIRECTORY LIST ***************************/  
    
if (isset($_POST['selected_directory']) && $_POST['selected_directory'] === "..parent")  //backup a level
    
{
      unset(
$d[count($d)-2]);
      
$selectedFile DEF_SELECT_FILE;
      
$pathDir implode("/"$d);
      
$pathDir = (substr($pathDirstrlen($pathDir) - 1)) == "/" $pathDir $pathDir "/";
      
$selectedDir $d[count($d)-2];
    }

    else
    {
      if (
$selectedDir !== $d[count($d)-2])  //same directory - probably a file change
      
{
        
$pathDir rtrim($pathDir"/"); //may or may not exist
        
$pathDir .= '/'.$_POST['selected_directory'] . '/';
      }
      else
      {
        
$selectedFile $_POST['file']; //save for redisplay
      
}  
    }
  }
  else
  {
     
$pathDir DIR_FS_CATALOG $initialDir//basic path as in configure file
     
if ($_POST['file'] !== DEF_SELECT_FILE)  //file was selected from default directory
       
$selectedFile $_POST['file'];
  }  

  
$pathFile DIR_FS_CATALOG DIR_WS_LANGUAGES $language '/';
  
$action = (isset($HTTP_POST_VARS['action']) ? $HTTP_POST_VARS['action'] : '');
        
  
/************************ RESTORE FILE FROM BACCKUP ***************************/
  
if (isset($_POST['restore_file']) && $_POST['restore_file'] == 'yes')
  {
    
$dir $pathDir;
    
$file =  $selectedFile;
    
$fullPath $dir $file ;          
    
$dest $fullPath;
    
$backupLocn $backupDir substr($fullPathstrlen(DIR_FS_CATALOG));
            
    if (
tep_not_null(($msg RestoreBackup($backupLocn$dest)))) //restore the selected file 
    
{
       
$messageStack->add($msg'error');
    }
    
    
$currentEdit $_POST['current_edit']; //re-enable the html editor for the correct box
    
$foundDefine SetupArrays($fp$fullPath$textArray$textString);  
    
$backupLocn $backupDir substr($fullPathstrlen(DIR_FS_CATALOG));
    
$restoreEnable BackupExists($backupLocn);
  }  
  
  
/************************ CANCEL PICKED ON FILE - JUST RESET ***********************/  
  
else if (tep_not_null($action) && isset($_POST['cancel_x']))
  {
    
$selectedFile DEF_SELECT_FILE;
  }
  
  
/************************ READ IN THE CHANGED FILE ***************************/  
  
else if (! $newDir && (tep_not_null($action) && isset($selectedFile) && $selectedFile != DEF_SELECT_FILE)) 
  { 
    
$dir $pathDir;
    
$file =  $selectedFile;
    
$fullPath $dir $file
    
    if (
tep_not_null($file))
    { 
      if (
tep_not_null($msg CanWriteToFile($fullPath)))
      {
        
$messageStack->add($msg);
      }
      
      else 
      {
        
$foundDefine SetupArrays($fp$fullPath$textArray$textString);
        
$backupLocn $backupDir substr($fullPathstrlen(DIR_FS_CATALOG));
        
$restoreEnable BackupExists($backupLocn);

        
/************************ SAVE THE CHANGES ***************************/
        
if (isset($_POST['update_x']))
        {
            if (isset(
$_POST['found_define']) && $_POST['found_define'] == 'on')
            {
              
$block false;         
              
$ctr 0;  
              
$new_contents = array();

              for (
$idx 0$idx count($fp); ++$idx)
              {
                 if ((
$pos1 strpos($fp[$idx], "define('")) !== FALSE  && IsVariableText($fp[$idx]))
                 {                           
//contains a define with text - defined 2nd arguments are skipped
                    
$block true//a define section is being read 
                    
$define "textmaster_define_$ctr"
                    
$text "textmaster_text_$ctr";
                    
$_POST[$text] = str_replace("'""\'"$_POST[$text]);
                    
$new_contents[] = sprintf("define('%s', '%s');\n",$_POST[$define],$_POST[$text]);
                    
$ctr++;
                 }
                 
                 if (
$block//ignore all original define lines for this block
                 
{
                   if (
strpos($fp[$idx], ");") !== FALSE//the block has completed
                    
$block false;
                 }
                 else
                 {
                   
$new_contents[] = $fp[$idx]; //add all lines not in a define block
                 
}  
              }
              
$fp = array();
              
$fp array_slice($new_contents0);
               
              if (
get_magic_quotes_gpc()) {
                function 
stripslashes_array($array) {
                    return 
is_array($array) ? array_map('stripslashes_array'$array) : stripslashes($array);
                }
            
                
$fp stripslashes_array($fp);  //remove the // if you have problems with extra slashes in the file
              
}
            }
            else
            {
              
$fp = array();
              
$fp explode("\n"$_POST['textmaster_string']);
               
              if (
get_magic_quotes_gpc()) {
                function 
stripslashes_array($array) {
                    return 
is_array($array) ? array_map('stripslashes_array'$array) : stripslashes($array);
                }
            
                
$fp stripslashes_array($fp);
              }
            }    
            
            
$src $fullPath;
            
$dest $backupDir substr($fullPathstrlen(DIR_FS_CATALOG));
            
$backupLocn substr(dirname($fullPath), strlen(DIR_FS_CATALOG));

            if (
tep_not_null(($msg CreateBackup($src$dest$backupLocn$backupDir)))) //create a backup first 
            
{
              
$messageStack->add($msg'error');
            }
            else                                                      
//backup was created so change file
            
{
              if (
tep_not_null(($msg SaveText($fullPath$fp))))
                
$messageStack->add($msg'error');
              else
                
$messageStack->add(SAVE_SUCCESS'success');  
            }
 
            
$currentEdit $_POST['current_edit']; //re-enable the html editor for the correct box
            
$foundDefine SetupArrays($fp$fullPath$textArray$textString);  
            
$backupLocn $backupDir substr($fullPathstrlen(DIR_FS_CATALOG));
            
$restoreEnable BackupExists($backupLocn);
        } 
      }    
    }
  } 

  
/***************  CREATE DIRECTORY and FILE LIST *****************/ 
  
$dirs = array();
  
$files = array();
  
clearstatcache();  //clear cache for new read
  
if ($handle opendir($pathDir))
  {
     
$dirs[] = array('id' => $selectedDir'text' => $selectedDir);
     
     if (
substr($pathDir, -strlen($initialDir)) !== $initialDir)  //don't allow backing up before the initial directory
     
{
       
$dirs[] = array('id' => '..parent''text' => '..parent');
     }
     
     
$files[] = array('id' => DEF_SELECT_FILE'text' => DEF_SELECT_FILE);

     while (
false !== ($file readdir($handle)))
     {
       if(! 
is_dir($pathDir.$file))
       {
          if( 
substr($file, -44) === ".php"//only look at php files
            
$files[] = array('id' => $file'text' => $file);
       }  
       else if (
$file != "." && $file != "..")
       { 
          if (
substr($file, -strlen("images")) !== "images"//don't list images directories
            
$dirs[] = array('id' => $file'text' => $file);
       } 
       
       
$files record_sort($files"text");
    }
         
closedir($handle);
  }   
?>
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET?>">
<title><?php echo TITLE?></title>
<link rel="stylesheet" type="text/css" href="includes/stylesheet.css">

<?php // START tinyMCE Anywhere
if (file_exists("includes/javascript/tiny_mce/general.php"))
{
  
$languages tep_get_languages(); // Get all languages
  // Build list of textareas to convert 
  
$allon = (isset($_POST['tinymce_status_all']) && $_POST['tinymce_status_all'] == EDITOR_FOR_ALL) ? true false;

  if (isset(
$_POST['found_define']) && $_POST['found_define'] == 'off')
  {
    if (
$allon)
    {
      
$mce_str "textmaster_string";
      
$editorStatus EDITOR_FOR_ALL 1;
    } 
    else 
    {
      
$mce_str '';
      
$editorStatus EDITOR_FOR_ALL;
    } 
  }
  else
  { 
    for (
$i 0$i count($textArray); ++$i
    { 
      if (
$allon || (isset($_POST['tinymce_status']) && $_POST['tinymce_status'] == $i) || ($currentEdit != DEF_SELECT_DIRECTORY && $currentEdit == $i))
      {
        
$str.="textmaster_text_$i,";
      } 
    }  
    
$mce_str rtrim ($str,","); // Removed the last comma from the string.
  
}
  echo 
'<script language="javascript" type="text/javascript" src="includes/javascript/tiny_mce/tiny_mce.js"></script>';
  include 
"includes/javascript/tiny_mce/general.php";
}
else
 
$messageStack->add(TINYMCE_NOT_FOUND'error');  
 
// END tinyMCE Anywhere 

?>
</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#FFFFFF">
<!-- header //-->
<?php require(DIR_WS_INCLUDES 'header.php'); ?>
<!-- header_eof //-->

<!-- body //-->
<table border="0" width="100%" cellspacing="2" cellpadding="2">
  <tr>
    <td width="<?php echo BOX_WIDTH?>" valign="top"><table border="0" width="<?php echo BOX_WIDTH?>" cellspacing="1" cellpadding="1" class="columnLeft">
<!-- left_navigation //-->
<?php require(DIR_WS_INCLUDES 'column_left.php'); ?>
<!-- left_navigation_eof //-->
    </table></td>
<!-- body_text //-->
    <td width="100%" valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="2">
     <tr>
      <td class="pageHeading"><?php echo HEADING_TITLE?></td>
     </tr>     
     <tr>
      <td><?php echo tep_draw_separator('pixel_trans.gif''100%''10'); ?></td>
     </tr>
     
     <!-- Begin of TextMaster -->
     <tr>
      <td colspan="4"><?php echo tep_draw_separator('pixel_silver.gif''100%''2'); ?></td>
     </tr>
     <tr>
      <td align="right"><?php echo tep_draw_form('textmaster'FILENAME_TEXTMASTER'''post') . tep_draw_hidden_field('action''process') . tep_draw_hidden_field('current_directory'$pathDir) . tep_draw_hidden_field('found_define', (($foundDefine) ? 'on' 'off')); ?></td>
       <tr>
        <td><table border="0" width="100%">
         <tr>
          <td class="main" width="120"><?php echo TEXT_SELECT_DIRECTORY?></td>
          <td class="main"><?php echo tep_draw_hidden_field('pathDir'serialize($pathDir)) . tep_draw_pull_down_menu('selected_directory'$dirs$selectedDir'onChange="this.form.submit();"'); ?></td>
          <td class="main"><?php echo TEXT_SELECT_FILE?></td>
          <td class="main"><?php echo tep_draw_pull_down_menu('file'$files$selectedFile'onChange="this.form.submit();"'); ?></td>
          <td><table border="0">
           <tr>
            <td class="main"><?php echo TEXT_ENABLE_HTML_EDITOR?></td>
            <td class="main" align="right"><input type="checkbox" name="tinymce_status_all" value="<?php echo $editorStatus?>" 'onChange="this.form.submit();"' ></td>
            <td class="main"><?php echo TEXT_RESTORE_FILE?></td>
            <td class="main" align="right"><input type="radio" name="restore_file" value="yes" <?php echo $restoreEnable?> onClick="this.form.submit();" ></td>
           </tr>
          </table></td> 
         </tr>
        </table></td>  
       </tr>       
       <tr>
        <td colspan="4"><?php echo tep_draw_separator('pixel_silver.gif''100%''2'); ?></td>
       </tr>  
       <tr>
        <td><?php echo tep_draw_separator('pixel_trans.gif''100%''10'); ?></td>
       </tr>  
       

       <tr>
        <td><table border="0" width="50%">        
        <?php if (tep_not_null($textString)) { ?>
         <tr>
          <td><table border="0" width="100%">
           <tr>
             <td class="main" align="left"><?php echo TEXT_ENABLE_HTML_EDITOR tep_draw_hidden_field('tinymce_string_status'$mce_str); ?><input type="checkbox" name="tinymce_status_string" value="" <?php echo $checked?>   onChange="this.form.submit();"  >&nbsp;&nbsp;&nbsp;  </td>
           </tr>
          </table></td> 
         </tr>        
         <tr>
          <td class="main"><?php echo tep_draw_textarea_field('textmaster_string''soft'1205$textString''false) . tep_draw_hidden_field('textmaster_define_' $i$textArray[$i]['define']); ?></td>
         </tr>
        <?php } else { ?>
        
         <?php  
         
for ($i 0$i count($textArray); ++$i) {
         
?>
         <tr>
          <td><table border="0" width="100%">
           <tr>
             <td class="main"><?php echo $textArray[$i]['define'] . ' (starts at line '$textArray[$i]['line'] . ')'?></td>
             <td class="main" align="right"><?php echo TEXT_ENABLE_HTML_EDITOR?><input type="checkbox" name="tinymce_status" value="<?php echo $i?>" onClick="this.form.submit();" >&nbsp;&nbsp;&nbsp;  </td>
           </tr>
          </table></td> 
         </tr>
         <tr> 
          <td class="main"><?php echo tep_draw_textarea_field('textmaster_text_'$i'soft'1205$textArray[$i]['text'], ''false) . tep_draw_hidden_field('current_edit'$currentEdit) . tep_draw_hidden_field('textmaster_define_' $i$textArray[$i]['define']); ?></td>
         </tr>   
         <tr>
          <td><?php echo tep_draw_separator('pixel_trans.gif''100%''10'); ?></td>
         </tr>            
         <?php ?>         
        </table></td>  
       </tr>            
       <?php ?>
     
    
       <?php if (tep_not_null($textString) || count($textArray)) { ?>
           <tr>
        <td align="center"><table border="0" width="30%">
         <tr>
              <td class="main"><?php echo tep_image_submit('button_update.gif'IMAGE_UPDATE'name="update"'); ?></td>
              <td class="main"><?php echo tep_image_submit('button_cancel.gif'IMAGE_CANCEL'name="cancel"'); ?></td>
         </tr>
        </table></td>  
           </tr>       
       <?php ?>
      </form>
      </td>
     </tr>
     <!-- end of Textmaster -->  
     
    </table></td>
<!-- body_text_eof //-->
  </tr>
</table>
<!-- body_eof //-->

<!-- footer //-->
<?php require(DIR_WS_INCLUDES 'footer.php'); ?>
<!-- footer_eof //-->
<br>
</body>
</html>
<?php require(DIR_WS_INCLUDES 'application_bottom.php'); ?>