XML PHP Class Example
the following example is for curl & XML exchanges with the cloud:
<?php
if (!function_exists("xortify_elekey2numeric")) {
function xortify_elekey2numeric($array, $name) {
$ret = array();
foreach($array as $key => $value) {
if (is_array($value)) {
$key = str_replace($name.'_', '', $key);
if (is_numeric($key))
$key = (integer)$key;
$ret[$key] = xortify_elekey2numeric($value, $name);
} else {
$key = str_replace($name.'_', '', $key);
if (is_numeric($key))
$key = (integer)$key;
$ret[$key] = $value;
}
}
return $ret;
}
}
if (!function_exists("xortify_xml2array")) {
function xortify_xml2array($contents, $get_attributes=1, $priority = 'tag') {
if(!$contents) return array();
if(!function_exists('xml_parser_create')) {
return array();
}
//Get the XML parser of PHP - PHP must have this module for the parser to work
$parser = xml_parser_create('');
xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8"); # http://minutillo.com/steve/weblog/200 ... adness-rage-and-data-loss
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($parser, trim($contents), $xml_values);
xml_parser_free($parser);
if(!$xml_values) return;//Hmm...
//Initializations
$xml_array = array();
$parents = array();
$opened_tags = array();
$arr = array();
$current = &$xml_array; //Refference
//Go through the tags.
$repeated_tag_index = array();//Multiple tags with same name will be turned into an array
foreach($xml_values as $data) {
unset($attributes,$value);//Remove existing values, or there will be trouble
//This command will extract these variables into the foreach scope
// tag(string), type(string), level(int), attributes(array).
extract($data);//We could use the array by itself, but this cooler.
$result = array();
$attributes_data = array();
if(isset($value)) {
if($priority == 'tag') $result = $value;
else $result['value'] = $value; //Put the value in a assoc array if we are in the 'Attribute' mode
}
//Set the attributes too.
if(isset($attributes) and $get_attributes) {
foreach($attributes as $attr => $val) {
if($priority == 'tag') $attributes_data[$attr] = $val;
else $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
}
}
//See tag status and do the needed.
if($type == "open") {//The starting of the tag '<tag>'
$parent[$level-1] = &$current;
if(!is_array($current) or (!in_array($tag, array_keys($current)))) { //Insert New tag
$current[$tag] = $result;
if($attributes_data) $current[$tag. '_attr'] = $attributes_data;
$repeated_tag_index[$tag.'_'.$level] = 1;
$current = &$current[$tag];
} else { //There was another element with the same tag name
if(isset($current[$tag][0])) {//If there is a 0th element it is already an array
$current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
$repeated_tag_index[$tag.'_'.$level]++;
} else {//This section will make the value an array if multiple tags with the same name appear together
$current[$tag] = array($current[$tag],$result);//This will combine the existing item and the new item together to make an array
$repeated_tag_index[$tag.'_'.$level] = 2;
if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well
$current[$tag]['0_attr'] = $current[$tag.'_attr'];
unset($current[$tag.'_attr']);
}
}
$last_item_index = $repeated_tag_index[$tag.'_'.$level]-1;
$current = &$current[$tag][$last_item_index];
}
} elseif($type == "complete") { //Tags that ends in 1 line '<tag />'
//See if the key is already taken.
if(!isset($current[$tag])) { //New Key
$current[$tag] = $result;
$repeated_tag_index[$tag.'_'.$level] = 1;
if($priority == 'tag' and $attributes_data) $current[$tag. '_attr'] = $attributes_data;
} else { //If taken, put all things inside a list(array)
if(isset($current[$tag][0]) and is_array($current[$tag])) {//If it is already an array...
// ...push the new element into that array.
$current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
if($priority == 'tag' and $get_attributes and $attributes_data) {
$current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
}
$repeated_tag_index[$tag.'_'.$level]++;
} else { //If it is not an array...
$current[$tag] = array($current[$tag],$result); //...Make it an array using using the existing value and the new value
$repeated_tag_index[$tag.'_'.$level] = 1;
if($priority == 'tag' and $get_attributes) {
if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well
$current[$tag]['0_attr'] = $current[$tag.'_attr'];
unset($current[$tag.'_attr']);
}
if($attributes_data) {
$current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
}
}
$repeated_tag_index[$tag.'_'.$level]++; //0 and 1 index is already taken
}
}
} elseif($type == 'close') { //End of tag '</tag>'
$current = &$parent[$level-1];
}
}
return($xml_array);
}
}
if (!function_exists("xortify_toXml")) {
function xortify_toXml($array, $name, $standalone=false, $beginning=true, $nested) {
if ($beginning) {
if ($standalone)
header("content-type:text/xml;charset="._CHARSET);
$output .= '<'.'?'.'xml version="1.0" encoding="'._CHARSET.'"'.'?'.'>' . "\\n";
$output .= '<' . $name . '>' . "\\n";
$nested = 0;
}
if (is_array($array)) {
foreach ($array as $key=>$value) {
$nested++;
if (is_array($value)) {
$output .= str_repeat("\\t", (1 * $nested)) . '<' . (is_string($key) ? $key : $name.'_' . $key) . '>' . "\\n";
$nested++;
$output .= xortify_toXml($value, $name, false, false, $nested);
$nested--;
$output .= str_repeat("\\t", (1 * $nested)) . '</' . (is_string($key) ? $key : $name.'_' . $key) . '>' . "\\n";
} else {
if (strlen($value)>0) {
$nested++;
$output .= str_repeat("\\t", (1 * $nested)) . ' <' . (is_string($key) ? $key : $name.'_' . $key) . '>' . trim($value) . '</' . (is_string($key) ? $key : $name.'_' . $key) . '>' . "\\n";
$nested--;
}
}
$nested--;
}
} elseif (strlen($array)>0) {
$nested++;
$output .= str_repeat("\\t", (1 * $nested)) . trim($array) ."\\n";
$nested--;
}
if ($beginning) {
$output .= '</' . $name . '>';
return $output;
} else {
return $output;
}
}
}
if (!function_exists("getIPData")) {
function getIPData($ip=false){
$ret = array();
if (is_object($GLOBALS['xoopsUser'])) {
$ret['uid'] = $GLOBALS['xoopsUser']->getVar('uid');
$ret['uname'] = $GLOBALS['xoopsUser']->getVar('uname');
$ret['email'] = $GLOBALS['xoopsUser']->getVar('email');
} else {
$ret['uid'] = 0;
$ret['uname'] = $_REQUEST['uname'];
$ret['email'] = $_REQUEST['email'];
}
if (!$ip) {
if ($_SERVER["HTTP_X_FORWARDED_FOR"] != ""){
$ip = (string)$_SERVER["HTTP_X_FORWARDED_FOR"];
$ret['is_proxied'] = true;
$proxy_ip = $_SERVER["REMOTE_ADDR"];
$ret['network-addy'] = @gethostbyaddr($ip);
$ret['long'] = @ip2long($ip);
if (is_ipv6($ip)) {
$ret['ip6'] = $ip;
$ret['proxy-ip6'] = $proxy_ip;
} else {
$ret['ip4'] = $ip;
$ret['proxy-ip4'] = $proxy_ip;
}
}else{
$ret['is_proxied'] = false;
$ip = (string)$_SERVER["REMOTE_ADDR"];
$ret['network-addy'] = @gethostbyaddr($ip);
$ret['long'] = @ip2long($ip);
if (is_ipv6($ip)) {
$ret['ip6'] = $ip;
} else {
$ret['ip4'] = $ip;
}
}
} else {
$ret['is_proxied'] = false;
$ret['network-addy'] = @gethostbyaddr($ip);
$ret['long'] = @ip2long($ip);
if (is_ipv6($ip)) {
$ret['ip6'] = $ip;
} else {
$ret['ip4'] = $ip;
}
}
$ret['made'] = time();
return $ret;
}
}
if (!function_exists("is_ipv6")) {
function is_ipv6($ip = "")
{
if ($ip == "")
return false;
if (substr_count($ip,":") > 0){
return true;
} else {
return false;
}
}
}
if (!function_exists("xortify_obj2array")) {
function xortify_obj2array($objects) {
$ret = array();
foreach($objects as $key => $value) {
if (is_a($value, 'stdClass')) {
$ret[$key] = xortify_obj2array((array)$value);
} elseif (is_array($value)) {
$ret[$key] = xortify_obj2array($value);
} else {
$ret[$key] = $value;
}
}
return $ret;
}
}
foreach (get_loaded_extensions() as $ext){
if ($ext=="curl")
$nativecurl=true;
}
if ($nativecurl==true) {
define('XOOPS_CURL_LIB', 'PHPCURL');
define('XORTIFY_USER_AGENT', 'Mozilla/5.0 (X11; U; Linux i686; pl-PL; rv:1.9.0.2) XOOPS/20100101 XoopsAuth/1.xx (php)');
}
define('XORTIFY_CURL_API', 'http://xortify.chronolabs.coop/xml/);
class CURLXortifyExchange {
var $curl_client;
var $curl_xoops_username = 'user';
var $curl_xoops_password = md5('pass');
var $refresh = 600;
function __construct()
{
$this->CURLXortifyExchange ();
}
function CURLXortifyExchange () {
if (!$ch = curl_init(XORTIFY_CURL_API)) {
trigger_error('Could not intialise CURL file: '.$url);
return false;
}
$cookies = XOOPS_VAR_PATH.'/cache/xoops_cache/authcurl_'.md5(XORTIFY_CURL_API).'.cookie';
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookies);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, XORTIFY_USER_AGENT);
$this->curl_client =& $ch;
}
function sendBan($comment, $category_id = 2, $ip=false) {
@$this->CURLXortifyExchange();
$ipData = getIPData($ip);
switch (XOOPS_CURL_LIB){
case "PHPCURL":
curl_setopt($this->curl_client, CURLOPT_POSTFIELDS, 'ban='.xortify_toXml(array( "username" => $this->curl_xoops_username,
"password" => $this->curl_xoops_password,
"bans" => array( 0 => array_merge(
$ipData,
array('category_id' => $category_id)
)
),
"comments" => array( 0 => array( 'uname' => $this->curl_xoops_username,
"comment" => $comment
)
) ), 'ban'));
$data = curl_exec($this->curl_client);
curl_close($this->curl_client);
$result = xortify_elekey2numeric(xortify_xml2array($data), 'ban');
break;
}
return $result['ban'];
}
function checkBans($ipdata, $category_id) {
@$this->CURLXortifyExchange();
switch (XOOPS_CURL_LIB){
case "PHPCURL":
curl_setopt($this->curl_client, CURLOPT_POSTFIELDS, 'banned='.xortify_toXml(array( "username" => $this->curl_xoops_username,
"password" => $this->curl_xoops_password,
"ipdata" => array_merge($ipdata, array('category_id' => $category_id))
), 'banned'));
$data = curl_exec($this->curl_client);
curl_close($this->curl_client);
$result = xortify_elekey2numeric(xortify_xml2array($data), 'banned');
break;
}
return $result['banned'];
}
function checkUnbans($ipdata, $category_id) {
@$this->CURLXortifyExchange();
switch (XOOPS_CURL_LIB){
case "PHPCURL":
curl_setopt($this->curl_client, CURLOPT_POSTFIELDS, 'unbanned='.xortify_toXml(array( "username" => $this->curl_xoops_username,
"password" => $this->curl_xoops_password,
"ipdata" => array_merge($ipdata, array('category_id' => $category_id))
), 'unbanned'));
$data = curl_exec($this->curl_client);
curl_close($this->curl_client);
$result = xortify_elekey2numeric(xortify_xml2array($data), 'unbanned');
break;
}
return $result['unbanned'];
}
function checkSFSBans($ipdata) {
@$this->CURLXortifyExchange();
switch (XOOPS_CURL_LIB){
case "PHPCURL":
curl_setopt($this->curl_client, CURLOPT_POSTFIELDS, 'checksfsbans='.xortify_toXml(array( "username" => $this->curl_xoops_username,
"password" => $this->curl_xoops_password,
"ipdata" => array_merge($ipdata, array('category_id' => $category_id))
), 'checksfsbans'));
$data = curl_exec($this->curl_client);
curl_close($this->curl_client);
$result = xortify_elekey2numeric(xortify_xml2array($data), 'checksfsbans');
break;
}
return $result['checksfsbans'];
}
function checkPHPBans($ipdata) {
@$this->CURLXortifyExchange();
switch (XOOPS_CURL_LIB){
case "PHPCURL":
curl_setopt($this->curl_client, CURLOPT_POSTFIELDS, 'checkphpbans='.xortify_toXml(array( "username" => $this->curl_xoops_username,
"password" => $this->curl_xoops_password,
"ipdata" => $ipdata
), 'checkphpbans'));
$data = curl_exec($this->curl_client);
curl_close($this->curl_client);
$result = xortify_elekey2numeric(xortify_xml2array($data), 'checkphpbans');
break;
}
return $result['checkphpbans'];
}
function retrieveBans() {
@$this->CURLXortifyExchange();
switch (XOOPS_CURL_LIB){
case "PHPCURL":
curl_setopt($this->curl_client, CURLOPT_POSTFIELDS, 'bans='.xortify_toXml(array("username"=> $this->curl_xoops_username, "password"=> $this->curl_xoops_password, "records"=> $this->refresh), 'bans'));
$data = curl_exec($this->curl_client);
curl_close($this->curl_client);
$result = xortify_elekey2numeric(xortify_xml2array($data), 'bans');
}
return $result['bans'];
}
function retrieveUnbans() {
@$this->CURLXortifyExchange();
switch (XOOPS_CURL_LIB){
case "PHPCURL":
curl_setopt($this->curl_client, CURLOPT_POSTFIELDS, 'unbans='.xortify_toXml(array("username"=> $this->curl_xoops_username, "password"=> $this->curl_xoops_password, "records"=> $this->refresh), 'unbans'));
$data = curl_exec($this->curl_client);
curl_close($this->curl_client);
$result = xortify_elekey2numeric(xortify_xml2array($data), 'unbans');
}
return $result['unbans'];
}
function sendSpider($spider) {
@$this->CURLXortifyExchange();
switch (XOOPS_CURL_LIB){
case "PHPCURL":
curl_setopt($this->curl_client, CURLOPT_POSTFIELDS, 'spider='.xortify_toXml(array( "username" => $this->curl_xoops_username, "password" => $this->curl_xoops_password, "spider" => $spider ), 'spider'));
$data = curl_exec($this->curl_client);
curl_close($this->curl_client);
$result = xortify_elekey2numeric(xortify_xml2array($data), 'spider');
break;
}
return $result['spider'];
}
function sendStatistic($statistic) {
@$this->CURLXortifyExchange();
switch (XOOPS_CURL_LIB){
case "PHPCURL":
curl_setopt($this->curl_client, CURLOPT_POSTFIELDS, 'spiderstat='.xortify_toXml(array( "username" => $this->curl_xoops_username, "password" => $this->curl_xoops_password, "statistic" => $statistic ), 'spiderstat'));
$data = curl_exec($this->curl_client);
curl_close($this->curl_client);
$result = xortify_elekey2numeric(xortify_xml2array($data), 'spiderstat');
break;
}
return $result['spiderstat'];
}
function getSpiders() {
@$this->CURLXortifyExchange();
switch (XOOPS_CURL_LIB){
case "PHPCURL":
curl_setopt($this->curl_client, CURLOPT_POSTFIELDS, 'spiders='.xortify_toXml(array( "username" => $this->curl_xoops_username, "password" => $this->curl_xoops_password ), 'spiders'));
$data = curl_exec($this->curl_client);
curl_close($this->curl_client);
$result = xortify_elekey2numeric(xortify_xml2array($data), 'spiders');
break;
}
return $result['spiders']['robots'];
}
function getSEOLinks() {
@$this->CURLXortifyExchange();
switch (XOOPS_CURL_LIB){
case "PHPCURL":
curl_setopt($this->curl_client, CURLOPT_POSTFIELDS, 'seolinks='.xortify_toXml(array( "username" => $this->curl_xoops_username, "password" => $this->curl_xoops_password ), 'seolinks'));
$data = curl_exec($this->curl_client);
curl_close($this->curl_client);
$result = xortify_elekey2numeric(xortify_xml2array($data), 'seolinks');
break;
}
return $result['seolinks']['seolinks];
}
}
?>