source: trunk/include/functions_cookie.inc.php @ 2006

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

Issue 0000619: bad result of cookie_path() function

After discussion with Radu of an other case, PATH_INFO is re-introduce with news conditions.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 3.8 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | PhpWebGallery - a PHP based picture gallery                           |
4// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
5// | Copyright (C) 2003-2007 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | file          : $Id: functions_cookie.inc.php 2006 2007-05-11 20:07:47Z rub $
8// | last update   : $Date: 2007-05-11 20:07:47 +0000 (Fri, 11 May 2007) $
9// | last modifier : $Author: rub $
10// | revision      : $Revision: 2006 $
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// cookie_path returns the path to use for the PhpWebGallery cookie.
28// If PhpWebGallery is installed on :
29// http://domain.org/meeting/gallery/category.php
30// cookie_path will return : "/meeting/gallery"
31function cookie_path()
32{
33  if ( isset($_SERVER['REDIRECT_SCRIPT_NAME']) and
34       !empty($_SERVER['REDIRECT_SCRIPT_NAME']) )
35  {
36    $scr = $_SERVER['REDIRECT_SCRIPT_NAME'];
37  }
38  else if ( isset($_SERVER['REDIRECT_URL']) )
39  {
40    // mod_rewrite is activated for upper level directories. we must set the
41    // cookie to the path shown in the browser otherwise it will be discarded.
42    if 
43      ( 
44        isset($_SERVER['PATH_INFO']) and !empty($_SERVER['PATH_INFO']) and
45        ($_SERVER['REDIRECT_URL'] !== $_SERVER['PATH_INFO']) and
46        (substr($_SERVER['REDIRECT_URL'],-strlen($_SERVER['PATH_INFO']))
47            == $_SERVER['PATH_INFO'])
48      )
49    {
50      $scr = substr($_SERVER['REDIRECT_URL'], 0, 
51        strlen($_SERVER['REDIRECT_URL'])-strlen($_SERVER['PATH_INFO']));
52    }
53    else
54    {
55      $scr = $_SERVER['REDIRECT_URL'];
56    }
57  }
58  else
59  {
60    $scr = $_SERVER['SCRIPT_NAME'];
61  }
62
63  $scr = substr($scr,0,strrpos( $scr,'/'));
64
65  // add a trailing '/' if needed
66  $scr .= ($scr{strlen($scr)-1} == '/') ? '' : '/';
67 
68  if ( substr(PHPWG_ROOT_PATH,0,3)=='../')
69  { // this is maybe a plugin inside pwg directory
70    // TODO - what if it is an external script outside PWG ?
71    $scr = $scr.PHPWG_ROOT_PATH;
72    while (1)
73    {
74      $new = preg_replace('#[^/]+/\.\.(/|$)#', '', $scr);
75      if ($new==$scr)
76      {
77        break;
78      }
79      $scr=$new;
80    }
81  }
82  return $scr;
83}
84
85/**
86 * persistently stores a variable in pwg cookie
87 * @return boolean true on success
88 * @see pwg_get_cookie_var
89 */
90function pwg_set_cookie_var($var, $value)
91{
92  $_COOKIE['pwg_'.$var] = $value;
93  return
94    setcookie('pwg_'.$var, $value, 
95      strtotime('+10 years'), cookie_path());
96}
97
98/**
99 * retrieves the value of a persistent variable in pwg cookie
100 * @return mixed
101 * @see pwg_set_cookie_var
102 */
103function pwg_get_cookie_var($var, $default = null)
104{
105  if (isset($_COOKIE['pwg_'.$var]))
106  {
107    return $_COOKIE['pwg_'.$var];
108  }
109  else
110  {
111    return $default;
112  }
113}
114
115?>
Note: See TracBrowser for help on using the repository browser.