[ Index ]

PHP Cross Reference of MyBB 1.8.38

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 = array_keys($this->read("usergroups", true));
 648          if(!empty($this->forum_permissions_forum_cache[$pid]))
 649          {
 650              foreach($this->forum_permissions_forum_cache[$pid] as $main)
 651              {
 652                  foreach($main as $forum)
 653                  {
 654                      $perms = $permissions;
 655                      foreach($usergroups as $gid)
 656                      {
 657                          if(isset($this->forum_permissions[$forum['fid']][$gid]) && $this->forum_permissions[$forum['fid']][$gid])
 658                          {
 659                              $perms[$gid] = $this->forum_permissions[$forum['fid']][$gid];
 660                          }
 661                          if(!empty($perms[$gid]))
 662                          {
 663                              $perms[$gid]['fid'] = $forum['fid'];
 664                              $this->built_forum_permissions[$forum['fid']][$gid] = $perms[$gid];
 665                          }
 666                      }
 667                      $this->build_forum_permissions($perms, $forum['fid']);
 668                  }
 669              }
 670          }
 671      }
 672  
 673      /**
 674       * Update the stats cache (kept for the sake of being able to rebuild this cache via the cache interface)
 675       *
 676       */
 677  	function update_stats()
 678      {
 679          require_once  MYBB_ROOT."inc/functions_rebuild.php";
 680          rebuild_stats();
 681      }
 682  
 683      /**
 684       * Update the statistics cache
 685       *
 686       */
 687  	function update_statistics()
 688      {
 689          global $db;
 690  
 691          $query = $db->simple_select('users', 'uid, username, referrals', 'referrals>0', array('order_by' => 'referrals', 'order_dir' => 'DESC', 'limit' => 1));
 692          $topreferrer = $db->fetch_array($query);
 693  
 694          $timesearch = TIME_NOW - 86400;
 695  
 696          $query = $db->query("
 697              SELECT u.uid, u.username, COUNT(*) AS poststoday
 698              FROM {$db->table_prefix}posts p
 699              LEFT JOIN {$db->table_prefix}users u ON (p.uid=u.uid)
 700              WHERE p.dateline > {$timesearch} AND p.visible=1
 701              GROUP BY u.uid, u.username
 702              ORDER BY poststoday DESC
 703          ");
 704  
 705          $most_posts = 0;
 706          $topposter = array();
 707          while($user = $db->fetch_array($query))
 708          {
 709              if($user['poststoday'] > $most_posts)
 710              {
 711                  $most_posts = $user['poststoday'];
 712                  $topposter = $user;
 713              }
 714          }
 715  
 716          $query = $db->simple_select('users', 'COUNT(uid) AS posters', 'postnum>0');
 717          $posters = $db->fetch_field($query, 'posters');
 718  
 719          $statistics = array(
 720              'time' => TIME_NOW,
 721              'top_referrer' => (array)$topreferrer,
 722              'top_poster' => (array)$topposter,
 723              'posters' => $posters
 724          );
 725  
 726          $this->update('statistics', $statistics);
 727      }
 728  
 729      /**
 730       * Update the moderators cache.
 731       *
 732       * @return bool Returns false on failure
 733       */
 734  	function update_moderators()
 735      {
 736          global $forum_cache, $db;
 737  
 738          $this->built_moderators = array(0);
 739  
 740          // Get our forum list
 741          cache_forums(true);
 742          if(!is_array($forum_cache))
 743          {
 744              return false;
 745          }
 746  
 747          reset($forum_cache);
 748          $fcache = array();
 749  
 750          // Resort in to the structure we require
 751          foreach($forum_cache as $fid => $forum)
 752          {
 753              $this->moderators_forum_cache[$forum['pid']][$forum['disporder']][$forum['fid']] = $forum;
 754          }
 755  
 756          // Sort children
 757          foreach($fcache as $pid => $value)
 758          {
 759              ksort($fcache[$pid]);
 760          }
 761          ksort($fcache);
 762  
 763          $this->moderators = array();
 764  
 765          // Fetch moderators from the database
 766          $query = $db->query("
 767              SELECT m.*, u.username, u.usergroup, u.displaygroup
 768              FROM ".TABLE_PREFIX."moderators m
 769              LEFT JOIN ".TABLE_PREFIX."users u ON (m.id=u.uid)
 770              WHERE m.isgroup = '0'
 771              ORDER BY u.username
 772          ");
 773          while($moderator = $db->fetch_array($query))
 774          {
 775              $this->moderators[$moderator['fid']]['users'][$moderator['id']] = $moderator;
 776          }
 777  
 778          if(!function_exists("sort_moderators_by_usernames"))
 779          {
 780  			function sort_moderators_by_usernames($a, $b)
 781              {
 782                  return strcasecmp($a['username'], $b['username']);
 783              }
 784          }
 785  
 786          //Fetch moderating usergroups from the database
 787          $query = $db->query("
 788              SELECT m.*, u.title
 789              FROM ".TABLE_PREFIX."moderators m
 790              LEFT JOIN ".TABLE_PREFIX."usergroups u ON (m.id=u.gid)
 791              WHERE m.isgroup = '1'
 792              ORDER BY u.title
 793          ");
 794          while($moderator = $db->fetch_array($query))
 795          {
 796              $this->moderators[$moderator['fid']]['usergroups'][$moderator['id']] = $moderator;
 797          }
 798  
 799          foreach(array_keys($this->moderators) as $fid)
 800          {
 801              if(isset($this->moderators[$fid]['users']))
 802              {
 803                  uasort($this->moderators[$fid]['users'], 'sort_moderators_by_usernames');
 804              }
 805          }
 806  
 807          $this->build_moderators();
 808  
 809          $this->update("moderators", $this->built_moderators);
 810  
 811          return true;
 812      }
 813  
 814      /**
 815       * Update the users awaiting activation cache.
 816       *
 817       */
 818  	function update_awaitingactivation()
 819      {
 820          global $db;
 821  
 822          $query = $db->simple_select('users', 'COUNT(uid) AS awaitingusers', 'usergroup=\'5\'');
 823          $awaitingusers = (int)$db->fetch_field($query, 'awaitingusers');
 824  
 825          $data = array(
 826              'users'    => $awaitingusers,
 827              'time'    => TIME_NOW
 828          );
 829  
 830          $this->update('awaitingactivation', $data);
 831      }
 832  
 833      /**
 834       * Build the moderators array
 835       *
 836       * @access private
 837       * @param array $moderators An optional moderators array (moderators of the parent forum for example).
 838       * @param int $pid An optional parent ID.
 839       */
 840  	private function build_moderators($moderators=array(), $pid=0)
 841      {
 842          if(isset($this->moderators_forum_cache[$pid]))
 843          {
 844              foreach($this->moderators_forum_cache[$pid] as $main)
 845              {
 846                  foreach($main as $forum)
 847                  {
 848                      $forum_mods = array();
 849                      if(count($moderators))
 850                      {
 851                          $forum_mods = $moderators;
 852                      }
 853                      // Append - local settings override that of a parent - array_merge works here
 854                      if(isset($this->moderators[$forum['fid']]))
 855                      {
 856                          if(is_array($forum_mods) && count($forum_mods))
 857                          {
 858                              $forum_mods = array_merge($forum_mods, $this->moderators[$forum['fid']]);
 859                          }
 860                          else
 861                          {
 862                              $forum_mods = $this->moderators[$forum['fid']];
 863                          }
 864                      }
 865                      $this->built_moderators[$forum['fid']] = $forum_mods;
 866                      $this->build_moderators($forum_mods, $forum['fid']);
 867                  }
 868              }
 869          }
 870      }
 871  
 872      /**
 873       * Update the forums cache.
 874       *
 875       */
 876  	function update_forums()
 877      {
 878          global $db;
 879  
 880          $forums = array();
 881  
 882          // Things we don't want to cache
 883          $exclude = array("unapprovedthreads", "unapprovedposts", "threads", "posts", "lastpost", "lastposter", "lastposttid", "lastposteruid", "lastpostsubject", "deletedthreads", "deletedposts");
 884  
 885          $query = $db->simple_select("forums", "*", "", array('order_by' => 'pid,disporder'));
 886          while($forum = $db->fetch_array($query))
 887          {
 888              foreach($forum as $key => $val)
 889              {
 890                  if(in_array($key, $exclude))
 891                  {
 892                      unset($forum[$key]);
 893                  }
 894              }
 895              $forums[$forum['fid']] = $forum;
 896          }
 897  
 898          $this->update("forums", $forums);
 899      }
 900  
 901      /**
 902       * Update usertitles cache.
 903       *
 904       */
 905  	function update_usertitles()
 906      {
 907          global $db;
 908  
 909          $usertitles = array();
 910          $query = $db->simple_select("usertitles", "utid, posts, title, stars, starimage", "", array('order_by' => 'posts', 'order_dir' => 'DESC'));
 911          while($usertitle = $db->fetch_array($query))
 912          {
 913              $usertitles[] = $usertitle;
 914          }
 915  
 916          $this->update("usertitles", $usertitles);
 917      }
 918  
 919      /**
 920       * Update reported content cache.
 921       *
 922       */
 923  	function update_reportedcontent()
 924      {
 925          global $db;
 926  
 927          $query = $db->simple_select("reportedcontent", "COUNT(rid) AS unreadcount", "reportstatus='0'");
 928          $unreadcount = $db->fetch_field($query, 'unreadcount');
 929  
 930          $query = $db->simple_select("reportedcontent", "COUNT(rid) AS reportcount");
 931          $reportcount = $db->fetch_field($query, 'reportcount');
 932          
 933          $query = $db->simple_select("reportedcontent", "dateline", "reportstatus='0'", array('order_by' => 'dateline', 'order_dir' => 'DESC', 'limit' => 1));
 934          $dateline = $db->fetch_field($query, 'dateline');
 935  
 936          $reports = array(
 937              'unread' => $unreadcount,
 938              'total' => $reportcount,
 939              'lastdateline' => $dateline,
 940          );
 941  
 942          $this->update("reportedcontent", $reports);
 943      }
 944  
 945      /**
 946       * Update mycode cache.
 947       *
 948       */
 949  	function update_mycode()
 950      {
 951          global $db;
 952  
 953          $mycodes = array();
 954          $query = $db->simple_select("mycode", "regex, replacement", "active=1", array('order_by' => 'parseorder'));
 955          while($mycode = $db->fetch_array($query))
 956          {
 957              $mycodes[] = $mycode;
 958          }
 959  
 960          $this->update("mycode", $mycodes);
 961      }
 962  
 963      /**
 964       * Update the mailqueue cache
 965       *
 966       * @param int $last_run
 967       * @param int $lock_time
 968       */
 969  	function update_mailqueue($last_run=0, $lock_time=0)
 970      {
 971          global $db;
 972  
 973          $query = $db->simple_select("mailqueue", "COUNT(*) AS queue_size");
 974          $queue_size = $db->fetch_field($query, "queue_size");
 975  
 976          $mailqueue = $this->read("mailqueue");
 977          if(!is_array($mailqueue))
 978          {
 979              $mailqueue = array();
 980          }
 981          $mailqueue['queue_size'] = $queue_size;
 982          if($last_run > 0)
 983          {
 984              $mailqueue['last_run'] = $last_run;
 985          }
 986          $mailqueue['locked'] = $lock_time;
 987  
 988          $this->update("mailqueue", $mailqueue);
 989      }
 990  
 991      /**
 992       * Update update_check cache (dummy function used by upgrade/install scripts)
 993       */
 994  	function update_update_check()
 995      {
 996          $update_cache = array(
 997              "dateline" => TIME_NOW
 998          );
 999  
1000          $this->update("update_check", $update_cache);
1001      }
1002  
1003      /**
1004       * Update default_theme cache
1005       */
1006  	function update_default_theme()
1007      {
1008          global $db;
1009  
1010          $query = $db->simple_select("themes", "name, tid, properties, stylesheets", "def='1'", array('limit' => 1));
1011          $theme = $db->fetch_array($query);
1012          $this->update("default_theme", $theme);
1013      }
1014  
1015      /**
1016       * Updates the tasks cache saving the next run time
1017       */
1018  	function update_tasks()
1019      {
1020          global $db;
1021  
1022          $query = $db->simple_select("tasks", "nextrun", "enabled=1", array("order_by" => "nextrun", "order_dir" => "asc", "limit" => 1));
1023          $next_task = $db->fetch_array($query);
1024  
1025          $task_cache = $this->read("tasks");
1026          if(!is_array($task_cache))
1027          {
1028              $task_cache = array();
1029          }
1030          $task_cache['nextrun'] = $next_task['nextrun'];
1031  
1032          if(!$task_cache['nextrun'])
1033          {
1034              $task_cache['nextrun'] = TIME_NOW+3600;
1035          }
1036  
1037          $this->update("tasks", $task_cache);
1038      }
1039  
1040      /**
1041       * Updates the banned IPs cache
1042       */
1043  	function update_bannedips()
1044      {
1045          global $db;
1046  
1047          $banned_ips = array();
1048          $query = $db->simple_select("banfilters", "fid,filter", "type=1");
1049          while($banned_ip = $db->fetch_array($query))
1050          {
1051              $banned_ips[$banned_ip['fid']] = $banned_ip;
1052          }
1053          $this->update("bannedips", $banned_ips);
1054      }
1055  
1056      /**
1057       * Updates the banned emails cache
1058       */
1059  	function update_bannedemails()
1060      {
1061          global $db;
1062  
1063          $banned_emails = array();
1064          $query = $db->simple_select("banfilters", "fid, filter", "type = '3'");
1065  
1066          while($banned_email = $db->fetch_array($query))
1067          {
1068              $banned_emails[$banned_email['fid']] = $banned_email;
1069          }
1070  
1071          $this->update("bannedemails", $banned_emails);
1072      }
1073  
1074      /**
1075       * Updates the search engine spiders cache
1076       */
1077  	function update_spiders()
1078      {
1079          global $db;
1080  
1081          $spiders = array();
1082          $query = $db->simple_select("spiders", "sid, name, useragent, usergroup", "", array("order_by" => "LENGTH(useragent)", "order_dir" => "DESC"));
1083          while($spider = $db->fetch_array($query))
1084          {
1085              $spiders[$spider['sid']] = $spider;
1086          }
1087          $this->update("spiders", $spiders);
1088      }
1089  
1090  	function update_most_replied_threads()
1091      {
1092          global $db, $mybb;
1093  
1094          $threads = array();
1095  
1096          $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']));
1097          while($thread = $db->fetch_array($query))
1098          {
1099              $threads[] = $thread;
1100          }
1101  
1102          $this->update("most_replied_threads", $threads);
1103      }
1104  
1105  	function update_most_viewed_threads()
1106      {
1107          global $db, $mybb;
1108  
1109          $threads = array();
1110  
1111          $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']));
1112          while($thread = $db->fetch_array($query))
1113          {
1114              $threads[] = $thread;
1115          }
1116  
1117          $this->update("most_viewed_threads", $threads);
1118      }
1119  
1120      /**
1121       * @deprecated
1122       */
1123  	function update_banned()
1124      {
1125          // "banned" cache removed
1126      }
1127  
1128  	function update_birthdays()
1129      {
1130          global $db;
1131  
1132          $birthdays = array();
1133  
1134          // Get today, yesterday, and tomorrow's time (for different timezones)
1135          $bdaytime = TIME_NOW;
1136          $bdaydate = my_date("j-n", $bdaytime, '', 0);
1137          $bdaydatetomorrow = my_date("j-n", ($bdaytime+86400), '', 0);
1138          $bdaydateyesterday = my_date("j-n", ($bdaytime-86400), '', 0);
1139  
1140          $query = $db->simple_select("users", "uid, username, usergroup, displaygroup, birthday, birthdayprivacy", "birthday LIKE '$bdaydate-%' OR birthday LIKE '$bdaydateyesterday-%' OR birthday LIKE '$bdaydatetomorrow-%'");
1141          while($bday = $db->fetch_array($query))
1142          {
1143              // Pop off the year from the birthday because we don't need it.
1144              $bday['bday'] = explode('-', $bday['birthday']);
1145              array_pop($bday['bday']);
1146              $bday['bday'] = implode('-', $bday['bday']);
1147  
1148              if($bday['birthdayprivacy'] != 'all')
1149              {
1150                  if(isset($birthdays[$bday['bday']]['hiddencount']))
1151                  {
1152                      ++$birthdays[$bday['bday']]['hiddencount'];
1153                  }
1154                  else
1155                  {
1156                      $birthdays[$bday['bday']]['hiddencount'] = 1;
1157                  }
1158                  continue;
1159              }
1160  
1161              // We don't need any excess caleries in the cache
1162              unset($bday['birthdayprivacy']);
1163  
1164              if(!isset($birthdays[$bday['bday']]['users']))
1165              {
1166                  $birthdays[$bday['bday']]['users'] = array();
1167              }
1168  
1169              $birthdays[$bday['bday']]['users'][] = $bday;
1170          }
1171  
1172          $this->update("birthdays", $birthdays);
1173      }
1174  
1175  	function update_groupleaders()
1176      {
1177          global $db;
1178  
1179          $groupleaders = array();
1180  
1181          $query = $db->simple_select("groupleaders");
1182          while($groupleader = $db->fetch_array($query))
1183          {
1184              $groupleaders[$groupleader['uid']][] = $groupleader;
1185          }
1186  
1187          $this->update("groupleaders", $groupleaders);
1188      }
1189  
1190  	function update_threadprefixes()
1191      {
1192          global $db;
1193  
1194          $prefixes = array();
1195          $query = $db->simple_select("threadprefixes", "*", "", array('order_by' => 'prefix', 'order_dir' => 'ASC'));
1196  
1197          while($prefix = $db->fetch_array($query))
1198          {
1199              $prefixes[$prefix['pid']] = $prefix;
1200          }
1201  
1202          $this->update("threadprefixes", $prefixes);
1203      }
1204  
1205  	function update_forumsdisplay()
1206      {
1207          global $db;
1208  
1209          $fd_statistics = array();
1210  
1211          $time = TIME_NOW; // Look for announcements that don't end, or that are ending some time in the future
1212          $query = $db->simple_select("announcements", "fid", "enddate = '0' OR enddate > '{$time}'", array("order_by" => "aid"));
1213  
1214          if($db->num_rows($query))
1215          {
1216              while($forum = $db->fetch_array($query))
1217              {
1218                  if(!isset($fd_statistics[$forum['fid']]['announcements']))
1219                  {
1220                      $fd_statistics[$forum['fid']]['announcements'] = 1;
1221                  }
1222              }
1223          }
1224  
1225          // Do we have any mod tools to use in our forums?
1226          $query = $db->simple_select("modtools", "forums, tid", '', array("order_by" => "tid"));
1227  
1228          if($db->num_rows($query))
1229          {
1230              unset($forum);
1231              while($tool = $db->fetch_array($query))
1232              {
1233                  $forums = explode(",", $tool['forums']);
1234  
1235                  foreach($forums as $forum)
1236                  {
1237                      if(!$forum)
1238                      {
1239                          $forum = -1;
1240                      }
1241  
1242                      if(!isset($fd_statistics[$forum]['modtools']))
1243                      {
1244                          $fd_statistics[$forum]['modtools'] = 1;
1245                      }
1246                  }
1247              }
1248          }
1249  
1250          $this->update("forumsdisplay", $fd_statistics);
1251      }
1252  
1253      /**
1254       * Update profile fields cache.
1255       *
1256       */
1257  	function update_profilefields()
1258      {
1259          global $db;
1260  
1261          $fields = array();
1262          $query = $db->simple_select("profilefields", "*", "", array('order_by' => 'disporder'));
1263          while($field = $db->fetch_array($query))
1264          {
1265              $fields[] = $field;
1266          }
1267  
1268          $this->update("profilefields", $fields);
1269      }
1270  
1271      /**
1272       * Update the report reasons cache.
1273       *
1274       */
1275  	function update_reportreasons($no_plugins = false)
1276      {
1277          global $db;
1278  
1279          $content_types = array('post', 'profile', 'reputation');
1280          if(!$no_plugins)
1281          {
1282              global $plugins;
1283              $content_types = $plugins->run_hooks("report_content_types", $content_types);
1284          }
1285  
1286          $reasons = array();
1287  
1288          $query = $db->simple_select("reportreasons", "*", "", array('order_by' => 'disporder'));
1289          while($reason = $db->fetch_array($query))
1290          {
1291              if($reason['appliesto'] == 'all')
1292              {
1293                  foreach($content_types as $content)
1294                  {
1295                      $reasons[$content][] = array(
1296                          'rid' => $reason['rid'],
1297                          'title' => $reason['title'],
1298                          'extra' => $reason['extra'],
1299                      );
1300                  }
1301              }
1302              elseif($reason['appliesto'] != '')
1303              {
1304                  $appliesto = explode(",", $reason['appliesto']);
1305                  foreach($appliesto as $content)
1306                  {
1307                      $reasons[$content][] = array(
1308                          'rid' => $reason['rid'],
1309                          'title' => $reason['title'],
1310                          'extra' => $reason['extra'],
1311                      );
1312                  }
1313              }
1314          }
1315  
1316          $this->update("reportreasons", $reasons);
1317      }
1318  
1319      /* Other, extra functions for reloading caches if we just changed to another cache extension (i.e. from db -> xcache) */
1320  	function reload_mostonline()
1321      {
1322          global $db;
1323  
1324          $query = $db->simple_select("datacache", "title,cache", "title='mostonline'");
1325          $this->update("mostonline", my_unserialize($db->fetch_field($query, "cache")));
1326      }
1327  
1328  	function reload_plugins()
1329      {
1330          global $db;
1331  
1332          $query = $db->simple_select("datacache", "title,cache", "title='plugins'");
1333          $this->update("plugins", my_unserialize($db->fetch_field($query, "cache")));
1334      }
1335  
1336  	function reload_last_backup()
1337      {
1338          global $db;
1339  
1340          $query = $db->simple_select("datacache", "title,cache", "title='last_backup'");
1341          $this->update("last_backup", my_unserialize($db->fetch_field($query, "cache")));
1342      }
1343  
1344  	function reload_internal_settings()
1345      {
1346          global $db;
1347  
1348          $query = $db->simple_select("datacache", "title,cache", "title='internal_settings'");
1349          $this->update("internal_settings", my_unserialize($db->fetch_field($query, "cache")));
1350      }
1351  
1352  	function reload_version_history()
1353      {
1354          global $db;
1355  
1356          $query = $db->simple_select("datacache", "title,cache", "title='version_history'");
1357          $this->update("version_history", my_unserialize($db->fetch_field($query, "cache")));
1358      }
1359  
1360  	function reload_modnotes()
1361      {
1362          global $db;
1363  
1364          $query = $db->simple_select("datacache", "title,cache", "title='modnotes'");
1365          $this->update("modnotes", my_unserialize($db->fetch_field($query, "cache")));
1366      }
1367  
1368  	function reload_adminnotes()
1369      {
1370          global $db;
1371  
1372          $query = $db->simple_select("datacache", "title,cache", "title='adminnotes'");
1373          $this->update("adminnotes", my_unserialize($db->fetch_field($query, "cache")));
1374      }
1375  }


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