-
Notifications
You must be signed in to change notification settings - Fork 0
/
cron.php
123 lines (99 loc) · 4.27 KB
/
cron.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
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
<?php
header("Content-Type: text/plain");
include dirname(__FILE__) . "/app/app.php";
$log = '';
# Track page
foreach($db->cron_list->limit(10) as $website) {
$log .= PHP_EOL.'Tracking: '.$website['label'].PHP_EOL;
$data = ($App->parseUrl($website['id']));
$now = new NotORM_Literal("NOW()");
foreach($data as $item) {
$item['item_id'] = trim($item['item_id']);
// Check if exists
$rowWhere = array();
if($website['tracking_type']!='single') $rowWhere['item_id'] = $item['item_id'];
if($website['content_id_context']=='website') $rowWhere['website_id'] = $website['id'];
$row = $db->records($rowWhere);
if($website['tracking_type']=='single') {
$row = $row->order('occurrence_first DESC');
$row = $row->limit(1);
}
$row = $row->fetch();
if($row) {
// single tracker
if($row['item_id']!=$item['item_id']) {
// add new
$item['website_id'] = $website['id'];
$item['occurrence_first'] = $now;
$insert = $db->records()->insert($item);
if($insert) {
$log .= ' + '.trim(str_replace(PHP_EOL, " ", $item['item_id'])).PHP_EOL;
} else {
$log .= 'ERROR: + '.trim(str_replace(PHP_EOL, " ", $item['item_id'])).PHP_EOL;
}
} else {
// update occurrence_last if exists
$update =$row->update(array("occurrence_last" => $now, "occurrence_count" => $row["occurrence_count"]+1));
if($update) {
$log .= ' * '.trim(str_replace(PHP_EOL, " ", $item['item_id'])).PHP_EOL;
} else {
$log .= 'ERROR: * '.trim(str_replace(PHP_EOL, " ", $item['item_id'])).PHP_EOL;
}
}
} else {
// add new
$item['website_id'] = $website['id'];
$item['occurrence_first'] = $now;
// print_r($item);
$insert = $db->records()->insert($item);
if($insert) {
$log .= ' + '.trim(str_replace(PHP_EOL, " ", $item['item_id'])).PHP_EOL;
} else {
$log .= 'ERROR: + '.trim(str_replace(PHP_EOL, " ", $item['item_id'])).PHP_EOL;
}
}
}
$website->update(array('tracking_last' => $now, 'tracking_priority' => 'schedule'));
}
if($log!='') {
echo $log;
$messageLog = $db->tracking_log()->insert(array(
'tracking_timestamp' => new NotORM_Literal("NOW()"),
'tracking_log' => $log
));
} else {
echo PHP_EOL.'No tracking scheduled'.PHP_EOL;
}
# E-mail
$messageBody = "";
foreach($db->records->where('message_sent=0') as $record) {
// $messageBody.=PHP_EOL.PHP_EOL;
$messageBody.='<div>';
$messageBody.='<div>';
$messageBody.=$App->createMessage($record->website['message'], $record);
$messageBody.='</div>';
$messageBody.='<small>source: <a href="'.$record->website['url'].'">'.$record->website['label'].'</a> | <a href="'.APP_URL.'?deactivate='.$record->website['id'].'">deactivate</a></small>';
$messageBody.='<hr />';
$messageBody.='</div>';
$messageBody.=PHP_EOL.PHP_EOL;
$record->update(array('message_sent' => 1));
}
if($messageBody!="") {
$messageLog = $db->message_log()->insert(array(
'message_sent' => new NotORM_Literal("NOW()"),
'message_body' => $messageBody
));
$messageHtml='<html><body>';
$messageHtml.=$messageBody;
$messageHtml.='</body></html>';
if($App->sendHtmlEmail(EMAIL_ADDRESS, 'WebChangesTracker ('.date('Y-m-d H:i').')', $messageHtml, (EMAIL_SENDER!='' ? 'From: ' . EMAIL_SENDER : ''))) {
echo PHP_EOL.'CONFIRMATION: Message sent';
$messageLog->update(array('message_status' => 'ok'));
} else {
echo PHP_EOL.'ERROR: the message was not sent';
}
}
# Maintenance
$db->records()->where("occurrence_last < '".date("Y-m-d H:i:s", strtotime("now - 300 day"))."'")->delete();
$db->tracking_log()->where("tracking_timestamp < '".date("Y-m-d H:i:s", strtotime("now - 30 day"))."'")->delete();
$db->message_log()->where("message_sent < '".date("Y-m-d H:i:s", strtotime("now - 30 day"))."'")->delete();