SHINDIG-943 by Pan Jie - Add support invalidation for CacheApc/CacheFile/CacheMemcach...
[shindig.git] / php / src / gadgets / GadgetContext.php
blob5eca0a91556093014701fae7418ace9b15075b87
1 <?php
2 /**
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
18 * under the License.
24 * GadgetContext contains all contextual variables and classes that are relevant for this request,
25 * such as url, httpFetcher, feature registry, etc.
26 * Server wide variables are stored in config.php
28 class GadgetContext {
29 const DEFAULT_VIEW = 'profile';
30 protected $httpFetcher = null;
31 protected $locale = null;
32 protected $renderingContext = null;
33 protected $registry = null;
34 protected $view = null;
35 protected $moduleId = null;
36 protected $url = null;
37 protected $cache = null;
38 protected $blacklist = null;
39 protected $ignoreCache = null;
40 protected $forcedJsLibs = null;
41 protected $containerConfig = null;
42 protected $container = null;
43 protected $refreshInterval;
45 public function __construct($renderingContext) {
46 // Rendering context is set by the calling event handler (either GADGET or CONTAINER)
47 $this->setRenderingContext($renderingContext);
49 // Request variables
50 $this->setIgnoreCache($this->getIgnoreCacheParam());
51 $this->setForcedJsLibs($this->getFocedJsLibsParam());
52 $this->setUrl($this->getUrlParam());
53 $this->setModuleId($this->getModuleIdParam());
54 $this->setView($this->getViewParam());
55 $this->setContainer($this->getContainerParam());
56 $this->setRefreshInterval($this->getRefreshIntervalParam());
57 //NOTE All classes are initialized when called (aka lazy loading) because we don't need all of them in every situation
60 private function getRefreshIntervalParam() {
61 return isset($_GET['refresh']) ? $_GET['refresh'] : Config::get('default_refresh_interval');
64 private function getContainerParam() {
65 $container = 'default';
66 if (! empty($_GET['container'])) {
67 $container = $_GET['container'];
68 } elseif (! empty($_POST['container'])) {
69 $container = $_POST['container'];
70 //FIXME The paramater used to be called 'synd' & is scheduled for removal
71 } elseif (! empty($_GET['synd'])) {
72 $container = $_GET['synd'];
73 } elseif (! empty($_POST['synd'])) {
74 $container = $_POST['synd'];
76 return $container;
79 private function getIgnoreCacheParam() {
80 // Support both the old Orkut style &bpc and new standard style &nocache= params
81 return (isset($_GET['nocache']) && intval($_GET['nocache']) == 1) || (isset($_GET['bpc']) && intval($_GET['bpc']) == 1);
84 private function getFocedJsLibsParam() {
85 return isset($_GET['libs']) ? trim($_GET['libs']) : null;
88 private function getUrlParam() {
89 if (! empty($_GET['url'])) {
90 return $_GET['url'];
91 } elseif (! empty($_POST['url'])) {
92 return $_POST['url'];
94 return null;
97 private function getModuleIdParam() {
98 return isset($_GET['mid']) && is_numeric($_GET['mid']) ? intval($_GET['mid']) : 0;
101 private function getViewParam() {
102 return ! empty($_GET['view']) ? $_GET['view'] : self::DEFAULT_VIEW;
105 private function instanceBlacklist() {
106 $blackListClass = Config::get('blacklist_class');
107 if (! empty($blackListClass)) {
108 return new $blackListClass();
109 } else {
110 return null;
114 private function instanceHttpFetcher() {
115 $remoteContent = Config::get('remote_content');
116 return new $remoteContent();
119 private function instanceRegistry() {
120 // feature parsing is very resource intensive so by caching the result this saves upto 30% of the processing time
121 $featureCache = Cache::createCache(Config::get('feature_cache'), 'FeatureCache');
122 if (! ($registry = $featureCache->get(md5(Config::get('features_path'))))) {
123 $registry = new GadgetFeatureRegistry(Config::get('features_path'));
124 $featureCache->set(md5(Config::get('features_path')), $registry);
126 return $registry;
129 private function instanceLocale() {
130 // Get language and country params, try the GET params first, if their not set try the POST, else use 'all' as default
131 $language = ! empty($_GET['lang']) ? $_GET['lang'] : (! empty($_POST['lang']) ? $_POST['lang'] : 'all');
132 $country = ! empty($_GET['country']) ? $_GET['country'] : (! empty($_POST['country']) ? $_POST['country'] : 'all');
133 return array('lang' => strtolower($language), 'country' => strtoupper($country));
136 private function instanceContainerConfig() {
137 return new ContainerConfig(Config::get('container_path'));
140 public function getContainer() {
141 return $this->container;
144 public function getContainerConfig() {
145 if ($this->containerConfig == null) {
146 $this->containerConfig = $this->instanceContainerConfig();
148 return $this->containerConfig;
151 public function getModuleId() {
152 return $this->moduleId;
155 public function getRegistry() {
156 if ($this->registry == null) {
157 $this->setRegistry($this->instanceRegistry());
159 return $this->registry;
162 public function getUrl() {
163 return $this->url;
166 public function getView() {
167 return $this->view;
170 public function setRefreshInterval($interval) {
171 $this->refreshInterval = $interval;
174 public function setContainer($container) {
175 $this->container = $container;
178 public function setContainerConfig($containerConfig) {
179 $this->containerConfig = $containerConfig;
182 public function setBlacklist($blacklist) {
183 $this->blacklist = $blacklist;
186 public function setCache($cache) {
187 $this->cache = $cache;
190 public function setHttpFetcher($httpFetcher) {
191 $this->httpFetcher = $httpFetcher;
194 public function setLocale($locale) {
195 $this->locale = $locale;
198 public function setModuleId($moduleId) {
199 $this->moduleId = $moduleId;
202 public function setRegistry($registry) {
203 $this->registry = $registry;
206 public function setRenderingContext($renderingContext) {
207 $this->renderingContext = $renderingContext;
210 public function setUrl($url) {
211 $this->url = $url;
214 public function setView($view) {
215 $this->view = $view;
218 public function setIgnoreCache($ignoreCache) {
219 $this->ignoreCache = $ignoreCache;
222 public function setForcedJsLibs($forcedJsLibs) {
223 $this->forcedJsLibs = $forcedJsLibs;
226 public function getRefreshInterval() {
227 return $this->refreshInterval;
230 public function getIgnoreCache() {
231 return $this->ignoreCache;
234 public function getForcedJsLibs() {
235 return $this->forcedJsLibs;
238 public function getBlacklist() {
239 if ($this->blacklist == null) {
240 $this->setBlacklist($this->instanceBlacklist());
242 return $this->blacklist;
245 public function getRenderingContext() {
246 return $this->renderingContext;
249 public function getHttpFetcher() {
250 if ($this->httpFetcher == null) {
251 $this->setHttpFetcher($this->instanceHttpFetcher());
253 return $this->httpFetcher;
256 public function getLocale() {
257 if ($this->locale == null) {
258 $this->setLocale($this->instanceLocale());
260 return $this->locale;
264 * Extracts the 'st' token from the GET or POST params and calls the
265 * signer to validate the token
267 * @param GadgetSigner $signer the signer to use (configured in config.php)
268 * @return string the token to use in the signed url
270 public function extractAndValidateToken($signer) {
271 if ($signer == null) {
272 return null;
274 $token = isset($_GET["st"]) ? $_GET["st"] : '';
275 if (! isset($token) || $token == '') {
276 $token = isset($_POST['st']) ? $_POST['st'] : '';
278 if (empty($token)) {
279 throw new Exception("Missing or invalid security token");
281 return $signer->createToken($token);