2018-12-07 05:19:51 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Classes;
|
|
|
|
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
|
|
|
|
|
|
|
use Sock\{SocketClient,SocketException};
|
|
|
|
|
2018-12-10 11:59:02 +00:00
|
|
|
use App\User;
|
2018-12-07 05:19:51 +00:00
|
|
|
use App\Classes\Frame as FrameClass;
|
|
|
|
use App\Models\Frame as FrameModel;
|
2018-12-08 22:43:18 +00:00
|
|
|
use App\Models\Mode;
|
2018-12-07 05:19:51 +00:00
|
|
|
|
|
|
|
abstract class Server {
|
2018-12-08 22:43:18 +00:00
|
|
|
private $mo = NULL; // Our Mode object
|
2018-12-08 23:13:51 +00:00
|
|
|
protected $blp = 0; // Size of Bottom Line Pollution
|
|
|
|
protected $pid = NULL; // Client PID
|
2018-12-07 05:19:51 +00:00
|
|
|
|
2018-12-08 22:43:18 +00:00
|
|
|
public function __construct(Mode $o)
|
2018-12-07 05:19:51 +00:00
|
|
|
{
|
2018-12-08 22:43:18 +00:00
|
|
|
$this->mo = $o;
|
|
|
|
|
2018-12-10 11:59:02 +00:00
|
|
|
define('MODE_BL',1); // Typing a * command on the baseline
|
|
|
|
define('MODE_FIELD',2); // typing into an imput field
|
|
|
|
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);
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
define('FRAMETYPE_INFO','i');
|
|
|
|
define('FRAMETYPE_ACTION','a');
|
|
|
|
define('FRAMETYPE_LOGIN','l');
|
|
|
|
|
|
|
|
// Keyboard presses
|
|
|
|
define('KEY_LEFT',chr(136));
|
|
|
|
define('KEY_RIGHT',chr(137));
|
|
|
|
define('KEY_DOWN',chr(138));
|
|
|
|
define('KEY_UP',chr(139));
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
2018-12-08 22:43:18 +00:00
|
|
|
define('MSG_SENDORNOT', GREEN.'KEY 1 TO SEND, 2 NOT TO SEND');
|
|
|
|
define('MSG_SENT', GREEN.'MESSAGE SENT - KEY _ TO CONTINUE');
|
|
|
|
define('MSG_NOTSENT', GREEN.'MESSAGE NOT SENT - KEY _ TO CONTINUE');
|
|
|
|
|
|
|
|
define('ERR_DATABASE', RED.'UNAVAILABLE AT PRESENT - PLSE TRY LATER');
|
|
|
|
define('ERR_NOTSENT', WHITE.'MESSAGE NOT SENT DUE TO AN ERROR');
|
|
|
|
define('ERR_PRIVATE', WHITE.'PRIVATE PAGE'.GREEN.'- FOR EXPLANATION *37_..');
|
|
|
|
define('ERR_ROUTE', WHITE.'MISTAKE?'.GREEN.'TRY AGAIN OR TELL US ON *08');
|
|
|
|
define('ERR_PAGE',ERR_ROUTE);
|
2018-12-10 11:59:02 +00:00
|
|
|
define('ERR_USER_ALREADYMEMBER', RED.'ALREADY MEMBER OF CUG');
|
2018-12-08 22:43:18 +00:00
|
|
|
|
|
|
|
define('MSG_TIMEWARP_ON', WHITE.'TIMEWARP ON'.GREEN.'VIEW INFO WITH *02');
|
|
|
|
define('MSG_TIMEWARP_OFF', WHITE.'TIMEWARP OFF'.GREEN.'VIEWING DATE IS FIXED');
|
|
|
|
define('MSG_TIMEWARP_TO', GREEN.'TIMEWARP TO %s');
|
|
|
|
define('MSG_TIMEWARP', WHITE.'OTHER VERSIONS EXIST'.GREEN.'KEY *02 TO VIEW');
|
2018-12-07 05:19:51 +00:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-12-07 05:19:51 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
|
2018-12-08 23:13:51 +00:00
|
|
|
$this->pid = getmypid();
|
|
|
|
$this->log('info','Connection from: ',['client'=>$client->getAddress(),'server'=>$this->mo->name]);
|
2018-12-07 05:19:51 +00:00
|
|
|
|
|
|
|
// We are now the child.
|
|
|
|
try {
|
|
|
|
$session_init = $session_option = FALSE;
|
|
|
|
$session_note = ''; // TCP Session Notice
|
|
|
|
$session_term = ''; // TCP Terminal Type
|
|
|
|
|
|
|
|
$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
|
|
|
|
|
2018-12-08 22:43:18 +00:00
|
|
|
$client->send(CLS.COFF);
|
2018-12-07 05:19:51 +00:00
|
|
|
|
|
|
|
// 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.
|
|
|
|
$cmd = ''; // Current *command being typed in
|
|
|
|
$mode = FALSE; // Current mode.
|
2018-12-10 11:59:02 +00:00
|
|
|
$user = User::find(1); // The logged in user
|
2018-12-07 05:19:51 +00:00
|
|
|
|
|
|
|
$current = []; // Attributes about the current page
|
|
|
|
// field/fieldnum indexes are for fields on the active page
|
|
|
|
$current['fieldreset'] = FALSE; // Flag to reset position (used in fields)
|
|
|
|
$current['fieldpos'] = 0; // For current field, position within.
|
|
|
|
$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'])) {
|
|
|
|
$page = ['frame'=>$config['loginpage'],'index'=>'a'];
|
|
|
|
} else if (!empty($service['start_page'])) {
|
|
|
|
$page = ['frame'=>$service['start_page'],'index'=>'a'];
|
|
|
|
} else {
|
|
|
|
$page = ['frame'=>'98','index'=>'a']; // next page
|
|
|
|
}
|
|
|
|
|
|
|
|
while ($action != ACTION_TERMINATE) {
|
|
|
|
// Read a character from the client session
|
|
|
|
$read = $client->read(1);
|
|
|
|
|
|
|
|
// 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 != '') {
|
|
|
|
dump(sprintf('Mode: [%s] CMD: [%s] frame: [%s] Received [%s (%s)]', $mode, $cmd, $page['frame'].$page['index'], $read, ord($read)));
|
|
|
|
|
|
|
|
// Client initiation input
|
|
|
|
// TELNET http://pcmicro.com/netfoss/telnet.html
|
|
|
|
if ($read == TCP_IAC OR $session_init OR $session_option) {
|
2018-12-08 23:13:51 +00:00
|
|
|
$this->log('debug',sprintf('Session Char (%s)',ord($read)),['init'=>$session_init,'option'=>$session_option]);
|
2018-12-07 05:19:51 +00:00
|
|
|
|
|
|
|
switch ($read) {
|
|
|
|
// Command being sent.
|
|
|
|
case TCP_IAC:
|
|
|
|
$session_init = TRUE;
|
|
|
|
$session_note = 'IAC ';
|
|
|
|
|
|
|
|
continue 2;
|
|
|
|
|
|
|
|
case TCP_SB:
|
|
|
|
$session_option = TRUE;
|
|
|
|
|
|
|
|
continue 2;
|
|
|
|
|
|
|
|
case TCP_SE:
|
|
|
|
$session_option = $session_init = FALSE;
|
2018-12-08 23:13:51 +00:00
|
|
|
$this->log('debug',sprintf('Session Terminal: %s',$session_term));
|
2018-12-07 05:19:51 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TCP_DO:
|
|
|
|
$session_note .= 'DO ';
|
|
|
|
|
|
|
|
continue 2;
|
|
|
|
|
|
|
|
case TCP_WILL:
|
|
|
|
$session_note .= 'WILL ';
|
|
|
|
|
|
|
|
continue 2;
|
|
|
|
|
|
|
|
case TCP_WONT:
|
|
|
|
$session_note .= 'WONT ';
|
|
|
|
|
|
|
|
continue 2;
|
|
|
|
|
|
|
|
case TCP_OPT_TERMTYPE:
|
|
|
|
|
|
|
|
continue 2;
|
|
|
|
|
|
|
|
case TCP_OPT_ECHO:
|
|
|
|
$session_note .= 'ECHO';
|
|
|
|
$session_init = FALSE;
|
|
|
|
$read = '';
|
2018-12-08 23:13:51 +00:00
|
|
|
|
|
|
|
$this->log('debug',sprintf('Session Note: %s',$session_note));
|
2018-12-07 05:19:51 +00:00
|
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case TCP_OPT_SUP_GOAHEAD:
|
|
|
|
$session_note .= 'SUPPRESS GO AHEAD';
|
|
|
|
$session_init = FALSE;
|
|
|
|
$read = '';
|
2018-12-08 23:13:51 +00:00
|
|
|
|
|
|
|
$this->log('debug',sprintf('Session Note: %s',$session_note));
|
2018-12-07 05:19:51 +00:00
|
|
|
|
|
|
|
continue;
|
|
|
|
|
2018-12-08 22:43:18 +00:00
|
|
|
case TCP_OPT_WINDOWSIZE:
|
|
|
|
$session_note .= 'WINDOWSIZE';
|
|
|
|
$session_init = FALSE;
|
|
|
|
$read = '';
|
2018-12-08 23:13:51 +00:00
|
|
|
|
|
|
|
$this->log('debug',sprintf('Session Note: %s',$session_note));
|
2018-12-08 22:43:18 +00:00
|
|
|
|
|
|
|
continue;
|
|
|
|
|
2018-12-07 05:19:51 +00:00
|
|
|
default:
|
|
|
|
if ($session_option AND $read) {
|
|
|
|
$session_term .= $read;
|
|
|
|
$read = '';
|
|
|
|
|
|
|
|
} else {
|
2018-12-08 23:13:51 +00:00
|
|
|
$this->log('debug',sprintf('Unhandled char in session_init: %s (%s)',$read,ord($read)));
|
2018-12-07 05:19:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ($mode) {
|
|
|
|
// Key presses during field input.
|
|
|
|
case MODE_FIELD:
|
|
|
|
dump(sprintf('** Processing Keypress in MODE_FIELD [%s (%s)]. Last POS [%s]',$read,ord($read),$current['fieldpos']));
|
|
|
|
$cmd = '';
|
|
|
|
$action = FALSE;
|
|
|
|
|
|
|
|
switch ($fo->type()) {
|
2018-12-10 11:59:02 +00:00
|
|
|
// Login frame.
|
|
|
|
case 'l':
|
2018-12-07 05:19:51 +00:00
|
|
|
// Response frame.
|
|
|
|
case 'a':
|
|
|
|
switch ($read) {
|
|
|
|
// End of field entry.
|
|
|
|
case HASH:
|
|
|
|
// Next Field
|
|
|
|
$current['fieldnum']++;
|
|
|
|
$current['fieldpos'] = 0;
|
|
|
|
|
|
|
|
if ($current['fieldnum'] < $fo->fields->count()) {
|
|
|
|
$current['fieldnum'] = $fo->getFieldId('edit',$current['fieldnum']);
|
|
|
|
|
|
|
|
if ($current['fieldnum'] !== FALSE) {
|
|
|
|
$current['field'] = $fo->getField($current['fieldnum']);
|
|
|
|
|
|
|
|
$client->send($this->outputPosition($current['field']->x,$current['field']->y).CON);
|
|
|
|
$mode = MODE_FIELD;
|
|
|
|
|
2018-12-10 11:59:02 +00:00
|
|
|
// There were no (more) editable fields.
|
2018-12-07 05:19:51 +00:00
|
|
|
} else {
|
|
|
|
$action = ACTION_SUBMITRF;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// Finished all editable fields.
|
|
|
|
$action = ACTION_SUBMITRF;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STAR:
|
|
|
|
$current['prevmode'] = MODE_FIELD;
|
|
|
|
$action = ACTION_STAR;
|
|
|
|
$current['fieldpos'] = 0;
|
|
|
|
$fielddata[$current['fieldnum']] = '';
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KEY_LEFT:
|
|
|
|
if ($current['fieldpos']--)
|
|
|
|
$client->send(LEFT);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KEY_RIGHT:
|
|
|
|
if ($current['fieldpos']++ < $current['field']->length)
|
|
|
|
$client->send(RIGHT);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KEY_DOWN:
|
|
|
|
if ($current['fieldpos'] + 40 < $current['field']->length) {
|
|
|
|
$client->send(DOWN);
|
|
|
|
$current['fieldpos'] = $current['fieldpos'] + 40;
|
|
|
|
};
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KEY_UP:
|
|
|
|
if ($current['fieldpos'] - 40 >= 0) {
|
|
|
|
$client->send($read);
|
|
|
|
$current['fieldpos'] = $current['fieldpos'] - 40;
|
|
|
|
};
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CR:
|
|
|
|
// On second or later line of a field
|
|
|
|
if ($current['fieldpos'] + $current['field']->x > 40) {
|
|
|
|
$client->send($read);
|
|
|
|
$current['fieldpos'] = (($current['fieldpos'] + $current['field']->x) % 40) * 40;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$client->send($this->outputPosition($current['field']->x,$current['field']->y).CON);
|
|
|
|
$current['fieldpos'] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ESC:
|
|
|
|
break;;
|
|
|
|
|
|
|
|
// Record Data Entry
|
|
|
|
default:
|
|
|
|
if (ord($read) > 31 && $current['fieldpos'] < $current['field']->length) {
|
2018-12-10 11:59:02 +00:00
|
|
|
if (! array_get($fielddata,$current['fieldnum']))
|
|
|
|
$fielddata[$current['fieldnum']] = '';
|
|
|
|
|
2018-12-07 05:19:51 +00:00
|
|
|
$fielddata[$current['fieldnum']]{$current['fieldpos']} = $read;
|
|
|
|
$current['fieldpos']++;
|
|
|
|
$client->send($read);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Other Frame Types - Shouldnt get here.
|
|
|
|
default:
|
|
|
|
$client->close();
|
|
|
|
|
|
|
|
throw new \Exception('Shouldnt get here', 500);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Form submission: 1 to send, 2 not to send.
|
|
|
|
case MODE_SUBMITRF:
|
|
|
|
switch ($read) {
|
|
|
|
// @todo Input received, process it.
|
|
|
|
case '1':
|
2018-12-10 11:59:02 +00:00
|
|
|
// dump(['line'=>__LINE__,'f' => $fielddata]);
|
2018-12-07 05:19:51 +00:00
|
|
|
// @todo if send successful or not
|
|
|
|
if (TRUE) {
|
|
|
|
$this->sendBaseline($client,MSG_SENT);
|
|
|
|
$mode = MODE_RFSENT;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$this->sendBaseline($client,ERR_NOTSENT);
|
|
|
|
$mode = MODE_RFERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '2':
|
|
|
|
$this->sendBaseline($client,MSG_NOTSENT);;
|
|
|
|
$mode = MODE_RFNOTSENT;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STAR:
|
|
|
|
$action = ACTION_STAR;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Response form after Sent processing
|
|
|
|
case MODE_RFSENT:
|
|
|
|
$client->send(COFF);
|
|
|
|
|
|
|
|
if ($read == HASH) {
|
|
|
|
if (! empty($pagedata['route1'])) {
|
|
|
|
$action = ACTION_GOTO;
|
|
|
|
$page['frame'] = $pagedata['route1'];
|
|
|
|
$page['index'] = 'a';
|
|
|
|
|
|
|
|
} elseif (FrameModel::where('frame',$fo->frame())->where('index',$fo->index_next())->exists()) {
|
|
|
|
$action = ACTION_GOTO;
|
|
|
|
$page['frame'] = array_get($current,'page.frame');
|
|
|
|
$page['index'] = chr(1 + ord($page['index']));
|
|
|
|
|
|
|
|
} elseif (! empty($pagedata['route0'])) {
|
|
|
|
$action = ACTION_GOTO;
|
|
|
|
$page['frame'] = $pagedata['route0'];
|
|
|
|
$page['index'] = 'a';
|
|
|
|
|
|
|
|
// No further routes defined, go home.
|
|
|
|
} else {
|
|
|
|
$action = ACTION_GOTO;
|
|
|
|
$page['frame'] = '0';
|
|
|
|
$page['index'] = 'a';
|
|
|
|
}
|
|
|
|
|
|
|
|
} elseif ($read == STAR) {
|
|
|
|
$action = ACTION_STAR;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Response form after NOT sending
|
|
|
|
case MODE_RFNOTSENT:
|
|
|
|
|
|
|
|
// Response form ERROR
|
|
|
|
case MODE_RFERROR:
|
|
|
|
$client->send(COFF);
|
|
|
|
|
|
|
|
if ($read == HASH) {
|
|
|
|
if (! empty($pagedata['route2'])) {
|
|
|
|
$action = ACTION_GOTO;
|
|
|
|
$page['frame'] = $pagedata['route2'];
|
|
|
|
$page['index'] = 'a';
|
|
|
|
|
|
|
|
} elseif (FrameModel::where('frame',$fo->frame())->where('index',$fo->index_next())->exists()) {
|
|
|
|
$action = ACTION_GOTO;
|
|
|
|
$page['frame'] = $fo->frame();
|
|
|
|
$page['index'] = $fo->index_next();
|
|
|
|
|
|
|
|
} elseif (! empty($pagedata['route0'])) {
|
|
|
|
$action = ACTION_GOTO;
|
|
|
|
$page['frame'] = $pagedata['route0'];
|
|
|
|
$page['index'] = 'a';
|
|
|
|
|
|
|
|
// No further routes defined, go home.
|
|
|
|
} else {
|
|
|
|
$action = ACTION_GOTO;
|
|
|
|
$page['frame'] = '0';
|
|
|
|
$page['index'] = 'a';
|
|
|
|
}
|
|
|
|
|
|
|
|
} elseif ($read == STAR) {
|
|
|
|
$action = ACTION_STAR;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
// List of alternate frames has been presented
|
|
|
|
case MODE_WARPTO:
|
|
|
|
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');
|
2018-12-07 05:19:51 +00:00
|
|
|
$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':
|
|
|
|
if ($frame = $fo->route($read)) {
|
|
|
|
$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 "was waiting for page number\n";
|
|
|
|
|
|
|
|
// if it's a number, continue entry
|
|
|
|
if (strpos('0123456789', $read) !== FALSE) {
|
|
|
|
$cmd .= $read;
|
|
|
|
$client->send($read);
|
|
|
|
$this->blp++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
$this->sendBaseline($client,($timewarp ? MSG_TIMEWARP_ON : MSG_TIMEWARP_OFF));
|
|
|
|
$cmd = '';
|
|
|
|
$action = $mode = FALSE;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Present Timewarp Frames
|
|
|
|
if ($cmd === '02') {
|
|
|
|
$client->send(COFF);
|
|
|
|
$action = ACTION_INFO;
|
|
|
|
$cmd = '';
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reload page
|
|
|
|
if ($cmd === '09') {
|
|
|
|
$client->send(COFF);
|
|
|
|
$action = ACTION_GOTO;
|
|
|
|
$cmd = '';
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Another star aborts the command.
|
|
|
|
if ($read === STAR) {
|
|
|
|
$action = FALSE;
|
|
|
|
$this->sendBaseline($client,'');
|
|
|
|
$cmd = '';
|
|
|
|
|
|
|
|
if ($current['prevmode'] == MODE_FIELD) {
|
|
|
|
$mode = $current['prevmode'];
|
|
|
|
$current['prevmode'] = FALSE;
|
|
|
|
$client->send($this->outputPosition($current['field']->x,$current['field']->y).CON);
|
|
|
|
$client->send(str_repeat('.', $current['field']->length));
|
|
|
|
$current['fieldreset'] = TRUE;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$mode = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Complete request
|
|
|
|
if ($read === HASH) {
|
|
|
|
$client->send(COFF);
|
|
|
|
$timewarpalt = FALSE;
|
|
|
|
|
|
|
|
// Nothing typed between * and #
|
|
|
|
if ($cmd === '') {
|
|
|
|
$action = ACTION_BACKUP;
|
|
|
|
|
|
|
|
// *# means go back
|
|
|
|
} else {
|
|
|
|
$page['frame'] = $cmd;
|
|
|
|
$page['index'] = 'a';
|
|
|
|
$action = ACTION_GOTO;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear the command, we are finished processing
|
|
|
|
$cmd = '';
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2018-12-08 23:13:51 +00:00
|
|
|
$this->log('debug','Not sure what we were doing?');
|
2018-12-07 05:19:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This section performs some action if it is deemed necessary
|
|
|
|
if ($action) {
|
|
|
|
echo "Performing action $action\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ($action) {
|
|
|
|
case ACTION_STAR:
|
|
|
|
echo " star command started\n";
|
|
|
|
$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;
|
|
|
|
|
|
|
|
// GO Backwards
|
|
|
|
case ACTION_BACKUP:
|
|
|
|
// Do we have anywhere to go, drop the current page from the history.
|
|
|
|
if ($history->count() > 1)
|
|
|
|
$history->pop();
|
|
|
|
|
|
|
|
$page = $history->last();
|
|
|
|
|
2018-12-08 23:13:51 +00:00
|
|
|
$this->log('debug','Backing up to:',$page);
|
2018-12-07 05:19:51 +00:00
|
|
|
|
|
|
|
// Go to next index frame.
|
|
|
|
case ACTION_NEXT:
|
|
|
|
// We need this extra test in case we come from ACTION_BACKUP
|
|
|
|
if ($action == ACTION_NEXT)
|
|
|
|
{
|
|
|
|
$current['page']['index'] = $fo->index();
|
|
|
|
$page['index'] = $fo->index_next();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look for requested page
|
|
|
|
case ACTION_GOTO:
|
|
|
|
// If we wanted a "Searching..." message, this is where to put it.
|
|
|
|
try {
|
|
|
|
$fo = $timewarpalt
|
2018-12-08 22:43:18 +00:00
|
|
|
? $this->mo->frame(FrameModel::findOrFail($timewarpalt))
|
|
|
|
: $this->mo->frameLoad($page['frame'],$page['index'],$this);
|
2018-12-07 05:19:51 +00:00
|
|
|
|
2018-12-08 23:13:51 +00:00
|
|
|
$this->log('debug',sprintf('Fetched frame: %s',$fo->id()));
|
|
|
|
|
2018-12-07 05:19:51 +00:00
|
|
|
} catch (ModelNotFoundException $e) {
|
|
|
|
$this->sendBaseline($client,ERR_PAGE);
|
|
|
|
$mode = $action = FALSE;
|
|
|
|
|
2018-12-08 22:43:18 +00:00
|
|
|
// We initialise $fo anyway
|
|
|
|
$fo = $this->mo->frame($this->testFrame());
|
|
|
|
|
2018-12-07 05:19:51 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-12-10 11:59:02 +00:00
|
|
|
// Is there a user logged in
|
|
|
|
if ($user)
|
|
|
|
{
|
|
|
|
if ($fo->isFramePublic() AND $fo->isAccessible())
|
|
|
|
{
|
|
|
|
if ($fo->type() == FRAMETYPE_LOGIN AND $user->isMemberCUG($fo->getCUG()))
|
|
|
|
{
|
|
|
|
$this->sendBaseline($client,ERR_USER_ALREADYMEMBER);
|
|
|
|
$mode = $action = FALSE;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this is a login frame and the user is already a member.
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if (! $fo->isOwner($user))
|
|
|
|
{
|
|
|
|
if (! $fo->isAccessible())
|
|
|
|
{
|
|
|
|
$this->sendBaseline($client,ERR_PAGE);
|
|
|
|
$mode = $action = FALSE;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! $user->isMemberCUG($fo->getCUG()))
|
|
|
|
{
|
|
|
|
$this->sendBaseline($client, ERR_PRIVATE);
|
|
|
|
$mode = $action = FALSE;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2018-12-07 05:19:51 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-10 11:59:02 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
// Is this a public frame in CUG 0?
|
|
|
|
if (! $fo->isCUG(0) OR ! $fo->isFramePublic())
|
|
|
|
{
|
|
|
|
$this->sendBaseline($client,ERR_PAGE);
|
|
|
|
$mode = $action = FALSE;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-07 05:19:51 +00:00
|
|
|
$current['page'] = $fo->page(TRUE);
|
|
|
|
$current['fieldpos'] = 0;
|
|
|
|
|
|
|
|
// Only if new location, not going backwards
|
|
|
|
if (($history->last() != $page) AND ($action == ACTION_GOTO || $action == ACTION_NEXT)) {
|
|
|
|
$history->push($page);
|
|
|
|
}
|
|
|
|
|
|
|
|
// drop into
|
|
|
|
case ACTION_RELOAD:
|
|
|
|
// @todo Move this $output into the object.
|
|
|
|
if ($fo->hasFlag('clear'))
|
|
|
|
{
|
|
|
|
$this->blp = 0;
|
|
|
|
$output = CLS;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$output = HOME
|
|
|
|
;
|
|
|
|
// Clear the baseline.
|
|
|
|
$this->sendBaseline($client,'');
|
|
|
|
}
|
|
|
|
|
|
|
|
$output .= (string)$fo;
|
|
|
|
|
|
|
|
if ($timewarpalt) {
|
|
|
|
$this->sendBaseline($client,sprintf(MSG_TIMEWARP_TO,$fo->created() ? $fo->created()->format('Y-m-d H:i:s') : 'UNKNOWN'));
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ($fo->type()) {
|
|
|
|
default:
|
2018-12-10 11:59:02 +00:00
|
|
|
// Standard Frame
|
2018-12-07 05:19:51 +00:00
|
|
|
case 'i':
|
|
|
|
$client->send($output);
|
|
|
|
$mode = $action = false;
|
2018-12-08 22:43:18 +00:00
|
|
|
|
2018-12-07 05:19:51 +00:00
|
|
|
break;
|
|
|
|
|
2018-12-10 11:59:02 +00:00
|
|
|
// Login Frame.
|
|
|
|
case 'l':
|
2018-12-07 05:19:51 +00:00
|
|
|
// Active Frame. Prestel uses this for a Response Frame.
|
|
|
|
case 'a':
|
|
|
|
$client->send($output);
|
|
|
|
// holds data entered by user.
|
|
|
|
$fielddata = [];
|
|
|
|
|
|
|
|
if (count($fo->fields)) {
|
|
|
|
// Get our first editable field.
|
|
|
|
$current['fieldnum'] = $fo->getFieldId('edit',0);
|
|
|
|
$current['field'] = $fo->getField($current['fieldnum']);
|
|
|
|
$current['fieldreset'] = TRUE;
|
|
|
|
|
|
|
|
if ($current['fieldnum'] !== FALSE) {
|
|
|
|
$mode = MODE_FIELD;
|
|
|
|
|
|
|
|
// There were no editable fields.
|
|
|
|
} else {
|
|
|
|
$mode = MODE_COMPLETE;
|
|
|
|
$client->send(COFF);
|
|
|
|
}
|
|
|
|
|
|
|
|
$current['fieldpos'] = 0;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$mode = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Terminate Frame
|
|
|
|
case 't':
|
|
|
|
$client->send($output);
|
|
|
|
$action = ACTION_TERMINATE;
|
|
|
|
|
2018-12-08 22:43:18 +00:00
|
|
|
break;
|
2018-12-07 05:19:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Timewarp Mode
|
|
|
|
case ACTION_INFO:
|
|
|
|
$mode = $action = FALSE;
|
|
|
|
$cmd = '';
|
|
|
|
$y = 0;
|
|
|
|
|
|
|
|
$output = $this->outputPosition(0, $y++) . WHITE . NEWBG . RED . 'TIMEWARP INFO FOR Pg.' . BLUE . $fo->page(). WHITE;
|
|
|
|
//$output .= $this->outputPosition(0, $y++) . WHITE . NEWBG . BLUE . 'Service : ' . substr($service['service_name'] . str_repeat(' ', 27), 0, 27);
|
|
|
|
//$output .= $this->outputPosition(0, $y++) . WHITE . NEWBG . BLUE . 'Varient : ' . substr($varient['varient_name'] . str_repeat(' ', 27), 0, 27);
|
|
|
|
$output .= $this->outputPosition(0, $y++) . WHITE . NEWBG . BLUE . 'Dated : ' .substr(($fo->created() ? $fo->created()->format('j F Y') : 'Unknown').str_repeat(' ', 27), 0, 27);
|
|
|
|
|
|
|
|
$alts = $fo->alts()->get();
|
|
|
|
|
|
|
|
if (count($alts)) {
|
|
|
|
$n = 1;
|
|
|
|
|
|
|
|
$output .= $this->outputPosition(0, $y++) . WHITE . 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.$n++;
|
|
|
|
}
|
|
|
|
|
|
|
|
$line .= BLUE.$date.' '.$o->note;
|
|
|
|
|
2018-12-08 22:43:18 +00:00
|
|
|
$output .= $this->outputPosition(0,$y++).$line.str_repeat(' ',40-$this->strlenv($line)); // @todo should use frame::page_length
|
2018-12-07 05:19:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($timewarp) {
|
|
|
|
$mode = MODE_WARPTO;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
$client->send($output);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need to reposition the cursor to the current field.
|
|
|
|
if ($current['fieldreset'] !== FALSE) {
|
|
|
|
$client->send($this->outputPosition($current['field']->x,$current['field']->y).CON);
|
|
|
|
$current['fieldreset'] = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-07 05:19:51 +00:00
|
|
|
|
2018-12-08 23:13:51 +00:00
|
|
|
return;
|
2018-12-07 05:19:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Something bad happened. We'll log it and then disconnect.
|
|
|
|
} catch (\Exception $e) {
|
2018-12-08 22:43:18 +00:00
|
|
|
Log::emergency($e->getMessage(),['line'=>$e->getLine(),'file'=>$e->getFile()]);
|
2018-12-07 05:19:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$client->close();
|
2018-12-08 23:13:51 +00:00
|
|
|
$this->log('debug',sprintf('Disconnected: %s',$client->getaddress()));
|
2018-12-07 05:19:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Move the cursor via the shortest path.
|
|
|
|
*/
|
|
|
|
function outputPosition($x,$y) {
|
|
|
|
// Take the shortest path.
|
|
|
|
if ($y < 12) {
|
|
|
|
return HOME.
|
|
|
|
(($x < 21)
|
|
|
|
? str_repeat(DOWN,$y).str_repeat(RIGHT,$x)
|
|
|
|
: str_repeat(DOWN,$y+1).str_repeat(LEFT,40-$x));
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return HOME.str_repeat(UP,24-$y).
|
|
|
|
(($x < 21)
|
|
|
|
? str_repeat(RIGHT,$x)
|
|
|
|
: str_repeat(LEFT,40-$x));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2018-12-08 22:43:18 +00:00
|
|
|
abstract function sendBaseline($client,$text,$reposition=FALSE);
|
2018-12-07 05:19:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate the length of text
|
|
|
|
*
|
|
|
|
* ESC characters are two chars, and need to be counted as one.
|
|
|
|
*
|
|
|
|
* @param $text
|
|
|
|
* @return int
|
|
|
|
*/
|
2018-12-08 22:43:18 +00:00
|
|
|
abstract function strlenv($text):int;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a test frame appropriate for this server.
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
abstract public function testFrame();
|
2018-12-07 05:19:51 +00:00
|
|
|
}
|