1: <?php
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: require_once(SERVERPATH . '/' . ZENFOLDER . '/template-functions.php');
63:
64: class feed {
65:
66: protected $feed = 'feed';
67: protected $mode;
68: protected $options;
69:
70: protected $feedtype = NULL;
71: protected $itemnumber = NULL;
72: protected $locale = NULL;
73: protected $locale_xml = NULL;
74: protected $host = NULL;
75: protected $sortorder = NULL;
76: protected $sortdirection = NULL;
77:
78: protected $albumfolder = NULL;
79: protected $collection = NULL;
80: protected $albumpath = NULL;
81: protected $imagepath = NULL;
82: protected $imagesize = NULL;
83: protected $modrewritesuffix = NULL;
84:
85: protected $catlink = NULL;
86: protected $cattitle = NULL;
87: protected $newsoption = NULL;
88: protected $titleappendix = NULL;
89:
90: protected $id = NULL;
91: protected $commentfeedtype = NULL;
92: protected $itemobj = NULL;
93:
94: protected $channel_title = NULL;
95: protected $feeditem = array();
96:
97: 98: 99: 100: 101:
102: protected function getCacheFilename() {
103: $filename = array();
104: foreach ($this->options as $key => $value) {
105: if (empty($value)) {
106: $filename[] = $key;
107: } else {
108: $filename[] = $value;
109: }
110: }
111: $filename = seoFriendly(implode('_', $filename));
112: return $filename . ".xml";
113: }
114:
115: 116: 117: 118:
119: protected function startCache() {
120: $caching = getOption($this->feed . "_cache") && !zp_loggedin();
121: if ($caching) {
122: $cachefilepath = SERVERPATH . '/' . STATIC_CACHE_FOLDER . '/' . strtolower($this->feed) . '/' . internalToFilesystem($this->getCacheFilename());
123: if (file_exists($cachefilepath) AND time() - filemtime($cachefilepath) < getOption($this->feed . "_cache_expire")) {
124: echo file_get_contents($cachefilepath);
125: exitZP();
126: } else {
127: if (file_exists($cachefilepath)) {
128: @chmod($cachefilepath, 0777);
129: @unlink($cachefilepath);
130: }
131: ob_start();
132: }
133: }
134: }
135:
136: 137: 138: 139:
140: protected function endCache() {
141: $caching = getOption($this->feed . "_cache") && !zp_loggedin();
142: if ($caching) {
143: $cachefilepath = internalToFilesystem($this->getCacheFilename());
144: if (!empty($cachefilepath)) {
145: $cachefilepath = SERVERPATH . '/' . STATIC_CACHE_FOLDER . '/' . strtolower($this->feed) . '/' . $cachefilepath;
146: mkdir_recursive(SERVERPATH . '/' . STATIC_CACHE_FOLDER . '/' . strtolower($this->feed) . '/', FOLDER_MOD);
147: $pagecontent = ob_get_contents();
148: ob_end_clean();
149: if ($fh = @fopen($cachefilepath, "w")) {
150: fputs($fh, $pagecontent);
151: fclose($fh);
152: clearstatcache();
153: }
154: echo $pagecontent;
155: }
156: }
157: }
158:
159: 160: 161: 162: 163:
164: function clearCache($cachefolder = NULL) {
165: zpFunctions::removeDir(SERVERPATH . '/' . STATIC_CACHE_FOLDER . '/' . strtolower($this->feed) . '/' . $cachefolder, true);
166: }
167:
168: function __construct($options) {
169: $this->options = $options;
170: if (isset($this->options['lang'])) {
171: $this->locale = $this->options['lang'];
172: } else {
173: $this->locale = getOption('locale');
174: }
175: $this->locale_xml = strtr($this->locale, '_', '-');
176: if (isset($this->options['sortdir'])) {
177: $this->sortdirection = strtolower($this->options['sortdir']) != 'asc';
178: } else {
179: $this->sortdirection = true;
180: }
181: if (isset($this->options['sortorder'])) {
182: $this->sortorder = $this->options['sortorder'];
183: } else {
184: $this->sortorder = NULL;
185: }
186: switch ($this->feedtype) {
187: case 'comments':
188: if (isset($this->options['type'])) {
189: $this->commentfeedtype = $this->options['type'];
190: } else {
191: $this->commentfeedtype = 'all';
192: }
193: if (isset($this->options['id'])) {
194: $this->id = (int) $this->options['id'];
195: }
196: break;
197: case 'gallery':
198: if (isset($this->options['albumsmode'])) {
199: $this->mode = 'albums';
200: }
201: if (isset($this->options['folder'])) {
202: $this->albumfolder = $this->options['folder'];
203: $this->collection = true;
204: } else if (isset($this->options['albumname'])) {
205: $this->albumfolder = $this->options['albumname'];
206: $this->collection = false;
207: } else {
208: $this->collection = false;
209: }
210: if (is_null($this->sortorder)) {
211: if ($this->mode == "albums") {
212: $this->sortorder = getOption($this->feed . "_sortorder_albums");
213: } else {
214: $this->sortorder = getOption($this->feed . "_sortorder");
215: }
216: }
217: break;
218: case 'news':
219: if ($this->sortorder == 'latest') {
220: $this->sortorder = NULL;
221: }
222:
223: if (isset($this->options['category'])) {
224: $this->catlink = $this->options['category'];
225: $catobj = new ZenpageCategory($this->catlink);
226: $this->cattitle = $catobj->getTitle();
227: $this->newsoption = 'category';
228: } else {
229: $this->catlink = '';
230: $this->cattitle = '';
231: $this->newsoption = 'news';
232: }
233: break;
234: case 'pages':
235: break;
236: case 'null':
237: return;
238: }
239: if (isset($this->options['itemnumber'])) {
240: $this->itemnumber = (int) $this->options['itemnumber'];
241: } else {
242: $this->itemnumber = getOption($this->feed . '_items');
243: }
244: }
245:
246: protected function getChannelTitleExtra() {
247: switch ($this->sortorder) {
248: default:
249: case 'latest':
250: case 'latest-date':
251: case 'latest-mtime':
252: case 'latest-publishdate':
253: if ($this->mode == 'albums') {
254: $albumextra = ' (' . gettext('Latest albums') . ')';
255: } else {
256: $albumextra = ' (' . gettext('Latest images') . ')';
257: }
258: break;
259: case 'latestupdated':
260: $albumextra = ' (' . gettext('latest updated albums') . ')';
261: break;
262: case 'popular':
263: if ($this->mode == 'albums') {
264: $albumextra = ' (' . gettext('Most popular albums') . ')';
265: } else {
266: $albumextra = ' (' . gettext('Most popular images') . ')';
267: }
268: break;
269: case 'toprated':
270: if ($this->mode == 'albums') {
271: $albumextra = ' (' . gettext('Top rated albums') . ')';
272: } else {
273: $albumextra = ' (' . gettext('Top rated images') . ')';
274: }
275: break;
276: case 'random':
277: if ($this->mode == 'albums') {
278: $albumextra = ' (' . gettext('Random albums') . ')';
279: } else {
280: $albumextra = ' (' . gettext('Random images') . ')';
281: }
282: break;
283: }
284: return $albumextra;
285: }
286:
287: 288: 289: 290: 291:
292: protected function getImageSize() {
293: if (isset($this->options['size'])) {
294: $imagesize = (int) $this->options['size'];
295: } else {
296: $imagesize = NULL;
297: }
298: if ($this->mode == 'albums') {
299: if (is_null($imagesize) || $imagesize > getOption($this->feed . '_imagesize_albums')) {
300: $imagesize = getOption($this->feed . '_imagesize_albums');
301: }
302: } else {
303: if (is_null($imagesize) || $imagesize > getOption($this->feed . '_imagesize')) {
304: $imagesize = getOption($this->feed . '_imagesize');
305: }
306: }
307: return $imagesize;
308: }
309:
310: 311: 312: 313: 314:
315: public function getitems() {
316: global $_zp_zenpage;
317: switch ($this->feedtype) {
318: case 'gallery':
319: if ($this->mode == "albums") {
320: $items = getAlbumStatistic($this->itemnumber, $this->sortorder, $this->albumfolder, $this->sortdirection);
321: } else {
322: $items = getImageStatistic($this->itemnumber, $this->sortorder, $this->albumfolder, $this->collection, 0, $this->sortdirection);
323: }
324: break;
325: case 'news':
326: switch ($this->newsoption) {
327: case "category":
328: if ($this->sortorder) {
329: $items = getZenpageStatistic($this->itemnumber, 'categories', $this->sortorder, $this->sortdirection);
330: } else {
331: $items = getLatestNews($this->itemnumber, $this->catlink, false, $this->sortdirection);
332: }
333: break;
334: default:
335: case "news":
336: if ($this->sortorder) {
337: $items = getZenpageStatistic($this->itemnumber, 'news', $this->sortorder, $this->sortdirection);
338: } else {
339:
340: $items = getLatestNews($this->itemnumber, '', false, $this->sortdirection);
341: }
342: break;
343: }
344: break;
345: case "pages":
346: if ($this->sortorder) {
347: $items = getZenpageStatistic($this->itemnumber, 'pages', $this->sortorder, $this->sortdirection);
348: } else {
349: $items = $_zp_zenpage->getPages(NULL, false, $this->itemnumber);
350: }
351: break;
352: case 'comments':
353: switch ($type = $this->commentfeedtype) {
354: case 'gallery':
355: $items = getLatestComments($this->itemnumber, 'all');
356: break;
357: case 'album':
358: $items = getLatestComments($this->itemnumber, 'album', $this->id);
359: break;
360: case 'image':
361: $items = getLatestComments($this->itemnumber, 'image', $this->id);
362: break;
363: case 'zenpage':
364: $type = 'all';
365: case 'news':
366: case 'page':
367: if (function_exists('getLatestZenpageComments')) {
368: $items = getLatestZenpageComments($this->itemnumber, $type, $this->id);
369: }
370: break;
371: case 'allcomments':
372: $items = getLatestComments($this->itemnumber, 'all');
373: $items_zenpage = array();
374: if (function_exists('getLatestZenpageComments')) {
375: $items_zenpage = getLatestZenpageComments($this->itemnumber, 'all', $this->id);
376: $items = array_merge($items, $items_zenpage);
377: $items = sortMultiArray($items, 'date', true);
378: $items = array_slice($items, 0, $this->itemnumber);
379: }
380: break;
381: }
382: break;
383: }
384: if (isset($items)) {
385: return $items;
386: }
387: if (TEST_RELEASE) {
388: trigger_error(gettext('Bad ' . $this->feed . ' feed:' . $this->feedtype), E_USER_WARNING);
389: }
390: return NULL;
391: }
392:
393: 394: 395: 396: 397: 398:
399: protected function getitemPages($item, $len) {
400: $obj = new ZenpagePage($item['titlelink']);
401: $feeditem['title'] = $feeditem['title'] = get_language_string($obj->getTitle('all'), $this->locale);
402: $feeditem['link'] = $obj->getLink();
403: $desc = $obj->getContent($this->locale);
404: $desc = str_replace('//<![CDATA[', '', $desc);
405: $desc = str_replace('//]]>', '', $desc);
406: $feeditem['desc'] = shortenContent($desc, $len, '...');
407: $feeditem['enclosure'] = '';
408: $feeditem['category'] = '';
409: $feeditem['media_content'] = '';
410: $feeditem['media_thumbnail'] = '';
411: $feeditem['pubdate'] = date("r", strtotime($obj->getDatetime()));
412: return $feeditem;
413: }
414:
415: 416: 417: 418: 419: 420:
421: protected function getitemComments($item) {
422: if ($item['anon']) {
423: $author = "";
424: } else {
425: $author = " " . gettext("by") . " " . $item['name'];
426: }
427: $commentpath = $imagetag = $title = '';
428: switch ($item['type']) {
429: case 'images':
430: $title = get_language_string($item['title']);
431: $obj = newImage(NULL, array('folder' => $item['folder'], 'filename' => $item['filename']));
432: $link = $obj->getlink();
433: $feeditem['pubdate'] = date("r", strtotime($item['date']));
434: $category = get_language_string($item['albumtitle']);
435: $website = $item['website'];
436: $title = $category . ": " . $title;
437: $commentpath = PROTOCOL . '://' . $this->host . $link . "#" . $item['id'];
438: break;
439: case 'albums':
440: $obj = newAlbum($item['folder']);
441: $link = rtrim($obj->getLink(), '/');
442: $feeditem['pubdate'] = date("r", strtotime($item['date']));
443: $title = get_language_string($item['albumtitle']);
444: $website = $item['website'];
445: $commentpath = PROTOCOL . '://' . $this->host . $link . "#" . $item['id'];
446: break;
447: case 'news':
448: case 'pages':
449: if (extensionEnabled('zenpage')) {
450: $album = '';
451: $feeditem['pubdate'] = date("r", strtotime($item['date']));
452: $category = '';
453: $title = get_language_string($item['title']);
454: $titlelink = $item['titlelink'];
455: $website = $item['website'];
456: if($item['type'] == 'news') {
457: $obj = new ZenpageNews($titlelink);
458: } else {
459: $obj = new ZenpagePage($titlelink);
460: }
461: $commentpath = PROTOCOL . '://' . $this->host . html_encode($obj->getLink()) . "#" . $item['id'];
462: } else {
463: $commentpath = '';
464: }
465:
466: break;
467: }
468: $feeditem['title'] = getBare($title . $author);
469: $feeditem['link'] = $commentpath;
470: $feeditem['desc'] = $item['comment'];
471: return $feeditem;
472: }
473:
474: static protected function feed404() {
475: header("HTTP/1.0 404 Not Found");
476: header("Status: 404 Not Found");
477: include(SERVERPATH . '/' . ZENFOLDER . '/404.php');
478: exitZP();
479: }
480:
481: }
482:
483: ?>