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 | * Elgg Annotations |
||
4 | * |
||
5 | * Annotations allow you to attach bits of information to entities. They are |
||
6 | * essentially the same as metadata, but with additional helper functions for |
||
7 | * performing calculations. |
||
8 | * |
||
9 | * @note Internal: Annotations are stored in the annotations table. |
||
10 | * |
||
11 | * @package Elgg.Core |
||
12 | * @subpackage DataModel.Annotations |
||
13 | */ |
||
14 | class ElggAnnotation extends \ElggExtender { |
||
15 | |||
16 | /** |
||
17 | * (non-PHPdoc) |
||
18 | * |
||
19 | * @see \ElggData::initializeAttributes() |
||
20 | * |
||
21 | * @return void |
||
22 | */ |
||
23 | protected function initializeAttributes() { |
||
24 | parent::initializeAttributes(); |
||
25 | |||
26 | $this->attributes['type'] = 'annotation'; |
||
27 | } |
||
28 | |||
29 | /** |
||
30 | * Construct a new annotation object |
||
31 | * |
||
32 | * Plugin developers will probably never use the constructor. |
||
33 | * See \ElggEntity for its API for adding annotations. |
||
34 | * |
||
35 | * @param \stdClass $row Database row as \stdClass object |
||
36 | */ |
||
37 | View Code Duplication | public function __construct($row = null) { |
|
0 ignored issues
–
show
|
|||
38 | $this->initializeAttributes(); |
||
39 | |||
40 | if (!empty($row)) { |
||
41 | // Create from db row |
||
42 | if ($row instanceof \stdClass) { |
||
43 | $annotation = $row; |
||
44 | |||
45 | $objarray = (array) $annotation; |
||
46 | foreach ($objarray as $key => $value) { |
||
47 | $this->attributes[$key] = $value; |
||
48 | } |
||
49 | } else { |
||
50 | // get an \ElggAnnotation object and copy its attributes |
||
51 | elgg_deprecated_notice('Passing an ID to constructor is deprecated. Use elgg_get_annotation_from_id()', 1.9); |
||
52 | $annotation = elgg_get_annotation_from_id($row); |
||
53 | $this->attributes = $annotation->attributes; |
||
54 | } |
||
55 | } |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * Save this instance |
||
60 | * |
||
61 | * @return int an object id |
||
62 | * |
||
63 | * @throws IOException |
||
64 | */ |
||
65 | View Code Duplication | public function save() { |
|
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. ![]() |
|||
66 | if ($this->id > 0) { |
||
67 | return update_annotation($this->id, $this->name, $this->value, $this->value_type, |
||
0 ignored issues
–
show
|
|||
68 | $this->owner_guid, $this->access_id); |
||
69 | } else { |
||
70 | $this->id = create_annotation($this->entity_guid, $this->name, $this->value, |
||
71 | $this->value_type, $this->owner_guid, $this->access_id); |
||
72 | |||
73 | if (!$this->id) { |
||
0 ignored issues
–
show
The expression
$this->id of type false|integer is loosely compared to false ; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.
In PHP, under loose comparison (like For 0 == false // true
0 == null // true
123 == false // false
123 == null // false
// It is often better to use strict comparison
0 === false // false
0 === null // false
![]() |
|||
74 | throw new \IOException("Unable to save new " . get_class()); |
||
75 | } |
||
76 | return $this->id; |
||
0 ignored issues
–
show
The return type of
return $this->id; (integer ) is incompatible with the return type declared by the abstract method ElggData::save of type boolean .
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 ![]() |
|||
77 | } |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * Delete the annotation. |
||
82 | * |
||
83 | * @return bool |
||
84 | */ |
||
85 | public function delete() { |
||
86 | $result = _elgg_delete_metastring_based_object_by_id($this->id, 'annotation'); |
||
87 | if ($result) { |
||
88 | elgg_delete_river(array('annotation_id' => $this->id)); |
||
89 | } |
||
90 | |||
91 | return $result; |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Disable the annotation. |
||
96 | * |
||
97 | * @return bool |
||
98 | * @since 1.8 |
||
99 | */ |
||
100 | public function disable() { |
||
101 | return _elgg_set_metastring_based_object_enabled_by_id($this->id, 'no', 'annotations'); |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * Enable the annotation. |
||
106 | * |
||
107 | * @return bool |
||
108 | * @since 1.8 |
||
109 | */ |
||
110 | public function enable() { |
||
111 | return _elgg_set_metastring_based_object_enabled_by_id($this->id, 'yes', 'annotations'); |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * Determines whether or not the user can edit this annotation |
||
116 | * |
||
117 | * @param int $user_guid The GUID of the user (defaults to currently logged in user) |
||
118 | * |
||
119 | * @return bool |
||
120 | * @see elgg_set_ignore_access() |
||
121 | */ |
||
122 | public function canEdit($user_guid = 0) { |
||
123 | |||
124 | View Code Duplication | if ($user_guid) { |
|
125 | $user = get_user($user_guid); |
||
126 | if (!$user) { |
||
127 | return false; |
||
128 | } |
||
129 | } else { |
||
130 | $user = _elgg_services()->session->getLoggedInUser(); |
||
131 | $user_guid = _elgg_services()->session->getLoggedInUserGuid(); |
||
132 | } |
||
133 | |||
134 | $result = false; |
||
135 | if ($user) { |
||
136 | // If the owner of annotation is the specified user, they can edit. |
||
137 | if ($this->getOwnerGUID() == $user_guid) { |
||
138 | $result = true; |
||
139 | } |
||
140 | |||
141 | // If the user can edit the entity this is attached to, they can edit. |
||
142 | $entity = $this->getEntity(); |
||
143 | if ($result == false && $entity->canEdit($user->getGUID())) { |
||
0 ignored issues
–
show
|
|||
144 | $result = true; |
||
145 | } |
||
146 | } |
||
147 | |||
148 | // Trigger plugin hook - note that $user may be null |
||
149 | $params = array('entity' => $entity, 'user' => $user, 'annotation' => $this); |
||
0 ignored issues
–
show
The variable
$entity does not seem to be defined for all execution paths leading up to this point.
If you define a variable conditionally, it can happen that it is not defined for all execution paths. Let’s take a look at an example: function myFunction($a) {
switch ($a) {
case 'foo':
$x = 1;
break;
case 'bar':
$x = 2;
break;
}
// $x is potentially undefined here.
echo $x;
}
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined. Available Fixes
![]() |
|||
150 | return _elgg_services()->hooks->trigger('permissions_check', 'annotation', $params, $result); |
||
151 | } |
||
152 | |||
153 | // SYSTEM LOG INTERFACE |
||
154 | |||
155 | /** |
||
156 | * For a given ID, return the object associated with it. |
||
157 | * This is used by the river functionality primarily. |
||
158 | * This is useful for checking access permissions etc on objects. |
||
159 | * |
||
160 | * @param int $id An annotation ID. |
||
161 | * |
||
162 | * @return \ElggAnnotation |
||
163 | */ |
||
164 | public function getObjectFromID($id) { |
||
165 | return elgg_get_annotation_from_id($id); |
||
0 ignored issues
–
show
The return type of
return elgg_get_annotation_from_id($id); (ElggExtender ) is incompatible with the return type declared by the interface Loggable::getObjectFromID of type ElggEntity .
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 ![]() |
|||
166 | } |
||
167 | } |
||
168 |
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.