@@ -12,20 +12,20 @@ |
||
12 | 12 | |
13 | 13 | interface ICredentialProvider |
14 | 14 | { |
15 | - /** |
|
16 | - * Validates a user-provided credential |
|
17 | - * |
|
18 | - * @param User $user The user to test the authentication against |
|
19 | - * @param string $data The raw credential data to be validated |
|
20 | - * |
|
21 | - * @return bool |
|
22 | - */ |
|
23 | - public function authenticate(User $user, $data); |
|
15 | + /** |
|
16 | + * Validates a user-provided credential |
|
17 | + * |
|
18 | + * @param User $user The user to test the authentication against |
|
19 | + * @param string $data The raw credential data to be validated |
|
20 | + * |
|
21 | + * @return bool |
|
22 | + */ |
|
23 | + public function authenticate(User $user, $data); |
|
24 | 24 | |
25 | - /** |
|
26 | - * @param User $user The user the credential belongs to |
|
27 | - * @param int $factor The factor this credential provides |
|
28 | - * @param string $data |
|
29 | - */ |
|
30 | - public function setCredential(User $user, $factor, $data); |
|
25 | + /** |
|
26 | + * @param User $user The user the credential belongs to |
|
27 | + * @param int $factor The factor this credential provides |
|
28 | + * @param string $data |
|
29 | + */ |
|
30 | + public function setCredential(User $user, $factor, $data); |
|
31 | 31 | } |
32 | 32 | \ No newline at end of file |
@@ -14,50 +14,50 @@ |
||
14 | 14 | |
15 | 15 | class PasswordCredentialProvider extends CredentialProviderBase |
16 | 16 | { |
17 | - const PASSWORD_COST = 10; |
|
18 | - |
|
19 | - public function __construct(PdoDatabase $database, SiteConfiguration $configuration) |
|
20 | - { |
|
21 | - parent::__construct($database, $configuration, 'password'); |
|
22 | - } |
|
23 | - |
|
24 | - public function authenticate(User $user, $data) |
|
25 | - { |
|
26 | - $storedData = $this->getCredentialData($user->getId()); |
|
27 | - if($storedData === null) |
|
28 | - { |
|
29 | - // No available credential matching these parameters |
|
30 | - return false; |
|
31 | - } |
|
32 | - |
|
33 | - if($storedData->getVersion() !== 2) { |
|
34 | - // Non-2 versions are not supported. |
|
35 | - return false; |
|
36 | - } |
|
37 | - |
|
38 | - if(password_verify($data, $storedData->getData())) { |
|
39 | - if(password_needs_rehash($storedData->getData(), PASSWORD_BCRYPT, array('cost' => self::PASSWORD_COST))){ |
|
40 | - $this->setCredential($user, $storedData->getFactor(), $data); |
|
41 | - } |
|
42 | - |
|
43 | - return true; |
|
44 | - } |
|
45 | - |
|
46 | - return false; |
|
47 | - } |
|
48 | - |
|
49 | - public function setCredential(User $user, $factor, $password) |
|
50 | - { |
|
51 | - $storedData = $this->getCredentialData($user->getId()); |
|
52 | - |
|
53 | - if($storedData === null){ |
|
54 | - $storedData = $this->createNewCredential($user); |
|
55 | - } |
|
56 | - |
|
57 | - $storedData->setData(password_hash($password, PASSWORD_BCRYPT, array('cost' => self::PASSWORD_COST))); |
|
58 | - $storedData->setFactor($factor); |
|
59 | - $storedData->setVersion(2); |
|
60 | - |
|
61 | - $storedData->save(); |
|
62 | - } |
|
17 | + const PASSWORD_COST = 10; |
|
18 | + |
|
19 | + public function __construct(PdoDatabase $database, SiteConfiguration $configuration) |
|
20 | + { |
|
21 | + parent::__construct($database, $configuration, 'password'); |
|
22 | + } |
|
23 | + |
|
24 | + public function authenticate(User $user, $data) |
|
25 | + { |
|
26 | + $storedData = $this->getCredentialData($user->getId()); |
|
27 | + if($storedData === null) |
|
28 | + { |
|
29 | + // No available credential matching these parameters |
|
30 | + return false; |
|
31 | + } |
|
32 | + |
|
33 | + if($storedData->getVersion() !== 2) { |
|
34 | + // Non-2 versions are not supported. |
|
35 | + return false; |
|
36 | + } |
|
37 | + |
|
38 | + if(password_verify($data, $storedData->getData())) { |
|
39 | + if(password_needs_rehash($storedData->getData(), PASSWORD_BCRYPT, array('cost' => self::PASSWORD_COST))){ |
|
40 | + $this->setCredential($user, $storedData->getFactor(), $data); |
|
41 | + } |
|
42 | + |
|
43 | + return true; |
|
44 | + } |
|
45 | + |
|
46 | + return false; |
|
47 | + } |
|
48 | + |
|
49 | + public function setCredential(User $user, $factor, $password) |
|
50 | + { |
|
51 | + $storedData = $this->getCredentialData($user->getId()); |
|
52 | + |
|
53 | + if($storedData === null){ |
|
54 | + $storedData = $this->createNewCredential($user); |
|
55 | + } |
|
56 | + |
|
57 | + $storedData->setData(password_hash($password, PASSWORD_BCRYPT, array('cost' => self::PASSWORD_COST))); |
|
58 | + $storedData->setFactor($factor); |
|
59 | + $storedData->setVersion(2); |
|
60 | + |
|
61 | + $storedData->save(); |
|
62 | + } |
|
63 | 63 | } |
64 | 64 | \ No newline at end of file |
@@ -24,19 +24,19 @@ discard block |
||
24 | 24 | public function authenticate(User $user, $data) |
25 | 25 | { |
26 | 26 | $storedData = $this->getCredentialData($user->getId()); |
27 | - if($storedData === null) |
|
27 | + if ($storedData === null) |
|
28 | 28 | { |
29 | 29 | // No available credential matching these parameters |
30 | 30 | return false; |
31 | 31 | } |
32 | 32 | |
33 | - if($storedData->getVersion() !== 2) { |
|
33 | + if ($storedData->getVersion() !== 2) { |
|
34 | 34 | // Non-2 versions are not supported. |
35 | 35 | return false; |
36 | 36 | } |
37 | 37 | |
38 | - if(password_verify($data, $storedData->getData())) { |
|
39 | - if(password_needs_rehash($storedData->getData(), PASSWORD_BCRYPT, array('cost' => self::PASSWORD_COST))){ |
|
38 | + if (password_verify($data, $storedData->getData())) { |
|
39 | + if (password_needs_rehash($storedData->getData(), PASSWORD_BCRYPT, array('cost' => self::PASSWORD_COST))) { |
|
40 | 40 | $this->setCredential($user, $storedData->getFactor(), $data); |
41 | 41 | } |
42 | 42 | |
@@ -50,7 +50,7 @@ discard block |
||
50 | 50 | { |
51 | 51 | $storedData = $this->getCredentialData($user->getId()); |
52 | 52 | |
53 | - if($storedData === null){ |
|
53 | + if ($storedData === null) { |
|
54 | 54 | $storedData = $this->createNewCredential($user); |
55 | 55 | } |
56 | 56 |
@@ -24,8 +24,7 @@ discard block |
||
24 | 24 | public function authenticate(User $user, $data) |
25 | 25 | { |
26 | 26 | $storedData = $this->getCredentialData($user->getId()); |
27 | - if($storedData === null) |
|
28 | - { |
|
27 | + if($storedData === null) { |
|
29 | 28 | // No available credential matching these parameters |
30 | 29 | return false; |
31 | 30 | } |
@@ -36,7 +35,7 @@ discard block |
||
36 | 35 | } |
37 | 36 | |
38 | 37 | if(password_verify($data, $storedData->getData())) { |
39 | - if(password_needs_rehash($storedData->getData(), PASSWORD_BCRYPT, array('cost' => self::PASSWORD_COST))){ |
|
38 | + if(password_needs_rehash($storedData->getData(), PASSWORD_BCRYPT, array('cost' => self::PASSWORD_COST))) { |
|
40 | 39 | $this->setCredential($user, $storedData->getFactor(), $data); |
41 | 40 | } |
42 | 41 | |
@@ -50,7 +49,7 @@ discard block |
||
50 | 49 | { |
51 | 50 | $storedData = $this->getCredentialData($user->getId()); |
52 | 51 | |
53 | - if($storedData === null){ |
|
52 | + if($storedData === null) { |
|
54 | 53 | $storedData = $this->createNewCredential($user); |
55 | 54 | } |
56 | 55 |
@@ -15,85 +15,85 @@ |
||
15 | 15 | |
16 | 16 | abstract class CredentialProviderBase implements ICredentialProvider |
17 | 17 | { |
18 | - /** |
|
19 | - * @var PdoDatabase |
|
20 | - */ |
|
21 | - private $database; |
|
22 | - /** |
|
23 | - * @var SiteConfiguration |
|
24 | - */ |
|
25 | - private $configuration; |
|
26 | - /** @var string */ |
|
27 | - private $type; |
|
18 | + /** |
|
19 | + * @var PdoDatabase |
|
20 | + */ |
|
21 | + private $database; |
|
22 | + /** |
|
23 | + * @var SiteConfiguration |
|
24 | + */ |
|
25 | + private $configuration; |
|
26 | + /** @var string */ |
|
27 | + private $type; |
|
28 | 28 | |
29 | - /** |
|
30 | - * CredentialProviderBase constructor. |
|
31 | - * |
|
32 | - * @param PdoDatabase $database |
|
33 | - * @param SiteConfiguration $configuration |
|
34 | - * @param string $type |
|
35 | - */ |
|
36 | - public function __construct(PdoDatabase $database, SiteConfiguration $configuration, $type) |
|
37 | - { |
|
38 | - $this->database = $database; |
|
39 | - $this->configuration = $configuration; |
|
40 | - $this->type = $type; |
|
41 | - } |
|
29 | + /** |
|
30 | + * CredentialProviderBase constructor. |
|
31 | + * |
|
32 | + * @param PdoDatabase $database |
|
33 | + * @param SiteConfiguration $configuration |
|
34 | + * @param string $type |
|
35 | + */ |
|
36 | + public function __construct(PdoDatabase $database, SiteConfiguration $configuration, $type) |
|
37 | + { |
|
38 | + $this->database = $database; |
|
39 | + $this->configuration = $configuration; |
|
40 | + $this->type = $type; |
|
41 | + } |
|
42 | 42 | |
43 | - /** |
|
44 | - * @param int $userId |
|
45 | - * |
|
46 | - * @return Credential |
|
47 | - */ |
|
48 | - protected function getCredentialData($userId) |
|
49 | - { |
|
50 | - $sql = 'SELECT * FROM credential WHERE type = :t AND user = :u AND disabled = 0'; |
|
43 | + /** |
|
44 | + * @param int $userId |
|
45 | + * |
|
46 | + * @return Credential |
|
47 | + */ |
|
48 | + protected function getCredentialData($userId) |
|
49 | + { |
|
50 | + $sql = 'SELECT * FROM credential WHERE type = :t AND user = :u AND disabled = 0'; |
|
51 | 51 | |
52 | - $statement = $this->database->prepare($sql); |
|
53 | - $statement->execute(array(':u' => $userId, ':t' => $this->type)); |
|
52 | + $statement = $this->database->prepare($sql); |
|
53 | + $statement->execute(array(':u' => $userId, ':t' => $this->type)); |
|
54 | 54 | |
55 | - /** @var Credential $obj */ |
|
56 | - $obj = $statement->fetchObject(Credential::class); |
|
55 | + /** @var Credential $obj */ |
|
56 | + $obj = $statement->fetchObject(Credential::class); |
|
57 | 57 | |
58 | - if ($obj === false) { |
|
59 | - return null; |
|
60 | - } |
|
58 | + if ($obj === false) { |
|
59 | + return null; |
|
60 | + } |
|
61 | 61 | |
62 | - $obj->setDatabase($this->database); |
|
62 | + $obj->setDatabase($this->database); |
|
63 | 63 | |
64 | - $statement->closeCursor(); |
|
64 | + $statement->closeCursor(); |
|
65 | 65 | |
66 | - return $obj; |
|
67 | - } |
|
66 | + return $obj; |
|
67 | + } |
|
68 | 68 | |
69 | - /** |
|
70 | - * @return PdoDatabase |
|
71 | - */ |
|
72 | - public function getDatabase() |
|
73 | - { |
|
74 | - return $this->database; |
|
75 | - } |
|
69 | + /** |
|
70 | + * @return PdoDatabase |
|
71 | + */ |
|
72 | + public function getDatabase() |
|
73 | + { |
|
74 | + return $this->database; |
|
75 | + } |
|
76 | 76 | |
77 | - /** |
|
78 | - * @return SiteConfiguration |
|
79 | - */ |
|
80 | - public function getConfiguration() |
|
81 | - { |
|
82 | - return $this->configuration; |
|
83 | - } |
|
77 | + /** |
|
78 | + * @return SiteConfiguration |
|
79 | + */ |
|
80 | + public function getConfiguration() |
|
81 | + { |
|
82 | + return $this->configuration; |
|
83 | + } |
|
84 | 84 | |
85 | - /** |
|
86 | - * @param User $user |
|
87 | - * |
|
88 | - * @return Credential |
|
89 | - */ |
|
90 | - protected function createNewCredential(User $user) |
|
91 | - { |
|
92 | - $credential = new Credential(); |
|
93 | - $credential->setDatabase($this->getDatabase()); |
|
94 | - $credential->setUserId($user->getId()); |
|
95 | - $credential->setType($this->type); |
|
85 | + /** |
|
86 | + * @param User $user |
|
87 | + * |
|
88 | + * @return Credential |
|
89 | + */ |
|
90 | + protected function createNewCredential(User $user) |
|
91 | + { |
|
92 | + $credential = new Credential(); |
|
93 | + $credential->setDatabase($this->getDatabase()); |
|
94 | + $credential->setUserId($user->getId()); |
|
95 | + $credential->setType($this->type); |
|
96 | 96 | |
97 | - return $credential; |
|
98 | - } |
|
97 | + return $credential; |
|
98 | + } |
|
99 | 99 | } |
100 | 100 | \ No newline at end of file |
@@ -20,58 +20,58 @@ |
||
20 | 20 | */ |
21 | 21 | class StatsAction extends ApiPageBase implements IApiAction |
22 | 22 | { |
23 | - /** |
|
24 | - * The target user |
|
25 | - * @var User $user |
|
26 | - */ |
|
27 | - private $user; |
|
23 | + /** |
|
24 | + * The target user |
|
25 | + * @var User $user |
|
26 | + */ |
|
27 | + private $user; |
|
28 | 28 | |
29 | - /** |
|
30 | - * Summary of execute |
|
31 | - * |
|
32 | - * @param \DOMElement $apiDocument |
|
33 | - * |
|
34 | - * @return \DOMElement |
|
35 | - * @throws ApiException |
|
36 | - * @throws \Exception |
|
37 | - */ |
|
38 | - public function executeApiAction(\DOMElement $apiDocument) |
|
39 | - { |
|
40 | - $username = WebRequest::getString('user'); |
|
41 | - $wikiusername = WebRequest::getString('wikiuser'); |
|
29 | + /** |
|
30 | + * Summary of execute |
|
31 | + * |
|
32 | + * @param \DOMElement $apiDocument |
|
33 | + * |
|
34 | + * @return \DOMElement |
|
35 | + * @throws ApiException |
|
36 | + * @throws \Exception |
|
37 | + */ |
|
38 | + public function executeApiAction(\DOMElement $apiDocument) |
|
39 | + { |
|
40 | + $username = WebRequest::getString('user'); |
|
41 | + $wikiusername = WebRequest::getString('wikiuser'); |
|
42 | 42 | |
43 | - if ($username === null && $wikiusername === null) { |
|
44 | - throw new ApiException("Please specify a username using either user or wikiuser parameters."); |
|
45 | - } |
|
43 | + if ($username === null && $wikiusername === null) { |
|
44 | + throw new ApiException("Please specify a username using either user or wikiuser parameters."); |
|
45 | + } |
|
46 | 46 | |
47 | - $userElement = $this->document->createElement("user"); |
|
48 | - $apiDocument->appendChild($userElement); |
|
47 | + $userElement = $this->document->createElement("user"); |
|
48 | + $apiDocument->appendChild($userElement); |
|
49 | 49 | |
50 | - if ($username !== null) { |
|
51 | - $user = User::getByUsername($username, $this->getDatabase()); |
|
52 | - } |
|
53 | - else { |
|
54 | - $user = User::getByOnWikiUsername($wikiusername, $this->getDatabase()); |
|
55 | - } |
|
50 | + if ($username !== null) { |
|
51 | + $user = User::getByUsername($username, $this->getDatabase()); |
|
52 | + } |
|
53 | + else { |
|
54 | + $user = User::getByOnWikiUsername($wikiusername, $this->getDatabase()); |
|
55 | + } |
|
56 | 56 | |
57 | - if ($user === false) { |
|
58 | - $userElement->setAttribute("missing", "true"); |
|
57 | + if ($user === false) { |
|
58 | + $userElement->setAttribute("missing", "true"); |
|
59 | 59 | |
60 | - return $apiDocument; |
|
61 | - } |
|
60 | + return $apiDocument; |
|
61 | + } |
|
62 | 62 | |
63 | - $this->user = $user; |
|
63 | + $this->user = $user; |
|
64 | 64 | |
65 | - $oauth = new OAuthUserHelper($user, $this->getDatabase(), $this->getOAuthProtocolHelper(), |
|
66 | - $this->getSiteConfiguration()); |
|
65 | + $oauth = new OAuthUserHelper($user, $this->getDatabase(), $this->getOAuthProtocolHelper(), |
|
66 | + $this->getSiteConfiguration()); |
|
67 | 67 | |
68 | - $userElement->setAttribute("username", $this->user->getUsername()); |
|
69 | - $userElement->setAttribute("status", $this->user->getStatus()); |
|
70 | - $userElement->setAttribute("lastactive", $this->user->getLastActive()); |
|
71 | - $userElement->setAttribute("welcome_template", $this->user->getWelcomeTemplate()); |
|
72 | - $userElement->setAttribute("onwikiname", $this->user->getOnWikiName()); |
|
73 | - $userElement->setAttribute("oauth", $oauth->isFullyLinked() ? "true" : "false"); |
|
68 | + $userElement->setAttribute("username", $this->user->getUsername()); |
|
69 | + $userElement->setAttribute("status", $this->user->getStatus()); |
|
70 | + $userElement->setAttribute("lastactive", $this->user->getLastActive()); |
|
71 | + $userElement->setAttribute("welcome_template", $this->user->getWelcomeTemplate()); |
|
72 | + $userElement->setAttribute("onwikiname", $this->user->getOnWikiName()); |
|
73 | + $userElement->setAttribute("oauth", $oauth->isFullyLinked() ? "true" : "false"); |
|
74 | 74 | |
75 | - return $apiDocument; |
|
76 | - } |
|
75 | + return $apiDocument; |
|
76 | + } |
|
77 | 77 | } |
@@ -19,52 +19,52 @@ |
||
19 | 19 | |
20 | 20 | interface IOAuthProtocolHelper |
21 | 21 | { |
22 | - /** |
|
23 | - * @return stdClass |
|
24 | - * |
|
25 | - * @throws Exception |
|
26 | - * @throws CurlException |
|
27 | - */ |
|
28 | - public function getRequestToken(); |
|
22 | + /** |
|
23 | + * @return stdClass |
|
24 | + * |
|
25 | + * @throws Exception |
|
26 | + * @throws CurlException |
|
27 | + */ |
|
28 | + public function getRequestToken(); |
|
29 | 29 | |
30 | - /** |
|
31 | - * @param string $requestToken |
|
32 | - * |
|
33 | - * @return string |
|
34 | - */ |
|
35 | - public function getAuthoriseUrl($requestToken); |
|
30 | + /** |
|
31 | + * @param string $requestToken |
|
32 | + * |
|
33 | + * @return string |
|
34 | + */ |
|
35 | + public function getAuthoriseUrl($requestToken); |
|
36 | 36 | |
37 | - /** |
|
38 | - * @param string $oauthRequestToken |
|
39 | - * @param string $oauthRequestSecret |
|
40 | - * @param string $oauthVerifier |
|
41 | - * |
|
42 | - * @return stdClass |
|
43 | - * @throws CurlException |
|
44 | - * @throws Exception |
|
45 | - */ |
|
46 | - public function callbackCompleted($oauthRequestToken, $oauthRequestSecret, $oauthVerifier); |
|
37 | + /** |
|
38 | + * @param string $oauthRequestToken |
|
39 | + * @param string $oauthRequestSecret |
|
40 | + * @param string $oauthVerifier |
|
41 | + * |
|
42 | + * @return stdClass |
|
43 | + * @throws CurlException |
|
44 | + * @throws Exception |
|
45 | + */ |
|
46 | + public function callbackCompleted($oauthRequestToken, $oauthRequestSecret, $oauthVerifier); |
|
47 | 47 | |
48 | - /** |
|
49 | - * @param string $oauthAccessToken |
|
50 | - * @param string $oauthAccessSecret |
|
51 | - * |
|
52 | - * @return stdClass |
|
53 | - * @throws CurlException |
|
54 | - * @throws Exception |
|
55 | - */ |
|
56 | - public function getIdentityTicket($oauthAccessToken, $oauthAccessSecret); |
|
48 | + /** |
|
49 | + * @param string $oauthAccessToken |
|
50 | + * @param string $oauthAccessSecret |
|
51 | + * |
|
52 | + * @return stdClass |
|
53 | + * @throws CurlException |
|
54 | + * @throws Exception |
|
55 | + */ |
|
56 | + public function getIdentityTicket($oauthAccessToken, $oauthAccessSecret); |
|
57 | 57 | |
58 | - /** |
|
59 | - * @param array $apiParams array of parameters to send to the API |
|
60 | - * @param string $accessToken user's access token |
|
61 | - * @param string $accessSecret user's secret |
|
62 | - * @param string $method HTTP method |
|
63 | - * |
|
64 | - * @return stdClass |
|
65 | - * @throws ApplicationLogicException |
|
66 | - * @throws CurlException |
|
67 | - * @throws Exception |
|
68 | - */ |
|
69 | - public function apiCall($apiParams, $accessToken, $accessSecret, $method = 'GET'); |
|
58 | + /** |
|
59 | + * @param array $apiParams array of parameters to send to the API |
|
60 | + * @param string $accessToken user's access token |
|
61 | + * @param string $accessSecret user's secret |
|
62 | + * @param string $method HTTP method |
|
63 | + * |
|
64 | + * @return stdClass |
|
65 | + * @throws ApplicationLogicException |
|
66 | + * @throws CurlException |
|
67 | + * @throws Exception |
|
68 | + */ |
|
69 | + public function apiCall($apiParams, $accessToken, $accessSecret, $method = 'GET'); |
|
70 | 70 | } |
71 | 71 | \ No newline at end of file |
@@ -12,109 +12,109 @@ |
||
12 | 12 | |
13 | 13 | class HttpHelper |
14 | 14 | { |
15 | - private $curlHandle; |
|
16 | - |
|
17 | - /** |
|
18 | - * HttpHelper constructor. |
|
19 | - * |
|
20 | - * @param string $userAgent |
|
21 | - * @param boolean $disableVerifyPeer |
|
22 | - * @param string $cookieJar |
|
23 | - */ |
|
24 | - public function __construct($userAgent, $disableVerifyPeer, $cookieJar = null) |
|
25 | - { |
|
26 | - $this->curlHandle = curl_init(); |
|
27 | - |
|
28 | - curl_setopt($this->curlHandle, CURLOPT_RETURNTRANSFER, true); |
|
29 | - curl_setopt($this->curlHandle, CURLOPT_USERAGENT, $userAgent); |
|
30 | - curl_setopt($this->curlHandle, CURLOPT_FAILONERROR, true); |
|
31 | - |
|
32 | - if ($disableVerifyPeer) { |
|
33 | - curl_setopt($this->curlHandle, CURLOPT_SSL_VERIFYPEER, false); |
|
34 | - } |
|
35 | - |
|
36 | - if($cookieJar !== null) { |
|
37 | - curl_setopt($this->curlHandle, CURLOPT_COOKIEFILE, $cookieJar); |
|
38 | - curl_setopt($this->curlHandle, CURLOPT_COOKIEJAR, $cookieJar); |
|
39 | - } |
|
40 | - } |
|
41 | - |
|
42 | - public function __destruct() |
|
43 | - { |
|
44 | - curl_close($this->curlHandle); |
|
45 | - } |
|
46 | - |
|
47 | - /** |
|
48 | - * Fetches the content of a URL, with an optional parameter set. |
|
49 | - * |
|
50 | - * @param string $url The URL to fetch. |
|
51 | - * @param null|array $parameters Key/value pair of GET parameters to add to the request. |
|
52 | - * Null lets you handle it yourself. |
|
53 | - * |
|
54 | - * @param array $headers |
|
55 | - * |
|
56 | - * @return string |
|
57 | - * @throws CurlException |
|
58 | - */ |
|
59 | - public function get($url, $parameters = null, $headers = array()) |
|
60 | - { |
|
61 | - if ($parameters !== null && is_array($parameters)) { |
|
62 | - $getString = '?' . http_build_query($parameters); |
|
63 | - $url .= $getString; |
|
64 | - } |
|
65 | - |
|
66 | - curl_setopt($this->curlHandle, CURLOPT_URL, $url); |
|
67 | - |
|
68 | - // Make sure we're doing a GET |
|
69 | - curl_setopt($this->curlHandle, CURLOPT_POST, false); |
|
70 | - |
|
71 | - curl_setopt($this->curlHandle, CURLOPT_HTTPHEADER, $headers); |
|
72 | - |
|
73 | - $result = curl_exec($this->curlHandle); |
|
74 | - |
|
75 | - if ($result === false) { |
|
76 | - $error = curl_error($this->curlHandle); |
|
77 | - throw new CurlException('Remote request failed with error ' . $error); |
|
78 | - } |
|
79 | - |
|
80 | - return $result; |
|
81 | - } |
|
82 | - |
|
83 | - /** |
|
84 | - * Posts data to a URL |
|
85 | - * |
|
86 | - * @param string $url The URL to fetch. |
|
87 | - * @param array $parameters Key/value pair of POST parameters to add to the request. |
|
88 | - * @param array $headers |
|
89 | - * |
|
90 | - * @return string |
|
91 | - * @throws CurlException |
|
92 | - */ |
|
93 | - public function post($url, $parameters, $headers = array()) |
|
94 | - { |
|
95 | - curl_setopt($this->curlHandle, CURLOPT_URL, $url); |
|
96 | - |
|
97 | - // Make sure we're doing a POST |
|
98 | - curl_setopt($this->curlHandle, CURLOPT_POST, true); |
|
99 | - curl_setopt($this->curlHandle, CURLOPT_POSTFIELDS, http_build_query($parameters)); |
|
100 | - |
|
101 | - curl_setopt($this->curlHandle, CURLOPT_HTTPHEADER, $headers); |
|
102 | - |
|
103 | - $result = curl_exec($this->curlHandle); |
|
104 | - |
|
105 | - if ($result === false) { |
|
106 | - $error = curl_error($this->curlHandle); |
|
107 | - throw new CurlException('Remote request failed with error ' . $error); |
|
108 | - } |
|
109 | - |
|
110 | - return $result; |
|
111 | - } |
|
112 | - |
|
113 | - /** |
|
114 | - * @return string |
|
115 | - */ |
|
116 | - public function getError() |
|
117 | - { |
|
118 | - return curl_error($this->curlHandle); |
|
119 | - } |
|
15 | + private $curlHandle; |
|
16 | + |
|
17 | + /** |
|
18 | + * HttpHelper constructor. |
|
19 | + * |
|
20 | + * @param string $userAgent |
|
21 | + * @param boolean $disableVerifyPeer |
|
22 | + * @param string $cookieJar |
|
23 | + */ |
|
24 | + public function __construct($userAgent, $disableVerifyPeer, $cookieJar = null) |
|
25 | + { |
|
26 | + $this->curlHandle = curl_init(); |
|
27 | + |
|
28 | + curl_setopt($this->curlHandle, CURLOPT_RETURNTRANSFER, true); |
|
29 | + curl_setopt($this->curlHandle, CURLOPT_USERAGENT, $userAgent); |
|
30 | + curl_setopt($this->curlHandle, CURLOPT_FAILONERROR, true); |
|
31 | + |
|
32 | + if ($disableVerifyPeer) { |
|
33 | + curl_setopt($this->curlHandle, CURLOPT_SSL_VERIFYPEER, false); |
|
34 | + } |
|
35 | + |
|
36 | + if($cookieJar !== null) { |
|
37 | + curl_setopt($this->curlHandle, CURLOPT_COOKIEFILE, $cookieJar); |
|
38 | + curl_setopt($this->curlHandle, CURLOPT_COOKIEJAR, $cookieJar); |
|
39 | + } |
|
40 | + } |
|
41 | + |
|
42 | + public function __destruct() |
|
43 | + { |
|
44 | + curl_close($this->curlHandle); |
|
45 | + } |
|
46 | + |
|
47 | + /** |
|
48 | + * Fetches the content of a URL, with an optional parameter set. |
|
49 | + * |
|
50 | + * @param string $url The URL to fetch. |
|
51 | + * @param null|array $parameters Key/value pair of GET parameters to add to the request. |
|
52 | + * Null lets you handle it yourself. |
|
53 | + * |
|
54 | + * @param array $headers |
|
55 | + * |
|
56 | + * @return string |
|
57 | + * @throws CurlException |
|
58 | + */ |
|
59 | + public function get($url, $parameters = null, $headers = array()) |
|
60 | + { |
|
61 | + if ($parameters !== null && is_array($parameters)) { |
|
62 | + $getString = '?' . http_build_query($parameters); |
|
63 | + $url .= $getString; |
|
64 | + } |
|
65 | + |
|
66 | + curl_setopt($this->curlHandle, CURLOPT_URL, $url); |
|
67 | + |
|
68 | + // Make sure we're doing a GET |
|
69 | + curl_setopt($this->curlHandle, CURLOPT_POST, false); |
|
70 | + |
|
71 | + curl_setopt($this->curlHandle, CURLOPT_HTTPHEADER, $headers); |
|
72 | + |
|
73 | + $result = curl_exec($this->curlHandle); |
|
74 | + |
|
75 | + if ($result === false) { |
|
76 | + $error = curl_error($this->curlHandle); |
|
77 | + throw new CurlException('Remote request failed with error ' . $error); |
|
78 | + } |
|
79 | + |
|
80 | + return $result; |
|
81 | + } |
|
82 | + |
|
83 | + /** |
|
84 | + * Posts data to a URL |
|
85 | + * |
|
86 | + * @param string $url The URL to fetch. |
|
87 | + * @param array $parameters Key/value pair of POST parameters to add to the request. |
|
88 | + * @param array $headers |
|
89 | + * |
|
90 | + * @return string |
|
91 | + * @throws CurlException |
|
92 | + */ |
|
93 | + public function post($url, $parameters, $headers = array()) |
|
94 | + { |
|
95 | + curl_setopt($this->curlHandle, CURLOPT_URL, $url); |
|
96 | + |
|
97 | + // Make sure we're doing a POST |
|
98 | + curl_setopt($this->curlHandle, CURLOPT_POST, true); |
|
99 | + curl_setopt($this->curlHandle, CURLOPT_POSTFIELDS, http_build_query($parameters)); |
|
100 | + |
|
101 | + curl_setopt($this->curlHandle, CURLOPT_HTTPHEADER, $headers); |
|
102 | + |
|
103 | + $result = curl_exec($this->curlHandle); |
|
104 | + |
|
105 | + if ($result === false) { |
|
106 | + $error = curl_error($this->curlHandle); |
|
107 | + throw new CurlException('Remote request failed with error ' . $error); |
|
108 | + } |
|
109 | + |
|
110 | + return $result; |
|
111 | + } |
|
112 | + |
|
113 | + /** |
|
114 | + * @return string |
|
115 | + */ |
|
116 | + public function getError() |
|
117 | + { |
|
118 | + return curl_error($this->curlHandle); |
|
119 | + } |
|
120 | 120 | } |
121 | 121 | \ No newline at end of file |
@@ -33,7 +33,7 @@ discard block |
||
33 | 33 | curl_setopt($this->curlHandle, CURLOPT_SSL_VERIFYPEER, false); |
34 | 34 | } |
35 | 35 | |
36 | - if($cookieJar !== null) { |
|
36 | + if ($cookieJar !== null) { |
|
37 | 37 | curl_setopt($this->curlHandle, CURLOPT_COOKIEFILE, $cookieJar); |
38 | 38 | curl_setopt($this->curlHandle, CURLOPT_COOKIEJAR, $cookieJar); |
39 | 39 | } |
@@ -59,7 +59,7 @@ discard block |
||
59 | 59 | public function get($url, $parameters = null, $headers = array()) |
60 | 60 | { |
61 | 61 | if ($parameters !== null && is_array($parameters)) { |
62 | - $getString = '?' . http_build_query($parameters); |
|
62 | + $getString = '?'.http_build_query($parameters); |
|
63 | 63 | $url .= $getString; |
64 | 64 | } |
65 | 65 | |
@@ -74,7 +74,7 @@ discard block |
||
74 | 74 | |
75 | 75 | if ($result === false) { |
76 | 76 | $error = curl_error($this->curlHandle); |
77 | - throw new CurlException('Remote request failed with error ' . $error); |
|
77 | + throw new CurlException('Remote request failed with error '.$error); |
|
78 | 78 | } |
79 | 79 | |
80 | 80 | return $result; |
@@ -104,7 +104,7 @@ discard block |
||
104 | 104 | |
105 | 105 | if ($result === false) { |
106 | 106 | $error = curl_error($this->curlHandle); |
107 | - throw new CurlException('Remote request failed with error ' . $error); |
|
107 | + throw new CurlException('Remote request failed with error '.$error); |
|
108 | 108 | } |
109 | 109 | |
110 | 110 | return $result; |
@@ -241,7 +241,8 @@ |
||
241 | 241 | * @param string $username |
242 | 242 | * @return bool |
243 | 243 | */ |
244 | - public function checkAccountExists($username) { |
|
244 | + public function checkAccountExists($username) |
|
245 | + { |
|
245 | 246 | $parameters = array( |
246 | 247 | 'action' => 'query', |
247 | 248 | 'list' => 'users', |
@@ -15,245 +15,245 @@ |
||
15 | 15 | |
16 | 16 | class MediaWikiHelper |
17 | 17 | { |
18 | - /** |
|
19 | - * @var IMediaWikiClient |
|
20 | - */ |
|
21 | - private $mediaWikiClient; |
|
22 | - /** |
|
23 | - * @var SiteConfiguration |
|
24 | - */ |
|
25 | - private $siteConfiguration; |
|
26 | - |
|
27 | - /** |
|
28 | - * MediaWikiHelper constructor. |
|
29 | - * |
|
30 | - * @param IMediaWikiClient $mediaWikiClient |
|
31 | - * @param SiteConfiguration $siteConfiguration |
|
32 | - */ |
|
33 | - public function __construct(IMediaWikiClient $mediaWikiClient, SiteConfiguration $siteConfiguration) |
|
34 | - { |
|
35 | - $this->mediaWikiClient = $mediaWikiClient; |
|
36 | - $this->siteConfiguration = $siteConfiguration; |
|
37 | - } |
|
38 | - |
|
39 | - /** |
|
40 | - * @todo handle override antispoof and titleblacklist issues |
|
41 | - * |
|
42 | - * @param string $username |
|
43 | - * @param string $emailAddress |
|
44 | - * @param string $reason |
|
45 | - * |
|
46 | - * @throws Exception |
|
47 | - * @throws MediaWikiApiException |
|
48 | - */ |
|
49 | - public function createAccount($username, $emailAddress, $reason) |
|
50 | - { |
|
51 | - // get token |
|
52 | - $tokenParams = array( |
|
53 | - 'action' => 'query', |
|
54 | - 'meta' => 'tokens', |
|
55 | - 'type' => 'createaccount', |
|
56 | - ); |
|
57 | - |
|
58 | - $response = $this->mediaWikiClient->doApiCall($tokenParams, 'POST'); |
|
59 | - |
|
60 | - if (isset($response->error)) { |
|
61 | - throw new MediaWikiApiException($response->error->code . ': ' . $response->error->info); |
|
62 | - } |
|
63 | - |
|
64 | - $token = $response->query->tokens->createaccounttoken; |
|
65 | - |
|
66 | - $callback = $this->siteConfiguration->getBaseUrl() . '/internal.php/oauth/createCallback'; |
|
67 | - |
|
68 | - $checkboxFields = array(); |
|
69 | - $requiredFields = array(); |
|
70 | - $this->getCreationFieldData($requiredFields, $checkboxFields); |
|
71 | - |
|
72 | - $apiCallData = array( |
|
73 | - 'action' => 'createaccount', |
|
74 | - 'createreturnurl' => $callback, |
|
75 | - 'createtoken' => $token, |
|
76 | - 'createmessageformat' => 'html', |
|
77 | - ); |
|
78 | - |
|
79 | - $createParams = array_fill_keys($requiredFields, '') + $apiCallData; |
|
80 | - |
|
81 | - $createParams['username'] = $username; |
|
82 | - $createParams['mailpassword'] = true; |
|
83 | - $createParams['email'] = $emailAddress; |
|
84 | - $createParams['reason'] = $reason; |
|
85 | - |
|
86 | - $createResponse = $this->mediaWikiClient->doApiCall($createParams, 'POST'); |
|
87 | - |
|
88 | - if (isset($createResponse->error)) { |
|
89 | - throw new MediaWikiApiException($response->error->code . ': ' . $response->error->info); |
|
90 | - } |
|
91 | - |
|
92 | - if (!isset($createResponse->createaccount) || !isset($createResponse->createaccount->status)) { |
|
93 | - throw new MediaWikiApiException('Unknown error creating account'); |
|
94 | - } |
|
95 | - |
|
96 | - if ($createResponse->createaccount->status === 'FAIL') { |
|
97 | - throw new MediaWikiApiException($createResponse->createaccount->message); |
|
98 | - } |
|
99 | - |
|
100 | - if ($createResponse->createaccount->status === 'PASS') { |
|
101 | - // success! |
|
102 | - return; |
|
103 | - } |
|
104 | - |
|
105 | - throw new Exception('API result reported status of ' . $createResponse->createaccount->status); |
|
106 | - } |
|
107 | - |
|
108 | - /** |
|
109 | - * @param string $username |
|
110 | - * @param string $title |
|
111 | - * @param $summary |
|
112 | - * @param string $message |
|
113 | - * @param bool $createOnly |
|
114 | - * |
|
115 | - * @throws MediaWikiApiException |
|
116 | - */ |
|
117 | - public function addTalkPageMessage($username, $title, $summary, $message, $createOnly = true) |
|
118 | - { |
|
119 | - // get token |
|
120 | - $tokenParams = array( |
|
121 | - 'action' => 'query', |
|
122 | - 'meta' => 'tokens', |
|
123 | - 'type' => 'csrf', |
|
124 | - ); |
|
125 | - |
|
126 | - $response = $this->mediaWikiClient->doApiCall($tokenParams, 'POST'); |
|
127 | - |
|
128 | - if (isset($response->error)) { |
|
129 | - throw new MediaWikiApiException($response->error->code . ': ' . $response->error->info); |
|
130 | - } |
|
131 | - |
|
132 | - $token = $response->query->tokens->csrftoken; |
|
133 | - |
|
134 | - if ($token === null) { |
|
135 | - throw new MediaWikiApiException('Edit token could not be acquired'); |
|
136 | - } |
|
137 | - |
|
138 | - $editParameters = array( |
|
139 | - 'action' => 'edit', |
|
140 | - 'title' => 'User talk:' . $username, |
|
141 | - 'section' => 'new', |
|
142 | - 'sectiontitle' => $title, |
|
143 | - 'summary' => $summary, |
|
144 | - 'text' => $message, |
|
145 | - 'token' => $token, |
|
146 | - ); |
|
147 | - |
|
148 | - if ($createOnly) { |
|
149 | - $editParameters['createonly'] = true; |
|
150 | - } |
|
151 | - |
|
152 | - $response = $this->mediaWikiClient->doApiCall($editParameters, 'POST'); |
|
153 | - |
|
154 | - if (!isset($response->edit)) { |
|
155 | - if (isset($response->error)) { |
|
156 | - throw new MediaWikiApiException($response->error->code . ': ' . $response->error->info); |
|
157 | - } |
|
158 | - |
|
159 | - throw new MediaWikiApiException('Unknown error encountered during editing.'); |
|
160 | - } |
|
161 | - |
|
162 | - $editResponse = $response->edit; |
|
163 | - if ($editResponse->result === "Success") { |
|
164 | - return; |
|
165 | - } |
|
166 | - |
|
167 | - throw new MediaWikiApiException('Edit status unsuccessful: ' . $editResponse->result); |
|
168 | - } |
|
169 | - |
|
170 | - public function getCreationFieldData(&$requiredFields, &$checkboxFields) |
|
171 | - { |
|
172 | - // get token |
|
173 | - $params = array( |
|
174 | - 'action' => 'query', |
|
175 | - 'meta' => 'authmanagerinfo', |
|
176 | - 'amirequestsfor' => 'create', |
|
177 | - ); |
|
178 | - |
|
179 | - $response = $this->mediaWikiClient->doApiCall($params, 'GET'); |
|
180 | - |
|
181 | - if (isset($response->error)) { |
|
182 | - throw new MediaWikiApiException($response->error->code . ': ' . $response->error->info); |
|
183 | - } |
|
184 | - |
|
185 | - $requests = $response->query->authmanagerinfo->requests; |
|
186 | - |
|
187 | - // We don't want to deal with these providers ever. |
|
188 | - $discardList = array( |
|
189 | - // Requires a username and password |
|
190 | - 'MediaWiki\\Auth\\PasswordAuthenticationRequest', |
|
191 | - ); |
|
192 | - |
|
193 | - // We require these providers to function |
|
194 | - $requireList = array( |
|
195 | - 'MediaWiki\\Auth\\TemporaryPasswordAuthenticationRequest', |
|
196 | - 'MediaWiki\\Auth\\UsernameAuthenticationRequest', |
|
197 | - 'MediaWiki\\Auth\\UserDataAuthenticationRequest', |
|
198 | - 'MediaWiki\\Auth\\CreationReasonAuthenticationRequest', |
|
199 | - ); |
|
200 | - |
|
201 | - $requiredFields = array(); |
|
202 | - // Keep checkbox fields separate, since "required" actually means optional as absent == false. |
|
203 | - $checkboxFields = array(); |
|
204 | - |
|
205 | - foreach ($requests as $req) { |
|
206 | - // Immediately discard anything that is on the discard list. |
|
207 | - if (in_array($req->id, $discardList)) { |
|
208 | - continue; |
|
209 | - } |
|
210 | - |
|
211 | - $required = false; |
|
212 | - |
|
213 | - if ($req->required === 'primary-required' && !in_array($req->id, $requireList)) { |
|
214 | - // Only want one. |
|
215 | - continue; |
|
216 | - } |
|
217 | - |
|
218 | - if (in_array($req->id, $requireList)) { |
|
219 | - unset($requireList[$req->id]); |
|
220 | - $required = true; |
|
221 | - } |
|
222 | - |
|
223 | - if ($req->required === 'required') { |
|
224 | - $required = true; |
|
225 | - } |
|
226 | - |
|
227 | - if ($required) { |
|
228 | - foreach ($req->fields as $name => $data) { |
|
229 | - if ($data->type === 'checkbox') { |
|
230 | - $checkboxFields[] = $name; |
|
231 | - } |
|
232 | - else { |
|
233 | - $requiredFields[] = $name; |
|
234 | - } |
|
235 | - } |
|
236 | - } |
|
237 | - } |
|
238 | - } |
|
239 | - |
|
240 | - /** |
|
241 | - * @param string $username |
|
242 | - * @return bool |
|
243 | - */ |
|
244 | - public function checkAccountExists($username) { |
|
245 | - $parameters = array( |
|
246 | - 'action' => 'query', |
|
247 | - 'list' => 'users', |
|
248 | - 'format' => 'php', |
|
249 | - 'ususers' => $username, |
|
250 | - ); |
|
251 | - |
|
252 | - $apiResult = $this->mediaWikiClient->doApiCall($parameters, 'GET'); |
|
253 | - |
|
254 | - $entry = $apiResult->query->users[0]; |
|
255 | - $exists = !isset($entry->missing); |
|
256 | - |
|
257 | - return $exists; |
|
258 | - } |
|
18 | + /** |
|
19 | + * @var IMediaWikiClient |
|
20 | + */ |
|
21 | + private $mediaWikiClient; |
|
22 | + /** |
|
23 | + * @var SiteConfiguration |
|
24 | + */ |
|
25 | + private $siteConfiguration; |
|
26 | + |
|
27 | + /** |
|
28 | + * MediaWikiHelper constructor. |
|
29 | + * |
|
30 | + * @param IMediaWikiClient $mediaWikiClient |
|
31 | + * @param SiteConfiguration $siteConfiguration |
|
32 | + */ |
|
33 | + public function __construct(IMediaWikiClient $mediaWikiClient, SiteConfiguration $siteConfiguration) |
|
34 | + { |
|
35 | + $this->mediaWikiClient = $mediaWikiClient; |
|
36 | + $this->siteConfiguration = $siteConfiguration; |
|
37 | + } |
|
38 | + |
|
39 | + /** |
|
40 | + * @todo handle override antispoof and titleblacklist issues |
|
41 | + * |
|
42 | + * @param string $username |
|
43 | + * @param string $emailAddress |
|
44 | + * @param string $reason |
|
45 | + * |
|
46 | + * @throws Exception |
|
47 | + * @throws MediaWikiApiException |
|
48 | + */ |
|
49 | + public function createAccount($username, $emailAddress, $reason) |
|
50 | + { |
|
51 | + // get token |
|
52 | + $tokenParams = array( |
|
53 | + 'action' => 'query', |
|
54 | + 'meta' => 'tokens', |
|
55 | + 'type' => 'createaccount', |
|
56 | + ); |
|
57 | + |
|
58 | + $response = $this->mediaWikiClient->doApiCall($tokenParams, 'POST'); |
|
59 | + |
|
60 | + if (isset($response->error)) { |
|
61 | + throw new MediaWikiApiException($response->error->code . ': ' . $response->error->info); |
|
62 | + } |
|
63 | + |
|
64 | + $token = $response->query->tokens->createaccounttoken; |
|
65 | + |
|
66 | + $callback = $this->siteConfiguration->getBaseUrl() . '/internal.php/oauth/createCallback'; |
|
67 | + |
|
68 | + $checkboxFields = array(); |
|
69 | + $requiredFields = array(); |
|
70 | + $this->getCreationFieldData($requiredFields, $checkboxFields); |
|
71 | + |
|
72 | + $apiCallData = array( |
|
73 | + 'action' => 'createaccount', |
|
74 | + 'createreturnurl' => $callback, |
|
75 | + 'createtoken' => $token, |
|
76 | + 'createmessageformat' => 'html', |
|
77 | + ); |
|
78 | + |
|
79 | + $createParams = array_fill_keys($requiredFields, '') + $apiCallData; |
|
80 | + |
|
81 | + $createParams['username'] = $username; |
|
82 | + $createParams['mailpassword'] = true; |
|
83 | + $createParams['email'] = $emailAddress; |
|
84 | + $createParams['reason'] = $reason; |
|
85 | + |
|
86 | + $createResponse = $this->mediaWikiClient->doApiCall($createParams, 'POST'); |
|
87 | + |
|
88 | + if (isset($createResponse->error)) { |
|
89 | + throw new MediaWikiApiException($response->error->code . ': ' . $response->error->info); |
|
90 | + } |
|
91 | + |
|
92 | + if (!isset($createResponse->createaccount) || !isset($createResponse->createaccount->status)) { |
|
93 | + throw new MediaWikiApiException('Unknown error creating account'); |
|
94 | + } |
|
95 | + |
|
96 | + if ($createResponse->createaccount->status === 'FAIL') { |
|
97 | + throw new MediaWikiApiException($createResponse->createaccount->message); |
|
98 | + } |
|
99 | + |
|
100 | + if ($createResponse->createaccount->status === 'PASS') { |
|
101 | + // success! |
|
102 | + return; |
|
103 | + } |
|
104 | + |
|
105 | + throw new Exception('API result reported status of ' . $createResponse->createaccount->status); |
|
106 | + } |
|
107 | + |
|
108 | + /** |
|
109 | + * @param string $username |
|
110 | + * @param string $title |
|
111 | + * @param $summary |
|
112 | + * @param string $message |
|
113 | + * @param bool $createOnly |
|
114 | + * |
|
115 | + * @throws MediaWikiApiException |
|
116 | + */ |
|
117 | + public function addTalkPageMessage($username, $title, $summary, $message, $createOnly = true) |
|
118 | + { |
|
119 | + // get token |
|
120 | + $tokenParams = array( |
|
121 | + 'action' => 'query', |
|
122 | + 'meta' => 'tokens', |
|
123 | + 'type' => 'csrf', |
|
124 | + ); |
|
125 | + |
|
126 | + $response = $this->mediaWikiClient->doApiCall($tokenParams, 'POST'); |
|
127 | + |
|
128 | + if (isset($response->error)) { |
|
129 | + throw new MediaWikiApiException($response->error->code . ': ' . $response->error->info); |
|
130 | + } |
|
131 | + |
|
132 | + $token = $response->query->tokens->csrftoken; |
|
133 | + |
|
134 | + if ($token === null) { |
|
135 | + throw new MediaWikiApiException('Edit token could not be acquired'); |
|
136 | + } |
|
137 | + |
|
138 | + $editParameters = array( |
|
139 | + 'action' => 'edit', |
|
140 | + 'title' => 'User talk:' . $username, |
|
141 | + 'section' => 'new', |
|
142 | + 'sectiontitle' => $title, |
|
143 | + 'summary' => $summary, |
|
144 | + 'text' => $message, |
|
145 | + 'token' => $token, |
|
146 | + ); |
|
147 | + |
|
148 | + if ($createOnly) { |
|
149 | + $editParameters['createonly'] = true; |
|
150 | + } |
|
151 | + |
|
152 | + $response = $this->mediaWikiClient->doApiCall($editParameters, 'POST'); |
|
153 | + |
|
154 | + if (!isset($response->edit)) { |
|
155 | + if (isset($response->error)) { |
|
156 | + throw new MediaWikiApiException($response->error->code . ': ' . $response->error->info); |
|
157 | + } |
|
158 | + |
|
159 | + throw new MediaWikiApiException('Unknown error encountered during editing.'); |
|
160 | + } |
|
161 | + |
|
162 | + $editResponse = $response->edit; |
|
163 | + if ($editResponse->result === "Success") { |
|
164 | + return; |
|
165 | + } |
|
166 | + |
|
167 | + throw new MediaWikiApiException('Edit status unsuccessful: ' . $editResponse->result); |
|
168 | + } |
|
169 | + |
|
170 | + public function getCreationFieldData(&$requiredFields, &$checkboxFields) |
|
171 | + { |
|
172 | + // get token |
|
173 | + $params = array( |
|
174 | + 'action' => 'query', |
|
175 | + 'meta' => 'authmanagerinfo', |
|
176 | + 'amirequestsfor' => 'create', |
|
177 | + ); |
|
178 | + |
|
179 | + $response = $this->mediaWikiClient->doApiCall($params, 'GET'); |
|
180 | + |
|
181 | + if (isset($response->error)) { |
|
182 | + throw new MediaWikiApiException($response->error->code . ': ' . $response->error->info); |
|
183 | + } |
|
184 | + |
|
185 | + $requests = $response->query->authmanagerinfo->requests; |
|
186 | + |
|
187 | + // We don't want to deal with these providers ever. |
|
188 | + $discardList = array( |
|
189 | + // Requires a username and password |
|
190 | + 'MediaWiki\\Auth\\PasswordAuthenticationRequest', |
|
191 | + ); |
|
192 | + |
|
193 | + // We require these providers to function |
|
194 | + $requireList = array( |
|
195 | + 'MediaWiki\\Auth\\TemporaryPasswordAuthenticationRequest', |
|
196 | + 'MediaWiki\\Auth\\UsernameAuthenticationRequest', |
|
197 | + 'MediaWiki\\Auth\\UserDataAuthenticationRequest', |
|
198 | + 'MediaWiki\\Auth\\CreationReasonAuthenticationRequest', |
|
199 | + ); |
|
200 | + |
|
201 | + $requiredFields = array(); |
|
202 | + // Keep checkbox fields separate, since "required" actually means optional as absent == false. |
|
203 | + $checkboxFields = array(); |
|
204 | + |
|
205 | + foreach ($requests as $req) { |
|
206 | + // Immediately discard anything that is on the discard list. |
|
207 | + if (in_array($req->id, $discardList)) { |
|
208 | + continue; |
|
209 | + } |
|
210 | + |
|
211 | + $required = false; |
|
212 | + |
|
213 | + if ($req->required === 'primary-required' && !in_array($req->id, $requireList)) { |
|
214 | + // Only want one. |
|
215 | + continue; |
|
216 | + } |
|
217 | + |
|
218 | + if (in_array($req->id, $requireList)) { |
|
219 | + unset($requireList[$req->id]); |
|
220 | + $required = true; |
|
221 | + } |
|
222 | + |
|
223 | + if ($req->required === 'required') { |
|
224 | + $required = true; |
|
225 | + } |
|
226 | + |
|
227 | + if ($required) { |
|
228 | + foreach ($req->fields as $name => $data) { |
|
229 | + if ($data->type === 'checkbox') { |
|
230 | + $checkboxFields[] = $name; |
|
231 | + } |
|
232 | + else { |
|
233 | + $requiredFields[] = $name; |
|
234 | + } |
|
235 | + } |
|
236 | + } |
|
237 | + } |
|
238 | + } |
|
239 | + |
|
240 | + /** |
|
241 | + * @param string $username |
|
242 | + * @return bool |
|
243 | + */ |
|
244 | + public function checkAccountExists($username) { |
|
245 | + $parameters = array( |
|
246 | + 'action' => 'query', |
|
247 | + 'list' => 'users', |
|
248 | + 'format' => 'php', |
|
249 | + 'ususers' => $username, |
|
250 | + ); |
|
251 | + |
|
252 | + $apiResult = $this->mediaWikiClient->doApiCall($parameters, 'GET'); |
|
253 | + |
|
254 | + $entry = $apiResult->query->users[0]; |
|
255 | + $exists = !isset($entry->missing); |
|
256 | + |
|
257 | + return $exists; |
|
258 | + } |
|
259 | 259 | } |
@@ -58,12 +58,12 @@ discard block |
||
58 | 58 | $response = $this->mediaWikiClient->doApiCall($tokenParams, 'POST'); |
59 | 59 | |
60 | 60 | if (isset($response->error)) { |
61 | - throw new MediaWikiApiException($response->error->code . ': ' . $response->error->info); |
|
61 | + throw new MediaWikiApiException($response->error->code.': '.$response->error->info); |
|
62 | 62 | } |
63 | 63 | |
64 | 64 | $token = $response->query->tokens->createaccounttoken; |
65 | 65 | |
66 | - $callback = $this->siteConfiguration->getBaseUrl() . '/internal.php/oauth/createCallback'; |
|
66 | + $callback = $this->siteConfiguration->getBaseUrl().'/internal.php/oauth/createCallback'; |
|
67 | 67 | |
68 | 68 | $checkboxFields = array(); |
69 | 69 | $requiredFields = array(); |
@@ -86,7 +86,7 @@ discard block |
||
86 | 86 | $createResponse = $this->mediaWikiClient->doApiCall($createParams, 'POST'); |
87 | 87 | |
88 | 88 | if (isset($createResponse->error)) { |
89 | - throw new MediaWikiApiException($response->error->code . ': ' . $response->error->info); |
|
89 | + throw new MediaWikiApiException($response->error->code.': '.$response->error->info); |
|
90 | 90 | } |
91 | 91 | |
92 | 92 | if (!isset($createResponse->createaccount) || !isset($createResponse->createaccount->status)) { |
@@ -102,7 +102,7 @@ discard block |
||
102 | 102 | return; |
103 | 103 | } |
104 | 104 | |
105 | - throw new Exception('API result reported status of ' . $createResponse->createaccount->status); |
|
105 | + throw new Exception('API result reported status of '.$createResponse->createaccount->status); |
|
106 | 106 | } |
107 | 107 | |
108 | 108 | /** |
@@ -126,7 +126,7 @@ discard block |
||
126 | 126 | $response = $this->mediaWikiClient->doApiCall($tokenParams, 'POST'); |
127 | 127 | |
128 | 128 | if (isset($response->error)) { |
129 | - throw new MediaWikiApiException($response->error->code . ': ' . $response->error->info); |
|
129 | + throw new MediaWikiApiException($response->error->code.': '.$response->error->info); |
|
130 | 130 | } |
131 | 131 | |
132 | 132 | $token = $response->query->tokens->csrftoken; |
@@ -137,7 +137,7 @@ discard block |
||
137 | 137 | |
138 | 138 | $editParameters = array( |
139 | 139 | 'action' => 'edit', |
140 | - 'title' => 'User talk:' . $username, |
|
140 | + 'title' => 'User talk:'.$username, |
|
141 | 141 | 'section' => 'new', |
142 | 142 | 'sectiontitle' => $title, |
143 | 143 | 'summary' => $summary, |
@@ -153,7 +153,7 @@ discard block |
||
153 | 153 | |
154 | 154 | if (!isset($response->edit)) { |
155 | 155 | if (isset($response->error)) { |
156 | - throw new MediaWikiApiException($response->error->code . ': ' . $response->error->info); |
|
156 | + throw new MediaWikiApiException($response->error->code.': '.$response->error->info); |
|
157 | 157 | } |
158 | 158 | |
159 | 159 | throw new MediaWikiApiException('Unknown error encountered during editing.'); |
@@ -164,7 +164,7 @@ discard block |
||
164 | 164 | return; |
165 | 165 | } |
166 | 166 | |
167 | - throw new MediaWikiApiException('Edit status unsuccessful: ' . $editResponse->result); |
|
167 | + throw new MediaWikiApiException('Edit status unsuccessful: '.$editResponse->result); |
|
168 | 168 | } |
169 | 169 | |
170 | 170 | public function getCreationFieldData(&$requiredFields, &$checkboxFields) |
@@ -179,7 +179,7 @@ discard block |
||
179 | 179 | $response = $this->mediaWikiClient->doApiCall($params, 'GET'); |
180 | 180 | |
181 | 181 | if (isset($response->error)) { |
182 | - throw new MediaWikiApiException($response->error->code . ': ' . $response->error->info); |
|
182 | + throw new MediaWikiApiException($response->error->code.': '.$response->error->info); |
|
183 | 183 | } |
184 | 184 | |
185 | 185 | $requests = $response->query->authmanagerinfo->requests; |
@@ -15,80 +15,80 @@ discard block |
||
15 | 15 | |
16 | 16 | class UserSearchHelper extends SearchHelperBase |
17 | 17 | { |
18 | - /** |
|
19 | - * UserSearchHelper constructor. |
|
20 | - * |
|
21 | - * @param PdoDatabase $database |
|
22 | - */ |
|
23 | - public function __construct(PdoDatabase $database) |
|
24 | - { |
|
25 | - parent::__construct($database, 'user', User::class); |
|
26 | - } |
|
27 | - |
|
28 | - /** |
|
29 | - * Initiates a search for requests |
|
30 | - * |
|
31 | - * @param PdoDatabase $database |
|
32 | - * |
|
33 | - * @return UserSearchHelper |
|
34 | - */ |
|
35 | - public static function get(PdoDatabase $database) |
|
36 | - { |
|
37 | - $helper = new UserSearchHelper($database); |
|
38 | - |
|
39 | - return $helper; |
|
40 | - } |
|
41 | - |
|
42 | - /** |
|
43 | - * @param string $status |
|
44 | - * |
|
45 | - * @return $this |
|
46 | - */ |
|
47 | - public function byStatus($status) |
|
48 | - { |
|
49 | - $this->whereClause .= ' AND status = ?'; |
|
50 | - $this->parameterList[] = $status; |
|
51 | - |
|
52 | - return $this; |
|
53 | - } |
|
54 | - |
|
55 | - public function statusIn($statuses) |
|
56 | - { |
|
57 | - $this->inClause('status', $statuses); |
|
58 | - |
|
59 | - return $this; |
|
60 | - } |
|
61 | - |
|
62 | - /** |
|
63 | - * @param string $role |
|
64 | - * |
|
65 | - * @return $this |
|
66 | - */ |
|
67 | - public function byRole($role) |
|
68 | - { |
|
69 | - $this->joinClause .= ' INNER JOIN userrole r on origin.id = r.user'; |
|
70 | - $this->whereClause .= ' AND r.role = ?'; |
|
71 | - $this->parameterList[] = $role; |
|
72 | - |
|
73 | - return $this; |
|
74 | - } |
|
75 | - |
|
76 | - /** |
|
77 | - * @param DateTime $instant |
|
78 | - * |
|
79 | - * @return $this |
|
80 | - */ |
|
81 | - public function lastActiveBefore(DateTime $instant) |
|
82 | - { |
|
83 | - $this->whereClause .= ' AND origin.lastactive < ?'; |
|
84 | - $this->parameterList[] = $instant->format("Y-m-d H:i:s"); |
|
85 | - |
|
86 | - return $this; |
|
87 | - } |
|
88 | - |
|
89 | - public function getRoleMap(&$roleMap) |
|
90 | - { |
|
91 | - $query = <<<SQL |
|
18 | + /** |
|
19 | + * UserSearchHelper constructor. |
|
20 | + * |
|
21 | + * @param PdoDatabase $database |
|
22 | + */ |
|
23 | + public function __construct(PdoDatabase $database) |
|
24 | + { |
|
25 | + parent::__construct($database, 'user', User::class); |
|
26 | + } |
|
27 | + |
|
28 | + /** |
|
29 | + * Initiates a search for requests |
|
30 | + * |
|
31 | + * @param PdoDatabase $database |
|
32 | + * |
|
33 | + * @return UserSearchHelper |
|
34 | + */ |
|
35 | + public static function get(PdoDatabase $database) |
|
36 | + { |
|
37 | + $helper = new UserSearchHelper($database); |
|
38 | + |
|
39 | + return $helper; |
|
40 | + } |
|
41 | + |
|
42 | + /** |
|
43 | + * @param string $status |
|
44 | + * |
|
45 | + * @return $this |
|
46 | + */ |
|
47 | + public function byStatus($status) |
|
48 | + { |
|
49 | + $this->whereClause .= ' AND status = ?'; |
|
50 | + $this->parameterList[] = $status; |
|
51 | + |
|
52 | + return $this; |
|
53 | + } |
|
54 | + |
|
55 | + public function statusIn($statuses) |
|
56 | + { |
|
57 | + $this->inClause('status', $statuses); |
|
58 | + |
|
59 | + return $this; |
|
60 | + } |
|
61 | + |
|
62 | + /** |
|
63 | + * @param string $role |
|
64 | + * |
|
65 | + * @return $this |
|
66 | + */ |
|
67 | + public function byRole($role) |
|
68 | + { |
|
69 | + $this->joinClause .= ' INNER JOIN userrole r on origin.id = r.user'; |
|
70 | + $this->whereClause .= ' AND r.role = ?'; |
|
71 | + $this->parameterList[] = $role; |
|
72 | + |
|
73 | + return $this; |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * @param DateTime $instant |
|
78 | + * |
|
79 | + * @return $this |
|
80 | + */ |
|
81 | + public function lastActiveBefore(DateTime $instant) |
|
82 | + { |
|
83 | + $this->whereClause .= ' AND origin.lastactive < ?'; |
|
84 | + $this->parameterList[] = $instant->format("Y-m-d H:i:s"); |
|
85 | + |
|
86 | + return $this; |
|
87 | + } |
|
88 | + |
|
89 | + public function getRoleMap(&$roleMap) |
|
90 | + { |
|
91 | + $query = <<<SQL |
|
92 | 92 | SELECT /* UserSearchHelper/roleMap */ |
93 | 93 | r.user user |
94 | 94 | , group_concat(r.role SEPARATOR ', ') roles |
@@ -97,22 +97,22 @@ discard block |
||
97 | 97 | GROUP BY r.user |
98 | 98 | SQL; |
99 | 99 | |
100 | - $statement = $this->database->prepare($query); |
|
101 | - $statement->execute($this->parameterList); |
|
100 | + $statement = $this->database->prepare($query); |
|
101 | + $statement->execute($this->parameterList); |
|
102 | 102 | |
103 | - $roleMap = array(); |
|
104 | - foreach ($statement->fetchAll(PDO::FETCH_ASSOC) as $row) { |
|
105 | - $roleMap[$row['user']] = $row['roles']; |
|
106 | - } |
|
103 | + $roleMap = array(); |
|
104 | + foreach ($statement->fetchAll(PDO::FETCH_ASSOC) as $row) { |
|
105 | + $roleMap[$row['user']] = $row['roles']; |
|
106 | + } |
|
107 | 107 | |
108 | - return $this; |
|
109 | - } |
|
108 | + return $this; |
|
109 | + } |
|
110 | 110 | |
111 | - public function withReservedRequest() |
|
112 | - { |
|
113 | - $this->joinClause = ' INNER JOIN request req ON req.reserved = origin.id'; |
|
114 | - $this->groupByClause = ' GROUP BY origin.id, origin.username'; |
|
111 | + public function withReservedRequest() |
|
112 | + { |
|
113 | + $this->joinClause = ' INNER JOIN request req ON req.reserved = origin.id'; |
|
114 | + $this->groupByClause = ' GROUP BY origin.id, origin.username'; |
|
115 | 115 | |
116 | - return $this->fetchMap('username'); |
|
117 | - } |
|
116 | + return $this->fetchMap('username'); |
|
117 | + } |
|
118 | 118 | } |
@@ -13,67 +13,67 @@ |
||
13 | 13 | |
14 | 14 | class JobQueueSearchHelper extends SearchHelperBase |
15 | 15 | { |
16 | - protected function __construct(PdoDatabase $database) |
|
17 | - { |
|
18 | - parent::__construct($database, 'jobqueue', JobQueue::class, null); |
|
19 | - } |
|
20 | - |
|
21 | - /** |
|
22 | - * @param PdoDatabase $database |
|
23 | - * |
|
24 | - * @return JobQueueSearchHelper |
|
25 | - */ |
|
26 | - public static function get(PdoDatabase $database) { |
|
27 | - $helper = new JobQueueSearchHelper($database); |
|
28 | - return $helper; |
|
29 | - } |
|
30 | - |
|
31 | - /** |
|
32 | - * @param string[] $statuses |
|
33 | - * |
|
34 | - * @return $this |
|
35 | - */ |
|
36 | - public function statusIn($statuses) { |
|
37 | - $this->inClause('status', $statuses); |
|
38 | - |
|
39 | - return $this; |
|
40 | - } |
|
41 | - |
|
42 | - /** |
|
43 | - * @return $this |
|
44 | - */ |
|
45 | - public function notAcknowledged() { |
|
46 | - $this->whereClause .= ' AND (acknowledged IS NULL OR acknowledged = 0)'; |
|
47 | - |
|
48 | - return $this; |
|
49 | - } |
|
50 | - |
|
51 | - public function byTask($task) { |
|
52 | - $this->whereClause .= ' AND task = ?'; |
|
53 | - $this->parameterList[] = $task; |
|
54 | - |
|
55 | - return $this; |
|
56 | - } |
|
57 | - |
|
58 | - public function byUser($userId) { |
|
59 | - $this->whereClause .= ' AND user = ?'; |
|
60 | - $this->parameterList[] = $userId; |
|
61 | - |
|
62 | - return $this; |
|
63 | - } |
|
64 | - |
|
65 | - public function byStatus($status) { |
|
66 | - $this->whereClause .= ' AND status = ?'; |
|
67 | - $this->parameterList[] = $status; |
|
68 | - |
|
69 | - return $this; |
|
70 | - } |
|
71 | - |
|
72 | - public function byRequest($request) |
|
73 | - { |
|
74 | - $this->whereClause .= ' AND request = ?'; |
|
75 | - $this->parameterList[] = $request; |
|
76 | - |
|
77 | - return $this; |
|
78 | - } |
|
16 | + protected function __construct(PdoDatabase $database) |
|
17 | + { |
|
18 | + parent::__construct($database, 'jobqueue', JobQueue::class, null); |
|
19 | + } |
|
20 | + |
|
21 | + /** |
|
22 | + * @param PdoDatabase $database |
|
23 | + * |
|
24 | + * @return JobQueueSearchHelper |
|
25 | + */ |
|
26 | + public static function get(PdoDatabase $database) { |
|
27 | + $helper = new JobQueueSearchHelper($database); |
|
28 | + return $helper; |
|
29 | + } |
|
30 | + |
|
31 | + /** |
|
32 | + * @param string[] $statuses |
|
33 | + * |
|
34 | + * @return $this |
|
35 | + */ |
|
36 | + public function statusIn($statuses) { |
|
37 | + $this->inClause('status', $statuses); |
|
38 | + |
|
39 | + return $this; |
|
40 | + } |
|
41 | + |
|
42 | + /** |
|
43 | + * @return $this |
|
44 | + */ |
|
45 | + public function notAcknowledged() { |
|
46 | + $this->whereClause .= ' AND (acknowledged IS NULL OR acknowledged = 0)'; |
|
47 | + |
|
48 | + return $this; |
|
49 | + } |
|
50 | + |
|
51 | + public function byTask($task) { |
|
52 | + $this->whereClause .= ' AND task = ?'; |
|
53 | + $this->parameterList[] = $task; |
|
54 | + |
|
55 | + return $this; |
|
56 | + } |
|
57 | + |
|
58 | + public function byUser($userId) { |
|
59 | + $this->whereClause .= ' AND user = ?'; |
|
60 | + $this->parameterList[] = $userId; |
|
61 | + |
|
62 | + return $this; |
|
63 | + } |
|
64 | + |
|
65 | + public function byStatus($status) { |
|
66 | + $this->whereClause .= ' AND status = ?'; |
|
67 | + $this->parameterList[] = $status; |
|
68 | + |
|
69 | + return $this; |
|
70 | + } |
|
71 | + |
|
72 | + public function byRequest($request) |
|
73 | + { |
|
74 | + $this->whereClause .= ' AND request = ?'; |
|
75 | + $this->parameterList[] = $request; |
|
76 | + |
|
77 | + return $this; |
|
78 | + } |
|
79 | 79 | } |
80 | 80 | \ No newline at end of file |
@@ -23,7 +23,8 @@ discard block |
||
23 | 23 | * |
24 | 24 | * @return JobQueueSearchHelper |
25 | 25 | */ |
26 | - public static function get(PdoDatabase $database) { |
|
26 | + public static function get(PdoDatabase $database) |
|
27 | + { |
|
27 | 28 | $helper = new JobQueueSearchHelper($database); |
28 | 29 | return $helper; |
29 | 30 | } |
@@ -33,7 +34,8 @@ discard block |
||
33 | 34 | * |
34 | 35 | * @return $this |
35 | 36 | */ |
36 | - public function statusIn($statuses) { |
|
37 | + public function statusIn($statuses) |
|
38 | + { |
|
37 | 39 | $this->inClause('status', $statuses); |
38 | 40 | |
39 | 41 | return $this; |
@@ -42,27 +44,31 @@ discard block |
||
42 | 44 | /** |
43 | 45 | * @return $this |
44 | 46 | */ |
45 | - public function notAcknowledged() { |
|
47 | + public function notAcknowledged() |
|
48 | + { |
|
46 | 49 | $this->whereClause .= ' AND (acknowledged IS NULL OR acknowledged = 0)'; |
47 | 50 | |
48 | 51 | return $this; |
49 | 52 | } |
50 | 53 | |
51 | - public function byTask($task) { |
|
54 | + public function byTask($task) |
|
55 | + { |
|
52 | 56 | $this->whereClause .= ' AND task = ?'; |
53 | 57 | $this->parameterList[] = $task; |
54 | 58 | |
55 | 59 | return $this; |
56 | 60 | } |
57 | 61 | |
58 | - public function byUser($userId) { |
|
62 | + public function byUser($userId) |
|
63 | + { |
|
59 | 64 | $this->whereClause .= ' AND user = ?'; |
60 | 65 | $this->parameterList[] = $userId; |
61 | 66 | |
62 | 67 | return $this; |
63 | 68 | } |
64 | 69 | |
65 | - public function byStatus($status) { |
|
70 | + public function byStatus($status) |
|
71 | + { |
|
66 | 72 | $this->whereClause .= ' AND status = ?'; |
67 | 73 | $this->parameterList[] = $status; |
68 | 74 |