Completed
Pull Request — newinternal (#418)
by Matthew
04:57
created
includes/DataObjects/GeoLocation.php 1 patch
Indentation   +99 added lines, -99 removed lines patch added patch discarded remove patch
@@ -21,110 +21,110 @@
 block discarded – undo
21 21
  */
22 22
 class GeoLocation extends DataObject
23 23
 {
24
-    private $address;
25
-    private $data;
26
-    private $creation;
27
-
28
-    /**
29
-     * @param string      $address
30
-     * @param PdoDatabase $database
31
-     *
32
-     * @return GeoLocation
33
-     */
34
-    public static function getByAddress($address, PdoDatabase $database)
35
-    {
36
-        $statement = $database->prepare("SELECT * FROM geolocation WHERE address = :id LIMIT 1;");
37
-        $statement->bindValue(":id", $address);
38
-
39
-        $statement->execute();
40
-
41
-        $resultObject = $statement->fetchObject(get_called_class());
42
-
43
-        if ($resultObject != false) {
44
-            $resultObject->setDatabase($database);
45
-        }
46
-
47
-        return $resultObject;
48
-    }
49
-
50
-    public function save()
51
-    {
52
-        if ($this->isNew()) {
53
-            // insert
54
-            $statement = $this->dbObject->prepare(<<<SQL
24
+	private $address;
25
+	private $data;
26
+	private $creation;
27
+
28
+	/**
29
+	 * @param string      $address
30
+	 * @param PdoDatabase $database
31
+	 *
32
+	 * @return GeoLocation
33
+	 */
34
+	public static function getByAddress($address, PdoDatabase $database)
35
+	{
36
+		$statement = $database->prepare("SELECT * FROM geolocation WHERE address = :id LIMIT 1;");
37
+		$statement->bindValue(":id", $address);
38
+
39
+		$statement->execute();
40
+
41
+		$resultObject = $statement->fetchObject(get_called_class());
42
+
43
+		if ($resultObject != false) {
44
+			$resultObject->setDatabase($database);
45
+		}
46
+
47
+		return $resultObject;
48
+	}
49
+
50
+	public function save()
51
+	{
52
+		if ($this->isNew()) {
53
+			// insert
54
+			$statement = $this->dbObject->prepare(<<<SQL
55 55
 INSERT INTO `geolocation` (address, data) VALUES (:address, :data);
56 56
 SQL
57
-            );
58
-            $statement->bindValue(":address", $this->address);
59
-            $statement->bindValue(":data", $this->data);
60
-
61
-            if ($statement->execute()) {
62
-                $this->id = (int)$this->dbObject->lastInsertId();
63
-            }
64
-            else {
65
-                throw new Exception($statement->errorInfo());
66
-            }
67
-        }
68
-        else {
69
-            // update
70
-            $statement = $this->dbObject->prepare(<<<SQL
57
+			);
58
+			$statement->bindValue(":address", $this->address);
59
+			$statement->bindValue(":data", $this->data);
60
+
61
+			if ($statement->execute()) {
62
+				$this->id = (int)$this->dbObject->lastInsertId();
63
+			}
64
+			else {
65
+				throw new Exception($statement->errorInfo());
66
+			}
67
+		}
68
+		else {
69
+			// update
70
+			$statement = $this->dbObject->prepare(<<<SQL
71 71
 UPDATE `geolocation`
72 72
 SET address = :address, data = :data, updateversion = updateversion + 1
73 73
 WHERE id = :id AND updateversion = :updateversion
74 74
 LIMIT 1;
75 75
 SQL
76
-            );
77
-
78
-            $statement->bindValue(":id", $this->id);
79
-            $statement->bindValue(":updateversion", $this->updateversion);
80
-
81
-            $statement->bindValue(":address", $this->address);
82
-            $statement->bindValue(":data", $this->data);
83
-
84
-            if (!$statement->execute()) {
85
-                throw new Exception($statement->errorInfo());
86
-            }
87
-
88
-            if ($statement->rowCount() !== 1) {
89
-                throw new OptimisticLockFailedException();
90
-            }
91
-
92
-            $this->updateversion++;
93
-        }
94
-    }
95
-
96
-    public function getAddress()
97
-    {
98
-        return $this->address;
99
-    }
100
-
101
-    /**
102
-     * @param string $address
103
-     */
104
-    public function setAddress($address)
105
-    {
106
-        $this->address = $address;
107
-    }
108
-
109
-    /**
110
-     * @return array
111
-     */
112
-    public function getData()
113
-    {
114
-        return unserialize($this->data);
115
-    }
116
-
117
-    /**
118
-     * @param array $data
119
-     */
120
-    public function setData($data)
121
-    {
122
-        $this->data = serialize($data);
123
-    }
124
-
125
-    /** @return DateTimeImmutable */
126
-    public function getCreation()
127
-    {
128
-        return new DateTimeImmutable($this->creation);
129
-    }
76
+			);
77
+
78
+			$statement->bindValue(":id", $this->id);
79
+			$statement->bindValue(":updateversion", $this->updateversion);
80
+
81
+			$statement->bindValue(":address", $this->address);
82
+			$statement->bindValue(":data", $this->data);
83
+
84
+			if (!$statement->execute()) {
85
+				throw new Exception($statement->errorInfo());
86
+			}
87
+
88
+			if ($statement->rowCount() !== 1) {
89
+				throw new OptimisticLockFailedException();
90
+			}
91
+
92
+			$this->updateversion++;
93
+		}
94
+	}
95
+
96
+	public function getAddress()
97
+	{
98
+		return $this->address;
99
+	}
100
+
101
+	/**
102
+	 * @param string $address
103
+	 */
104
+	public function setAddress($address)
105
+	{
106
+		$this->address = $address;
107
+	}
108
+
109
+	/**
110
+	 * @return array
111
+	 */
112
+	public function getData()
113
+	{
114
+		return unserialize($this->data);
115
+	}
116
+
117
+	/**
118
+	 * @param array $data
119
+	 */
120
+	public function setData($data)
121
+	{
122
+		$this->data = serialize($data);
123
+	}
124
+
125
+	/** @return DateTimeImmutable */
126
+	public function getCreation()
127
+	{
128
+		return new DateTimeImmutable($this->creation);
129
+	}
130 130
 }
Please login to merge, or discard this patch.
includes/DataObjects/Comment.php 1 patch
Indentation   +158 added lines, -158 removed lines patch added patch discarded remove patch
@@ -20,173 +20,173 @@
 block discarded – undo
20 20
  */
21 21
 class Comment extends DataObject
22 22
 {
23
-    private $time;
24
-    private $user;
25
-    private $comment;
26
-    private $visibility = "user";
27
-    private $request;
28
-
29
-    /**
30
-     * @param integer     $id
31
-     * @param PdoDatabase $database
32
-     *
33
-     * @return Comment[]
34
-     * @throws Exception
35
-     */
36
-    public static function getForRequest($id, PdoDatabase $database)
37
-    {
38
-        $currentUser = User::getCurrent($database);
39
-
40
-        if ($currentUser->isAdmin() || $currentUser->isCheckuser()) {
41
-            // current user is an admin or checkuser, so retrieve everything.
42
-            $statement = $database->prepare("SELECT * FROM comment WHERE request = :target;");
43
-        }
44
-        else {
45
-            // current user isn't an admin, so limit to only those which are visible to users, and private comments
46
-            // the user has posted themselves.
47
-            $statement = $database->prepare(<<<SQL
23
+	private $time;
24
+	private $user;
25
+	private $comment;
26
+	private $visibility = "user";
27
+	private $request;
28
+
29
+	/**
30
+	 * @param integer     $id
31
+	 * @param PdoDatabase $database
32
+	 *
33
+	 * @return Comment[]
34
+	 * @throws Exception
35
+	 */
36
+	public static function getForRequest($id, PdoDatabase $database)
37
+	{
38
+		$currentUser = User::getCurrent($database);
39
+
40
+		if ($currentUser->isAdmin() || $currentUser->isCheckuser()) {
41
+			// current user is an admin or checkuser, so retrieve everything.
42
+			$statement = $database->prepare("SELECT * FROM comment WHERE request = :target;");
43
+		}
44
+		else {
45
+			// current user isn't an admin, so limit to only those which are visible to users, and private comments
46
+			// the user has posted themselves.
47
+			$statement = $database->prepare(<<<SQL
48 48
 SELECT * FROM comment
49 49
 WHERE request = :target AND (visibility = 'user' OR user = :userid);
50 50
 SQL
51
-            );
52
-            $statement->bindValue(":userid", $currentUser->getId());
53
-        }
54
-
55
-        $statement->bindValue(":target", $id);
56
-
57
-        $statement->execute();
58
-
59
-        $result = array();
60
-        /** @var Comment $v */
61
-        foreach ($statement->fetchAll(PDO::FETCH_CLASS, get_called_class()) as $v) {
62
-            $v->setDatabase($database);
63
-            $result[] = $v;
64
-        }
65
-
66
-        return $result;
67
-    }
68
-
69
-    /**
70
-     * @throws Exception
71
-     */
72
-    public function save()
73
-    {
74
-        if ($this->isNew()) {
75
-            // insert
76
-            $statement = $this->dbObject->prepare(<<<SQL
51
+			);
52
+			$statement->bindValue(":userid", $currentUser->getId());
53
+		}
54
+
55
+		$statement->bindValue(":target", $id);
56
+
57
+		$statement->execute();
58
+
59
+		$result = array();
60
+		/** @var Comment $v */
61
+		foreach ($statement->fetchAll(PDO::FETCH_CLASS, get_called_class()) as $v) {
62
+			$v->setDatabase($database);
63
+			$result[] = $v;
64
+		}
65
+
66
+		return $result;
67
+	}
68
+
69
+	/**
70
+	 * @throws Exception
71
+	 */
72
+	public function save()
73
+	{
74
+		if ($this->isNew()) {
75
+			// insert
76
+			$statement = $this->dbObject->prepare(<<<SQL
77 77
 INSERT INTO comment ( time, user, comment, visibility, request )
78 78
 VALUES ( CURRENT_TIMESTAMP(), :user, :comment, :visibility, :request );
79 79
 SQL
80
-            );
81
-            $statement->bindValue(":user", $this->user);
82
-            $statement->bindValue(":comment", $this->comment);
83
-            $statement->bindValue(":visibility", $this->visibility);
84
-            $statement->bindValue(":request", $this->request);
85
-
86
-            if ($statement->execute()) {
87
-                $this->id = (int)$this->dbObject->lastInsertId();
88
-            }
89
-            else {
90
-                throw new Exception($statement->errorInfo());
91
-            }
92
-        }
93
-        else {
94
-            // update
95
-            $statement = $this->dbObject->prepare(<<<SQL
80
+			);
81
+			$statement->bindValue(":user", $this->user);
82
+			$statement->bindValue(":comment", $this->comment);
83
+			$statement->bindValue(":visibility", $this->visibility);
84
+			$statement->bindValue(":request", $this->request);
85
+
86
+			if ($statement->execute()) {
87
+				$this->id = (int)$this->dbObject->lastInsertId();
88
+			}
89
+			else {
90
+				throw new Exception($statement->errorInfo());
91
+			}
92
+		}
93
+		else {
94
+			// update
95
+			$statement = $this->dbObject->prepare(<<<SQL
96 96
 UPDATE comment
97 97
 SET comment = :comment, visibility = :visibility, updateversion = updateversion + 1
98 98
 WHERE id = :id AND updateversion = :updateversion
99 99
 LIMIT 1;
100 100
 SQL
101
-            );
102
-
103
-            $statement->bindValue(':id', $this->id);
104
-            $statement->bindValue(':updateversion', $this->updateversion);
105
-
106
-            $statement->bindValue(':comment', $this->comment);
107
-            $statement->bindValue(':visibility', $this->visibility);
108
-
109
-            if (!$statement->execute()) {
110
-                throw new Exception($statement->errorInfo());
111
-            }
112
-
113
-            if ($statement->rowCount() !== 1) {
114
-                throw new OptimisticLockFailedException();
115
-            }
116
-
117
-            $this->updateversion++;
118
-        }
119
-    }
120
-
121
-    /**
122
-     * @return DateTimeImmutable
123
-     */
124
-    public function getTime()
125
-    {
126
-        return new DateTimeImmutable($this->time);
127
-    }
128
-
129
-    /**
130
-     * @return int
131
-     */
132
-    public function getUser()
133
-    {
134
-        return $this->user;
135
-    }
136
-
137
-    /**
138
-     * @param int $user
139
-     */
140
-    public function setUser($user)
141
-    {
142
-        $this->user = $user;
143
-    }
144
-
145
-    /**
146
-     * @return string
147
-     */
148
-    public function getComment()
149
-    {
150
-        return $this->comment;
151
-    }
152
-
153
-    /**
154
-     * @param string $comment
155
-     */
156
-    public function setComment($comment)
157
-    {
158
-        $this->comment = $comment;
159
-    }
160
-
161
-    /**
162
-     * @return string
163
-     */
164
-    public function getVisibility()
165
-    {
166
-        return $this->visibility;
167
-    }
168
-
169
-    /**
170
-     * @param string $visibility
171
-     */
172
-    public function setVisibility($visibility)
173
-    {
174
-        $this->visibility = $visibility;
175
-    }
176
-
177
-    /**
178
-     * @return int
179
-     */
180
-    public function getRequest()
181
-    {
182
-        return $this->request;
183
-    }
184
-
185
-    /**
186
-     * @param int $request
187
-     */
188
-    public function setRequest($request)
189
-    {
190
-        $this->request = $request;
191
-    }
101
+			);
102
+
103
+			$statement->bindValue(':id', $this->id);
104
+			$statement->bindValue(':updateversion', $this->updateversion);
105
+
106
+			$statement->bindValue(':comment', $this->comment);
107
+			$statement->bindValue(':visibility', $this->visibility);
108
+
109
+			if (!$statement->execute()) {
110
+				throw new Exception($statement->errorInfo());
111
+			}
112
+
113
+			if ($statement->rowCount() !== 1) {
114
+				throw new OptimisticLockFailedException();
115
+			}
116
+
117
+			$this->updateversion++;
118
+		}
119
+	}
120
+
121
+	/**
122
+	 * @return DateTimeImmutable
123
+	 */
124
+	public function getTime()
125
+	{
126
+		return new DateTimeImmutable($this->time);
127
+	}
128
+
129
+	/**
130
+	 * @return int
131
+	 */
132
+	public function getUser()
133
+	{
134
+		return $this->user;
135
+	}
136
+
137
+	/**
138
+	 * @param int $user
139
+	 */
140
+	public function setUser($user)
141
+	{
142
+		$this->user = $user;
143
+	}
144
+
145
+	/**
146
+	 * @return string
147
+	 */
148
+	public function getComment()
149
+	{
150
+		return $this->comment;
151
+	}
152
+
153
+	/**
154
+	 * @param string $comment
155
+	 */
156
+	public function setComment($comment)
157
+	{
158
+		$this->comment = $comment;
159
+	}
160
+
161
+	/**
162
+	 * @return string
163
+	 */
164
+	public function getVisibility()
165
+	{
166
+		return $this->visibility;
167
+	}
168
+
169
+	/**
170
+	 * @param string $visibility
171
+	 */
172
+	public function setVisibility($visibility)
173
+	{
174
+		$this->visibility = $visibility;
175
+	}
176
+
177
+	/**
178
+	 * @return int
179
+	 */
180
+	public function getRequest()
181
+	{
182
+		return $this->request;
183
+	}
184
+
185
+	/**
186
+	 * @param int $request
187
+	 */
188
+	public function setRequest($request)
189
+	{
190
+		$this->request = $request;
191
+	}
192 192
 }
Please login to merge, or discard this patch.
includes/DataObjects/User.php 2 patches
Indentation   +1128 added lines, -1128 removed lines patch added patch discarded remove patch
@@ -27,285 +27,285 @@  discard block
 block discarded – undo
27 27
  */
28 28
 class User extends DataObject
29 29
 {
30
-    const STATUS_USER = 'User';
31
-    const STATUS_ADMIN = 'Admin';
32
-    const STATUS_SUSPENDED = 'Suspended';
33
-    const STATUS_DECLINED = 'Declined';
34
-    const STATUS_NEW = 'New';
35
-    private $username;
36
-    private $email;
37
-    private $password;
38
-    private $status = self::STATUS_NEW;
39
-    private $onwikiname = "##OAUTH##";
40
-    private $welcome_sig = "";
41
-    private $lastactive = "0000-00-00 00:00:00";
42
-    private $forcelogout = 0;
43
-    private $checkuser = 0;
44
-    private $forceidentified = null;
45
-    private $welcome_template = 0;
46
-    private $abortpref = 0;
47
-    private $confirmationdiff = 0;
48
-    private $emailsig = "";
49
-    /** @var null|string */
50
-    private $oauthrequesttoken = null;
51
-    /** @var null|string */
52
-    private $oauthrequestsecret = null;
53
-    /** @var null|string */
54
-    private $oauthaccesstoken = null;
55
-    /** @var null|string */
56
-    private $oauthaccesssecret = null;
57
-    private $oauthidentitycache = null;
58
-    /** @var User Cache variable of the current user - it's never going to change in the middle of a request. */
59
-    private static $currentUser;
60
-    /** @var null|JWT The identity cache */
61
-    private $identityCache = null;
62
-    #region Object load methods
63
-
64
-    /**
65
-     * Gets the currently logged in user
66
-     *
67
-     * @param PdoDatabase $database
68
-     *
69
-     * @return User|CommunityUser
70
-     */
71
-    public static function getCurrent(PdoDatabase $database)
72
-    {
73
-        if (self::$currentUser === null) {
74
-            $sessionId = WebRequest::getSessionUserId();
75
-
76
-            if ($sessionId !== null) {
77
-                /** @var User $user */
78
-                $user = self::getById($sessionId, $database);
79
-
80
-                if ($user === false) {
81
-                    self::$currentUser = new CommunityUser();
82
-                }
83
-                else {
84
-                    self::$currentUser = $user;
85
-                }
86
-            }
87
-            else {
88
-                $anonymousCoward = new CommunityUser();
89
-
90
-                self::$currentUser = $anonymousCoward;
91
-            }
92
-        }
93
-
94
-        return self::$currentUser;
95
-    }
96
-
97
-    /**
98
-     * Gets a user by their user ID
99
-     *
100
-     * Pass -1 to get the community user.
101
-     *
102
-     * @param int|null    $id
103
-     * @param PdoDatabase $database
104
-     *
105
-     * @return User|false
106
-     */
107
-    public static function getById($id, PdoDatabase $database)
108
-    {
109
-        if ($id === null || $id == -1) {
110
-            return new CommunityUser();
111
-        }
112
-
113
-        /** @var User|false $user */
114
-        $user = parent::getById($id, $database);
115
-
116
-        return $user;
117
-    }
118
-
119
-    /**
120
-     * @return CommunityUser
121
-     */
122
-    public static function getCommunity()
123
-    {
124
-        return new CommunityUser();
125
-    }
126
-
127
-    /**
128
-     * Gets a user by their username
129
-     *
130
-     * @param  string      $username
131
-     * @param  PdoDatabase $database
132
-     *
133
-     * @return CommunityUser|User|false
134
-     */
135
-    public static function getByUsername($username, PdoDatabase $database)
136
-    {
137
-        global $communityUsername;
138
-        if ($username == $communityUsername) {
139
-            return new CommunityUser();
140
-        }
141
-
142
-        $statement = $database->prepare("SELECT * FROM user WHERE username = :id LIMIT 1;");
143
-        $statement->bindValue(":id", $username);
144
-
145
-        $statement->execute();
146
-
147
-        $resultObject = $statement->fetchObject(get_called_class());
148
-
149
-        if ($resultObject != false) {
150
-            $resultObject->setDatabase($database);
151
-        }
152
-
153
-        return $resultObject;
154
-    }
155
-
156
-    /**
157
-     * Gets a user by their on-wiki username.
158
-     *
159
-     * Don't use without asking me first. It's really inefficient in it's current implementation.
160
-     * We need to restructure the user table again to make this more efficient.
161
-     * We don't actually store the on-wiki name in the table any more, instead we
162
-     * are storing JSON in a column (!!). Yep, my fault. Code review is an awesome thing.
163
-     *            -- stw 2015-10-20
164
-     *
165
-     * @param string      $username
166
-     * @param PdoDatabase $database
167
-     *
168
-     * @return User|false
169
-     */
170
-    public static function getByOnWikiUsername($username, PdoDatabase $database)
171
-    {
172
-        // Firstly, try to search by the efficient database lookup.
173
-        $statement = $database->prepare("SELECT * FROM user WHERE onwikiname = :id LIMIT 1;");
174
-        $statement->bindValue(":id", $username);
175
-        $statement->execute();
176
-
177
-        $resultObject = $statement->fetchObject(get_called_class());
178
-
179
-        if ($resultObject != false) {
180
-            $resultObject->setDatabase($database);
181
-
182
-            return $resultObject;
183
-        }
184
-
185
-        // For active users, the above has failed. Let's do it the hard way.
186
-        $sqlStatement = "SELECT * FROM user WHERE onwikiname = '##OAUTH##' AND oauthaccesstoken IS NOT NULL;";
187
-        $statement = $database->prepare($sqlStatement);
188
-        $statement->execute();
189
-        $resultSet = $statement->fetchAll(PDO::FETCH_CLASS, get_called_class());
190
-
191
-        /** @var User $user */
192
-        foreach ($resultSet as $user) {
193
-            // We have to set this before doing OAuth queries. :(
194
-            $user->setDatabase($database);
195
-
196
-            // Using cached data here!
197
-            if ($user->getOAuthOnWikiName(true) == $username) {
198
-                // Success.
199
-                return $user;
200
-            }
201
-        }
202
-
203
-        // Cached data failed. Let's do it the *REALLY* hard way.
204
-        foreach ($resultSet as $user) {
205
-            // We have to set this before doing OAuth queries. :(
206
-            $user->setDatabase($database);
207
-
208
-            // Don't use the cached data, but instead query the API.
209
-            if ($user->getOAuthOnWikiName(false) == $username) {
210
-                // Success.
211
-                return $user;
212
-            }
213
-        }
214
-
215
-        // Nope. Sorry.
216
-        return false;
217
-    }
218
-
219
-    /**
220
-     * Gets a user by their OAuth request token
221
-     *
222
-     * @param string      $requestToken
223
-     * @param PdoDatabase $database
224
-     *
225
-     * @return User|false
226
-     */
227
-    public static function getByRequestToken($requestToken, PdoDatabase $database)
228
-    {
229
-        $statement = $database->prepare("SELECT * FROM user WHERE oauthrequesttoken = :id LIMIT 1;");
230
-        $statement->bindValue(":id", $requestToken);
231
-
232
-        $statement->execute();
233
-
234
-        $resultObject = $statement->fetchObject(get_called_class());
235
-
236
-        if ($resultObject != false) {
237
-            $resultObject->setDatabase($database);
238
-        }
239
-
240
-        return $resultObject;
241
-    }
242
-
243
-    /**
244
-     * Gets all users with a supplied status
245
-     *
246
-     * @param string      $status
247
-     * @param PdoDatabase $database
248
-     *
249
-     * @return User[]
250
-     */
251
-    public static function getAllWithStatus($status, PdoDatabase $database)
252
-    {
253
-        $statement = $database->prepare("SELECT * FROM user WHERE status = :status");
254
-        $statement->execute(array(":status" => $status));
255
-
256
-        $resultObject = $statement->fetchAll(PDO::FETCH_CLASS, get_called_class());
257
-
258
-        /** @var User $u */
259
-        foreach ($resultObject as $u) {
260
-            $u->setDatabase($database);
261
-        }
262
-
263
-        return $resultObject;
264
-    }
265
-
266
-    /**
267
-     * Gets all checkusers
268
-     *
269
-     * @param PdoDatabase $database
270
-     *
271
-     * @return User[]
272
-     */
273
-    public static function getAllCheckusers(PdoDatabase $database)
274
-    {
275
-        $statement = $database->prepare("SELECT * FROM user WHERE checkuser = 1;");
276
-        $statement->execute();
277
-
278
-        $resultObject = $statement->fetchAll(PDO::FETCH_CLASS, get_called_class());
279
-
280
-        $resultsCollection = array();
281
-
282
-        /** @var User $u */
283
-        foreach ($resultObject as $u) {
284
-            $u->setDatabase($database);
285
-
286
-            if (!$u->isCheckuser()) {
287
-                continue;
288
-            }
289
-
290
-            $resultsCollection[] = $u;
291
-        }
292
-
293
-        return $resultsCollection;
294
-    }
295
-
296
-    /**
297
-     * Gets all inactive users
298
-     *
299
-     * @param PdoDatabase $database
300
-     *
301
-     * @return User[]
302
-     */
303
-    public static function getAllInactive(PdoDatabase $database)
304
-    {
305
-        $date = new DateTime();
306
-        $date->modify("-45 days");
307
-
308
-        $statement = $database->prepare(<<<SQL
30
+	const STATUS_USER = 'User';
31
+	const STATUS_ADMIN = 'Admin';
32
+	const STATUS_SUSPENDED = 'Suspended';
33
+	const STATUS_DECLINED = 'Declined';
34
+	const STATUS_NEW = 'New';
35
+	private $username;
36
+	private $email;
37
+	private $password;
38
+	private $status = self::STATUS_NEW;
39
+	private $onwikiname = "##OAUTH##";
40
+	private $welcome_sig = "";
41
+	private $lastactive = "0000-00-00 00:00:00";
42
+	private $forcelogout = 0;
43
+	private $checkuser = 0;
44
+	private $forceidentified = null;
45
+	private $welcome_template = 0;
46
+	private $abortpref = 0;
47
+	private $confirmationdiff = 0;
48
+	private $emailsig = "";
49
+	/** @var null|string */
50
+	private $oauthrequesttoken = null;
51
+	/** @var null|string */
52
+	private $oauthrequestsecret = null;
53
+	/** @var null|string */
54
+	private $oauthaccesstoken = null;
55
+	/** @var null|string */
56
+	private $oauthaccesssecret = null;
57
+	private $oauthidentitycache = null;
58
+	/** @var User Cache variable of the current user - it's never going to change in the middle of a request. */
59
+	private static $currentUser;
60
+	/** @var null|JWT The identity cache */
61
+	private $identityCache = null;
62
+	#region Object load methods
63
+
64
+	/**
65
+	 * Gets the currently logged in user
66
+	 *
67
+	 * @param PdoDatabase $database
68
+	 *
69
+	 * @return User|CommunityUser
70
+	 */
71
+	public static function getCurrent(PdoDatabase $database)
72
+	{
73
+		if (self::$currentUser === null) {
74
+			$sessionId = WebRequest::getSessionUserId();
75
+
76
+			if ($sessionId !== null) {
77
+				/** @var User $user */
78
+				$user = self::getById($sessionId, $database);
79
+
80
+				if ($user === false) {
81
+					self::$currentUser = new CommunityUser();
82
+				}
83
+				else {
84
+					self::$currentUser = $user;
85
+				}
86
+			}
87
+			else {
88
+				$anonymousCoward = new CommunityUser();
89
+
90
+				self::$currentUser = $anonymousCoward;
91
+			}
92
+		}
93
+
94
+		return self::$currentUser;
95
+	}
96
+
97
+	/**
98
+	 * Gets a user by their user ID
99
+	 *
100
+	 * Pass -1 to get the community user.
101
+	 *
102
+	 * @param int|null    $id
103
+	 * @param PdoDatabase $database
104
+	 *
105
+	 * @return User|false
106
+	 */
107
+	public static function getById($id, PdoDatabase $database)
108
+	{
109
+		if ($id === null || $id == -1) {
110
+			return new CommunityUser();
111
+		}
112
+
113
+		/** @var User|false $user */
114
+		$user = parent::getById($id, $database);
115
+
116
+		return $user;
117
+	}
118
+
119
+	/**
120
+	 * @return CommunityUser
121
+	 */
122
+	public static function getCommunity()
123
+	{
124
+		return new CommunityUser();
125
+	}
126
+
127
+	/**
128
+	 * Gets a user by their username
129
+	 *
130
+	 * @param  string      $username
131
+	 * @param  PdoDatabase $database
132
+	 *
133
+	 * @return CommunityUser|User|false
134
+	 */
135
+	public static function getByUsername($username, PdoDatabase $database)
136
+	{
137
+		global $communityUsername;
138
+		if ($username == $communityUsername) {
139
+			return new CommunityUser();
140
+		}
141
+
142
+		$statement = $database->prepare("SELECT * FROM user WHERE username = :id LIMIT 1;");
143
+		$statement->bindValue(":id", $username);
144
+
145
+		$statement->execute();
146
+
147
+		$resultObject = $statement->fetchObject(get_called_class());
148
+
149
+		if ($resultObject != false) {
150
+			$resultObject->setDatabase($database);
151
+		}
152
+
153
+		return $resultObject;
154
+	}
155
+
156
+	/**
157
+	 * Gets a user by their on-wiki username.
158
+	 *
159
+	 * Don't use without asking me first. It's really inefficient in it's current implementation.
160
+	 * We need to restructure the user table again to make this more efficient.
161
+	 * We don't actually store the on-wiki name in the table any more, instead we
162
+	 * are storing JSON in a column (!!). Yep, my fault. Code review is an awesome thing.
163
+	 *            -- stw 2015-10-20
164
+	 *
165
+	 * @param string      $username
166
+	 * @param PdoDatabase $database
167
+	 *
168
+	 * @return User|false
169
+	 */
170
+	public static function getByOnWikiUsername($username, PdoDatabase $database)
171
+	{
172
+		// Firstly, try to search by the efficient database lookup.
173
+		$statement = $database->prepare("SELECT * FROM user WHERE onwikiname = :id LIMIT 1;");
174
+		$statement->bindValue(":id", $username);
175
+		$statement->execute();
176
+
177
+		$resultObject = $statement->fetchObject(get_called_class());
178
+
179
+		if ($resultObject != false) {
180
+			$resultObject->setDatabase($database);
181
+
182
+			return $resultObject;
183
+		}
184
+
185
+		// For active users, the above has failed. Let's do it the hard way.
186
+		$sqlStatement = "SELECT * FROM user WHERE onwikiname = '##OAUTH##' AND oauthaccesstoken IS NOT NULL;";
187
+		$statement = $database->prepare($sqlStatement);
188
+		$statement->execute();
189
+		$resultSet = $statement->fetchAll(PDO::FETCH_CLASS, get_called_class());
190
+
191
+		/** @var User $user */
192
+		foreach ($resultSet as $user) {
193
+			// We have to set this before doing OAuth queries. :(
194
+			$user->setDatabase($database);
195
+
196
+			// Using cached data here!
197
+			if ($user->getOAuthOnWikiName(true) == $username) {
198
+				// Success.
199
+				return $user;
200
+			}
201
+		}
202
+
203
+		// Cached data failed. Let's do it the *REALLY* hard way.
204
+		foreach ($resultSet as $user) {
205
+			// We have to set this before doing OAuth queries. :(
206
+			$user->setDatabase($database);
207
+
208
+			// Don't use the cached data, but instead query the API.
209
+			if ($user->getOAuthOnWikiName(false) == $username) {
210
+				// Success.
211
+				return $user;
212
+			}
213
+		}
214
+
215
+		// Nope. Sorry.
216
+		return false;
217
+	}
218
+
219
+	/**
220
+	 * Gets a user by their OAuth request token
221
+	 *
222
+	 * @param string      $requestToken
223
+	 * @param PdoDatabase $database
224
+	 *
225
+	 * @return User|false
226
+	 */
227
+	public static function getByRequestToken($requestToken, PdoDatabase $database)
228
+	{
229
+		$statement = $database->prepare("SELECT * FROM user WHERE oauthrequesttoken = :id LIMIT 1;");
230
+		$statement->bindValue(":id", $requestToken);
231
+
232
+		$statement->execute();
233
+
234
+		$resultObject = $statement->fetchObject(get_called_class());
235
+
236
+		if ($resultObject != false) {
237
+			$resultObject->setDatabase($database);
238
+		}
239
+
240
+		return $resultObject;
241
+	}
242
+
243
+	/**
244
+	 * Gets all users with a supplied status
245
+	 *
246
+	 * @param string      $status
247
+	 * @param PdoDatabase $database
248
+	 *
249
+	 * @return User[]
250
+	 */
251
+	public static function getAllWithStatus($status, PdoDatabase $database)
252
+	{
253
+		$statement = $database->prepare("SELECT * FROM user WHERE status = :status");
254
+		$statement->execute(array(":status" => $status));
255
+
256
+		$resultObject = $statement->fetchAll(PDO::FETCH_CLASS, get_called_class());
257
+
258
+		/** @var User $u */
259
+		foreach ($resultObject as $u) {
260
+			$u->setDatabase($database);
261
+		}
262
+
263
+		return $resultObject;
264
+	}
265
+
266
+	/**
267
+	 * Gets all checkusers
268
+	 *
269
+	 * @param PdoDatabase $database
270
+	 *
271
+	 * @return User[]
272
+	 */
273
+	public static function getAllCheckusers(PdoDatabase $database)
274
+	{
275
+		$statement = $database->prepare("SELECT * FROM user WHERE checkuser = 1;");
276
+		$statement->execute();
277
+
278
+		$resultObject = $statement->fetchAll(PDO::FETCH_CLASS, get_called_class());
279
+
280
+		$resultsCollection = array();
281
+
282
+		/** @var User $u */
283
+		foreach ($resultObject as $u) {
284
+			$u->setDatabase($database);
285
+
286
+			if (!$u->isCheckuser()) {
287
+				continue;
288
+			}
289
+
290
+			$resultsCollection[] = $u;
291
+		}
292
+
293
+		return $resultsCollection;
294
+	}
295
+
296
+	/**
297
+	 * Gets all inactive users
298
+	 *
299
+	 * @param PdoDatabase $database
300
+	 *
301
+	 * @return User[]
302
+	 */
303
+	public static function getAllInactive(PdoDatabase $database)
304
+	{
305
+		$date = new DateTime();
306
+		$date->modify("-45 days");
307
+
308
+		$statement = $database->prepare(<<<SQL
309 309
 			SELECT * 
310 310
 			FROM user 
311 311
 			WHERE lastactive < :lastactivelimit 
@@ -314,104 +314,104 @@  discard block
 block discarded – undo
314 314
 				AND status != 'New' 
315 315
 			ORDER BY lastactive ASC;
316 316
 SQL
317
-        );
318
-        $statement->execute(array(":lastactivelimit" => $date->format("Y-m-d H:i:s")));
319
-
320
-        $resultObject = $statement->fetchAll(PDO::FETCH_CLASS, get_called_class());
321
-
322
-        /** @var User $u */
323
-        foreach ($resultObject as $u) {
324
-            $u->setDatabase($database);
325
-        }
326
-
327
-        return $resultObject;
328
-    }
329
-
330
-    /**
331
-     * Gets all the usernames in the system
332
-     *
333
-     * @param PdoDatabase      $database
334
-     * @param null|bool|string $filter If null, no filter. If true, active users only, otherwise provided status.
335
-     *
336
-     * @return string[]
337
-     */
338
-    public static function getAllUsernames(PdoDatabase $database, $filter = null)
339
-    {
340
-        if ($filter === null) {
341
-            $userListQuery = "SELECT username FROM user;";
342
-            $userListResult = $database->query($userListQuery);
343
-        }
344
-        elseif ($filter === true) {
345
-            $userListQuery = "SELECT username FROM user WHERE status IN ('User', 'Admin');";
346
-            $userListResult = $database->query($userListQuery);
347
-        }
348
-        else {
349
-            $userListQuery = "SELECT username FROM user WHERE status = :status;";
350
-            $userListResult = $database->prepare($userListQuery);
351
-            $userListResult->execute(array(":status" => $filter));
352
-        }
353
-
354
-        return $userListResult->fetchAll(PDO::FETCH_COLUMN);
355
-    }
356
-
357
-    /**
358
-     * @param array       $userIds
359
-     * @param PdoDatabase $database
360
-     *
361
-     * @return array
362
-     * @throws Exception
363
-     */
364
-    public static function getUsernames($userIds, PdoDatabase $database)
365
-    {
366
-        if (!is_array($userIds)) {
367
-            throw new Exception('getUsernames() expects array');
368
-        }
369
-
370
-        if (count($userIds) === 0) {
371
-            // empty set of data
372
-            return array();
373
-        }
374
-
375
-        // Urgh. OK. You can't use IN() with parameters directly, so let's munge something together.
376
-        $userCount = count($userIds);
377
-
378
-        // Firstly, let's create a string of question marks, which will do as positional parameters.
379
-        $inSection = str_repeat('?,', $userCount - 1) . '?';
380
-
381
-        // Now, let's put that into the query. Direct string building here is OK, we're not dealing with user input,
382
-        // only the *count* of user input, which is safe.
383
-        $query = "SELECT id, username FROM user WHERE id IN ({$inSection})";
384
-
385
-        // Prepare the query
386
-        $statement = $database->prepare($query);
387
-
388
-        // Bind the parameters and execute - in one go - since we already have a handy array kicking around.
389
-        $statement->execute($userIds);
390
-
391
-        $resultSet = $statement->fetchAll(PDO::FETCH_ASSOC);
392
-
393
-        $users = array();
394
-        foreach ($resultSet as $row) {
395
-            $users[$row['id']] = $row['username'];
396
-        }
397
-
398
-        $statement->closeCursor();
399
-
400
-        return $users;
401
-    }
402
-
403
-    #endregion
404
-
405
-    /**
406
-     * Saves the current object
407
-     *
408
-     * @throws Exception
409
-     */
410
-    public function save()
411
-    {
412
-        if ($this->isNew()) {
413
-            // insert
414
-            $statement = $this->dbObject->prepare(<<<SQL
317
+		);
318
+		$statement->execute(array(":lastactivelimit" => $date->format("Y-m-d H:i:s")));
319
+
320
+		$resultObject = $statement->fetchAll(PDO::FETCH_CLASS, get_called_class());
321
+
322
+		/** @var User $u */
323
+		foreach ($resultObject as $u) {
324
+			$u->setDatabase($database);
325
+		}
326
+
327
+		return $resultObject;
328
+	}
329
+
330
+	/**
331
+	 * Gets all the usernames in the system
332
+	 *
333
+	 * @param PdoDatabase      $database
334
+	 * @param null|bool|string $filter If null, no filter. If true, active users only, otherwise provided status.
335
+	 *
336
+	 * @return string[]
337
+	 */
338
+	public static function getAllUsernames(PdoDatabase $database, $filter = null)
339
+	{
340
+		if ($filter === null) {
341
+			$userListQuery = "SELECT username FROM user;";
342
+			$userListResult = $database->query($userListQuery);
343
+		}
344
+		elseif ($filter === true) {
345
+			$userListQuery = "SELECT username FROM user WHERE status IN ('User', 'Admin');";
346
+			$userListResult = $database->query($userListQuery);
347
+		}
348
+		else {
349
+			$userListQuery = "SELECT username FROM user WHERE status = :status;";
350
+			$userListResult = $database->prepare($userListQuery);
351
+			$userListResult->execute(array(":status" => $filter));
352
+		}
353
+
354
+		return $userListResult->fetchAll(PDO::FETCH_COLUMN);
355
+	}
356
+
357
+	/**
358
+	 * @param array       $userIds
359
+	 * @param PdoDatabase $database
360
+	 *
361
+	 * @return array
362
+	 * @throws Exception
363
+	 */
364
+	public static function getUsernames($userIds, PdoDatabase $database)
365
+	{
366
+		if (!is_array($userIds)) {
367
+			throw new Exception('getUsernames() expects array');
368
+		}
369
+
370
+		if (count($userIds) === 0) {
371
+			// empty set of data
372
+			return array();
373
+		}
374
+
375
+		// Urgh. OK. You can't use IN() with parameters directly, so let's munge something together.
376
+		$userCount = count($userIds);
377
+
378
+		// Firstly, let's create a string of question marks, which will do as positional parameters.
379
+		$inSection = str_repeat('?,', $userCount - 1) . '?';
380
+
381
+		// Now, let's put that into the query. Direct string building here is OK, we're not dealing with user input,
382
+		// only the *count* of user input, which is safe.
383
+		$query = "SELECT id, username FROM user WHERE id IN ({$inSection})";
384
+
385
+		// Prepare the query
386
+		$statement = $database->prepare($query);
387
+
388
+		// Bind the parameters and execute - in one go - since we already have a handy array kicking around.
389
+		$statement->execute($userIds);
390
+
391
+		$resultSet = $statement->fetchAll(PDO::FETCH_ASSOC);
392
+
393
+		$users = array();
394
+		foreach ($resultSet as $row) {
395
+			$users[$row['id']] = $row['username'];
396
+		}
397
+
398
+		$statement->closeCursor();
399
+
400
+		return $users;
401
+	}
402
+
403
+	#endregion
404
+
405
+	/**
406
+	 * Saves the current object
407
+	 *
408
+	 * @throws Exception
409
+	 */
410
+	public function save()
411
+	{
412
+		if ($this->isNew()) {
413
+			// insert
414
+			$statement = $this->dbObject->prepare(<<<SQL
415 415
 				INSERT INTO `user` ( 
416 416
 					username, email, password, status, onwikiname, welcome_sig, 
417 417
 					lastactive, forcelogout, checkuser, forceidentified,
@@ -425,36 +425,36 @@  discard block
 block discarded – undo
425 425
 					:ort, :ors, :oat, :oas
426 426
 				);
427 427
 SQL
428
-            );
429
-            $statement->bindValue(":username", $this->username);
430
-            $statement->bindValue(":email", $this->email);
431
-            $statement->bindValue(":password", $this->password);
432
-            $statement->bindValue(":status", $this->status);
433
-            $statement->bindValue(":onwikiname", $this->onwikiname);
434
-            $statement->bindValue(":welcome_sig", $this->welcome_sig);
435
-            $statement->bindValue(":lastactive", $this->lastactive);
436
-            $statement->bindValue(":forcelogout", $this->forcelogout);
437
-            $statement->bindValue(":checkuser", $this->checkuser);
438
-            $statement->bindValue(":forceidentified", $this->forceidentified);
439
-            $statement->bindValue(":welcome_template", $this->welcome_template);
440
-            $statement->bindValue(":abortpref", $this->abortpref);
441
-            $statement->bindValue(":confirmationdiff", $this->confirmationdiff);
442
-            $statement->bindValue(":emailsig", $this->emailsig);
443
-            $statement->bindValue(":ort", $this->oauthrequesttoken);
444
-            $statement->bindValue(":ors", $this->oauthrequestsecret);
445
-            $statement->bindValue(":oat", $this->oauthaccesstoken);
446
-            $statement->bindValue(":oas", $this->oauthaccesssecret);
447
-
448
-            if ($statement->execute()) {
449
-                $this->id = (int)$this->dbObject->lastInsertId();
450
-            }
451
-            else {
452
-                throw new Exception($statement->errorInfo());
453
-            }
454
-        }
455
-        else {
456
-            // update
457
-            $statement = $this->dbObject->prepare(<<<SQL
428
+			);
429
+			$statement->bindValue(":username", $this->username);
430
+			$statement->bindValue(":email", $this->email);
431
+			$statement->bindValue(":password", $this->password);
432
+			$statement->bindValue(":status", $this->status);
433
+			$statement->bindValue(":onwikiname", $this->onwikiname);
434
+			$statement->bindValue(":welcome_sig", $this->welcome_sig);
435
+			$statement->bindValue(":lastactive", $this->lastactive);
436
+			$statement->bindValue(":forcelogout", $this->forcelogout);
437
+			$statement->bindValue(":checkuser", $this->checkuser);
438
+			$statement->bindValue(":forceidentified", $this->forceidentified);
439
+			$statement->bindValue(":welcome_template", $this->welcome_template);
440
+			$statement->bindValue(":abortpref", $this->abortpref);
441
+			$statement->bindValue(":confirmationdiff", $this->confirmationdiff);
442
+			$statement->bindValue(":emailsig", $this->emailsig);
443
+			$statement->bindValue(":ort", $this->oauthrequesttoken);
444
+			$statement->bindValue(":ors", $this->oauthrequestsecret);
445
+			$statement->bindValue(":oat", $this->oauthaccesstoken);
446
+			$statement->bindValue(":oas", $this->oauthaccesssecret);
447
+
448
+			if ($statement->execute()) {
449
+				$this->id = (int)$this->dbObject->lastInsertId();
450
+			}
451
+			else {
452
+				throw new Exception($statement->errorInfo());
453
+			}
454
+		}
455
+		else {
456
+			// update
457
+			$statement = $this->dbObject->prepare(<<<SQL
458 458
 				UPDATE `user` SET 
459 459
 					username = :username, email = :email, 
460 460
 					password = :password, status = :status,
@@ -469,721 +469,721 @@  discard block
 block discarded – undo
469 469
 				WHERE id = :id AND updateversion = :updateversion
470 470
 				LIMIT 1;
471 471
 SQL
472
-            );
473
-            $statement->bindValue(":forceidentified", $this->forceidentified);
474
-
475
-            $statement->bindValue(':id', $this->id);
476
-            $statement->bindValue(':updateversion', $this->updateversion);
477
-
478
-            $statement->bindValue(':username', $this->username);
479
-            $statement->bindValue(':email', $this->email);
480
-            $statement->bindValue(':password', $this->password);
481
-            $statement->bindValue(':status', $this->status);
482
-            $statement->bindValue(':onwikiname', $this->onwikiname);
483
-            $statement->bindValue(':welcome_sig', $this->welcome_sig);
484
-            $statement->bindValue(':lastactive', $this->lastactive);
485
-            $statement->bindValue(':forcelogout', $this->forcelogout);
486
-            $statement->bindValue(':checkuser', $this->checkuser);
487
-            $statement->bindValue(':forceidentified', $this->forceidentified);
488
-            $statement->bindValue(':welcome_template', $this->welcome_template);
489
-            $statement->bindValue(':abortpref', $this->abortpref);
490
-            $statement->bindValue(':confirmationdiff', $this->confirmationdiff);
491
-            $statement->bindValue(':emailsig', $this->emailsig);
492
-            $statement->bindValue(':ort', $this->oauthrequesttoken);
493
-            $statement->bindValue(':ors', $this->oauthrequestsecret);
494
-            $statement->bindValue(':oat', $this->oauthaccesstoken);
495
-            $statement->bindValue(':oas', $this->oauthaccesssecret);
496
-
497
-            if (!$statement->execute()) {
498
-                throw new Exception($statement->errorInfo());
499
-            }
500
-
501
-            if ($statement->rowCount() !== 1) {
502
-                throw new OptimisticLockFailedException();
503
-            }
504
-
505
-            $this->updateversion++;
506
-        }
507
-    }
508
-
509
-    /**
510
-     * Authenticates the user with the supplied password
511
-     *
512
-     * @param string $password
513
-     *
514
-     * @return bool
515
-     * @throws Exception
516
-     * @category Security-Critical
517
-     */
518
-    public function authenticate($password)
519
-    {
520
-        $result = AuthUtility::testCredentials($password, $this->password);
521
-
522
-        if ($result === true) {
523
-            // password version is out of date, update it.
524
-            if (!AuthUtility::isCredentialVersionLatest($this->password)) {
525
-                $this->password = AuthUtility::encryptPassword($password);
526
-                $this->save();
527
-            }
528
-        }
529
-
530
-        return $result;
531
-    }
532
-
533
-    #region properties
534
-
535
-    /**
536
-     * Gets the tool username
537
-     * @return string
538
-     */
539
-    public function getUsername()
540
-    {
541
-        return $this->username;
542
-    }
543
-
544
-    /**
545
-     * Sets the tool username
546
-     *
547
-     * @param string $username
548
-     */
549
-    public function setUsername($username)
550
-    {
551
-        $this->username = $username;
552
-
553
-        // If this isn't a brand new user, then it's a rename, force the logout
554
-        if (!$this->isNew()) {
555
-            $this->forcelogout = 1;
556
-        }
557
-    }
558
-
559
-    /**
560
-     * Gets the user's email address
561
-     * @return string
562
-     */
563
-    public function getEmail()
564
-    {
565
-        return $this->email;
566
-    }
567
-
568
-    /**
569
-     * Sets the user's email address
570
-     *
571
-     * @param string $email
572
-     */
573
-    public function setEmail($email)
574
-    {
575
-        $this->email = $email;
576
-    }
577
-
578
-    /**
579
-     * Sets the user's password
580
-     *
581
-     * @param string $password the plaintext password
582
-     *
583
-     * @category Security-Critical
584
-     */
585
-    public function setPassword($password)
586
-    {
587
-        $this->password = AuthUtility::encryptPassword($password);
588
-    }
589
-
590
-    /**
591
-     * Gets the status (User, Admin, Suspended, etc - excludes checkuser) of the user.
592
-     * @return string
593
-     */
594
-    public function getStatus()
595
-    {
596
-        return $this->status;
597
-    }
598
-
599
-    /**
600
-     * @param string $status
601
-     */
602
-    public function setStatus($status)
603
-    {
604
-        $this->status = $status;
605
-    }
606
-
607
-    /**
608
-     * Gets the user's on-wiki name
609
-     * @return string
610
-     */
611
-    public function getOnWikiName()
612
-    {
613
-        if ($this->oauthaccesstoken !== null) {
614
-            try {
615
-                return $this->getOAuthOnWikiName();
616
-            }
617
-            catch (Exception $ex) {
618
-                // urm.. log this?
619
-                return $this->onwikiname;
620
-            }
621
-        }
622
-
623
-        return $this->onwikiname;
624
-    }
625
-
626
-    /**
627
-     * This is probably NOT the function you want!
628
-     *
629
-     * Take a look at getOnWikiName() instead.
630
-     * @return string
631
-     */
632
-    public function getStoredOnWikiName()
633
-    {
634
-        return $this->onwikiname;
635
-    }
636
-
637
-    /**
638
-     * Sets the user's on-wiki name
639
-     *
640
-     * This can have interesting side-effects with OAuth.
641
-     *
642
-     * @param string $onWikiName
643
-     */
644
-    public function setOnWikiName($onWikiName)
645
-    {
646
-        $this->onwikiname = $onWikiName;
647
-    }
648
-
649
-    /**
650
-     * Gets the welcome signature
651
-     * @return string
652
-     */
653
-    public function getWelcomeSig()
654
-    {
655
-        return $this->welcome_sig;
656
-    }
657
-
658
-    /**
659
-     * Sets the welcome signature
660
-     *
661
-     * @param string $welcomeSig
662
-     */
663
-    public function setWelcomeSig($welcomeSig)
664
-    {
665
-        $this->welcome_sig = $welcomeSig;
666
-    }
667
-
668
-    /**
669
-     * Gets the last activity date for the user
670
-     *
671
-     * @return string
672
-     * @todo This should probably return an instance of DateTime
673
-     */
674
-    public function getLastActive()
675
-    {
676
-        return $this->lastactive;
677
-    }
678
-
679
-    /**
680
-     * Gets the user's forced logout status
681
-     *
682
-     * @return bool
683
-     */
684
-    public function getForceLogout()
685
-    {
686
-        return $this->forcelogout == 1;
687
-    }
688
-
689
-    /**
690
-     * Sets the user's forced logout status
691
-     *
692
-     * @param bool $forceLogout
693
-     */
694
-    public function setForceLogout($forceLogout)
695
-    {
696
-        $this->forcelogout = $forceLogout ? 1 : 0;
697
-    }
698
-
699
-    /**
700
-     * Returns the ID of the welcome template used.
701
-     * @return int
702
-     */
703
-    public function getWelcomeTemplate()
704
-    {
705
-        return $this->welcome_template;
706
-    }
707
-
708
-    /**
709
-     * Sets the ID of the welcome template used.
710
-     *
711
-     * @param int $welcomeTemplate
712
-     */
713
-    public function setWelcomeTemplate($welcomeTemplate)
714
-    {
715
-        $this->welcome_template = $welcomeTemplate;
716
-    }
717
-
718
-    /**
719
-     * Gets the user's abort preference
720
-     * @todo this is badly named too! Also a bool that's actually an int.
721
-     * @return int
722
-     */
723
-    public function getAbortPref()
724
-    {
725
-        return $this->abortpref;
726
-    }
727
-
728
-    /**
729
-     * Sets the user's abort preference
730
-     * @todo rename, retype, and re-comment.
731
-     *
732
-     * @param int $abortPreference
733
-     */
734
-    public function setAbortPref($abortPreference)
735
-    {
736
-        $this->abortpref = $abortPreference;
737
-    }
738
-
739
-    /**
740
-     * Gets the user's confirmation diff. Unused if OAuth is in use.
741
-     * @return int the diff ID
742
-     */
743
-    public function getConfirmationDiff()
744
-    {
745
-        return $this->confirmationdiff;
746
-    }
747
-
748
-    /**
749
-     * Sets the user's confirmation diff.
750
-     *
751
-     * @param int $confirmationDiff
752
-     */
753
-    public function setConfirmationDiff($confirmationDiff)
754
-    {
755
-        $this->confirmationdiff = $confirmationDiff;
756
-    }
757
-
758
-    /**
759
-     * Gets the users' email signature used on outbound mail.
760
-     * @todo rename me!
761
-     * @return string
762
-     */
763
-    public function getEmailSig()
764
-    {
765
-        return $this->emailsig;
766
-    }
767
-
768
-    /**
769
-     * Sets the user's email signature for outbound mail.
770
-     *
771
-     * @param string $emailSignature
772
-     */
773
-    public function setEmailSig($emailSignature)
774
-    {
775
-        $this->emailsig = $emailSignature;
776
-    }
777
-
778
-    /**
779
-     * Gets the user's OAuth request token.
780
-     *
781
-     * @todo move me to a collaborator.
782
-     * @return null|string
783
-     */
784
-    public function getOAuthRequestToken()
785
-    {
786
-        return $this->oauthrequesttoken;
787
-    }
788
-
789
-    /**
790
-     * Sets the user's OAuth request token
791
-     * @todo move me to a collaborator
792
-     *
793
-     * @param string $oAuthRequestToken
794
-     */
795
-    public function setOAuthRequestToken($oAuthRequestToken)
796
-    {
797
-        $this->oauthrequesttoken = $oAuthRequestToken;
798
-    }
799
-
800
-    /**
801
-     * Gets the users OAuth request secret
802
-     * @category Security-Critical
803
-     * @todo     move me to a collaborator
804
-     * @return null|string
805
-     */
806
-    public function getOAuthRequestSecret()
807
-    {
808
-        return $this->oauthrequestsecret;
809
-    }
810
-
811
-    /**
812
-     * Sets the user's OAuth request secret
813
-     * @todo move me to a collaborator
814
-     *
815
-     * @param string $oAuthRequestSecret
816
-     */
817
-    public function setOAuthRequestSecret($oAuthRequestSecret)
818
-    {
819
-        $this->oauthrequestsecret = $oAuthRequestSecret;
820
-    }
821
-
822
-    /**
823
-     * Gets the user's access token
824
-     * @category Security-Critical
825
-     * @todo     move me to a collaborator
826
-     * @return null|string
827
-     */
828
-    public function getOAuthAccessToken()
829
-    {
830
-        return $this->oauthaccesstoken;
831
-    }
832
-
833
-    /**
834
-     * Sets the user's access token
835
-     * @todo move me to a collaborator
836
-     *
837
-     * @param string $oAuthAccessToken
838
-     */
839
-    public function setOAuthAccessToken($oAuthAccessToken)
840
-    {
841
-        $this->oauthaccesstoken = $oAuthAccessToken;
842
-    }
843
-
844
-    /**
845
-     * Gets the user's OAuth access secret
846
-     * @category Security-Critical
847
-     * @todo     move me to a collaborator
848
-     * @return null|string
849
-     */
850
-    public function getOAuthAccessSecret()
851
-    {
852
-        return $this->oauthaccesssecret;
853
-    }
854
-
855
-    /**
856
-     * Sets the user's OAuth access secret
857
-     * @todo move me to a collaborator
858
-     *
859
-     * @param string $oAuthAccessSecret
860
-     */
861
-    public function setOAuthAccessSecret($oAuthAccessSecret)
862
-    {
863
-        $this->oauthaccesssecret = $oAuthAccessSecret;
864
-    }
865
-
866
-    #endregion
867
-
868
-    #region user access checks
869
-
870
-    /**
871
-     * Tests if the user is an admin
872
-     * @return bool
873
-     * @category Security-Critical
874
-     */
875
-    public function isAdmin()
876
-    {
877
-        return $this->status == "Admin";
878
-    }
879
-
880
-    /**
881
-     * Tests if the user is a checkuser
882
-     * @return bool
883
-     * @category Security-Critical
884
-     */
885
-    public function isCheckuser()
886
-    {
887
-        return $this->checkuser == 1 || $this->oauthCanCheckUser();
888
-    }
889
-
890
-    /**
891
-     * Tests if the user is identified
892
-     *
893
-     * @param IdentificationVerifier $iv
894
-     *
895
-     * @return bool
896
-     * @todo     Figure out what on earth is going on with PDO's typecasting here.  Apparently, it returns string("0") for
897
-     *       the force-unidentified case, and int(1) for the identified case?!  This is quite ugly, but probably needed
898
-     *       to play it safe for now.
899
-     * @category Security-Critical
900
-     */
901
-    public function isIdentified(IdentificationVerifier $iv)
902
-    {
903
-        if ($this->forceidentified === 0 || $this->forceidentified === "0") {
904
-            // User forced to unidentified in the database.
905
-            return false;
906
-        }
907
-        elseif ($this->forceidentified === 1 || $this->forceidentified === "1") {
908
-            // User forced to identified in the database.
909
-            return true;
910
-        }
911
-        else {
912
-            // User not forced to any particular identified status; consult IdentificationVerifier
913
-            return $iv->isUserIdentified($this->getOnWikiName());
914
-        }
915
-    }
916
-
917
-    /**
918
-     * Tests if the user is suspended
919
-     * @return bool
920
-     * @category Security-Critical
921
-     */
922
-    public function isSuspended()
923
-    {
924
-        return $this->status == "Suspended";
925
-    }
926
-
927
-    /**
928
-     * Tests if the user is new
929
-     * @return bool
930
-     * @category Security-Critical
931
-     */
932
-    public function isNewUser()
933
-    {
934
-        return $this->status == "New";
935
-    }
936
-
937
-    /**
938
-     * Tests if the user is a standard-level user
939
-     * @return bool
940
-     * @category Security-Critical
941
-     */
942
-    public function isUser()
943
-    {
944
-        return $this->status == "User";
945
-    }
946
-
947
-    /**
948
-     * Tests if the user has been declined access to the tool
949
-     * @return bool
950
-     * @category Security-Critical
951
-     */
952
-    public function isDeclined()
953
-    {
954
-        return $this->status == "Declined";
955
-    }
956
-
957
-    /**
958
-     * Tests if the user is the community user
959
-     *
960
-     * @todo     decide if this means logged out. I think it usually does.
961
-     * @return bool
962
-     * @category Security-Critical
963
-     */
964
-    public function isCommunityUser()
965
-    {
966
-        return false;
967
-    }
968
-
969
-    #endregion 
970
-
971
-    #region OAuth
972
-
973
-    /**
974
-     * @todo     move me to a collaborator
975
-     *
976
-     * @param bool $useCached
977
-     *
978
-     * @return mixed|null
979
-     * @category Security-Critical
980
-     */
981
-    public function getOAuthIdentity($useCached = false)
982
-    {
983
-        if ($this->oauthaccesstoken === null) {
984
-            $this->clearOAuthData();
985
-        }
986
-
987
-        global $oauthConsumerToken, $oauthMediaWikiCanonicalServer;
988
-
989
-        if ($this->oauthidentitycache == null) {
990
-            $this->identityCache = null;
991
-        }
992
-        else {
993
-            $this->identityCache = unserialize($this->oauthidentitycache);
994
-        }
995
-
996
-        // check the cache
997
-        if (
998
-            $this->identityCache != null &&
999
-            $this->identityCache->aud == $oauthConsumerToken &&
1000
-            $this->identityCache->iss == $oauthMediaWikiCanonicalServer
1001
-        ) {
1002
-            if (
1003
-                $useCached || (
1004
-                    DateTime::createFromFormat("U", $this->identityCache->iat) < new DateTime() &&
1005
-                    DateTime::createFromFormat("U", $this->identityCache->exp) > new DateTime()
1006
-                )
1007
-            ) {
1008
-                // Use cached value - it's either valid or we don't care.
1009
-                return $this->identityCache;
1010
-            }
1011
-            else {
1012
-                // Cache expired and not forcing use of cached value
1013
-                $this->getIdentityCache();
1014
-
1015
-                return $this->identityCache;
1016
-            }
1017
-        }
1018
-        else {
1019
-            // Cache isn't ours or doesn't exist
1020
-            $this->getIdentityCache();
1021
-
1022
-            return $this->identityCache;
1023
-        }
1024
-    }
1025
-
1026
-    /**
1027
-     * @todo     move me to a collaborator
1028
-     *
1029
-     * @param mixed $useCached Set to false for everything where up-to-date data is important.
1030
-     *
1031
-     * @return mixed
1032
-     * @category Security-Critical
1033
-     */
1034
-    private function getOAuthOnWikiName($useCached = false)
1035
-    {
1036
-        $identity = $this->getOAuthIdentity($useCached);
1037
-        if ($identity !== null) {
1038
-            return $identity->username;
1039
-        }
1040
-
1041
-        return false;
1042
-    }
1043
-
1044
-    /**
1045
-     * @return bool
1046
-     * @todo move me to a collaborator
1047
-     */
1048
-    public function isOAuthLinked()
1049
-    {
1050
-        if ($this->onwikiname === "##OAUTH##") {
1051
-            return true; // special value. If an account must be oauth linked, this is true.
1052
-        }
1053
-
1054
-        return $this->oauthaccesstoken !== null;
1055
-    }
1056
-
1057
-    /**
1058
-     * @return null
1059
-     * @todo move me to a collaborator
1060
-     */
1061
-    public function clearOAuthData()
1062
-    {
1063
-        $this->identityCache = null;
1064
-        $this->oauthidentitycache = null;
1065
-        $clearCacheQuery = "UPDATE user SET oauthidentitycache = NULL WHERE id = :id;";
1066
-        $this->dbObject->prepare($clearCacheQuery)->execute(array(":id" => $this->id));
1067
-
1068
-        return null;
1069
-    }
1070
-
1071
-    /**
1072
-     * @throws Exception
1073
-     * @todo     move me to a collaborator
1074
-     * @category Security-Critical
1075
-     */
1076
-    private function getIdentityCache()
1077
-    {
1078
-        /** @var IOAuthHelper $oauthHelper */
1079
-        global $oauthHelper;
1080
-
1081
-        try {
1082
-            $this->identityCache = $oauthHelper->getIdentityTicket($this->oauthaccesstoken, $this->oauthaccesssecret);
1083
-
1084
-            $this->oauthidentitycache = serialize($this->identityCache);
1085
-            $this->dbObject->prepare("UPDATE user SET oauthidentitycache = :identity WHERE id = :id;")
1086
-                ->execute(array(":id" => $this->id, ":identity" => $this->oauthidentitycache));
1087
-        }
1088
-        catch (UnexpectedValueException $ex) {
1089
-            $this->identityCache = null;
1090
-            $this->oauthidentitycache = null;
1091
-            $this->dbObject->prepare("UPDATE user SET oauthidentitycache = NULL WHERE id = :id;")
1092
-                ->execute(array(":id" => $this->id));
1093
-
1094
-            SessionAlert::warning("OAuth error getting identity from MediaWiki: " . $ex->getMessage());
1095
-        }
1096
-    }
1097
-
1098
-    /**
1099
-     * @return bool
1100
-     * @todo move me to a collaborator
1101
-     */
1102
-    public function oauthCanUse()
1103
-    {
1104
-        try {
1105
-            return in_array('useoauth', $this->getOAuthIdentity()->grants);
1106
-        }
1107
-        catch (Exception $ex) {
1108
-            return false;
1109
-        }
1110
-    }
1111
-
1112
-    /**
1113
-     * @return bool
1114
-     * @todo move me to a collaborator
1115
-     */
1116
-    public function oauthCanEdit()
1117
-    {
1118
-        try {
1119
-            return in_array('useoauth', $this->getOAuthIdentity()->grants)
1120
-            && in_array('createeditmovepage', $this->getOAuthIdentity()->grants)
1121
-            && in_array('createtalk', $this->getOAuthIdentity()->rights)
1122
-            && in_array('edit', $this->getOAuthIdentity()->rights)
1123
-            && in_array('writeapi', $this->getOAuthIdentity()->rights);
1124
-        }
1125
-        catch (Exception $ex) {
1126
-            return false;
1127
-        }
1128
-    }
1129
-
1130
-    /**
1131
-     * @return bool
1132
-     * @todo move me to a collaborator
1133
-     */
1134
-    public function oauthCanCreateAccount()
1135
-    {
1136
-        try {
1137
-            return in_array('useoauth', $this->getOAuthIdentity()->grants)
1138
-            && in_array('createaccount', $this->getOAuthIdentity()->grants)
1139
-            && in_array('createaccount', $this->getOAuthIdentity()->rights)
1140
-            && in_array('writeapi', $this->getOAuthIdentity()->rights);
1141
-        }
1142
-        catch (Exception $ex) {
1143
-            return false;
1144
-        }
1145
-    }
1146
-
1147
-    /**
1148
-     * @return bool
1149
-     * @todo     move me to a collaborator
1150
-     * @category Security-Critical
1151
-     */
1152
-    protected function oauthCanCheckUser()
1153
-    {
1154
-        if (!$this->isOAuthLinked()) {
1155
-            return false;
1156
-        }
1157
-
1158
-        try {
1159
-            $identity = $this->getOAuthIdentity();
1160
-
1161
-            return in_array('checkuser', $identity->rights);
1162
-        }
1163
-        catch (Exception $ex) {
1164
-            return false;
1165
-        }
1166
-    }
1167
-
1168
-    #endregion
1169
-
1170
-    /**
1171
-     * Gets a hash of data for the user to reset their password with.
1172
-     * @category Security-Critical
1173
-     * @return string
1174
-     */
1175
-    public function getForgottenPasswordHash()
1176
-    {
1177
-        return md5($this->username . $this->email . $this->welcome_template . $this->id . $this->password);
1178
-    }
1179
-
1180
-    /**
1181
-     * Gets the approval date of the user
1182
-     * @return DateTime|false
1183
-     */
1184
-    public function getApprovalDate()
1185
-    {
1186
-        $query = $this->dbObject->prepare(<<<SQL
472
+			);
473
+			$statement->bindValue(":forceidentified", $this->forceidentified);
474
+
475
+			$statement->bindValue(':id', $this->id);
476
+			$statement->bindValue(':updateversion', $this->updateversion);
477
+
478
+			$statement->bindValue(':username', $this->username);
479
+			$statement->bindValue(':email', $this->email);
480
+			$statement->bindValue(':password', $this->password);
481
+			$statement->bindValue(':status', $this->status);
482
+			$statement->bindValue(':onwikiname', $this->onwikiname);
483
+			$statement->bindValue(':welcome_sig', $this->welcome_sig);
484
+			$statement->bindValue(':lastactive', $this->lastactive);
485
+			$statement->bindValue(':forcelogout', $this->forcelogout);
486
+			$statement->bindValue(':checkuser', $this->checkuser);
487
+			$statement->bindValue(':forceidentified', $this->forceidentified);
488
+			$statement->bindValue(':welcome_template', $this->welcome_template);
489
+			$statement->bindValue(':abortpref', $this->abortpref);
490
+			$statement->bindValue(':confirmationdiff', $this->confirmationdiff);
491
+			$statement->bindValue(':emailsig', $this->emailsig);
492
+			$statement->bindValue(':ort', $this->oauthrequesttoken);
493
+			$statement->bindValue(':ors', $this->oauthrequestsecret);
494
+			$statement->bindValue(':oat', $this->oauthaccesstoken);
495
+			$statement->bindValue(':oas', $this->oauthaccesssecret);
496
+
497
+			if (!$statement->execute()) {
498
+				throw new Exception($statement->errorInfo());
499
+			}
500
+
501
+			if ($statement->rowCount() !== 1) {
502
+				throw new OptimisticLockFailedException();
503
+			}
504
+
505
+			$this->updateversion++;
506
+		}
507
+	}
508
+
509
+	/**
510
+	 * Authenticates the user with the supplied password
511
+	 *
512
+	 * @param string $password
513
+	 *
514
+	 * @return bool
515
+	 * @throws Exception
516
+	 * @category Security-Critical
517
+	 */
518
+	public function authenticate($password)
519
+	{
520
+		$result = AuthUtility::testCredentials($password, $this->password);
521
+
522
+		if ($result === true) {
523
+			// password version is out of date, update it.
524
+			if (!AuthUtility::isCredentialVersionLatest($this->password)) {
525
+				$this->password = AuthUtility::encryptPassword($password);
526
+				$this->save();
527
+			}
528
+		}
529
+
530
+		return $result;
531
+	}
532
+
533
+	#region properties
534
+
535
+	/**
536
+	 * Gets the tool username
537
+	 * @return string
538
+	 */
539
+	public function getUsername()
540
+	{
541
+		return $this->username;
542
+	}
543
+
544
+	/**
545
+	 * Sets the tool username
546
+	 *
547
+	 * @param string $username
548
+	 */
549
+	public function setUsername($username)
550
+	{
551
+		$this->username = $username;
552
+
553
+		// If this isn't a brand new user, then it's a rename, force the logout
554
+		if (!$this->isNew()) {
555
+			$this->forcelogout = 1;
556
+		}
557
+	}
558
+
559
+	/**
560
+	 * Gets the user's email address
561
+	 * @return string
562
+	 */
563
+	public function getEmail()
564
+	{
565
+		return $this->email;
566
+	}
567
+
568
+	/**
569
+	 * Sets the user's email address
570
+	 *
571
+	 * @param string $email
572
+	 */
573
+	public function setEmail($email)
574
+	{
575
+		$this->email = $email;
576
+	}
577
+
578
+	/**
579
+	 * Sets the user's password
580
+	 *
581
+	 * @param string $password the plaintext password
582
+	 *
583
+	 * @category Security-Critical
584
+	 */
585
+	public function setPassword($password)
586
+	{
587
+		$this->password = AuthUtility::encryptPassword($password);
588
+	}
589
+
590
+	/**
591
+	 * Gets the status (User, Admin, Suspended, etc - excludes checkuser) of the user.
592
+	 * @return string
593
+	 */
594
+	public function getStatus()
595
+	{
596
+		return $this->status;
597
+	}
598
+
599
+	/**
600
+	 * @param string $status
601
+	 */
602
+	public function setStatus($status)
603
+	{
604
+		$this->status = $status;
605
+	}
606
+
607
+	/**
608
+	 * Gets the user's on-wiki name
609
+	 * @return string
610
+	 */
611
+	public function getOnWikiName()
612
+	{
613
+		if ($this->oauthaccesstoken !== null) {
614
+			try {
615
+				return $this->getOAuthOnWikiName();
616
+			}
617
+			catch (Exception $ex) {
618
+				// urm.. log this?
619
+				return $this->onwikiname;
620
+			}
621
+		}
622
+
623
+		return $this->onwikiname;
624
+	}
625
+
626
+	/**
627
+	 * This is probably NOT the function you want!
628
+	 *
629
+	 * Take a look at getOnWikiName() instead.
630
+	 * @return string
631
+	 */
632
+	public function getStoredOnWikiName()
633
+	{
634
+		return $this->onwikiname;
635
+	}
636
+
637
+	/**
638
+	 * Sets the user's on-wiki name
639
+	 *
640
+	 * This can have interesting side-effects with OAuth.
641
+	 *
642
+	 * @param string $onWikiName
643
+	 */
644
+	public function setOnWikiName($onWikiName)
645
+	{
646
+		$this->onwikiname = $onWikiName;
647
+	}
648
+
649
+	/**
650
+	 * Gets the welcome signature
651
+	 * @return string
652
+	 */
653
+	public function getWelcomeSig()
654
+	{
655
+		return $this->welcome_sig;
656
+	}
657
+
658
+	/**
659
+	 * Sets the welcome signature
660
+	 *
661
+	 * @param string $welcomeSig
662
+	 */
663
+	public function setWelcomeSig($welcomeSig)
664
+	{
665
+		$this->welcome_sig = $welcomeSig;
666
+	}
667
+
668
+	/**
669
+	 * Gets the last activity date for the user
670
+	 *
671
+	 * @return string
672
+	 * @todo This should probably return an instance of DateTime
673
+	 */
674
+	public function getLastActive()
675
+	{
676
+		return $this->lastactive;
677
+	}
678
+
679
+	/**
680
+	 * Gets the user's forced logout status
681
+	 *
682
+	 * @return bool
683
+	 */
684
+	public function getForceLogout()
685
+	{
686
+		return $this->forcelogout == 1;
687
+	}
688
+
689
+	/**
690
+	 * Sets the user's forced logout status
691
+	 *
692
+	 * @param bool $forceLogout
693
+	 */
694
+	public function setForceLogout($forceLogout)
695
+	{
696
+		$this->forcelogout = $forceLogout ? 1 : 0;
697
+	}
698
+
699
+	/**
700
+	 * Returns the ID of the welcome template used.
701
+	 * @return int
702
+	 */
703
+	public function getWelcomeTemplate()
704
+	{
705
+		return $this->welcome_template;
706
+	}
707
+
708
+	/**
709
+	 * Sets the ID of the welcome template used.
710
+	 *
711
+	 * @param int $welcomeTemplate
712
+	 */
713
+	public function setWelcomeTemplate($welcomeTemplate)
714
+	{
715
+		$this->welcome_template = $welcomeTemplate;
716
+	}
717
+
718
+	/**
719
+	 * Gets the user's abort preference
720
+	 * @todo this is badly named too! Also a bool that's actually an int.
721
+	 * @return int
722
+	 */
723
+	public function getAbortPref()
724
+	{
725
+		return $this->abortpref;
726
+	}
727
+
728
+	/**
729
+	 * Sets the user's abort preference
730
+	 * @todo rename, retype, and re-comment.
731
+	 *
732
+	 * @param int $abortPreference
733
+	 */
734
+	public function setAbortPref($abortPreference)
735
+	{
736
+		$this->abortpref = $abortPreference;
737
+	}
738
+
739
+	/**
740
+	 * Gets the user's confirmation diff. Unused if OAuth is in use.
741
+	 * @return int the diff ID
742
+	 */
743
+	public function getConfirmationDiff()
744
+	{
745
+		return $this->confirmationdiff;
746
+	}
747
+
748
+	/**
749
+	 * Sets the user's confirmation diff.
750
+	 *
751
+	 * @param int $confirmationDiff
752
+	 */
753
+	public function setConfirmationDiff($confirmationDiff)
754
+	{
755
+		$this->confirmationdiff = $confirmationDiff;
756
+	}
757
+
758
+	/**
759
+	 * Gets the users' email signature used on outbound mail.
760
+	 * @todo rename me!
761
+	 * @return string
762
+	 */
763
+	public function getEmailSig()
764
+	{
765
+		return $this->emailsig;
766
+	}
767
+
768
+	/**
769
+	 * Sets the user's email signature for outbound mail.
770
+	 *
771
+	 * @param string $emailSignature
772
+	 */
773
+	public function setEmailSig($emailSignature)
774
+	{
775
+		$this->emailsig = $emailSignature;
776
+	}
777
+
778
+	/**
779
+	 * Gets the user's OAuth request token.
780
+	 *
781
+	 * @todo move me to a collaborator.
782
+	 * @return null|string
783
+	 */
784
+	public function getOAuthRequestToken()
785
+	{
786
+		return $this->oauthrequesttoken;
787
+	}
788
+
789
+	/**
790
+	 * Sets the user's OAuth request token
791
+	 * @todo move me to a collaborator
792
+	 *
793
+	 * @param string $oAuthRequestToken
794
+	 */
795
+	public function setOAuthRequestToken($oAuthRequestToken)
796
+	{
797
+		$this->oauthrequesttoken = $oAuthRequestToken;
798
+	}
799
+
800
+	/**
801
+	 * Gets the users OAuth request secret
802
+	 * @category Security-Critical
803
+	 * @todo     move me to a collaborator
804
+	 * @return null|string
805
+	 */
806
+	public function getOAuthRequestSecret()
807
+	{
808
+		return $this->oauthrequestsecret;
809
+	}
810
+
811
+	/**
812
+	 * Sets the user's OAuth request secret
813
+	 * @todo move me to a collaborator
814
+	 *
815
+	 * @param string $oAuthRequestSecret
816
+	 */
817
+	public function setOAuthRequestSecret($oAuthRequestSecret)
818
+	{
819
+		$this->oauthrequestsecret = $oAuthRequestSecret;
820
+	}
821
+
822
+	/**
823
+	 * Gets the user's access token
824
+	 * @category Security-Critical
825
+	 * @todo     move me to a collaborator
826
+	 * @return null|string
827
+	 */
828
+	public function getOAuthAccessToken()
829
+	{
830
+		return $this->oauthaccesstoken;
831
+	}
832
+
833
+	/**
834
+	 * Sets the user's access token
835
+	 * @todo move me to a collaborator
836
+	 *
837
+	 * @param string $oAuthAccessToken
838
+	 */
839
+	public function setOAuthAccessToken($oAuthAccessToken)
840
+	{
841
+		$this->oauthaccesstoken = $oAuthAccessToken;
842
+	}
843
+
844
+	/**
845
+	 * Gets the user's OAuth access secret
846
+	 * @category Security-Critical
847
+	 * @todo     move me to a collaborator
848
+	 * @return null|string
849
+	 */
850
+	public function getOAuthAccessSecret()
851
+	{
852
+		return $this->oauthaccesssecret;
853
+	}
854
+
855
+	/**
856
+	 * Sets the user's OAuth access secret
857
+	 * @todo move me to a collaborator
858
+	 *
859
+	 * @param string $oAuthAccessSecret
860
+	 */
861
+	public function setOAuthAccessSecret($oAuthAccessSecret)
862
+	{
863
+		$this->oauthaccesssecret = $oAuthAccessSecret;
864
+	}
865
+
866
+	#endregion
867
+
868
+	#region user access checks
869
+
870
+	/**
871
+	 * Tests if the user is an admin
872
+	 * @return bool
873
+	 * @category Security-Critical
874
+	 */
875
+	public function isAdmin()
876
+	{
877
+		return $this->status == "Admin";
878
+	}
879
+
880
+	/**
881
+	 * Tests if the user is a checkuser
882
+	 * @return bool
883
+	 * @category Security-Critical
884
+	 */
885
+	public function isCheckuser()
886
+	{
887
+		return $this->checkuser == 1 || $this->oauthCanCheckUser();
888
+	}
889
+
890
+	/**
891
+	 * Tests if the user is identified
892
+	 *
893
+	 * @param IdentificationVerifier $iv
894
+	 *
895
+	 * @return bool
896
+	 * @todo     Figure out what on earth is going on with PDO's typecasting here.  Apparently, it returns string("0") for
897
+	 *       the force-unidentified case, and int(1) for the identified case?!  This is quite ugly, but probably needed
898
+	 *       to play it safe for now.
899
+	 * @category Security-Critical
900
+	 */
901
+	public function isIdentified(IdentificationVerifier $iv)
902
+	{
903
+		if ($this->forceidentified === 0 || $this->forceidentified === "0") {
904
+			// User forced to unidentified in the database.
905
+			return false;
906
+		}
907
+		elseif ($this->forceidentified === 1 || $this->forceidentified === "1") {
908
+			// User forced to identified in the database.
909
+			return true;
910
+		}
911
+		else {
912
+			// User not forced to any particular identified status; consult IdentificationVerifier
913
+			return $iv->isUserIdentified($this->getOnWikiName());
914
+		}
915
+	}
916
+
917
+	/**
918
+	 * Tests if the user is suspended
919
+	 * @return bool
920
+	 * @category Security-Critical
921
+	 */
922
+	public function isSuspended()
923
+	{
924
+		return $this->status == "Suspended";
925
+	}
926
+
927
+	/**
928
+	 * Tests if the user is new
929
+	 * @return bool
930
+	 * @category Security-Critical
931
+	 */
932
+	public function isNewUser()
933
+	{
934
+		return $this->status == "New";
935
+	}
936
+
937
+	/**
938
+	 * Tests if the user is a standard-level user
939
+	 * @return bool
940
+	 * @category Security-Critical
941
+	 */
942
+	public function isUser()
943
+	{
944
+		return $this->status == "User";
945
+	}
946
+
947
+	/**
948
+	 * Tests if the user has been declined access to the tool
949
+	 * @return bool
950
+	 * @category Security-Critical
951
+	 */
952
+	public function isDeclined()
953
+	{
954
+		return $this->status == "Declined";
955
+	}
956
+
957
+	/**
958
+	 * Tests if the user is the community user
959
+	 *
960
+	 * @todo     decide if this means logged out. I think it usually does.
961
+	 * @return bool
962
+	 * @category Security-Critical
963
+	 */
964
+	public function isCommunityUser()
965
+	{
966
+		return false;
967
+	}
968
+
969
+	#endregion 
970
+
971
+	#region OAuth
972
+
973
+	/**
974
+	 * @todo     move me to a collaborator
975
+	 *
976
+	 * @param bool $useCached
977
+	 *
978
+	 * @return mixed|null
979
+	 * @category Security-Critical
980
+	 */
981
+	public function getOAuthIdentity($useCached = false)
982
+	{
983
+		if ($this->oauthaccesstoken === null) {
984
+			$this->clearOAuthData();
985
+		}
986
+
987
+		global $oauthConsumerToken, $oauthMediaWikiCanonicalServer;
988
+
989
+		if ($this->oauthidentitycache == null) {
990
+			$this->identityCache = null;
991
+		}
992
+		else {
993
+			$this->identityCache = unserialize($this->oauthidentitycache);
994
+		}
995
+
996
+		// check the cache
997
+		if (
998
+			$this->identityCache != null &&
999
+			$this->identityCache->aud == $oauthConsumerToken &&
1000
+			$this->identityCache->iss == $oauthMediaWikiCanonicalServer
1001
+		) {
1002
+			if (
1003
+				$useCached || (
1004
+					DateTime::createFromFormat("U", $this->identityCache->iat) < new DateTime() &&
1005
+					DateTime::createFromFormat("U", $this->identityCache->exp) > new DateTime()
1006
+				)
1007
+			) {
1008
+				// Use cached value - it's either valid or we don't care.
1009
+				return $this->identityCache;
1010
+			}
1011
+			else {
1012
+				// Cache expired and not forcing use of cached value
1013
+				$this->getIdentityCache();
1014
+
1015
+				return $this->identityCache;
1016
+			}
1017
+		}
1018
+		else {
1019
+			// Cache isn't ours or doesn't exist
1020
+			$this->getIdentityCache();
1021
+
1022
+			return $this->identityCache;
1023
+		}
1024
+	}
1025
+
1026
+	/**
1027
+	 * @todo     move me to a collaborator
1028
+	 *
1029
+	 * @param mixed $useCached Set to false for everything where up-to-date data is important.
1030
+	 *
1031
+	 * @return mixed
1032
+	 * @category Security-Critical
1033
+	 */
1034
+	private function getOAuthOnWikiName($useCached = false)
1035
+	{
1036
+		$identity = $this->getOAuthIdentity($useCached);
1037
+		if ($identity !== null) {
1038
+			return $identity->username;
1039
+		}
1040
+
1041
+		return false;
1042
+	}
1043
+
1044
+	/**
1045
+	 * @return bool
1046
+	 * @todo move me to a collaborator
1047
+	 */
1048
+	public function isOAuthLinked()
1049
+	{
1050
+		if ($this->onwikiname === "##OAUTH##") {
1051
+			return true; // special value. If an account must be oauth linked, this is true.
1052
+		}
1053
+
1054
+		return $this->oauthaccesstoken !== null;
1055
+	}
1056
+
1057
+	/**
1058
+	 * @return null
1059
+	 * @todo move me to a collaborator
1060
+	 */
1061
+	public function clearOAuthData()
1062
+	{
1063
+		$this->identityCache = null;
1064
+		$this->oauthidentitycache = null;
1065
+		$clearCacheQuery = "UPDATE user SET oauthidentitycache = NULL WHERE id = :id;";
1066
+		$this->dbObject->prepare($clearCacheQuery)->execute(array(":id" => $this->id));
1067
+
1068
+		return null;
1069
+	}
1070
+
1071
+	/**
1072
+	 * @throws Exception
1073
+	 * @todo     move me to a collaborator
1074
+	 * @category Security-Critical
1075
+	 */
1076
+	private function getIdentityCache()
1077
+	{
1078
+		/** @var IOAuthHelper $oauthHelper */
1079
+		global $oauthHelper;
1080
+
1081
+		try {
1082
+			$this->identityCache = $oauthHelper->getIdentityTicket($this->oauthaccesstoken, $this->oauthaccesssecret);
1083
+
1084
+			$this->oauthidentitycache = serialize($this->identityCache);
1085
+			$this->dbObject->prepare("UPDATE user SET oauthidentitycache = :identity WHERE id = :id;")
1086
+				->execute(array(":id" => $this->id, ":identity" => $this->oauthidentitycache));
1087
+		}
1088
+		catch (UnexpectedValueException $ex) {
1089
+			$this->identityCache = null;
1090
+			$this->oauthidentitycache = null;
1091
+			$this->dbObject->prepare("UPDATE user SET oauthidentitycache = NULL WHERE id = :id;")
1092
+				->execute(array(":id" => $this->id));
1093
+
1094
+			SessionAlert::warning("OAuth error getting identity from MediaWiki: " . $ex->getMessage());
1095
+		}
1096
+	}
1097
+
1098
+	/**
1099
+	 * @return bool
1100
+	 * @todo move me to a collaborator
1101
+	 */
1102
+	public function oauthCanUse()
1103
+	{
1104
+		try {
1105
+			return in_array('useoauth', $this->getOAuthIdentity()->grants);
1106
+		}
1107
+		catch (Exception $ex) {
1108
+			return false;
1109
+		}
1110
+	}
1111
+
1112
+	/**
1113
+	 * @return bool
1114
+	 * @todo move me to a collaborator
1115
+	 */
1116
+	public function oauthCanEdit()
1117
+	{
1118
+		try {
1119
+			return in_array('useoauth', $this->getOAuthIdentity()->grants)
1120
+			&& in_array('createeditmovepage', $this->getOAuthIdentity()->grants)
1121
+			&& in_array('createtalk', $this->getOAuthIdentity()->rights)
1122
+			&& in_array('edit', $this->getOAuthIdentity()->rights)
1123
+			&& in_array('writeapi', $this->getOAuthIdentity()->rights);
1124
+		}
1125
+		catch (Exception $ex) {
1126
+			return false;
1127
+		}
1128
+	}
1129
+
1130
+	/**
1131
+	 * @return bool
1132
+	 * @todo move me to a collaborator
1133
+	 */
1134
+	public function oauthCanCreateAccount()
1135
+	{
1136
+		try {
1137
+			return in_array('useoauth', $this->getOAuthIdentity()->grants)
1138
+			&& in_array('createaccount', $this->getOAuthIdentity()->grants)
1139
+			&& in_array('createaccount', $this->getOAuthIdentity()->rights)
1140
+			&& in_array('writeapi', $this->getOAuthIdentity()->rights);
1141
+		}
1142
+		catch (Exception $ex) {
1143
+			return false;
1144
+		}
1145
+	}
1146
+
1147
+	/**
1148
+	 * @return bool
1149
+	 * @todo     move me to a collaborator
1150
+	 * @category Security-Critical
1151
+	 */
1152
+	protected function oauthCanCheckUser()
1153
+	{
1154
+		if (!$this->isOAuthLinked()) {
1155
+			return false;
1156
+		}
1157
+
1158
+		try {
1159
+			$identity = $this->getOAuthIdentity();
1160
+
1161
+			return in_array('checkuser', $identity->rights);
1162
+		}
1163
+		catch (Exception $ex) {
1164
+			return false;
1165
+		}
1166
+	}
1167
+
1168
+	#endregion
1169
+
1170
+	/**
1171
+	 * Gets a hash of data for the user to reset their password with.
1172
+	 * @category Security-Critical
1173
+	 * @return string
1174
+	 */
1175
+	public function getForgottenPasswordHash()
1176
+	{
1177
+		return md5($this->username . $this->email . $this->welcome_template . $this->id . $this->password);
1178
+	}
1179
+
1180
+	/**
1181
+	 * Gets the approval date of the user
1182
+	 * @return DateTime|false
1183
+	 */
1184
+	public function getApprovalDate()
1185
+	{
1186
+		$query = $this->dbObject->prepare(<<<SQL
1187 1187
 			SELECT timestamp 
1188 1188
 			FROM log 
1189 1189
 			WHERE objectid = :userid
@@ -1192,12 +1192,12 @@  discard block
 block discarded – undo
1192 1192
 			ORDER BY id DESC 
1193 1193
 			LIMIT 1;
1194 1194
 SQL
1195
-        );
1196
-        $query->execute(array(":userid" => $this->id));
1195
+		);
1196
+		$query->execute(array(":userid" => $this->id));
1197 1197
 
1198
-        $data = DateTime::createFromFormat("Y-m-d H:i:s", $query->fetchColumn());
1199
-        $query->closeCursor();
1198
+		$data = DateTime::createFromFormat("Y-m-d H:i:s", $query->fetchColumn());
1199
+		$query->closeCursor();
1200 1200
 
1201
-        return $data;
1202
-    }
1201
+		return $data;
1202
+	}
1203 1203
 }
Please login to merge, or discard this patch.
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -376,7 +376,7 @@  discard block
 block discarded – undo
376 376
         $userCount = count($userIds);
377 377
 
378 378
         // Firstly, let's create a string of question marks, which will do as positional parameters.
379
-        $inSection = str_repeat('?,', $userCount - 1) . '?';
379
+        $inSection = str_repeat('?,', $userCount - 1).'?';
380 380
 
381 381
         // Now, let's put that into the query. Direct string building here is OK, we're not dealing with user input,
382 382
         // only the *count* of user input, which is safe.
@@ -1091,7 +1091,7 @@  discard block
 block discarded – undo
1091 1091
             $this->dbObject->prepare("UPDATE user SET oauthidentitycache = NULL WHERE id = :id;")
1092 1092
                 ->execute(array(":id" => $this->id));
1093 1093
 
1094
-            SessionAlert::warning("OAuth error getting identity from MediaWiki: " . $ex->getMessage());
1094
+            SessionAlert::warning("OAuth error getting identity from MediaWiki: ".$ex->getMessage());
1095 1095
         }
