Umfragen
xml.class.php
Go to the documentation of this file.
1 <?php
2 /*
3  * untitled.php
4  *
5  * Copyright 2013 Johannes ter Haak <jojo@jojo-42>
6  *
7  * This program 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 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program 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 this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20  * MA 02110-1301, USA.
21  *
22  *
23  */
24 
25 
26 require_once("widget.class.php");
27 // see http://php.net/manual/en/function.htmlspecialchars.php
28 // in php 5.3 ENT_XML1 is not defined!!
29 if(! defined("ENT_XML1") ){
30  define("ENT_XML1",16);
31 }
32 
33 class xml{
34 
35  private $doc;
36 
37  public function __construct(){
38  // specify version and ecoding so SimpleXMLElement::asXML() does not encode öäü
39  $this->doc = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><widget_list></widget_list>');
40  }
41 
47  public function import($xml){
48  $this->doc = new SimpleXMLElement($xml);
49  $name = $this->doc->getName(); // check if we import widget list or poll
50 
51  if( $name == "widget_list" ){
52  // make the widget list
53  $widget_list = array();
54  foreach( $this->doc->xpath("/widget_list/*") as $sxe){
55  $widget_list[] = $this->make_widget($sxe) ;
56  }
57  return $widget_list;
58 
59  }else if( $name == "poll" ){
60  // make a poll
61  }else if( $name == "poll_list"){
62  // import a poll list
63  $poll_list = array();
64  foreach( $this->doc->xpath("/poll_list/*") as $sxe){
65  $poll_list[] = $this->make_poll($sxe) ;
66  }
67  return $poll_list;
68 
69  }else{
70  //throw new Exception( "There is nothing to import" );
71  return;
72  }
73  }
74 
80  public function export($objects){
81  if( $objects instanceof poll ){
82  // import a poll
83  $this->doc = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><poll_list></poll_list>');
84  $this->insert_poll($objects,$this->doc);
85  $xml = $this->doc->asXML();
86  unset($this->doc);
87  return $xml;
88 
89  }else if( $objects instanceof widget ){
90  // import one widget
91  $this->insert_widget($objects,$this->doc);
92  $xml = $this->doc->asXML();
93  unset($this->doc);
94  return $xml;
95 
96  }else if ( is_array($objects) ){
97  // widget list
98  }else{
99  //throw new Exception();
100  return "";
101  }
102  }
103 
104 
110  private function insert_widget(widget $widget,SimpleXMLElement $parent){
111  $sxe = $parent->addChild("widget");
112  $sxe->addAttribute("version","1.1");
113  // generic widget properties
114  $sxe->addChild("typ", $widget->typ );
115  //$sxe->addChild("name", htmlentities($widget->name , ENT_XML1 , "UTF-8" , false ) );
116  //$sxe->addChild("dispName", htmlentities($widget->dispName,ENT_XML1,"UTF-8",false) );
117  //$sxe->addChild("value", htmlentities($widget->value,ENT_XML1,"UTF-8",false) );
118  $sxe->addChild("name", htmlspecialchars($widget->name , ENT_XML1&ENT_QUOTES , "UTF-8" , false ) );
119  $sxe->addChild("dispName", htmlspecialchars($widget->dispName,ENT_XML1&ENT_QUOTES,"UTF-8",false) );
120  $sxe->addChild("value", htmlspecialchars($widget->value,ENT_XML1&ENT_QUOTES,"UTF-8",false) );
121  $sxe->addChild("is_required", $this->bool2string((bool) $widget->is_required) );
122  $sxe->addChild("is_child", $this->bool2string((bool) $widget->is_child) );
123  $sxe->addChild("parent", (string) $widget->parent );
124  $sxe->addChild("index", $widget->index );
125  $sxe->addChild("ID", $widget->ID );
126  // widget config
127  $c_sxe = $sxe->addChild("config");
128  $widget->unserialize_config(); // make an config array (if not array)
129  $keys = array_keys($widget->config);
130  foreach($keys as $key){
131  // <key name='configname'> <value>val</value> </key>
132  $k = $c_sxe->addchild("key");
133  $k->addAttribute("name",$key);
134  $cnf = $widget->get_config($key);
135  if( ($cnf === true) OR ($cnf === false) ){
136  $cnf = $this->bool2string($cnf);
137  }
138  $k->addChild("value",(string) $cnf);
139  }
140  // container widgets have option_list with widgets inside
141  if( $widget instanceof container ){
142  $this->insert_option_list($widget->option_list , $sxe);
143  $this->insert_widget_list($widget->widget_list , $sxe);
144  }
145  return;
146  }
147 
153  private function insert_poll(poll $poll,SimpleXMLElement $parent){
154  $sxe = $parent->addChild("poll");
155  $sxe->addAttribute("version","1.0");
156  // generic poll properties
157  $sxe->addChild("ID", $poll->ID );
158  $sxe->addChild("hashID", $poll->hashID );
159  $sxe->addChild("name", htmlspecialchars($poll->name , ENT_XML1&ENT_QUOTES , "UTF-8" , false ) );
160  $sxe->addChild("timeout", $poll->timeout );
161  $sxe->addChild("active_since", $poll->active_since );
162  $sxe->addChild("groups", implode(",",$poll->groups) );
163  $sxe->addChild("status", (int) $poll->status );
164  $sxe->addChild("type", (int) $poll->type );
165  $sxe->addChild("anonymous", $this->bool2string((bool) $poll->anonymous) );
166  $sxe->addChild("is_public", $this->bool2string((bool) $poll->is_public) );
167  $sxe->addChild("noDisplay", $this->bool2string((bool) $poll->noDisplay) );
168  $sxe->addChild("theme", htmlspecialchars($poll->theme , ENT_XML1&ENT_QUOTES , "UTF-8" , false ) );
169 
170  // owner
171  // owner_fn
172 
173  // widget list
174  $w_list_sxe = $sxe->addChild("widget_list");
175  foreach( $poll->widget_list as $widget ){
176  $this->insert_widget($widget,$w_list_sxe);
177  }
178  return;
179  }
180 
181 
188  private function make_widget(SimpleXMLElement $sxe){
189  $ver = "";
190  foreach($sxe->attributes() as $attr){
191  if( $attr->getName() === "version" ){
192  $ver = (string) $attr;
193  }
194  }
195  if( $ver == "1.1"){
196  $widget = widgetFactory( $sxe->typ ); // get the widget
197  // generic widget properties
198  //$widget->name = html_entity_decode( $sxe->name , ENT_XML1 , false );
199  //$widget->dispName = html_entity_decode( $sxe->dispName , ENT_XML1 , false );
200  //$widget->value = html_entity_decode( $sxe->value , ENT_XML1 , false );
201  $widget->name = htmlspecialchars_decode( $sxe->name , ENT_XML1&ENT_QUOTES);
202  $widget->dispName = htmlspecialchars_decode( $sxe->dispName , ENT_XML1&ENT_QUOTES);
203  $widget->value = htmlspecialchars_decode( $sxe->value , ENT_XML1&ENT_QUOTES);
204  $widget->is_required= $this->string2bool( $sxe->is_required );
205  $widget->is_child = $this->string2bool( $sxe->is_child );
206  $widget->parent = intval( $sxe->parent );
207  $widget->index = intval( $sxe->index );
208  // widget config
209  foreach($sxe->config->children() as $key){
210  $name = (string) $key->attributes()->name;
211  $val = (string) $key->value;
212  if( $this->is_string_bool($val) ){
213  $val = $this->string2bool($val);
214  }
215  $widget->set_config($name,$val); // ints have to be cast internally, bools are cast just above
216  }
217 
218  if( $widget instanceof container ){
219  $widget->option_list = new object_list(-1,-1);
220  $widget->widget_list = new object_list(-1,-1);
221  foreach( $sxe->option_list->children() as $child){
222  $option = $this->make_widget( $child );
223  $widget->option_list[] = $option;
224  }
225  foreach( $sxe->widget_list->children() as $child){
226  $child_widget = $this->make_widget( $child );
227  $widget->widget_list[] = $child_widget;
228  }
229  }
230  return $widget;
231  }else{
232  throw new Exception("Bad widget version");
233  }
234  }
235 
242  private function make_poll(SimpleXMLElement $sxe){
243  $ver = "";
244  foreach($sxe->attributes() as $attr){
245  if( $attr->getName() === "version" ){
246  $ver = (string) $attr;
247  }
248  }
249  if( $ver == "1.0"){
250  $poll = new poll();
251  // owner
252  // owner_fn
253  $poll->name = htmlspecialchars_decode( $sxe->name , ENT_XML1&ENT_QUOTES);
254  $poll->timeout = $sxe->timeout;
255  $poll->active_since = $sxe->active_since;
256  $poll->groups = explode(",",$sxe->groups);
257  $poll->status = (int) $sxe->status;
258  $poll->type = (int) $sxe->type;
259  $poll->anonymous = $this->string2bool( $sxe->anonymous );
260  $poll->is_public = $this->string2bool( $sxe->is_public );
261  $poll->noDisplay = $this->string2bool( $sxe->noDisplay );
262  $poll->theme = htmlspecialchars_decode( $sxe->theme , ENT_XML1&ENT_QUOTES);
263 
264 
265  $poll->widget_list = array();
266  foreach( $sxe->widget_list->children() as $child){
267  $child_widget = $this->make_widget( $child );
268  $poll->insert_widget(-1,$child_widget);
269  //$poll->widget_list[] = $child_widget;
270  }
271  return $poll;
272  }else{
273  throw new Exception("Bad poll version");
274  }
275  }
276 
277 
283  private function insert_option_list(object_list $list, SimpleXMLElement $parent){
284  $option_list_sxe = $parent->addChild("option_list");
285  foreach($list as $option){
286  $this->insert_widget($option , $option_list_sxe);
287  }
288  }
289 
295  private function insert_widget_list(object_list $list, SimpleXMLElement $parent){
296  $option_list_sxe = $parent->addChild("widget_list");
297  foreach($list as $option){
298  $this->insert_widget($option , $option_list_sxe);
299  }
300  }
301 
302 
309  private function string2bool($str){
310  $res = false;
311  if( mb_strtoupper(trim($str)) === "TRUE" ){
312  $res = true;
313  }
314  return $res;
315  }
316 
323  private function bool2string($bool){
324  $res = "false";
325  if( $bool === true ){
326  $res = "true";
327  }
328  return $res;
329  }
330 
338  private function is_string_bool($str){
339  $str = mb_strtoupper( trim($str));
340  if ( ($str === "TRUE") OR ($str === "FALSE") ){
341  return true;
342  }
343  return false;
344  }
345 
346 
347 }
348 // ! no ending newLine
349 ?>