Commit | Line | Data |
---|---|---|
11ade432 AE |
1 | <?php |
2 | namespace wcf\system\application; | |
bc62b9c1 | 3 | use wcf\data\application\Application; |
f1c1fc65 AE |
4 | use wcf\data\application\ApplicationAction; |
5 | use wcf\data\application\ApplicationList; | |
b401cd0d | 6 | use wcf\system\cache\builder\ApplicationCacheBuilder; |
dccac621 | 7 | use wcf\system\Regex; |
11ade432 AE |
8 | use wcf\system\SingletonFactory; |
9 | ||
10 | /** | |
11 | * Handles multi-application environments. | |
12 | * | |
13 | * @author Alexander Ebert | |
2b6cb5c2 | 14 | * @copyright 2001-2015 WoltLab GmbH |
11ade432 AE |
15 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> |
16 | * @package com.woltlab.wcf | |
17 | * @subpackage system.application | |
9f959ced | 18 | * @category Community Framework |
11ade432 AE |
19 | */ |
20 | class ApplicationHandler extends SingletonFactory { | |
21 | /** | |
22 | * application cache | |
39abe192 | 23 | * @var Application[] |
d726f13d | 24 | */ |
39abe192 | 25 | protected $cache; |
11ade432 | 26 | |
6d5cc010 AE |
27 | /** |
28 | * list of page URLs | |
39abe192 | 29 | * @var string[] |
6d5cc010 | 30 | */ |
39abe192 | 31 | protected $pageURLs = []; |
6d5cc010 | 32 | |
11ade432 AE |
33 | /** |
34 | * Initializes cache. | |
35 | */ | |
36 | protected function init() { | |
f341086b | 37 | $this->cache = ApplicationCacheBuilder::getInstance()->getData(); |
11ade432 AE |
38 | } |
39 | ||
11ade432 AE |
40 | /** |
41 | * Returns an application based upon it's abbreviation. Will return the | |
42 | * primary application if $abbreviation equals to 'wcf' | |
43 | * | |
e3530f10 AE |
44 | * @param string $abbreviation package abbreviation, e.g. `wbb` for `com.woltlab.wbb` |
45 | * @return Application | |
a17de04e | 46 | */ |
11ade432 | 47 | public function getApplication($abbreviation) { |
11ade432 AE |
48 | if (isset($this->cache['abbreviation'][$abbreviation])) { |
49 | $packageID = $this->cache['abbreviation'][$abbreviation]; | |
50 | ||
51 | if (isset($this->cache['application'][$packageID])) { | |
52 | return $this->cache['application'][$packageID]; | |
53 | } | |
54 | } | |
55 | ||
56 | return null; | |
57 | } | |
58 | ||
02c1a7fe AE |
59 | /** |
60 | * Returns an application by package id. | |
61 | * | |
62 | * @param integer $packageID package id | |
63 | * @return Application application object | |
64 | */ | |
65 | public function getApplicationByID($packageID) { | |
66 | if (isset($this->cache['application'][$packageID])) { | |
67 | return $this->cache['application'][$packageID]; | |
68 | } | |
69 | ||
70 | return null; | |
71 | } | |
72 | ||
11ade432 AE |
73 | /** |
74 | * Returns pseudo-application representing WCF used for special cases, | |
75 | * e.g. cross-domain files requestable through the webserver. | |
76 | * | |
e3530f10 | 77 | * @return Application |
39abe192 | 78 | * @deprecated 2.2 please use `getApplication()` instead |
11ade432 AE |
79 | */ |
80 | public function getWCF() { | |
39abe192 | 81 | return $this->getApplicationByID(1); |
11ade432 AE |
82 | } |
83 | ||
84 | /** | |
85 | * Returns the currently active application. | |
86 | * | |
e3530f10 | 87 | * @return Application |
a17de04e | 88 | */ |
11ade432 | 89 | public function getActiveApplication() { |
2dc89c0a AE |
90 | // work-around during WCFSetup |
91 | if (isset($this->cache['application'][PACKAGE_ID])) { | |
92 | return $this->cache['application'][PACKAGE_ID]; | |
93 | } | |
94 | ||
95 | return $this->getWCF(); | |
11ade432 AE |
96 | } |
97 | ||
98 | /** | |
99 | * Returns a list of dependent applications. | |
100 | * | |
e3530f10 | 101 | * @return Application[] |
a17de04e | 102 | */ |
11ade432 | 103 | public function getDependentApplications() { |
5e92c58f AE |
104 | $applications = $this->getApplications(); |
105 | foreach ($applications as $key => $application) { | |
106 | if ($application->packageID == $this->getActiveApplication()->packageID) { | |
107 | unset($applications[$key]); | |
108 | break; | |
109 | } | |
11ade432 AE |
110 | } |
111 | ||
112 | return $applications; | |
113 | } | |
114 | ||
ebbfe043 AE |
115 | /** |
116 | * Returns a list of all active applications. | |
117 | * | |
e3530f10 | 118 | * @return Application[] |
ebbfe043 AE |
119 | */ |
120 | public function getApplications() { | |
5e92c58f | 121 | return $this->cache['application']; |
ebbfe043 AE |
122 | } |
123 | ||
11ade432 AE |
124 | /** |
125 | * Returns abbreviation for a given package id or null if application is unknown. | |
126 | * | |
e3530f10 | 127 | * @param int $packageID unique package id |
11ade432 AE |
128 | * @return string |
129 | */ | |
130 | public function getAbbreviation($packageID) { | |
131 | foreach ($this->cache['abbreviation'] as $abbreviation => $applicationID) { | |
132 | if ($packageID == $applicationID) { | |
133 | return $abbreviation; | |
134 | } | |
135 | } | |
136 | ||
137 | return null; | |
138 | } | |
6d5cc010 AE |
139 | |
140 | /** | |
28410a97 | 141 | * Returns true if given $url is an internal URL. |
6d5cc010 AE |
142 | * |
143 | * @param string $url | |
144 | * @return boolean | |
145 | */ | |
146 | public function isInternalURL($url) { | |
dccac621 | 147 | $protocolRegex = new Regex('^https(?=://)'); |
6d5cc010 AE |
148 | if (empty($this->pageURLs)) { |
149 | foreach ($this->getApplications() as $application) { | |
f7518cc4 | 150 | $this->pageURLs[] = preg_replace('~/$~', '', $protocolRegex->replace($application->getPageURL(), 'http')); |
6d5cc010 | 151 | } |
6d5cc010 AE |
152 | } |
153 | ||
154 | foreach ($this->pageURLs as $pageURL) { | |
dccac621 | 155 | if (stripos($protocolRegex->replace($url, 'http'), $pageURL) === 0) { |
6d5cc010 AE |
156 | return true; |
157 | } | |
158 | } | |
159 | ||
196ea246 AE |
160 | // relative urls contain no protocol, including implied |
161 | if (!preg_match('~^([a-z]+)?://~', $url)) { | |
162 | return true; | |
163 | } | |
164 | ||
6d5cc010 AE |
165 | return false; |
166 | } | |
f1c1fc65 AE |
167 | |
168 | /** | |
169 | * Rebuilds cookie domain/path for all applications. | |
170 | */ | |
171 | public static function rebuild() { | |
172 | $applicationList = new ApplicationList(); | |
f1c1fc65 AE |
173 | $applicationList->readObjects(); |
174 | ||
175 | $applicationAction = new ApplicationAction($applicationList->getObjects(), 'rebuild'); | |
176 | $applicationAction->executeAction(); | |
177 | } | |
11ade432 | 178 | } |