Index: infusionBuilder-secure/php/postProcessor.php =================================================================== --- infusionBuilder-secure/php/postProcessor.php (revision 8946) +++ infusionBuilder-secure/php/postProcessor.php (working copy) @@ -118,6 +118,32 @@ echo fpassthru($fp); } +/** + * Performs a mysql query to determine if the requested file is already cached. + * If the file is cached already, the row is returned, otherwise FALSE. + * + * @param object $cacheKey The key, which is the directory name of the stored file + * @param object $intMin The integer value representing minified or source download + * @param object $error_message An error string for if the mysql query fails + * @return object $cache_row A row of data from the cache table if successful, FALSE otherwise + */ +function check_cache_mysql($cacheKey, $intMin, $error_message) { + $cache_query = "SELECT * FROM cache WHERE id = '{$cacheKey}' AND minified = ".$intMin; + $cache_result = mysql_query($cache_query); + if (!$cache_result) { //mysql_query resulted in error + returnError($error_message); + exit (1); + } + + //a valid resource was obtained, but the cache may still be empty if no rows were returned + $cache_row = false; + if (mysql_num_rows($cache_result) == 1) { //must be 0 or 1 because of table restrictions + $cache_row = true; + } + mysql_free_result($cache_result); + return $cache_row; +} + //START OF EXECUTION //process posted values @@ -174,21 +200,11 @@ //check if file are already cached if (!empty($cacheKey)) { - $cache_query = "SELECT * FROM cache WHERE id = '{$cacheKey}' AND minified = ".$intMin; - $cache_result = mysql_query($cache_query); - if (!$cache_result) { - returnError("Cannot complete cache retrieval query"); - exit (1); - } - - if (mysql_num_rows($cache_result) == 1) { - $cache_row = mysql_fetch_assoc($cache_result); - mysql_free_result($cache_result); - } + $is_cached = check_cache_mysql($cacheKey, $intMin, "Cannot complete cache retrieval query"); } //file is not cached - go through build process -if (empty($cache_row)) { +if (!$is_cached) { //create cache directory if (!file_exists($cachepath)) { @@ -216,10 +232,14 @@ $insert_query = "INSERT INTO cache (id, minified) VALUES('$cacheKey', ".$intMin.")"; $insert_result = mysql_query($insert_query); if (!$insert_result) { - returnError("Cannot insert cache entry for this build"); - exit(1); + //check if the cache entry is already in place + $is_cached_again = check_cache_mysql($cacheKey, $intMin, "Error occured inserting cache entry for this build"); + if (!$is_cached_again) { + returnError("Cannot insert cache entry for this build"); + exit (1) + } + //cache entry is in place - continue. } - } //deliver file from cache location to user