1096 1096
     }
1097 1097
 
@@ -1174,7 +1174,7 @@  discard block
 block discarded – undo
1174 1174
      */
1175 1175
     public function getForgottenPasswordHash()
1176 1176
     {
1177
-        return md5($this->username . $this->email . $this->welcome_template . $this->id . $this->password);
1177
+        return md5($this->username.$this->email.$this->welcome_template.$this->id.$this->password);
1178 1178
     }
1179 1179
 
1180 1180
     /**
Please login to merge, or discard this patch.
includes/DataObjects/Log.php 1 patch
Indentation   +144 added lines, -144 removed lines patch added patch discarded remove patch
@@ -17,151 +17,151 @@
 block discarded – undo
17 17
  */
18 18
 class Log extends DataObject
19 19
 {
20
-    /** @var int */
21
-    private $objectid;
22
-    /** @var string */
23
-    private $objecttype;
24
-    /** @var int */
25
-    private $user;
26
-    /** @var string */
27
-    private $action;
28
-    private $timestamp;
29
-    /** @var string|null */
30
-    private $comment;
31
-
32
-    /**
33
-     * @throws Exception
34
-     */
35
-    public function save()
36
-    {
37
-        if ($this->isNew()) {
38
-            $statement = $this->dbObject->prepare(<<<SQL
20
+	/** @var int */
21
+	private $objectid;
22
+	/** @var string */
23
+	private $objecttype;
24
+	/** @var int */
25
+	private $user;
26
+	/** @var string */
27
+	private $action;
28
+	private $timestamp;
29
+	/** @var string|null */
30
+	private $comment;
31
+
32
+	/**
33
+	 * @throws Exception
34
+	 */
35
+	public function save()
36
+	{
37
+		if ($this->isNew()) {
38
+			$statement = $this->dbObject->prepare(<<<SQL
39 39
                 INSERT INTO log (objectid, objecttype, user, action, timestamp, comment) 
40 40
                 VALUES (:id, :type, :user, :action, CURRENT_TIMESTAMP(), :comment);
41 41
 SQL
42
-            );
43
-
44
-            $statement->bindValue(":id", $this->objectid);
45
-            $statement->bindValue(":type", $this->objecttype);
46
-            $statement->bindValue(":user", $this->user);
47
-            $statement->bindValue(":action", $this->action);
48
-            $statement->bindValue(":comment", $this->comment);
49
-
50
-            if ($statement->execute()) {
51
-                $this->id = (int)$this->dbObject->lastInsertId();
52
-            }
53
-            else {
54
-                throw new Exception($statement->errorInfo());
55
-            }
56
-        }
57
-        else {
58
-            throw new Exception("Updating logs is not available");
59
-        }
60
-    }
61
-
62
-    /**
63
-     * @throws Exception
64
-     */
65
-    public function delete()
66
-    {
67
-        throw new Exception("Deleting logs is not available.");
68
-    }
69
-
70
-    /**
71
-     * @return int
72
-     */
73
-    public function getObjectId()
74
-    {
75
-        return $this->objectid;
76
-    }
77
-
78
-    /**
79
-     * Summary of setObjectId
80
-     *
81
-     * @param int $objectId
82
-     */
83
-    public function setObjectId($objectId)
84
-    {
85
-        $this->objectid = $objectId;
86
-    }
87
-
88
-    /**
89
-     * @return string
90
-     */
91
-    public function getObjectType()
92
-    {
93
-        return $this->objecttype;
94
-    }
95
-
96
-    /**
97
-     * Summary of setObjectType
98
-     *
99
-     * @param string $objectType
100
-     */
101
-    public function setObjectType($objectType)
102
-    {
103
-        $this->objecttype = $objectType;
104
-    }
105
-
106
-    /**
107
-     * @return int
108
-     */
109
-    public function getUser()
110
-    {
111
-        return $this->user;
112
-    }
113
-
114
-    /**
115
-     * Summary of setUser
116
-     *
117
-     * @param User $user
118
-     */
119
-    public function setUser(User $user)
120
-    {
121
-        $this->user = $user->getId();
122
-    }
123
-
124
-    /**
125
-     * @return string
126
-     */
127
-    public function getAction()
128
-    {
129
-        return $this->action;
130
-    }
131
-
132
-    /**
133
-     * Summary of setAction
134
-     *
135
-     * @param string $action
136
-     */
137
-    public function setAction($action)
138
-    {
139
-        $this->action = $action;
140
-    }
141
-
142
-    /**
143
-     * @return DateTimeImmutable
144
-     */
145
-    public function getTimestamp()
146
-    {
147
-        return new DateTimeImmutable($this->timestamp);
148
-    }
149
-
150
-    /**
151
-     * @return string|null
152
-     */
153
-    public function getComment()
154
-    {
155
-        return $this->comment;
156
-    }
157
-
158
-    /**
159
-     * Summary of setComment
160
-     *
161
-     * @param string $comment
162
-     */
163
-    public function setComment($comment)
164
-    {
165
-        $this->comment = $comment;
166
-    }
42
+			);
43
+
44
+			$statement->bindValue(":id", $this->objectid);
45
+			$statement->bindValue(":type", $this->objecttype);
46
+			$statement->bindValue(":user", $this->user);
47
+			$statement->bindValue(":action", $this->action);
48
+			$statement->bindValue(":comment", $this->comment);
49
+
50
+			if ($statement->execute()) {
51
+				$this->id = (int)$this->dbObject->lastInsertId();
52
+			}
53
+			else {
54
+				throw new Exception($statement->errorInfo());
55
+			}
56
+		}
57
+		else {
58
+			throw new Exception("Updating logs is not available");
59
+		}
60
+	}
61
+
62
+	/**
63
+	 * @throws Exception
64
+	 */
65
+	public function delete()
66
+	{
67
+		throw new Exception("Deleting logs is not available.");
68
+	}
69
+
70
+	/**
71
+	 * @return int
72
+	 */
73
+	public function getObjectId()
74
+	{
75
+		return $this->objectid;
76
+	}
77
+
78
+	/**
79
+	 * Summary of setObjectId
80
+	 *
81
+	 * @param int $objectId
82
+	 */
83
+	public function setObjectId($objectId)
84
+	{
85
+		$this->objectid = $objectId;
86
+	}
87
+
88
+	/**
89
+	 * @return string
90
+	 */
91
+	public function getObjectType()
92
+	{
93
+		return $this->objecttype;
94
+	}
95
+
96
+	/**
97
+	 * Summary of setObjectType
98
+	 *
99
+	 * @param string $objectType
100
+	 */
101
+	public function setObjectType($objectType)
102
+	{
103
+		$this->objecttype = $objectType;
104
+	}
105
+
106
+	/**
107
+	 * @return int
108
+	 */
109
+	public function getUser()
110
+	{
111
+		return $this->user;
112
+	}
113
+
114
+	/**
115
+	 * Summary of setUser
116
+	 *
117
+	 * @param User $user
118
+	 */
119
+	public function setUser(User $user)
120
+	{
121
+		$this->user = $user->getId();
122
+	}
123
+
124
+	/**
125
+	 * @return string
126
+	 */
127
+	public function getAction()
128
+	{
129
+		return $this->action;
130
+	}
131
+
132
+	/**
133
+	 * Summary of setAction
134
+	 *
135
+	 * @param string $action
136
+	 */
137
+	public function setAction($action)
138
+	{
139
+		$this->action = $action;
140
+	}
141
+
142
+	/**
143
+	 * @return DateTimeImmutable
144
+	 */
145
+	public function getTimestamp()
146
+	{
147
+		return new DateTimeImmutable($this->timestamp);
148
+	}
149
+
150
+	/**
151
+	 * @return string|null
152
+	 */
153
+	public function getComment()
154
+	{
155
+		return $this->comment;
156
+	}
157
+
158
+	/**
159
+	 * Summary of setComment
160
+	 *
161
+	 * @param string $comment
162
+	 */
163
+	public function setComment($comment)
164
+	{
165
+		$this->comment = $comment;
166
+	}
167 167
 }
