Skip to content

Commit

Permalink
Merge pull request #555 from haraldpdl/db_connection
Browse files Browse the repository at this point in the history
Db connection
  • Loading branch information
haraldpdl authored Nov 23, 2016
2 parents 99fbb60 + 76e25af commit 0f3a138
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 19 deletions.
29 changes: 26 additions & 3 deletions catalog/includes/OSC/OM/Db.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@ class Db extends \PDO
protected $table_prefix;
protected $port;
protected $driver_options = [];
protected $options = [];

public static function initialize(
$server = null,
$username = null,
$password = null,
$database = null,
$port = null,
array $driver_options = []
array $driver_options = null,
array $options = null
) {
if (!isset($server)) {
$server = OSCOM::getConfig('db_server');
Expand All @@ -46,6 +48,10 @@ public static function initialize(
$database = OSCOM::getConfig('db_database');
}

if (!is_array($driver_options)) {
$driver_options = [];
}

if (!isset($driver_options[\PDO::ATTR_ERRMODE])) {
$driver_options[\PDO::ATTR_ERRMODE] = \PDO::ERRMODE_EXCEPTION;
}
Expand All @@ -58,8 +64,25 @@ public static function initialize(
$driver_options[\PDO::ATTR_STATEMENT_CLASS] = array('OSC\OM\DbStatement');
}

$class = 'OSC\OM\Db\MySQL';
$object = new $class($server, $username, $password, $database, $port, $driver_options);
if (!is_array($options)) {
$options = [];
}

$object = false;

try {
$class = 'OSC\OM\Db\MySQL';
$object = new $class($server, $username, $password, $database, $port, $driver_options, $options);
} catch (\Exception $e) {
$message = $e->getMessage();
// $message .= "\n" . $e->getTraceAsString(); // the trace will contain the password in plain text

if (!isset($options['log_errors']) || ($options['log_errors'] === true)) {
error_log('OSC\OM\Db::initialize(): ' . $message);
}

throw new \Exception($message, $e->getCode());
}

return $object;
}
Expand Down
3 changes: 2 additions & 1 deletion catalog/includes/OSC/OM/Db/MySQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@

class MySQL extends \OSC\OM\Db
{
public function __construct($server, $username, $password, $database, $port, $driver_options)
public function __construct($server, $username, $password, $database, $port, $driver_options, $options)
{
$this->server = $server;
$this->username = $username;
$this->password = $password;
$this->database = $database;
$this->port = $port;
$this->driver_options = $driver_options;
$this->options = $options;

if (!isset($this->driver_options[\PDO::MYSQL_ATTR_INIT_COMMAND])) {
// STRICT_ALL_TABLES 5.0.2
Expand Down
14 changes: 8 additions & 6 deletions catalog/includes/OSC/OM/OSCOM.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,6 @@ public static function isRPC()

public static function link($page, $parameters = null, $add_session_id = true, $search_engine_safe = true)
{
$OSCOM_Session = Registry::get('Session');

$page = HTML::sanitize($page);

$site = $req_site = static::$site;
Expand Down Expand Up @@ -170,17 +168,21 @@ public static function link($page, $parameters = null, $add_session_id = true, $
}

// Add the session ID when moving from different HTTP and HTTPS servers, or when SID is defined
if (($add_session_id == true) && $OSCOM_Session->hasStarted() && ($OSCOM_Session->isForceCookies() === false)) {
if ((strlen(SID) > 0) || (((HTTP::getRequestType() == 'NONSSL') && (parse_url(static::getConfig('http_server', $req_site), PHP_URL_SCHEME) == 'https')) || ((HTTP::getRequestType() == 'SSL') && (parse_url(static::getConfig('http_server', $req_site), PHP_URL_SCHEME) == 'http')))) {
$link .= $separator . HTML::sanitize(session_name() . '=' . session_id());
if (($add_session_id == true) && Registry::exists('Session')) {
$OSCOM_Session = Registry::get('Session');

if ($OSCOM_Session->hasStarted() && ($OSCOM_Session->isForceCookies() === false)) {
if ((strlen(SID) > 0) || (((HTTP::getRequestType() == 'NONSSL') && (parse_url(static::getConfig('http_server', $req_site), PHP_URL_SCHEME) == 'https')) || ((HTTP::getRequestType() == 'SSL') && (parse_url(static::getConfig('http_server', $req_site), PHP_URL_SCHEME) == 'http')))) {
$link .= $separator . HTML::sanitize(session_name() . '=' . session_id());
}
}
}

while (strpos($link, '&&') !== false) {
$link = str_replace('&&', '&', $link);
}

if ((SEARCH_ENGINE_FRIENDLY_URLS == 'true') && ($search_engine_safe == true)) {
if (($search_engine_safe == true) && defined('SEARCH_ENGINE_FRIENDLY_URLS') && (SEARCH_ENGINE_FRIENDLY_URLS == 'true')) {
$link = str_replace(['?', '&', '='], '/', $link);
}

Expand Down
9 changes: 7 additions & 2 deletions catalog/includes/OSC/Sites/Admin/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,13 @@ protected function init()
$OSCOM_Cookies = new Cookies();
Registry::set('Cookies', $OSCOM_Cookies);

$OSCOM_Db = Db::initialize();
Registry::set('Db', $OSCOM_Db);
try {
$OSCOM_Db = Db::initialize();
Registry::set('Db', $OSCOM_Db);
} catch (\Exception $e) {
include(OSCOM::getConfig('dir_root', 'Shop') . 'includes/error_documents/maintenance.php');
exit;
}

Registry::set('Hooks', new Hooks());

Expand Down
9 changes: 7 additions & 2 deletions catalog/includes/OSC/Sites/Shop/Shop.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,13 @@ protected function init()
$OSCOM_Cookies = new Cookies();
Registry::set('Cookies', $OSCOM_Cookies);

$OSCOM_Db = Db::initialize();
Registry::set('Db', $OSCOM_Db);
try {
$OSCOM_Db = Db::initialize();
Registry::set('Db', $OSCOM_Db);
} catch (\Exception $e) {
include(OSCOM::getConfig('dir_root') . 'includes/error_documents/maintenance.php');
exit;
}

Registry::set('Hooks', new Hooks());

Expand Down
24 changes: 21 additions & 3 deletions catalog/includes/error_documents/404.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,26 @@
http_response_code(404);
?>

<h1>Error - Page Not Found (404)</h1>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Error - Page Not Found</title>
<link href="<?= OSCOM::link('Shop/ext/bootstrap/css/bootstrap.min.css', null, false); ?>" rel="stylesheet">
<link href="<?= OSCOM::link('Shop/ext/font-awesome/4.7.0/css/font-awesome.min.css', null, false); ?>" rel="stylesheet">
<script src="<?= OSCOM::link('Shop/ext/jquery/jquery-3.1.1.min.js', null, false); ?>"></script>
</head>
<body>
<div class="container">
<div class="jumbotron" style="margin-top: 40px;">
<h1>This Page is Missing</h1>

<div>
<?php echo HTML::button('Continue', 'glyphicon glyphicon-chevron-right', OSCOM::link('index.php')); ?>
<p>It looks like this page is missing. Please continue back to our website and try again.</p>

<p style="margin-top: 40px;"><?= HTML::button('Return to website', 'fa fa-chevron-right', OSCOM::link('index.php'), null, 'btn-primary'); ?></p>
</div>
</div>
<script src="<?= OSCOM::link('Shop/ext/bootstrap/js/bootstrap.min.js', null, false); ?>"></script>
</body>
</html>
38 changes: 38 additions & 0 deletions catalog/includes/error_documents/maintenance.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* osCommerce Online Merchant
*
* @copyright (c) 2016 osCommerce; https://www.oscommerce.com
* @license MIT; https://www.oscommerce.com/license/mit.txt
*/

use OSC\OM\HTML;
use OSC\OM\OSCOM;

http_response_code(503);
header('Retry-After: 300');
?>

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Maintenance</title>
<link href="<?= OSCOM::link('Shop/ext/bootstrap/css/bootstrap.min.css', null, false); ?>" rel="stylesheet">
<link href="<?= OSCOM::link('Shop/ext/font-awesome/4.7.0/css/font-awesome.min.css', null, false); ?>" rel="stylesheet">
<script src="<?= OSCOM::link('Shop/ext/jquery/jquery-3.1.1.min.js', null, false); ?>"></script>
</head>
<body>
<div class="container">
<div class="jumbotron" style="margin-top: 40px;">
<h1>We'll be back soon!</h1>

<p>We're currently working on and improving our website. We'll be back in a few moments..</p>

<p style="margin-top: 40px;"><?= HTML::button('Return to website', 'fa fa-refresh', OSCOM::link('index.php'), null, 'btn-primary'); ?></p>
</div>
</div>
<script src="<?= OSCOM::link('Shop/ext/bootstrap/js/bootstrap.min.js', null, false); ?>"></script>
</body>
</html>
4 changes: 2 additions & 2 deletions catalog/install/rpc.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@

case 'dbCheck':
try {
$OSCOM_Db = Db::initialize(isset($_POST['server']) ? $_POST['server'] : '', isset($_POST['username']) ? $_POST['username'] : '', isset($_POST['password']) ? $_POST['password'] : '', isset($_POST['name']) ? $_POST['name'] : '');
$OSCOM_Db = Db::initialize(isset($_POST['server']) ? $_POST['server'] : '', isset($_POST['username']) ? $_POST['username'] : '', isset($_POST['password']) ? $_POST['password'] : '', isset($_POST['name']) ? $_POST['name'] : '', null, null, ['log_errors' => false]);

$result['status'] = '1';
$result['message'] = 'success';
Expand All @@ -71,7 +71,7 @@

if (($e->getCode() == '1049') && isset($_GET['createDb']) && ($_GET['createDb'] == 'true')) {
try {
$OSCOM_Db = Db::initialize($_POST['server'], $_POST['username'], $_POST['password'], '');
$OSCOM_Db = Db::initialize($_POST['server'], $_POST['username'], $_POST['password'], '', null, null, ['log_errors' => false]);

$OSCOM_Db->exec('create database ' . Db::prepareIdentifier($_POST['name']) . ' character set utf8 collate utf8_unicode_ci');

Expand Down

0 comments on commit 0f3a138

Please sign in to comment.