<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
$url = $_REQUEST['url'];

if ($url) {
	$url = base64_decode($url);

	$parseUrlArr = parse_url($url);
	$hostName = isset($parseUrlArr['host']) ? $parseUrlArr['host'] : '';

	$ch = curl_init();
	curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; rv:19.0) Gecko/20100101 Firefox/19.0');
	curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
	curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	$contents = curl_exec($ch);
	$content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
	curl_close($ch);

	$doc = new DOMDocument();
	@$doc->loadHTML($contents);
	$tags = $doc->getElementsByTagName('img');
	$res = '';
	$cnt = 0;
	$content_type_array = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png');

	if ($tags->length <= 0 && array_search($content_type, $content_type_array) !== false && $content_type != '' && $content_type != null && $content_type != false) {
		$url = effectiveURL($url, $hostName);
		if (urlExist($url)) {
			$res.= '<img id="imgsrc' . $cnt++ . '" src="' . $url . '" />:;';
		}
	} else {
		$image_new_array = array();
		foreach ($tags as $tag) {

			$image_new = $tag->getAttribute('src');
			if ($image_new != '' && $image_new != null) {
				$image_new = effectiveURL($image_new, $hostName);
				if (urlExist($image_new) && array_search($image_new, $image_new_array) === false) {

					$image_new_array[] = $image_new;
					$res.= '<img id="imgsrc' . $cnt++ . '" src="' . $image_new . '" />:;';

					if( $cnt > 23 ) {
						break;
					}
				}
			}
		}
	}
}

echo $res;

function urlExist($url) {

	error_reporting(0);
	$handle = curl_init();
	if (false === $handle) {
		return false;
	}

	curl_setopt($handle, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; rv:19.0) Gecko/20100101 Firefox/19.0');
	curl_setopt($handle, CURLOPT_COOKIEJAR, 'cookie.txt');
	curl_setopt($handle, CURLOPT_COOKIEFILE, 'cookie.txt');
	curl_setopt($handle, CURLOPT_URL, $url);
	curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
	$connectable = curl_exec($handle);
	$content_type_o = curl_getinfo($handle, CURLINFO_CONTENT_TYPE);
	curl_close($handle);

	if( $connectable == '' || $connectable == null || $connectable == false ) {
		return false;
	}
	
	$content_type_a = explode(';', $content_type_o);
	$content_type = '';
	if ( isset($content_type_a[0]) ) {
		$content_type = $content_type_a[0];
	}

	$content_type_array = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png');
	if (array_search($content_type, $content_type_array) === false || $content_type == '' || $content_type == null || $content_type == false) {
		return false;
	}

	$image = imagecreatefromstring($connectable);

	if( $image === false ) {
		return false;
	}

	$width = imagesx($image);
	$height = imagesy($image);
	if( $width <= 100 || $height <= 100 || $height == '' || $height == null || $height == false ) {
		return false;
	}

	return true;
}

function effectiveURL($url, $hostName) {

	if( substr($url, 0, 2) == '//' ) {
		return 'http:' . $url;
	}

	$parseUrlArr = parse_url($url);
	$parseUrl = isset($parseUrlArr['scheme']) ? $parseUrlArr['scheme'] . '://' : 'http://';
	$parseUrl .= isset($parseUrlArr['host']) ? $parseUrlArr['host'] : "$hostName";
	$parseUrl .= isset($parseUrlArr['path']) ? $parseUrlArr['path'] : '/';
	$parseUrl .= isset($parseUrlArr['query']) ? '?' . $parseUrlArr['query'] : '';
	$parseUrl .= isset($parseUrlArr['fragment']) ? '#' . $parseUrlArr['fragment'] : '';

	return $parseUrl;
}

?>