-
Notifications
You must be signed in to change notification settings - Fork 2
/
atomlib.php
256 lines (218 loc) · 7.5 KB
/
atomlib.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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* News block System/Config class
* This library provides functions to generate Atom feeds and tries to follow the
* same API as lib/rsslib.php
*
* @package blocks
* @subpackage news
* @copyright 2011 The Open University
* @author Matt Clarkson <[email protected]>
* @author Jon Sharp <[email protected]>(adapted for news block)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
if (!defined('MOODLE_INTERNAL')) {
die('Direct access to this script is forbidden.');
// It must be included from a Moodle page
}
/** Return the common atom headers in XML
*
* @param string $nsi Special namespace uri '...news:expires'
* @param string $uniqueid For atom:id
* @param string $linkself For atom:link (self)
* @param string $linkalt For atom:link (alternate)
* @param integer $updated For atom:updated
* @param string $title For atom:title
* @param string $description For atom:subtitle
* @return mixed
*/
function atom_standard_header($nsi, $uniqueid, $linkself, $linkalt, $updated,
$title = null, $description = null) {
global $OUTPUT;
$status = true;
$result = "";
if (!$site = get_site()) {
$status = false;
}
if ($status) {
//Calculate title, link and description
if (empty($title)) {
$title = format_string($site->fullname);
}
//xml headers
$result .= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
$result .= "<feed xmlns=\"http://www.w3.org/2005/Atom\"".$nsi.">\n";
//open the channel
//write channel info
$result .= atom_full_tag('id', 1, false, htmlspecialchars($uniqueid));
$result .= atom_full_tag('updated', 1, false, date_format_rfc3339($updated));
$result .= atom_full_tag('title', 1, false, htmlspecialchars(html_to_text($title)));
$result .= atom_full_tag('link', 1, false, null, array('href'=>$linkself, 'rel'=>'self'));
$result .= atom_full_tag('link', 1, false, null, array('href'=>$linkalt,
'rel'=>'alternate'));
if (!empty($description)) {
$result .= atom_full_tag('subtitle', 1, false, $description);
}
$result .= atom_full_tag('generator', 1, false, 'Moodle');
$today = getdate();
$result .= atom_full_tag('rights', 1, false, '© '. $today['year'] .' '.
format_string($site->fullname));
//write image info
$atompix = $OUTPUT->image_url('i/rsssitelogo');
//write the info
$result .= atom_full_tag('logo', 1, false, $atompix);
}
if (!$status) {
return false;
} else {
return $result;
}
}
/**
* Return an entry in XML
* @param array $items StdClass of atom:entry elements
* @return mixed
*/
function atom_add_items($items) {
$result = '';
$xhtmlattr = array('type' => 'xhtml');
if (!empty($items)) {
foreach ($items as $item) {
$result .= atom_start_tag('entry', 1, true);
$result .= atom_full_tag('title', 2, false,
htmlspecialchars(html_to_text($item->title)));
$result .= atom_full_tag('link', 2, false, null, array('href' => $item->link,
'rel'=>'alternate'));
$result .= atom_full_tag('updated', 2, false, date_format_rfc3339($item->pubdate));
//Include the author if exists
if (isset($item->author)) {
$result .= atom_start_tag('author', 2, true);
$result .= atom_full_tag('name', 3, false, $item->author);
$result .= atom_end_tag('author', 2, true);
}
$result .= atom_full_tag('content', 2, false,
'<div xmlns="http://www.w3.org/1999/xhtml">'
.clean_text($item->content, FORMAT_HTML).'</div>',
$xhtmlattr);
$result .= atom_full_tag('id', 2, false, $item->link);
if (isset($item->tags)) {
$tagdata = array();
if (isset($item->tagscheme)) {
$tagdata['scheme'] = $item->tagscheme;
}
foreach ($item->tags as $tag) {
$tagdata['term'] = $tag;
$result .= atom_full_tag('category', 2, true, false, $tagdata);
}
}
$result .= atom_end_tag('entry', 1, true);
}
} else {
$result = false;
}
return $result;
}
/**
* Return the common footers in XML (typically args are null - they are present
* for conformity with RSS lib)
*
* @param $title
* @param $link
* @param $description
* @return string
*/
function atom_standard_footer($title = null, $link = null, $description = null) {
$result = '';
////Close the feed
$result .= '</feed>';
return $result;
}
/**
* Return the start tag in XML
*
* @param string $tag Tag name
* @param integer $level Indentation (for output formatting)
* @param boolean $endline
* @param array $attributes Tag attributes
* @return string
*/
function atom_start_tag($tag, $level=0, $endline=false, $attributes=null) {
if ($endline) {
$endchar = "\n";
} else {
$endchar = "";
}
$attrstring = '';
if (!empty($attributes) && is_array($attributes)) {
foreach ($attributes as $key => $value) {
$attrstring .= " ".$key."=\"".htmlspecialchars($value)."\"";
}
}
return str_repeat(" ", $level*2)."<".$tag.$attrstring.">".$endchar;
}
/**
* Return the end tag in XML
*
* @param string $tag Tag name
* @param integer $level Indentation
* @param boolean $endline
* @return string
*/
function atom_end_tag($tag, $level=0, $endline=true) {
if ($endline) {
$endchar = "\n";
} else {
$endchar = "";
}
return str_repeat(" ", $level*2)."</".$tag.">".$endchar;
}
/**
* Return the start tag, the contents and the end tag in one call
*
* @param string $tag Tag name
* @param integer $level Indentation
* @param boolean $endline
* @param $content
* @param $attributes
* @return string
*/
function atom_full_tag($tag, $level, $endline, $content, $attributes = null) {
$st = atom_start_tag($tag, $level, $endline, $attributes);
if ($content === false) {
$st = preg_replace('~>$~', ' />', $st);
return $st;
}
$co = preg_replace("/\r\n|\r/", "\n", $content ?? '');
$et = atom_end_tag($tag, 0, true);
return $st.$co.$et;
}
/**
* Convert secs to RFC3339 compliant format
*
* @param integer $timestamp Seconds since start of epoch
* @return string Formatted date
*/
function date_format_rfc3339($timestamp=0) {
$date = date('Y-m-d\TH:i:s', $timestamp);
$matches = array();
if (preg_match('/^([\-+])(\d{2})(\d{2})$/', date('O', $timestamp), $matches)) {
$date .= $matches[1].$matches[2].':'.$matches[3];
} else {
$date .= 'Z';
}
return $date;
}