arthurmcneil /plugins/system/backlink.php

Language PHP Lines 529
MD5 Hash 9a0a8f58c60e0d127bf1c85d7af38ef5 Estimated Cost $10,160 (why?)
Repository https://github.com/chrisinammo/arthurmcneil.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
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
<?php

/**
* @version		$Id: backlink.php 10094 2008-03-02 04:35:10Z instance $
* @package		Joomla
* @copyright	Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
* @license		GNU/GPL, see LICENSE.php
* Joomla! is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*/

// no direct access
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.menu');
jimport( 'joomla.plugin.plugin' );

class plgSystemBacklink extends JPlugin
{

	var $_db = null;

	/**
	 * Constructor
	 *
	 * For php4 compatability we must not use the __constructor as a constructor for plugins
	 * because func_get_args ( void ) returns a copy of all passed arguments NOT references.
	 * This causes problems with cross-referencing necessary for the observer design pattern.
	 *
	 * @access	protected
	 * @param	object	$subject The object to observe
	 * @param 	array   $config  An array that holds the plugin configuration
	 * @since	1.0
	 */
	function plgSystemBacklink(& $subject, $config)
	{
		$this->_db = JFactory::getDBO();
		parent :: __construct($subject, $config);
	}

	function onAfterInitialise()
	{
		global $mainframe;
		if ($mainframe->isAdmin()) {
			return; // Dont run in admin
		}

		$sef = $this->params->get('sef', 1);
		$url = $this->params->get('url', 1);

		$legacysef = $this->params->get('legacysef', 1);
		if (!$sef && !$url && !$legacysef)
			return; // None of the options enabled, bail!

		// Grab the system as early as possible, we're going to terminate it potentially
		// Case 1: Query string match (shouldn't need this but its here anyway)
		if ($url && isset ($_SERVER['QUERY_STRING']) && strlen($_SERVER['QUERY_STRING']) && strpos($_SERVER['QUERY_STRING'], '&')) {
			$query_string = $_SERVER['QUERY_STRING'];
			$this->_lookup($query_string);
		}

		// Case 2: SEF or similar match
		if ($sef && isset ($_SERVER['SCRIPT_NAME']) && isset ($_SERVER['REQUEST_URI'])) {
			$part = str_replace('index.php', '', $_SERVER['SCRIPT_NAME']);
			if($part != '/') {
				$search = str_replace($part, '', $_SERVER['REQUEST_URI']);
			} else {
				$search = ltrim($_SERVER['REQUEST_URI'],'/');
			}
			$this->_lookup($search);
		}
		// Case 3: Old school core sef; used to backlink
		// Enable only if:
		// 1: SEF is enabled
		// 2: Legacy SEF Plugin Param is set
		// 3: And there is no backlink
		if ($mainframe->getCfg('sef')
			&& $legacysef
			&& !strstr($_SERVER['REQUEST_URI'],'nobacklink')
			&& !strlen($_SERVER['QUERY_STRING'])) {
			$this->_legacysef();
		}

	}

	function _lookup($searchstring)
	{
		// return blank strings and just index.php on its own...
		if (!strlen($searchstring) || $searchstring == trim('index.php',' ?')) {
			return;
		}

		$sef = $this->params->get('sef', 1);
		$url = $this->params->get('url', 1);

		if (!$sef && !$url) {
			return; // Neither option enabled, bail!
		}

		$query	= 'SELECT * FROM #__migration_backlinks WHERE ';
		$where	= Array ();
		$search	= $this->_db->Quote( $this->_db->getEscaped( $searchstring, true ).'%', false );

		if ($url) {
			$where[] = 'url LIKE ' . $search;
		}
		if ($sef) {
			$where[] = 'sefurl LIKE ' . $search;
		}

		$query .= implode(' OR ', $where);
		$this->_db->setQuery($query);
		$results = $this->_db->loadAssocList();

		if (count($results)) {
			// Get the first one...
			$this->_redirect($results[0]['itemid'], $results[0]['name'], $results[0]['newurl']);
		}
	}

