Ejemplo n.º 1
0
 public function invokePostAction()
 {
     if ($_SERVER['REQUEST_METHOD'] != self::METHOD_POST) {
         throw new Exception('Controller can only handle POST requests.');
     } elseif (!isset($_REQUEST[self::ACTION])) {
         throw new Exception('Action not specified.');
     }
     $action = $_REQUEST[self::ACTION];
     switch ($action) {
         case self::ACTION_LOGIN:
             if (!AuthenticationManager::authenticate($_REQUEST[self::USR_NAME], $_REQUEST[self::USR_PASSWORD])) {
                 $this->forwardRequest(array('Invalid username or password.'), '?view=login', array(self::USR_NAME => $_REQUEST[self::USR_NAME]));
             }
             break;
         case self::ACTION_LOGOUT:
             AuthenticationManager::signOut();
             Util::redirect();
             break;
         case self::ACTION_REGISTER:
             if (!AuthenticationManager::isAuthenticated()) {
                 self::handleRegister();
             }
             break;
         case self::ACTION_NEWPOST:
             if (AuthenticationManager::isAuthenticated()) {
                 self::handleNewPost();
             }
             break;
         case self::ACTION_EDITPOST:
             if (AuthenticationManager::isAuthenticated()) {
                 self::handleEditPost();
             }
             break;
         case self::ACTION_DELETEPOST:
             if (AuthenticationManager::isAuthenticated()) {
                 self::handleDeletePost();
             }
             break;
         case self::ACTION_SETFAVORITE:
             if (AuthenticationManager::isAuthenticated()) {
                 self::handleSetFavorite();
             }
             break;
         default:
             throw new Exception('Unknown controller action ' . $action);
     }
 }
Ejemplo n.º 2
0
<?php

include_once "views/partials/header.php";
?>

