Failed Conditions
Push — newinternal-releasecandidate ( a30d14...06bb07 )
by Simon
08:31
created
includes/Background/Task/BotCreationTask.php 2 patches
Indentation   +11 added lines, -11 removed lines patch added patch discarded remove patch
@@ -16,16 +16,16 @@
 block discarded – undo
16 16
 
17 17
 class BotCreationTask extends CreationTaskBase
18 18
 {
19
-    /**
20
-     * @return IMediaWikiClient
21
-     */
22
-    protected function getMediaWikiClient()
23
-    {
24
-        return new BotMediaWikiClient($this->getSiteConfiguration());
25
-    }
19
+	/**
20
+	 * @return IMediaWikiClient
21
+	 */
22
+	protected function getMediaWikiClient()
23
+	{
24
+		return new BotMediaWikiClient($this->getSiteConfiguration());
25
+	}
26 26
 
27
-    protected function getCreationReason(Request $request, User $user)
28
-    {
29
-        return parent::getCreationReason($request, $user) . ', on behalf of [[User:' . $user->getOnWikiName() . ']]';
30
-    }
27
+	protected function getCreationReason(Request $request, User $user)
28
+	{
29
+		return parent::getCreationReason($request, $user) . ', on behalf of [[User:' . $user->getOnWikiName() . ']]';
30
+	}
31 31
 }
32 32
\ 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
@@ -26,6 +26,6 @@
 block discarded – undo
26 26
 
27 27
     protected function getCreationReason(Request $request, User $user)
28 28
     {
29
-        return parent::getCreationReason($request, $user) . ', on behalf of [[User:' . $user->getOnWikiName() . ']]';
29
+        return parent::getCreationReason($request, $user).', on behalf of [[User:'.$user->getOnWikiName().']]';
30 30
     }
31 31
 }
32 32
\ No newline at end of file
Please login to merge, or discard this patch.
includes/Background/Task/UserCreationTask.php 1 patch
Indentation   +9 added lines, -9 removed lines patch added patch discarded remove patch
@@ -14,14 +14,14 @@
 block discarded – undo
14 14
 
15 15
 class UserCreationTask extends CreationTaskBase
16 16
 {
17
-    /**
18
-     * @return IMediaWikiClient
19
-     */
20
-    protected function getMediaWikiClient()
21
-    {
22
-        $oauth = new OAuthUserHelper($this->getTriggerUser(), $this->getDatabase(), $this->getOauthProtocolHelper(),
23
-            $this->getSiteConfiguration());
17
+	/**
18
+	 * @return IMediaWikiClient
19
+	 */
20
+	protected function getMediaWikiClient()
21
+	{
22
+		$oauth = new OAuthUserHelper($this->getTriggerUser(), $this->getDatabase(), $this->getOauthProtocolHelper(),
23
+			$this->getSiteConfiguration());
24 24
 
25
-        return $oauth;
26
-    }
25
+		return $oauth;
26
+	}
27 27
 }
28 28
\ No newline at end of file
Please login to merge, or discard this patch.
includes/Background/Task/WelcomeUserTask.php 3 patches
Indentation   +69 added lines, -69 removed lines patch added patch discarded remove patch
@@ -20,73 +20,73 @@
 block discarded – undo
20 20
 
21 21
 class WelcomeUserTask extends BackgroundTaskBase
22 22
 {
23
-    public static function enqueue(User $triggerUser, Request $request, PdoDatabase $database)
24
-    {
25
-        $job = new JobQueue();
26
-        $job->setDatabase($database);
27
-        $job->setTask(WelcomeUserTask::class);
28
-        $job->setRequest($request->getId());
29
-        $job->setTriggerUserId($triggerUser->getId());
30
-        $job->save();
31
-    }
32
-
33
-    public function execute()
34
-    {
35
-        $database = $this->getDatabase();
36
-        $request = $this->getRequest();
37
-        $user = $this->getTriggerUser();
38
-
39
-        if ($user->getWelcomeTemplate() === null) {
40
-            $this->markFailed('Welcome template not specified');
41
-
42
-            return;
43
-        }
44
-
45
-        /** @var WelcomeTemplate $template */
46
-        $template = WelcomeTemplate::getById($user->getWelcomeTemplate(), $database);
47
-
48
-        if ($template === false) {
49
-            $this->markFailed('Welcome template missing');
50
-
51
-            return;
52
-        }
53
-
54
-        $oauth = new OAuthUserHelper($user, $database, $this->getOauthProtocolHelper(),
55
-            $this->getSiteConfiguration());
56
-        $mediaWikiHelper = new MediaWikiHelper($oauth, $this->getSiteConfiguration());
57
-
58
-        if ($request->getStatus() !== RequestStatus::CLOSED) {
59
-            $this->markFailed('Request is currently open');
60
-
61
-            return;
62
-        }
63
-
64
-        if (!$mediaWikiHelper->checkAccountExists($request->getName())){
65
-            $this->markFailed('Account does not exist!');
66
-
67
-            return;
68
-        }
69
-
70
-        $this->performWelcome($template, $request, $mediaWikiHelper);
71
-        $this->markComplete();
72
-    }
73
-
74
-    /**
75
-     * Performs the welcome
76
-     *
77
-     * @param WelcomeTemplate $template
78
-     * @param Request         $request
79
-     * @param MediaWikiHelper $mediaWikiHelper
80
-     */
81
-    private function performWelcome(
82
-        WelcomeTemplate $template,
83
-        Request $request,
84
-        MediaWikiHelper $mediaWikiHelper
85
-    ) {
86
-        $templateText = $template->getBotCode();
87
-        $templateText = str_replace('$signature', '~~~~', $templateText);
88
-        $templateText = str_replace('$username', $request->getName(), $templateText);
89
-
90
-        $mediaWikiHelper->addTalkPageMessage($request->getName(), 'Welcome!', 'Welcoming user created through [[WP:ACC]]', $templateText);
91
-    }
23
+	public static function enqueue(User $triggerUser, Request $request, PdoDatabase $database)
24
+	{
25
+		$job = new JobQueue();
26
+		$job->setDatabase($database);
27
+		$job->setTask(WelcomeUserTask::class);
28
+		$job->setRequest($request->getId());
29
+		$job->setTriggerUserId($triggerUser->getId());
30
+		$job->save();
31
+	}
32
+
33
+	public function execute()
34
+	{
35
+		$database = $this->getDatabase();
36
+		$request = $this->getRequest();
37
+		$user = $this->getTriggerUser();
38
+
39
+		if ($user->getWelcomeTemplate() === null) {
40
+			$this->markFailed('Welcome template not specified');
41
+
42
+			return;
43
+		}
44
+
45
+		/** @var WelcomeTemplate $template */
46
+		$template = WelcomeTemplate::getById($user->getWelcomeTemplate(), $database);
47
+
48
+		if ($template === false) {
49
+			$this->markFailed('Welcome template missing');
50
+
51
+			return;
52
+		}
53
+
54
+		$oauth = new OAuthUserHelper($user, $database, $this->getOauthProtocolHelper(),
55
+			$this->getSiteConfiguration());
56
+		$mediaWikiHelper = new MediaWikiHelper($oauth, $this->getSiteConfiguration());
57
+
58
+		if ($request->getStatus() !== RequestStatus::CLOSED) {
59
+			$this->markFailed('Request is currently open');
60
+
61
+			return;
62
+		}
63
+
64
+		if (!$mediaWikiHelper->checkAccountExists($request->getName())){
65
+			$this->markFailed('Account does not exist!');
66
+
67
+			return;
68
+		}
69
+
70
+		$this->performWelcome($template, $request, $mediaWikiHelper);
71
+		$this->markComplete();
72
+	}
73
+
74
+	/**
75
+	 * Performs the welcome
76
+	 *
77
+	 * @param WelcomeTemplate $template
78
+	 * @param Request         $request
79
+	 * @param MediaWikiHelper $mediaWikiHelper
80
+	 */
81
+	private function performWelcome(
82
+		WelcomeTemplate $template,
83
+		Request $request,
84
+		MediaWikiHelper $mediaWikiHelper
85
+	) {
86
+		$templateText = $template->getBotCode();
87
+		$templateText = str_replace('$signature', '~~~~', $templateText);
88
+		$templateText = str_replace('$username', $request->getName(), $templateText);
89
+
90
+		$mediaWikiHelper->addTalkPageMessage($request->getName(), 'Welcome!', 'Welcoming user created through [[WP:ACC]]', $templateText);
91
+	}
92 92
 }
93 93
\ 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
@@ -61,7 +61,7 @@
 block discarded – undo
61 61
             return;
62 62
         }
63 63
 
