registry = ipsRegistry::instance(); $this->rep_system_on = ipsRegistry::$settings['reputation_enabled']; } /** * ANDROID FIX: Функция перекодировки из кодов utf8 * * @access public * @param string $str Строка кодов utf8 * @return string $str Читабельная строка */ public function unicode_escape($str) { $escape_table = array( '%20' => ' ', '%21' => '!', '%2C' => ',', '%3A' => ':', '%3B' => ';', '%u0410' => 'А', '%u0411' => 'Б', '%u0412' => 'В', '%u0413' => 'Г', '%u0414' => 'Д', '%u0415' => 'Е', '%u0401' => 'Ё', '%u0416' => 'Ж', '%u0417' => 'З', '%u0418' => 'И', '%u0419' => 'Й', '%u041A' => 'К', '%u041B' => 'Л', '%u041C' => 'М', '%u041D' => 'Н', '%u041E' => 'О', '%u041F' => 'П', '%u0420' => 'Р', '%u0421' => 'С', '%u0422' => 'Т', '%u0423' => 'У', '%u0424' => 'Ф', '%u0425' => 'Х', '%u0426' => 'Ц', '%u0427' => 'Ч', '%u0428' => 'Ш', '%u0429' => 'Щ', '%u042A' => 'Ъ', '%u042B' => 'Ы', '%u042C' => 'Ь', '%u042D' => 'Э', '%u042E' => 'Ю', '%u042F' => 'Я', '%u0430' => 'а', '%u0431' => 'б', '%u0432' => 'в', '%u0433' => 'г', '%u0434' => 'д', '%u0435' => 'е', '%u0451' => 'ё', '%u0436' => 'ж', '%u0437' => 'з', '%u0438' => 'и', '%u0439' => 'й', '%u043A' => 'к', '%u043B' => 'л', '%u043C' => 'м', '%u043D' => 'н', '%u043E' => 'о', '%u043F' => 'п', '%u0440' => 'р', '%u0441' => 'с', '%u0442' => 'т', '%u0443' => 'у', '%u0444' => 'ф', '%u0445' => 'х', '%u0446' => 'ц', '%u0447' => 'ч', '%u0448' => 'ш', '%u0449' => 'щ', '%u044A' => 'ъ', '%u044B' => 'ы', '%u044C' => 'ь', '%u044D' => 'э', '%u044E' => 'ю', '%u044F' => 'я', //ukrainian char '%u0456' => 'і', '%u0406' => 'І', '%u0457' => 'ї', '%u0407' => 'Ї', '%u0454' => 'є', '%u0404' => 'Є', ); return strtr($str, $escape_table); } /** * Adds a rating to the index and updates caches * * @access public * @param string $type Type of content, ex; Post * @param integer $type_id ID of the type, ex: pid * @param integer $rating Either 1 or -1 * @param string $message Message associated with this rating * @param integer $member_id Id of the owner of the content being rated * @param string [$app] App for this content, by default the current application * @return bool */ public function addRate( $type, $type_id, $rating, $message='', $member_id=0, $app='' ) { //ANDROID FIX START $message = $this->unicode_escape($message); //ANDROID FIX STOP ipsRegistry::instance()->getClass('class_localization')->loadLanguageFile( array( 'public_global' ), 'core' ); /* Online? */ if( ! $this->rep_system_on ) { $this->error_message = ipsRegistry::instance()->getClass( 'class_localization' )->words['reputation_offline']; return false; } /* INIT */ $app = ( $app ) ? $app : ipsRegistry::$current_application; $rating = intval( $rating ); if( ! ipsRegistry::member()->getProperty( 'member_id' ) ) { $this->error_message = ipsRegistry::instance()->getClass( 'class_localization' )->words['reputation_guest']; return false; } if( $rating != -1 && $rating != 1 ) { $this->error_message = ipsRegistry::instance()->getClass( 'class_localization' )->words['reputation_invalid']; return false; } if( $rating == -1 AND ipsRegistry::$settings['reputation_point_types'] == 'positive' ) { $this->error_message = ipsRegistry::instance()->getClass( 'class_localization' )->words['reputation_invalid']; return false; } if( $rating == 1 AND ipsRegistry::$settings['reputation_point_types'] == 'negative' ) { $this->error_message = ipsRegistry::instance()->getClass( 'class_localization' )->words['reputation_invalid']; return false; } /* Check the point types */ if( $rating == -1 && ipsRegistry::$settings['reputation_point_types'] == 'postive' ) { $this->error_message = ipsRegistry::instance()->getClass( 'class_localization' )->words['reputation_invalid']; return false; } if( $rating == 1 && ipsRegistry::$settings['reputation_point_types'] == 'negative' ) { $this->error_message = ipsRegistry::instance()->getClass( 'class_localization' )->words['reputation_invalid']; return false; } /* Day Cutoff */ $day_cutoff = time() - 86400; /* Check Max Positive Votes */ if( ipsRegistry::member()->getProperty( 'g_rep_max_positive' ) && $rating == 1 ) { $total = ipsRegistry::DB()->buildAndFetch( array( 'select' => 'count(*) as votes', 'from' => 'reputation_index', 'where' => 'member_id=' . ipsRegistry::member()->getProperty( 'member_id' ) . ' AND rep_rating=1 AND rep_date > ' . $day_cutoff ) ); if( $total['votes'] >= ipsRegistry::member()->getProperty( 'g_rep_max_positive' ) ) { $this->error_message = ipsRegistry::instance()->getClass( 'class_localization' )->words['reputation_quota_pos']; return false; } } /* Check Max Negative Votes */ if( ipsRegistry::member()->getProperty( 'g_rep_max_negative' ) && $rating == -1 ) { $total = ipsRegistry::DB()->buildAndFetch( array( 'select' => 'count(*) as votes', 'from' => 'reputation_index', 'where' => 'member_id=' . ipsRegistry::member()->getProperty( 'member_id' ) . ' AND rep_rating=-1 AND rep_date > ' . $day_cutoff ) ); if( $total['votes'] >= ipsRegistry::member()->getProperty( 'g_rep_max_negative' ) ) { $this->error_message = ipsRegistry::instance()->getClass( 'class_localization' )->words['reputation_quota_neg']; return false; } } /* If no member id was passted in, we have to query it using the config file */ if( ! $member_id ) { /* Reputation Config */ if( file_exists( IPSLib::getAppDir( $app ) . '/extensions/reputation.php' ) ) { require( IPSLib::getAppDir( $app ) . '/extensions/reputation.php' ); } else { $this->error_message = ipsRegistry::instance()->getClass( 'class_localization' )->words['reputation_config']; return false; } if( ! $rep_author_config[$type]['column'] || ! $rep_author_config[$type]['table'] ) { $this->error_message = ipsRegistry::instance()->getClass( 'class_localization' )->words['reputation_config']; return false; } /* Query the content author */ $content_author = ipsRegistry::DB()->buildAndFetch( array( 'select' => "{$rep_author_config[$type]['column']} as id", 'from' => $rep_author_config[$type]['table'], 'where' => "{$type}={$type_id}" ) ); $member_id = $content_author['id']; } if( ! ipsRegistry::$settings['reputation_can_self_vote'] && $member_id == ipsRegistry::member()->getProperty( 'member_id' ) ) { $this->error_message = ipsRegistry::instance()->getClass( 'class_localization' )->words['reputation_yourown']; return false; } /* Query the member group */ if( ipsRegistry::$settings['reputation_protected_groups'] ) { $member_group = ipsRegistry::DB()->buildAndFetch( array( 'select' => 'member_group_id', 'from' => 'members', 'where' => "member_id={$member_id}" ) ); if( in_array( $member_group['member_group_id'], explode( ',', ipsRegistry::$settings['reputation_protected_groups'] ) ) ) { $this->error_message = ipsRegistry::instance()->getClass( 'class_localization' )->words['reputation_protected']; return false; } } $message = IPSText::truncate( $message, 199 ); /* Build the insert array */ $db_insert = array( 'member_id' => ipsRegistry::member()->getProperty( 'member_id' ), 'app' => $app, 'type' => $type, 'type_id' => $type_id, 'misc' => '', 'rep_date' => time(), 'rep_msg' => $message, 'rep_rating' => $rating, 'rep_for' => $member_id ); /* Check for existing rating */ $current_rating = ipsRegistry::DB()->buildAndFetch( array( 'select' => '*', 'from' => 'reputation_index', 'where' => "app='{$app}' AND type='{$type}' AND type_id={$type_id} AND member_id=".ipsRegistry::member()->getProperty( 'member_id' ), ) ); /* Insert */ if( $current_rating ) { ipsRegistry::DB()->update( 'reputation_index', $db_insert, "app='{$app}' AND type='{$type}' AND type_id={$type_id} AND member_id=".ipsRegistry::member()->getProperty( 'member_id' ) ); } else { ipsRegistry::DB()->insert( 'reputation_index', $db_insert ); } /* Update type cache */ $this->_updateTypeCache( $app, $type, $type_id, $rating, $current_rating ); /* Get authors current rep */ $author_points = ipsRegistry::DB()->buildAndFetch( array( 'select' => 'pp_reputation_points', 'from' => 'profile_portal', 'where' => "pp_member_id={$member_id}" ) ); /* Figure out new rep */ if( $current_rating['rep_rating'] == -1 ) { $author_points['pp_reputation_points'] += 1; } else if( $current_rating['rep_rating'] == 1 ) { $author_points['pp_reputation_points'] -= 1; } $author_points['pp_reputation_points'] += $rating; /* (SOS30) PM Notification on Reputation v1.0.1 */ if ( $app == 'forums' AND $type == 'pid' ) { $topic = ipsRegistry::DB()->buildAndFetch( array( 'select' => 'p.pid', 'from' => array( 'posts' => 'p' ), 'add_join' => array( array( 'select' => 't.tid, t.title, t.forum_id', 'from' => array( 'topics' => 't' ), 'where' => 't.tid=p.topic_id', 'type' => 'left', ) ), 'where' => 'p.pid = '.$type_id ) ); $grupo = IPSMember::load( $member_id, 'all' ); if ( in_array( $topic['forum_id'], explode( ',', ipsRegistry::$settings['sos_mprep_foruns'] ) ) AND in_array( $grupo['member_group_id'], explode( ',', ipsRegistry::$settings['sos_mprep_grupos'] ) ) ) { if ( $rating == ipsRegistry::$settings['sos_mprep_tipo'] OR ipsRegistry::$settings['sos_mprep_tipo'] == 0 ) { $titulo = stripslashes( ipsRegistry::$settings['sos_mprep_titulo'] ); $mensagem = IPSText::getTextClass( 'editor' )->processRawPost( ipsRegistry::$settings['sos_mprep_mensagem'] ); IPSText::getTextClass('bbcode')->parse_html = 0; IPSText::getTextClass('bbcode')->parse_nl2br = 1; IPSText::getTextClass('bbcode')->parse_smilies = 1; IPSText::getTextClass('bbcode')->parse_bbcode = 1; IPSText::getTextClass('bbcode')->parsing_section = 'pms'; $mensagem = IPSText::getTextClass('bbcode')->preEditParse( $mensagem ); $template = IPSText::getTextClass('bbcode')->preEditParse( ipsRegistry::$settings['sos_mprep_mensagem'] ); require_once( IPSLib::getAppDir( 'members' ) . '/sources/classes/messaging/messengerFunctions.php' ); $messengerFunctions = new messengerFunctions( $this->registry ); $topico = $topic['title']; $link = "[url='".ipsRegistry::$settings['board_url']."/index.php?showtopic=".$topic['tid']."&view=findpost&p=".$topic['pid']."']".ipsRegistry::$settings['board_url']."/index.php?showtopic=".$topic['tid']."&view=findpost&p=".$topic['pid']."[/url]"; $reputacao = $rating == 1 ? '[color="#2E8B57"][b]Повысили[/b][/color]' : '[color="#FF0000"][b]Понизили[/b][/color]'; $find = array( "{topic}", "{reputation}", "{link}" ); $replace = array( $topico, $reputacao, $link ); $mensagem = str_replace( $find, $replace, $template ); try { $messengerFunctions->sendNewPersonalTopic( $member_id, ipsRegistry::$settings['sos_mprep_autormp'], array(), $titulo, IPSText::getTextClass( 'editor' )->method == 'rte' ? nl2br($mensagem) : $mensagem, array( 'origMsgID' => 0, 'fromMsgID' => 0, 'postKey' => md5(microtime()), 'trackMsg' => 0, 'addToSentFolder' => 0, 'hideCCUser' => 0, 'forcePm' => 1, 'isSystem' => TRUE, ) ); } catch( Exception $error ) { $msg = $error->getMessage(); $toMember = IPSMember::load( $member_id, 'core' ); if ( strstr( $msg, 'BBCODE_' ) ) { $msg = str_replace( 'BBCODE_', '', $msg ); $this->registry->output->showError( $msg, 10252 ); } else if ( isset($this->lang->words[ 'err_' . $msg ]) ) { $this->lang->words[ 'err_' . $msg ] = $this->lang->words[ 'err_' . $msg ]; $this->lang->words[ 'err_' . $msg ] = str_replace( '#NAMES#' , implode( ",", $messengerFunctions->exceptionData ), $this->lang->words[ 'err_' . $msg ] ); $this->lang->words[ 'err_' . $msg ] = str_replace( '#TONAME#' , $toMember['members_display_name'] , $this->lang->words[ 'err_' . $msg ] ); $this->lang->words[ 'err_' . $msg ] = str_replace( '#FROMNAME#', $this->memberData['members_display_name'], $this->lang->words[ 'err_' . $msg ] ); $this->registry->output->showError( 'err_' . $msg, 10253 ); } else if( $msg != 'CANT_SEND_TO_SELF' ) { $_msgString = $this->lang->words['err_UNKNOWN'] . ' ' . $msg; $this->registry->output->showError( $_msgString, 10254 ); } } } } } /* (SOS30) PM Notification on Reputation v1.0.1 */ ipsRegistry::DB()->update( 'profile_portal', array( 'pp_reputation_points' => $author_points['pp_reputation_points'] ), "pp_member_id={$member_id}" ); return true; } /** * Handles updating and creating new caches * * @access private * @param string $app App for this content * @param string $type Type of content, ex; Post * @param integer $type_id ID of the type, ex: pid * @param integer $rating Either 1 or -1 * @param array $old_record If this is a revote, then this array contains the previous vote * @return void */ private function _updateTypeCache( $app, $type, $type_id, $rating, $old_record ) { /* Update type cache */ $type_cache = ipsRegistry::DB()->buildAndFetch( array( 'select' => '*', 'from' => 'reputation_cache', 'where' => "app='{$app}' AND type='$type' AND type_id='$type_id'", ) ); /* Update cache */ if( $type_cache ) { /* Previous rating? */ if( $old_record['rep_rating'] == -1 ) { $type_cache['rep_points'] += 1; } else if( $old_record['rep_rating'] == 1 ) { $type_cache['rep_points'] -= 1; } ipsRegistry::DB()->update( 'reputation_cache', array( 'rep_points' => $type_cache['rep_points'] + $rating ), "app='{$app}' AND type='$type' AND type_id='$type_id'" ); } /* Insert Cache */ else { ipsRegistry::DB()->insert( 'reputation_cache', array( 'app' => $app, 'type' => $type, 'type_id' => $type_id, 'rep_points' => $rating ) ); } } }