This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
vbbs/app/Classes/Server.php

1042 lines
29 KiB
PHP
Raw Permalink Normal View History

<?php
namespace App\Classes;
use Illuminate\Support\Facades\Log;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Sock\{SocketClient,SocketException};
use App\User;
use App\Classes\Frame as FrameClass;
use App\Models\Frame as FrameModel;
use App\Models\Mode;
abstract class Server {
2019-07-12 03:42:01 +00:00
// Our Mode object, this determines if we are an ANSI or VIDEOTEX server
private $mo = NULL;
// The client connection object of the currently connected client.
public $co = NULL;
// The currently rendered Frame to the client of Frame/*
public $fo = NULL;
// Size of Bottom Line Pollution
protected $blp = 0;
// Whats on the baseline currently
protected $baseline = '';
// Client PID
protected $pid = NULL;
public function __construct(Mode $o)
{
$this->mo = $o;
2018-12-11 12:31:44 +00:00
define('MODE_BL', 1); // Typing a * command on the baseline
define('MODE_FIELD', 2); // typing into an input field
2018-12-11 12:31:44 +00:00
define('MODE_WARPTO', 3); // awaiting selection of a timewarp
define('MODE_COMPLETE', 4); // Entry of data is complete ..
define('MODE_SUBMITRF', 5); // asking if should send or not.
define('MODE_RFSENT', 6);
define('MODE_RFERROR', 7);
define('MODE_RFNOTSENT', 8);
2019-07-12 03:42:01 +00:00
define('MODE_CONTROL', 9); // Do nothing, a method is controlling input
2018-12-11 12:31:44 +00:00
define('ACTION_RELOAD', 1);
define('ACTION_GOTO', 2);
define('ACTION_BACKUP', 3);
define('ACTION_NEXT', 4);
define('ACTION_INFO', 5);
define('ACTION_TERMINATE', 6);
define('ACTION_SUBMITRF', 7); // Offer to submit a response frame
define('ACTION_STAR', 8);
2019-07-12 03:42:01 +00:00
define('ACTION_EDIT', 9); // Edit current frame
2018-12-25 01:48:57 +00:00
define('CONTROL_TELNET', 1); // Telnet session control
define('CONTROL_METHOD', 2); // Send input to an external method
2019-07-12 03:42:01 +00:00
define('CONTROL_EDIT', 3); // Controller to edit frame
2018-12-11 12:31:44 +00:00
define('TCP_IAC', chr(255));
define('TCP_DONT', chr(254));
define('TCP_DO', chr(253));
define('TCP_WONT', chr(252));
define('TCP_WILL', chr(251));
define('TCP_SB', chr(250));
define('TCP_AYT', chr(246));
define('TCP_SE', chr(240));
define('TCP_BINARY', chr(0));
define('TCP_OPT_ECHO', chr(1));
define('TCP_OPT_SUP_GOAHEAD', chr(3));
define('TCP_OPT_TERMTYPE', chr(24));
define('TCP_OPT_WINDOWSIZE', chr(31));
define('TCP_OPT_LINEMODE', chr(34));
2019-07-12 03:42:01 +00:00
// Status messages
define('MSG_SENDORNOT', GREEN.SPACE.'KEY 1 TO SEND, 2 NOT TO SEND');
define('MSG_SENT', GREEN.SPACE.'MESSAGE SENT - KEY '.HASH.' TO CONTINUE');
define('MSG_NOTSENT', GREEN.SPACE.'MESSAGE NOT SENT - KEY '.HASH.' TO CONTINUE');
define('ERR_DATABASE', RED.SPACE.'UNAVAILABLE AT PRESENT - PLS TRY LATER');
define('ERR_NOTSENT', RED.SPACE.'MESSAGE NOT SENT DUE TO AN ERROR');
define('ERR_PRIVATE', WHITE.SPACE.'PRIVATE PAGE'.GREEN.SPACE.'- FOR EXPLANATION *37'.HASH.'..');
define('ERR_ROUTE', WHITE.SPACE.'MISTAKE?'.GREEN.SPACE.'TRY AGAIN OR TELL US ON *08');
define('ERR_PAGE',ERR_ROUTE);
define('ERR_USER_ALREADYMEMBER', RED.SPACE.'ALREADY MEMBER OF CUG');
define('MSG_TIMEWARP_ON', WHITE.SPACE.'TIMEWARP ON'.GREEN.SPACE.'VIEW INFO WITH *02');
define('MSG_TIMEWARP_OFF', WHITE.SPACE.'TIMEWARP OFF'.GREEN.SPACE.'VIEWING DATE IS FIXED');
define('MSG_TIMEWARP_TO', GREEN.SPACE.'TIMEWARP TO %s');
define('MSG_TIMEWARP', WHITE.SPACE.'OTHER VERSIONS EXIST'.GREEN.SPACE.'KEY *02 TO VIEW');
}
2019-07-12 03:42:01 +00:00
/**
* Write something to the system log.
*
* @param string $mode
* @param string $message
* @param array $data
*/
2018-12-08 23:13:51 +00:00
public function log(string $mode,string $message,array $data=[])
{
Log::$mode(sprintf('%s: %s',$this->pid,$message),$data);
}
/**
* Connection handler
*
* @param SocketClient $client
* @return bool|void
* @throws SocketException
*/
function onConnect(SocketClient $client) {
$pid = pcntl_fork();
if ($pid == -1)
throw new SocketException(SocketException::CANT_ACCEPT,'Could not fork process');
// Parent return ready for next connection
elseif ($pid)
return;
2019-07-12 03:42:01 +00:00
// The next page we will load - this should have frame=,index=
$next_page = NULL;
2018-12-11 12:31:44 +00:00
$this->co = $client;
2018-12-08 23:13:51 +00:00
$this->pid = getmypid();
$this->log('info','Connection from: ',['client'=>$client->getAddress(),'server'=>$this->mo->name]);
// We are now the child.
try {
2018-12-25 01:48:57 +00:00
$session = NULL; // TCP Session Details
$client->send(TCP_IAC.TCP_DO.TCP_OPT_SUP_GOAHEAD); // DO SUPPRES GO AHEAD
$client->send(TCP_IAC.TCP_WONT.TCP_OPT_LINEMODE); // WONT LINEMODE
$client->send(TCP_IAC.TCP_DO.TCP_OPT_ECHO); // DO ECHO
// $client->send(TCP_IAC.TCP_AYT); // AYT
$client->send(TCP_IAC.TCP_DO.TCP_OPT_TERMTYPE.TCP_IAC.TCP_SB.TCP_OPT_TERMTYPE.TCP_OPT_ECHO.TCP_IAC.TCP_SE); // Request Term Type
$client->send(CLS.COFF);
$client->send('Press a key...');
// Setup VARS
$timewarp = FALSE; // Is timewarp active.
$timewarpalt = FALSE; // Alternative timewarp frame to get
$history = collect(); // Page history for going backwards
$action = ACTION_GOTO; // Initial action.
$control = FALSE; // Logic in control
$mode = FALSE; // Current mode.
$cmd = ''; // Current *command being typed in
2018-12-11 12:31:44 +00:00
$user = new User; // The logged in user
2018-12-25 01:48:57 +00:00
$method = collect(); // Method in control for CONTROL_METHOD
$current = []; // Attributes about the current page
$current['prevmode'] = FALSE; // Previous mode - in case we need to go back to MODE_FIELD
// @todo Get the login/start page, and if it is not available, throw the ERR_DATEBASE error.
if (isset($config['loginpage'])) {
2019-07-12 03:42:01 +00:00
$next_page = ['frame'=>$config['loginpage']];
} else if (!empty($service['start_page'])) {
2019-07-12 03:42:01 +00:00
$next_page = ['frame'=>$service['start_page']];
} else {
2019-07-12 03:42:01 +00:00
$next_page = ['frame'=>'980']; // Default Login Page
}
while ($action != ACTION_TERMINATE) {
// Read a character from the client session
2019-07-12 03:42:01 +00:00
// @todo Add a timeout to read, and log user off if timeout exceeded.
$read = $client->read(1);
2019-07-12 03:42:01 +00:00
echo "\n";
printf(". Got: %s (%s): Mode: [%s], Action: [%s], Control: [%s]\n",
(ord($read) < 32 ? '.' : $read),
ord($read),
serialize($mode),
serialize($action),
$control);
// It appears that read will return '' instead of false when a disconnect has occurred.
// We'll set it to NULL so its caught later
if ($read === '')
$read = NULL;
if ($read != '') {
2018-12-25 01:48:57 +00:00
if ($read == TCP_IAC) {
// If we are not already in a TELNET LOOP
if ($control !== CONTROL_TELNET) {
$control = CONTROL_TELNET;
2018-12-25 01:48:57 +00:00
// Remember our Telnet Session Object
2019-07-12 03:42:01 +00:00
if (! $session)
2018-12-25 01:48:57 +00:00
$session = Control::factory('telnet',$this);
2018-12-25 01:48:57 +00:00
$method->push($session);
}
}
if ($mode != MODE_BL AND $control AND $method->count()) {
2019-07-12 03:42:01 +00:00
printf("= Start CONTROL: Going to method: %s\n",get_class($method->last()));
2018-12-08 23:13:51 +00:00
2018-12-25 01:48:57 +00:00
// Capture our state when we enter this method.
if (! array_key_exists('control',$method->last()->state)) {
$method->last()->state['control'] = $control;
$method->last()->state['action'] = $action;
}
2018-12-25 01:48:57 +00:00
$method->last()->state['mode'] = $mode;
$action = FALSE;
2018-12-25 01:48:57 +00:00
// Pass Control to Method
$read = $method->last()->handle($read,$current);
$mode = $method->last()->state['mode'];
2018-12-08 23:13:51 +00:00
2018-12-25 01:48:57 +00:00
if ($method->last()->complete()) {
2019-07-12 03:42:01 +00:00
printf("- Complete CONTROL: %s\n",get_class($method->last()));
2018-12-25 01:48:57 +00:00
$save = $method->pop();
2018-12-25 01:48:57 +00:00
if ($method->count()) {
$control = $method->last()->state['control'];
2018-12-25 01:48:57 +00:00
} else {
$mode = $save->state['mode'];
$action = $save->state['action'];
$control = FALSE;
}
}
2019-07-12 03:42:01 +00:00
printf("- End CONTROL: Read %s (%s): Mode: [%s], Action: [%s], Control: [%s]\n",
(ord($read) < 32 ? '.' : $read),
ord($read),
serialize($mode),
serialize($action),
$control);
}
2019-07-12 03:42:01 +00:00
printf("= Start MODE: %s\n",serialize($mode));
switch ($mode) {
// Key presses during field input.
case MODE_FIELD:
$cmd = '';
$action = FALSE;
2019-07-12 03:42:01 +00:00
switch ($this->fo->type()) {
// Login frame.
2018-12-11 12:31:44 +00:00
case Frame::FRAMETYPE_LOGIN:
switch ($read) {
case HASH:
// If we are the main login screen, see if it is a new user
2019-07-12 03:42:01 +00:00
if ($this->fo->isCUG(0))
2018-12-11 12:31:44 +00:00
{
if ($this->fo->getField()->type == 't' AND $this->fo->getFieldCurrentInput() == 'NEW')
2018-12-11 12:31:44 +00:00
{
$action = ACTION_GOTO;
2019-07-12 03:42:01 +00:00
$next_page = ['frame'=>'981']; // @todo This should be in the DB.
2018-12-25 01:48:57 +00:00
break 2;
2018-12-11 12:31:44 +00:00
}
}
break;
}
// Response frame.
2018-12-11 12:31:44 +00:00
case Frame::FRAMETYPE_ACTION:
switch ($read) {
// End of field entry.
case CR:
case HASH:
// Next Field
$this->fo->setFieldNext();
if ($x=$this->fo->getField()) {
$client->send($this->moveCursor($x->x,$x->y).CON);
$mode = MODE_FIELD;
// Finished all editable fields.
} else {
$action = ACTION_SUBMITRF;
}
break;
case STAR:
$current['prevmode'] = MODE_FIELD;
$action = ACTION_STAR;
break;
2018-12-11 12:31:44 +00:00
case KEY_DELETE:
if ($this->fo->setFieldCurrentInputDelete())
2019-07-12 03:42:01 +00:00
$client->send(LEFT.$this->fo::$if_filler.LEFT);
break;
case ESC:
2018-12-25 01:48:57 +00:00
break;
// Record Data Entry
default:
if (ord($read) > 31 && strlen($this->fo->getFieldCurrentInput()) < $this->fo->getField()->length) {
$this->fo->setFieldCurrentInput($read);
2018-12-25 01:48:57 +00:00
$client->send($this->fo->isFieldCurrentMask() ?: $read);
}
}
break;
// Other Frame Types - Shouldnt get here.
default:
$client->close();
2019-07-12 03:42:01 +00:00
throw new \Exception('Shouldnt get here',500);
}
break;
// Form submission: 1 to send, 2 not to send.
case MODE_SUBMITRF:
switch ($read) {
case '1':
2018-12-25 01:48:57 +00:00
// If we are in a control method, complete it
if ($control AND $method->count()) {
$method->last()->process();
2019-07-12 03:42:01 +00:00
} elseif ($this->fo->route(1) == '*' OR is_numeric($this->fo->route(1))) {
$this->sendBaseline($client,RED.'NO ACTION PERFORMED');
$mode = MODE_RFSENT;
2019-07-12 03:42:01 +00:00
} elseif ($ao = FrameClass\Action::factory($this->fo->route(1),$this,$user,$action,$mode)) {
$ao->handle();
2018-12-11 12:31:44 +00:00
$mode = $ao->mode;
$action = $ao->action;
2019-07-12 03:42:01 +00:00
// Is this a user logging in?
if ($ao->uo->exists AND $ao instanceof FrameClass\Action\Login)
{
$user = $ao->uo;
$history = collect();
}
2018-12-11 12:31:44 +00:00
if ($ao->page)
2019-07-12 03:42:01 +00:00
$next_page = $ao->page;
2018-12-11 12:31:44 +00:00
} else {
2019-07-12 03:42:01 +00:00
$this->sendBaseline($client,RED.'NO method exists...');
2018-12-11 12:31:44 +00:00
$mode = MODE_RFSENT;
}
break;
case '2':
// @todo Check if HASH is a valid next destination
2018-12-25 01:48:57 +00:00
$this->sendBaseline($client,MSG_NOTSENT);
$mode = MODE_RFNOTSENT;
2018-12-25 01:48:57 +00:00
// If a Control method was rejected, we can clear it
2018-12-29 12:24:41 +00:00
if ($control AND $method->count()) {
$save = $method->pop();
2018-12-25 01:48:57 +00:00
2018-12-29 12:24:41 +00:00
if ($method->count()) {
$control = $method->last()->state['control'];
} else {
$mode = $save->state['mode'];
$action = $save->state['action'];
$control = FALSE;
}
}
break;
case STAR:
$action = ACTION_STAR;
break;
}
break;
// Response form after Sent processing
case MODE_RFSENT:
$client->send(COFF);
if ($read == HASH) {
2019-07-12 03:42:01 +00:00
if ($x = $this->fo->route(2) AND $x !== '*' AND is_numeric($x)) {
$next_page = ['frame'=>$x];
2019-07-12 03:42:01 +00:00
} elseif (FrameModel::where('frame',$this->fo->frame())->where('index',$this->fo->index_next())->exists()) {
$next_page = ['frame'=>$this->fo->frame(),'index'=>$this->fo->index_next()];
2019-07-12 03:42:01 +00:00
} elseif ($x = $this->fo->route(0) AND $x !== '*' AND is_numeric($x)) {
$next_page = ['frame'=>$x];
// No further routes defined, go home.
} else {
2019-07-12 03:42:01 +00:00
$next_page = ['frame'=>0];
}
$action = ACTION_GOTO;
} elseif ($read == STAR) {
$action = ACTION_STAR;
break;
}
break;
// Response form after NOT sending
case MODE_RFNOTSENT:
2019-07-12 03:42:01 +00:00
// Response form ERROR
case MODE_RFERROR:
$client->send(COFF);
if ($read == HASH) {
2019-07-12 03:42:01 +00:00
if ($x = $this->fo->route(2) AND $x !== '*' AND is_numeric($x)) {
$next_page = ['frame'=>$x];
2019-07-12 03:42:01 +00:00
} elseif (FrameModel::where('frame',$this->fo->frame())->where('index',$this->fo->index_next())->exists()) {
$next_page = ['frame'=>$this->fo->frame(),'index'=>$this->fo->index_next()];
2019-07-12 03:42:01 +00:00
} elseif ($x = $this->fo->route(0) AND $x !== '*' AND is_numeric($x)) {
$next_page = ['frame'=>$x];
// No further routes defined, go home.
} else {
2019-07-12 03:42:01 +00:00
$next_page = ['frame'=>0];
}
$action = ACTION_GOTO;
} elseif ($read == STAR) {
$action = ACTION_STAR;
break;
}
break;
// List of alternate frames has been presented
case MODE_WARPTO:
2019-10-02 05:47:56 +00:00
// @todo If we are in a control, we need to terminate it.
if (is_numeric($read) AND $read) {
$timewarpalt = $alts->get($read-1)->id;
$action = ACTION_GOTO;
} elseif ($read === '0') {
$action = ACTION_RELOAD;
}
break;
// Not doing anything in particular.
case MODE_COMPLETE:
case FALSE:
2018-12-08 23:13:51 +00:00
$this->log('debug','Idle');
$cmd = '';
switch ($read) {
case HASH:
$action = ACTION_NEXT;
break;
case STAR:
$action = ACTION_STAR;
break;
// Frame Routing
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
2019-07-12 03:42:01 +00:00
if (is_numeric($this->fo->route($read))) {
$next_page = ['frame'=>$this->fo->route($read)];
2018-12-12 05:45:02 +00:00
$action = ACTION_GOTO;
} else {
$this->sendBaseline($client,ERR_ROUTE);
$mode = $action = FALSE;
}
break;
}
break;
// Currently accepting baseline input after a * was received
case MODE_BL:
echo "- Waiting for Page Number\n";
// if it's a number, continue entry
2019-07-12 03:42:01 +00:00
if (strpos('0123456789',$read) !== FALSE) {
$client->send($read);
$this->blp++;
2019-07-12 03:42:01 +00:00
$cmd .= $read;
}
// If its a backspace, delete last input
if ($read === KEY_DELETE AND strlen($cmd))
{
$client->send(BS.' '.BS);
$this->blp--;
$cmd = substr($cmd,0,-1);
}
// if we hit a special numeric command, deal with it.
// Refresh page
if ($cmd === '00') {
$client->send(COFF);
$action = ACTION_RELOAD;
$cmd = '';
break;
}
// Toggle Timewarp Mode
if ($cmd === '01') {
$client->send(COFF);
$timewarp = !$timewarp;
2019-07-12 03:42:01 +00:00
$this->sendBaseline($client,($timewarp ? MSG_TIMEWARP_ON : MSG_TIMEWARP_OFF));
$cmd = '';
$action = $mode = FALSE;
if ($current['prevmode'] == MODE_FIELD) {
$mode = $current['prevmode'];
$current['prevmode'] = FALSE;
if ($x=$this->fo->getField()) {
// @todo This WHITE should be removed, and the color set to whatever is in the frame
$client->send($this->moveCursor($x->x+strlen($this->fo->getFieldCurrentInput()),$x->y).CON.WHITE);
}
}
break;
}
// Present Timewarp Frames
if ($cmd === '02') {
$client->send(COFF);
$action = ACTION_INFO;
$cmd = '';
break;
}
2019-07-12 03:42:01 +00:00
// Edit frame
// Catch if we are going to edit a child frame
if (preg_match('/^04/',$cmd) AND preg_match('/^[a-z]$/',$read))
{
$client->send(COFF);
$next_page = ['frame'=>substr($cmd,2),'index'=>$read];
$cmd = '';
$action = ACTION_EDIT;
break;
}
// Bookmark page
if ($cmd === '05') {
2019-07-12 03:42:01 +00:00
$this->sendBaseline($client,RED.'NOT IMPLEMENTED YET?');
$mode = FALSE;
break;
}
2018-12-25 01:48:57 +00:00
// Report a problem
if ($cmd === '08') {
2019-07-12 03:42:01 +00:00
$this->sendBaseline($client,RED.'NOT IMPLEMENTED YET?');
$mode = FALSE;
2018-12-25 01:48:57 +00:00
break;
}
// Reload page
if ($cmd === '09') {
$client->send(COFF);
$action = ACTION_GOTO;
$cmd = '';
2019-07-12 03:42:01 +00:00
$next_page = $this->fo->page(TRUE);
break;
}
// Another star aborts the command.
if ($read === STAR) {
$action = FALSE;
2018-12-25 01:48:57 +00:00
$this->sendBaseline($client,array_get($current,'baseline',''));
$cmd = '';
if ($current['prevmode'] == MODE_FIELD) {
$mode = $current['prevmode'];
$current['prevmode'] = FALSE;
2018-12-25 01:48:57 +00:00
if (! $this->fo->getField())
$this->fo->setFieldPrev();
if ($x=$this->fo->getField()) {
// @todo This WHITE should be removed, and the color set to whatever is in the frame
$client->send($this->moveCursor($x->x,$x->y).CON.WHITE);
$client->send(str_repeat($this->fo::$if_filler,$x->length));
$client->send($this->moveCursor($x->x,$x->y));
$this->fo->resetCurrentFieldData();
}
} else {
$mode = FALSE;
}
break;
}
// Complete request
if ($read === HASH or $read === LF) {
$client->send(COFF);
$timewarpalt = FALSE;
2019-07-12 03:42:01 +00:00
// If input is in a control, terminate it
if ($control)
{
$method->pop();
$control = FALSE;
// Our method count should be zero
if ($method->count())
{
dump($method);
throw new \Exception('Method count should be zero, but its not...',500);
}
2019-07-12 03:42:01 +00:00
}
// Nothing typed between * and #
2018-12-11 12:31:44 +00:00
// *# means go back
if ($cmd === '') {
$action = ACTION_BACKUP;
2018-12-11 12:31:44 +00:00
} elseif ($cmd === '0') {
2019-07-12 03:42:01 +00:00
$next_page = ['frame'=>$user->exists ? 1 : 980]; // @todo Get from DB.
2018-12-11 12:31:44 +00:00
$action = ACTION_GOTO;
2019-07-12 03:42:01 +00:00
// Edit Frame
} elseif (preg_match('/^04/',$cmd)) {
$client->send(COFF);
$action = ACTION_EDIT;
$next_page = [
'frame' => substr($cmd,2) ?: $this->fo->frame(),
];
} else {
2019-07-12 03:42:01 +00:00
$next_page = ['frame'=>$cmd];
$action = ACTION_GOTO;
}
// Clear the command, we are finished processing
$cmd = '';
2019-10-02 05:47:56 +00:00
$mode = FALSE;
break;
}
break;
2019-07-12 03:42:01 +00:00
// Control is taking input
case MODE_CONTROL:
break;
default:
2018-12-08 23:13:51 +00:00
$this->log('debug','Not sure what we were doing?');
}
2019-07-12 03:42:01 +00:00
printf("- End MODE: Read %s (%s): Mode: [%s], Action: [%s], Control: [%s]\n",
(ord($read) < 32 ? '.' : $read),
ord($read),
serialize($mode),
serialize($action),
$control);
}
// This section performs some action if it is deemed necessary
2019-07-12 03:42:01 +00:00
printf("= Start ACTION: %s\n",serialize($action));
switch ($action) {
case ACTION_STAR:
echo "+ Star command...\n";
2018-12-25 01:48:57 +00:00
// If there is something on the baseline, lets preserve it
if ($this->blp)
2019-07-12 03:42:01 +00:00
{
printf(". Preserving Baseline: %s\n",$this->baseline);
2018-12-25 01:48:57 +00:00
$current['baseline'] = $this->baseline;
2019-07-12 03:42:01 +00:00
}
2018-12-25 01:48:57 +00:00
$this->sendBaseline($client,GREEN.STAR,TRUE);
$client->send(CON);
$action = FALSE;
$mode = MODE_BL;
break;
case ACTION_SUBMITRF:
$action = FALSE;
$client->send(COFF);
$this->sendBaseline($client,MSG_SENDORNOT);
$mode = MODE_SUBMITRF;
break;
2019-07-12 03:42:01 +00:00
// Edit Frame
case ACTION_EDIT:
$this->log('debug','Editing frame:',[$next_page]);
$next_fo = NULL;
// If we are editing a different frame, load it
try {
$next_fo = $this->mo->framePage($next_page['frame'],array_get($next_page,'index','a'),$this);
} catch (ModelNotFoundException $e) {
$next_fo = $this->mo->frameNew($this,$next_page['frame'],$next_page['index']);
}
$control = CONTROL_EDIT;
2019-10-02 05:47:56 +00:00
$method->push(Control::factory('editframe',$this,['fo'=>$next_fo]));
2019-07-12 03:42:01 +00:00
$next_fo = NULL;
$method->last()->state['control'] = $control;
$method->last()->state['action'] = FALSE;
$method->last()->state['mode'] = MODE_FIELD;
$mode = MODE_CONTROL;
$action = FALSE;
break;
// GO Backwards
case ACTION_BACKUP:
// Do we have anywhere to go, drop the current page from the history.
if ($history->count() > 1)
$history->pop();
2018-12-29 12:24:41 +00:00
if ($control AND $method->count()) {
$save = $method->pop();
2018-12-29 12:24:41 +00:00
// Do we still have more nested methods to complete
2018-12-29 12:24:41 +00:00
if ($method->count()) {
$control = $method->last()->state['control'];
} else {
$mode = $save->state['mode'];
$action = $save->state['action'];
$control = FALSE;
}
}
2019-07-12 03:42:01 +00:00
$next_page = $history->last();
2019-07-12 03:42:01 +00:00
// If there is no next page, we'll refresh the current page.
if ($next_page)
$this->log('debug','Backing up to:',$next_page);
// Go to next index frame.
case ACTION_NEXT:
// We need this extra test in case we come from ACTION_BACKUP
if ($action == ACTION_NEXT)
{
$next_page = [
'frame'=>$this->fo->frame(),
'index'=>$this->fo->index_next()
];
}
2019-07-12 03:42:01 +00:00
// Look for requested page - charge for it to be loaded.
case ACTION_GOTO:
2019-07-12 03:42:01 +00:00
if ($next_page OR $timewarpalt)
{
// If we wanted a "Searching..." message, this is where to put it.
try {
// Store our next frame in a temporary var while we determine if it can be displayed
$fo = $timewarpalt
? $this->mo->frameID($timewarpalt)
: $this->mo->framePage($next_page['frame'],array_get($next_page,'index','a'),$this);
2019-07-12 03:42:01 +00:00
$this->log('debug',sprintf('Fetched frame: %s (%s)',$fo->id(),$fo->page()));
2018-12-08 23:13:51 +00:00
2019-07-12 03:42:01 +00:00
} catch (ModelNotFoundException $e) {
// @todo Make sure parent frame exists, or display error
$this->sendBaseline($client,ERR_PAGE);
$mode = $action = FALSE;
2019-07-12 03:42:01 +00:00
if (! $fo)
$fo = $this->mo->frameTest($this);
2019-07-12 03:42:01 +00:00
break;
}
2019-07-12 03:42:01 +00:00
// Is there a user logged in
if ($user)
{
2019-07-12 03:42:01 +00:00
if ($fo->isFramePublic() AND $fo->isAccessible())
{
2019-07-12 03:42:01 +00:00
if ($fo->type() == Frame::FRAMETYPE_LOGIN AND $user->isMemberCUG($fo->getCUG()))
{
2019-07-12 03:42:01 +00:00
$this->sendBaseline($client,ERR_USER_ALREADYMEMBER);
$next_page = $history->last();
$mode = $action = FALSE;
2019-07-12 03:42:01 +00:00
$this->log('debug',sprintf('Frame Denied - Already Member: %s (%s)',$fo->id(),$fo->page()));
break;
}
2019-07-12 03:42:01 +00:00
// If this is a login frame and the user is already a member.
} else {
if (! $fo->isOwner($user))
{
2019-07-12 03:42:01 +00:00
if (! $fo->isAccessible())
{
$this->sendBaseline($client,ERR_PAGE);
$next_page = $history->last();
$mode = $action = FALSE;
$this->log('debug',sprintf('Frame Denied - In Accessible: %s (%s)',$fo->id(),$fo->page()));
2019-07-12 03:42:01 +00:00
break;
}
if (! $user->isMemberCUG($fo->getCUG()))
{
$this->sendBaseline($client,ERR_PRIVATE);
$next_page = $history->last();
$mode = $action = FALSE;
$this->log('debug',sprintf('Frame Denied - Not in CUG [%s]: %s (%s)',$fo->getCUG()->id,$fo->id(),$fo->page()));
break;
}
}
}
2019-07-12 03:42:01 +00:00
} else {
// Is this a public frame in CUG 0?
if (! $fo->isCUG(0) OR ! $fo->isFramePublic())
{
$this->sendBaseline($client,ERR_PAGE);
$next_page = $history->last();
$mode = $action = FALSE;
2019-07-12 03:42:01 +00:00
break;
}
}
}
// Only if new location, not going backwards
2019-07-12 03:42:01 +00:00
if (($history->last() != $next_page) AND ($action == ACTION_GOTO || $action == ACTION_NEXT)) {
$history->push($next_page);
}
2019-07-12 03:42:01 +00:00
$this->fo = $fo;
$fo = NULL;
$next_page = NULL;
$timewarpalt = NULL;
2018-12-25 01:48:57 +00:00
printf("+ Mode is: %s\n",$mode);
// drop into
case ACTION_RELOAD:
2019-07-12 03:42:01 +00:00
// Clear the baseline history
$this->sendBaseline($client,'');
2019-07-12 03:42:01 +00:00
$current['baseline'] = '';
$output = (string)$this->fo;
if ($timewarpalt) {
2019-07-12 03:42:01 +00:00
$this->sendBaseline($client,sprintf(MSG_TIMEWARP_TO,$this->fo->created() ? $this->fo->created()->format('Y-m-d H:i:s') : 'UNKNOWN'));
}
2019-07-12 03:42:01 +00:00
switch ($this->fo->type()) {
default:
// Standard Frame
2018-12-11 12:31:44 +00:00
case Frame::FRAMETYPE_INFO:
$client->send($output);
$mode = $action = FALSE;
break;
// Login Frame.
2018-12-11 12:31:44 +00:00
case Frame::FRAMETYPE_LOGIN:
2018-12-25 01:48:57 +00:00
$client->send($output);
$action = FALSE;
2018-12-25 01:48:57 +00:00
$output = '';
// If this is the registration page
// @todo Should be evaluated out of the DB
2019-07-12 03:42:01 +00:00
if ($this->fo->page() == '981a') {
2018-12-25 01:48:57 +00:00
$control = CONTROL_METHOD;
$method->push(Control::factory('register',$this));
$method->last()->state['control'] = $control;
$method->last()->state['action'] = FALSE;
2018-12-25 01:48:57 +00:00
$method->last()->state['mode'] = MODE_FIELD;
}
// Active Frame. Prestel uses this for a Response Frame.
2018-12-11 12:31:44 +00:00
case Frame::FRAMETYPE_ACTION:
$client->send($output);
2019-07-12 03:42:01 +00:00
if ($this->fo->fields()->count()) {
$this->fo->resetCurrentField();
if ($x=$this->fo->getField()) {
$mode = MODE_FIELD;
// @todo This WHITE should be removed, and the color set to whatever is in the frame
$client->send($this->moveCursor($x->x,$x->y).CON.WHITE);
// There were no editable fields.
} else {
$mode = MODE_COMPLETE;
$client->send(COFF);
}
} else {
$mode = FALSE;
}
break;
2019-10-02 05:47:56 +00:00
// External Frame - run by a control.
case FRAME::FRAMETYPE_EXTERNAL:
$external = explode(' ',$this->fo->content());
$x = Control::factory(array_shift($external),$this,$external);
if (! $x)
{
$this->sendBaseline($client,ERR_PAGE);
$mode = $action = FALSE;
break;
}
$method->push($x);
$control = CONTROL_METHOD;
$action = FALSE;
break;
// Terminate Frame
2018-12-11 12:31:44 +00:00
case Frame::FRAMETYPE_TERMINATE:
$client->send($output);
$action = ACTION_TERMINATE;
break;
}
break;
// Timewarp Mode
case ACTION_INFO:
$mode = $action = FALSE;
$cmd = '';
$y = 0;
2019-07-12 03:42:01 +00:00
$output = $this->moveCursor(0,$y++).WHITE.NEWBG.RED.'TIMEWARP INFO FOR Pg.'.BLUE.$this->fo->page().WHITE;
//$output .= $this->moveCursor(0,$y++).WHITE.NEWBG.BLUE.'Service : '.substr($service['service_name'].str_repeat(' ',27),0,27);
//$output .= $this->moveCursor(0,$y++).WHITE.NEWBG.BLUE.'Varient : '.substr($varient['varient_name'].str_repeat(' ',27),0,27);
$output .= $this->moveCursor(0,$y++).WHITE.NEWBG.BLUE.'Dated : ' .substr(($this->fo->created() ? $this->fo->created()->format('j F Y') : 'Unknown').str_repeat(' ',27),0,27);
2019-07-12 03:42:01 +00:00
$alts = $this->fo->alts($this->mo)->get();
if (count($alts)) {
$n = 1;
$output .= $this->moveCursor(0,$y++).WHITE.SPACE.NEWBG.RED.'ALTERNATIVE VERSIONS:'.str_repeat(' ',16);
foreach ($alts as $o) {
$date = $o->created_at->format('d M Y');
$line = WHITE.NEWBG;
if ($timewarp) {
$line .= RED.SPACE.$n++;
}
$line .= BLUE.SPACE.$date.' '.$o->note;
2019-07-12 03:42:01 +00:00
$output .= $this->moveCursor(0,$y++).$line.str_repeat(' ',40-$this->fo->strlenv($line)); // @todo should use frame::page_length
}
if ($timewarp) {
$mode = MODE_WARPTO;
}
}
$client->send($output);
break;
}
2019-07-12 03:42:01 +00:00
printf("- End ACTION: Read %s (%s): Mode: [%s], Action: [%s], Control: [%s]\n",
(ord($read) < 32 ? '.' : $read),
ord($read),
serialize($mode),
serialize($action),
$control);
// Did the client disconnect
if ($read === NULL || socket_last_error()) {
$client->close();
2018-12-08 23:13:51 +00:00
$this->log('debug',sprintf('Client Disconnected: %s',$client->getaddress()));
2018-12-08 23:13:51 +00:00
return;
}
}
// Something bad happened. We'll log it and then disconnect.
} catch (\Exception $e) {
Log::emergency($e->getMessage(),['line'=>$e->getLine(),'file'=>$e->getFile()]);
}
$client->close();
2018-12-08 23:13:51 +00:00
$this->log('debug',sprintf('Disconnected: %s',$client->getaddress()));
}
/**
* Move the cursor via the shortest path.
*/
2019-07-12 03:42:01 +00:00
abstract function moveCursor($x,$y);
/**
* Send a message to the base line
*
* @note "$this->blp" remembers how many chars are there, so that they can be replace with next call
* @param $client
* @param $text
* @param bool $reposition
*/
abstract function sendBaseline($client,$text,$reposition=FALSE);
}