-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler_class.php
More file actions
192 lines (151 loc) · 4.13 KB
/
Copy pathhandler_class.php
File metadata and controls
192 lines (151 loc) · 4.13 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
include("vendor/advanced_html_dom.php");
class Handler {
public $url;
public $initFunc;
public $handlerFunc;
public $testFunc;
private $min_name;
private $items;
private $items_saved;
private $firstRead;
public $diffTxt;
public $hasRun;
public function __construct($url, $min_name = null){
$this->url = $url;
$this->min_name = is_null($min_name) ? urlencode(basename($url)) : $min_name;
$this->hasRun = false;
}
public function start(){
$setupLoad = $this->startSetup();
if($setupLoad){
$this->saveDifferences();
$this->hasRun = true;
if(isset($this->pushMessageArray)){
$this->sendPushMessageInternal();
}
}
}
public function test(){
$setupLoad = $this->startSetup();
if($setupLoad){
$testFunc = $this->testFunc;
$testFunc($this->items);
}
}
private function startSetup(){
$html = @file_get_html($this->url);
if($html === FALSE){
$this->errorHandler("Failed to load website: $this->url");
return false;
}
$this->items = array();
$this->firstRead = !$this->readFileContents();
if($this->firstRead){
if(isset($this->initFunc)){
$initFunc = $this->initfunc;
$initFunc($html, $this->items);
} else {
$handlerFunc = $this->handlerFunc;
$handlerFunc($html, $this->items);
}
} else {
$handlerFunc = $this->handlerFunc;
$handlerFunc($html, $this->items);
}
return true;
}
public function setInitFunction($anonFunc){
$this->initFunc = $anonFunc;
}
public function setHandler($anonFunc){
$this->handlerFunc = $anonFunc;
}
public function setTestFunction($anonFunc){
$this->testFunc = $anonFunc;
}
private function readFileContents(){
$this->items_saved = [];
$file_exists = file_exists('db/' . $this->min_name . '.txt');
if($file_exists){
$fp = fopen('db/' . $this->min_name . '.txt', 'r');
$content = fread($fp, filesize('db/' . $this->min_name . '.txt'));
fclose($fp);
$this->items_saved = explode("\r\n", $content);
return true;
} else {
return false;
}
}
private function saveDifferences(){
$diffTxt = array_diff_once($this->items, $this->items_saved);
if($this->firstRead || count($diffTxt) > 0){
$text = "";
if(count($this->items_saved) > 0){
foreach($diffTxt as $item){
$text .= $item . "\r\n";
}
} else {
$i = 0;
$count = count($diffTxt)-1;
foreach($diffTxt as $item){
if($i++ < $count)
$text .= $item . "\r\n";
else
$text .= $item;
}
}
$this->diffTxt = $diffTxt;
$filepath = 'db/' . $this->min_name . '.txt';
if(!$this->firstRead)
$text = $text . file_get_contents($filepath);
$fp = fopen($filepath, 'w');
fwrite($fp, $text);
fclose($fp);
}
}
public function setPushMessage($token, $target, $title){
$this->pushMessageArray = array("token" => $token, "target" => $target, "title" => $title);
if($this->hasRun)
$this->sendPushMessageInternal();
}
private function sendPushMessageInternal(){
$twists = count($this->diffTxt);
if($twists > 0){
$pb = new Pushbullet($this->pushMessageArray["token"]);
$pb->pushNote($this->pushMessageArray["target"], $this->pushMessageArray["title"] . " ($twists)", implode("\r\n", $this->diffTxt));
}
}
public function setQPushMessage($name, $code, $title){
$twists = count($this->diffTxt);
if($twists > 0){
curl_setopt_array($ch = curl_init(), array(
CURLOPT_URL => "https://qpush.me/pusher/push_site/",
CURLOPT_POSTFIELDS => array(
"name" => $name,
"code" => $code,
"cache" => "false",
"msg[text]" => $title . " ($twists)\r\n\r\n" . implode("\r\n", $this->diffTxt)
),
CURLOPT_RETURNTRANSFER => true,
));
curl_exec($ch);
curl_close($ch);
}
}
public function setGroupMeMessage($access_token, $bot_id, $title){
$twists = count($this->diffTxt);
if($twists > 0){
require('groupme.php');
$gm = new GroupMePHP\groupme($access_token);
$gm->bots->post(array(
"bot_id" => $bot_id,
"text" => $title . " ($twists)\r\n\r\n" . implode("\r\n", $this->diffTxt)
));
}
}
public function errorHandler($msg){
echo "<p><b>ERROR:</b> " . $msg . "</p>";
}
}
?>