97 lines
2.6 KiB
PHP
97 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Models\Frame;
|
|
use App\Models\Mode;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
|
|
|
class FrameImport extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'frame:import {frame} {index} {file} '.
|
|
'{--access=0 : Is frame accessible }'.
|
|
'{--public=0 : Is frame limited to CUG }'.
|
|
'{--cost=0 : Frame Cost }'.
|
|
'{--mode=videotex : Frame Emulation Mode }'.
|
|
'{--replace : Replace existing frame}'.
|
|
'{--type=i : Frame Type}'.
|
|
'{--trim : Trim off header (first 40 chars)}';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Import frames into the database. The frames should be in binary format.';
|
|
|
|
/**
|
|
* Create a new command instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
}
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*
|
|
* @return mixed
|
|
* @throws \Exception
|
|
*/
|
|
public function handle()
|
|
{
|
|
if (! is_numeric($this->argument('frame')))
|
|
throw new \Exception('Frame is not numeric: '.$this->argument('frame'));
|
|
|
|
if (strlen($this->argument('index')) != 1 OR ! preg_match('/^[a-z]$/',$this->argument('index')))
|
|
throw new \Exception('Subframe failed validation');
|
|
|
|
if (! file_exists($this->argument('file')))
|
|
throw new \Exception('File not found: '.$this->argument('file'));
|
|
|
|
$mo = Mode::where('name',$this->option('mode'))->firstOrFail();
|
|
|
|
$o = new Frame;
|
|
if ($this->option('replace')) {
|
|
try {
|
|
$o = $o->where('frame',$this->argument('frame'))
|
|
->where('index',$this->argument('index'))
|
|
->where('mode_id',$mo->id)
|
|
->firstOrFail();
|
|
|
|
} catch (ModelNotFoundException $e) {
|
|
$this->error('Page not found to replace: '.$this->argument('frame').$this->argument('index'));
|
|
die(1);
|
|
}
|
|
|
|
} else {
|
|
$o->frame = $this->argument('frame');
|
|
$o->index = $this->argument('index');
|
|
$o->mode_id = $mo->id;
|
|
$o->access = $this->option('access');
|
|
$o->public = $this->option('public');
|
|
$o->cost = $this->option('cost');
|
|
$o->type = $this->option('type');
|
|
}
|
|
|
|
// We need to escape any back slashes.
|
|
$o->content = str_replace('\\','\\\\',($this->option('trim'))
|
|
? substr(file_get_contents($this->argument('file')),40)
|
|
: file_get_contents($this->argument('file')));
|
|
|
|
// If we have 0x1aSAUCE, we'll discard the sauce.
|
|
if ($x = strpos($o->content,chr(0x1a).'SAUCE')) {
|
|
$o->content = substr($o->content,0,$x-1).chr(0x0a);
|
|
}
|
|
|
|
$o->save();
|
|
}
|
|
} |