| 1 |
<?php |
| 2 |
namespace MailPoet\Config; |
| 3 |
|
| 4 |
use ORM as ORM; |
| 5 |
use PDO as PDO; |
| 6 |
|
| 7 |
if(!defined('ABSPATH')) exit; |
| 8 |
|
| 9 |
require_once(ABSPATH . 'wp-admin/includes/plugin.php'); |
| 10 |
|
| 11 |
class Database { |
| 12 |
public $driver_option_wait_timeout = 60; |
| 13 |
|
| 14 |
function init() { |
| 15 |
$this->setupConnection(); |
| 16 |
$this->setupLogging(); |
| 17 |
$this->setupDriverOptions(); |
| 18 |
$this->defineTables(); |
| 19 |
} |
| 20 |
|
| 21 |
function setupConnection() { |
| 22 |
ORM::configure(Env::$db_source_name); |
| 23 |
ORM::configure('username', Env::$db_username); |
| 24 |
ORM::configure('password', Env::$db_password); |
| 25 |
} |
| 26 |
|
| 27 |
function setupLogging() { |
| 28 |
ORM::configure('logging', WP_DEBUG); |
| 29 |
} |
| 30 |
|
| 31 |
function setupDriverOptions() { |
| 32 |
$driver_options = array( |
| 33 |
'TIME_ZONE = "' . Env::$db_timezone_offset . '"', |
| 34 |
'sql_mode=(SELECT REPLACE(@@sql_mode,"ONLY_FULL_GROUP_BY",""))', |
| 35 |
); |
| 36 |
|
| 37 |
if(!empty(Env::$db_charset)) { |
| 38 |
$character_set = 'NAMES ' . Env::$db_charset; |
| 39 |
if(!empty(Env::$db_collation)) { |
| 40 |
$character_set .= ' COLLATE ' . Env::$db_collation; |
| 41 |
} |
| 42 |
$driver_options[] = $character_set; |
| 43 |
} |
| 44 |
|
| 45 |
ORM::configure('driver_options', array( |
| 46 |
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET ' . implode(', ', $driver_options) |
| 47 |
)); |
| 48 |
|
| 49 |
try { |
| 50 |
$current_options = ORM::for_table("") |
| 51 |
->raw_query('SELECT @@session.wait_timeout as wait_timeout') |
| 52 |
->findOne(); |
| 53 |
if($current_options && (int)$current_options->wait_timeout < $this->driver_option_wait_timeout) { |
| 54 |
ORM::raw_execute('SET SESSION wait_timeout = ' . $this->driver_option_wait_timeout); |
| 55 |
} |
| 56 |
} catch (\PDOException $e) { |
| 57 |
// Rethrow PDOExceptions to prevent exposing sensitive data in stack traces |
| 58 |
throw new \Exception($e->getMessage()); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
function defineTables() { |
| 63 |
if(!defined('MP_SETTINGS_TABLE')) { |
| 64 |
$settings = Env::$db_prefix . 'settings'; |
| 65 |
$segments = Env::$db_prefix . 'segments'; |
| 66 |
$forms = Env::$db_prefix . 'forms'; |
| 67 |
$custom_fields = Env::$db_prefix . 'custom_fields'; |
| 68 |
$subscribers = Env::$db_prefix . 'subscribers'; |
| 69 |
$subscriber_segment = Env::$db_prefix . 'subscriber_segment'; |
| 70 |
$subscriber_custom_field = Env::$db_prefix . 'subscriber_custom_field'; |
| 71 |
$subscriber_ips = Env::$db_prefix . 'subscriber_ips'; |
| 72 |
$newsletter_segment = Env::$db_prefix . 'newsletter_segment'; |
| 73 |
$scheduled_tasks = Env::$db_prefix . 'scheduled_tasks'; |
| 74 |
$scheduled_task_subscribers = Env::$db_prefix . 'scheduled_task_subscribers'; |
| 75 |
$sending_queues = Env::$db_prefix . 'sending_queues'; |
| 76 |
$newsletters = Env::$db_prefix . 'newsletters'; |
| 77 |
$newsletter_templates = Env::$db_prefix . 'newsletter_templates'; |
| 78 |
$newsletter_option_fields = Env::$db_prefix . 'newsletter_option_fields'; |
| 79 |
$newsletter_option = Env::$db_prefix . 'newsletter_option'; |
| 80 |
$newsletter_links = Env::$db_prefix . 'newsletter_links'; |
| 81 |
$newsletter_posts = Env::$db_prefix . 'newsletter_posts'; |
| 82 |
$statistics_newsletters = Env::$db_prefix . 'statistics_newsletters'; |
| 83 |
$statistics_clicks = Env::$db_prefix . 'statistics_clicks'; |
| 84 |
$statistics_opens = Env::$db_prefix . 'statistics_opens'; |
| 85 |
$statistics_unsubscribes = Env::$db_prefix . 'statistics_unsubscribes'; |
| 86 |
$statistics_forms = Env::$db_prefix . 'statistics_forms'; |
| 87 |
$mapping_to_external_entities = Env::$db_prefix . 'mapping_to_external_entities'; |
| 88 |
$log = Env::$db_prefix . 'log'; |
| 89 |
|
| 90 |
define('MP_SETTINGS_TABLE', $settings); |
| 91 |
define('MP_SEGMENTS_TABLE', $segments); |
| 92 |
define('MP_FORMS_TABLE', $forms); |
| 93 |
define('MP_CUSTOM_FIELDS_TABLE', $custom_fields); |
| 94 |
define('MP_SUBSCRIBERS_TABLE', $subscribers); |
| 95 |
define('MP_SUBSCRIBER_SEGMENT_TABLE', $subscriber_segment); |
| 96 |
define('MP_SUBSCRIBER_CUSTOM_FIELD_TABLE', $subscriber_custom_field); |
| 97 |
define('MP_SUBSCRIBER_IPS_TABLE', $subscriber_ips); |
| 98 |
define('MP_SCHEDULED_TASKS_TABLE', $scheduled_tasks); |
| 99 |
define('MP_SCHEDULED_TASK_SUBSCRIBERS_TABLE', $scheduled_task_subscribers); |
| 100 |
define('MP_SENDING_QUEUES_TABLE', $sending_queues); |
| 101 |
define('MP_NEWSLETTERS_TABLE', $newsletters); |
| 102 |
define('MP_NEWSLETTER_TEMPLATES_TABLE', $newsletter_templates); |
| 103 |
define('MP_NEWSLETTER_SEGMENT_TABLE', $newsletter_segment); |
| 104 |
define('MP_NEWSLETTER_OPTION_FIELDS_TABLE', $newsletter_option_fields); |
| 105 |
define('MP_NEWSLETTER_LINKS_TABLE', $newsletter_links); |
| 106 |
define('MP_NEWSLETTER_POSTS_TABLE', $newsletter_posts); |
| 107 |
define('MP_NEWSLETTER_OPTION_TABLE', $newsletter_option); |
| 108 |
define('MP_STATISTICS_NEWSLETTERS_TABLE', $statistics_newsletters); |
| 109 |
define('MP_STATISTICS_CLICKS_TABLE', $statistics_clicks); |
| 110 |
define('MP_STATISTICS_OPENS_TABLE', $statistics_opens); |
| 111 |
define('MP_STATISTICS_UNSUBSCRIBES_TABLE', $statistics_unsubscribes); |
| 112 |
define('MP_STATISTICS_FORMS_TABLE', $statistics_forms); |
| 113 |
define('MP_MAPPING_TO_EXTERNAL_ENTITIES_TABLE', $mapping_to_external_entities); |
| 114 |
define('MP_LOG_TABLE', $log); |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|