Skip to content

Commit

Permalink
initial work, very rough
Browse files Browse the repository at this point in the history
  • Loading branch information
rich committed Nov 12, 2008
0 parents commit 6016055
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
64 changes: 64 additions & 0 deletions Hoptoad.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
class Hoptoad
{
public static function errorHandler($code, $message)
{
if ($code == E_STRICT) return;

Hoptoad::notifyHoptoad(HOPTOAD_API_KEY, $message, null, 2);
}

public static function exceptionHandler($exception)
{
Hoptoad::notifyHoptoad(HOPTOAD_API_KEY, $exception->getMessage(), null, 2);
}

public static function notifyHoptoad($api_key, $message, $error_class=null, $offset=1)
{
$lines = array_slice(Hoptoad::tracer(), $offset);

$req =& new HTTP_Request("http://hoptoadapp.com/notices/", array("method" => "POST", "timeout" => 1));
$req->addHeader('Accept', 'text/xml, application/xml');
$req->addHeader('Content-type', 'application/x-yaml');

if (isset($_SESSION)) {
$session = array('key' => session_id(), 'data' => $_SESSION);
} else {
$session = array();
}

$body = array(
'api_key' => $api_key,
'error_class' => $error_class,
'error_message' => $message,
'backtrace' => $lines,
'request' => array("params" => $_REQUEST, "url" => "http://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}"),
'session' => $session,
'environment' => $_SERVER
);

$req->setBody(Horde_Yaml::dump(array("notice" => $body)));
$req->sendRequest();
}

public static function tracer()
{
$lines = Array();

$trace = debug_backtrace();

$indent = '';
$func = '';

foreach($trace as $val) {
if (!isset($val['file']) || !isset($val['line'])) continue;

$line = $val['file'] . ' on line ' . $val['line'];

if ($func) $line .= ' in function ' . $func;
$func = $val['function'];
$lines[] = $line;
}
return $lines;
}
}
27 changes: 27 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Introduction

This is a very rough first stab at a PHP [Hoptoad](http://hoptoadapp.com) notifier. I'm using it in production right now with success but that's no guarantee it won't take down your site, destroy your business and ransom your family.

# Known Bugs

The Hoptoad::tracer() method does not generate traces that are 100% correct.

# Requirements

The notifier uses the Horde_Yaml class. You can install this class using the commands below.

pear channel-discover pear.horde.org
pear install horde/yaml

# License

Copyright (c) 2008, Rich Cavanaugh
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
6 changes: 6 additions & 0 deletions example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
require_once('Hoptoad.php');

define("HOPTOAD_API_KEY", "YOUR_HOPTOAD_API_KEY");
set_error_handler(array("Hoptoad", "errorHandler"));
set_exception_handler(array("Hoptoad", "exceptionHandler"));

0 comments on commit 6016055

Please sign in to comment.