You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

416 lines
13 KiB

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