-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNotification.php
More file actions
207 lines (182 loc) · 4.61 KB
/
Notification.php
File metadata and controls
207 lines (182 loc) · 4.61 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
/**
* Notifications Model
*
* @author Noriko Arai <arai@nii.ac.jp>
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
*/
App::uses('NotificationsAppModel', 'Notifications.Model');
/**
* Notification Model
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @package NetCommons\Notifications\Model
*/
class Notification extends NotificationsAppModel {
/**
* Cache time
*
* @var string
*/
const CACHE_TIME = 'PT1H';
/**
* Max row
*
* @var string
*/
const MAX_ROW = 20;
/**
* Notification url
*
* @var string
*/
const NOTIFICATION_PING_URL = 'www.netcommons.org';
/**
* Notification url
*
* @var string
*/
const NOTIFICATION_URL = 'https://www.netcommons.org/topics/topics/index.xml?frame_id=12';
/**
* PING
*
* @param string|null $url pingのURL(テストで使用する)
* @return mixed fsockopenの結果
*/
public function ping($url = null) {
//サイトの生死確認
$errno = 0;
$errstr = null;
if (! $url) {
$url = self::NOTIFICATION_PING_URL;
}
CakeLog::info('Execute ping ' . $url);
try {
$resource = fsockopen($url, 80, $errno, $errstr, 3);
} catch (Exception $ex) {
$resource = false;
CakeLog::error($ex);
}
if (! $resource) {
CakeLog::info('Failure ping ' . $url);
$result = false;
} else {
fclose($resource);
$result = true;
CakeLog::info('Success ping ' . $url);
}
return $result;
}
/**
* Serialize to array data from xml
*
* @param string|null $url XMLのURL(テストで使用する)
* @return array Xml serialize array data
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function serialize($url = null) {
if (! $url) {
$url = self::NOTIFICATION_URL;
}
$xmlData = Xml::toArray(Xml::build($url));
// rssの種類によってタグ名が異なる
if (isset($xmlData['feed'])) {
$items = Hash::get($xmlData, 'feed.entry');
$dateKey = 'published';
$linkKey = 'link.@href';
$summaryKey = 'summary';
} elseif (Hash::get($xmlData, 'rss.@version') === '2.0') {
$items = Hash::get($xmlData, 'rss.channel.item');
$dateKey = 'pubDate';
$linkKey = 'link';
$summaryKey = 'description';
} elseif (isset($xmlData['RDF'])) {
$items = Hash::get($xmlData, 'RDF.item');
$dateKey = 'dc:date';
$linkKey = 'link';
$summaryKey = 'description';
} else {
return false;
}
if (! isset($items[0]) && is_array($items)) {
$items = array($items);
}
$data = array();
foreach ($items as $item) {
if (! $item['title']) {
continue;
}
$date = new DateTime($item[$dateKey]);
$summary = Hash::get($item, $summaryKey);
$data[] = array(
'title' => $item['title'],
'link' => Hash::get($item, $linkKey),
'summary' => $summary ? strip_tags($summary) : '',
'last_updated' => $date->format('Y-m-d H:i:s'),
'key' => Security::hash(Hash::get($item, $linkKey), 'md5')
);
}
return $data;
}
/**
* Update notifications
*
* @param array $data received post data
* @return bool true on success, exception on error
* @throws InternalErrorException
*/
public function updateNotifications($data) {
if (! $data['Notification']) {
return true;
}
//トランザクションBegin
$this->begin();
//Notificationsのvalidate
if (! $this->validateMany($data['Notification'])) {
$this->rollback();
return false;
}
try {
//既存データの削除
$conditions = Hash::extract($data, 'Notification.{n}.Notification.key');
if (! $this->deleteAll(array($this->alias . '.key' => $conditions), true, false)) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
//Notificationsの登録
if (! $this->saveMany($data['Notification'], array('validate' => false))) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
$this->commit();
} catch (Exception $ex) {
$this->rollback($ex);
}
return true;
}
/**
* Valide cache time
*
* @return bool True on valid time, false on no valid
*/
public function validCacheTime() {
$date = new DateTime();
$now = $date->format('Y-m-d H:i:s');
$notification = $this->find('first', array(
'recursive' => -1,
'fields' => 'modified',
'order' => array('modified' => 'desc'),
));
if (! $notification) {
return false;
}
$date = new DateTime($notification['Notification']['modified']);
$date->add(new DateInterval(self::CACHE_TIME));
$modified = $date->format('Y-m-d H:i:s');
if ($now < $modified) {
return true;
}
return false;
}
}