Completed
Push — search ( 076ba7...2a5267 )
by Simon
16:36 queued 11:48
created

WebStart::isPublic()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
/******************************************************************************
3
 * Wikipedia Account Creation Assistance tool                                 *
4
 *                                                                            *
5
 * All code in this file is released into the public domain by the ACC        *
6
 * Development Team. Please see team.json for a list of contributors.         *
7
 ******************************************************************************/
8
9
namespace Waca;
10
11
use Waca\DataObjects\User;
12
use Waca\Exceptions\EnvironmentException;
13
use Waca\Exceptions\ReadableException;
14
use Waca\Helpers\BlacklistHelper;
15
use Waca\Helpers\FakeBlacklistHelper;
16
use Waca\Helpers\TypeAheadHelper;
17
use Waca\Providers\GlobalState\GlobalStateProvider;
18
use Waca\Router\IRequestRouter;
19
use Waca\Security\ContentSecurityPolicyManager;
20
use Waca\Security\RoleConfiguration;
21
use Waca\Security\SecurityManager;
22
use Waca\Security\TokenManager;
23
use Waca\Tasks\ITask;
24
use Waca\Tasks\InternalPageBase;
25
use Waca\Tasks\PageBase;
26
27
/**
28
 * Application entry point.
29
 *
30
 * @package Waca
31
 */
