<?php
header("Content-Type: text/xml;charset=iso-8859-1");
        define('DB_SERVER', 'mysql50-56.wc1.sat1.stabletransit.com');
	define('DB_SERVER_USERNAME', '438769_egyptusr');
	define('DB_SERVER_PASSWORD', 'M@srd1OMMYyaPach@');
	define('DB_DATABASE', '438769_egyptolution');
        function tinymce_insert($string){
	//quote
	preg_match_all('/\[quote]([\w\W]+?)\[\/quote]/',$string ,$quoteMatches);
	$countQuotes = count($quoteMatches)+1;
	for( $j = 0; $j <= $countLeft; $j++ ){
		$string= str_replace("[quote]".$quoteMatches[1][$j]."[/quote]",'<blockquote>'.$quoteMatches[1][$j].'</blockquote>', $string);
		}
	//left paragraph
	preg_match_all('/\[left]([\w\W]+?)\[\/left]/',$string ,$leftMatches);
	$countLeft = count($leftMatches)+1;
	for( $j = 0; $j <= $countLeft; $j++ ){
		$string= str_replace("[left]".$leftMatches[1][$j]."[/left]",'<div class="left_pragraph">'.$leftMatches[1][$j].'</div><br class="clear" />', $string);
		}
		//right paragraph
	preg_match_all('/\[right]([\w\W]+?)\[\/right]/',$string ,$rightMatches);
	$countRight = count($rightMatches)+1;
	for( $j = 0; $j <= $countRight; $j++ ){
		$string= str_replace("[right]".$rightMatches[1][$j]."[/right]",'<div class="right_pragraph">'.$rightMatches[1][$j].'</div><br class="clear" />', $string);
		}
		//Tips
	preg_match_all('/\[tip]([\w\W]+?)\[\/tip]/',$string ,$tipMatches);
	$countTip = count($tipMatches)+1;
	for( $j = 0; $j <= $countTip; $j++ ){
		$string= str_replace("[tip]".$tipMatches[1][$j]."[/tip]",'<div class="tip"><span>'.$tipMatches[1][$j].'</span></div>', $string);
		}

	//vimeo
	preg_match_all('/\[vimeo#([\d]+)]/',$string ,$vimeoMatches);
	$countVimeo = count($vimeoMatches) +1;
	for( $t=0; $t <= $countVimeo; $t++ ){
		$string= str_replace("[vimeo#".$vimeoMatches[1][$t]."]",'<iframe src="http://player.vimeo.com/video/'.$vimeoMatches[1][$t].'?title=0&amp;byline=0&amp;portrait=0&amp;color=ff9933" width="452" height="339" frameborder="0"></iframe>', $string);
		}
	//youtube
	preg_match_all('/\[youtube#([A-Z,a-z,0-9]+)]/',$string,$youtubeMatches);
	$countYoutube = count($youtubeMatches)+1;
	for($n=0;$n<=$countYoutube;$n++){
	$string=str_replace("[youtube#".$youtubeMatches[1][$n]."]",'<iframe class="youtube-player" type="text/html" width="452" height="339" src="http://www.youtube.com/embed/'.$youtubeMatches[1][$n].'" frameborder="0"></iframe>', $string);
	}

	return $string;
}
function image_insert($string, $id){
	preg_match_all('/\[img#([A-Z,a-z,0-9,\.]+)]/',$string ,$imageMatches);
	$countImages = count($imageMatches) +1;
	for( $j=0; $j <= $countImages; $j++ ){
		$string= str_replace("[img#".$imageMatches[1][$j]."]",'<img src="media/' . $id .'-post_images-'.$imageMatches[1][$j].'"/>', $string);
		}
	preg_match_all('[imgLeft#([ A-Z,a-z,0-9,\.]+)#([ A-Z,a-z,0-9_\.\-]+)]',$string ,$imageMatchestony);
	$countImages = count($imageMatchestony) +1;
	for( $j=0; $j <= $countImages; $j++ ){
		$string= str_replace("[imgLeft#".$imageMatchestony[1][$j]."#".$imageMatchestony[2][$j]."]",'<img class="left" src="media/' . $id .'-post_images-'.$imageMatchestony[1][$j].'" title="'.$imageMatchestony[2][$j].'"/>', $string);		}

		preg_match_all('[imgRight#([ A-Z,a-z,0-9,\.]+)#([ A-Z,a-z,0-9_\.\-]+)]',$string ,$imageMatchestony);
	$countImages = count($imageMatchestony) +1;
	for( $j=0; $j <= $countImages; $j++ ){
		$string= str_replace("[imgRight#".$imageMatchestony[1][$j]."#".$imageMatchestony[2][$j]."]",'<img class="right" src="media/' . $id .'-post_images-'.$imageMatchestony[1][$j].'" title="'.$imageMatchestony[2][$j].'"/>', $string);		}


	preg_match_all('/\[imgLeft#([A-Z,a-z,0-9,\.]+)]/',$string ,$imageLeftMatches);
	$countImages = count($imageLeftMatches) +1;
	for( $j=0; $j <= $countImages; $j++ ){
		$string= str_replace("[imgLeft#".$imageLeftMatches[1][$j]."]",'<img class="left" src="media/' . $id .'-post_images-'.$imageLeftMatches[1][$j].'"/>', $string);
		}

	preg_match_all('/\[imgRight#([A-Z,a-z,0-9,\.]+)]/',$string ,$imageRightMatches);
	$countImages = count($imageRightMatches) +1;
	for( $j=0; $j <= $countImages; $j++ ){
		$string= str_replace("[imgRight#".$imageRightMatches[1][$j]."]",'<img class="right" src="media/' . $id .'-post_images-'.$imageRightMatches[1][$j].'"/>', $string);
		}
		return $string;
}
    echo '<rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xmlns:ynews="http://news.yahoo.com/rss/" version="2.0">
    <channel>
    <title>Egyptolution RSS Feed</title>
    <language>en-us</language>
    <link href="http://egyptolution.com/" rel="alternate"/>
    <atom:link href="http://egyptolution.com/rss.xml" rel="self" type="application/rss+xml"/>
    <image>
