Issues (1490)

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/Http/Api/CommentsController.php (12 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 Fabrica\Http\Api;
4
5
use DB;
6
use Fabrica\Events\IssueEvent;
7
use Fabrica\Http\Api\Controller;
8
9
use Fabrica\Http\Requests;
10
use Fabrica\Project\Provider;
11
12
use Illuminate\Http\Request;
13
use Illuminate\Support\Facades\Event;
14
use Sentinel;
15
16
class CommentsController extends Controller
17
{
18
    /**
19
     * Display a listing of the resource.
20
     *
21
     * @return \Illuminate\Http\Response
22
     */
23 View Code Duplication
    public function index(Request $request, $project_key, $issue_id)
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...
24
    {
25
        $sort = ($request->input('sort') === 'asc') ? 'asc' : 'desc';
26
27
        $comments = DB::collection('comments_' . $project_key)
28
            ->where('issue_id', $issue_id)
29
            ->orderBy('created_at', $sort)
30
            ->get();
31
32
        return response()->json([ 'ecode' => 0, 'data' => parent::arrange($comments), 'options' => [ 'current_time' => time() ] ]);
0 ignored issues
show
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
Comprehensibility Bug introduced by
It seems like you call parent on a different method (arrange() instead of index()). Are you sure this is correct? If so, you might want to change this to $this->arrange().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
33
    }
34
35
    /**
36
     * Store a newly created resource in storage.
37
     *
38
     * @param  \Illuminate\Http\Request $request
39
     * @return \Illuminate\Http\Response
40
     */
41
    public function store(Request $request, $project_key, $issue_id)
42
    {
43
        if (!$this->isPermissionAllowed($project_key, 'add_comments')) {
44
            return response()->json(['ecode' => -10002, 'emsg' => 'permission denied.']);
0 ignored issues
show
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
45
        }
46
47
        $contents = $request->input('contents');
48
        if (!$contents) {
49
            throw new \UnexpectedValueException('the contents can not be empty.', -11200);
50
        }
51
52
        $creator = [ 'id' => $this->user->id, 'name' => $this->user->first_name, 'email' => $this->user->email ];
53
54
        $table = 'comments_' . $project_key;
55
56
        $id = DB::collection($table)->insertGetId(array_only($request->all(), [ 'contents', 'atWho' ]) + [ 'issue_id' => $issue_id, 'creator' => $creator, 'created_at' => time() ]);
57
58
        // trigger event of comments added
59
        Event::fire(new IssueEvent($project_key, $issue_id, $creator, [ 'event_key' => 'add_comments', 'data' => array_only($request->all(), [ 'contents', 'atWho' ]) ])); 
60
61
        $comments = DB::collection($table)->find($id);
62
        return response()->json([ 'ecode' => 0, 'data' => parent::arrange($comments) ]);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (arrange() instead of store()). Are you sure this is correct? If so, you might want to change this to $this->arrange().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
63
    }
64
65
    /**
66
     * Display the specified resource.
67
     *
68
     * @param  int $id
69
     * @return \Illuminate\Http\Response
70
     */
71
    public function show($project_key, $id)
72
    {
73
        $comments = DB::collection('comments_' . $project_key)->find($id);
74
        return response()->json(['ecode' => 0, 'data' => parent::arrange($comments)]);
0 ignored issues
show
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
Comprehensibility Bug introduced by
It seems like you call parent on a different method (arrange() instead of show()). Are you sure this is correct? If so, you might want to change this to $this->arrange().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
75
    }
76
77
    /**
78
     * Update the specified resource in storage.
79
     *
80
     * @param  \Illuminate\Http\Request $request
81
     * @param  int                      $id
82
     * @return \Illuminate\Http\Response
83
     */
84
    public function update(Request $request, $project_key, $issue_id, $id)
85
    {
86
        $comments = DB::collection('comments_' . $project_key)->find($id);
87
        if (!$comments) {
88
            throw new \UnexpectedValueException('the comments does not exist or is not in the project.', -11201);
89
        }
90
91
        $contents = $request->input('contents');
92
        if (isset($contents)) {
93
            if (!$contents) {
94
                throw new \UnexpectedValueException('the contents can not be empty.', -11200);
95
            }
96
        }
97
        // record the changed comments
98
        $changedComments = [];
99
100
        $table = 'comments_' . $project_key;
101
        $user = [ 'id' => $this->user->id, 'name' => $this->user->first_name, 'email' => $this->user->email ];
102
        $operation = $request->input('operation');
103
        if (isset($operation)) {
104
            if (!in_array($operation, [ 'addReply', 'editReply', 'delReply' ])) {
105
                throw new \UnexpectedValueException('the operation is incorrect value.', -11204);
106
            }
107
            if (!isset($comments['reply']) || !$comments['reply']) {
108
                $comments['reply'] = [];
109
            }
110
111
            if ($operation == 'addReply') {
112
                if (!$this->isPermissionAllowed($project_key, 'add_comments')) {
113
                    return response()->json(['ecode' => -10002, 'emsg' => 'permission denied.']);
0 ignored issues
show
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
114
                }
115
116
                $reply_id = md5(microtime() . $this->user->id); 
117
                array_push($comments['reply'], array_only($request->all(), [ 'contents', 'atWho' ]) + [ 'id' => $reply_id , 'creator' => $user, 'created_at' => time() ]);
118
                $changedComments = array_only($request->all(), [ 'contents', 'atWho' ]) + [ 'to' => $comments['creator'] ];
119
            } 
120
            else if ($operation == 'editReply') {
121
                $reply_id = $request->input('reply_id');
122
                if (!isset($reply_id) || !$reply_id) {
123
                    throw new \UnexpectedValueException('the reply id can not be empty.', -11202);
124
                }
125
                $index = $this->array_find([ 'id' => $reply_id ], $comments['reply']); 
126
                if ($index !== false) {
127
                    if (!$this->isPermissionAllowed($project_key, 'edit_comments') && !($comments['reply'][$index]['creator']['id'] == $this->user->id && $this->isPermissionAllowed($project_key, 'edit_self_comments'))) {
128
                        return response()->json(['ecode' => -10002, 'emsg' => 'permission denied.']);
129
                    }
130
               
131
                    $comments['reply'][$index] = array_merge($comments['reply'][$index], [ 'updated_at' => time(), 'edited_flag' => 1 ] + array_only($request->all(), [ 'contents', 'atWho' ]));
132
                    $changedComments = array_only($comments['reply'][$index], [ 'contents', 'atWho' ]) + [ 'to' => $comments['creator'] ];
133
                }
134
                else
135
                {
136
                    throw new \UnexpectedValueException('the reply does not exist', -11203);
137
                }
138
            }
139
            else if ($operation == 'delReply') {
140
                $reply_id = $request->input('reply_id');
141
                if (!isset($reply_id) || !$reply_id) {
142
                    throw new \UnexpectedValueException('the reply id can not be empty.', -11202);
143
                }
144
                $index = $this->array_find([ 'id' => $reply_id ], $comments['reply']); 
145
                if ($index !== false) {
146
                    if (!$this->isPermissionAllowed($project_key, 'delete_comments') && !($comments['reply'][$index]['creator']['id'] == $this->user->id && $this->isPermissionAllowed($project_key, 'delete_self_comments'))) {
147
                        return response()->json(['ecode' => -10002, 'emsg' => 'permission denied.']);
148
                    }
149
150
                    $changedComments = array_only($comments['reply'][$index], [ 'contents', 'atWho' ]) + [ 'to' => $comments['creator'] ];
151
                    array_splice($comments['reply'], $index, 1);
152
                }
153
                else
154
                {
155
                    throw new \UnexpectedValueException('the reply does not exist', -11203);
156
                }
157
            }
158
            DB::collection($table)->where('_id', $id)->update([ 'reply' => $comments['reply'] ]);
159
        }
160
        else
161
        {
162 View Code Duplication
            if (!$this->isPermissionAllowed($project_key, 'edit_comments') && !($comments['creator']['id'] == $this->user->id && $this->isPermissionAllowed($project_key, 'edit_self_comments'))) {
0 ignored issues
show
This code seems to be duplicated across 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...
163
                return response()->json(['ecode' => -10002, 'emsg' => 'permission denied.']);
164
            }
165
166
            DB::collection($table)->where('_id', $id)->update([ 'updated_at' => time(), 'edited_flag' => 1 ] + array_only($request->all(), [ 'contents', 'atWho' ]));
167
            $changedComments = array_only($request->all(), [ 'contents', 'atWho' ]);
168
        }
169
170
        // trigger event of comments 
171
        $event_key = '';
172
        if (isset($operation)) {
173
            $operation === 'addReply'   && $event_key = 'add_comments';
174
            $operation === 'editReply'  && $event_key = 'edit_comments';
175
            $operation === 'delReply'   && $event_key = 'del_comments';
176
        }
177
        else
178
        {
179
            $event_key = 'edit_comments';
180
        }
181
        Event::fire(new IssueEvent($project_key, $issue_id, $user, [ 'event_key' => $event_key, 'data' => $changedComments ]));
182
183
        return response()->json([ 'ecode' => 0, 'data' => parent::arrange(DB::collection($table)->find($id)) ]);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (arrange() instead of update()). Are you sure this is correct? If so, you might want to change this to $this->arrange().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
184
    }
185
186
    /**
187
     * Remove the specified resource from storage.
188
     *
189
     * @param  int $id
190
     * @return \Illuminate\Http\Response
191
     */
192
    public function destroy($project_key, $issue_id, $id)
193
    {
194
        $table = 'comments_' . $project_key;
195
        $comments = DB::collection($table)->find($id);
196
        if (!$comments) {
197
            throw new \UnexpectedValueException('the comments does not exist or is not in the project.', -11201);
198
        }
199
200 View Code Duplication
        if (!$this->isPermissionAllowed($project_key, 'manage_project') && !($comments['creator']['id'] == $this->user->id && $this->isPermissionAllowed($project_key, 'delete_self_comments'))) {
0 ignored issues
show
This code seems to be duplicated across 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...
201
            return response()->json(['ecode' => -10002, 'emsg' => 'permission denied.']);
0 ignored issues
show
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
202
        }
203
204
        DB::collection($table)->where('_id', $id)->delete();
205
206
        $user = [ 'id' => $this->user->id, 'name' => $this->user->first_name, 'email' => $this->user->email ];
207
        // trigger the event of del comments
208
        Event::fire(new IssueEvent($project_key, $issue_id, $user, [ 'event_key' => 'del_comments', 'data' => array_only($comments, [ 'contents', 'atWho' ]) ])); 
209
210
        return response()->json(['ecode' => 0, 'data' => ['id' => $id]]);
211
    }
212
213
    /**
214
     * define the array function of searching for array object.
215
     *
216
     * @param  array $needle
217
     * @param  array $haystack
218
     * @return \Illuminate\Http\Response
219
     */
220
    public function array_find($needle, $haystack)
221
    {
222
        foreach($haystack as $key => $val)
223
        {
224
            if ($needle['id'] == $val['id']) {
225
                return $key;
226
            }
227
        }
228
        return false;
229
    }
230
}
231