Author: chabotc
Date: Thu Jul 10 10:47:34 2008
New Revision: 675660

URL: http://svn.apache.org/viewvc?rev=675660&view=rev
Log:
SHINDIG-427 - Add Content-Rewrite support.
Big thanks to Jorge Condomi & Gonzalo Aune for their hard work on this.

This patch impements support for the optional content-rewrite feature,
including support for include-urls, exclude-urls and include and exclude
tags. 

Usage (to exclude everything):
        <Optional feature="content-rewrite">
                <Param name="exclude-urls">.*</Param>
        </Optional>

Usage to include everything:
        <Optional feature="content-rewrite">
                <Param name="include-urls">.*</Param>
        </Optional>

See javascript/samplecontainer/examples/rewriter{on,off}.xml as an
example on how to use this.

Rewriting by default has been disabled since this was causing trouble
with quite a few gadgets, so this is an opt-in only feature, however
gadget developers are strongly encouraged to use this since it helps
reduce the load on their servers & latency a lot.

Note: this doesn't currently rewrite the content of included css/js files
since there's no gadget feature state available during the proxy request,
this is something we might change in the future.

This patch also sneaks in a few fixes to the FilesServlet and HttpServlet
classes to optimize the browser caching behaviour.


Added:
    incubator/shindig/trunk/php/src/gadgets/rewrite/
    incubator/shindig/trunk/php/src/gadgets/rewrite/ContentRewriteFeature.php
    incubator/shindig/trunk/php/src/gadgets/rewrite/ContentRewriter.php
Modified:
    incubator/shindig/trunk/php/config/container.php
    incubator/shindig/trunk/php/src/common/HttpServlet.php
    incubator/shindig/trunk/php/src/gadgets/ViewSpec.php
    incubator/shindig/trunk/php/src/gadgets/http/FilesServlet.php
    incubator/shindig/trunk/php/src/gadgets/http/GadgetRenderingServlet.php
    incubator/shindig/trunk/php/src/gadgets/http/JsonRpcServlet.php
    incubator/shindig/trunk/php/src/gadgets/http/ProxyServlet.php

Modified: incubator/shindig/trunk/php/config/container.php
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/php/config/container.php?rev=675660&r1=675659&r2=675660&view=diff
==============================================================================
--- incubator/shindig/trunk/php/config/container.php (original)
+++ incubator/shindig/trunk/php/config/container.php Thu Jul 10 10:47:34 2008
@@ -43,7 +43,7 @@
        // Allow plain text security tokens, this is only here to allow the 
sample files to work. Disable on a production site
        'allow_plaintext_token' => true,
        // Compress the inlined javascript, saves upto 50% of the document size
-       'compress_javascript' => true, 
+       'compress_javascript' => true, 
 
        // The URL Prefix under which shindig lives ie if you have 
http://myhost.com/shindig/php set web_prefix to /shindig/php
        'web_prefix' => '', 

Modified: incubator/shindig/trunk/php/src/common/HttpServlet.php
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/common/HttpServlet.php?rev=675660&r1=675659&r2=675660&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/common/HttpServlet.php (original)
+++ incubator/shindig/trunk/php/src/common/HttpServlet.php Thu Jul 10 10:47:34 
2008
@@ -40,12 +40,7 @@
                // 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
