1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15: namespace Cake\View\Helper;
16:
17: use Cake\Utility\Xml;
18: use Cake\View\Helper;
19: use Cake\View\View;
20:
21: 22: 23: 24: 25: 26: 27: 28:
29: class RssHelper extends Helper
30: {
31: 32: 33: 34: 35:
36: public $helpers = ['Url', 'Time'];
37:
38: 39: 40: 41: 42:
43: public $base;
44:
45: 46: 47: 48: 49:
50: public $here;
51:
52: 53: 54: 55: 56:
57: public $params = [];
58:
59: 60: 61: 62: 63:
64: public $action;
65:
66: 67: 68: 69: 70:
71: public $data;
72:
73: 74: 75: 76: 77:
78: public $model;
79:
80: 81: 82: 83: 84:
85: public $field;
86:
87: 88: 89: 90: 91:
92: public $version = '2.0';
93:
94: 95: 96:
97: public function __construct(View $view, array $settings = [])
98: {
99: deprecationWarning('RssHelper is deprecated and will be removed in 4.0.0');
100: parent::__construct($view, $settings);
101: }
102:
103: 104: 105: 106: 107: 108: 109:
110: public function document($attrib = [], $content = null)
111: {
112: if ($content === null) {
113: $content = $attrib;
114: $attrib = [];
115: }
116: if (!isset($attrib['version']) || empty($attrib['version'])) {
117: $attrib['version'] = $this->version;
118: }
119:
120: return $this->elem('rss', $attrib, $content);
121: }
122:
123: 124: 125: 126: 127: 128: 129: 130:
131: public function channel($attrib = [], $elements = [], $content = null)
132: {
133: if (!isset($elements['link'])) {
134: $elements['link'] = '/';
135: }
136: if (!isset($elements['title'])) {
137: $elements['title'] = '';
138: }
139: if (!isset($elements['description'])) {
140: $elements['description'] = '';
141: }
142: $elements['link'] = $this->Url->build($elements['link'], true);
143:
144: $elems = '';
145: foreach ($elements as $elem => $data) {
146: $attributes = [];
147: if (is_array($data)) {
148: if (strtolower($elem) === 'cloud') {
149: $attributes = $data;
150: $data = [];
151: } elseif (isset($data['attrib']) && is_array($data['attrib'])) {
152: $attributes = $data['attrib'];
153: unset($data['attrib']);
154: } else {
155: $innerElements = '';
156: foreach ($data as $subElement => $value) {
157: $innerElements .= $this->elem($subElement, [], $value);
158: }
159: $data = $innerElements;
160: }
161: }
162: $elems .= $this->elem($elem, $attributes, $data);
163: }
164:
165: return $this->elem('channel', $attrib, $elems . $content, !($content === null));
166: }
167:
168: 169: 170: 171: 172: 173: 174: 175: 176:
177: public function items($items, $callback = null)
178: {
179: if ($callback) {
180: $items = array_map($callback, $items);
181: }
182:
183: $out = '';
184: $c = count($items);
185:
186: for ($i = 0; $i < $c; $i++) {
187: $out .= $this->item([], $items[$i]);
188: }
189:
190: return $out;
191: }
192:
193: 194: 195: 196: 197: 198: 199:
200: public function item($att = [], $elements = [])
201: {
202: $content = null;
203:
204: if (isset($elements['link']) && !isset($elements['guid'])) {
205: $elements['guid'] = $elements['link'];
206: }
207:
208: foreach ($elements as $key => $val) {
209: $attrib = [];
210:
211: $escape = true;
212: if (is_array($val) && isset($val['convertEntities'])) {
213: $escape = $val['convertEntities'];
214: unset($val['convertEntities']);
215: }
216:
217: switch ($key) {
218: case 'pubDate':
219: $val = $this->time($val);
220: break;
221: case 'category':
222: if (is_array($val) && !empty($val[0])) {
223: $categories = [];
224: foreach ($val as $category) {
225: $attrib = [];
226: if (is_array($category) && isset($category['domain'])) {
227: $attrib['domain'] = $category['domain'];
228: unset($category['domain']);
229: }
230: $categories[] = $this->elem($key, $attrib, $category);
231: }
232: $elements[$key] = implode('', $categories);
233: continue 2;
234: }
235: if (is_array($val) && isset($val['domain'])) {
236: $attrib['domain'] = $val['domain'];
237: }
238: break;
239: case 'link':
240: case 'guid':
241: case 'comments':
242: if (is_array($val) && isset($val['url'])) {
243: $attrib = $val;
244: unset($attrib['url']);
245: $val = $val['url'];
246: }
247: $val = $this->Url->build($val, true);
248: break;
249: case 'source':
250: if (is_array($val) && isset($val['url'])) {
251: $attrib['url'] = $this->Url->build($val['url'], true);
252: $val = $val['title'];
253: } elseif (is_array($val)) {
254: $attrib['url'] = $this->Url->build($val[0], true);
255: $val = $val[1];
256: }
257: break;
258: case 'enclosure':
259: if (is_string($val['url']) && is_file(WWW_ROOT . $val['url']) && file_exists(WWW_ROOT . $val['url'])) {
260: if (!isset($val['length']) && strpos($val['url'], '://') === false) {
261: $val['length'] = sprintf('%u', filesize(WWW_ROOT . $val['url']));
262: }
263: if (!isset($val['type']) && function_exists('mime_content_type')) {
264: $val['type'] = mime_content_type(WWW_ROOT . $val['url']);
265: }
266: }
267: $val['url'] = $this->Url->build($val['url'], true);
268: $attrib = $val;
269: $val = null;
270: break;
271: default:
272: $attrib = $att;
273: }
274: if ($val !== null && $escape) {
275: $val = h($val);
276: }
277: $elements[$key] = $this->elem($key, $attrib, $val);
278: }
279: if (!empty($elements)) {
280: $content = implode('', $elements);
281: }
282:
283: return $this->elem('item', (array)$att, $content, !($content === null));
284: }
285:
286: 287: 288: 289: 290: 291: 292:
293: public function time($time)
294: {
295: return $this->Time->toRss($time);
296: }
297:
298: 299: 300: 301: 302: 303: 304: 305: 306:
307: public function elem($name, $attrib = [], $content = null, $endTag = true)
308: {
309: $namespace = null;
310: if (isset($attrib['namespace'])) {
311: $namespace = $attrib['namespace'];
312: unset($attrib['namespace']);
313: }
314: $cdata = false;
315: if (is_array($content) && isset($content['cdata'])) {
316: $cdata = true;
317: unset($content['cdata']);
318: }
319: if (is_array($content) && array_key_exists('value', $content)) {
320: $content = $content['value'];
321: }
322: $children = [];
323: if (is_array($content)) {
324: $children = $content;
325: $content = null;
326: }
327:
328: $xml = '<' . $name;
329: if (!empty($namespace)) {
330: $xml .= ' xmlns';
331: if (is_array($namespace)) {
332: $xml .= ':' . $namespace['prefix'];
333: $namespace = $namespace['url'];
334: }
335: $xml .= '="' . $namespace . '"';
336: }
337: $bareName = $name;
338: if (strpos($name, ':') !== false) {
339: list($prefix, $bareName) = explode(':', $name, 2);
340: switch ($prefix) {
341: case 'atom':
342: $xml .= ' xmlns:atom="http://www.w3.org/2005/Atom"';
343: break;
344: }
345: }
346: if ($cdata && !empty($content)) {
347: $content = '<![CDATA[' . $content . ']]>';
348: }
349: $xml .= '>' . $content . '</' . $name . '>';
350: $elem = Xml::build($xml, ['return' => 'domdocument']);
351: $nodes = $elem->getElementsByTagName($bareName);
352: if ($attrib) {
353: foreach ($attrib as $key => $value) {
354: $nodes->item(0)->setAttribute($key, $value);
355: }
356: }
357: foreach ($children as $child) {
358: $child = $elem->createElement($name, $child);
359: $nodes->item(0)->appendChild($child);
360: }
361:
362: $xml = $elem->saveXml();
363: $xml = trim(substr($xml, strpos($xml, '?>') + 2));
364:
365: return $xml;
366: }
367:
368: 369: 370: 371: 372:
373: public function implementedEvents()
374: {
375: return [];
376: }
377: }
378: