[go: up one dir, main page]

Menu

[r3]: / global.php  Maximize  Restore  History

Download this file

223 lines (190 with data), 5.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
/*
global.php - ViaVoip
Copyright (C) 2015 Attilio Pavone <tilly@utillyty.eu>
This file is part of ViaVoip.
ViaVoip is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ViaVoip is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with ViaVoip. If not, see <http://www.gnu.org/licenses/>.
*/
// tilly 03/04/2015
const VVV_ERR_INFO = 0;
const VVV_ERR_NOTICE = 1;
const VVV_ERR_WARNING = 2;
const VVV_ERR_CRITICAL = 3;
include("version.php");
include("config.php");
$_vvv_db = NULL;
$_vvv_errors = array();
$_vvv_version = $_REQUEST['VVV_VERSION'];
$_app_name = $_REQUEST['APP_NAME'];
$_app_version = $_REQUEST['APP_VERSION'];
$_os_name = $_REQUEST['OS_NAME'];
set_error_handler("vvv_error_handler", -1); // -1 include E_ALL | E_STRICT, and future possible values!
date_default_timezone_set(VVV_TIMEZONE);
if (VVV_DEBUG) {
ini_set('display_startup_errors', TRUE);
ini_set('display_errors', TRUE); // if inside error_handler desperately write on output
error_reporting(-1); // -1 include E_ALL | E_STRICT, and future possible values!
} else {
ini_set('display_errors', FALSE);
error_reporting(E_ALL);
}
function vvv_error_handler($errno, $errstr, $errfile, $errline)
{
global $_vvv_errors;
if ($errno & error_reporting())
$_vvv_errors[] = "php error($errno) $errstr - file:'$errfile', line:$errline";
//echo "php error($errno) $errstr - file:'$errfile', line:$errline";
return TRUE;
}
// level = 'I','N','W','C'
function vvv_log($level, $message)
{
global $_vvv_version, $_app_name, $_app_version, $_os_name;
$filename = VVV_LOG_PATH."/".date('Ymd');
$log = vvv_err_level_str($level)
. " [".date("Y-m-d H:i:s")."]"
. " ".str_pad($_SERVER['REMOTE_ADDR'], 15)." " // 255.255.255.255
. " ".$_vvv_version
. " ".$_app_name
. " ".str_pad($_app_version, 6) // 1.0.11
. " ".str_pad($_os_name, 10) // blackbarry
. $message
. "\n";
error_log($log, 3, $filename);
}
function vvv_err_level_str($level)
{
switch ($level) {
case VVV_ERR_INFO: return "I";
case VVV_ERR_NOTICE: return "N";
case VVV_ERR_WARNING: return "W";
case VVV_ERR_CRITICAL: return "C";
}
return "?";
}
function vvv_db_open()
{
global $_vvv_db;
$_vvv_db = @mysqli_connect(VVV_DB_HOST, VVV_DB_USER, VVV_DB_PASS, VVV_DB_NAME);
if (!$_vvv_db) {
vvv_response_end(VVV_ERR_CRITICAL
, "mysqli_connect returned null", TRUE);
}
if ($_vvv_db->connect_error) {
vvv_response_end(VVV_ERR_CRITICAL
, "mysqli.connect_error(".mysqli_connect_errno().") ".mysqli_connect_error()
, TRUE);
}
}
// write header line: VVV X(version)
// no start marker
function vvv_response_begin()
{
header("Content-type:text/plain;charset=utf-8");
echo "VVV ".VVV_VERSION;
}
// write:
// - body terminator (empty line with no start marker)
// - response result level and message lines (headers like: no start marker)
// - trailing php errors (likewise: no start marker)
// - buffer terminator (another final empty line with no start marker)
function vvv_response_end($level, $message, $log=FALSE)
{
global $_vvv_errors;
if ($log)
vvv_log($level, $message);
// body terminator + result level + result message
echo "\n"
, "\n", $level
, "\n", "vvv-server: ".vvv_response_escape($message);
// trailing cached errors
foreach($_vvv_errors as $errstr) {
echo "\n", "vvv-server: ".vvv_response_escape($errstr);
}
// response terminator
echo "\n\n";
exit();
}
// writes a body line with start marker
// (to be distinguisced by end of body/end of buffer lines!)
function vvv_response_write($value)
{
echo "\n>", vvv_response_escape($value);
}
// replaces all '\r\n' | '\n' with '\r'
// '\n' is the PROTO_NEW_LINE marker
function vvv_response_escape($value)
{
return preg_replace("/\r\n|\n/", "\r", $value);
}
// 1.4 -------------------------------------------------------------------------
function vvv_db_time($time=0)
{
if (!$time) $time = time();
return date('Y-m-d H-i-s', $time);
}
function vvv_db_query($sql)
{
global $_vvv_db;
$result = mysqli_query($_vvv_db, $sql);
if ($result === FALSE) {
vvv_db_log($sql, "E");
vvv_response_end(VVV_ERR_CRITICAL
, "mysqli.error(".mysqli_errno($_vvv_db).") ".mysqli_error($_vvv_db)
, TRUE);
}
if (VVV_DEBUG) vvv_db_log($sql, vvv_db_count($result));
return $result;
}
function vvv_db_exec($sql)
{
global $_vvv_db;
$result = mysqli_query($_vvv_db, $sql);
$count = -1;
if ($result === FALSE) {
vvv_db_log($sql, "E");
vvv_response_end(VVV_ERR_CRITICAL
, "mysqli.error(".mysqli_errno($_vvv_db).") ".mysqli_error($_vvv_db)
, TRUE);
} else {
$count = mysqli_affected_rows($_vvv_db);
}
if (VVV_DEBUG) vvv_db_log($sql, $count);
return $count;
}
function vvv_db_count($result)
{
return $result->num_rows;
}
function vvv_db_fetch($result)
{
return mysqli_fetch_assoc($result);
}
function vvv_db_escape($text)
{
global $_vvv_db;
return $_vvv_db->real_escape_string($text);
}
function vvv_db_insert_id()
{
global $_vvv_db;
return $_vvv_db->insert_id;
}
function vvv_db_log($sql, $count)
{
$filename = VVV_LOG_PATH."/dblog-".date('Ymd');
$log = vvv_db_time()
. " " . $count
. " " . $sql
. "\n";
error_log($log, 3, $filename);
}