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.
22 function ContentUrl() {
23 $this->rule = array(0=>'cid');
27 * Construct pretty url by the given query string.
29 function constructPrettyUrl($query){
30 $url = ''; //url to be returned
31 $bookmark = ''; //html bookmark
38 if (($pos = strpos($query, '#'))!==FALSE){
39 $bookmark = substr($query, $pos);
40 $query = substr($query, 0, $pos);
43 //If this is already a pretty url,but without mod_apache rule
44 //unwrap it and reconstruct
45 if (is_array($query)){
47 foreach($query as $fk=>$fv){
48 if (preg_match('/\.php/', $fv)==1){
49 continue; //skip the php file
52 //check if this is part of the rule, if so,add it, o/w ignore
53 if (array_search($fv, $this->rule)!==FALSE){
54 $new_query .= $fv . '=' . $query[$fk+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;
71 //construct pretty url on mapping
72 foreach ($this->rule as $key=>$value){
73 //if this value is empty, the url construction should quit.
74 if ($query_parts[$value] ==''){
77 $url .= $query_parts[$value].'/';
79 //if the query parts are not in the defined rules, set it back to query string again
80 if ($query_parts[$this->rule[$key]]!=''){
81 $query_parts[$this->rule[$key]] = '';
85 //Go through the query_parts again, and for those values that are not empty
86 // add it to the querystring
87 foreach($query_parts as $key=>$value){
88 //paginator are handle differently
89 if ($value!='' && $key!='page'){
90 $query_string .= $key.'='.$value.SEP;
93 //take out the last sep.
94 $query_string = substr($query_string, 0, -1);
97 // if ($query_parts['page']!=''){
98 // $url .= '/'.$query_parts['page'].'.html';
101 //append query string at the back
102 if ($query_string!=''){
103 $url .= '?'.$query_string;
106 //finally, append bookmark if not emptied