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

Last change on this file since 29111 was 26461, checked in by mistic100, 11 years ago

Update headers to 2014. Happy new year!!

  • Property svn:eol-style set to LF
File size: 4.1 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2014 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/**
25 * @package functions\cookie
26 */
27
28
29/**
30 * Returns the path to use for the Piwigo cookie.
31 * If Piwigo is installed on :
32 * http://domain.org/meeting/gallery/
33 * it will return : "/meeting/gallery"
34 *
35 * @return string
36 */
37function cookie_path()
38{
39  if ( isset($_SERVER['REDIRECT_SCRIPT_NAME']) and
40       !empty($_SERVER['REDIRECT_SCRIPT_NAME']) )
41  {
42    $scr = $_SERVER['REDIRECT_SCRIPT_NAME'];
43  }
44  else if ( isset($_SERVER['REDIRECT_URL']) )
45  {
46    // mod_rewrite is activated for upper level directories. we must set the
47    // cookie to the path shown in the browser otherwise it will be discarded.
48    if
49      (
50        isset($_SERVER['PATH_INFO']) and !empty($_SERVER['PATH_INFO']) and
51        ($_SERVER['REDIRECT_URL'] !== $_SERVER['PATH_INFO']) and
52        (substr($_SERVER['REDIRECT_URL'],-strlen($_SERVER['PATH_INFO']))
53            == $_SERVER['PATH_INFO'])
54      )
55    {
56      $scr = substr($_SERVER['REDIRECT_URL'], 0,
57        strlen($_SERVER['REDIRECT_URL'])-strlen($_SERVER['PATH_INFO']));
58    }
59    else
60    {
61      $scr = $_SERVER['REDIRECT_URL'];
62    }
63  }
64  else
65  {
66    $scr = $_SERVER['SCRIPT_NAME'];
67  }
68
69  $scr = substr($scr,0,strrpos( $scr,'/'));
70
71  // add a trailing '/' if needed
72  if ((strlen($scr) == 0) or ($scr{strlen($scr)-1} !== '/'))
73  {
74    $scr .= '/';
75  }
76
77  if ( substr(PHPWG_ROOT_PATH,0,3)=='../')
78  { // this is maybe a plugin inside pwg directory
79    // TODO - what if it is an external script outside PWG ?
80    $scr = $scr.PHPWG_ROOT_PATH;
81    while (1)
82    {
83      $new = preg_replace('#[^/]+/\.\.(/|$)#', '', $scr);
84      if ($new==$scr)
85      {
86        break;
87      }
88      $scr=$new;
89    }
90  }
91  return $scr;
92}
93
94/**
95 * Persistently stores a variable in pwg cookie.
96 * Set $value to null to delete the cookie.
97 *
98 * @param string $car
99 * @param mixed $value
100 * @param int|null $expire
101 * @return bool
102 */
103function pwg_set_cookie_var($var, $value, $expire=null)
104{
105  if ($value==null or $expire===0)
106  {
107    unset($_COOKIE['pwg_'.$var]);
108    return setcookie('pwg_'.$var, false, 0, cookie_path());
109
110  }
111  else
112  {
113    $_COOKIE['pwg_'.$var] = $value;
114    $expire = is_numeric($expire) ? $expire : strtotime('+10 years');
115    return setcookie('pwg_'.$var, $value, $expire, cookie_path());
116  }
117}
118
119/**
120 * Retrieves the value of a persistent variable in pwg cookie
121 * @see pwg_set_cookie_var
122 *
123 * @param string $var
124 * @param mixed $default
125 * @return mixed
126 */
127function pwg_get_cookie_var($var, $default = null)
128{
129  if (isset($_COOKIE['pwg_'.$var]))
130  {
131    return $_COOKIE['pwg_'.$var];
132  }
133  else
134  {
135    return $default;
136  }
137}
138
139?>
Note: See TracBrowser for help on using the repository browser.