2018-12-01 21:50:34 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Classes;
|
|
|
|
|
2018-12-10 11:59:02 +00:00
|
|
|
use App\Models\Mode;
|
2018-12-01 21:50:34 +00:00
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
|
2018-12-10 11:59:02 +00:00
|
|
|
use App\User;
|
|
|
|
use App\Models\CUG;
|
|
|
|
|
2018-12-01 21:50:34 +00:00
|
|
|
/**
|
|
|
|
* Handles all aspects of frame
|
|
|
|
*
|
|
|
|
* Frame are constructed:
|
|
|
|
* + First line is the header, displaying TITLE/CUG TITLE|PAGE #|COST
|
|
|
|
* + Up to $frame_length for content
|
|
|
|
* + Input/Status Line
|
|
|
|
*
|
|
|
|
* NOTES:
|
|
|
|
* + Frames are stored in binary. ESC codes are stored as a single char < 32.
|
|
|
|
* + Header is on line 1.
|
|
|
|
* + Input field is on Line 24.
|
|
|
|
* + 'i' Frames are info frames, no looking for fields. (Lines 2-23)
|
|
|
|
* + 'a' Frames have active frames with responses.
|
|
|
|
* + 't' Frames terminate the session
|
|
|
|
*
|
2018-12-05 10:50:38 +00:00
|
|
|
* + Frame types:
|
|
|
|
* + 'ip' Frames are Information Provider frames - no header added. (Lines 1-23)
|
|
|
|
*
|
2018-12-01 21:50:34 +00:00
|
|
|
* To Consider
|
|
|
|
* + 'x' External frames - living in another viewdata server
|
|
|
|
*
|
|
|
|
* @package App\Classes
|
|
|
|
*/
|
2018-12-08 22:43:18 +00:00
|
|
|
abstract class Frame
|
2018-12-01 21:50:34 +00:00
|
|
|
{
|
2018-12-08 22:43:18 +00:00
|
|
|
protected $frame = NULL;
|
|
|
|
protected $output = NULL;
|
2018-12-01 21:50:34 +00:00
|
|
|
|
2018-12-08 22:43:18 +00:00
|
|
|
// All this vars should be overridden in the child class
|
|
|
|
/*
|
|
|
|
protected $frame_length = 22;
|
|
|
|
protected $frame_width = 40;
|
|
|
|
|
|
|
|
protected $header_length = 20; // 20
|
|
|
|
protected $pagenum_length = 9; // 11 (prefixed with a color, suffixed with frame)
|
|
|
|
protected $cost_length = 7; // 9 (prefixed with a color, suffixed with unit)
|
|
|
|
protected $cost_unit = 'u';
|
|
|
|
*/
|
2018-12-01 21:50:34 +00:00
|
|
|
|
|
|
|
public $fields = NULL; // The fields in this frame.
|
|
|
|
|
2018-12-05 10:50:38 +00:00
|
|
|
// Magic Fields that are pre-filled
|
2018-12-08 22:43:18 +00:00
|
|
|
protected $fieldmap = [
|
2018-12-05 10:50:38 +00:00
|
|
|
'a'=>'address#',
|
|
|
|
'd'=>'%date',
|
|
|
|
];
|
|
|
|
|
|
|
|
// Fields that are editable
|
|
|
|
private $fieldoptions = [
|
|
|
|
'a'=>['edit'=>TRUE], // Address
|
|
|
|
'p'=>['edit'=>TRUE], // Password
|
|
|
|
'u'=>['edit'=>TRUE], // User
|
|
|
|
];
|
|
|
|
|
2018-12-01 21:50:34 +00:00
|
|
|
// @todo Move this to the database
|
|
|
|
private $header = RED.'T'.BLUE.'E'.GREEN.'S'.YELLOW.'T'.MAGENTA.'!';
|
|
|
|
|
|
|
|
public function __construct(\App\Models\Frame $o,string $msg=NULL)
|
|
|
|
{
|
|
|
|
$this->frame = $o;
|
|
|
|
|
|
|
|
$this->output = $this->hasFlag('clear') ? CLS : HOME;
|
|
|
|
|
2018-12-05 10:50:38 +00:00
|
|
|
// If we have a message to display on the bottom line.
|
2018-12-01 21:50:34 +00:00
|
|
|
if ($msg)
|
|
|
|
$this->output .= UP.$msg.HOME;
|
|
|
|
|
2018-12-05 10:50:38 +00:00
|
|
|
$startline = 0;
|
|
|
|
|
2018-12-01 21:50:34 +00:00
|
|
|
if (! $this->hasFlag('ip')) {
|
|
|
|
// Set the page header: CUG/Site Name | Page # | Cost
|
|
|
|
$this->output .= $this->render_header($this->header).
|
2018-12-03 12:59:22 +00:00
|
|
|
$this->render_page($this->frame->frame,$this->frame->index).
|
2018-12-01 21:50:34 +00:00
|
|
|
$this->render_cost($this->frame->cost);
|
2018-12-05 10:50:38 +00:00
|
|
|
|
|
|
|
$startline = 1;
|
2018-12-01 21:50:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate fields and render output.
|
2018-12-08 22:43:18 +00:00
|
|
|
$this->fields = collect(); // Fields in this frame.
|
2018-12-05 10:50:38 +00:00
|
|
|
$this->fields($startline);
|
2018-12-01 21:50:34 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 10:50:38 +00:00
|
|
|
/**
|
|
|
|
* Render the frame
|
|
|
|
*
|
|
|
|
* @return null|string
|
|
|
|
*/
|
2018-12-01 21:50:34 +00:00
|
|
|
public function __toString()
|
|
|
|
{
|
|
|
|
return $this->output;
|
|
|
|
}
|
|
|
|
|
2018-12-06 03:09:43 +00:00
|
|
|
/**
|
|
|
|
* Return a list of alternative versions of this frame.
|
2018-12-10 11:59:02 +00:00
|
|
|
*
|
|
|
|
* @todo: Need to adjust to not include access=0 frames unless owner
|
2018-12-06 03:09:43 +00:00
|
|
|
*/
|
2018-12-10 11:59:02 +00:00
|
|
|
public function alts(Mode $o)
|
2018-12-06 03:09:43 +00:00
|
|
|
{
|
|
|
|
return \App\Models\Frame::where('frame',$this->frame())
|
|
|
|
->where('index',$this->index())
|
|
|
|
->where('id','<>',$this->frame->id)
|
2018-12-10 11:59:02 +00:00
|
|
|
->where('mode_id',$o->id)
|
2018-12-06 03:09:43 +00:00
|
|
|
->limit(9);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Frame Created Date
|
|
|
|
*/
|
|
|
|
public function created()
|
|
|
|
{
|
|
|
|
return $this->frame->created_at;
|
|
|
|
}
|
|
|
|
|
2018-12-01 21:50:34 +00:00
|
|
|
/**
|
|
|
|
* Convert the frame from Binary to Output
|
|
|
|
* Look for fields within the frame.
|
|
|
|
*
|
|
|
|
* @param int $startline
|
|
|
|
*/
|
2018-12-08 22:43:18 +00:00
|
|
|
abstract public function fields($startline=0,$fieldchar='.');
|
2018-12-01 21:50:34 +00:00
|
|
|
|
|
|
|
/**
|
2018-12-05 10:50:38 +00:00
|
|
|
* Returns the current frame.
|
|
|
|
*/
|
|
|
|
public function frame()
|
|
|
|
{
|
|
|
|
return $this->frame->frame;
|
|
|
|
}
|
|
|
|
|
2018-12-10 11:59:02 +00:00
|
|
|
/**
|
|
|
|
* Get the CUG for a frame
|
|
|
|
*
|
|
|
|
* Frame CUG are derived from their frame number.
|
|
|
|
* EG: Frame 642 is a member of 642, or 64, or 6, or 0, whichever matches first.
|
|
|
|
*
|
|
|
|
* @return CUG
|
|
|
|
*/
|
|
|
|
public function getCUG()
|
|
|
|
{
|
|
|
|
$co = NULL;
|
|
|
|
$frame = $this->frame->frame;
|
|
|
|
|
|
|
|
while (! $co)
|
|
|
|
{
|
|
|
|
$co = CUG::find($frame);
|
|
|
|
|
|
|
|
if (! $co) {
|
|
|
|
$frame = substr($frame,0,strlen($frame)-1);
|
|
|
|
|
|
|
|
if (! $frame)
|
|
|
|
$frame = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $co;
|
|
|
|
}
|
|
|
|
|
2018-12-05 10:50:38 +00:00
|
|
|
/**
|
|
|
|
* Return the current field configuration
|
|
|
|
*/
|
|
|
|
public function getField(int $id)
|
|
|
|
{
|
|
|
|
return $this->fields->get($id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a specific key of the field options that passes a filter test
|
|
|
|
*
|
|
|
|
* @param string $type
|
|
|
|
* @param int $after
|
|
|
|
* @return mixed
|
2018-12-01 21:50:34 +00:00
|
|
|
*/
|
2018-12-05 10:50:38 +00:00
|
|
|
public function getFieldId($type='edit',$after=0)
|
2018-12-01 21:50:34 +00:00
|
|
|
{
|
2018-12-05 10:50:38 +00:00
|
|
|
return $this->fields
|
|
|
|
->search(function($item,$key) use ($type,$after) {
|
|
|
|
return $key >= $after AND $this->isFieldEditable($item->type);
|
|
|
|
});
|
2018-12-01 21:50:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the flag for this page
|
|
|
|
*
|
|
|
|
* CLEAR: Clear Screen before rendering.
|
|
|
|
*
|
|
|
|
* @param $flag
|
|
|
|
* @return bool
|
|
|
|
*/
|
2018-12-05 10:50:38 +00:00
|
|
|
public function hasFlag($flag)
|
2018-12-01 21:50:34 +00:00
|
|
|
{
|
|
|
|
return $this->frame->hasFlag($flag);
|
|
|
|
}
|
|
|
|
|
2018-12-05 10:50:38 +00:00
|
|
|
/**
|
|
|
|
* Return the frame DB id
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function id()
|
|
|
|
{
|
|
|
|
return $this->frame->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Current frame index
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function index()
|
|
|
|
{
|
|
|
|
return $this->frame->index;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the next index
|
|
|
|
*/
|
|
|
|
public function index_next()
|
|
|
|
{
|
|
|
|
return chr(ord($this->frame->index)+1);
|
|
|
|
}
|
|
|
|
|
2018-12-10 11:59:02 +00:00
|
|
|
public function isAccessible():bool
|
|
|
|
{
|
|
|
|
return $this->frame->access ? TRUE : FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if the frame is a particular CUG
|
|
|
|
* @param int $cug
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isCUG(int $cug): bool
|
|
|
|
{
|
|
|
|
return ($this->getCUG()->id == $cug);
|
|
|
|
}
|
|
|
|
|
2018-12-05 10:50:38 +00:00
|
|
|
/**
|
|
|
|
* Determine if a field is editable
|
|
|
|
*
|
|
|
|
* @param string $field
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function isFieldEditable(string $field)
|
|
|
|
{
|
|
|
|
return array_get(array_get($this->fieldoptions,$field),'edit',FALSE);
|
|
|
|
}
|
|
|
|
|
2018-12-10 11:59:02 +00:00
|
|
|
/**
|
|
|
|
* Is this frame Public
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isFramePublic(): bool
|
|
|
|
{
|
|
|
|
return $this->frame->closed ? FALSE : TRUE;
|
|
|
|
}
|
|
|
|
// @todo To implement
|
|
|
|
|
|
|
|
public function isOwner(User $o):bool
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2018-12-05 10:50:38 +00:00
|
|
|
/**
|
|
|
|
* Return the Page Number
|
|
|
|
*/
|
|
|
|
public function page(bool $as_array=FALSE)
|
|
|
|
{
|
|
|
|
return $as_array ? ['frame'=>$this->frame->frame,'index'=>$this->frame->index] : $this->frame->page;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the next page number.
|
|
|
|
*
|
|
|
|
* @param bool $as_array
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function pagenext(bool $as_array=FALSE)
|
|
|
|
{
|
|
|
|
return $as_array ? ['frame'=>$this->frame->frame,'index'=>$this->index_next()] : $this->frame->frame.$this->index_next();
|
|
|
|
}
|
|
|
|
|
2018-12-01 21:50:34 +00:00
|
|
|
/**
|
|
|
|
* Render the cost of the frame
|
|
|
|
*
|
|
|
|
* @param int $cost
|
|
|
|
* @return string
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
private function render_cost(int $cost)
|
|
|
|
{
|
|
|
|
if ($cost > 999)
|
|
|
|
throw new \Exception('Price too high');
|
|
|
|
|
|
|
|
if ($cost > 100)
|
|
|
|
$color = RED;
|
|
|
|
elseif ($cost > 0)
|
|
|
|
$color = YELLOW;
|
|
|
|
else
|
|
|
|
$color = GREEN;
|
|
|
|
|
2018-12-08 22:43:18 +00:00
|
|
|
return sprintf($color.'% '.static::$cost_length.'.0f%s',$cost,static::$cost_unit);
|
2018-12-01 21:50:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render the Site Header
|
|
|
|
*
|
|
|
|
* @param string $header
|
|
|
|
* @return bool|string
|
|
|
|
*/
|
|
|
|
private function render_header(string $header)
|
|
|
|
{
|
2018-12-08 22:43:18 +00:00
|
|
|
$filler = ($this->strlenv($header) < static::$header_length) ? str_repeat(' ',static::$header_length-$this->strlenv($header)) : '';
|
2018-12-01 21:50:34 +00:00
|
|
|
|
2018-12-08 22:43:18 +00:00
|
|
|
return substr($header.$filler,0,static::$header_length+strlen($header)-$this->strlenv($header));
|
2018-12-01 21:50:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render the Frame Number
|
|
|
|
*
|
|
|
|
* @param int $num
|
|
|
|
* @param string $frame
|
|
|
|
* @return string
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
private function render_page(int $num,string $frame)
|
|
|
|
{
|
|
|
|
if ($num > 999999999)
|
|
|
|
throw new \Exception('Page Number too big',500);
|
|
|
|
|
|
|
|
if (strlen($frame) !== 1)
|
|
|
|
throw new \Exception('Frame invalid',500);
|
|
|
|
|
2018-12-08 22:43:18 +00:00
|
|
|
return sprintf(WHITE.'% '.static::$pagenum_length.'.0f%s',$num,$frame);
|
2018-12-01 21:50:34 +00:00
|
|
|
}
|
|
|
|
|
2018-12-03 12:59:22 +00:00
|
|
|
/**
|
|
|
|
* Get the route for the key press
|
|
|
|
*
|
|
|
|
* @param string $read
|
|
|
|
*/
|
|
|
|
public function route(string $read)
|
|
|
|
{
|
|
|
|
// @todo
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2018-12-01 21:50:34 +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;
|
2018-12-01 21:50:34 +00:00
|
|
|
|
2018-12-08 22:43:18 +00:00
|
|
|
public static function testFrame(Server $so)
|
2018-12-01 21:50:34 +00:00
|
|
|
{
|
|
|
|
// Simulate a DB load
|
|
|
|
$o = new \App\Models\Frame;
|
|
|
|
|
2018-12-03 12:59:22 +00:00
|
|
|
$o->content = '';
|
2018-12-01 21:50:34 +00:00
|
|
|
$o->flags = ['ip'];
|
2018-12-03 12:59:22 +00:00
|
|
|
$o->type = 'a';
|
|
|
|
$o->frame = 999;
|
|
|
|
$o->index = 'a';
|
2018-12-10 11:59:02 +00:00
|
|
|
$o->access = 1;
|
|
|
|
$o->closed = 0;
|
2018-12-01 21:50:34 +00:00
|
|
|
|
|
|
|
// Header
|
2018-12-08 22:43:18 +00:00
|
|
|
$sid = R_RED.'T'.R_BLUE.'E'.R_GREEN.'S'.R_YELLOW.'T';
|
|
|
|
$o->content .= substr($sid.'-'.str_repeat('12345678901234567890',4),0,static::$header_length+(strlen($sid)-$so->strlenv($sid))).
|
2018-12-01 21:50:34 +00:00
|
|
|
R_WHITE.'999999999a'.R_RED.sprintf('%07.0f',999).'u';
|
|
|
|
|
2018-12-03 12:59:22 +00:00
|
|
|
$o->content .= str_repeat('+-',18).' '.R_RED.'01';
|
|
|
|
$o->content .= 'Name: '.ESC.str_repeat('u',5).str_repeat('+-',14);
|
2018-12-05 10:50:38 +00:00
|
|
|
$o->content .= 'Date: '.ESC.str_repeat('d',25).str_repeat('+-',4);
|
|
|
|
$o->content .= 'Address: '.ESC.str_repeat('a',19).' '.str_repeat('+-',5);
|
|
|
|
$o->content .= ' : '.ESC.str_repeat('a',19).' '.str_repeat('+-',5);
|
2018-12-01 21:50:34 +00:00
|
|
|
|
|
|
|
return $o;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the Frame Type
|
|
|
|
*/
|
|
|
|
public function type()
|
|
|
|
{
|
|
|
|
return $this->frame->type();
|
|
|
|
}
|
|
|
|
}
|