source: trunk/include/functions_url.inc.php @ 1503

Last change on this file since 1503 was 1503, checked in by nikrou, 18 years ago

function names are case-insensitive but it's a good idea to call functions
as they appear in their declaration.
So all functions names that manipulate url like make_index_url()
are write with lowercase

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.0 KB
Line 
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-07-26 21:00:16 +0000 (Wed, 26 Jul 2006) $
9// | last modifier : $Author: nikrou $
10// | revision      : $Revision: 1503 $
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 */
33function 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&amp;a=b
58 * @param string url
59 * @param array params
60 * @return string
61 */
62function 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 ) ? '?' :'&amp;';
74      }
75      else
76      {
77        $url .= '&amp;';
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 */
95function 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 */
124function 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 */
138function 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 */
175function 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 */
188function 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           and !is_numeric($params['image_file']) )
218      {
219        $url .= get_filename_wo_extension($params['image_file']);
220      }
221      else
222      {
223        $url .= $params['image_id'];
224      }
225      break;
226    default:
227      $url .= $params['image_id'];
228  }
229  $url .= make_section_in_url($params);
230  $url = add_well_known_params_in_url($url, $params);
231  return $url;
232}
233
234/**
235 *adds to the url the chronology and start parameters
236*/
237function add_well_known_params_in_url($url, $params)
238{
239  if ( isset($params['chronology_field']) )
240  {
241    $url .= '/'. $params['chronology_field'];
242    $url .= '-'. $params['chronology_style'];
243    if ( isset($params['chronology_view']) )
244    {
245      $url .= '-'. $params['chronology_view'];
246    }
247    if ( !empty($params['chronology_date']) )
248    {
249      $url .= '-'. implode('-', $params['chronology_date'] );
250    }
251  }
252
253  if (isset($params['start']) and $params['start'] > 0)
254  {
255    $url.= '/start-'.$params['start'];
256  }
257  return $url;
258}
259
260/**
261 * return the section token of an index or picture URL.
262 *
263 * Depending on section, other parameters are required (see function code
264 * for details)
265 *
266 * @param array
267 * @return string
268 */
269function make_section_in_url($params)
270{
271  global $conf;
272  $section_string = '';
273
274  $section_of = array(
275    'category' => 'categories',
276    'tags'     => 'tags',
277    'list'     => 'list',
278    'search'   => 'search',
279    );
280
281  foreach ($section_of as $param => $section)
282  {
283    if (isset($params[$param]))
284    {
285      $params['section'] = $section;
286    }
287  }
288
289  if (!isset($params['section']))
290  {
291    $params['section'] = 'categories';
292  }
293
294  switch($params['section'])
295  {
296    case 'categories' :
297    {
298      if (!isset($params['category']))
299      {
300        $section_string.= '/categories';
301      }
302      else
303      {
304        $section_string.= '/category/'.$params['category'];
305        if ($conf['category_url_style']=='id-name' and isset($params['cat_name']) )
306        {
307          if ( is_string($params['cat_name']) )
308          {
309            $section_string.= '-'.str2url($params['cat_name']);
310          }
311          elseif ( is_array( $params['cat_name'] ) and
312                isset( $params['cat_name'][$params['category']] ) )
313          {
314            $section_string.= '-'
315                .str2url($params['cat_name'][$params['category']]);
316          }
317        }
318      }
319
320      break;
321    }
322    case 'tags' :
323    {
324      if (!isset($params['tags']) or count($params['tags']) == 0)
325      {
326        die('make_section_in_url: require at least one tag');
327      }
328
329      $section_string.= '/tags';
330
331      foreach ($params['tags'] as $tag)
332      {
333        switch ( $conf['tag_url_style'] )
334        {
335          case 'id':
336            $section_string.= '/'.$tag['id'];
337            break;
338          case 'tag':
339            if (isset($tag['url_name']) and !is_numeric($tag['url_name']) )
340            {
341              $section_string.= '/'.$tag['url_name'];
342              break;
343            }
344          default:
345            $section_string.= '/'.$tag['id'];
346            if (isset($tag['url_name']))
347            {
348              $section_string.= '-'.$tag['url_name'];
349            }
350        }
351      }
352
353      break;
354    }
355    case 'search' :
356    {
357      if (!isset($params['search']))
358      {
359        die('make_section_in_url: require a search identifier');
360      }
361
362      $section_string.= '/search/'.$params['search'];
363
364      break;
365    }
366    case 'list' :
367    {
368      if (!isset($params['list']))
369      {
370        die('make_section_in_url: require a list of items');
371      }
372
373      $section_string.= '/list/'.implode(',', $params['list']);
374
375      break;
376    }
377    default :
378    {
379      $section_string.= '/'.$params['section'];
380    }
381  }
382
383  return $section_string;
384}
385?>
Note: See TracBrowser for help on using the repository browser.