Please login to merge, or discard this patch.
includes/DataObjects/WelcomeTemplate.php 1 patch
Indentation   +156 added lines, -156 removed lines patch added patch discarded remove patch
@@ -19,167 +19,167 @@
 block discarded – undo
19 19
  */
20 20
 class WelcomeTemplate extends DataObject
21 21
 {
22
-    /** @var string */
23
-    private $usercode;
24
-    /** @var string */
25
-    private $botcode;
26
-    private $usageCache;
27
-    private $deleted = 0;
28
-
29
-    /**
30
-     * Summary of getAll
31
-     *
32
-     * @param PdoDatabase $database
33
-     *
34
-     * @return WelcomeTemplate[]
35
-     */
36
-    public static function getAll(PdoDatabase $database)
37
-    {
38
-        $statement = $database->prepare("SELECT * FROM welcometemplate WHERE deleted = 0;");
39
-
40
-        $statement->execute();
41
-
42
-        $result = array();
43
-        /** @var WelcomeTemplate $v */
44
-        foreach ($statement->fetchAll(PDO::FETCH_CLASS, self::class) as $v) {
45
-            $v->setDatabase($database);
46
-            $result[] = $v;
47
-        }
48
-
49
-        return $result;
50
-    }
51
-
52
-    /**
53
-     * @throws Exception
54
-     */
55
-    public function save()
56
-    {
57
-        if ($this->isNew()) {
58
-            // insert
59
-            $statement = $this->dbObject->prepare(<<<SQL
22
+	/** @var string */
23
+	private $usercode;
24
+	/** @var string */
25
+	private $botcode;
26
+	private $usageCache;
27
+	private $deleted = 0;
28
+
29
+	/**
30
+	 * Summary of getAll
31
+	 *
32
+	 * @param PdoDatabase $database
33
+	 *
34
+	 * @return WelcomeTemplate[]
35
+	 */
36
+	public static function getAll(PdoDatabase $database)
37
+	{
38
+		$statement = $database->prepare("SELECT * FROM welcometemplate WHERE deleted = 0;");
39
+
40
+		$statement->execute();
41
+
42
+		$result = array();
43
+		/** @var WelcomeTemplate $v */
44
+		foreach ($statement->fetchAll(PDO::FETCH_CLASS, self::class) as $v) {
45
+			$v->setDatabase($database);
46
+			$result[] = $v;
47
+		}
48
+
49
+		return $result;
50
+	}
51
+
52
+	/**
53
+	 * @throws Exception
54
+	 */
55
+	public function save()
56
+	{
57
+		if ($this->isNew()) {
58
+			// insert
59
+			$statement = $this->dbObject->prepare(<<<SQL
60 60
 INSERT INTO welcometemplate (usercode, botcode) VALUES (:usercode, :botcode);
61 61
 SQL
62
-            );
63
-            $statement->bindValue(":usercode", $this->usercode);
64
-            $statement->bindValue(":botcode", $this->botcode);
65
-
66
-            if ($statement->execute()) {
67
-                $this->id = (int)$this->dbObject->lastInsertId();
68
-            }
69
-            else {
70
-                throw new Exception($statement->errorInfo());
71
-            }
72
-        }
73
-        else {
74
-            // update
75
-            $statement = $this->dbObject->prepare(<<<SQL
62
+			);
63
+			$statement->bindValue(":usercode", $this->usercode);
64
+			$statement->bindValue(":botcode", $this->botcode);
65
+
66
+			if ($statement->execute()) {
67
+				$this->id = (int)$this->dbObject->lastInsertId();
68
+			}
69
+			else {
70
+				throw new Exception($statement->errorInfo());
71
+			}
72
+		}
73
+		else {
74
+			// update
75
+			$statement = $this->dbObject->prepare(<<<SQL
76 76
 UPDATE `welcometemplate`
77 77
 SET usercode = :usercode, botcode = :botcode, updateversion = updateversion + 1
78 78
 WHERE id = :id AND updateversion = :updateversion
79 79
 LIMIT 1;
80 80
 SQL
81
-            );
82
-
83
-            $statement->bindValue(':id', $this->id);
84
-            $statement->bindValue(':updateversion', $this->updateversion);
85
-
86
-            $statement->bindValue(':usercode', $this->usercode);
87
-            $statement->bindValue(':botcode', $this->botcode);
88
-
89
-            if (!$statement->execute()) {
90
-                throw new Exception($statement->errorInfo());
91
-            }
92
-
93
-            if ($statement->rowCount() !== 1) {
94
-                throw new OptimisticLockFailedException();
95
-            }
96
-
97
-            $this->updateversion++;
98
-        }
99
-    }
100
-
101
-    /**
102
-     * @return string
103
-     */
104
-    public function getUserCode()
105
-    {
106
-        return $this->usercode;
107
-    }
108
-
109
-    /**
110
-     * @param string $usercode
111
-     */
112
-    public function setUserCode($usercode)
113
-    {
114
-        $this->usercode = $usercode;
115
-    }
116
-
117
-    /**
118
-     * @return string
119
-     */
120
-    public function getBotCode()
121
-    {
122
-        return $this->botcode;
123
-    }
124
-
125
-    /**
126
-     * @param string $botcode
127
-     */
128
-    public function setBotCode($botcode)
129
-    {
130
-        $this->botcode = $botcode;
131
-    }
132
-
133
-    /**
134
-     * @return User[]
135
-     */
136
-    public function getUsersUsingTemplate()
137
-    {
138
-        if ($this->usageCache === null) {
139
-            $statement = $this->dbObject->prepare("SELECT * FROM user WHERE welcome_template = :id;");
140
-
141
-            $statement->execute(array(":id" => $this->id));
142
-
143
-            $result = array();
144
-            /** @var WelcomeTemplate $v */
145
-            foreach ($statement->fetchAll(PDO::FETCH_CLASS, User::class) as $v) {
146
-                $v->setDatabase($this->dbObject);
147
-                $result[] = $v;
148
-            }
149
-
150
-            $this->usageCache = $result;
151
-        }
152
-
153
-        return $this->usageCache;
154
-    }
155
-
156
-    /**
157
-     * Deletes the object from the database
158
-     */
159
-    public function delete()
160
-    {
161
-        if ($this->id === null) {
162
-            // wtf?
163
-            return;
164
-        }
165
-
166
-        $deleteQuery = "UPDATE welcometemplate SET deleted = 1 WHERE id = :id AND updateversion = :updateversion;";
167
-        $statement = $this->dbObject->prepare($deleteQuery);
168
-
169
-        $statement->bindValue(":id", $this->id);
170
-        $statement->bindValue(":updateversion", $this->updateversion);
171
-        $statement->execute();
172
-
173
-        if ($statement->rowCount() !== 1) {
174
-            throw new OptimisticLockFailedException();
175
-        }
176
-    }
177
-
178
-    /**
179
-     * @return bool
180
-     */
181
-    public function isDeleted()
182
-    {
183
-        return ((int)$this->deleted) === 1;
184
-    }
81
+			);
82
+
83
+			$statement->bindValue(':id', $this->id);
84
+			$statement->bindValue(':updateversion', $this->updateversion);
85
+
86
+			$statement->bindValue(':usercode', $this->usercode);
87
+			$statement->bindValue(':botcode', $this->botcode);
88
+
89
+			if (!$statement->execute()) {
90
+				throw new Exception($statement->errorInfo());
91
+			}
92
+
93
+			if ($statement->rowCount() !== 1) {
94
+				throw new OptimisticLockFailedException();
95
+			}
96
+
97
+			$this->updateversion++;
98
+		}
99
+	}
100
+
101
+	/**
102
+	 * @return string
103
+	 */
104
+	public function getUserCode()
105
+	{
106
+		return $this->usercode;
107
+	}
108
+
109
+	/**
110
+	 * @param string $usercode
111
+	 */
112
+	public function setUserCode($usercode)
113
+	{
114
+		$this->usercode = $usercode;
115
+	}
116
+
117
+	/**
118
+	 * @return string
119
+	 */
120
+	public function getBotCode()
121
+	{
122
+		return $this->botcode;
123
+	}
124
+
125
+	/**
126
+	 * @param string $botcode
127
+	 */
128
+	public function setBotCode($botcode)
129
+	{
130
+		$this->botcode = $botcode;
131
+	}
132
+
133
+	/**
134
+	 * @return User[]
135
+	 */
136
+	public function getUsersUsingTemplate()
137
+	{
138
+		if ($this->usageCache === null) {
139
+			$statement = $this->dbObject->prepare("SELECT * FROM user WHERE welcome_template = :id;");
140
+
141
+			$statement->execute(array(":id" => $this->id));
142
+
143
+			$result = array();
144
+			/** @var WelcomeTemplate $v */
145
+			foreach ($statement->fetchAll(PDO::FETCH_CLASS, User::class) as $v) {
146
+				$v->setDatabase($this->dbObject);
147
+				$result[] = $v;
148
+			}
149
+
150
+			$this->usageCache = $result;
151
+		}
152
+
153
+		return $this->usageCache;
154
+	}
155
+
156
+	/**
157
+	 * Deletes the object from the database
158
+	 */
159
+	public function delete()
160
+	{
161
+		if ($this->id === null) {
162
+			// wtf?
163
+			return;
164
+		}
165
+
166
+		$deleteQuery = "UPDATE welcometemplate SET deleted = 1 WHERE id = :id AND updateversion = :updateversion;";
167
+		$statement = $this->dbObject->prepare($deleteQuery);
168
+
169
+		$statement->bindValue(":id", $this->id);
170
+		$statement->bindValue(":updateversion", $this->updateversion);
171
+		$statement->execute();
172
+
173
+		if ($statement->rowCount() !== 1) {
174
+			throw new OptimisticLockFailedException();
175
+		}
176
+	}
177
+
178
+	/**
179
+	 * @return bool
180
+	 */
181
+	public function isDeleted()
182
+	{
183
+		return ((int)$this->deleted) === 1;
184
+	}
185 185
 }
