* @author Slawkens * @copyright 2017 MyAAC * @version 0.6.6 * @link http://my-aac.org */ defined('MYAAC') or die('Direct access not allowed!'); class Forum { static public function canPost($account) { global $db, $config; if(!$account->isLoaded() || $account->isBanned()) return false; if(self::isModerator()) return true; return $db->query( 'SELECT `id` FROM `players` WHERE `account_id` = ' . $db->quote($account->getId()) . ' AND `level` >= ' . $db->quote($config['forum_level_required']) . ' LIMIT 1')->rowCount() > 0; } static public function isModerator() { return hasFlag(FLAG_CONTENT_FORUM) || superAdmin(); } static public function add_thread($title, $body, $section_id, $player_id, $account_id, &$errors) { global $db; $thread_id = 0; if($db->insert(TABLE_PREFIX . 'forum', array('first_post' => 0, 'last_post' => time(), 'section' => $section_id, 'replies' => 0, 'views' => 0, 'author_aid' => isset($account_id) ? $account_id : 0, 'author_guid' => isset($player_id) ? $player_id : 0, 'post_text' => $body, 'post_topic' => $title, 'post_smile' => 0, 'post_date' => time(), 'last_edit_aid' => 0, 'edit_date' => 0, 'post_ip' => $_SERVER['REMOTE_ADDR']))) { $thread_id = $db->lastInsertId(); $db->query("UPDATE `" . TABLE_PREFIX . "forum` SET `first_post`=".(int) $thread_id." WHERE `id` = ".(int) $thread_id); } return $thread_id; } static public function add_post($thread_id, $section, $author_aid, $author_guid, $post_text, $post_topic, $smile) { global $db; $db->insert(TABLE_PREFIX . 'forum', array( 'first_post' => $thread_id, 'section' => $section, 'author_aid' => $author_aid, 'author_guid' => $author_guid, 'post_text' => $post_text, 'post_topic' => $post_topic, 'post_smile' => $smile, 'post_date' => time(), 'post_ip' => $_SERVER['REMOTE_ADDR'] )); } static public function add_board($name, $description, $access, $guild, &$errors) { global $db; if(isset($name[0]) && isset($description[0])) { $query = $db->select(TABLE_PREFIX . 'forum_boards', array('name' => $name)); if($query === false) { $query = $db->query( 'SELECT ' . $db->fieldName('ordering') . ' FROM ' . $db->tableName(TABLE_PREFIX . 'forum_boards') . ' ORDER BY ' . $db->fieldName('ordering') . ' DESC LIMIT 1' ); $ordering = 0; if($query->rowCount() > 0) { $query = $query->fetch(); $ordering = $query['ordering'] + 1; } $db->insert(TABLE_PREFIX . 'forum_boards', array('name' => $name, 'description' => $description, 'access' => $access, 'guild' => $guild, 'ordering' => $ordering)); } else $errors[] = 'Forum board with this name already exists.'; } else $errors[] = 'Please fill all inputs.'; return !count($errors); } static public function get_board($id) { global $db; return $db->select(TABLE_PREFIX . 'forum_boards', array('id' => $id)); } static public function update_board($id, $name, $access, $guild, $description) { global $db; $db->update(TABLE_PREFIX . 'forum_boards', array('name' => $name, 'description' => $description, 'access' => $access, 'guild' => $guild), array('id' => $id)); } static public function delete_board($id, &$errors) { global $db; if(isset($id)) { if(self::get_board($id) !== false) $db->delete(TABLE_PREFIX . 'forum_boards', array('id' => $id)); else $errors[] = 'Forum board with id ' . $id . ' does not exists.'; } else $errors[] = 'id not set'; return !count($errors); } static public function toggleHidden_board($id, &$errors) { global $db; if(isset($id)) { $query = self::get_board($id); if($query !== false) $db->update(TABLE_PREFIX . 'forum_boards', array('hidden' => ($query['hidden'] == 1 ? 0 : 1)), array('id' => $id)); else $errors[] = 'Forum board with id ' . $id . ' does not exists.'; } else $errors[] = 'id not set'; return !count($errors); } static public function move_board($id, $i, &$errors) { global $db; $query = self::get_board($id); if($query !== false) { $ordering = $query['ordering'] + $i; $old_record = $db->select(TABLE_PREFIX . 'forum_boards', array('ordering' => $ordering)); if($old_record !== false) $db->update(TABLE_PREFIX . 'forum_boards', array('ordering' => $query['ordering']), array('ordering' => $ordering)); $db->update(TABLE_PREFIX . 'forum_boards', array('ordering' => $ordering), array('id' => $id)); } else $errors[] = 'Forum board with id ' . $id . ' does not exists.'; return !count($errors); } public static function parseSmiles($text) { $smileys = array( ';D' => 1, ':D' => 1, ':cool:' => 2, ';cool;' => 2, ':ekk:' => 3, ';ekk;' => 3, ';o' => 4, ';O' => 4, ':o' => 4, ':O' => 4, ':(' => 5, ';(' => 5, ':mad:' => 6, ';mad;' => 6, ';rolleyes;' => 7, ':rolleyes:' => 7, ':)' => 8, ';d' => 9, ':d' => 9, ';)' => 10 ); foreach($smileys as $search => $replace) $text = str_replace($search, ''. $search .'', $text); return $text; } public static function parseBBCode($text, $smiles) { $rows = 0; while(stripos($text, '[code]') !== false && stripos($text, '[/code]') !== false ) { $code = substr($text, stripos($text, '[code]')+6, stripos($text, '[/code]') - stripos($text, '[code]') - 6); if(!is_int($rows / 2)) { $bgcolor = 'ABED25'; } else { $bgcolor = '23ED25'; } $rows++; $text = str_ireplace('[code]'.$code.'[/code]', 'Code:
'.$code.'
', $text); } $rows = 0; while(stripos($text, '[quote]') !== false && stripos($text, '[/quote]') !== false ) { $quote = substr($text, stripos($text, '[quote]')+7, stripos($text, '[/quote]') - stripos($text, '[quote]') - 7); if(!is_int($rows / 2)) { $bgcolor = 'AAAAAA'; } else { $bgcolor = 'CCCCCC'; } $rows++; $text = str_ireplace('[quote]'.$quote.'[/quote]', '
'.$quote.'
', $text); } $rows = 0; while(stripos($text, '[url]') !== false && stripos($text, '[/url]') !== false ) { $url = substr($text, stripos($text, '[url]')+5, stripos($text, '[/url]') - stripos($text, '[url]') - 5); $text = str_ireplace('[url]'.$url.'[/url]', ''.$url.'', $text); } $xhtml = false; $tags = array( '#\[b\](.*?)\[/b\]#si' => ($xhtml ? '\\1' : '\\1'), '#\[i\](.*?)\[/i\]#si' => ($xhtml ? '\\1' : '\\1'), '#\[u\](.*?)\[/u\]#si' => ($xhtml ? '\\1' : '\\1'), '#\[s\](.*?)\[/s\]#si' => ($xhtml ? '\\1' : '\\1'), '#\[guild\](.*?)\[/guild\]#si' => urldecode(generateLink(getGuildLink('$1', false), '$1', true)), '#\[house\](.*?)\[/house\]#si' => urldecode(generateLink(getHouseLink('$1', false), '$1', true)), '#\[player\](.*?)\[/player\]#si' => urldecode(generateLink(getPlayerLink('$1', false), '$1', true)), // TODO: [poll] tag '#\[color=(.*?)\](.*?)\[/color\]#si' => ($xhtml ? '\\2' : '\\2'), '#\[img\](.*?)\[/img\]#si' => ($xhtml ? '' : ''), '#\[url=(.*?)\](.*?)\[/url\]#si' => '\\2', // '#\[email\](.*?)\[/email\]#si' => '\\1', '#\[code\](.*?)\[/code\]#si' => '\\1', // '#\[align=(.*?)\](.*?)\[/align\]#si' => ($xhtml ? '
\\2
' : '
\\2
'), // '#\[br\]#si' => ($xhtml ? '
' : '
'), ); foreach($tags as $search => $replace) $text = preg_replace($search, $replace, $text); return ($smiles == 0 ? Forum::parseSmiles($text) : $text); } public static function showPost($topic, $text, $smiles) { $text = nl2br($text); $post = ''; if(!empty($topic)) $post .= ''.($smiles == 0 ? self::parseSmiles($topic) : $topic).'
'; $post .= self::parseBBCode($text, $smiles); return $post; } public static function hasAccess($board_id) { global $sections, $logged, $account_logged, $logged_access; if(!isset($sections[$board_id])) return false; $hasAccess = true; $section = $sections[$board_id]; if($section['guild'] > 0) { if($logged) { $guild = new OTS_Guild(); $guild->load($section['guild']); $status = false; if($guild->isLoaded()) { $account_players = $account_logged->getPlayers(); foreach ($account_players as $player) { if($guild->hasMember($player)) { $status = true; } } } if (!$status) $hasAccess = false; } else { $hasAccess = false; } } if($section['access'] > 0) { if($logged_access < $section['access']) { $hasAccess = false; } } return $hasAccess; } } ?>