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

Last change on this file since 1131 was 1131, checked in by rvelices, 18 years ago

improvement: urls for tags can contain now only the tag or the id and tag

improvement: urls for category can be now id and category names (instead
of only id)

improvement: added 2 indexes (#image_tag.tag_id and #tags.url_name)

improvement: identification, register, search pages automatically set focus
on first form input

improvement: focus, nofocus css class now valid for all forms

fix: category comment is tag stripped in category_subcats.inc.php
(otherwise issues with html/scripts inside category comment)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.8 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-04-06 02:23:54 +0000 (Thu, 06 Apr 2006) $
9// | last modifier : $Author: rvelices $
10// | revision      : $Revision: 1131 $
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 * @return string
31 */
32function get_root_url()
33{
34  global $page;
35  if ( isset($page['root_path']) )
36  {
37    return $page['root_path'];
38  }
39  return PHPWG_ROOT_PATH;
40}
41
42/**
43 * adds one or more _GET style parameters to an url
44 * example: add_url_params('/x', array('a'=>'b')) returns /x?a=b
45 * add_url_params('/x?cat_id=10', array('a'=>'b')) returns /x?cat_id=10&amp;a=b
46 * @param string url
47 * @param array params
48 * @return string
49 */
50function add_url_params($url, $params)
51{
52  if ( !empty($params) )
53  {
54    assert( is_array($params) );
55    $is_first = true;
56    foreach($params as $param=>$val)
57    {
58      if ($is_first)
59      {
60        $is_first = false;
61        $url .= ( strstr($url, '?')===false ) ? '?' :'&amp;';
62      }
63      else
64      {
65        $url .= '&amp;';
66      }
67      $url .= $param;
68      if (isset($val))
69      {
70        $url .= '='.$val;
71      }
72    }
73  }
74  return $url;
75}
76
77/**
78 * build an index URL for a specific section
79 *
80 * @param array
81 * @return string
82 */
83function make_index_URL($params = array())
84{
85  global $conf;
86  $url = get_root_url().'index';
87  if ($conf['php_extension_in_urls'])
88  {
89    $url .= '.php';
90  }
91  if ($conf['question_mark_in_urls'])
92  {
93    $url .= '?';
94  }
95  $url.= make_section_in_URL($params);
96  $url = add_well_known_params_in_url($url, $params);
97  return $url;
98}
99
100/**
101 * build an index URL with current page parameters, but with redefinitions
102 * and removes.
103 *
104 * duplicate_index_URL(array('category' => 12), array('start')) will create
105 * an index URL on the current section (categories), but on a redefined
106 * category and without the start URL parameter.
107 *
108 * @param array redefined keys
109 * @param array removed keys
110 * @return string
111 */
112function duplicate_index_URL($redefined = array(), $removed = array())
113{
114  return make_index_URL(
115    params_for_duplication($redefined, $removed)
116    );
117}
118
119/**
120 * returns $page global array with key redefined and key removed
121 *
122 * @param array redefined keys
123 * @param array removed keys
124 * @return array
125 */
126function params_for_duplication($redefined, $removed)
127{
128  global $page;
129
130  if (count($removed) > 0)
131  {
132    $params = array();
133
134    foreach ($page as $page_item_key => $page_item_value)
135    {
136      if (!in_array($page_item_key, $removed))
137      {
138        $params[$page_item_key] = $page_item_value;
139      }
140    }
141  }
142  else
143  {
144    $params = $page;
145  }
146
147  foreach ($redefined as $redefined_param => $redefined_value)
148  {
149    $params[$redefined_param] = $redefined_value;
150  }
151
152  return $params;
153}
154
155/**
156 * create a picture URL with current page parameters, but with redefinitions
157 * and removes. See duplicate_index_URL.
158 *
159 * @param array redefined keys
160 * @param array removed keys
161 * @return string
162 */
163function duplicate_picture_URL($redefined = array(), $removed = array())
164{
165  return make_picture_URL(
166    params_for_duplication($redefined, $removed)
167    );
168}
169
170/**
171 * create a picture URL on a specific section for a specific picture
172 *
173 * @param array
174 * @return string
175 */
176function make_picture_URL($params)
177{
178  global $conf;
179  if (!isset($params['image_id']))
180  {
181    die('make_picture_URL: image_id is a required parameter');
182  }
183
184  $url = get_root_url().'picture';
185  if ($conf['php_extension_in_urls'])
186  {
187    $url .= '.php';
188  }
189  if ($conf['question_mark_in_urls'])
190  {
191    $url .= '?';
192  }
193  $url.= '/';
194  switch ( $conf['picture_url_style'] )
195  {
196    case 'id-file':
197      $url .= $params['image_id'];
198      if ( isset($params['image_file']) )
199      {
200        $url .= '-'.get_filename_wo_extension($params['image_file']);
201      }
202      break;
203    case 'file':
204      if ( isset($params['image_file'])
205           and !is_numeric($params['image_file']) )
206      {
207        $url .= get_filename_wo_extension($params['image_file']);
208      }
209      else
210      {
211        $url .= $params['image_id'];
212      }
213      break;
214    default:
215      $url .= $params['image_id'];
216  }
217  $url .= make_section_in_URL($params);
218  $url = add_well_known_params_in_url($url, $params);
219  return $url;
220}
221
222/**
223 *adds to the url the chronology and start parameters
224*/
225function add_well_known_params_in_url($url, $params)
226{
227  if ( isset($params['chronology_field']) )
228  {
229    $url .= '/'. $params['chronology_field'];
230    $url .= '-'. $params['chronology_style'];
231    if ( isset($params['chronology_view']) )
232    {
233      $url .= '-'. $params['chronology_view'];
234    }
235    if ( !empty($params['chronology_date']) )
236    {
237      $url .= '-'. implode('-', $params['chronology_date'] );
238    }
239  }
240
241  if (isset($params['start']) and $params['start'] > 0)
242  {
243    $url.= '/start-'.$params['start'];
244  }
245  return $url;
246}
247
248/**
249 * return the section token of an index or picture URL.
250 *
251 * Depending on section, other parameters are required (see function code
252 * for details)
253 *
254 * @param array
255 * @return string
256 */
257function make_section_in_URL($params)
258{
259  global $conf;
260  $section_string = '';
261
262  $section_of = array(
263    'category' => 'categories',
264    'tags'     => 'tags',
265    'list'     => 'list',
266    'search'   => 'search',
267    );
268
269  foreach ($section_of as $param => $section)
270  {
271    if (isset($params[$param]))
272    {
273      $params['section'] = $section;
274    }
275  }
276
277  if (!isset($params['section']))
278  {
279    $params['section'] = 'categories';
280  }
281
282  switch($params['section'])
283  {
284    case 'categories' :
285    {
286      if (!isset($params['category']))
287      {
288        $section_string.= '/categories';
289      }
290      else
291      {
292        $section_string.= '/category/'.$params['category'];
293        if ($conf['category_url_style']=='id-name' and isset($params['cat_name']) )
294        {
295          if ( is_string($params['cat_name']) )
296          {
297            $section_string.= '-'.str2url($params['cat_name']);
298          }
299          elseif ( is_array( $params['cat_name'] ) and
300                isset( $params['cat_name'][$params['category']] ) )
301          {
302            $section_string.= '-'
303                .str2url($params['cat_name'][$params['category']]);
304          }
305        }
306      }
307
308      break;
309    }
310    case 'tags' :
311    {
312      if (!isset($params['tags']) or count($params['tags']) == 0)
313      {
314        die('make_section_in_URL: require at least one tag');
315      }
316
317      $section_string.= '/tags';
318
319      foreach ($params['tags'] as $tag)
320      {
321        switch ( $conf['tag_url_style'] )
322        {
323          case 'id':
324            $section_string.= '/'.$tag['id'];
325            break;
326          case 'tag':
327            if (isset($tag['url_name']) and !is_numeric($tag['url_name']) )
328            {
329              $section_string.= '/'.$tag['url_name'];
330              break;
331            }
332          default:
333            $section_string.= '/'.$tag['id'];
334            if (isset($tag['url_name']))
335            {
336              $section_string.= '-'.$tag['url_name'];
337            }
338        }
339      }
340
341      break;
342    }
343    case 'search' :
344    {
345      if (!isset($params['search']))
346      {
347        die('make_section_in_URL: require a search identifier');
348      }
349
350      $section_string.= '/search/'.$params['search'];
351
352      break;
353    }
354    case 'list' :
355    {
356      if (!isset($params['list']))
357      {
358        die('make_section_in_URL: require a list of items');
359      }
360
361      $section_string.= '/list/'.implode(',', $params['list']);
362
363      break;
364    }
365    default :
366    {
367      $section_string.= '/'.$params['section'];
368    }
369  }
370
371  return $section_string;
372}
373?>
Note: See TracBrowser for help on using the repository browser.