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.
23 function ContentUrl() {
24 $this->rule = array(0=>'cid');
28 * Construct pretty url by the given query string.
30 function constructPrettyUrl($query){
31 $url = ''; //url to be returned
32 $bookmark = ''; //html bookmark
39 if (($pos = strpos($query, '#'))!==FALSE){
40 $bookmark = substr($query, $pos);
41 $query = substr($query, 0, $pos);
44 //If this is already a pretty url,but without mod_apache rule
45 //unwrap it and reconstruct
46 if (is_array($query)){
48 foreach($query as $fk=>$fv){
49 if (preg_match('/\.php/', $fv)==1){
50 continue; //skip the php file
53 //check if this is part of the rule, if so,add it, o/w ignore
54 if (array_search($fv, $this->rule)!==FALSE){
55 $new_query .= $fv . '=' . $query[$fk+1] . SEP;
58 $query = $new_query; //done
61 $temp = explode(SEP, $query);
62 foreach ($temp as $index=>$attributes){
63 if(empty($attributes)){
64 //skip the ones that are empty.
67 list($key, $value) = preg_split('/\=/', $attributes, 2);
68 $query_parts[$key] = $value;
72 //construct pretty url on mapping
73 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