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 function ForumsUrl() {
23 $this->rule = array(0=>'fid', 1=>'pid');
28 * Construct pretty url by the given query string.
30 function constructPrettyUrl($query){
35 //If this is already a pretty url,but without mod_apache rule
36 //unwrap it and reconstruct
37 if (is_array($query)){
39 foreach($query as $fk=>$fv){
40 if (preg_match('/\.php/', $fv)==1){
41 continue; //skip the php file
44 //check if this is part of the rule, if so,add it, o/w ignore
45 if (array_search($fv, $this->rule)!==FALSE){
46 $new_query .= $fv . '=' . $query[$fk+1] . SEP;
47 } elseif (preg_match('/([0-9]+)\.html/', $fv, $matches)==1){
48 $new_query .= 'page=' . $matches[1] . SEP;
51 $query = $new_query; //done
54 $temp = explode(SEP, $query);
55 foreach ($temp as $index=>$attributes){
56 if(empty($attributes)){
57 //skip the ones that are empty.
60 list($key, $value) = preg_split('/\=/', $attributes, 2);
61 $query_parts[$key] = $value;
66 //construct pretty url on mapping
67 foreach ($this->rule as $key=>$value){
69 //if this value is empty, the url construction should quit.
70 if ($query_parts[$value] ==''){
73 $url .= $query_parts[$value].'/';
75 //if the query parts are not in the defined rules, set it back to query string again
76 if ($query_parts[$this->rule[$key]]!=''){
77 $query_parts[$this->rule[$key]] = '';
81 //Go through the query_parts again, and for those values that are not empty
82 // add it to the querystring
83 foreach($query_parts as $key=>$value){
84 //paginator are handle differently
85 if ($value!='' && $key!='page'){
86 $query_string .= $key.'='.$value.SEP;
89 //take out the last sep.
90 $query_string = substr($query_string, 0, -1);
93 if ($query_parts['page']!=''){
94 $url .= $query_parts['page'].'.html';
97 //append query string at the back
98 if ($query_string!=''){
99 $url .= '?'.$query_string;