[ Index ]

PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008]

title

Body

[close]

/admin/ -> langdoc.php (source)

   1  <?php // $Id: langdoc.php,v 1.20.4.2 2007/11/21 02:56:58 mudrd8mz Exp $
   2  
   3  /**
   4   * This script enables Moodle translators to edit /docs and /help language
   5   * files directly via WWW interface.
   6   *
   7   * Author:     mudrd8mz@uxit.pedf.cuni.cz (http://moodle.cz)
   8   * Based on:   lang.php in 1.4.3+ release
   9   * Thanks:     Jaime Villate for important bug fixing, koen roggemans for his job and all moodlers
  10   *             for intensive testing of this my first contribution
  11   */
  12      require_once ('../config.php');
  13      require_once($CFG->libdir.'/adminlib.php');
  14      admin_externalpage_setup('langedit');
  15  
  16      //
  17      // Some local configuration
  18      //
  19      $fileeditorrows = 12;           // number of textareas' rows
  20      $fileeditorcols = 100;          // dtto cols
  21      $fileeditorinline = 1;          // shall be textareas put in one row?
  22      $filemissingmark = ' (***)';    // mark to add to non-existing filenames in selection form
  23      $fileoldmark = ' (old?)';       // mark to add to filenames in selection form id english version is newer
  24                                      // or to filenames with filesize() == 0
  25      $filetemplate = '';             // template for new files, i.e. '$Id: langdoc.php,v 1.20.4.2 2007/11/21 02:56:58 mudrd8mz Exp $';
  26  
  27      $currentfile = optional_param('currentfile', 'docs/README.txt', PARAM_PATH); 
  28  
  29      $strlanguage = get_string("language");
  30      $strcurrentlanguage = get_string("currentlanguage");
  31      $strthislanguage = get_string("thislanguage");
  32      $stredithelpdocs = get_string('edithelpdocs', 'admin');
  33  
  34      admin_externalpage_print_header();
  35  
  36      notify('NOTICE: This interface is obsolete now and will be removed. You should use 
  37          improved <a href="lang.php?mode=helpfiles">lang.php</a> interface.');
  38  
  39      $currentlang = current_language();
  40      $langdir = "$CFG->dataroot/lang/$currentlang";
  41      $enlangdir = "$CFG->dirroot/lang/en_utf8";
  42  
  43  
  44      if (!file_exists($langdir)) {
  45          error ('to edit this language pack, you need to put it in '.$CFG->dataroot.'/lang');
  46      }
  47      // Shall I save POSTed data?
  48  
  49      if (isset($_POST['currentfile'])) {
  50          if (confirm_sesskey()) {
  51              if (langdoc_save_file($langdir, $currentfile, $_POST['filedata'])) {
  52                  notify(get_string("changessaved")." ($langdir/$currentfile)", "green");
  53              } else {
  54                  error("Could not save the file '$currentfile'!", "langdoc.php?currentfile=$currentfile&sesskey=$USER->sesskey");
  55              }
  56          }
  57      }
  58  
  59      error_reporting(0); // Error reporting turned off due to non-existing files
  60  
  61      // Generate selection for all help and documentation files
  62  
  63      // Get all files from /docs directory
  64  
  65      if (! $files = get_directory_list("$CFG->dirroot/lang/en_utf8/docs", "CVS")) {
  66          error("Could not find English language docs files!");
  67      }
  68  
  69      $options = array();
  70  
  71      foreach ($files as $filekey => $file) {    // check all the docs files.
  72          $options["docs/$file"] = "docs/$file";
  73          // add mark if file doesn't exist or is empty
  74          if (( !file_exists("$langdir/docs/$file")) || (filesize("$langdir/docs/$file") == 0)) {
  75              $options["docs/$file"] .= "$filemissingmark";
  76          } else {       
  77              if (filemtime("$langdir/docs/$file") < filemtime("$CFG->dirroot/lang/en_utf8/docs/$file")) {
  78                  $options["docs/$file"] .= "$fileoldmark";
  79              }
  80          }    
  81      }
  82  
  83      // Get all files from /help directory
  84  
  85      if (! $files = get_directory_list("$CFG->dirroot/lang/en_utf8/help", "CVS")) {
  86          error("Could not find English language help files!");
  87      }
  88  
  89      foreach ($files as $filekey => $file) {    // check all the help files.
  90          $options["help/$file"] = "help/$file";
  91          if (( !file_exists("$langdir/help/$file")) || (filesize("$CFG->dirroot/lang/en_utf8/help/$file") == 0)) {
  92              $options["help/$file"] .= "$filemissingmark";
  93          } else {
  94              if (filemtime("$langdir/help/$file") < filemtime("$langdir/help/$file")) {
  95                  $options["help/$file"] .= "$fileoldmark";
  96              }
  97          }    
  98      }
  99  
 100      echo "<table align=\"center\"><tr><td align=\"center\">";
 101      echo popup_form ("$CFG->wwwroot/$CFG->admin/langdoc.php?sesskey=$USER->sesskey&amp;currentfile=", $options, "choosefile", $currentfile, "", "", "", true);
 102      echo "</td></tr></table>";
 103  
 104      // Generate textareas
 105  
 106      if (!empty($currentfile)) {
 107  
 108          if (!file_exists("$langdir/$currentfile")) {
 109              //check if directory exist
 110              $pathparts = explode('/',$currentfile);
 111              $checkpath = $langdir;
 112              for ($a=0; $a < count($pathparts)-1 ; $a++) {
 113                  $checkpath .= "/".$pathparts[$a];
 114                  if(!file_exists($checkpath)){
 115                       if(!mkdir($checkpath)){
 116                           echo ("Cannot create directory: $checkpath");
 117                       }    
 118                  }    
 119              }
 120              //
 121              // file doesn't exist - let's check webserver's permission to create it
 122              //
 123              if (!touch("$langdir/$currentfile")) {
 124                  //
 125                  // webserver is unable to create new file
 126                  //
 127                  echo "<p align=\"center\"><font color=red>".get_string("filemissing", "", "
 128  $langdir/$currentfile")."</font></p>";
 129                  $editable = false;
 130              } else {
 131                  //
 132                  // webserver can create new file - we can delete it now and let
 133                  // the langdoc_save_file() create it again if its filesize() > 0
 134                  //
 135                  $editable = true;
 136                  unlink("$langdir/$currentfile");
 137              }
 138          } elseif ($f = fopen("$langdir/$currentfile","r+")) {
 139              //
 140              // file exists and is writeable - good for you, translator ;-)
 141              //
 142              $editable = true;
 143              fclose($f);
 144          } else {
 145              //
 146              // file exists but it is not writeable by web server process :-(
 147              //
 148              $editable = false;
 149              echo "<p><font size=1>".get_string("makeeditable", "", "$langdir/$currentfile")
 150  ."</font></p>";
 151          }
 152  
 153          //en_utf8 in dataroot is not editable
 154          if ($currentlang == 'en_utf8') {
 155              $editable = false;
 156          }
 157  
 158          echo "<table align=\"center\"><tr valign=\"center\"><td align=\"center\">\n";
 159          echo "<textarea rows=\"$fileeditorrows\" cols=\"$fileeditorcols\" name=\"\">\n";
 160          echo htmlspecialchars(file_get_contents("$enlangdir/$currentfile"));
 161          echo "</textarea>\n";
 162          //link_to_popup_window("/lang/en_utf8/$currentfile", "popup", get_string("preview"));
 163          $preview_url = langdoc_preview_url($currentfile);
 164          if ($preview_url) {
 165              link_to_popup_window($preview_url.'&amp;forcelang=en_utf8', 'popup', get_string('preview'));
 166          }
 167          echo "</td>\n";
 168          if ($fileeditorinline == 1) {
 169              echo "</tr>\n<tr valign=\"center\">\n";
 170          }
 171          echo "<td align=\"center\">\n";
 172  
 173          if ($editable) {
 174              echo "<form id=\"$currentfile\" action=\"langdoc.php\" method=\"post\">";
 175              echo '<input type="hidden" name="sesskey" value="'.$USER->sesskey.'" />';
 176              echo '<input type="hidden" name="currentfile" value="'.$currentfile.'" />';
 177  
 178              echo "<textarea rows=\"$fileeditorrows\" cols=\"$fileeditorcols\" name=\"filedata\">\n";
 179              if (file_exists("$langdir/$currentfile")) {
 180                  echo htmlspecialchars(file_get_contents("$langdir/$currentfile"));
 181              } else {
 182                  echo ($filetemplate);
 183              }
 184              echo "</textarea>\n";
 185              $preview_url = langdoc_preview_url($currentfile);
 186              if ($preview_url) {
 187                  link_to_popup_window($preview_url, 'popup', get_string('preview'));
 188              }
 189              echo '<div align="center"><input type="submit" value="'.get_string('savechanges').': lang/'.$currentlang.'/'.$currentfile.'" /></div>';
 190              echo '</form>';
 191          }
 192  
 193          echo "</td>\n</tr>\n</table>";
 194  
 195  
 196          error_reporting($CFG->debug);
 197      }
 198  
 199      admin_externalpage_print_footer();
 200  
 201  //////////////////////////////////////////////////////////////////////
 202  
 203  function langdoc_save_file($path, $file, $content) {
 204  
 205  // $path is a full pathname to the file
 206  // $file is the file to overwrite.
 207  // $content are data to write
 208  
 209      global $CFG, $USER;
 210  
 211      error_reporting(0);
 212      
 213      if (!$f = fopen("$path/$file","w")) {
 214          error_reporting($CFG->debug);
 215          return false;
 216      }
 217      
 218      error_reporting($CFG->debug);
 219  
 220      $content = str_replace("\r", "",$content);              // Remove linefeed characters
 221      $content = preg_replace("/\n{3,}/", "\n\n", $content);  // Collapse runs of blank lines
 222      $content = trim($content);                              // Delete leading/trailing whitespace
 223          
 224      fwrite($f, stripslashes($content));
 225  
 226      fclose($f);
 227  
 228      // Remove file if its empty
 229  
 230      if (filesize("$path/$file") == 0) {
 231          unlink("$path/$file");
 232      }
 233  
 234      return true;
 235  }
 236  
 237  /**
 238   * Return a preview URL for the file, if available.
 239   *
 240   * Documentation will be moved into moodle.org wiki and current version 1.6 does not
 241   * seem to be able to display local documentation. Thus, return empty URL for doc files.
 242   * See lib/moodlelib.php document_file() - it still relies on old pre-UTF8 lang/ location.
 243   */
 244  function langdoc_preview_url($currentfile) {
 245      if (substr($currentfile, 0, 5) == 'help/') {
 246          $currentfile = substr($currentfile, 5);
 247          $currentpathexp = explode('/', $currentfile);
 248          if (count($currentpathexp) > 1) {
 249              $url = '/help.php?module='.implode('/',array_slice($currentpathexp,0,count($currentpathexp)-1)).'&amp;file='.end($currentpathexp); 
 250          } else {
 251              $url = '/help.php?module=moodle&amp;file='.$currentfile;
 252          }
 253      } else {
 254          $url = '';
 255      }
 256      return $url;
 257  }
 258  
 259  ?>


Generated: Wed Jan 14 11:33:29 2009 Cross-referenced by PHPXref 0.7