Advertisement
copper

ratings

Sep 16th, 2013
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.51 KB | None | 0 0
  1. <?php
  2.  
  3. // Rate theme, and convert old cookie format to a new one
  4. public function ratetheme($id, $rating) {  
  5.     if (!is_numeric($rating)) { // make sure $rating is a valid number
  6.         return FALSE;
  7.     }
  8.     $rating = (int) $rating; // force $rating into an integer
  9.     if ($rating < 0 || $rating > 10) { // make sure $rating is within range
  10.         return FALSE;
  11.     }
  12.  
  13.     $alreadyRated = FALSE;
  14.     $convertOldCookie = FALSE;
  15.     $themeIdList = array (); // list of theme IDs that have been rated by the user
  16.  
  17.     // Handle old cookies
  18.     $oldCookieName = "rating_{$id}";
  19.     if (array_key_exists($oldCookieName, $_COOKIE)) {
  20.         $alreadyRated = TRUE;
  21.         $convertOldCookie = TRUE;
  22.         $themeIdList[] = $id; // add ID to the empty list
  23.         @setcookie($oldCookieName, '', time() - 86400); // delete old cookie
  24.     }
  25.  
  26.     $cookieName = 'ratings';
  27.     if (array_key_exists($cookieName, $_COOKIE)) { // user already has a ratings cookie
  28.         $themeIdList = explode(',', "{$_COOKIE[$cookieName]}"); // turn the coma-separated list of theme IDs into an array
  29.         if ($themeIdList === FALSE) { // explode() returns FALSE when fed an empty string
  30.             $themeIdList = array ();
  31.         }
  32.         if (in_array("$id", $themeIdList)) { // theme ID present in the list
  33.             $alreadyRated = TRUE;
  34.         } else if ($alreadyRated) { // got an old cookie
  35.             $themeIdList[] = $id; // add ID to the list
  36.         }
  37.         if (!$alreadyRated || $convertOldCookie) { // Sanitize the list of theme IDs:
  38.             // no point in executing the following code if the theme has already been rated (no change),
  39.             // or if no old cookie needs to be converted to the new one
  40.             $sanitizedThemeIdList = array ();
  41.             foreach ($themeIdList as $value) {
  42.                 if (is_numeric($value)) { // is $value a valid number?
  43.                     $value = (int) $value; // convert the number string into an integer
  44.                     if ($value > 0) { // is $value a valid theme ID?
  45.                         $sanitizedThemeIdList[] = $value;
  46.                     }
  47.                 }
  48.             }
  49.             $themeIdList = $sanitizedThemeIdList; // replace $themeIdList with sanitized list
  50.         }
  51.     }
  52.  
  53.     if (!$alreadyRated) { // theme hasn't been rated yet: update the database
  54.         $sql = 'UPDATE themes SET ratings=ratings+:rating, numratings=numratings+1 WHERE themeid=:id';
  55.         $args = array (
  56.             ':rating' => $rating,
  57.             ':id' => $id,
  58.         );
  59.         $this->db->query($sql, $args);
  60.     }
  61.  
  62.     if (!$alreadyRated || $convertOldCookie) { // update cookie if needed
  63.         // Turn $themeIdList into a coma-separated list of IDs, and store it in a cookie:
  64.         @setcookie($cookiename, implode(',', $themeIdList), time()+(60*60*24*365*10)); // 10 years
  65.     }
  66.  
  67.     return TRUE;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement