Completed
Push — master ( 9f6cba...c5de59 )
by Mikael
05:05 queued 02:06
created

functions.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * General functions to use in img.php.
4
 */
5
6
7
8
/**
9
 * Trace and log execution to logfile, useful for debugging and development.
10
 *
11
 * @param string $msg message to log to file.
12
 *
13
 * @return void
14
 */
15
function trace($msg)
16
{
17
    $file = CIMAGE_DEBUG_FILE;
18
    if (!is_writable($file)) {
19
        return;
20
    }
21
22
    $timer = number_format((microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"]), 6);
23
    $details  = "{$timer}ms";
24
    $details .= ":" . round(memory_get_peak_usage()/1024/1024, 3) . "MB";
25
    $details .= ":" . count(get_included_files());
26
    file_put_contents($file, "$details:$msg\n", FILE_APPEND);
27
}
28
29
30
31
/**
32
 * Display error message.
33
 *
34
 * @param string $msg to display.
35
 * @param int $type of HTTP error to display.
36
 *
37
 * @return void
38
 */
39
function errorPage($msg, $type = 500)
40
{
41
    global $mode;
42
43
    switch ($type) {
44
        case 403:
45
            $header = "403 Forbidden";
46
            break;
47
        case 404:
48
            $header = "404 Not Found";
49
            break;
50
        default:
51
            $header = "500 Internal Server Error";
52
    }
53
54
    if ($mode == "strict") {
55
        $header = "404 Not Found";
56
    }
57
58
    header("HTTP/1.0 $header");
59
60
    if ($mode == "development") {
61
        die("[img.php] $msg");
62
    }
63
64
    error_log("[img.php] $msg");
65
    die("HTTP/1.0 $header");
66
}
67
68
69
70
/**
71
 * Custom exception handler.
72
 */
73
set_exception_handler(function ($exception) {
74
    errorPage(
75
        "<p><b>img.php: Uncaught exception:</b> <p>"
76
        . $exception->getMessage()
77
        . "</p><pre>"
78
        . $exception->getTraceAsString()
79
        . "</pre>",
80
        500
81
    );
82
});
83
84
85
86
/**
87
 * Get input from query string or return default value if not set.
88
 *
89
 * @param mixed $key     as string or array of string values to look for in $_GET.
90
 * @param mixed $default value to return when $key is not set in $_GET.
91
 *
92
 * @return mixed value from $_GET or default value.
93
 */
94
function get($key, $default = null)
95
{
96
    if (is_array($key)) {
97
        foreach ($key as $val) {
98
            if (isset($_GET[$val])) {
99
                return $_GET[$val];
100
            }
101
        }
102
    } elseif (isset($_GET[$key])) {
103
        return $_GET[$key];
104
    }
105
    return $default;
106
}
107
108
109
110
/**
111
 * Get input from query string and set to $defined if defined or else $undefined.
112
 *
113
 * @param mixed $key       as string or array of string values to look for in $_GET.
114
 * @param mixed $defined   value to return when $key is set in $_GET.
115
 * @param mixed $undefined value to return when $key is not set in $_GET.
116
 *
117
 * @return mixed value as $defined or $undefined.
118
 */
119
function getDefined($key, $defined, $undefined)
120
{
121
    return get($key) === null ? $undefined : $defined;
122
}
123
124
125
126
/**
127
 * Get value from config array or default if key is not set in config array.
128
 *
129
 * @param string $key    the key in the config array.
130
 * @param mixed $default value to be default if $key is not set in config.
131
 *
132
 * @return mixed value as $config[$key] or $default.
133
 */
134
function getConfig($key, $default)
135
{
136
    global $config;
137
    return isset($config[$key])
138
        ? $config[$key]
139
        : $default;
140
}
141
142
143
144
/**
145
 * Log when verbose mode, when used without argument it returns the result.
146
 *
147
 * @param string $msg to log.
148
 *
149
 * @return void or array.
150
 */
151
function verbose($msg = null)
152
{
153
    global $verbose, $verboseFile;
154
    static $log = array();
155
156
    if (!($verbose || $verboseFile)) {
157
        return;
158
    }
159
160
    if (is_null($msg)) {
161
        return $log;
162
    }
163
164
    $log[] = $msg;
165
}
166
167
168
169
/**
170
 * Log when verbose mode, when used without argument it returns the result.
171
 *
172
 * @param string $msg to log.
0 ignored issues
show
There is no parameter named $msg. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
173
 *
174
 * @return void or array.
175
 */
176
function checkExternalCommand($what, $enabled, $commandString)
177
{
178
    $no = $enabled ? null : 'NOT';
179
    $text = "Post processing $what is $no enabled.<br>";
180
181
    list($command) = explode(" ", $commandString);
182
    $no = is_executable($command) ? null : 'NOT';
183
    $text .= "The command for $what is $no an executable.<br>";
184
185
    return $text;
186
}
187