(bug 33471) compare detectProtocol to 'https'
[lhc/web/wiklou.git] / includes / PathRouter.php
index 53ddd42..2dbc7ec 100644 (file)
@@ -1,4 +1,25 @@
 <?php
+/**
+ * Parser to extract query parameters out of REQUEST_URI paths.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ */
+
 /**
  * PathRouter class.
  * This class can take patterns such as /wiki/$1 and use them to
@@ -34,7 +55,7 @@
  *     the relevant contents
  *   - The default behavior is equivalent to `array( 'title' => '$1' )`,
  *     if you don't want the title parameter you can explicitly use `array( 'title' => false )`
- *   - You can specify a value that won't have replacements in it 
+ *   - You can specify a value that won't have replacements in it
  *     using `'foo' => array( 'value' => 'bar' );`
  *
  * Options:
  */
 class PathRouter {
 
+       /**
+        * @var array
+        */
+       private $patterns = array();
+
        /**
         * Protected helper to do the actual bulk work of adding a single pattern.
         * This is in a separate method so that add() can handle the difference between
         * a single string $path and an array() $path that contains multiple path
         * patterns each with an associated $key to pass on.
+        * @param $path string
+        * @param $params array
+        * @param $options array
+        * @param $key null|string
         */
        protected function doAdd( $path, $params, $options, $key = null ) {
                // Make sure all paths start with a /
@@ -123,7 +153,7 @@ class PathRouter {
        /**
         * Add a new path pattern to the path router
         *
-        * @param $path string The path pattern to add
+        * @param $path string|array The path pattern to add
         * @param $params array The params for this path pattern
         * @param $options array The options for this path pattern
         */
@@ -140,6 +170,9 @@ class PathRouter {
        /**
         * Add a new path pattern to the path router with the strict option on
         * @see self::add
+        * @param $path string|array
+        * @param $params array
+        * @param $options array
         */
        public function addStrict( $path, $params = array(), $options = array() ) {
                $options['strict'] = true;
@@ -158,6 +191,10 @@ class PathRouter {
                array_multisort( $weights, SORT_DESC, SORT_NUMERIC, $this->patterns );
        }
 
+       /**
+        * @param $pattern object
+        * @return float|int
+        */
        protected static function makeWeight( $pattern ) {
                # Start with a weight of 0
                $weight = 0;
@@ -202,7 +239,7 @@ class PathRouter {
                // Make sure our patterns are sorted by weight so the most specific
                // matches are tested first
                $this->sortByWeight();
-               
+
                $matches = null;
 
                foreach ( $this->patterns as $pattern ) {
@@ -219,6 +256,11 @@ class PathRouter {
                return is_null( $matches ) ? array() : $matches;
        }
 
+       /**
+        * @param $path string
+        * @param $pattern string
+        * @return array|null
+        */
        protected static function extractTitle( $path, $pattern ) {
                // Convert the path pattern into a regexp we can match with
                $regexp = preg_quote( $pattern->path, '#' );
@@ -278,20 +320,14 @@ class PathRouter {
                                } elseif ( isset( $paramData['pattern'] ) ) {
                                        // For patterns we have to make value replacements on the string
                                        $value = $paramData['pattern'];
-                                       // For each $# match replace any $# within the value
-                                       foreach ( $m as $matchKey => $matchValue ) {
-                                               if ( preg_match( '/^par\d+$/u', $matchKey ) ) {
-                                                       $n = intval( substr( $matchKey, 3 ) );
-                                                       $value = str_replace( '$' . $n, rawurldecode( $matchValue ), $value );
-                                               }
-                                       }
-                                       // If a key was set replace any $key within the value
+                                       $replacer = new PathRouterPatternReplacer;
+                                       $replacer->params = $m;
                                        if ( isset( $pattern->key ) ) {
-                                               $value = str_replace( '$key', $pattern->key, $value );
+                                               $replacer->key = $pattern->key;
                                        }
-                                       if ( preg_match( '/\$(\d+|key)/u', $value ) ) {
-                                               // Still contains $# or $key patterns after replacement
-                                               // Seams like we don't have all the data, abort
+                                       $value = $replacer->replace( $value );
+                                       if ( $value === false ) {
+                                               // Pattern required data that wasn't available, abort
                                                return null;
                                        }
                                }
@@ -317,3 +353,47 @@ class PathRouter {
        }
 
 }
+
+class PathRouterPatternReplacer {
+
+       public $key, $params, $error;
+
+       /**
+        * Replace keys inside path router patterns with text.
+        * We do this inside of a replacement callback because after replacement we can't tell the
+        * difference between a $1 that was not replaced and a $1 that was part of
+        * the content a $1 was replaced with.
+        * @param $value string
+        * @return string
+        */
+       public function replace( $value ) {
+               $this->error = false;
+               $value = preg_replace_callback( '/\$(\d+|key)/u', array( $this, 'callback' ), $value );
+               if ( $this->error ) {
+                       return false;
+               }
+               return $value;
+       }
+
+       /**
+        * @param $m array
+        * @return string
+        */
+       protected function callback( $m ) {
+               if ( $m[1] == "key" ) {
+                       if ( is_null( $this->key ) ) {
+                               $this->error = true;
+                               return '';
+                       }
+                       return $this->key;
+               } else {
+                       $d = $m[1];
+                       if ( !isset( $this->params["par$d"] ) ) {
+                               $this->error = true;
+                               return '';
+                       }
+                       return rawurldecode( $this->params["par$d"] );
+               }
+       }
+
+}