|
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
|
|
|
* Get input from query string or return default value if not set. |
|
72
|
|
|
* |
|
73
|
|
|
* @param mixed $key as string or array of string values to look for in $_GET. |
|
74
|
|
|
* @param mixed $default value to return when $key is not set in $_GET. |
|
75
|
|
|
* |
|
76
|
|
|
* @return mixed value from $_GET or default value. |
|
77
|
|
|
*/ |
|
78
|
|
|
function get($key, $default = null) |
|
79
|
|
|
{ |
|
80
|
|
|
if (is_array($key)) { |
|
81
|
|
|
foreach ($key as $val) { |
|
82
|
|
|
if (isset($_GET[$val])) { |
|
83
|
|
|
return $_GET[$val]; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
} elseif (isset($_GET[$key])) { |
|
87
|
|
|
return $_GET[$key]; |
|
88
|
|
|
} |
|
89
|
|
|
return $default; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Get input from query string and set to $defined if defined or else $undefined. |
|
96
|
|
|
* |
|
97
|
|
|
* @param mixed $key as string or array of string values to look for in $_GET. |
|
98
|
|
|
* @param mixed $defined value to return when $key is set in $_GET. |
|
99
|
|
|
* @param mixed $undefined value to return when $key is not set in $_GET. |
|
100
|
|
|
* |
|
101
|
|
|
* @return mixed value as $defined or $undefined. |
|
102
|
|
|
*/ |
|
103
|
|
|
function getDefined($key, $defined, $undefined) |
|
104
|
|
|
{ |
|
105
|
|
|
return get($key) === null ? $undefined : $defined; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Get value of input from query string or else $undefined. |
|
112
|
|
|
* |
|
113
|
|
|
* @param mixed $key as string or array of string values to look for in $_GET. |
|
114
|
|
|
* @param mixed $undefined value to return when $key has no, or empty value in $_GET. |
|
115
|
|
|
* |
|
116
|
|
|
* @return mixed value as or $undefined. |
|
117
|
|
|
*/ |
|
118
|
|
|
function getValue($key, $undefined) |
|
119
|
|
|
{ |
|
120
|
|
|
$val = get($key); |
|
121
|
|
|
if (is_null($val) || $val === "") { |
|
122
|
|
|
return $undefined; |
|
123
|
|
|
} |
|
124
|
|
|
return $val; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
|
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Get value from config array or default if key is not set in config array. |
|
131
|
|
|
* |
|
132
|
|
|
* @param string $key the key in the config array. |
|
133
|
|
|
* @param mixed $default value to be default if $key is not set in config. |
|
134
|
|
|
* |
|
135
|
|
|
* @return mixed value as $config[$key] or $default. |
|
136
|
|
|
*/ |
|
137
|
|
|
function getConfig($key, $default) |
|
138
|
|
|
{ |
|
139
|
|
|
global $config; |
|
140
|
|
|
return isset($config[$key]) |
|
141
|
|
|
? $config[$key] |
|
142
|
|
|
: $default; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
|
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Log when verbose mode, when used without argument it returns the result. |
|
149
|
|
|
* |
|
150
|
|
|
* @param string $msg to log. |
|
151
|
|
|
* |
|
152
|
|
|
* @return void or array. |
|
153
|
|
|
*/ |
|
154
|
|
|
function verbose($msg = null, $arg = "") |
|
155
|
|
|
{ |
|
156
|
|
|
global $verbose, $verboseFile; |
|
157
|
|
|
static $log = array(); |
|
158
|
|
|
|
|
159
|
|
|
if (!($verbose || $verboseFile)) { |
|
160
|
|
|
return; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
if (is_null($msg)) { |
|
164
|
|
|
return $log; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
if (is_null($arg)) { |
|
168
|
|
|
$arg = "null"; |
|
169
|
|
|
} elseif ($arg === false) { |
|
170
|
|
|
$arg = "false"; |
|
171
|
|
|
} elseif ($arg === true) { |
|
172
|
|
|
$arg = "true"; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
$log[] = $msg . $arg; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
|
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Log when verbose mode, when used without argument it returns the result. |
|
182
|
|
|
* |
|
183
|
|
|
* @param string $msg to log. |
|
|
|
|
|
|
184
|
|
|
* |
|
185
|
|
|
* @return void or array. |
|
186
|
|
|
*/ |
|
187
|
|
|
function checkExternalCommand($what, $enabled, $commandString) |
|
188
|
|
|
{ |
|
189
|
|
|
$no = $enabled ? null : 'NOT'; |
|
190
|
|
|
$text = "Post processing $what is $no enabled.<br>"; |
|
191
|
|
|
|
|
192
|
|
|
list($command) = explode(" ", $commandString); |
|
193
|
|
|
$no = is_executable($command) ? null : 'NOT'; |
|
194
|
|
|
$text .= "The command for $what is $no an executable.<br>"; |
|
195
|
|
|
|
|
196
|
|
|
return $text; |
|
197
|
|
|
} |
|
198
|
|
|
|
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
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.