Passed
Push — master ( 8f5e6a...061d3e )
by Felipe
03:26
created

jpgraph.php ➔ CheckPHPVersion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
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 22 and the first side effect is on line 11.

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
// File:        JPGRAPH.PHP
4
// Description: PHP Graph Plotting library. Base module.
5
// Created:     2001-01-08
6
// Ver:         $Id: jpgraph.php 1924 2010-01-11 14:03:26Z ljp $
7
//
8
// Copyright (c) Asial Corporation. All rights reserved.
9
//========================================================================
10
11
require_once 'jpg-config.inc.php';
12
//require_once('jpgraph_gradient.php'); *
1 ignored issue
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
13
//require_once('jpgraph_errhandler.inc.php');*
14
require_once 'jpgraph_ttf.inc.php';
15
//require_once('jpgraph_rgb.inc.php');*
1 ignored issue
show
Unused Code Comprehensibility introduced by
74% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
16
//require_once('jpgraph_text.inc.php');*
17
//require_once('jpgraph_legend.inc.php');*
18
//require_once('jpgraph_theme.inc.php');*
19
//require_once('gd_image.inc.php');*
20
21
// Line styles
22
define('LINESTYLE_SOLID', 1);
23
define('LINESTYLE_DOTTED', 2);
24
define('LINESTYLE_DASHED', 3);
25
define('LINESTYLE_LONGDASH', 4);
26
27
// The DEFAULT_GFORMAT sets the default graphic encoding format, i.e.
28
// PNG, JPG or GIF depending on what is installed on the target system
29
// in that order.
30
if (!DEFINED("DEFAULT_GFORMAT")) {
31
    define("DEFAULT_GFORMAT", "auto");
32
}
33
34
require_once 'imageSmoothArc.php';
35
36
// Styles for gradient color fill
37
define("GRAD_VER", 1);
38
define("GRAD_VERT", 1);
39
define("GRAD_HOR", 2);
40
define("GRAD_MIDHOR", 3);
41
define("GRAD_MIDVER", 4);
42
define("GRAD_CENTER", 5);
43
define("GRAD_WIDE_MIDVER", 6);
44
define("GRAD_WIDE_MIDHOR", 7);
45
define("GRAD_LEFT_REFLECTION", 8);
46
define("GRAD_RIGHT_REFLECTION", 9);
47
define("GRAD_RAISED_PANEL", 10);
48
define("GRAD_DIAGONAL", 11);
49
DEFINE('_DEFAULT_LPM_SIZE', 8); // Default Legend Plot Mark size
50
51
// Version info
52
define('JPG_VERSION', '3.5.0b1');
53
54
// Minimum required PHP version
55
define('MIN_PHPVERSION', '5.1.0');
56
57
// Special file name to indicate that we only want to calc
58
// the image map in the call to Graph::Stroke() used
59
// internally from the GetHTMLCSIM() method.
60
define('_CSIM_SPECIALFILE', '_csim_special_');
61
62
// HTTP GET argument that is used with image map
63
// to indicate to the script to just generate the image
64
// and not the full CSIM HTML page.
65
define('_CSIM_DISPLAY', '_jpg_csimd');
66
67
// Special filename for Graph::Stroke(). If this filename is given
68
// then the image will NOT be streamed to browser of file. Instead the
69
// Stroke call will return the handler for the created GD image.
70
define('_IMG_HANDLER', '__handle');
71
72
// Special filename for Graph::Stroke(). If this filename is given
73
// the image will be stroked to a file with a name based on the script name.
74
define('_IMG_AUTO', 'auto');
75
76
// Tick density
77
define("TICKD_DENSE", 1);
78
define("TICKD_NORMAL", 2);
79
define("TICKD_SPARSE", 3);
80
define("TICKD_VERYSPARSE", 4);
81
82
// Side for ticks and labels.
83
define("SIDE_LEFT", -1);
84
define("SIDE_RIGHT", 1);
85
define("SIDE_DOWN", -1);
86
define("SIDE_BOTTOM", -1);
87
define("SIDE_UP", 1);
88
define("SIDE_TOP", 1);
89
90
// Legend type stacked vertical or horizontal
91
define("LEGEND_VERT", 0);
92
define("LEGEND_HOR", 1);
93
94
// Mark types for plot marks
95
define("MARK_SQUARE", 1);
96
define("MARK_UTRIANGLE", 2);
97
define("MARK_DTRIANGLE", 3);
98
define("MARK_DIAMOND", 4);
99
define("MARK_CIRCLE", 5);
100
define("MARK_FILLEDCIRCLE", 6);
101
define("MARK_CROSS", 7);
102
define("MARK_STAR", 8);
103
define("MARK_X", 9);
104
define("MARK_LEFTTRIANGLE", 10);
105
define("MARK_RIGHTTRIANGLE", 11);
106
define("MARK_FLASH", 12);
107
define("MARK_IMG", 13);
108
define("MARK_FLAG1", 14);
109
define("MARK_FLAG2", 15);
110
define("MARK_FLAG3", 16);
111
define("MARK_FLAG4", 17);
112
113
// Builtin images
114
define("MARK_IMG_PUSHPIN", 50);
115
define("MARK_IMG_SPUSHPIN", 50);
116
define("MARK_IMG_LPUSHPIN", 51);
117
define("MARK_IMG_DIAMOND", 52);
118
define("MARK_IMG_SQUARE", 53);
119
define("MARK_IMG_STAR", 54);
120
define("MARK_IMG_BALL", 55);
121
define("MARK_IMG_SBALL", 55);
122
define("MARK_IMG_MBALL", 56);
123
define("MARK_IMG_LBALL", 57);
124
define("MARK_IMG_BEVEL", 58);
125
126
// Inline defines
127
define("INLINE_YES", 1);
128
define("INLINE_NO", 0);
129
130
// Format for background images
131
define("BGIMG_FILLPLOT", 1);
132
define("BGIMG_FILLFRAME", 2);
133
define("BGIMG_COPY", 3);
134
define("BGIMG_CENTER", 4);
135
define("BGIMG_FREE", 5);
136
137
// Depth of objects
138
define("DEPTH_BACK", 0);
139
define("DEPTH_FRONT", 1);
140
141
// Direction
142
define("VERTICAL", 1);
143
define("HORIZONTAL", 0);
144
145
// Axis styles for scientific style axis
146
define('AXSTYLE_SIMPLE', 1);
147
define('AXSTYLE_BOXIN', 2);
148
define('AXSTYLE_BOXOUT', 3);
149
define('AXSTYLE_YBOXIN', 4);
150
define('AXSTYLE_YBOXOUT', 5);
151
152
// Style for title backgrounds
153
define('TITLEBKG_STYLE1', 1);
154
define('TITLEBKG_STYLE2', 2);
155
define('TITLEBKG_STYLE3', 3);
156
define('TITLEBKG_FRAME_NONE', 0);
157
define('TITLEBKG_FRAME_FULL', 1);
158
define('TITLEBKG_FRAME_BOTTOM', 2);
159
define('TITLEBKG_FRAME_BEVEL', 3);
160
define('TITLEBKG_FILLSTYLE_HSTRIPED', 1);
161
define('TITLEBKG_FILLSTYLE_VSTRIPED', 2);
162
define('TITLEBKG_FILLSTYLE_SOLID', 3);
163
164
// Styles for axis labels background
165
define('LABELBKG_NONE', 0);
166
define('LABELBKG_XAXIS', 1);
167
define('LABELBKG_YAXIS', 2);
168
define('LABELBKG_XAXISFULL', 3);
169
define('LABELBKG_YAXISFULL', 4);
170
define('LABELBKG_XYFULL', 5);
171
define('LABELBKG_XY', 6);
172
173
// Style for background gradient fills
174
define('BGRAD_FRAME', 1);
175
define('BGRAD_MARGIN', 2);
176
define('BGRAD_PLOT', 3);
177
178
// Width of tab titles
179
define('TABTITLE_WIDTHFIT', 0);
180
define('TABTITLE_WIDTHFULL', -1);
181
182
// Defines for 3D skew directions
183
define('SKEW3D_UP', 0);
184
define('SKEW3D_DOWN', 1);
185
define('SKEW3D_LEFT', 2);
186
define('SKEW3D_RIGHT', 3);
187
188
// For internal use only
189
define("_JPG_DEBUG", false);
190
define("_FORCE_IMGTOFILE", false);
191
define("_FORCE_IMGDIR", '/tmp/jpgimg/');
192
193
//
194
// Automatic settings of path for cache and font directory
195
// if they have not been previously specified
196
//
197
if (USE_CACHE) {
198 View Code Duplication
    if (!defined('CACHE_DIR')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
199
        if (strstr(PHP_OS, 'WIN')) {
200
            if (empty($_SERVER['TEMP'])) {
201
                $t   = new ErrMsgText();
202
                $msg = $t->Get(11, $file, $lineno);
203
                die($msg);
204
            } else {
205
                define('CACHE_DIR', $_SERVER['TEMP'] . '/');
206
            }
207
        } else {
208
            define('CACHE_DIR', '/tmp/jpgraph_cache/');
209
        }
210
    }
211
} elseif (!defined('CACHE_DIR')) {
212
    define('CACHE_DIR', '');
213
}
214
215
//
216
// Setup path for western/latin TTF fonts
217
//
218 View Code Duplication
if (!defined('TTF_DIR')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
219
    if (strstr(PHP_OS, 'WIN')) {
220
        $sroot = getenv('SystemRoot');
221
        if (empty($sroot)) {
222
            $t   = new ErrMsgText();
223
            $msg = $t->Get(12, $file, $lineno);
224
            die($msg);
225
        } else {
226
            define('TTF_DIR', $sroot . '/fonts/');
227
        }
228
    } else {
229
        define('TTF_DIR', '/usr/share/fonts/truetype/');
230
    }
231
}
232
233
//
234
// Setup path for MultiByte TTF fonts (japanese, chinese etc.)
235
//
236 View Code Duplication
if (!defined('MBTTF_DIR')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
237
    if (strstr(PHP_OS, 'WIN')) {
238
        $sroot = getenv('SystemRoot');
239
        if (empty($sroot)) {
240
            $t   = new ErrMsgText();
241
            $msg = $t->Get(12, $file, $lineno);
242
            die($msg);
243
        } else {
244
            define('MBTTF_DIR', $sroot . '/fonts/');
245
        }
246
    } else {
247
        define('MBTTF_DIR', '/usr/share/fonts/truetype/');
248
    }
249
}
250
251
//
252
// Check minimum PHP version
253
//
254
function CheckPHPVersion($aMinVersion)
255
{
256
    return version_compare(PHP_VERSION, $aMinVersion) >= 0;
257
}
258
259
//
260
// Make sure PHP version is high enough
261
//
262
if (!CheckPHPVersion(MIN_PHPVERSION)) {
263
    Amenadiel\JpGraph\Util\JpGraphError::RaiseL(13, PHP_VERSION, MIN_PHPVERSION);
264
    die();
265
}
266
267
//
268
// Make GD sanity check
269
//
270
if (!function_exists("imagetypes") || !function_exists('imagecreatefromstring')) {
271
    Amenadiel\JpGraph\Util\JpGraphError::RaiseL(25001);
272
    //("This PHP installation is not configured with the GD library. Please recompile PHP with GD support to run JpGraph. (Neither function imagetypes() nor imagecreatefromstring() does exist)");
273
}
274
275
//
276
// Setup PHP error handler
277
//
278
function _phpErrorHandler($errno, $errmsg, $filename, $linenum, $vars)
0 ignored issues
show
Unused Code introduced by
The parameter $vars is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
279
{
280
    // Respect current error level
281
    if ($errno & error_reporting()) {
282
        Amenadiel\JpGraph\Util\JpGraphError::RaiseL(25003, basename($filename), $linenum, $errmsg);
283
    }
284
}
285
286
if (INSTALL_PHP_ERR_HANDLER) {
287
    set_error_handler("_phpErrorHandler");
288
}
289
290
//
291
// Check if there were any warnings, perhaps some wrong includes by the user. In this
292
// case we raise it immediately since otherwise the image will not show and makes
293
// debugging difficult. This is controlled by the user setting CATCH_PHPERRMSG
294
//
295
if (isset($GLOBALS['php_errormsg']) && CATCH_PHPERRMSG && !preg_match('/|Deprecated|/i', $GLOBALS['php_errormsg'])) {
296
    Amenadiel\JpGraph\Util\JpGraphError::RaiseL(25004, $GLOBALS['php_errormsg']);
297
}
298
299
// Useful mathematical function
300
function sign($a)
301
{
302
    return $a >= 0 ? 1 : -1;
303
}
304
305
//
306
// Utility function to generate an image name based on the filename we
307
// are running from and assuming we use auto detection of graphic format
308
// (top level), i.e it is safe to call this function
309
// from a script that uses JpGraph
310
//
311
function GenImgName()
312
{
313
    // Determine what format we should use when we save the images
314
    $supported = imagetypes();
315
    if ($supported & IMG_PNG) {
316
        $img_format = "png";
317
    } elseif ($supported & IMG_GIF) {
318
        $img_format = "gif";
319
    } elseif ($supported & IMG_JPG) {
320
        $img_format = "jpeg";
321
    } elseif ($supported & IMG_WBMP) {
322
        $img_format = "wbmp";
323
    } elseif ($supported & IMG_XPM) {
324
        $img_format = "xpm";
325
    }
326
327
    if (!isset($_SERVER['PHP_SELF'])) {
328
        Amenadiel\JpGraph\Util\JpGraphError::RaiseL(25005);
329
        //(" Can't access PHP_SELF, PHP global variable. You can't run PHP from command line if you want to use the 'auto' naming of cache or image files.");
330
    }
331
    $fname = basename($_SERVER['PHP_SELF']);
332
    if (!empty($_SERVER['QUERY_STRING'])) {
333
        $q = @$_SERVER['QUERY_STRING'];
334
        $fname .= '_' . preg_replace("/\W/", "_", $q) . '.' . $img_format;
0 ignored issues
show
Bug introduced by
The variable $img_format does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
335
    } else {
336
        $fname = substr($fname, 0, strlen($fname) - 4) . '.' . $img_format;
337
    }
338
    return $fname;
339
}
340
341
global $gDateLocale;
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...
342
// Global object handlers
343
$gDateLocale    = new Amenadiel\JpGraph\Util\DateLocale();
344
$gJpgDateLocale = new Amenadiel\JpGraph\Util\DateLocale();
345
346
// <EOF>
347