[ Index ]

PHP Cross Reference of Joomla 1.5.25

title

Body

[close]

/plugins/system/ -> backlink.php (source)

   1  <?php
   2  
   3  /**
   4  * @version        $Id: backlink.php 14401 2010-01-26 14:10:00Z louis $
   5  * @package        Joomla
   6  * @copyright    Copyright (C) 2005 - 2010 Open Source Matters. All rights reserved.
   7  * @license        GNU/GPL, see LICENSE.php
   8  * Joomla! is free software. This version may have been modified pursuant
   9  * to the GNU General Public License, and as distributed it includes or
  10  * is derivative of works licensed under the GNU General Public License or
  11  * other free or open source software licenses.
  12  * See COPYRIGHT.php for copyright notices and details.
  13  */
  14  
  15  // no direct access
  16  defined('_JEXEC') or die('Restricted access');
  17  
  18  jimport('joomla.application.menu');
  19  jimport( 'joomla.plugin.plugin' );
  20  
  21  class plgSystemBacklink extends JPlugin
  22  {
  23  
  24      var $_db = null;
  25  
  26      /**
  27       * Constructor
  28       *
  29       * For php4 compatability we must not use the __constructor as a constructor for plugins
  30       * because func_get_args ( void ) returns a copy of all passed arguments NOT references.
  31       * This causes problems with cross-referencing necessary for the observer design pattern.
  32       *
  33       * @access    protected
  34       * @param    object    $subject The object to observe
  35       * @param     array   $config  An array that holds the plugin configuration
  36       * @since    1.0
  37       */
  38  	function plgSystemBacklink(& $subject, $config)
  39      {
  40          $this->_db = JFactory::getDBO();
  41          parent :: __construct($subject, $config);
  42      }
  43  
  44  	function onAfterInitialise()
  45      {
  46          global $mainframe;
  47          if ($mainframe->isAdmin()) {
  48              return; // Dont run in admin
  49          }
  50  
  51          $sef = $this->params->get('sef', 1);
  52          $url = $this->params->get('url', 1);
  53  
  54          $legacysef = $this->params->get('legacysef', 1);
  55          if (!$sef && !$url && !$legacysef)
  56              return; // None of the options enabled, bail!
  57  
  58          // Grab the system as early as possible, we're going to terminate it potentially
  59          // Case 1: Query string match (shouldn't need this but its here anyway)
  60          if ($url && isset ($_SERVER['QUERY_STRING']) && strlen($_SERVER['QUERY_STRING']) && strpos($_SERVER['QUERY_STRING'], '&')) {
  61              $query_string = $_SERVER['QUERY_STRING'];
  62              $this->_lookup($query_string);
  63          }
  64  
  65          // Case 2: SEF or similar match
  66          if ($sef && isset ($_SERVER['SCRIPT_NAME']) && isset ($_SERVER['REQUEST_URI'])) {
  67              $part = str_replace('index.php', '', $_SERVER['SCRIPT_NAME']);
  68              if($part != '/') {
  69                  $search = str_replace($part, '', $_SERVER['REQUEST_URI']);
  70              } else {
  71                  $search = ltrim($_SERVER['REQUEST_URI'],'/');
  72              }
  73              $this->_lookup($search);
  74          }
  75          // Case 3: Old school core sef; used to backlink
  76          // Enable only if:
  77          // 1: SEF is enabled
  78          // 2: Legacy SEF Plugin Param is set
  79          // 3: And there is no backlink
  80          if ($mainframe->getCfg('sef')
  81              && $legacysef
  82              && !strstr($_SERVER['REQUEST_URI'],'nobacklink')
  83              && !strlen($_SERVER['QUERY_STRING'])) {
  84              $this->_legacysef();
  85          }
  86  
  87      }
  88  
  89  	function _lookup($searchstring)
  90      {
  91          // return blank strings and just index.php on its own...
  92          if (!strlen($searchstring) || $searchstring == trim('index.php',' ?')) {
  93              return;
  94          }
  95  
  96          $sef = $this->params->get('sef', 1);
  97          $url = $this->params->get('url', 1);
  98  
  99          if (!$sef && !$url) {
 100              return; // Neither option enabled, bail!
 101          }
 102  
 103          $query    = 'SELECT * FROM #__migration_backlinks WHERE ';
 104          $where    = Array ();
 105          $search    = $this->_db->Quote( $this->_db->getEscaped( $searchstring, true ).'%', false );
 106  
 107          if ($url) {
 108              $where[] = 'url LIKE ' . $search;
 109          }
 110          if ($sef) {
 111              $where[] = 'sefurl LIKE ' . $search;
 112          }
 113  
 114          $query .= implode(' OR ', $where);
 115          $this->_db->setQuery($query);
 116          $results = $this->_db->loadAssocList();
 117  
 118          if (count($results)) {
 119              // Get the first one...
 120              $this->_redirect($results[0]['itemid'], $results[0]['name'], $results[0]['newurl']);
 121          }
 122      }
 123  
 124  	function _redirect($Itemid, $name, $url = null)
 125      {
 126          global $mainframe;
 127          if (!strlen($url))
 128          {
 129              $menu = & JSite :: getMenu();
 130              $item = $menu->getItem($Itemid);
 131              //$url = $item->link;
 132  
 133              switch ($item->type)
 134              {
 135                  case 'url' :
 136                      if ((strpos($item->link, 'index.php?') !== false) && (strpos($item->link, 'Itemid=') === false)) {
 137                          $url = $item->link . '&amp;Itemid=' . $item->id;
 138                      } else {
 139                          $url = $item->link;
 140                      }
 141                      break;
 142  
 143                  default :
 144                      $url = 'index.php?Itemid=' . $item->id;
 145                      //$url = $item->link . '&Itemid='.$item->id;
 146                      break;
 147              }
 148              $url = JRoute :: _($url);
 149              //$url = JURI :: base() . $url; // was $surl with third option of below being url and second being surl
 150              $name = $item->name;
 151          }
 152          // Check we're not redirecting to ourselves
 153          if(!stristr($url,$_SERVER['REQUEST_URI']) && !stristr($url,$_SERVER['SCRIPT_NAME'].'/'.$_SERVER['QUERY_STRING'])) {
 154              return;
 155          }
 156  
 157          $name = $name ? $name : "Unknown";
 158  
 159          header('Location: ' . str_replace('&amp;','&',$url), true, '301'); // redirect and kill of and &amp;
 160          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));
 161      }
 162  
 163  	function _legacysef()
 164      {
 165          $mosConfig_absolute_path = JPATH_SITE;
 166          $mosConfig_live_site = JURI :: base();
 167          $url_array = explode('/', $_SERVER['REQUEST_URI']);
 168  
 169          if (in_array('content', $url_array))
 170          {
 171              /**
 172              * Content
 173              * http://www.domain.com/$option/$task/$sectionid/$id/$Itemid/$limit/$limitstart
 174              */
 175  
 176              $uri = explode('content/', $_SERVER['REQUEST_URI']);
 177              $option = 'com_content';
 178              $_GET['option'] = $option;
 179              $_REQUEST['option'] = $option;
 180              $pos = array_search('content', $url_array);
 181  
 182              // language hook for content
 183              $lang = '';
 184              foreach ($url_array as $key => $value)
 185              {
 186                  if (!strcasecmp(substr($value, 0, 5), 'lang,'))
 187                  {
 188                      $temp = explode(',', $value);
 189                      if (isset ($temp[0]) && $temp[0] != '' && isset ($temp[1]) && $temp[1] != '')
 190                      {
 191                          $_GET['lang'] = $temp[1];
 192                          $_REQUEST['lang'] = $temp[1];
 193                          $lang = $temp[1];
 194                      }
 195                      unset ($url_array[$key]);
 196                  }
 197              }
 198  
 199              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))
 200              {
 201                  // $option/$task/$sectionid/$id/$Itemid/$order/$filter/$limit/$limitstart
 202                  $task = $url_array[$pos +1];
 203                  $sectionid = $url_array[$pos +2];
 204                  $id = $url_array[$pos +3];
 205                  $Itemid = $url_array[$pos +4];
 206                  $order = str_replace('order,', '', $url_array[$pos +5]);
 207                  $filter = str_replace('filter,', '', $url_array[$pos +6]);
 208                  $limit = $url_array[$pos +7];
 209                  $limitstart = $url_array[$pos +8];
 210  
 211                  // pass data onto global variables
 212                  $_GET['task'] = $task;
 213                  $_REQUEST['task'] = $task;
 214                  $_GET['sectionid'] = $sectionid;
 215                  $_REQUEST['sectionid'] = $sectionid;
 216                  $_GET['id'] = $id;
 217                  $_REQUEST['id'] = $id;
 218                  $_GET['Itemid'] = $Itemid;
 219                  $_REQUEST['Itemid'] = $Itemid;
 220                  $_GET['order'] = $order;
 221                  $_REQUEST['order'] = $order;
 222                  $_GET['filter'] = $filter;
 223                  $_REQUEST['filter'] = $filter;
 224                  $_GET['limit'] = $limit;
 225                  $_REQUEST['limit'] = $limit;
 226                  $_GET['limitstart'] = $limitstart;
 227                  $_REQUEST['limitstart'] = $limitstart;
 228  
 229                  $QUERY_STRING = "option=com_content&task=$task&sectionid=$sectionid&id=$id&Itemid=$Itemid&order=$order&filter=$filter&limit=$limit&limitstart=$limitstart";
 230              }
 231              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)))
 232              {
 233                  // $option/$task/$id/$limit/$limitstart/year/month/module
 234                  $task = $url_array[$pos +1];
 235                  $id = $url_array[$pos +2];
 236                  $limit = $url_array[$pos +3];
 237                  $limitstart = $url_array[$pos +4];
 238                  $year = $url_array[$pos +5];
 239                  $month = $url_array[$pos +6];
 240                  $module = $url_array[$pos +7];
 241  
 242                  // pass data onto global variables
 243                  $_GET['task'] = $task;
 244                  $_REQUEST['task'] = $task;
 245                  $_GET['id'] = $id;
 246                  $_REQUEST['id'] = $id;
 247                  $_GET['limit'] = $limit;
 248                  $_REQUEST['limit'] = $limit;
 249                  $_GET['limitstart'] = $limitstart;
 250                  $_REQUEST['limitstart'] = $limitstart;
 251                  $_GET['year'] = $year;
 252                  $_REQUEST['year'] = $year;
 253                  $_GET['month'] = $month;
 254                  $_REQUEST['month'] = $month;
 255                  $_GET['module'] = $module;
 256                  $_REQUEST['module'] = $module;
 257  
 258                  $QUERY_STRING = "option=com_content&task=$task&id=$id&limit=$limit&limitstart=$limitstart&year=$year&month=$month&module=$module";
 259              }
 260              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)))
 261              {
 262                  // $option/$task/$id/$Itemid/$limit/$limitstart/year/month
 263                  $task = $url_array[$pos +1];
 264                  $id = $url_array[$pos +2];
 265                  $Itemid = $url_array[$pos +3];
 266                  $limit = $url_array[$pos +4];
 267                  $limitstart = $url_array[$pos +5];
 268                  $year = $url_array[$pos +6];
 269                  $month = $url_array[$pos +7];
 270  
 271                  // pass data onto global variables
 272                  $_GET['task'] = $task;
 273                  $_REQUEST['task'] = $task;
 274                  $_GET['id'] = $id;
 275                  $_REQUEST['id'] = $id;
 276                  $_GET['Itemid'] = $Itemid;
 277                  $_REQUEST['Itemid'] = $Itemid;
 278                  $_GET['limit'] = $limit;
 279                  $_REQUEST['limit'] = $limit;
 280                  $_GET['limitstart'] = $limitstart;
 281                  $_REQUEST['limitstart'] = $limitstart;
 282                  $_GET['year'] = $year;
 283                  $_REQUEST['year'] = $year;
 284                  $_GET['month'] = $month;
 285                  $_REQUEST['month'] = $month;
 286  
 287                  $QUERY_STRING = "option=com_content&task=$task&id=$id&Itemid=$Itemid&limit=$limit&limitstart=$limitstart&year=$year&month=$month";
 288              }
 289              else if (isset ($url_array[$pos +7]) && $url_array[$pos +7] != '' && in_array('category', $url_array) && (strpos($url_array[$pos +5], 'order,') !== false))
 290              {
 291                  // $option/$task/$sectionid/$id/$Itemid/$order/$limit/$limitstart
 292                  $task = $url_array[$pos +1];
 293                  $sectionid = $url_array[$pos +2];
 294                  $id = $url_array[$pos +3];
 295                  $Itemid = $url_array[$pos +4];
 296                  $order = str_replace('order,', '', $url_array[$pos +5]);
 297                  $limit = $url_array[$pos +6];
 298                  $limitstart = $url_array[$pos +7];
 299  
 300                  // pass data onto global variables
 301                  $_GET['task'] = $task;
 302                  $_REQUEST['task'] = $task;
 303                  $_GET['sectionid'] = $sectionid;
 304                  $_REQUEST['sectionid'] = $sectionid;
 305                  $_GET['id'] = $id;
 306                  $_REQUEST['id'] = $id;
 307                  $_GET['Itemid'] = $Itemid;
 308                  $_REQUEST['Itemid'] = $Itemid;
 309                  $_GET['order'] = $order;
 310                  $_REQUEST['order'] = $order;
 311                  $_GET['limit'] = $limit;
 312                  $_REQUEST['limit'] = $limit;
 313                  $_GET['limitstart'] = $limitstart;
 314                  $_REQUEST['limitstart'] = $limitstart;
 315  
 316                  $QUERY_STRING = "option=com_content&task=$task&sectionid=$sectionid&id=$id&Itemid=$Itemid&order=$order&limit=$limit&limitstart=$limitstart";
 317              }
 318              else if (isset ($url_array[$pos +6]) && $url_array[$pos +6] != '')
 319              {
 320                  // $option/$task/$sectionid/$id/$Itemid/$limit/$limitstart
 321                  $task = $url_array[$pos +1];
 322                  $sectionid = $url_array[$pos +2];
 323                  $id = $url_array[$pos +3];
 324                  $Itemid = $url_array[$pos +4];
 325                  $limit = $url_array[$pos +5];
 326                  $limitstart = $url_array[$pos +6];
 327  
 328                  // pass data onto global variables
 329                  $_GET['task'] = $task;
 330                  $_REQUEST['task'] = $task;
 331                  $_GET['sectionid'] = $sectionid;
 332                  $_REQUEST['sectionid'] = $sectionid;
 333                  $_GET['id'] = $id;
 334                  $_REQUEST['id'] = $id;
 335                  $_GET['Itemid'] = $Itemid;
 336                  $_REQUEST['Itemid'] = $Itemid;
 337                  $_GET['limit'] = $limit;
 338                  $_REQUEST['limit'] = $limit;
 339                  $_GET['limitstart'] = $limitstart;
 340                  $_REQUEST['limitstart'] = $limitstart;
 341  
 342                  $QUERY_STRING = "option=com_content&task=$task&sectionid=$sectionid&id=$id&Itemid=$Itemid&limit=$limit&limitstart=$limitstart";
 343              }
 344              else if (isset ($url_array[$pos +5]) && $url_array[$pos +5] != '')
 345              {
 346                  // $option/$task/$id/$Itemid/$limit/$limitstart
 347                  $task = $url_array[$pos +1];
 348                  $id = $url_array[$pos +2];
 349                  $Itemid = $url_array[$pos +3];
 350                  $limit = $url_array[$pos +4];
 351                  $limitstart = $url_array[$pos +5];
 352  
 353                  // pass data onto global variables
 354                  $_GET['task'] = $task;
 355                  $_REQUEST['task'] = $task;
 356                  $_GET['id'] = $id;
 357                  $_REQUEST['id'] = $id;
 358                  $_GET['Itemid'] = $Itemid;
 359                  $_REQUEST['Itemid'] = $Itemid;
 360                  $_GET['limit'] = $limit;
 361                  $_REQUEST['limit'] = $limit;
 362                  $_GET['limitstart'] = $limitstart;
 363                  $_REQUEST['limitstart'] = $limitstart;
 364  
 365                  $QUERY_STRING = "option=com_content&task=$task&id=$id&Itemid=$Itemid&limit=$limit&limitstart=$limitstart";
 366              }
 367              else if (isset ($url_array[$pos +4]) && $url_array[$pos +4] != '' && (in_array('archivecategory', $url_array) || in_array('archivesection', $url_array)))
 368              {
 369                  // $option/$task/$year/$month/$module
 370                  $task = $url_array[$pos +1];
 371                  $year = $url_array[$pos +2];
 372                  $month = $url_array[$pos +3];
 373                  $module = $url_array[$pos +4];
 374  
 375                  // pass data onto global variables
 376                  $_GET['task'] = $task;
 377                  $_REQUEST['task'] = $task;
 378                  $_GET['year'] = $year;
 379                  $_REQUEST['year'] = $year;
 380                  $_GET['month'] = $month;
 381                  $_REQUEST['month'] = $month;
 382                  $_GET['module'] = $module;
 383                  $_REQUEST['module'] = $module;
 384  
 385                  $QUERY_STRING = "option=com_content&task=$task&year=$year&month=$month&module=$module";
 386              }
 387              else if (!(isset ($url_array[$pos +5]) && $url_array[$pos +5] != '') && isset ($url_array[$pos +4]) && $url_array[$pos +4] != '')
 388              {
 389                  // $option/$task/$sectionid/$id/$Itemid
 390                  $task = $url_array[$pos +1];
 391                  $sectionid = $url_array[$pos +2];
 392                  $id = $url_array[$pos +3];
 393                  $Itemid = $url_array[$pos +4];
 394  
 395                  // pass data onto global variables
 396                  $_GET['task'] = $task;
 397                  $_REQUEST['task'] = $task;
 398                  $_GET['sectionid'] = $sectionid;
 399                  $_REQUEST['sectionid'] = $sectionid;
 400                  $_GET['id'] = $id;
 401                  $_REQUEST['id'] = $id;
 402                  $_GET['Itemid'] = $Itemid;
 403                  $_REQUEST['Itemid'] = $Itemid;
 404  
 405                  $QUERY_STRING = "option=com_content&task=$task&sectionid=$sectionid&id=$id&Itemid=$Itemid";
 406              }
 407              else if (!(isset ($url_array[$pos +4]) && $url_array[$pos +4] != '') && (isset ($url_array[$pos +3]) && $url_array[$pos +3] != ''))
 408              {
 409                  // $option/$task/$id/$Itemid
 410                  $task = $url_array[$pos +1];
 411                  $id = $url_array[$pos +2];
 412                  $Itemid = $url_array[$pos +3];
 413  
 414                  // pass data onto global variables
 415                  $_GET['task'] = $task;
 416                  $_REQUEST['task'] = $task;
 417                  $_GET['id'] = $id;
 418                  $_REQUEST['id'] = $id;
 419                  $_GET['Itemid'] = $Itemid;
 420                  $_REQUEST['Itemid'] = $Itemid;
 421  
 422                  $QUERY_STRING = "option=com_content&task=$task&id=$id&Itemid=$Itemid";
 423              }
 424              else if (!(isset ($url_array[$pos +3]) && $url_array[$pos +3] != '') && (isset ($url_array[$pos +2]) && $url_array[$pos +2] != ''))
 425              {
 426                  // $option/$task/$id
 427                  $task = $url_array[$pos +1];
 428                  $id = $url_array[$pos +2];
 429  
 430                  // pass data onto global variables
 431                  $_GET['task'] = $task;
 432                  $_REQUEST['task'] = $task;
 433                  $_GET['id'] = $id;
 434                  $_REQUEST['id'] = $id;
 435  
 436                  $QUERY_STRING = "option=com_content&task=$task&id=$id";
 437              }
 438              else if (!(isset ($url_array[$pos +2]) && $url_array[$pos +2] != '') && (isset ($url_array[$pos +1]) && $url_array[$pos +1] != ''))
 439              {
 440                  // $option/$task
 441                  $task = $url_array[$pos +1];
 442  
 443                  $_GET['task'] = $task;
 444                  $_REQUEST['task'] = $task;
 445  
 446                  $QUERY_STRING = 'option=com_content&task=' . $task;
 447              }
 448  
 449              if ($lang != '') {
 450                  $QUERY_STRING .= '&amp;lang=' . $lang;
 451              }
 452  
 453              $_SERVER['QUERY_STRING'] = $QUERY_STRING;
 454              $REQUEST_URI = $uri[0] . 'index.php?' . $QUERY_STRING;
 455              $_SERVER['REQUEST_URI'] = $REQUEST_URI;
 456  
 457          }
 458          else if (in_array('component', $url_array))
 459          {
 460              $name = 'component';
 461              /*
 462              Components
 463              http://www.domain.com/component/$name,$value
 464              */
 465              $uri = explode('component/', $_SERVER['REQUEST_URI']);
 466              $uri_array = explode('/', $uri[1]);
 467              $QUERY_STRING = '';
 468  
 469              // needed for check if component exists
 470              $path = $mosConfig_absolute_path . '/components';
 471              $dirlist = array ();
 472              if (is_dir($path))
 473              {
 474                  $base = opendir($path);
 475                  while (false !== ($dir = readdir($base)))
 476                  {
 477                      if ($dir !== '.' && $dir !== '..' && is_dir($path . '/' . $dir) && strtolower($dir) !== 'cvs' && strtolower($dir) !== '.svn') {
 478                          $dirlist[] = $dir;
 479                      }
 480                  }
 481                  closedir($base);
 482              }
 483  
 484              foreach ($uri_array as $value)
 485              {
 486                  $temp = explode(',', $value);
 487                  if (isset ($temp[0]) && $temp[0] != '' && isset ($temp[1]) && $temp[1] != '')
 488                  {
 489                      $_GET[$temp[0]] = $temp[1];
 490                      $_REQUEST[$temp[0]] = $temp[1];
 491  
 492                      // check to ensure component actually exists
 493                      if ($temp[0] == 'option')
 494                      {
 495                          $check = '';
 496                          if (count($dirlist)) {
 497                              foreach ($dirlist as $dir) {
 498                                  if ($temp[1] == $dir) {
 499                                      $check = 1;
 500                                      break;
 501                                  }
 502                              }
 503                          }
 504                          // redirect to 404 page if no component found to match url
 505                          if (!$check)
 506                          {
 507                              header('HTTP/1.0 404 Not Found');
 508                              require_once ($mosConfig_absolute_path . '/templates/404.php');
 509                              exit (404);
 510                          }
 511                      }
 512  
 513                      if ($QUERY_STRING == '') {
 514                          $QUERY_STRING .= "$temp[0]=$temp[1]";
 515                      } else {
 516                          $QUERY_STRING .= "&$temp[0]=$temp[1]";
 517                      }
 518                  }
 519              }
 520  
 521              $_SERVER['QUERY_STRING'] = $QUERY_STRING;
 522              $REQUEST_URI = $uri[0] . 'index.php?' . $QUERY_STRING;
 523              $_SERVER['REQUEST_URI'] = $REQUEST_URI;
 524  
 525          }
 526          // let this go through and the rest of the system should handle it properly
 527      }
 528  
 529  }


Generated: Mon Nov 14 16:47:20 2011 Cross-referenced by PHPXref 0.7.1