-
Notifications
You must be signed in to change notification settings - Fork 20
/
atomlib.php
195 lines (159 loc) · 6 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
<?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/>.
/**
* Library for Atom feeds, sort of like the system RSS library. (Originally
* by Matt Clarkson from Catalyst.)
* @package mod
* @subpackage forumng
* @copyright 2011 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
function atom_standard_header($uniqueid, $link, $updated, $title = null, $description = null) {
global $CFG, $USER;
$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\">\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'=>$link, 'rel'=>'self'));
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.
$out = mod_forumng_utils::get_renderer();
$atompix = $out->image_url('/i/rsssitelogo');
// Write the info.
$result .= atom_full_tag('logo', 1, false, $atompix);
}
if (!$status) {
return false;
} else {
return $result;
}
}
function atom_add_items($items) {
global $CFG;
$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->description,
FORMAT_HTML).'</div>', $xhtmlattr);
$result .= atom_full_tag('id', 2, false, htmlspecialchars($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;
}
// This function return all the common footers for every rss feed in the site.
function atom_standard_footer($title = null, $link = null, $description = null) {
global $CFG, $USER;
$status = true;
$result = '';
// Close the rss tag.
$result .= '</feed>';
return $result;
}
// Return the xml start tag.
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 xml end tag.
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.
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 = '';
if (is_string($content) && $content !== '') {
$co = preg_replace("/\r\n|\r/", "\n", $content);
}
$et = atom_end_tag($tag, 0, true);
return $st.$co.$et;
}
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;
}