Author: chabotc
Date: Fri Nov 21 07:27:14 2008
New Revision: 719595

URL: http://svn.apache.org/viewvc?rev=719595&view=rev
Log:
Checking if files exists can cause to much disk io on prod systems, implemented 
a flag to be able to turn this off in cases the presense of the file can be 
'assumed'

Added:
    incubator/shindig/trunk/php/src/common/File.php
Modified:
    incubator/shindig/trunk/php/config/container.php
    incubator/shindig/trunk/php/index.php
    incubator/shindig/trunk/php/src/common/CacheFile.php
    incubator/shindig/trunk/php/src/gadgets/ContainerConfig.php
    incubator/shindig/trunk/php/src/gadgets/JsFeatureLoader.php
    incubator/shindig/trunk/php/src/gadgets/JsLibrary.php
    incubator/shindig/trunk/php/src/gadgets/SigningFetcherFactory.php
    incubator/shindig/trunk/php/src/gadgets/sample/BasicGadgetBlacklist.php

Modified: incubator/shindig/trunk/php/config/container.php
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/php/config/container.php?rev=719595&r1=719594&r2=719595&view=diff
==============================================================================
--- incubator/shindig/trunk/php/config/container.php (original)
+++ incubator/shindig/trunk/php/config/container.php Fri Nov 21 07:27:14 2008
@@ -35,7 +35,8 @@
  *     'app_data_service' => 'MyAppDataService',
  *     'app_data_service' => 'MyAppDataService',
  *     'oauth_lookup_service' => 'MyOAuthLookupService'
- *     'xrds_location' => 'http://www.mycontainer.com/xrds'
+ *     'xrds_location' => 'http://www.mycontainer.com/xrds',
+ *      'check_file_exists' => false
  * );
  *  
  */
