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

Revision 3282, 4.0 KB checked in by plg, 10 years ago (diff)

change: according to topic:15067, svn:keywords property was removed

  • Property svn:eol-style set to LF
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2009 Piwigo Team                  http://piwigo.org |
6// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
7// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
8// +-----------------------------------------------------------------------+
9// | This program is free software; you can redistribute it and/or modify  |
10// | it under the terms of the GNU General Public License as published by  |
11// | the Free Software Foundation                                          |
12// |                                                                       |
13// | This program is distributed in the hope that it will be useful, but   |
14// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
16// | General Public License for more details.                              |
17// |                                                                       |
18// | You should have received a copy of the GNU General Public License     |
19// | along with this program; if not, write to the Free Software           |
20// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
21// | USA.                                                                  |
22// +-----------------------------------------------------------------------+
23
24// cookie_path returns the path to use for the Piwigo cookie.
25// If Piwigo is installed on :
26// http://domain.org/meeting/gallery/category.php
27// cookie_path will return : "/meeting/gallery"
28function cookie_path()
29{
30  if ( isset($_SERVER['REDIRECT_SCRIPT_NAME']) and
31       !empty($_SERVER['REDIRECT_SCRIPT_NAME']) )
32  {
33    $scr = $_SERVER['REDIRECT_SCRIPT_NAME'];
34  }
35  else if ( isset($_SERVER['REDIRECT_URL']) )
36  {
37    // mod_rewrite is activated for upper level directories. we must set the
38    // cookie to the path shown in the browser otherwise it will be discarded.
39    if
40      (
41        isset($_SERVER['PATH_INFO']) and !empty($_SERVER['PATH_INFO']) and
42        ($_SERVER['REDIRECT_URL'] !== $_SERVER['PATH_INFO']) and
43        (substr($_SERVER['REDIRECT_URL'],-strlen($_SERVER['PATH_INFO']))
44            == $_SERVER['PATH_INFO'])
45      )
46    {
47      $scr = substr($_SERVER['REDIRECT_URL'], 0,
48        strlen($_SERVER['REDIRECT_URL'])-strlen($_SERVER['PATH_INFO']));
49    }
50    else
51    {
52      $scr = $_SERVER['REDIRECT_URL'];
53    }
54  }
55  else
56  {
57    $scr = $_SERVER['SCRIPT_NAME'];
58  }
59
60  $scr = substr($scr,0,strrpos( $scr,'/'));
61
62  // add a trailing '/' if needed
63  if ((strlen($scr) == 0) or ($scr{strlen($scr)-1} !== '/'))
64  {
65    $scr .= '/';
66  }
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, $expire=null)
91{
92  if ($value==null or $expire===0)
93  {
94    unset($_COOKIE['pwg_'.$var]);
95    return setcookie('pwg_'.$var, false, 0, cookie_path());
96
97  }
98  else
99  {
100    $_COOKIE['pwg_'.$var] = $value;
101    $expire = is_numeric($expire) ? $expire : strtotime('+10 years');
102    return setcookie('pwg_'.$var, $value, $expire, cookie_path());
103  }
104}
105
106/**
107 * retrieves the value of a persistent variable in pwg cookie
108 * @return mixed
109 * @see pwg_set_cookie_var
110 */
111function pwg_get_cookie_var($var, $default = null)
112{
113  if (isset($_COOKIE['pwg_'.$var]))
114  {
115    return $_COOKIE['pwg_'.$var];
116  }
117  else
118  {
119    return $default;
120  }
121}
122
123?>
Note: See TracBrowser for help on using the repository browser.