Issues (243)

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