-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.php
68 lines (61 loc) · 1.79 KB
/
base.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
/**
* @package Push
* @author Alan Hardman <alan@phpizza.com>
* @version 0.0.1
*/
namespace Plugin\Push;
class Base extends \Plugin {
protected $_socket;
/**
* Get a socket connection instance to the push server
* @return resource
*/
protected function _getSocket() {
$f3 = \Base::instance();
$host = $f3->get("pushconfig.host");
$port = $f3->get("pushconfig.port");
if (!$this->_socket) {
$this->_socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if($f3->get("pushconfig.enabled")) {
$result = @socket_connect($this->_socket, $host, $port);
$f3->set("push_socket_status", $result);
if(!$result) {
$err = socket_last_error();
$str = socket_strerror($err);
$log = new \Log("push.log");
$log->write("Failed to create socket connection: [$err] $str");
$f3->set("error", "An error occurred connecting to the push server.");
}
socket_write($this->_socket, "TEST MESSAGE");
}
}
return $this->_socket;
}
/**
* Initialize the plugin
* @todo load configuration and initialize socket connection
*/
public function _load() {
$f3 = \Base::instance();
if (!is_file(__DIR__ . "/config.php")) {
throw new Exception("Push plugin requires a config.php file!");
}
$config = require("config.php");
$config = $config + array("host" => $f3->get("HOST"));
$f3->set("pushconfig", $config);
if(@$config["enabled"]) {
$this->_addJs("<script>var pushclient_port = {$config['port']};</script>", "code");
$this->_addJs($f3->get("BASE") . "/app/plugin/push/js/push-client.js", "file");
}
}
/**
* Generate page for admin panel
*/
public function _admin() {
$f3 = \Base::instance();
$f3->set("pushsocket", $this->_getSocket());
// Render view
echo \Helper\View::instance()->render("push/view/admin.html");
}
}