3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
22 * GadgetContext contains all contextual variables and classes that are relevant for this request,
23 * such as url, httpFetcher, feature registry, etc.
24 * Server wide variables are stored in config.php
27 const DEFAULT_VIEW
= 'profile';
28 protected $httpFetcher = null;
29 protected $locale = null;
30 protected $renderingContext = null;
31 protected $registry = null;
32 protected $view = null;
33 protected $moduleId = null;
34 protected $url = null;
35 protected $cache = null;
36 protected $blacklist = null;
37 protected $ignoreCache = null;
38 protected $forcedJsLibs = null;
39 protected $containerConfig = null;
40 protected $container = null;
41 protected $refreshInterval;
43 public function __construct($renderingContext) {
44 // Rendering context is set by the calling event handler (either GADGET or CONTAINER)
45 $this->setRenderingContext($renderingContext);
48 $this->setIgnoreCache($this->getIgnoreCacheParam());
49 $this->setForcedJsLibs($this->getFocedJsLibsParam());
50 $this->setUrl($this->getUrlParam());
51 $this->setModuleId($this->getModuleIdParam());
52 $this->setView($this->getViewParam());
53 $this->setContainer($this->getContainerParam());
54 $this->setRefreshInterval($this->getRefreshIntervalParam());
55 //NOTE All classes are initialized when called (aka lazy loading) because we don't need all of them in every situation
58 private function getRefreshIntervalParam() {
59 return isset($_GET['refresh']) ?
$_GET['refresh'] : Config
::get('default_refresh_interval');
62 private function getContainerParam() {
63 $container = 'default';
64 if (! empty($_GET['container'])) {
65 $container = $_GET['container'];
66 } elseif (! empty($_POST['container'])) {
67 $container = $_POST['container'];
68 //FIXME The paramater used to be called 'synd' & is scheduled for removal
69 } elseif (! empty($_GET['synd'])) {
70 $container = $_GET['synd'];
71 } elseif (! empty($_POST['synd'])) {
72 $container = $_POST['synd'];
77 private function getIgnoreCacheParam() {
78 // Support both the old Orkut style &bpc and new standard style &nocache= params
79 return (isset($_GET['nocache']) && intval($_GET['nocache']) == 1) ||
(isset($_GET['bpc']) && intval($_GET['bpc']) == 1);
82 private function getFocedJsLibsParam() {
83 return isset($_GET['libs']) ?
trim($_GET['libs']) : null;
86 private function getUrlParam() {
87 if (! empty($_GET['url'])) {
89 } elseif (! empty($_POST['url'])) {
95 private function getModuleIdParam() {
96 return isset($_GET['mid']) && is_numeric($_GET['mid']) ?
intval($_GET['mid']) : 0;
99 private function getViewParam() {
100 return ! empty($_GET['view']) ?
$_GET['view'] : self
::DEFAULT_VIEW
;
103 private function instanceBlacklist() {
104 $blackListClass = Config
::get('blacklist_class');
105 if (! empty($blackListClass)) {
106 return new $blackListClass();
112 private function instanceHttpFetcher() {
113 $remoteContent = Config
::get('remote_content');
114 return new $remoteContent();
117 private function instanceRegistry() {
118 // feature parsing is very resource intensive so by caching the result this saves upto 30% of the processing time
119 $featureCache = Cache
::createCache(Config
::get('feature_cache'), 'FeatureCache');
120 if (! ($registry = $featureCache->get(md5(Config
::get('features_path'))))) {
121 $registry = new GadgetFeatureRegistry(Config
::get('features_path'));
122 $featureCache->set(md5(Config
::get('features_path')), $registry);
127 private function instanceLocale() {
128 // Get language and country params, try the GET params first, if their not set try the POST, else use 'all' as default
129 $language = ! empty($_GET['lang']) ?
$_GET['lang'] : (! empty($_POST['lang']) ?
$_POST['lang'] : 'all');
130 $country = ! empty($_GET['country']) ?
$_GET['country'] : (! empty($_POST['country']) ?
$_POST['country'] : 'all');
131 return array('lang' => strtolower($language), 'country' => strtoupper($country));
134 private function instanceContainerConfig() {
135 return new ContainerConfig(Config
::get('container_path'));
138 public function getContainer() {
139 return $this->container
;
142 public function getContainerConfig() {
143 if ($this->containerConfig
== null) {
144 $this->containerConfig
= $this->instanceContainerConfig();
146 return $this->containerConfig
;
149 public function getModuleId() {
150 return $this->moduleId
;
153 public function getRegistry() {
154 if ($this->registry
== null) {
155 $this->setRegistry($this->instanceRegistry());
157 return $this->registry
;
160 public function getUrl() {
164 public function getView() {
168 public function setRefreshInterval($interval) {
169 $this->refreshInterval
= $interval;
172 public function setContainer($container) {
173 $this->container
= $container;
176 public function setContainerConfig($containerConfig) {
177 $this->containerConfig
= $containerConfig;
180 public function setBlacklist($blacklist) {
181 $this->blacklist
= $blacklist;
184 public function setCache($cache) {
185 $this->cache
= $cache;
188 public function setHttpFetcher($httpFetcher) {
189 $this->httpFetcher
= $httpFetcher;
192 public function setLocale($locale) {
193 $this->locale
= $locale;
196 public function setModuleId($moduleId) {
197 $this->moduleId
= $moduleId;
200 public function setRegistry($registry) {
201 $this->registry
= $registry;
204 public function setRenderingContext($renderingContext) {
205 $this->renderingContext
= $renderingContext;
208 public function setUrl($url) {
212 public function setView($view) {
216 public function setIgnoreCache($ignoreCache) {
217 $this->ignoreCache
= $ignoreCache;
220 public function setForcedJsLibs($forcedJsLibs) {
221 $this->forcedJsLibs
= $forcedJsLibs;
224 public function getRefreshInterval() {
225 return $this->refreshInterval
;
228 public function getIgnoreCache() {
229 return $this->ignoreCache
;
232 public function getForcedJsLibs() {
233 return $this->forcedJsLibs
;
236 public function getBlacklist() {
237 if ($this->blacklist
== null) {
238 $this->setBlacklist($this->instanceBlacklist());
240 return $this->blacklist
;
243 public function getRenderingContext() {
244 return $this->renderingContext
;
247 public function getHttpFetcher() {
248 if ($this->httpFetcher
== null) {
249 $this->setHttpFetcher($this->instanceHttpFetcher());
251 return $this->httpFetcher
;
254 public function getLocale() {
255 if ($this->locale
== null) {
256 $this->setLocale($this->instanceLocale());
258 return $this->locale
;
262 * Extracts the 'st' token from the GET or POST params and calls the
263 * signer to validate the token
265 * @param GadgetSigner $signer the signer to use (configured in config.php)
266 * @return string the token to use in the signed url
268 public function extractAndValidateToken($signer) {
269 if ($signer == null) {
272 $token = isset($_GET["st"]) ?
$_GET["st"] : '';
273 if (! isset($token) ||
$token == '') {
274 $token = isset($_POST['st']) ?
$_POST['st'] : '';
276 if (count(explode(':', $token)) != 6) {
277 $token = urldecode(base64_decode($token));
280 throw new Exception("Missing or invalid security token");
282 return $signer->createToken($token);