Advertisement
Guest User

fhs.php

a guest
Dec 28th, 2011
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.21 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Allows users to to rate posts
  4.  *
  5.  * @copyright Copyright (C) 2009 crurf.info
  6.  * @license http://creativecommons.org/licenses/by-sa/3.0/ Creative Commons License version 3.0 or higher
  7.  * @package ajax_rating
  8.  */
  9.  
  10. function AR_loadRating($postid) {
  11.     global $forum_db;
  12.     $retvalue = array(
  13.         'postid' => $postid,
  14.         'yescnt' => 0,
  15.         'nocnt' => 0,
  16.         'rating' => 0,
  17.         'cnt' => 0,
  18.     );
  19.    
  20.     $query = array(
  21.       'SELECT' => '*',
  22.       'FROM' => 'postrating',
  23.       'WHERE' => 'postid = ' . $postid,
  24.     );
  25.    
  26.     $result = $forum_db->query_build($query);
  27.     if ((int)$forum_db->num_rows($result)) {
  28.         list($retvalue['postid'], $retvalue['yescnt'], $retvalue['nocnt'], $retvalue['rating'], $retvalue['cnt']) = $forum_db->fetch_row($result);
  29.     }
  30.    
  31.     return $retvalue;
  32. }
  33.  
  34. function AR_getRating($postid, &$opts, $is_guest) {
  35.     global $lang_ajax_rating, $forum_config, $forum_user;
  36.     if (!(int)$postid) return;
  37.    
  38.     $info = AR_loadRating($postid);
  39.     $active = (boolean)(!$forum_user['is_guest'] || ($forum_user['is_guest'] && (int)$forum_config['p_ajax_rating_allow_guest']));
  40.     $html = '<div class="post_rating">';
  41.     if ((int)$forum_config['p_ajax_rating_type']) {
  42.         $html .= '
  43.             <ul id="star' . $postid . '" class="star" ' . ($active ? 'onmousedown="star.update(event,this)" onmousemove="star.mouse(event,this)"' : 'onclick="alert(\'' . $lang_ajax_rating['Login to rate'] . '\');"') . ' title="' . $lang_ajax_rating['Rate This Post'] . '">
  44.                 <li id="starCur' . $postid . '" class="curr" title="' . (int)$info['rating'] . '" style="width: ' . (int)$info['rating'] . '%;"></li>
  45.             </ul>
  46.             <div id="starUser' . $postid . '" class="user">' . (int)$info['rating'] . '%</div>
  47.         ';
  48.     } else {
  49.         $html .= '<a class="rate_yes" title="' . $lang_ajax_rating['Approve'] . '" href="javascript: void(0);" ' . ($active ? 'onclick="AR_rate(this, ' . (int)$postid . ', 0, 1);"' : 'onclick="alert(\'' . $lang_ajax_rating['Login to rate'] . '\');"') . '>' . $info['yescnt'] . '</a>';
  50.         $html .= '<a class="rate_no" title="' . $lang_ajax_rating['Decline'] . '" href="javascript: void(0);" ' . ($active ? 'onclick="AR_rate(this, ' . (int)$postid . ', 0, -1);"' : 'onclick="alert(\'' . $lang_ajax_rating['Login to rate'] . '\');"') . '>' . $info['nocnt'] . '</a>';
  51.     }
  52.     $html .= '</div>';
  53.    
  54.     if (!is_array($opts)) $opts = array();
  55.     $opts['rating'] = $html;
  56. }
  57.  
  58.  function AR_processRequest($action) {
  59.     global $forum_db, $forum_config, $forum_user;
  60.     if ($action == 'ajax_rating') {
  61.         $postid = (int)$_POST['postid'];
  62.         $type = (int)$_POST['type'];
  63.         $rate = (float)$_POST['rate'];
  64.         $content = '{postid:' . $postid . '}';
  65.         $active = (boolean)(!$forum_user['is_guest'] || ($forum_user['is_guest'] && (int)$forum_config['p_ajax_rating_allow_guest']));
  66.        
  67.         if (!$postid || !$active) {
  68.             header('X-json: "' . md5($content) . '"');
  69.             echo $content;
  70.             exit;
  71.         }
  72.        
  73.         $to_cancel_vote = 0;
  74.         if (!(int)$forum_config['p_ajax_rating_allow_multiple']) {
  75.             $query = array(
  76.               'SELECT' => '*',
  77.               'FROM' => 'postrating_det',
  78.               'WHERE' => 'postid = ' . $postid . ' AND ip = \'' . $_SERVER['REMOTE_ADDR'] . '\'',
  79.             );
  80.            
  81.             $result = $forum_db->query_build($query);
  82.             $to_cancel_vote = (int)$forum_db->num_rows($result);
  83.         }
  84.        
  85.         if ($to_cancel_vote) {
  86.             header('X-json: "' . md5($content) . '"');
  87.             echo $content;
  88.             exit;
  89.         }  
  90.        
  91.         $query = array(
  92.           'SELECT' => '*',
  93.           'FROM' => 'postrating',
  94.           'WHERE' => 'postid = ' . $postid,
  95.         );
  96.        
  97.         $result = $forum_db->query_build($query);
  98.         $to_update = (int)$forum_db->num_rows($result);
  99.        
  100.         $info = AR_loadRating($postid);
  101.         if ($type == 0) {
  102.             $newcnt = ((int)$info[($rate > 0 ? 'yescnt' : 'nocnt')] + 1);
  103.             $newrate = (((int)$info['yescnt'] + ($rate > 0 ? 1 : 0)) * 100) / ((int)$info['cnt'] + 1);
  104.             $newrate = (float)round($newrate);
  105.            
  106.             // Yes/No rate
  107.             if ($to_update) {
  108.                 $forum_db->query('UPDATE ' . $forum_db->prefix . 'postrating
  109.                     SET ' . ($rate > 0 ? 'yescnt' : 'nocnt') . ' = ' . $newcnt . ',
  110.                         rating = ' . $newrate . ', cnt = (cnt + 1) WHERE postid = ' . $postid
  111.                 );
  112.             } else {
  113.                 $forum_db->query('INSERT INTO ' . $forum_db->prefix . 'postrating
  114.                     (postid, cnt, ' . ($rate > 0 ? 'yescnt' : 'nocnt') . ', rating)
  115.                     VALUES (' . $postid . ', 1, ' . $newcnt . ', ' . $newrate . ')'
  116.                 );         
  117.             }
  118.            
  119.             $forum_db->query('INSERT INTO ' . $forum_db->prefix . 'postrating_det
  120.                 (postid, ip, rating, tstamp) VALUES (' . $postid . ', \'' . $_SERVER['REMOTE_ADDR'] . '\', ' . ($rate > 0 ? '1' : '0') . ', ' . mktime() . ')'
  121.             );
  122.            
  123.             $info = AR_loadRating($postid);
  124.             $content = '{postid:' . $postid . ', newvalue:' . $info[($rate > 0 ? 'yescnt' : 'nocnt')] . '}';
  125.         } elseif ($type == 1) {
  126.             $old_yescnt = ceil(((int)$info['cnt'] * (float)$info['rating']) / 100);
  127.             $old_nocnt = (int)$info['cnt'] - $old_yescnt;
  128.             $new_yescnt = ceil(5 * $rate);
  129.             $new_nocnt = 5 - $new_yescnt;
  130.             $newrate = (float)((($old_yescnt + $new_yescnt) * 100) / ((int)$info['cnt'] + 5));
  131.             if ($to_update) {
  132.                 $forum_db->query('UPDATE ' . $forum_db->prefix . 'postrating
  133.                     SET yescnt = ' . ($old_yescnt + $new_yescnt) . ', nocnt = ' . ($old_nocnt + $new_nocnt) . ',
  134.                         rating = ' . $newrate . ', cnt = (cnt + 5) WHERE postid = ' . $postid
  135.                 );
  136.             } else {
  137.                 $forum_db->query('INSERT INTO ' . $forum_db->prefix . 'postrating
  138.                     (postid, cnt, yescnt, nocnt, rating)
  139.                     VALUES (' . $postid . ', 5, ' . ($old_yescnt + $new_yescnt) . ', ' . ($old_nocnt + $new_nocnt) . ', ' . $newrate . ')'
  140.                 );         
  141.             }
  142.            
  143.             $posttime = mktime();
  144.             for ($i = 0; $i < $new_yescnt; $i ++) {
  145.                 $forum_db->query('INSERT INTO ' . $forum_db->prefix . 'postrating_det
  146.                     (postid, ip, rating, tstamp)
  147.                     VALUES (' . $postid . ', \'' . $_SERVER['REMOTE_ADDR'] . '\', 1, ' . $posttime . ')'
  148.                 );
  149.             }
  150.             for ($i = 0; $i < $new_nocnt; $i ++) {
  151.                 $forum_db->query('INSERT INTO ' . $forum_db->prefix . 'postrating_det
  152.                     (postid, ip, rating, tstamp)
  153.                     VALUES (' . $postid . ', \'' . $_SERVER['REMOTE_ADDR'] . '\', 0, ' . $posttime . ')'
  154.                 );
  155.             }
  156.            
  157.             $info = AR_loadRating($postid);
  158.             $content = '{postid:' . $postid . ', newvalue:' . $info['rating'] . '}';
  159.         }
  160.        
  161.         header('X-json: "' . md5($content) . '"');
  162.         echo $content;
  163.         exit;
  164.     }
  165.  }
  166.  
  167. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement