Skip to content

Commit

Permalink
* added forums for guilds and groups
Browse files Browse the repository at this point in the history
* (internal) new function: OTS_Guild::hasMember(OTS_Player $player)
* (internal) new function: Forum::hasAccess($board_id)
  • Loading branch information
slawkens committed Oct 20, 2017
1 parent c2678aa commit 762fa31
Showing 17 changed files with 274 additions and 130 deletions.
2 changes: 1 addition & 1 deletion common.php
Original file line number Diff line number Diff line change
@@ -28,7 +28,7 @@

define('MYAAC', true);
define('MYAAC_VERSION', '0.6.1');
define('DATABASE_VERSION', 14);
define('DATABASE_VERSION', 15);
define('TABLE_PREFIX', 'myaac_');
define('START_TIME', microtime(true));
define('MYAAC_OS', (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? 'WINDOWS' : (strtoupper(PHP_OS) == 'DARWIN' ? 'MAC' : 'LINUX'));
2 changes: 2 additions & 0 deletions install/includes/schema.sql
Original file line number Diff line number Diff line change
@@ -85,6 +85,8 @@ CREATE TABLE `myaac_forum_boards`
`description` VARCHAR(255) NOT NULL DEFAULT '',
`ordering` INT(11) NOT NULL DEFAULT 0,
`closed` TINYINT(1) NOT NULL DEFAULT 0,
`guild` INT(11) NOT NULL DEFAULT 0,
`access` INT(11) NOT NULL DEFAULT 0,
`hidden` TINYINT(1) NOT NULL DEFAULT 0,
PRIMARY KEY (`id`)
) ENGINE = MyISAM;
2 changes: 1 addition & 1 deletion system/functions.php
Original file line number Diff line number Diff line change
@@ -215,7 +215,7 @@ function generateRandomString($length, $lowCase = true, $upCase = false, $numeri
function getForumBoards()
{
global $db, $canEdit;
$sections = $db->query('SELECT `id`, `name`, `description`, `closed`' . ($canEdit ? ', `hidden`, `ordering`' : '') . ' FROM `' . TABLE_PREFIX . 'forum_boards` ' . (!$canEdit ? ' WHERE `hidden` != 1' : '') .
$sections = $db->query('SELECT `id`, `name`, `description`, `closed`, `guild`, `access`' . ($canEdit ? ', `hidden`, `ordering`' : '') . ' FROM `' . TABLE_PREFIX . 'forum_boards` ' . (!$canEdit ? ' WHERE `hidden` != 1' : '') .
' ORDER BY `ordering`;');
if($sections)
return $sections->fetchAll();
23 changes: 22 additions & 1 deletion system/libs/pot/OTS_Guild.php
Original file line number Diff line number Diff line change
@@ -74,6 +74,7 @@ public function __sleep()
*
* @version 0.1.3
*/
/*
public function __clone()
{
unset($this->data['id']);
@@ -90,7 +91,7 @@ public function __clone()
$this->requests->__construct($this);
}
}

*/
/**
* Assigns invites handler.
*
@@ -282,6 +283,26 @@ public function setOwner(OTS_Player $owner)
$this->data['ownerid'] = $owner->getId();
}

public function hasMember(OTS_Player $player) {
global $db;

if(!$player || !$player->isLoaded()) {
return false;
}

$player_rank = $player->getRank();
if(!$player_rank->isLoaded()) {
return false;
}

foreach($this->getGuildRanksList() as $rank) {
if($rank->getId() == $player_rank->getId()) {
return true;
}
}

return false;
}
/**
* Guild creation data.
*
11 changes: 11 additions & 0 deletions system/migrations/15.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

// add new forum.guild and forum.access fields
if(!fieldExist('guild', TABLE_PREFIX . 'forum_boards')) {
$db->query("ALTER TABLE `" . TABLE_PREFIX . "forum_boards` ADD `guild` TINYINT(1) NOT NULL DEFAULT 0 AFTER `closed`;");
}

if(!fieldExist('access', TABLE_PREFIX . 'forum_boards')) {
$db->query("ALTER TABLE `" . TABLE_PREFIX . "forum_boards` ADD `access` TINYINT(1) NOT NULL DEFAULT 0 AFTER `guild`;");
}
?>
104 changes: 81 additions & 23 deletions system/pages/forum.php
Original file line number Diff line number Diff line change
@@ -30,11 +30,19 @@
$canEdit = hasFlag(FLAG_CONTENT_FORUM) || superAdmin();
if($canEdit)
{
$groups = new OTS_Groups_List();

if(!empty($action))
{
if($action == 'delete_board' || $action == 'edit_board' || $action == 'hide_board' || $action == 'moveup_board' || $action == 'movedown_board')
$id = $_REQUEST['id'];

if(isset($_REQUEST['access']))
$access = $_REQUEST['access'];

if(isset($_REQUEST['guild']))
$guild = $_REQUEST['guild'];

if(isset($_REQUEST['name']))
$name = $_REQUEST['name'];

@@ -44,7 +52,7 @@
$errors = array();

if($action == 'add_board') {
if(Forum::add_board($name, $description, $errors))
if(Forum::add_board($name, $description, $access, $guild, $errors))
$action = $name = $description = '';
}
else if($action == 'delete_board') {
@@ -56,11 +64,14 @@
if(isset($id) && !isset($name)) {
$board = Forum::get_board($id);
$name = $board['name'];
$access = $board['access'];
$guild = $board['guild'];
$description = $board['description'];
}
else {
Forum::update_board($id, $name, $description);
Forum::update_board($id, $name, $access, $guild, $description);
$action = $name = $description = '';
$access = $guild = 0;
}
}
else if($action == 'hide_board') {
@@ -83,12 +94,17 @@
}

if(empty($action) || $action == 'edit_board') {
$guilds = $db->query('SELECT `id`, `name` FROM `guilds`')->fetchAll();
echo $twig->render('forum.add_board.html.twig', array(
'link' => getLink('forum', ($action == 'edit_board' ? 'edit_board' : 'add_board')),
'action' => $action,
'id' => isset($id) ? $id : null,
'name' => isset($name) ? $name : null,
'description' => isset($description) ? $description : null
'description' => isset($description) ? $description : null,
'access' => isset($access) ? $access : 0,
'guild' => isset($guild) ? $guild : null,
'groups' => $groups,
'guilds' => $guilds
));

if($action == 'edit_board')
@@ -103,7 +119,9 @@
'id' => $section['id'],
'name' => $section['name'],
'description' => $section['description'],
'closed' => $section['closed'] == '1'
'closed' => $section['closed'] == '1',
'guild' => $section['guild'],
'access' => $section['access']
);

if($canEdit) {
@@ -124,21 +142,24 @@
$counters[$data['section']] = array('threads' => $data['threads'], 'posts' => $data['replies'] + $data['threads']);
foreach($sections as $id => $section)
{
$last_post = $db->query("SELECT `players`.`name`, `" . TABLE_PREFIX . "forum`.`post_date` FROM `players`, `" . TABLE_PREFIX . "forum` WHERE `" . TABLE_PREFIX . "forum`.`section` = ".(int) $id." AND `players`.`id` = `" . TABLE_PREFIX . "forum`.`author_guid` ORDER BY `post_date` DESC LIMIT 1")->fetch();
$boards[] = array(
'id' => $id,
'link' => getForumBoardLink($id),
'name' => $section['name'],
'description' => $section['description'],
'hidden' => $section['hidden'],
'posts' => isset($counters[$id]['posts']) ? $counters[$id]['posts'] : 0,
'threads' => isset($counters[$id]['threads']) ? $counters[$id]['threads'] : 0,
'last_post' => array(
'name' => isset($last_post['name']) ? $last_post['name'] : null,
'date' => isset($last_post['post_date']) ? $last_post['post_date'] : null,
'player_link' => isset($last_post['name']) ? getPlayerLink($last_post['name']) : null,
)
);
$show = true;
if(Forum::hasAccess($id)) {
$last_post = $db->query("SELECT `players`.`name`, `" . TABLE_PREFIX . "forum`.`post_date` FROM `players`, `" . TABLE_PREFIX . "forum` WHERE `" . TABLE_PREFIX . "forum`.`section` = ".(int) $id." AND `players`.`id` = `" . TABLE_PREFIX . "forum`.`author_guid` ORDER BY `post_date` DESC LIMIT 1")->fetch();
$boards[] = array(
'id' => $id,
'link' => getForumBoardLink($id),
'name' => $section['name'],
'description' => $section['description'],
'hidden' => $section['hidden'],
'posts' => isset($counters[$id]['posts']) ? $counters[$id]['posts'] : 0,
'threads' => isset($counters[$id]['threads']) ? $counters[$id]['threads'] : 0,
'last_post' => array(
'name' => isset($last_post['name']) ? $last_post['name'] : null,
'date' => isset($last_post['post_date']) ? $last_post['post_date'] : null,
'player_link' => isset($last_post['name']) ? getPlayerLink($last_post['name']) : null,
)
);
}
}

echo $twig->render('forum.boards.html.twig', array(
@@ -205,7 +226,7 @@ static public function add_post($thread_id, $section, $author_aid, $author_guid,
'post_ip' => $_SERVER['REMOTE_ADDR']
));
}
static public function add_board($name, $description, &$errors)
static public function add_board($name, $description, $access, $guild, &$errors)
{
global $db;
if(isset($name[0]) && isset($description[0]))
@@ -226,7 +247,7 @@ static public function add_board($name, $description, &$errors)
$query = $query->fetch();
$ordering = $query['ordering'] + 1;
}
$db->insert(TABLE_PREFIX . 'forum_boards', array('name' => $name, 'description' => $description, 'ordering' => $ordering));
$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.';
@@ -242,9 +263,9 @@ static public function get_board($id) {
return $db->select(TABLE_PREFIX . 'forum_boards', array('id' => $id));
}

static public function update_board($id, $name, $description) {
static public function update_board($id, $name, $access, $guild, $description) {
global $db;
$db->update(TABLE_PREFIX . 'forum_boards', array('name' => $name, 'description' => $description), array('id' => $id));
$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)
@@ -389,4 +410,41 @@ public static function showPost($topic, $text, $smiles)
$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;
}
}
4 changes: 2 additions & 2 deletions system/pages/forum/edit_post.php
Original file line number Diff line number Diff line change
@@ -19,12 +19,12 @@
return;
}

$thread = $db->query("SELECT `" . TABLE_PREFIX . "forum`.`author_guid`, `" . TABLE_PREFIX . "forum`.`author_aid`, `" . TABLE_PREFIX . "forum`.`first_post`, `" . TABLE_PREFIX . "forum`.`post_topic`, `" . TABLE_PREFIX . "forum`.`post_date`, `" . TABLE_PREFIX . "forum`.`post_text`, `" . TABLE_PREFIX . "forum`.`post_smile`, `" . TABLE_PREFIX . "forum`.`id`, `" . TABLE_PREFIX . "forum`.`section` FROM `" . TABLE_PREFIX . "forum` WHERE `" . TABLE_PREFIX . "forum`.`id` = ".$post_id." LIMIT 1")->fetch();
$thread = $db->query("SELECT `author_guid`, `author_aid`, `first_post`, `post_topic`, `post_date`, `post_text`, `post_smile`, `id`, `section` FROM `" . TABLE_PREFIX . "forum` WHERE `id` = ".$post_id." LIMIT 1")->fetch();
if(isset($thread['id']))
{
$first_post = $db->query("SELECT `" . TABLE_PREFIX . "forum`.`author_guid`, `" . TABLE_PREFIX . "forum`.`author_aid`, `" . TABLE_PREFIX . "forum`.`first_post`, `" . TABLE_PREFIX . "forum`.`post_topic`, `" . TABLE_PREFIX . "forum`.`post_text`, `" . TABLE_PREFIX . "forum`.`post_smile`, `" . TABLE_PREFIX . "forum`.`id`, `" . TABLE_PREFIX . "forum`.`section` FROM `" . TABLE_PREFIX . "forum` WHERE `" . TABLE_PREFIX . "forum`.`id` = ".(int) $thread['first_post']." LIMIT 1")->fetch();
echo '<a href="' . getLink('forum') . '">Boards</a> >> <a href="' . getForumBoardLink($thread['section']) . '">'.$sections[$thread['section']]['name'].'</a> >> <a href="' . getForumThreadLink($thread['first_post']) . '">'.$first_post['post_topic'].'</a> >> <b>Edit post</b>';
if($account_logged->getId() == $thread['author_aid'] || Forum::isModerator())
if(Forum::hasAccess($thread['section'] && ($account_logged->getId() == $thread['author_aid'] || Forum::isModerator())))
{
$char_id = $post_topic = $text = $smile = null;
$players_from_account = $db->query("SELECT `players`.`name`, `players`.`id` FROM `players` WHERE `players`.`account_id` = ".(int) $account_logged->getId())->fetchAll();
79 changes: 46 additions & 33 deletions system/pages/forum/move_thread.php
Original file line number Diff line number Diff line change
@@ -11,42 +11,55 @@
*/
defined('MYAAC') or die('Direct access not allowed!');

if(!Forum::isModerator()) {
echo 'You are not logged in or you are not moderator.';
}

$save = isset($_REQUEST['save']) ? (int)$_REQUEST['save'] == 1 : false;
if($save) {
if (Forum::isModerator()) {
$id = (int)$_REQUEST['id'];
$board = (int)$_REQUEST['section'];
$post = $db->query("SELECT `id`, `first_post`, `section` FROM `" . TABLE_PREFIX . "forum` WHERE `id` = " . $id . " LIMIT 1")->fetch();
if ($post['id'] == $id) {
if ($post['id'] == $post['first_post']) {
$db->query("UPDATE `" . TABLE_PREFIX . "forum` SET `section` = " . $board . " WHERE `id` = " . $post['id'] . "") or die(mysql_error());
$nPost = $db->query('SELECT `section` FROM `' . TABLE_PREFIX . 'forum` WHERE `id` = \'' . $id . '\' LIMIT 1;')->fetch();
header('Location: ' . getForumBoardLink($nPost['section']));
}
} else
echo 'Post with ID ' . $id . ' does not exist.';
} else
echo 'You are not logged in or you are not moderator.';
$post_id = (int)$_REQUEST['id'];
$board = (int)$_REQUEST['section'];
if(!Forum::hasAccess($board)) {
echo "You don't have access to this board.";
return;
}

$post = $db->query("SELECT `id`, `first_post`, `section` FROM `" . TABLE_PREFIX . "forum` WHERE `id` = " . $post_id . " LIMIT 1")->fetch();
if ($post['id'] == $post_id) {
if ($post['id'] == $post['first_post']) {
$db->query("UPDATE `" . TABLE_PREFIX . "forum` SET `section` = " . $board . " WHERE `id` = " . $post['id'] . "");
$nPost = $db->query('SELECT `section` FROM `' . TABLE_PREFIX . 'forum` WHERE `id` = \'' . $post_id . '\' LIMIT 1;')->fetch();
header('Location: ' . getForumBoardLink($nPost['section']));
}
}
else
echo 'Post with ID ' . $post_id . ' does not exist.';
}
else {
if (Forum::isModerator()) {
$id = (int)$_REQUEST['id'];
$post = $db->query("SELECT `id`, `section`, `first_post`, `post_topic`, `author_guid` FROM `" . TABLE_PREFIX . "forum` WHERE `id` = " . $id . " LIMIT 1")->fetch();
$name = $db->query("SELECT `name` FROM `players` WHERE `id` = " . $post['author_guid'] . " ")->fetch();
if ($post['id'] == $id) {
if ($post['id'] == $post['first_post']) {
echo $twig->render('forum.move_thread.html.twig', array(
'thread' => $post['post_topic'],
'author' => $name[0],
'board' => $sections[$post['section']]['name'],
'post_id' => $post['id'],
'sections' => $sections,
'section_link' => getForumBoardLink($post['section']),
));
}
} else
echo 'Post with ID ' . $id . ' does not exist.';
} else
echo 'You are not logged in or you are not moderator.';
$post_id = (int)$_REQUEST['id'];
$post = $db->query("SELECT `id`, `section`, `first_post`, `post_topic`, `author_guid` FROM `" . TABLE_PREFIX . "forum` WHERE `id` = " . $post_id . " LIMIT 1")->fetch();
$name = $db->query("SELECT `name` FROM `players` WHERE `id` = " . $post['author_guid'] . " ")->fetch();

$sections_allowed = array();
foreach($sections as $id => $section) {
if(Forum::hasAccess($id)) {
$sections_allowed[$id] = $section;
}
}

if ($post['id'] == $post_id) {
if ($post['id'] == $post['first_post']) {
echo $twig->render('forum.move_thread.html.twig', array(
'thread' => $post['post_topic'],
'author' => $name['name'],
'board' => $sections[$post['section']]['name'],
'post_id' => $post['id'],
'sections' => $sections_allowed,
'section_link' => getForumBoardLink($post['section']),
));
}
}
else
echo 'Post with ID ' . $post_id . ' does not exist.';
}
?>
3 changes: 2 additions & 1 deletion system/pages/forum/new_post.php
Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@

$thread = $db->query("SELECT `" . TABLE_PREFIX . "forum`.`post_topic`, `" . TABLE_PREFIX . "forum`.`id`, `" . TABLE_PREFIX . "forum`.`section` FROM `" . TABLE_PREFIX . "forum` WHERE `" . TABLE_PREFIX . "forum`.`id` = ".(int) $thread_id." AND `" . TABLE_PREFIX . "forum`.`first_post` = ".(int) $thread_id." LIMIT 1")->fetch();
echo '<a href="' . getLink('forum') . '">Boards</a> >> <a href="' . getForumBoardLink($thread['section']) . '">'.$sections[$thread['section']]['name'].'</a> >> <a href="' . getForumThreadLink($thread_id) . '">'.$thread['post_topic'].'</a> >> <b>Post new reply</b><br /><h3>'.$thread['post_topic'].'</h3>';
if(isset($thread['id']))
if(isset($thread['id']) && Forum::hasAccess($thread['section']))
{
$quote = isset($_REQUEST['quote']) ? (int) $_REQUEST['quote'] : NULL;
$text = isset($_REQUEST['text']) ? stripslashes(trim($_REQUEST['text'])) : NULL;
@@ -81,6 +81,7 @@
echo '<br />Thank you for posting.<br /><a href="' . getForumThreadLink($thread_id, $_page) . '">GO BACK TO LAST THREAD</a>';
}
}

if(!$saved)
{
if(!empty($errors))
Loading

0 comments on commit 762fa31

Please sign in to comment.