Return to Snippet

Revision: 6720
at June 10, 2008 21:44 by dereklio


Initial Code
<?php

require_once('HTTP/Client.php');
require_once(dirname(__FILE__) . '/Shell.class.php');

class HttpBot {

	private static $singleton;
	
	public static function singleton() {
		if (!isset(self::$singleton)) {
			$c = __CLASS__;
			self::$singleton = new $c;
		}
		
		return self::$singleton;
	}
	
	private $http;

	private function __construct() {
		$shell = Shell::singleton();
		$this->http = new HTTP_Client;
		$this->signature_purge(2);
		
		// debug codes, enabled for debug
		$this->signature_purge(0);
	}
	
	public function __clone() {
		trigger_error('Clone is not allowed.', E_USER_ERROR);
	}
	
	public function signature_exists($html) {
		$shell = Shell::singleton();
		return file_exists($shell->temp(md5($html) . '.sig'));
	}
	
	public function signature_save($html) {
		$shell = Shell::singleton();
		return $shell->touch_file($shell->temp(md5($html) . '.sig'));
	}
	
	public function signature_purge($days_old) {
		$shell = Shell::singleton();
		if ($dh = opendir($shell->temp())) {
			while (($file = readdir($dh)) !== false) {
				$path = $shell->temp($file);
				if (is_file($path)) {
					if ((time() - filemtime($path)) / 3600 / 24 > $days_old) {
						if (array_pop(explode('.', $file)) == 'sig') {
							unlink($path);
						}
					}
				}
			}
			closedir($dh);
		}
	}

	private function assert_response($code, $text = false) {
		if ($code != 200) {
			if ($text !== false) trigger_error($text . ' (Response Code: ' . $code . ')', E_USER_ERROR);
			return false;
		}
		return true;
	}
	
	private function assert_text($body, $chk, $text = false) {
		if (strpos($body, $chk) === false) {
			if ($text !== false) trigger_error($text, E_USER_ERROR);
			return false;
		}
		return true;
	}
	
	public function normalize_string($text) {
		$text = str_replace(array("\r", "\n", "\t"), " ", $text);
		while (strpos($text, '  ') !== false) {
			$text = str_replace('  ', ' ', $text);
		}
		return trim($text);
	}
	
	public function response($encoding = false) {
		$shell = Shell::singleton();
		$resp = $this->http->currentResponse();
		$raw = $resp['body'];
		
		if ($encoding !== false) {
			$raw = $shell->concmd_utf8($raw, $encoding);
		}
		$raw = iconv('utf-8', 'utf-8//IGNORE', $raw);
		
		$body = mb_convert_encoding($raw, 'HTML-ENTITIES', 'utf-8');
		
		$dom = new DOMDocument;
		$dom->preserveWhiteSpace = false;
		@$dom->loadHTML($body);
		
		$xpath = new DOMXPath($dom);
		
		return array($dom, $xpath, $raw);
	}
	
	public function geta($url, $error = false) {
		return $this->assert_response($this->http->get($url), $error);
	}
	
	public function posta($url, $data, $error = false) {
		return $this->assert_response($this->http->post($url, $data), $error);
	}

	public function contenta($chk, $text = false) {
		$resp = $this->http->currentResponse();
		return $this->assert_text($resp['body'], $chk, $text);
	}

}

?>

Initial URL


Initial Description


Initial Title
Http Bot class in PHP

Initial Tags


Initial Language
PHP