PHP File Manager
Editing File: Common.php
<?php /* * Copyright (C) Wayne Purton-Smith - All Rights Reserved * Unauthorized copying of this file or removing this paragraph, via any medium is strictly prohibited * Proprietary and confidential * Written by Wayne Purton-Smith <waynepurtonsmith@hotmail.co.uk> February 2014 */ class Common extends CI_Model { public $responseMessage = ''; public $responseIsError = true; public $emailDebug = ''; public $vat = 0; public $whitelisted_ips = array ( '172.16.0.1', // Internal network IP '78.33.181.81', // Public network IP '109.203.99.140' // Server 3 IP ); public $is_bst = false; public $page_notes = array(); public $latest_tweet = NULL; function __construct() { if(SYSTEM_FORCE_SSL) { if(strtolower($this->input->server('HTTPS')) != 'on') { redirect(preg_replace('/^http/i', 'https', current_url())); exit; } } // For shitty IE support header("X-UA-Compatible: IE=" . (($this->input->get('ie_v')) ? $this->input->get('ie_v') : 'Edge') . ", chrome=1", true); $this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file')); $this->users->checkSession(); if(SYSTEM_IS_PANEL && !$this->users->logged_in && !in_array($this->uri->segment(1), array('login', 'logout', 'api', 'cron')) && (!($this->uri->segment(1) == 'orders' && $this->uri->segment(2) == 'bakers') && !in_array($this->input->ip_address(), array($this->whitelisted_ips)) && !$this->input->get_post('_bakersPanelAccess'))) { if($this->input->is_ajax_request()) { $parsed_url_path = trim(parse_url($this->agent->referrer(), PHP_URL_PATH), '/'); $full_login_url = '/login' . (($parsed_url_path) ? '/?redirect=/' . $parsed_url_path : ''); exit_with_json(array ( 'loginRequired' => true, 'url' => $full_login_url )); } else { $redirect_back = (($uri_segments = $this->uri->segment_array())) ? '/' . implode('/', $uri_segments) : NULL; $full_login_url = '/login' . (($redirect_back) ? '/?redirect=' . $redirect_back : ''); redirect($full_login_url); exit; } } $settings = $this->db->query("SELECT * FROM `settings`"); if($settings->num_rows()) { $rows = $settings->result(); $settings->free_result(); foreach($rows as $config_row) { $this->config->set_item($config_row->config_name, $config_row->config_value); } } else { show_error('Error loading the Intranet settings'); } $this->load->model('orders'); $order_statuses = $this->orders->getOrderStatuses(); foreach($order_statuses as $status_info) { $this->config->set_item('order_status_' . $status_info->status_id, $status_info->label); } $this->vat = (float) $this->config->item('vat'); $this->default_per_page = (($page_per = (int) $this->input->cookie('results_per_page')) > 0) ? $page_per : 30; // Check if we're in BST (Daylight savings) //date_default_timezone_set('Europe/London'); //$current_time = time(); //$this->is_bst = (date('H:i', $current_time) != gmdate('H:i', $current_time)); //date_default_timezone_set('GMT'); $this->load->model('notes'); if(strcasecmp($this->input->server('REQUEST_METHOD'), 'get') === 0) { $this->page_notes = $this->notes->get(NULL, '/' . trim(uri_string(), '/')); } } public function saveSettings($data = array()) { if(!empty($data)) { $settings = $this->db->query("SELECT * FROM `settings`"); if($settings->num_rows()) { $rows = $settings->result(); $settings->free_result(); foreach($rows as $config_row) { if(isset($data[$config_row->config_name])) { $this->db->query($this->db->update_string('settings', array('config_value' => $data[$config_row->config_name]), "`config_name` = '" . $config_row->config_name . "'")); } } return true; } } return false; } public function setResponseMessage($msg = '', $return_inline = false) { //$debug = debug_backtrace(); $this->responseMessage = $msg; $this->responseIsError = ($return_inline === false) ? true : false; return $return_inline; } public function getResponseMessage($html_error = false) { $this->responseMessage = ($this->session->flashdata('_responseMsg') !== NULL) ? $this->session->flashdata('_responseMsg') : $this->responseMessage; $this->responseIsError = ($this->session->flashdata('_responseError') !== NULL) ? $this->session->flashdata('_responseError') : $this->responseIsError; return (!$this->responseMessage) ? '' : (($html_error) ? '<div class="alert alert-' . (($this->responseIsError) ? 'danger' : 'success') . '">' . $this->responseMessage . '</div>' : $this->responseMessage); } public function keepResponseMessage() { if($this->responseMessage) { $this->session->set_flashdata(array('_responseMsg' => $this->responseMessage, '_responseError' => $this->responseIsError)); } } public function getPostData() { if(($post_data = $this->session->flashdata('_postData')) !== NULL && is_array($post_data)) { $_POST = (count($_POST)) ? array_merge($_POST, $post_data) : $post_data; } } public function keepPostData() { $this->session->set_flashdata('_postData', $this->input->post()); } public function countPreviousQuery($sql = NULL) { $sql = "SELECT COUNT(1) AS `rows_count` FROM ( " . preg_replace('/# start limit \s+.*\s+ # end limit/ism', '', trim(($sql) ? $sql : $this->db->last_query())) . " ) AS `rc_" . rand(10, 300) . "`"; if(wayne()) { //preprint($sql); } return (int) $this->db->query($sql)->row('rows_count'); } public function generatePagination($base_url = '', $segment = NULL, $uri_suffix = NULL, $rows = NULL, $per_page = 30, $class_name = 'pagination') { $this->load->library('pagination'); $this->pagination->initialize(array ( 'base_url' => $base_url, 'uri_segment' => $segment, 'total_rows' => ($rows > 0) ? $rows : $this->countPreviousQuery(), 'per_page' => $per_page, 'num_links' => 10, 'use_page_numbers' => true, 'suffix' => $uri_suffix, 'full_tag_open' => '<ul class="' . $class_name . '">', 'full_tag_close' => '</ul>', 'first_link' => '« First', 'first_tag_open' => '<li class="first-link">', 'first_tag_close' => '</li>', 'last_link' => 'Last »', 'last_tag_open' => '<li class="last-link">', 'last_tag_close' => '</li>', 'prev_link' => '« Previous', 'next_link' => 'Next »', 'num_tag_open' => '<li>', 'num_tag_close' => '</li>', 'cur_tag_open' => '<li class="active"><span>', 'cur_tag_close' => '</span></li>', 'prev_tag_open' => '<li class="prev-link">', 'prev_tag_close' => '</li>', 'next_tag_open' => '<li class="next-link">', 'next_tag_close' => '</li>' )); return $this->pagination->create_links(); } public function returnOffset(&$page = 1, &$limit = 0, $removeable = true) { $page = (int) $page; $limit = (int) $limit; if($removeable && ($page === -1 || $limit === -1)) { return ''; } return (($removeable) ? " \n # START LIMIT \n " : '') . " LIMIT " . ((($offset = ($page - 1) * $limit) >= 0) ? $offset : 0) . ", $limit " . (($removeable) ? " \n # END LIMIT \n" : ''); } public function sendEmail($data = array(), $email_template = 'message') { if(isset($data['email'])) { $this->load->library('email'); $this->load->model('logs'); $this->email->clear(); $this->email->from('sending@' . SYSTEM_ROOT_URL, $this->config->item('company_name')); $this->email->to($data['email']); if(isset($data['cc'])) $this->email->cc($data['cc']); if(isset($data['bcc'])) $this->email->bcc($data['bcc']); if(isset($data['reply'])) { if(is_array($data['reply']) && isset($data['reply'][0])) { $reply_email = $data['reply'][0]; $reply_name = (isset($data['reply'][1])) ? $data['reply'][1] : ''; } elseif(is_string($data['reply'])) { $reply_email = $data['reply']; $reply_name = ''; } $this->email->reply_to($reply_email, $reply_name); } $this->email->subject((isset($data['subject'])) ? $data['subject'] : 'Email from ' . $this->config->item('company_name')); $message_body = $this->load->view('templates/email/' . $email_template, (isset($data['variables'])) ? $data['variables'] : NULL, true); if(isset($data['files']) && !empty($data['files'])) { foreach($data['files'] as $file_info) { $this->email->attach($file_info[0], 'attachment', $file_info[1]); } } $this->email->message($message_body); $result = $this->email->send(false); $this->emailDebug = trim($this->email->print_debugger(), '<pre></pre>'); //$this->logs->add($this->emailDebug); if($result) { return true; } } return false; } public function geocodeAddress($query = '', $get_address = false) { if(strlen(($query = trim($query)))) { $response = $this->curlURL('https://maps.googleapis.com/maps/api/geocode/json', array ( 'address' => $query, 'key' => $this->config->item('maps_api_key'), 'userIp' => $this->input->ip_address() ), array ( CURLOPT_REFERER => SYSTEM_DEFAULT_HOST )); $response = @json_decode($response); if(isset($response->results[0]->geometry->location)) { if($get_address) { $search_components = array ( 'home_number' => array('long', array('street_number')), 'street' => array('long', array('route')), 'town' => array('long', array('postal_town', 'locality')), 'county' => array('long', array('administrative_area_level_2')), 'postcode' => array('long', array('postal_code')), 'country' => array('long', array('country')), 'country_code' => array('short', array('country')) ); $data = array(); foreach($response->results[0]->address_components as $component_data) { $component_found = false; foreach($search_components as $key_name => $search_data) { if(!isset($data[$key_name])) $data[$key_name] = NULL; foreach($component_data->types as $component_type) { if(in_array($component_type, $search_data[1])) { $data[$key_name] = $component_data->{$search_data[0] . '_name'}; $component_found = true; break; } } if($component_found) { break; } } } return (object) $data; } else { return $response->results[0]->geometry->location; } } } return false; } public function curlURL($url = '', $get_data = array(), $curl_opts = array()) { if(filter_var($url, FILTER_VALIDATE_URL)) { if(!empty($get_data) && is_array($get_data)) { $url .= ((stripos($url, '?') !== false) ? '' : '?') . http_build_query($get_data); } $ch = curl_init($url); if(!empty($curl_opts) && is_array($curl_opts)) { curl_setopt_array($ch, $curl_opts); } curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_CAINFO, APPPATH . 'third_party/cacert.pem'); $response = curl_exec($ch); curl_close($ch); return $response; } return NULL; } public function getReferrals() { $sql = "SELECT * FROM `referral_types`"; $result = $this->db->query($sql); if($result->num_rows()) { $rows = $result->result(); $result->free_result(); return $rows; } return array(); } public function getPaymentTypes($payment_type = NULL, $exclude_unknown = false) { $sql = "SELECT * FROM `payment_types` WHERE `is_deleted` = 0"; if(($payment_type = (int) $payment_type) > 0) { $sql .= " AND `type_id` = $payment_type "; } if($exclude_unknown === true) { $sql .= " AND `type_id` <> 1 "; } $sql .= " ORDER BY CASE WHEN `type_id` = 1 THEN 0 ELSE 1 END, `label`"; $result = $this->db->query($sql); if($result->num_rows()) { if($payment_type) return $result->row(); $rows = $result->result(); $result->free_result(); return $rows; } return array(); } public function getCountries($country_id = NULL, $country_code = '', $country_name = '') { $sql = "SELECT * FROM `countries` WHERE 1 = 1 "; if(($country_id = (int) $country_id) > 0) { $sql .= " AND `country_id` = $country_id "; } if(($country_name = format_whitespace($country_name)) != '') { $sql .= " AND `name` = " . $this->db->escape($country_name) . " "; } if(($country_code = trim($country_code))) { $sql .= " AND `iso` = " . $this->db->escape($country_code) . " "; } $sql .= " ORDER BY CASE WHEN `country_id` = 1 THEN 0 ELSE 1 END, `name`"; $result = $this->db->query($sql); if($result->num_rows()) { if($country_id > 0 || $country_code || $country_name) return $result->row(); $rows = $result->result(); $result->free_result(); return $rows; } return array(); } public function getOriginLocations($location_id = 0) { $sql = "SELECT * FROM `origin_locations` "; if(($location_id = (int) $location_id) > 0) { $sql .= " WHERE `location_id` = $location_id LIMIT 1"; } $result = $this->db->query($sql); if($result->num_rows()) { if($location_id > 0) return $result->row(); $rows = $result->result(); $result->free_result(); return $rows; } return array(); } public function saveOriginLocations($data = array()) { if(isset($data['existing_location']) && is_array($data['existing_location'])) { $existing_locations_ids = cast_int_ids($data['existing_location']); if(count($existing_locations_ids)) { $this->db->query("DELETE FROM `origin_locations` WHERE `location_id` NOT IN (" . implode(',', $existing_locations_ids) . ")"); } $total_locations = count($data['existing_location']); for($i = 0; $i < $total_locations; $i++) { foreach(array('address_1', 'address_2', 'address_3', 'town', 'county', 'postcode', 'country', 'phone', 'email', 'free_delivery_range', 'existing_location') as $j => $input_key) { $$input_key = (isset($data[$input_key][$i])) ? nullify(format_whitespace($data[$input_key][$i])) : NULL; } if(trim($address_1) != '') { $lookup_query = format_whitespace(format_display($address_1, $town, $postcode, $country)); $existing_location = (($existing_location = (int) $existing_location) > 0) ? $existing_location : NULL; $free_delivery_range = (($free_delivery_range = (float) $free_delivery_range) > 0) ? $free_delivery_range : NULL; $sql_data = array ( 'address_1' => $address_1, 'address_2' => $address_2, 'address_3' => $address_3, 'town' => $town, 'county' => $county, 'postcode' => $postcode, 'country' => NULL, 'country_code' => $country, 'phone' => $phone, 'email' => $email, 'latitude' => NULL, 'longitude' => NULL, 'free_delivery_range' => $free_delivery_range ); if(trim($lookup_query) != '') { if(($country_name = iso_code_to_country($country)) && ($response = $this->geocodeAddress($lookup_query))) { $sql_data['country'] = $country_name; $sql_data['latitude'] = $response->lat; $sql_data['longitude'] = $response->lng; if($existing_location > 0) { $sql = $this->db->update_string('origin_locations', $sql_data, "`location_id` = $existing_location"); } else { $sql = $this->db->insert_string('origin_locations', $sql_data); } $this->db->query($sql); } } } } return true; } return false; } public function getOpeningTimes($location_id = NULL, $twenty_four_hour = true) { $sql = "SELECT `ot`.* FROM `opening_times` `ot` LEFT OUTER JOIN `origin_locations` `ol` ON `ol`.`location_id` = `ot`.`location_id`"; if(($location_id = (int) $location_id) > 0) { $sql .= " WHERE `ot`.`location_id` = $location_id"; } $sql .= " ORDER BY `ot`.`day` LIMIT 7"; $result = $this->db->query($sql); if($result->num_rows()) { $rows = $result->result(); $result->free_result(); $data = array(); foreach($rows as $time_info) { if($time_info->closed) { $data[$time_info->day_text][-1] = -1; } else { $data[$time_info->time_from][$time_info->time_to][] = $time_info->day_text; } } foreach($data as $open_time => $closing_times) { foreach($closing_times as $close_time => $closing_data) { $_open_time = ($twenty_four_hour) ? substr($open_time, 0, -3) : date('g:ia', strtotime($open_time)); $_close_time = ($twenty_four_hour) ? substr($close_time, 0, -3) : date('g:ia', strtotime($close_time)); $times[$_open_time . ' to ' . $_close_time] = ($close_time === -1) ? 'Closed' : ((count($closing_data) > 1) ? $closing_data[0] . ' - ' . end($closing_data) : $closing_data[0]); } } $times = array_flip($times); return $times; } return array(); } }
Cancel