Issues (7)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/Countries.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
namespace DigitalCanvas\Options;
3
4
use RuntimeException;
5
6
class Countries
7
{
8
    /**
9
     * @var string|null
10
     */
11
    private static $file;
12
13
    /**
14
     * @var array|null
15
     */
16
    private static $countries;
17
18
    /**
19
     * Sets the xml file to use to load countries
20
     *
21
     * @param string|null $file
22
     *
23
     * @throws RuntimeException
24
     */
25 3
    public static function setXML($file = null)
26
    {
27 3
        if (is_null($file)) {
28 1
            $file = __DIR__ . '/countries.xml';
29
        }
30 3
        if (!is_file($file)) {
31 1
            throw new RuntimeException('Country XML file does not exist.');
32
        }
33 2
        self::$file = realpath($file);
34 2
        self::$countries = null;
35 2
    }
36
37
    /**
38
     * Loads the countries from the xml file.
39
     */
40 7
    private static function loadCountries()
41
    {
42 7
        if (is_null(self::$file)) {
43 1
            self::setXML();
44
        }
45
        // Load xml file
46 7
        $xml = simplexml_load_file(self::$file);
47 7
        $countries = array();
48
        // loop through states
49 7
        foreach ($xml as $country) {
50 7
            $countries[(string) $country->code] = array(
51 7
                'code' => (string) $country->code,
52 7
                'name' => (string) $country->name
53
            );
54
        }
55
        // Cache states
56 7
        self::$countries = $countries;
57
        // Clear xml instance
58 7
        unset($xml, $countries, $country);
59 7
    }
60
61
    /**
62
     * Clears the data from the cache.
63
     */
64 8
    public static function clearCache()
65
    {
66 8
        self::$countries = null;
67 8
    }
68
69
    /**
70
     * Checks if data has been cached
71
     *
72
     * @return boolean
73
     */
74 1
    public static function isCached()
75
    {
76 1
        return !is_null(self::$countries);
77
    }
78
79
    /**
80
     * Returns array of countries
81
     *
82
     * @return array
83
     */
84 1
    public static function getArray()
85
    {
86
        // Load States if they are not yet loaded.
87 1
        if (is_null(self::$countries)) {
88 1
            self::loadCountries();
89
        }
90 1
        return self::$countries;
91
    }
92
93
    /**
94
     * Returns countries as abbreviation=>name pair array
95
     *
96
     * @return array
97
     */
98 3
    public static function getPairs()
99
    {
100
        // Load States if they are not yet loaded.
101 3
        if (is_null(self::$countries)) {
102 3
            self::loadCountries();
103
        }
104 3
        $countries = array();
105 3
        foreach (self::$countries as $country) {
0 ignored issues
show
The expression self::$countries of type null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
106 3
            $countries[$country['code']] = $country['name'];
107
        }
108 3
        return $countries;
109
    }
110
111
    /**
112
     * Returns a single country by abbreviation
113
     *
114
     * @param string $country
115
     * @param bool $name_only
116
     *
117
     * @return string|array|null
118
     */
119 3
    public static function getCountry($country, $name_only = true)
120
    {
121
        // Load States if they are not yet loaded.
122 3
        if (is_null(self::$countries)) {
123 3
            self::loadCountries();
124
        }
125 3
        if (array_key_exists($country, self::$countries)) {
126 2
            return ($name_only) ? self::$countries[$country]['name'] : self::$countries[$country];
127
        } else {
128 1
            return null;
129
        }
130
    }
131
}
132