<?php

/**
 * to get the advertisement
 *
 * @access	public
 * @param	string	block name
 * @return	string
 */
$______ADVERTISEMENT_ID = array();
function getAdvertisement($block, $return = false)
	{
		global $CFG;
		global $db;
		global $______ADVERTISEMENT_ID;

		if(!chkAllowedModule(array('banner')))
			return;

		$sql = 'SELECT add_id, source FROM '.$CFG['db']['tbl']['advertisement'].
				' WHERE post_from=\'Admin\' AND block='.$db->Param('block').
				' AND status=\'activate\'';

		if($CFG['admin']['banner']['impressions_date'])
			{
				$sql .= ' AND NOW()>=start_date AND (((allowed_impressions!=\'\' AND allowed_impressions!=0) AND'.
						' (completed_impressions<allowed_impressions)) OR ((allowed_impressions=\'\''.
						' OR allowed_impressions=0) AND (end_date!=\'0000-00-00 00:00:00\''.
						' AND end_date>NOW())))';
			}

		$stmt = $db->Prepare($sql);
		$rs = $db->Execute($stmt, array($block));
		    if (!$rs)
			    trigger_db_error($db);

		$total_count = $rs->PO_RecordCount();

   		if(!$total_count)
   			return false;

		$add_array = array();

		$need = rand(1, $total_count);
		$i = 1;
		while($row = $rs->FetchRow())
			{
				if($need!=$i)
					{
						$i++;
						continue;
					}
				$______ADVERTISEMENT_ID[$row['add_id']] = $row['add_id'];

				if($return)
					return $block;
				echo htmlentitydecode($row['source']);
				return;
			}
	}

/**
 * to update the advertisement view count
 *
 * @access	public
 * @param
 * @return	static
 */
function updateAdvertisementCount()
	{
		global $______ADVERTISEMENT_ID;
		global $CFG;
		global $db;

		if(!$CFG['admin']['banner']['impressions_date'])
			return;

		if(sizeof($______ADVERTISEMENT_ID))
			{
				$sql = 'UPDATE '.$CFG['db']['tbl']['advertisement'].' SET'.
						' completed_impressions=completed_impressions+1'.
						' WHERE add_id IN('.implode(',', $______ADVERTISEMENT_ID).')';

				$stmt = $db->Prepare($sql);
				$rs = $db->Execute($stmt);
				    if (!$rs)
					    trigger_db_error($db);

				$______ADVERTISEMENT_ID = array();
			}
	}

/**
 * displayTopAnalystSmallImage()
 *
 * @param mixed $img_arr
 * @param mixed $showPopup
 * @param mixed $contentDisplay
 * @return
 */
function displayTopAnalystSmallImage($img_arr, $showPopup = true, $contentDisplay = false)
    {
    	displayProfileImage($img_arr, 'small', $showPopup, $contentDisplay);
		return ;
    }

/**
 * displayTopAnalystTinyImage()
 *
 * @param mixed $img_arr
 * @param mixed $showPopup
 * @param mixed $contentDisplay
 * @return
 */
function displayTopAnalystTinyImage($img_arr, $showPopup = true, $contentDisplay = false)
    {
    	displayProfileImage($img_arr, 'tiny', $showPopup, $contentDisplay);
		return ;
    }

function fileGetContentsManual($url, $check_for_url_exists=false)
	{
		if($check_for_url_exists)
			{
				if (function_exists('curl_init'))
					{
						$resURL = curl_init();
						curl_setopt($resURL, CURLOPT_URL, $url);
						curl_setopt($resURL, CURLOPT_BINARYTRANSFER, 1);
						curl_setopt($resURL, CURLOPT_FAILONERROR, 1);
						curl_exec ($resURL);
						$intReturnCode = curl_getinfo($resURL, CURLINFO_HTTP_CODE);
						curl_close ($resURL);
						if ($intReturnCode != 200 && $intReturnCode != 302 && $intReturnCode != 304)
						  return false;
					}
				return true;
			}

		if(file_exists($url) or !function_exists('curl_init'))
			return @file_get_contents($url);


		$options = array(
		        CURLOPT_RETURNTRANSFER => true,     // return web page
		        CURLOPT_HEADER         => false,    // don't return headers
		        CURLOPT_FOLLOWLOCATION => true,     // follow redirects
		        CURLOPT_ENCODING       => "",       // handle all encodings
		        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", // who am i
		        CURLOPT_AUTOREFERER    => true,     // set referer on redirect
//			    CURLOPT_CONNECTTIMEOUT => 60,      // timeout on connect
		        CURLOPT_TIMEOUT        => 300,      // timeout on response
		        CURLOPT_MAXREDIRS      => 3,       // stop after 10 redirects
		    );

		    $ch      = curl_init( $url );
		    curl_setopt_array( $ch, $options );
		    $content = curl_exec( $ch );
		    $err     = curl_errno( $ch );
		    $errmsg  = curl_error( $ch );
		    $header  = curl_getinfo( $ch );
		    curl_close( $ch );

		    $header['errno']   = $err;
		    $header['errmsg']  = $errmsg;
		    $header['content'] = $content;


			if ( $header['errno'] != 0 )
					return false;

			if ( $header['http_code'] != 200 )
				return false;

			return $content;

	}

?>