Advertisement
carbonize

like.php

Nov 16th, 2011
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.52 KB | None | 0 0
  1. <?php
  2. /*
  3.  * like.php
  4.  * by Stewart Souter
  5.  *
  6.  * This scrupt is for the handling of the likes
  7.  * Will add the like and the ip to the database and
  8.  * write a cookie for additional remembering.
  9.  */
  10.  
  11. // Lets grab the database info etc
  12. define('LAZ_INCLUDE_PATH', dirname(__FILE__));
  13. require_once LAZ_INCLUDE_PATH.'/admin/version.php';
  14. require_once LAZ_INCLUDE_PATH.'/admin/config.inc.php';
  15. require_once LAZ_INCLUDE_PATH.'/lib/mysql.class.php';
  16. require_once LAZ_INCLUDE_PATH.'/lib/vars.class.php';
  17. require_once LAZ_INCLUDE_PATH.'/lib/template.class.php';
  18.  
  19. // Set the prefix for the table names
  20. define('LAZ_TABLE_PREFIX', $table_prefix);
  21.  
  22. // Get the guestbook settings
  23. $db = new guestbook_vars(LAZ_INCLUDE_PATH);
  24. $db->getVars();
  25.  
  26. $entry   = (!empty($_GET['entry'])) ? intval($_GET['entry']) : '';
  27. $backUrl = (preg_match('/\?/',$this->db->VARS['laz_url'])) ? '&entry=' . $entry : '?entry=' . $entry;
  28. $backUrl = (!empty($entry)) ? $this->db->VARS['laz_url'] . $backUrl : $this->db->VARS['laz_url'];
  29.  
  30. $likedID = (!empty($_GET['like'])) ? intval($_GET['like']) : 0;
  31.  
  32. if(!empty($_COOKIE['liked']))
  33. {
  34.   $cookieLikes = explode(',', $_COOKIE['liked']);
  35. }
  36. else
  37. {
  38.   $cookieLikes[] = 0;
  39. }
  40.  
  41. // Check that we have an id to like, the id is of a real post and that it has not already been liked by them.
  42. if(($likedID == 0) ||
  43.    (mysql_num_rows(mysql_query('SELECT id FROM ' . LAZ_TABLE_PREFIX . '_likes WHERE ip=`$userid` AND likeid=' . $likedID))) ||
  44.    (in_array($likedID, $cookieLikes)) ||
  45.    (!mysql_num_rows(mysql_query('SELECT id FROM ' . LAZ_TABLE_PREFIX . '_data WHERE id=' . $likedID)))
  46.   )
  47. {
  48.   header('Location: ' . $backUrl);  // Return to the guestbook
  49. }
  50. else
  51. {
  52.   if(isset($_SERVER['HTTP_X_FORWARDED_FOR']) && !empty($_SERVER['HTTP_X_FORWARDED_FOR']) && preg_match('/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/',$_SERVER['HTTP_X_FORWARDED_FOR']) && !preg_match('/^(192\.168\.)|(127\.0\.0\.1)|(10\.)|(169\.((1[2-9])|(2[0-9])|(30)|(31))\.)/', $_SERVER['HTTP_X_FORWARDED_FOR']))
  53.   {
  54.     $theirIP = addslashes($_SERVER['HTTP_X_FORWARDED_FOR']);
  55.   }
  56.   else
  57.   {
  58.     $theirIP = addslashes($_SERVER['REMOTE_ADDR']);
  59.   }
  60.  
  61.   if($sqlQuery = 'INSERT INTO ' . LAZ_TABLE_PREFIX . '_likes (ip,likedid,time) VALUES (\''. $theirIP . '\', \'' . $likedID  '\', \'' . time() . '\')')
  62.   {
  63.     $cookieValue = (!empty($_COOKIE['liked'])) ? $_COOKIE['liked'] . ',' . $likedID : $likedID;
  64.     setcookie('liked', $cookieValue, '1609480740');
  65.   }
  66.   header('Location: ' . $backUrl);  // Return to the guestbook
  67. }
  68. ?>
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement