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

functions.php ➔ checkExternalCommand()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 4
nop 3
dl 0
loc 11
rs 9.4285
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 15 and the first side effect is on line 73.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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)
0 ignored issues
show
Coding Style introduced by
trace uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
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;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
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");
0 ignored issues
show
Coding Style Compatibility introduced by
The function errorPage() contains an exit expression.

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 exit expression 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.

Loading history...
62
    }
63
64
    error_log("[img.php] $msg");
65
    die("HTTP/1.0 $header");
0 ignored issues
show
Coding Style Compatibility introduced by
The function errorPage() contains an exit expression.

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 exit expression 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.

Loading history...
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)
0 ignored issues
show
Coding Style introduced by
get uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
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;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
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;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
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
Bug introduced by
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