-               $zlib_output = ini_get('zlib.output_compression');
-               if ((!$zlib_output || strtolower($zlib_output) == 'off') && 
function_exists('ob_gzhandler')) { 
-                       ob_start('ob_gzhandler');
-               } else {
-                       ob_start();
-               }
+               ob_start();
        }
 
        /**
@@ -57,7 +52,7 @@
        public function __destruct()
        {
                if (! $this->noHeaders) {
-                       header("Content-Type: $this->contentType; 
charset={$this->charset}");
+                       header("Content-Type: 
$this->contentType".(!empty($this->charset) ? "; charset={$this->charset}" : 
''));
                        header('Accept-Ranges: bytes');
                        $content = ob_get_contents();
                        if ($this->noCache) {
@@ -71,7 +66,7 @@
                                // 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 (md5 sum of content), if so send a not modified header instead of 
content
-                                       $etag = md5($content);
+                                       $etag = '"'.md5($content).'"';
                                        if 
(isset($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] == 
$etag) {
                                                header("ETag: \"$etag\"");
                                                if ($this->lastModified) {
@@ -82,7 +77,7 @@
                                                ob_end_clean();
                                                die();
                                        }
-                                       header("ETag: \"$etag\"");
+                                       header("ETag: $etag");
                                        // If no etag is present, then check if 
maybe this browser supports if_modified_since tags,
                                        // check it against our lastModified 
(if it's set)
                                        if 
(isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $this->lastModified && ! 
isset($_SERVER['HTTP_IF_NONE_MATCH'])) {
@@ -100,6 +95,16 @@
                        }
                }
        }
+       
+       public function getCharset()
+       {
+               return $this->charset;
+       }
+       
+       public function setCharset($charset)
+       {
+               $this->charset = $charset;
+       }
 
        /**
         * Sets the time in seconds that the browser's cache should be 

Modified: incubator/shindig/trunk/php/src/gadgets/ViewSpec.php
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/gadgets/ViewSpec.php?rev=675660&r1=675659&r2=675660&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/gadgets/ViewSpec.php (original)
+++ incubator/shindig/trunk/php/src/gadgets/ViewSpec.php Thu Jul 10 10:47:34 
2008
@@ -32,7 +32,8 @@
        public $view;
        public $preferedHeight;
        public $preferedWidth;
-       
+       public $rewrittenContent;
+
        public function __construct($name, $gadgetContent)
        {
                $attributes = $gadgetContent->attributes();
@@ -56,39 +57,49 @@
                        $this->type = 'HTML';
                }
        }
-       
+
        public function getName()
        {
                return $this->name;
        }
-       
+
        public function getType()
        {
                return $this->type;
        }
-       
+
        public function getHref()
        {
                return $this->href;
        }
-       
+
        public function getQuirks()
        {
                return $this->quirks;
        }
-       
+
        public function getContent()
        {
                return $this->content;
        }
-       
+
        public function getView()
        {
                return $this->view;
        }
-       
+
        public function addContent($data)
        {
                $this->content .= $data;
        }
+
+       public function getRewrittenContent()
+       {
+               return $this->rewrittenContent;
+       }
+
+       public function setRewrittenContent($rewrittenContent)
+       {
+               $this->rewrittenContent = $rewrittenContent;
+       }
 }

Modified: incubator/shindig/trunk/php/src/gadgets/http/FilesServlet.php
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/gadgets/http/FilesServlet.php?rev=675660&r1=675659&r2=675660&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/gadgets/http/FilesServlet.php (original)
+++ incubator/shindig/trunk/php/src/gadgets/http/FilesServlet.php Thu Jul 10 
10:47:34 2008
@@ -60,8 +60,17 @@
                                $this->setContentType('text/javascript');
                        } elseif ($ext == 'css') {
                                $this->setContentType('text/css');
+                       } elseif ($ext == 'xml') {
+                               $this->setContentType('text/xml');
+                       } elseif ($ext == 'png') {
+                               $this->setContentType('image/png');
+                       } elseif ($ext == 'gif') {
+                               $this->setContentType('image/gif');
+                       } elseif ($ext == 'jpg' || $ext == 'jpeg') {
+                               $this->setContentType('image/jpeg');
                        }
                }
+               $this->setCharset('');
                $this->setLastModified(filemtime($file));
                readfile($file);
        }

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=675660&r1=675659&r2=675660&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/gadgets/http/GadgetRenderingServlet.php 
(original)
+++ incubator/shindig/trunk/php/src/gadgets/http/GadgetRenderingServlet.php Thu 
Jul 10 10:47:34 2008
@@ -44,6 +44,8 @@
 require 'src/gadgets/JsLibrary.php';
 require 'src/gadgets/http/HttpUtil.php';
 require 'src/gadgets/ContainerConfig.php';
+require 'src/gadgets/rewrite/ContentRewriter.php';
+require 'src/gadgets/rewrite/ContentRewriteFeature.php';
 
 /**
  * This class deals with the gadget rendering requests (in default config this
@@ -158,11 +160,11 @@
                // Forced libs first.
                if (! empty($forcedLibs)) {
                        $libs = explode(':', $forcedLibs);
-                       echo sprintf($externFmt, 
Config::get('default_js_prefix') . $this->getJsUrl($libs, 
$gadget)."&container=".$context->getContainer()) . "\n";
+                       echo sprintf($externFmt, 
Config::get('default_js_prefix') . $this->getJsUrl($libs, $gadget) . 
"&container=" . $context->getContainer()) . "\n";
                }
                echo "<script>\n";
                
-               if (!empty($forcedLibs)) {
+               if (! empty($forcedLibs)) {
                        // if some of the feature libraries are externalized 
(through a browser cachable <script 
src="/gadgets/js/opensocial-0.7:settitle.js">
                        // type url), then we don't want to include 
dependencies twice, so find the complete features chain, so we can skip over 
those
                        $forcedLibsArray = explode(':', $forcedLibs);
@@ -176,20 +178,23 @@
                                // TODO: This case needs to be handled more 
gracefully by the js
                                // servlet. We should probably inline external 
JS as well.
                                $externJs .= sprintf($externFmt, 
$library->getContent()) . "\n";
-                       // else check if there are no forcedLibs, or if it 
wasn't included in their dep chain
-                       } elseif (empty($forcedLibs) || 
!in_array($library->getFeatureName(), $forcedLibsArray)) {
+                               // else check if there are no forcedLibs, or if 
it wasn't included in their dep chain
+                       } elseif (empty($forcedLibs) || ! 
in_array($library->getFeatureName(), $forcedLibsArray)) {
                                echo $library->getContent();
                        }
                        // otherwise it was already included by 
config.forceJsLibs.
                }
-               echo $this->appendJsConfig($context, $gadget, 
!empty($forcedLibs)) . $this->appendMessages($gadget) . 
-                        $this->appendPreloads($gadget, $context).
-                        "</script>";
+               echo $this->appendJsConfig($context, $gadget, ! 
empty($forcedLibs)) . $this->appendMessages($gadget) . 
$this->appendPreloads($gadget, $context) . "</script>";
                if (strlen($externJs) > 0) {
                        echo $externJs;
-               }       
+               }
                $gadgetExceptions = array();
-               $content = 
$gadget->getSubstitutions()->substitute($view->getContent());
+               $rewriter = new ContentRewriter();
+               if ($rewriter->rewriteGadgetView($gadget, $view)) {
+                       $content = 
$gadget->getSubstitutions()->substitute($view->getRewrittenContent());
+               } else {
+                       $content = 
$gadget->getSubstitutions()->substitute($view->getContent());
+               }
                if (empty($content)) {
                        // Unknown view
                        $gadgetExceptions[] = "View: '" . $context->getView() . 
"' invalid for gadget: " . $gadget->getId()->getKey();
@@ -197,8 +202,7 @@
                if (count($gadgetExceptions)) {
                        throw new GadgetException(print_r($gadgetExceptions, 
true));
                }
-               echo $content . 
-                        
"\n<script>gadgets.util.runOnLoadHandlers();</script></body>\n</html>";
+               echo $content . 
"\n<script>gadgets.util.runOnLoadHandlers();</script></body>\n</html>";
        }
 
        /**
@@ -391,9 +395,8 @@
                                                        die();
                                        }
                                        $resp[$preload->getHref()] = array(
-                                               'body' => 
$response->getResponseContent(), 
-                                               'rc' => $response->getHttpCode()
-                                       );
+                                                       'body' => 
$response->getResponseContent(), 
+                                                       'rc' => 
$response->getHttpCode());
                                }
                        } catch (Exception $e) {
                                throw new Exception($e);

Modified: incubator/shindig/trunk/php/src/gadgets/http/JsonRpcServlet.php
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/gadgets/http/JsonRpcServlet.php?rev=675660&r1=675659&r2=675660&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/gadgets/http/JsonRpcServlet.php (original)
+++ incubator/shindig/trunk/php/src/gadgets/http/JsonRpcServlet.php Thu Jul 10 
10:47:34 2008
@@ -47,6 +47,8 @@
 require 'src/gadgets/MessageBundleParser.php';
 require 'src/gadgets/MessageBundle.php';
 require 'src/gadgets/GadgetException.php';
+require 'src/gadgets/rewrite/ContentRewriter.php';
+require 'src/gadgets/rewrite/ContentRewriteFeature.php';
 
 class JsonRpcServlet extends HttpServlet {
 

Modified: incubator/shindig/trunk/php/src/gadgets/http/ProxyServlet.php
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/gadgets/http/ProxyServlet.php?rev=675660&r1=675659&r2=675660&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/gadgets/http/ProxyServlet.php (original)
+++ incubator/shindig/trunk/php/src/gadgets/http/ProxyServlet.php Thu Jul 10 
10:47:34 2008
@@ -28,6 +28,8 @@
 require 'src/common/RemoteContentFetcher.php';
 require 'src/gadgets/oauth/OAuth.php';
 require 'src/gadgets/oauth/OAuthStore.php';
+require 'src/gadgets/rewrite/ContentRewriter.php';
+require 'src/gadgets/rewrite/ContentRewriteFeature.php';
 
 class ProxyServlet extends HttpServlet {
 

Added: incubator/shindig/trunk/php/src/gadgets/rewrite/ContentRewriteFeature.php
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/gadgets/rewrite/ContentRewriteFeature.php?rev=675660&view=auto
==============================================================================
--- incubator/shindig/trunk/php/src/gadgets/rewrite/ContentRewriteFeature.php 
(added)
+++ incubator/shindig/trunk/php/src/gadgets/rewrite/ContentRewriteFeature.php 
Thu Jul 10 10:47:34 2008
@@ -0,0 +1,178 @@
+<?php
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
+ * 
+ */
+
+class ContentRewriteFeature {
+       public static $REWRITE_TAG = "content-rewrite";
+       public static $INCLUDE_URLS = "include-urls";
+       public static $EXCLUDE_URLS = "exclude-urls";
+       public static $INCLUDE_TAGS = "include-tags";
+       public static $PROXY_URL = "/gadgets/proxy?url=";
+       private $includeAll = false;
+       private $includeNone = false;
+       private $includeParam;
+       private $excludeParam;
+       private $tagsParam;
+
+       public function createRewriteFeature(Gadget $gadget)
+       {
+               $requires = $gadget->getRequires();
+               $rewriteFeature = 
$requires[ContentRewriteFeature::$REWRITE_TAG];
+               $rewriteParams = $rewriteFeature->getParams();
+               if 
(isset($rewriteParams[ContentRewriteFeature::$INCLUDE_URLS])) {
+                       $this->includeParam = 
$this->normalizeParam($rewriteParams[ContentRewriteFeature::$INCLUDE_URLS], 
'//');
+               } else {
+                       $this->includeParam = '//';
+               }
+               if 
(isset($rewriteParams[ContentRewriteFeature::$EXCLUDE_URLS])) {
+                       $this->excludeParam = 
$this->normalizeParam($rewriteParams[ContentRewriteFeature::$EXCLUDE_URLS], 
'//');
+               } else {
+                       $this->excludeParam = '//';
+               }
+               if 
(isset($rewriteParams[ContentRewriteFeature::$INCLUDE_TAGS])) {
+                       $this->tagsParam = 
$rewriteParams[ContentRewriteFeature::$INCLUDE_TAGS];
+                       $this->tagsParam = explode(',', $this->tagsParam);
+               } else {
+                       $this->tagsParam = array();
+               }
+               if ($this->excludeParam == '.*' || $this->includeParam == null) 
{
+                       $this->includeNone = true;
+               }
+               if ($this->includeParam == '.*' || $this->excludeParam == null) 
{
+                       $this->includeAll = true;
+               }
+       }
+
+       public function createDefaultRewriteFeature(Gadget $gadget)
+       {
+               $this->includeParam = '/.*/';
+               $this->includeAll = true;
+       }
+
+       public function normalizeParam($paramValue, $defaultVal)
+       {
+               if (empty($paramValue)) {
+                       return $defaultVal;
+               }
+               if ($paramValue{0} != '/') {
+                       return '/' . $paramValue . '/';
+               } else {
+                       return $paramValue;
+               }
+       }
+
+       public function isRewriteEnabled()
+       {
+               return ! $this->includeNone;
+       }
+
+       public function shouldRewriteURL($url)
+       {
+               if ($this->includeNone) {
+                       return false;
+               } else 
+                       if ($this->includeAll) {
+                               return true;
+                       } else 
+                               if (preg_match($this->includeParam, $url) != 0) 
{
+                                       return ($this->excludeParam != null && 
preg_match($this->excludeParam, $url) != 0);
+                               }
+               return false;
+       }
+
+       public function shouldRewriteTag($tag)
+       {
+               if ($tag != null) {
+                       return in_array(strtolower($tag), $this->tagsParam);
+               }
+               return false;
+       }
+
+       public static function defaultHTMLTags()
+       {
+               return array('img' => '/\<img[^\>]*?src\=(\'|\")(.*?)\1/', 
+                               'link' => 
'/\<link[^\>]*?href\=(\'|\")(.*?)\1/', 
+                               'embed' => 
'/\<embed[^\>]*?src\=(\'|\")(.*?)\1/', 
+                               'script' => 
'/\<script[^\>]*?src\=(\'|\")(.*?)\1/', 
+                               'style' => 
'/url\(\s*(\'|\"|)([^\'\"]*?)(\'|\"|)\s*\)/');
+       }
+
+       public static function styleRegex()
+       {
+               return array('css' => 
'/url\(\s*(\'|\"|)([^\'\"]*?)(\'|\"|)\s*\)/');
+       }
+
+       public function isTagIncluded($tag)
+       {
+               if (empty($this->tagsParam)) {
+                       return false;
+               }
+               return in_array($tag, $this->getTagsParam());
+       }
+
+       public function getExcludeParam()
+       {
+               return $this->excludeParam;
+       }
+
+       public function getIncludeParam()
+       {
+               return $this->includeParam;
+       }
+
+       public function getIncludeAll()
+       {
+               return $this->includeAll;
+       }
+
+       public function getIncludeNone()
+       {
+               return $this->includeNone;
+       }
+
+       public function getTagsParam()
+       {
+               return $this->tagsParam;
+       }
+
+       public function setExcludeParam($excludeParam)
+       {
+               $this->excludeParam = $excludeParam;
+       }
+
+       public function setIncludeParam($includeParam)
+       {
+               $this->includeParam = $includeParam;
+       }
+
+       public function setIncludeAll($includeAll)
+       {
+               $this->includeAll = $includeAll;
+       }
+
+       public function setIncludeNone($includeNone)
+       {
+               $this->includeNone = $includeNone;
+       }
+
+       public function setTagsParam($tagsParam)
+       {
+               $this->tagsParam = $tagsParam;
+       }
+}

