1 | <?php |
---|
2 | // +-----------------------------------------------------------------------+ |
---|
3 | // | PhpWebGallery - a PHP based picture gallery | |
---|
4 | // | Copyright (C) 2003-2006 PhpWebGallery Team - http://phpwebgallery.net | |
---|
5 | // +-----------------------------------------------------------------------+ |
---|
6 | // | branch : BSF (Best So Far) |
---|
7 | // | file : $RCSfile$ |
---|
8 | // | last update : $Date: 2006-10-13 00:31:29 +0000 (Fri, 13 Oct 2006) $ |
---|
9 | // | last modifier : $Author: rvelices $ |
---|
10 | // | revision : $Revision: 1561 $ |
---|
11 | // +-----------------------------------------------------------------------+ |
---|
12 | // | This program is free software; you can redistribute it and/or modify | |
---|
13 | // | it under the terms of the GNU General Public License as published by | |
---|
14 | // | the Free Software Foundation | |
---|
15 | // | | |
---|
16 | // | This program is distributed in the hope that it will be useful, but | |
---|
17 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
18 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
19 | // | General Public License for more details. | |
---|
20 | // | | |
---|
21 | // | You should have received a copy of the GNU General Public License | |
---|
22 | // | along with this program; if not, write to the Free Software | |
---|
23 | // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
24 | // | USA. | |
---|
25 | // +-----------------------------------------------------------------------+ |
---|
26 | |
---|
27 | |
---|
28 | /** |
---|
29 | * returns a prefix for each url link on displayed page |
---|
30 | * and return an empty string for current path |
---|
31 | * @return string |
---|
32 | */ |
---|
33 | function get_root_url() |
---|
34 | { |
---|
35 | global $page; |
---|
36 | if ( isset($page['root_path']) ) |
---|
37 | { |
---|
38 | $root_url = $page['root_path']; |
---|
39 | } |
---|
40 | else |
---|
41 | { |
---|
42 | $root_url = PHPWG_ROOT_PATH; |
---|
43 | } |
---|
44 | if ( dirname($root_url)!='.' ) |
---|
45 | { |
---|
46 | return $root_url; |
---|
47 | } |
---|
48 | else |
---|
49 | { |
---|
50 | return substr($root_url, 2); |
---|
51 | } |
---|
52 | } |
---|
53 | |
---|
54 | /** |
---|
55 | * adds one or more _GET style parameters to an url |
---|
56 | * example: add_url_params('/x', array('a'=>'b')) returns /x?a=b |
---|
57 | * add_url_params('/x?cat_id=10', array('a'=>'b')) returns /x?cat_id=10&a=b |
---|
58 | * @param string url |
---|
59 | * @param array params |
---|
60 | * @return string |
---|
61 | */ |
---|
62 | function add_url_params($url, $params) |
---|
63 | { |
---|
64 | if ( !empty($params) ) |
---|
65 | { |
---|
66 | assert( is_array($params) ); |
---|
67 | $is_first = true; |
---|
68 | foreach($params as $param=>$val) |
---|
69 | { |
---|
70 | if ($is_first) |
---|
71 | { |
---|
72 | $is_first = false; |
---|
73 | $url .= ( strstr($url, '?')===false ) ? '?' :'&'; |
---|
74 | } |
---|
75 | else |
---|
76 | { |
---|
77 | $url .= '&'; |
---|
78 | } |
---|
79 | $url .= $param; |
---|
80 | if (isset($val)) |
---|
81 | { |
---|
82 | $url .= '='.$val; |
---|
83 | } |
---|
84 | } |
---|
85 | } |
---|
86 | return $url; |
---|
87 | } |
---|
88 | |
---|
89 | /** |
---|
90 | * build an index URL for a specific section |
---|
91 | * |
---|
92 | * @param array |
---|
93 | * @return string |
---|
94 | */ |
---|
95 | function make_index_url($params = array()) |
---|
96 | { |
---|
97 | global $conf; |
---|
98 | $url = get_root_url().'index'; |
---|
99 | if ($conf['php_extension_in_urls']) |
---|
100 | { |
---|
101 | $url .= '.php'; |
---|
102 | } |
---|
103 | if ($conf['question_mark_in_urls']) |
---|
104 | { |
---|
105 | $url .= '?'; |
---|
106 | } |
---|
107 | $url.= make_section_in_url($params); |
---|
108 | $url = add_well_known_params_in_url($url, $params); |
---|
109 | return $url; |
---|
110 | } |
---|
111 | |
---|
112 | /** |
---|
113 | * build an index URL with current page parameters, but with redefinitions |
---|
114 | * and removes. |
---|
115 | * |
---|
116 | * duplicate_index_url(array('category' => 12), array('start')) will create |
---|
117 | * an index URL on the current section (categories), but on a redefined |
---|
118 | * category and without the start URL parameter. |
---|
119 | * |
---|
120 | * @param array redefined keys |
---|
121 | * @param array removed keys |
---|
122 | * @return string |
---|
123 | */ |
---|
124 | function duplicate_index_url($redefined = array(), $removed = array()) |
---|
125 | { |
---|
126 | return make_index_url( |
---|
127 | params_for_duplication($redefined, $removed) |
---|
128 | ); |
---|
129 | } |
---|
130 | |
---|
131 | /** |
---|
132 | * returns $page global array with key redefined and key removed |
---|
133 | * |
---|
134 | * @param array redefined keys |
---|
135 | * @param array removed keys |
---|
136 | * @return array |
---|
137 | */ |
---|
138 | function params_for_duplication($redefined, $removed) |
---|
139 | { |
---|
140 | global $page; |
---|
141 | |
---|
142 | if (count($removed) > 0) |
---|
143 | { |
---|
144 | $params = array(); |
---|
145 | |
---|
146 | foreach ($page as $page_item_key => $page_item_value) |
---|
147 | { |
---|
148 | if (!in_array($page_item_key, $removed)) |
---|
149 | { |
---|
150 | $params[$page_item_key] = $page_item_value; |
---|
151 | } |
---|
152 | } |
---|
153 | } |
---|
154 | else |
---|
155 | { |
---|
156 | $params = $page; |
---|
157 | } |
---|
158 | |
---|
159 | foreach ($redefined as $redefined_param => $redefined_value) |
---|
160 | { |
---|
161 | $params[$redefined_param] = $redefined_value; |
---|
162 | } |
---|
163 | |
---|
164 | return $params; |
---|
165 | } |
---|
166 | |
---|
167 | /** |
---|
168 | * create a picture URL with current page parameters, but with redefinitions |
---|
169 | * and removes. See duplicate_index_url. |
---|
170 | * |
---|
171 | * @param array redefined keys |
---|
172 | * @param array removed keys |
---|
173 | * @return string |
---|
174 | */ |
---|
175 | function duplicate_picture_url($redefined = array(), $removed = array()) |
---|
176 | { |
---|
177 | return make_picture_url( |
---|
178 | params_for_duplication($redefined, $removed) |
---|
179 | ); |
---|
180 | } |
---|
181 | |
---|
182 | /** |
---|
183 | * create a picture URL on a specific section for a specific picture |
---|
184 | * |
---|
185 | * @param array |
---|
186 | * @return string |
---|
187 | */ |
---|
188 | function make_picture_url($params) |
---|
189 | { |
---|
190 | global $conf; |
---|
191 | if (!isset($params['image_id'])) |
---|
192 | { |
---|
193 | die('make_picture_url: image_id is a required parameter'); |
---|
194 | } |
---|
195 | |
---|
196 | $url = get_root_url().'picture'; |
---|
197 | if ($conf['php_extension_in_urls']) |
---|
198 | { |
---|
199 | $url .= '.php'; |
---|
200 | } |
---|
201 | if ($conf['question_mark_in_urls']) |
---|
202 | { |
---|
203 | $url .= '?'; |
---|
204 | } |
---|
205 | $url.= '/'; |
---|
206 | switch ( $conf['picture_url_style'] ) |
---|
207 | { |
---|
208 | case 'id-file': |
---|
209 | $url .= $params['image_id']; |
---|
210 | if ( isset($params['image_file']) ) |
---|
211 | { |
---|
212 | $url .= '-'.get_filename_wo_extension($params['image_file']); |
---|
213 | } |
---|
214 | break; |
---|
215 | case 'file': |
---|
216 | if ( isset($params['image_file']) ) |
---|
217 | { |
---|
218 | $fname_wo_ext = get_filename_wo_extension($params['image_file']); |
---|
219 | if (! preg_match('/^\d+(-|$)/', $fname_wo_ext) ) |
---|
220 | { |
---|
221 | $url .= $fname_wo_ext; |
---|
222 | break; |
---|
223 | } |
---|
224 | } |
---|
225 | default: |
---|
226 | $url .= $params['image_id']; |
---|
227 | } |
---|
228 | $url .= make_section_in_url($params); |
---|
229 | $url = add_well_known_params_in_url($url, $params); |
---|
230 | return $url; |
---|
231 | } |
---|
232 | |
---|
233 | /** |
---|
234 | *adds to the url the chronology and start parameters |
---|
235 | */ |
---|
236 | function add_well_known_params_in_url($url, $params) |
---|
237 | { |
---|
238 | if ( isset($params['chronology_field']) ) |
---|
239 | { |
---|
240 | $url .= '/'. $params['chronology_field']; |
---|
241 | $url .= '-'. $params['chronology_style']; |
---|
242 | if ( isset($params['chronology_view']) ) |
---|
243 | { |
---|
244 | $url .= '-'. $params['chronology_view']; |
---|
245 | } |
---|
246 | if ( !empty($params['chronology_date']) ) |
---|
247 | { |
---|
248 | $url .= '-'. implode('-', $params['chronology_date'] ); |
---|
249 | } |
---|
250 | } |
---|
251 | |
---|
252 | if (isset($params['start']) and $params['start'] > 0) |
---|
253 | { |
---|
254 | $url.= '/start-'.$params['start']; |
---|
255 | } |
---|
256 | return $url; |
---|
257 | } |
---|
258 | |
---|
259 | /** |
---|
260 | * return the section token of an index or picture URL. |
---|
261 | * |
---|
262 | * Depending on section, other parameters are required (see function code |
---|
263 | * for details) |
---|
264 | * |
---|
265 | * @param array |
---|
266 | * @return string |
---|
267 | */ |
---|
268 | function make_section_in_url($params) |
---|
269 | { |
---|
270 | global $conf; |
---|
271 | $section_string = ''; |
---|
272 | |
---|
273 | $section_of = array( |
---|
274 | 'category' => 'categories', |
---|
275 | 'tags' => 'tags', |
---|
276 | 'list' => 'list', |
---|
277 | 'search' => 'search', |
---|
278 | ); |
---|
279 | |
---|
280 | foreach ($section_of as $param => $section) |
---|
281 | { |
---|
282 | if (isset($params[$param])) |
---|
283 | { |
---|
284 | $params['section'] = $section; |
---|
285 | } |
---|
286 | } |
---|
287 | |
---|
288 | if (!isset($params['section'])) |
---|
289 | { |
---|
290 | $params['section'] = 'categories'; |
---|
291 | } |
---|
292 | |
---|
293 | switch($params['section']) |
---|
294 | { |
---|
295 | case 'categories' : |
---|
296 | { |
---|
297 | if (!isset($params['category'])) |
---|
298 | { |
---|
299 | $section_string.= '/categories'; |
---|
300 | } |
---|
301 | else |
---|
302 | { |
---|
303 | $section_string.= '/category/'.$params['category']; |
---|
304 | if ($conf['category_url_style']=='id-name' and isset($params['cat_name']) ) |
---|
305 | { |
---|
306 | if ( is_string($params['cat_name']) ) |
---|
307 | { |
---|
308 | $section_string.= '-'.str2url($params['cat_name']); |
---|
309 | } |
---|
310 | elseif ( is_array( $params['cat_name'] ) and |
---|
311 | isset( $params['cat_name'][$params['category']] ) ) |
---|
312 | { |
---|
313 | $section_string.= '-' |
---|
314 | .str2url($params['cat_name'][$params['category']]); |
---|
315 | } |
---|
316 | } |
---|
317 | } |
---|
318 | |
---|
319 | break; |
---|
320 | } |
---|
321 | case 'tags' : |
---|
322 | { |
---|
323 | if (!isset($params['tags']) or count($params['tags']) == 0) |
---|
324 | { |
---|
325 | die('make_section_in_url: require at least one tag'); |
---|
326 | } |
---|
327 | |
---|
328 | $section_string.= '/tags'; |
---|
329 | |
---|
330 | foreach ($params['tags'] as $tag) |
---|
331 | { |
---|
332 | switch ( $conf['tag_url_style'] ) |
---|
333 | { |
---|
334 | case 'id': |
---|
335 | $section_string.= '/'.$tag['id']; |
---|
336 | break; |
---|
337 | case 'tag': |
---|
338 | if (isset($tag['url_name']) and !is_numeric($tag['url_name']) ) |
---|
339 | { |
---|
340 | $section_string.= '/'.$tag['url_name']; |
---|
341 | break; |
---|
342 | } |
---|
343 | default: |
---|
344 | $section_string.= '/'.$tag['id']; |
---|
345 | if (isset($tag['url_name'])) |
---|
346 | { |
---|
347 | $section_string.= '-'.$tag['url_name']; |
---|
348 | } |
---|
349 | } |
---|
350 | } |
---|
351 | |
---|
352 | break; |
---|
353 | } |
---|
354 | case 'search' : |
---|
355 | { |
---|
356 | if (!isset($params['search'])) |
---|
357 | { |
---|
358 | die('make_section_in_url: require a search identifier'); |
---|
359 | } |
---|
360 | |
---|
361 | $section_string.= '/search/'.$params['search']; |
---|
362 | |
---|
363 | break; |
---|
364 | } |
---|
365 | case 'list' : |
---|
366 | { |
---|
367 | if (!isset($params['list'])) |
---|
368 | { |
---|
369 | die('make_section_in_url: require a list of items'); |
---|
370 | } |
---|
371 | |
---|
372 | $section_string.= '/list/'.implode(',', $params['list']); |
---|
373 | |
---|
374 | break; |
---|
375 | } |
---|
376 | default : |
---|
377 | { |
---|
378 | $section_string.= '/'.$params['section']; |
---|
379 | } |
---|
380 | } |
---|
381 | |
---|
382 | return $section_string; |
---|
383 | } |
---|
384 | ?> |
---|