You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@shindig.apache.org by ch...@apache.org on 2008/05/06 18:13:45 UTC

svn commit: r653825 - in /incubator/shindig/trunk/php: config.php src/common/Crypto.php src/common/HttpServlet.php src/common/UrlGenerator.php src/gadgets/Gadget.php src/gadgets/JsonRpcHandler.php src/gadgets/http/GadgetRenderingServlet.php

Author: chabotc
Date: Tue May  6 09:13:44 2008
New Revision: 653825

URL: http://svn.apache.org/viewvc?rev=653825&view=rev
Log:
- Fixed JsonRpcService to generate real iframe urls, and include v= param for cachability of the gadgets
- Added variable cache times based on nocache/v=/no v= params to gadget renderer
- Added a check to crypto to see if the cipher is available
- Fixed bug in GadgetRenderingServlet with unknown views
- Made gadget rendering servlet use default_js_prefix so that it actually finds js libs now in metadata gadgets
- Made JsonRpcService use the default_iframe_prefix config key instead of hard coded value


Modified:
    incubator/shindig/trunk/php/config.php
    incubator/shindig/trunk/php/src/common/Crypto.php
    incubator/shindig/trunk/php/src/common/HttpServlet.php
    incubator/shindig/trunk/php/src/common/UrlGenerator.php
    incubator/shindig/trunk/php/src/gadgets/Gadget.php
    incubator/shindig/trunk/php/src/gadgets/JsonRpcHandler.php
    incubator/shindig/trunk/php/src/gadgets/http/GadgetRenderingServlet.php

Modified: incubator/shindig/trunk/php/config.php
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/php/config.php?rev=653825&r1=653824&r2=653825&view=diff
==============================================================================
--- incubator/shindig/trunk/php/config.php (original)
+++ incubator/shindig/trunk/php/config.php Tue May  6 09:13:44 2008
@@ -39,7 +39,7 @@
 
 	// The data handlers for the social data, this is a list of class names
 	// seperated by a , For example:
-	// 'handlers' => 'PartuzaHandler',
+	//'handlers' => 'PartuzaHandler',
 	// if the value is empty, the defaults used in the example above will be used.
 	'handlers' => '',
 
@@ -66,8 +66,10 @@
 	// gadget server specific settings
 	'userpref_param_prefix' => 'up_',
 	'libs_param_name' => 'libs',
-	'default_js_prefix' => '/js/',
-	'default_iframe_prefix' => 'ifr?', 
+	// location  of the javascript handler (include the full path), default this is /gadgets/js
+	'default_js_prefix' => '/gadgets/js/',
+	// location of the gadget iframe renderer, default this is /gadgets/ifr?
+	'default_iframe_prefix' => '/gadgets/ifr?', 
 	
 	// if your using memcached, these values are used for locating the server
 	// if your not using memcached, ignore these values

Modified: incubator/shindig/trunk/php/src/common/Crypto.php
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/common/Crypto.php?rev=653825&r1=653824&r2=653825&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/common/Crypto.php (original)
+++ incubator/shindig/trunk/php/src/common/Crypto.php Tue May  6 09:13:44 2008
@@ -68,7 +68,12 @@
 	{
 		/* Open the cipher */
 		$td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
-		$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_DEV_RANDOM);
+		if (!$td) {
+			throw new GeneralSecurityException('Invalid mcrypt cipher, check your libmcrypt library and php-mcrypt extention');
+		}
+		// replaced MCRYPT_DEV_RANDOM with MCRYPT_RAND since windows doesn't have /dev/rand :)
+		srand((double) microtime() * 1000000);
+		$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
 		/* Intialize encryption */
 		mcrypt_generic_init($td, $key, $iv);
 		/* Encrypt data */

