Issues (20)

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/Controllers/PostController.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 namespace jlourenco\blog\Controllers;
2
3
use Illuminate\Http\Request;
4
use App\Http\Requests;
5
use App\Http\Controllers\Controller;
6
use Blog;
7
use Sentinel;
8
use Searchy;
9
use Validator;
10
use Input;
11
use Base;
12
use Redirect;
13
use Lang;
14
15
class PostController extends Controller
16
{
17
18
    /**
19
     * Declare the rules for the form validation
20
     *
21
     * @var array
22
     */
23
    protected $validationRules = array(
24
        'title'               => 'required|min:3',
25
        'slug'               => 'required|min:3|unique:BlogPost,slug',
26
        'category'        => 'required|exists:BlogCategory,id',
27
        'author'            => 'required|exists:User,id',
28
        'contents'        => 'required|min:3',
29
    );
30
31
    /**
32
     * Show list of posts.
33
     *
34
     * @return \Illuminate\Http\Response
35
     */
36
    public function index()
37
    {
38
        $posts = Blog::getPostsRepository()->all();
39
40
        return view('admin.posts.list', compact('posts'));
41
    }
42
43
    /**
44
     * Show details of a post,
45
     *
46
     * @param  int  $id
47
     * @return View
48
     */
49
    public function show($id)
50
    {
51
        $post = Blog::getPostsRepository()->findOrFail($id);
52
53
        // Show the page
54
        return View('admin.posts.show', compact('post'));
55
    }
56
57
    /**
58
     * Post update.
59
     *
60
     * @param  int  $id
61
     * @return View
62
     */
63
    public function getEdit($id = null)
64
    {
65
        $post = Blog::getPostsRepository()->find($id);
66
67
        // Get the post information
68
        if($post == null)
69
        {
70
            // Prepare the error message
71
            $error = Lang::get('blog.posts.not_found');
72
73
            // Redirect to the post management page
74
            return Redirect::route('posts')->with('error', $error);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \Redirect::route(...>with('error', $error); (Illuminate\Http\RedirectResponse) is incompatible with the return type documented by jlourenco\blog\Controllers\PostController::getEdit of type jlourenco\blog\Controllers\View.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
75
        }
76
77
        $cats = null;
78
        $users = null;
79
80
        $categories = Blog::getCategoriesRepository()->all(['id', 'name']);
81
        $authors = Sentinel::createModel()->all(['id', 'first_name', 'last_name']);
82
83
        foreach ($categories as $cat)
84
            $cats[$cat->id] = $cat->name;
85
86
        foreach ($authors as $author)
87
            $users[$author->id] = $author->first_name . ' ' . $author->last_name;
88
89
        // Show the page
90
        return View('admin.posts.edit', compact('post', 'cats', 'users'));
91
    }
92
93
    /**
94
     * Post update form processing page.
95
     *
96
     * @param  int      $id
97
     * @return Redirect
98
     */
99
    public function postEdit($id = null)
100
    {
101
        // Get the post information
102
        $post = Blog::getPostsRepository()->find($id);
103
104
        if ($post == null)
105
        {
106
            // Prepare the error message
107
            $error = Lang::get('blog.posts.not_found');
108
109
            // Redirect to the post management page
110
            return Redirect::route('admin.posts.show')->with('error', $error);
111
        }
112
113
        unset($this->validationRules['slug']);
114
        $this->validationRules['slug'] = "required|min:3|unique:BlogPost,slug,{$post->slug},slug";
115
116
        $slug = str_slug(Input::get('title'), '_');
117
118
        $input = Input::all();
119
        $input['slug'] = $slug;
120
121
        // Create a new validator instance from our validation rules
122
        $validator = Validator::make($input, $this->validationRules);
123
124
        // If validation fails, we'll exit the operation now.
125
        if ($validator->fails()) {
126
            // Ooops.. something went wrong
127
            return Redirect::back()->withInput()->withErrors($validator);
128
        }
129
130
        // Update the post
131
        $post->title = Input::get('title');
132
        $post->slug = $slug;
133
        $post->author = Input::get('author');
134
        $post->category = Input::get('category');
135
        $post->contents = Input::get('contents');
136
137
        // Was the post updated?
138
        if ($post->save())
139
        {
140
            Base::Log('Post (' . $post->id . ') was edited.');
141
142
            // Prepare the success message
143
            $success = Lang::get('blog.posts.changed');
144
145
            // Redirect to the user page
146
            return Redirect::route('posts')->with('success', $success);
147
        }
148
149
        $error = Lang::get('blog.posts.error');
150
151
        // Redirect to the post page
152
        return Redirect::route('post.update', $id)->withInput()->with('error', $error);
0 ignored issues
show
$id is of type integer|null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
153
    }
154
155
    /**
156
     * Create new post
157
     *
158
     * @return View
159
     */
160
    public function getCreate()
161
    {
162
        $cats = null;
163
        $users = null;
164
165
        $categories = Blog::getCategoriesRepository()->all(['id', 'name']);
166
        $authors = Sentinel::createModel()->all(['id', 'first_name', 'last_name']);
167
168
        foreach ($categories as $cat)
169
            $cats[$cat->id] = $cat->name;
170
171
        foreach ($authors as $author)
172
            $users[$author->id] = $author->first_name . ' ' . $author->last_name;
173
174
        // Show the page
175
        return View('admin.posts.create', compact('cats', 'users'));
176
    }
177
178
    /**
179
     * Post create form processing.
180
     *
181
     * @return Redirect
182
     */
183
    public function postCreate()
184
    {
185
        $slug = str_slug(Input::get('title'), '_');
186
187
        $input = Input::all();
188
        $input['slug'] = $slug;
189
190
        // Create a new validator instance from our validation rules
191
        $validator = Validator::make($input, $this->validationRules);
192
193
        // If validation fails, we'll exit the operation now.
194
        if ($validator->fails()) {
195
            // Ooops.. something went wrong
196
            return Redirect::back()->withInput()->withErrors($validator);
197
        }
198
199
        $post = Blog::getPostsRepository()->findBySlug($slug);
200
201
        if ($post != null)
202
            return Redirect::route("posts")->with('error', Lang::get('blog.posts.already_exists'));
203
204
        $post = Blog::getPostsRepository()->create([
205
            'title' => Input::get('title'),
206
            'slug' => $slug,
207
            'author' => Input::get('author'),
208
            'category' => Input::get('category'),
209
            'keywords' => Input::get('keywords'),
210
            'contents' => Input::get('contents'),
211
            'likes' => 0,
212
            'shares' => 0,
213
            'views' => 0,
214
        ]);
215
216
        $post->save();
217
218
        Base::Log('A new post (' . $post->id . ') was created.');
219
220
        // Redirect to the home page with success menu
221
        return Redirect::route("posts")->with('success', Lang::get('blog.posts.created'));
222
    }
223
224
    /**
225
     * Delete Confirm
226
     *
227
     * @param   int   $id
228
     * @return  View
229
     */
230 View Code Duplication
    public function getModalDelete($id = null)
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...
231
    {
232
        $confirm_route = $error = null;
233
234
        $title = 'Delete post';
235
        $message = 'Are you sure to delete this post?';
236
237
        // Get post information
238
        $post = Blog::getPostsRepository()->findOrFail($id);
239
240
        if ($post == null)
241
        {
242
            // Prepare the error message
243
            $error = Lang::get('blog.posts.not_found');
244
            return View('layouts.modal_confirmation', compact('title', 'message', 'error', 'model', 'confirm_route'));
245
        }
246
247
        $confirm_route = route('delete/post', ['id' => $post->id]);
248
        return View('layouts.modal_confirmation', compact('title', 'message', 'error', 'model', 'confirm_route'));
249
    }
250
251
    /**
252
     * Delete the given post.
253
     *
254
     * @param  int      $id
255
     * @return Redirect
256
     */
257 View Code Duplication
    public function getDelete($id = null)
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...
258
    {
259
        // Get post information
260
        $post = Blog::getPostsRepository()->find($id);
261
262
        if ($post == null)
263
        {
264
            // Prepare the error message
265
            $error = Lang::get('blog.posts.not_found');
266
267
            // Redirect to the post management page
268
            return Redirect::route('posts')->with('error', $error);
269
        }
270
271
        Base::Log('Post (' . $post->id . ') was deleted.');
272
273
        // Delete the post
274
        $post->delete();
275
276
        // Prepare the success message
277
        $success = Lang::get('blog.posts.deleted');
278
279
        // Redirect to the post management page
280
        return Redirect::route('posts')->with('success', $success);
281
    }
282
283
    /**
284
     * Show a list of all the deleted posts.
285
     *
286
     * @return View
287
     */
288
    public function getDeletedCategories()
289
    {
290
        // Grab deleted posts
291
        $posts = Blog::getPostsRepository()->onlyTrashed()->get();
292
293
        // Show the page
294
        return View('admin.posts.deleted', compact('posts'));
295
    }
296
297
    /**
298
     * Restore a deleted post.
299
     *
300
     * @param  int      $id
301
     * @return Redirect
302
     */
303 View Code Duplication
    public function getRestore($id = null)
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...
304
    {
305
        // Get post information
306
        $post = Blog::getPostsRepository()->withTrashed()->find($id);
307
308
        if ($post == null)
309
        {
310
            // Prepare the error message
311
            $error = Lang::get('blog.posts.not_found');
312
313
            // Redirect to the post management page
314
            return Redirect::route('post.deleted')->with('error', $error);
315
        }
316
317
        Base::Log('Post (' . $post->id . ') was restored.');
318
319
        // Restore the post
320
        $post->restore();
321
322
        // Prepare the success message
323
        $success = Lang::get('blog.posts.restored');
324
325
        // Redirect to the post management page
326
        return Redirect::route('posts.deleted')->with('success', $success);
327
    }
328
329
}
330