<?php
// MOBFOX PUBLISHER TAG
// Language: PHP (curl)

$pcl_params = array(
  'INVENTORY_HASH'      => '2d41799811918fa82bdfdf0847dc730b', // Required to request ads. To find your Inventory Hash (former Publisher ID), log in to your MobFox account and click on the "Sites & Apps" tab.
  'ADSPACE_WIDTH'      => 728, // Maximum Width of your Banner
  'ADSPACE_HEIGHT'      => 90, // Maximum Height of your Banner
  'ADSPACE_STRICT'      => 1, // If you change this parameter to 1, the ad size set above will be strict and no smaller ads will be shown.
  'AD_REQUEST'        => true, // To request an ad, set to TRUE.
  'TEST_MODE'         => true, // While testing, set to TRUE. When you are ready to make live requests, set to FALSE.
  // Additional optional parameters are available at: http://developers.mobfox.com/index.php/Ad_Requests
  'OPTIONAL'          => array()
);

/* This code supports the ability for your website to set a cookie on behalf of MobFox
 * To set an MobFox cookie, simply call pcl_setcookie() on any page that you call pcl_request()
 * The call to pcl_setcookie() must occur before any output has been written to the page (http://www.php.net/setcookie)
 * If your mobile site uses multiple subdomains (e.g. "a.example.com" and "b.example.com"), then pass the root domain of your mobile site (e.g. "example.com") as a parameter to pcl_setcookie().
 * This will allow the MobFox cookie to be visible across subdomains
 */
//pcl_setcookie();

/* MobFox strongly recommends using cookies as it allows us to better uniquely identify users on your website.
 * This benefits your mobile site by providing:
 *    - Improved ad targeting = higher click through rates = more revenue!
 */
 
// Send request to MobFox. To make additional ad requests per page, copy and paste this function call elsewhere on your page.
echo pcl_request($pcl_params);

/////////////////////////////////
// Do not edit below this line //
/////////////////////////////////

// This section defines MobFox functions and should be used AS IS.
// We recommend placing the following code in a separate file that is included where needed.

function pcl_request($pcl_params) {
  static $pixel_sent = false;

  $ad_mode = false;
  if (!empty($pcl_params['AD_REQUEST']) && !empty($pcl_params['INVENTORY_HASH'])) $ad_mode = true;
    
  $protocol = 'http';
  if (!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) != 'off') $protocol = 'https';
  
  $rt = 1;
  
  list($usec, $sec) = explode(' ', microtime()); 
  $params = array('rt=' . $rt,
                  'z=' . ($sec + $usec),
                  'u=' . urlencode($_SERVER['HTTP_USER_AGENT']), 
                  'i=' . urlencode($_SERVER['REMOTE_ADDR']), 
                  'p=' . urlencode("$protocol://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']),
                  'v=' . urlencode('20081105-PHPCURL-acda0040bcdea222')); 

  $sid = empty($pcl_params['SID']) ? session_id() : $pcl_params['SID'];
  if (!empty($sid)) $params[] = 't=' . md5($sid);
  if ($ad_mode) $params[] = 's=' . $pcl_params['INVENTORY_HASH'];
  if (!empty($_COOKIE['pclcookie'])) $params[] = 'o=' . $_COOKIE['pclcookie'];
  if (!empty($pcl_params['ADSPACE_WIDTH'])) $params[] = 'adspace_width=' . $pcl_params['ADSPACE_WIDTH'];
  if (!empty($pcl_params['ADSPACE_HEIGHT'])) $params[] = 'adspace_height=' . $pcl_params['ADSPACE_HEIGHT'];
  if (!empty($pcl_params['ADSPACE_STRICT'])) $params[] = 'adspace_strict=' . $pcl_params['ADSPACE_STRICT'];
  if (!empty($pcl_params['TEST_MODE'])) $params[] = 'm=test';

  if (!empty($pcl_params['OPTIONAL'])) {
    foreach ($pcl_params['OPTIONAL'] as $k => $v) {
      $params[] = urlencode($k) . '=' . urlencode($v);
    }
  }

  $ignore = array('HTTP_PRAGMA' => true, 'HTTP_CACHE_CONTROL' => true, 'HTTP_CONNECTION' => true, 'HTTP_USER_AGENT' => true, 'HTTP_COOKIE' => true);
  foreach ($_SERVER as $k => $v) {
    if (substr($k, 0, 4) == 'HTTP' && empty($ignore[$k]) && isset($v)) {
      $params[] = urlencode('h[' . $k . ']') . '=' . urlencode($v);
    }
  }
  
  $post = implode('&', $params);
  $request = curl_init();
  $request_timeout = 1; // 1 second timeout
  curl_setopt($request, CURLOPT_URL, 'http://my.mobfox.com/request.php');
  curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($request, CURLOPT_TIMEOUT, $request_timeout);
  curl_setopt($request, CURLOPT_CONNECTTIMEOUT, $request_timeout);
  curl_setopt($request, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded', 'Connection: Close'));
  curl_setopt($request, CURLOPT_POSTFIELDS, $post);
  list($usec_start, $sec_start) = explode(' ', microtime());
  $contents = curl_exec($request);
  list($usec_end, $sec_end) = explode(' ', microtime());
  curl_close($request);

  if ($contents === true || $contents === false) $contents = '';
  
  if (preg_match('<!--NOAD-->', $contents)){ 
  global $no_ad_availible;
  $no_ad_availible=true;
  }
  else
  {
  global $no_ad_availible;
  $no_ad_availible=false;  
  }


  /*if (!$pixel_sent) {
    $pixel_sent = true;
    $contents .= "<img src=\"http://my.mobfox.com/sendcookie.php?"
              . 'rt=' . $rt
              . '&z=' . ($sec + $usec)
              . '&s=' . ($ad_mode ? $pcl_params['INVENTORY_HASH'] : '')
              . '&o=' . (empty($_COOKIE['pclcookie']) ? '' : $_COOKIE['pclcookie'])
              . '&lt=' . ($sec_end + $usec_end - $sec_start - $usec_start)
              . '&to=' . $request_timeout
              . '" alt="" width="1" height="1"/>';
  }*/
  
  return $contents;
}

function pcl_setcookie($domain = '', $path = '/') {
  if (empty($_COOKIE['pclcookie'])) {    
    $value = md5(uniqid(rand(), true));
    if (!empty($domain) && $domain[0] != '.') $domain = ".$domain";
    if (setcookie('pclcookie', $value, mktime(0, 0, 0, 1, 1, 2038), $path, $domain)) {
      $_COOKIE['pclcookie'] = $value; // make it visible to pcl_request()
    } 
  }
}
?>
