# HG changeset patch # User Dan # Date 1230635788 18000 # Node ID a7d884914a74df7acf62e349cb8145b9024430b9 # Parent c3179049f67072532ef7b16b2de146dc648c9c13 Added extern script to allow exporting some stats through a JSON or XML API. WiP. diff -r c3179049f670 -r a7d884914a74 htdocs/extern.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/htdocs/extern.php Tue Dec 30 06:16:28 2008 -0500 @@ -0,0 +1,157 @@ +send_headers(); + die_in_output_format('Invalid output format specified.'); +} + +$formatclass = "format_$format"; +$formatter = new $formatclass(); +$formatter->send_headers(); + +if ( !isset($_GET['action']) ) + die_in_output_format('Please specify action on GET.'); + +switch($_GET['action']) +{ + case 'get_activity': + $minutes = isset($_GET['minutes']) ? intval($_GET['minutes']) : 10; + if ( $minutes < 1 ) + die_in_output_format('minutes < 1'); + $datum = stats_activity_percent($channel, $minutes); + $count = stats_message_count($channel, $minutes); + foreach ( $datum as &$pct ) + { + $pct = $pct * $count; + } + $output = array( + 'result' => 'success', + 'minutes' => $minutes, + 'message_count' => $count, + 'active_users' => $datum + ); + $result = $formatter->encode($output); + if ( $format == 'xml' ) + { + $activeusers = ''; + foreach ( $datum as $nick => $count ) + { + $activeusers .= ''; + } + $activeusers .= ''; + $result = preg_replace('#(.*?)#', $activeusers, $result); + } + echo $result; + break; +} + +/** FUNCTIONS **/ + +function die_in_output_format($message) +{ + global $formatter; + echo $formatter->encode(array( + 'result' => 'error', + 'error' => $message + )); + exit; +} + +/** FORMATS **/ + +class format_json +{ + public function encode($data) + { + require_once('../libjson.php'); + return eb_json_encode($data); + } + public function send_headers() + { + header('Content-type: text/javascript'); + } +} + +/** + * From . + */ + +class format_xml +{ + /** + * The main function for converting to an XML document. + * Pass in a multi dimensional array and this recrusively loops through and builds up an XML document. + * + * @param array $data + * @param string $rootNodeName - what you want the root node to be - defaultsto data. + * @param SimpleXMLElement $xml - should only be used recursively + * @return string XML + */ + public static function toXml($data, $rootNodeName = 'response', $xml = null) + { + // turn off compatibility mode as simple xml throws a wobbly if you don't. + if (ini_get('zend.ze1_compatibility_mode') == 1) + { + ini_set ('zend.ze1_compatibility_mode', 0); + } + + if ($xml == null) + { + $xml = simplexml_load_string("<$rootNodeName />"); + } + + // loop through the data passed in. + foreach($data as $key => $value) + { + // no numeric keys in our xml please! + if (is_numeric($key)) + { + // make string key... + $key = "unknownNode_". (string) $key; + } + + // replace anything not alpha numeric + $key = preg_replace('/[^a-z0-9]/i', '', $key); + + // if there is another array found recrusively call this function + if (is_array($value)) + { + $node = $xml->addChild($key); + // recrusive call. + format_xml::toXml($value, $rootNodeName, $node); + } + else + { + // add single node. + $value = htmlentities($value); + $xml->addChild($key,$value); + } + + } + // pass back as string. or simple xml object if you want! + return $xml->asXML(); + } + + public function encode($data) + { + return format_xml::toXml($data); + } + + public function send_headers() + { + header('Content-type: text/xml'); + } +} diff -r c3179049f670 -r a7d884914a74 stats-fe.php --- a/stats-fe.php Sun Dec 07 17:54:29 2008 -0500 +++ b/stats-fe.php Tue Dec 30 06:16:28 2008 -0500 @@ -59,7 +59,7 @@ { eb_mysql_query("INSERT INTO stats_count_cache(channel, time_min, time_max, message_count) VALUES('$channel', $time_min, $time_max, $count);"); } - return $count; + return intval($count); } return false; }