joomla-2.5 /components/com_sef/sef_ext/sef.ext.extended.php

Language PHP Lines 405
MD5 Hash a1a4748d6a1f2c09baa39f1b3a1bc7da Estimated Cost $5,510 (why?)
Repository https://bitbucket.org/organicdevelopment/joomla-2.5.git View Raw File View Project SPDX
  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
<?php
/**
 * SEF Extended extension for Joomla!
 *
 * @author      $Author: Organic Development
 * @copyright   Organic Development Ltd.
 * @package     JoomSEF
 */

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die('Restricted access.');

require_once dirname(__FILE__).'/../sef.ext.php';
abstract class SefExtExtended extends SefExt
{
	public $parts = array();
	public $includeVars= array('task');
	public $ignoreVars= array('option','Itemid');
	public $config;
	public $params;
	public $option;
	public $controller;
	public $view;	
	public $layout;
	public $task;
	public $id;
	public $limit;
	public $limitstart;
	public $lang;
	public $lookup = true;
	public static $lookups = array();
	
	protected $view_default = '';
	protected $layout_default = 'default';
	
	/**
	 * Clean ids like id:xxx
	 */
	protected function cleanId($id){
		$id = is_array($id) ? $id : explode(':', $id);
		return (int) $id[0];
	}
	
	
	/**
	 * Adds a variable to the include array
	 * @param string $var
	 */
	public function includeVar($var = null){
		if($var) $this->includeVars[$var] = $var;
	}
	
	/**
	 * Adds a variable to the ignore array
	 * @param string $var
	 */
	public function ignoreVar($var = null){
		if($var) $this->ignoreVars[$var] = $var;
	}
	
	/**
	 * Adds a variable to the nonsef vars array
	 * @param string $var
	 */
	public function nonSefVar($var, $value = ''){
		$this->nonSefVars[$var] = $value;
	}
	
	
	/**
	 * Adds a variable to the title array and include array
	 * Also looks up in the plugin params for an alternative string
	 * @param string $var
	 */
	public function addPart( $var, $part = '', $lang_lookup = 1){
		static $lang;
		
		//Include the variable
		$this->includeVar($var);
		
		//Lookup param string for title
		if($lang_lookup && $part){
			//Load language object			
			if(!$lang){
				$lang =& JFactory::getLanguage();
				$lang->load($this->option);
			}
			$option = strtoupper(preg_replace('/com_/','',$this->option));
			
			//Check if there is a language translation for this view
			$key = $option.'_SEF_'.strtoupper($var).'_'.strtoupper($part);
			$sef_title = $lang->hasKey($key) ? $lang->_($key) : '';
			
			//If there is, use that, if not see if there is a param translation,if not use the view var
			$part = $sef_title ? $sef_title : $this->params->get($var.'_'.$part, $part);					
		}
		
		if($part) $this->parts[] = $part;
	}
	
	/**
	 * Remove a part from the array
	 */
	public function removePart($var){
		$value = $this->uri->getVar($var);
		$key = array_search($value, $this->parts);
		if($key !== false){
			unset($this->parts[$key]);
			$this->parts = array_merge(array(), $this->parts);
		} 		
	}
	
	
	
