you /Ionize/application/libraries/MY_Controller.php

Language PHP Lines 735
MD5 Hash 227ac9f12b7595a030597b462b9f3bcc Estimated Cost $7,205 (why?)
Repository http://you.googlecode.com/svn/trunk/ View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Ionize
 *
 * @package		Ionize
 * @author		Ionize Dev Team
 * @license		http://ionizecms.com/doc-license
 * @link		http://ionizecms.com
 * @since		Version 0.90
 */

// ------------------------------------------------------------------------


/**
 * MY_Controller Class
 *
 * Extends CodeIgniter Controller
 * Basic Model loads and settings set.
 *
 */
class MY_Controller extends Controller 
{
	/* Template array
	 * This array will be send to the view in case of standard output
	 * Used by $this->output
	 */
	protected $template = array();

	/* Default FTL tag
	 */
	protected $context_tag = 'ion';

	/**
	 * Contructor
	 *
	 */
    public function __construct()
	{
		parent::__construct();

		// Check the database settings
		if ($this->test_database_config() === false)
		{
			redirect(base_url().'install/');
			die();
		}

		$this->load->database();

		// Models
		$this->load->model('base_model', '', true);
		$this->load->model('settings_model', '', true);

		// Helpers
		$this->load->helper('file');
		$this->load->helper('trace');
		
		/*
		 * Language / Languages
		 *
		 */
		// Get all the website languages from DB and store them into config file "languages" key
		$languages = $this->settings_model->get_languages();

		// Put DB languages array to Settings
		Settings::set_languages($languages);	


		if( Connect()->is('editors', true))
		{
			Settings::set_all_languages_online();
		}

		/*
		 * Settings
		 *
		 */
		// 	Lang independant settings : google analytics string, filemanager, etc.
		//	Each setting is accessible through : 
		//	Settings::get('setting_name');
		Settings::set_settings_from_list($this->settings_model->get_settings(), 'name','content');

		/*
		 * Security : No access if install folder is already there
		 *
		 */
//		if (config_item('protect_installer') == TRUE)
//		{
			// Try to find the installer class
			$installer = glob(BASEPATH.'../*/class/installer'.EXT);
	
			// If installer class is already here, avoid site access
			if (!empty($installer))
			{
				// Get languages codes from availables languages folder/translation file
				$languages = $this->settings_model->get_admin_langs();
				
				if ( ! in_array(config_item('language_abbr'), $languages))
					$this->config->set_item('language_abbr', config_item('default_lang'));
				
				$this->lang->load('admin', config_item('language_abbr'));
				
				Theme::set_theme('admin');
				
				// Set the view to output
				$this->output('delete_installer');
				
				// Display the view directly
				$this->output->_display();
				
				// Don't do anything more
				die();
			}
//		}
    }


	// ------------------------------------------------------------------------


    /**
     * Outputs the global template regarding to the used library to do this stuff
     *
     * @param	string	The view name
     *
     */
    public function output($view)
    {
    	Theme::output($view, $this->template);
    }


	// ------------------------------------------------------------------------


	/**
	 * Returns true if this is an XMLHttpRequest (ie. Javascript).
	 * 
	 * This requires a special header to be sent from the JS
	 * (usually the Javascript frameworks' Ajax/XHR methods add it automatically):
	 * 
	 * <code>
	 * X-Requested-With: XMLHttpRequest
	 * </code>
	 * 
	 * @return bool
	 */
	public function is_xhr()
	{
		return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
	}


	/**
	 * Returns true if database settings are correct
	 *
	 */
	public function test_database_config()
	{
		require(config_item('base_path') . '/application/config/database.php');
				
		if ($db[$active_group]['hostname'] == '' || $db[$active_group]['username'] == '' || $db[$active_group]['database'] == '')
		{
			return false;
		}
		return true;
	}
	
	public function get_modules_config()
	{
		// Modules config include
		$config_files = glob(config_item('module_path').'*/config/config.php');

		if ( ! empty($config_files))
		{
			// Add each module config element to the main config 
			foreach($config_files as $file)
			{
				include($file);
				
				if ( isset($config))
				{
					foreach($config as $k=>$v)
						$this->config->set_item($k, $v);
	
					unset($config);
				}
			}
		}
	}
} 
// End MY_Controller


