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

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

Issue 0000639: Force selected page on index.php

When page index.php is called without defined section, redirect to a page selected by random on a user list.

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