// Define the start and end of the repeating region. These tags are required. // These must be contained within the header and footer tags. // // hides contents in algo results // // hides contents in paid results // // Replaced with title data // // Replaced with description data // // Replaced with referal URL // // Replaced with a human readable URL // // Replaced with cost to advertiser amount, 0 if algo result // // Replaced with advertiser Logo, null if algo result // // Replaced with result index // // Replaces with number of hits // // Replaced with result total // // Replaces with feed access time // You have the option of using PHP code inside the template file but do bear in mind that this taxes the server resources slightly. // SIMPLE SAMPLE TEMPLATE (template.tpl): //

Promotions

// //
// .
// class mivaResults { var $feedParsed; // array of parsed feed // download and parse a PCMS query function mivaContentRequest($pcms,$affiliateId,$numResults,$page,$region) { $hostIpAddress = (isset($_SERVER["REMOTE_ADDR"]) ? $_SERVER["REMOTE_ADDR"] : $HTTP_SERVER_VARS["REMOTE_ADDR"]); $this->parseRequestData($this->mivaFetch("http://contentsearch.{$region}.espotting.com/search/content/results.asp?resulttype=qsearch&contentidentifier={$pcms}&hits={$numResults}&affiliateid={$affiliateId}&page={$page}&host=$hostIpAddress&domain={$region}")); } // download and parse a free search query function mivaSearchRequest($keyword,$affiliateId,$numResults,$page,$region,$bkfill) { $hostIpAddress = (isset($_SERVER["REMOTE_ADDR"]) ? $_SERVER["REMOTE_ADDR"] : $HTTP_SERVER_VARS["REMOTE_ADDR"]); $keyword = urlencode($keyword); $this->parseRequestData($this->mivaFetch("http://affiliate.{$region}.espotting.com/search/qsearch/results.asp?keyword={$keyword}&hits={$numResults}&affiliateid={$affiliateId}&page={$page}&bkfill={$bkfill}&resulttype=qsearch&host=$hostIpAddress&domain={$region}")); } // fetch local or remote file data.Remote files must begin with 'http:' you might need to prepend $_SERVER['DOCUMENT_ROOT'] for local files function mivaFetch($url) { if (ereg("^http:",$url)) { $fileData = ''; $url_parsed = parse_url($url); // host port path if (!isset($url_parsed['port'])) {$url_parsed['port'] = 80;} $handle = fsockopen($url_parsed['host'], $url_parsed['port'], $errno, $errstr, 2); socket_set_timeout($handle, 2); if ($handle) { fwrite ($handle , "GET /{$url_parsed['path']}?{$url_parsed['query']} HTTP/1.0\r\n\r\n"); while (!feof($handle )) { $fileData .= fgets ($handle ,8192); } } fclose($handle); unset($handle); } else { $fileData = file_get_contents($url); } return $fileData; } // Parse the plain text feed and populate var $feedParsed; function parseRequestData($resultDat) { $this->feedParsed = array(); preg_match_all("/[\n\r]*([a-zA-Z0-9]*)\=([^\n\r]*)[\n\r]*/i", $resultDat,$dat); $templateHash = array('T'=>'Title','D'=>'Description','U'=>'ActualURL','Y'=>'ShowURL','C'=>'Cost','I'=>'ImageURL','P'=>'Position','HITS'=>'Hits','TOTAL'=>'Total','TIME'=>'Time'); foreach($dat[1] as $key=>$value) { if (!isset($this->feedParsed[$templateHash[$value]])) { $this->feedParsed[$templateHash[$value]][0] = $dat[2][$key]; } else { $this->feedParsed[$templateHash[$value]][count($this->feedParsed[$templateHash[$value]])] = $dat[2][$key]; } } } // This function parses the tempalte data using the parsed feed data stored in var $feedParsed and returns it as a string. // You must of called mivaSearchRequest() or mivaContentRequest() beforehand function mivaTemplate($templateData,$numResults,$skip=0) { $outputHtml = ''; if (ereg("\<\!\-\- *[header]{4,6} *\-\->",$templateData)) {$headFlag = true;} if (ereg("\<\!\-\- *[footer]{4,6} *\-\->",$templateData)) {$footFlag = true;} $templateData = preg_split("/\<\!\-\- *[header|footer]{4,6} *\-\->/",$templateData); if ($headFlag == true && $footFlag = true) { $templateHead = $templateData[0];$templateBody = $templateData[1];$templateFoot = $templateData[2]; } elseif ($headFlag == true && $footFlag = false) { $templateHead = $templateData[0];$templateBody = $templateData[1]; } elseif ($headFlag == false && $footFlag = true) {} else { $templateBody = $templateData[0];$templateFoot = $templateData[1]; } $cpcPattern = "/|/i"; $algoPattern = "/|/i"; for ($i=0;$i < $numResults;$i++) { $this->feedParsed['ActualURL'][$i + $skip] = str_replace("&", "&", $this->feedParsed['ActualURL'][$i + $skip]); if ($this->feedParsed['Title'][$i + $skip] != null) { $templateBodyTemp = $templateBody; $matches = array(); if ($this->feedParsed['Cost'][$i + $skip] == 0) { preg_match_all($cpcPattern, $templateBodyTemp, $matches, PREG_OFFSET_CAPTURE); for($iMatch = 0; $iMatch < count($matches[0]); $iMatch = $iMatch + 2) { $templateBodyTemp = substr($templateBodyTemp,0,$matches[0][$iMatch][1]) . substr($templateBodyTemp,$matches[0][$iMatch + 1][1] + strlen($matches[0][$iMatch + 1][0]),strlen($templateBodyTemp)); } } else { preg_match_all($algoPattern, $templateBodyTemp, $matches, PREG_OFFSET_CAPTURE); for($iMatch = 0; $iMatch < count($matches[0]); $iMatch = $iMatch + 2) { $templateBodyTemp = substr($templateBodyTemp,0,$matches[0][$iMatch][1]) . substr($templateBodyTemp,$matches[0][$iMatch + 1][1] + strlen($matches[0][$iMatch + 1][0]),strlen($templateBodyTemp)); } } $outputHtml .= $templateBodyTemp; foreach ($this->feedParsed as $dataType=>$valueArr) { if (isset($this->feedParsed[$dataType][$i + $skip])) {$outputHtml = str_replace("",$this->feedParsed[$dataType][$i + $skip],$outputHtml);} } } } $outputHtml = preg_replace('/|||/i','',$outputHtml); return $templateHead . $outputHtml . $templateFoot; } // Blank function to catch errors function mivaError() { } // Execute PHP code contained within the template, uses the output buffer to catch the output function mivaEval($strInput) { if(!ob_get_status() == null){ $strObuffer = ob_get_contents(); ob_clean(); $adObuffer = eval('?>' . $strInput); $adObuffer = ob_get_contents(); ob_clean(); echo $strObuffer; return $adObuffer; } else { ob_start(); eval('?>' . $strInput); $adObuffer = ob_get_clean (); return $adObuffer; } } // Return input string as a javascript docuemnt.write() function function mivaJavaScript($strInput) { $strInput = str_replace('"','\"',$strInput); $strInput = ereg_replace("[[:space:]\n\r\t]{1,20}",' ',$strInput); return "\n\n"; } // end class mivaResults } // ***************** USAGE ****************** // ** SINGLE TEMPLATE SEARCH EXAMPLE** // include_once('mivaCode.php'); // $feedObj = new mivaSearch($_GET['keyword'],4460,10); // echo $feedObj->mivaSearchTemplate('template.tpl'); // unset($feedObj); // ** SINGLE TEMPLATE SEARCH WITH BACKFILL EXAMPLE** // include_once('mivaCode.php'); // $feedObj = new mivaSearch($_GET['keyword'],4460,10,null,null,1); // echo $feedObj->mivaSearchTemplate('template.tpl'); // unset($feedObj); class mivaSearch extends mivaResults { function mivaSearch($keyword,$affiliateId,$numResults,$page=1,$region='uk',$bkfill=0) { set_error_handler(array(&$this, 'mivaError')); if ($page == null) {$page = 1;} if ($region == null) {$region = 'se';} if ($bkfill == null) {$bkfill = 0;} $this->numResults = $numResults; $this->mivaSearchRequest($keyword,$affiliateId,$numResults,$page,$region,$bkfill); } function mivaSearchTemplate($templatePath) { $strOutHTML = $this->mivaTemplate($this->mivaFetch($templatePath),$this->numResults,0); if (strpos($strOutHTML,' -1) { return $this->mivaEval($strOutHTML); } else { return $strOutHTML; } } } // ** SINGLE TEMPLATE CONTENT EXAMPLE** // include_once('mivaCode.php'); // $feedObj = new mivaContent("es001",4460,3); // echo $feedObj->mivaContentTemplate('template.tpl'); // unset($feedObj); class mivaContent extends mivaResults { function mivaContent($pcms,$affiliateId,$numResults,$page=1,$region='uk') { set_error_handler(array(&$this, 'mivaError')); if ($page == null) {$page = 1;} if ($region == null) {$region = 'uk';} $this->numResults = $numResults; $this->mivaContentRequest($pcms,$affiliateId,$numResults,$page,$region); } function mivaContentTemplate($templatePath) { $strOutHTML = $this->mivaTemplate($this->mivaFetch($templatePath),$this->numResults,0); if (strpos($strOutHTML,' -1) { return $this->mivaEval($strOutHTML); } else { return $strOutHTML; } } } // ** DOUBLE TEMPLATE SEARCH EXAMPLE** // include_once('mivaCode.php'); // $feedObj = new mivaSplitSearch($_GET['keyword'],4460,8); // echo $feedObj->mivaSplitSearchTemplate('template.tpl',3); // echo $feedObj->mivaSplitSearchTemplate('template.tpl',5); // unset($feedObj); // ** DOUBLE TEMPLATE SEARCH WITH BACKFILL EXAMPLE** // include_once('mivaCode.php'); // $feedObj = new mivaSplitSearch($_GET['keyword'],4460,8,null,null,1); // echo $feedObj->mivaSplitSearchTemplate('template.tpl',3); // echo $feedObj->mivaSplitSearchTemplate('template2.tpl',5); // unset($feedObj); class mivaSplitSearch extends mivaResults { function mivaSplitSearch($keyword,$affiliateId,$numResults,$page=1,$region='uk',$bkfill=0) { set_error_handler(array(&$this, 'mivaError')); if ($page == null) {$page = 1;} if ($region == null) {$region = 'uk';} if ($bkfill == null) {$bkfill = 0;} $this->resultCursor = 0; $this->mivaSearchRequest($keyword,$affiliateId,$numResults,$page,$region,$bkfill); } function mivaSplitSearchTemplate($templatePath,$numResults) { $this->resultCursor = $this->resultCursor + $numResults; $strOutHTML = $this->mivaTemplate($this->mivaFetch($templatePath),$numResults,$this->resultCursor - $numResults); if (strpos($strOutHTML,' -1) { return $this->mivaEval($strOutHTML); } else { return $strOutHTML; } } } // ** DOUBLE TEMPLATE CONTENT ** // include_once('mivaCode.php'); // $feedObj = new mivaSplitContent("es001",4460,10); // echo $feedObj->mivaSplitContentTemplate('template.tpl',3); // echo $feedObj->mivaSplitContentTemplate('template.tpl',5); // unset($feedObj); class mivaSplitContent extends mivaResults { function mivaSplitContent($pcms,$affiliateId,$numResults,$page=1,$region='uk') { set_error_handler(array(&$this, 'mivaError')); if ($page == null) {$page = 1;} if ($region == null) {$region = 'uk';} $this->resultCursor = 0; $this->mivaContentRequest($pcms,$affiliateId,$numResults,$page,$region); } function mivaSplitContentTemplate($templatePath,$numResults) { $this->resultCursor = $this->resultCursor + $numResults; $strOutHTML = $this->mivaTemplate($this->mivaFetch($templatePath),$numResults,$this->resultCursor - $numResults); if (strpos($strOutHTML,' -1) { return $this->mivaEval($strOutHTML); } else { return $strOutHTML; } } } ?>