Modified: incubator/shindig/trunk/php/src/common/HttpServlet.php
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/common/HttpServlet.php?rev=653825&r1=653824&r2=653825&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/common/HttpServlet.php (original)
+++ incubator/shindig/trunk/php/src/common/HttpServlet.php Tue May  6 09:13:44 2008
@@ -27,31 +27,24 @@
 	private $lastModified = false;
 	private $contentType = 'text/html';
 	private $charset = 'UTF-8';
-	public $noHeaders = false;
 	private $noCache = false;
+	private $cacheTime;
+	public $noHeaders = false;
 	
 	/**
 	 * Enables output buffering so we can do correct header handling in the destructor
 	 *
 	 */
-	/**
-	 * Enables output buffering so we can do correct header handling in the destructor
-	 *
-	 */
 	public function __construct()
 	{
+		// set our default cache time (config cache time defaults to 24 hours aka 1 day)
+		$this->cacheTime = Config::get('cache_time');
 		// to do our header magic, we need output buffering on
 		ob_start();
 	}
 	
 	/**
-	 * Enter description here...
-	 * If noHeaders is false, it adds all the correct http/1.1 headers to the request
-	 * and deals with modified/expires/e-tags/etc. This makes the server behave more like
-	 * a real http server.
-	 */
-	/**
-	 * Enter description here...
+	 * Code ran after the event handler, adds headers etc to the request
 	 * If noHeaders is false, it adds all the correct http/1.1 headers to the request
 	 * and deals with modified/expires/e-tags/etc. This makes the server behave more like
 	 * a real http server.
@@ -71,8 +64,8 @@
 			} else {
 				// attempt at some propper header handling from php
 				// this departs a little from the shindig code but it should give is valid http protocol handling
-				header('Cache-Control: public,max-age=' . Config::get('cache_time'));
-				header("Expires: " . gmdate("D, d M Y H:i:s", time() + Config::get('cache_time')) . " GMT");
+				header('Cache-Control: public,max-age=' . $this->cacheTime);
+				header("Expires: " . gmdate("D, d M Y H:i:s", time() + $this->cacheTime) . " GMT");
 				// Obey browsers (or proxy's) request to send a fresh copy if we recieve a no-cache pragma or cache-control request
 				if (! isset($_SERVER['HTTP_PRAGMA']) || ! strstr(strtolower($_SERVER['HTTP_PRAGMA']), 'no-cache') && (! isset($_SERVER['HTTP_CACHE_CONTROL']) || ! strstr(strtolower($_SERVER['HTTP_CACHE_CONTROL']), 'no-cache'))) {
 					// If the browser send us a E-TAG check if it matches (sha1 sum of content), if so send a not modified header instead of content
@@ -98,9 +91,7 @@
 							die();
 						}
 					}
-					if ($this->lastModified) {
-						header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $this->lastModified));
-					}
+					header('Last-Modified: ' . gmdate('D, d M Y H:i:s', ($this->lastModified ? $this->lastModified : time())));
 				}
 			}
 			echo $content;
@@ -108,10 +99,26 @@
 	}
 	
 	/**
-	 * Sets the content type of this request (forinstance: text/html or text/javascript, etc) 
+	 * Sets the time in seconds that the browser's cache should be 
+	 * considered out of date (through the Expires header) 
 	 *
-	 * @param string $type content type header to use
+	 * @param int $time time in seconds
+	 */
+	public function setCacheTime($time)
+	{
+		$this->cacheTime = $time;
+	}
+	
+	/**
+	 * Returns the time in seconds that the browser is allowed to cache the content
+	 *
+	 * @return int $time
 	 */
+	public function getCacheTime()
+	{
+		return $this->cacheTime;
+	}
+	
 	/**
 	 * Sets the content type of this request (forinstance: text/html or text/javascript, etc) 
 	 *
@@ -127,11 +134,6 @@
 	 *
 	 * @return string content type string
 	 */
