2 /************************************************************************/
4 /************************************************************************/
5 /* Copyright (c) 2002-2010 */
6 /* Inclusive Design Institute */
8 /* This program is free software. You can redistribute it and/or */
9 /* modify it under the terms of the GNU General Public License */
10 /* as published by the Free Software Foundation. */
11 /************************************************************************/
15 * Class for rewriting pretty urls on forums.
22 // var $rule; //an array that maps [lvl->query parts]
23 // var $className; //the name of this class
27 $this->rule = array(0=>'tid', 1=>'action');
32 * Construct pretty url by the given query string.
34 function constructPrettyUrl($query){
35 $url = ''; //url to be returned
41 //If this is already a pretty url,but without mod_apache rule
42 //unwrap it and reconstruct
43 if (is_array($query)){
45 foreach($query as $fk=>$fv){
46 if (preg_match('/\.php/', $fv)==1){
47 continue; //skip the php file
50 //check if this is part of the rule, if so,add it, o/w ignore
51 if (array_search($fv, $this->rule)!==FALSE){
52 $new_query .= $fv . '=' . $query[$fk+1] . SEP;
53 } elseif (preg_match('/([0-9]+)\.html/', $fv, $matches)==1){
54 $new_query .= 'page=' . $matches[1] . SEP;
57 $query = $new_query; //done
60 $temp = explode(SEP, $query);
61 foreach ($temp as $index=>$attributes){
62 if(empty($attributes)){
63 //skip the ones that are empty.
66 list($key, $value) = preg_split('/\=/', $attributes, 2);
67 $query_parts[$key] = $value;
72 //construct pretty url on mapping
73 foreach ($this->rule as $key=>$value){
75 //if this value is empty, the url construction should quit.
76 if ($query_parts[$value] ==''){
79 $url .= $query_parts[$value].'/';
81 //if the query parts are not in the defined rules, set it back to query string again
82 if ($query_parts[$this->rule[$key]]!=''){
83 $query_parts[$this->rule[$key]] = '';
87 //Go through the query_parts again, and for those values that are not empty
88 // add it to the querystring
89 foreach($query_parts as $key=>$value){
90 //paginator are handle differently
91 if ($value!='' && $key!='page'){
92 $query_string .= $key.'='.$value.SEP;
95 //take out the last sep.
96 $query_string = substr($query_string, 0, -1);
99 if ($query_parts['page']!=''){
100 $url .= $query_parts['page'].'.html';
103 //append query string at the back
104 if ($query_string!=''){
105 $url .= '?'.$query_string;