diff options
Diffstat (limited to 'public/catalog/controller/startup')
-rw-r--r-- | public/catalog/controller/startup/error.php | 43 | ||||
-rw-r--r-- | public/catalog/controller/startup/event.php | 13 | ||||
-rw-r--r-- | public/catalog/controller/startup/maintenance.php | 25 | ||||
-rw-r--r-- | public/catalog/controller/startup/router.php | 36 | ||||
-rw-r--r-- | public/catalog/controller/startup/sass.php | 27 | ||||
-rw-r--r-- | public/catalog/controller/startup/seo_url.php | 127 | ||||
-rw-r--r-- | public/catalog/controller/startup/session.php | 28 | ||||
-rw-r--r-- | public/catalog/controller/startup/startup.php | 194 |
8 files changed, 493 insertions, 0 deletions
diff --git a/public/catalog/controller/startup/error.php b/public/catalog/controller/startup/error.php new file mode 100644 index 0000000..6e9052f --- /dev/null +++ b/public/catalog/controller/startup/error.php @@ -0,0 +1,43 @@ +<?php +class ControllerStartupError extends Controller { + public function index() { + $this->registry->set('log', new Log($this->config->get('config_error_filename'))); + + set_error_handler(array($this, 'handler')); + } + + public function handler($code, $message, $file, $line) { + // error suppressed with @ + if (error_reporting() === 0) { + return false; + } + + switch ($code) { + case E_NOTICE: + case E_USER_NOTICE: + $error = 'Notice'; + break; + case E_WARNING: + case E_USER_WARNING: + $error = 'Warning'; + break; + case E_ERROR: + case E_USER_ERROR: + $error = 'Fatal Error'; + break; + default: + $error = 'Unknown'; + break; + } + + if ($this->config->get('config_error_display')) { + echo '<b>' . $error . '</b>: ' . $message . ' in <b>' . $file . '</b> on line <b>' . $line . '</b>'; + } + + if ($this->config->get('config_error_log')) { + $this->log->write('PHP ' . $error . ': ' . $message . ' in ' . $file . ' on line ' . $line); + } + + return true; + } +}
\ No newline at end of file diff --git a/public/catalog/controller/startup/event.php b/public/catalog/controller/startup/event.php new file mode 100644 index 0000000..dc42816 --- /dev/null +++ b/public/catalog/controller/startup/event.php @@ -0,0 +1,13 @@ +<?php +class ControllerStartupEvent extends Controller { + public function index() { + // Add events from the DB + $this->load->model('setting/event'); + + $results = $this->model_setting_event->getEvents(); + + foreach ($results as $result) { + $this->event->register(substr($result['trigger'], strpos($result['trigger'], '/') + 1), new Action($result['action']), $result['sort_order']); + } + } +}
\ No newline at end of file diff --git a/public/catalog/controller/startup/maintenance.php b/public/catalog/controller/startup/maintenance.php new file mode 100644 index 0000000..7c09cb0 --- /dev/null +++ b/public/catalog/controller/startup/maintenance.php @@ -0,0 +1,25 @@ +<?php +class ControllerStartupMaintenance extends Controller { + public function index() { + if ($this->config->get('config_maintenance')) { + // Route + if (isset($this->request->get['route']) && $this->request->get['route'] != 'startup/router') { + $route = $this->request->get['route']; + } else { + $route = $this->config->get('action_default'); + } + + $ignore = array( + 'common/language/language', + 'common/currency/currency' + ); + + // Show site if logged in as admin + $this->user = new Cart\User($this->registry); + + if ((substr($route, 0, 17) != 'extension/payment' && substr($route, 0, 3) != 'api') && !in_array($route, $ignore) && !$this->user->isLogged()) { + return new Action('common/maintenance'); + } + } + } +} diff --git a/public/catalog/controller/startup/router.php b/public/catalog/controller/startup/router.php new file mode 100644 index 0000000..426466d --- /dev/null +++ b/public/catalog/controller/startup/router.php @@ -0,0 +1,36 @@ +<?php +class ControllerStartupRouter extends Controller { + public function index() { + // Route + if (isset($this->request->get['route']) && $this->request->get['route'] != 'startup/router') { + $route = $this->request->get['route']; + } else { + $route = $this->config->get('action_default'); + } + + // Sanitize the call + $route = preg_replace('/[^a-zA-Z0-9_\/]/', '', (string)$route); + + // Trigger the pre events + $result = $this->event->trigger('controller/' . $route . '/before', array(&$route, &$data)); + + if (!is_null($result)) { + return $result; + } + + // We dont want to use the loader class as it would make an controller callable. + $action = new Action($route); + + // Any output needs to be another Action object. + $output = $action->execute($this->registry); + + // Trigger the post events + $result = $this->event->trigger('controller/' . $route . '/after', array(&$route, &$data, &$output)); + + if (!is_null($result)) { + return $result; + } + + return $output; + } +} diff --git a/public/catalog/controller/startup/sass.php b/public/catalog/controller/startup/sass.php new file mode 100644 index 0000000..2e0fb36 --- /dev/null +++ b/public/catalog/controller/startup/sass.php @@ -0,0 +1,27 @@ +<?php +class ControllerStartupSass extends Controller { + public function index() { + $file = DIR_APPLICATION . 'view/theme/' . $this->config->get('theme_directory') . '/stylesheet/bootstrap.css'; + + if (!is_file($file) || (is_file(DIR_APPLICATION . 'view/theme/' . $this->config->get('theme_directory') . '/stylesheet/sass/_bootstrap.scss') && !$this->config->get('developer_sass'))) { + include_once(DIR_STORAGE . 'vendor/scss.inc.php'); + + $scss = new Scssc(); + $scss->setImportPaths(DIR_APPLICATION . 'view/theme/' . $this->config->get('theme_directory') . '/stylesheet/sass/'); + + $output = $scss->compile('@import "_bootstrap.scss"'); + + $handle = fopen($file, 'w'); + + flock($handle, LOCK_EX); + + fwrite($handle, $output); + + fflush($handle); + + flock($handle, LOCK_UN); + + fclose($handle); + } + } +} diff --git a/public/catalog/controller/startup/seo_url.php b/public/catalog/controller/startup/seo_url.php new file mode 100644 index 0000000..f7690c4 --- /dev/null +++ b/public/catalog/controller/startup/seo_url.php @@ -0,0 +1,127 @@ +<?php +class ControllerStartupSeoUrl extends Controller { + public function index() { + // Add rewrite to url class + if ($this->config->get('config_seo_url')) { + $this->url->addRewrite($this); + } + + // Decode URL + if (isset($this->request->get['_route_'])) { + $parts = explode('/', $this->request->get['_route_']); + + // remove any empty arrays from trailing + if (utf8_strlen(end($parts)) == 0) { + array_pop($parts); + } + + foreach ($parts as $part) { + $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "seo_url WHERE keyword = '" . $this->db->escape($part) . "' AND store_id = '" . (int)$this->config->get('config_store_id') . "'"); + + if ($query->num_rows) { + $url = explode('=', $query->row['query']); + + if ($url[0] == 'product_id') { + $this->request->get['product_id'] = $url[1]; + } + + if ($url[0] == 'category_id') { + if (!isset($this->request->get['path'])) { + $this->request->get['path'] = $url[1]; + } else { + $this->request->get['path'] .= '_' . $url[1]; + } + } + + if ($url[0] == 'manufacturer_id') { + $this->request->get['manufacturer_id'] = $url[1]; + } + + if ($url[0] == 'information_id') { + $this->request->get['information_id'] = $url[1]; + } + + if ($query->row['query'] && $url[0] != 'information_id' && $url[0] != 'manufacturer_id' && $url[0] != 'category_id' && $url[0] != 'product_id') { + $this->request->get['route'] = $query->row['query']; + } + } else { + $this->request->get['route'] = 'error/not_found'; + + break; + } + } + + if (!isset($this->request->get['route'])) { + if (isset($this->request->get['product_id'])) { + $this->request->get['route'] = 'product/product'; + } elseif (isset($this->request->get['path'])) { + $this->request->get['route'] = 'product/category'; + } elseif (isset($this->request->get['manufacturer_id'])) { + $this->request->get['route'] = 'product/manufacturer/info'; + } elseif (isset($this->request->get['information_id'])) { + $this->request->get['route'] = 'information/information'; + } + } + } + } + + public function rewrite($link) { + $url_info = parse_url(str_replace('&', '&', $link)); + + $url = ''; + + $data = array(); + + parse_str($url_info['query'], $data); + + foreach ($data as $key => $value) { + if (isset($data['route'])) { + if (($data['route'] == 'product/product' && $key == 'product_id') || (($data['route'] == 'product/manufacturer/info' || $data['route'] == 'product/product') && $key == 'manufacturer_id') || ($data['route'] == 'information/information' && $key == 'information_id')) { + $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "seo_url WHERE `query` = '" . $this->db->escape($key . '=' . (int)$value) . "' AND store_id = '" . (int)$this->config->get('config_store_id') . "' AND language_id = '" . (int)$this->config->get('config_language_id') . "'"); + + if ($query->num_rows && $query->row['keyword']) { + $url .= '/' . $query->row['keyword']; + + unset($data[$key]); + } + } elseif ($key == 'path') { + $categories = explode('_', $value); + + foreach ($categories as $category) { + $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "seo_url WHERE `query` = 'category_id=" . (int)$category . "' AND store_id = '" . (int)$this->config->get('config_store_id') . "' AND language_id = '" . (int)$this->config->get('config_language_id') . "'"); + + if ($query->num_rows && $query->row['keyword']) { + $url .= '/' . $query->row['keyword']; + } else { + $url = ''; + + break; + } + } + + unset($data[$key]); + } + } + } + + if ($url) { + unset($data['route']); + + $query = ''; + + if ($data) { + foreach ($data as $key => $value) { + $query .= '&' . rawurlencode((string)$key) . '=' . rawurlencode((is_array($value) ? http_build_query($value) : (string)$value)); + } + + if ($query) { + $query = '?' . str_replace('&', '&', trim($query, '&')); + } + } + + return $url_info['scheme'] . '://' . $url_info['host'] . (isset($url_info['port']) ? ':' . $url_info['port'] : '') . str_replace('/index.php', '', $url_info['path']) . $url . $query; + } else { + return $link; + } + } +} diff --git a/public/catalog/controller/startup/session.php b/public/catalog/controller/startup/session.php new file mode 100644 index 0000000..5a2ad04 --- /dev/null +++ b/public/catalog/controller/startup/session.php @@ -0,0 +1,28 @@ +<?php +class ControllerStartupSession extends Controller { + public function index() { + if (isset($this->request->get['api_token']) && isset($this->request->get['route']) && substr($this->request->get['route'], 0, 4) == 'api/') { + $this->db->query("DELETE FROM `" . DB_PREFIX . "api_session` WHERE TIMESTAMPADD(HOUR, 1, date_modified) < NOW()"); + + // Make sure the IP is allowed + $api_query = $this->db->query("SELECT DISTINCT * FROM `" . DB_PREFIX . "api` `a` LEFT JOIN `" . DB_PREFIX . "api_session` `as` ON (a.api_id = as.api_id) LEFT JOIN " . DB_PREFIX . "api_ip `ai` ON (a.api_id = ai.api_id) WHERE a.status = '1' AND `as`.`session_id` = '" . $this->db->escape($this->request->get['api_token']) . "' AND ai.ip = '" . $this->db->escape($this->request->server['REMOTE_ADDR']) . "'"); + + if ($api_query->num_rows) { + $this->session->start($this->request->get['api_token']); + + // keep the session alive + $this->db->query("UPDATE `" . DB_PREFIX . "api_session` SET `date_modified` = NOW() WHERE `api_session_id` = '" . (int)$api_query->row['api_session_id'] . "'"); + } + } else { + if (isset($_COOKIE[$this->config->get('session_name')])) { + $session_id = $_COOKIE[$this->config->get('session_name')]; + } else { + $session_id = ''; + } + + $this->session->start($session_id); + + setcookie($this->config->get('session_name'), $this->session->getId(), ini_get('session.cookie_lifetime'), ini_get('session.cookie_path'), ini_get('session.cookie_domain')); + } + } +}
\ No newline at end of file diff --git a/public/catalog/controller/startup/startup.php b/public/catalog/controller/startup/startup.php new file mode 100644 index 0000000..41b6ec4 --- /dev/null +++ b/public/catalog/controller/startup/startup.php @@ -0,0 +1,194 @@ +<?php +class ControllerStartupStartup extends Controller { + public function index() { + // Store + if ($this->request->server['HTTPS']) { + $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "store WHERE REPLACE(`ssl`, 'www.', '') = '" . $this->db->escape('https://' . str_replace('www.', '', $_SERVER['HTTP_HOST']) . rtrim(dirname($_SERVER['PHP_SELF']), '/.\\') . '/') . "'"); + } else { + $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "store WHERE REPLACE(`url`, 'www.', '') = '" . $this->db->escape('http://' . str_replace('www.', '', $_SERVER['HTTP_HOST']) . rtrim(dirname($_SERVER['PHP_SELF']), '/.\\') . '/') . "'"); + } + + if (isset($this->request->get['store_id'])) { + $this->config->set('config_store_id', (int)$this->request->get['store_id']); + } else if ($query->num_rows) { + $this->config->set('config_store_id', $query->row['store_id']); + } else { + $this->config->set('config_store_id', 0); + } + + if (!$query->num_rows) { + $this->config->set('config_url', HTTP_SERVER); + $this->config->set('config_ssl', HTTPS_SERVER); + } + + // Settings + $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "setting` WHERE store_id = '0' OR store_id = '" . (int)$this->config->get('config_store_id') . "' ORDER BY store_id ASC"); + + foreach ($query->rows as $result) { + if (!$result['serialized']) { + $this->config->set($result['key'], $result['value']); + } else { + $this->config->set($result['key'], json_decode($result['value'], true)); + } + } + + // Theme + $this->config->set('template_cache', $this->config->get('developer_theme')); + + // Url + $this->registry->set('url', new Url($this->config->get('config_url'), $this->config->get('config_ssl'))); + + // Language + $code = ''; + + $this->load->model('localisation/language'); + + $languages = $this->model_localisation_language->getLanguages(); + + if (isset($this->session->data['language'])) { + $code = $this->session->data['language']; + } + + if (isset($this->request->cookie['language']) && !array_key_exists($code, $languages)) { + $code = $this->request->cookie['language']; + } + + // Language Detection + if (!empty($this->request->server['HTTP_ACCEPT_LANGUAGE']) && !array_key_exists($code, $languages)) { + $detect = ''; + + $browser_languages = explode(',', $this->request->server['HTTP_ACCEPT_LANGUAGE']); + + // Try using local to detect the language + foreach ($browser_languages as $browser_language) { + foreach ($languages as $key => $value) { + if ($value['status']) { + $locale = explode(',', $value['locale']); + + if (in_array($browser_language, $locale)) { + $detect = $key; + break 2; + } + } + } + } + + if (!$detect) { + // Try using language folder to detect the language + foreach ($browser_languages as $browser_language) { + if (array_key_exists(strtolower($browser_language), $languages)) { + $detect = strtolower($browser_language); + + break; + } + } + } + + $code = $detect ? $detect : ''; + } + + if (!array_key_exists($code, $languages)) { + $code = $this->config->get('config_language'); + } + + if (!isset($this->session->data['language']) || $this->session->data['language'] != $code) { + $this->session->data['language'] = $code; + } + + if (!isset($this->request->cookie['language']) || $this->request->cookie['language'] != $code) { + setcookie('language', $code, time() + 60 * 60 * 24 * 30, '/', $this->request->server['HTTP_HOST']); + } + + // Overwrite the default language object + $language = new Language($code); + $language->load($code); + + $this->registry->set('language', $language); + + // Set the config language_id + $this->config->set('config_language_id', $languages[$code]['language_id']); + + // Customer + $customer = new Cart\Customer($this->registry); + $this->registry->set('customer', $customer); + + // Customer Group + if (isset($this->session->data['customer']) && isset($this->session->data['customer']['customer_group_id'])) { + // For API calls + $this->config->set('config_customer_group_id', $this->session->data['customer']['customer_group_id']); + } elseif ($this->customer->isLogged()) { + // Logged in customers + $this->config->set('config_customer_group_id', $this->customer->getGroupId()); + } elseif (isset($this->session->data['guest']) && isset($this->session->data['guest']['customer_group_id'])) { + $this->config->set('config_customer_group_id', $this->session->data['guest']['customer_group_id']); + } + + // Tracking Code + if (isset($this->request->get['tracking'])) { + setcookie('tracking', $this->request->get['tracking'], time() + 3600 * 24 * 1000, '/'); + + $this->db->query("UPDATE `" . DB_PREFIX . "marketing` SET clicks = (clicks + 1) WHERE code = '" . $this->db->escape($this->request->get['tracking']) . "'"); + } + + // Currency + $code = ''; + + $this->load->model('localisation/currency'); + + $currencies = $this->model_localisation_currency->getCurrencies(); + + if (isset($this->session->data['currency'])) { + $code = $this->session->data['currency']; + } + + if (isset($this->request->cookie['currency']) && !array_key_exists($code, $currencies)) { + $code = $this->request->cookie['currency']; + } + + if (!array_key_exists($code, $currencies)) { + $code = $this->config->get('config_currency'); + } + + if (!isset($this->session->data['currency']) || $this->session->data['currency'] != $code) { + $this->session->data['currency'] = $code; + } + + if (!isset($this->request->cookie['currency']) || $this->request->cookie['currency'] != $code) { + setcookie('currency', $code, time() + 60 * 60 * 24 * 30, '/', $this->request->server['HTTP_HOST']); + } + + $this->registry->set('currency', new Cart\Currency($this->registry)); + + // Tax + $this->registry->set('tax', new Cart\Tax($this->registry)); + + if (isset($this->session->data['shipping_address'])) { + $this->tax->setShippingAddress($this->session->data['shipping_address']['country_id'], $this->session->data['shipping_address']['zone_id']); + } elseif ($this->config->get('config_tax_default') == 'shipping') { + $this->tax->setShippingAddress($this->config->get('config_country_id'), $this->config->get('config_zone_id')); + } + + if (isset($this->session->data['payment_address'])) { + $this->tax->setPaymentAddress($this->session->data['payment_address']['country_id'], $this->session->data['payment_address']['zone_id']); + } elseif ($this->config->get('config_tax_default') == 'payment') { + $this->tax->setPaymentAddress($this->config->get('config_country_id'), $this->config->get('config_zone_id')); + } + + $this->tax->setStoreAddress($this->config->get('config_country_id'), $this->config->get('config_zone_id')); + + // Weight + $this->registry->set('weight', new Cart\Weight($this->registry)); + + // Length + $this->registry->set('length', new Cart\Length($this->registry)); + + // Cart + $this->registry->set('cart', new Cart\Cart($this->registry)); + + // Encryption + $this->registry->set('encryption', new Encryption($this->config->get('config_encryption'))); + + // OpenBay Pro + $this->registry->set('openbay', new Openbay($this->registry)); + } +} |