/halogy/application/libraries/Site.php

https://bitbucket.org/haloweb/halogy-1.0/ · PHP · 318 lines · 228 code · 41 blank · 49 comment · 43 complexity · a59e49e1d367e051d6800e20fbfdf5af MD5 · raw file

  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * Halogy
  4. *
  5. * A user friendly, modular content management system for PHP 5.0
  6. * Built on CodeIgniter - http://codeigniter.com
  7. *
  8. * @package Halogy
  9. * @author Haloweb Ltd.
  10. * @copyright Copyright (c) 2008-2011, Haloweb Ltd.
  11. * @license http://halogy.com/license
  12. * @link http://halogy.com/
  13. * @since Version 1.0
  14. * @filesource
  15. */
  16. // ------------------------------------------------------------------------
  17. class Site {
  18. var $siteID;
  19. var $siteDomain;
  20. var $config = array();
  21. var $plans = array();
  22. function Site()
  23. {
  24. // init vars
  25. $this->CI =& get_instance();
  26. // initialise site
  27. $this->_init_site();
  28. // find out what type of page request this is
  29. if (!preg_match('/\.(gif|jpg|jpeg|png|css|js|ico|shtml)$/i', $this->CI->uri->uri_string()))
  30. {
  31. // load session lib
  32. $this->CI->load->library('permission');
  33. $this->CI->load->library('parser');
  34. $this->CI->load->library('form_validation');
  35. $this->CI->load->library('pagination');
  36. $this->CI->load->library('template');
  37. // log in with cookie
  38. $this->_login_cookie();
  39. // check that the request is not admin or ajax
  40. if (!preg_match('/^\/admin\//i', $this->CI->uri->uri_string()))
  41. {
  42. // init tracking
  43. $this->_track_user();
  44. }
  45. }
  46. }
  47. function get_quota()
  48. {
  49. // get image quota
  50. $this->CI->db->where('siteID', $this->config['siteID']);
  51. $this->CI->db->select('SUM(filesize) as quota');
  52. $query = $this->CI->db->get('images');
  53. $row = $query->row_array();
  54. $quota = $row['quota'];
  55. // get file quota
  56. $this->CI->db->where('siteID', $this->config['siteID']);
  57. $this->CI->db->select('SUM(filesize) as quota');
  58. $query = $this->CI->db->get('files');
  59. $row = $query->row_array();
  60. $quota += $row['quota'];
  61. return $quota;
  62. }
  63. function _init_site()
  64. {
  65. // get hash of base URL
  66. $siteHash = md5($this->CI->config->item('base_url'));
  67. // get site domain
  68. $siteDomain = substr($this->CI->config->item('base_url'), 0, -1);
  69. $siteDomain = strtolower(preg_replace('/^(http)s?:\/+(www.)?/i', '', $siteDomain));
  70. $this->siteDomain = $siteDomain;
  71. // if multisite is enabled, then make sure uploads folder is based on domain
  72. if ($this->CI->config->item('stagingSites') === TRUE)
  73. {
  74. $this->CI->config->set_item('uploadsPath', $this->CI->config->item('uploadsPath').'/'.$siteDomain);
  75. }
  76. // look up site
  77. if ($this->CI->db->get('sites')->num_rows() !== 0)
  78. {
  79. // look in db
  80. $this->CI->db->where('siteDomain', $siteDomain);
  81. $this->CI->db->or_where('altDomain', $siteDomain);
  82. $query = $this->CI->db->get('sites t1', 1);
  83. if ($query->num_rows() > 0)
  84. {
  85. // get config for site
  86. $this->config = $query->row_array();
  87. // check site is active
  88. if (!$this->config['active'])
  89. {
  90. show_error('This site is currently offline, we are sorry for the inconvenience.');
  91. }
  92. // define the site variable
  93. define('SITEID', $this->config['siteID']);
  94. define('SITEGROUPID', $this->config['groupID']);
  95. // run defaults function
  96. $this->_set_defaults();
  97. return TRUE;
  98. }
  99. else
  100. {
  101. show_error('This domain has not been configured properly.');
  102. }
  103. }
  104. // no sites have been set up yet so lets create one
  105. else
  106. {
  107. $this->CI->load->library('permission');
  108. $set = array(
  109. 'siteDomain' => $siteDomain,
  110. 'siteName' => 'My Site',
  111. 'siteURL' => site_url('/'),
  112. 'dateCreated' => date("Y-m-d H:i:s"),
  113. 'groupID' => 1
  114. );
  115. $this->CI->db->set($set)->insert('sites');
  116. $siteID = $this->CI->db->insert_id();
  117. $this->CI->permission->add_default_permissions('-1', $siteID);
  118. $groupID = $this->CI->permission->add_group('Administrator', $siteID);
  119. $this->CI->permission->add_default_permissions($groupID, $siteID);
  120. redirect('/admin');
  121. }
  122. }
  123. function _login_cookie()
  124. {
  125. // load auth lib
  126. $this->CI->load->library('form_validation');
  127. $this->CI->load->library('auth');
  128. // check no session is set
  129. if (!$this->CI->session->userdata('session_user'))
  130. {
  131. if ($cookie = get_cookie('halogy'))
  132. {
  133. // get cookie
  134. $cookie = get_cookie('halogy');
  135. $session = unserialize(base64_decode(strtr($cookie, '-_', '+/')));
  136. // set admin session name, if given
  137. if ($this->CI->auth->do_login($session[0], $session[1], $session[2], TRUE))
  138. {
  139. // for use with ce
  140. if ($this->CI->session->userdata('groupID') > 0 && $this->CI->permission->get_group_permissions($this->CI->session->userdata('groupID')))
  141. {
  142. $this->CI->session->set_userdata('session_admin', TRUE);
  143. }
  144. }
  145. // get error message
  146. else
  147. {
  148. $this->CI->form_validation->set_error($this->CI->auth->error);
  149. }
  150. }
  151. }
  152. return FALSE;
  153. }
  154. function _track_user()
  155. {
  156. // set last page
  157. $this->CI->session->set_userdata('lastPage', $this->CI->uri->uri_string());
  158. // don't do this if the user is admin
  159. if (!$this->CI->session->userdata('session_admin') && $this->CI->input->user_agent())
  160. {
  161. $userdata = ($this->CI->session->userdata('firstName')) ? serialize(array(
  162. 'dateCreated' => $this->CI->session->userdata('dateCreated'),
  163. 'userID' => $this->CI->session->userdata('userID'),
  164. 'username' => $this->CI->session->userdata('username'),
  165. 'firstName' => $this->CI->session->userdata('firstName'),
  166. 'lastName' => $this->CI->session->userdata('lastName')
  167. )) : '';
  168. // find out if this user has been to the site today
  169. $userKey = md5(substr($this->CI->input->ip_address(),0,strrpos($this->CI->input->ip_address(),'.')).substr($this->CI->input->user_agent(), 0, 50));
  170. $this->CI->db->where('siteID', $this->config['siteID']);
  171. $this->CI->db->where('userKey', $userKey);
  172. $this->CI->db->where('date > ', "DATE_SUB(CONCAT(CURDATE(), ' 00:00:00'), INTERVAL 0 DAY)", FALSE);
  173. $query = $this->CI->db->get('tracking');
  174. // get last page
  175. $lastPage = ($this->CI->uri->uri_string()) ? $this->CI->uri->uri_string() : '/';
  176. // if not, enter a row in the db
  177. if ($query->num_rows() == 0)
  178. {
  179. $this->CI->db->set('date', date("Y-m-d H:i:s"));
  180. $this->CI->db->set('userKey', $userKey);
  181. $this->CI->db->set('ipAddress', $this->CI->input->ip_address());
  182. $this->CI->db->set('userAgent', substr($this->CI->input->user_agent(), 0, 50));
  183. if (isset($_SERVER['HTTP_REFERER'])) $this->CI->db->set('referer', $_SERVER['HTTP_REFERER']);
  184. $this->CI->db->set('lastPage', $lastPage);
  185. $this->CI->db->set('userdata', $userdata);
  186. $this->CI->db->set('siteID', $this->config['siteID']);
  187. $this->CI->db->insert('tracking');
  188. }
  189. // otherwise update the page views
  190. else
  191. {
  192. $row = $query->row_array();
  193. $this->CI->db->set('views', 'views+1', false);
  194. $this->CI->db->set('lastPage', $lastPage);
  195. if ($userdata) $this->CI->db->set('userdata', $userdata);
  196. $this->CI->db->where('siteID', $this->config['siteID']);
  197. $this->CI->db->where('trackingID', $row['trackingID']);
  198. $this->CI->db->update('tracking');
  199. }
  200. }
  201. }
  202. function _set_defaults()
  203. {
  204. // set plans
  205. if ($this->config['plan'] == 1)
  206. {
  207. $this->plans['storage'] = 20000;
  208. }
  209. elseif ($this->config['plan'] == 2)
  210. {
  211. $this->plans['storage'] = 500000;
  212. }
  213. elseif ($this->config['plan'] == 3)
  214. {
  215. $this->plans['storage'] = 1000000;
  216. }
  217. elseif ($this->config['plan'] == 4)
  218. {
  219. $this->plans['storage'] = 2000000;
  220. }
  221. elseif ($this->config['plan'] == 5)
  222. {
  223. $this->plans['storage'] = 5000000;
  224. }
  225. else
  226. {
  227. $this->plans['storage'] = -1;
  228. }
  229. // shop defaults
  230. if (!$this->config['shopVariation1'])
  231. {
  232. $this->config['shopVariation1'] = 'Colour';
  233. }
  234. if (!$this->config['shopVariation2'])
  235. {
  236. $this->config['shopVariation2'] = 'Size';
  237. }
  238. if (!$this->config['shopVariation3'])
  239. {
  240. $this->config['shopVariation3'] = 'Other';
  241. }
  242. // email defaults
  243. if (!$this->config['emailHeader'])
  244. {
  245. $this->config['emailHeader'] = "Dear {name},";
  246. }
  247. if (!$this->config['emailFooter'])
  248. {
  249. $this->config['emailFooter'] = "Best Regards,\n".$this->config['siteName']."\n".$this->config['siteURL']."\n\n";
  250. }
  251. if (!$this->config['emailTicket'])
  252. {
  253. $this->config['emailTicket'] = "Thank you for contacting us, a new ticket has been created. This is an automated response confirming the receipt of your message. We will attend to your enquiry soon as possible. The details of your enquiry are below for your records. When replying, please keep the ticket ID in the subject to ensure that your replies are dealt with correctly.";
  254. }
  255. if (!$this->config['emailOrder'])
  256. {
  257. $this->config['emailOrder'] = "This is a confirmation to say that your order on ".$this->config['siteName']." has been placed and is currently being processed. We will email you again once your order has been shipped.\n\nIf you have any queries about your order, please do not hesitate to contact us at ".$this->config['siteEmail']." quoting your unique order reference number. Thank you for your custom.";
  258. }
  259. if (!$this->config['emailAccount'])
  260. {
  261. $this->config['emailAccount'] = "Your account for ".$this->config['siteName']." has been set up. Thank you for registering with us.\n\nPlease keep the information below safe.";
  262. }
  263. if (!$this->config['emailDonation'])
  264. {
  265. $this->config['emailDonation'] = "Thank you for your donation placed on ".$this->config['siteName'].".";
  266. }
  267. if (!$this->config['emailSubscription'])
  268. {
  269. $this->config['emailSubscription'] = "This is a confirmation to say that your subscription has been created on ".$this->config['siteName'].". You can update your subscription and view invoices by logging in to your account. Please note that your subscription will renew at the intervals stated on the website unless you cancel the subscription prior to the renewal date. See our website for more information. To login to your account please click on the URL below:\n\n".site_url('/shop/account')."\n\nYour subscription details are below, thank you for your custom.";
  270. }
  271. if (!$this->config['emailDispatch'])
  272. {
  273. $this->config['emailDispatch'] = "This is a notification to say that your order {order-id} on ".$this->config['siteName']." has been shipped.\n\nYou can track your order and view past orders by clicking on the link below.\n\n".site_url('/shop/orders')."\n\nIf you have any other queries about your order, please do not hesitate to contact us at ".$this->config['siteEmail']." quoting your unique order reference number.";
  274. }
  275. return TRUE;
  276. }
  277. }