Issues (13)

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/FlashCard/Entity/Card.php (1 issue)

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 Moo\FlashCard\Entity;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Validation\Rule;
8
use Moo\FlashCard\Traits\Validatable;
9
use Spatie\Sluggable\HasSlug;
10
use Spatie\Sluggable\SlugOptions;
11
12
/**
13
 * Card is the entity class that represents a record from the database.
14
 *
15
 * @author Mohamed Alsharaf <[email protected]>
16
 */
17
class Card extends Model
18
{
19
    use HasSlug, Validatable;
20
21
    /**
22
     * Constant for card active status
23
     *
24
     * @var bool
25
     */
26
    const ACTIVE = true;
27
    /**
28
     * Constant for card inactive status
29
     *
30
     * @var int
31
     */
32
    const INACTIVE = false;
33
34
    /**
35
     * Max size of slug
36
     *
37
     * @var int
38
     */
39
    const SLUG_SIZE = 100;
40
41
    /**
42
     * Timestamp enabled.
43
     *
44
     * @var bool
45
     */
46
    public $timestamps = true;
47
    /**
48
     * Name of database table.
49
     *
50
     * @var string
51
     */
52
    protected $table = 'card';
53
    /**
54
     * List of allowed columns to be used in $this->fill().
55
     *
56
     * @var array
57
     */
58
    protected $fillable = ['title', 'slug', 'active', 'content', 'meta_description', 'category_id'];
59
60
    protected $appends = ['short_modified'];
61
62
    protected $casts = [
63
        'active' => 'boolean',
64
    ];
65
66
    /**
67
     * Get the category this card belong to
68
     */
69 2
    public function category()
70
    {
71 2
        return $this->belongsTo(Category::class);
72
    }
73
74
    /**
75
     * Get collection of card views
76
     */
77
    public function cardViews()
78
    {
79
        return $this->hasMany(CardView::class);
80
    }
81
82
    /**
83
     * Get formatted version of updated_at attribute
84
     *
85
     * @return string
86
     */
87 5
    public function getShortModifiedAttribute(): string
88
    {
89 5
        if (!is_null($this->updated_at)) {
90 5
            return (string) $this->updated_at->format('F d, Y');
91
        }
92
93
        return '';
94
    }
95
96
    /**
97
     * Get the options for generating the slug.
98
     */
99 11
    public function getSlugOptions(): SlugOptions
100
    {
101 11
        return SlugOptions::create()
102 11
            ->generateSlugsFrom('title')
103 11
            ->saveSlugsTo('slug')
104 11
            ->slugsShouldBeNoLongerThan(static::SLUG_SIZE);
105
    }
106
107
    /**
108
     * Get collection of validation rules for model attributes
109
     *
110
     * @return array
111
     */
112 11
    protected function getRules(): array
113
    {
114
        return [
115 11
            'title'       => 'required|max:255|min:5',
116 11
            'category_id' => 'required',
117
            'slug'        => [
118 11
                'required',
119
                // Ensure slug unique except when editing
120 11
                Rule::unique($this->table, 'slug')->ignore($this),
121 11
                'max:' . static::SLUG_SIZE,
122
            ],
123
        ];
124
    }
125
126
    /**
127
     * Score to filter by active cards
128
     *
129
     * @param  Builder $query
130
     * @return Builder
131
     */
132 6
    public function scopeActive(Builder $query): Builder
133
    {
134 6
        return $query->where('active', '=', static::ACTIVE);
135
    }
136
137
    /**
138
     * Score to filter by LIKE search
139
     *
140
     * @param  Builder $query
141
     * @param  string  $keyword
142
     * @return Builder
143
     */
144 3 View Code Duplication
    public function scopeSearch(Builder $query, string $keyword): Builder
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...
145
    {
146 3
        $query->where('title', 'LIKE', '%' . $keyword . '%');
147 3
        $query->orWhere('content', 'LIKE', '%' . $keyword . '%');
148
149 3
        return $query;
150
    }
151
152
    /**
153
     * Update card view counter by 1 and insert record in card view table
154
     *
155
     * @param string $ip
156
     */
157
    public function incrementViews(string $ip): void
158
    {
159
        try {
160
            // Insert record in card view table
161
            $cardView = new CardView([
162
                'card_id' => $this->id,
163
                'ip'      => $ip,
164
            ]);
165
            $cardView->save();
166
167
            // Increase the view counter by 1
168
            $this->views = $this->views + 1;
169
            $this->save();
170
        } catch (\Exception $exception) {
171
            // We ignore any error as this is just logging of views!
172
        }
173
    }
174
}
175