Lokorin Site Admin
Joined: 03 Apr 2006 Posts: 697
|
Posted: Tue Jun 20, 2006 6:35 pm Post subject: The official preprocessor package in development |
|
|
This is the DKPLP preprocessor (written in PHP) that is currently used for testing during development, it will hopefully include a few more preprocessors than just the Ventrilo one before the release. It is posted here as an example of a preprocessor for a news entry on the subject.
| Code: |
<?php
/*
* DKP Log Parser
* Copyright 2005-2006
* Licensed under the GNU GPL. See COPYING for full terms.
*/
/**
* A DKPLP preprocessor that satisfies the preprocessor XML-RPC interface,
* @author Andreas Launila
* @version $Revision: 1.1.2.1 $
*/
/**
* For access to a stand-alone XML-RPC implementation.
*/
include_once('xmlrpc.php');
/**
* For access to a stand-alone XML-RPC implementation.
*/
include_once('xmlrpcs.php');
/**
* The specification's number string, altering this may cause unexpected
* behaviour as the client might think the preprocessor supports things that
* it actually doesn't. So don't alter this unless you know what you're doing.
* The specification is located at
* http://wiki.lokorin.com/wiki/DKP_Log_Parser_-_Preprocessor_interface
* @var string
* @access public
*/
define('PREPROCESSOR_SPECIFICATION_NUMBER', 'P1');
/**#@+
* Unique identifiers for preprocessors.
* @access private
* @var string
*/
define('PPUID_VENTRILO', 'ventrilo');
/**#@-*/
/**
* Gets a list of all available preprocessors.
* @return array<array<string>> An array in with two elements, both arrays of
* strings. Each element in the first of those two arrays contains unique
* identifiers used by the server to identify each preprocessor. The
* second array contains the same number of elements as the first array
* and the element at the i:th position in the second array is a
* describing name for the unique identifier at position i in the first
* array.
* @access public
*/
function getAvailablePreprocessors($params) {
global $xmlrpcerruser;
if ($params->getNumParams() != 0) {
return new xmlrpcresp(0, $xmlrpcerruser+3, "Incorrect number of parameters.");
}
//The following should probably to in some more elegant way when the number
//of preprocessor increases.
$uidArray = array(
new xmlrpcval(PPUID_VENTRILO, "string")
);
$nameArray = array(
new xmlrpcval('Ventrilo', "string")
);
$result = array(
new xmlrpcval($uidArray, "array"),
new xmlrpcval($nameArray, "array")
);
return new xmlrpcresp(new xmlrpcval($result, "array"));
}
/**
* Retrieves the specification number of the latest specification that the
* preprocessor's implements.
* @return A string with format Pn where n is a positive integer. The string
* should reflect the preprocessor specification number of the implemented
* specification.
* @access public
*/
function getSpecificationNumber($params) {
return new xmlrpcresp(new xmlrpcval(PREPROCESSOR_SPECIFICATION_NUMBER, "string"));
}
/**
* Executes the specified preprocessor on the specified text.
* @param string first The unique identifier of the preprocessor that should
* be used (as given by getAvailablePreprocessors).
* @param string second The text that should be preprocessed.
* @return string The text after the preprocessor has been applied.
* @access public
*/
function preprocess($params) {
global $xmlrpcerruser;
if ($params->getNumParams() != 2) {
return new xmlrpcresp(0, $xmlrpcerruser+3, "Incorrect number of parameters.");
}
$uid = toScalar($params->getParam(0));
$text = toScalar($params->getParam(1));
if($uid == PPUID_VENTRILO) {
//Ventrilo log.
$output = ventriloPreprocess($text);
} else {
//No unique identifier matched.
return new xmlrpcresp(0, $xmlrpcerruser+3, "Unrecognized preprocessor requested.");
}
return new xmlrpcresp(new xmlrpcval($output, "string"));
}
/**
* Preprocesses the specified text as if it is a ventrilo log.
* @param string $text The text that should be preprocessed.
* @return string The preprocessed text.
* @access private
*/
function ventriloPreprocess($text) {
$output = '';
$lines = array_diff(explode("\n", stripslashes($text)), array("", "\r"));
$logFound = false;
$logStarted = false;
$matches = array();
foreach($lines as $line) {
if($logFound) {
if(preg_match("/\*?\s+\d+\s+\d+\s+\d+\s+\w+.+/", $line)) {
//We found a status log, convert it.
$output .= $matches[1].' '.$matches[2].' '.$line."\n";
$logStarted = true;
} else if($logStarted) {
//The log has ended.
$logStarted = $logFound = false;
}
} else if(preg_match(
"/(\d{8})\s(\d{2}:\d{2}:\d{2}).+issued 'status'/", $line, $matches)) {
//We have found a status log.
$logFound = true;
}
}
return $output;
}
/**
* Converts an xmlrpcval array into a php array.
* @param xmlrpcval $xmlrpcArray The xmlrpcval array that should be converted,
* @return array A php array with the scalar values of the xmlrpcval array.
* @access private
*/
function toArray($xmlrpcArray) {
if(!is_object($xmlrpcArray) || !$xmlrpcArray->kindOf() == "array") {
return array();
}
$result = array();
for($i=0; $i < $xmlrpcArray->arraysize(); $i++) {
$result[] = $xmlrpcArray->arraymem($i);
}
return $result;
}
/**
* Converts xmlrpc values to scalar values. It also convert arrays
* recursivly.
* @param mixed $xmlrpcValue The xmlrpc values that should be converted
* to scalar.
* @return mixed The scalar form of the input.
* @access private
*/
function toScalar($xmlrpcValue) {
if(!is_object($xmlrpcValue)) {
return $xmlrpcValue;
}
if($xmlrpcValue->kindOf() == "array") {
$result = array();
$values = toArray($xmlrpcValue);
foreach($values as $value) {
if(is_object($value)) {
$result[] = toScalar($value);
} else {
$result[] = $value;
}
}
return $result;
} else {
return $xmlrpcValue->scalarval();
}
}
$server = new xmlrpc_server(array(
"getAvailablePreprocessors" => array("function" => "getAvailablePreprocessors"),
"preprocess" => array("function" => "preprocess"),
"getSpecificationNumber" => array("function" => "getSpecificationNumber")
));
?> |
|
|