<?php
/**
* $Id: AdvancedQuery.php 1631 2007-05-12 22:40:28Z matthieu $
*/
if (!class_exists('Google_AdvancedQuery')) {
if (!defined('__CLASS_PATH__')) {
define('__CLASS_PATH__', realpath(dirname(__FILE__) . '/../'));
}
require_once __CLASS_PATH__ . '/Autoload.php';
/**
* tools for making easy query on google search browser
* @author Matthieu MARY <hide@address.com>
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
* @package google
*/
class Google_AdvancedQuery extends Google_Query {
const __PATTERN_REMOTE_URL__ = "http://www.phplibrairies.com/display_pattern_google_query.txt";
const __PATTERN_LOCAL_FILE__ = "google.pattern";
/**
* builder
* @param bool bCache : does you wants to enable cache?
* @param int $update_pattern_frequency : number of seconds when pattern
* must be updated
* @return void
* @access public
* @throws Exception
*/
public function __construct($update_pattern_frequency = 604800, $cache = null) {
parent :: __construct($cache);
$destinationFile = dirname(__FILE__) . "/" . self :: __PATTERN_LOCAL_FILE__;
$new_pattern = '';
// if we are over the duration validity, we delete the file to retrieve the new pattern
if (file_exists($destinationFile) && ((time() - $update_pattern_frequency) > filemtime($destinationFile))) {
unlink($destinationFile);
}
// if not file exists, we update the pattern from remote adress
if (!file_exists($destinationFile)) {
$new_pattern = $this->_retrievePattern();
if (empty ($new_pattern)) {
throw new Exception("Cannot retrieve the new detection pattern. send email to hide@address.com to ensure that remote url work properly");
}
else {
if (!file_put_contents($destinationFile, $new_pattern)) {
throw new Exception("Cannot write $destinationFile to put the new detection pattern. check your write permissions on this folder");
}
}
}
// update parent from the new detection pattern
if (file_exists($destinationFile)) {
$this->_updatePattern((empty ($new_pattern) ? trim(file_get_contents($destinationFile)) : $new_pattern));
}
}
/**
* return the file which contains the pattern
* @access public
* @return string
* @static
*/
public static function getFilePattern() {
return dirname(__FILE__).'/'.self :: __PATTERN_LOCAL_FILE__;
}
/**
* retrieve the latest pattern from remote url
* @access private
* @return string
*/
private function _retrievePattern() {
return trim(file_get_contents(self :: __PATTERN_REMOTE_URL__));
}
}
}