[ Index ]

PHP Cross Reference of MyBB 1.8.27

title

Body

[close]

/inc/ -> class_datacache.php (source)

   1  <?php
   2  /**
   3   * MyBB 1.8
   4   * Copyright 2014 MyBB Group, All Rights Reserved
   5   *
   6   * Website: http://www.mybb.com
   7   * License: http://www.mybb.com/about/license
   8   *
   9   */
  10  
  11  class datacache
  12  {
  13      /**
  14       * Cache contents.
  15       *
  16       * @var array
  17       */
  18      public $cache = array();
  19  
  20      /**
  21       * The current cache handler we're using
  22       *
  23       * @var CacheHandlerInterface
  24       */
  25      public $handler = null;
  26  
  27      /**
  28       * A count of the number of calls.
  29       *
  30       * @var int
  31       */
  32      public $call_count = 0;
  33  
  34      /**
  35       * A list of the performed calls.
  36       *
  37       * @var array
  38       */
  39      public $calllist = array();
  40  
  41      /**
  42       * The time spent on cache operations
  43       *
  44       * @var float
  45       */
  46      public $call_time = 0;
  47  
  48      /**
  49       * Explanation of a cache call.
  50       *
  51       * @var string
  52       */
  53      public $cache_debug;
  54  
  55      /**
  56       * Build cache data.
  57       *
  58       */
  59  	function cache()
  60      {
  61          global $db, $mybb;
  62  
  63          require_once  MYBB_ROOT."/inc/cachehandlers/interface.php";
  64  
  65          switch($mybb->config['cache_store'])
  66          {
  67              // Disk cache
  68              case "files":
  69                  require_once  MYBB_ROOT."/inc/cachehandlers/disk.php";
  70                  $this->handler = new diskCacheHandler();
  71                  break;
  72              // Memcache cache
  73              case "memcache":
  74                  require_once  MYBB_ROOT."/inc/cachehandlers/memcache.php";
  75                  $this->handler = new memcacheCacheHandler();
  76                  break;
  77              // Memcached cache
  78              case "memcached":
  79                  require_once  MYBB_ROOT."/inc/cachehandlers/memcached.php";
  80                  $this->handler = new memcachedCacheHandler();
  81                  break;
  82              // eAccelerator cache
  83              case "eaccelerator":
  84                  require_once  MYBB_ROOT."/inc/cachehandlers/eaccelerator.php";
  85                  $this->handler = new eacceleratorCacheHandler();
  86                  break;
  87              // Xcache cache
  88              case "xcache":
  89                  require_once  MYBB_ROOT."/inc/cachehandlers/xcache.php";
  90                  $this->handler = new xcacheCacheHandler();
  91                  break;
  92              // APC cache
  93              case "apc":
  94                  require_once  MYBB_ROOT."/inc/cachehandlers/apc.php";
  95                  $this->handler = new apcCacheHandler();
  96                  break;
  97              // APCu cache
  98              case "apcu":
  99                  require_once  MYBB_ROOT."/inc/cachehandlers/apcu.php";
 100                  $this->handler = new apcuCacheHandler();
 101                  break;
 102              // Redis cache
 103              case "redis":
 104                  require_once  MYBB_ROOT."/inc/cachehandlers/redis.php";
 105                  $this->handler = new redisCacheHandler();
 106                  break;
 107          }
 108  
 109          if($this->handler instanceof CacheHandlerInterface)
 110          {
 111              if(!$this->handler->connect())
 112              {
 113                  $this->handler = null;
 114              }
 115          }
 116          else
 117          {
 118              // Database cache
 119              $query = $db->simple_select("datacache", "title,cache");
 120              while($data = $db->fetch_array($query))
 121              {
 122                  $this->cache[$data['title']] = unserialize($data['cache']);
 123              }
 124          }
 125      }
 126  
 127      /**
 128       * Read cache from files or db.
 129       *
 130       * @param string $name The cache component to read.
 131       * @param boolean $hard If true, cannot be overwritten during script execution.
 132       * @return mixed
 133       */
 134  	function read($name, $hard=false)
 135      {
 136          global $db, $mybb;
 137  
 138          // Already have this cache and we're not doing a hard refresh? Return cached copy
 139          if(isset($this->cache[$name]) && $hard == false)
 140          {
 141              return $this->cache[$name];
 142          }
 143          // If we're not hard refreshing, and this cache doesn't exist, return false
 144          // It would have been loaded pre-global if it did exist anyway...
 145          else if($hard == false && !($this->handler instanceof CacheHandlerInterface))
 146          {
 147              return false;
 148          }
 149  
 150          if($this->handler instanceof CacheHandlerInterface)
 151          {
 152              get_execution_time();
 153  
 154              $data = $this->handler->fetch($name);
 155  
 156              $call_time = get_execution_time();
 157              $this->call_time += $call_time;
 158              $this->call_count++;
 159  
 160              if($mybb->debug_mode)
 161              {
 162                  $hit = true;
 163                  if($data === false)
 164                  {
 165                      $hit = false;
 166                  }
 167                  $this->debug_call('read:'.$name, $call_time, $hit);
 168              }
 169  
 170              // No data returned - cache gone bad?
 171              if($data === false)
 172              {
 173                  // Fetch from database
 174                  $query = $db->simple_select("datacache", "title,cache", "title='".$db->escape_string($name)."'");
 175                  $cache_data = $db->fetch_array($query);
 176                  $data = my_unserialize($cache_data['cache']);
 177  
 178                  // Update cache for handler
 179                  get_execution_time();
 180  
 181                  $hit = $this->handler->put($name, $data);
 182  
 183                  $call_time = get_execution_time();
 184                  $this->call_time += $call_time;
 185                  $this->call_count++;
 186  
 187                  if($mybb->debug_mode)
 188                  {
 189                      $this->debug_call('set:'.$name, $call_time, $hit);
 190                  }
 191              }
 192          }
 193          // Else, using internal database cache
 194          else
 195          {
 196              $query = $db->simple_select("datacache", "title,cache", "title='$name'");
 197              $cache_data = $db->fetch_array($query);
 198  
 199              if(!$cache_data['title'])
 200              {
 201                  $data = false;
 202              }
 203              else
 204              {
 205                  $data = unserialize($cache_data['cache']);
 206              }
 207          }
 208  
 209          // Cache locally
 210          $this->cache[$name] = $data;
 211  
 212          if($data !== false)
 213          {
 214              return $data;
 215          }
 216          else
 217          {
 218              return false;
 219          }
 220      }
 221  
 222      /**
 223       * Update cache contents.
 224       *
 225       * @param string $name The cache content identifier.
 226       * @param mixed $contents The cache content.
 227       */
 228  	function update($name, $contents)
 229      {
 230          global $db, $mybb;
 231  
 232          $this->cache[$name] = $contents;
 233  
 234          // We ALWAYS keep a running copy in the db just incase we need it
 235          $dbcontents = $db->escape_string(my_serialize($contents));
 236  
 237          $replace_array = array(
 238              "title" => $db->escape_string($name),
 239              "cache" => $dbcontents
 240          );
 241          $db->replace_query("datacache", $replace_array, "", false);
 242  
 243          // Do we have a cache handler we're using?
 244          if($this->handler instanceof CacheHandlerInterface)
 245          {
 246              get_execution_time();
 247  
 248              $hit = $this->handler->put($name, $contents);
 249  
 250              $call_time = get_execution_time();
 251              $this->call_time += $call_time;
 252              $this->call_count++;
 253  
 254              if($mybb->debug_mode)
 255              {
 256                  $this->debug_call('update:'.$name, $call_time, $hit);
 257              }
 258          }
 259      }
 260  
 261      /**
 262       * Delete cache contents.
 263       * Originally from frostschutz's PluginLibrary
 264       * github.com/frostschutz
 265       *
 266       * @param string $name Cache name or title
 267       * @param boolean $greedy To delete a cache starting with name_
 268       */
 269  	 function delete($name, $greedy = false)
 270       {
 271          global $db, $mybb, $cache;
 272  
 273          // Prepare for database query.
 274          $dbname = $db->escape_string($name);
 275          $where = "title = '{$dbname}'";
 276  
 277          // Delete on-demand or handler cache
 278          if($this->handler instanceof CacheHandlerInterface)
 279          {
 280              get_execution_time();
 281  
 282              $hit = $this->handler->delete($name);
 283  
 284              $call_time = get_execution_time();
 285              $this->call_time += $call_time;
 286              $this->call_count++;
 287  
 288              if($mybb->debug_mode)
 289              {
 290                  $this->debug_call('delete:'.$name, $call_time, $hit);
 291              }
 292          }
 293  
 294          // Greedy?
 295          if($greedy)
 296          {
 297              $name .= '_';
 298              $names = array();
 299              $keys = array_keys($cache->cache);
 300  
 301              foreach($keys as $key)
 302              {
 303                  if(strpos($key, $name) === 0)
 304                  {
 305                      $names[$key] = 0;
 306                  }
 307              }
 308  
 309              $ldbname = strtr($dbname,
 310                  array(
 311                      '%' => '=%',
 312                      '=' => '==',
 313                      '_' => '=_'
 314                  )
 315              );
 316  
 317              $where .= " OR title LIKE '{$ldbname}=_%' ESCAPE '='";
 318  
 319              if($this->handler instanceof CacheHandlerInterface)
 320              {
 321                  $query = $db->simple_select("datacache", "title", $where);
 322  
 323                  while($row = $db->fetch_array($query))
 324                  {
 325                      $names[$row['title']] = 0;
 326                  }
 327  
 328                  // ...from the filesystem...
 329                  $start = strlen(MYBB_ROOT."cache/");
 330                  foreach((array)@glob(MYBB_ROOT."cache/{$name}*.php") as $filename)
 331                  {
 332                      if($filename)
 333                      {
 334                          $filename = substr($filename, $start, strlen($filename)-4-$start);
 335                          $names[$filename] = 0;
 336                      }
 337                  }
 338  
 339                  foreach($names as $key => $val)
 340                  {
 341                      get_execution_time();
 342  
 343                      $hit = $this->handler->delete($key);
 344  
 345                      $call_time = get_execution_time();
 346                      $this->call_time += $call_time;
 347                      $this->call_count++;
 348  
 349                      if($mybb->debug_mode)
 350                      {
 351                          $this->debug_call('delete:'.$name, $call_time, $hit);
 352                      }
 353                  }
 354              }
 355          }
 356  
 357          // Delete database cache
 358          $db->delete_query("datacache", $where);
 359      }
 360  
 361      /**
 362       * Debug a cache call to a non-database cache handler
 363       *
 364       * @param string $string The cache key
 365       * @param string $qtime The time it took to perform the call.
 366       * @param boolean $hit Hit or miss status
 367       */
 368  	function debug_call($string, $qtime, $hit)
 369      {
 370          global $mybb, $plugins;
 371  
 372          $debug_extra = '';
 373          if($plugins->current_hook)
 374          {
 375              $debug_extra = "<div style=\"float_right\">(Plugin Hook: {$plugins->current_hook})</div>";
 376          }
 377  
 378          if($hit)
 379          {
 380              $hit_status = 'HIT';
 381          }
 382          else
 383          {
 384              $hit_status = 'MISS';
 385          }
 386  
 387          $cache_data = explode(':', $string);
 388          $cache_method = $cache_data[0];
 389          $cache_key = $cache_data[1];
 390  
 391          $this->cache_debug = "<table style=\"background-color: #666;\" width=\"95%\" cellpadding=\"4\" cellspacing=\"1\" align=\"center\">
 392  <tr>
 393      <td style=\"background-color: #ccc;\">{$debug_extra}<div><strong>#{$this->call_count} - ".ucfirst($cache_method)." Call</strong></div></td>
 394  </tr>
 395  <tr style=\"background-color: #fefefe;\">
 396      <td><span style=\"font-family: Courier; font-size: 14px;\">({$mybb->config['cache_store']}) [{$hit_status}] ".htmlspecialchars_uni($cache_key)."</span></td>
 397  </tr>
 398  <tr>
 399      <td bgcolor=\"#ffffff\">Call Time: ".format_time_duration($qtime)."</td>
 400  </tr>
 401  </table>
 402  <br />\n";
 403  
 404          $this->calllist[$this->call_count]['key'] = $string;
 405          $this->calllist[$this->call_count]['time'] = $qtime;
 406      }
 407  
 408      /**
 409       * Select the size of the cache
 410       *
 411       * @param string $name The name of the cache
 412       * @return integer the size of the cache
 413       */
 414  	function size_of($name='')
 415      {
 416          global $db;
 417  
 418          if($this->handler instanceof CacheHandlerInterface)
 419          {
 420              $size = $this->handler->size_of($name);
 421              if(!$size)
 422              {
 423                  if($name)
 424                  {
 425                      $query = $db->simple_select("datacache", "cache", "title='{$name}'");
 426                      return strlen($db->fetch_field($query, "cache"));
 427                  }
 428                  else
 429                  {
 430                      return $db->fetch_size("datacache");
 431                  }
 432              }
 433              else
 434              {
 435                  return $size;
 436              }
 437          }
 438          // Using MySQL as cache
 439          else
 440          {
 441              if($name)
 442              {
 443                  $query = $db->simple_select("datacache", "cache", "title='{$name}'");
 444                  return strlen($db->fetch_field($query, "cache"));
 445              }
 446              else
 447              {
 448                  return $db->fetch_size("datacache");
 449              }
 450          }
 451      }
 452  
 453      /**
 454       * Update the MyBB version in the cache.
 455       *
 456       */
 457  	function update_version()
 458      {
 459          global $mybb;
 460  
 461          $version = array(
 462              "version" => $mybb->version,
 463              "version_code" => $mybb->version_code
 464          );
 465  
 466          $this->update("version", $version);
 467      }
 468  
 469      /**
 470       * Update the attachment type cache.
 471       *
 472       */
 473  	function update_attachtypes()
 474      {
 475          global $db;
 476  
 477          $types = array();
 478  
 479          $query = $db->simple_select('attachtypes', '*', 'enabled=1');
 480          while($type = $db->fetch_array($query))
 481          {
 482              $type['extension'] = my_strtolower($type['extension']);
 483              $types[$type['extension']] = $type;
 484          }
 485  
 486          $this->update("attachtypes", $types);
 487      }
 488  
 489      /**
 490       * Update the smilies cache.
 491       *
 492       */
 493  	function update_smilies()
 494      {
 495          global $db;
 496  
 497          $smilies = array();
 498  
 499          $query = $db->simple_select("smilies", "*", "", array('order_by' => 'disporder', 'order_dir' => 'ASC'));
 500          while($smilie = $db->fetch_array($query))
 501          {
 502              $smilies[$smilie['sid']] = $smilie;
 503          }
 504  
 505          $this->update("smilies", $smilies);
 506      }
 507  
 508      /**
 509       * Update the posticon cache.
 510       *
 511       */
 512  	function update_posticons()
 513      {
 514          global $db;
 515  
 516          $icons = array();
 517  
 518          $query = $db->simple_select("icons", "iid, name, path");
 519          while($icon = $db->fetch_array($query))
 520          {
 521              $icons[$icon['iid']] = $icon;
 522          }
 523  
 524          $this->update("posticons", $icons);
 525      }
 526  
 527      /**
 528       * Update the badwords cache.
 529       *
 530       */
 531  	function update_badwords()
 532      {
 533          global $db;
 534  
 535          $badwords = array();
 536  
 537          $query = $db->simple_select("badwords", "*");
 538          while($badword = $db->fetch_array($query))
 539          {
 540              $badwords[$badword['bid']] = $badword;
 541          }
 542  
 543          $this->update("badwords", $badwords);
 544      }
 545  
 546      /**
 547       * Update the usergroups cache.
 548       *
 549       */
 550  	function update_usergroups()
 551      {
 552          global $db;
 553  
 554          $query = $db->simple_select("usergroups");
 555  
 556          $gs = array();
 557          while($g = $db->fetch_array($query))
 558          {
 559              $gs[$g['gid']] = $g;
 560          }
 561  
 562          $this->update("usergroups", $gs);
 563      }
 564  
 565      /**
 566       * Update the forum permissions cache.
 567       *
 568       * @return bool When failed, returns false.
 569       */
 570  	function update_forumpermissions()
 571      {
 572          global $forum_cache, $db;
 573  
 574          $this->forum_permissions = $this->built_forum_permissions = array(0);
 575  
 576          // Get our forum list
 577          cache_forums(true);
 578          if(!is_array($forum_cache))
 579          {
 580              return false;
 581          }
 582  
 583          reset($forum_cache);
 584          $fcache = array();
 585  
 586          // Resort in to the structure we require
 587          foreach($forum_cache as $fid => $forum)
 588          {
 589              $this->forum_permissions_forum_cache[$forum['pid']][$forum['disporder']][$forum['fid']] = $forum;
 590          }
 591  
 592          // Sort children
 593          foreach($fcache as $pid => $value)
 594          {
 595              ksort($fcache[$pid]);
 596          }
 597          ksort($fcache);
 598  
 599          // Fetch forum permissions from the database
 600          $query = $db->simple_select("forumpermissions");
 601          while($forum_permission = $db->fetch_array($query))
 602          {
 603              $this->forum_permissions[$forum_permission['fid']][$forum_permission['gid']] = $forum_permission;
 604          }
 605  
 606          $this->build_forum_permissions();
 607          $this->update("forumpermissions", $this->built_forum_permissions);
 608  
 609          return true;
 610      }
 611  
 612      /**
 613       * Build the forum permissions array
 614       *
 615       * @access private
 616       * @param array $permissions An optional permissions array.
 617       * @param int $pid An optional permission id.
 618       */
 619  	private function build_forum_permissions($permissions=array(), $pid=0)
 620      {
 621          $usergroups = array_keys($this->read("usergroups", true));
 622          if($this->forum_permissions_forum_cache[$pid])
 623          {
 624              foreach($this->forum_permissions_forum_cache[$pid] as $main)
 625              {
 626                  foreach($main as $forum)
 627                  {
 628                      $perms = $permissions;
 629                      foreach($usergroups as $gid)
 630                      {
 631                          if(isset($this->forum_permissions[$forum['fid']][$gid]) && $this->forum_permissions[$forum['fid']][$gid])
 632                          {
 633                              $perms[$gid] = $this->forum_permissions[$forum['fid']][$gid];
 634                          }
 635                          if($perms[$gid])
 636                          {
 637                              $perms[$gid]['fid'] = $forum['fid'];
 638                              $this->built_forum_permissions[$forum['fid']][$gid] = $perms[$gid];
 639                          }
 640                      }
 641                      $this->build_forum_permissions($perms, $forum['fid']);
 642                  }
 643              }
 644          }
 645      }
 646  
 647      /**
 648       * Update the stats cache (kept for the sake of being able to rebuild this cache via the cache interface)
 649       *
 650       */
 651  	function update_stats()
 652      {
 653          require_once  MYBB_ROOT."inc/functions_rebuild.php";
 654          rebuild_stats();
 655      }
 656  
 657      /**
 658       * Update the statistics cache
 659       *
 660       */
 661  	function update_statistics()
 662      {
 663          global $db;
 664  
 665          $query = $db->simple_select('users', 'uid, username, referrals', 'referrals>0', array('order_by' => 'referrals', 'order_dir' => 'DESC', 'limit' => 1));
 666          $topreferrer = $db->fetch_array($query);
 667  
 668          $timesearch = TIME_NOW - 86400;
 669          switch($db->type)
 670          {
 671              case 'pgsql':
 672                  $group_by = $db->build_fields_string('users', 'u.');
 673                  break;
 674              default:
 675                  $group_by = 'p.uid';
 676                  break;
 677          }
 678  
 679          $query = $db->query("
 680              SELECT u.uid, u.username, COUNT(*) AS poststoday
 681              FROM {$db->table_prefix}posts p
 682              LEFT JOIN {$db->table_prefix}users u ON (p.uid=u.uid)
 683              WHERE p.dateline > {$timesearch} AND p.visible=1
 684              GROUP BY {$group_by}
 685              ORDER BY poststoday DESC
 686          ");
 687  
 688          $most_posts = 0;
 689          $topposter = array();
 690          while($user = $db->fetch_array($query))
 691          {
 692              if($user['poststoday'] > $most_posts)
 693              {
 694                  $most_posts = $user['poststoday'];
 695                  $topposter = $user;
 696              }
 697          }
 698  
 699          $query = $db->simple_select('users', 'COUNT(uid) AS posters', 'postnum>0');
 700          $posters = $db->fetch_field($query, 'posters');
 701  
 702          $statistics = array(
 703              'time' => TIME_NOW,
 704              'top_referrer' => (array)$topreferrer,
 705              'top_poster' => (array)$topposter,
 706              'posters' => $posters
 707          );
 708  
 709          $this->update('statistics', $statistics);
 710      }
 711  
 712      /**
 713       * Update the moderators cache.
 714       *
 715       * @return bool Returns false on failure
 716       */
 717  	function update_moderators()
 718      {
 719          global $forum_cache, $db;
 720  
 721          $this->built_moderators = array(0);
 722  
 723          // Get our forum list
 724          cache_forums(true);
 725          if(!is_array($forum_cache))
 726          {
 727              return false;
 728          }
 729  
 730          reset($forum_cache);
 731          $fcache = array();
 732  
 733          // Resort in to the structure we require
 734          foreach($forum_cache as $fid => $forum)
 735          {
 736              $this->moderators_forum_cache[$forum['pid']][$forum['disporder']][$forum['fid']] = $forum;
 737          }
 738  
 739          // Sort children
 740          foreach($fcache as $pid => $value)
 741          {
 742              ksort($fcache[$pid]);
 743          }
 744          ksort($fcache);
 745  
 746          $this->moderators = array();
 747  
 748          // Fetch moderators from the database
 749          $query = $db->query("
 750              SELECT m.*, u.username, u.usergroup, u.displaygroup
 751              FROM ".TABLE_PREFIX."moderators m
 752              LEFT JOIN ".TABLE_PREFIX."users u ON (m.id=u.uid)
 753              WHERE m.isgroup = '0'
 754              ORDER BY u.username
 755          ");
 756          while($moderator = $db->fetch_array($query))
 757          {
 758              $this->moderators[$moderator['fid']]['users'][$moderator['id']] = $moderator;
 759          }
 760  
 761          if(!function_exists("sort_moderators_by_usernames"))
 762          {
 763  			function sort_moderators_by_usernames($a, $b)
 764              {
 765                  return strcasecmp($a['username'], $b['username']);
 766              }
 767          }
 768  
 769          //Fetch moderating usergroups from the database
 770          $query = $db->query("
 771              SELECT m.*, u.title
 772              FROM ".TABLE_PREFIX."moderators m
 773              LEFT JOIN ".TABLE_PREFIX."usergroups u ON (m.id=u.gid)
 774              WHERE m.isgroup = '1'
 775              ORDER BY u.title
 776          ");
 777          while($moderator = $db->fetch_array($query))
 778          {
 779              $this->moderators[$moderator['fid']]['usergroups'][$moderator['id']] = $moderator;
 780          }
 781  
 782          if(is_array($this->moderators))
 783          {
 784              foreach(array_keys($this->moderators) as $fid)
 785              {
 786                  uasort($this->moderators[$fid], 'sort_moderators_by_usernames');
 787              }
 788          }
 789  
 790          $this->build_moderators();
 791  
 792          $this->update("moderators", $this->built_moderators);
 793  
 794          return true;
 795      }
 796  
 797      /**
 798       * Update the users awaiting activation cache.
 799       *
 800       */
 801  	function update_awaitingactivation()
 802      {
 803          global $db;
 804  
 805          $query = $db->simple_select('users', 'COUNT(uid) AS awaitingusers', 'usergroup=\'5\'');
 806          $awaitingusers = (int)$db->fetch_field($query, 'awaitingusers');
 807  
 808          $data = array(
 809              'users'    => $awaitingusers,
 810              'time'    => TIME_NOW
 811          );
 812  
 813          $this->update('awaitingactivation', $data);
 814      }
 815  
 816      /**
 817       * Build the moderators array
 818       *
 819       * @access private
 820       * @param array $moderators An optional moderators array (moderators of the parent forum for example).
 821       * @param int $pid An optional parent ID.
 822       */
 823  	private function build_moderators($moderators=array(), $pid=0)
 824      {
 825          if(isset($this->moderators_forum_cache[$pid]))
 826          {
 827              foreach($this->moderators_forum_cache[$pid] as $main)
 828              {
 829                  foreach($main as $forum)
 830                  {
 831                      $forum_mods = array();
 832                      if(count($moderators))
 833                      {
 834                          $forum_mods = $moderators;
 835                      }
 836                      // Append - local settings override that of a parent - array_merge works here
 837                      if(isset($this->moderators[$forum['fid']]))
 838                      {
 839                          if(is_array($forum_mods) && count($forum_mods))
 840                          {
 841                              $forum_mods = array_merge($forum_mods, $this->moderators[$forum['fid']]);
 842                          }
 843                          else
 844                          {
 845                              $forum_mods = $this->moderators[$forum['fid']];
 846                          }
 847                      }
 848                      $this->built_moderators[$forum['fid']] = $forum_mods;
 849                      $this->build_moderators($forum_mods, $forum['fid']);
 850                  }
 851              }
 852          }
 853      }
 854  
 855      /**
 856       * Update the forums cache.
 857       *
 858       */
 859  	function update_forums()
 860      {
 861          global $db;
 862  
 863          $forums = array();
 864  
 865          // Things we don't want to cache
 866          $exclude = array("unapprovedthreads", "unapprovedposts", "threads", "posts", "lastpost", "lastposter", "lastposttid", "lastposteruid", "lastpostsubject", "deletedthreads", "deletedposts");
 867  
 868          $query = $db->simple_select("forums", "*", "", array('order_by' => 'pid,disporder'));
 869          while($forum = $db->fetch_array($query))
 870          {
 871              foreach($forum as $key => $val)
 872              {
 873                  if(in_array($key, $exclude))
 874                  {
 875                      unset($forum[$key]);
 876                  }
 877              }
 878              $forums[$forum['fid']] = $forum;
 879          }
 880  
 881          $this->update("forums", $forums);
 882      }
 883  
 884      /**
 885       * Update usertitles cache.
 886       *
 887       */
 888  	function update_usertitles()
 889      {
 890          global $db;
 891  
 892          $usertitles = array();
 893          $query = $db->simple_select("usertitles", "utid, posts, title, stars, starimage", "", array('order_by' => 'posts', 'order_dir' => 'DESC'));
 894          while($usertitle = $db->fetch_array($query))
 895          {
 896              $usertitles[] = $usertitle;
 897          }
 898  
 899          $this->update("usertitles", $usertitles);
 900      }
 901  
 902      /**
 903       * Update reported content cache.
 904       *
 905       */
 906  	function update_reportedcontent()
 907      {
 908          global $db;
 909  
 910          $query = $db->simple_select("reportedcontent", "COUNT(rid) AS unreadcount", "reportstatus='0'");
 911          $unreadcount = $db->fetch_field($query, 'unreadcount');
 912  
 913          $query = $db->simple_select("reportedcontent", "COUNT(rid) AS reportcount");
 914          $reportcount = $db->fetch_field($query, 'reportcount');
 915          
 916          $query = $db->simple_select("reportedcontent", "dateline", "reportstatus='0'", array('order_by' => 'dateline', 'order_dir' => 'DESC', 'limit' => 1));
 917          $dateline = $db->fetch_field($query, 'dateline');
 918  
 919          $reports = array(
 920              'unread' => $unreadcount,
 921              'total' => $reportcount,
 922              'lastdateline' => $dateline,
 923          );
 924  
 925          $this->update("reportedcontent", $reports);
 926      }
 927  
 928      /**
 929       * Update mycode cache.
 930       *
 931       */
 932  	function update_mycode()
 933      {
 934          global $db;
 935  
 936          $mycodes = array();
 937          $query = $db->simple_select("mycode", "regex, replacement", "active=1", array('order_by' => 'parseorder'));
 938          while($mycode = $db->fetch_array($query))
 939          {
 940              $mycodes[] = $mycode;
 941          }
 942  
 943          $this->update("mycode", $mycodes);
 944      }
 945  
 946      /**
 947       * Update the mailqueue cache
 948       *
 949       * @param int $last_run
 950       * @param int $lock_time
 951       */
 952  	function update_mailqueue($last_run=0, $lock_time=0)
 953      {
 954          global $db;
 955  
 956          $query = $db->simple_select("mailqueue", "COUNT(*) AS queue_size");
 957          $queue_size = $db->fetch_field($query, "queue_size");
 958  
 959          $mailqueue = $this->read("mailqueue");
 960          if(!is_array($mailqueue))
 961          {
 962              $mailqueue = array();
 963          }
 964          $mailqueue['queue_size'] = $queue_size;
 965          if($last_run > 0)
 966          {
 967              $mailqueue['last_run'] = $last_run;
 968          }
 969          $mailqueue['locked'] = $lock_time;
 970  
 971          $this->update("mailqueue", $mailqueue);
 972      }
 973  
 974      /**
 975       * Update update_check cache (dummy function used by upgrade/install scripts)
 976       */
 977  	function update_update_check()
 978      {
 979          $update_cache = array(
 980              "dateline" => TIME_NOW
 981          );
 982  
 983          $this->update("update_check", $update_cache);
 984      }
 985  
 986      /**
 987       * Update default_theme cache
 988       */
 989  	function update_default_theme()
 990      {
 991          global $db;
 992  
 993          $query = $db->simple_select("themes", "name, tid, properties, stylesheets", "def='1'", array('limit' => 1));
 994          $theme = $db->fetch_array($query);
 995          $this->update("default_theme", $theme);
 996      }
 997  
 998      /**
 999       * Updates the tasks cache saving the next run time
1000       */
1001  	function update_tasks()
1002      {
1003          global $db;
1004  
1005          $query = $db->simple_select("tasks", "nextrun", "enabled=1", array("order_by" => "nextrun", "order_dir" => "asc", "limit" => 1));
1006          $next_task = $db->fetch_array($query);
1007  
1008          $task_cache = $this->read("tasks");
1009          if(!is_array($task_cache))
1010          {
1011              $task_cache = array();
1012          }
1013          $task_cache['nextrun'] = $next_task['nextrun'];
1014  
1015          if(!$task_cache['nextrun'])
1016          {
1017              $task_cache['nextrun'] = TIME_NOW+3600;
1018          }
1019  
1020          $this->update("tasks", $task_cache);
1021      }
1022  
1023      /**
1024       * Updates the banned IPs cache
1025       */
1026  	function update_bannedips()
1027      {
1028          global $db;
1029  
1030          $banned_ips = array();
1031          $query = $db->simple_select("banfilters", "fid,filter", "type=1");
1032          while($banned_ip = $db->fetch_array($query))
1033          {
1034              $banned_ips[$banned_ip['fid']] = $banned_ip;
1035          }
1036          $this->update("bannedips", $banned_ips);
1037      }
1038  
1039      /**
1040       * Updates the banned emails cache
1041       */
1042  	function update_bannedemails()
1043      {
1044          global $db;
1045  
1046          $banned_emails = array();
1047          $query = $db->simple_select("banfilters", "fid, filter", "type = '3'");
1048  
1049          while($banned_email = $db->fetch_array($query))
1050          {
1051              $banned_emails[$banned_email['fid']] = $banned_email;
1052          }
1053  
1054          $this->update("bannedemails", $banned_emails);
1055      }
1056  
1057      /**
1058       * Updates the search engine spiders cache
1059       */
1060  	function update_spiders()
1061      {
1062          global $db;
1063  
1064          $spiders = array();
1065          $query = $db->simple_select("spiders", "sid, name, useragent, usergroup", "", array("order_by" => "LENGTH(useragent)", "order_dir" => "DESC"));
1066          while($spider = $db->fetch_array($query))
1067          {
1068              $spiders[$spider['sid']] = $spider;
1069          }
1070          $this->update("spiders", $spiders);
1071      }
1072  
1073  	function update_most_replied_threads()
1074      {
1075          global $db, $mybb;
1076  
1077          $threads = array();
1078  
1079          $query = $db->simple_select("threads", "tid, subject, replies, fid, uid", "visible='1'", array('order_by' => 'replies', 'order_dir' => 'DESC', 'limit_start' => 0, 'limit' => $mybb->settings['statslimit']));
1080          while($thread = $db->fetch_array($query))
1081          {
1082              $threads[] = $thread;
1083          }
1084  
1085          $this->update("most_replied_threads", $threads);
1086      }
1087  
1088  	function update_most_viewed_threads()
1089      {
1090          global $db, $mybb;
1091  
1092          $threads = array();
1093  
1094          $query = $db->simple_select("threads", "tid, subject, views, fid, uid", "visible='1'", array('order_by' => 'views', 'order_dir' => 'DESC', 'limit_start' => 0, 'limit' => $mybb->settings['statslimit']));
1095          while($thread = $db->fetch_array($query))
1096          {
1097              $threads[] = $thread;
1098          }
1099  
1100          $this->update("most_viewed_threads", $threads);
1101      }
1102  
1103      /**
1104       * @deprecated
1105       */
1106  	function update_banned()
1107      {
1108          // "banned" cache removed
1109      }
1110  
1111  	function update_birthdays()
1112      {
1113          global $db;
1114  
1115          $birthdays = array();
1116  
1117          // Get today, yesterday, and tomorrow's time (for different timezones)
1118          $bdaytime = TIME_NOW;
1119          $bdaydate = my_date("j-n", $bdaytime, '', 0);
1120          $bdaydatetomorrow = my_date("j-n", ($bdaytime+86400), '', 0);
1121          $bdaydateyesterday = my_date("j-n", ($bdaytime-86400), '', 0);
1122  
1123          $query = $db->simple_select("users", "uid, username, usergroup, displaygroup, birthday, birthdayprivacy", "birthday LIKE '$bdaydate-%' OR birthday LIKE '$bdaydateyesterday-%' OR birthday LIKE '$bdaydatetomorrow-%'");
1124          while($bday = $db->fetch_array($query))
1125          {
1126              // Pop off the year from the birthday because we don't need it.
1127              $bday['bday'] = explode('-', $bday['birthday']);
1128              array_pop($bday['bday']);
1129              $bday['bday'] = implode('-', $bday['bday']);
1130  
1131              if($bday['birthdayprivacy'] != 'all')
1132              {
1133                  ++$birthdays[$bday['bday']]['hiddencount'];
1134                  continue;
1135              }
1136  
1137              // We don't need any excess caleries in the cache
1138              unset($bday['birthdayprivacy']);
1139  
1140              $birthdays[$bday['bday']]['users'][] = $bday;
1141          }
1142  
1143          $this->update("birthdays", $birthdays);
1144      }
1145  
1146  	function update_groupleaders()
1147      {
1148          global $db;
1149  
1150          $groupleaders = array();
1151  
1152          $query = $db->simple_select("groupleaders");
1153          while($groupleader = $db->fetch_array($query))
1154          {
1155              $groupleaders[$groupleader['uid']][] = $groupleader;
1156          }
1157  
1158          $this->update("groupleaders", $groupleaders);
1159      }
1160  
1161  	function update_threadprefixes()
1162      {
1163          global $db;
1164  
1165          $prefixes = array();
1166          $query = $db->simple_select("threadprefixes", "*", "", array('order_by' => 'prefix', 'order_dir' => 'ASC'));
1167  
1168          while($prefix = $db->fetch_array($query))
1169          {
1170              $prefixes[$prefix['pid']] = $prefix;
1171          }
1172  
1173          $this->update("threadprefixes", $prefixes);
1174      }
1175  
1176  	function update_forumsdisplay()
1177      {
1178          global $db;
1179  
1180          $fd_statistics = array();
1181  
1182          $time = TIME_NOW; // Look for announcements that don't end, or that are ending some time in the future
1183          $query = $db->simple_select("announcements", "fid", "enddate = '0' OR enddate > '{$time}'", array("order_by" => "aid"));
1184  
1185          if($db->num_rows($query))
1186          {
1187              while($forum = $db->fetch_array($query))
1188              {
1189                  if(!isset($fd_statistics[$forum['fid']]['announcements']))
1190                  {
1191                      $fd_statistics[$forum['fid']]['announcements'] = 1;
1192                  }
1193              }
1194          }
1195  
1196          // Do we have any mod tools to use in our forums?
1197          $query = $db->simple_select("modtools", "forums, tid", '', array("order_by" => "tid"));
1198  
1199          if($db->num_rows($query))
1200          {
1201              unset($forum);
1202              while($tool = $db->fetch_array($query))
1203              {
1204                  $forums = explode(",", $tool['forums']);
1205  
1206                  foreach($forums as $forum)
1207                  {
1208                      if(!$forum)
1209                      {
1210                          $forum = -1;
1211                      }
1212  
1213                      if(!isset($fd_statistics[$forum]['modtools']))
1214                      {
1215                          $fd_statistics[$forum]['modtools'] = 1;
1216                      }
1217                  }
1218              }
1219          }
1220  
1221          $this->update("forumsdisplay", $fd_statistics);
1222      }
1223  
1224      /**
1225       * Update profile fields cache.
1226       *
1227       */
1228  	function update_profilefields()
1229      {
1230          global $db;
1231  
1232          $fields = array();
1233          $query = $db->simple_select("profilefields", "*", "", array('order_by' => 'disporder'));
1234          while($field = $db->fetch_array($query))
1235          {
1236              $fields[] = $field;
1237          }
1238  
1239          $this->update("profilefields", $fields);
1240      }
1241  
1242      /**
1243       * Update the report reasons cache.
1244       *
1245       */
1246  	function update_reportreasons($no_plugins = false)
1247      {
1248          global $db;
1249  
1250          $content_types = array('post', 'profile', 'reputation');
1251          if(!$no_plugins)
1252          {
1253              global $plugins;
1254              $content_types = $plugins->run_hooks("report_content_types", $content_types);
1255          }
1256  
1257          $reasons = array();
1258  
1259          $query = $db->simple_select("reportreasons", "*", "", array('order_by' => 'disporder'));
1260          while($reason = $db->fetch_array($query))
1261          {
1262              if($reason['appliesto'] == 'all')
1263              {
1264                  foreach($content_types as $content)
1265                  {
1266                      $reasons[$content][] = array(
1267                          'rid' => $reason['rid'],
1268                          'title' => $reason['title'],
1269                          'extra' => $reason['extra'],
1270                      );
1271                  }
1272              }
1273              elseif($reason['appliesto'] != '')
1274              {
1275                  $appliesto = explode(",", $reason['appliesto']);
1276                  foreach($appliesto as $content)
1277                  {
1278                      $reasons[$content][] = array(
1279                          'rid' => $reason['rid'],
1280                          'title' => $reason['title'],
1281                          'extra' => $reason['extra'],
1282                      );
1283                  }
1284              }
1285          }
1286  
1287          $this->update("reportreasons", $reasons);
1288      }
1289  
1290      /* Other, extra functions for reloading caches if we just changed to another cache extension (i.e. from db -> xcache) */
1291  	function reload_mostonline()
1292      {
1293          global $db;
1294  
1295          $query = $db->simple_select("datacache", "title,cache", "title='mostonline'");
1296          $this->update("mostonline", unserialize($db->fetch_field($query, "cache")));
1297      }
1298  
1299  	function reload_plugins()
1300      {
1301          global $db;
1302  
1303          $query = $db->simple_select("datacache", "title,cache", "title='plugins'");
1304          $this->update("plugins", unserialize($db->fetch_field($query, "cache")));
1305      }
1306  
1307  	function reload_last_backup()
1308      {
1309          global $db;
1310  
1311          $query = $db->simple_select("datacache", "title,cache", "title='last_backup'");
1312          $this->update("last_backup", unserialize($db->fetch_field($query, "cache")));
1313      }
1314  
1315  	function reload_internal_settings()
1316      {
1317          global $db;
1318  
1319          $query = $db->simple_select("datacache", "title,cache", "title='internal_settings'");
1320          $this->update("internal_settings", unserialize($db->fetch_field($query, "cache")));
1321      }
1322  
1323  	function reload_version_history()
1324      {
1325          global $db;
1326  
1327          $query = $db->simple_select("datacache", "title,cache", "title='version_history'");
1328          $this->update("version_history", unserialize($db->fetch_field($query, "cache")));
1329      }
1330  
1331  	function reload_modnotes()
1332      {
1333          global $db;
1334  
1335          $query = $db->simple_select("datacache", "title,cache", "title='modnotes'");
1336          $this->update("modnotes", unserialize($db->fetch_field($query, "cache")));
1337      }
1338  
1339  	function reload_adminnotes()
1340      {
1341          global $db;
1342  
1343          $query = $db->simple_select("datacache", "title,cache", "title='adminnotes'");
1344          $this->update("adminnotes", unserialize($db->fetch_field($query, "cache")));
1345      }
1346  }


2005 - 2021 © MyBB.de | Alle Rechte vorbehalten! | Sponsor: netcup Cross-referenced by PHPXref