[2] | 1 | <?php |
---|
[362] | 2 | // +-----------------------------------------------------------------------+ |
---|
[593] | 3 | // | PhpWebGallery - a PHP based picture gallery | |
---|
| 4 | // | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net | |
---|
[675] | 5 | // | Copyright (C) 2003-2005 PhpWebGallery Team - http://phpwebgallery.net | |
---|
[362] | 6 | // +-----------------------------------------------------------------------+ |
---|
[593] | 7 | // | branch : BSF (Best So Far) |
---|
[362] | 8 | // | file : $RCSfile$ |
---|
| 9 | // | last update : $Date: 2006-02-12 14:08:57 +0000 (Sun, 12 Feb 2006) $ |
---|
| 10 | // | last modifier : $Author: nikrou $ |
---|
| 11 | // | revision : $Revision: 1034 $ |
---|
| 12 | // +-----------------------------------------------------------------------+ |
---|
| 13 | // | This program is free software; you can redistribute it and/or modify | |
---|
| 14 | // | it under the terms of the GNU General Public License as published by | |
---|
| 15 | // | the Free Software Foundation | |
---|
| 16 | // | | |
---|
| 17 | // | This program is distributed in the hope that it will be useful, but | |
---|
| 18 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
| 19 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
| 20 | // | General Public License for more details. | |
---|
| 21 | // | | |
---|
| 22 | // | You should have received a copy of the GNU General Public License | |
---|
| 23 | // | along with this program; if not, write to the Free Software | |
---|
| 24 | // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
| 25 | // | USA. | |
---|
| 26 | // +-----------------------------------------------------------------------+ |
---|
[2] | 27 | |
---|
[1013] | 28 | // The function generate_key creates a string with pseudo random characters. |
---|
| 29 | // the size of the string depends on the $conf['session_id_size']. |
---|
| 30 | // Characters used are a-z A-Z and numerical values. Examples : |
---|
| 31 | // "Er4Tgh6", "Rrp08P", "54gj" |
---|
| 32 | // input : none (using global variable) |
---|
| 33 | // output : $key |
---|
| 34 | function generate_key($size) |
---|
| 35 | { |
---|
| 36 | global $conf; |
---|
| 37 | |
---|
| 38 | $md5 = md5(substr(microtime(), 2, 6)); |
---|
| 39 | $init = ''; |
---|
| 40 | for ( $i = 0; $i < strlen( $md5 ); $i++ ) |
---|
| 41 | { |
---|
| 42 | if ( is_numeric( $md5[$i] ) ) $init.= $md5[$i]; |
---|
| 43 | } |
---|
| 44 | $init = substr( $init, 0, 8 ); |
---|
| 45 | mt_srand( $init ); |
---|
| 46 | $key = ''; |
---|
| 47 | for ( $i = 0; $i < $size; $i++ ) |
---|
| 48 | { |
---|
| 49 | $c = mt_rand( 0, 2 ); |
---|
| 50 | if ( $c == 0 ) $key .= chr( mt_rand( 65, 90 ) ); |
---|
| 51 | else if ( $c == 1 ) $key .= chr( mt_rand( 97, 122 ) ); |
---|
| 52 | else $key .= mt_rand( 0, 9 ); |
---|
| 53 | } |
---|
| 54 | return $key; |
---|
| 55 | } |
---|
| 56 | |
---|
[1007] | 57 | if (isset($conf['session_save_handler']) |
---|
[1013] | 58 | and ($conf['session_save_handler'] == 'db') |
---|
| 59 | and defined('PHPWG_INSTALLED')) |
---|
[1007] | 60 | { |
---|
[1004] | 61 | session_set_save_handler('pwg_session_open', |
---|
[1007] | 62 | 'pwg_session_close', |
---|
| 63 | 'pwg_session_read', |
---|
| 64 | 'pwg_session_write', |
---|
| 65 | 'pwg_session_destroy', |
---|
| 66 | 'pwg_session_gc' |
---|
| 67 | ); |
---|
[1023] | 68 | ini_set('session.use_cookies', $conf['session_use_cookies']); |
---|
| 69 | ini_set('session.use_only_cookies', $conf['session_use_only_cookies']); |
---|
| 70 | ini_set('session.use_trans_sid', intval($conf['session_use_trans_sid'])); |
---|
| 71 | ini_set('session.name', $conf['session_name']); |
---|
[1029] | 72 | ini_set('session.cookie_path', cookie_path() ); |
---|
[1004] | 73 | } |
---|
| 74 | |
---|
[1029] | 75 | // cookie_path returns the path to use for the PhpWebGallery cookie. |
---|
| 76 | // If PhpWebGallery is installed on : |
---|
| 77 | // http://domain.org/meeting/gallery/category.php |
---|
| 78 | // cookie_path will return : "/meeting/gallery" |
---|
| 79 | function cookie_path() |
---|
| 80 | { |
---|
| 81 | if ( isset($_SERVER['REDIRECT_URL']) ) |
---|
| 82 | { // mod_rewrite is activated for upper level directories. we must set the |
---|
| 83 | // cookie to the path shown in the browser otherwise it will be discarded. |
---|
| 84 | $scr = $_SERVER['REDIRECT_URL']; |
---|
| 85 | } |
---|
| 86 | else |
---|
| 87 | { |
---|
| 88 | $scr = $_SERVER['PHP_SELF']; |
---|
| 89 | } |
---|
| 90 | return substr($scr,0,strrpos( $scr,'/')); |
---|
| 91 | } |
---|
| 92 | |
---|
[1010] | 93 | /** |
---|
| 94 | * returns true; used when the session_start() function is called |
---|
| 95 | * |
---|
| 96 | * @params not use but useful for php engine |
---|
| 97 | */ |
---|
[1004] | 98 | function pwg_session_open($path, $name) |
---|
[2] | 99 | { |
---|
[1004] | 100 | return true; |
---|
| 101 | } |
---|
[45] | 102 | |
---|
[1010] | 103 | /** |
---|
| 104 | * returns true; used when the session is closed (unset($_SESSION)) |
---|
| 105 | * |
---|
| 106 | */ |
---|
[1004] | 107 | function pwg_session_close() |
---|
| 108 | { |
---|
| 109 | return true; |
---|
[2] | 110 | } |
---|
[45] | 111 | |
---|
[1010] | 112 | /** |
---|
| 113 | * this function returns |
---|
| 114 | * a string corresponding to the value of the variable save in the session |
---|
| 115 | * or an empty string when the variable doesn't exist |
---|
| 116 | * |
---|
| 117 | * @param string session id |
---|
| 118 | */ |
---|
[1004] | 119 | function pwg_session_read($session_id) |
---|
[2] | 120 | { |
---|
[1007] | 121 | $query = ' |
---|
[1010] | 122 | SELECT data |
---|
| 123 | FROM '.SESSIONS_TABLE.' |
---|
| 124 | WHERE id = \''.$session_id.'\' |
---|
| 125 | ;'; |
---|
[1004] | 126 | $result = pwg_query($query); |
---|
[1007] | 127 | if ($result) |
---|
| 128 | { |
---|
[1004] | 129 | $row = mysql_fetch_assoc($result); |
---|
| 130 | return $row['data']; |
---|
[1007] | 131 | } |
---|
| 132 | else |
---|
| 133 | { |
---|
[1004] | 134 | return ''; |
---|
[2] | 135 | } |
---|
| 136 | } |
---|
| 137 | |
---|
[1010] | 138 | /** |
---|
| 139 | * returns true; writes set a variable in the active session |
---|
| 140 | * |
---|
| 141 | * @param string session id |
---|
| 142 | * @data string value of date to be saved |
---|
| 143 | */ |
---|
[1004] | 144 | function pwg_session_write($session_id, $data) |
---|
[2] | 145 | { |
---|
[1007] | 146 | $query = ' |
---|
[1010] | 147 | SELECT id |
---|
| 148 | FROM '.SESSIONS_TABLE.' |
---|
| 149 | WHERE id = \''.$session_id.'\' |
---|
| 150 | ;'; |
---|
[1004] | 151 | $result = pwg_query($query); |
---|
[1007] | 152 | if (mysql_num_rows($result)) |
---|
| 153 | { |
---|
| 154 | $query = ' |
---|
[1010] | 155 | UPDATE '.SESSIONS_TABLE.' |
---|
[1032] | 156 | SET expiration = now(), |
---|
| 157 | data = \''.$data.'\' |
---|
[1010] | 158 | WHERE id = \''.$session_id.'\' |
---|
| 159 | ;'; |
---|
[1004] | 160 | pwg_query($query); |
---|
[1007] | 161 | } |
---|
| 162 | else |
---|
| 163 | { |
---|
| 164 | $query = ' |
---|
[1010] | 165 | INSERT INTO '.SESSIONS_TABLE.' |
---|
| 166 | (id,data,expiration) |
---|
| 167 | VALUES(\''.$session_id.'\',\''.$data.'\',now()) |
---|
| 168 | ;'; |
---|
[1004] | 169 | pwg_query($query); |
---|
[2] | 170 | } |
---|
[1004] | 171 | return true; |
---|
| 172 | } |
---|
[808] | 173 | |
---|
[1010] | 174 | /** |
---|
| 175 | * returns true; delete the active session |
---|
| 176 | * |
---|
| 177 | * @param string session id |
---|
| 178 | */ |
---|
[1004] | 179 | function pwg_session_destroy($session_id) |
---|
| 180 | { |
---|
[1007] | 181 | $query = ' |
---|
[1010] | 182 | DELETE |
---|
| 183 | FROM '.SESSIONS_TABLE.' |
---|
| 184 | WHERE id = \''.$session_id.'\' |
---|
| 185 | ;'; |
---|
[1004] | 186 | pwg_query($query); |
---|
| 187 | return true; |
---|
[2] | 188 | } |
---|
[45] | 189 | |
---|
[1010] | 190 | /** |
---|
| 191 | * returns true; delete expired sessions |
---|
| 192 | * called each time a session is closed. |
---|
| 193 | */ |
---|
[1004] | 194 | function pwg_session_gc() |
---|
[45] | 195 | { |
---|
[1004] | 196 | global $conf; |
---|
| 197 | |
---|
[1007] | 198 | $query = ' |
---|
[1010] | 199 | DELETE |
---|
| 200 | FROM '.SESSIONS_TABLE.' |
---|
[1007] | 201 | WHERE UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(expiration) > ' |
---|
[1010] | 202 | .$conf['session_length'].' |
---|
| 203 | ;'; |
---|
[1004] | 204 | pwg_query($query); |
---|
| 205 | return true; |
---|
[45] | 206 | } |
---|
[362] | 207 | ?> |
---|