File: 0.00.1a/core/XML.php (View as HTML)

  1: <?php
  2: /* -------------------------------------------------------------
  3: This file is part of FreeDESK
  4: 
  5: FreeDESK is (C) Copyright 2012 David Cutting
  6: 
  7: FreeDESK is free software: you can redistribute it and/or modify
  8: it under the terms of the GNU General Public License as published by
  9: the Free Software Foundation, either version 3 of the License, or
 10: (at your option) any later version.
 11: 
 12: FreeDESK is distributed in the hope that it will be useful,
 13: but WITHOUT ANY WARRANTY; without even the implied warranty of
 14: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15: GNU General Public License for more details.
 16: 
 17: You should have received a copy of the GNU General Public License
 18: along with FreeDESK.  If not, see www.gnu.org/licenses
 19: 
 20: For more information see www.purplepixie.org/freedesk/
 21: -------------------------------------------------------------- */
 22: 
 23: /**
 24:  * Main xmlCreate class object
 25: */
 26: class xmlCreate
 27: {
 28: 
 29: /**
 30:  * Contains XML Output
 31: */
 32: var $xml = "";
 33: 
 34: /**
 35:  * Contains open tags
 36:  * Not yet implemented 
 37: */
 38: var $open_tags = array();
 39: 
 40: /**
 41:  * Contains Depth of Elements
 42: */
 43: var $depth = 0;
 44: 
 45: /** 
 46:  * Control the depth (variance)
 47:  * @param integer $varience What to alter the depth by
 48: */
 49: function setDepth($varience)
 50: {
 51: 	$this->depth += $varience;
 52: }
 53: 	
 54: /** 
 55:  * Pad to depth
 56: */
 57: function pad()
 58: {
 59: 	if ($this->depth<1) return "";
 60: 	return str_pad("",$this->depth," ");
 61: }
 62: 
 63: /**
 64:  * Start building an open-ended XML element
 65:  * @param string $element Name of the element
 66:  * @param array $vars Optional array of key values to use (or int 0 to skip)
 67:  * @param boolean $single Optional indicator of single element if true
 68:  * @param boolean $newline Optional insert newline at the end
 69: */
 70: function startElement($element,$vars=0,$single=false,$newline=true)
 71: {
 72: 	$this->xml.=$this->pad();
 73: 	$this->xml.="<".$element;
 74: 	if ( ($vars!=0) && (is_array($vars)) )
 75: 	{
 76: 		foreach($vars as $key => $val)
 77: 		{
 78: 			$this->xml.=" ".$key."=\"".$val."\"";
 79: 		}
 80: 	}
 81: 	if ($single) $this->xml.=" /";
 82: 	$this->xml.=">";
 83: 	if ($newline) $this->xml.="\n";
 84: 	if (!$single) $this->setDepth(1);
 85: }
 86: 	
 87: 	
 88: /**
 89:  * End an open-ended XML element
 90:  * @param string $element Name of the element
 91:  * @param boolean $pad Optional use padding (default true)
 92: */
 93: function endElement($element,$pad=true)
 94: {
 95: 	$this->setDepth(-1);
 96: 	if ($pad) $this->xml.=$this->pad();
 97: 	$this->xml.="</".$element.">\n";
 98: }
 99: 
100: /**
101:  * Insert data (textual/char data)
102:  * @param string $data Actual data to insert
103:  * @param boolean $cdata Wrap with CDATA (optional, default NO)
104:  * @param boolean $htmlcode Use HTML special characters (optional, default NO)
105:  * @param boolean $newline Insert newline after data (optional, default YES)
106: */
107: function charData($data, $cdata=false, $htmlcode=false, $newline=true)
108: {
109: 	$dataline="";
110: 	if ($cdata) $dataline.="<![CDATA[";
111: 	if ($htmlcode) $data=htmlspecialchars($data);
112: 	$dataline.=$data;
113: 	if ($cdata) $dataline.="]]>";
114: 	if ($newline) $dataline.="\n";
115: 	$this->xml.=$dataline;
116: }
117: 	
118: /**
119:  * Single element wrapper for (@link startElement)
120:  * @param string $element Element Name
121:  * @param array $vars Optional variable array
122: */
123: function singleElement($element,$vars=0)
124: {
125: 	$this->startElement($element,$vars,true);
126: }
127: 	
128: /**
129:  * Single char-element wrapper
130:  * @param string $element Element Name
131:  * @param string $data Element Data Content
132:  * @param array $vars Optional array element variables (int 0 to skip)
133:  * @param boolean $htmlchars Optional convert data to html special chars (default false)
134:  * @param boolean $cdata Optional enclose char data in CDATA block (default false)
135: */
136: function charElement($element,$data,$vars=0,$htmlchars=false,$cdata=false)
137: {
138: 	$this->startElement($element,$vars,false,false);
139: 	$this->charData($data,$cdata,$htmlchars,false);
140: 	$this->endElement($element,false);
141: }
142: 	
143: /**
144:  * Returns XML Buffer
145:  * @param boolean $header Show an xml header (optional, default NO)
146:  * @param boolean $html Convert to HTML-friendly output (optional, default NO)
147:  * @param string $head_version XML Header version if shown (optional, default 1.0)
148:  * @param string $head_encode XML encoding in header if shown (optional, default utf-8)
149:  * @return string XML Output
150: */
151: function getXML($header=false,$html=false,$head_version="1.0",$head_encode="utf-8")
152: {
153: 	$out="";
154: 	if ($header)
155: 		$out="<?xml version=\"".$head_version."\" encoding=\"".$head_encode."\"?>\n";
156: 	$out.=$this->xml;
157: 	if ($html) $out=nl2br(htmlspecialchars($out));
158: 	return $out;
159: }
160: 
161: /**
162:  * Returns XML Header
163:  * @param string $head_version XML Header version (optional, default 1.0)
164:  * @param string $head_encode XML encoding (optional, default utf-8)
165:  * @return string xml header
166: **/
167: static function getHeader($head_version="1.0",$head_encode="utf-8")
168: {
169: 	return "<?xml version=\"".$head_version."\" encoding=\"".$head_encode."\"?>\n";
170: }
171: 
172: /**
173:  * Convert an array to XML
174:  * @param array &$data Data array
175:  * @param bool $cdata CData all values (optional, default true)
176: **/
177: function xmlArray(&$data, $cdata=true)
178: {
179: 	foreach($data as $key => $item)
180: 	{
181: 		if(is_array($item))
182: 		{
183: 			$this->startElement($key);
184: 			$this->xmlArray($item);
185: 			$this->endElement($key);
186: 		}
187: 		else
188: 		{
189: 			$this->charElement($key, $item, 0, false, $cdata);
190: 		}
191: 	}
192: }
193: 
194: 
195: /**
196:  * Writes XML Buffer to Screen
197:  * @param boolean $header Show an xml header (optional, default NO)
198:  * @param boolean $html Convert to HTML-friendly output (optional, default NO)
199:  * @param string $head_version XML Header version if shown (optional, default 1.0)
200:  * @param string $head_encode XML encoding in header if shown (optional, default utf-8)
201: */
202: function echoXML($header=false,$html=false,$head_version="1.0",$head_encode="utf-8")
203: {
204: 	$out="";
205: 	if ($header) $out.="<?xml version=\"".$head_version."\" encoding=\"".$head_encode."\"?>\n";
206: 	$out.=$this->xml;
207: 	if ($html) $out=nl2br(htmlspecialchars($out));
208: 	echo $out;
209: }
210: 
211: /**
212:  * Register ourselves with FreeDESK
213:  * @param mixed $desk FreeDESK instance
214: **/
215: static function Exec(&$desk)
216: {
217: 	$desk->PluginManager->Register( new Plugin(
218: 		"XML Creator", "0.01", "Core", "XML" ));
219: }
220: 
221: }
222: ?>
223: