Issues (28)

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.

models/behaviors/SourceRepository.php (2 issues)

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
3
namespace yiicod\fileupload\models\behaviors;
4
5
use Yii;
6
use yii\db\ActiveRecord;
7
use yii\helpers\FileHelper;
8
use yii\helpers\Url;
9
use yii\web\Application;
10
11
/**
12
 * Class SourceRepository
13
 *
14
 * @package yiicod\fileupload\models\behaviors
15
 */
16
class SourceRepository implements SourceRepositoryInterface
17
{
18
    /**
19
     * @var ActiveRecord
20
     */
21
    public $owner;
22
23
    /**
24
     * The public url for file
25
     * 'url' => 'uploads'
26
     */
27
    public $uploadUrl;
28
29
    /**
30
     * The directory path for file
31
     * 'uploadDir' => Yii::getAlias('@webroot/uploads')
32
     */
33
    public $uploadDir;
34
35
    /**
36
     * Get relative folder path
37
     *
38
     * @return string
39
     */
40
    public function getRelativeFolderPath(): string
41
    {
42
        $primaryKey = ActiveRecord::getDb()
43
            ->getSchema()
44
            ->getTableSchema(get_class($this->owner)::collectionName())
0 ignored issues
show
The method collectionName cannot be called on get_class($this->owner) (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
45
            ->primaryKey;
46
47
        return '/' . lcfirst(get_class($this->owner)::tableName()) . '/' . $this->owner->{$primaryKey};
0 ignored issues
show
The method tableName cannot be called on get_class($this->owner) (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
48
    }
49
50
    /**
51
     * Get folder path
52
     *
53
     * @param string $field Field name
54
     *
55
     * @author Orlov Alexey <[email protected]>
56
     *
57
     * @return string Return path to entity img with|out field name
58
     */
59
    public function getFolderPath(string $field = ''): string
60
    {
61
        $path = rtrim($this->getUploadDir(), '/') . $this->getRelativeFolderPath();
62
63
        if ('' === $field) {
64
            return $path;
65
        }
66
67
        return rtrim($path, '/') . '/' . $field . '/';
68
    }
69
70
    /**
71
     * Get file path by "getFolderPath"
72
     *
73
     * @param string $field Field name
74
     *
75
     * @author Orlov Alexey <[email protected]>
76
     *
77
     * @return string Return path to file
78
     */
79
    public function getFilePath(string $field): string
80
    {
81
        return rtrim($this->getFolderPath($field), '/') . '/' . $this->owner->{$field};
82
    }
83
84
    /**
85
     * Get file type
86
     *
87
     * @author Orlov Alexey <[email protected]>
88
     *
89
     * @param string $field Field name
90
     * @param bool $full Full or not full file type
91
     *
92
     * @todo If no need then delete in the next version
93
     *
94
     * @return string Return file type
95
     */
96
    public function getFileType(string $field, $full = true)
97
    {
98
        $filePath = $this->getFilePath($field);
99
        if (empty($this->owner->{$field}) || !file_exists($filePath)) {
100
            return '';
101
        }
102
103
        $mimeType = FileHelper::getMimeType($filePath);
104
        if (null === $mimeType) {
105
            $ext = pathinfo($filePath, PATHINFO_EXTENSION);
106
            $mimeType = FileHelper::getMimeTypeByExtension($ext);
107
        }
108
109
        if (true === $full) {
110
            return $mimeType;
111
        }
112
113
        $mimeContentType = explode('/', $mimeType);
114
115
        return isset($mimeContentType[1]) ? $mimeContentType[1] : false;
116
    }
117
118
    /**
119
     * Get file src
120
     *
121
     * @author Orlov Alexey <[email protected]>
122
     *
123
     * @param $field
124
     * @param null $default
125
     *
126
     * @return string File src
127
     *
128
     * @throws Exception
129
     */
130
    public function getFileSrc(string $field, $default = null): string
131
    {
132
        if (empty($this->owner->{$field})) {
133
            $url = null === $default ? '' : $default;
134
        } else {
135
            $url = '';
136
            $url .= $this->getUploadUrl();
137
            $url .= rtrim($this->getRelativeFolderPath(), '/') . '/' . $field . '/' . $this->owner->{$field};
138
        }
139
140
        return $url;
141
    }
142
143
    /**
144
     * Base upload dir
145
     *
146
     * @return string
147
     */
148
    public function getUploadDir(): string
149
    {
150
        return $this->uploadDir;
151
    }
152
153
    /**
154
     * Base upload url
155
     *
156
     * @return string
157
     */
158
    public function getUploadUrl(): string
159
    {
160
        if (Yii::$app instanceof Application) {
161
            $this->uploadUrl = 0 === strpos($this->uploadUrl, Url::base(true)) ?
162
                $this->uploadUrl : trim(Url::base(true), '/') . '/' . trim($this->uploadUrl, '/');
163
        }
164
165
        return $this->uploadUrl;
166
    }
167
}
168