--- cache/file/ad_cache_file copy.inc	2009-03-31 10:59:00.000000000 -0700
+++ cache/file/ad_cache_file.inc	2009-04-17 10:35:08.000000000 -0700
@@ -101,7 +101,7 @@ function ad_cache_file_id($type, $id, $h
         $cache['tids'][$id] = array();
         $tids = explode(',', $id);
         foreach ($tids as $tid) {
-          if (is_array($cache['tid'][$tid]['aid'])) {
+          if (isset($cache['tid'][$tid]['aid']) && is_array($cache['tid'][$tid]['aid'])) {
             $cache['tids'][$id] += $cache['tid'][$tid]['aid'];
           }
         }
@@ -180,7 +180,7 @@ function ad_cache_file_increment($action
   adserve_variable('extra', $extra);
 
   // increment counter
-  if (is_array($cache['ad'][$aid]) &&
+  if (isset($cache['ad'][$aid]) && is_array($cache['ad'][$aid]) &&
       isset($cache['ad'][$aid]['counts']) &&
       isset($cache['ad'][$aid]['counts'][$group]) &&
       is_array($cache['ad'][$aid]['counts'][$group]) &&
@@ -218,14 +218,14 @@ function ad_cache_file_close() {
 
   // gather data to determine if we should flush the cache
   $cache = ad_cache_file_cache();
-  $last_sync = $cache['last_sync'];
-  $lifetime = $cache['lifetime'];
+  $last_sync = isset($cache['last_sync']) ? $cache['last_sync'] : 0;
+  $lifetime = isset($cache['lifetime']) ? $cache['lifetime'] : 0;
 
   // serialize the array to write it to disk
   $cache = serialize($cache);
 
   // write updated cache back to file and release the lock
-  if (rewind(adserve_variable('fd')) == TRUE) {
+  if (is_resource(adserve_variable('fd')) && rewind(adserve_variable('fd')) == TRUE) {
     if (ftruncate(adserve_variable('fd'), 0) == TRUE) {
       $bytes = fwrite(adserve_variable('fd'), $cache, strlen($cache));
       if ($bytes) {
