<?php
/**
 * Changes by nao-pon 2008-2011.
 * - A revision for a class.
 * - Add a function "getHtml()".
 */

/**
 * Copyright (C) 2007 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     @license http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

class googleAdsense {

	var $google_dt;
	var $notWork = FALSE;

	function googleAdsense() {
		$GLOBALS['google']['https']=$this->read_global('HTTPS');
		$GLOBALS['google']['ip']=$this->read_global('REMOTE_ADDR');
		$GLOBALS['google']['markup']='xhtml';
		$GLOBALS['google']['output']='xhtml';
		$GLOBALS['google']['ref']=$this->read_global('HTTP_REFERER');
		$GLOBALS['google']['url']=$this->read_global('HTTP_HOST') . $this->read_global('REQUEST_URI');
		$GLOBALS['google']['useragent']=$this->read_global('HTTP_USER_AGENT');
		$this->google_dt = time();
		if (! empty($GLOBALS['google']['client'])) {
			$this->google_set_screen_res();
			$this->google_set_muid();
			$this->google_set_via_and_accept();
		} else {
			$this->notWork = TRUE;
		}
	}

	function read_global($var) {
	  return isset($_SERVER[$var]) ? $_SERVER[$var]: '';
	}

	function google_append_url(&$url, $param, $value) {
	  $url .= '&' . $param . '=' . urlencode($value);
	}

	function google_append_globals(&$url, $param) {
	  $this->google_append_url($url, $param, $GLOBALS['google'][$param]);
	}

	function google_append_color(&$url, $param) {
	  $color_array = explode(',', $GLOBALS['google'][$param]);
	  $this->google_append_url($url, $param,
	                    $color_array[$this->google_dt % count($color_array)]);
	}

	function google_set_screen_res() {
	  $screen_res = $this->read_global('HTTP_UA_PIXELS');
	  if ($screen_res == '') {
	    $screen_res = $this->read_global('HTTP_X_UP_DEVCAP_SCREENPIXELS');
	  }
	  if ($screen_res == '') {
	    $screen_res = $this->read_global('HTTP_X_JPHONE_DISPLAY');
	  }
	  $res_array = preg_split('/[x,*]/', $screen_res);
	  if (count($res_array) == 2) {
	    $GLOBALS['google']['u_w']=$res_array[0];
	    $GLOBALS['google']['u_h']=$res_array[1];
	  }
	}

	function google_set_muid() {
	  $muid = $this->read_global('HTTP_X_DCMGUID');
	  if ($muid != '') {
	    $GLOBALS['google']['muid']=$muid;
	     return;
	  }
	  $muid = $this->read_global('HTTP_X_UP_SUBNO');
	  if ($muid != '') {
	    $GLOBALS['google']['muid']=$muid;
	     return;
	  }
	  $muid = $this->read_global('HTTP_X_JPHONE_UID');
	  if ($muid != '') {
	    $GLOBALS['google']['muid']=$muid;
	     return;
	  }
	  $muid = $this->read_global('HTTP_X_EM_UID');
	  if ($muid != '') {
	    $GLOBALS['google']['muid']=$muid;
	     return;
	  }
	}

	function google_set_via_and_accept() {
	  $ua = $this->read_global('HTTP_USER_AGENT');
	  if ($ua == '') {
	    $GLOBALS['google']['via']=$this->read_global('HTTP_VIA');
	    $GLOBALS['google']['accept']=$this->read_global('HTTP_ACCEPT');
	  }
	}

	function google_get_ad_url() {
	  $google_ad_url = 'http://pagead2.googlesyndication.com/pagead/ads?';
	  $this->google_append_url($google_ad_url, 'dt',
	                    round(1000 * array_sum(explode(' ', microtime()))));
	  foreach ($GLOBALS['google'] as $param => $value) {
	    if ($param === 'client' && substr($value, 0, 4) === 'pub-') {
	      $this->google_append_url($google_ad_url, $param,
	                        'ca-mb-' . $GLOBALS['google'][$param]);
	    } elseif (strpos($param, 'color_') === 0) {
	      $this->google_append_color($google_ad_url, $param);
	    } else if (strpos($param, 'url') === 0) {
	      $google_scheme = ($GLOBALS['google']['https'] == 'on')
	          ? 'https://' : 'http://';
	      $this->google_append_url($google_ad_url, $param,
	                        $google_scheme . $GLOBALS['google'][$param]);
	    } else {
	      $this->google_append_globals($google_ad_url, $param);
	    }
	  }
	  return $google_ad_url;
	}

	function getHtml() {
		if ($this->notWork) {
			return '';
		}
		$html = '';
		$h = new Hyp_HTTP_Request();
		$h->init();
		$h->url = $this->google_get_ad_url();
		$h->connect_timeout = 3;
		$h->read_timeout = 2;
		$h->get();
		if ($h->rc === 200) {
			$html = $h->data;
		}
		return $html;
	}
}
