<?php
# search2feed 
# @author: bronco@warriordudimanche.net / warriordudimanche.net
# @version 0.1
# @license  free and opensource
# @use: search2feed.php?q=your+query+here


function Random_referer(){
		return array_rand(array(
			'http://oudanstoncul.com.free.fr/‎',
			'http://googlearretedenousfliquer.fr/‎',
			'http://stopspyingme.fr/‎',
			'http://spyyourassfuckinggoogle.fr/‎',
			'http://dontfuckinglookatme.fr/‎',
			'http://matemonculgoogle.fr/‎',
			'http://auxarmescitoyens.fr/‎',
			'http://jetlametsavecdugravier.con/‎',
			'http://lesdeuxpiedsdanstagueule.fr/‎',
			'http://moncoudedanstabouche.con/‎',
			'http://monpieddanston.uk/‎',
			'http://bienfaitpourvosgueul.es/‎',
			'http://pandanstesdents.fr/‎',
			'http://tupuessouslesbras.fr/‎',
			'http://mangetescrottesdenez.fr/‎',
			'http://jtepourristesstats.fr/‎',
			'http://ontecompissevigoureusement.com/‎',
			'http://lepoingleveetlemajeuraussi.com/‎',
		));
	}


	function return_UTF8($var){		
		$utf8=str_replace("\xef\xbb\xbf\xA0", '',utf8_encode($var));// SANS BOM !!!!

		if (strpos($var,"\xA0")){return $utf8;}
		if (strpos($utf8,'Ã')){return $var;}else {return $utf8;}
	
	}

	function file_curl_contents($url){
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept-Charset: UTF-8'));
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,  FALSE);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_URL, $url);
		if (!ini_get("safe_mode") && !ini_get('open_basedir') ) {curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);}
		curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
		curl_setopt($ch, CURLOPT_REFERER, random_referer());// notez le referer "custom"

		$data = curl_exec($ch);
		$response_headers = curl_getinfo($ch);

		// Google seems to be sending ISO encoded page + htmlentities, why??
		if($response_headers['content_type'] == 'text/html; charset=ISO-8859-1') $data = html_entity_decode(iconv('ISO-8859-1', 'UTF-8//TRANSLIT', $data)); 
		
		# $data = curl_exec($ch);

		curl_close($ch);

		return return_utf8($data);
	}
	function parse_query($query,$title='Search2feed',$nb, $link='http://warriordudimanche.net',$lang='fr',$safesearch='&safe=off'){
		$REGEX_WEB='#(?<=<h3 class="r"><a href="/url\?q=)([^&]+).*?>(.*?)</a>.*?(?<=<span class="st">)(.*?)(?=</span>)#';
		$REGEX_PAGES='#&start=([0-9]+)|&start=([0-9]+)#';
		$lang=strip_tags($lang);
		$URL='https://www.google.com/search?hl='.$lang.$safesearch.'&id=hp&q=';

		$page=file_curl_contents($URL.str_replace(' ','+',urlencode($query)).'&num='.$nb);

		if (!$page){return false;}
		preg_match_all($REGEX_WEB, $page, $r);
		preg_match_all($REGEX_PAGES,$page,$p);
		$p=count($p[2]);

		$content=array();
		foreach ($r[1] as $key=>$val){
			$content[$key]['description']=strip_tags($r[3][$key]);
			$content[$key]['link']=strip_tags($r[1][$key]);
			$content[$key]['title']=strip_tags($r[2][$key]);
			$content[$key]['guid']=strip_tags($r[1][$key]).'#1';
			$content[$key]['pubDate']=@date('r');

		}

		$rss=array(
			'infos'=>array(
				'type'=>'rss',
				'description'=>strip_tags($query),
				'title'=>$title,
				'link'=>$link,
				
			),
			'items'=>$content
		);

		return $rss;
		
	}
