root/trunk/wifidog-auth/wifidog/include/common.php @ 920

Revision 920, 10.8 KB (checked in by max-horvath, 7 years ago)

"Configuration file (config.php) will be read before path detection is being started (otherwise there was no chance to set a manual SYSTEM_PATH)"

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
RevLine 
[152]1<?php
[866]2
[895]3
[866]4/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
5
6// +-------------------------------------------------------------------+
7// | WiFiDog Authentication Server                                     |
8// | =============================                                     |
9// |                                                                   |
10// | The WiFiDog Authentication Server is part of the WiFiDog captive  |
11// | portal suite.                                                     |
12// +-------------------------------------------------------------------+
13// | PHP version 5 required.                                           |
14// +-------------------------------------------------------------------+
15// | Homepage:     http://www.wifidog.org/                             |
16// | Source Forge: http://sourceforge.net/projects/wifidog/            |
17// +-------------------------------------------------------------------+
18// | This program is free software; you can redistribute it and/or     |
19// | modify it under the terms of the GNU General Public License as    |
20// | published by the Free Software Foundation; either version 2 of    |
21// | the License, or (at your option) any later version.               |
22// |                                                                   |
23// | This program is distributed in the hope that it will be useful,   |
24// | but WITHOUT ANY WARRANTY; without even the implied warranty of    |
25// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the     |
26// | GNU General Public License for more details.                      |
27// |                                                                   |
28// | You should have received a copy of the GNU General Public License |
29// | along with this program; if not, contact:                         |
30// |                                                                   |
31// | Free Software Foundation           Voice:  +1-617-542-5942        |
32// | 59 Temple Place - Suite 330        Fax:    +1-617-542-2652        |
33// | Boston, MA  02111-1307,  USA       gnu@gnu.org                    |
34// |                                                                   |
35// +-------------------------------------------------------------------+
36
37/**
38 * @package    WiFiDogAuthServer
39 * @author     Benoit Gregoire <bock@step.polymtl.ca>
[916]40 * @copyright  2004-2006 Benoit Gregoire, Technologies Coeus inc.
41 * @version    Subversion $Id$
42 * @link       http://www.wifidog.org/
[866]43 */
44
[916]45/**
[920]46 * Include configuration file
[916]47 */
[920]48cmnRequireConfig();
[866]49
[874]50/**
[920]51 * Include path detection code
[874]52 */
[920]53require_once ('path_defines_base.php');
[916]54
[895]55require_once ('path_defines_url_content.php');
[716]56
[891]57
[716]58undo_magic_quotes();
59
[895]60require_once ('classes/EventLogging.php');
61require_once ('classes/AbstractDb.php');
62require_once ('classes/Locale.php');//Must be included for gettext handling
63require_once ('classes/Dependencies.php');
[888]64// require_once('classes/Session.php');
[685]65
[152]66global $db;
[874]67
[888]68// $db = AbstractDb::Connect('DEFAULT');
[152]69$db = new AbstractDb();
[162]70
[895]71/* Constant shared with the gateway
72 * NEVER edit these, as they mush match the C code of the gateway */
[686]73define('ACCOUNT_STATUS_ERROR', -1);
74define('ACCOUNT_STATUS_DENIED', 0);
75define('ACCOUNT_STATUS_ALLOWED', 1);
76define('ACCOUNT_STATUS_VALIDATION', 5);
77define('ACCOUNT_STATUS_VALIDATION_FAILED', 6);
78define('ACCOUNT_STATUS_LOCKED', 254);
[152]79
80$account_status_to_text[ACCOUNT_STATUS_ERROR] = "Error";
81$account_status_to_text[ACCOUNT_STATUS_DENIED] = "Denied";
82$account_status_to_text[ACCOUNT_STATUS_ALLOWED] = "Allowed";
83$account_status_to_text[ACCOUNT_STATUS_VALIDATION] = "Validation";
84$account_status_to_text[ACCOUNT_STATUS_VALIDATION_FAILED] = "Validation Failed";
85$account_status_to_text[ACCOUNT_STATUS_LOCKED] = "Locked";
86
[686]87define('TOKEN_UNUSED', 'UNUSED');
88define('TOKEN_INUSE', 'INUSE');
89define('TOKEN_USED', 'USED');
[152]90
[895]91$token_to_text[TOKEN_UNUSED] = _("Unused");
92$token_to_text[TOKEN_INUSE] = _("In use");
93$token_to_text[TOKEN_USED] = _("Used");
[152]94
[686]95define('STAGE_LOGIN', "login");
96define('STAGE_LOGOUT', "logout");
97define('STAGE_COUNTERS', "counters");
[152]98
[686]99define('ONLINE_STATUS_ONLINE', 1);
100define('ONLINE_STATUS_OFFLINE', 2);
[895]101/* End Constant shared with the gateway*/
[152]102
[895]103/* session constants, perhaps this coulb be moved to Session.php?  benoitg, 2005-08-01 */
[216]104define('SESS_USERNAME_VAR', 'SESS_USERNAME');
[512]105define('SESS_USER_ID_VAR', 'SESS_USER_ID');
[216]106define('SESS_PASSWORD_HASH_VAR', 'SESS_PASSWORD_HASH');
107define('SESS_ORIGINAL_URL_VAR', 'SESS_ORIGINAL_URL');
[316]108define('SESS_LANGUAGE_VAR', 'SESS_LANGUAGE');
[497]109define('SESS_GW_ADDRESS_VAR', 'SESS_GW_ADDRESS');
110define('SESS_GW_PORT_VAR', 'SESS_GW_PORT');
111define('SESS_GW_ID_VAR', 'SESS_GW_ID');
[895]112/* End session constants */
[216]113
[316]114
[905]115function stripslashes_cb(&$item, $key)
116{
117   $item = stripslashes($item);
118}
[311]119
[895]120function undo_magic_quotes() {
121    if (get_magic_quotes_gpc()) {
[905]122        array_walk_recursive($_GET, 'stripslashes_cb');
123        array_walk_recursive($_POST, 'stripslashes_cb');
124        array_walk_recursive($_COOKIE, 'stripslashes_cb');
125        array_walk_recursive($_REQUEST, 'stripslashes_cb');
[895]126    }
[686]127}
[655]128
[174]129/** Convert a password hash form a NoCat passwd file into the same format as get_password_hash().
130* @return The 32 character hash.
131*/
[895]132function convert_nocat_password_hash($hash) {
[866]133    return $hash.'==';
[174]134}
135
[895]136function iso8601_date($unix_timestamp) {
[866]137    $tzd = date('O', $unix_timestamp);
138    $tzd = substr(chunk_split($tzd, 3, ':'), 0, 6);
139    $date = date('Y-m-d\TH:i:s', $unix_timestamp).$tzd;
140    return $date;
[206]141}
142
[152]143/** Cleanup dangling tokens and connections from the database, left if a gateway crashed, etc. */
[895]144function garbage_collect() {
[866]145    global $db;
[152]146
[866]147    // 10 minutes
148    $expiration = time() - 60 * 10;
149    $expiration = iso8601_date($expiration);
[877]150    $db->execSqlUpdate("UPDATE connections SET token_status='".TOKEN_USED."' WHERE last_updated < '$expiration' AND token_status = '".TOKEN_INUSE."'", false);
[152]151}
152
153/** Get the url from the local content_specific folder if the file exists, and from the default content folder otherwise */
[895]154function find_local_content_url($filename) {
[866]155    //echo "find_local_content_url():  Looking for:                  ".NODE_CONTENT_PHP_RELATIVE_PATH.$filename."<br>\n";
[895]156    if (is_file(NODE_CONTENT_PHP_RELATIVE_PATH.$filename)) {
[866]157        $retval = NODE_CONTENT_URL.$filename;
158    }
[895]159    else {
[866]160        $retval = DEFAULT_CONTENT_URL.$filename;
161    }
162    //echo "find_local_content_url():  Returned:                  $retval<br>\n";
163    return $retval;
[152]164}
[512]165
166/** Return a 32 byte guid valid for database use */
[895]167function get_guid() {
[866]168    return md5(uniqid(rand(), true));
[512]169}
170
[672]171/** like the php function print_r(), but the way it was meant to be... */
[895]172function pretty_print_r($param) {
[866]173    echo "\n<pre>\n";
174    print_r($param);
175    echo "\n</pre>\n";
[672]176}
[866]177
[888]178/** pop directory path */
[895]179function cmnPopDir($dirname = null, $popcount = 1) {
180    if (empty ($dirname))
181        $dirname = dirname($_SERVER['PHP_SELF']);
182    if ($dirname === DIRECTORY_SEPARATOR)
183        return DIRECTORY_SEPARATOR;
184    if (substr($dirname, -1, 1) === DIRECTORY_SEPARATOR)
185        $popcount ++;
[888]186
[895]187    $popped = implode(DIRECTORY_SEPARATOR, array_slice(explode(DIRECTORY_SEPARATOR, $dirname), 0, - $popcount));
[888]188
[895]189    return empty ($popped) ? DIRECTORY_SEPARATOR : substr($popped, -1, 1) === DIRECTORY_SEPARATOR ? $popped : $popped.DIRECTORY_SEPARATOR;
[888]190}
191
192function cmnDirectorySlash($dirname) {
[895]193    return empty ($dirname) ? DIRECTORY_SEPARATOR : substr($dirname, -1, 1) === DIRECTORY_SEPARATOR ? $dirname : $dirname.DIRECTORY_SEPARATOR;
[888]194}
195
196/** search parent directory hierarchy for a file */
197function cmnSearchParentDirectories($dirname, $searchfor) {
[895]198    $pieces = explode(DIRECTORY_SEPARATOR, $dirname);
199    $is_absolute = substr($dirname, 0, 1) === DIRECTORY_SEPARATOR ? 1 : 0;
[888]200
[895]201    for ($i = count($pieces); $i > $is_absolute; $i --) {
202        $filename = implode(DIRECTORY_SEPARATOR, array_merge(array_slice($pieces, 0, $i), array ($searchfor)));
203        if (file_exists($filename))
204            return $filename;
205    }
[888]206
[895]207    return false;
[888]208}
209
210/** join file path pieces together */
211function cmnJoinPath() {
[895]212    $fullpath = '';
[888]213
[895]214    //$arguments = func_get_args();
[888]215
[895]216    for ($i = 0; $i < func_num_args(); $i ++) {
217        $pathelement = func_get_arg($i);
218        if ($pathelement == '')
219            continue;
[888]220
[895]221        if ($fullpath == '')
222            $fullpath = $pathelement;
223        elseif (substr($fullpath, -1, 1) == DIRECTORY_SEPARATOR) {
224            if (substr($pathelement, 0, 1) == DIRECTORY_SEPARATOR)
225                $fullpath .= substr($pathelement, 1);
226            else
227                $fullpath .= $pathelement;
228        }
229        else {
230            if (substr($pathelement, 0, 1) == DIRECTORY_SEPARATOR)
231                $fullpath .= $pathelement;
232            else
233                $fullpath .= DIRECTORY_SEPARATOR.$pathelement;
234        }
[888]235    }
236
[895]237    return $fullpath;
[888]238}
239
240/** find a named file in the include path */
[895]241function cmnFindPackage($rel_path, $private = false) {
[888]242
[895]243    $paths = isset ($private) && ($private === true || $private === 'PRIVATE') ? array (WIFIDOG_ABS_FILE_PATH) : explode(PATH_SEPARATOR, get_include_path());
[888]244
[895]245    foreach ($paths as $topdir) {
246        $package = cmnJoinPath($topdir, $rel_path);
247        if (file_exists($package)) {
248            if ($private)
249                return $package;
250            else
251                return $rel_path;
252        }
[888]253    }
254
[895]255    return false; // package was not found
[888]256}
257
258/** require_once a named file */
[895]259function cmnRequirePackage($rel_path, $private = false) {
[888]260
[895]261    $paths = isset ($private) && ($private === true || $private === 'PRIVATE') ? array (WIFIDOG_ABS_FILE_PATH) : explode(PATH_SEPARATOR, get_include_path());
[888]262
[895]263    foreach ($paths as $topdir) {
264        $package = cmnJoinPath($topdir, $rel_path);
265        if (file_exists($package)) {
266            if ($private)
267                @ require_once $package;
268            else
269                @ require_once $rel_path;
[888]270
[895]271            return true; // package was found
272        }
[888]273    }
274
[895]275    return false; // package was not found
[888]276}
277
278/** include_once a named file */
[895]279function cmnIncludePackage($rel_path, $private = false) {
[888]280
[895]281    $paths = isset ($private) && ($private === true || $private === 'PRIVATE') ? array (WIFIDOG_ABS_FILE_PATH) : explode(PATH_SEPARATOR, get_include_path());
[888]282
[895]283    foreach ($paths as $topdir) {
284        $package = cmnJoinPath($topdir, $rel_path);
285        if (file_exists($package)) {
286            if ($private)
287                @ include_once $package;
288            else
289                @ include_once $rel_path;
[888]290
[895]291            return true; // package was found
292        }
[888]293    }
294
[895]295    return false; // package was not found
[888]296}
297
[895]298function cmnRequireConfig($config_file = 'config.php') {
299    global $AVAIL_LOCALE_ARRAY; // so that nobody has to change their custom config.php
300    $config_path = cmnSearchParentDirectories(dirname(__FILE__), $config_file);
301    if (!empty ($config_path))
302        require_once ($config_path);
[888]303}
304
[866]305/*
306 * Local variables:
307 * tab-width: 4
308 * c-basic-offset: 4
309 * c-hanging-comment-ender-p: nil
310 * End:
311 */
[904]312?>
Note: See TracBrowser for help on using the browser.