Verzeichnisstruktur phpBB-3.0.0


Veröffentlicht
12.12.2007

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

      (Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

acm_file.php

Zuletzt modifiziert: 09.10.2024, 12:51 - Dateigröße: 9.51 KiB


 001  <?php
 002  /**
 003  *
 004  * @package acm
 005  * @version $Id$
 006  * @copyright (c) 2005 phpBB Group
 007  * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 008  *
 009  */
 010   
 011  /**
 012  * @ignore
 013  */
 014  if (!defined('IN_PHPBB'))
 015  {
 016      exit;
 017  }
 018   
 019  /**
 020  * ACM File Based Caching
 021  * @package acm
 022  */
 023  class acm
 024  {
 025      var $vars = array();
 026      var $var_expires = array();
 027      var $is_modified = false;
 028   
 029      var $sql_rowset = array();
 030      var $sql_row_pointer = array();
 031      var $cache_dir = '';
 032   
 033      /**
 034      * Set cache path
 035      */
 036      function acm()
 037      {
 038          global $phpbb_root_path;
 039          $this->cache_dir = $phpbb_root_path . 'cache/';
 040      }
 041   
 042      /**
 043      * Load global cache
 044      */
 045      function load()
 046      {
 047          global $phpEx;
 048          if (file_exists($this->cache_dir . 'data_global.' . $phpEx))
 049          {
 050              @include($this->cache_dir . 'data_global.' . $phpEx);
 051          }
 052          else
 053          {
 054              return false;
 055          }
 056   
 057          return true;
 058      }
 059   
 060      /**
 061      * Unload cache object
 062      */
 063      function unload()
 064      {
 065          $this->save();
 066          unset($this->vars);
 067          unset($this->var_expires);
 068          unset($this->sql_rowset);
 069          unset($this->sql_row_pointer);
 070   
 071          $this->vars = array();
 072          $this->var_expires = array();
 073          $this->sql_rowset = array();
 074          $this->sql_row_pointer = array();
 075      }
 076   
 077      /**
 078      * Save modified objects
 079      */
 080      function save()
 081      {
 082          if (!$this->is_modified)
 083          {
 084              return;
 085          }
 086   
 087          global $phpEx;
 088   
 089          if ($fp = @fopen($this->cache_dir . 'data_global.' . $phpEx, 'wb'))
 090          {
 091              @flock($fp, LOCK_EX);
 092              fwrite($fp, "<?php\n\$this->vars = " . var_export($this->vars, true) . ";\n\n\$this->var_expires = " . var_export($this->var_expires, true) . "\n?>");
 093              @flock($fp, LOCK_UN);
 094              fclose($fp);
 095   
 096              @chmod($this->cache_dir . 'data_global.' . $phpEx, 0666);
 097          }
 098          else
 099          {
 100              // Now, this occurred how often? ... phew, just tell the user then...
 101              if (!@is_writable($this->cache_dir))
 102              {
 103                  trigger_error($this->cache_dir . ' is NOT writable.', E_USER_ERROR);
 104              }
 105   
 106              trigger_error('Not able to open ' . $this->cache_dir . 'data_global.' . $phpEx, E_USER_ERROR);
 107          }
 108   
 109          $this->is_modified = false;
 110      }
 111   
 112      /**
 113      * Tidy cache
 114      */
 115      function tidy()
 116      {
 117          global $phpEx;
 118   
 119          $dir = @opendir($this->cache_dir);
 120   
 121          if (!$dir)
 122          {
 123              return;
 124          }
 125   
 126          while (($entry = readdir($dir)) !== false)
 127          {
 128              if (!preg_match('/^(sql_|data_(?!global))/', $entry))
 129              {
 130                  continue;
 131              }
 132   
 133              $expired = true;
 134              @include($this->cache_dir . $entry);
 135              if ($expired)
 136              {
 137                  $this->remove_file($this->cache_dir . $entry);
 138              }
 139          }
 140          closedir($dir);
 141   
 142          if (file_exists($this->cache_dir . 'data_global.' . $phpEx))
 143          {
 144              if (!sizeof($this->vars))
 145              {
 146                  $this->load();
 147              }
 148   
 149              foreach ($this->var_expires as $var_name => $expires)
 150              {
 151                  if (time() > $expires)
 152                  {
 153                      $this->destroy($var_name);
 154                  }
 155              }
 156          }
 157          
 158          set_config('cache_last_gc', time(), true);
 159      }
 160   
 161      /**
 162      * Get saved cache object
 163      */
 164      function get($var_name)
 165      {
 166          if ($var_name[0] == '_')
 167          {
 168              global $phpEx;
 169   
 170              if (!$this->_exists($var_name))
 171              {
 172                  return false;
 173              }
 174   
 175              @include($this->cache_dir . "data{$var_name}.$phpEx");
 176              return (isset($data)) ? $data : false;
 177          }
 178          else
 179          {
 180              return ($this->_exists($var_name)) ? $this->vars[$var_name] : false;
 181          }
 182      }
 183   
 184      /**
 185      * Put data into cache
 186      */
 187      function put($var_name, $var, $ttl = 31536000)
 188      {
 189          if ($var_name[0] == '_')
 190          {
 191              global $phpEx;
 192   
 193              if ($fp = @fopen($this->cache_dir . "data{$var_name}.$phpEx", 'wb'))
 194              {
 195                  @flock($fp, LOCK_EX);
 196                  fwrite($fp, "<?php\n\$expired = (time() > " . (time() + $ttl) . ") ? true : false;\nif (\$expired) { return; }\n\n\$data = " . var_export($var, true) . ";\n?>");
 197                  @flock($fp, LOCK_UN);
 198                  fclose($fp);
 199   
 200                  @chmod($this->cache_dir . "data{$var_name}.$phpEx", 0666);
 201              }
 202          }
 203          else
 204          {
 205              $this->vars[$var_name] = $var;
 206              $this->var_expires[$var_name] = time() + $ttl;
 207              $this->is_modified = true;
 208          }
 209      }
 210   
 211      /**
 212      * Purge cache data
 213      */
 214      function purge()
 215      {
 216          // Purge all phpbb cache files
 217          $dir = @opendir($this->cache_dir);
 218   
 219          if (!$dir)
 220          {
 221              return;
 222          }
 223   
 224          while (($entry = readdir($dir)) !== false)
 225          {
 226              if (strpos($entry, 'sql_') !== 0 && strpos($entry, 'data_') !== 0 && strpos($entry, 'ctpl_') !== 0 && strpos($entry, 'tpl_') !== 0)
 227              {
 228                  continue;
 229              }
 230   
 231              $this->remove_file($this->cache_dir . $entry);
 232          }
 233          closedir($dir);
 234   
 235          unset($this->vars);
 236          unset($this->var_expires);
 237          unset($this->sql_rowset);
 238          unset($this->sql_row_pointer);
 239   
 240          $this->vars = array();
 241          $this->var_expires = array();
 242          $this->sql_rowset = array();
 243          $this->sql_row_pointer = array();
 244   
 245          $this->is_modified = false;
 246      }
 247   
 248      /**
 249      * Destroy cache data
 250      */
 251      function destroy($var_name, $table = '')
 252      {
 253          global $phpEx;
 254   
 255          if ($var_name == 'sql' && !empty($table))
 256          {
 257              if (!is_array($table))
 258              {
 259                  $table = array($table);
 260              }
 261   
 262              $dir = @opendir($this->cache_dir);
 263   
 264              if (!$dir)
 265              {
 266                  return;
 267              }
 268   
 269              while (($entry = readdir($dir)) !== false)
 270              {
 271                  if (strpos($entry, 'sql_') !== 0)
 272                  {
 273                      continue;
 274                  }
 275   
 276                  // The following method is more failproof than simply assuming the query is on line 3 (which it should be)
 277                  $check_line = @file_get_contents($this->cache_dir . $entry);
 278   
 279                  if (empty($check_line))
 280                  {
 281                      continue;
 282                  }
 283   
 284                  // Now get the contents between /* and */
 285                  $check_line = substr($check_line, strpos($check_line, '/* ') + 3, strpos($check_line, ' */') - strpos($check_line, '/* ') - 3);
 286   
 287                  $found = false;
 288                  foreach ($table as $check_table)
 289                  {
 290                      // Better catch partial table names than no table names. ;)
 291                      if (strpos($check_line, $check_table) !== false)
 292                      {
 293                          $found = true;
 294                          break;
 295                      }
 296                  }
 297   
 298                  if ($found)
 299                  {
 300                      $this->remove_file($this->cache_dir . $entry);
 301                  }
 302              }
 303              closedir($dir);
 304   
 305              return;
 306          }
 307   
 308          if (!$this->_exists($var_name))
 309          {
 310              return;
 311          }
 312   
 313          if ($var_name[0] == '_')
 314          {
 315              $this->remove_file($this->cache_dir . 'data' . $var_name . ".$phpEx");
 316          }
 317          else if (isset($this->vars[$var_name]))
 318          {
 319              $this->is_modified = true;
 320              unset($this->vars[$var_name]);
 321              unset($this->var_expires[$var_name]);
 322   
 323              // We save here to let the following cache hits succeed
 324              $this->save();
 325          }
 326      }
 327   
 328      /**
 329      * Check if a given cache entry exist
 330      */
 331      function _exists($var_name)
 332      {
 333          if ($var_name[0] == '_')
 334          {
 335              global $phpEx;
 336              return file_exists($this->cache_dir . 'data' . $var_name . ".$phpEx");
 337          }
 338          else
 339          {
 340              if (!sizeof($this->vars))
 341              {
 342                  $this->load();
 343              }
 344   
 345              if (!isset($this->var_expires[$var_name]))
 346              {
 347                  return false;
 348              }
 349   
 350              return (time() > $this->var_expires[$var_name]) ? false : isset($this->vars[$var_name]);
 351          }
 352      }
 353   
 354      /**
 355      * Load cached sql query
 356      */
 357      function sql_load($query)
 358      {
 359          global $phpEx;
 360   
 361          // Remove extra spaces and tabs
 362          $query = preg_replace('/[\n\r\s\t]+/', ' ', $query);
 363          $query_id = sizeof($this->sql_rowset);
 364   
 365          if (!file_exists($this->cache_dir . 'sql_' . md5($query) . ".$phpEx"))
 366          {
 367              return false;
 368          }
 369   
 370          @include($this->cache_dir . 'sql_' . md5($query) . ".$phpEx");
 371   
 372          if (!isset($expired))
 373          {
 374              return false;
 375          }
 376          else if ($expired)
 377          {
 378              $this->remove_file($this->cache_dir . 'sql_' . md5($query) . ".$phpEx");
 379              return false;
 380          }
 381   
 382          $this->sql_row_pointer[$query_id] = 0;
 383   
 384          return $query_id;
 385      }
 386   
 387      /**
 388      * Save sql query
 389      */
 390      function sql_save($query, &$query_result, $ttl)
 391      {
 392          global $db, $phpEx;
 393   
 394          // Remove extra spaces and tabs
 395          $query = preg_replace('/[\n\r\s\t]+/', ' ', $query);
 396          $filename = $this->cache_dir . 'sql_' . md5($query) . '.' . $phpEx;
 397   
 398          if ($fp = @fopen($filename, 'wb'))
 399          {
 400              @flock($fp, LOCK_EX);
 401   
 402              $query_id = sizeof($this->sql_rowset);
 403              $this->sql_rowset[$query_id] = array();
 404              $this->sql_row_pointer[$query_id] = 0;
 405   
 406              while ($row = $db->sql_fetchrow($query_result))
 407              {
 408                  $this->sql_rowset[$query_id][] = $row;
 409              }
 410              $db->sql_freeresult($query_result);
 411   
 412              $file = "<?php\n\n/* " . str_replace('*/', '*\/', $query) . " */\n";
 413              $file .= "\n\$expired = (time() > " . (time() + $ttl) . ") ? true : false;\nif (\$expired) { return; }\n";
 414   
 415              fwrite($fp, $file . "\n\$this->sql_rowset[\$query_id] = " . var_export($this->sql_rowset[$query_id], true) . ";\n?>");
 416              @flock($fp, LOCK_UN);
 417              fclose($fp);
 418   
 419              @chmod($filename, 0666);
 420   
 421              $query_result = $query_id;
 422          }
 423      }
 424   
 425      /**
 426      * Ceck if a given sql query exist in cache
 427      */
 428      function sql_exists($query_id)
 429      {
 430          return isset($this->sql_rowset[$query_id]);
 431      }
 432   
 433      /**
 434      * Fetch row from cache (database)
 435      */
 436      function sql_fetchrow($query_id)
 437      {
 438          if ($this->sql_row_pointer[$query_id] < sizeof($this->sql_rowset[$query_id]))
 439          {
 440              return $this->sql_rowset[$query_id][$this->sql_row_pointer[$query_id]++];
 441          }
 442   
 443          return false;
 444      }
 445   
 446      /**
 447      * Fetch a field from the current row of a cached database result (database)
 448      */
 449      function sql_fetchfield($query_id, $field)
 450      {
 451          if ($this->sql_row_pointer[$query_id] < sizeof($this->sql_rowset[$query_id]))
 452          {
 453              return (isset($this->sql_rowset[$query_id][$this->sql_row_pointer[$query_id]][$field])) ? $this->sql_rowset[$query_id][$this->sql_row_pointer[$query_id]][$field] : false;
 454          }
 455   
 456          return false;
 457      }
 458   
 459      /**
 460      * Seek a specific row in an a cached database result (database)
 461      */
 462      function sql_rowseek($rownum, $query_id)
 463      {
 464          if ($rownum >= sizeof($this->sql_rowset[$query_id]))
 465          {
 466              return false;
 467          }
 468   
 469          $this->sql_row_pointer[$query_id] = $rownum;
 470          return true;
 471      }
 472   
 473      /**
 474      * Free memory used for a cached database result (database)
 475      */
 476      function sql_freeresult($query_id)
 477      {
 478          if (!isset($this->sql_rowset[$query_id]))
 479          {
 480              return false;
 481          }
 482   
 483          unset($this->sql_rowset[$query_id]);
 484          unset($this->sql_row_pointer[$query_id]);
 485   
 486          return true;
 487      }
 488   
 489      /**
 490      * Removes/unlinks file
 491      */
 492      function remove_file($filename)
 493      {
 494          if (!@unlink($filename))
 495          {
 496              // E_USER_ERROR - not using language entry - intended.
 497              trigger_error('Unable to remove files within ' . $this->cache_dir . '. Please check directory permissions.', E_USER_ERROR);
 498          }
 499      }
 500  }
 501   
 502  ?>