This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * Comment Class |
||
4 | * |
||
5 | * @license http://opensource.org/licenses/MIT The MIT License (MIT) |
||
6 | * @author Omar El Gabry <[email protected]> |
||
7 | */ |
||
8 | |||
9 | class Comment extends Model{ |
||
0 ignored issues
–
show
|
|||
10 | |||
11 | /** |
||
12 | * get all comments of a post |
||
13 | * |
||
14 | * @access public |
||
15 | * @param array $postId |
||
16 | * @param integer $pageNum |
||
17 | * @param integer $commentsCreated |
||
18 | * @return array Associative array of the comments, and Pagination Object(View More). |
||
19 | * |
||
20 | */ |
||
21 | public function getAll($postId, $pageNum = 1, $commentsCreated = 0){ |
||
22 | |||
23 | // Only for comments, We use $commentsCreated |
||
24 | // What's it? Whenever we create a comment, It will be added in-place to the current comments in current .php page, |
||
25 | // So, we need to track of those were created, and skip them in the Pagination($offset & $totalCount). |
||
26 | |||
27 | $options = "WHERE comments.post_id = :post_id "; |
||
28 | $pagination = Pagination::pagination("comments", $options, [":post_id" => $postId], $pageNum, $commentsCreated); |
||
29 | $offset = $pagination->getOffset() + $commentsCreated; |
||
30 | $limit = $pagination->perPage; |
||
31 | |||
32 | $database = Database::openConnection(); |
||
33 | $query = "SELECT comments.id AS id, users.profile_picture, users.id AS user_id, users.name AS user_name, comments.content, comments.date "; |
||
34 | $query .= "FROM users, posts, comments "; |
||
35 | $query .= "WHERE comments.post_id = :post_id "; |
||
36 | $query .= "AND posts.id = comments.post_id "; |
||
37 | $query .= "AND users.id = comments.user_id "; |
||
38 | $query .= "ORDER BY comments.date DESC "; |
||
39 | $query .= "LIMIT $limit OFFSET $offset"; |
||
40 | |||
41 | $database->prepare($query); |
||
42 | $database->bindValue(':post_id', (int)$postId); |
||
43 | $database->execute(); |
||
44 | $comments = $database->fetchAllAssociative(); |
||
45 | |||
46 | // you can have post with no comments yet! |
||
47 | return array("comments" => $comments, "pagination" => $pagination); |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * get comment by Id |
||
52 | * |
||
53 | * @access public |
||
54 | * @param string $commentId |
||
55 | * @return array Array holds the data of the comment |
||
56 | * |
||
57 | */ |
||
58 | View Code Duplication | public function getById($commentId){ |
|
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. ![]() |
|||
59 | |||
60 | $database = Database::openConnection(); |
||
61 | $query = "SELECT comments.id AS id, users.profile_picture, users.id AS user_id, users.name AS user_name, comments.content, comments.date "; |
||
62 | $query .= "FROM users, posts, comments "; |
||
63 | $query .= "WHERE comments.id = :id "; |
||
64 | $query .= "AND posts.id = comments.post_id "; |
||
65 | $query .= "AND users.id = comments.user_id LIMIT 1"; |
||
66 | |||
67 | $database->prepare($query); |
||
68 | $database->bindValue(':id', (int)$commentId); |
||
69 | $database->execute(); |
||
70 | |||
71 | $comment = $database->fetchAllAssociative(); |
||
72 | return $comment; |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * create Comment. |
||
77 | * |
||
78 | * @access public |
||
79 | * @param string $userId |
||
80 | * @param string $postId |
||
81 | * @param string $content |
||
82 | * @return array Array holds the created comment |
||
83 | * @throws Exception If comment couldn't be created |
||
84 | * |
||
85 | */ |
||
86 | public function create($userId, $postId, $content){ |
||
87 | |||
88 | $validation = new Validation(); |
||
89 | if(!$validation->validate([ 'Content' => [$content, 'required|minLen(1)|maxLen(300)']])) { |
||
90 | $this->errors = $validation->errors(); |
||
91 | return false; |
||
92 | } |
||
93 | |||
94 | $database = Database::openConnection(); |
||
95 | $query = "INSERT INTO comments (user_id, post_id, content) VALUES (:user_id, :post_id, :content)"; |
||
96 | |||
97 | $database->prepare($query); |
||
98 | $database->bindValue(':user_id', $userId); |
||
99 | $database->bindValue(':post_id', $postId); |
||
100 | $database->bindValue(':content', $content); |
||
101 | $database->execute(); |
||
102 | |||
103 | if($database->countRows() !== 1){ |
||
104 | throw new Exception ("Couldn't add comment"); |
||
105 | } |
||
106 | |||
107 | $commentId = $database->lastInsertedId(); |
||
108 | $comment = $this->getById($commentId); |
||
109 | return $comment; |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * update Comment |
||
114 | * |
||
115 | * @access public |
||
116 | * @param string $commentId |
||
117 | * @param string $content |
||
118 | * @return array Array holds the updated comment |
||
119 | * @throws Exception If comment couldn't be updated |
||
120 | * |
||
121 | */ |
||
122 | View Code Duplication | public function update($commentId, $content){ |
|
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. ![]() |
|||
123 | |||
124 | $validation = new Validation(); |
||
125 | if(!$validation->validate([ 'Content' => [$content, 'required|minLen(1)|maxLen(300)']])) { |
||
126 | $this->errors = $validation->errors(); |
||
127 | return false; |
||
128 | } |
||
129 | |||
130 | $database = Database::openConnection(); |
||
131 | $query = "UPDATE comments SET content = :content WHERE id = :id LIMIT 1 "; |
||
132 | $database->prepare($query); |
||
133 | $database->bindValue(':content', $content); |
||
134 | $database->bindValue(':id', $commentId); |
||
135 | $result = $database->execute(); |
||
136 | |||
137 | if(!$result){ |
||
138 | throw new Exception("Couldn't update comment of ID: " . $commentId); |
||
139 | } |
||
140 | |||
141 | $comment = $this->getById($commentId); |
||
142 | return $comment; |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * counting the number of comments of a post. |
||
147 | * |
||
148 | * @access public |
||
149 | * @static static method |
||
150 | * @param string $postId |
||
151 | * @return integer number of comments |
||
152 | * |
||
153 | */ |
||
154 | View Code Duplication | public static function countComments($postId){ |
|
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. ![]() |
|||
155 | |||
156 | $database = Database::openConnection(); |
||
157 | $database->prepare("SELECT COUNT(*) AS count FROM comments WHERE post_id = :post_id"); |
||
158 | $database->bindValue(":post_id", $postId); |
||
159 | $database->execute(); |
||
160 | |||
161 | return (int)$database->fetchAssociative()["count"]; |
||
162 | } |
||
163 | |||
164 | } |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.