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