[1992] | 1 | <?php |
---|
| 2 | // +-----------------------------------------------------------------------+ |
---|
[8728] | 3 | // | Piwigo - a PHP based photo gallery | |
---|
[2297] | 4 | // +-----------------------------------------------------------------------+ |
---|
[19703] | 5 | // | Copyright(C) 2008-2013 Piwigo Team http://piwigo.org | |
---|
[2297] | 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 | // +-----------------------------------------------------------------------+ |
---|
[1992] | 23 | |
---|
[25548] | 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 | */ |
---|
[1992] | 37 | function 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']) ) |
---|
[2004] | 45 | { |
---|
| 46 | // mod_rewrite is activated for upper level directories. we must set the |
---|
[1992] | 47 | // cookie to the path shown in the browser otherwise it will be discarded. |
---|
[2543] | 48 | if |
---|
| 49 | ( |
---|
[2006] | 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 | { |
---|
[2543] | 56 | $scr = substr($_SERVER['REDIRECT_URL'], 0, |
---|
[2006] | 57 | strlen($_SERVER['REDIRECT_URL'])-strlen($_SERVER['PATH_INFO'])); |
---|
| 58 | } |
---|
| 59 | else |
---|
| 60 | { |
---|
| 61 | $scr = $_SERVER['REDIRECT_URL']; |
---|
| 62 | } |
---|
[1992] | 63 | } |
---|
| 64 | else |
---|
| 65 | { |
---|
| 66 | $scr = $_SERVER['SCRIPT_NAME']; |
---|
| 67 | } |
---|
[2004] | 68 | |
---|
[1992] | 69 | $scr = substr($scr,0,strrpos( $scr,'/')); |
---|
| 70 | |
---|
| 71 | // add a trailing '/' if needed |
---|
[2010] | 72 | if ((strlen($scr) == 0) or ($scr{strlen($scr)-1} !== '/')) |
---|
| 73 | { |
---|
| 74 | $scr .= '/'; |
---|
| 75 | } |
---|
[2543] | 76 | |
---|
[1992] | 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 | /** |
---|
[25548] | 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 |
---|
[1992] | 102 | */ |
---|
[2543] | 103 | function pwg_set_cookie_var($var, $value, $expire=null) |
---|
[1992] | 104 | { |
---|
[2543] | 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 | } |
---|
[1992] | 117 | } |
---|
| 118 | |
---|
| 119 | /** |
---|
[25548] | 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 |
---|
[1992] | 125 | * @return mixed |
---|
| 126 | */ |
---|
| 127 | function 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 | |
---|
[25548] | 139 | ?> |
---|