PluginProbe
ManageWP Worker / 4.9.36
ManageWP Worker v4.9.36
4.9.38 4.9.37 4.9.36 4.9.35 4.9.34 3.8.7 3.8.8 3.9.0 3.9.1 3.9.10 3.9.11 3.9.12 3.9.13 3.9.14 3.9.15 3.9.16 3.9.17 3.9.18 3.9.19 3.9.2 3.9.20 3.9.21 3.9.22 3.9.23 3.9.24 All 73 releases
worker / src / MWP / IncrementalBackup / Database / PdoConnection.php

PdoConnection.php in ManageWP Worker 4.9.36, at src/MWP/IncrementalBackup/Database/PdoConnection.php

109 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 * This file is part of the ManageWP Worker plugin.
4 *
5 * (c) ManageWP LLC <[email protected]>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10
11 class MWP_IncrementalBackup_Database_PdoConnection implements MWP_IncrementalBackup_Database_ConnectionInterface
12 {
13
14 /**
15 * @var MWP_IncrementalBackup_Database_Configuration
16 */
17 private $configuration;
18
19 /**
20 * @var PDO
21 */
22 private $connection;
23
24 public function __construct(MWP_IncrementalBackup_Database_Configuration $configuration)
25 {
26 $this->configuration = $configuration;
27
28 if (!extension_loaded('pdo_mysql')) {
29 throw new MWP_IncrementalBackup_Database_Exception_ConnectionException("PDO extension is disabled.");
30 }
31
32 $options = array(
33 PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
34 PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
35 );
36
37 // Mute the constructor since this error doesn't get thrown as an exception, but as a PHP warning:
38 // Warning: PDO::__construct(): The server requested authentication method unknown to the client [mysql_old_password]
39 $this->connection = @new PDO(self::getDsn($this->configuration), $this->configuration->getUsername(), $this->configuration->getPassword(), $options);
40
41 $this->connection->exec('SET NAMES '.$configuration->getCharset());
42 }
43
44 /**
45 * {@inheritdoc}
46 */
47 public function query($query, $useResult = false)
48 {
49 /** @handled constant */
50 $previousAttr = $this->connection->getAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY);
51
52 if ($useResult) {
53 // Temporarily switch to unbuffered queries
54 /** @handled constant */
55 $this->connection->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
56 }
57
58 $statement = new MWP_IncrementalBackup_Database_PdoStatement($this->connection->query($query));
59
60 if ($useResult) {
61 // Restore configuration
62 /** @handled constant */
63 $this->connection->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, $previousAttr);
64 }
65
66 return $statement;
67 }
68
69 /**
70 * {@inheritdoc}
71 */
72 public function quote($value)
73 {
74 return $this->connection->quote($value);
75 }
76
77 /**
78 * @param MWP_IncrementalBackup_Database_Configuration $configuration
79 *
80 * @return string
81 */
82 private static function getDsn(MWP_IncrementalBackup_Database_Configuration $configuration)
83 {
84 $pdoParameters = array(
85 'dbname' => $configuration->getDatabase(),
86 'charset' => $configuration->getCharset(),
87 );
88
89 if ($configuration->isSocket()) {
90 $pdoParameters['unix_socket'] = $configuration->getSocketPath();
91 } else {
92 $pdoParameters['host'] = $configuration->getHost();
93
94 if (($port = $configuration->getPort()) !== null) {
95 $pdoParameters['port'] = $configuration->getPort();
96 }
97 }
98
99 $parameters = array();
100 foreach ($pdoParameters as $name => $value) {
101 $parameters[] = $name.'='.$value;
102 }
103
104 $dsn = sprintf("mysql:%s", implode(';', $parameters));
105
106 return $dsn;
107 }
108 }
109