Advertise here with AdTwirl.com', 'Earn money with your mobile site @ AdTwirl.com'); // ads to backfill empty result from adtwirl
var $_test_mode = 0; // 1 for test mode on, 0 for off
var $_callback_key = ''; // define unique callback key here (optional)
/* additional parameters as array */
var $_params = array(); //dont change
var $_format = 'html'; // dont change
var $_ads_found = false; // dont change
var $_is_bot = false; // dont change
var $_is_mobile = false; // dont change
var $_version = '20130402'; // dont change
var $_contents = ''; // dont change
function __construct($_site_code=null)
{
$this->_ad_source_url = 'http://'.$this->_ad_source_host.'/'.$this->_ad_source_file;
if(!empty($_site_code))$this->_site_code = $_site_code;
}
function is_mobile()
{
if($this->is_mobile_header() == true)
{
$this->_is_mobile = true;
return true;
}
$keyname_ua_arr = array('HTTP_X_ORIGINAL_USER_AGENT', 'HTTP_X_DEVICE_USER_AGENT', 'HTTP_X_OPERAMINI_PHONE_UA', 'HTTP_X_BOLT_PHONE_UA', 'HTTP_X_MOBILE_UA', 'HTTP_X_SKYFIRE_PHONE', 'HTTP_USER_AGENT');
foreach ($keyname_ua_arr as $keyname_ua)
{
if (!empty($_SERVER[$keyname_ua]))
{
if($this->is_mobile_useragent($_SERVER[$keyname_ua])==true)
{
$this->_is_mobile = true;
return true;
break;
}
}
}
return false;
}
function is_mobile_header()
{
if(
isset($_SERVER['HTTP_ACCEPT']) &&
(strpos($_SERVER['HTTP_ACCEPT'], 'application/x-obml2d') !== false || // Opera Mini; @reference: http://dev.opera.com/articles/view/opera-binary-markup-language/
strpos($_SERVER['HTTP_ACCEPT'], 'application/vnd.rim.html') !== false || // BlackBerry devices.
strpos($_SERVER['HTTP_ACCEPT'], 'text/vnd.wap.wml') !== false ||
strpos($_SERVER['HTTP_ACCEPT'], 'application/vnd.wap.xhtml+xml') !== false) ||
isset($_SERVER['HTTP_X_WAP_PROFILE']) || // @todo: validate
isset($_SERVER['HTTP_X_WAP_CLIENTID']) ||
isset($_SERVER['HTTP_WAP_CONNECTION']) ||
isset($_SERVER['HTTP_PROFILE']) ||
isset($_SERVER['HTTP_X_OPERAMINI_PHONE_UA']) || // Reported by Nokia devices (eg. C3)
isset($_SERVER['HTTP_X_NOKIA_IPADDRESS']) ||
isset($_SERVER['HTTP_X_NOKIA_GATEWAY_ID']) ||
isset($_SERVER['HTTP_X_ORANGE_ID']) ||
isset($_SERVER['HTTP_X_VODAFONE_3GPDPCONTEXT']) ||
isset($_SERVER['HTTP_X_HUAWEI_USERID']) ||
isset($_SERVER['HTTP_UA_OS']) || // Reported by Windows Smartphones.
isset($_SERVER['HTTP_X_MOBILE_GATEWAY']) || // Reported by Verizon, Vodafone proxy system.
isset($_SERVER['HTTP_X_ATT_DEVICEID']) || // Seend this on HTC Sensation. @ref: SensationXE_Beats_Z715e
//HTTP_X_NETWORK_TYPE = WIFI
( isset($_SERVER['HTTP_UA_CPU']) &&
$_SERVER['HTTP_UA_CPU'] == 'ARM' // Seen this on a HTC.
)
){
return true;
}
return false;
}
function is_mobile_useragent($ua)
{
$regex_match="/(nokia|phone|pod|pad|tablet|touch|android|motorola|^mot\-|softbank|foma|docomo|kddi|up\.(browser|link)|";
$regex_match.="htc|dopod|blazer|netfront|helio|hosin|huawei|novarra|ios|meego|webos|techfaith|palm|";
$regex_match.="blackberry|alcatel|amoi|nexian|samsung|^sam\-|s[cg]h|^lge|ericsson|philips|sagem|wellcom|bunjalloo|maui|";
$regex_match.="series(4|6)0|symbian|smartphone|midp|wap|windows (ce|phone)|iemobile|^spice|^bird|^zte\-|longcos|pantech|gionee|^sie\-|portalmmm|";
$regex_match.="jig\s browser|hiptop|^ucweb|^benq|haier|^lct|opera\s*mobi|opera\*mini|320x320|240x320|176x220|480x640|600x800|";
$regex_match.="avantgo|bolt|vodafone|pocket|pdxgw|astel|minimo|plucker|pda|xiino|cricket|silk|zune|";
$regex_match.="playstation|fennec|hiptop|maemo|compal|kindle|mmp|p(ixi|re)\/|psp|treo|xda|wireless| mobi";
$regex_match.=")/i";
if(preg_match($regex_match, strtolower($ua)))
{
return true;
}
return false;
}
function is_bot()
{
$bot_arr = array(
"google", "bot",
"yahoo", "spider",
"archiver", "curl",
"python", "nambu",
"twitt", "perl",
"sphere", "pear",
"java", "wordpress",
"radian", "crawl",
"yandex", "eventbox",
"monitor", "mechanize",
"facebook",
"majestic", "alexa",
"froogle", "inktomi",
"jeeves", "webbug",
"rambler", "bing",
"zyborg", "rabaz",
"fetcher", "partners",
"t9space", "phonefavs",
"phonif", "adtwirl"
);
foreach($bot_arr as $bot)
{
if( stristr($_SERVER['HTTP_USER_AGENT'], $bot) )
{
$this->_is_bot = true;
return true;
break;
}
}
}
function get_contents()
{
if($this->_test_mode != 1)
{
if($this->is_bot() == true) return 'AdTwirl.com - Mobile Advertising Network';
if($this->is_mobile() == false) return '';
if(empty($this->_site_code)) return 'no site code specified
';
}
if((function_exists('curl_init')&&$this->_request_method==0)||$this->_request_method==1)
{
$contents = $this->curl_ad_request();
}
elseif((function_exists('file_get_contents')&&$this->_request_method==0)||$this->_request_method==2)
{
$contents = $this->file_get_contents_ad_request();
}
elseif($this->_request_method==99)
{
$contents = $this->non_ad_request();
}
else
{
$contents = $this->fsockopen_ad_request();
}
return trim($contents);
}
function curl_ad_request()
{
$curl_opt = array (
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
//CURLOPT_FOLLOWLOCATION => true,
CURLOPT_POST => true,
CURLOPT_FORBID_REUSE => true,
CURLOPT_POSTFIELDS => $this->get_post_body(),
CURLOPT_CONNECTTIMEOUT => $this->_timeout,
CURLOPT_TIMEOUT => ($this->_timeout+1),
CURLOPT_HTTPHEADER => array('Content-Type: application/x-www-form-urlencoded', 'Connection: Close')
);
if( $ch = curl_init($this->_ad_source_url) )
{
curl_setopt_array($ch, $curl_opt);
$contents = curl_exec($ch);
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if( 200 > $http_status || 299 < $http_status )
{
return false;
}
}
return $contents;
}
function file_get_contents_ad_request()
{
ini_set(allow_url_fopen, '1');
$opts = array('http' =>
array(
'method' => 'POST',
'header' => "Content-type: application/x-www-form-urlencoded\r\nConnection: Close\r\n",
'timeout' => $this->_timeout,
'content' => $this->get_post_body()
)
);
$context = stream_context_create($opts);
$contents = file_get_contents($this->_ad_source_url, false, $context);
return $contents;
}
function fsockopen_ad_request()
{
ini_set(allow_url_fopen, '1');
$query = $this->get_post_body();
$request = @fsockopen($this->_ad_source_host, 80, $errno, $errstr, $this->_timeout);
$post = "POST /".$this->_ad_source_file." HTTP/1.0\r\n";
$post .= "Host: ".$this->_ad_source_host."\r\n";
$post .= "Content-type: application/x-www-form-urlencoded\r\n";
$post .= "Content-length: ".strlen($query)."\r\n";
$post .= "Connection: close\r\n\r\n$query\r\n\r\n";
fwrite($request, $post);
while (!feof($request)) {
$contents .= fgets($request, 512);
}
fclose($request);
return $contents;
}
function non_ad_request()
{
return $this->_contents;
}
function ads_array()
{
$contents = $this->get_contents();
$arr = explode("\n", $contents);
$count = count($arr);
$ads = array();
for($i=0;$i$/', $arr[$i])) $ads[] = $arr[$i];
if(!empty($arr[$i])) $ads[] = $arr[$i];
}
$this->_ads_found = count($ads);
if($this->_ads_found<$this->_num_of_ads && $this->_backfill==1)
{
for($i=$this->_ads_found;$i<$this->_num_of_ads;$i++)
{
if(!empty($this->_backfill_ads))$ads[] = $this->_backfill_ads[rand(0, count($this->_backfill_ads))];
}
}
return $ads;
}
function show_ad()
{
$arr = (array)$this->ads_array();
$ad = implode('
', $arr);
return $ad;
}
function get_apache_headers()
{
$headers_arr = array('HTTP_ACCEPT_LANGUAGE', 'HTTP_USER_AGENT', 'HTTP_HOST', 'REQUEST_URI', 'HTTP_ACCEPT', 'HTTP_REFERER', 'HTTP_CLIENT_IP', 'REMOTE_ADDR', 'REMOTE_HOST', 'HTTP_VIA', 'HTTP_WAP_CONNECTION', 'HTTP_UA_CPU', 'HTTP_XID', 'HTTP_CLIENTID', 'HTTP_MSISDN');
//x headers and geoip
foreach($_SERVER as $k => $v ) {
if(stristr($k, 'HTTP_X'))
{
$header_key = str_replace(array('HTTP_X_', '_'), array('x-', '-'), $k);
$arr[] = strtolower($header_key).'='.urlencode($v);
}
elseif(stristr($k, 'GEOIP_'))
{
$header_key = str_replace('_', '-', $k);
$arr[] = strtolower($header_key).'='.urlencode($v);
}
elseif(in_array($k, $headers_arr))
{
$header_key = str_replace('_', '-', $k);
$arr[] = strtolower($header_key).'='.urlencode($v);
}
}
$keyname_ua_arr = array('HTTP_X_ORIGINAL_USER_AGENT', 'HTTP_X_DEVICE_USER_AGENT', 'HTTP_X_OPERAMINI_PHONE_UA', 'HTTP_X_BOLT_PHONE_UA', 'HTTP_X_MOBILE_UA', 'HTTP_X_UCBROWSER_DEVICE_UA', 'HTTP_X_ORIGINAL_PHONE_UA', 'HTTP_USER_AGENT');
foreach ($keyname_ua_arr as $keyname_ua) {
if (!empty($_SERVER[$keyname_ua])) {
$arr[] = 'ua='.trim($_SERVER[$keyname_ua]);
break;
}
}
$keyname_ip_arr = array('HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_REMOTE_ADDR_REAL', 'HTTP_CLIENT_IP', 'HTTP_X_REAL_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'HTTP_X_COMING_FROM', 'HTTP_COMING_FROM', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_X_MOBILE_GATEWAY', 'HTTP_X_NOKIA_IPADDRESS', 'HTTP_HTTP_CLIENT_IP', 'HTTP_X_UP_FORWARDED_FOR', 'REMOTE_ADDR');
foreach ($keyname_ip_arr as $keyname_ip) {
if (!empty($_SERVER[$keyname_ip])) {
$ip = trim($_SERVER[$keyname_ip]);
if (strstr($ip, ',')) {
$ips = explode(',', $ip);
foreach ($ips as $ip)
{
if(filter_var(trim($ip), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE)) break(2);
}
}
if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE)) break;
}
}
$arr[] = 'ip='.trim($ip);
return $arr;
}
function get_post_body()
{
$arr = $this->get_apache_headers();
$arr[] = 'site_code='.$this->_site_code;
$arr[] = 'adult='.$this->_adult_ads;
$arr[] = 'image='.$this->_image_ads;
$arr[] = 'free='.$this->_free_ads;
$arr[] = 'cpa='.$this->_cpa_ads;
$arr[] = 'test='.$this->_test_mode;
$arr[] = 'ads='.$this->_num_of_ads;
$arr[] = 'version='.$this->_version;
$arr[] = 'format='.$this->_format;
$arr[] = 'cb='.$this->_callback_key;
foreach($this->_params as $k => $v)
{
$arr[] = $k.'='.$v;
}
$post = implode('&', $arr);
return $post;
}
}
?>