[ Index ]

PHP Cross Reference of MyBB 1.8.39

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


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