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 GlossaryUrl() {
23 $this->rule = array(0=>'p');
27 * Construct pretty url by the given query string.
29 function constructPrettyUrl($query){
35 if (($pos = strpos($query, '#'))!==FALSE){
36 $bookmark = substr($query, $pos);
37 $query = substr($query, 0, $pos);
40 //If this is already a pretty url,but without mod_apache rule
41 //unwrap it and reconstruct
42 if (is_array($query)){
44 foreach($query as $fk=>$fv){
45 if (preg_match('/\.php/', $fv)==1){
46 continue; //skip the php file
49 //check if this is part of the rule, if so,add it, o/w ignore
50 if (array_search($fv, $this->rule)!==FALSE){
51 $new_query .= $fv . '=' . $query[$fk+1] . SEP;
52 } elseif (preg_match('/([0-9]+)\.html/', $fv, $matches)==1){
53 $new_query .= 'page=' . $matches[1] . SEP;
56 $query = $new_query; //done
59 $temp = explode(SEP, $query);
60 foreach ($temp as $index=>$attributes){
61 if(empty($attributes)){
62 //skip the ones that are empty.
65 list($key, $value) = preg_split('/\=/', $attributes, 2);
66 $query_parts[$key] = $value;
71 //construct pretty url on mapping
72 foreach ($this->rule as $key=>$value){
74 //if this value is empty, the url construction should quit.
75 if ($query_parts[$value] ==''){
78 $url .= $query_parts[$value].'/';
80 //if the query parts are not in the defined rules, set it back to query string again
81 if ($query_parts[$this->rule[$key]]!=''){
82 $query_parts[$this->rule[$key]] = '';
86 //Go through the query_parts again, and for those values that are not empty
87 // add it to the querystring
88 foreach($query_parts as $key=>$value){
89 //paginator are handle differently
90 if ($value!='' && $key!='page'){
91 $query_string .= $key.'='.$value.SEP;
94 //take out the last sep.
95 $query_string = substr($query_string, 0, -1);
98 if ($query_parts['page']!=''){
99 $url .= $query_parts['page'].'.html';
102 //append query string at the back
103 if ($query_string!=''){
104 $url .= '?'.$query_string;
107 //finally, append bookmark if not emptied