Issues (4)

Security Analysis    no request data  

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

  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.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  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.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  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.
  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.
  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.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
  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.
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  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.
  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.
  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.
  Header Injection
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/ModelOverride.php (2 issues)

1
<?php
2
3
namespace Okipa\LaravelModelJsonStorage;
4
5
use Exception;
6
use File;
7
use Illuminate\Support\Collection;
8
9
trait ModelOverride
10
{
11
    /**
12
     * Get all of the models from the json file.
13
     *
14
     * @param array $columns
15
     *
16
     * @return Collection
17
     */
18 22
    public static function all($columns = ['*'])
19
    {
20 22
        return (new static)->get($columns);
21
    }
22
23
    /**
24
     * Add a "where not in" clause to the query.
25
     *
26
     * @param string $column
27
     * @param array  $values
28
     *
29
     * @return $this
30
     */
31
    abstract public function whereNotIn(string $column, array $values);
32
    
33
    
34
35
    /**
36
     * Get the class name of the parent model.
37
     *
38
     * @return string
39
     */
40
    abstract public function getMorphClass();
41
42
    /**
43
     * Load all of the models from the json file in the "modelsFromJson" variable.
44
     *
45
     * @return Collection
46
     */
47
    abstract protected function loadModelsFromJson();
48
49
    /**
50
     * Execute the query as a "select" statement.
51
     *
52
     * @param  array $columns
53
     *
54
     * @return Collection
55
     */
56
    abstract public function get(array $columns = ['*']);
57
58
    /**
59
     * Fill the model with an array of attributes.
60
     *
61
     * @param  array $attributes
62
     *
63
     * @return $this
64
     * @throws \Illuminate\Database\Eloquent\MassAssignmentException
65
     */
66
    abstract public function fill(array $attributes);
67
68
    /**
69
     * Determine if the model uses timestamps.
70
     *
71
     * @return bool
72
     */
73
    abstract public function usesTimestamps();
74
75
    /**
76
     * Get a fresh timestamp for the model.
77
     *
78
     * @return string
79
     */
80
    abstract public function freshTimestampString();
81
82
    /**
83
     * Set the value of the "created at" attribute.
84
     *
85
     * @param  mixed $value
86
     *
87
     * @return $this
88
     */
89
    abstract public function setCreatedAt($value);
90
91
    /**
92
     * Set the value of the "updated at" attribute.
93
     *
94
     * @param  mixed $value
95
     *
96
     * @return $this
97
     */
98
    abstract public function setUpdatedAt($value);
99
100
    /**
101
     * Get an attribute from the model.
102
     *
103
     * @param  string $key
104
     *
105
     * @return mixed
106
     */
107
    abstract public function getAttribute($key);
108
109
    /**
110
     * Get all of the current attributes on the model.
111
     *
112
     * @return array
113
     */
114
    abstract public function getAttributes();
115
116
    /**
117
     * Make the given, typically hidden, attributes visible.
118
     *
119
     * @param  array|string $attributes
120
     *
121
     * @return $this
122
     */
123
    abstract public function makeVisible($attributes);
124
125
    /**
126
     * Set the array of model attributes. No checking is done.
127
     *
128
     * @param  array $attributes
129
     * @param  bool  $sync
130
     *
131
     * @return $this
132
     */
133
    abstract public function setRawAttributes(array $attributes, $sync = false);
134
135
    /**
136
     * Get the hidden attributes for the model.
137
     *
138
     * @return array
139
     */
140
    abstract public function getHidden();
141
142
    /**
143
     * Get the primary key for the model.
144
     *
145
     * @return string
146
     */
147
    abstract public function getKeyName();
148
149
    /**
150
     * Save the model to the json file.
151
     *
152
     * @param  array $options
153
     *
154
     * @return bool
155
     */
156 22
    public function save(array $options = [])
0 ignored issues
show
The parameter $options is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

156
    public function save(/** @scrutinizer ignore-unused */ array $options = [])

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

Loading history...
157
    {
158 22
        return $this->saveToJson();
159
    }
160
161
    /**
162
     * Save the model to the json file.
163
     *
164
     * @return bool
165
     */
166 22
    protected function saveToJson()
167
    {
168 22
        if ($this->{$this->getKeyName()}) {
169 2
            $this->updateModelInJson();
170
        } else {
171 22
            $this->createModelInJson();
172
        }
173
174 22
        return true;
175
    }
176
177
    /**
178
     * Update the model in the json file.
179
     *
180
     * @return void
181
     */
182 2
    protected function updateModelInJson()
183
    {
184 2
        if ($this->usesTimestamps()) {
185 2
            $this->setTimestampFields(true);
186
        }
187 2
        $withoutCurrentModel = $this->whereNotIn(
188 2
            $this->getKeyName(),
189 2
            [$this->getAttribute($this->getKeyName())]
190 2
        )->get();
191 2
        $models = $withoutCurrentModel->push($this->makeVisible($this->getHidden()))->sortBy($this->getKeyName());
192 2
        File::put($this->getJsonStoragePath(), $models->toJson());
193 2
    }
194
195
    /**
196
     * Set the model timestamp field
197
     *
198
     * @param bool $update
199
     *
200
     * @return void
201
     */
202 22
    protected function setTimestampFields($update = false)
203
    {
204 22
        $now = $this->freshTimestampString();
205 22
        $this->setUpdatedAt($now);
206 22
        if (! $update) {
207 22
            $this->setCreatedAt($now);
208
        }
209 22
    }
210
211
    /**
212
     * Get the storage path for the model json file.
213
     *
214
     * @return string
215
     */
216 23
    public function getJsonStoragePath()
217
    {
218 23
        $modelName = str_slug(last(explode('\\', $this->getMorphClass())));
219 23
        $configStoragePath = storage_path(config('model-json-storage.storage_path'));
220 23
        if (! is_dir($configStoragePath)) {
221 23
            mkdir($configStoragePath, 0777, true);
222
        }
223 23
        $jsonStoragePath = $configStoragePath . '/' . $modelName . '.json';
224
225 23
        return $jsonStoragePath;
226
    }
227
228
    /**
229
     * Save a new model in the json file.
230
     *
231
     * @return void
232
     */
233 22
    protected function createModelInJson()
234
    {
235 22
        if ($this->usesTimestamps()) {
236 22
            $this->setTimestampFields();
237
        }
238 22
        $this->setModelPrimaryKeyValue();
239 22
        $models = $this->all()->push($this->makeVisible($this->getHidden()));
240 22
        File::put($this->getJsonStoragePath(), $models->toJson());
241 22
    }
242
243
    /**
244
     * Set the model primary key by incrementing from the bigger id found in the json file.
245
     *
246
     * @return void
247
     */
248 22
    protected function setModelPrimaryKeyValue()
249
    {
250 22
        $modelPrimaryKeyValue = 1;
251 22
        $modelsCollection = $this->loadModelsFromJson();
252 22
        if (! $modelsCollection->isEmpty()) {
253 19
            $lastModelId = $modelsCollection->sortBy('id')->last()->getAttribute($this->getKeyName());
254 19
            $modelPrimaryKeyValue = $lastModelId + 1;
255
        }
256 22
        $this->setRawAttributes(array_merge(['id' => $modelPrimaryKeyValue], $this->getAttributes()));
257 22
    }
258
259
    /**
260
     * Update the model in the json file.
261
     *
262
     * @param array $attributes
263
     * @param array $options
264
     *
265
     * @return mixed
266
     */
267 1
    public function update(array $attributes = [], array $options = [])
268
    {
269 1
        return $this->fill($attributes)->save($options);
270
    }
271
272
    /**
273
     * Delete the model from the json file.
274
     *
275
     * @return bool|null
276
     * @throws Exception
277
     */
278 2
    public function delete()
279
    {
280 2
        if (is_null($this->getKeyName())) {
0 ignored issues
show
The condition is_null($this->getKeyName()) is always false.
Loading history...
281 1
            throw new Exception('No primary key defined on model.');
282
        }
283 1
        $this->deleteModelFromJson();
284
285 1
        return true;
286
    }
287
288
    /**
289
     * Delete the model from the json file.
290
     *
291
     * @return void
292
     */
293 1
    protected function deleteModelFromJson()
294
    {
295 1
        $withoutCurrentModel = $this->whereNotIn(
296 1
            $this->getKeyName(),
297 1
            [$this->getAttribute($this->getKeyName())]
298 1
        )->get();
299 1
        File::put($this->getJsonStoragePath(), $withoutCurrentModel->values()->toJson());
300 1
    }
301
}
302