Page MenuHomeIn-Portal Phabricator

in-portal
No OneTemporary

File Metadata

Created
Sat, Feb 1, 6:00 PM

in-portal

Index: branches/5.1.x/core/units/helpers/spam_helper.php
===================================================================
--- branches/5.1.x/core/units/helpers/spam_helper.php (revision 13434)
+++ branches/5.1.x/core/units/helpers/spam_helper.php (revision 13435)
@@ -1,144 +1,166 @@
<?php
/**
* @version $Id$
* @package In-Portal
* @copyright Copyright (C) 1997 - 2009 Intechnic. All rights reserved.
* @license GNU/GPL
* In-Portal is Open Source software.
* This means that this software may have been modified pursuant
* the GNU General Public License, and as distributed it includes
* or is derivative of works licensed under the GNU General Public License
* or other free or open source software licenses.
* See http://www.in-portal.org/license for copyright notices and details.
*/
defined('FULL_PATH') or die('restricted access!');
class SpamHelper extends kHelper {
/**
* Table name where spam control information is keeped
*
* @var string
*/
var $TableName;
/**
* RecourceId field of current item
*
* @var int
*/
var $ResourceId = 0;
/**
+ * ResourceId from all items in list
+ *
+ * @var Array
+ */
+ var $ListResourceIDs = Array ();
+
+ /**
* Type of information to put into spam control
*
* @var string
*/
var $DataType = '';
/**
* Default spam control record expiration
*
* @var int
*/
var $Expiration = 0;
+ var $ExpirationCache = Array ();
+
function SpamHelper()
{
parent::kHelper();
$this->TableName = TABLE_PREFIX.'SpamControl';
}
/**
* Initializes helper for concrete item
*
* @param int $resource_id
* @param string $data_type
* @param int $expiration
*/
- function InitHelper($resource_id, $data_type, $expiration)
+ function InitHelper($resource_id, $data_type, $expiration, $list_resource_ids = Array ())
{
- $this->ResourceId = $resource_id;
$this->DataType = $data_type;
+ $this->ResourceId = $resource_id;
+ $this->ListResourceIDs = $list_resource_ids ? $list_resource_ids : Array ($resource_id);
if (preg_match('/(.*):(.*)/', $expiration, $regs)) {
$delay_value = $this->Application->ConfigValue($regs[1]);
$delay_interval = $this->Application->ConfigValue($regs[2]);
$expiration = $delay_value * $delay_interval;
}
$this->Expiration = adodb_mktime() + $expiration;
}
/**
* Returns WHERE clause that identified each spam control record
*
* @param bool $as_array return result as array, not string
*
* @return string
*/
function GetKeyClause($as_array = false)
{
$user_id = $this->Application->RecallVar('user_id');
if ($user_id == 0) {
$user_id = -2;
}
$keys = Array (
- 'ItemResourceId' => $this->ResourceId,
'IPaddress' => $_SERVER['REMOTE_ADDR'],
'PortalUserId' => $user_id,
'DataType' => $this->DataType,
);
if ($as_array) {
+ $keys['ItemResourceId'] = $this->ResourceId;
+
return $keys;
}
$ret = '';
foreach ($keys as $field_name => $field_value) {
- $ret .= '('.$field_name.' = '.$this->Conn->qstr($field_value).') AND ';
+ $ret .= '(' . $field_name . ' = ' . $this->Conn->qstr($field_value) . ') AND ';
}
- return substr($ret, 0, -5);
+ $ret .= '(ItemResourceId IN (' . implode(',', $this->ListResourceIDs) . '))';
+
+ return $ret;
}
/**
* Allows to add current item in spam control
*
*/
function AddToSpamControl()
{
$fields_hash = $this->GetKeyClause(true);
$fields_hash['Expire'] = $this->Expiration;
$this->Conn->doInsert($fields_hash, $this->TableName);
+
+ if (!array_key_exists($this->DataType, $this->ExpirationCache)) {
+ $this->ExpirationCache[$this->DataType][$this->ResourceId] = $this->Expiration;
+ }
}
/**
* Allows to check if current item is in spam control
*
* @return bool
*/
function InSpamControl()
{
- $key_clause = $this->GetKeyClause();
+ if (!array_key_exists($this->DataType, $this->ExpirationCache)) {
+ $key_clause = $this->GetKeyClause();
+
+ $sql = 'SELECT Expire, ItemResourceId
+ FROM '.$this->TableName.'
+ WHERE '.$key_clause;
+ $this->ExpirationCache[$this->DataType] = $this->Conn->GetCol($sql, 'ItemResourceId');
+ }
- $sql = 'SELECT Expire
- FROM '.$this->TableName.'
- WHERE '.$key_clause;
- $expires = $this->Conn->GetOne($sql);
+ $cache =& $this->ExpirationCache[$this->DataType];
+ $expires = array_key_exists($this->ResourceId, $cache) ? $cache[$this->ResourceId] : false;
if ($expires && $expires < adodb_mktime()) {
// spam control record is expired
$sql = 'DELETE FROM '.$this->TableName.'
WHERE '.$key_clause;
$this->Conn->Query($sql);
return false;
}
return $expires ? true : false;
}
}
\ No newline at end of file
Index: branches/5.1.x/core/units/helpers/rating_helper.php
===================================================================
--- branches/5.1.x/core/units/helpers/rating_helper.php (revision 13434)
+++ branches/5.1.x/core/units/helpers/rating_helper.php (revision 13435)
@@ -1,195 +1,195 @@
<?php
/**
* @version $Id$
* @package In-Portal
* @copyright Copyright (C) 1997 - 2009 Intechnic. All rights reserved.
* @license GNU/GPL
* In-Portal is Open Source software.
* This means that this software may have been modified pursuant
* the GNU General Public License, and as distributed it includes
* or is derivative of works licensed under the GNU General Public License
* or other free or open source software licenses.
* See http://www.in-portal.org/license for copyright notices and details.
*/
defined('FULL_PATH') or die('restricted access!');
class RatingHelper extends kHelper {
/**
* One star width/height in pixels
*
* @var int
*/
var $ratingUnitWidth = 25;
var $ratingSmallUnitWidth = 20;
/**
* Maximal star count
*
* @var int
*/
var $ratingMaximal = 5;
var $_phrases = Array (
'current_rating' => 'lu_CurrentRating',
'vote_title' => 'lu_VoteTitle',
'vote_count' => 'lu_VoteCount',
'invalid_rating' => 'lu_InvalidRating',
'already_voted' => 'lu_AlreadyVoted',
'thanks_for_voting' => 'lu_ThanksForVoting',
);
/**
* Draws rating bar for a given category item
*
* @param kDBItem $object
* @param bool $show_div
* @param string $additional_msg
* @return string
*/
function ratingBar(&$object, $show_div = true, $additional_msg = '', $additional_style = '')
{
$perm_prefix = $this->Application->getUnitOption($object->Prefix, 'PermItemPrefix');
$static = !$this->Application->CheckPermission($perm_prefix . '.RATE', 0, $object->GetDBField('CategoryId'));
$total_votes = $object->GetDBField('CachedVotesQty');
$total_rating = $object->GetDBField('CachedRating') * $total_votes;
$spam_helper =& $this->Application->recallObject('SpamHelper');
/* @var $spam_helper SpamHelper */
$config_mapping = $this->Application->getUnitOption($object->Prefix, 'ConfigMapping');
$review_settings = $config_mapping['RatingDelayValue'].':'.$config_mapping['RatingDelayInterval'];
- $spam_helper->InitHelper($object->GetDBField('ResourceId'), 'Rating', $review_settings);
+ $spam_helper->InitHelper($object->GetDBField('ResourceId'), 'Rating', $review_settings, $object->GetCol('ResourceId'));
$user_voted = $spam_helper->InSpamControl();
// now draw the rating bar
$unit_selected_width = $additional_style? $this->ratingSmallUnitWidth : $this->ratingUnitWidth;
$rating_width = $total_votes ? @number_format($total_rating / $total_votes, 2) * $unit_selected_width : 0;
$rating1 = $total_votes ? @number_format($total_rating / $total_votes, 1) : 0;
$rating2 = $total_votes ? @number_format($total_rating / $total_votes, 2) : 0;
$rater = '<span class="inline-rating">
<ul class="star-rating '.$additional_style.'" style="width: ' . $unit_selected_width * $this->ratingMaximal . 'px;">
<li class="current-rating" style="width: ' . $rating_width . 'px;">' . $this->_replaceInPhrase('current_rating', Array ('<strong>' . $rating2 . '</strong>', $this->ratingMaximal)) . '</li>'."\n";;
if (!$static && !$user_voted) {
// allow to set rating when not static and user not voted before
for ($ncount = 1; $ncount <= $this->ratingMaximal; $ncount++) {
$rater .= '<li><a href="#vote-' . $ncount . '" onclick="aRatingManager.makeVote(' . $ncount . ', \'' . $object->Prefix . '\', ' . $object->GetID() . ', \''.$additional_style.'\'); return false;" title="' . $this->_replaceInPhrase('vote_title', Array ($ncount, $this->ratingMaximal)) . '" class="r' . $ncount . '-unit rater" rel="nofollow">' . $ncount . '</a></li>'."\n";
}
}
$msg_class = Array ();
if ($static) {
$msg_class[] = 'static';
}
if ($user_voted) {
$msg_class[] = 'voted';
}
$rater .= ' </ul></span>';
// this part is disabled for now, will be addressed once properly review
// $rater .= ' <p class="' . implode(' ', $msg_class) . '">' .
$this->_replaceInPhrase('vote_title', Array('<strong>'.$rating1.'</strong>', $this->ratingMaximal)) . ' ('. $this->_replaceInPhrase('vote_count', Array($total_votes)) . ') </p>';
$rater .= '&nbsp;<span class="' . implode(' ', $msg_class) . '">'.$additional_msg.'</span>';
if ($show_div) {
// adds div around rating stars (when drawing rating first time)
$rater = '<div class="inline-rating" id="page_rating_' . $object->GetID() . '">' . $rater . '</div>';
}
return $rater;
}
/**
* Saves user's vote, when allowed
*
* @param kDBItem $object
* @return string
*/
function makeVote(&$object)
{
$spam_helper =& $this->Application->recallObject('SpamHelper');
/* @var $spam_helper SpamHelper */
$config_mapping = $this->Application->getUnitOption($object->Prefix, 'ConfigMapping');
$review_settings = $config_mapping['RatingDelayValue'].':'.$config_mapping['RatingDelayInterval'];
- $spam_helper->InitHelper($object->GetDBField('ResourceId'), 'Rating', $review_settings);
+ $spam_helper->InitHelper($object->GetDBField('ResourceId'), 'Rating', $review_settings, $object->GetCol('ResourceId'));
if (!$object->isLoaded() || $spam_helper->InSpamControl()) {
return '@err:' . $this->_replaceInPhrase('already_voted');
}
$perm_prefix = $this->Application->getUnitOption($object->Prefix, 'PermItemPrefix');
$can_rate = $this->Application->CheckPermission($perm_prefix . '.RATE', 0, $object->GetDBField('CategoryId'));
$rating = (int)$this->Application->GetVar('rating'); // not numeric rating is from GoogleBot :(
$additional_style = $this->Application->GetVar('size');
if (($rating <= 0) || ($rating > $this->ratingMaximal) || !$can_rate) {
return '@err:' . $this->_replaceInPhrase('invalid_rating');
}
// save current rating
$fields_hash = Array (
'ItemId' => $object->GetID(),
'RatingValue' => $rating,
'IPAddress' => $_SERVER['REMOTE_ADDR'],
'CreatedOn' => adodb_mktime(),
);
$this->Conn->doInsert($fields_hash, TABLE_PREFIX.'ItemRating');
// recalculate average rating
$votes_count = $object->GetDBField('CachedVotesQty');
$avg_rating = $object->GetDBField('CachedRating');
$avg_rating = round((($votes_count * $avg_rating) + $rating) / ($votes_count + 1), 2);
$object->SetDBField('CachedRating', "$avg_rating");
$object->Update();
$sql = 'UPDATE '.$object->TableName.'
SET CachedVotesQty = CachedVotesQty + 1
WHERE '.$object->IDField.' = '.$object->GetID();
$this->Conn->Query($sql);
$object->SetDBField('CachedVotesQty', $object->GetDBField('CachedVotesQty') + 1); // for using in template
// prevent user from voting too quickly
$spam_helper->AddToSpamControl();
return $this->ratingBar($object, false, '<span class="thanks">' . $this->_replaceInPhrase('thanks_for_voting') . '</span>', $additional_style);
}
/*function purgeVotes()
{
$expired = adodb_mktime() - 86400 * $this->Application->ConfigValue('Timeout_Rating'); // 3600
$sql = 'DELETE FROM ' . TABLE_PREFIX . 'ItemRating
WHERE CreatedOn < ' . $expired;
$this->Conn->Query($sql);
}*/
/**
* Performs sprintf on phrase translation using given variables
*
* @param string $phrase
* @param Array $arguments
* @return string
*/
function _replaceInPhrase($phrase, $arguments = Array ())
{
$value = $this->Application->Phrase($this->_phrases[$phrase]);
if ($arguments) {
return vsprintf($value, $arguments);
}
return $value;
}
}
\ No newline at end of file
Index: branches/5.1.x/core/units/reviews/reviews_tag_processor.php
===================================================================
--- branches/5.1.x/core/units/reviews/reviews_tag_processor.php (revision 13434)
+++ branches/5.1.x/core/units/reviews/reviews_tag_processor.php (revision 13435)
@@ -1,155 +1,155 @@
<?php
/**
* @version $Id$
* @package In-Portal
* @copyright Copyright (C) 1997 - 2009 Intechnic. All rights reserved.
* @license GNU/GPL
* In-Portal is Open Source software.
* This means that this software may have been modified pursuant
* the GNU General Public License, and as distributed it includes
* or is derivative of works licensed under the GNU General Public License
* or other free or open source software licenses.
* See http://www.in-portal.org/license for copyright notices and details.
*/
defined('FULL_PATH') or die('restricted access!');
class ReviewsTagProcessor extends kDBTagProcessor
{
/**
* Returns a link for editing product
*
* @param Array $params
* @return string
*/
function ItemEditLink($params)
{
$object =& $this->getObject();
/* @var $object kDBList */
$item_prefix = $this->Application->findModule('Name', $object->GetDBField('Module'), 'Var');
$edit_template = $this->Application->getUnitOption($item_prefix, 'AdminTemplatePath') . '/' . $this->Application->getUnitOption($item_prefix, 'AdminTemplatePrefix') . 'edit';
$url_params = Array (
'm_opener' => 'd',
$item_prefix.'_mode' => 't',
$item_prefix.'_event' => 'OnEdit',
$item_prefix.'_id' => $object->GetDBField('CatalogItemId'),
'm_cat_id' => $object->GetDBField('CatalogItemCategory'),
'pass' => 'all,'.$item_prefix,
'no_pass_through' => 1,
);
return $this->Application->HREF($edit_template,'', $url_params);
}
/**
* Get's reuested field value
*
* @param Array $params
* @return string
* @access public
*/
function Field($params)
{
$field = $this->SelectParam($params, 'name,field');
$object =& $this->Application->recallObject($this->getPrefixSpecial(),$this->Prefix, $params);
if ($field == 'ReviewText') {
if ($object->GetDBField('TextFormat') == 1) {
$params['no_special'] = 'no_special';
}
else {
unset($params['no_special']);
}
}
return parent::Field($params);
}
function AlreadyReviewed($params)
{
$parent_prefix = $this->Application->getUnitOption($this->Prefix, 'ParentPrefix');
$main_object =& $this->Application->recallObject($parent_prefix);
/* @var $main_object kCatDBItem */
$spam_helper =& $this->Application->recallObject('SpamHelper');
/* @var $spam_helper SpamHelper */
- $spam_helper->InitHelper($main_object->GetDBField('ResourceId'), 'Review', 0);
+ $spam_helper->InitHelper($main_object->GetDBField('ResourceId'), 'Review', 0, $main_object->GetCol('ResourceId'));
return $spam_helper->InSpamControl();
}
function HasError($params)
{
$object =& $this->Application->recallObject($this->getPrefixSpecial(), $this->Prefix, $params);
if (method_exists($object, 'GetErrorMsg')) {
return parent::HasError($params);
}
else {
return 0;
}
}
/**
* Preserve main item id in subitem pagination url
*
* @param Array $params
* @return string
*/
function PageLink($params)
{
$object =& $this->getObject($params);
/* @var kDBList */
$parent_info = $object->getLinkedInfo();
if ($parent_info['ParentId'] > 0) {
$params['pass'] = 'm,'.$this->getPrefixSpecial().','.$parent_info['ParentPrefix'];
}
return parent::PageLink($params);
}
function InitCatalogTab($params)
{
$tab_params['mode'] = $this->Application->GetVar('tm'); // single/multi selection possible
$tab_params['special'] = $this->Application->GetVar('ts'); // use special for this tab
$tab_params['dependant'] = $this->Application->GetVar('td'); // is grid dependant on categories grid
// set default params (same as in catalog)
if ($tab_params['mode'] === false) $tab_params['mode'] = 'multi';
if ($tab_params['special'] === false) $tab_params['special'] = '';
if ($tab_params['dependant'] === false) $tab_params['dependant'] = 'yes';
// pass params to block with tab content
$params['name'] = $params['render_as'];
$params['prefix'] = trim($this->Prefix.'.'.($tab_params['special'] ? $tab_params['special'] : $this->Special), '.');
$params['cat_prefix'] = trim('c.'.($tab_params['special'] ? $tab_params['special'] : $this->Special), '.');
$params['tab_mode'] = $tab_params['mode'];
$params['grid_name'] = ($tab_params['mode'] == 'multi') ? $params['default_grid'] : $params['radio_grid'];
$params['tab_dependant'] = $tab_params['dependant'];
$params['show_category'] = $tab_params['special'] == 'showall' ? 1 : 0; // this is advanced view -> show category name
$params['tab_name'] = $this->Application->GetVar('tab_name');
return $this->Application->ParseBlock($params, 1);
}
/**
* Returns reviews count for each item type (in "Reviews" section)
*
* @param Array $params
* @return string
*/
function CatalogItemCount($params)
{
$params['skip_quering'] = true;
$object =& $this->GetList($params);
if (!$object->Counted) {
$object->CountRecs();
}
return $object->NoFilterCount != $object->RecordsCount ? $object->RecordsCount.' / '.$object->NoFilterCount : $object->RecordsCount;
}
}
\ No newline at end of file

Event Timeline