	function _redirect($Itemid, $name, $url = null)
	{
		global $mainframe;
		if (!strlen($url))
		{
			$menu = & JSite :: getMenu();
			$item = $menu->getItem($Itemid);
			//$url = $item->link;

			switch ($item->type)
			{
				case 'url' :
					if ((strpos($item->link, 'index.php?') !== false) && (strpos($item->link, 'Itemid=') === false)) {
						$url = $item->link . '&amp;Itemid=' . $item->id;
					} else {
						$url = $item->link;
					}
					break;

				default :
					$url = 'index.php?Itemid=' . $item->id;
					//$url = $item->link . '&Itemid='.$item->id;
					break;
			}
			$url = JRoute :: _($url);
			//$url = JURI :: base() . $url; // was $surl with third option of below being url and second being surl
			$name = $item->name;
		}
		// Check we're not redirecting to ourselves
		if(!stristr($url,$_SERVER['REQUEST_URI']) && !stristr($url,$_SERVER['SCRIPT_NAME'].'/'.$_SERVER['QUERY_STRING'])) {
			return;
		}

		$name = $name ? $name : "Unknown";

		header('Location: ' . str_replace('&amp;','&',$url), true, '301'); // redirect and kill of and &amp;
		jexit(JText :: sprintf('"%s" has moved to <a href="%s">%s</a>. Click the link if your browser does not redirect you automatically.', $name, $url, $url));
	}