-	/**
-	 * Returns the current content type 
-	 *
-	 * @return string content type string
-	 */
 	public function getContentType()
 	{
 		return $this->contentType;
@@ -142,11 +144,6 @@
 	 *
 	 * @return int timestamp
 	 */
-	/**
-	 * returns the current last modified time stamp
-	 *
-	 * @return int timestamp
-	 */
 	public function getLastModified()
 	{
 		return $this->lastModified;
@@ -158,12 +155,6 @@
 	 *
 	 * @param int $modified timestamp
 	 */
-	/**
-	 * Sets the last modified timestamp. It automaticly checks if this timestamp
-	 * is larger then its current timestamp, and if not ignores the call
-	 *
-	 * @param int $modified timestamp
-	 */
 	public function setLastModified($modified)
 	{
 		$this->lastModified = max($this->lastModified, $modified);
@@ -175,12 +166,6 @@
 	 *
 	 * @param boolean $cache send no-cache headers?
 	 */
-	/**
-	 * Sets the noCache boolean. If its set to true, no-caching headers will be send
-	 * (pragma no cache, expiration in the past)
-	 *
-	 * @param boolean $cache send no-cache headers?
-	 */
 	public function setNoCache($cache = false)
 	{
 		$this->noCache = $cache;
@@ -191,11 +176,6 @@
 	 *
 	 * @return boolean
 	 */
-	/**
-	 * returns the noCache boolean
-	 *
-	 * @return boolean
-	 */
 	public function getNoCache()
 	{
 		return $this->noCache;

Modified: incubator/shindig/trunk/php/src/common/UrlGenerator.php
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/common/UrlGenerator.php?rev=653825&r1=653824&r2=653825&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/common/UrlGenerator.php (original)
+++ incubator/shindig/trunk/php/src/common/UrlGenerator.php Tue May  6 09:13:44 2008
@@ -19,11 +19,40 @@
  */
 
 class UrlGenerator {
-	static function getIframeURL($gadget)
+	static function getIframeURL($gadget, $context)	
 	{
-		//TODO this
-		// returns fake demo value to make the basic JsonRpc service work
+		$inlineJs = '';
+		foreach ( $gadget->getJsLibraries() as $library ) {
+			$type = $library->getType();
+			if ($type != 'URL') {
+				$inlineJs .= $library->getContent() . "\n";
+			}
+		}
+		$v = md5($inlineJs);
+		
+		$view = HttpUtil::getView($gadget, $context);
+		
+		$up = '';
+		$prefs = $context->getUserPrefs();
+		foreach ($gadget->getUserPrefs() as $pref) {
+			$name = $pref->getName();
+			$value = $prefs->getPref($name);
+			if ($value == null) {
+				$value = $pref->getDefaultValue();
+			}
+			$up .= '&up_'.urlencode($name).'='.urlencode($value);
+		}
+		
 		// note: put the URL last, else some browsers seem to get confused (reported by hi5)
-		return "/gadgets/ifr?container=default&mid=1&v=db18c863f15d5d1e758a91f2a44881b4&lang=en&country=US&url=http%3A%2F%2Fwww.google.com%2Fig%2Fmodules%2Fhello.xml";
+		return
+			Config::get('default_iframe_prefix').
+			'container='.$context->getContainer().
+			($context->getIgnoreCache() ? 'nocache=1' : '&v='.$v).
+			($context->getModuleId() != 0 ? '&mid='.$context->getModuleId() : '').
+			'&lang='.$context->getLocale()->getLanguage().
+			'&country='.$context->getLocale()->getCountry().			
+			'&view='.$view->getName().
+			$up.
+			'&url='.urlencode($context->getUrl());		
 	}
 }

Modified: incubator/shindig/trunk/php/src/gadgets/Gadget.php
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/gadgets/Gadget.php?rev=653825&r1=653824&r2=653825&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/gadgets/Gadget.php (original)
+++ incubator/shindig/trunk/php/src/gadgets/Gadget.php Tue May  6 09:13:44 2008
@@ -293,7 +293,7 @@
 	
 	public function getView($viewName)
 	{
-		return isset($this->views[$viewName]) ? $this->views[$viewName] : false;
+		return isset($this->views[$viewName]) ? $this->views[$viewName] : $this->views[DEFAULT_VIEW];
 	}
 }
 

Modified: incubator/shindig/trunk/php/src/gadgets/JsonRpcHandler.php
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/gadgets/JsonRpcHandler.php?rev=653825&r1=653824&r2=653825&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/gadgets/JsonRpcHandler.php (original)
+++ incubator/shindig/trunk/php/src/gadgets/JsonRpcHandler.php Tue May  6 09:13:44 2008
@@ -12,7 +12,7 @@
 				$context = new JsonRpcGadgetContext($requests->context, $gadgetUrl);
 				$gadgetServer = new GadgetServer();
 				$gadget = $gadgetServer->processGadget($context);
-				$response[] = $this->makeResponse($gadget, $gadgetModuleId, $gadgetUrl);
+				$response[] = $this->makeResponse($gadget, $gadgetModuleId, $gadgetUrl, $context);
 			} catch (Exception $e) {
 				$response[] = array('errors' => array($e->getMessage()), 'moduleId' => $gadgetModuleId, 'url' => $gadgetUrl);
 			}
@@ -20,7 +20,7 @@
 		return $response;
 	}
 	
-	private function makeResponse($gadget, $gadgetModuleId, $gadgetUrl)
+	private function makeResponse($gadget, $gadgetModuleId, $gadgetUrl, $context)
 	{
 		$response = array();
 		$prefs = array();
@@ -64,7 +64,7 @@
 		$response['titleUrl'] = $gadget->getTitleUrl();
 		$response['directoryTitle'] = $gadget->getDirectoryTitle();
 		$response['author'] = $gadget->getAuthor();
-		$response['iframeUrl'] = UrlGenerator::getIframeURL($gadget);	
+		$response['iframeUrl'] = UrlGenerator::getIframeURL($gadget, $context);	
 		$response['userPrefs'] = $prefs;
 		return $response;
 	}

Modified: incubator/shindig/trunk/php/src/gadgets/http/GadgetRenderingServlet.php
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/gadgets/http/GadgetRenderingServlet.php?rev=653825&r1=653824&r2=653825&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/gadgets/http/GadgetRenderingServlet.php (original)
+++ incubator/shindig/trunk/php/src/gadgets/http/GadgetRenderingServlet.php Tue May  6 09:13:44 2008
@@ -134,7 +134,7 @@
 		// Forced libs first.
 		if (! empty($forcedLibs)) {
 			$libs = explode(':', $forcedLibs);
-			$output .= sprintf($externFmt, $this->getJsUrl($libs)) . "\n";
+			$output .= sprintf($externFmt, Config::get('default_js_prefix').$this->getJsUrl($libs, $gadget)) . "\n";
 		}
 		if (strlen($inlineJs) > 0) {
 			$output .= "<script><!--\n" . $inlineJs . "\n-->\n</script>\n";
@@ -158,6 +158,17 @@
 		$output .= $content . "\n";
 		$output .= "<script>gadgets.util.runOnLoadHandlers();</script>\n";
 		$output .= "</body>\n</html>";
+		if ($context->getIgnoreCache()) {
+			// no cache was requested, set non-caching-headers
+			$this->setNoCache(true);
+		} elseif (isset($_GET['v'])) {
+			// version was given, cache for a long long time (a year)
+			$this->setCacheTime(365 * 24 * 60 * 60);
+		} else {
+			// no version was given, cache for 5 minutes
+			$this->setCacheTime(5 * 60);
+		}
+		// Was a privacy policy header configured? if so set it
 		if (Config::get('P3P') != '') {
 			header("P3P: ".Config::get('P3P'));
 		}
@@ -269,7 +280,7 @@
 				$inlineJs .= $library->getContent() . "\n";
 			}
 		}
-		$buf .= ".js?v=" . sha1($inlineJs);
+		$buf .= ".js?v=" . md5($inlineJs);
 		return $buf;
 	}