32
class WebStart extends ApplicationBase
33
{
34
    /**
35
     * @var IRequestRouter $requestRouter The request router to use. Note that different entry points have different
36
     *                                    routers and hence different URL mappings
37
     */
38
    private $requestRouter;
39
    /**
40
     * @var bool $isPublic Determines whether to use public interface objects or internal interface objects
41
     */
42
    private $isPublic = false;
43
44
    /**
45
     * WebStart constructor.
46
     *
47
     * @param SiteConfiguration $configuration The site configuration
48
     * @param IRequestRouter    $router        The request router to use
49
     */
50 2
    public function __construct(SiteConfiguration $configuration, IRequestRouter $router)
0 ignored issues
show
Bug introduced by
The type Waca\SiteConfiguration was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
51
    {
52 2
        parent::__construct($configuration);
53
54 2
        $this->requestRouter = $router;
55 2
    }
56
57
    /**
58
     * @param ITask             $page
59
     * @param SiteConfiguration $siteConfiguration
60
     * @param PdoDatabase       $database
61
     * @param PdoDatabase       $notificationsDatabase
62
     *
63
     * @return void
64
     */
65
    protected function setupHelpers(
66
        ITask $page,
67
        SiteConfiguration $siteConfiguration,
68
        PdoDatabase $database,
69
        PdoDatabase $notificationsDatabase = null
70
    ) {
71
        parent::setupHelpers($page, $siteConfiguration, $database, $notificationsDatabase);
72
73
        if ($page instanceof PageBase) {
74
            $page->setTokenManager(new TokenManager());
75
            $page->setCspManager(new ContentSecurityPolicyManager($siteConfiguration));
76
77
            if ($page instanceof InternalPageBase) {
78
                $page->setTypeAheadHelper(new TypeAheadHelper());
79
80
                $identificationVerifier = new IdentificationVerifier($page->getHttpHelper(), $siteConfiguration,
81
                    $database);
82
                $page->setSecurityManager(new SecurityManager($identificationVerifier, new RoleConfiguration()));
83
84
                if ($siteConfiguration->getTitleBlacklistEnabled()) {
85
                    $page->setBlacklistHelper(new FakeBlacklistHelper());
86
                }
87
                else {
88
                    $page->setBlacklistHelper(new BlacklistHelper($page->getHttpHelper(),
89
                        $siteConfiguration->getMediawikiWebServiceEndpoint()));
90
                }
91
            }
92
        }
93
    }
94
95
    /**
96
     * Application entry point.
97
     *
98
     * Sets up the environment and runs the application, performing any global cleanup operations when done.
99
     */
100
    public function run()
101
    {
102
        try {
103
            if ($this->setupEnvironment()) {
104
                $this->main();
105
            }
106
        }
107
        catch (EnvironmentException $ex) {
108
            ob_end_clean();
109
            print Offline::getOfflineMessage($this->isPublic(), $ex->getMessage());
110
        }
111
            /** @noinspection PhpRedundantCatchClauseInspection */
112
        catch (ReadableException $ex) {
113
            ob_end_clean();
114
            print $ex->getReadableError();
115
        }
116
        finally {
117
            $this->cleanupEnvironment();
118
        }
119
    }
120
121
    /**
122
     * Environment setup
123
     *
124
     * This method initialises the tool environment. If the tool cannot be initialised correctly, it will return false
125
     * and shut down prematurely.
126
     *
127
     * @return bool
128
     * @throws EnvironmentException
129
     */
130
    protected function setupEnvironment()
131
    {
132
        // initialise global exception handler
133
        set_exception_handler(array(ExceptionHandler::class, 'exceptionHandler'));
134
        set_error_handler(array(ExceptionHandler::class, 'errorHandler'), E_RECOVERABLE_ERROR);
135
136
        // start output buffering if necessary
137
        if (ob_get_level() === 0) {
138
            ob_start();
139
        }
140
141
        // initialise super-global providers
142
        WebRequest::setGlobalStateProvider(new GlobalStateProvider());
143
144
        if (Offline::isOffline()) {
145
            print Offline::getOfflineMessage($this->isPublic());
146
            ob_end_flush();
147
148
            return false;
149
        }
150
151
        // Call parent setup
152
        if (!parent::setupEnvironment()) {
153
            return false;
154
        }
155
156
        // Start up sessions
157
        Session::start();
158
159
        // Check the user is allowed to be logged in still. This must be before we call any user-loading functions and
160
        // get the current user cached.
161
        // I'm not sure if this function call being here is particularly a good thing, but it's part of starting up a
162
        // session I suppose.
163
        $this->checkForceLogout();
164
165
        // environment initialised!
166
        return true;
167
    }
168
169
    /**
170
     * Main application logic
171
     */
172
    protected function main()
173
    {
174
        // Get the right route for the request
175
        $page = $this->requestRouter->route();
176
177
        $siteConfiguration = $this->getConfiguration();
178
        $database = PdoDatabase::getDatabaseConnection('acc');
179
180
        if ($siteConfiguration->getIrcNotificationsEnabled()) {
181
            $notificationsDatabase = PdoDatabase::getDatabaseConnection('notifications');
182
        }
183
        else {
184
            // @todo federated table here?
185
            $notificationsDatabase = $database;
186
        }
187
188
        $this->setupHelpers($page, $siteConfiguration, $database, $notificationsDatabase);
189
190
        // run the route code for the request.
191
        $page->execute();
192
    }
193
194
    /**
195
     * Any cleanup tasks should go here
196
     *
197
     * Note that we need to be very careful here, as exceptions may have been thrown and handled.
198
     * This should *only* be for cleaning up, no logic should go here.
199
     */
200
    protected function cleanupEnvironment()
201
    {
202
        // Clean up anything we splurged after sending the page.
203
        if (ob_get_level() > 0) {
204
            for ($i = ob_get_level(); $i > 0; $i--) {
205
                ob_end_clean();
206
            }
207
        }
208
    }
209
210
    private function checkForceLogout()
211
    {
212
        $database = PdoDatabase::getDatabaseConnection('acc');
213
214
        $sessionUserId = WebRequest::getSessionUserId();
215
        iF ($sessionUserId === null) {
216
            return;
217
        }
218
219
        // Note, User::getCurrent() caches it's result, which we *really* don't want to trigger.
220
        $currentUser = User::getById($sessionUserId, $database);
221
222
        if ($currentUser === false) {
0 ignored issues
show
introduced by
The condition $currentUser === false is always false.
Loading history...
223
            // Umm... this user has a session cookie with a userId set, but no user exists...
224
            Session::restart();
225
226
            $currentUser = User::getCurrent($database);
227
        }
228
229
        if ($currentUser->getForceLogout()) {
230
            Session::restart();
231
232
            $currentUser->setForceLogout(false);
233
            $currentUser->save();
234
        }
235
    }
236
237 1
    public function isPublic()
238
    {
239 1
        return $this->isPublic;
240
    }
241
242 1
    public function setPublic($isPublic)
243
    {
244 1
        $this->isPublic = $isPublic;
245 1
    }
246
}
247