1<?php
2
3/*
4 * Nibbleblog -
5 * http://www.nibbleblog.com
6 * Author Diego Ignacio Gabriel Najar Carrascal
7
8 * All NibbleBlog code is released under the GNU General Public License.
9 * See COPYRIGHT.txt and LICENSE.txt.
10*/
11
12class POST_DBXML extends NBFUNCTIONS {
13
14/*
15======================================================================================
16	VARIABLES
17======================================================================================
18*/
19		public $file_xml; 			// Contains the link to the blog_config.xml file
20		public $obj_xml; 				// Contains the object of the blog_config.xml file
21		public $flag_error;			// Error Flag
22		public $flag_i18n;			// Menssage Error
23
24		private $files;
25		private $files_count;
26
27		private $last_insert_id;
28
29/*
30======================================================================================
31	CONSTRUCTORS
32======================================================================================
33*/
34		function POST_DBXML($file)
35		{
36			$this->flag_error = false;
37			$this->flag_i18n = "";
38			$this->last_insert_id = max($this->get_autoinc() - 1, 0);
39			$this->files = array();
40			$this->files_count = 0;
41
42			$this->file_xml = $file;
43
44			if (file_exists($this->file_xml))
45			{
46				$this->obj_xml = new NBXML($this->file_xml, 0, TRUE, '', FALSE);
47			}
48			else
49			{
50				$this->flag_error = true;
51				$this->flag_i18n = 'FILE_COULDNT_BE_OPENED';
52			}
53		}
54
55/*
56======================================================================================
57	VIDEO FUNCTIONS
58======================================================================================
59*/
60	// Return Video's ID
61	private function get_video_id($url)
62	{
63		// Youtube ID
64		preg_match('/[\\?\\&]v=([^\\?\\&]+)/', $url, $matches);
65
66		return($matches[1]);
67	}
68
69	// Return Video's thumbnail
70	private function get_video_thumbnail($url)
71	{
72		// Youtube thumbnail
73		$video_id = $this->get_video_id( $url );
74		$thumbnail = 'http://img.youtube.com/vi/' . $video_id . '/2.jpg';
75
76		return( $thumbnail );
77	}
78
79	// Return Video's embed
80	private function get_video_embed($url)
81	{
82		// Youtube embed HTML 5
83		$video_id = $this->get_video_id( $url );
84		$embed = '<iframe class="youtube-player" type="text/html" width="640" height="385" src="http://www.youtube.com/embed/'.$video_id.'" frameborder="0"></iframe>';
85
86		return( $embed );
87	}
88
89/*
90======================================================================================
91	METHODS
92======================================================================================
93*/
94
95		public function flag_error()
96		{
97			return($this->flag_error);
98		}
99
100		public function flag_i18n()
101		{
102			return($this->flag_i18n);
103		}
104
105		public function savetofile()
106		{
107			if( !($this->obj_xml->asXML($this->file_xml)) )
108			{
109				$this->flag_error = true;
110				$this->flag_i18n = 'FILE_COULDNT_BE_SAVED';
111			}
112		}
113
114		public function get_count()
115		{
116			return( $this->files_count );
117		}
118
119		public function get_autoinc()
120		{
121			return( (int) $this->obj_xml['autoinc'] );
122		}
123
124		private function set_autoinc($value = 0)
125		{
126			$this->obj_xml['autoinc'] = $value + $this->get_autoinc();
127		}
128
129		/*
130			* ARGS['type']
131			* ARGS['title']
132			* ARGS['content']
133			* ARGS['video']
134			* ARGS['album']
135			* ARGS['allow_comments']
136			* ARGS['idcat']
137			* ARGS['iduser']
138		*/
139		public function add($ARGS)
140		{
141			$time_unix = $this->unixstamp();
142			$time = $this->unixstamp_to_date(0, $time_unix);
143
144			$xml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
145			$xml .= '<post>';
146			$xml .= '</post>';
147
148			$obj = new NBXML($xml, 0, FALSE, '', FALSE);
149
150			$obj->ADD_Element('type',				$this->html2text($ARGS['type']) );
151			$obj->ADD_Element('title',				$this->html2text($ARGS['title']) );
152			$obj->ADD_Element('content',			$ARGS['content']);
153			$obj->ADD_Element('pub_date',			$time_unix);
154			$obj->ADD_Element('mod_date',			'0');
155			$obj->ADD_Element('visits',			'0');
156			$obj->ADD_Element('allow_comments',	$this->html2text($ARGS['allow_comments']) );
157
158			if( $ARGS['type'] == 'video' )
159				$obj->ADD_Element('video', $this->html2text($ARGS['video']) );
160
161			// Last insert ID
162			$idpost = $this->last_insert_id = $this->get_autoinc();
163
164			// Filename for new post
165			$time_tmp = $time['Y'] . '.' . $time['m'] . '.' . $time['d'] . '.' . $time['H'] . '.' . $time['i'] . '.' . $time['s'];
166			$filename = $idpost . '.' . $ARGS['idcat'] . '.' . $ARGS['iduser'] . '.NULL.' . $time_tmp . '.xml';
167
168			// Save to file
169			if( $obj->asXml( PATH_POSTS . $filename ) )
170			{
171				// Sticky
172				if( $ARGS['sticky'] == 1 )
173					$this->add_sticky( $this->last_insert_id );
174
175				// Autoinc
176				$this->set_autoinc(1);
177
178				// Save post.xml
179				$this->savetofile();
180			}
181			else
182			{
183				$this->flag_error = true;
184				$this->flag_i18n = 'FILE_COULDNT_BE_SAVED';
185			}
186
187		}
188
189		public function set($ARGS)
190		{
191			$this->set_file( $ARGS['idpost'] );
192
193			if($this->files_count > 0)
194			{
195				$obj_xml = new NBXML(PATH_POSTS . $this->files[0], 0, TRUE, '', FALSE);
196
197				$obj_xml->title				= utf8_encode($this->html2text($ARGS['title']));
198				$obj_xml->content 			= utf8_encode($ARGS['content']);
199				$obj_xml->mod_date 			= utf8_encode($this->unixstamp());
200				$obj_xml->allow_comments	= utf8_encode($this->html2text($ARGS['allow_comments']));
201
202				if( $ARGS['sticky'] == 1 )
203					$this->add_sticky( $ARGS['idpost'] );
204				else
205					$this->remove_sticky( $ARGS['idpost'] );
206
207				// Save to file post.xml
208				$this->savetofile();
209
210				// Save to file the post
211				if( !$obj_xml->asXml( PATH_POSTS . $this->files[0] ) )
212				{
213					$this->flag_error = true;
214					$this->flag_i18n = 'FILE_COULDNT_BE_SAVED';
215				}
216			}
217		}
218
219		public function add_sticky($idpost)
220		{
221			if( !$this->is_sticky($idpost)  )
222				$this->obj_xml->sticky->ADD_Element('id', $idpost);
223		}
224
225		public function remove_sticky($idpost)
226		{
227			if( $this->is_sticky($idpost)  )
228			{
229				$tmp_node = $this->obj_xml->xpath('/post/sticky/id[.="'.$idpost.'"]');
230				$dom = dom_import_simplexml($tmp_node[0]);
231				$dom->parentNode->removeChild($dom);
232			}
233		}
234
235		public function get_last_insert_id()
236		{
237			return( $this->last_insert_id );
238		}
239
240		private function set_file($idpost)
241		{
242			$this->files = $this->ls(PATH_POSTS, $idpost.'.*.*.*.*.*.*.*.*.*', 'xml', false, false, false);
243			$this->files_count = count( $this->files );
244		}
245
246		// setea los parametros de la clase
247		// obtiene todos los archivos post
248		private function set_files()
249		{
250			$this->files = $this->ls(PATH_POSTS, '*', 'xml', false, false, true);
251			$this->files_count = count( $this->files );
252		}
253
254		private function set_files_by_category($idcat)
255		{
256			$this->files = $this->ls(PATH_POSTS, '*.'.$idcat.'.*.*.*.*.*.*.*.*', 'xml', false, false, true);
257			$this->files_count = count( $this->files );
258		}
259
260		// Devuelve los items de un post
261		// File name: IdPost.IdCategory.Iduser.IdOther.YYYY.MM.DD.HH.mm.ss.xml
262		private function get_items($file)
263		{
264			$obj_xml = simplexml_load_file(PATH_POSTS . $file, NULL, TRUE);
265
266			$file_info = explode('.', $file);
267
268			$content = (string) utf8_decode($obj_xml->content);
269			$tmp_content = explode("<!-- pagebreak -->", $content);
270
271			$tmp_array = array();
272
273			$tmp_array['filename']			= (string) $file;
274			$tmp_array['idpost']				= (int) $file_info[0];
275			$tmp_array['idcategory']		= (int) $file_info[1];
276			$tmp_array['iduser']				= (int) $file_info[2];
277			$tmp_array['type']				= (string) utf8_decode($obj_xml->type);
278			$tmp_array['title']				= (string) utf8_decode($obj_xml->title);
279			$tmp_array['pub_date']			= (string) utf8_decode($obj_xml->pub_date);
280			$tmp_array['mod_date']			= (string) utf8_decode($obj_xml->mod_date);
281			$tmp_array['allow_comments']	= (int) utf8_decode($obj_xml->allow_comments);
282			$tmp_array['visits']				= (int) utf8_decode($obj_xml->visits);
283
284			$tmp_array['content']			= (string) $content;
285			$tmp_array['content_part1']	= (string) $tmp_content[0];
286			$tmp_array['content_part2']	= (string) $tmp_content[1];
287
288			$tmp_array['title_rewurl']		= $this->clean_url($tmp_array['title']);
289
290			if($tmp_array['type'] == 'video')
291			{
292				$tmp_array['video']			= (string) utf8_decode($obj_xml->video);
293				$tmp_array['idvideo']		= (string) $this->get_video_id( $tmp_array['video'] );
294				$tmp_array['thumbnail1']	= (string) $this->get_video_thumbnail( $tmp_array['video'] );
295				$tmp_array['embed']			= (string) $this->get_video_embed( $tmp_array['video'] );
296			}
297
298			return( $tmp_array );
299		}
300
301		public function is_sticky($idpost)
302		{
303			return( $this->obj_xml->xpath('/post/sticky/id[.="'.$idpost.'"]') != array() );
304		}
305
306		private function get_list_by($page_number, $post_per_page)
307		{
308			$init = (int) $post_per_page * $page_number;
309			$end  = (int) min( ($init + $post_per_page - 1), $this->files_count - 1 );
310			$outrange = $init > $end;
311
312			$tmp_array = array();
313
314			if( !$outrange )
315			{
316				for($init; $init <= $end; $init++)
317				{
318					array_push( $tmp_array, $this->get_items( $this->files[$init] ) );
319				}
320			}
321
322			return( $tmp_array );
323		}
324
325		public function get_list_by_sticky()
326		{
327			$tmp_array = array();
328			foreach( $this->obj_xml->sticky->id as $idpost )
329			{
330				$this->set_file((int)$idpost);
331				array_push( $tmp_array, $this->get_items( $this->files[0] ) );
332			}
333
334			return( $tmp_array );
335		}
336
337		public function get_list_by_tag($dbxml_tags, $page_number, $post_per_page)
338		{
339			return( array() );
340		}
341
342		public function get_list_by_archives($month, $year, $page_number, $post_per_page)
343		{
344			return( array() );
345		}
346
347		public function get_list_by_category($page_number, $post_per_page, $idcat)
348		{
349			$this->set_files_by_category($idcat);
350
351			if($this->files_count > 0)
352				return( $this->get_list_by($page_number, $post_per_page) );
353			else
354				return( array() );
355		}
356
357		public function get_list_by_page($page_number, $post_per_page)
358		{
359			// Set the list of post
360			$this->set_files();
361
362			if($this->files_count > 0)
363				return( $this->get_list_by($page_number, $post_per_page) );
364			else
365				return( array() );
366		}
367
368		public function get($idpost)
369		{
370			// Set the post
371			$this->set_file($idpost);
372
373			if($this->files_count > 0)
374				return( $this->get_items( $this->files[0] ) );
375			else
376				return( array() );
377		}
378
379		public function delete($idpost)
380		{
381			$this->set_file($idpost);
382
383			if($this->files_count > 0)
384			{
385				if( !unlink( PATH_POSTS . $this->files[0] ) )
386				{
387					$this->flag_error = true;
388					$this->flag_i18n = 'FILE_COULDNT_BE_DELETED';
389				}
390			}
391		}
392
393
394} // END Class
395
396?>
397