mosbth /
cimage
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 | die("Using trace without a writable logfile. Create the file '$file' and make it writable for the web server."); |
||
|
0 ignored issues
–
show
|
|||
| 20 | } |
||
| 21 | |||
| 22 | $details = ":" . (string) round((microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"]), 6) . "ms"; |
||
| 23 | $details .= ":" . round(memory_get_peak_usage()/1024/1024, 3) . "MB"; |
||
| 24 | $details .= ":" . count(get_included_files()); |
||
| 25 | file_put_contents($file, "$details:$msg\n", FILE_APPEND); |
||
| 26 | } |
||
| 27 | |||
| 28 | |||
| 29 | |||
| 30 | /** |
||
| 31 | * Display error message. |
||
| 32 | * |
||
| 33 | * @param string $msg to display. |
||
| 34 | * @param int $type of HTTP error to display. |
||
| 35 | * |
||
| 36 | * @return void |
||
| 37 | */ |
||
| 38 | function errorPage($msg, $type = 500) |
||
| 39 | { |
||
| 40 | global $mode; |
||
| 41 | |||
| 42 | switch ($type) { |
||
| 43 | case 403: |
||
| 44 | $header = "403 Forbidden"; |
||
| 45 | break; |
||
| 46 | case 404: |
||
| 47 | $header = "404 Not Found"; |
||
| 48 | break; |
||
| 49 | default: |
||
| 50 | $header = "500 Internal Server Error"; |
||
| 51 | } |
||
| 52 | |||
| 53 | if ($mode == "strict") { |
||
| 54 | $header = "404 Not Found"; |
||
| 55 | } |
||
| 56 | |||
| 57 | header("HTTP/1.0 $header"); |
||
| 58 | |||
| 59 | if ($mode == "development") { |
||
| 60 | die("[img.php] $msg"); |
||
| 61 | } |
||
| 62 | |||
| 63 | error_log("[img.php] $msg"); |
||
| 64 | die("HTTP/1.0 $header"); |
||
| 65 | } |
||
| 66 | |||
| 67 | |||
| 68 | |||
| 69 | /** |
||
| 70 | * Custom exception handler. |
||
| 71 | */ |
||
| 72 | set_exception_handler(function ($exception) { |
||
| 73 | errorPage( |
||
| 74 | "<p><b>img.php: Uncaught exception:</b> <p>" |
||
| 75 | . $exception->getMessage() |
||
| 76 | . "</p><pre>" |
||
| 77 | . $exception->getTraceAsString() |
||
| 78 | . "</pre>", |
||
| 79 | 500 |
||
| 80 | ); |
||
| 81 | }); |
||
| 82 | |||
| 83 | |||
| 84 | |||
| 85 | /** |
||
| 86 | * Get input from query string or return default value if not set. |
||
| 87 | * |
||
| 88 | * @param mixed $key as string or array of string values to look for in $_GET. |
||
| 89 | * @param mixed $default value to return when $key is not set in $_GET. |
||
| 90 | * |
||
| 91 | * @return mixed value from $_GET or default value. |
||
| 92 | */ |
||
| 93 | function get($key, $default = null) |
||
| 94 | { |
||
| 95 | if (is_array($key)) { |
||
| 96 | foreach ($key as $val) { |
||
| 97 | if (isset($_GET[$val])) { |
||
| 98 | return $_GET[$val]; |
||
| 99 | } |
||
| 100 | } |
||
| 101 | } elseif (isset($_GET[$key])) { |
||
| 102 | return $_GET[$key]; |
||
| 103 | } |
||
| 104 | return $default; |
||
| 105 | } |
||
| 106 | |||
| 107 | |||
| 108 | |||
| 109 | /** |
||
| 110 | * Get input from query string and set to $defined if defined or else $undefined. |
||
| 111 | * |
||
| 112 | * @param mixed $key as string or array of string values to look for in $_GET. |
||
| 113 | * @param mixed $defined value to return when $key is set in $_GET. |
||
| 114 | * @param mixed $undefined value to return when $key is not set in $_GET. |
||
| 115 | * |
||
| 116 | * @return mixed value as $defined or $undefined. |
||
| 117 | */ |
||
| 118 | function getDefined($key, $defined, $undefined) |
||
| 119 | { |
||
| 120 | return get($key) === null ? $undefined : $defined; |
||
| 121 | } |
||
| 122 | |||
| 123 | |||
| 124 | |||
| 125 | /** |
||
| 126 | * Get value from config array or default if key is not set in config array. |
||
| 127 | * |
||
| 128 | * @param string $key the key in the config array. |
||
| 129 | * @param mixed $default value to be default if $key is not set in config. |
||
| 130 | * |
||
| 131 | * @return mixed value as $config[$key] or $default. |
||
| 132 | */ |
||
| 133 | function getConfig($key, $default) |
||
| 134 | { |
||
| 135 | global $config; |
||
| 136 | return isset($config[$key]) |
||
| 137 | ? $config[$key] |
||
| 138 | : $default; |
||
| 139 | } |
||
| 140 | |||
| 141 | |||
| 142 | |||
| 143 | /** |
||
| 144 | * Log when verbose mode, when used without argument it returns the result. |
||
| 145 | * |
||
| 146 | * @param string $msg to log. |
||
| 147 | * |
||
| 148 | * @return void or array. |
||
| 149 | */ |
||
| 150 | function verbose($msg = null) |
||
| 151 | { |
||
| 152 | global $verbose, $verboseFile; |
||
| 153 | static $log = array(); |
||
| 154 | |||
| 155 | if (!($verbose || $verboseFile)) { |
||
| 156 | return; |
||
| 157 | } |
||
| 158 | |||
| 159 | if (is_null($msg)) { |
||
| 160 | return $log; |
||
| 161 | } |
||
| 162 | |||
| 163 | $log[] = $msg; |
||
| 164 | } |
||
| 165 |
An exit expression should only be used in rare cases. For example, if you write a short command line script.
In most cases however, using an
exitexpression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.