commit f0a7082f06e6ce2335ad5da198112f76ed6ca0f2
parent 55bfb03ed50a461cc2e3a2acb13792975fa6556d
Author: dankert <devnull@localhost>
Date: Tue, 21 Apr 2009 00:16:20 +0200
Verbesserung der API: Bugfix XML-Ausgabe, Ausgabe der Session-Id.
Diffstat:
2 files changed, 84 insertions(+), 32 deletions(-)
diff --git a/actionClasses/Action.class.php b/actionClasses/Action.class.php
@@ -289,7 +289,11 @@ class Action
$httpAccept = getenv('HTTP_ACCEPT');
$types = explode(',',$httpAccept);
- if ( sizeof($types)==1 && in_array('application/json',$types) )
+ // Weitere Variablen anreichern.
+ $this->templateVars['session'] = array('name'=>session_name(),'id'=>session_id());
+ $this->templateVars['version'] = OR_VERSION;
+
+ if ( sizeof($types)==1 && in_array('application/json',$types) || $this->getRequestVar('output')=='json' )
{
require_once( OR_SERVICECLASSES_DIR."JSON.class.".PHP_EXT );
$json = new JSON();
@@ -298,10 +302,11 @@ class Action
exit;
}
- if ( sizeof($types)==1 && in_array('application/xml',$types) )
+ if ( sizeof($types)==1 && in_array('application/xml',$types) || $this->getRequestVar('output')=='xml' )
{
require_once( OR_SERVICECLASSES_DIR."XML.class.".PHP_EXT );
$xml = new XML();
+ $xml->root = 'server'; // Name des XML-root-Elementes
header('Content-Type: application/xml');
echo $xml->encode( $this->templateVars );
exit;
diff --git a/serviceClasses/XML.class.php b/serviceClasses/XML.class.php
@@ -8,7 +8,7 @@
* echo $xml->encode( $yourBigArray );
* exit;
*
- * Author: Honoré Vasconcelos, Jan Dankert
+ * Author: Honor� Vasconcelos, Jan Dankert
*
* Original from:
* Clean XML To Array: http://www.phpclasses.org/browse/package/3598.html
@@ -47,7 +47,7 @@ class XML
* @param array $array
* @return String
*/
- var $XMLtext = '';
+ var $xmlText = '';
/**
@@ -55,7 +55,7 @@ class XML
*
* @var String
*/
- var $root = 'server';
+ var $root = 'xml';
/*
* Char to indent with.
@@ -65,7 +65,12 @@ class XML
var $indentChar = "\t";
-
+ /**
+ * Newline-Char
+ * @var String
+ */
+ var $nl = "\n";
+
/**
* Encode a array to XML.
*
@@ -75,47 +80,89 @@ class XML
function encode($array)
{
//star and end the XML document
- $this->XMLtext="<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<".$this->root.">\n";
- $this->array_transform($array);
- $this->XMLtext .="</".$this->root.">";
- return $this->XMLtext;
+ $this->xmlText = '<?xml version="1.0" encoding="utf-8"?>'.$this->nl;
+ $this->xmlText .= '<'.$this->root.'>'.$this->nl;
+ $this->array_transform($array,1);
+ $this->xmlText .='</'.$this->root.'>';
+
+ return $this->xmlText;
}
/**
* @access private
*/
- function array_transform($array){
- static $Depth = 0;
+ function array_transform($array,$depth){
foreach($array as $key => $value)
{
- if ( ! is_array($value) )
+ $attr = array();
+ if ( is_numeric($key) )
+ {
+ // Array-Einträge mit numerischen Index können nicht direkt in ein XML-Element
+ // umgewandelt werden, da nur-numerische Element-Namen nicht erlaubt sind.
+ // Daher verwenden wir dann 'entry' als Elementnamen.
+ $attr['id'] = $key;
+ $key = 'entry';
+ }
+
+ $indent = str_repeat($this->indentChar,$depth);
+
+ if ( empty($value) )
+ {
+ $this->xmlText .= $indent.$this->shortTag($key,$attr).$this->nl;
+ }
+ elseif ( is_object($value) )
{
- $Tabs = str_repeat($this->indentChar,$Depth+1);
- if ( is_numeric($key) )
- $kkey = "n$key";
- else
- $kkey = $key;
- $this->XMLtext .= "$Tabs<$kkey id=\"$key\">$value</$key>\n";
+ // Der Inhalt ist ein Array, daher rekursiv verzweigen.
+ $this->xmlText .= $indent.$this->openTag($key,$attr).$this->nl;
+ $prop = get_object_vars($value);
+ $this->array_transform($prop,$depth+1); // Rekursiver Aufruf
+ $this->xmlText .= $indent.$this->closeTag($key).$this->nl;
+ }
+ elseif ( is_array($value) )
+ {
+ // Der Inhalt ist ein Array, daher rekursiv verzweigen.
+ $this->xmlText .= $indent.$this->openTag($key,$attr).$this->nl;
+ $this->array_transform($value,$depth+1); // Rekursiver Aufruf
+ $this->xmlText .= $indent.$this->closeTag($key).$this->nl;
}
else
{
- $Depth += 1;
- $Tabs = str_repeat($this->indentChar,$Depth);
- if ( is_numeric($key) )
- $keyval = "n$key";
- else
- $keyval = $key;
- $closekey = $keyval;
- $this->XMLtext.="$Tabs<$keyval>\n";
- $this->array_transform($value);
- $this->XMLtext.="$Tabs</$closekey>\n";
- $Depth -= 1;
-
+ // Der Inhalt ist ein einfacher Inhalt (kein Array).
+ $this->xmlText .= $indent.$this->openTag($key,$attr);
+ $this->xmlText .= $value;
+ $this->xmlText .= $this->closeTag($key).$this->nl;
}
}
- return;
+ }
+
+
+ function openTag($key,$attr)
+ {
+ $tag = '<'.$key;
+ foreach( $attr as $attr_name=>$attr_value )
+ $tag .= ' '.$attr_name.'="'.$attr_value.'"';
+ $tag .= '>';
+ return $tag;
+ }
+
+
+
+ function shortTag($key,$attr)
+ {
+ $tag = '<'.$key;
+ foreach( $attr as $attr_name=>$attr_value )
+ $tag .= ' '.$attr_name.'="'.$attr_value.'"';
+ $tag .= ' />';
+ return $tag;
+ }
+
+
+
+ function closeTag($key)
+ {
+ return '</'.$key.'>';
}
}