Please login to merge, or discard this patch.
includes/Fragments/TemplateOutput.php 2 patches
Indentation   +59 added lines, -59 removed lines patch added patch discarded remove patch
@@ -15,73 +15,73 @@
 block discarded – undo
15 15
 
16 16
 trait TemplateOutput
17 17
 {
18
-    /** @var Smarty */
19
-    private $smarty;
20
-    /** @var string Extra JavaScript to include at the end of the page's execution */
21
-    private $tailScript;
18
+	/** @var Smarty */
19
+	private $smarty;
20
+	/** @var string Extra JavaScript to include at the end of the page's execution */
21
+	private $tailScript;
22 22
 
23
-    /**
24
-     * @return SiteConfiguration
25
-     */
26
-    protected abstract function getSiteConfiguration();
23
+	/**
24
+	 * @return SiteConfiguration
25
+	 */
26
+	protected abstract function getSiteConfiguration();
27 27
 
28
-    /**
29
-     * Include extra JavaScript at the end of the page's execution
30
-     *
31
-     * @param $script string JavaScript to include at the end of the page
32
-     */
33
-    final protected function setTailScript($script)
34
-    {
35
-        $this->tailScript = $script;
36
-    }
28
+	/**
29
+	 * Include extra JavaScript at the end of the page's execution
30
+	 *
31
+	 * @param $script string JavaScript to include at the end of the page
32
+	 */
33
+	final protected function setTailScript($script)
34
+	{
35
+		$this->tailScript = $script;
36
+	}
37 37
 
38
-    /**
39
-     * Assigns a Smarty variable
40
-     *
41
-     * @param  array|string $name  the template variable name(s)
42
-     * @param  mixed        $value the value to assign
43
-     */
44
-    final protected function assign($name, $value)
45
-    {
46
-        $this->smarty->assign($name, $value);
47
-    }
38
+	/**
39
+	 * Assigns a Smarty variable
40
+	 *
41
+	 * @param  array|string $name  the template variable name(s)
42
+	 * @param  mixed        $value the value to assign
43
+	 */
44
+	final protected function assign($name, $value)
45
+	{
46
+		$this->smarty->assign($name, $value);
47
+	}
48 48
 
49
-    /**
50
-     * Sets up the variables used by the main Smarty base template.
51
-     *
52
-     * This list is getting kinda long.
53
-     */
54
-    final protected function setUpSmarty()
55
-    {
56
-        $this->smarty = new Smarty();
57
-        $this->smarty->addPluginsDir($this->getSiteConfiguration()->getFilePath() . '/smarty-plugins');
49
+	/**
50
+	 * Sets up the variables used by the main Smarty base template.
51
+	 *
52
+	 * This list is getting kinda long.
53
+	 */
54
+	final protected function setUpSmarty()
55
+	{
56
+		$this->smarty = new Smarty();
57
+		$this->smarty->addPluginsDir($this->getSiteConfiguration()->getFilePath() . '/smarty-plugins');
58 58
 
59
-        $this->assign('currentUser', User::getCommunity());
60
-        $this->assign('loggedIn', false);
61
-        $this->assign('baseurl', $this->getSiteConfiguration()->getBaseUrl());
62
-        $this->assign('mediawikiScriptPath', $this->getSiteConfiguration()->getMediawikiScriptPath());
59
+		$this->assign('currentUser', User::getCommunity());
60
+		$this->assign('loggedIn', false);
61
+		$this->assign('baseurl', $this->getSiteConfiguration()->getBaseUrl());
62
+		$this->assign('mediawikiScriptPath', $this->getSiteConfiguration()->getMediawikiScriptPath());
63 63
 
64
-        $this->assign('siteNoticeText', '');
65
-        $this->assign('toolversion', Environment::getToolVersion());
64
+		$this->assign('siteNoticeText', '');
65
+		$this->assign('toolversion', Environment::getToolVersion());
66 66
 
67
-        // default these
68
-        $this->assign('onlineusers', array());
69
-        $this->assign('typeAheadBlock', '');
67
+		// default these
68
+		$this->assign('onlineusers', array());
69
+		$this->assign('typeAheadBlock', '');
70 70
 
71
-        $this->assign('page', $this);
72
-    }
71
+		$this->assign('page', $this);
72
+	}
73 73
 
74
-    /**
75
-     * Fetches a rendered Smarty template
76
-     *
77
-     * @param $template string Template file path, relative to /templates/
78
-     *
79
-     * @return string Templated HTML
80
-     */
81
-    final protected function fetchTemplate($template)
82
-    {
83
-        $this->assign("tailScript", $this->tailScript);
74
+	/**
75
+	 * Fetches a rendered Smarty template
76
+	 *
77
+	 * @param $template string Template file path, relative to /templates/
78
+	 *
79
+	 * @return string Templated HTML
80
+	 */
81
+	final protected function fetchTemplate($template)
82
+	{
83
+		$this->assign("tailScript", $this->tailScript);
84 84
 
85
-        return $this->smarty->fetch($template);
86
-    }
85
+		return $this->smarty->fetch($template);
86
+	}
87 87
 }