<?php 
if (AuthenticationManager::isAuthenticated()) {
    ?>
    <?php 
    $currUserId = isset($_SESSION['username']) ? $_SESSION['username'] : null;
    $currUser = null;
    if ($currUserId) {
        $currUser = DataManager::getUserById($currUserId);
    }
    ?>
    <div class = "chatContainer">
        <div class = "col-md-8">
            <div class="panel panel-info">
                <div class="panel-heading">
                    <h4>Favorite</h4>
                </div>
                <div class="panel-body favorite">
                    <ul class = "media-list">
                    <?php 
    $channel = DataManager::getChannelByName($_SESSION['channel']);
    $messages = DataManager::getPostsByChannel($channel->getID());
    //Util::stable_uasort($messages, 'Util::MessageCmp');
    foreach ($messages as $message) {
        $author = DataManager::getUserById($message->getAuthor());
        $status = DataManager::getPostStatus($message->getId());
        if ($status == Status::PRIOR) {
Ejemplo n.º 3
0
<?php

require_once __DIR__ . '/../../config/config.php';
$objAuthenticationManager = new AuthenticationManager(COOKIE_NAME, COOKIE_EXPIRE_TIME);
if (!$objAuthenticationManager->isAuthenticated($_COOKIE)) {
    header('Location: ' . LOGIN_URL . '?login_attempt=1');
    exit(0);
}
Ejemplo n.º 4
0
<?php

$channelId = isset($_REQUEST['id']) ? $_REQUEST['id'] : null;
$channel = DataManager::getChannelById($channelId);
if ($channel === null || !AuthenticationManager::isAuthenticated()) {
    Util::redirect('/');
}
?>

<!-- Page Heading -->
<div class="row">
    <div class="col-lg-12">
        <h1 class="page-header">
            <?php 
echo $channel->getName();
?>
        </h1>
    </div>
</div>
<!-- /.row -->

<div id="messages" class="row"></div>

<div class="row">
    <div class="col-lg-8">
        <form class="form-horizontal" id="addNewPost">
            <div>
                <input type="text" class="form-control" name="title" placeholder="Title" id="postTitle" required>
                <textarea class="form-control" rows="3" placeholder="Text" name="text" id="postText"
                          required></textarea>
                <button type="submit" class="btn btn-default">Submit</button>
Ejemplo n.º 5
0
<?php

require_once 'inc/bootstrap.php';
require_once 'views/partials/header.php';
$postAction = isset($_REQUEST['action']) ? $_REQUEST['action'] : null;
if ($postAction != null) {
    Controller::getInstance()->invokePostAction();
}
?>
    <div id="page-wrapper">

        <div class="container-fluid">

            <?php 
$view = isset($_REQUEST['view']) ? $_REQUEST['view'] : (AuthenticationManager::isAuthenticated() ? 'overview' : 'welcome');
$path = 'views/' . $view . '.php';
if (file_exists($path)) {
    require_once $path;
}
?>

        </div>
        <!-- /.container-fluid -->

    </div>
    <!-- /#page-wrapper -->

<?php 
require_once 'views/partials/footer.php';
Ejemplo n.º 6
0
 public function invokePostAction()
 {
     if ($_SERVER['REQUEST_METHOD'] != self::REQUEST_METHOD) {
         throw new Exception("Controller can only handle " . self::REQUEST_METHOD . ' requests');
         return null;
     } else {
         if (!isset($_REQUEST[self::ACTION_PARAM])) {
             throw new Exception(self::ACTION_PARAM . ' parameter is not specified');
         }
     }
     $action = $_REQUEST[self::ACTION_PARAM];
     switch ($action) {
         case self::ACTION_LOGIN:
             if (!AuthenticationManager::authenticate($_REQUEST[self::USR_NAME], $_REQUEST[self::USR_PASSWORD])) {
                 $this->forwardRequest(['Invalid user information provided']);
             }
             $user = DataManager::getUserByUsername($_REQUEST[self::USR_NAME]);
             $_SESSION['username'] = $user->getID();
             $user = AuthenticationManager::getAuthenticatedUser();
             $channels = DataManager::getChannelsByUserId($user->getID());
             $_SESSION['channel'] = $channels[0]->getName();
             Util::redirect();
             break;
         case self::ACTION_LOGOUT:
             if (AuthenticationManager::isAuthenticated()) {
                 AuthenticationManager::signOut();
             }
             Util::redirect();
             break;
         case self::ACTION_REGISTRATION:
             $channels = $_REQUEST['channels'];
             foreach ($channels as $ch) {
                 $channel = DataManager::getChannelByName($ch);
                 $registratedUsers = DataManager::getUsersByChannelId($channel->getID());
                 foreach ($registratedUsers as $user) {
                     if ($user->getUsername() === $_REQUEST[self::USR_NAME]) {
                         $this->forwardRequest(['The username ' . $_REQUEST[self::USR_NAME] . ' is already used!'], 'index.php?view=registration');
                     }
                 }
                 $user = DataManager::getUserByUsername($_REQUEST[self::USR_NAME]);
                 $userId = null;
                 if ($user) {
                     $userId = $user->getID();
                 } else {
                     $userId = DataManager::saveNewUser($_REQUEST[self::USR_FIRST_NAME], $_REQUEST[self::USR_LAST_NAME], $_REQUEST[self::USR_NAME], AuthenticationManager::getHash($_REQUEST[self::USR_NAME], $_REQUEST[self::USR_PASSWORD]));
                 }
                 DataManager::registrateUser($userId, $channel->getID());
             }
             if (!AuthenticationManager::authenticate($_REQUEST[self::USR_NAME], $_REQUEST[self::USR_PASSWORD])) {
                 $this->forwardRequest(['Invalid user information provided'], "index.php?view=registration");
             }
             $_SESSION[self::USR_CHANNELS] = $_REQUEST[self::USR_CHANNELS];
             // first channel should be selected as default channel
             $_SESSION['channel'] = $channels[0];
             Util::redirect();
             break;
         case self::POST_MSG:
             $channel = DataManager::getChannelByName($_SESSION['channel']);
             $user = AuthenticationManager::getAuthenticatedUser();
             $messages = DataManager::getAllUnansweredPosts($channel->getID());
             //TODO: mark message as answered
             foreach ($messages as $message) {
                 if ($message->getAuthor() != $user->getID()) {
                     DataManager::changePostStatus($message->getID(), Status::ANSWERED);
                 }
             }
             DataManager::publishMessage($user->getID(), $channel->getID(), $_REQUEST[self::POST_TITLE], $_REQUEST[self::POST_CONTENT], Status::UNREAD);
             break;
         case self::ACTION_CHANGE_CHANNEL:
             //print_r($_REQUEST);
             $_SESSION['channel'] = $_REQUEST['selectedChannel'];
             Util::redirect();
             break;
         case self::ACTION_JOIN_CHANNEL:
             $channel = DataManager::getChannelByName($_REQUEST[self::USR_CHANNEL]);
             $registratedUsers = DataManager::getUsersByChannelId($channel->getID());
             foreach ($registratedUsers as $user) {
                 if ($user->getUsername() === $_REQUEST[self::USR_NAME]) {
                     $this->forwardRequest(['User ' . $_REQUEST[self::USR_NAME] . ' is already registered!'], "index.php?view=join");
                 }
             }
             $user = DataManager::getUserByUsername($_REQUEST[self::USR_NAME]);
             if (!$user) {
                 $this->forwardRequest(['Please registrate, the user ' . $_REQUEST[self::USR_NAME] . ' does not exists!'], "index.php?view=register");
             }
             DataManager::registrateUser($user->getID(), $channel->getID());
             if (!AuthenticationManager::authenticate($_REQUEST[self::USR_NAME], $_REQUEST[self::USR_PASSWORD], $_REQUEST[self::USR_CHANNEL])) {
                 $this->forwardRequest(['Invalid user information provided'], "index.php?view=registration");
             }
             $_SESSION[self::USR_CHANNEL] = $_REQUEST[self::USR_CHANNEL];
             Util::redirect();
             break;
         case self::AJAX_SET_PRIO:
             if (isset($_POST) && $_POST) {
                 DataManager::changePostStatus($_POST['id'], Status::PRIOR);
                 echo "index.php?view=welcome";
             }
             break;
         case self::AJAX_RESET_PRIO:
             if (isset($_POST) && $_POST) {
                 DataManager::changePostStatus($_POST['id'], Status::READ);
                 echo "index.php?view=welcome";
             }
             break;
         case self::AJAX_DELETE_MESSAGE:
             if (isset($_POST) && $_POST) {
                 DataManager::changePostStatus($_POST['id'], Status::DELETED);
                 echo "index.php?view=welcome";
             }
             break;
         case self::AJAX_UPDATE_CHAT:
             $currUserId = isset($_SESSION['username']) ? $_SESSION['username'] : null;
             $channel = isset($_SESSION['channel']) ? $_SESSION['channel'] : null;
             if ($currUserId && $channel) {
                 $unreadPosts = DataManager::getAllUnreadPostsByUserId($currUserId);
                 foreach ($unreadPosts as $post) {
                     if ($post->getAuthor() != $currUserId) {
                         DataManager::changePostStatus($post->getId(), Status::READ);
                     }
                 }
             }
             if (isset($_POST) && $_POST) {
                 $channel = DataManager::getChannelByName($_REQUEST['channel']);
                 $messages = DataManager::getPostsByChannel($channel->getID());
                 $return = "";
                 foreach ($messages as $message) {
                     if ($message->exists()) {
                         $return .= Viewtility::viewMessage($message, DataManager::getPostStatus($message->getId()));
                     }
                 }
                 echo $return;
             }
             break;
     }
 }