// ------------------------------------------------------------------------


class Base_Controller extends MY_Controller
{
	/**
	 * Constructor
	 *
	 */		
    public function __construct()
    {
        parent::__construct();

// $this->output->enable_profiler(true);
//		$this->output->cache(100);
		
		// Unlock filtering if admin or editor users is logged in
//		$this->load->library('connect');

		$this->connect = Connect::get_instance();


		// Libraries
		$this->load->library('structure');	
		$this->load->library('widget');

		// FTL parser
		require_once APPPATH.'libraries/ftl/parser.php';

		// Models
		$this->load->model('structure_model', '', true);
		$this->load->model('menu_model', '', true);

		// Modules config
		$this->get_modules_config();


		/*
		 * Theme
		 *
		 */
		// Set the current theme
		Theme::set_theme(Settings::get('theme'));

		// Theme config file
		// Overwrite Ionize standard config.
		if (is_file($file = Theme::get_theme_path().'config/config.php'))
		{
			include($file);
			if ( ! empty($config))
			{
				foreach($config as $k=>$v)
					$this->config->set_item($k, $v);

				unset($config);
			}
		}

		/*
		 * Menus
		 *
		 */
		Settings::set('menus', $this->menu_model->get_list());
		  

		/*
		 * Language
		 *
		 */
		// Get all the website languages from DB and store them into config file "languages" key
		$languages = $this->settings_model->get_languages();

		// Put all DB languages array to Settings
		Settings::set_languages($languages);	

		// Set all languages online if conected as editor or more
		if( Connect()->is('editors', true))
		{
			Settings::set_all_languages_online();
		}

		
		// Simple languages code array, used to detect if Routers found language is in DB languages
		$lang_codes = array();
		foreach(Settings::get_online_languages() as $language)
		{
			$online_lang_codes[] = $language['lang'];
		}


		// If Router detected that the lang code is not in DB languages, set it to the DB default one
		if ( ! in_array(config_item('language_abbr'), $online_lang_codes))
		{
			// Settings::get_lang('default') returns the DB default lang code
			Settings::set('current_lang', Settings::get_lang('default'));
			
			$this->config->set_item('language_abbr', Settings::get_lang('default'));
		}
		else
		{
			// Store the current lang code (found by Router) to Settings
			Settings::set('current_lang', config_item('language_abbr'));		
		}

		// Lang dependant settings for the current language : Meta, etc.
		Settings::set_settings_from_list($this->settings_model->get_lang_settings(config_item('language_abbr')), 'name','content');


		/*
		 * Static language
		 *
		 */
		$lang_folder = Theme::get_theme_path().'language/'.Settings::get_lang().'/';

		// Core languages files : Including
		$lang_files = glob(APPPATH.'language/'.Settings::get_lang().'/*_lang.php');

		// Theme languages files : Including. Can be empty
		$lf = glob(Theme::get_theme_path().'language/'.Settings::get_lang().'/*_lang.php');
		if ( !empty($lf))
			$lang_files = array_merge((Array)$lang_files, $lf);
		
		// Modules languages files : Including. Can be empty
		$mla = glob(config_item('module_path').'*/language/'.Settings::get_lang().'/*_lang.php');
		if ( !empty($mla))
			$lang_files = array_merge((Array)$lang_files, $mla);

		$modules = glob(config_item('module_path').'*');


		// Add the module path to the Finder
		if (!empty($modules))
		{
			foreach ($modules as $module)
			{
				Finder::add_path($module.'/');
			}
		}
		
		// Widgets languages translations loading
		// Now done by the Widget library


		// Load all modules lang files
		if ( ! empty($lang_files))
		{
			foreach($lang_files as $l)
			{
				if (is_file($l) && '.'.end(explode('.', $l)) == EXT )
				{
					include $l;
					if ( ! empty($lang))
					{
						$this->lang->language = array_merge($this->lang->language, $lang);
						unset($lang);
					}
				}
			}
		}
	}


