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

PdoProfilerStorage.php

Zuletzt modifiziert: 09.10.2024, 12:58 - Dateigröße: 7.62 KiB


 001  <?php
 002   
 003  /*
 004   * This file is part of the Symfony package.
 005   *
 006   * (c) Fabien Potencier <fabien@symfony.com>
 007   *
 008   * For the full copyright and license information, please view the LICENSE
 009   * file that was distributed with this source code.
 010   */
 011   
 012  namespace Symfony\Component\HttpKernel\Profiler;
 013   
 014  /**
 015   * Base PDO storage for profiling information in a PDO database.
 016   *
 017   * @author Fabien Potencier <fabien@symfony.com>
 018   * @author Jan Schumann <js@schumann-it.com>
 019   */
 020  abstract class PdoProfilerStorage implements ProfilerStorageInterface
 021  {
 022      protected $dsn;
 023      protected $username;
 024      protected $password;
 025      protected $lifetime;
 026      protected $db;
 027   
 028      /**
 029       * Constructor.
 030       *
 031       * @param string  $dsn      A data source name
 032       * @param string  $username The username for the database
 033       * @param string  $password The password for the database
 034       * @param int     $lifetime The lifetime to use for the purge
 035       */
 036      public function __construct($dsn, $username = '', $password = '', $lifetime = 86400)
 037      {
 038          $this->dsn = $dsn;
 039          $this->username = $username;
 040          $this->password = $password;
 041          $this->lifetime = (int) $lifetime;
 042      }
 043   
 044      /**
 045       * {@inheritdoc}
 046       */
 047      public function find($ip, $url, $limit, $method, $start = null, $end = null)
 048      {
 049          if (null === $start) {
 050              $start = 0;
 051          }
 052   
 053          if (null === $end) {
 054              $end = time();
 055          }
 056   
 057          list($criteria, $args) = $this->buildCriteria($ip, $url, $start, $end, $limit, $method);
 058   
 059          $criteria = $criteria ? 'WHERE '.implode(' AND ', $criteria) : '';
 060   
 061          $db = $this->initDb();
 062          $tokens = $this->fetch($db, 'SELECT token, ip, method, url, time, parent FROM sf_profiler_data '.$criteria.' ORDER BY time DESC LIMIT '.((int) $limit), $args);
 063          $this->close($db);
 064   
 065          return $tokens;
 066      }
 067   
 068      /**
 069       * {@inheritdoc}
 070       */
 071      public function read($token)
 072      {
 073          $db = $this->initDb();
 074          $args = array(':token' => $token);
 075          $data = $this->fetch($db, 'SELECT data, parent, ip, method, url, time FROM sf_profiler_data WHERE token = :token LIMIT 1', $args);
 076          $this->close($db);
 077          if (isset($data[0]['data'])) {
 078              return $this->createProfileFromData($token, $data[0]);
 079          }
 080      }
 081   
 082      /**
 083       * {@inheritdoc}
 084       */
 085      public function write(Profile $profile)
 086      {
 087          $db = $this->initDb();
 088          $args = array(
 089              ':token'      => $profile->getToken(),
 090              ':parent'     => $profile->getParentToken(),
 091              ':data'       => base64_encode(serialize($profile->getCollectors())),
 092              ':ip'         => $profile->getIp(),
 093              ':method'     => $profile->getMethod(),
 094              ':url'        => $profile->getUrl(),
 095              ':time'       => $profile->getTime(),
 096              ':created_at' => time(),
 097          );
 098   
 099          try {
 100              if ($this->has($profile->getToken())) {
 101                  $this->exec($db, 'UPDATE sf_profiler_data SET parent = :parent, data = :data, ip = :ip, method = :method, url = :url, time = :time, created_at = :created_at WHERE token = :token', $args);
 102              } else {
 103                  $this->exec($db, 'INSERT INTO sf_profiler_data (token, parent, data, ip, method, url, time, created_at) VALUES (:token, :parent, :data, :ip, :method, :url, :time, :created_at)', $args);
 104              }
 105              $this->cleanup();
 106              $status = true;
 107          } catch (\Exception $e) {
 108              $status = false;
 109          }
 110   
 111          $this->close($db);
 112   
 113          return $status;
 114      }
 115   
 116      /**
 117       * {@inheritdoc}
 118       */
 119      public function purge()
 120      {
 121          $db = $this->initDb();
 122          $this->exec($db, 'DELETE FROM sf_profiler_data');
 123          $this->close($db);
 124      }
 125   
 126      /**
 127       * Build SQL criteria to fetch records by ip and url
 128       *
 129       * @param string $ip     The IP
 130       * @param string $url    The URL
 131       * @param string $start  The start period to search from
 132       * @param string $end    The end period to search to
 133       * @param string $limit  The maximum number of tokens to return
 134       * @param string $method The request method
 135       *
 136       * @return array An array with (criteria, args)
 137       */
 138      abstract protected function buildCriteria($ip, $url, $start, $end, $limit, $method);
 139   
 140      /**
 141       * Initializes the database
 142       *
 143       * @throws \RuntimeException When the requested database driver is not installed
 144       */
 145      abstract protected function initDb();
 146   
 147      protected function cleanup()
 148      {
 149          $db = $this->initDb();
 150          $this->exec($db, 'DELETE FROM sf_profiler_data WHERE created_at < :time', array(':time' => time() - $this->lifetime));
 151          $this->close($db);
 152      }
 153   
 154      protected function exec($db, $query, array $args = array())
 155      {
 156          $stmt = $this->prepareStatement($db, $query);
 157   
 158          foreach ($args as $arg => $val) {
 159              $stmt->bindValue($arg, $val, is_int($val) ? \PDO::PARAM_INT : \PDO::PARAM_STR);
 160          }
 161          $success = $stmt->execute();
 162          if (!$success) {
 163              throw new \RuntimeException(sprintf('Error executing query "%s"', $query));
 164          }
 165      }
 166   
 167      protected function prepareStatement($db, $query)
 168      {
 169          try {
 170              $stmt = $db->prepare($query);
 171          } catch (\Exception $e) {
 172              $stmt = false;
 173          }
 174   
 175          if (false === $stmt) {
 176              throw new \RuntimeException('The database cannot successfully prepare the statement');
 177          }
 178   
 179          return $stmt;
 180      }
 181   
 182      protected function fetch($db, $query, array $args = array())
 183      {
 184          $stmt = $this->prepareStatement($db, $query);
 185   
 186          foreach ($args as $arg => $val) {
 187              $stmt->bindValue($arg, $val, is_int($val) ? \PDO::PARAM_INT : \PDO::PARAM_STR);
 188          }
 189          $stmt->execute();
 190          $return = $stmt->fetchAll(\PDO::FETCH_ASSOC);
 191   
 192          return $return;
 193      }
 194   
 195      protected function close($db)
 196      {
 197      }
 198   
 199      protected function createProfileFromData($token, $data, $parent = null)
 200      {
 201          $profile = new Profile($token);
 202          $profile->setIp($data['ip']);
 203          $profile->setMethod($data['method']);
 204          $profile->setUrl($data['url']);
 205          $profile->setTime($data['time']);
 206          $profile->setCollectors(unserialize(base64_decode($data['data'])));
 207   
 208          if (!$parent && !empty($data['parent'])) {
 209              $parent = $this->read($data['parent']);
 210          }
 211   
 212          if ($parent) {
 213              $profile->setParent($parent);
 214          }
 215   
 216          $profile->setChildren($this->readChildren($token, $profile));
 217   
 218          return $profile;
 219      }
 220   
 221      /**
 222       * Reads the child profiles for the given token.
 223       *
 224       * @param string $token  The parent token
 225       * @param string $parent The parent instance
 226       *
 227       * @return Profile[] An array of Profile instance
 228       */
 229      protected function readChildren($token, $parent)
 230      {
 231          $db = $this->initDb();
 232          $data = $this->fetch($db, 'SELECT token, data, ip, method, url, time FROM sf_profiler_data WHERE parent = :token', array(':token' => $token));
 233          $this->close($db);
 234   
 235          if (!$data) {
 236              return array();
 237          }
 238   
 239          $profiles = array();
 240          foreach ($data as $d) {
 241              $profiles[] = $this->createProfileFromData($d['token'], $d, $parent);
 242          }
 243   
 244          return $profiles;
 245      }
 246   
 247      /**
 248       * Returns whether data for the given token already exists in storage.
 249       *
 250       * @param string $token The profile token
 251       *
 252       * @return string
 253       */
 254      protected function has($token)
 255      {
 256          $db = $this->initDb();
 257          $tokenExists = $this->fetch($db, 'SELECT 1 FROM sf_profiler_data WHERE token = :token LIMIT 1', array(':token' => $token));
 258          $this->close($db);
 259   
 260          return !empty($tokenExists);
 261      }
 262  }
 263