Files
thetempusproject/app/plugins/chat/controllers/chat.php
2024-08-04 21:15:59 -04:00

192 lines
6.1 KiB
PHP

<?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);
}
}
}