Issues (1020)

Security Analysis    no vulnerabilities found

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.

models/Contest.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
2
/**
3
 * @property integer $activeId
4
 * @property string $recommendedCollect
5
 * @property integer $recommendedPrize
6
 * @property string $collect
7
 */
8
class Contest extends CModel
9
{
10
    const LIFETIME = 172800; //2 days
11
    const PRIZE = 500;
12
13
    const ID_ACTIVE = 'contest:active';
14
    const ID_LIST = 'contest:list:';
15
    const ID_RECOMMENDED_COLLECT = 'contest:r_collect';
16
    const ID_RECOMMENDED_PRIZE = 'contest:r_prize';
17
18
    const ACT_MISSION = 1;
19
    const ACT_DUEL = 2;
20
    const ACT_CLUB = 3;
21
22
    private $activeId;
23
    private $recommendedCollect;
24
    private $recommendedPrize;
25
    private $collect;
26
    private $collectParam;
27
    private $collectTypes = [
28
        'xp','xp_duel','xp_mission',
29
        'dollar','dollar_duel','dollar_mission'
30
        ];
31
32
    public function attributeNames()
33
    {
34
        return [];
35
    }
36
37
    public function getCollect()
0 ignored issues
show
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
38
    {
39
        if (!$this->collect) {
40
            $this->collect = Yii::app()->redis->getClient()->get(self::ID_LIST . $this->activeId . ':collect');
41
        }
42
        return $this->collect;
43
    }
44
45
    public function getActiveId()
0 ignored issues
show
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
46
    {
47
        if (!$this->activeId) {
48
            $this->activeId = Yii::app()->redis->getClient()->get(self::ID_ACTIVE);
49
        }
50
        return $this->activeId;
51
    }
52
53
    public function getRecommendedCollect()
0 ignored issues
show
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
54
    {
55 View Code Duplication
        if (!$this->recommendedCollect) {
56
            $this->recommendedCollect = Yii::app()->redis->getClient()->get(self::ID_RECOMMENDED_COLLECT);
57
            Yii::app()->redis->getClient()->del(self::ID_RECOMMENDED_COLLECT);
58
        }
59
60
        if (!in_array($this->recommendedCollect, $this->collectTypes)) {
61
            $randomId = array_rand($this->collectTypes);
62
            $this->recommendedCollect = $this->collectTypes[$randomId];
63
        }
64
65
        return $this->recommendedCollect;
66
    }
67
68
    public function getRecommendedPrize()
69
    {
70 View Code Duplication
        if (!$this->recommendedPrize) {
71
            $this->recommendedPrize = Yii::app()->redis->getClient()->get(self::ID_RECOMMENDED_PRIZE);
72
            Yii::app()->redis->getClient()->del(self::ID_RECOMMENDED_PRIZE);
73
        }
74
75
        if (!$this->recommendedPrize) {
76
            $this->recommendedPrize = self::PRIZE;
77
        }
78
79
        return (int)$this->recommendedPrize;
80
    }
81
82
    public function create()
83
    {
84
        if ($this->activeId) {
85
            return false;
86
        }
87
88
        $redis = Yii::app()->redis->getClient();
89
90
        $this->activeId = time();
91
92
        $redis->set('contest:active', $this->activeId);
93
        $redis->lPush('contest:log', $this->activeId); //log of contests
94
95
        $redis->set(self::ID_LIST . $this->activeId.':collect', $this->getRecommendedCollect());
96
        $redis->set(self::ID_LIST . $this->activeId.':prize', $this->getRecommendedPrize());
97
        $redis->set(self::ID_LIST . $this->activeId.':created', date('Y.m.d. H:i:s', $this->getActiveId()));
98
        return true;
99
    }
100
101
    /**
102
     * @param integer $energy
103
     */
104
    public function addPoints($uid, $activity, $energy, $xp, $dollar)
105
    {
106
        //todo: log common collecting
107
108
        if (!$this->getActiveId()) {
109
            return false; //no active contest
110
        }
111
        if (!$this->validateContest($activity, $xp, $dollar)) {
112
            return false;
113
        }
114
115
        //collect
116
        $points = 0;
117
        if ($this->collectParam == 'xp') {
118
            $points = $xp;
119
        }
120
121
        if ($this->collectParam == 'dollar') {
122
            $points = $dollar;
123
        }
124
125
        Yii::app()->redis->getClient()->zIncrBy(self::ID_LIST . $this->getActiveId() . ':points', $points, $uid);
126
        return true;
127
    }
128
129
    public function complete()
0 ignored issues
show
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
130
    {
131
        $redis = Yii::app()->redis->getClient();
132
133
        $completed = $redis->del(self::ID_ACTIVE);
134
        if ($completed) {
135
            $this->logWinners();
136
            $this->activeId = 0;
137
        }
138
139
        return $completed;
140
    }
141
142
    private function logWinners()
143
    {
144
        $winners = $this->fetchWinners();
145
        if (!$winners) {
146
            return false;
147
        }
148
149
        $redis = Yii::app()->redis->getClient();
150
        $b = Yii::app()->badge->model;
151
        foreach ($winners as $winner) {
152
            //add badges
153
            $b->uid = $winner;
154
            $b->triggerSimple('win_contest');
155
156
            //add winner to the log
157
            $redis->sadd(self::ID_LIST . $this->activeId . ':winners', $winner);
158
        }
159
    }
160
161
    private function fetchWinners()
0 ignored issues
show
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
162
    {
163
        $redis = Yii::app()->redis->getClient();
164
        //get max point
165
        $key = self::ID_LIST . $this->activeId . ':points';
166
        $max = $redis->zRevRange($key, 0, 0, true);
167
168
        if (count($max)) {
169
            $maxScore = array_values($max)[0];
170
            $winners = $redis->zRevRangeByScore($key, $maxScore, $maxScore);
171
            return $winners;
172
        }
173
        return false;
174
    }
175
176
    public function validateContest($activity, $xp, $dollar)
177
    {
178
        $toCollect = $this->getCollect();
179
180
        //check the lifetime
181
        if (time() > $this->activeId + self::LIFETIME) {
182
            return false;
183
        }
184
185
        $valid = true;
186
        //check activity+toCollect
187
        switch ($toCollect) {
188
            case 'xp':
189
                if (!$xp) {
190
                    $valid = false;
191
                }
192
                $this->collectParam = 'xp';
193
                break;
194
            case 'xp_duel':
195
                if (!$xp || $activity != self::ACT_DUEL) {
196
                    $valid = false;
197
                }
198
                $this->collectParam = 'xp';
199
                break;
200
            case 'xp_mission':
201
                if (!$xp || $activity != self::ACT_MISSION) {
202
                    $valid = false;
203
                }
204
                $this->collectParam = 'xp';
205
                break;
206
            case 'dollar':
207
                if (!$dollar) {
208
                    $valid = false;
209
                }
210
                $this->collectParam = 'dollar';
211
                break;
212
            case 'dollar_duel':
213
                if (!$dollar || $activity != self::ACT_DUEL) {
214
                    $valid = false;
215
                }
216
                $this->collectParam = 'dollar';
217
                break;
218
            case 'dollar_mission':
219
                if (!$dollar || $activity != self::ACT_MISSION) {
220
                    $valid = false;
221
                }
222
                $this->collectParam = 'dollar';
223
                break;
224
        }
225
226
        return $valid;
227
    }
228
}
229