	protected function parse($string, $context, $tag_prefix = 'ion')
	{
		$p = new FTL_Parser($context, array('tag_prefix' => $tag_prefix));

		return $p->parse($string);
	}


	protected function render($view, &$context = null, $return = false)
	{
		// Loads the view to parse
		$parsed = Theme::load($view);

		// We can now check if the file is a PHP one or a FTL one
		if (substr($parsed, 0, 5) == '<?php')
		{
			$parsed = $this->load->view($view, array(), true);					
		}
		else
		{
			$parsed = $this->parse($parsed, $context);
		}
		
		// Returns the result or output it directly
		if ($return)
			return $parsed;
		else
			$this->output->set_output($parsed);
	}
}



// ------------------------------------------------------------------------



/**
 * MY_Admin Class
 *
 * Extends MY_Controller
 *
 */
class MY_Admin extends MY_Controller
{
	/* Response message type
	 * Used by controller to send answer to request
	 * can be 'error', 'notice', 'success'
	 *
	 */
	public $message_type = '';
	
	/* Response message to the user
	 * Human understandable message
	 *
	 */
	public $message = '';
	
	/* Array of HTMLDomElement to update with corresponding update URL
	 * Array (
	 *		'htmlDomElement' => 'controller/method/'
	 * );
	 *
	 */
	public $update = array();
	
	/* Current element ID
	 *
	 */
	public $id;
	
	/* Javascript callback function 
	 * Not implemented yet
	 *
	 */
	public $callback;


	/**
	 * Constructor
	 *
	 */		
    public function __construct()
    {
        parent::__construct();

		// Redirect the not authorized user to the login panel. See /application/config/connect.php
		Connect()->restrict_type_redirect = array(
					'uri' => '/admin/user/login',
					'flash_msg' => 'You have been denied access to %s',
					'flash_use_lang' => false,
					'flash_var' => 'error');

//	$this->output->enable_profiler(true);

		// PHP standard session is mandatory for MCE FileManager authentication
		// and other external lib
		session_start();
		$_SESSION['isLoggedIn'] = true;

		// Librairies
		$this->connect = Connect::get_instance();
		
		// Current user		
		$this->template['current_user'] = $this->connect->get_current_user();

		// Set the admin theme as current theme
		Theme::set_theme('admin');
		
		
		/*
		 * Admin languages : Depending on installed translations in /application/languages/
		 * The Admin translations are only stored in the translation file /application/languages/xx/admin_lang.php
		 *
		 */
		// Set admin lang codes array
		Settings::set('admin_languages', $this->settings_model->get_admin_langs());
		
		// Set Router's found language code as current language
		Settings::set('current_lang', config_item('language_abbr'));

		// Load the current language translations file
		$this->lang->load('admin', Settings::get_lang());


		/*
		 * Modules config
		 *
		 */
		$this->get_modules_config();

		// Including all modules languages files
		// $lang_files = glob(config_item('module_path').'*/language/'.Settings::get_lang().'/*');
		
		
		
		
		
		
		// Load all modules lang files
/*
Look how to make Modules translations available in javascript 
Notice : $yhis->lang object is transmitted to JS through load->view('javascript_lang')
 
		if ( ! empty($lang_files))
		{
			foreach($lang_files as $l)
			{
				if (is_file($l))
				{
//					$logical_name = substr($l, strripos($l, '/') +1);
//					$logical_name = str_replace('_lang.php', '', $logical_name);
//					$this->lang->load($logical_name, Settings::get_lang());

					include $l;
					$this->lang->language = array_merge($this->lang->language, $lang);
					unset($lang);

				}
			}
		}
*/

	
		/*
		 * Settings
		 *
		 */
		


		// @TODO : Remove this thing from the global CMS. Not more mandatory, but necessary for compatibility with historical version
		// Available menus
		// Each menu was a root node in which you can put several pages, wich are composing a menu.
		// Was never really implemented in ionize historical version, but already used as : menus[0]...
		Settings::set('menus', config_item('menus'));

		
		// Don't want to cache this content
		$this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate");
		$this->output->set_header("Cache-Control: post-check=0, pre-check=0", false);
		$this->output->set_header("Pragma: no-cache");
    }
    

