<?php
/*
Related Topics Mod for ExBB FM 1.0 RC2
Copyright (c) 2008 - 2010 by Yuri Antonov aka yura3d
http://www.exbb.org/
ICQ: 313321962
*/
defined('IN_EXBB')
or die('Emo sucks;)');
define('CONFIG', 'modules/relatedtop/data/config.php');
$fm->_LoadModuleLang('relatedtop');
switch ($fm->_String('action')) {
case 'get':
get_related();
break;
}
function backend_error($title, $text) {
$GLOBALS['_RESULT'] = array(
'error' => 0,
'backend_error' => 1,
'text' => $title."\n\n".$text
);
die;
}
function related_return($related = array()) {
$GLOBALS['_RESULT'] = array(
'error' => 0,
'backend_error' => 0,
'total' => count($related),
'related' => $related
);
die;
}
function get_words($str) {
preg_match_all('#[a-zA-Zа-яА-ЯёЁ0-9]{4,}#is', $str, $words);
return $words[0];
}
function check_perms($forum) {
global $fm;
if (!$fm->user['id'] && ($forum['stview'] != 'all' || $forum['private']))
return false;
else if ($forum['stview'] == 'admo' && !defined('IS_ADMIN') && $fm->user['status'] != 'sm' && !isset($forum['moderator'][$fm->user['id']]))
return false;
else if ($forum['private'] && !defined('IS_ADMIN') && !isset($fm->user['private'][$forum['id']]))
return false;
return true;
}
function forums_names($forum) {
return $forum['name'];
}
function string_filter($topic) {
global $topics, $key, $value;
return ($topic['state'] != 'moved' && stristr($topic[$key], $value) !== false && !isset($topics[$topic['id']])) ? true : false;
}
function equal_filter($topic) {
global $topics, $key, $value;
return ($topic['state'] != 'moved' && $topic[$key] === $value && !isset($topics[$topic['id']])) ? true : false;
}
function ranking($topic) {
global $rank;
$topic['rank'] = $rank;
return $topic;
}
function sorting($a, $b) {
global $key;
if ($a[$key] == $b[$key])
return 0;
return ($a[$key] < $b[$key]) ? -1 : 1;
}
function icon_and_dating($topic) {
global $fm, $t_visits, $f_readed;
$f_readed = $fm->_GetCookie('f'.$topic['forum'], 0);
$topic['icon'] = topic_icon($topic, (isset($t_visits[($key = $topic['fid'].':'.$topic['id'])]) && $t_visits[$key] > $fm->user['last_visit']) ? $t_visits[$key] : $fm->user['last_visit']);
$topic['postdate'] = $fm->_DateFormat($topic['postdate'] + $fm->user['timedif'] * 3600);
return $topic;
}
function get_related() {
global $fm, $topics, $key, $rank, $value, $t_visits;
$config = $fm->_Read(CONFIG);
$forum = $fm->_Intval('forum');
$topic = $fm->_Intval('topic');
if (!($list = $fm->_Read('forum'.$forum.'/list.php')))
backend_error($fm->LANG['GetRelatedError'], $fm->LANG['RelatedForumNo']);
if (!isset($list[$topic]))
backend_error($fm->LANG['GetRelatedError'], $fm->LANG['RelatedTopicNo']);
$topic_words = array_merge(get_words($list[$topic]['name']), get_words($list[$topic]['desc']));
$topic_author = $list[$topic]['a_id'];
unset($list[$topic]);
$related = array();
$allforums = array_filter($fm->_Read(FM_ALLFORUMS), 'check_perms');
$forums = array_map('forums_names', $allforums);
unset($allforums[$forum]);
$allforums = array_merge(array($forum), array_keys($allforums));
foreach ($allforums as $forum_id) {
$topics = array();
if ($forum != $forum_id)
$list = $fm->_Read('forum'.$forum_id.'/list.php');
if ($config['bytitle']) {
$key = 'name';
$rank = 30;
foreach ($topic_words as $value)
$topics += array_map('ranking', array_filter($list, 'string_filter'));
}
if ($config['bydesc']) {
$key = 'desc';
$rank = 20;
foreach ($topic_words as $value)
$topics += array_map('ranking', array_filter($list, 'string_filter'));
}
if (!$config['byauthor']) {
$key = 'a_id';
$rank = 10;
$value = $topic_author;
$topics += array_map('ranking', array_filter($list, 'equal_filter'));
}
foreach ($topics as $topic_id => $tinfo)
$related[$forum_id.':'.$topic_id] = array(
'id' => $tinfo['id'],
'name' => $tinfo['name'],
'desc' => $tinfo['desc'],
'fid' => $tinfo['fid'],
'forum' => $forums[$forum_id],
'state' => $tinfo['state'],
'pinned' => $tinfo['pinned'],
'posts' => $tinfo['posts'],
'views' => $tinfo['views'],
'author' => $tinfo['author'],
'a_id' => $tinfo['a_id'],
'date' => $tinfo['date'],
'poster' => $tinfo['poster'],
'p_id' => $tinfo['p_id'],
'postdate' => $tinfo['postdate']
);
unset($list, $topics);
}
if (!$related)
related_return();
$sort = array('date', 'postdate', 'posts', 'views', 'rank');
foreach ($sort as $key)
uasort($related, 'sorting');
$t_visits = $fm->_GetCookieArray('t_visits');
$related = array_map('icon_and_dating', array_splice($related, 0, $config['fortopic']));
related_return($related);
}
?>