Added: incubator/shindig/trunk/php/src/gadgets/rewrite/ContentRewriter.php
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/gadgets/rewrite/ContentRewriter.php?rev=675660&view=auto
==============================================================================
--- incubator/shindig/trunk/php/src/gadgets/rewrite/ContentRewriter.php (added)
+++ incubator/shindig/trunk/php/src/gadgets/rewrite/ContentRewriter.php Thu Jul 
10 10:47:34 2008
@@ -0,0 +1,135 @@
+<?php
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
+ * 
+ */
+
+class ContentRewriter {
+
+       public function rewriteGadgetView(Gadget $gadget, ViewSpec $gadgetView)
+       {
+               // Dont rewrite content if the spec is unavailable
+               $requires = $gadget->getRequires();
+               if (isset($requires[ContentRewriteFeature::$REWRITE_TAG])) {
+                       // Store the feature in the spec so we dont keep 
parsing it
+                       $rewriteFeature = new ContentRewriteFeature();
+                       $rewriteFeature->createRewriteFeature($gadget);
+               } else {
+                       return false;
+               }
+               if (! $rewriteFeature->isRewriteEnabled()) {
+                       return false;
+               }
+               if (ContentRewriteFeature::$PROXY_URL != null) {
+                       $defaultTags = ContentRewriteFeature::defaultHTMLTags();
+                       $htmlTags = null;
+                       if (count($rewriteFeature->getTagsParam()) > 0) {
+                               foreach ($rewriteFeature->getTagsParam() as 
$tag) {
+                                       if (isset($defaultTags[$tag])) {
+                                               $htmlTags[$tag] = 
$defaultTags[$tag];
+                                       }
+                               }
+                       } else {
+                               $htmlTags = $defaultTags;
+                       }
+               }
+               
$gadgetView->setRewrittenContent($this->rewrite($gadgetView->getContent(), 
$htmlTags, $rewriteFeature->getExcludeParam(), 
$rewriteFeature->getIncludeParam(), Config::get('web_prefix') . 
ContentRewriteFeature::$PROXY_URL, $gadget->getId()->getURI(), 
$rewriteFeature->getTagsParam()));
+               return true;
+       }
+
+       public function rewriteRequest($request)
+       {
+               if (! $request->isPost() && 
($this->isCSS($request->getContentType()) || 
$this->isHTML($request->getContentType()))) {
+                       if ($this->isCSS($request->getContentType())) {
+                               $htmlTags = ContentRewriteFeature::styleRegex();
+                       } else {
+                               $htmlTags = 
ContentRewriteFeature::defaultHTMLTags();
+                       }
+                       $content = $request->getResponseContent();
+                       $request->setResponseContent($this->rewrite($content, 
$htmlTags, '//', '/(.*)/', Config::get('web_prefix') . 
ContentRewriteFeature::$PROXY_URL, $request->getUrl()));
+               }
+               return false;
+       }
+
+       public function rewrite($content, $htmlTags, $excRegex, $incRegex, 
$prefix, $gadgetUrl, $tagsParam = null)
+       {
+               $original = $content;
+               $toReplace = array();
+               $toMatch = array();
+               if (isset($htmlTags)) {
+                       foreach ($htmlTags as $rx) {
+                               $matchTemp = array();
+                               @preg_match_all($rx, $content, $matchTemp[0]);
+                               if (! empty($matchTemp[0][0])) {
+                                       $matchTemp[0][1] = $matchTemp[0][2];
+                                       unset($matchTemp[0][2]);
+                                       $toMatch = array_merge($toMatch, 
$matchTemp);
+                               }
+                       }
+               }
+               $count = 0;
+               foreach ($toMatch as $match) {
+                       $size = count($match[1]);
+                       for ($i = 0; $i < $size; $i ++) {
+                               $url = trim($match[1][$i]);
+                               if (($excRegex != '//' && 
@preg_match($excRegex, $url) != 0) || ($incRegex != '//' && 
@preg_match($incRegex, $url) == 0)) {
+                                       continue;
+                               }
+                               $toReplace[$count]['original'] = $match[0][$i];
+                               $toReplace[$count]['url'] = $url;
+                               if ($this->isRelative($url)) {
+                                       $lPosition = strrpos($gadgetUrl, "/");
+                                       $gadgetUrl = substr($gadgetUrl, 0, 
$lPosition + 1);
+                                       $url = $gadgetUrl . $url;
+                               }
+                               $toReplace[$count]['new'] = $prefix . 
urlencode($url);
+                               $count ++;
+                       }
+               }
+               if (! empty($toReplace)) {
+                       foreach ($toReplace as $target) {
+                               $tagTemp = str_replace($target['url'], 
$target['new'], $target['original']);
+                               $content = str_replace($target['original'], 
$tagTemp, $content);
+                       }
+               } else {
+                       return $original;
+               }
+               return $content;
+       }
+
+       private function isRelative($url)
+       {
+               return ! (strpos($url, "http://";) !== false);
+       }
+
+       public function isHTML($mime)
+       {
+               if ($mime == null) {
+                       return false;
+               }
+               return (strpos(strtolower($mime), 'html') !== false);
+       }
+
+       public function isCSS($mime)
+       {
+               if ($mime == null) {
+                       return false;
+               }
+               return (strpos(strtolower($mime), 'css') !== false);
+       }
+
+}
\ No newline at end of file


Reply via email to