PageRenderTime 83ms CodeModel.GetById 42ms app.highlight 33ms RepoModel.GetById 1ms app.codeStats 0ms

/halogy/application/modules/images/controllers/admin.php

https://bitbucket.org/haloweb/halogy-1.0/
PHP | 547 lines | 368 code | 86 blank | 93 comment | 68 complexity | 0827c25bf7025e489c29532548e26bb3 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 Admin extends Controller {
 20
 21	// set defaults
 22	var $table = 'images';								// table to update
 23	var $includes_path = '/includes/admin';				// path to includes for header and footer
 24	var $redirect = '/admin/images/viewall';			// default redirect
 25	var $objectID = 'imageID';							// default unique ID									
 26	var $permissions = array();
 27	var $sitePermissions = array();	
 28	var $selections = array();
 29	
 30	function Admin()
 31	{
 32		parent::Controller();
 33
 34		// check user is logged in, if not send them away from this controller
 35		if (!$this->session->userdata('session_admin'))
 36		{
 37			redirect('/admin/login/'.$this->core->encode($this->uri->uri_string()));
 38		}
 39
 40		// get site permissions and redirect if it don't have access to this module
 41		$this->permission->sitePermissions = $this->permission->get_group_permissions($this->site->config['groupID']);
 42				
 43		// get permissions and redirect if they don't have access to this module
 44		if (!$this->permission->permissions)
 45		{
 46			if (@$this->core->is_ajax())
 47			{
 48				die('<p>Sorry, you do not have permissions to do what you just tried to do. <a class="halogycms_close" href="#">Close</a></p>');
 49			}
 50			else
 51			{			
 52				redirect('/admin/dashboard/permissions');
 53			}
 54		}
 55		if (!in_array($this->uri->segment(2), $this->permission->permissions))
 56		{
 57			if (@$this->core->is_ajax())
 58			{
 59				die('<p>Sorry, you do not have permissions to do what you just tried to do. <a class="halogycms_close" href="#">Close</a></p>');
 60			}
 61			else
 62			{			
 63				redirect('/admin/dashboard/permissions');
 64			}
 65		}
 66
 67		// get preset selections for this module
 68		$selections = $this->session->userdata('selections');
 69		$this->selections = (is_array($selections)) ? @$selections[$this->uri->segment(2)] : '';
 70
 71		// get siteID, if available
 72		if (defined('SITEID'))
 73		{
 74			$this->siteID = SITEID;
 75		}
 76
 77		// load libs etc
 78		$this->load->model('images_model', 'images');
 79	}
 80	
 81	function index()
 82	{
 83		redirect($this->redirect);
 84	}
 85	
 86	function viewall($folderID = '')
 87	{
 88		if (count($_FILES))
 89		{			
 90			// allowed ZIP mime types
 91			$allowedZips = array('application/x-zip', 'application/zip', 'application/x-zip-compressed');
 92			
 93			if ($this->input->post('upload_zip'))
 94			{
 95				if (substr($_FILES['zip']['name'],-3) == 'zip' && in_array($_FILES['zip']['type'], $allowedZips))
 96				{
 97					// get started
 98					$success = FALSE;
 99					$this->load->library('zip');
100					$this->load->library('encrypt');
101					$this->load->library('image_lib');
102	
103					// unzip files
104					$uploadsPath = $this->uploads->uploadsPath;
105					
106					$zip = zip_open($_FILES['zip']['tmp_name']);
107					if ($zip)
108					{
109						// cycle through the zip
110						while ($zip_entry = zip_read($zip))
111						{
112							if (!preg_match('/(\_)+MACOSX/', zip_entry_name($zip_entry)) && preg_match('/\.(jpg|gif|png)$/i', zip_entry_name($zip_entry)))
113							{
114								if (zip_entry_filesize($zip_entry) > 300000)
115								{
116									$this->form_validation->set_error('<p>Some files were too big to upload. Please only use small gfx files under 300kb.</p>');
117								}
118								else
119								{
120									// format filename
121									$filenames = explode('.', zip_entry_name($zip_entry));
122									$filename = trim(basename($filenames[0]));
123									$extension = end($filenames);
124									
125									// get file name
126									$imageRef = url_title(trim(strtolower($filename)));
127		
128									// check ref is unique and upload
129									if ($this->form_validation->unique($imageRef, 'images.imageRef'))
130									{																
131										// set stuff
132										$this->core->set['dateCreated'] = date("Y-m-d H:i:s");
133										$this->core->set['imageName'] = 'Graphic';
134										$this->core->set['filename'] = md5($filename).'.'.$extension;
135										$this->core->set['imageRef'] = $imageRef;
136										$this->core->set['filesize'] = floor(zip_entry_filesize($zip_entry) / 1024);
137										$this->core->set['groupID'] = 1;
138										$this->core->set['userID'] = $this->session->userdata('userID');
139
140										// update and then unset easy
141										if ($this->core->update('images'));
142																				
143										// upload file
144										$fp = fopen('.'.$uploadsPath.'/'.md5($filename).'.'.$extension, "w+");				
145										if (zip_entry_open($zip, $zip_entry, "r"))
146										{
147											$buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
148											zip_entry_close($zip_entry);
149										}
150										fwrite($fp, $buf);
151										fclose($fp);
152										
153										// get image size
154										$imageSize = @getimagesize('.'.$uploadsPath.'/'.md5($filename).'.'.$extension);
155
156										// make a thumbnail
157										if ($imageSize[0] > $this->uploads->thumbSize || $imageSize[1] > $this->uploads->thumbSize)
158										{
159											$config['image_library'] = 'gd2';
160											$config['source_image'] = '.'.$uploadsPath.'/'.md5($filename).'.'.$extension;
161											$config['create_thumb'] = true;
162											$config['maintain_ratio'] = true;
163											$config['width'] = $this->uploads->thumbSize;
164											$config['height'] = $this->uploads->thumbSize;
165										
166											$this->image_lib->initialize($config);
167											$this->image_lib->resize();
168										}
169	
170										$success = TRUE;							
171									}
172								}
173							}
174						}
175						zip_close($zip);
176					}
177	
178					// redirect
179					if ($success === TRUE)
180					{
181						redirect('/admin/images/viewall/'.(($this->input->post('folderID')) ? $this->input->post('folderID') : ''));
182					}
183				}
184				else
185				{
186					$this->form_validation->set_error('<p>There was a problem opening the zip file, sorry.</p>');
187				}				
188			}
189
190			// upload image
191			elseif ($oldFileName = @$_FILES['image']['name'])
192			{
193				$this->uploads->allowedTypes = 'jpg|gif|png';
194				
195				// get image name
196				$imageName = ($this->input->post('imageName')) ? $this->input->post('imageName') : preg_replace('/.([a-z]+)$/i', '', $oldFileName);
197				
198				// set image reference and only add to db if its unique
199				$imageRef = url_title(trim(substr(strtolower($imageName),0,30)));
200		
201				if ($this->form_validation->unique($imageRef, 'images.imageRef'))
202				{	
203					if ($imageData = $this->uploads->upload_image())
204					{
205						$this->core->set['filename'] = $imageData['file_name'];
206						$this->core->set['filesize'] = $imageData['file_size'];						
207					}
208		
209					// get image errors if there are any
210					if ($this->uploads->errors)
211					{
212						$this->form_validation->set_error($this->uploads->errors);
213					}
214					else
215					{						
216						// set image ref
217						$this->core->set['class'] = 'default';
218						$this->core->set['imageRef'] = $imageRef;
219						$this->core->set['imageName'] = ($this->input->post('imageName')) ? $this->input->post('imageName') : 'Image';
220						$this->core->set['dateCreated'] = date("Y-m-d H:i:s");
221						$this->core->set['userID'] = $this->session->userdata('userID');												
222				
223						// update
224						if ($this->core->update('images'))
225						{
226							// where to redirect to
227							redirect('/admin/images/viewall/'.(($this->input->post('folderID')) ? $this->input->post('folderID') : ''));
228						}			
229					}
230				}
231				else
232				{
233					$this->form_validation->set_error('<p>The image reference you entered has already been used, please try another.</p>');
234				}		
235			}			
236		}
237
238		// search
239		if ($this->input->post('searchbox'))
240		{
241			$output['images'] = $this->images->search_images($this->input->post('searchbox'));
242		}
243		
244		// get images
245		else
246		{
247			// set default wheres
248			$where = array('siteID' => $this->siteID, 'deleted' => 0);
249			
250			// get preset selections for this dropdown
251			if ($folderID == '' && @array_key_exists('folderID', $this->selections))
252			{
253				$folderID = $this->selections['folderID'];
254			}
255			
256			// folderID	
257			if ($folderID != '')
258			{
259				// get ones uploaded by this user
260				if ($folderID == 'me')
261				{
262					$where['userID'] = $this->session->userdata('userID');
263				}		
264				
265				// make sure that all is not selected
266				elseif ($folderID != 'all' && $folderID != 'page' && $folderID != 'me')
267				{
268					$where['folderID'] = $folderID;
269				}
270				
271				// set preset selections for this dropdown
272				$this->session->set_userdata('selections', array($this->uri->segment(2) => array('folderID' => $folderID)));
273			}
274	
275			// check they have permissions to see all images
276			if (!@in_array('images_all', $this->permission->permissions))
277			{
278				$where['userID'] = $this->session->userdata('userID');
279			}
280			
281			// grab data and display
282			$output = $this->core->viewall($this->table, $where, NULL, 15);
283		}
284
285		// get folderID if set	
286		$output['folderID'] = $folderID;		
287
288		// get quota
289		$output['quota'] = $this->site->get_quota();
290
291		// get categories
292		$output['folders'] = $this->images->get_folders();
293
294		$this->load->view($this->includes_path.'/header');
295		$this->load->view('admin/viewall',$output);
296		$this->load->view($this->includes_path.'/footer');
297	}
298
299	function edit($imageID, $redirect = '', $popup = FALSE)
300	{
301		// required
302		$this->core->required = array(
303			'imageRef' => array('label' => 'Image name', 'rules' => 'required|unique[images.imageRef]')
304		);
305		
306		// set object ID
307		$objectID = array($this->objectID => $imageID);
308
309		// get values
310		$output['data'] = $this->core->get_values($this->table, $objectID);
311
312		// handle post
313		if (count($_POST))
314		{
315			// set image reference and only add to db if its unique
316			$imageRef = url_title(trim(substr(strtolower($this->input->post('imageRef')),0,30)));
317			
318			if ($oldFileName = @$_FILES['image']['name'])
319			{
320				$this->uploads->allowedTypes = 'jpg|gif|png';
321						
322				if (!$this->form_validation->unique($imageRef, 'images.imageRef') && $this->input->post('imageRef') != $output['data']['imageRef'])
323				{	
324					$this->uploads->errors = '<p>The image reference you entered has already been used, please try another.</p>';
325				}
326				else
327				{
328					if ($imageData = $this->uploads->upload_image())
329					{
330						$this->core->set['filename'] = $imageData['file_name'];
331						$this->core->set['filesize'] = $imageData['file_size'];
332					}					
333				}
334			}
335
336			// get image errors if there are any
337			if ($this->uploads->errors)
338			{
339				$this->form_validation->set_error($this->uploads->errors);
340			}
341			else
342			{			
343				// set image ref
344				$this->core->set['imageRef'] = $imageRef;
345				$this->core->set['dateModified'] = date("Y-m-d H:i:s");	
346		
347				// update
348				if ($this->core->update('images', $objectID))
349				{
350					// if its not coming from ajax then just go to admin
351					if ($redirect && !$popup)
352					{						
353						$redirect = $this->core->decode($redirect);
354					}
355					elseif (!$redirect && !$popup)
356					{						
357						$redirect = '/admin/images/viewall';
358					}
359					
360					// where to redirect to
361					redirect($redirect);
362				}			
363			}
364		}
365
366		// define view (based on popup)
367		$view = ($popup) ? 'admin/popup' : 'admin/edit';
368		
369		// get categories
370		$output['folders'] = $this->images->get_folders();
371		
372		// templates
373		if (!@$this->core->is_ajax()) $this->load->view($this->includes_path.'/header');
374		$this->load->view($view, $output);
375		if (!@$this->core->is_ajax()) $this->load->view($this->includes_path.'/footer');
376	}
377
378	function delete($objectID, $redirect = '')
379	{
380		// delete image
381		$query = $this->db->get_where($this->table, array($this->objectID => $objectID));
382		if ($row = $query->row_array())
383		{
384			$this->uploads->delete_file($row['filename']);
385		}
386		
387		if ($this->core->delete($this->table, array($this->objectID => $objectID)));
388		{	
389			$redirect = ($redirect) ? $this->core->decode($redirect) : $this->redirect;
390		
391			// where to redirect to
392			redirect($redirect);
393		}
394	}
395
396	function popup($encodedID)
397	{
398		// decodes the image ID and splits it in to the URI and image ID
399		$decode = explode('|', $this->core->decode($encodedID));
400
401		$uri = $decode[0];
402		$imageID = $decode[1];
403
404		$this->edit($imageID, $uri, TRUE);
405	}
406
407	function browser()
408	{
409		// set default wheres
410		$where = array('siteID' => $this->siteID, 'deleted' => 0);
411
412		// check they have permissions to see all images
413		if (!@in_array('images_all', $this->permission->permissions))
414		{
415			$where['userID'] = $this->session->userdata('userID');
416		}
417
418		// grab data and display
419		$output = $this->core->viewall($this->table, array('folderID' => 0), 'imageRef', 999);
420
421		// get folders
422		if ($folders = $this->images->get_folders())
423		{
424			foreach($folders as $folder)
425			{
426				// grab data and display
427				$data = $this->core->viewall($this->table, array('folderID' => $folder['folderID']), 'imageRef', 999);
428				$output['folders'][$folder['folderID']]['folderName'] = $folder['folderName'];
429				$output['folders'][$folder['folderID']]['images'] = $data['images'];
430			}
431		}
432
433		$this->load->view('admin/browser',$output);
434	}
435	
436	function folders()
437	{
438		// check permissions for this page
439		if (!in_array('images', $this->permission->permissions))
440		{
441			redirect('/admin/dashboard/permissions');
442		}
443				
444		// required fields
445		$this->core->required = array('folderName' => 'Folder Name');
446
447		// set date
448		$this->core->set['dateCreated'] = date("Y-m-d H:i:s");
449		$this->core->set['folderSafe'] = strtolower(url_title($this->input->post('folderName')));
450
451		// get values
452		$output = $this->core->get_values('image_folders');
453
454		// update
455		if ($this->core->update('image_folders') && count($_POST))
456		{
457			// where to redirect to
458			redirect('/admin/images/folders');
459		}
460
461		$output['folders'] = $this->images->get_folders();
462
463		$this->load->view($this->includes_path.'/header');
464		$this->load->view('admin/folders',$output);
465		$this->load->view($this->includes_path.'/footer');
466	}
467
468	function edit_folder()
469	{
470		// check permissions for this page
471		if (!in_array('images', $this->permission->permissions))
472		{
473			redirect('/admin/dashboard/permissions');
474		}
475
476		// go through post and edit each list item
477		$listArray = $this->core->get_post();
478		if (count($listArray))
479		{
480			foreach($listArray as $ID => $value)
481			{
482				if ($ID != '' && sizeof($value) > 0)
483				{	
484					// set object ID
485					$objectID = array('folderID' => $ID);
486					$this->core->set['folderName'] = $value['folderName'];
487					$this->core->set['folderSafe'] = strtolower(url_title($value['folderName']));
488					$this->core->update('image_folders', $objectID);
489				}
490			}
491		}
492
493		// where to redirect to
494		redirect('/admin/images/folders');		
495	}	
496
497	function delete_folder($folderID)
498	{
499		// check permissions for this page
500		if (!in_array('images', $this->permission->permissions))
501		{
502			redirect('/admin/dashboard/permissions');
503		}
504				
505		// where
506		$objectID = array('folderID' => $folderID);	
507		
508		if ($this->core->soft_delete('image_folders', $objectID))
509		{
510			// set children to no parent
511			$this->images->update_children($folderID);
512			
513			// where to redirect to
514			redirect('/admin/images/folders');
515		}		
516	}
517
518	function order($field = '')
519	{
520		$this->core->order(key($_POST), $field);
521	}
522	
523	function ac_images()
524	{	
525		$q = strtolower($_POST["q"]);
526		if (!$q) return;
527		
528		// form dropdown
529		$results = $this->images->search_images($q);
530		
531		// go foreach
532		foreach((array)$results as $row)
533		{
534			$items[$row['imageRef']] = $row['imageName'];
535		}
536		
537		// output
538		$output = '';
539		foreach ($items as $key=>$value)
540		{
541			$output .= "$key|$value\n";
542		}
543		
544		$this->output->set_output($output);
545	}
546
547}