initial
This commit is contained in:
173
index.php
Normal file
173
index.php
Normal file
@@ -0,0 +1,173 @@
|
||||
<?php
|
||||
|
||||
// Kickstart the framework
|
||||
// $f3=require('lib/base.php');
|
||||
require_once './vendor/autoload.php';
|
||||
$f3 = \Base::instance();
|
||||
$f3->set('DB',new DB\Jig('data/'));
|
||||
$db=new \DB\Jig('data/');
|
||||
class Item extends \DB\Jig\Mapper {
|
||||
public function __construct() {
|
||||
parent::__construct( \Base::instance()->get('DB'), 'items' );
|
||||
}
|
||||
}
|
||||
$f3->set('DEBUG',1);
|
||||
if ((float)PCRE_VERSION<8.0)
|
||||
trigger_error('PCRE version is out of date');
|
||||
|
||||
// Load configuration
|
||||
$f3->config('config.ini');
|
||||
|
||||
$f3->route('GET /',
|
||||
function($f3) {
|
||||
$f3->set('template', 'home.htm');
|
||||
echo View::instance()->render('layout.htm');
|
||||
}
|
||||
);
|
||||
$f3->route('GET /admin',
|
||||
function($f3) {
|
||||
$f3->set('template', 'home.htm');
|
||||
$f3->set('admin', true);
|
||||
echo View::instance()->render('layout.htm');
|
||||
}
|
||||
);
|
||||
$f3->route('GET /add',
|
||||
function($f3) {
|
||||
$f3->set('template', 'add.htm');
|
||||
echo View::instance()->render('layout.htm');
|
||||
}
|
||||
);
|
||||
$f3->route('POST /add',
|
||||
function($f3) use ($db) {
|
||||
$item =new Item;
|
||||
$item->copyFrom(json_decode($f3->get('BODY')));
|
||||
$item->save();
|
||||
|
||||
}
|
||||
);
|
||||
|
||||
$f3->route('GET /api/items',
|
||||
function($f3) {
|
||||
$item = new Item;
|
||||
$items = $item->find();
|
||||
$list = array_map([$item, 'cast'],$items);
|
||||
echo json_encode($list);
|
||||
}
|
||||
);
|
||||
|
||||
$f3->route('POST /sendsignal', function($f3) {
|
||||
$post = json_decode($f3->get('BODY'), true);
|
||||
// print_r($post);
|
||||
// die;
|
||||
$iparray = preg_split('/\r\n|\r|\n/', $post['item']['ip']);
|
||||
$messages = [];
|
||||
switch ($post['item']['channel']) {
|
||||
case 'telnet':
|
||||
foreach ($iparray as $ip) {
|
||||
$result = telnet($ip, $post['item']['port'], $post['action']);
|
||||
array_push($messages, $result);
|
||||
}
|
||||
break;
|
||||
case 'udp':
|
||||
foreach ($iparray as $ip) {
|
||||
$result = udp($ip, $post['item']['port'], $post['action']);
|
||||
array_push($messages, $result);
|
||||
}
|
||||
break;
|
||||
case 'wol':
|
||||
foreach ($iparray as $ip) {
|
||||
$result = wol($post['item']['macAddress'], $post['item']['broadcastIP']);
|
||||
array_push($messages, $result);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
array_push($messages, ['success' => false, 'message' => 'No channel set for these equipments']);
|
||||
break;
|
||||
}
|
||||
echo json_encode($messages);
|
||||
});
|
||||
|
||||
$f3->route('POST /del', function ($f3) {
|
||||
$item = new Item;
|
||||
$target = json_decode($f3->get('BODY'), true);
|
||||
$items = $item->load(['@name = ?', $target['name']]);
|
||||
$items->erase();
|
||||
echo json_encode(['success' => true, 'message' => 'Deleted']);
|
||||
|
||||
});
|
||||
|
||||
function telnet($ip, $port, $action) {
|
||||
try {
|
||||
$factory = new \Socket\Raw\Factory();
|
||||
$socket = $factory->createClient($ip . ':' . $port, 2);
|
||||
$client = \Graze\TelnetClient\TelnetClient::factory();
|
||||
$client->setSocket($socket);
|
||||
// $dsn = '192.168.0.100:23';
|
||||
$client->setLineEnding(null);
|
||||
$client->setPrompt("Optoma_PJ>");
|
||||
$client->setPromptError("F");
|
||||
try {
|
||||
$conn = $client->connect($ip . ':' . $port);
|
||||
$client->setReadTimeout(1);
|
||||
$response = $client->execute($action . "\r");
|
||||
return ['success' => true, 'message' => 'successfully sent command to ' . $ip];
|
||||
} catch (Exception $e) {
|
||||
return ['success' => false, 'message' => $e->getMessage() . $ip];
|
||||
}
|
||||
|
||||
} catch (Exception $e) {
|
||||
return ['success' => false, 'message' => $e->getMessage() . ' ' . $ip];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function udp($ip, $port, $action) {
|
||||
if ($socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP)) {
|
||||
socket_sendto($socket, $action, strlen($action), 0, $ip, $port);
|
||||
return ['success' => true, 'message' => 'UDP successful'];
|
||||
} else {
|
||||
return ['success' => false, 'message' => 'UDP not sent...'];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function wol($macAddress, $broadcastIP) {
|
||||
$f = new \Phpwol\Factory();
|
||||
$magicPacket = $f->magicPacket();
|
||||
if ($result = $magicPacket->send($macAddress, $broadcastIP)) {
|
||||
return ['success' => true, 'message' => 'Successful Wake On Lan'];
|
||||
} else {
|
||||
return ['success' => false, 'message' => 'Unsuccessful WOL for IP ' . $macAddress];
|
||||
}
|
||||
}
|
||||
|
||||
$f3->route('GET /optomaon',
|
||||
function($f3) {
|
||||
$client = \Graze\TelnetClient\TelnetClient::factory();
|
||||
$dsn = '192.168.0.100:23';
|
||||
$client->setLineEnding(null);
|
||||
$client->setPrompt("Optoma_PJ>");
|
||||
$client->setPromptError("F");
|
||||
$conn = $client->connect($dsn);
|
||||
$client->setReadTimeout(1);
|
||||
$response = $client->execute("~0000 1\r");
|
||||
print_r($response);
|
||||
});
|
||||
|
||||
|
||||
function getItems($db) {
|
||||
return $db->read('items');
|
||||
}
|
||||
|
||||
function setItems($db) {
|
||||
$items = $db->read('items');
|
||||
|
||||
}
|
||||
$f3->route('GET /userref',
|
||||
function($f3) {
|
||||
$f3->set('content','userref.htm');
|
||||
echo View::instance()->render('layout.htm');
|
||||
}
|
||||
);
|
||||
|
||||
$f3->run();
|
||||
Reference in New Issue
Block a user