PageRenderTime 28ms CodeModel.GetById 2ms app.highlight 20ms RepoModel.GetById 1ms app.codeStats 0ms

/halogy/application/modules/pages/controllers/pages.php

https://bitbucket.org/haloweb/halogy-1.0/
PHP | 457 lines | 327 code | 57 blank | 73 comment | 68 complexity | aec3f5cdccb858c518a0a8fedda8831f MD5 | raw file
  1<?php
  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 Pages extends Controller {
 20	
 21	function Pages()
 22	{
 23		parent::Controller();
 24
 25		// get siteID, if available
 26		if (defined('SITEID'))
 27		{
 28			$this->siteID = SITEID;
 29		}
 30	}
 31	
 32	function index()
 33	{			
 34		if ($this->uri->segment(1))
 35		{
 36			// deprecated uri code (now its always just the uri string)
 37			$num = 1;
 38			$uri = '';
 39			while ($segment = $this->uri->segment($num))
 40			{
 41				$uri .= $segment.'/';
 42				$num ++;
 43			}
 44			$new_length = strlen($uri) - 1;
 45			$uri = substr($uri, 0, $new_length);			
 46		}
 47		else
 48		{
 49			$uri = 'home';
 50		}
 51		
 52		$this->view($uri);
 53	}
 54	
 55	function view($page, $sendthru = '', $module = FALSE, $return = FALSE)
 56	{	
 57		// set default parse file
 58		$parseFile = 'default';
 59		
 60		// check the page is not ajax or a return
 61		if (!$this->core->is_ajax() && !$return)
 62		{
 63			// check to see if the user is logged in as admin and has rights to edit the page inline
 64			if ($this->session->userdata('session_admin'))
 65			{
 66				$parseFile = 'view_template_inline';			
 67			}
 68		}
 69		
 70		// handle web form
 71		if (count($_POST) && !$module)
 72		{
 73			if (!$sendthru['message'] = $this->core->web_form())
 74			{
 75				$sendthru['errors'] = validation_errors();
 76			}
 77		}
 78		
 79		// see if the cms is to generate a page from a module or a function of the site
 80		if ($module)
 81		{
 82			// set template tag
 83			$this->template->template['page:template'] = $page;
 84			
 85			// look up the page to see if there is any overriding meta data
 86			if ($metadata = $this->core->get_page(FALSE, substr($this->uri->uri_string(), 1)))
 87			{
 88				// redirect if set
 89				if ($metadata['redirect'])
 90				{
 91					$metadata['redirect'] = preg_replace('/^\//', '', $metadata['redirect']);
 92					redirect($metadata['redirect']);
 93				}				
 94				
 95				if ($metadata['active'] ||
 96					(!$metadata['active'] && $this->session->userdata('session_admin') &&
 97						((@in_array('pages_edit', $this->permission->permissions) && in_array('pages_all', $this->permission->permissions)) ||
 98						(!@in_array('pages_all', $this->permission->permissions) && $this->session->userdata('groupID') && $metadata['groupID'] == $this->session->userdata('groupID')))
 99					)
100				)
101				{
102					// set a title as long as its not a default
103					if ($metadata['title'] != $metadata['pageName'])
104					{
105						$sendthru['page:title'] = $metadata['title'];
106					}
107
108					// set meta data
109					$sendthru['page:keywords'] = $metadata['keywords'];
110					$sendthru['page:description'] = $metadata['description'];
111				}
112				else
113				{
114					show_404();
115				}
116			}
117			
118			// get template by name
119			if ($pagedata = $this->core->get_module_template($page))
120			{
121				// get template and blocks from cms
122				$module = $this->template->generate_template($pagedata);
123	
124				// merge the sendthru data with page data		
125				$template = (is_array($sendthru)) ? array_merge($module, $sendthru) : $module;
126
127				// set a null title
128				$template['page:title'] = (!isset($sendthru['page:title'])) ? $this->site->config['siteName'] : $sendthru['page:title'];
129	
130				// output data
131				if ($return === FALSE)
132				{
133					$this->parser->parse($parseFile, $template);
134				}
135				else
136				{
137					return $this->parser->parse($parseFile, $template, TRUE);
138				}
139			}	
140
141			// else just show it from a file template
142			else
143			{	
144				// get module name
145				$module = (is_string($module)) ? $module : $this->uri->segment(1);
146
147				// get module template
148				if ($file = @file_get_contents(APPPATH.'modules/'.$module.'/views/templates/'.$page.'.php'))
149				{	
150					// make a template out of the file
151					$module = $this->template->generate_template(FALSE, $file);
152	
153					// merge the sendthru data with page data		
154					$template = (is_array($sendthru)) ? array_merge($module, $sendthru) : $module;
155
156					// set a null title
157					$template['page:title'] = (!isset($sendthru['page:title'])) ? $this->site->config['siteName'] : $sendthru['page:title'];
158
159					// output data
160					if ($return === FALSE)
161					{
162						$this->parser->parse($parseFile, $template);
163					}
164					else
165					{
166						return $this->parser->parse($parseFile, $template, TRUE);
167					}
168				}
169				else
170				{
171					show_error('Templating error!');
172				}
173			}
174		}
175
176		// else just grab the page from cms
177		elseif ($this->session->userdata('session_admin') && $pagedata = $this->core->get_page(FALSE, $page))
178		{
179			// redirect if set
180			if ($pagedata['redirect'])
181			{
182				$pagedata['redirect'] = preg_replace('/^\//', '', $pagedata['redirect']);
183				redirect($pagedata['redirect']);
184			}
185			
186			// show cms with admin functions
187			if ((@in_array('pages_edit', $this->permission->permissions) && in_array('pages_all', $this->permission->permissions)) ||
188			(!@in_array('pages_all', $this->permission->permissions) && $this->session->userdata('groupID') && $pagedata['groupID'] == $this->session->userdata('groupID')))
189			{
190				$versionIDs = array();
191				
192				// check that this is not the live version and then add page version
193				if ($versions = $this->core->get_versions($pagedata['pageID']))
194				{
195					foreach ($versions as $version)
196					{
197						$versionIDs[] = $version['versionID'];
198					}
199				}
200				if ((!$pagedata['versionID'] && !$pagedata['draftID']) || @in_array($pagedata['draftID'], $versionIDs))
201				{
202					$this->core->add_draft($pagedata['pageID']);
203					redirect($this->uri->uri_string());
204				}		
205				
206				// set no cache headers
207				$this->output->set_header('Cache-Control: no-Store, no-Cache, must-revalidate');
208				$this->output->set_header('Expires: -1');
209				
210				// show admin inline editor
211				$output = $this->core->generate_page($pagedata['pageID'], TRUE);
212				
213				// merge output with any other data
214				$output = (is_array($sendthru)) ? array_merge($output, $sendthru) : $output;
215
216				// output images
217				$where = '';
218				if (!@in_array('images_all', $this->permission->permissions))
219				{
220					$where['userID'] = $this->session->userdata('userID');
221				}
222				$images = $this->core->viewall('images', $where, array('dateCreated', 'desc'), 99);
223				$output['images'] = $images['images'];
224
225				// parse with main cms template
226				if ($return === FALSE)
227				{
228					$this->parser->parse($parseFile, $output);
229				}
230				else
231				{
232					return $this->parser->parse($parseFile, $output, TRUE);
233				}
234			}
235
236			// otherwise they are admin but they don't have permission to this page
237			else
238			{
239				// just get normal page
240				$output = $this->core->generate_page($pagedata['pageID']);
241
242				// merge output with any other data
243				$output = (is_array($sendthru)) ? array_merge($output, $sendthru) : $output;
244
245				// parse with main cms template				
246				if ($return === FALSE)
247				{
248					$this->parser->parse($parseFile, $output);
249				}
250				else
251				{
252					return $this->parser->parse($parseFile, $output, TRUE);
253				}				
254			}
255		}
256
257		// display normal page
258		elseif ($pagedata = $this->core->get_active_page($page))
259		{
260			// redirect if set
261			if ($pagedata['redirect'])
262			{
263				$pagedata['redirect'] = preg_replace('/^\//', '', $pagedata['redirect']);
264				redirect($pagedata['redirect']);
265			}
266		
267			// add view
268			$this->core->add_view($pagedata['pageID']);
269
270			// merge output with any other data
271			$pagedata = (is_array($sendthru)) ? array_merge($pagedata, $sendthru) : $pagedata;
272
273			// just get normal page
274			$output = $this->core->generate_page($pagedata['pageID']);
275
276			// merge output with any other data
277			$output = (is_array($sendthru)) ? array_merge($output, $sendthru) : $output;
278
279			// set no cache headers
280			$this->output->set_header('Content-Type: text/html');
281
282			// parse with main cms template
283			if ($return === FALSE)
284			{
285				$this->parser->parse($parseFile, $output);
286			}
287			else
288			{
289				return $this->parser->parse($parseFile, $output, TRUE);
290			}
291		}
292
293		// if nothing then 404 it!
294		else
295		{
296			show_404();
297		}		
298	}
299
300	// file viewer
301	function files($type = '', $ref = '')
302	{
303		// format filename
304		$filenames = @explode('.', $ref);
305		$extension = end($filenames);
306		$filename = str_replace('.'.$extension, '', $ref);
307		
308		// css
309		if ($type == 'css')
310		{
311			if ($include = $this->core->get_include($ref))
312			{
313				$this->output->set_header('Content-Type: text/css');
314				$this->output->set_header('Expires: ' . gmdate('D, d M Y H:i:s', time()+14*24*60*60) . ' GMT');
315				
316				$this->output->set_output($include['body']);
317			}
318			else
319			{
320				show_404();
321			}
322		}
323
324		// js
325		elseif ($type == 'js')
326		{
327			if ($include = $this->core->get_include($ref))
328			{
329				$this->output->set_header('Content-Type: text/javascript');
330				$this->output->set_header('Expires: ' . gmdate('D, d M Y H:i:s', time()+14*24*60*60) . ' GMT');				
331
332				$this->output->set_output($include['body']);
333			}
334			else
335			{
336				show_404();
337			}
338		}
339
340		// images
341		elseif ($type == 'images' || $type == 'gfx' | $type == 'thumbs')
342		{
343			if ($extension == 'gif')
344			{
345				$this->output->set_header('Content-Type: image/gif');
346			}
347			elseif ($extension == 'jpg' || $extension == 'jpeg')
348			{
349				$this->output->set_header('Content-Type: image/pjpeg');
350				$this->output->set_header('Content-Type: image/jpeg');
351			}
352			elseif ($extension == 'png')
353			{
354				$this->output->set_header('Content-Type: image/png');
355			}
356			else
357			{
358				show_404();
359			}
360
361			// output image
362			if ($image = $this->uploads->load_image($filename))
363			{
364				// set thumbnail
365				$image = ($type == 'thumbs' && $thumb = $this->uploads->load_image($filename, TRUE)) ? $thumb : $image;
366
367				$imageOutput = file_get_contents('.'.$image['src']);
368
369				$fs = stat('.'.$image['src']);
370				
371				$this->output->set_header("Etag: ".sprintf('"%x-%x-%s"', $fs['ino'], $fs['size'],base_convert(str_pad($fs['mtime'],16,"0"),10,16)));
372				$this->output->set_header('Expires: '.gmdate('D, d M Y H:i:s', time()+14*24*60*60) . ' GMT');
373				$this->output->set_output($imageOutput);
374			}
375			else
376			{
377				show_404();
378			}
379		}
380
381		// uploaded files
382		elseif ($type == 'files')
383		{	
384			// get the file, by reference or by filename
385			if (@!$filenames[1])
386			{
387				$file = $this->uploads->load_file($ref, TRUE);
388			}
389			else
390			{
391				$file = $this->uploads->load_file($filename, TRUE);
392			}
393			
394			if ($file)
395			{
396				if (@$file['error'] == 'expired')
397				{
398					show_error('Sorry, this download has now expired. Please contact support.');
399				}
400				elseif (@$file['error'] == 'premium')
401				{
402					show_error('This is a premium item and must be purchased in the shop.');
403				}
404				else
405				{
406					// set headers
407					if ($extension == 'ico')
408					{
409						$this->output->set_header('Content-Type: image/x-icon');
410					}
411					elseif ($extension == 'swf')
412					{
413						$this->output->set_header('Content-Type: application/x-shockwave-flash');
414					}
415					else
416					{	
417						$this->output->set_header("Pragma: public");
418						$this->output->set_header("Expires: -1");
419						$this->output->set_header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
420						$this->output->set_header("Content-Type: application/force-download");
421						$this->output->set_header("Content-Type: application/octet-stream");
422						$this->output->set_header("Content-Length: " .(string)(filesize('.'.$file['src'])) );
423						$this->output->set_header("Content-Disposition: attachment; filename=".$file['fileRef'].$file['extension']);
424						$this->output->set_header("Content-Description: File Transfer");
425					}
426					
427					// output file contents
428					$output = file_get_contents('.'.$file['src']);
429					$this->output->set_output($output);
430				}
431			}
432			else
433			{
434				show_404();
435			}
436		}
437
438		// else 404 it
439		else
440		{
441			show_404();
442		}
443	}
444
445	function _captcha_check()
446	{
447		if (!$this->core->captcha_check())
448		{
449			return FALSE;
450		}
451		else
452		{
453			return TRUE;
454		}
455	}
456
457}