<title>Egyptolution RSS Feed</title>
<url>http://egyptolution.com/images/last_logo.png</url>
<link>http://egyptolution.com/</link>
</image>
    <copyright>Copyright (C) 2011 egyptolution.com</copyright>
    <id>http://egyptolution.com/rss.xml</id>';

    $connection = @mysql_connect(DB_SERVER, DB_SERVER_USERNAME, DB_SERVER_PASSWORD)
        or die('Could not connect to database');
    mysql_select_db(DB_DATABASE)
        or die ('Could not select database');


    $sql="SELECT * FROM post
													LEFT JOIN author on post.author_id = author.author_id ORDER BY post_id DESC LIMIT 20";
    $result=mysql_query($sql);
    if($result)
                {
                while($rss=mysql_fetch_array($result))
	{
                    $rss['post_text'] = tinymce_insert($rss['post_text']);
                    $rss['post_text'] = image_insert($rss['post_text'], $rss[0]);
                    $rss['post_text']=str_replace("[vimeo#".$matches[1][$j]."]",'<object width="480" height="385"><param name="movie" value=http://player.vimeo.com/video/'.$matches[1][$j].'"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://player.vimeo.com/video/'.$matches[1][$j].'" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed></object>', $rss['post_text']);

     ?>
        <item>
	<title><?=$rss['post_title'];?></title>
        <link>http://egyptolution.com/post/<?=$rss['post_slug'];?>/<?=$rss['post_id'];?></link>
            <pubDate><?=date('r',strtotime($rss['post_date']));?></pubDate>
	<description><![CDATA[<img src="http://egyptolution.com/<?=$rss['img_thumb'];?>" title="<?=$rss['post_title'];?>">
	<?=$rss['post_text'];?>
	]]></description>
  </item>
    <?}}
    echo'</channel>
</rss>';?>