64
-        if (!$mediaWikiHelper->checkAccountExists($request->getName())){
64
+        if (!$mediaWikiHelper->checkAccountExists($request->getName())) {
65 65
             $this->markFailed('Account does not exist!');
66 66
 
67 67
             return;
Please login to merge, or discard this patch.
Braces   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -61,7 +61,7 @@
 block discarded – undo
61 61
             return;
62 62
         }
63 63
 
64
-        if (!$mediaWikiHelper->checkAccountExists($request->getName())){
64
+        if (!$mediaWikiHelper->checkAccountExists($request->getName())) {
65 65
             $this->markFailed('Account does not exist!');
66 66
 
67 67
             return;
Please login to merge, or discard this patch.
includes/Background/CreationTaskBase.php 3 patches
Indentation   +115 added lines, -115 removed lines patch added patch discarded remove patch
@@ -20,123 +20,123 @@
 block discarded – undo
20 20
 
21 21
 abstract class CreationTaskBase extends BackgroundTaskBase
22 22
 {
23
-    /** @var Request */
24
-    private $request;
25
-    /**
26
-     * @var MediaWikiHelper
27
-     * Don't use this directly.
28
-     */
29
-    private $mwHelper = null;
30
-
31
-    public function execute()
32
-    {
33
-        $this->request = $this->getRequest();
34
-        $user = $this->getTriggerUser();
35
-
36
-        if ($this->request->getStatus() !== RequestStatus::JOBQUEUE) {
37
-            $this->markCancelled('Request is not deferred to the job queue');
38
-
39
-            return;
40
-        }
41
-
42
-        if ($this->request->getEmailSent() != 0) {
43
-            $this->markFailed('Request has already been sent an email');
44
-
45
-            return;
46
-        }
47
-
48
-        if ($this->getEmailTemplate() === null) {
49
-            $this->markFailed('No email template specified');
50
-
51
-            return;
52
-        }
53
-
54
-        try {
55
-            $this->performCreation($user);
56
-
57
-            $this->request->setStatus(RequestStatus::CLOSED);
58
-            $this->request->setReserved(null);
59
-            $this->request->save();
60
-
61
-            // Log the closure as the user
62
-            Logger::closeRequest($this->getDatabase(), $this->request, $this->getEmailTemplate()->getId(), null,
63
-                $this->getTriggerUser());
64
-
65
-            $requestEmailHelper = new RequestEmailHelper($this->getEmailHelper());
66
-            $requestEmailHelper->sendMail($this->request, $this->getEmailTemplate()->getText(), $this->getTriggerUser(),
67
-                false);
68
-
69
-            $this->getNotificationHelper()->requestClosed($this->request, $this->getEmailTemplate()->getName());
70
-        }
71
-        catch (Exception $ex) {
72
-            $this->markFailed($ex->getMessage());
73
-
74
-            return;
75
-        }
76
-
77
-        $this->markComplete();
78
-    }
79
-
80
-    /**
81
-     * @return IMediaWikiClient
82
-     */
83
-    protected abstract function getMediaWikiClient();
84
-
85
-    protected function getMediaWikiHelper(){
86
-        if($this->mwHelper === null) {
87
-            $this->mwHelper = new MediaWikiHelper($this->getMediaWikiClient(), $this->getSiteConfiguration());
88
-        }
89
-
90
-        return $this->mwHelper;
91
-    }
92
-
93
-    protected function getCreationReason(Request $request, User $user)
94
-    {
95
-        return 'Requested account at [[WP:ACC]], request #' . $request->getId();
96
-    }
97
-
98
-    /**
99
-     * @param string $name
100
-     *
101
-     * @return bool
102
-     */
103
-    protected function checkAccountExists($name)
104
-    {
105
-        return $this->getMediaWikiHelper()->checkAccountExists($name);
106
-    }
107
-
108
-    protected function markFailed($reason = null)
109
-    {
110
-        $this->request->setStatus(RequestStatus::HOSPITAL);
111
-        $this->request->save();
112
-
113
-        Logger::hospitalised($this->getDatabase(), $this->request);
114
-
115
-        parent::markFailed($reason);
116
-    }
117
-
118
-    /**
119
-     * @param $user
120
-     *
121
-     * @throws ApplicationLogicException
122
-     */
123
-    protected function performCreation($user)
124
-    {
125
-        $mw = $this->getMediaWikiHelper();
126
-
127
-        $reason = $this->getCreationReason($this->request, $user);
23
+	/** @var Request */
24
+	private $request;
25
+	/**
26
+	 * @var MediaWikiHelper
27
+	 * Don't use this directly.
28
+	 */
29
+	private $mwHelper = null;
30
+
31
+	public function execute()
32
+	{
33
+		$this->request = $this->getRequest();
34
+		$user = $this->getTriggerUser();
35
+
36
+		if ($this->request->getStatus() !== RequestStatus::JOBQUEUE) {
37
+			$this->markCancelled('Request is not deferred to the job queue');
38
+
39
+			return;
40
+		}
41
+
42
+		if ($this->request->getEmailSent() != 0) {
43
+			$this->markFailed('Request has already been sent an email');
44
+
45
+			return;
46
+		}
47
+
48
+		if ($this->getEmailTemplate() === null) {
49
+			$this->markFailed('No email template specified');
50
+
51
+			return;
52
+		}
53
+
54
+		try {
55
+			$this->performCreation($user);
56
+
57
+			$this->request->setStatus(RequestStatus::CLOSED);
58
+			$this->request->setReserved(null);
59
+			$this->request->save();
60
+
61
+			// Log the closure as the user
62
+			Logger::closeRequest($this->getDatabase(), $this->request, $this->getEmailTemplate()->getId(), null,
63
+				$this->getTriggerUser());
64
+
65
+			$requestEmailHelper = new RequestEmailHelper($this->getEmailHelper());
66
+			$requestEmailHelper->sendMail($this->request, $this->getEmailTemplate()->getText(), $this->getTriggerUser(),
67
+				false);
68
+
69
+			$this->getNotificationHelper()->requestClosed($this->request, $this->getEmailTemplate()->getName());
70
+		}
71
+		catch (Exception $ex) {
72
+			$this->markFailed($ex->getMessage());
73
+
74
+			return;
75
+		}
76
+
77
+		$this->markComplete();
78
+	}
79
+
80
+	/**
81
+	 * @return IMediaWikiClient
82
+	 */
83
+	protected abstract function getMediaWikiClient();
84
+
85
+	protected function getMediaWikiHelper(){
86
+		if($this->mwHelper === null) {
87
+			$this->mwHelper = new MediaWikiHelper($this->getMediaWikiClient(), $this->getSiteConfiguration());
88
+		}
89
+
90
+		return $this->mwHelper;
91
+	}
92
+
93
+	protected function getCreationReason(Request $request, User $user)
94
+	{
95
+		return 'Requested account at [[WP:ACC]], request #' . $request->getId();
96
+	}
97
+
98
+	/**
99
+	 * @param string $name
100
+	 *
101
+	 * @return bool
102
+	 */
103
+	protected function checkAccountExists($name)
104
+	{
105
+		return $this->getMediaWikiHelper()->checkAccountExists($name);
106
+	}
107
+
108
+	protected function markFailed($reason = null)
109
+	{
110
+		$this->request->setStatus(RequestStatus::HOSPITAL);
111
+		$this->request->save();
112
+
113
+		Logger::hospitalised($this->getDatabase(), $this->request);
114
+
115
+		parent::markFailed($reason);
116
+	}
117
+
118
+	/**
119
+	 * @param $user
120
+	 *
121
+	 * @throws ApplicationLogicException
122
+	 */
123
+	protected function performCreation($user)
124
+	{
125
+		$mw = $this->getMediaWikiHelper();
126
+
127
+		$reason = $this->getCreationReason($this->request, $user);
128 128
 
129
-        if ($this->checkAccountExists($this->request->getName())) {
130
-            throw new ApplicationLogicException('Account already exists');
131
-        }
129
+		if ($this->checkAccountExists($this->request->getName())) {
130
+			throw new ApplicationLogicException('Account already exists');
131
+		}
132 132
 
133
-        $mw->createAccount($this->request->getName(), $this->request->getEmail(), $reason);
133
+		$mw->createAccount($this->request->getName(), $this->request->getEmail(), $reason);
134 134
 
135
-        if (!$this->checkAccountExists($this->request->getName())) {
136
-            throw new ApplicationLogicException('Account creation appeared to succeed but account does not exist.');
137
-        }
135
+		if (!$this->checkAccountExists($this->request->getName())) {
136
+			throw new ApplicationLogicException('Account creation appeared to succeed but account does not exist.');
137
+		}
138 138
 
139
-        $this->request->setStatus(RequestStatus::CLOSED);
140
-        $this->request->save();
141
-    }
139
+		$this->request->setStatus(RequestStatus::CLOSED);
140
+		$this->request->save();
141
+	}
142 142
 }
143 143
\ No newline at end of file
Please login to merge, or discard this patch.
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -82,8 +82,8 @@  discard block
 block discarded – undo
82 82
      */
83 83
     protected abstract function getMediaWikiClient();
84 84
 
85
-    protected function getMediaWikiHelper(){
86
-        if($this->mwHelper === null) {
85
+    protected function getMediaWikiHelper() {
86
+        if ($this->mwHelper === null) {
87 87
             $this->mwHelper = new MediaWikiHelper($this->getMediaWikiClient(), $this->getSiteConfiguration());
88 88
         }
89 89
 
@@ -92,7 +92,7 @@  discard block
 block discarded – undo
92 92
 
93 93
     protected function getCreationReason(Request $request, User $user)
94 94
     {
95
-        return 'Requested account at [[WP:ACC]], request #' . $request->getId();
95
+        return 'Requested account at [[WP:ACC]], request #'.$request->getId();
96 96
     }
97 97
 
98 98
     /**
Please login to merge, or discard this patch.
Braces   +2 added lines, -1 removed lines patch added patch discarded remove patch
@@ -82,7 +82,8 @@
 block discarded – undo
82 82
      */
83 83
     protected abstract function getMediaWikiClient();
84 84
 
85
-    protected function getMediaWikiHelper(){
85
+    protected function getMediaWikiHelper()
86
+    {
86 87
         if($this->mwHelper === null) {
87 88
             $this->mwHelper = new MediaWikiHelper($this->getMediaWikiClient(), $this->getSiteConfiguration());
88 89
         }
Please login to merge, or discard this patch.
includes/Background/BackgroundTaskBase.php 3 patches
Braces   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -177,7 +177,7 @@
 block discarded – undo
177 177
             throw new ApplicationLogicException('Cannot locate request');
178 178
         }
179 179
 
180
-        if($this->job->getEmailTemplate() !== null){
180
+        if($this->job->getEmailTemplate() !== null) {
181 181
             $this->emailTemplate = EmailTemplate::getById($this->job->getEmailTemplate(), $this->getDatabase());
182 182
 
183 183
             if ($this->emailTemplate === false) {
Please login to merge, or discard this patch.
Indentation   +263 added lines, -263 removed lines patch added patch discarded remove patch
@@ -23,267 +23,267 @@
 block discarded – undo
23 23
 
24 24
 abstract class BackgroundTaskBase
25 25
 {
26
-    /** @var JobQueue */
27
-    private $job;
28
-    /** @var PdoDatabase */
29
-    private $database;
30
-    /** @var IOAuthProtocolHelper */
31
-    private $oauthProtocolHelper;
32
-    /** @var SiteConfiguration */
33
-    private $siteConfiguration;
34
-    /** @var IEmailHelper */
35
-    private $emailHelper;
36
-    /** @var HttpHelper */
37
-    private $httpHelper;
38
-    /** @var IrcNotificationHelper */
39
-    private $notificationHelper;
40
-    /** @var User */
41
-    private $triggerUser;
42
-    /** @var Request */
43
-    private $request;
44
-    /** @var EmailTemplate */
45
-    private $emailTemplate = null;
46
-    /** @var mixed */
47
-    private $parameters;
48
-
49
-    /**
50
-     * @return JobQueue
51
-     */
52
-    public function getJob()
53
-    {
54
-        return $this->job;
55
-    }
56
-
57
-    /**
58
-     * @param JobQueue $job
59
-     */
60
-    public function setJob(JobQueue $job)
61
-    {
62
-        $this->job = $job;
63
-    }
64
-
65
-    /**
66
-     * @return PdoDatabase
67
-     */
68
-    public function getDatabase()
69
-    {
70
-        return $this->database;
71
-    }
72
-
73
-    /**
74
-     * @param PdoDatabase $database
75
-     */
76
-    public function setDatabase(PdoDatabase $database)
77
-    {
78
-        $this->database = $database;
79
-    }
80
-
81
-    /**
82
-     * @return IOAuthProtocolHelper
83
-     */
84
-    public function getOauthProtocolHelper()
85
-    {
86
-        return $this->oauthProtocolHelper;
87
-    }
88
-
89
-    /**
90
-     * @param IOAuthProtocolHelper $oauthProtocolHelper
91
-     */
92
-    public function setOauthProtocolHelper(IOAuthProtocolHelper $oauthProtocolHelper)
93
-    {
94
-        $this->oauthProtocolHelper = $oauthProtocolHelper;
95
-    }
96
-
97
-    /**
98
-     * @return SiteConfiguration
99
-     */
100
-    public function getSiteConfiguration()
101
-    {
102
-        return $this->siteConfiguration;
103
-    }
104
-
105
-    /**
106
-     * @param SiteConfiguration $siteConfiguration
107
-     */
108
-    public function setSiteConfiguration(SiteConfiguration $siteConfiguration)
109
-    {
110
-        $this->siteConfiguration = $siteConfiguration;
111
-    }
112
-
113
-    /**
114
-     * @return HttpHelper
115
-     */
116
-    public function getHttpHelper()
117
-    {
118
-        return $this->httpHelper;
119
-    }
120
-
121
-    /**
122
-     * @param HttpHelper $httpHelper
123
-     */
124
-    public function setHttpHelper(HttpHelper $httpHelper)
125
-    {
126
-        $this->httpHelper = $httpHelper;
127
-    }
128
-
129
-    /**
130
-     * @return IEmailHelper
131
-     */
132
-    public function getEmailHelper()
133
-    {
134
-        return $this->emailHelper;
135
-    }
136
-
137
-    /**
138
-     * @param IEmailHelper $emailHelper
139
-     */
140
-    public function setEmailHelper(IEmailHelper $emailHelper)
141
-    {
142
-        $this->emailHelper = $emailHelper;
143
-    }
144
-
145
-    /**
146
-     * @return IrcNotificationHelper
147
-     */
148
-    public function getNotificationHelper()
149
-    {
150
-        return $this->notificationHelper;
151
-    }
152
-
153
-    /**
154
-     * @param IrcNotificationHelper $notificationHelper
155
-     */
156
-    public function setNotificationHelper($notificationHelper)
157
-    {
158
-        $this->notificationHelper = $notificationHelper;
159
-    }
160
-
161
-    /**
162
-     * @return void
163
-     */
164
-    protected abstract function execute();
165
-
166
-    public function run()
167
-    {
168
-        $this->triggerUser = User::getById($this->job->getTriggerUserId(), $this->getDatabase());
169
-
170
-        if ($this->triggerUser === false) {
171
-            throw new ApplicationLogicException('Cannot locate trigger user');
172
-        }
173
-
174
-        $this->request = Request::getById($this->job->getRequest(), $this->getDatabase());
175
-
176
-        if ($this->request === false) {
177
-            throw new ApplicationLogicException('Cannot locate request');
178
-        }
179
-
180
-        if($this->job->getEmailTemplate() !== null){
181
-            $this->emailTemplate = EmailTemplate::getById($this->job->getEmailTemplate(), $this->getDatabase());
182
-
183
-            if ($this->emailTemplate === false) {
184
-                throw new ApplicationLogicException('Cannot locate email template');
185
-            }
186
-        }
187
-
188
-        if($this->job->getParameters() !== null) {
189
-            $this->parameters = json_decode($this->job->getParameters());
190
-
191
-            if (json_last_error() !== JSON_ERROR_NONE) {
192
-                throw new ApplicationLogicException('JSON decode: ' . json_last_error_msg());
193
-            }
194
-        }
195
-
196
-        // Should we wait for a parent job?
197
-        if($this->job->getParent() !== null) {
198
-            /** @var JobQueue $parentJob */
199
-            $parentJob = JobQueue::getById($this->job->getParent(), $this->getDatabase());
200
-
201
-            if($parentJob === false) {
202
-                $this->markFailed("Parent job could not be found");
203
-                return;
204
-            }
205
-
206
-            switch ($parentJob->getStatus()) {
207
-                case JobQueue::STATUS_CANCELLED:
208
-                case JobQueue::STATUS_FAILED:
209
-                    $this->markCancelled('Parent job failed/cancelled');
210
-                    return;
211
-                case JobQueue::STATUS_WAITING:
212
-                case JobQueue::STATUS_READY:
213
-                case JobQueue::STATUS_RUNNING:
214
-                case JobQueue::STATUS_HELD:
215
-                    // Defer to next execution
216
-                    $this->job->setStatus(JobQueue::STATUS_READY);
217
-                    $this->job->save();
218
-                    return;
219
-                case JobQueue::STATUS_COMPLETE:
220
-                    // do nothing
221
-                    break;
222
-            }
223
-        }
224
-
225
-        $this->execute();
226
-    }
227
-
228
-    protected function markComplete()
229
-    {
230
-        $this->job->setStatus(JobQueue::STATUS_COMPLETE);
231
-        $this->job->setError(null);
232
-        $this->job->setAcknowledged(null);
233
-        $this->job->save();
234
-
235
-        Logger::backgroundJobComplete($this->getDatabase(), $this->getJob());
236
-    }
237
-
238
-    protected function markCancelled($reason = null)
239
-    {
240
-        $this->job->setStatus(JobQueue::STATUS_CANCELLED);
241
-        $this->job->setError($reason);
242
-        $this->job->setAcknowledged(null);
243
-        $this->job->save();
244
-
245
-        Logger::backgroundJobIssue($this->getDatabase(), $this->getJob());
246
-    }
247
-
248
-    protected function markFailed($reason = null)
249
-    {
250
-        $this->job->setStatus(JobQueue::STATUS_FAILED);
251
-        $this->job->setError($reason);
252
-        $this->job->setAcknowledged(0);
253
-        $this->job->save();
254
-
255
-        Logger::backgroundJobIssue($this->getDatabase(), $this->getJob());
256
-    }
257
-
258
-    /**
259
-     * @return User
260
-     */
261
-    public function getTriggerUser()
262
-    {
263
-        return $this->triggerUser;
264
-    }
265
-
266
-    /**
267
-     * @return Request
268
-     */
269
-    public function getRequest()
270
-    {
271
-        return $this->request;
272
-    }
273
-
274
-    /**
275
-     * @return EmailTemplate
276
-     */
277
-    public function getEmailTemplate()
278
-    {
279
-        return $this->emailTemplate;
280
-    }
281
-
282
-    /**
283
-     * @return mixed
284
-     */
285
-    public function getParameters()
286
-    {
287
-        return $this->parameters;
288
-    }
26
+	/** @var JobQueue */
27
+	private $job;
28
+	/** @var PdoDatabase */
29
+	private $database;
30
+	/** @var IOAuthProtocolHelper */
31
+	private $oauthProtocolHelper;
32
+	/** @var SiteConfiguration */
33
+	private $siteConfiguration;
34
+	/** @var IEmailHelper */
35
+	private $emailHelper;
36
+	/** @var HttpHelper */
37
+	private $httpHelper;
38
+	/** @var IrcNotificationHelper */
39
+	private $notificationHelper;
40
+	/** @var User */
41
+	private $triggerUser;
42
+	/** @var Request */
43
+	private $request;
44
+	/** @var EmailTemplate */
45
+	private $emailTemplate = null;
46
+	/** @var mixed */
47
+	private $parameters;
48
+
49
+	/**
50
+	 * @return JobQueue
51
+	 */
52
+	public function getJob()
53
+	{
54
+		return $this->job;
55
+	}
56
+
57
+	/**
58
+	 * @param JobQueue $job
59
+	 */
60
+	public function setJob(JobQueue $job)
61
+	{
62
+		$this->job = $job;
63
+	}
64
+
65
+	/**
66
+	 * @return PdoDatabase
67
+	 */
68
+	public function getDatabase()
69
+	{
70
+		return $this->database;
71
+	}
72
+
73
+	/**
74
+	 * @param PdoDatabase $database
75
+	 */
76
+	public function setDatabase(PdoDatabase $database)
77
+	{
78
+		$this->database = $database;
79
+	}
80
+
81
+	/**
82
+	 * @return IOAuthProtocolHelper
83
+	 */
84
+	public function getOauthProtocolHelper()
85
+	{
86
+		return $this->oauthProtocolHelper;
87
+	}
88
+
89
+	/**
90
+	 * @param IOAuthProtocolHelper $oauthProtocolHelper
91
+	 */
92
+	public function setOauthProtocolHelper(IOAuthProtocolHelper $oauthProtocolHelper)
93
+	{
94
+		$this->oauthProtocolHelper = $oauthProtocolHelper;
95
+	}
96
+
97
+	/**
98
+	 * @return SiteConfiguration
99
+	 */
100
+	public function getSiteConfiguration()
101
+	{
102
+		return $this->siteConfiguration;
103
+	}
104
+
105
+	/**
106
+	 * @param SiteConfiguration $siteConfiguration
107
+	 */
108
+	public function setSiteConfiguration(SiteConfiguration $siteConfiguration)
109
+	{
110
+		$this->siteConfiguration = $siteConfiguration;
111
+	}
112
+
113
+	/**
114
+	 * @return HttpHelper
115
+	 */
116
+	public function getHttpHelper()
117
+	{
118
+		return $this->httpHelper;
119
+	}
120
+
121
+	/**
122
+	 * @param HttpHelper $httpHelper
123
+	 */
124
+	public function setHttpHelper(HttpHelper $httpHelper)
125
+	{
126
+		$this->httpHelper = $httpHelper;
127
+	}
128
+
129
+	/**
130
+	 * @return IEmailHelper
131
+	 */
132
+	public function getEmailHelper()
133
+	{
134
+		return $this->emailHelper;
135
+	}
136
+
137
+	/**
138
+	 * @param IEmailHelper $emailHelper
139
+	 */
140
+	public function setEmailHelper(IEmailHelper $emailHelper)
141
+	{
142
+		$this->emailHelper = $emailHelper;
143
+	}
144
+
145
+	/**
146
+	 * @return IrcNotificationHelper
147
+	 */
148
+	public function getNotificationHelper()
149
+	{
150
+		return $this->notificationHelper;
151
+	}
152
+
153
+	/**
154
+	 * @param IrcNotificationHelper $notificationHelper
155
+	 */
156
+	public function setNotificationHelper($notificationHelper)
157
+	{
158
+		$this->notificationHelper = $notificationHelper;
159
+	}
160
+
161
+	/**
162
+	 * @return void
163
+	 */
164
+	protected abstract function execute();
165
+
166
+	public function run()
167
+	{
168
+		$this->triggerUser = User::getById($this->job->getTriggerUserId(), $this->getDatabase());
169
+
170
+		if ($this->triggerUser === false) {
171
+			throw new ApplicationLogicException('Cannot locate trigger user');
172
+		}
173
+
174
+		$this->request = Request::getById($this->job->getRequest(), $this->getDatabase());
175
+
176
+		if ($this->request === false) {
177
+			throw new ApplicationLogicException('Cannot locate request');
178
+		}
179
+
180
+		if($this->job->getEmailTemplate() !== null){
181
+			$this->emailTemplate = EmailTemplate::getById($this->job->getEmailTemplate(), $this->getDatabase());
182
+
183
+			if ($this->emailTemplate === false) {
184
+				throw new ApplicationLogicException('Cannot locate email template');
185
+			}
186
+		}
187
+
188
+		if($this->job->getParameters() !== null) {
189
+			$this->parameters = json_decode($this->job->getParameters());
190
+
191
+			if (json_last_error() !== JSON_ERROR_NONE) {
192
+				throw new ApplicationLogicException('JSON decode: ' . json_last_error_msg());
193
+			}
194
+		}
195
+
196
+		// Should we wait for a parent job?
197
+		if($this->job->getParent() !== null) {
198
+			/** @var JobQueue $parentJob */
199
+			$parentJob = JobQueue::getById($this->job->getParent(), $this->getDatabase());
200
+
201
+			if($parentJob === false) {
202
+				$this->markFailed("Parent job could not be found");
203
+				return;
204
+			}
205
+
206
+			switch ($parentJob->getStatus()) {
207
+				case JobQueue::STATUS_CANCELLED:
208
+				case JobQueue::STATUS_FAILED:
209
+					$this->markCancelled('Parent job failed/cancelled');
210
+					return;
211
+				case JobQueue::STATUS_WAITING:
212
+				case JobQueue::STATUS_READY:
213
+				case JobQueue::STATUS_RUNNING:
214
+				case JobQueue::STATUS_HELD:
215
+					// Defer to next execution
216
+					$this->job->setStatus(JobQueue::STATUS_READY);
217
+					$this->job->save();
218
+					return;
219
+				case JobQueue::STATUS_COMPLETE:
220
+					// do nothing
221
+					break;
222
+			}
223
+		}
224
+
225
+		$this->execute();
226
+	}
227
+
228
+	protected function markComplete()
229
+	{
230
+		$this->job->setStatus(JobQueue::STATUS_COMPLETE);
231
+		$this->job->setError(null);
232
+		$this->job->setAcknowledged(null);
233
+		$this->job->save();
234
+
235
+		Logger::backgroundJobComplete($this->getDatabase(), $this->getJob());
236
+	}
237
+
238
+	protected function markCancelled($reason = null)
239
+	{
240
+		$this->job->setStatus(JobQueue::STATUS_CANCELLED);
241
+		$this->job->setError($reason);
242
+		$this->job->setAcknowledged(null);
243
+		$this->job->save();
244
+
245
+		Logger::backgroundJobIssue($this->getDatabase(), $this->getJob());
246
+	}
247
+
248
+	protected function markFailed($reason = null)
249
+	{
250
+		$this->job->setStatus(JobQueue::STATUS_FAILED);
251
+		$this->job->setError($reason);
252
+		$this->job->setAcknowledged(0);
253
+		$this->job->save();
254
+
255
+		Logger::backgroundJobIssue($this->getDatabase(), $this->getJob());
256
+	}
257
+
258
+	/**
259
+	 * @return User
260
+	 */
261
+	public function getTriggerUser()
262
+	{
263
+		return $this->triggerUser;
264
+	}
265
+
266
+	/**
267
+	 * @return Request
268
+	 */
269
+	public function getRequest()
270
+	{
271
+		return $this->request;
272
+	}
273
+
274
+	/**
275
+	 * @return EmailTemplate
276
+	 */
277
+	public function getEmailTemplate()
278
+	{
279
+		return $this->emailTemplate;
280
+	}
281
+
282
+	/**
283
+	 * @return mixed
284
+	 */
285
+	public function getParameters()
286
+	{
287
+		return $this->parameters;
288
+	}
289 289
 }
Please login to merge, or discard this patch.
Spacing   +5 added lines, -5 removed lines patch added patch discarded remove patch
@@ -177,7 +177,7 @@  discard block
 block discarded – undo
177 177
             throw new ApplicationLogicException('Cannot locate request');
178 178
         }
179 179
 
180
-        if($this->job->getEmailTemplate() !== null){
180
+        if ($this->job->getEmailTemplate() !== null) {
181 181
             $this->emailTemplate = EmailTemplate::getById($this->job->getEmailTemplate(), $this->getDatabase());
182 182
 
183 183
             if ($this->emailTemplate === false) {
@@ -185,20 +185,20 @@  discard block
 block discarded – undo
185 185
             }
186 186
         }
187 187
 
188
-        if($this->job->getParameters() !== null) {
188
+        if ($this->job->getParameters() !== null) {
189 189
             $this->parameters = json_decode($this->job->getParameters());
190 190
 
191 191
             if (json_last_error() !== JSON_ERROR_NONE) {
192
-                throw new ApplicationLogicException('JSON decode: ' . json_last_error_msg());
192
+                throw new ApplicationLogicException('JSON decode: '.json_last_error_msg());
193 193
             }
194 194
         }
195 195
 
196 196
         // Should we wait for a parent job?
197
-        if($this->job->getParent() !== null) {
197
+        if ($this->job->getParent() !== null) {
198 198
             /** @var JobQueue $parentJob */
199 199
             $parentJob = JobQueue::getById($this->job->getParent(), $this->getDatabase());
200 200
 
201
-            if($parentJob === false) {
201
+            if ($parentJob === false) {
202 202
                 $this->markFailed("Parent job could not be found");
203 203
                 return;
204 204
             }
Please login to merge, or discard this patch.
includes/Fragments/NavigationMenuAccessControl.php 1 patch
Indentation   +39 added lines, -39 removed lines patch added patch discarded remove patch
@@ -25,48 +25,48 @@
 block discarded – undo
25 25
 
26 26
 trait NavigationMenuAccessControl
27 27
 {
28
-    protected abstract function assign($name, $value);
28
+	protected abstract function assign($name, $value);
29 29
 
30
-    /**
31
-     * @return SecurityManager
32
-     */
33
-    protected abstract function getSecurityManager();
30
+	/**
31
+	 * @return SecurityManager
32
+	 */
33
+	protected abstract function getSecurityManager();
34 34
 
35
-    /**
36
-     * @param $currentUser
37
-     */
38
-    protected function setupNavMenuAccess($currentUser)
39
-    {
40
-        $this->assign('nav__canRequests', $this->getSecurityManager()
41
-                ->allows(PageMain::class, RoleConfiguration::MAIN, $currentUser) === SecurityManager::ALLOWED);
35
+	/**
36
+	 * @param $currentUser
37
+	 */
38
+	protected function setupNavMenuAccess($currentUser)
39
+	{
40
+		$this->assign('nav__canRequests', $this->getSecurityManager()
41
+				->allows(PageMain::class, RoleConfiguration::MAIN, $currentUser) === SecurityManager::ALLOWED);
42 42
 
43
-        $this->assign('nav__canLogs', $this->getSecurityManager()
44
-                ->allows(PageLog::class, RoleConfiguration::MAIN, $currentUser) === SecurityManager::ALLOWED);
45
-        $this->assign('nav__canUsers', $this->getSecurityManager()
46
-                ->allows(StatsUsers::class, RoleConfiguration::MAIN, $currentUser) === SecurityManager::ALLOWED);
47
-        $this->assign('nav__canSearch', $this->getSecurityManager()
48
-                ->allows(PageSearch::class, RoleConfiguration::MAIN, $currentUser) === SecurityManager::ALLOWED);
49
-        $this->assign('nav__canStats', $this->getSecurityManager()
50
-                ->allows(StatsMain::class, RoleConfiguration::MAIN, $currentUser) === SecurityManager::ALLOWED);
43
+		$this->assign('nav__canLogs', $this->getSecurityManager()
44
+				->allows(PageLog::class, RoleConfiguration::MAIN, $currentUser) === SecurityManager::ALLOWED);
45
+		$this->assign('nav__canUsers', $this->getSecurityManager()
46
+				->allows(StatsUsers::class, RoleConfiguration::MAIN, $currentUser) === SecurityManager::ALLOWED);
47
+		$this->assign('nav__canSearch', $this->getSecurityManager()
48
+				->allows(PageSearch::class, RoleConfiguration::MAIN, $currentUser) === SecurityManager::ALLOWED);
49
+		$this->assign('nav__canStats', $this->getSecurityManager()
50
+				->allows(StatsMain::class, RoleConfiguration::MAIN, $currentUser) === SecurityManager::ALLOWED);
51 51
 
52
-        $this->assign('nav__canBan', $this->getSecurityManager()
53
-                ->allows(PageBan::class, RoleConfiguration::MAIN, $currentUser) === SecurityManager::ALLOWED);
54
-        $this->assign('nav__canEmailMgmt', $this->getSecurityManager()
55
-                ->allows(PageEmailManagement::class, RoleConfiguration::MAIN,
56
-                    $currentUser) === SecurityManager::ALLOWED);
57
-        $this->assign('nav__canWelcomeMgmt', $this->getSecurityManager()
58
-                ->allows(PageWelcomeTemplateManagement::class, RoleConfiguration::MAIN,
59
-                    $currentUser) === SecurityManager::ALLOWED);
60
-        $this->assign('nav__canSiteNoticeMgmt', $this->getSecurityManager()
61
-                ->allows(PageSiteNotice::class, RoleConfiguration::MAIN, $currentUser) === SecurityManager::ALLOWED);
62
-        $this->assign('nav__canUserMgmt', $this->getSecurityManager()
63
-                ->allows(PageUserManagement::class, RoleConfiguration::MAIN,
64
-                    $currentUser) === SecurityManager::ALLOWED);
65
-        $this->assign('nav__canJobQueue', $this->getSecurityManager()
66
-                ->allows(PageJobQueue::class, RoleConfiguration::MAIN,
67
-                    $currentUser) === SecurityManager::ALLOWED);
52
+		$this->assign('nav__canBan', $this->getSecurityManager()
53
+				->allows(PageBan::class, RoleConfiguration::MAIN, $currentUser) === SecurityManager::ALLOWED);
54
+		$this->assign('nav__canEmailMgmt', $this->getSecurityManager()
55
+				->allows(PageEmailManagement::class, RoleConfiguration::MAIN,
56
+					$currentUser) === SecurityManager::ALLOWED);
57
+		$this->assign('nav__canWelcomeMgmt', $this->getSecurityManager()
58
+				->allows(PageWelcomeTemplateManagement::class, RoleConfiguration::MAIN,
59
+					$currentUser) === SecurityManager::ALLOWED);
60
+		$this->assign('nav__canSiteNoticeMgmt', $this->getSecurityManager()
61
+				->allows(PageSiteNotice::class, RoleConfiguration::MAIN, $currentUser) === SecurityManager::ALLOWED);
62
+		$this->assign('nav__canUserMgmt', $this->getSecurityManager()
63
+				->allows(PageUserManagement::class, RoleConfiguration::MAIN,
64
+					$currentUser) === SecurityManager::ALLOWED);
65
+		$this->assign('nav__canJobQueue', $this->getSecurityManager()
66
+				->allows(PageJobQueue::class, RoleConfiguration::MAIN,
67
+					$currentUser) === SecurityManager::ALLOWED);
68 68
 
69
-        $this->assign('nav__canViewRequest', $this->getSecurityManager()
70
-                ->allows(PageViewRequest::class, RoleConfiguration::MAIN, $currentUser) === SecurityManager::ALLOWED);
71
-    }
69
+		$this->assign('nav__canViewRequest', $this->getSecurityManager()
70
+				->allows(PageViewRequest::class, RoleConfiguration::MAIN, $currentUser) === SecurityManager::ALLOWED);
71
+	}
72 72
 }
73 73
\ No newline at end of file
Please login to merge, or discard this patch.
includes/Fragments/RequestData.php 1 patch
Indentation   +320 added lines, -320 removed lines patch added patch discarded remove patch
@@ -24,324 +24,324 @@
 block discarded – undo
24 24
 
25 25
 trait RequestData
26 26
 {
27
-    /**
28
-     * @var array Array of IP address classed as 'private' by RFC1918.
29
-     */
30
-    protected static $rfc1918ips = array(
31
-        "10.0.0.0"    => "10.255.255.255",
32
-        "172.16.0.0"  => "172.31.255.255",
33
-        "192.168.0.0" => "192.168.255.255",
34
-        "169.254.0.0" => "169.254.255.255",
35
-        "127.0.0.0"   => "127.255.255.255",
36
-    );
37
-
38
-    /**
39
-     * Gets a request object
40
-     *
41
-     * @param PdoDatabase $database  The database connection
42
-     * @param int         $requestId The ID of the request to retrieve
43
-     *
44
-     * @return Request
45
-     * @throws ApplicationLogicException
46
-     */
47
-    protected function getRequest(PdoDatabase $database, $requestId)
48
-    {
49
-        if ($requestId === null) {
50
-            throw new ApplicationLogicException("No request specified");
51
-        }
52
-
53
-        $request = Request::getById($requestId, $database);
54
-        if ($request === false || !is_a($request, Request::class)) {
55
-            throw new ApplicationLogicException('Could not load the requested request!');
56
-        }
57
-
58
-        return $request;
59
-    }
60
-
61
-    /**
62
-     * Returns a value stating whether the user is allowed to see private data or not
63
-     *
64
-     * @param Request $request
65
-     * @param User    $currentUser
66
-     *
67
-     * @return bool
68
-     * @category Security-Critical
69
-     */
70
-    protected function isAllowedPrivateData(Request $request, User $currentUser)
71
-    {
72
-        // Test the main security barrier for private data access using SecurityManager
73
-        if ($this->barrierTest('alwaysSeePrivateData', $currentUser, 'RequestData')) {
74
-            // Tool admins/check-users can always see private data
75
-            return true;
76
-        }
77
-
78
-        // reserving user is allowed to see the data
79
-        if ($currentUser->getId() === $request->getReserved()
80
-            && $request->getReserved() !== null
81
-            && $this->barrierTest('seePrivateDataWhenReserved', $currentUser, 'RequestData')
82
-        ) {
83
-            return true;
84
-        }
85
-
86
-        // user has the reveal hash
87
-        if (WebRequest::getString('hash') === $request->getRevealHash()
88
-            && $this->barrierTest('seePrivateDataWithHash', $currentUser, 'RequestData')
89
-        ) {
90
-            return true;
91
-        }
92
-
93
-        // nope. Not allowed.
94
-        return false;
95
-    }
96
-
97
-    /**
98
-     * Tests the security barrier for a specified action.
99
-     *
100
-     * Don't use within templates
101
-     *
102
-     * @param string      $action
103
-     *
104
-     * @param User        $user
105
-     * @param null|string $pageName
106
-     *
107
-     * @return bool
108
-     * @category Security-Critical
109
-     */
110
-    abstract protected function barrierTest($action, User $user, $pageName = null);
111
-
112
-    /**
113
-     * Gets the name of the route that has been passed from the request router.
114
-     * @return string
115
-     */
116
-    abstract protected function getRouteName();
117
-
118
-    /** @return SecurityManager */
119
-    abstract protected function getSecurityManager();
120
-
121
-    /**
122
-     * Sets the name of the template this page should display.
123
-     *
124
-     * @param string $name
125
-     */
126
-    abstract protected function setTemplate($name);
127
-
128
-    /** @return IXffTrustProvider */
129
-    abstract protected function getXffTrustProvider();
130
-
131
-    /** @return ILocationProvider */
132
-    abstract protected function getLocationProvider();
133
-
134
-    /** @return IRDnsProvider */
135
-    abstract protected function getRdnsProvider();
136
-
137
-    /**
138
-     * Assigns a Smarty variable
139
-     *
140
-     * @param  array|string $name  the template variable name(s)
141
-     * @param  mixed        $value the value to assign
142
-     */
143
-    abstract protected function assign($name, $value);
144
-
145
-    /**
146
-     * @param int         $requestReservationId
147
-     * @param PdoDatabase $database
148
-     * @param User        $currentUser
149
-     */
150
-    protected function setupReservationDetails($requestReservationId, PdoDatabase $database, User $currentUser)
151
-    {
152
-        $requestIsReserved = $requestReservationId !== null;
153
-        $this->assign('requestIsReserved', $requestIsReserved);
154
-        $this->assign('requestIsReservedByMe', false);
155
-
156
-        if ($requestIsReserved) {
157
-            $this->assign('requestReservedByName', User::getById($requestReservationId, $database)->getUsername());
158
-            $this->assign('requestReservedById', $requestReservationId);
159
-
160
-            if ($requestReservationId === $currentUser->getId()) {
161
-                $this->assign('requestIsReservedByMe', true);
162
-            }
163
-        }
164
-
165
-        $this->assign('canBreakReservation', $this->barrierTest('force', $currentUser, PageBreakReservation::class));
166
-    }
167
-
168
-    /**
169
-     * Adds private request data to Smarty. DO NOT USE WITHOUT FIRST CHECKING THAT THE USER IS AUTHORISED!
170
-     *
171
-     * @param Request           $request
172
-     * @param User              $currentUser
173
-     * @param SiteConfiguration $configuration
174
-     *
175
-     * @param PdoDatabase       $database
176
-     */
177
-    protected function setupPrivateData(
178
-        $request,
179
-        User $currentUser,
180
-        SiteConfiguration $configuration,
181
-        PdoDatabase $database
182
-    ) {
183
-        $xffProvider = $this->getXffTrustProvider();
184
-
185
-        $relatedEmailRequests = RequestSearchHelper::get($database)
186
-            ->byEmailAddress($request->getEmail())
187
-            ->withConfirmedEmail()
188
-            ->excludingPurgedData($configuration)
189
-            ->excludingRequest($request->getId())
190
-            ->fetch();
191
-
192
-        $this->assign('requestEmail', $request->getEmail());
193
-        $emailDomain = explode("@", $request->getEmail())[1];
194
-        $this->assign("emailurl", $emailDomain);
195
-        $this->assign('requestRelatedEmailRequestsCount', count($relatedEmailRequests));
196
-        $this->assign('requestRelatedEmailRequests', $relatedEmailRequests);
197
-
198
-        $trustedIp = $xffProvider->getTrustedClientIp($request->getIp(), $request->getForwardedIp());
199
-        $this->assign('requestTrustedIp', $trustedIp);
200
-        $this->assign('requestRealIp', $request->getIp());
201
-        $this->assign('requestForwardedIp', $request->getForwardedIp());
202
-
203
-        $trustedIpLocation = $this->getLocationProvider()->getIpLocation($trustedIp);
204
-        $this->assign('requestTrustedIpLocation', $trustedIpLocation);
205
-
206
-        $this->assign('requestHasForwardedIp', $request->getForwardedIp() !== null);
207
-
208
-        $relatedIpRequests = RequestSearchHelper::get($database)
209
-            ->byIp($trustedIp)
210
-            ->withConfirmedEmail()
211
-            ->excludingPurgedData($configuration)
212
-            ->excludingRequest($request->getId())
213
-            ->fetch();
214
-
215
-        $this->assign('requestRelatedIpRequestsCount', count($relatedIpRequests));
216
-        $this->assign('requestRelatedIpRequests', $relatedIpRequests);
217
-
218
-        $this->assign('showRevealLink', false);
219
-        if ($request->getReserved() === $currentUser->getId() ||
220
-            $this->barrierTest('alwaysSeeHash', $currentUser, 'RequestData')
221
-        ) {
222
-            $this->assign('showRevealLink', true);
223
-            $this->assign('revealHash', $request->getRevealHash());
224
-        }
225
-
226
-        $this->setupForwardedIpData($request);
227
-    }
228
-
229
-    /**
230
-     * Adds checkuser request data to Smarty. DO NOT USE WITHOUT FIRST CHECKING THAT THE USER IS AUTHORISED!
231
-     *
232
-     * @param Request $request
233
-     */
234
-    protected function setupCheckUserData(Request $request)
235
-    {
236
-        $this->assign('requestUserAgent', $request->getUserAgent());
237
-    }
238
-
239
-    /**
240
-     * Sets up the basic data for this request, and adds it to Smarty
241
-     *
242
-     * @param Request           $request
243
-     * @param SiteConfiguration $config
244
-     */
245
-    protected function setupBasicData(Request $request, SiteConfiguration $config)
246
-    {
247
-        $this->assign('requestId', $request->getId());
248
-        $this->assign('updateVersion', $request->getUpdateVersion());
249
-        $this->assign('requestName', $request->getName());
250
-        $this->assign('requestDate', $request->getDate());
251
-        $this->assign('requestStatus', $request->getStatus());
252
-
253
-        $isClosed = !array_key_exists($request->getStatus(), $config->getRequestStates())
254
-            && $request->getStatus() !== RequestStatus::HOSPITAL;
255
-        $this->assign('requestIsClosed', $isClosed);
256
-    }
257
-
258
-    /**
259
-     * Sets up the forwarded IP data for this request and adds it to Smarty
260
-     *
261
-     * @param Request $request
262
-     */
263
-    protected function setupForwardedIpData(Request $request)
264
-    {
265
-        if ($request->getForwardedIp() !== null) {
266
-            $requestProxyData = array(); // Initialize array to store data to be output in Smarty template.
267
-            $proxyIndex = 0;
268
-
269
-            // Assuming [client] <=> [proxy1] <=> [proxy2] <=> [proxy3] <=> [us], we will see an XFF header of [client],
270
-            // [proxy1], [proxy2], and our actual IP will be [proxy3]
271
-            $proxies = explode(",", $request->getForwardedIp());
272
-            $proxies[] = $request->getIp();
273
-
274
-            // Origin is the supposed "client" IP.
275
-            $origin = $proxies[0];
276
-            $this->assign("forwardedOrigin", $origin);
277
-
278
-            // We step through the servers in reverse order, from closest to furthest
279
-            $proxies = array_reverse($proxies);
280
-
281
-            // By default, we have trust, because the first in the chain is now REMOTE_ADDR, which is hardest to spoof.
282
-            $trust = true;
283
-
284
-            /**
285
-             * @var int    $index     The zero-based index of the proxy.
286
-             * @var string $proxyData The proxy IP address (although possibly not!)
287
-             */
288
-            foreach ($proxies as $index => $proxyData) {
289
-                $proxyAddress = trim($proxyData);
290
-                $requestProxyData[$proxyIndex]['ip'] = $proxyAddress;
291
-
292
-                // get data on this IP.
293
-                $thisProxyIsTrusted = $this->getXffTrustProvider()->isTrusted($proxyAddress);
294
-
295
-                $proxyIsInPrivateRange = $this->getXffTrustProvider()
296
-                    ->ipInRange(self::$rfc1918ips, $proxyAddress);
297
-
298
-                if (!$proxyIsInPrivateRange) {
299
-                    $proxyReverseDns = $this->getRdnsProvider()->getReverseDNS($proxyAddress);
300
-                    $proxyLocation = $this->getLocationProvider()->getIpLocation($proxyAddress);
301
-                }
302
-                else {
303
-                    // this is going to fail, so why bother trying?
304
-                    $proxyReverseDns = false;
305
-                    $proxyLocation = false;
306
-                }
307
-
308
-                // current trust chain status BEFORE this link
309
-                $preLinkTrust = $trust;
310
-
311
-                // is *this* link trusted? Note, this will be true even if there is an untrusted link before this!
312
-                $requestProxyData[$proxyIndex]['trustedlink'] = $thisProxyIsTrusted;
313
-
314
-                // set the trust status of the chain to this point
315
-                $trust = $trust & $thisProxyIsTrusted;
316
-
317
-                // If this is the origin address, and the chain was trusted before this point, then we can trust
318
-                // the origin.
319
-                if ($preLinkTrust && $proxyAddress == $origin) {
320
-                    // if this is the origin, then we are at the last point in the chain.
321
-                    // @todo: this is probably the cause of some bugs when an IP appears twice - we're missing a check
322
-                    // to see if this is *really* the last in the chain, rather than just the same IP as it.
323
-                    $trust = true;
324
-                }
325
-
326
-                $requestProxyData[$proxyIndex]['trust'] = $trust;
327
-
328
-                $requestProxyData[$proxyIndex]['rdnsfailed'] = $proxyReverseDns === false;
329
-                $requestProxyData[$proxyIndex]['rdns'] = $proxyReverseDns;
330
-                $requestProxyData[$proxyIndex]['routable'] = !$proxyIsInPrivateRange;
331
-
332
-                $requestProxyData[$proxyIndex]['location'] = $proxyLocation;
333
-
334
-                if ($proxyReverseDns === $proxyAddress && $proxyIsInPrivateRange === false) {
335
-                    $requestProxyData[$proxyIndex]['rdns'] = null;
336
-                }
337
-
338
-                $showLinks = (!$trust || $proxyAddress == $origin) && !$proxyIsInPrivateRange;
339
-                $requestProxyData[$proxyIndex]['showlinks'] = $showLinks;
340
-
341
-                $proxyIndex++;
342
-            }
343
-
344
-            $this->assign("requestProxyData", $requestProxyData);
345
-        }
346
-    }
27
+	/**
28
+	 * @var array Array of IP address classed as 'private' by RFC1918.
29
+	 */
30
+	protected static $rfc1918ips = array(
31
+		"10.0.0.0"    => "10.255.255.255",
32
+		"172.16.0.0"  => "172.31.255.255",
33
+		"192.168.0.0" => "192.168.255.255",
34
+		"169.254.0.0" => "169.254.255.255",
35
+		"127.0.0.0"   => "127.255.255.255",
36
+	);
37
+
38
+	/**
39
+	 * Gets a request object
40
+	 *
41
+	 * @param PdoDatabase $database  The database connection
42
+	 * @param int         $requestId The ID of the request to retrieve
43
+	 *
44
+	 * @return Request
45
+	 * @throws ApplicationLogicException
46
+	 */
47
+	protected function getRequest(PdoDatabase $database, $requestId)
48
+	{
49
+		if ($requestId === null) {
50
+			throw new ApplicationLogicException("No request specified");
51
+		}
52
+
53
+		$request = Request::getById($requestId, $database);
54
+		if ($request === false || !is_a($request, Request::class)) {
55
+			throw new ApplicationLogicException('Could not load the requested request!');
56
+		}
57
+
58
+		return $request;
59
+	}
60
+
61
+	/**
62
+	 * Returns a value stating whether the user is allowed to see private data or not
63
+	 *
64
+	 * @param Request $request
65
+	 * @param User    $currentUser
66
+	 *
67
+	 * @return bool
68
+	 * @category Security-Critical
69
+	 */
70
+	protected function isAllowedPrivateData(Request $request, User $currentUser)
71
+	{
72
+		// Test the main security barrier for private data access using SecurityManager
73
+		if ($this->barrierTest('alwaysSeePrivateData', $currentUser, 'RequestData')) {
74
+			// Tool admins/check-users can always see private data
75
+			return true;
76
+		}
77
+
78
+		// reserving user is allowed to see the data
79
+		if ($currentUser->getId() === $request->getReserved()
80
+			&& $request->getReserved() !== null
81
+			&& $this->barrierTest('seePrivateDataWhenReserved', $currentUser, 'RequestData')
82
+		) {
83
+			return true;
84
+		}
85
+
86
+		// user has the reveal hash
87
+		if (WebRequest::getString('hash') === $request->getRevealHash()
88
+			&& $this->barrierTest('seePrivateDataWithHash', $currentUser, 'RequestData')
89
+		) {
90
+			return true;
91
+		}
92
+
93
+		// nope. Not allowed.
94
+		return false;
95
+	}
96
+
97
+	/**
98
+	 * Tests the security barrier for a specified action.
99
+	 *
100
+	 * Don't use within templates
101
+	 *
102
+	 * @param string      $action
103
+	 *
104
+	 * @param User        $user
105
+	 * @param null|string $pageName
106
+	 *
107
+	 * @return bool
108
+	 * @category Security-Critical
109
+	 */
110
+	abstract protected function barrierTest($action, User $user, $pageName = null);
111
+
112
+	/**
113
+	 * Gets the name of the route that has been passed from the request router.
114
+	 * @return string
115
+	 */
116
+	abstract protected function getRouteName();
117
+
118
+	/** @return SecurityManager */
119
+	abstract protected function getSecurityManager();
120
+
121
+	/**
122
+	 * Sets the name of the template this page should display.
123
+	 *
124
+	 * @param string $name
125
+	 */
126
+	abstract protected function setTemplate($name);
127
+
128
+	/** @return IXffTrustProvider */
129
+	abstract protected function getXffTrustProvider();
130
+
131
+	/** @return ILocationProvider */
132
+	abstract protected function getLocationProvider();
133
+
134
+	/** @return IRDnsProvider */
135
+	abstract protected function getRdnsProvider();
136
+
137
+	/**
138
+	 * Assigns a Smarty variable
139
+	 *
140
+	 * @param  array|string $name  the template variable name(s)
141
+	 * @param  mixed        $value the value to assign
142
+	 */
143
+	abstract protected function assign($name, $value);
144
+
145
+	/**
146
+	 * @param int         $requestReservationId
147
+	 * @param PdoDatabase $database
148
+	 * @param User        $currentUser
149
+	 */
150
+	protected function setupReservationDetails($requestReservationId, PdoDatabase $database, User $currentUser)
151
+	{
152
+		$requestIsReserved = $requestReservationId !== null;
153
+		$this->assign('requestIsReserved', $requestIsReserved);
154
+		$this->assign('requestIsReservedByMe', false);
155
+
156
+		if ($requestIsReserved) {
157
+			$this->assign('requestReservedByName', User::getById($requestReservationId, $database)->getUsername());
158
+			$this->assign('requestReservedById', $requestReservationId);
159
+
160
+			if ($requestReservationId === $currentUser->getId()) {
161
+				$this->assign('requestIsReservedByMe', true);
162
+			}
163
+		}
164
+
165
+		$this->assign('canBreakReservation', $this->barrierTest('force', $currentUser, PageBreakReservation::class));
166
+	}
167
+
168
+	/**
169
+	 * Adds private request data to Smarty. DO NOT USE WITHOUT FIRST CHECKING THAT THE USER IS AUTHORISED!
170
+	 *
171
+	 * @param Request           $request
172
+	 * @param User              $currentUser
173
+	 * @param SiteConfiguration $configuration
174
+	 *
175
+	 * @param PdoDatabase       $database
176
+	 */
177
+	protected function setupPrivateData(
178
+		$request,
179
+		User $currentUser,
180
+		SiteConfiguration $configuration,
181
+		PdoDatabase $database
182
+	) {
183
+		$xffProvider = $this->getXffTrustProvider();
184
+
185
+		$relatedEmailRequests = RequestSearchHelper::get($database)
186
+			->byEmailAddress($request->getEmail())
187
+			->withConfirmedEmail()
188
+			->excludingPurgedData($configuration)
189
+			->excludingRequest($request->getId())
190
+			->fetch();
191
+
192
+		$this->assign('requestEmail', $request->getEmail());
193
+		$emailDomain = explode("@", $request->getEmail())[1];
194
+		$this->assign("emailurl", $emailDomain);
195
+		$this->assign('requestRelatedEmailRequestsCount', count($relatedEmailRequests));
196
+		$this->assign('requestRelatedEmailRequests', $relatedEmailRequests);
197
+
198
+		$trustedIp = $xffProvider->getTrustedClientIp($request->getIp(), $request->getForwardedIp());
199
+		$this->assign('requestTrustedIp', $trustedIp);
200
+		$this->assign('requestRealIp', $request->getIp());
201
+		$this->assign('requestForwardedIp', $request->getForwardedIp());
202
+
203
+		$trustedIpLocation = $this->getLocationProvider()->getIpLocation($trustedIp);
204
+		$this->assign('requestTrustedIpLocation', $trustedIpLocation);
205
+
206
+		$this->assign('requestHasForwardedIp', $request->getForwardedIp() !== null);
207
+
208
+		$relatedIpRequests = RequestSearchHelper::get($database)
209
+			->byIp($trustedIp)
210
+			->withConfirmedEmail()
211
+			->excludingPurgedData($configuration)
212
+			->excludingRequest($request->getId())
213
+			->fetch();
214
+
215
+		$this->assign('requestRelatedIpRequestsCount', count($relatedIpRequests));
216
+		$this->assign('requestRelatedIpRequests', $relatedIpRequests);
217
+
218
+		$this->assign('showRevealLink', false);
219
+		if ($request->getReserved() === $currentUser->getId() ||
220
+			$this->barrierTest('alwaysSeeHash', $currentUser, 'RequestData')
221
+		) {
222
+			$this->assign('showRevealLink', true);
223
+			$this->assign('revealHash', $request->getRevealHash());
224
+		}
225
+
226
+		$this->setupForwardedIpData($request);
227
+	}
228
+
229
+	/**
230
+	 * Adds checkuser request data to Smarty. DO NOT USE WITHOUT FIRST CHECKING THAT THE USER IS AUTHORISED!
231
+	 *
232
+	 * @param Request $request
233
+	 */
234
+	protected function setupCheckUserData(Request $request)
235
+	{
236
+		$this->assign('requestUserAgent', $request->getUserAgent());
237
+	}
238
+
239
+	/**
240
+	 * Sets up the basic data for this request, and adds it to Smarty
241
+	 *
242
+	 * @param Request           $request
243
+	 * @param SiteConfiguration $config
244
+	 */
245
+	protected function setupBasicData(Request $request, SiteConfiguration $config)
246
+	{
247
+		$this->assign('requestId', $request->getId());
248
+		$this->assign('updateVersion', $request->getUpdateVersion());
249
+		$this->assign('requestName', $request->getName());
250
+		$this->assign('requestDate', $request->getDate());
251
+		$this->assign('requestStatus', $request->getStatus());
252
+
253
+		$isClosed = !array_key_exists($request->getStatus(), $config->getRequestStates())
254
+			&& $request->getStatus() !== RequestStatus::HOSPITAL;
255
+		$this->assign('requestIsClosed', $isClosed);
256
+	}
257
+
258
+	/**
259
+	 * Sets up the forwarded IP data for this request and adds it to Smarty
260
+	 *
261
+	 * @param Request $request
262
+	 */
263
+	protected function setupForwardedIpData(Request $request)
264
+	{
265
+		if ($request->getForwardedIp() !== null) {
266
+			$requestProxyData = array(); // Initialize array to store data to be output in Smarty template.
267
+			$proxyIndex = 0;
268
+
269
+			// Assuming [client] <=> [proxy1] <=> [proxy2] <=> [proxy3] <=> [us], we will see an XFF header of [client],
270
+			// [proxy1], [proxy2], and our actual IP will be [proxy3]
271
+			$proxies = explode(",", $request->getForwardedIp());
272
+			$proxies[] = $request->getIp();
273
+
274
+			// Origin is the supposed "client" IP.
275
+			$origin = $proxies[0];
276
+			$this->assign("forwardedOrigin", $origin);
277
+
278
+			// We step through the servers in reverse order, from closest to furthest
279
+			$proxies = array_reverse($proxies);
280
+
281
+			// By default, we have trust, because the first in the chain is now REMOTE_ADDR, which is hardest to spoof.
282
+			$trust = true;
283
+
284
+			/**
285
+			 * @var int    $index     The zero-based index of the proxy.
286
+			 * @var string $proxyData The proxy IP address (although possibly not!)
287
+			 */
288
+			foreach ($proxies as $index => $proxyData) {
289
+				$proxyAddress = trim($proxyData);
290
+				$requestProxyData[$proxyIndex]['ip'] = $proxyAddress;
291
+
292
+				// get data on this IP.
293
+				$thisProxyIsTrusted = $this->getXffTrustProvider()->isTrusted($proxyAddress);
294
+
295
+				$proxyIsInPrivateRange = $this->getXffTrustProvider()
296
+					->ipInRange(self::$rfc1918ips, $proxyAddress);
297
+
298
+				if (!$proxyIsInPrivateRange) {
299
+					$proxyReverseDns = $this->getRdnsProvider()->getReverseDNS($proxyAddress);
300
+					$proxyLocation = $this->getLocationProvider()->getIpLocation($proxyAddress);
301
+				}
302
+				else {
303
+					// this is going to fail, so why bother trying?
304
+					$proxyReverseDns = false;
305
+					$proxyLocation = false;
306
+				}
307
+
308
+				// current trust chain status BEFORE this link
309
+				$preLinkTrust = $trust;
310
+
311
+				// is *this* link trusted? Note, this will be true even if there is an untrusted link before this!
312
+				$requestProxyData[$proxyIndex]['trustedlink'] = $thisProxyIsTrusted;
313
+
314
+				// set the trust status of the chain to this point
315
+				$trust = $trust & $thisProxyIsTrusted;
316
+
317
+				// If this is the origin address, and the chain was trusted before this point, then we can trust
318
+				// the origin.
319
+				if ($preLinkTrust && $proxyAddress == $origin) {
320
+					// if this is the origin, then we are at the last point in the chain.
321
+					// @todo: this is probably the cause of some bugs when an IP appears twice - we're missing a check
322
+					// to see if this is *really* the last in the chain, rather than just the same IP as it.
323
+					$trust = true;
324
+				}
325
+
326
+				$requestProxyData[$proxyIndex]['trust'] = $trust;
327
+
328
+				$requestProxyData[$proxyIndex]['rdnsfailed'] = $proxyReverseDns === false;
329
+				$requestProxyData[$proxyIndex]['rdns'] = $proxyReverseDns;
330
+				$requestProxyData[$proxyIndex]['routable'] = !$proxyIsInPrivateRange;
331
+
332
+				$requestProxyData[$proxyIndex]['location'] = $proxyLocation;
333
+
334
+				if ($proxyReverseDns === $proxyAddress && $proxyIsInPrivateRange === false) {
335
+					$requestProxyData[$proxyIndex]['rdns'] = null;
336
+				}
337
+
338
+				$showLinks = (!$trust || $proxyAddress == $origin) && !$proxyIsInPrivateRange;
339
+				$requestProxyData[$proxyIndex]['showlinks'] = $showLinks;
340
+
341
+				$proxyIndex++;
342
+			}
343
+
344
+			$this->assign("requestProxyData", $requestProxyData);
345
+		}
346
+	}
347 347
 }
Please login to merge, or discard this patch.
includes/Router/OAuthRequestRouter.php 1 patch
Indentation   +5 added lines, -5 removed lines patch added patch discarded remove patch
@@ -17,9 +17,9 @@
 block discarded – undo
17 17
  */
18 18
 class OAuthRequestRouter extends RequestRouter
19 19
 {
20
-    protected function getRouteFromPath($pathInfo)
21
-    {
22
-        // Hardcode the route for this entry point
23
-        return array(PageOAuthCallback::class, 'authorise');
24
-    }
20
+	protected function getRouteFromPath($pathInfo)
21
+	{
22
+		// Hardcode the route for this entry point
23
+		return array(PageOAuthCallback::class, 'authorise');
24
+	}
25 25
 }
26 26
\ No newline at end of file
Please login to merge, or discard this patch.
includes/Tasks/PagedInternalPageBase.php 3 patches
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -102,7 +102,7 @@
 block discarded – undo
102 102
         $formParameters['limit'] = $this->limit;
103 103
         $this->assign('searchParamsUrl', http_build_query($formParameters, '', '&amp;'));
104 104
 
105
-        foreach ($formParameters as $key => $value)  {
105
+        foreach ($formParameters as $key => $value) {
106 106
             $this->assign($key, $value);
107 107
         }
108 108
     }
Please login to merge, or discard this patch.
Braces   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -102,7 +102,7 @@
 block discarded – undo
102 102
         $formParameters['limit'] = $this->limit;
103 103
         $this->assign('searchParamsUrl', http_build_query($formParameters, '', '&amp;'));
104 104
 
105
-        foreach ($formParameters as $key => $value)  {
105
+        foreach ($formParameters as $key => $value) {
106 106
             $this->assign($key, $value);
107 107
         }
108 108
     }
Please login to merge, or discard this patch.
Indentation   +93 added lines, -93 removed lines patch added patch discarded remove patch
@@ -13,97 +13,97 @@
 block discarded – undo
13 13
 
14 14
 abstract class PagedInternalPageBase extends InternalPageBase
15 15
 {
16
-    /** @var SearchHelperBase */
17
-    private $searchHelper;
18
-    private $page;
19
-    private $limit;
20
-
21
-    /**
22
-     * Sets up the pager with the current page, current limit, and total number of records.
23
-     *
24
-     * @param int   $count
25
-     * @param array $formParameters
26
-     */
27
-    protected function setupPageData($count, $formParameters)
28
-    {
29
-        $page = $this->page;
30
-        $limit = $this->limit;
31
-
32
-        // The number of pages on the pager to show. Must be odd
33
-        $pageLimit = 9;
34
-
35
-        $pageData = array(
36
-            // Can the user go to the previous page?
37
-            'canprev'   => $page != 1,
38
-            // Can the user go to the next page?
39
-            'cannext'   => ($page * $limit) < $count,
40
-            // Maximum page number
41
-            'maxpage'   => max(1, ceil($count / $limit)),
42
-            // Limit to the number of pages to display
43
-            'pagelimit' => $pageLimit,
44
-        );
45
-
46
-        // number of pages either side of the current to show
47
-        $pageMargin = (($pageLimit - 1) / 2);
48
-
49
-        // Calculate the number of pages either side to show - this is for situations like:
50
-        //  [1]  [2] [[3]] [4]  [5]  [6]  [7]  [8]  [9] - where you can't just use the page margin calculated
51
-        $pageData['lowpage'] = max(1, $page - $pageMargin);
52
-        $pageData['hipage'] = min($pageData['maxpage'], $page + $pageMargin);
53
-        $pageCount = ($pageData['hipage'] - $pageData['lowpage']) + 1;
54
-
55
-        if ($pageCount < $pageLimit) {
56
-            if ($pageData['lowpage'] == 1 && $pageData['hipage'] < $pageData['maxpage']) {
57
-                $pageData['hipage'] = min($pageLimit, $pageData['maxpage']);
58
-            }
59
-            elseif ($pageData['lowpage'] > 1 && $pageData['hipage'] == $pageData['maxpage']) {
60
-                $pageData['lowpage'] = max(1, $pageData['maxpage'] - $pageLimit + 1);
61
-            }
62
-        }
63
-
64
-        // Put the range of pages into the page data
65
-        $pageData['pages'] = range($pageData['lowpage'], $pageData['hipage']);
66
-
67
-        $this->assign("pagedata", $pageData);
68
-
69
-        $this->assign("limit", $limit);
70
-        $this->assign("page", $page);
71
-
72
-        $this->setupFormParameters($formParameters);
73
-    }
74
-
75
-    protected function setSearchHelper(SearchHelperBase $searchHelper)
76
-    {
77
-        $this->searchHelper = $searchHelper;
78
-    }
79
-
80
-    protected function setupLimits()
81
-    {
82
-        $limit = WebRequest::getInt('limit');
83
-        if ($limit === null) {
84
-            $limit = 100;
85
-        }
86
-
87
-        $page = WebRequest::getInt('page');
88
-        if ($page === null) {
89
-            $page = 1;
90
-        }
91
-
92
-        $offset = ($page - 1) * $limit;
93
-
94
-        $this->searchHelper->limit($limit, $offset);
95
-
96
-        $this->page = $page;
97
-        $this->limit = $limit;
98
-    }
99
-
100
-    private function setupFormParameters($formParameters)
101
-    {
102
-        $formParameters['limit'] = $this->limit;
103
-        $this->assign('searchParamsUrl', http_build_query($formParameters, '', '&amp;'));
104
-
105
-        foreach ($formParameters as $key => $value)  {
106
-            $this->assign($key, $value);
107
-        }
108
-    }
16
+	/** @var SearchHelperBase */
17
+	private $searchHelper;
18
+	private $page;
19
+	private $limit;
20
+
21
+	/**
22
+	 * Sets up the pager with the current page, current limit, and total number of records.
23
+	 *
24
+	 * @param int   $count
25
+	 * @param array $formParameters
26
+	 */
27
+	protected function setupPageData($count, $formParameters)
28
+	{
29
+		$page = $this->page;
30
+		$limit = $this->limit;
31
+
32
+		// The number of pages on the pager to show. Must be odd
33
+		$pageLimit = 9;
34
+
35
+		$pageData = array(
36
+			// Can the user go to the previous page?
37
+			'canprev'   => $page != 1,
38
+			// Can the user go to the next page?
39
+			'cannext'   => ($page * $limit) < $count,
40
+			// Maximum page number
41
+			'maxpage'   => max(1, ceil($count / $limit)),
42
+			// Limit to the number of pages to display
43
+			'pagelimit' => $pageLimit,
44
+		);
45
+
46
+		// number of pages either side of the current to show
47
+		$pageMargin = (($pageLimit - 1) / 2);
48
+
49
+		// Calculate the number of pages either side to show - this is for situations like:
50
+		//  [1]  [2] [[3]] [4]  [5]  [6]  [7]  [8]  [9] - where you can't just use the page margin calculated
51
+		$pageData['lowpage'] = max(1, $page - $pageMargin);
52
+		$pageData['hipage'] = min($pageData['maxpage'], $page + $pageMargin);
53
+		$pageCount = ($pageData['hipage'] - $pageData['lowpage']) + 1;
54
+
55
+		if ($pageCount < $pageLimit) {
56
+			if ($pageData['lowpage'] == 1 && $pageData['hipage'] < $pageData['maxpage']) {
57
+				$pageData['hipage'] = min($pageLimit, $pageData['maxpage']);
58
+			}
59
+			elseif ($pageData['lowpage'] > 1 && $pageData['hipage'] == $pageData['maxpage']) {
60
+				$pageData['lowpage'] = max(1, $pageData['maxpage'] - $pageLimit + 1);
61
+			}
62
+		}
63
+
64
+		// Put the range of pages into the page data
65
+		$pageData['pages'] = range($pageData['lowpage'], $pageData['hipage']);
66
+
67
+		$this->assign("pagedata", $pageData);
68
+
69
+		$this->assign("limit", $limit);
70
+		$this->assign("page", $page);
71
+
72
+		$this->setupFormParameters($formParameters);
73
+	}
74
+
75
+	protected function setSearchHelper(SearchHelperBase $searchHelper)
76
+	{
77
+		$this->searchHelper = $searchHelper;
78
+	}
79
+
80
+	protected function setupLimits()
81
+	{
82
+		$limit = WebRequest::getInt('limit');
83
+		if ($limit === null) {
84
+			$limit = 100;
85
+		}
86
+
87
+		$page = WebRequest::getInt('page');
88
+		if ($page === null) {
89
+			$page = 1;
90
+		}
91
+
92
+		$offset = ($page - 1) * $limit;
93
+
94
+		$this->searchHelper->limit($limit, $offset);
95
+
96
+		$this->page = $page;
97
+		$this->limit = $limit;
98
+	}
99
+
100
+	private function setupFormParameters($formParameters)
101
+	{
102
+		$formParameters['limit'] = $this->limit;
103
+		$this->assign('searchParamsUrl', http_build_query($formParameters, '', '&amp;'));
104
+
105
+		foreach ($formParameters as $key => $value)  {
106
+			$this->assign($key, $value);
107
+		}
108
+	}
109 109
 }
Please login to merge, or discard this patch.