| 1 | <?php |
|---|
| 2 | error_reporting(E_ALL); |
|---|
| 3 | require_once BASEPATH.'config.php'; |
|---|
| 4 | require_once BASEPATH.'classes/AbstractDb.php'; |
|---|
| 5 | require_once BASEPATH.'classes/Session.php'; |
|---|
| 6 | require_once BASEPATH.'include/schema_validate.php'; |
|---|
| 7 | global $db; |
|---|
| 8 | $db = new AbstractDb(); |
|---|
| 9 | validate_schema(); |
|---|
| 10 | |
|---|
| 11 | /* NEVER edit these, as they mush match the C code of the gateway */ |
|---|
| 12 | define('ACCOUNT_STATUS_ERROR', -1); |
|---|
| 13 | define('ACCOUNT_STATUS_DENIED', 0); |
|---|
| 14 | define('ACCOUNT_STATUS_ALLOWED', 1); |
|---|
| 15 | define('ACCOUNT_STATUS_VALIDATION', 5); |
|---|
| 16 | define('ACCOUNT_STATUS_VALIDATION_FAILED', 6); |
|---|
| 17 | define('ACCOUNT_STATUS_LOCKED', 254); |
|---|
| 18 | |
|---|
| 19 | $account_status_to_text[ACCOUNT_STATUS_ERROR] = "Error"; |
|---|
| 20 | $account_status_to_text[ACCOUNT_STATUS_DENIED] = "Denied"; |
|---|
| 21 | $account_status_to_text[ACCOUNT_STATUS_ALLOWED] = "Allowed"; |
|---|
| 22 | $account_status_to_text[ACCOUNT_STATUS_VALIDATION] = "Validation"; |
|---|
| 23 | $account_status_to_text[ACCOUNT_STATUS_VALIDATION_FAILED] = "Validation Failed"; |
|---|
| 24 | $account_status_to_text[ACCOUNT_STATUS_LOCKED] = "Locked"; |
|---|
| 25 | |
|---|
| 26 | define('TOKEN_UNUSED', 'UNUSED'); |
|---|
| 27 | define('TOKEN_INUSE', 'INUSE'); |
|---|
| 28 | define('TOKEN_USED', 'USED'); |
|---|
| 29 | |
|---|
| 30 | $token_to_text[TOKEN_UNUSED] = "Unused"; |
|---|
| 31 | $token_to_text[TOKEN_INUSE] = "In use"; |
|---|
| 32 | $token_to_text[TOKEN_USED] = "Used"; |
|---|
| 33 | |
|---|
| 34 | define('STAGE_LOGIN', "login"); |
|---|
| 35 | define('STAGE_LOGOUT', "logout"); |
|---|
| 36 | define('STAGE_COUNTERS',"counters"); |
|---|
| 37 | |
|---|
| 38 | define('ONLINE_STATUS_ONLINE', 1); |
|---|
| 39 | define('ONLINE_STATUS_OFFLINE', 2); |
|---|
| 40 | |
|---|
| 41 | /* This section deals with sessions */ |
|---|
| 42 | |
|---|
| 43 | define('SESS_USERNAME_VAR', 'SESS_USERNAME'); |
|---|
| 44 | define('SESS_USER_ID_VAR', 'SESS_USER_ID'); |
|---|
| 45 | define('SESS_PASSWORD_HASH_VAR', 'SESS_PASSWORD_HASH'); |
|---|
| 46 | define('SESS_ORIGINAL_URL_VAR', 'SESS_ORIGINAL_URL'); |
|---|
| 47 | define('SESS_LANGUAGE_VAR', 'SESS_LANGUAGE'); |
|---|
| 48 | define('SESS_GW_ADDRESS_VAR', 'SESS_GW_ADDRESS'); |
|---|
| 49 | define('SESS_GW_PORT_VAR', 'SESS_GW_PORT'); |
|---|
| 50 | define('SESS_GW_ID_VAR', 'SESS_GW_ID'); |
|---|
| 51 | |
|---|
| 52 | /* End */ |
|---|
| 53 | |
|---|
| 54 | /* This section deals with PATHs */ |
|---|
| 55 | define('BASE_NON_SSL_PATH', 'http://' . $_SERVER['SERVER_NAME'] . SYSTEM_PATH); |
|---|
| 56 | |
|---|
| 57 | //echo "<pre>";print_r($_SERVER);echo "</pre>"; |
|---|
| 58 | |
|---|
| 59 | $curent_url='http'; |
|---|
| 60 | if($_SERVER['SERVER_PORT']=='443'){$curent_url.='s';} |
|---|
| 61 | $curent_url.= '://'.$_SERVER['HTTP_HOST']; |
|---|
| 62 | if($_SERVER['SERVER_PORT']!=80 && $_SERVER['SERVER_PORT']!=443) $curent_url.=':'.$_SERVER['SERVER_PORT']; |
|---|
| 63 | $curent_url.=$_SERVER['REQUEST_URI']; |
|---|
| 64 | define('CURRENT_REQUEST_URL', $curent_url); |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | if(SSL_AVAILABLE) |
|---|
| 69 | { |
|---|
| 70 | define('BASE_SSL_PATH', 'https://' . $_SERVER['SERVER_NAME'] . SYSTEM_PATH); |
|---|
| 71 | } |
|---|
| 72 | else |
|---|
| 73 | { |
|---|
| 74 | define('BASE_SSL_PATH', BASE_NON_SSL_PATH); |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | /* If we actually ARE in SSL mode, make all URLS http:// to avoid security warnings. */ |
|---|
| 78 | if(isset($_SERVER['HTTPS'])) |
|---|
| 79 | { |
|---|
| 80 | define('BASE_URL_PATH', BASE_SSL_PATH); |
|---|
| 81 | } |
|---|
| 82 | else |
|---|
| 83 | { |
|---|
| 84 | define('BASE_URL_PATH', BASE_NON_SSL_PATH); |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | if(empty($_REQUEST['gw_id'])) |
|---|
| 88 | { |
|---|
| 89 | define('CURRENT_NODE_ID', DEFAULT_NODE_ID); |
|---|
| 90 | } |
|---|
| 91 | else |
|---|
| 92 | { |
|---|
| 93 | define('CURRENT_NODE_ID', trim($_REQUEST['gw_id'])); |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | define('DEFAULT_CONTENT_URL', BASE_URL_PATH.LOCAL_CONTENT_REL_PATH.DEFAULT_NODE_ID.'/'); |
|---|
| 97 | define('DEFAULT_CONTENT_PHP_RELATIVE_PATH', BASEPATH.LOCAL_CONTENT_REL_PATH.DEFAULT_NODE_ID.'/'); |
|---|
| 98 | |
|---|
| 99 | define('NODE_CONTENT_URL', BASE_URL_PATH.LOCAL_CONTENT_REL_PATH.CURRENT_NODE_ID.'/'); |
|---|
| 100 | define('NODE_CONTENT_PHP_RELATIVE_PATH', BASEPATH.LOCAL_CONTENT_REL_PATH.CURRENT_NODE_ID.'/'); |
|---|
| 101 | |
|---|
| 102 | define('COMMON_CONTENT_URL', BASE_URL_PATH.LOCAL_CONTENT_REL_PATH.'common/'); |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | define('GENERIC_OBJECT_ADMIN_ABS_HREF', BASE_URL_PATH.'/admin/generic_object_admin.php'); |
|---|
| 106 | define('CONTENT_ADMIN_ABS_HREF', BASE_URL_PATH.'/admin/content_admin.php'); |
|---|
| 107 | |
|---|
| 108 | /** Convert a password hash form a NoCat passwd file into the same format as get_password_hash(). |
|---|
| 109 | * @return The 32 character hash. |
|---|
| 110 | */ |
|---|
| 111 | function convert_nocat_password_hash($hash) |
|---|
| 112 | { |
|---|
| 113 | return $hash . '=='; |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | function iso8601_date($unix_timestamp) { |
|---|
| 117 | $tzd = date('O',$unix_timestamp); |
|---|
| 118 | $tzd = substr(chunk_split($tzd, 3, ':'),0,6); |
|---|
| 119 | $date = date('Y-m-d\TH:i:s', $unix_timestamp) . $tzd; |
|---|
| 120 | return $date; |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | /** Cleanup dangling tokens and connections from the database, left if a gateway crashed, etc. */ |
|---|
| 124 | function garbage_collect() |
|---|
| 125 | { |
|---|
| 126 | global $db; |
|---|
| 127 | |
|---|
| 128 | // 10 minutes |
|---|
| 129 | $expiration = time() - 60*10; |
|---|
| 130 | $expiration=iso8601_date($expiration); |
|---|
| 131 | $db -> ExecSqlUpdate ("UPDATE connections SET token_status='" . TOKEN_USED . "' WHERE last_updated < '$expiration' AND token_status = '".TOKEN_INUSE."'", false); |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | /** Get the url from the local content_specific folder if the file exists, and from the default content folder otherwise */ |
|---|
| 135 | function find_local_content_url($filename) |
|---|
| 136 | { |
|---|
| 137 | //echo "find_local_content_url(): Looking for: ".NODE_CONTENT_PHP_RELATIVE_PATH.$filename."<br>\n"; |
|---|
| 138 | if (is_file(NODE_CONTENT_PHP_RELATIVE_PATH.$filename)) |
|---|
| 139 | { |
|---|
| 140 | $retval = NODE_CONTENT_URL.$filename; |
|---|
| 141 | } |
|---|
| 142 | else |
|---|
| 143 | { |
|---|
| 144 | $retval = DEFAULT_CONTENT_URL.$filename; |
|---|
| 145 | } |
|---|
| 146 | //echo "find_local_content_url(): Returned: $retval<br>\n"; |
|---|
| 147 | return $retval; |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | /** Return a 32 byte guid valid for database use */ |
|---|
| 151 | function get_guid() |
|---|
| 152 | { |
|---|
| 153 | return md5(uniqid(rand(), true)); |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | /** like the php function print_r(), but the way it was meant to be... */ |
|---|
| 157 | function pretty_print_r($param) |
|---|
| 158 | { |
|---|
| 159 | echo "\n<pre>\n"; |
|---|
| 160 | print_r($param); |
|---|
| 161 | echo "\n</pre>\n"; |
|---|
| 162 | } |
|---|
| 163 | ?> |
|---|