Textpattern PHP Cross Reference Content Management Systems

Source: /textpattern/vendors/Textpattern/DB/Core.php - 267 lines - 7832 bytes - Summary - Text - Print

Description: Core.

   1  <?php
   2  
   3  /*
   4   * Textpattern Content Management System
   5   * https://textpattern.com/
   6   *
   7   * Copyright (C) 2020 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 <https://www.gnu.org/licenses/>.
  22   */
  23  
  24  /**
  25   * Core.
  26   *
  27   * @since   4.7.0
  28   * @package DB
  29   */
  30  
  31  namespace Textpattern\DB;
  32  
  33  class Core
  34  {
  35      /**
  36       * Textpattern table structure directory.
  37       *
  38       * @var string
  39       */
  40  
  41      protected $tables_dir;
  42      protected $tables_structure = array();
  43  
  44      protected $data_dir;
  45  
  46      /**
  47       * Constructor.
  48       */
  49  
  50      public function __construct()
  51      {
  52          $this->tables_dir = dirname(__FILE__).DS.'Tables';
  53          $this->data_dir = dirname(__FILE__).DS.'Data';
  54      }
  55  
  56      /**
  57       * getStructure
  58       *
  59       * @param table name or empty
  60       */
  61  
  62      public function getStructure($table='')
  63      {
  64          if (empty($this->tables_structure)) {
  65              $this->tables_structure = get_files_content($this->tables_dir, 'table');
  66          }
  67  
  68          if (!empty($table)) {
  69              return @$this->tables_structure[$table];
  70          }
  71  
  72          return $this->tables_structure;
  73      }
  74  
  75      /**
  76       * Create All Tables
  77       */
  78  
  79      public function createAllTables()
  80      {
  81          foreach ($this->getStructure() as $key=>$data) {
  82              safe_create($key, $data);
  83          }
  84      }
  85  
  86      /**
  87       * Create Table
  88       *
  89       * @param table name
  90       */
  91  
  92      public function createTable($table)
  93      {
  94          if ($data = $this->getStructure($table)) {
  95              safe_create($table, $data);
  96          }
  97      }
  98  
  99      /**
 100       * Initial mandatory data
 101       */
 102  
 103      public function initData()
 104      {
 105          $import = new \Textpattern\Import\TxpXML();
 106  
 107          foreach (get_files_content($this->data_dir, 'xml') as $key=>$data) {
 108              $import->importXml($data);
 109          }
 110      }
 111  
 112      /**
 113       * Create core prefs
 114       */
 115  
 116      public function initPrefs()
 117      {
 118          foreach ($this->getPrefsDefault() as $name => $p) {
 119              if (empty($p['private'])) {
 120                  @create_pref($name, $p['val'], $p['event'], $p['type'], $p['html'], $p['position']);
 121              }
 122          }
 123      }
 124  
 125      /**
 126       * Get Textpattern tables name
 127       *
 128       * @return array
 129       */
 130  
 131      public function getTablesName()
 132      {
 133          return array_keys($this->getStructure());
 134      }
 135  
 136      /**
 137       * Get default core prefs
 138       *
 139       * @return array
 140       */
 141  
 142      public function getPrefsDefault()
 143      {
 144          global $permlink_mode, $siteurl, $theme_name, $pref, $language;
 145  
 146          $out = json_decode(txp_get_contents($this->data_dir.DS.'core.prefs'), true);
 147  
 148          if (empty($out)) {
 149              return array();
 150          }
 151  
 152          if (empty($language)) {
 153              $language = safe_field('lang', 'txp_lang', '1=1 GROUP BY lang ORDER BY COUNT(*) DESC');
 154  
 155              if (empty($language)) {
 156                  $language = TEXTPATTERN_DEFAULT_LANG;
 157              }
 158          }
 159  
 160          // Legacy pref name, just in case.
 161          $permlink_format = get_pref('permalink_title_format', null);
 162  
 163          if ($permlink_format === null) {
 164              $permlink_format = get_pref('permlink_format', 1);
 165          }
 166  
 167          $language = \Txp::get('\Textpattern\L10n\Locale')->validLocale($language);
 168  
 169          $path_to_public_site = (isset($txpcfg['multisite_root_path'])) ? $txpcfg['multisite_root_path'].DS.'public' : dirname(txpath);
 170  
 171          $pf = array();
 172          $pf['file_base_path']  = $path_to_public_site.DS.'files';
 173          $pf['path_to_site']    = $path_to_public_site;
 174          $pf['tempdir']         = find_temp_dir();
 175          $pf['siteurl']         = $siteurl;
 176          $pf['theme_name']      = empty($theme_name) ? 'hive' : $theme_name;
 177          $pf['blog_mail_uid']   = empty($_SESSION['email']) ? md5(rand()).'blog@example.com' : $_SESSION['email'];
 178          $pf['blog_uid']        = empty($pref['blog_uid']) ? md5(uniqid(rand(), true)) : $pref['blog_uid'];
 179          $pf['language']        = $language;
 180          $pf['language_ui']     = $language;
 181          $pf['locale']          = getlocale($language);
 182          $pf['sitename']        = gTxt('my_site');
 183          $pf['site_slogan']     = gTxt('my_slogan');
 184          $pf['gmtoffset']       = sprintf("%+d", gmmktime(0, 0, 0) - mktime(0, 0, 0));
 185          $pf['permlink_mode']   = empty($permlink_mode) ? 'messy' : $permlink_mode;
 186          $pf['permlink_format'] = $permlink_format;
 187          $pf['sql_now_posted']  = $pf['sql_now_expires'] = $pf['sql_now_created'] = time();
 188          $pf['comments_default_invite'] = (gTxt('setup_comment_invite') == 'setup_comment_invite') ? 'Comment'
 189              : gTxt('setup_comment_invite');
 190          $pf['default_section'] = empty($pref['default_section']) ? safe_field('name', 'txp_section', "name<>'default'")
 191              : $pref['default_section'];
 192  
 193          foreach ($pf as $name => $val) {
 194              if (isset($out[$name])) {
 195                  $out[$name]['val'] = $val;
 196              }
 197          }
 198  
 199          return $out;
 200      }
 201  
 202      /**
 203       * Checks prefs integrity and AutoCreate missing prefs.
 204       */
 205  
 206      public function checkPrefsIntegrity()
 207      {
 208          global $prefs, $txp_user;
 209  
 210          // Rename previous Global/Private prefs.
 211          $renamed = json_decode(txp_get_contents($this->data_dir.DS.'renamed.prefs'), true);
 212  
 213          if (!empty($renamed['global'])) {
 214              foreach ($renamed['global'] as $oldKey => $newKey) {
 215                  rename_pref($newKey, $oldKey);
 216              }
 217          }
 218  
 219          if (!empty($deleted['private'])) {
 220              foreach ($renamed['private'] as $oldKey => $newKey) {
 221                  safe_update('txp_prefs', "name = '".doSlash($newKey)."'", "name='".doSlash($oldKey)."' AND user_name != ''");
 222              }
 223          }
 224  
 225          // Delete old Global/Private prefs.
 226          $deleted = json_decode(txp_get_contents($this->data_dir.DS.'deleted.prefs'), true);
 227  
 228          if (!empty($deleted['global'])) {
 229              safe_delete('txp_prefs', "name in ('".join("','", doSlash($deleted['global']))."') AND user_name = ''");
 230          }
 231  
 232          if (!empty($deleted['private'])) {
 233              safe_delete('txp_prefs', "name in ('".join("','", doSlash($deleted['private']))."') AND user_name != ''");
 234          }
 235  
 236          $prefs_check = array_merge(
 237              get_prefs_theme(),
 238              $this->getPrefsDefault()
 239          );
 240  
 241          if ($rs = safe_rows_start('name, type, event, html, position', 'txp_prefs', "user_name = '' OR user_name = '".doSlash($txp_user)."'")) {
 242              while ($row = nextRow($rs)) {
 243                  $name = array_shift($row);
 244  
 245                  if ($def = @$prefs_check[$name]) {
 246                      $private = empty($def['private']) ? PREF_GLOBAL : PREF_PRIVATE;
 247                      unset($def['val'], $def['private']);
 248  
 249  
 250                      if ($def['event'] != 'custom' && $def != $row) {
 251                          @update_pref($name, null, $def['event'], $def['type'], $def['html'], $def['position'], $private);
 252                      }
 253  
 254                      unset($prefs_check[$name]);
 255                  }
 256              }
 257          }
 258  
 259          // Create missing prefs.
 260          foreach ($prefs_check as $name => $p) {
 261              $private = empty($p['private']) ? PREF_GLOBAL : PREF_PRIVATE;
 262              @create_pref($name, $p['val'], $p['event'], $p['type'], $p['html'], $p['position'], $private);
 263          }
 264  
 265          $prefs = get_prefs();
 266      }
 267  }

title

Description

title

Description

title

Description

title

title

Body