QueryBuilder
2 years ago
DbInfo.php
2 years ago
ExcludedTables.php
4 years ago
OptionPreservationHandler.php
2 years ago
SearchReplace.php
2 years ago
SelectedTables.php
2 years ago
TableDto.php
5 years ago
TableService.php
2 years ago
TablesRenamer.php
2 years ago
WpDbInfo.php
2 years ago
WpOptionsInfo.php
2 years ago
iDbInfo.php
2 years ago
DbInfo.php
85 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Framework\Database; |
| 4 | |
| 5 | use Exception; |
| 6 | use mysqli; |
| 7 | use wpdb; |
| 8 | use WPStaging\Framework\Adapter\Database\DatabaseException; |
| 9 | |
| 10 | class DbInfo extends WpDbInfo |
| 11 | { |
| 12 | /** |
| 13 | * @var string |
| 14 | */ |
| 15 | protected $server; |
| 16 | |
| 17 | /** |
| 18 | * @var string |
| 19 | */ |
| 20 | protected $user; |
| 21 | |
| 22 | /** |
| 23 | * @var string |
| 24 | */ |
| 25 | protected $password; |
| 26 | |
| 27 | /** |
| 28 | * @var string |
| 29 | */ |
| 30 | protected $database; |
| 31 | |
| 32 | /** |
| 33 | * @var bool |
| 34 | */ |
| 35 | protected $useSsl; |
| 36 | |
| 37 | /** |
| 38 | * @param string $hostServer |
| 39 | * @param string $user |
| 40 | * @param string $password |
| 41 | * @param string $database |
| 42 | * @param bool $useSsl |
| 43 | * @throws DatabaseException |
| 44 | */ |
| 45 | public function __construct(string $hostServer, string $user, string $password, string $database, bool $useSsl = false) |
| 46 | { |
| 47 | $this->server = $hostServer; |
| 48 | $this->user = $user; |
| 49 | $this->password = $password; |
| 50 | $this->database = $database; |
| 51 | $this->useSsl = $useSsl; |
| 52 | |
| 53 | parent::__construct($this->connect()); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * @return wpdb |
| 58 | * @throws DatabaseException |
| 59 | */ |
| 60 | public function connect() |
| 61 | { |
| 62 | if ($this->useSsl) { |
| 63 | // wpdb requires this constant for SSL use |
| 64 | if (!defined('MYSQL_CLIENT_FLAGS')) { |
| 65 | // phpcs:disable PHPCompatibility.Constants.NewConstants.mysqli_client_ssl_dont_verify_server_certFound |
| 66 | define('MYSQL_CLIENT_FLAGS', MYSQLI_CLIENT_SSL | MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT); |
| 67 | } |
| 68 | |
| 69 | $db = mysqli_init(); |
| 70 | $db->real_connect($this->server, $this->user, $this->password, $this->database, null, null, MYSQL_CLIENT_FLAGS); |
| 71 | } else { |
| 72 | $db = new mysqli($this->server, $this->user, $this->password, $this->database); |
| 73 | } |
| 74 | |
| 75 | if ($db->connect_error) { |
| 76 | throw new DatabaseException('Connect Error (' . $db->connect_errno . ') ' . $db->connect_error); |
| 77 | } |
| 78 | |
| 79 | $db->close(); |
| 80 | |
| 81 | $wpdb = new wpdb($this->user, $this->password, $this->database, $this->server); |
| 82 | return $wpdb; |
| 83 | } |
| 84 | } |
| 85 |