<?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);
}