Issues (138)

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.

code/model/PageRaterExtension.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
/**
4
 *@author nicolaas [at] sunnysideup up .co .nz
5
 * <% loop $PageRatings %>
6
 *
7
 * <% end_loop %>
8
 *
9
 **/
10
11
class PageRaterExtension extends DataExtension
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
12
{
13
    private static $db = array(
14
        'PageRating' => 'Double(4,2)'
15
    );
16
17
    private static $has_many = array(
18
        'PageRatings' => 'PageRating'
19
    );
20
21
    private static $indexes = array(
22
        'PageRating' => true
23
    );
24
    /**
25
     * add the default rating to each page ...
26
     * @var boolean
27
     */
28
    private static $add_default_rating = false;
29
30
31
    /**
32
     * @var boolean
33
     */
34
    private static $number_of_default_records_to_be_added = 5;
35
36
    public function updateCMSFields(FieldList $fields)
37
    {
38
        if ($this->owner->PageRatings() && $this->owner->PageRatings()->count()) {
39
            $fields->addFieldToTab(
40
                "Root.Ratings",
41
                GridField::create(
42
                    "PageRatings",
43
                    Injector::inst()->get("PageRating")->plural_name(),
44
                    $this->owner->PageRatings(),
45
                    GridFieldConfig_RecordViewer::create()
46
                )
47
            );
48
            $fields->addFieldToTab(
49
                "Root.Ratings",
50
                ReadonlyField::create(
51
                    "PageRating",
52
                    'Average Rating'
53
                )
54
            );
55
        }
56
    }
57
58
59
    public function requireDefaultRecords()
60
    {
61
        parent::requireDefaultRecords();
62
        $step = 50;
63
        if (Config::inst()->get("PageRaterExtension", "add_default_rating")) {
64
            for ($i = 0; $i < 1000000; $i = $i + $step) {
65
                $pages = SiteTree::get()
66
                    ->leftJoin("PageRating", "\"PageRating\".\"ParentID\" = \"SiteTree\".\"ID\"")
67
                    ->where("\"PageRating\".\"ID\" IS NULL")
68
                    ->limit($step, $i);
69
70
                if ($pages->count()) {
71
                    foreach ($pages as $page) {
72
                        $count = 0;
73
                        $max = PageRating::get_number_of_stars();
74
                        $goingBackTo = ($max / rand(1, $max)) - 1;
75
                        $stepsBack = $max - $goingBackTo;
76
                        $ratings = Config::inst()->get("PageRaterExtension", "number_of_default_records_to_be_added") / $stepsBack;
77
                        for ($i = 1; $i <= $ratings; $i++) {
78
                            for ($j = $max; $j > $goingBackTo; $j--) {
79
                                $PageRating = new PageRating();
80
                                $PageRating->Rating = round(rand(1, $j));
81
                                $PageRating->IsDefault = 1;
82
                                $PageRating->ParentID = $page->ID;
83
                                $PageRating->write();
84
                                $count++;
85
                            }
86
                        }
87
                        DB::alteration_message("Created Initial Ratings for Page with title ".$page->Title.". Ratings created: $count", "created");
88
                    }
89
                } else {
90
                    $i = 1000000;
91
                }
92
            }
93
        }
94
    }
95
96
97
    /**
98
     * return the average rating...
99
     * @return Double
100
     */
101
    public function StarRating()
102
    {
103
        return $this->getStarRating();
104
    }
105
    /**
106
     *
107
     * @param string $character optional character (e.g. ★,
108
     *                           if supplied and the number of stars is 3
109
     *                           then it will return ★★★)
110
     * @return int|string
111
     */
112
    public function getStarRating($character = '')
113
    {
114
        $ratings = $this->owner->PageRatingResults();
115
        $rating = 0;
116
        if ($ratings->Count() == 1) {
117
            foreach ($ratings as $ratingItem) {
118
                $rating = $ratingItem->Stars;
119
            }
120
        }
121
        if ($character && $rating) {
122
            return str_repeat($character, $rating);
123
        } else {
124
            return $rating;
125
        }
126
    }
127
    /**
128
     *
129
     * @return int
130
     */
131 View Code Duplication
    public function NumberOfPageRatings()
132
    {
133
        $doSet = new ArrayList();
134
        $sqlQuery = new SQLQuery();
135
        $sqlQuery->setSelect("COUNT(\"PageRating\".\"Rating\") RatingCount");
136
        $sqlQuery->setFrom("\"PageRating\" ");
137
        if ($this->onlyShowApprovedPageRatings()) {
138
            $sqlQuery->setWhere("\"ParentID\" = ".$this->owner->ID." AND \"PageRating\".\"IsApproved\" = 1");
139
        } else {
140
            $sqlQuery->setWhere("\"ParentID\" = ".$this->owner->ID."");
141
        }
142
        $sqlQuery->setOrderBy("RatingCount ASC");
143
        $sqlQuery->setGroupBy("\"ParentID\"");
144
        $sqlQuery->setLimit(1);
145
        $data = $sqlQuery->execute();
146
        if ($data) {
147
            foreach ($data as $record) {
148
                return $record["RatingCount"];
149
            }
150
        }
151
        return 0;
152
    }
153
154
155
    /**
156
     * rating for this page ...
157
     * @return ArrayList
158
     */
159 View Code Duplication
    public function PageRatingResults()
160
    {
161
        $sqlQuery = new SQLQuery();
162
        $sqlQuery->setSelect("AVG(\"PageRating\".\"Rating\") RatingAverage, ParentID");
163
        $sqlQuery->setFrom("\"PageRating\" ");
164
        if ($this->onlyShowApprovedPageRatings()) {
165
            $sqlQuery->setWhere("\"ParentID\" = ".$this->owner->ID." AND \"PageRating\".\"IsApproved\" = 1");
166
        } else {
167
            $sqlQuery->setWhere("\"ParentID\" = ".$this->owner->ID."");
168
        }
169
        $sqlQuery->setOrderBy("RatingAverage DESC");
170
        $sqlQuery->setGroupby("\"ParentID\"");
171
        $sqlQuery->setLimit(1);
172
        return $this->turnPageRaterSQLIntoArrayList($sqlQuery, "PageRatingResults");
173
    }
174
175
    /**
176
     * rating of this page by this user ...
177
     * @return ArrayList
178
     */
179 View Code Duplication
    public function CurrentUserRating()
180
    {
181
        $sqlQuery = new SQLQuery();
182
        $sqlQuery->setSelect("AVG(\"PageRating\".\"Rating\") RatingAverage, ParentID");
183
        $sqlQuery->setFrom("\"PageRating\" ");
184
        if ($this->onlyShowApprovedPageRatings()) {
185
            $sqlQuery->setWhere("\"ParentID\" = ".$this->owner->ID." AND \"PageRating\".\"ID\" = '".Session::get('PageRated'.$this->owner->ID)."' AND \"PageRating\".\"IsApproved\" = 1");
186
        } else {
187
            $sqlQuery->setWhere("\"ParentID\" = ".$this->owner->ID." AND \"PageRating\".\"ID\" = '".Session::get('PageRated'.$this->owner->ID)."'");
188
        }
189
190
        $sqlQuery->setOrderBy("RatingAverage DESC");
191
        $sqlQuery->setGroupby("\"ParentID\"");
192
        $sqlQuery->setLimit(1);
193
        return $this->turnPageRaterSQLIntoArrayList($sqlQuery, "CurrentUserRating");
194
    }
195
196
197
    /**
198
     * @param $data $sqlQuery | DataList
199
     * @param string $method
200
     *
201
     * @return ArrayList
202
     */
203 View Code Duplication
    public function turnPageRaterSQLIntoArrayList($data, $method = "unknown")
204
    {
205
        if ($data instanceof SQLQuery) {
206
            $data = $data->execute();
207
        }
208
        $al = new ArrayList();
209
        if ($data) {
210
            foreach ($data as $record) {
211
                if ($record instanceof PageRating) {
212
                    $record->Method = $method;
213
                } else {
214
                    $score = $record["RatingAverage"];
215
                    $parentID = $record["ParentID"];
216
                    $record = PageRating::get_star_details_as_array_data($score, $parentID, $method);
217
                }
218
                $al->push($record);
219
            }
220
        }
221
        return $al;
222
    }
223
224
    public function onlyShowApprovedPageRatings()
225
    {
226
        return Config::inst()->get("PageRaterExtension_Controller", "only_show_approved");
227
    }
228
}
229