88 88
\ No newline at end of file
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -54,7 +54,7 @@
 block discarded – undo
54 54
     final protected function setUpSmarty()
55 55
     {
56 56
         $this->smarty = new Smarty();
57
-        $this->smarty->addPluginsDir($this->getSiteConfiguration()->getFilePath() . '/smarty-plugins');
57
+        $this->smarty->addPluginsDir($this->getSiteConfiguration()->getFilePath().'/smarty-plugins');
58 58
 
59 59
         $this->assign('currentUser', User::getCommunity());
60 60
         $this->assign('loggedIn', false);
Please login to merge, or discard this patch.
includes/Session.php 1 patch
Indentation   +17 added lines, -17 removed lines patch added patch discarded remove patch
@@ -17,25 +17,25 @@
 block discarded – undo
17 17
  */
18 18
 class Session
19 19
 {
20
-    public static function start()
21
-    {
22
-        ini_set('session.cookie_httponly', 1);
20
+	public static function start()
21
+	{
22
+		ini_set('session.cookie_httponly', 1);
23 23
 
24
-        if (WebRequest::isHttps()) {
25
-            ini_set('session.cookie_secure', 1);
26
-        }
24
+		if (WebRequest::isHttps()) {
25
+			ini_set('session.cookie_secure', 1);
26
+		}
27 27
 
28
-        session_start();
29
-    }
28
+		session_start();
29
+	}
30 30
 
31
-    public static function destroy()
32
-    {
33
-        session_destroy();
34
-    }
31
+	public static function destroy()
32
+	{
33
+		session_destroy();
34
+	}
35 35
 
36
-    public static function restart()
37
-    {
38
-        self::destroy();
39
-        self::start();
40
-    }
36
+	public static function restart()
37
+	{
38
+		self::destroy();
39
+		self::start();
40
+	}
41 41
 }
Please login to merge, or discard this patch.
includes/SessionAlert.php 1 patch
Indentation   +137 added lines, -137 removed lines patch added patch discarded remove patch
@@ -21,141 +21,141 @@
 block discarded – undo
21 21
  */
22 22
 class SessionAlert
23 23
 {
24
-    private $message;
25
-    private $title;
26
-    private $type;
27
-    private $closable;
28
-    private $block;
29
-
30
-    /**
31
-     * @param string $message
32
-     * @param string $title
33
-     * @param string $type
34
-     * @param bool   $closable
35
-     * @param bool   $block
36
-     */
37
-    public function __construct($message, $title, $type = "alert-info", $closable = true, $block = true)
38
-    {
39
-        $this->message = $message;
40
-        $this->title = $title;
41
-        $this->type = $type;
42
-        $this->closable = $closable;
43
-        $this->block = $block;
44
-    }
45
-
46
-    /**
47
-     * Shows a quick one-liner message
48
-     *
49
-     * @param string $message
50
-     * @param string $type
51
-     */
52
-    public static function quick($message, $type = "alert-info")
53
-    {
54
-        self::append(new SessionAlert($message, "", $type, true, false));
55
-    }
56
-
57
-    /**
58
-     * @param SessionAlert $alert
59
-     */
60
-    public static function append(SessionAlert $alert)
61
-    {
62
-        $data = WebRequest::getSessionAlertData();
63
-        $data[] = serialize($alert);
64
-        WebRequest::setSessionAlertData($data);
65
-    }
66
-
67
-    /**
68
-     * Shows a quick one-liner success message
69
-     *
70
-     * @param string $message
71
-     */
72
-    public static function success($message)
73
-    {
74
-        self::append(new SessionAlert($message, "", "alert-success", true, true));
75
-    }
76
-
77
-    /**
78
-     * Shows a quick one-liner warning message
79
-     *
80
-     * @param string $message
81
-     * @param string $title
82
-     */
83
-    public static function warning($message, $title = "Warning!")
84
-    {
85
-        self::append(new SessionAlert($message, $title, "alert-warning", true, true));
86
-    }
87
-
88
-    /**
89
-     * Shows a quick one-liner error message
90
-     *
91
-     * @param string $message
92
-     * @param string $title
93
-     */
94
-    public static function error($message, $title = "Error!")
95
-    {
96
-        self::append(new SessionAlert($message, $title, "alert-error", true, true));
97
-    }
98
-
99
-    /**
100
-     * Retrieves the alerts which have been saved to the session
101
-     * @return array
102
-     */
103
-    public static function getAlerts()
104
-    {
105
-        $alertData = array();
106
-
107
-        foreach (WebRequest::getSessionAlertData() as $a) {
108
-            $alertData[] = unserialize($a);
109
-        }
110
-
111
-        return $alertData;
112
-    }
113
-
114
-    /**
115
-     * Clears the alerts from the session
116
-     */
117
-    public static function clearAlerts()
118
-    {
119
-        WebRequest::clearSessionAlertData();
120
-    }
121
-
122
-    /**
123
-     * @return boolean
124
-     */
125
-    public function isBlock()
126
-    {
127
-        return $this->block;
128
-    }
129
-
130
-    /**
131
-     * @return boolean
132
-     */
133
-    public function isClosable()
134
-    {
135
-        return $this->closable;
136
-    }
137
-
138
-    /**
139
-     * @return string
140
-     */
141
-    public function getType()
142
-    {
143
-        return $this->type;
144
-    }
145
-
146
-    /**
147
-     * @return string
148
-     */
149
-    public function getTitle()
150
-    {
151
-        return $this->title;
152
-    }
153
-
154
-    /**
155
-     * @return string
156
-     */
157
-    public function getMessage()
158
-    {
159
-        return $this->message;
160
-    }
24
+	private $message;
25
+	private $title;
26
+	private $type;
27
+	private $closable;
28
+	private $block;
29
+
30
+	/**
31
+	 * @param string $message
32
+	 * @param string $title
33
+	 * @param string $type
34
+	 * @param bool   $closable
35
+	 * @param bool   $block
36
+	 */
37
+	public function __construct($message, $title, $type = "alert-info", $closable = true, $block = true)
38
+	{
39
+		$this->message = $message;
40
+		$this->title = $title;
41
+		$this->type = $type;
42
+		$this->closable = $closable;
43
+		$this->block = $block;
44
+	}
45
+
46
+	/**
47
+	 * Shows a quick one-liner message
48
+	 *
49
+	 * @param string $message
50
+	 * @param string $type
51
+	 */
52
+	public static function quick($message, $type = "alert-info")
53
+	{
54
+		self::append(new SessionAlert($message, "", $type, true, false));
55
+	}
56
+
57
+	/**
58
+	 * @param SessionAlert $alert
59
+	 */
60
+	public static function append(SessionAlert $alert)
61
+	{
62
+		$data = WebRequest::getSessionAlertData();
63
+		$data[] = serialize($alert);
64
+		WebRequest::setSessionAlertData($data);
65
+	}
66
+
67
+	/**
68
+	 * Shows a quick one-liner success message
69
+	 *
70
+	 * @param string $message
71
+	 */
72
+	public static function success($message)
73
+	{
74
+		self::append(new SessionAlert($message, "", "alert-success", true, true));
75
+	}
76
+
77
+	/**
78
+	 * Shows a quick one-liner warning message
79
+	 *
80
+	 * @param string $message
81
+	 * @param string $title
82
+	 */
83
+	public static function warning($message, $title = "Warning!")
84
+	{
85
+		self::append(new SessionAlert($message, $title, "alert-warning", true, true));
86
+	}
87
+
88
+	/**
89
+	 * Shows a quick one-liner error message
90
+	 *
91
+	 * @param string $message
92
+	 * @param string $title
93
+	 */
94
+	public static function error($message, $title = "Error!")
95
+	{
96
+		self::append(new SessionAlert($message, $title, "alert-error", true, true));
97
+	}
98
+
99
+	/**
100
+	 * Retrieves the alerts which have been saved to the session
101
+	 * @return array
102
+	 */
103
+	public static function getAlerts()
104
+	{
105
+		$alertData = array();
106
+
107
+		foreach (WebRequest::getSessionAlertData() as $a) {
108
+			$alertData[] = unserialize($a);
109
+		}
110
+
111
+		return $alertData;
112
+	}
113
+
114
+	/**
115
+	 * Clears the alerts from the session
116
+	 */
117
+	public static function clearAlerts()
118
+	{
119
+		WebRequest::clearSessionAlertData();
120
+	}
121
+
122
+	/**
123
+	 * @return boolean
124
+	 */
125
+	public function isBlock()
126
+	{
127
+		return $this->block;
128
+	}
129
+
130
+	/**
131
+	 * @return boolean
132
+	 */
133
+	public function isClosable()
134
+	{
135
+		return $this->closable;
136
+	}
137
+
138
+	/**
139
+	 * @return string
140
+	 */
141
+	public function getType()
142
+	{
143
+		return $this->type;
144
+	}
145
+
146
+	/**
147
+	 * @return string
148
+	 */
149
+	public function getTitle()
150
+	{
151
+		return $this->title;
152
+	}
153
+
154
+	/**
155
+	 * @return string
156
+	 */
157
+	public function getMessage()
158
+	{
159
+		return $this->message;
160
+	}
161 161
 }
Please login to merge, or discard this patch.
includes/API/IApiAction.php 1 patch
Indentation   +12 added lines, -12 removed lines patch added patch discarded remove patch
@@ -17,17 +17,17 @@
 block discarded – undo
17 17
  */
18 18
 interface IApiAction extends IRoutedTask
19 19
 {
20
-    /**
21
-     * Method that runs API action
22
-     *
23
-     * @param DOMElement $apiDocument
24
-     *
25
-     * @return DOMElement The modified API document
26
-     */
27
-    public function executeApiAction(DOMElement $apiDocument);
20
+	/**
21
+	 * Method that runs API action
22
+	 *
23
+	 * @param DOMElement $apiDocument
24
+	 *
25
+	 * @return DOMElement The modified API document
26
+	 */
27
+	public function executeApiAction(DOMElement $apiDocument);
28 28
 
29
-    /**
30
-     * @return string the XML, or false if an error occurred.
31
-     */
32
-    public function runApiPage();
29
+	/**
30
+	 * @return string the XML, or false if an error occurred.
31
+	 */
32
+	public function runApiPage();
33 33
 }
Please login to merge, or discard this patch.