startup.php (7390B)
1 <?php 2 class ControllerStartupStartup extends Controller { 3 public function index() { 4 // Store 5 if ($this->request->server['HTTPS']) { 6 $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']), '/.\\') . '/') . "'"); 7 } else { 8 $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']), '/.\\') . '/') . "'"); 9 } 10 11 if (isset($this->request->get['store_id'])) { 12 $this->config->set('config_store_id', (int)$this->request->get['store_id']); 13 } else if ($query->num_rows) { 14 $this->config->set('config_store_id', $query->row['store_id']); 15 } else { 16 $this->config->set('config_store_id', 0); 17 } 18 19 if (!$query->num_rows) { 20 $this->config->set('config_url', HTTP_SERVER); 21 $this->config->set('config_ssl', HTTPS_SERVER); 22 } 23 24 // Settings 25 $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"); 26 27 foreach ($query->rows as $result) { 28 if (!$result['serialized']) { 29 $this->config->set($result['key'], $result['value']); 30 } else { 31 $this->config->set($result['key'], json_decode($result['value'], true)); 32 } 33 } 34 35 // Theme 36 $this->config->set('template_cache', $this->config->get('developer_theme')); 37 38 // Url 39 $this->registry->set('url', new Url($this->config->get('config_url'), $this->config->get('config_ssl'))); 40 41 // Language 42 $code = ''; 43 44 $this->load->model('localisation/language'); 45 46 $languages = $this->model_localisation_language->getLanguages(); 47 48 if (isset($this->session->data['language'])) { 49 $code = $this->session->data['language']; 50 } 51 52 if (isset($this->request->cookie['language']) && !array_key_exists($code, $languages)) { 53 $code = $this->request->cookie['language']; 54 } 55 56 // Language Detection 57 if (!empty($this->request->server['HTTP_ACCEPT_LANGUAGE']) && !array_key_exists($code, $languages)) { 58 $detect = ''; 59 60 $browser_languages = explode(',', $this->request->server['HTTP_ACCEPT_LANGUAGE']); 61 62 // Try using local to detect the language 63 foreach ($browser_languages as $browser_language) { 64 foreach ($languages as $key => $value) { 65 if ($value['status']) { 66 $locale = explode(',', $value['locale']); 67 68 if (in_array($browser_language, $locale)) { 69 $detect = $key; 70 break 2; 71 } 72 } 73 } 74 } 75 76 if (!$detect) { 77 // Try using language folder to detect the language 78 foreach ($browser_languages as $browser_language) { 79 if (array_key_exists(strtolower($browser_language), $languages)) { 80 $detect = strtolower($browser_language); 81 82 break; 83 } 84 } 85 } 86 87 $code = $detect ? $detect : ''; 88 } 89 90 if (!array_key_exists($code, $languages)) { 91 $code = $this->config->get('config_language'); 92 } 93 94 if (!isset($this->session->data['language']) || $this->session->data['language'] != $code) { 95 $this->session->data['language'] = $code; 96 } 97 98 if (!isset($this->request->cookie['language']) || $this->request->cookie['language'] != $code) { 99 setcookie('language', $code, time() + 60 * 60 * 24 * 30, '/', $this->request->server['HTTP_HOST']); 100 } 101 102 // Overwrite the default language object 103 $language = new Language($code); 104 $language->load($code); 105 106 $this->registry->set('language', $language); 107 108 // Set the config language_id 109 $this->config->set('config_language_id', $languages[$code]['language_id']); 110 111 // Customer 112 $customer = new Cart\Customer($this->registry); 113 $this->registry->set('customer', $customer); 114 115 // Customer Group 116 if (isset($this->session->data['customer']) && isset($this->session->data['customer']['customer_group_id'])) { 117 // For API calls 118 $this->config->set('config_customer_group_id', $this->session->data['customer']['customer_group_id']); 119 } elseif ($this->customer->isLogged()) { 120 // Logged in customers 121 $this->config->set('config_customer_group_id', $this->customer->getGroupId()); 122 } elseif (isset($this->session->data['guest']) && isset($this->session->data['guest']['customer_group_id'])) { 123 $this->config->set('config_customer_group_id', $this->session->data['guest']['customer_group_id']); 124 } 125 126 // Tracking Code 127 if (isset($this->request->get['tracking'])) { 128 setcookie('tracking', $this->request->get['tracking'], time() + 3600 * 24 * 1000, '/'); 129 130 $this->db->query("UPDATE `" . DB_PREFIX . "marketing` SET clicks = (clicks + 1) WHERE code = '" . $this->db->escape($this->request->get['tracking']) . "'"); 131 } 132 133 // Currency 134 $code = ''; 135 136 $this->load->model('localisation/currency'); 137 138 $currencies = $this->model_localisation_currency->getCurrencies(); 139 140 if (isset($this->session->data['currency'])) { 141 $code = $this->session->data['currency']; 142 } 143 144 if (isset($this->request->cookie['currency']) && !array_key_exists($code, $currencies)) { 145 $code = $this->request->cookie['currency']; 146 } 147 148 if (!array_key_exists($code, $currencies)) { 149 $code = $this->config->get('config_currency'); 150 } 151 152 if (!isset($this->session->data['currency']) || $this->session->data['currency'] != $code) { 153 $this->session->data['currency'] = $code; 154 } 155 156 if (!isset($this->request->cookie['currency']) || $this->request->cookie['currency'] != $code) { 157 setcookie('currency', $code, time() + 60 * 60 * 24 * 30, '/', $this->request->server['HTTP_HOST']); 158 } 159 160 $this->registry->set('currency', new Cart\Currency($this->registry)); 161 162 // Tax 163 $this->registry->set('tax', new Cart\Tax($this->registry)); 164 165 if (isset($this->session->data['shipping_address'])) { 166 $this->tax->setShippingAddress($this->session->data['shipping_address']['country_id'], $this->session->data['shipping_address']['zone_id']); 167 } elseif ($this->config->get('config_tax_default') == 'shipping') { 168 $this->tax->setShippingAddress($this->config->get('config_country_id'), $this->config->get('config_zone_id')); 169 } 170 171 if (isset($this->session->data['payment_address'])) { 172 $this->tax->setPaymentAddress($this->session->data['payment_address']['country_id'], $this->session->data['payment_address']['zone_id']); 173 } elseif ($this->config->get('config_tax_default') == 'payment') { 174 $this->tax->setPaymentAddress($this->config->get('config_country_id'), $this->config->get('config_zone_id')); 175 } 176 177 $this->tax->setStoreAddress($this->config->get('config_country_id'), $this->config->get('config_zone_id')); 178 179 // Weight 180 $this->registry->set('weight', new Cart\Weight($this->registry)); 181 182 // Length 183 $this->registry->set('length', new Cart\Length($this->registry)); 184 185 // Cart 186 $this->registry->set('cart', new Cart\Cart($this->registry)); 187 188 // Encryption 189 $this->registry->set('encryption', new Encryption($this->config->get('config_encryption'))); 190 191 // OpenBay Pro 192 $this->registry->set('openbay', new Openbay($this->registry)); 193 } 194 }