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

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

Apply property svn:eol-style Value: LF

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 11.1 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | PhpWebGallery - a PHP based picture gallery                           |
4// | Copyright (C) 2003-2007 PhpWebGallery Team - http://phpwebgallery.net |
5// +-----------------------------------------------------------------------+
6// | file          : $Id: functions_url.inc.php 1900 2007-03-12 22:33:53Z rub $
7// | last update   : $Date: 2007-03-12 22:33:53 +0000 (Mon, 12 Mar 2007) $
8// | last modifier : $Author: rub $
9// | revision      : $Revision: 1900 $
10// +-----------------------------------------------------------------------+
11// | This program is free software; you can redistribute it and/or modify  |
12// | it under the terms of the GNU General Public License as published by  |
13// | the Free Software Foundation                                          |
14// |                                                                       |
15// | This program is distributed in the hope that it will be useful, but   |
16// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
17// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
18// | General Public License for more details.                              |
19// |                                                                       |
20// | You should have received a copy of the GNU General Public License     |
21// | along with this program; if not, write to the Free Software           |
22// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
23// | USA.                                                                  |
24// +-----------------------------------------------------------------------+
25
26
27/**
28 * returns a prefix for each url link on displayed page
29 * and return an empty string for current path
30 * @return string
31 */
32function get_root_url()
33{
34  global $page;
35  if ( isset($page['root_path']) )
36  {
37    $root_url = $page['root_path'];
38  }
39  else
40  {// TODO - add HERE the possibility to call PWG functions from external scripts
41    $root_url = PHPWG_ROOT_PATH;
42  }
43  if ( dirname($root_url)!='.' )
44  {
45    return $root_url;
46  }
47  else
48  {
49    return substr($root_url, 2);
50  }
51}
52
53/**
54 * returns the absolute url to the root of PWG
55 * @param boolean with_scheme if false - does not add http://toto.com
56 */
57function get_absolute_root_url($with_scheme=true)
58{
59  // TODO - add HERE the possibility to call PWG functions from external scripts
60  $url = '';
61  if ($with_scheme)
62  {
63    $url .= 'http://'.$_SERVER['HTTP_HOST'];
64    if ($_SERVER['SERVER_PORT']!=80)
65    {
66      $url .= ':'.$_SERVER['SERVER_PORT'];
67    }
68  }
69  $url .= cookie_path();
70  return $url;
71}
72
73/**
74 * adds one or more _GET style parameters to an url
75 * example: add_url_params('/x', array('a'=>'b')) returns /x?a=b
76 * add_url_params('/x?cat_id=10', array('a'=>'b')) returns /x?cat_id=10&amp;a=b
77 * @param string url
78 * @param array params
79 * @return string
80 */
81function add_url_params($url, $params)
82{
83  if ( !empty($params) )
84  {
85    assert( is_array($params) );
86    $is_first = true;
87    foreach($params as $param=>$val)
88    {
89      if ($is_first)
90      {
91        $is_first = false;
92        $url .= ( strstr($url, '?')===false ) ? '?' :'&amp;';
93      }
94      else
95      {
96        $url .= '&amp;';
97      }
98      $url .= $param;
99      if (isset($val))
100      {
101        $url .= '='.$val;
102      }
103    }
104  }
105  return $url;
106}
107
108/**
109 * build an index URL for a specific section
110 *
111 * @param array
112 * @return string
113 */
114function make_index_url($params = array())
115{
116  global $conf;
117  $url = get_root_url().'index';
118  if ($conf['php_extension_in_urls'])
119  {
120    $url .= '.php';
121  }
122  if ($conf['question_mark_in_urls'])
123  {
124    $url .= '?';
125  }
126  $url.= make_section_in_url($params);
127  $url = add_well_known_params_in_url($url, $params);
128  return $url;
129}
130
131/**
132 * build an index URL with current page parameters, but with redefinitions
133 * and removes.
134 *
135 * duplicate_index_url( array(
136 *   'category' => array('id'=>12, 'name'=>'toto'),
137 *   array('start')
138 * ) will create an index URL on the current section (categories), but on
139 * a redefined category and without the start URL parameter.
140 *
141 * @param array redefined keys
142 * @param array removed keys
143 * @return string
144 */
145function duplicate_index_url($redefined = array(), $removed = array())
146{
147  return make_index_url(
148    params_for_duplication($redefined, $removed)
149    );
150}
151
152/**
153 * returns $page global array with key redefined and key removed
154 *
155 * @param array redefined keys
156 * @param array removed keys
157 * @return array
158 */
159function params_for_duplication($redefined, $removed)
160{
161  global $page;
162
163  if (count($removed) > 0)
164  {
165    $params = array();
166
167    foreach ($page as $page_item_key => $page_item_value)
168    {
169      if (!in_array($page_item_key, $removed))
170      {
171        $params[$page_item_key] = $page_item_value;
172      }
173    }
174  }
175  else
176  {
177    $params = $page;
178  }
179
180  foreach ($redefined as $redefined_param => $redefined_value)
181  {
182    $params[$redefined_param] = $redefined_value;
183  }
184
185  return $params;
186}
187
188/**
189 * create a picture URL with current page parameters, but with redefinitions
190 * and removes. See duplicate_index_url.
191 *
192 * @param array redefined keys
193 * @param array removed keys
194 * @return string
195 */
196function duplicate_picture_url($redefined = array(), $removed = array())
197{
198  return make_picture_url(
199    params_for_duplication($redefined, $removed)
200    );
201}
202
203/**
204 * create a picture URL on a specific section for a specific picture
205 *
206 * @param array
207 * @return string
208 */
209function make_picture_url($params)
210{
211  global $conf;
212  if (!isset($params['image_id']))
213  {
214    die('make_picture_url: image_id is a required parameter');
215  }
216
217  $url = get_root_url().'picture';
218  if ($conf['php_extension_in_urls'])
219  {
220    $url .= '.php';
221  }
222  if ($conf['question_mark_in_urls'])
223  {
224    $url .= '?';
225  }
226  $url.= '/';
227  switch ( $conf['picture_url_style'] )
228  {
229    case 'id-file':
230      $url .= $params['image_id'];
231      if ( isset($params['image_file']) )
232      {
233        $url .= '-'.get_filename_wo_extension($params['image_file']);
234      }
235      break;
236    case 'file':
237      if ( isset($params['image_file']) )
238      {
239        $fname_wo_ext = get_filename_wo_extension($params['image_file']);
240        if (! preg_match('/^\d+(-|$)/', $fname_wo_ext) )
241        {
242          $url .= $fname_wo_ext;
243          break;
244        }
245      }
246    default:
247      $url .= $params['image_id'];
248  }
249  $url .= make_section_in_url($params);
250  $url = add_well_known_params_in_url($url, $params);
251  return $url;
252}
253
254/**
255 *adds to the url the chronology and start parameters
256*/
257function add_well_known_params_in_url($url, $params)
258{
259  if ( isset($params['chronology_field']) )
260  {
261    $url .= '/'. $params['chronology_field'];
262    $url .= '-'. $params['chronology_style'];
263    if ( isset($params['chronology_view']) )
264    {
265      $url .= '-'. $params['chronology_view'];
266    }
267    if ( !empty($params['chronology_date']) )
268    {
269      $url .= '-'. implode('-', $params['chronology_date'] );
270    }
271  }
272
273  if (isset($params['flat']))
274  {
275    $url.= '/flat';
276  }
277
278  if (isset($params['start']) and $params['start'] > 0)
279  {
280    $url.= '/start-'.$params['start'];
281  }
282  return $url;
283}
284
285/**
286 * return the section token of an index or picture URL.
287 *
288 * Depending on section, other parameters are required (see function code
289 * for details)
290 *
291 * @param array
292 * @return string
293 */
294function make_section_in_url($params)
295{
296  global $conf;
297  $section_string = '';
298
299  $section_of = array(
300    'category' => 'categories',
301    'tags'     => 'tags',
302    'list'     => 'list',
303    'search'   => 'search',
304    );
305
306  foreach ($section_of as $param => $section)
307  {
308    if (isset($params[$param]))
309    {
310      $params['section'] = $section;
311    }
312  }
313
314  if (!isset($params['section']))
315  {
316    $params['section'] = 'none';
317  }
318
319  switch($params['section'])
320  {
321    case 'categories' :
322    {
323      if (!isset($params['category']))
324      {
325        $section_string.= '/categories';
326      }
327      else
328      {
329        is_array($params['category']) or trigger_error(
330            'make_section_in_url wrong type for category', E_USER_WARNING
331            );
332        is_numeric($params['category']['id']) or trigger_error(
333            'make_section_in_url category id not numeric', E_USER_WARNING
334            );
335        isset($params['category']['name']) or trigger_error(
336            'make_section_in_url category name not set', E_USER_WARNING
337            );
338
339        array_key_exists('permalink', $params['category']) or trigger_error(
340            'make_section_in_url category permalink not set', E_USER_WARNING
341            );
342
343        $section_string.= '/category/';
344        if ( empty($params['category']['permalink']) )
345        {
346          $section_string.= $params['category']['id'];
347          if ( $conf['category_url_style']=='id-name' )
348          {
349            $section_string.= '-'.str2url($params['category']['name']);
350          }
351        }
352        else
353        {
354          $section_string.= $params['category']['permalink'];
355        }
356      }
357
358      break;
359    }
360    case 'tags' :
361    {
362      if (!isset($params['tags']) or count($params['tags']) == 0)
363      {
364        die('make_section_in_url: require at least one tag');
365      }
366
367      $section_string.= '/tags';
368
369      foreach ($params['tags'] as $tag)
370      {
371        switch ( $conf['tag_url_style'] )
372        {
373          case 'id':
374            $section_string.= '/'.$tag['id'];
375            break;
376          case 'tag':
377            if (isset($tag['url_name']) and !is_numeric($tag['url_name']) )
378            {
379              $section_string.= '/'.$tag['url_name'];
380              break;
381            }
382          default:
383            $section_string.= '/'.$tag['id'];
384            if (isset($tag['url_name']))
385            {
386              $section_string.= '-'.$tag['url_name'];
387            }
388        }
389      }
390
391      break;
392    }
393    case 'search' :
394    {
395      if (!isset($params['search']))
396      {
397        die('make_section_in_url: require a search identifier');
398      }
399
400      $section_string.= '/search/'.$params['search'];
401
402      break;
403    }
404    case 'list' :
405    {
406      if (!isset($params['list']))
407      {
408        die('make_section_in_url: require a list of items');
409      }
410
411      $section_string.= '/list/'.implode(',', $params['list']);
412
413      break;
414    }
415    case 'none' :
416    {
417      break;
418    }
419    default :
420    {
421      $section_string.= '/'.$params['section'];
422    }
423  }
424
425  return $section_string;
426}
427
428/**
429 * Indicate to build url with full path
430 *
431 * @param null
432 * @return null
433 */
434function set_make_full_url()
435{
436  global $page;
437
438  if (!isset($page['save_root_path']))
439  {
440    if (isset($page['root_path']))
441    {
442      $page['save_root_path']['path'] = $page['root_path'];
443    }
444    $page['save_root_path']['count'] = 1;
445    $page['root_path'] = get_absolute_root_url();
446  }
447  else
448  {
449    $page['save_root_path']['count'] += 1;
450  }
451}
452
453/**
454 * Restore old parameter to build url with full path
455 *
456 * @param null
457 * @return null
458 */
459function unset_make_full_url()
460{
461  global $page;
462
463  if (isset($page['save_root_path']))
464  {
465    if ($page['save_root_path']['count'] == 1)
466    {
467      if (isset($page['save_root_path']['path']))
468      {
469        $page['root_path'] = $page['save_root_path']['path'];
470      }
471      else
472      {
473        unset($page['root_path']);
474      }
475      unset($page['save_root_path']);
476    }
477    else
478    {
479      $page['save_root_path']['count'] -= 1;
480    }
481  }
482}
483
484?>
Note: See TracBrowser for help on using the repository browser.