-
Notifications
You must be signed in to change notification settings - Fork 1
/
PageChecker.php
90 lines (74 loc) · 2.03 KB
/
PageChecker.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
require_once 'config.php';
require_once 'Mail.php';
require_once 'facebook-php-sdk/src/facebook.php';
class PageChecker
{
protected $lastPostId;
protected $pageFBId;
protected $facebook;
protected $message;
function __construct($pageId){
$this->pageFBId = $pageId;
$this->facebook = new Facebook(array('appId'=> FB_APP_ID, 'secret' => FB_APP_SECRET));
$data = $this->getFeedInfo();
$this->lastPostId = $this->getPostId($data[0]);
//echo 'Init lastPostId to '.$this->lastPostId;
}
function exec(){
while(1){
$this->checkLastPost();
sleep(1);
}
}
function checkLastPost(){
if ($this->shouldNotify($this->getFeedInfo())){
if (SEND_EMAIL)
{
//echo "sending email \r\n";
$this->sendEmail(TO_EMAIL_ADDRESS);
}
if (SEND_TEXT)
{
//echo "sending text \r\n";
$this->sendText();
}
}
}
function getFeedInfo(){
$ret_obj = $this->facebook->api($this->pageFBId.'/feed');
return $data = $ret_obj['data'];
}
function shouldNotify(&$data){
if ($data){
$id = $this->getPostId($data[0]);
$message = $data[0]["message"];
if ($message && $id > $this->lastPostId)
{
$this->lastPostId = $id;
$this->message = $message;
//echo "updated last postId to $id\r\n";
return true;
}
}
return false;
}
function getPostId(&$post)
{
$pieces = explode("_", $post['id']);
$id = $pieces[1];
return $id;
}
function sendEmail($address){
$mail = Mail::factory("mail");
$subject = "Page ".$this->pageFBId." changed!";
$headers = array(
"From"=>FROM_EMAIL_ADDRESS,
"Subject"=>$subject);
$body = $this->message;
$mail->send($address, $headers, $body);
}
function sendText(){
$this->sendEmail(CARRIER_TEXT_EMAIL);
}
}