<?php
$CFG['mods']['include_files'][] = 'common/classes/class_Videopian.lib.php';


class GrapUrl
	{
		public function GrapUrl()
			{
				global $CFG, $LANG;
				$this->CFG = $CFG;
				$this->LANG =$LANG;
			}

		public function getContents($url)
			{
				$result = '';

				if(!strstr($url, '://'))
					$url = 'http://'.$url;

				if (function_exists('curl_init'))
					{
						$ch = curl_init(html_entity_decode(html_entity_decode($url)));
						curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
					    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.2) Gecko/20070219 Firefox/2.0.0.2');
					    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
					    $result = curl_exec($ch);
					    if (!curl_errno($ch))
					        curl_close($ch);
					     else
					        $result = false;
					}
				return $result;
			}

		public function getMetaDescription($url)
			{
				$content = $this->getContents($url);
				if(preg_match("|<meta[^>]*description[^>]*content=\"([^>]+)\"[^>]*>|Ui",$content, $matches))
					$meta_description = $matches[1];
				else
					$meta_description = '';
				return $meta_description;
			}
		public function getMetaTitle($url)
			{
				$title_content = $this->getContents($url);
				if(preg_match("|<meta[^>]*title[^>]*content=\"([^>]+)\"[^>]*>|Ui",$title_content, $matches))
					$meta_title = $matches[1];
				else
					$meta_title = '';
				return $meta_title;
			}
		public function getYouTubeVideoURL($url)
			{
				$obj = new Videopian();
				$voj = $obj->get($url);
				if(isset($voj->files['video/x-flv']) and $voj->files['video/x-flv'])
					{
						return $voj->files['video/x-flv'];
					}
				return false;
			}

		public function getYouTubeImageList($url)
			{
				$obj = new Videopian();
				$voj = $obj->get($url);
				$img_list = array();
				if(isset($voj->thumbnails) and $voj->thumbnails)
					{
						foreach($voj->thumbnails as $obj)
							{
								$img_list[] = $obj->url;
							}
						if($img_list)
							{
								return $img_list;
							}
					}
				return false;
			}

		public function getDailymotionVideoURL($url)
			{
				$obj = new Videopian();
				$voj = $obj->get($url);
				if(isset($voj->files['video/x-flv']) and $voj->files['video/x-flv'])
					{
						return $voj->files['video/x-flv'];
					}
				return false;
			}

		public function getDailymotionImageList($url)
			{
				$obj = new Videopian();
				$voj = $obj->get($url);
				$img_list = array();
				if(isset($voj->thumbnails) and $voj->thumbnails)
					{
						foreach($voj->thumbnails as $obj)
							{
								$img_list[] = $obj->url;
							}
						if($img_list)
							{
								return $img_list;
							}
					}
				return false;
			}

		public function getMetaCafeVideoURL($url)
			{
				$obj = new Videopian();
				$voj = $obj->get($url);
				if(isset($voj->files['video/x-flv']) and $voj->files['video/x-flv'])
					{
						return $voj->files['video/x-flv'];
					}
				return false;
			}

		public function getMetaCafeImageList($url)
			{
				$obj = new Videopian();
				$voj = $obj->get($url);
				if(isset($voj->thumbnails) and $voj->thumbnails)
					{
						foreach($voj->thumbnails as $obj)
							{
								$img_list[] = $obj->url;
							}
						if($img_list)
							{
								return $img_list;
							}
					}
				return false;
			}

		public function getBlipVideoURL($url)
			{
				$obj = new Videopian();
				$voj = $obj->get($url);
				if(isset($voj->files['video/x-flv']) and $voj->files['video/x-flv'])
					{
						return $voj->files['video/x-flv'];
					}
				return false;
			}

		public function getBlipImageList($url)
			{
				$obj = new Videopian();
				$voj = $obj->get($url);
				$img_list = array();
				if(isset($voj->thumbnails) and $voj->thumbnails)
					{
						foreach($voj->thumbnails as $obj)
							{
								$img_list[] = $obj->url;
							}
						if($img_list)
							{
								return $img_list;
							}
					}
				return false;
			}

		public function getGoogleVideoURL($url)
			{
				$obj = new Videopian();
				$voj = $obj->get($url);
				if(isset($voj->files['video/x-flv']) and $voj->files['video/x-flv'])
					{
						return $voj->files['video/x-flv'];
					}
				return false;
			}

		public function getGoogleImageList($url)
			{
				$obj = new Videopian();
				$voj = $obj->get($url);
				$img_list = array();
				if(isset($voj->thumbnails) and $voj->thumbnails)
					{
						foreach($voj->thumbnails as $obj)
							{
								$img_list[] = $obj->url;
							}
						if($img_list)
							{
								return $img_list;
							}
					}
				return false;
			}

		public function getMySpaceVideoUrl($url)
			{
				$obj = new Videopian();
				$voj = $obj->get($url);
				if(isset($voj->files['video/x-flv']) and $voj->files['video/x-flv'])
					{
						return str_replace('rtmp://', 'http://', $voj->files['video/x-flv']);
					}
				return false;
			}

		public function getMySpaceImageList($url)
			{
				$obj = new Videopian();
				$voj = $obj->get($url);
				$img_list = array();
				if(isset($voj->thumbnails) and $voj->thumbnails)
					{
						foreach($voj->thumbnails as $obj)
							{
								$img_list[] = $obj->url;
							}
						if($img_list)
							{
								return $img_list;
							}
					}
				return false;
			}

		public function getExternalVideoData($url, $option = '')
			{
				$obj = new Videopian();
				$voj = $obj->get($url);
				if($option)
					{
						if(isset($voj->$option) and $voj->$option)
							{
								return $voj->$option;
							}
						return false;
					}
				return $voj;
			}

		public function getValidImagesList($arr)
			{
				$return_arr = array();
				$time = time()+$this->CFG['bookmark']['total_image_grap_time'];
				$i = 0;
				foreach($arr as $key=>$url)
					{
						//if(preg_match("/^http.+\..+jpg$/i", $url) or preg_match("/^http.+\..+gif/i", $url))
						if(preg_match("/^http.+\..+jpg$/i", $url))
							{
								ini_set('default_socket_timeout',    $this->CFG['bookmark']['image_grap_time_per_image']);
								$att_arr = @getimagesize($url);
								if(is_array($att_arr))
									{
										if(($att_arr[0]>=$this->CFG['bookmark']['image_min_width'] and $att_arr[0]<=$this->CFG['bookmark']['image_max_width']) and ($att_arr[1]>=$this->CFG['bookmark']['image_min_height'] and $att_arr[1]<=$this->CFG['bookmark']['image_max_height']) and ($att_arr[2]>=$this->CFG['bookmark']['image_min_size'] and $att_arr[2]<=$this->CFG['bookmark']['image_max_size']))
											{
												$return_arr[] = $url;
												$i++;
												if($i>=$this->CFG['bookmark']['total_image_grap'])
													{
														return $return_arr;
													}
											}
									}
								if($time<time())
									{
										return $return_arr;
									}
							}
					}
				return $return_arr;
			}

		//type may be normal/youtube
		public function getImagesList($url, $type = "Other")
			{
				if($content = $this->getContents($url))
					{
						if($type == "Other")
							{
								return $url;
								$arr = MatchPattern('/\< *[img][^\>]*[src] *= *[\"\']{0,1}([^\"\'\ >]*)/', $content, 1);
								if($arr = $this->getValidImagesList($arr))
									{
										return $arr;
									}
							}
						else if($type == "Youtube")
							{
								return $this->getYouTubeImageList($url);
							}
						else if($type=="Dailymotion")
							{
								return $this->getDailymotionImageList($url);
							}
						else if($type=="Google")
							{
								return $this->getGoogleImageList($url);
							}
						else if($type=="Myspace")
							{
								return $this->getMySpaceImageList($url);
							}
						else if($type=="Metacafe")
							{
								return $this->getMetaCafeImageList($url);
							}
						else if($type=="Blip")
							{
								return $this->getBlipImageList($url);
							}
					}
				return false;
			}
	}
?>