@@ -43,6 +44,9 @@
 $shindigConfig = array(
        // Show debug backtrace's. Disable this on a production site
        'debug' => true,
+       // do real file_exist checks? Turning this off can be a big performance 
gain on prod servers but also risky & less verbose errors
+        'check_file_exists' => true,
+
        // 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

Modified: incubator/shindig/trunk/php/index.php
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/php/index.php?rev=719595&r1=719594&r2=719595&view=diff
==============================================================================
--- incubator/shindig/trunk/php/index.php (original)
+++ incubator/shindig/trunk/php/index.php Fri Nov 21 07:27:14 2008
@@ -18,6 +18,7 @@
  */
 
 include_once ('config.php');
+include_once ('src/common/File.php');
 
 // Basic sanity check if we have all required modules
 $modules = array('json', 'SimpleXML', 'libxml', 'curl', 'openssl');

Modified: incubator/shindig/trunk/php/src/common/CacheFile.php
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/common/CacheFile.php?rev=719595&r1=719594&r2=719595&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/common/CacheFile.php (original)
+++ incubator/shindig/trunk/php/src/common/CacheFile.php Fri Nov 21 07:27:14 
2008
@@ -100,7 +100,7 @@
                if ($this->isLocked($cacheFile)) {
                        $this->waitForLock($cacheFile);
                }
-               if (file_exists($cacheFile) && is_readable($cacheFile)) {
+               if (File::exists($cacheFile) && File::readable($cacheFile)) {
                        $now = time();
                        if (($mtime = filemtime($cacheFile)) !== false && ($now 
- $mtime) < $expiration) {
                                if (($data = @file_get_contents($cacheFile)) 
!== false) {

Added: incubator/shindig/trunk/php/src/common/File.php
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/common/File.php?rev=719595&view=auto
==============================================================================
--- incubator/shindig/trunk/php/src/common/File.php (added)
+++ incubator/shindig/trunk/php/src/common/File.php Fri Nov 21 07:27:14 2008
@@ -0,0 +1,41 @@
+<?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 File {
+
+       public static function exists($file)
+       {
+               // only really check if check_file_exists == true, big 
performance hit on production systems, but also much safer :)
+               if (Config::get('check_file_exists')) {
+                       return file_exists($file);
+               } else {
+                       return true;
+               }
+       }
+       
+       public static function readable($file)
+       {
+               // only really check if check_file_exists == true, big 
performance hit on production systems, but also much safer :)
+               if (Config::get('check_file_exists')) {
+                       return is_readable($file);
+               } else {
+                       return true;
+               }
+       }
+}
\ No newline at end of file

Modified: incubator/shindig/trunk/php/src/gadgets/ContainerConfig.php
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/gadgets/ContainerConfig.php?rev=719595&r1=719594&r2=719595&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/gadgets/ContainerConfig.php (original)
+++ incubator/shindig/trunk/php/src/gadgets/ContainerConfig.php Fri Nov 21 
07:27:14 2008
@@ -32,11 +32,11 @@
 
        private function loadContainers($containers)
        {
-               if (! file_exists($containers) || ! is_dir($containers)) {
+               if (! File::exists($containers)) {
                        throw new Exception("Invalid container path");
                }
                foreach (glob("$containers/*.js") as $file) {
-                       if (! is_readable($file)) {
+                       if (! File::readable($file)) {
                                throw new Exception("Could not read container 
config: $file");
                        }
                        if (is_dir($file)) {

Modified: incubator/shindig/trunk/php/src/gadgets/JsFeatureLoader.php
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/gadgets/JsFeatureLoader.php?rev=719595&r1=719594&r2=719595&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/gadgets/JsFeatureLoader.php (original)
+++ incubator/shindig/trunk/php/src/gadgets/JsFeatureLoader.php Fri Nov 21 
07:27:14 2008
@@ -50,7 +50,7 @@
        private function loadFiles($path, &$features)
        {
                $featuresFile = $path . '/features.txt';
-               if (file_exists($featuresFile) && is_readable($featuresFile)) {
+               if (File::exists($featuresFile)) {
                        $files = explode("\n", 
file_get_contents($featuresFile));
                        // custom sort, else core.io seems to bubble up before 
core, which breaks the dep chain order
                        usort($files, array($this, 'sortFeaturesFiles'));
@@ -67,7 +67,7 @@
        private function processFile($file)
        {
                $feature = null;
-               if (file_exists($file) && is_file($file) && is_readable($file)) 
{
+               if (File::exists($file)) {
                        if (($content = @file_get_contents($file))) {
                                $feature = $this->parse($content, 
dirname($file));
                        }

Modified: incubator/shindig/trunk/php/src/gadgets/JsLibrary.php
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/gadgets/JsLibrary.php?rev=719595&r1=719594&r2=719595&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/gadgets/JsLibrary.php (original)
+++ incubator/shindig/trunk/php/src/gadgets/JsLibrary.php Fri Nov 21 07:27:14 
2008
@@ -1,4 +1,5 @@
 <?php
+
 /*
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements. See the NOTICE file
@@ -92,14 +93,16 @@
                if (empty($fileName) || (strpos($fileName, 'res://') !== 
false)) {
                        return '';
                }
-               if (! file_exists($fileName)) {
-                       throw new Exception("JsLibrary file missing: 
$fileName");
-               }
-               if (! is_file($fileName)) {
-                       throw new Exception("JsLibrary file is not a file: 
$fileName");
-               }
-               if (! is_readable($fileName)) {
-                       throw new Exception("JsLibrary file not readable: 
$fileName");
+               if (Config::get('debug')) {
+                       if (! ile::exists($fileName)) {
+                               throw new Exception("JsLibrary file missing: 
$fileName");
+                       }
+                       if (! is_file($fileName)) {
+                               throw new Exception("JsLibrary file is not a 
file: $fileName");
+                       }
+                       if (! File::readable($fileName)) {
+                               throw new Exception("JsLibrary file not 
readable: $fileName");
+                       }
                }
                if (! ($content = @file_get_contents($fileName))) {
                        throw new Exception("JsLibrary error reading file: 
$fileName");

Modified: incubator/shindig/trunk/php/src/gadgets/SigningFetcherFactory.php
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/gadgets/SigningFetcherFactory.php?rev=719595&r1=719594&r2=719595&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/gadgets/SigningFetcherFactory.php (original)
+++ incubator/shindig/trunk/php/src/gadgets/SigningFetcherFactory.php Fri Nov 
21 07:27:14 2008
@@ -48,8 +48,8 @@
                if (! empty($keyFile)) {
                        $privateKey = null;
                        try {
-                               if (file_exists($keyFile)) {
-                                       if (is_readable($keyFile)) {
+                               if (File::exists($keyFile)) {
+                                       if (File::readable($keyFile)) {
                                                $rsa_private_key = 
@file_get_contents($keyFile);
                                        } else {
                                                throw new Exception("Could not 
read keyfile ($keyFile), check the file name and permission");

Modified: 
incubator/shindig/trunk/php/src/gadgets/sample/BasicGadgetBlacklist.php
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/gadgets/sample/BasicGadgetBlacklist.php?rev=719595&r1=719594&r2=719595&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/gadgets/sample/BasicGadgetBlacklist.php 
(original)
+++ incubator/shindig/trunk/php/src/gadgets/sample/BasicGadgetBlacklist.php Fri 
Nov 21 07:27:14 2008
@@ -32,8 +32,8 @@
                if (!$file) {
                        $file = Config::get('base_path') . '/blacklist.txt';
                }
-               if (file_exists($file) && is_readable($file)) {
-                       $this->rules = explode("\n", file_get_contents($file));
+               if (File::exists($file)) {
+                       $this->rules = explode("\n", @file_get_contents($file));
                }
        }
 
@@ -46,7 +46,7 @@
        function isBlacklisted($url)
        {
                foreach ($this->rules as $rule) {
-                       if (preg_match($rule, $url)) {
+                       if (!empty($rule) && preg_match($rule, $url)) {
                                return true;
                        }
                }


Reply via email to