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

Last change on this file since 1676 was 1676, checked in by rub, 17 years ago

Apply Nicco's style footer to HTML mail.
Add 2 news functions to indicate to build URL with full path

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.3 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-12-19 22:05:01 +0000 (Tue, 19 Dec 2006) $
9// | last modifier : $Author: rub $
10// | revision      : $Revision: 1676 $
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 * returns the url of the current host (e.g. http://www.toto.com )
56 * TODO: if required by someone, treat https case
57 */
58function get_host_url()
59{
60  $url = "http://";
61  $url .= $_SERVER['HTTP_HOST'];
62  if ($_SERVER['SERVER_PORT']!=80)
63  {
64    $url .= ':'.$_SERVER['SERVER_PORT'];
65  }
66  return $url;
67}
68
69/**
70 * adds one or more _GET style parameters to an url
71 * example: add_url_params('/x', array('a'=>'b')) returns /x?a=b
72 * add_url_params('/x?cat_id=10', array('a'=>'b')) returns /x?cat_id=10&amp;a=b
73 * @param string url
74 * @param array params
75 * @return string
76 */
77function add_url_params($url, $params)
78{
79  if ( !empty($params) )
80  {
81    assert( is_array($params) );
82    $is_first = true;
83    foreach($params as $param=>$val)
84    {
85      if ($is_first)
86      {
87        $is_first = false;
88        $url .= ( strstr($url, '?')===false ) ? '?' :'&amp;';
89      }
90      else
91      {
92        $url .= '&amp;';
93      }
94      $url .= $param;
95      if (isset($val))
96      {
97        $url .= '='.$val;
98      }
99    }
100  }
101  return $url;
102}
103
104/**
105 * build an index URL for a specific section
106 *
107 * @param array
108 * @return string
109 */
110function make_index_url($params = array())
111{
112  global $conf;
113  $url = get_root_url().'index';
114  if ($conf['php_extension_in_urls'])
115  {
116    $url .= '.php';
117  }
118  if ($conf['question_mark_in_urls'])
119  {
120    $url .= '?';
121  }
122  $url.= make_section_in_url($params);
123  $url = add_well_known_params_in_url($url, $params);
124  return $url;
125}
126
127/**
128 * build an index URL with current page parameters, but with redefinitions
129 * and removes.
130 *
131 * duplicate_index_url(array('category' => 12), array('start')) will create
132 * an index URL on the current section (categories), but on a redefined
133 * category and without the start URL parameter.
134 *
135 * @param array redefined keys
136 * @param array removed keys
137 * @return string
138 */
139function duplicate_index_url($redefined = array(), $removed = array())
140{
141  return make_index_url(
142    params_for_duplication($redefined, $removed)
143    );
144}
145
146/**
147 * returns $page global array with key redefined and key removed
148 *
149 * @param array redefined keys
150 * @param array removed keys
151 * @return array
152 */
153function params_for_duplication($redefined, $removed)
154{
155  global $page;
156
157  if (count($removed) > 0)
158  {
159    $params = array();
160
161    foreach ($page as $page_item_key => $page_item_value)
162    {
163      if (!in_array($page_item_key, $removed))
164      {
165        $params[$page_item_key] = $page_item_value;
166      }
167    }
168  }
169  else
170  {
171    $params = $page;
172  }
173
174  foreach ($redefined as $redefined_param => $redefined_value)
175  {
176    $params[$redefined_param] = $redefined_value;
177  }
178
179  return $params;
180}
181
182/**
183 * create a picture URL with current page parameters, but with redefinitions
184 * and removes. See duplicate_index_url.
185 *
186 * @param array redefined keys
187 * @param array removed keys
188 * @return string
189 */
190function duplicate_picture_url($redefined = array(), $removed = array())
191{
192  return make_picture_url(
193    params_for_duplication($redefined, $removed)
194    );
195}
196
197/**
198 * create a picture URL on a specific section for a specific picture
199 *
200 * @param array
201 * @return string
202 */
203function make_picture_url($params)
204{
205  global $conf;
206  if (!isset($params['image_id']))
207  {
208    die('make_picture_url: image_id is a required parameter');
209  }
210
211  $url = get_root_url().'picture';
212  if ($conf['php_extension_in_urls'])
213  {
214    $url .= '.php';
215  }
216  if ($conf['question_mark_in_urls'])
217  {
218    $url .= '?';
219  }
220  $url.= '/';
221  switch ( $conf['picture_url_style'] )
222  {
223    case 'id-file':
224      $url .= $params['image_id'];
225      if ( isset($params['image_file']) )
226      {
227        $url .= '-'.get_filename_wo_extension($params['image_file']);
228      }
229      break;
230    case 'file':
231      if ( isset($params['image_file']) )
232      {
233        $fname_wo_ext = get_filename_wo_extension($params['image_file']);
234        if (! preg_match('/^\d+(-|$)/', $fname_wo_ext) )
235        {
236          $url .= $fname_wo_ext;
237          break;
238        }
239      }
240    default:
241      $url .= $params['image_id'];
242  }
243  $url .= make_section_in_url($params);
244  $url = add_well_known_params_in_url($url, $params);
245  return $url;
246}
247
248/**
249 *adds to the url the chronology and start parameters
250*/
251function add_well_known_params_in_url($url, $params)
252{
253  if ( isset($params['chronology_field']) )
254  {
255    $url .= '/'. $params['chronology_field'];
256    $url .= '-'. $params['chronology_style'];
257    if ( isset($params['chronology_view']) )
258    {
259      $url .= '-'. $params['chronology_view'];
260    }
261    if ( !empty($params['chronology_date']) )
262    {
263      $url .= '-'. implode('-', $params['chronology_date'] );
264    }
265  }
266
267  if (isset($params['start']) and $params['start'] > 0)
268  {
269    $url.= '/start-'.$params['start'];
270  }
271  return $url;
272}
273
274/**
275 * return the section token of an index or picture URL.
276 *
277 * Depending on section, other parameters are required (see function code
278 * for details)
279 *
280 * @param array
281 * @return string
282 */
283function make_section_in_url($params)
284{
285  global $conf;
286  $section_string = '';
287
288  $section_of = array(
289    'category' => 'categories',
290    'tags'     => 'tags',
291    'list'     => 'list',
292    'search'   => 'search',
293    );
294
295  foreach ($section_of as $param => $section)
296  {
297    if (isset($params[$param]))
298    {
299      $params['section'] = $section;
300    }
301  }
302
303  if (!isset($params['section']))
304  {
305    $params['section'] = 'categories';
306  }
307
308  switch($params['section'])
309  {
310    case 'categories' :
311    {
312      if (!isset($params['category']))
313      {
314        $section_string.= '/categories';
315      }
316      else
317      {
318        $section_string.= '/category/'.$params['category'];
319        if ($conf['category_url_style']=='id-name' and isset($params['cat_name']) )
320        {
321          if ( is_string($params['cat_name']) )
322          {
323            $section_string.= '-'.str2url($params['cat_name']);
324          }
325          elseif ( is_array( $params['cat_name'] ) and
326                isset( $params['cat_name'][$params['category']] ) )
327          {
328            $section_string.= '-'
329                .str2url($params['cat_name'][$params['category']]);
330          }
331        }
332      }
333
334      break;
335    }
336    case 'tags' :
337    {
338      if (!isset($params['tags']) or count($params['tags']) == 0)
339      {
340        die('make_section_in_url: require at least one tag');
341      }
342
343      $section_string.= '/tags';
344
345      foreach ($params['tags'] as $tag)
346      {
347        switch ( $conf['tag_url_style'] )
348        {
349          case 'id':
350            $section_string.= '/'.$tag['id'];
351            break;
352          case 'tag':
353            if (isset($tag['url_name']) and !is_numeric($tag['url_name']) )
354            {
355              $section_string.= '/'.$tag['url_name'];
356              break;
357            }
358          default:
359            $section_string.= '/'.$tag['id'];
360            if (isset($tag['url_name']))
361            {
362              $section_string.= '-'.$tag['url_name'];
363            }
364        }
365      }
366
367      break;
368    }
369    case 'search' :
370    {
371      if (!isset($params['search']))
372      {
373        die('make_section_in_url: require a search identifier');
374      }
375
376      $section_string.= '/search/'.$params['search'];
377
378      break;
379    }
380    case 'list' :
381    {
382      if (!isset($params['list']))
383      {
384        die('make_section_in_url: require a list of items');
385      }
386
387      $section_string.= '/list/'.implode(',', $params['list']);
388
389      break;
390    }
391    default :
392    {
393      $section_string.= '/'.$params['section'];
394    }
395  }
396
397  return $section_string;
398}
399
400/**
401 * Indicate to build url with full path
402 *
403 * @param null
404 * @return null
405 */
406function set_make_full_url()
407{
408  global $page;
409
410  if (!isset($page['save_root_path']))
411  {
412    if (isset($page['root_path']))
413    {
414      $page['save_root_path']['path'] = $page['root_path'];
415    }
416    $page['save_root_path']['count'] = 1;
417    $page['root_path'] = 'http://'.$_SERVER['HTTP_HOST'].cookie_path();
418  }
419  else
420  {
421    $page['save_root_path']['count'] += 1;
422  }
423}
424
425/**
426 * Restore old parameter to build url with full path
427 *
428 * @param null
429 * @return null
430 */
431function unset_make_full_url()
432{
433  global $page;
434
435  if (isset($page['save_root_path']))
436  {
437    if ($page['save_root_path']['count'] == 1)
438    {
439      if (isset($page['save_root_path']['path']))
440      {
441        $page['root_path'] = $page['save_root_path']['path'];
442      }
443      else
444      {
445        unset($page['root_path']);
446      }
447      unset($page['save_root_path']);
448    }
449    else
450    {
451      $page['save_root_path']['count'] -= 1;
452    }
453  }
454}
455
456?>
Note: See TracBrowser for help on using the repository browser.