2 /************************************************************************/
4 /************************************************************************/
5 /* Copyright (c) 2002-2008 by Greg Gay, Joel Kronenberg & Heidi Hazelton*/
6 /* Adaptive Technology Resource Centre / University of Toronto */
9 /* This program is free software. You can redistribute it and/or */
10 /* modify it under the terms of the GNU General Public License */
11 /* as published by the Free Software Foundation. */
12 /************************************************************************/
16 * Class for rewriting pretty urls on forums.
23 function GlossaryUrl() {
24 $this->rule = array(0=>'p');
28 * Construct pretty url by the given query string.
30 function constructPrettyUrl($query){
36 if (($pos = strpos($query, '#'))!==FALSE){
37 $bookmark = substr($query, $pos);
38 $query = substr($query, 0, $pos);
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;
108 //finally, append bookmark if not emptied