htdocs/extern.php
changeset 36 a7d884914a74
equal deleted inserted replaced
33:c3179049f670 36:a7d884914a74
       
     1 <?php
       
     2 
       
     3 require('../stats-fe.php');
       
     4 require('../timezone.php');
       
     5 
       
     6 $channel_list = stats_channel_list();
       
     7 $first_channel = $channel_list[0];
       
     8 $channel = ( isset($_REQUEST['channel']) && in_array($_REQUEST['channel'], $channel_list) ) ? $_REQUEST['channel'] : $first_channel;
       
     9 
       
    10 $formats = array('json', 'xml');
       
    11 $format = ( isset($_GET['format']) ) ? $_GET['format'] : $formats[0];
       
    12 if ( !in_array($format, $formats) )
       
    13 {
       
    14   $format = $formats[0];
       
    15   $formatclass = "format_$format";
       
    16   $formatter = new $formatclass();
       
    17   $formatter->send_headers();
       
    18   die_in_output_format('Invalid output format specified.');
       
    19 }
       
    20 
       
    21 $formatclass = "format_$format";
       
    22 $formatter = new $formatclass();
       
    23 $formatter->send_headers();
       
    24 
       
    25 if ( !isset($_GET['action']) )
       
    26   die_in_output_format('Please specify action on GET.');
       
    27 
       
    28 switch($_GET['action'])
       
    29 {
       
    30   case 'get_activity':
       
    31     $minutes = isset($_GET['minutes']) ? intval($_GET['minutes']) : 10;
       
    32     if ( $minutes < 1 )
       
    33       die_in_output_format('minutes < 1');
       
    34     $datum = stats_activity_percent($channel, $minutes);
       
    35     $count = stats_message_count($channel, $minutes);
       
    36     foreach ( $datum as &$pct )
       
    37     {
       
    38       $pct = $pct * $count;
       
    39     }
       
    40     $output = array(
       
    41         'result' => 'success',
       
    42         'minutes' => $minutes,
       
    43         'message_count' => $count,
       
    44         'active_users' => $datum
       
    45       );
       
    46     $result = $formatter->encode($output);
       
    47     if ( $format == 'xml' )
       
    48     {
       
    49       $activeusers = '<activeusers>';
       
    50       foreach ( $datum as $nick => $count )
       
    51       {
       
    52         $activeusers .= '<user nick="' . htmlspecialchars($nick) . '" count="' . $count . '" />';
       
    53       }
       
    54       $activeusers .= '</activeusers>';
       
    55       $result = preg_replace('#<activeusers>(.*?)</activeusers>#', $activeusers, $result);
       
    56     }
       
    57     echo $result;
       
    58     break;
       
    59 }
       
    60 
       
    61 /** FUNCTIONS **/
       
    62 
       
    63 function die_in_output_format($message)
       
    64 {
       
    65   global $formatter;
       
    66   echo $formatter->encode(array(
       
    67       'result' => 'error',
       
    68       'error' => $message
       
    69     ));
       
    70   exit;
       
    71 }
       
    72 
       
    73 /** FORMATS **/
       
    74 
       
    75 class format_json
       
    76 {
       
    77   public function encode($data)
       
    78   {
       
    79     require_once('../libjson.php');
       
    80     return eb_json_encode($data);
       
    81   }
       
    82   public function send_headers()
       
    83   {
       
    84     header('Content-type: text/javascript');
       
    85   }
       
    86 }
       
    87 
       
    88 /**
       
    89  * From <http://snipplr.com/view/3491/convert-php-array-to-xml-or-simple-xml-object-if-you-wish/>.
       
    90  */
       
    91 
       
    92 class format_xml
       
    93 {
       
    94 	/**
       
    95 	 * The main function for converting to an XML document.
       
    96 	 * Pass in a multi dimensional array and this recrusively loops through and builds up an XML document.
       
    97 	 *
       
    98 	 * @param array $data
       
    99 	 * @param string $rootNodeName - what you want the root node to be - defaultsto data.
       
   100 	 * @param SimpleXMLElement $xml - should only be used recursively
       
   101 	 * @return string XML
       
   102 	 */
       
   103 	public static function toXml($data, $rootNodeName = 'response', $xml = null)
       
   104 	{
       
   105 		// turn off compatibility mode as simple xml throws a wobbly if you don't.
       
   106 		if (ini_get('zend.ze1_compatibility_mode') == 1)
       
   107 		{
       
   108 			ini_set ('zend.ze1_compatibility_mode', 0);
       
   109 		}
       
   110 		
       
   111 		if ($xml == null)
       
   112 		{
       
   113 			$xml = simplexml_load_string("<?xml version='1.0' encoding='utf-8'?><$rootNodeName />");
       
   114 		}
       
   115 		
       
   116 		// loop through the data passed in.
       
   117 		foreach($data as $key => $value)
       
   118 		{
       
   119 			// no numeric keys in our xml please!
       
   120 			if (is_numeric($key))
       
   121 			{
       
   122 				// make string key...
       
   123 				$key = "unknownNode_". (string) $key;
       
   124 			}
       
   125 			
       
   126 			// replace anything not alpha numeric
       
   127 			$key = preg_replace('/[^a-z0-9]/i', '', $key);
       
   128 			
       
   129 			// if there is another array found recrusively call this function
       
   130 			if (is_array($value))
       
   131 			{
       
   132 				$node = $xml->addChild($key);
       
   133 				// recrusive call.
       
   134 				format_xml::toXml($value, $rootNodeName, $node);
       
   135 			}
       
   136 			else 
       
   137 			{
       
   138 				// add single node.
       
   139         $value = htmlentities($value);
       
   140 				$xml->addChild($key,$value);
       
   141 			}
       
   142 			
       
   143 		}
       
   144 		// pass back as string. or simple xml object if you want!
       
   145 		return $xml->asXML();
       
   146 	}
       
   147   
       
   148   public function encode($data)
       
   149   {
       
   150     return format_xml::toXml($data);
       
   151   }
       
   152   
       
   153   public function send_headers()
       
   154   {
       
   155     header('Content-type: text/xml');
       
   156   }
       
   157 }