summaryrefslogtreecommitdiffstats
path: root/lib/functions.php
blob: eb4d093a47502e413a5b66c7212c5fb7548332a8 (plain)
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
<?php
/*
 +--------------------------------------------------------------------------+
 | This file is part of the Kolab Web Admin Panel                           |
 |                                                                          |
 | Copyright (C) 2011-2012, Kolab Systems AG                                |
 |                                                                          |
 | This program is free software: you can redistribute it and/or modify     |
 | it under the terms of the GNU Affero General Public License as published |
 | by the Free Software Foundation, either version 3 of the License, or     |
 | (at your option) any later version.                                      |
 |                                                                          |
 | This program 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 Affero General Public License for more details.                      |
 |                                                                          |
 | You should have received a copy of the GNU Affero General Public License |
 | along with this program. If not, see <http://www.gnu.org/licenses/>      |
 +--------------------------------------------------------------------------+
 | Author: Aleksander Machniak <machniak@kolabsys.com>                      |
 | Author: Jeroen van Meeuwen <vanmeeuwen@kolabsys.com>                     |
 +--------------------------------------------------------------------------+
*/

// Initialization and basic functions

// application constants
define('KADM_START', microtime(true));
define('KADM_VERSION', '0.1');
define('KADM_CHARSET', 'utf-8');
define('INSTALL_PATH', dirname(__FILE__));

// Check critical PHP settings here.
$crit_opts = array(
    'mbstring.func_overload' => 0,
    'magic_quotes_runtime' => 0,
);

foreach ($crit_opts as $optname => $optval) {
    if ($optval != ini_get($optname)) {
        die("ERROR: Wrong '$optname' option value!");
    }
}

$include_path = INSTALL_PATH . PATH_SEPARATOR;
$include_path .= INSTALL_PATH . '/client' . PATH_SEPARATOR;
$include_path .= INSTALL_PATH . '/api' . PATH_SEPARATOR;
$include_path .= INSTALL_PATH . '/ext' . PATH_SEPARATOR;
$include_path .= ini_get('include_path');

if (set_include_path($include_path) === false) {
    die("Fatal error: ini_set/set_include_path does not work.");
}

ini_set('error_reporting', E_ALL &~ E_NOTICE &~ E_STRICT);
ini_set('error_log', INSTALL_PATH . '/../logs/errors');

// Set internal charset
mb_internal_encoding(KADM_CHARSET);
@mb_regex_encoding(KADM_CHARSET);

// include composer autoloader (if available)
if (@file_exists(INSTALL_PATH . '/../vendor/autoload.php')) {
    require INSTALL_PATH . '/../vendor/autoload.php';
}

// register autoloader
function class_autoloader($classname)
{
    $classname = preg_replace('/(Net|HTTP|SQL)_(.+)/', "\\1/\\2", $classname);

    if ($fp = @fopen("$classname.php", 'r', true)) {
        include_once "$classname.php";
        fclose($fp);
        return true;
    }

    return false;
}

spl_autoload_register('class_autoloader');

/**
 * Prints debug info into the 'console' log
 */
function console()
{
    $args = func_get_args();

    $msg = array();
    foreach ($args as $arg) {
        $msg[] = !is_string($arg) ? var_export($arg, true) : $arg;
    }

    write_log('console', join(";\n", $msg));
}

/**
 * Appends a line to a log file in the logs directory.
 * Date will be added automatically to the line.
 *
 * @param string $name  Name of the log file
 * @param mixed  $line  Line to append
 */
function write_log($name, $line)
{
    if (!is_string($line)) {
        $line = var_export($line, true);
    }

    $log_dir = dirname(__FILE__) . '/../logs';
    $logfile = $log_dir . '/' . $name;
    $date    = date('d-M-Y H:i:s O');
    $sess_id = session_id();
    $logline = sprintf("[%s]%s: %s\n", $date, $sess_id ? "($sess_id)" : '', $line);

    if ($fp = @fopen($logfile, 'a')) {
        fwrite($fp, $logline);
        fflush($fp);
        fclose($fp);
        return;
    }

    if ($name == 'errors') {
        // send error to PHPs error handler if write to file didn't succeed
        trigger_error($line, E_USER_ERROR);
    }
}

function timer($time = null, $label = '')
{
    $now = microtime(true);
    if ($time) {
        console(($label ? $label.' ' : '') . sprintf('%.4f', $now - $time));
    }
    return $now;
}