Textpattern PHP Cross Reference Content Management Systems

Source: /textpattern/lib/txplib_update.php - 142 lines - 5540 bytes - Summary - Text - Print

Description: Collection of update tools.

   1  <?php
   2  
   3  /*
   4   * Textpattern Content Management System
   5   * http://textpattern.com
   6   *
   7   * Copyright (C) 2016 The Textpattern Development Team
   8   *
   9   * This file is part of Textpattern.
  10   *
  11   * Textpattern is free software; you can redistribute it and/or
  12   * modify it under the terms of the GNU General Public License
  13   * as published by the Free Software Foundation, version 2.
  14   *
  15   * Textpattern is distributed in the hope that it will be useful,
  16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18   * GNU General Public License for more details.
  19   *
  20   * You should have received a copy of the GNU General Public License
  21   * along with Textpattern. If not, see <http://www.gnu.org/licenses/>.
  22   */
  23  
  24  /**
  25   * Collection of update tools.
  26   *
  27   * @package Update
  28   */
  29  
  30  /**
  31   * Installs language strings from a file.
  32   *
  33   * This function imports language strings to the database
  34   * from a file placed in the ../lang directory.
  35   *
  36   * Running this function will delete any missing strings of any
  37   * language specific event that were included in the file. Empty
  38   * strings are also stripped from the database.
  39   *
  40   * @param   string $lang The language code
  41   * @return  bool TRUE on success
  42   * @package L10n
  43   */
  44  
  45  function install_language_from_file($lang)
  46  {
  47      global $DB;
  48      $lang_files = glob(txpath.'/lang/'.$lang.'.{txt,textpack}', GLOB_BRACE);
  49  
  50      if (!empty($lang_files)) {
  51          $lang_file = $lang_files[0];
  52  
  53          if (!is_file($lang_file) || !is_readable($lang_file)) {
  54              return false;
  55          }
  56  
  57          $file = @fopen($lang_file, "r");
  58  
  59          if ($file) {
  60              $lastmod = @filemtime($lang_file);
  61              $lastmod = date('YmdHis', $lastmod);
  62              $data = $core_events = array();
  63              $event = '';
  64  
  65              // TODO: General overhaul: Try install_textpack() to replace this parser; use safe_* db functions.
  66              while (!feof($file)) {
  67                  $line = fgets($file, 4096);
  68  
  69                  // Ignore empty lines and simple comments (any line starting
  70                  // with #, not followed by @).
  71                  if (trim($line) === '' || ($line[0] == '#' && $line[1] != '@' && $line[1] != '#')) {
  72                      continue;
  73                  }
  74  
  75                  // If available use the lastmod time from the file.
  76                  if (strpos($line, '#@version') === 0) {
  77                      // Looks like: "#@version id;unixtimestamp".
  78                      @list($fversion, $ftime) = explode(';', trim(substr($line, strpos($line, ' ', 1))));
  79                      $lastmod = date("YmdHis", min($ftime, time()));
  80                  }
  81  
  82                  // Each language section should be prefixed by #@.
  83                  if ($line[0] == '#' && $line[1] == '@') {
  84                      if (!empty($data)) {
  85                          foreach ($data as $name => $value) {
  86                              $value = addslashes($value);
  87                              $exists = mysqli_query($DB->link, "SELECT name, lastmod FROM `".PFX."txp_lang` WHERE lang = '".$lang."' AND name = '$name' AND event = '$event'");
  88  
  89                              if ($exists) {
  90                                  $exists = mysqli_fetch_row($exists);
  91                              }
  92  
  93                              if ($exists[1]) {
  94                                  mysqli_query($DB->link, "UPDATE `".PFX."txp_lang` SET lastmod = '$lastmod', data = '$value' WHERE owner = '".doSlash(TEXTPATTERN_LANG_OWNER_SYSTEM)."' AND lang = '".$lang."' AND name = '$name' AND event = '$event'");
  95                                  echo mysqli_error($DB->link);
  96                              } else {
  97                                  mysqli_query($DB->link, "INSERT DELAYED INTO `".PFX."txp_lang` SET lang = '".$lang."', name = '$name', lastmod = '$lastmod', event = '$event', data = '$value'");
  98                                  echo mysqli_error($DB->link);
  99                              }
 100                          }
 101                      }
 102  
 103                      // Reset.
 104                      $data = array();
 105                      $event = substr($line, 2, (strlen($line) - 2));
 106                      $event = rtrim($event);
 107  
 108                      if (strpos($event, 'version') === false) {
 109                          $core_events[] = $event;
 110                      }
 111  
 112                      continue;
 113                  }
 114  
 115                  // Guard against setup strings being loaded.
 116                  // TODO: Setup strings will be removed from the .txt files at some point; this check can then be removed.
 117                  if ($event !== 'setup') {
 118                      @list($name, $val) = explode(' => ', trim($line));
 119                      $data[$name] = $val;
 120                  }
 121              }
 122  
 123              // Remember to add the last one.
 124              if (!empty($data)) {
 125                  foreach ($data as $name => $value) {
 126                      mysqli_query($DB->link, "INSERT DELAYED INTO `".PFX."txp_lang` SET lang = '".$lang."', name = '$name', lastmod = '$lastmod', event = '$event', data = '$value'");
 127                  }
 128              }
 129  
 130              mysqli_query($DB->link, "DELETE FROM `".PFX."txp_lang` WHERE owner = '' AND lang = '".$lang."' AND event IN ('".join("','", array_unique($core_events))."') AND  lastmod > $lastmod");
 131              @fclose($file);
 132  
 133              // Delete empty fields if any.
 134              mysqli_query($DB->link, "DELETE FROM `".PFX."txp_lang` WHERE data = ''");
 135              mysqli_query($DB->link, "FLUSH TABLE `".PFX."txp_lang`");
 136  
 137              return true;
 138          }
 139      }
 140  
 141      return false;
 142  }

title

Description

title

Description

title

Description

title

title

Body