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

Last change on this file since 2229 was 2229, checked in by patdenice, 16 years ago

Merge from revision 2228.
Resolved bugs:
741 and 793: Added user does not appear in listing.
769: show email address in adviser mode when editing user profil.
800: get_absolute_root_url does not work with https protocol.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 17.3 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 2229 2008-02-29 13:10:58Z patdenice $
7// | last update   : $Date: 2008-02-29 13:10:58 +0000 (Fri, 29 Feb 2008) $
8// | last modifier : $Author: patdenice $
9// | revision      : $Revision: 2229 $
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 (string)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    if (empty($_SERVER['HTTPS']))
64    {
65      $url .= 'http://';
66    }
67    else
68    {
69      $url .= 'https://';
70    }
71    $url .= $_SERVER['HTTP_HOST'];
72    if ($_SERVER['SERVER_PORT'] != 80)
73    {
74      $url_port = ':'.$_SERVER['SERVER_PORT'];
75      if (strrchr($url, ':') != $url_port)
76      {
77        $url .= $url_port;
78      }
79    }
80  }
81  $url .= cookie_path();
82  return $url;
83}
84
85/**
86 * adds one or more _GET style parameters to an url
87 * example: add_url_params('/x', array('a'=>'b')) returns /x?a=b
88 * add_url_params('/x?cat_id=10', array('a'=>'b')) returns /x?cat_id=10&amp;a=b
89 * @param string url
90 * @param array params
91 * @return string
92 */
93function add_url_params($url, $params)
94{
95  if ( !empty($params) )
96  {
97    assert( is_array($params) );
98    $is_first = true;
99    foreach($params as $param=>$val)
100    {
101      if ($is_first)
102      {
103        $is_first = false;
104        $url .= ( strstr($url, '?')===false ) ? '?' :'&amp;';
105      }
106      else
107      {
108        $url .= '&amp;';
109      }
110      $url .= $param;
111      if (isset($val))
112      {
113        $url .= '='.$val;
114      }
115    }
116  }
117  return $url;
118}
119
120/**
121 * build an index URL for a specific section
122 *
123 * @param array
124 * @return string
125 */
126function make_index_url($params = array())
127{
128  global $conf;
129  $url = get_root_url().'index';
130  if ($conf['php_extension_in_urls'])
131  {
132    $url .= '.php';
133  }
134  if ($conf['question_mark_in_urls'])
135  {
136    $url .= '?';
137  }
138  $url.= make_section_in_url($params);
139  $url = add_well_known_params_in_url($url, $params);
140  return $url;
141}
142
143/**
144 * build an index URL with current page parameters, but with redefinitions
145 * and removes.
146 *
147 * duplicate_index_url( array(
148 *   'category' => array('id'=>12, 'name'=>'toto'),
149 *   array('start')
150 * ) will create an index URL on the current section (categories), but on
151 * a redefined category and without the start URL parameter.
152 *
153 * @param array redefined keys
154 * @param array removed keys
155 * @return string
156 */
157function duplicate_index_url($redefined = array(), $removed = array())
158{
159  return make_index_url(
160    params_for_duplication($redefined, $removed)
161    );
162}
163
164/**
165 * returns $page global array with key redefined and key removed
166 *
167 * @param array redefined keys
168 * @param array removed keys
169 * @return array
170 */
171function params_for_duplication($redefined, $removed)
172{
173  global $page;
174
175  if (count($removed) > 0)
176  {
177    $params = array();
178
179    foreach ($page as $page_item_key => $page_item_value)
180    {
181      if (!in_array($page_item_key, $removed))
182      {
183        $params[$page_item_key] = $page_item_value;
184      }
185    }
186  }
187  else
188  {
189    $params = $page;
190  }
191
192  foreach ($redefined as $redefined_param => $redefined_value)
193  {
194    $params[$redefined_param] = $redefined_value;
195  }
196
197  return $params;
198}
199
200/**
201 * create a picture URL with current page parameters, but with redefinitions
202 * and removes. See duplicate_index_url.
203 *
204 * @param array redefined keys
205 * @param array removed keys
206 * @return string
207 */
208function duplicate_picture_url($redefined = array(), $removed = array())
209{
210  return make_picture_url(
211    params_for_duplication($redefined, $removed)
212    );
213}
214
215/**
216 * create a picture URL on a specific section for a specific picture
217 *
218 * @param array
219 * @return string
220 */
221function make_picture_url($params)
222{
223  global $conf;
224  if (!isset($params['image_id']))
225  {
226    die('make_picture_url: image_id is a required parameter');
227  }
228
229  $url = get_root_url().'picture';
230  if ($conf['php_extension_in_urls'])
231  {
232    $url .= '.php';
233  }
234  if ($conf['question_mark_in_urls'])
235  {
236    $url .= '?';
237  }
238  $url.= '/';
239  switch ( $conf['picture_url_style'] )
240  {
241    case 'id-file':
242      $url .= $params['image_id'];
243      if ( isset($params['image_file']) )
244      {
245        $url .= '-'.get_filename_wo_extension($params['image_file']);
246      }
247      break;
248    case 'file':
249      if ( isset($params['image_file']) )
250      {
251        $fname_wo_ext = get_filename_wo_extension($params['image_file']);
252        if (! preg_match('/^\d+(-|$)/', $fname_wo_ext) )
253        {
254          $url .= $fname_wo_ext;
255          break;
256        }
257      }
258    default:
259      $url .= $params['image_id'];
260  }
261  if ( !isset($params['category'] ) )
262  {// make urls shorter ...
263    unset( $params['flat'] );
264  }
265  $url .= make_section_in_url($params);
266  $url = add_well_known_params_in_url($url, $params);
267  return $url;
268}
269
270/**
271 *adds to the url the chronology and start parameters
272*/
273function add_well_known_params_in_url($url, $params)
274{
275  if ( isset($params['chronology_field']) )
276  {
277    $url .= '/'. $params['chronology_field'];
278    $url .= '-'. $params['chronology_style'];
279    if ( isset($params['chronology_view']) )
280    {
281      $url .= '-'. $params['chronology_view'];
282    }
283    if ( !empty($params['chronology_date']) )
284    {
285      $url .= '-'. implode('-', $params['chronology_date'] );
286    }
287  }
288
289  if (isset($params['flat']))
290  {
291    $url.= '/flat';
292  }
293
294  if (isset($params['start']) and $params['start'] > 0)
295  {
296    $url.= '/start-'.$params['start'];
297  }
298  return $url;
299}
300
301/**
302 * return the section token of an index or picture URL.
303 *
304 * Depending on section, other parameters are required (see function code
305 * for details)
306 *
307 * @param array
308 * @return string
309 */
310function make_section_in_url($params)
311{
312  global $conf;
313  $section_string = '';
314
315  $section_of = array(
316    'category' => 'categories',
317    'tags'     => 'tags',
318    'list'     => 'list',
319    'search'   => 'search',
320    );
321
322  foreach ($section_of as $param => $section)
323  {
324    if (isset($params[$param]))
325    {
326      $params['section'] = $section;
327    }
328  }
329
330  if (!isset($params['section']))
331  {
332    $params['section'] = 'none';
333  }
334
335  switch($params['section'])
336  {
337    case 'categories' :
338    {
339      if (!isset($params['category']))
340      {
341        $section_string.= '/categories';
342      }
343      else
344      {
345        is_array($params['category']) or trigger_error(
346            'make_section_in_url wrong type for category', E_USER_WARNING
347            );
348        is_numeric($params['category']['id']) or trigger_error(
349            'make_section_in_url category id not numeric', E_USER_WARNING
350            );
351        isset($params['category']['name']) or trigger_error(
352            'make_section_in_url category name not set', E_USER_WARNING
353            );
354
355        array_key_exists('permalink', $params['category']) or trigger_error(
356            'make_section_in_url category permalink not set', E_USER_WARNING
357            );
358
359        $section_string.= '/category/';
360        if ( empty($params['category']['permalink']) )
361        {
362          $section_string.= $params['category']['id'];
363          if ( $conf['category_url_style']=='id-name' )
364          {
365            $section_string.= '-'.str2url($params['category']['name']);
366          }
367        }
368        else
369        {
370          $section_string.= $params['category']['permalink'];
371        }
372      }
373
374      break;
375    }
376    case 'tags' :
377    {
378      if (!isset($params['tags']) or count($params['tags']) == 0)
379      {
380        die('make_section_in_url: require at least one tag');
381      }
382
383      $section_string.= '/tags';
384
385      foreach ($params['tags'] as $tag)
386      {
387        switch ( $conf['tag_url_style'] )
388        {
389          case 'id':
390            $section_string.= '/'.$tag['id'];
391            break;
392          case 'tag':
393            if (isset($tag['url_name']) and !is_numeric($tag['url_name']) )
394            {
395              $section_string.= '/'.$tag['url_name'];
396              break;
397            }
398          default:
399            $section_string.= '/'.$tag['id'];
400            if (isset($tag['url_name']))
401            {
402              $section_string.= '-'.$tag['url_name'];
403            }
404        }
405      }
406
407      break;
408    }
409    case 'search' :
410    {
411      if (!isset($params['search']))
412      {
413        die('make_section_in_url: require a search identifier');
414      }
415
416      $section_string.= '/search/'.$params['search'];
417
418      break;
419    }
420    case 'list' :
421    {
422      if (!isset($params['list']))
423      {
424        die('make_section_in_url: require a list of items');
425      }
426
427      $section_string.= '/list/'.implode(',', $params['list']);
428
429      break;
430    }
431    case 'none' :
432    {
433      break;
434    }
435    default :
436    {
437      $section_string.= '/'.$params['section'];
438    }
439  }
440
441  return $section_string;
442}
443
444/**
445 * the reverse of make_section_in_url
446 * returns the 'section' (categories/tags/...) and the data associated with it
447 *
448 * Depending on section, other parameters are returned (category/tags/list/...)
449 *
450 * @param array of url tokens to parse
451 * @param int the index in the array of url tokens; in/out
452 * @return array
453 */
454function parse_section_url( $tokens, &$next_token)
455{
456  $page=array();
457  if (0 === strpos(@$tokens[$next_token], 'categor'))
458  {
459    $page['section'] = 'categories';
460    $next_token++;
461
462    if (isset($tokens[$next_token]) )
463    {
464      if (preg_match('/^(\d+)(?:-(.+))?$/', $tokens[$next_token], $matches))
465      {
466        if ( isset($matches[2]) )
467          $page['hit_by']['cat_url_name'] = $matches[2];
468        $page['category'] = $matches[1];
469        $next_token++;
470      }
471      else
472      {// try a permalink
473        $maybe_permalinks = array();
474        $current_token = $next_token;
475        while ( isset($tokens[$current_token])
476            and strpos($tokens[$current_token], 'created-')!==0
477            and strpos($tokens[$current_token], 'posted-')!==0
478            and strpos($tokens[$next_token], 'start-')!==0
479            and $tokens[$current_token] != 'flat')
480        {
481          if (empty($maybe_permalinks))
482          {
483            array_push($maybe_permalinks, $tokens[$current_token]);
484          }
485          else
486          {
487            array_push($maybe_permalinks,
488                $maybe_permalinks[count($maybe_permalinks)-1]
489                . '/' . $tokens[$current_token]
490              );
491          }
492          $current_token++;
493        }
494
495        if ( count($maybe_permalinks) )
496        {
497          $cat_id = get_cat_id_from_permalinks($maybe_permalinks, $perma_index);
498          if ( isset($cat_id) )
499          {
500            $next_token += $perma_index+1;
501            $page['category'] = $cat_id;
502            $page['hit_by']['cat_permalink'] = $maybe_permalinks[$perma_index];
503          }
504          else
505          {
506            page_not_found('Permalink for album not found');
507          }
508        }
509      }
510    }
511
512    if (isset($page['category']))
513    {
514      $result = get_cat_info($page['category']);
515      if (empty($result))
516      {
517        page_not_found('Requested category does not exist' );
518      }
519      $page['category']=$result;
520    }
521  }
522  else if (0 === strpos(@$tokens[$next_token], 'tag'))
523  {
524    $page['section'] = 'tags';
525    $page['tags'] = array();
526
527    $next_token++;
528    $i = $next_token;
529
530    $requested_tag_ids = array();
531    $requested_tag_url_names = array();
532
533    while (isset($tokens[$i]))
534    {
535      if ( preg_match('/^(created-|posted-|start-(\d)+)/', $tokens[$i]) )
536        break;
537
538      if ( preg_match('/^(\d+)(?:-(.*))?/', $tokens[$i], $matches) )
539      {
540        array_push($requested_tag_ids, $matches[1]);
541      }
542      else
543      {
544        array_push($requested_tag_url_names, $tokens[$i]);
545      }
546      $i++;
547    }
548    $next_token = $i;
549
550    if ( empty($requested_tag_ids) && empty($requested_tag_url_names) )
551    {
552      bad_request('at least one tag required');
553    }
554
555    $page['tags'] = find_tags($requested_tag_ids, $requested_tag_url_names);
556    if ( empty($page['tags']) )
557    {
558      page_not_found('Requested tag does not exist', get_root_url().'tags.php' );
559    }
560  }
561  else if (0 === strpos(@$tokens[$next_token], 'fav'))
562  {
563    $page['section'] = 'favorites';
564    $next_token++;
565  }
566  else if ('most_visited' == @$tokens[$next_token])
567  {
568    $page['section'] = 'most_visited';
569    $next_token++;
570  }
571  else if ('best_rated' == @$tokens[$next_token])
572  {
573    $page['section'] = 'best_rated';
574    $next_token++;
575  }
576  else if ('recent_pics' == @$tokens[$next_token])
577  {
578    $page['section'] = 'recent_pics';
579    $next_token++;
580  }
581  else if ('recent_cats' == @$tokens[$next_token])
582  {
583    $page['section'] = 'recent_cats';
584    $next_token++;
585  }
586  else if ('search' == @$tokens[$next_token])
587  {
588    $page['section'] = 'search';
589    $next_token++;
590
591    preg_match('/(\d+)/', @$tokens[$next_token], $matches);
592    if (!isset($matches[1]))
593    {
594      bad_request('search identifier is missing');
595    }
596    $page['search'] = $matches[1];
597    $next_token++;
598  }
599  else if ('list' == @$tokens[$next_token])
600  {
601    $page['section'] = 'list';
602    $next_token++;
603
604    $page['list'] = array();
605
606    // No pictures
607    if (empty($tokens[$next_token]))
608    {
609      // Add dummy element list
610      array_push($page['list'], -1);
611    }
612    // With pictures list
613    else
614    {
615      if (!preg_match('/^\d+(,\d+)*$/', $tokens[$next_token]))
616      {
617        bad_request('wrong format on list GET parameter');
618      }
619      foreach (explode(',', $tokens[$next_token]) as $image_id)
620      {
621        array_push($page['list'], $image_id);
622      }
623    }
624    $next_token++;
625  }
626  return $page;
627}
628
629/**
630 * the reverse of add_well_known_params_in_url
631 * parses start, flat and chronology from url tokens
632*/
633function parse_well_known_params_url($tokens, &$i)
634{
635  $page = array();
636  while (isset($tokens[$i]))
637  {
638    if (preg_match('/^start-(\d+)/', $tokens[$i], $matches))
639    {
640      $page['start'] = $matches[1];
641    }
642
643    if ( 'flat' == $tokens[$i] )
644    {
645      // indicate a special list of images
646      $page['flat'] = true;
647    }
648
649    if (preg_match('/^(posted|created)/', $tokens[$i] ))
650    {
651      $chronology_tokens = explode('-', $tokens[$i] );
652
653      $page['chronology_field'] = $chronology_tokens[0];
654
655      array_shift($chronology_tokens);
656      $page['chronology_style'] = $chronology_tokens[0];
657
658      array_shift($chronology_tokens);
659      if ( count($chronology_tokens)>0 )
660      {
661        if ('list'==$chronology_tokens[0] or
662            'calendar'==$chronology_tokens[0])
663        {
664          $page['chronology_view'] = $chronology_tokens[0];
665          array_shift($chronology_tokens);
666        }
667        $page['chronology_date'] = $chronology_tokens;
668      }
669    }
670    $i++;
671  }
672  return $page;
673}
674
675/**
676 * Indicate to build url with full path
677 *
678 * @param null
679 * @return null
680 */
681function set_make_full_url()
682{
683  global $page;
684
685  if (!isset($page['save_root_path']))
686  {
687    if (isset($page['root_path']))
688    {
689      $page['save_root_path']['path'] = $page['root_path'];
690    }
691    $page['save_root_path']['count'] = 1;
692    $page['root_path'] = get_absolute_root_url();
693  }
694  else
695  {
696    $page['save_root_path']['count'] += 1;
697  }
698}
699
700/**
701 * Restore old parameter to build url with full path
702 *
703 * @param null
704 * @return null
705 */
706function unset_make_full_url()
707{
708  global $page;
709
710  if (isset($page['save_root_path']))
711  {
712    if ($page['save_root_path']['count'] == 1)
713    {
714      if (isset($page['save_root_path']['path']))
715      {
716        $page['root_path'] = $page['save_root_path']['path'];
717      }
718      else
719      {
720        unset($page['root_path']);
721      }
722      unset($page['save_root_path']);
723    }
724    else
725    {
726      $page['save_root_path']['count'] -= 1;
727    }
728  }
729}
730
731/**
732 * Embellish the url argument
733 *
734 * @param $url
735 * @return $url embellished
736 */
737function embellish_url($url)
738{
739  return str_replace('/./', '/', $url);
740}
741
742?>
Note: See TracBrowser for help on using the repository browser.