	function _legacysef()
	{
		$mosConfig_absolute_path = JPATH_SITE;
		$mosConfig_live_site = JURI :: base();
		$url_array = explode('/', $_SERVER['REQUEST_URI']);

		if (in_array('content', $url_array))
		{
			/**
			* Content
			* http://www.domain.com/$option/$task/$sectionid/$id/$Itemid/$limit/$limitstart
			*/

			$uri = explode('content/', $_SERVER['REQUEST_URI']);
			$option = 'com_content';
			$_GET['option'] = $option;
			$_REQUEST['option'] = $option;
			$pos = array_search('content', $url_array);

			// language hook for content
			$lang = '';
			foreach ($url_array as $key => $value)
			{
				if (!strcasecmp(substr($value, 0, 5), 'lang,'))
				{
					$temp = explode(',', $value);
					if (isset ($temp[0]) && $temp[0] != '' && isset ($temp[1]) && $temp[1] != '')
					{
						$_GET['lang'] = $temp[1];
						$_REQUEST['lang'] = $temp[1];
						$lang = $temp[1];
					}
					unset ($url_array[$key]);
				}
			}

			if (isset ($url_array[$pos +8]) && $url_array[$pos +8] != '' && in_array('category', $url_array) && (strpos($url_array[$pos +5], 'order,') !== false) && (strpos($url_array[$pos +6], 'filter,') !== false))
			{
				// $option/$task/$sectionid/$id/$Itemid/$order/$filter/$limit/$limitstart
				$task = $url_array[$pos +1];
				$sectionid = $url_array[$pos +2];
				$id = $url_array[$pos +3];
				$Itemid = $url_array[$pos +4];
				$order = str_replace('order,', '', $url_array[$pos +5]);
				$filter = str_replace('filter,', '', $url_array[$pos +6]);
				$limit = $url_array[$pos +7];
				$limitstart = $url_array[$pos +8];

				// pass data onto global variables
				$_GET['task'] = $task;
				$_REQUEST['task'] = $task;
				$_GET['sectionid'] = $sectionid;
				$_REQUEST['sectionid'] = $sectionid;
				$_GET['id'] = $id;
				$_REQUEST['id'] = $id;
				$_GET['Itemid'] = $Itemid;
				$_REQUEST['Itemid'] = $Itemid;
				$_GET['order'] = $order;
				$_REQUEST['order'] = $order;
				$_GET['filter'] = $filter;
				$_REQUEST['filter'] = $filter;
				$_GET['limit'] = $limit;
				$_REQUEST['limit'] = $limit;
				$_GET['limitstart'] = $limitstart;
				$_REQUEST['limitstart'] = $limitstart;

				$QUERY_STRING = "option=com_content&task=$task&sectionid=$sectionid&id=$id&Itemid=$Itemid&order=$order&filter=$filter&limit=$limit&limitstart=$limitstart";
			}
			else if (isset ($url_array[$pos +7]) && $url_array[$pos +7] != '' && $url_array[$pos +5] > 1000 && (in_array('archivecategory', $url_array) || in_array('archivesection', $url_array)))
			{
				// $option/$task/$id/$limit/$limitstart/year/month/module
				$task = $url_array[$pos +1];
				$id = $url_array[$pos +2];
				$limit = $url_array[$pos +3];
				$limitstart = $url_array[$pos +4];
				$year = $url_array[$pos +5];
				$month = $url_array[$pos +6];
				$module = $url_array[$pos +7];

				// pass data onto global variables
				$_GET['task'] = $task;
				$_REQUEST['task'] = $task;
				$_GET['id'] = $id;
				$_REQUEST['id'] = $id;
				$_GET['limit'] = $limit;
				$_REQUEST['limit'] = $limit;
				$_GET['limitstart'] = $limitstart;
				$_REQUEST['limitstart'] = $limitstart;
				$_GET['year'] = $year;
				$_REQUEST['year'] = $year;
				$_GET['month'] = $month;
				$_REQUEST['month'] = $month;
				$_GET['module'] = $module;
				$_REQUEST['module'] = $module;

				$QUERY_STRING = "option=com_content&task=$task&id=$id&limit=$limit&limitstart=$limitstart&year=$year&month=$month&module=$module";
			}
			else if (isset ($url_array[$pos +7]) && $url_array[$pos +7] != '' && $url_array[$pos +6] > 1000 && (in_array('archivecategory', $url_array) || in_array('archivesection', $url_array)))
			{
				// $option/$task/$id/$Itemid/$limit/$limitstart/year/month
				$task = $url_array[$pos +1];
				$id = $url_array[$pos +2];
				$Itemid = $url_array[$pos +3];
				$limit = $url_array[$pos +4];
				$limitstart = $url_array[$pos +5];
				$year = $url_array[$pos +6];
				$month = $url_array[$pos +7];

				// pass data onto global variables
				$_GET['task'] = $task;
				$_REQUEST['task'] = $task;
				$_GET['id'] = $id;
				$_REQUEST['id'] = $id;
				$_GET['Itemid'] = $Itemid;
				$_REQUEST['Itemid'] = $Itemid;
				$_GET['limit'] = $limit;
				$_REQUEST['limit'] = $limit;
				$_GET['limitstart'] = $limitstart;
				$_REQUEST['limitstart'] = $limitstart;
				$_GET['year'] = $year;
				$_REQUEST['year'] = $year;
				$_GET['month'] = $month;
				$_REQUEST['month'] = $month;

				$QUERY_STRING = "option=com_content&task=$task&id=$id&Itemid=$Itemid&limit=$limit&limitstart=$limitstart&year=$year&month=$month";
			}
			else if (isset ($url_array[$pos +7]) && $url_array[$pos +7] != '' && in_array('category', $url_array) && (strpos($url_array[$pos +5], 'order,') !== false))
			{
				// $option/$task/$sectionid/$id/$Itemid/$order/$limit/$limitstart
				$task = $url_array[$pos +1];
				$sectionid = $url_array[$pos +2];
				$id = $url_array[$pos +3];
				$Itemid = $url_array[$pos +4];
				$order = str_replace('order,', '', $url_array[$pos +5]);
				$limit = $url_array[$pos +6];
				$limitstart = $url_array[$pos +7];

				// pass data onto global variables
				$_GET['task'] = $task;
				$_REQUEST['task'] = $task;
				$_GET['sectionid'] = $sectionid;
				$_REQUEST['sectionid'] = $sectionid;
				$_GET['id'] = $id;
				$_REQUEST['id'] = $id;
				$_GET['Itemid'] = $Itemid;
				$_REQUEST['Itemid'] = $Itemid;
				$_GET['order'] = $order;
				$_REQUEST['order'] = $order;
				$_GET['limit'] = $limit;
				$_REQUEST['limit'] = $limit;
				$_GET['limitstart'] = $limitstart;
				$_REQUEST['limitstart'] = $limitstart;

				$QUERY_STRING = "option=com_content&task=$task&sectionid=$sectionid&id=$id&Itemid=$Itemid&order=$order&limit=$limit&limitstart=$limitstart";
			}
			else if (isset ($url_array[$pos +6]) && $url_array[$pos +6] != '')
			{
				// $option/$task/$sectionid/$id/$Itemid/$limit/$limitstart
				$task = $url_array[$pos +1];
				$sectionid = $url_array[$pos +2];
				$id = $url_array[$pos +3];
				$Itemid = $url_array[$pos +4];
				$limit = $url_array[$pos +5];
				$limitstart = $url_array[$pos +6];

				// pass data onto global variables
				$_GET['task'] = $task;
				$_REQUEST['task'] = $task;
				$_GET['sectionid'] = $sectionid;
				$_REQUEST['sectionid'] = $sectionid;
				$_GET['id'] = $id;
				$_REQUEST['id'] = $id;
				$_GET['Itemid'] = $Itemid;
				$_REQUEST['Itemid'] = $Itemid;
				$_GET['limit'] = $limit;
				$_REQUEST['limit'] = $limit;
				$_GET['limitstart'] = $limitstart;
				$_REQUEST['limitstart'] = $limitstart;

				$QUERY_STRING = "option=com_content&task=$task&sectionid=$sectionid&id=$id&Itemid=$Itemid&limit=$limit&limitstart=$limitstart";
			}
			else if (isset ($url_array[$pos +5]) && $url_array[$pos +5] != '')
			{
				// $option/$task/$id/$Itemid/$limit/$limitstart
				$task = $url_array[$pos +1];
				$id = $url_array[$pos +2];
				$Itemid = $url_array[$pos +3];
				$limit = $url_array[$pos +4];
				$limitstart = $url_array[$pos +5];

				// pass data onto global variables
				$_GET['task'] = $task;
				$_REQUEST['task'] = $task;
				$_GET['id'] = $id;
				$_REQUEST['id'] = $id;
				$_GET['Itemid'] = $Itemid;
				$_REQUEST['Itemid'] = $Itemid;
				$_GET['limit'] = $limit;
				$_REQUEST['limit'] = $limit;
				$_GET['limitstart'] = $limitstart;
				$_REQUEST['limitstart'] = $limitstart;

				$QUERY_STRING = "option=com_content&task=$task&id=$id&Itemid=$Itemid&limit=$limit&limitstart=$limitstart";
			}
			else if (isset ($url_array[$pos +4]) && $url_array[$pos +4] != '' && (in_array('archivecategory', $url_array) || in_array('archivesection', $url_array)))
			{
				// $option/$task/$year/$month/$module
				$task = $url_array[$pos +1];
				$year = $url_array[$pos +2];
				$month = $url_array[$pos +3];
				$module = $url_array[$pos +4];

				// pass data onto global variables
				$_GET['task'] = $task;
				$_REQUEST['task'] = $task;
				$_GET['year'] = $year;
				$_REQUEST['year'] = $year;
				$_GET['month'] = $month;
				$_REQUEST['month'] = $month;
				$_GET['module'] = $module;
				$_REQUEST['module'] = $module;

				$QUERY_STRING = "option=com_content&task=$task&year=$year&month=$month&module=$module";
			}
			else if (!(isset ($url_array[$pos +5]) && $url_array[$pos +5] != '') && isset ($url_array[$pos +4]) && $url_array[$pos +4] != '')
			{
				// $option/$task/$sectionid/$id/$Itemid
				$task = $url_array[$pos +1];
				$sectionid = $url_array[$pos +2];
				$id = $url_array[$pos +3];
				$Itemid = $url_array[$pos +4];

				// pass data onto global variables
				$_GET['task'] = $task;
				$_REQUEST['task'] = $task;
				$_GET['sectionid'] = $sectionid;
				$_REQUEST['sectionid'] = $sectionid;
				$_GET['id'] = $id;
				$_REQUEST['id'] = $id;
				$_GET['Itemid'] = $Itemid;
				$_REQUEST['Itemid'] = $Itemid;

				$QUERY_STRING = "option=com_content&task=$task&sectionid=$sectionid&id=$id&Itemid=$Itemid";
			}
			else if (!(isset ($url_array[$pos +4]) && $url_array[$pos +4] != '') && (isset ($url_array[$pos +3]) && $url_array[$pos +3] != ''))
			{
				// $option/$task/$id/$Itemid
				$task = $url_array[$pos +1];
				$id = $url_array[$pos +2];
				$Itemid = $url_array[$pos +3];

				// pass data onto global variables
				$_GET['task'] = $task;
				$_REQUEST['task'] = $task;
				$_GET['id'] = $id;
				$_REQUEST['id'] = $id;
				$_GET['Itemid'] = $Itemid;
				$_REQUEST['Itemid'] = $Itemid;

				$QUERY_STRING = "option=com_content&task=$task&id=$id&Itemid=$Itemid";
			}
			else if (!(isset ($url_array[$pos +3]) && $url_array[$pos +3] != '') && (isset ($url_array[$pos +2]) && $url_array[$pos +2] != ''))
			{
				// $option/$task/$id
				$task = $url_array[$pos +1];
				$id = $url_array[$pos +2];

				// pass data onto global variables
				$_GET['task'] = $task;
				$_REQUEST['task'] = $task;
				$_GET['id'] = $id;
				$_REQUEST['id'] = $id;

				$QUERY_STRING = "option=com_content&task=$task&id=$id";
			}
			else if (!(isset ($url_array[$pos +2]) && $url_array[$pos +2] != '') && (isset ($url_array[$pos +1]) && $url_array[$pos +1] != ''))
			{
				// $option/$task
				$task = $url_array[$pos +1];

				$_GET['task'] = $task;
				$_REQUEST['task'] = $task;

				$QUERY_STRING = 'option=com_content&task=' . $task;
			}

			if ($lang != '') {
				$QUERY_STRING .= '&amp;lang=' . $lang;
			}

			$_SERVER['QUERY_STRING'] = $QUERY_STRING;
			$REQUEST_URI = $uri[0] . 'index.php?' . $QUERY_STRING;
			$_SERVER['REQUEST_URI'] = $REQUEST_URI;

		}
		else if (in_array('component', $url_array))
		{
			$name = 'component';
			/*
			Components
			http://www.domain.com/component/$name,$value
			*/
			$uri = explode('component/', $_SERVER['REQUEST_URI']);
			$uri_array = explode('/', $uri[1]);
			$QUERY_STRING = '';

			// needed for check if component exists
			$path = $mosConfig_absolute_path . '/components';
			$dirlist = array ();
			if (is_dir($path))
			{
				$base = opendir($path);
				while (false !== ($dir = readdir($base)))
				{
					if ($dir !== '.' && $dir !== '..' && is_dir($path . '/' . $dir) && strtolower($dir) !== 'cvs' && strtolower($dir) !== '.svn') {
						$dirlist[] = $dir;
					}
				}
				closedir($base);
			}

			foreach ($uri_array as $value)
			{
				$temp = explode(',', $value);
				if (isset ($temp[0]) && $temp[0] != '' && isset ($temp[1]) && $temp[1] != '')
				{
					$_GET[$temp[0]] = $temp[1];
					$_REQUEST[$temp[0]] = $temp[1];

					// check to ensure component actually exists
					if ($temp[0] == 'option')
					{
						$check = '';
						if (count($dirlist)) {
							foreach ($dirlist as $dir) {
								if ($temp[1] == $dir) {
									$check = 1;
									break;
								}
							}
						}
						// redirect to 404 page if no component found to match url
						if (!$check)
						{
							header('HTTP/1.0 404 Not Found');
							require_once ($mosConfig_absolute_path . '/templates/404.php');
							exit (404);
						}
					}

					if ($QUERY_STRING == '') {
						$QUERY_STRING .= "$temp[0]=$temp[1]";
					} else {
						$QUERY_STRING .= "&$temp[0]=$temp[1]";
					}
				}
			}

			$_SERVER['QUERY_STRING'] = $QUERY_STRING;
			$REQUEST_URI = $uri[0] . 'index.php?' . $QUERY_STRING;
			$_SERVER['REQUEST_URI'] = $REQUEST_URI;

		}
		// let this go through and the rest of the system should handle it properly
	}

}
Back to Top