· 9 years ago · Aug 19, 2016, 09:26 AM
1<?php
2
3namespace app\libs;
4
5use app\libs\config;
6use app\libs\log;
7use app\libs\db;
8// use app\libs\JYMEngine;
9use app\libs\ymClient;
10
11/**
12*
13*/
14class ym
15{
16 private $username = null;
17 private $shell = null;
18 private $runOnBackground = true;
19 public $exec = null;
20 private $touchID = null;
21 private $touchSequence = null;
22 private $nSleep = 5;
23
24 function __construct($username, $password, $consumer_key, $secret_key)
25 {
26
27 ini_set('max_execution_time', 0);
28
29 $this->consumer_key = $consumer_key;
30 $this->secret_key = $secret_key;
31 $this->username = $username;
32 $this->password = $password;
33
34 $this->exec = new ymClient(
35 $this->username,
36 $this->password,
37 $this->consumer_key,
38 $this->secret_key
39 );
40 }
41
42 public function setRunOnReceive($shell,$runOnBackground=true)
43 {
44 $this->shell = $shell;
45 $this->runOnBackground = $runOnBackground;
46 }
47
48 public function connect($stage = 0)
49 {
50 var_dump("-P-p_P_P_P_p-P_P_P__p-P__-p-p--_P__P___");
51 # login
52 $this->exec->logIn();
53
54 # check session
55 if ($this->exec->hasSession() == true) {
56 $this->exec->keepAliveSession();
57 $this->touchHistory();
58 }
59 else{
60 return false;
61 }
62
63 }
64
65 public function listen()
66 {
67 # START prepare variables
68 $ymDB = new db;
69 $oldNotifs = [];
70 $notifSequence = 0;
71 # END prepare variables
72
73 # START check active session
74 if ($this->touchID==null) {
75 return false;
76 }
77 # END check active session
78
79 # START infinite LOOP
80 for ($i=0; $i >= 0; $i++) {
81
82 # START touch history
83 $this->touchHistory();
84 # END touch history
85
86 # [START] get notifs
87
88 $fullNotifs = $this->exec->fetchNotifications($notifSequence);
89 $notifs = $fullNotifs["responses"];
90 # [END] get
91
92 # [START] Sending chat
93 $this->runInjectedChat();
94 # [END] Sending chat
95
96 dump($notifs, CUSTOM_LOGS . "ymListener.log");
97 # [START] check new notifs
98 if (count($notifs) < 1) continue;
99 foreach ($notifs as $index => $array) {
100
101 # START check the message notif
102 if (isset($array["message"])) {
103
104 # START save message
105 $sequence = $this->saveMessage(
106 $array["message"]["sender"],
107 $array["message"]["msg"]
108 );
109 # END save message
110
111 # START onReceive shell
112 $this->runOnReceive($sequence);
113 # END onReceive shell
114 }
115 # END check the message notif
116
117 # START update notifSequence
118 $notifSequence++;
119 # END update notifSequence
120
121 }
122 # STOP check new notifs
123
124 # [START] delay and unset any thin
125 unset($fullNotifs);
126 unset($notifs);
127
128 sleep(1);
129 # [END] delay
130 }
131 # END infinite LOOP
132 }
133
134 public function runOnReceive($sequence)
135 {
136 # [START] run on receive
137 if ($this->shell !== null) {
138 $backgroundShell = " > /dev/null 2>/dev/null & ";
139 $shell = $this->shell . " " . $sequence. ($this->runOnBackground === true ? $backgroundShell: null);
140 shell_exec($shell);
141 }
142 # [END] run on receive
143
144 }
145 public function saveMessage($sender, $message, $recipient = null)
146 {
147 $ymDB = new db();
148 $recipient = $recipient === null ? $this->username : $recipient;
149
150 # [START] store it to db
151 $insertQuery = "INSERT INTO globalSpace
152 (
153 `globalSenderAccount`,
154 `globalRecipientAccount`,
155 `globalContent`,
156 `serviceProvider`,
157 `method`,
158 `globalSpaceType`
159 )
160 VALUES
161 (
162 '".$sender."',
163 '".$recipient."',
164 '".$message."',
165 'ym',
166 'chat',
167 'inbox'
168 )";
169 $ymDB->query($insertQuery)->execute();
170 $sequence = $ymDB->lastInsertId();
171 $ymDB->null();
172
173 return $sequence;
174 # [END] store
175 }
176 static public function injectChat(
177 $globalSenderAccount,
178 $globalRecipientAccount,
179 $globalContent,
180 $serviceProvider = "ym",
181 $method = "chat"
182 ) {
183 $db = new db;
184 $query = "INSERT INTO `outgoingQueue` (
185 `globalSenderAccount`,
186 `globalRecipientAccount`,
187 `globalContent`,
188 `serviceProvider`,
189 `method`)
190 VALUES (
191 '$globalSenderAccount',
192 '$globalRecipientAccount',
193 '$globalContent',
194 'ym',
195 'chat')";
196 $db->query($query)->execute();
197 }
198
199 public function runInjectedChat()
200 {
201 $db = new db;
202
203 # get out going queue
204 $queueQuery = "SELECT * FROM outgoingQueue WHERE serviceProvider = 'ym' AND method='chat' AND globalSenderAccount ='".$this->username."' ORDER BY sequence ASC";
205
206 $db->query($queueQuery)->execute();
207 $fetchALL = $db->fetchALL();
208
209 if (!isset($fetchALL[0])) {
210 return false;
211 # code...
212 }
213 # foreach
214 # delete query
215 $deleteQueue = null;
216 $insertGlobalSpace = null;
217 foreach ($fetchALL as $key => $fetch) {
218 $this->exec->sendMessage(
219 $fetch["globalContent"],
220 $fetch["globalRecipientAccount"]);
221
222 $deleteQueue .= "
223 DELETE FROM outgoingQueue
224 WHERE sequence = '".$fetch["sequence"]."';";
225 $insertGlobalSpace .="
226 INSERT INTO globalSpace
227 (
228 `globalSenderAccount`,
229 `globalRecipientAccount`,
230 `globalContent`,
231 `globalSpaceType`,
232 `serviceProvider`,
233 `method`
234 )
235 VALUES
236 (
237 '".$this->username."',
238 '".$fetch["globalRecipientAccount"]."',
239 '".$fetch["globalContent"]."',
240 'outbox',
241 'ym',
242 'chat'
243 );";
244 }
245
246 $db->null();
247 # insert global Space
248 $db->query($insertGlobalSpace)->execute();
249 $db->null();
250
251 # delete queue
252 $db->query($deleteQueue)->execute();
253 $db->null();
254 }
255
256 public function touchHistory()
257 {
258 $db = new db();
259 if ($this->touchID === null) {
260 $touchQuery = "
261 INSERT INTO loginHistory
262 (
263 `globalAccount`,
264 `serviceProvider`,
265 `method`,
266 `nTouch`
267 )
268 VALUES
269 (
270 '".$this->username."',
271 'ym',
272 'chat',
273 '1'
274 )";
275 $db->query($touchQuery)->execute();
276 $this->touchID = $db->lastInsertId();
277 $this->touchSequence = 1;
278 }
279 else{
280 $this->touchSequence++;
281 $touchQuery = "
282 UPDATE loginHistory
283 SET nTouch = '".$this->touchSequence."'
284 WHERE sequence = '".$this->touchID."'";
285 $db->query($touchQuery)->execute();
286
287 }
288 }
289
290 public function reconnect()
291 {
292 $this->exec->signoff();
293 $this->touchID = null;
294 $this->touchSequence = null;
295 sleep($this->nSleep);
296 $this->nSleep = $this->nSleep * 2;
297 $this->exec = null;
298 $this->connect();
299 }
300
301}