	/**
	 * Pre processing to extract item,cat,app id form url
	 * @param object $uri
	 * @return 
	 */
	public function beforeCreate(&$uri){
		
    	//Get the config
    	$this->config =& SEFConfig::getConfig();

        // Get key uri parts.	
		$this->uri			=& $uri;	
		$this->option 		= $uri->getVar('option');
		$this->controller 	= $uri->getVar('controller');		
		$this->layout 		= $uri->getVar('layout');
		$this->task 		= $uri->getVar('task');		
		$this->view 		= $uri->getVar('view', $this->task);
		$this->id 			= $this->cleanId($uri->getVar('id'));
		if($this->id)		$uri->setVar('id', $this->id);
		$cid = (array) $uri->getVar('cid');
		$this->uri 			=& $uri;
		$this->limit	 	= $uri->getVar('list');
		$this->limitstart	= $uri->getVar('limitstart');
		$this->lang			= $uri->getVar('lang');
		$this->itemid		= $uri->getVar('Itemid');
		
		/**
		 * Controller & View Handling
		 **/
		
		//If no controller and view, default controller is homepage
		if(!$this->controller && !$this->view){
			$this->view = $this->controller = $this->params->get('view_default', $this->view_default);
			if($this->view) $uri->setVar('view',$this->view);
			
		//If controller is set and view isnt, set the view to the controller
		} else if($this->controller && !$this->view){
			$this->view = $this->controller;	
			$uri->setVar('view', $this->view);			
			$uri->delVar('controller');
		
		//If controller and view are the same, unset controller
		}else if($this->controller == $this->view){
			$uri->delVar('controller');	
		}
		
		/**
		 * Task & layout handling
		 **/
		//If layout is form and no task is set, set the task according to $this->id
		if($this->layout == 'form' && !$this->task){
			//Set the task
			if($this->id) $this->task = 'edit';
			else $this->task = 'add';
			//Remove layout 
			$this->layout = null;
			$uri->delVar('layout');
			$uri->setVar('task', $this->task);
		}
		
		//Get the params
		$this->params = SEFTools::getExtParams($this->option);		
		
		//Parse the lookups
		$this->parseLookups();
		
		//Add title element
		$this->addPart('jmenu', JoomSEF::_getMenuTitle($this->option, $this->task, $this->itemid));
	}
	
	
	/**
	 * Parse the lookup info from the XML file
	 */
	public function parseLookups(){
		static $lookups_parsed;
		
		if($lookups_parsed) return;
		
		$xml = & SEFTools::getExtXML($this->option);

		//Check for a lookups node
		if(isset($xml->document->lookups[0])){

			//Loop through lookups
			foreach($xml->document->lookups[0]->children() AS $lookup){
				
				//Get the attributes
				$attribs = $lookup->attributes();

				//Assign required items
				$required = array('table','key','value');				
				$tmp = new stdClass;
				foreach($required AS $k){
					$tmp->$k = JArrayHelper::getValue($attribs, $k);
					unset($attribs[$k]);
				}

				//Check required items exist
				if(!$tmp->table || !$tmp->key || !$tmp->value) continue;
				
				//Grab the ID variable
				$tmp->id = JArrayHelper::getValue($attribs, 'id', $tmp->key);				
				unset($attribs['id']);
				
				//If there are no more vars, theres nothing to match
				if(empty($attribs)) continue;
				
				//Parse vars
				$tmp->vars = $attribs;				
				
				//Add to lookups array
				if(isset($tmp->vars['view'])){
					if(!isset(self::$lookups['views'][$tmp->vars['view']])) self::$lookups[$tmp->vars['view']] = array();
					
					if(isset($tmp->vars['layout'])){
						self::$lookups[$tmp->vars['view']][$tmp->vars['layout']] = $tmp;
					}else{
						self::$lookups[$tmp->vars['view']][] = $tmp;
					}
				}
				else self::$lookups[] = $tmp;				
			}
		}
		$lookups_parsed = true;
	}
	
	
	/**
	 * Lookup attempts to match the request arguments with
	 * lookup parameters parsed from earlier
	 * If a match if found, the value is looked up from the DB
	 */
	public function lookup(){
		static $lookups = array(), $looked_up = array();
		
		$vars = $this->uri->getQuery(true);
		
		//Check we have lookups
		if(!count(self::$lookups)) return;		
		unset($vars['option']);
		
		$var_hash = md5(serialize($vars));
		
		//Have we looked this url up before?
		if(!isset($lookups[$var_hash])){
		
			$lookup = null;
			
			//Check if we have a view specific lookup
			if($this->view && isset(self::$lookups[$this->view])){				

				//Check if we have a view and layout specific lookup
				if($this->layout && isset(self::$lookups[$this->view][$this->layout])){
					$lookup = self::$lookups[$this->view][$this->layout];
				}else{
					//If no layout is supplied, loop through lookups and attempt to match
					foreach(self::$lookups[$this->view] AS $k => $v){
						if(is_int($k)){
							$matches = array_diff_assoc($v->vars, $vars);
							if(count($matches) == 0){
								$lookup = $v;
								break;
							}
						}
					}
				}								
				
			}else{
				//if no view found loop through lookups and attempt to match
				foreach(self::$lookups AS $k => $v){
					if(is_int($k)){
						$matches = array_diff_assoc($v->vars, $vars);
						if(count($matches) == 0){
							$lookup = $v;
							break;
						}
					}
				}
			}
			
			//Add lookup
			$lookups[$var_hash] = $lookup;
		}else{
			$lookup = $looked_up[$var_hash];
		}
		
		
		//If we found a lookup record, look it up in the DB
		if($lookup && isset($lookup->value) && isset($lookup->table) && isset($lookup->key)){
			$lookup_hash = md5(serialize($lookup));
			
			//Have we already done this lookup
			if(!isset($looked_up[$lookup_hash])){
				$id = $this->uri->getVar($lookup->id);
				$db =& JFactory::getDBO();			
				$db->setQuery(sprintf("SELECT `%s` FROM `%s` WHERE `%s` = '%s' LIMIT 1", $lookup->value, $lookup->table, $lookup->key, $id));
				$result = $db->loadResult();
				if($error = $db->getErrorMsg()){
					JError::raiseWarning(500, JText::sprintf('SEF Lookup Error:: %s::%s()', __CLASS__, __FUNCTION__));
					JError::raiseWarning(500, JText::sprintf('SEF Lookup Error::%s', $error));
				}
			}else{
				$result = $looked_up[$lookup_hash];
			}
			//Add result if one retrieved
			if($result){
				$this->addPart($lookup->id, $result);
			}
		}
	}
	
