XMPPtoForm.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. <?php
  2. class XMPPtoForm
  3. {
  4. private $fieldset;
  5. private $xmpp;
  6. private $html;
  7. public function __construct()
  8. {
  9. $this->fieldset = 0;
  10. $this->html = new \DOMDocument('1.0', 'UTF-8');
  11. $this->xmpp = '';
  12. }
  13. public function getHTML($xmpp)
  14. {
  15. $this->setXMPP($xmpp);
  16. $this->create();
  17. return $this->html->saveXML();
  18. }
  19. public function getArray($xmpp)
  20. {
  21. $array = [];
  22. foreach ($xmpp->children() as $element) {
  23. $array[(string)$element->attributes()->var] = (string)$element->value;
  24. }
  25. return $array;
  26. }
  27. public function setXMPP($xmpp)
  28. {
  29. $this->xmpp = $xmpp;
  30. }
  31. public function create()
  32. {
  33. $this->xmpp = str_replace('xmlns=', 'ns=', $this->xmpp);
  34. $x = new SimpleXMLElement($this->xmpp);
  35. foreach($x->children() as $element){
  36. switch($element->getName()){
  37. case "title":
  38. $this->outTitle($element);
  39. break;
  40. case "instructions":
  41. $this->outP($element);
  42. break;
  43. case "field":
  44. //if($element['type'] != 'hidden' && $element['type'] != 'fixed')
  45. // $this->html .='<div>';
  46. switch($element['type']){
  47. case "boolean":
  48. $this->outCheckbox($element);
  49. break;
  50. //case "fixed":
  51. // $this->outBold($element);
  52. // break;
  53. case "text-single":
  54. $this->outInput($element, "");
  55. break;
  56. case "text-multi":
  57. $this->outTextarea($element);
  58. break;
  59. case "text-private":
  60. $this->outInput($element, "password");
  61. break;
  62. case "hidden":
  63. $this->outHiddeninput($element);
  64. break;
  65. case "list-multi":
  66. //$this->outList($element);
  67. break;
  68. case "list-single":
  69. $this->outList($element);
  70. break;
  71. case "jid-multi":
  72. $this->outInput($element, "email");
  73. break;
  74. case "jid-single":
  75. $this->outInput($element, "email");
  76. break;
  77. case "fixed":
  78. $this->outP((string)$element->value);
  79. break;
  80. default:
  81. $this->outInput($element, "text");
  82. break;
  83. }
  84. //if($element['type'] != 'hidden')
  85. // $this->html .='</div>';
  86. break;
  87. case 'url':
  88. break;
  89. /*XML without <x> element*/
  90. case 'username':
  91. case 'email':
  92. case 'password':
  93. //$this->html .='<div class="element">';
  94. $this->outGeneric($element->getName());
  95. //$this->html .='</div>';
  96. break;
  97. default:
  98. //$this->html .= "";
  99. }
  100. }
  101. /*if($this->fieldset>0){
  102. $this->html .= '</fieldset>';
  103. }*/
  104. }
  105. private function outGeneric($s)
  106. {
  107. $div = $this->html->createElement('div');
  108. $div->setAttribute('class', 'element');
  109. $this->html->appendChild($div);
  110. $input = $this->html->createElement('input');
  111. $input->setAttribute('type', $s);
  112. $input->setAttribute('id', $s);
  113. $input->setAttribute('name', 'generic_'.$s);
  114. $input->setAttribute('required', 'required');
  115. $div->appendChild($input);
  116. $label = $this->html->createElement('label', $s);
  117. $label->setAttribute('for', $s);
  118. $div->appendChild($label);
  119. }
  120. private function outTitle($s)
  121. {
  122. $title = $this->html->createElement('h3', $s);
  123. $this->html->appendChild($title);
  124. }
  125. private function outP($s)
  126. {
  127. $title = $this->html->createElement('p', $s);
  128. $this->html->appendChild($title);
  129. }
  130. private function outUrl($s)
  131. {
  132. $a = $this->html->createElement('a', $s->getName());
  133. $a->setAttribute('href', $s->getName());
  134. $this->html->appendChild($a);
  135. }
  136. /*
  137. private function outBold($s){
  138. if($this->fieldset > 0){
  139. $this->html .= '</fieldset>';
  140. }
  141. $this->html .= '<fieldset><legend>'.$s->value.'</legend><br />';
  142. $this->fieldset ++;
  143. }
  144. */
  145. private function outCheckbox($s)
  146. {
  147. $container = $this->html->createElement('div');
  148. $this->html->appendChild($container);
  149. $div = $this->html->createElement('div');
  150. $div->setAttribute('class', 'select');
  151. $container->appendChild($div);
  152. $select = $this->html->createElement('select');
  153. $select->setAttribute('type', $s['type']);
  154. $select->setAttribute('label', $s['label']);
  155. $select->setAttribute('id', $s['var']);
  156. $select->setAttribute('name', $s['var']);
  157. if ($s->required)
  158. $select->setAttribute('required', 'required');
  159. $div->appendChild($select);
  160. $option = $this->html->createElement('option', __('button.bool_yes'));
  161. $option->setAttribute('value', 'true');
  162. if (isset($s->value) || $s->value == "true" || $s->value == "1")
  163. $option->setAttribute('selected', 'selected');
  164. $select->appendChild($option);
  165. $option = $this->html->createElement('option', __('button.bool_no'));
  166. $option->setAttribute('value', 'false');
  167. if (!isset($s->value) || $s->value == "false" || $s->value == "0")
  168. $option->setAttribute('selected', 'selected');
  169. $select->appendChild($option);
  170. $label = $this->html->createElement('label', $s['label']);
  171. $label->setAttribute('for', $s['var']);
  172. $label->setAttribute('title', $s['label']);
  173. $container->appendChild($label);
  174. }
  175. private function outTextarea($s)
  176. {
  177. $container = $this->html->createElement('div');
  178. $this->html->appendChild($container);
  179. $textarea = $this->html->createElement('textarea');
  180. $textarea->setAttribute('type', $s['type']);
  181. $textarea->setAttribute('label', $s['label']);
  182. $textarea->setAttribute('id', $s['var']);
  183. $textarea->setAttribute('name', $s['var']);
  184. if ($s->required)
  185. $textarea->setAttribute('required', 'required');
  186. foreach ($s->children() as $value){
  187. if($value->getName() == "value"){
  188. $textarea->nodeValue .= $value . "\n";
  189. }
  190. }
  191. if (empty($textarea->nodeValue)) {
  192. $textarea->nodeValue = ' ';
  193. }
  194. $container->appendChild($textarea);
  195. $label = $this->html->createElement('label', $s['label']);
  196. $label->setAttribute('for', $s['var']);
  197. $label->setAttribute('title', $s['label']);
  198. $container->appendChild($label);
  199. }
  200. private function outInput($s, $type = false){
  201. $container = $this->html->createElement('div');
  202. $this->html->appendChild($container);
  203. $input = $this->html->createElement('input');
  204. $input->setAttribute('id', $s['var']);
  205. $input->setAttribute('name', $s['var']);
  206. $input->setAttribute('type', $type);
  207. $input->setAttribute('title', $s->desc);
  208. if($type) {
  209. $input->setAttribute('type', $type);
  210. } else {
  211. $input->setAttribute('type', $s['type']);
  212. }
  213. $input->setAttribute('label', $s['label']);
  214. if($s->required) {
  215. $input->setAttribute('required', 'required');
  216. }
  217. foreach($s->children() as $value){
  218. if($value->getName() == "value"){
  219. $input->setAttribute('value', $value);
  220. }
  221. }
  222. if($s['var'] == 'username') {
  223. $input->setAttribute('pattern', '[a-z0-9_-]*');
  224. }
  225. $container->appendChild($input);
  226. $label = $this->html->createElement('label', $s['label']);
  227. $label->setAttribute('for', $s['var']);
  228. $label->setAttribute('title', $s['label']);
  229. $container->appendChild($label);
  230. }
  231. private function outHiddeninput($s){
  232. $input = $this->html->createElement('input');
  233. $input->setAttribute('name', $s['var']);
  234. $input->setAttribute('type', 'hidden');
  235. $input->setAttribute('value', $s->value);
  236. $this->html->appendChild($input);
  237. }
  238. private function outList($s){
  239. $container = $this->html->createElement('div');
  240. $this->html->appendChild($container);
  241. $div = $this->html->createElement('div');
  242. $div->setAttribute('class', 'select');
  243. $container->appendChild($div);
  244. $select = $this->html->createElement('select');
  245. $select->setAttribute('type', $s['type']);
  246. $select->setAttribute('label', $s['label']);
  247. $select->setAttribute('id', $s['var']);
  248. $select->setAttribute('name', $s['var']);
  249. if($s->required)
  250. $select->setAttribute('required', 'required');
  251. $div->appendChild($select);
  252. if(count($s->xpath('option')) > 0){
  253. foreach($s->option as $option){
  254. if(isset($option->attributes()->label)) {
  255. $opt = $this->html->createElement('option', $option->attributes()->label);
  256. } else {
  257. $opt = $this->html->createElement('option', $option->value);
  258. }
  259. $opt->setAttribute('value', $option->value);
  260. if(
  261. in_array(
  262. (string)$option->value,
  263. array_map(
  264. function($sxml) {
  265. return (string)$sxml;
  266. },
  267. $s->xpath('value')
  268. )
  269. )
  270. ) {
  271. $opt->setAttribute('selected', 'selected');
  272. }
  273. $select->appendChild($opt);
  274. }
  275. }
  276. else{
  277. foreach($s->value as $option){
  278. $label = $option['label'];
  279. $option = $this->html->createElement('option', $option);
  280. $option->setAttribute('value', $label);
  281. $option->setAttribute('selected', 'selected');
  282. $select->appendChild($option);
  283. }
  284. }
  285. $label = $this->html->createElement('label', $s['label']);
  286. $label->setAttribute('for', $s['var']);
  287. $label->setAttribute('title', $s['label']);
  288. $container->appendChild($label);
  289. }
  290. }
  291. class FormtoXMPP
  292. {
  293. private $_form;
  294. private $_inputs;
  295. public function __construct($inputs)
  296. {
  297. $this->_form = new \DOMDocument('1.0', 'UTF-8');
  298. $this->_inputs = $inputs;
  299. }
  300. public function appendToX(\DomDocument $dom)
  301. {
  302. $fields = $this->_form->getElementsByTagName('field');
  303. $list = $dom->getElementsByTagName('x');
  304. foreach ($fields as $field) {
  305. $field = $dom->importNode($field, true);
  306. $list[0]->appendChild($field);
  307. }
  308. }
  309. public function create()
  310. {
  311. foreach($this->_inputs as $key => $value) {
  312. $container = $this->_form->createElement('container');
  313. $this->_form->appendChild($container);
  314. $field = $this->_form->createElement('field');
  315. $container->appendChild($field);
  316. $val = $this->_form->createElement('value');
  317. $field->appendChild($val);
  318. if ($value->value === 'true') {
  319. $val->nodeValue = '1';
  320. }
  321. if ($value->value === 'false') {
  322. $val->nodeValue = '0';
  323. }
  324. if (is_bool($value->value)) {
  325. $val->nodeValue = ($value) ? '1' : '0';
  326. }
  327. if (empty($val->nodeValue)
  328. && $value !== 'false' // WTF PHP !!!
  329. ) {
  330. $val->nodeValue = trim($value->value);
  331. }
  332. $field->setAttribute('var', trim($key));
  333. }
  334. return $this->_form->saveXML();
  335. }
  336. }
  337. ?>