Index: banner.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/banner/banner.module,v
retrieving revision 1.50.2.1
diff -u -r1.50.2.1 banner.module
--- banner.module	4 Oct 2005 13:03:47 -0000	1.50.2.1
+++ banner.module	18 Mar 2006 17:49:04 -0000
@@ -47,7 +47,7 @@
   }
 
   $mail_variables = ' %username, %bannername, %sitename, %day_views, %day_clicks, %week_views, %week_clicks, %total_views, %total_clicks, %url, %expire';
-  
+
   $group = form_textfield(t('Banner image path'), 'banner_image_path', variable_get('banner_image_path', 'banners'), 45, 255, t('Subdirectory in the directory "%dir" where banner images will be stored.', array('%dir' => variable_get('file_directory_path', 'files') . '/')) . $error);
   $output = form_group(t('Images'), $group);
 
@@ -232,7 +232,7 @@
       if (user_access('manage banners')) {
         if ($edit) {
           $output .= theme('box', t('Status'), banner_user_save($edit));
-  
+
           # display each banner in its own box
           $result = db_query("SELECT * FROM {banner} WHERE uid = '%d'", $user->uid);
           while ($banner = db_fetch_object($result)) {
@@ -254,7 +254,7 @@
         if ($string = banner_user_update($edit)) {
           $output .= theme('box', t('Status'), $string);
         }
-  
+
         // display each banner in its own box
         if (user_access('administer banners') && (arg(1) == 'view')) {
           switch (arg(2)) {
@@ -502,7 +502,7 @@
   $fields = array('title', 'url');
 
   _banner_handle_upload($fields, $edit);
-  
+
   $fields[] = 'created';
   $edit['created'] = time();
   $fields[] = 'id';
@@ -616,9 +616,9 @@
     $to = $user->mail;
     $from = variable_get('site_mail', ini_get('sendmail_from'));
     $headers = "From: $from\nReply-to: $from\nX-Mailer: Drupal\nReturn-path: <$from>\nErrors-to: $from\n";
-  
+
     $variables = array('%username' => $user->name, '%bannername' => $banner->title, '%sitename' => variable_get('site_name', 'drupal'), '%day_views' => $banner->day_views, '%day_clicks' => $banner->day_clicks, '%week_views' => $banner->week_views, '%week_clicks' => $banner->week_clicks, '%total_views' => $banner->views, '%total_clicks' => $banner->clicks, '%url' => "$base_url/banners/", '%expire' => (($banner->unpublish_date > 0 ) ? format_date($banner->unpublish_date, 'custom', 'M d, Y') : t('never')));
-  
+
     // use user.module mail function to actually send the mail
     if (!user_mail($to, $subject, wordwrap(strtr($message, $variables), 72), $headers)) {
       // mail notification failed
@@ -738,7 +738,7 @@
     }
     if ($notifications) {
       $output .= $notifications;
-    } 
+    }
     else {
       $output .= t('[none]');
     }
@@ -815,19 +815,19 @@
   $headers = array('', t('views'), t('clicks'), t('reset'));
   $rows = array();
   $rows[] = array(
-              array('data' => t('today'), 'align' => 'left'), 
+              array('data' => t('today'), 'align' => 'left'),
               array('data' => $edit['day_views'], 'align' => 'center'),
               array('data' => $edit['day_clicks'], 'align' => 'center'),
               array('data' => form_checkbox('', 'reset_day'), 'align' => 'center'),
              );
   $rows[] = array(
-              array('data' => t('this week'), 'align' => 'left'), 
+              array('data' => t('this week'), 'align' => 'left'),
               array('data' => $edit['week_views'], 'align' => 'center'),
               array('data' => $edit['week_clicks'], 'align' => 'center'),
               array('data' => form_checkbox('', 'reset_week'), 'align' => 'center'),
              );
   $rows[] = array(
-              array('data' => t('total'), 'align' => 'left'), 
+              array('data' => t('total'), 'align' => 'left'),
               array('data' => $edit['views'], 'align' => 'center'),
               array('data' => $edit['clicks'], 'align' => 'center'),
               array('data' => form_checkbox('', 'reset_total'), 'align' => 'center'),
@@ -850,7 +850,7 @@
     $form .= form_hidden('id', $edit['id']);
     $form .= form_submit(t('Delete'));
   }
-  
+
   return form($form, 'post', 0, array('enctype' => 'multipart/form-data'));
 }
 
@@ -935,14 +935,14 @@
     foreach ($fields as $field) {
       $update[] = "$field = '" .db_escape_string($edit[$field]). "'";
     }
-    
+
     _banner_refresh_cache();
     db_query('UPDATE {banner} SET ' .implode(', ', $update). ' WHERE id = %d', $edit['id']);
 
     $string = t('banner updated.');
   }
   else {
-    // insert    
+    // insert
     $fields[] = 'id';
     $edit['id'] = db_next_id('banner');
 
@@ -1103,33 +1103,32 @@
           }
         }
         // obtain lock to prevent corruption
-        @flock($fd, LOCK_EX);
+        @flock($fd, LOCK_SH);
         $contents = file_get_contents(file_create_path($cache_file));
+        @flock($fd, LOCK_UN);
 
         // update views in db
         $struct = unserialize($contents);
         if ($struct) {
-	  // [1][term id][banner id]->data
           foreach ($struct[1] as $tid) {
-	    foreach ($tid as $id => $banner) {
+            foreach ($tid as $id => $banner) {
               if ($param['banner_reset'] != $id) {
                 db_query('UPDATE {banner} SET views = views + %d, day_views = day_views + %d, week_views = week_views + %d WHERE id = %d', $banner->views, $banner->views, $banner->views, $id);
               }
             }
-	  }
+          }
         }
-  
+
         _banner_reset_counters($param);
       }
 
       $struct = _banner_get_struct();
       if ($struct) {
         $struct[] = time();
-  
+
         $data = serialize($struct);
-  
-        file_save_data($data, $cache_file, 1);
-	// file_save_data() closes fd and frees the lock
+
+        $return = file_save_data($data, file_create_path($cache_file), FILE_EXISTS_REPLACE);
       }
     }
   }
@@ -1207,7 +1206,7 @@
   }
   if ($param[reset_week_counters]) {
     db_query('UPDATE {banner} SET week_views = 0, week_clicks = 0');
-    // TODO: Watchdog entry 
+    // TODO: Watchdog entry
     // change from status (3) "week's limit reached" to (1) "enabled"
     db_query('UPDATE {banner} SET status = 1 where status = 3');
   }
