Verzeichnisstruktur phpBB-3.1.0


Veröffentlicht
27.10.2014

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

db.php

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


 001  <?php
 002  /**
 003  *
 004  * This file is part of the phpBB Forum Software package.
 005  *
 006  * @copyright (c) phpBB Limited <https://www.phpbb.com>
 007  * @license GNU General Public License, version 2 (GPL-2.0)
 008  *
 009  * For full copyright and license information, please see
 010  * the docs/CREDITS.txt file.
 011  *
 012  */
 013   
 014  namespace phpbb\lock;
 015   
 016  /**
 017  * Database locking class
 018  */
 019  class db
 020  {
 021      /**
 022      * Name of the config variable this lock uses
 023      * @var string
 024      */
 025      private $config_name;
 026   
 027      /**
 028      * Unique identifier for this lock.
 029      *
 030      * @var string
 031      */
 032      private $unique_id;
 033   
 034      /**
 035      * Stores the state of this lock
 036      * @var bool
 037      */
 038      private $locked;
 039   
 040      /**
 041      * The phpBB configuration
 042      * @var \phpbb\config\config
 043      */
 044      private $config;
 045   
 046      /**
 047      * A database connection
 048      * @var \phpbb\db\driver\driver_interface
 049      */
 050      private $db;
 051   
 052      /**
 053      * Creates a named released instance of the lock.
 054      *
 055      * You have to call acquire() to actually create the lock.
 056      *
 057      * @param    string                                $config_name    A config variable to be used for locking
 058      * @param    \phpbb\config\config                $config            The phpBB configuration
 059      * @param    \phpbb\db\driver\driver_interface    $db                A database connection
 060      */
 061      public function __construct($config_name, \phpbb\config\config $config, \phpbb\db\driver\driver_interface $db)
 062      {
 063          $this->config_name = $config_name;
 064          $this->config = $config;
 065          $this->db = $db;
 066      }
 067   
 068      /**
 069      * Tries to acquire the lock by updating
 070      * the configuration variable in the database.
 071      *
 072      * As a lock may only be held by one process at a time, lock
 073      * acquisition may fail if another process is holding the lock
 074      * or if another process obtained the lock but never released it.
 075      * Locks are forcibly released after a timeout of 1 hour.
 076      *
 077      * @return    bool            true if lock was acquired
 078      *                            false otherwise
 079      */
 080      public function acquire()
 081      {
 082          if ($this->locked)
 083          {
 084              return false;
 085          }
 086   
 087          if (!isset($this->config[$this->config_name]))
 088          {
 089              $this->config->set($this->config_name, '0', false);
 090          }
 091          $lock_value = $this->config[$this->config_name];
 092   
 093          // make sure lock cannot be acquired by multiple processes
 094          if ($lock_value)
 095          {
 096              // if the other process is running more than an hour already we have to assume it
 097              // aborted without cleaning the lock
 098              $time = explode(' ', $lock_value);
 099              $time = $time[0];
 100   
 101              if ($time + 3600 >= time())
 102              {
 103                  return false;
 104              }
 105          }
 106   
 107          $this->unique_id = time() . ' ' . unique_id();
 108   
 109          // try to update the config value, if it was already modified by another
 110          // process we failed to acquire the lock.
 111          $this->locked = $this->config->set_atomic($this->config_name, $lock_value, $this->unique_id, false);
 112   
 113          return $this->locked;
 114      }
 115   
 116      /**
 117      * Does this process own the lock?
 118      *
 119      * @return    bool            true if lock is owned
 120      *                            false otherwise
 121      */
 122      public function owns_lock()
 123      {
 124          return (bool) $this->locked;
 125      }
 126   
 127      /**
 128      * Releases the lock.
 129      *
 130      * The lock must have been previously obtained, that is, acquire() call
 131      * was issued and returned true.
 132      *
 133      * Note: Attempting to release a lock that is already released,
 134      * that is, calling release() multiple times, is harmless.
 135      *
 136      * @return null
 137      */
 138      public function release()
 139      {
 140          if ($this->locked)
 141          {
 142              $this->config->set_atomic($this->config_name, $this->unique_id, '0', false);
 143              $this->locked = false;
 144          }
 145      }
 146  }
 147