includes/paths.php
changeset 313 854eecfada20
parent 304 e2cb5f1432c8
parent 312 6c7060d36a23
child 326 ab66d6d1f1f4
equal deleted inserted replaced
310:199b9708f4a2 313:854eecfada20
    16  * @subpackage PathManager
    16  * @subpackage PathManager
    17  * @see http://enanocms.org/Help:API_Documentation
    17  * @see http://enanocms.org/Help:API_Documentation
    18  */
    18  */
    19  
    19  
    20 class pathManager {
    20 class pathManager {
    21   var $pages, $custom_page, $cpage, $page, $fullpage, $page_exists, $namespace, $nslist, $admin_tree, $wiki_mode, $page_protected, $template_cache;
    21   var $pages, $custom_page, $cpage, $page, $fullpage, $page_exists, $namespace, $nslist, $admin_tree, $wiki_mode, $page_protected, $template_cache, $anonymous_page;
    22   function __construct()
    22   function __construct()
    23   {
    23   {
    24     global $db, $session, $paths, $template, $plugins; // Common objects
    24     global $db, $session, $paths, $template, $plugins; // Common objects
    25     
    25     
    26     $GLOBALS['paths'] =& $this;
    26     $GLOBALS['paths'] =& $this;
    27     $this->pages = Array();
    27     $this->pages = Array();
    28     
       
    29     dc_here('paths: setting up namespaces, admin nodes');
       
    30     
    28     
    31     // DEFINE NAMESPACES HERE
    29     // DEFINE NAMESPACES HERE
    32     // The key names should NOT EVER be changed, or Enano will be very broken
    30     // The key names should NOT EVER be changed, or Enano will be very broken
    33     $this->nslist = Array(
    31     $this->nslist = Array(
    34       'Article' =>'',
    32       'Article' =>'',
    38       'Admin'   =>'Admin:',
    36       'Admin'   =>'Admin:',
    39       'Special' =>'Special:',
    37       'Special' =>'Special:',
    40       'System'  =>'Enano:',
    38       'System'  =>'Enano:',
    41       'Template'=>'Template:',
    39       'Template'=>'Template:',
    42       'Category'=>'Category:',
    40       'Category'=>'Category:',
       
    41       'Anonymous'=>'PhysicalRedirect:',
    43       'Project' =>sanitize_page_id(getConfig('site_name')).':',
    42       'Project' =>sanitize_page_id(getConfig('site_name')).':',
    44       );
    43       );
    45     
    44     
    46     // ACL types
    45     // ACL types
    47     // These can also be added from within plugins
    46     // These can also be added from within plugins
   108   }
   107   }
   109   function init()
   108   function init()
   110   {
   109   {
   111     global $db, $session, $paths, $template, $plugins; // Common objects
   110     global $db, $session, $paths, $template, $plugins; // Common objects
   112     
   111     
   113     dc_here('paths: selecting master page data');
       
   114     
       
   115     $code = $plugins->setHook('paths_init_before');
   112     $code = $plugins->setHook('paths_init_before');
   116     foreach ( $code as $cmd )
   113     foreach ( $code as $cmd )
   117     {
   114     {
   118       eval($cmd);
   115       eval($cmd);
   119     }
   116     }
   150       $this->pages[$r['urlname']] = $r;
   147       $this->pages[$r['urlname']] = $r;
   151       $this->pages[] =& $this->pages[$r['urlname']];
   148       $this->pages[] =& $this->pages[$r['urlname']];
   152       
   149       
   153     }
   150     }
   154     $db->free_result();
   151     $db->free_result();
   155     dc_here('paths: determining page ID');
   152     if ( defined('ENANO_INTERFACE_INDEX') || defined('ENANO_INTERFACE_AJAX') || defined('IN_ENANO_INSTALL') || defined('IN_ENANO_UPGRADE') )
   156     if( isset($_GET['title']) )
   153     {
   157     {
   154       if( isset($_GET['title']) )
   158       if ( $_GET['title'] == '' && getConfig('main_page') != '' )
   155       {
   159       {
   156         if ( $_GET['title'] == '' && getConfig('main_page') != '' )
   160         $this->main_page();
   157         {
   161       }
   158           $this->main_page();
   162       if(strstr($_GET['title'], ' '))
   159         }
   163       {
   160         if(strstr($_GET['title'], ' '))
   164         $loc = urldecode(rawurldecode($_SERVER['REQUEST_URI']));
   161         {
   165         $loc = str_replace(' ', '_', $loc);
   162           $loc = urldecode(rawurldecode($_SERVER['REQUEST_URI']));
   166         $loc = str_replace('+', '_', $loc);
   163           $loc = str_replace(' ', '_', $loc);
   167         $loc = str_replace('%20', '_', $loc);
   164           $loc = str_replace('+', '_', $loc);
   168         redirect($loc, 'Redirecting...', 'Space detected in the URL, please wait whilst you are redirected', 0);
   165           $loc = str_replace('%20', '_', $loc);
   169         exit;
   166           redirect($loc, 'Redirecting...', 'Space detected in the URL, please wait whilst you are redirected', 0);
   170       }
   167           exit;
   171       $url_namespace_special = substr($_GET['title'], 0, strlen($this->nslist['Special']) );
   168         }
   172       $url_namespace_template = substr($_GET['title'], 0, strlen($this->nslist['Template']) );
   169         $url_namespace_special = substr($_GET['title'], 0, strlen($this->nslist['Special']) );
   173       if($url_namespace_special == $this->nslist['Special'] || $url_namespace_template == $this->nslist['Template'] )
   170         $url_namespace_template = substr($_GET['title'], 0, strlen($this->nslist['Template']) );
   174       {
   171         if($url_namespace_special == $this->nslist['Special'] || $url_namespace_template == $this->nslist['Template'] )
   175         $ex = explode('/', $_GET['title']);
   172         {
   176         $this->page = $ex[0];
   173           $ex = explode('/', $_GET['title']);
       
   174           $this->page = $ex[0];
       
   175         }
       
   176         else
       
   177         {
       
   178           $this->page = $_GET['title'];
       
   179         }
       
   180         $this->fullpage = $_GET['title'];
       
   181       }
       
   182       elseif( isset($_SERVER['PATH_INFO']) )
       
   183       {
       
   184         $pi = explode('/', $_SERVER['PATH_INFO']);
       
   185         
       
   186         if( !isset($pi[1]) || (isset($pi[1]) && $pi[1] == '' && getConfig('main_page') != '') )
       
   187         {
       
   188           $this->main_page();
       
   189         }
       
   190         if( strstr($pi[1], ' ') )
       
   191         {
       
   192           $loc = str_replace(' ', '_', urldecode(rawurldecode($_SERVER['REQUEST_URI'])));
       
   193           $loc = str_replace('+', '_', $loc);
       
   194           $loc = str_replace('%20', '_', $loc);
       
   195           redirect($loc, 'Redirecting...', 'Please wait whilst you are redirected', 3);
       
   196           exit;
       
   197         }
       
   198         unset($pi[0]);
       
   199         if( substr($pi[1], 0, strlen($this->nslist['Special'])) == $this->nslist['Special'] || substr($pi[1], 0, strlen($this->nslist['Template'])) == $this->nslist['Template'] )
       
   200         {
       
   201           $pi2 = $pi[1];
       
   202         }
       
   203         else
       
   204         {
       
   205           $pi2 = implode('/', $pi);
       
   206         }
       
   207         $this->page = $pi2;
       
   208         $this->fullpage = implode('/', $pi);
   177       }
   209       }
   178       else
   210       else
   179       {
   211       {
   180         $this->page = $_GET['title'];
   212         $k = array_keys($_GET);
   181       }
   213         foreach($k as $c)
   182       $this->fullpage = $_GET['title'];
   214         {
   183     }
   215           if(substr($c, 0, 1) == '/')
   184     elseif( isset($_SERVER['PATH_INFO']) )
   216           {
   185     {
   217             $this->page = substr($c, 1, strlen($c));
   186       $pi = explode('/', $_SERVER['PATH_INFO']);
   218             
   187       
   219             // Bugfix for apache somehow passing dots as underscores
   188       if( !isset($pi[1]) || (isset($pi[1]) && $pi[1] == '' && getConfig('main_page') != '') )
   220             global $mime_types;
   189       {
   221             
   190         $this->main_page();
   222             $exts = array_keys($mime_types);
   191       }
   223             $exts = '(' . implode('|', $exts) . ')';
   192       if( strstr($pi[1], ' ') )
   224             
   193       {
   225             if ( preg_match( '#_'.$exts.'#i', $this->page ) )
   194         $loc = str_replace(' ', '_', urldecode(rawurldecode($_SERVER['REQUEST_URI'])));
   226             {
   195         $loc = str_replace('+', '_', $loc);
   227               $this->page = preg_replace( '#_'.$exts.'#i', '.\\1', $this->page );
   196         $loc = str_replace('%20', '_', $loc);
   228             }
   197         redirect($loc, 'Redirecting...', 'Please wait whilst you are redirected', 3);
   229             
   198         exit;
   230             $this->fullpage = $this->page;
   199       }
   231             
   200       unset($pi[0]);
   232             if(substr($this->page, 0, strlen($this->nslist['Special']))==$this->nslist['Special'] || substr($this->page, 0, strlen($this->nslist['Template']))==$this->nslist['Template'])
   201       if( substr($pi[1], 0, strlen($this->nslist['Special'])) == $this->nslist['Special'] || substr($pi[1], 0, strlen($this->nslist['Template'])) == $this->nslist['Template'] )
   233             {
   202       {
   234               $ex = explode('/', $this->page);
   203         $pi2 = $pi[1];
   235               $this->page = $ex[0];
       
   236             }
       
   237             if(strstr($this->page, ' '))
       
   238             {
       
   239               $loc = str_replace(' ', '_', urldecode(rawurldecode($_SERVER['REQUEST_URI'])));
       
   240               $loc = str_replace('+', '_', $loc);
       
   241               $loc = str_replace('%20', '_', $loc);
       
   242               redirect($loc, 'Redirecting...', 'Space in the URL detected, please wait whilst you are redirected', 0);
       
   243               exit;
       
   244             }
       
   245             break;
       
   246           }
       
   247         }
       
   248         if(!$this->page && !($this->page == '' && getConfig('main_page') == ''))
       
   249         {
       
   250           $this->main_page();
       
   251         }
       
   252       }
       
   253     }
       
   254     else
       
   255     {
       
   256       // Starting up Enano with the API from a page that wants to do its own thing. Generate
       
   257       // metadata for an anonymous page and avoid redirection at all costs.
       
   258       if ( isset($GLOBALS['title']) )
       
   259       {
       
   260         $title =& $GLOBALS['title'];
   204       }
   261       }
   205       else
   262       else
   206       {
   263       {
   207         $pi2 = implode('/', $pi);
   264         $title = basename($_SERVER['SCRIPT_NAME']);
   208       }
   265       }
   209       $this->page = $pi2;
   266       $base_uri = str_replace( scriptPath . '/', '', $_SERVER['SCRIPT_NAME'] );
   210       $this->fullpage = implode('/', $pi);
   267       $this->page = $this->nslist['Anonymous'] . sanitize_page_id($base_uri);
   211     }
   268       $this->fullpage = $this->nslist['Anonymous'] . sanitize_page_id($base_uri);
   212     else
   269       $this->namespace = 'Anonymous';
   213     {
   270       $this->cpage = array(
   214       $k = array_keys($_GET);
   271           'name' => $title,
   215       foreach($k as $c)
   272           'urlname' => sanitize_page_id($base_uri),
   216       {
   273           'namespace' => 'Anonymous',
   217         if(substr($c, 0, 1) == '/')
   274           'special' => 1,
   218         {
   275           'visible' => 1,
   219           $this->page = substr($c, 1, strlen($c));
   276           'comments_on' => 1,
   220           
   277           'protected' => 1,
   221           // Bugfix for apache somehow passing dots as underscores
   278           'delvotes' => 0,
   222           global $mime_types;
   279           'delvote_ips' => ''
   223           
   280         );
   224           $exts = array_keys($mime_types);
   281       $this->anonymous_page = true;
   225           $exts = '(' . implode('|', $exts) . ')';
   282       $code = $plugins->setHook('paths_anonymous_page');
   226           
   283       foreach ( $code as $cmd )
   227           if ( preg_match( '#_'.$exts.'#i', $this->page ) )
   284       {
   228           {
   285         eval($cmd);
   229             $this->page = preg_replace( '#_'.$exts.'#i', '.\\1', $this->page );
       
   230           }
       
   231           
       
   232           $this->fullpage = $this->page;
       
   233           
       
   234           if(substr($this->page, 0, strlen($this->nslist['Special']))==$this->nslist['Special'] || substr($this->page, 0, strlen($this->nslist['Template']))==$this->nslist['Template'])
       
   235           {
       
   236             $ex = explode('/', $this->page);
       
   237             $this->page = $ex[0];
       
   238           }
       
   239           if(strstr($this->page, ' '))
       
   240           {
       
   241             $loc = str_replace(' ', '_', urldecode(rawurldecode($_SERVER['REQUEST_URI'])));
       
   242             $loc = str_replace('+', '_', $loc);
       
   243             $loc = str_replace('%20', '_', $loc);
       
   244             redirect($loc, 'Redirecting...', 'Space in the URL detected, please wait whilst you are redirected', 0);
       
   245             exit;
       
   246           }
       
   247           break;
       
   248         }
       
   249       }
       
   250       if(!$this->page && !($this->page == '' && getConfig('main_page') == ''))
       
   251       {
       
   252         $this->main_page();
       
   253       }
   286       }
   254     }
   287     }
   255     
   288     
   256     $this->page = sanitize_page_id($this->page);
   289     $this->page = sanitize_page_id($this->page);
   257     $this->fullpage = sanitize_page_id($this->fullpage);
   290     $this->fullpage = sanitize_page_id($this->fullpage);
   258     
   291     
   259     dc_here('paths: setting $paths->cpage');
       
   260     
       
   261     if(isset($this->pages[$this->page]))
   292     if(isset($this->pages[$this->page]))
   262     {
   293     {
   263       dc_here('paths: page existence verified, our page ID is: '.$this->page);
       
   264       $this->page_exists = true;
   294       $this->page_exists = true;
   265       $this->cpage = $this->pages[$this->page];
   295       $this->cpage = $this->pages[$this->page];
   266       $this->namespace = $this->cpage['namespace'];
   296       $this->namespace = $this->cpage['namespace'];
   267       if(!isset($this->cpage['wiki_mode'])) $this->cpage['wiki_mode'] = 2;
   297       if(!isset($this->cpage['wiki_mode'])) $this->cpage['wiki_mode'] = 2;
   268       
   298       
   302         $this->page_protected = $this->cpage['protected'];
   332         $this->page_protected = $this->cpage['protected'];
   303       }
   333       }
   304     }
   334     }
   305     else
   335     else
   306     {
   336     {
   307       dc_here('paths: page doesn\'t exist, creating new page in memory<br />our page ID is: '.$this->page);
       
   308       $this->page_exists = false;
   337       $this->page_exists = false;
   309       $page_name = dirtify_page_id($this->page);
   338       $page_name = dirtify_page_id($this->page);
   310       $page_name = str_replace('_', ' ', $page_name);
   339       $page_name = str_replace('_', ' ', $page_name);
   311       
   340       
   312       $pid_cleaned = sanitize_page_id($this->page);
   341       $pid_cleaned = sanitize_page_id($this->page);
   313       if ( $pid_cleaned != $this->page )
   342       if ( $pid_cleaned != $this->page )
   314       {
   343       {
   315         redirect($pid_cleaned, 'Sanitizer message', 'page id sanitized', 0);
   344         redirect($pid_cleaned, 'Sanitizer message', 'page id sanitized', 0);
   316       }
   345       }
   317       
   346       
   318       $this->cpage = Array(
   347       if ( !is_array($this->cpage) )
   319         'name'=>$page_name,
   348       {
   320         'urlname'=>$this->page,
   349         $this->cpage = Array(
   321         'namespace'=>'Article',
   350           'name'=>$page_name,
   322         'special'=>0,
   351           'urlname'=>$this->page,
   323         'visible'=>0,
   352           'namespace'=>'Article',
   324         'comments_on'=>1,
   353           'special'=>0,
   325         'protected'=>0,
   354           'visible'=>0,
   326         'delvotes'=>0,
   355           'comments_on'=>1,
   327         'delvote_ips'=>'',
   356           'protected'=>0,
   328         'wiki_mode'=>2,
   357           'delvotes'=>0,
   329         );
   358           'delvote_ips'=>'',
       
   359           'wiki_mode'=>2,
       
   360           );
       
   361       }
   330       // Look for a namespace prefix in the urlname, and assign a different namespace, if necessary
   362       // Look for a namespace prefix in the urlname, and assign a different namespace, if necessary
   331       $k = array_keys($this->nslist);
   363       $k = array_keys($this->nslist);
   332       for($i=0;$i<sizeof($this->nslist);$i++)
   364       for($i=0;$i<sizeof($this->nslist);$i++)
   333       {
   365       {
   334         $ln = strlen($this->nslist[$k[$i]]);
   366         $ln = strlen($this->nslist[$k[$i]]);
   346       
   378       
   347       if($this->namespace=='System') 
   379       if($this->namespace=='System') 
   348       {
   380       {
   349         $this->cpage['protected'] = 1;
   381         $this->cpage['protected'] = 1;
   350       }
   382       }
   351       if($this->namespace == 'Special')
   383       if($this->namespace == 'Special' && !$this->anonymous_page)
   352       {
   384       {
   353         // Can't load nonexistent pages
   385         // Can't load nonexistent pages
   354         if( is_string(getConfig('main_page')) )
   386         if( is_string(getConfig('main_page')) )
   355         {
   387         {
   356           $main_page = makeUrl(getConfig('main_page'));
   388           $main_page = makeUrl(getConfig('main_page'));
   382     $session->init_permissions();
   414     $session->init_permissions();
   383   }
   415   }
   384   
   416   
   385   function add_page($flags)
   417   function add_page($flags)
   386   {
   418   {
   387     //dc_dump($flags, 'paths: page added by plugin:');
       
   388     $flags['urlname_nons'] = $flags['urlname'];
   419     $flags['urlname_nons'] = $flags['urlname'];
   389     $flags['urlname'] = $this->nslist[$flags['namespace']] . $flags['urlname']; // Applies the User:/File:/etc prefixes to the URL names
   420     $flags['urlname'] = $this->nslist[$flags['namespace']] . $flags['urlname']; // Applies the User:/File:/etc prefixes to the URL names
   390     $pages_len = sizeof($this->pages)/2;
   421     $pages_len = sizeof($this->pages)/2;
   391     $this->pages[$pages_len] = $flags;
   422     $this->pages[$pages_len] = $flags;
   392     $this->pages[$flags['urlname']] =& $this->pages[$pages_len];
   423     $this->pages[$flags['urlname']] =& $this->pages[$pages_len];
   407   }
   438   }
   408   
   439   
   409   function sysmsg($n)
   440   function sysmsg($n)
   410   {
   441   {
   411     global $db, $session, $paths, $template, $plugins; // Common objects
   442     global $db, $session, $paths, $template, $plugins; // Common objects
   412     dc_here('paths: system message requested: '.$n);
       
   413     $q = $db->sql_query('SELECT page_text, char_tag FROM '.table_prefix.'page_text WHERE page_id=\''.$db->escape($n).'\' AND namespace=\'System\'');
   443     $q = $db->sql_query('SELECT page_text, char_tag FROM '.table_prefix.'page_text WHERE page_id=\''.$db->escape($n).'\' AND namespace=\'System\'');
   414     if( !$q )
   444     if( !$q )
   415     {
   445     {
   416       $db->_die('Error during generic selection of system page data.');
   446       $db->_die('Error during generic selection of system page data.');
   417     }
   447     }