	// ------------------------------------------------------------------------


	/**
	 * Sets an error message and call the response method
	 * 
	 * @param	string		Message to the user
	 * @param	array		Additional data to put to the answer. Optional.
	 *
	 */
    public function error($message, $addon_data = null)
    {
    	$this->message_type = 'error';
    	$this->message = $message;
    	
    	if ( !isset($this->redirect) )
    	{
    		$this->redirect = $_SERVER['HTTP_REFERER'];
    	}
    	
    	$this->response($addon_data);

		exit();
    }

   
	// ------------------------------------------------------------------------


	/**
	 * Sets a success message and call the response method
	 * 
	 * @param	string		Message to the user
	 * @param	array		Additional data to put to the answer. Optional.
	 *
	 */
    public function success($message, $addon_data = null)
    {
    	$this->message_type = 'success';
    	$this->message = $message;
    	
    	$this->response($addon_data);
    }


	// ------------------------------------------------------------------------


	/**
	 * Sets a notice message and call the response method
	 * 
	 * @param	string		Message to the user
	 * @param	array		Additional data to put to the answer. Optional.
	 *
	 */
    public function notice($message, $addon_data = null)
    {
    	$this->message_type = 'notice';
    	$this->message = $message;
    	
    	$this->response($addon_data);
    }


	// ------------------------------------------------------------------------


    /**
     * Send an answer to the browser depending on the incoming request
     * If the request cames from XHR, sends a JSON object as response
     * else, check if redirect is defined and redirect
     *
     * @param	array	Additional data to put to the answer. Optional.
     *
     */
    public function response($addon_data = null)
    {
    	/* XHR request : JSON answer
    	 * Sends a JSON javascript object
    	 *  
    	 */
    	if ($this->is_xhr() === true)
    	{
			// Basic JSON answser
    		$data = array (    	
				'message_type' => $this->message_type,
				'message' => $this->message,
				'update' => $this->update,
				'callback' => $this->callback
			);
			
			// Puts additional data to answer
			if ( ! empty($addon_data))
			{
				$data = array_merge($data, $addon_data);
			}
			
			// Adds element ID if isset
			if (isset($this->id) )
			{
				$data['id'] = $this->id;
			}
			echo json_encode($data);
    	}
    }
}


/**
 * Base Admin Module Class
 *
 * All modules Admin class must extend this class
 *
 * @author	Martin Wernstahl
 *
 */

abstract class Module_Admin extends MY_Admin
{
	protected $parent;
	
	/**
	 * Constructor
	 *
	 * @param	CI object	The CI object ($this)
	 *
	 */
	final public function __construct(Controller $c)
	{
		$this->parent = $c;
		$this->construct();
	}
	
	/**
	 * The deported construct function
	 * Should be called instead of parent::__construct by inherited classes
	 *
	 */
	abstract protected function construct();
	

	// ------------------------------------------------------------------------


	public function __get($prop)
	{
		if(property_exists($this->parent, $prop))
		{
			return $this->parent->$prop;
		}
		else
		{
			throw new Exception('Missing property');
		}
	}
	

	// ------------------------------------------------------------------------


	public function __call($method, $param)
	{
	 	if(method_exists($this->parent, $method))
	 	{
	 		return call_user_func_array(array($this->parent, $method), $param);
	 	}
	 	else
	 	{
	 		throw new BadMethodCallException(get_class($this).'::'.$method);
	 	}
	}


	// ------------------------------------------------------------------------


	/**
	 * Returns a simple view
	 * Available from each module
	 *
	 */
	public function get($view = false)
	{
		$args = func_get_args();
		$args = implode('/', $args);

		$this->output($args);
	}
}


/* End of file MY_Controller.php */
/* Location: ./application/libraries/MY_Controller.php */
Back to Top