Created
October 17, 2014 19:03
-
-
Save atomicpages/6a4243e5fac8933447db to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Class FileNotFoundException | |
* @author Dennis Thompson | |
* @license MIT | |
* @version 1.0 | |
*/ | |
class FileNotFoundException extends Exception { | |
public function __construct($message, $code = 0, Exception $previous = null) { | |
parent::__construct($message, $code, $previous); | |
} | |
} | |
/** | |
* Class Log | |
* @author Dennis Thompson | |
* @license MIT | |
* @version 1.0 | |
*/ | |
class Log { | |
private $handle; // store resource | |
/** | |
* Open $file in cwd for appending, create if not created | |
* @param $file | |
* @param string $mode | |
*/ | |
public function __construct($file, $mode = "a") { | |
$this->handle = fopen($file, $mode); | |
} | |
/** | |
* Writes info to the log | |
* @param mixed $entries and string or an array to write to log | |
* @access public | |
*/ | |
public function log($entries) { | |
if(is_string($entries)) { | |
fwrite($this->handle, "Error: [" . date("d/M/Y H:i:s") . "] " . $entries . "\n"); | |
} else { | |
foreach($entries as $value) { | |
fwrite($this->handle, "Error: [" . date("d/M/Y H:i:s") . "] " . $value . "\n"); | |
} | |
} | |
} | |
} | |
/** | |
* Class Import | |
* @author Dennis Thompson | |
* @license MIT | |
* @version 1.1 | |
*/ | |
class Import { | |
const LOG_NAME = "beer_map_error.log"; | |
const REMOTE_URL = "http://beermapping.com/kml/"; | |
const REMOTE_FILE = "sandiego.kml"; | |
const LOCAL_KML_FILENAME = "sandiego.kml"; | |
private $errors, $mail_on_failure, $to, $subject, $headers, $log; | |
public function __construct($mail_on_failure = true) { | |
$this->log = new Log(self::LOG_NAME); // open log for appending | |
$this->errors = array(); | |
$this->mail_on_failure = $mail_on_failure; | |
if($this->mail_on_failure) { | |
$this->to = "[email protected]"; | |
$this->subject = "Error retrieving beer maps kml"; | |
$this->headers = "From: " . $_SERVER["SERVER_NAME"] . "<[email protected]>\r\n"; | |
$this->headers .= "X-Mailer: PHP/" . phpversion(); | |
} | |
} | |
/** | |
* Gets the errors for custom error handling | |
* @return array | |
* @access public | |
* @since 1.0 | |
*/ | |
public function getErrors() { | |
return $this->errors; | |
} | |
/** | |
* Gets a file from a remote server and downloads it | |
* @access public | |
*/ | |
public function getRemoteFile() { | |
if(!file_exists("public_html/" . self::LOCAL_KML_FILENAME) || sha1_file("http://beermapping.com/kml/sandiego.kml") !== sha1_file("public_html/" . self::LOCAL_KML_FILENAME)) { | |
if(function_exists("curl_version")) { | |
$file = $this->getRemoteFileCurl(); | |
$handle = fopen("public_html/" . self::LOCAL_KML_FILENAME, "w"); | |
fwrite($handle, $file); | |
fclose($handle); | |
} else { | |
$this->getRemoteFileCopy(); | |
} | |
} | |
} | |
/** | |
* Sends email upon failure | |
* @access private | |
*/ | |
private function sendErrorEmail() { | |
$message = "Errors:\r\n"; | |
for($i = 0; $i < count($this->errors); $i++) { | |
$message .= "#" . $i . " " . $this->errors[$i] . "\r\n"; | |
} | |
if(mail($this->to, $this->subject, $message, $this->headers)) { | |
$this->log->log("Success sending error report to " . $this->to); | |
} else { | |
$this->log->log($this->errors[] = "Error sending failure email. Issues are described below"); | |
} | |
} | |
/** | |
* Gets remote file and returns it's contents using cURL | |
* @access private | |
* @return string | |
*/ | |
private function getRemoteFileCurl() { | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, self::REMOTE_URL . self::REMOTE_FILE); | |
curl_setopt($ch, CURLOPT_VERBOSE, 0); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_AUTOREFERER, false); | |
curl_setopt($ch, CURLOPT_REFERER, self::REMOTE_URL); | |
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); | |
curl_setopt($ch, CURLOPT_HEADER, 0); | |
$result = curl_exec($ch); | |
curl_close($ch); | |
return $result; | |
} | |
/** | |
* Gets remote file and returns it's contents using copy | |
* @throws Exception | |
*/ | |
private function getRemoteFileCopy() { | |
if(!copy(self::REMOTE_URL . self::REMOTE_FILE, "./" . self::LOCAL_KML_FILENAME)) { | |
$msg = "Failed to copy " . self::REMOTE_URL . self::REMOTE_FILE . " into " . self::LOCAL_KML_FILENAME; | |
$this->log->log($msg); | |
$this->sendErrorEmail(); | |
throw new Exception($msg, 1); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment