Issues (165)

Security Analysis    not enabled

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/View/CViewContainer.php (4 issues)

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
3
namespace Anax\View;
4
5
/**
6
 * A view container, store all views per region, render at will.
7
 *
8
 */
9
class CViewContainer implements \Anax\DI\IInjectionAware
10
{
11
    use \Anax\TConfigure,
12
        \Anax\DI\TInjectionAware;
13
14
15
16
    /**
17
     * Properties
18
     *
19
     */
20
    private $views = []; // Array for all views
21
22
23
24
    /**
25
     * Convert template to path to template file.
26
     *
27
     * @param string $template the name of the template file to include
28
     *
29
     * @throws Anax\View\Exception when template file is missing
30
     *
31
     * @return string as path to the template file
32
     */
33
    public function getTemplateFile($template)
34
    {
35
        $paths  = $this->config["path"];
36
        $suffix = $this->config["suffix"];
37
38
        foreach ($paths as $path) {
39
            $file = $path . "/" . $template . $suffix;
40
            if (is_file($file)) {
41
                return $file;
42
            }
43
        }
44
45
        throw new Exception("Could not find template file '$template'.");
46
    }
47
48
49
50
    /**
51
     * Add a view to be included as a template file.
52
     *
53
     * @param string $template the name of the template file to include
54
     * @param array  $data     variables to make available to the view, default is empty
55
     * @param string $region   which region to attach the view
56
     * @param int    $sort     which order to display the views
57
     *
58
     * @return $this
59
     */
60
    public function add($template, $data = [], $region = "main", $sort = 0)
61
    {
62
        if (empty($template)) {
63
            return $this;
64
        }
65
66
        $view = $this->di->get("view");
67
68
        if (is_string($template)) {
69
            $tpl = $this->getTemplateFile($template);
70
            $type = "file";
71
        } elseif (is_array($template)) {
72
            // Can be array with complete view or array with callback
73
            $tpl = $template;
74
            $type = null;
75
            $region = isset($tpl["region"])
76
                ? $tpl["region"]
77
                : $region;
78
79
            if (isset($tpl["callback"])) {
80
                // Need to test the callback!
81
                $tpl["template"] = $template;
82
                $tpl["type"] = "callback";
83
            } elseif (isset($tpl["template"])) {
84
                if (!isset($tpl["type"]) || $tpl["type"] === "file") {
85
                    $tpl["type"] = "file";
86
                    $tpl["template"] = $this->getTemplateFile($tpl["template"]);
87
                }
88
            }
89
        }
90
91
        $view->set($tpl, $data, $sort, $type);
0 ignored issues
show
The variable $tpl 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...
The variable $type 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...
92
        $view->setDI($this->di);
93
        $this->views[$region][] = $view;
94
95
        return $this;
96
    }
97
98
99
100
    /**
101
     * Add a callback to be rendered as a view.
102
     *
103
     * @param string $callback function to call to get the content of the view
104
     * @param array  $data     variables to make available to the view, default is empty
105
     * @param string $region   which region to attach the view
106
     * @param int    $sort     which order to display the views
107
     *
108
     * @return $this
109
     */
110 View Code Duplication
    public function addCallback($callback, $data = [], $region = "main", $sort = 0)
0 ignored issues
show
This method seems to be duplicated in 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...
111
    {
112
        $view = $this->di->get("view");
113
        $view->set(["callback" => $callback], $data, $sort, "callback");
114
        $view->setDI($this->di);
115
        $this->views[$region][] = $view;
116
117
        return $this;
118
    }
119
120
121
122
    /**
123
     * Add a string as a view.
124
     *
125
     * @param string $content the content
126
     * @param string $region  which region to attach the view
127
     * @param int    $sort    which order to display the views
128
     *
129
     * @return $this
130
     */
131 View Code Duplication
    public function addString($content, $region = "main", $sort = 0)
0 ignored issues
show
This method seems to be duplicated in 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...
132
    {
133
        $view = $this->di->get("view");
134
        $view->set($content, [], $sort, "string");
135
        $view->setDI($this->di);
136
        $this->views[$region][] = $view;
137
        
138
        return $this;
139
    }
140
141
142
143
    /**
144
     * Check if a region has views to render.
145
     *
146
     * @param string $region which region to check
147
     *
148
     * @return $this
149
     */
150
    public function hasContent($region)
151
    {
152
        return isset($this->views[$region]);
153
    }
154
155
156
157
    /**
158
     * Render all views for a specific region.
159
     *
160
     * @param string $region which region to use
161
     *
162
     * @return $this
163
     */
164
    public function render($region = "main")
165
    {
166
        if (!isset($this->views[$region])) {
167
            return $this;
168
        }
169
170
        mergesort($this->views[$region], function ($a, $b) {
171
            $sa = $a->sortOrder();
172
            $sb = $b->sortOrder();
173
174
            if ($sa == $sb) {
175
                return 0;
176
            }
177
178
            return $sa < $sb ? -1 : 1;
179
        });
180
181
        foreach ($this->views[$region] as $view) {
182
            $view->render();
183
        }
184
185
        return $this;
186
    }
187
}
188