function array2feed($array=null){
	if (!$array){return false;}
	
	if (empty($array['infos']['type'])){$array['infos']['type']='rss';}else{$array['infos']['type']=strtolower($array['infos']['type']);}
	if (empty($array['infos']['description'])){$array['infos']['description']='';}
	$r="\n";$t="\t";
	$tpl=array('rss'=>array(),'atom'=>array());
	$tpl['rss']['header']='<?xml version="1.0" encoding="utf-8" ?>'.$r.'<rss version="2.0"  xmlns:content="http://purl.org/rss/1.0/modules/content/">'.$r.$t.'<channel>'.$r;
	$tpl['atom']['header']='<feed xmlns="http://www.w3.org/2005/Atom">'.$r;
	$tpl['rss']['footer']=$t.'</channel></rss>'.$r;
	$tpl['atom']['footer']='</feed>'.$r;
	$tpl['rss']['content-type']='Content-Type: application/rss+xml';
	$tpl['atom']['content-type']='Content-Type: application/atom+xml;charset=utf-8';

	header($tpl[$array['infos']['type']]['content-type']);
	$feed=$tpl[$array['infos']['type']]['header'];
		//create the feed's info content
		foreach($array['infos'] as $key=>$value){
			if ($array['infos']['type']=='atom'){ // ATOM
				if ($key=='link'){$feed.=$t.$t.'<link href="'.$value.'" rel="self" type="application/atom+xml"/>'.$r;}
				elseif ($key=='author'){$feed.=$t.$t.'<author><name>'.$value.'</name></author>'.$r;}
				elseif ($key=='licence'){$feed.=$t.$t.'<'.$key.' href="'.$value.'" rel="license"/>'.$r;} // in atom feed, licence is the link to the licence type
				elseif ($key!='version'&&$key!='type'){$feed.=$t.$t.'<'.$key.'>'.$value.'</'.$key.'>'.$r;}
			}else{ // RSS
				if ($key!='version'&&$key!='type'){$feed.=$t.$t.'<'.$key.'>'.$value.'</'.$key.'>'.$r;}
			}
		}

		//then the items content
		foreach ($array['items'] as $item){
			if ($array['infos']['type']=='atom'){ $feed.=$t.$t.$t.'<entry>'.$r;}else{$feed.=$t.$t.$t.'<item>'.$r;}
				foreach($item as $key=>$value){
					if ($array['infos']['type']=='atom'){ // ATOM
						if ($key=='link'){$feed.=$t.$t.$t.$t.'<link href="'.$value.'" rel="alternate" type="text/html"/>'.$r;}
						elseif ($key=='content'){$feed.=$t.$t.$t.$t.'<content type="text">'.htmlspecialchars($value).'</content>'.$r;}
						else{$feed.=$t.$t.$t.$t.'<'.$key.'>'.$value.'</'.$key.'>'.$r;}
					}else{ // RSS
						if ($key=='date'||$key=='pubDate'||$key=='title'||$key=='link'){$feed.=$t.$t.$t.$t.'<'.$key.'>'.htmlspecialchars($value).'</'.$key.'>'.$r;}
						elseif($key=='guid'){ $feed.=$t.$t.$t.$t.'<guid isPermaLink="false">'.$value.'</guid>'.$r;}
						else{$feed.=$t.$t.$t.$t.'<'.$key.'><![CDATA['.$value.']]></'.$key.'>'.$r;}
					}
				}
			if ($array['infos']['type']=='atom'){ $feed.=$t.$t.$t.'</entry>'.$r;}else{$feed.=$t.$t.$t.'</item>'.$r;}
		}


	$feed.=$tpl[$array['infos']['type']]['footer'];
	return $feed;
}

// récupération des parametres get

if (isset($_GET['q'])){$query=strip_tags($_GET['q']);}else{exit('pas de query');}
if (isset($_GET['title'])){$title=strip_tags($_GET['title']);}else{$title='GoogolSearch for '.$query;}
if (isset($_GET['nb'])){$nb=strip_tags($_GET['nb']);}else{$nb=20;}

echo array2feed(parse_query($query,$title,$nb));

?>