PageRenderTime 24ms CodeModel.GetById 1ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 0ms

/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// ------------------------------------------------------------------------
 18
 19class Site {
 20	
 21	var $siteID;
 22	var $siteDomain;
 23	var $config = array();
 24	var $plans = array();
 25
 26	function Site()
 27	{
 28		// init vars
 29		$this->CI =& get_instance();
 30
 31		// initialise site
 32		$this->_init_site();
 33
 34		// find out what type of page request this is
 35		if (!preg_match('/\.(gif|jpg|jpeg|png|css|js|ico|shtml)$/i', $this->CI->uri->uri_string()))
 36		{
 37			// load session lib
 38			$this->CI->load->library('permission');
 39			$this->CI->load->library('parser');
 40			$this->CI->load->library('form_validation');
 41			$this->CI->load->library('pagination');
 42			$this->CI->load->library('template');
 43						
 44			// log in with cookie
 45			$this->_login_cookie();
 46
 47			// check that the request is not admin or ajax
 48			if (!preg_match('/^\/admin\//i', $this->CI->uri->uri_string()))
 49			{
 50				// init tracking
 51				$this->_track_user();
 52			}
 53		}
 54	}
 55
 56	function get_quota()
 57	{
 58		// get image quota
 59		$this->CI->db->where('siteID', $this->config['siteID']);
 60		$this->CI->db->select('SUM(filesize) as quota');
 61		$query = $this->CI->db->get('images');
 62		$row = $query->row_array();	
 63		$quota = $row['quota'];
 64
 65		// get file quota
 66		$this->CI->db->where('siteID', $this->config['siteID']);
 67		$this->CI->db->select('SUM(filesize) as quota');
 68		$query = $this->CI->db->get('files');
 69		$row = $query->row_array();
 70		$quota += $row['quota'];
 71
 72		return $quota;
 73	}	
 74
 75	function _init_site()
 76	{
 77		// get hash of base URL
 78		$siteHash = md5($this->CI->config->item('base_url'));			
 79			
 80		// get site domain
 81		$siteDomain = substr($this->CI->config->item('base_url'), 0, -1);
 82		$siteDomain = strtolower(preg_replace('/^(http)s?:\/+(www.)?/i', '', $siteDomain));
 83		$this->siteDomain = $siteDomain;
 84		
 85		// if multisite is enabled, then make sure uploads folder is based on domain
 86		if ($this->CI->config->item('stagingSites') === TRUE)
 87		{
 88			$this->CI->config->set_item('uploadsPath', $this->CI->config->item('uploadsPath').'/'.$siteDomain);
 89		}
 90		
 91		// look up site
 92		if ($this->CI->db->get('sites')->num_rows() !== 0)
 93		{
 94			// look in db
 95			$this->CI->db->where('siteDomain', $siteDomain);
 96			$this->CI->db->or_where('altDomain', $siteDomain);
 97			$query = $this->CI->db->get('sites t1', 1);
 98
 99			if ($query->num_rows() > 0)
100			{	
101				// get config for site	
102				$this->config = $query->row_array();
103	
104				// check site is active
105				if (!$this->config['active'])
106				{
107					show_error('This site is currently offline, we are sorry for the inconvenience.');
108				}
109	
110				// define the site variable
111				define('SITEID', $this->config['siteID']);
112				define('SITEGROUPID', $this->config['groupID']);
113	
114				// run defaults function
115				$this->_set_defaults();
116		
117				return TRUE;
118			}
119			else
120			{
121				show_error('This domain has not been configured properly.');
122			}
123		}
124		
125		// no sites have been set up yet so lets create one
126		else
127		{
128			$this->CI->load->library('permission');			
129			$set = array(
130				'siteDomain' => $siteDomain,
131				'siteName' => 'My Site',
132				'siteURL' => site_url('/'),
133				'dateCreated' => date("Y-m-d H:i:s"),
134				'groupID' => 1
135			);
136			$this->CI->db->set($set)->insert('sites');
137			$siteID = $this->CI->db->insert_id();
138			$this->CI->permission->add_default_permissions('-1', $siteID);
139			$groupID = $this->CI->permission->add_group('Administrator', $siteID);
140			$this->CI->permission->add_default_permissions($groupID, $siteID);
141			
142			redirect('/admin');
143		}
144	}
145
146	function _login_cookie()
147	{
148		// load auth lib
149		$this->CI->load->library('form_validation');
150		$this->CI->load->library('auth');
151		
152		// check no session is set
153		if (!$this->CI->session->userdata('session_user'))
154		{
155			if ($cookie = get_cookie('halogy'))
156			{
157				// get cookie
158				$cookie = get_cookie('halogy');
159				$session = unserialize(base64_decode(strtr($cookie, '-_', '+/')));
160
161				// set admin session name, if given
162				if ($this->CI->auth->do_login($session[0], $session[1], $session[2], TRUE))
163				{
164					// for use with ce
165					if ($this->CI->session->userdata('groupID') > 0 && $this->CI->permission->get_group_permissions($this->CI->session->userdata('groupID')))
166					{
167						$this->CI->session->set_userdata('session_admin', TRUE);
168					}
169				}
170
171				// get error message
172				else
173				{
174					$this->CI->form_validation->set_error($this->CI->auth->error);
175				}
176			}
177		}
178
179		return FALSE;
180	}
181
182	function _track_user()
183	{			
184		// set last page
185		$this->CI->session->set_userdata('lastPage', $this->CI->uri->uri_string());
186	
187		// don't do this if the user is admin
188		if (!$this->CI->session->userdata('session_admin') && $this->CI->input->user_agent())
189		{
190			$userdata = ($this->CI->session->userdata('firstName')) ? serialize(array(
191				'dateCreated' => $this->CI->session->userdata('dateCreated'),
192				'userID' => $this->CI->session->userdata('userID'),
193				'username' => $this->CI->session->userdata('username'),
194				'firstName' => $this->CI->session->userdata('firstName'), 
195				'lastName' => $this->CI->session->userdata('lastName')
196			)) : '';
197			
198			// find out if this user has been to the site today
199			$userKey = md5(substr($this->CI->input->ip_address(),0,strrpos($this->CI->input->ip_address(),'.')).substr($this->CI->input->user_agent(), 0, 50));
200			$this->CI->db->where('siteID', $this->config['siteID']);
201			$this->CI->db->where('userKey', $userKey);			
202			$this->CI->db->where('date > ', "DATE_SUB(CONCAT(CURDATE(), ' 00:00:00'), INTERVAL 0 DAY)", FALSE);
203			$query = $this->CI->db->get('tracking');
204
205			// get last page
206			$lastPage = ($this->CI->uri->uri_string()) ? $this->CI->uri->uri_string() : '/';
207	
208			// if not, enter a row in the db
209			if ($query->num_rows() == 0)
210			{
211				$this->CI->db->set('date', date("Y-m-d H:i:s"));
212				$this->CI->db->set('userKey', $userKey);
213				$this->CI->db->set('ipAddress', $this->CI->input->ip_address());
214				$this->CI->db->set('userAgent', substr($this->CI->input->user_agent(), 0, 50));
215				if (isset($_SERVER['HTTP_REFERER'])) $this->CI->db->set('referer', $_SERVER['HTTP_REFERER']);
216				$this->CI->db->set('lastPage', $lastPage);
217				$this->CI->db->set('userdata', $userdata);	
218				$this->CI->db->set('siteID', $this->config['siteID']);
219	
220				$this->CI->db->insert('tracking');
221			}
222	
223			// otherwise update the page views
224			else
225			{
226				$row = $query->row_array();
227				
228				$this->CI->db->set('views', 'views+1', false);
229				$this->CI->db->set('lastPage', $lastPage);				
230				if ($userdata) $this->CI->db->set('userdata', $userdata);
231				$this->CI->db->where('siteID', $this->config['siteID']);
232				$this->CI->db->where('trackingID', $row['trackingID']);
233				$this->CI->db->update('tracking');
234			}
235		}
236	}
237
238	function _set_defaults()
239	{
240		// set plans
241		if ($this->config['plan'] == 1)
242		{
243			$this->plans['storage'] = 20000;
244		}
245		elseif ($this->config['plan'] == 2)
246		{
247			$this->plans['storage'] = 500000;
248		}
249		elseif ($this->config['plan'] == 3)
250		{
251			$this->plans['storage'] = 1000000;
252		}
253		elseif ($this->config['plan'] == 4)
254		{
255			$this->plans['storage'] = 2000000;
256		}			
257		elseif ($this->config['plan'] == 5)
258		{
259			$this->plans['storage'] = 5000000;
260		}
261		else
262		{
263			$this->plans['storage'] = -1;
264		}		
265		
266		// shop defaults
267		if (!$this->config['shopVariation1'])
268		{
269			$this->config['shopVariation1'] = 'Colour';
270		}
271		if (!$this->config['shopVariation2'])
272		{
273			$this->config['shopVariation2'] = 'Size';
274		}
275		if (!$this->config['shopVariation3'])
276		{
277			$this->config['shopVariation3'] = 'Other';
278		}
279		
280		// email defaults
281		if (!$this->config['emailHeader'])
282		{
283			$this->config['emailHeader'] = "Dear {name},";
284		}
285		if (!$this->config['emailFooter'])
286		{
287			$this->config['emailFooter'] = "Best Regards,\n".$this->config['siteName']."\n".$this->config['siteURL']."\n\n";
288		}
289		if (!$this->config['emailTicket'])
290		{
291			$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.";
292		}
293		if (!$this->config['emailOrder'])
294		{
295			$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.";
296		}
297		if (!$this->config['emailAccount'])
298		{
299			$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.";
300		}
301		if (!$this->config['emailDonation'])
302		{
303			$this->config['emailDonation'] = "Thank you for your donation placed on ".$this->config['siteName'].".";
304		}
305		if (!$this->config['emailSubscription'])
306		{
307			$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.";
308		}
309		if (!$this->config['emailDispatch'])
310		{
311			$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.";
312		}
313
314
315		return TRUE;
316	}
317
318}