1<?php
2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
3//
4// All Rights Reserved. See copyright.txt for details and a complete list of authors.
5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
6// $Id$
7
8use Tiki\FileGallery;
9
10// Required path: /lib/core/Feed
11//
12// Programmer: Robert Plummer
13//
14// Purpose: The base class reused in FutureLink, PastLink and others
15
16abstract class Feed_Abstract
17{
18	public $name = "";
19	public $items = [];
20	public $item = [];
21	public $contents = [];
22	public $type = "";
23	public $isFileGal = false;
24	public $version = "0.0";
25	public $encoding = "";
26
27	function __construct($name = "")
28	{
29		if (empty($name)) {
30			$name = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
31			$name = explode('/', $name);
32			array_pop($name);
33			$name = implode($name, '/');
34		} else {
35			$name = str_replace("http://", "", $name);
36			$name = str_replace("https://", "", $name);
37			$name = explode('/', $name);
38			$name = array_shift($name);
39		}
40
41		$this->name = $this->type . "_" . $name;
42	}
43
44	public function getItems()
45	{
46		$contents = $this->getContents();
47
48		if (empty($contents->entry)) {
49			return [];
50		}
51
52		return $contents->entry;
53	}
54
55	public function listItemNames()
56	{
57		$result = [];
58		foreach ($this->getItems() as $item) {
59			if (! empty($item->name)) {
60				$result[] = addslashes(htmlspecialchars($item->name));
61			}
62		}
63		return $result;
64	}
65
66	public function getItem($name)
67	{
68		foreach ($this->getItems() as $item) {
69			if ($name == $item->name) {
70				return $item;
71			}
72		}
73		return [];
74	}
75
76	public function replace()
77	{
78	}
79
80	public function setEncoding($contents)
81	{
82		if (is_array($contents)) {
83			throw new Exception('die');
84		}
85		$this->encoding = mb_detect_encoding($contents, "ASCII, UTF-8, ISO-8859-1");
86	}
87
88	private function open()
89	{
90		if ($this->isFileGal == true) {
91			$contents = FileGallery\File::filename($this->name)->data();
92		} else {
93			$contents = TikiLib::lib("cache")->getCached($this->name, get_class($this));
94		}
95
96		$this->setEncoding($contents);
97
98		$contents = json_decode($contents);
99		if (empty($contents)) {
100			return [];
101		}
102		return $contents;
103	}
104
105	private function save($contents)
106	{
107		$contents = json_encode($contents);
108
109		if ($this->isFileGal == true) {
110			//TODO: abstract
111			FileGallery\File::filename($this->name)
112				->setParam('description', '')
113				->replace($contents);
114		} else {
115			//TODO: abstract
116			TikiLib::lib("cache")->cacheItem($this->name, $contents, get_class($this));
117		}
118
119		return $this;
120	}
121
122	public function getContents()
123	{
124		global $tikilib;
125		$contents = $this->open();
126
127		if (! empty($contents)) {
128			return $contents;
129		}
130
131		//at this point contents is empty, so lets fill it
132		$this->replace();
133
134		$contents = $this->open();
135
136		return $contents;
137	}
138
139	public function delete()
140	{
141		global $tikilib;
142
143		if ($this->isFileGal == true) {
144			FileGallery\File::filename($this->name)->delete();
145		} else {
146			TikiLib::lib("cache")->empty_type_cache(get_class($this));
147		}
148	}
149
150	function appendToContents(&$contents, $items)
151	{
152		if (isset($items->feed->entry)) {
153			$contents->entry[] = $items->feed->entry;
154		} elseif (isset($items)) {
155			$contents->entry[] = $items;
156		}
157	}
158
159	public function addItem($item)
160	{
161		global $tikilib;
162		$contents = $this->open();
163
164		if (empty($contents)) {
165			$contents = new Feed_Contents($this->type);
166		}
167
168		//this allows us to intercept the contents and do things like check the validity of the content being appended to the contents
169		$this->appendToContents($contents, $item);
170
171		$this->save($contents);
172
173		return $this;
174	}
175
176	public function feed($origin = '')
177	{
178		global $tikilib;
179		$contents = $this->getContents();
180
181		//TODO: convert to actual object
182		$feed = new Feed_Container(
183			$this->version,
184			$this->encoding, //we get this from the above call to open
185			$contents,
186			(! empty($origin) ? $origin : $tikilib->tikiUrl() . 'tiki-feed.php'),
187			$this->type
188		);
189
190		return $feed;
191	}
192
193	public function listArchives()
194	{
195		$archives = [];
196
197		if ($this->isFileGal == true) {
198			$file = FileGallery\File::filename($this->name);
199			foreach ($file->listArchives() as $archive) {
200				$archive = $this->open();
201				$archives[$archive->feed->date] = $archive->feed->entry;
202			}
203		}
204
205		return $archives;
206	}
207
208	public function getItemsFromDate($date)
209	{
210		$archives = $this->listArchives();
211		$archive = $archives[$date];
212		return $archive;
213	}
214
215	public function getItemFromDate($name, $date)
216	{
217		$archive = $this->getItemsFromDate($date);
218		foreach ($archive as $item) {
219			if ($name == $item->name) {
220				return $item;
221			}
222		}
223	}
224
225	public function getItemFromDates($name)
226	{
227		$archives = [];
228
229		foreach ($this->listArchives() as $archive) {
230			foreach ($archive as $item) {
231				if ($name == $item->name) {
232					$archives[] = $item;
233				}
234			}
235		}
236
237		return $archives;
238	}
239}
240