Initial commit
This commit is contained in:
37
app/plugins/chat/controllers/admin/chat.php
Normal file
37
app/plugins/chat/controllers/admin/chat.php
Normal file
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
/**
|
||||
* app/plugins/chat/controllers/admin/chat.php
|
||||
*
|
||||
* This is the chat admin controller.
|
||||
*
|
||||
* @package TP Chat
|
||||
* @version 3.0
|
||||
* @author Joey Kimsey <Joey@thetempusproject.com>
|
||||
* @link https://TheTempusProject.com
|
||||
* @license https://opensource.org/licenses/MIT [MIT LICENSE]
|
||||
*/
|
||||
namespace TheTempusProject\Controllers\Admin;
|
||||
|
||||
use TheTempusProject\Bedrock\Functions\Input;
|
||||
use TheTempusProject\Houdini\Classes\Issues;
|
||||
use TheTempusProject\Houdini\Classes\Views;
|
||||
use TheTempusProject\Houdini\Classes\Navigation;
|
||||
use TheTempusProject\Houdini\Classes\Components;
|
||||
use TheTempusProject\Classes\AdminController;
|
||||
use TheTempusProject\Models\Chat as ChatModel;
|
||||
|
||||
class Chat extends AdminController {
|
||||
protected static $chat;
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct();
|
||||
self::$title = 'Admin - Chat';
|
||||
self::$chat = new ChatModel;
|
||||
$view = Navigation::activePageSelect( 'nav.admin', '/admin/chat' );
|
||||
Components::set( 'ADMINNAV', $view );
|
||||
}
|
||||
|
||||
public function index() {
|
||||
// Views::view( 'chat.admin.list', self::$chat->list() );
|
||||
}
|
||||
}
|
45
app/plugins/chat/controllers/api/messages.php
Normal file
45
app/plugins/chat/controllers/api/messages.php
Normal file
@ -0,0 +1,45 @@
|
||||
<?php
|
||||
/**
|
||||
* app/controllers/api/users.php
|
||||
*
|
||||
* This is the users' api controller.
|
||||
*
|
||||
* @version 3.0
|
||||
* @author Joey Kimsey <Joey@thetempusproject.com>
|
||||
* @link https://TheTempusProject.com
|
||||
* @license https://opensource.org/licenses/MIT [MIT LICENSE]
|
||||
*/
|
||||
namespace TheTempusProject\Controllers\Api;
|
||||
|
||||
use TheTempusProject\Models\User;
|
||||
use TheTempusProject\Classes\ApiController;
|
||||
use TheTempusProject\Houdini\Classes\Views;
|
||||
use TheTempusProject\Bedrock\Functions\Input;
|
||||
use TheTempusProject\Classes\Forms;
|
||||
use TheTempusProject\Houdini\Classes\Template;
|
||||
use TheTempusProject\Models\Chat;
|
||||
|
||||
class Messages extends ApiController {
|
||||
public static $chat;
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct();
|
||||
self::$chat = new Chat;
|
||||
Template::setTemplate( 'api' );
|
||||
}
|
||||
|
||||
public function sendMessage() {
|
||||
$response = true;
|
||||
if ( ! Forms::check( 'newChatMessage' ) ) {
|
||||
$response = 'Invalid Form';
|
||||
} else {
|
||||
$response = self::$chat->create( Input::post( 'chatMessage' ) );
|
||||
}
|
||||
Views::view( 'api.response', ['response' => json_encode( [ 'data' => $response ], true )]);
|
||||
}
|
||||
|
||||
public function getMessages() {
|
||||
$response = Views::simpleView( 'chat.chat', self::$chat->recent( 50 ) );
|
||||
Views::view( 'api.response', ['response' => json_encode( [ 'data' => $response ], true )]);
|
||||
}
|
||||
}
|
191
app/plugins/chat/controllers/chat.php
Normal file
191
app/plugins/chat/controllers/chat.php
Normal file
@ -0,0 +1,191 @@
|
||||
<?php
|
||||
/**
|
||||
* app/plugins/chat/controllers/chat.php
|
||||
*
|
||||
* This is the public chat controller.
|
||||
*
|
||||
* @package TP Chat
|
||||
* @version 3.0
|
||||
* @author Joey Kimsey <Joey@thetempusproject.com>
|
||||
* @link https://TheTempusProject.com
|
||||
* @license https://opensource.org/licenses/MIT [MIT LICENSE]
|
||||
*/
|
||||
namespace TheTempusProject\Controllers;
|
||||
|
||||
use TheTempusProject\Hermes\Functions\Redirect;
|
||||
use TheTempusProject\Bedrock\Functions\Upload;
|
||||
use TheTempusProject\Bedrock\Functions\Check;
|
||||
use TheTempusProject\Bedrock\Functions\Input;
|
||||
use TheTempusProject\Bedrock\Functions\Session;
|
||||
use TheTempusProject\Houdini\Classes\Issues;
|
||||
use TheTempusProject\Houdini\Classes\Views;
|
||||
use TheTempusProject\Classes\Controller;
|
||||
use TheTempusProject\Classes\Forms;
|
||||
use TheTempusProject\Models\Chat as ChatModel;
|
||||
use TheTempusProject\Models\Upload as UploadModel;
|
||||
use TheTempusProject\TheTempusProject as App;
|
||||
use TheTempusProject\Houdini\Classes\Components;
|
||||
use TheTempusProject\Houdini\Classes\Template;
|
||||
use TheTempusProject\Canary\Canary as Debug;
|
||||
|
||||
class Chat extends Controller {
|
||||
protected static $chat;
|
||||
public function __construct() {
|
||||
parent::__construct();
|
||||
self::$chat = new ChatModel;
|
||||
Template::setTemplate( 'chat' );
|
||||
}
|
||||
|
||||
public function index() {
|
||||
if ( !App::$isMember ) {
|
||||
Session::flash( 'error', 'You do not have permission to view this page.' );
|
||||
return Redirect::home();
|
||||
}
|
||||
self::$title = '{SITENAME} Chat';
|
||||
self::$pageDescription = 'One of the privleges of membership is the ability to chat with your fellow members.';
|
||||
|
||||
if ( App::$isLoggedIn ) {
|
||||
Components::set( 'CREATE_MESSAGE', Views::simpleView( 'chat.create' ) );
|
||||
} else {
|
||||
Components::set( 'CREATE_MESSAGE', '' );
|
||||
}
|
||||
|
||||
$upload = '';
|
||||
$sharePlugin = 'TheTempusProject\Plugins\Fileshare';
|
||||
if ( class_exists( $sharePlugin ) ) {
|
||||
$plugin = new $sharePlugin;
|
||||
if ( $plugin->checkEnabled() ) {
|
||||
$upload = Views::simpleView( 'chat.upload' );
|
||||
}
|
||||
}
|
||||
Components::set( 'FILE_UPLOAD', $upload );
|
||||
|
||||
Components::set( 'CHAT', Views::simpleView( 'chat.chat' ) );
|
||||
return Views::view( 'chat.index' );
|
||||
}
|
||||
|
||||
public function sendMessage() {
|
||||
Template::setTemplate( 'api' );
|
||||
$out = [ 'response' => true ];
|
||||
if ( !Forms::check( 'newChatMessage' ) ) {
|
||||
$out = [ 'response' => false ];
|
||||
echo json_encode($out);
|
||||
return;
|
||||
}
|
||||
self::$chat->create(
|
||||
Input::post( 'chatMessage' ),
|
||||
);
|
||||
echo json_encode($out);
|
||||
return;
|
||||
}
|
||||
|
||||
public function uploadFile() {
|
||||
Template::setTemplate( 'api' );
|
||||
$out = [ 'response' => true ];
|
||||
if ( ! Forms::check( 'newFileUpload' ) ) {
|
||||
$out = [ 'response' => false ];
|
||||
echo json_encode($out);
|
||||
return;
|
||||
}
|
||||
|
||||
$sharePlugin = 'TheTempusProject\Plugins\Fileshare';
|
||||
if ( ! class_exists( $sharePlugin ) ) {
|
||||
$out = [ 'error' => 'Fileshare must be installed and enabled for this feature to work1.' ];
|
||||
echo json_encode($out);
|
||||
return;
|
||||
}
|
||||
|
||||
$plugin = new $sharePlugin;
|
||||
|
||||
if ( ! $plugin->checkEnabled() ) {
|
||||
$out = [ 'error' => 'Fileshare must be installed and enabled for this feature to work2.' ];
|
||||
echo json_encode($out);
|
||||
return;
|
||||
}
|
||||
|
||||
$folder = UPLOAD_DIRECTORY . App::$activeUser->username . DIRECTORY_SEPARATOR;
|
||||
if ( ! Upload::image( 'file', $folder ) ) {
|
||||
$out = [ 'error' => 'could not upload image' ];
|
||||
echo json_encode($out);
|
||||
return;
|
||||
}
|
||||
|
||||
$route = str_replace( APP_ROOT_DIRECTORY, '', $folder );
|
||||
$location = $route . Upload::last();
|
||||
$uploads = new UploadModel;
|
||||
$result = $uploads->create( 'Chat Upload', $location );
|
||||
|
||||
if ( ! $result ) {
|
||||
$out = [ 'error' => 'could not add upload to fileshare.' ];
|
||||
echo json_encode( $out );
|
||||
return;
|
||||
}
|
||||
|
||||
self::$chat->create(
|
||||
'Shared a file with the chat: ' .
|
||||
'<a href="/' . $location . '" target="_blank">Upload</a>'
|
||||
);
|
||||
echo json_encode($out);
|
||||
return;
|
||||
}
|
||||
|
||||
public function getMessages() {
|
||||
Template::setTemplate( 'api' );
|
||||
echo Views::simpleView( 'chat.chat', self::$chat->recent( 50 ) );
|
||||
return;
|
||||
}
|
||||
|
||||
public function getMessageEvents() {
|
||||
if ($_SERVER['HTTP_ACCEPT'] !== 'text/event-stream') {
|
||||
Debug::info( 'connection refused, wrong HTTP_ACCEPT' );
|
||||
exit();
|
||||
}
|
||||
|
||||
header("X-Accel-Buffering: no");
|
||||
header('Content-Type: text/event-stream');
|
||||
header('Cache-Control: no-cache');
|
||||
header('Connection: keep-alive');
|
||||
|
||||
// Ensure unlimited script execution time
|
||||
set_time_limit(0);
|
||||
|
||||
// Disable output buffering
|
||||
ini_set('output_buffering', 'off');
|
||||
ini_set('zlib.output_compression', false);
|
||||
|
||||
if (Input::exists('lastId')) {
|
||||
$lastId = Input::get('lastId');
|
||||
} else {
|
||||
$lastId = 0;
|
||||
}
|
||||
|
||||
while ( true ) {
|
||||
echo "id: 0\n";
|
||||
echo "event: ping\n";
|
||||
echo 'data: {"time": "' . time() . '"}';
|
||||
echo "\n\n";
|
||||
if ( connection_aborted() ) {
|
||||
Debug::info( 'getMessageEvents connection aborted' );
|
||||
break;
|
||||
}
|
||||
|
||||
$newMessages = self::$chat->sinceMessage( $lastId );
|
||||
if ( ! empty( $newMessages )) {
|
||||
foreach ( $newMessages as $message ) {
|
||||
$lastId = $message->ID;
|
||||
echo "id: {$message->ID}\n";
|
||||
echo "data: " . json_encode($message) . "\n\n";
|
||||
}
|
||||
}
|
||||
|
||||
// If there were any messages added, flush the output buffer
|
||||
if (ob_get_contents()) {
|
||||
ob_end_flush();
|
||||
}
|
||||
flush();
|
||||
// sessions will block the end-user from sending messages unless we close the session
|
||||
session_write_close();
|
||||
sleep(1);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user