	protected function createStart(){}
	protected function createEnd(){}	
	
	
	/**
	 * Main sef create function. This builds the url according to the rules below
	 * @param object $uri
	 * @return object $uri
	 */
    public function create(&$uri)
    {
    	//Before Processling
    	$this->createStart();
    	
		//If controller is not equal to view then add controller to url
		if($this->controller != $this->view){
			$this->addPart( 'controller', $this->controller );
		}

		/**
		 * Dynamic method call to a method called createLayout$this->view$this->layout() eg createLayoutContactsAll()
		 * This allows for view specific functions to add to the url
		 **/
	
		if($this->view || $this->layout){
			//Construct the method for the layout
			$method = $this->layout && $this->params->get('show_layout', 1) && $this->view ? 'createLayout'.ucfirst($this->view).ucfirst($this->layout) : null;
			if($method && method_exists($this, $method)) $this->$method();
			else{
				
				/**
				 * Dynamic method call to a method called createView$this->view() eg createViewContacts()
				 * This allows for view specific functions to add to the url
				 **/ 		
				if($this->view && $this->params->get('show_view', 1)){	
					$method = 'createView'.ucfirst($this->view);
					if(method_exists($this, $method)) $this->$method();
					else if($this->params->get('show_view_'.$this->view, 1)) $this->addPart( 'view', $this->params->get('text_'.$this->view, $this->view ));
				}	
			 		
				//Remove the layout if it matches the default
				if($this->layout == $this->layout_default){
					$this->uri->delVar('layout');
				
				//Else Add the layout part to the url
				}else if($this->params->get('show_layout_'.$this->view.'_'.$this->layout, 1)){
					$this->addPart( 'layout', $this->layout );
				}
			}
		}		
		

		/**
		 * Loop through vars and exclude the nonsef vars
		 */		
		foreach($this->uri->getQuery(true) AS $k => $v){			
			if(!in_array($k, $this->includeVars) && !in_array($k, $this->ignoreVars) && $k != 'lang'){
				$this->nonSefVars[$k] = $v;
			}			
		}			
		
		//Attempt to lookup
		if($this->lookup) $this->lookup();
		
		//After processing
		$this->createEnd();

		//Create new uri object
        $newUri = $this->uri;
        if (count($this->parts) > 0) {
            //Get the title
            $newUri = JoomSEF::_sefGetLocation($this->uri, $this->parts, $this->task, $this->limit, $this->limitstart, $this->lang, $this->nonSefVars, $this->ignoreVars);
        }    
        
        return $newUri;
    }
}
Back to Top