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.

257 lines
7.0 KiB

13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
  1. <?php
  2. class XMPPtoForm{
  3. private $fieldset;
  4. private $xmpp;
  5. private $html;
  6. public function __construct(){
  7. $this->fieldset = 0;
  8. $this->html = '';
  9. $this->xmpp = '';
  10. }
  11. public function getHTML($xmpp){
  12. $this->setXMPP($xmpp);
  13. $this->create();
  14. return $this->html;
  15. }
  16. public function setXMPP($xmpp){
  17. $this->xmpp = $xmpp;
  18. }
  19. public function create(){
  20. $this->xmpp = str_replace('xmlns=', 'ns=', $this->xmpp);
  21. $x = new SimpleXMLElement($this->xmpp);
  22. \movim_log($x);
  23. foreach($x->children() as $element){
  24. switch($element->getName()){
  25. case "title":
  26. $this->outTitle($element);
  27. break;
  28. case "instructions":
  29. $this->outP($element);
  30. break;
  31. case "field":
  32. if($element['type'] != 'hidden' && $element['type'] != 'fixed')
  33. $this->html .='<div class="element">';
  34. switch($element['type']){
  35. case "boolean":
  36. $this->outCheckbox($element);
  37. break;
  38. case "fixed":
  39. $this->outBold($element);
  40. break;
  41. case "text-single":
  42. $this->outInput($element, "", "");
  43. break;
  44. case "text-multi":
  45. $this->outTextarea($element);
  46. break;
  47. case "text-private":
  48. $this->outInput($element, "password", "");
  49. break;
  50. case "hidden":
  51. $this->outHiddeninput($element);
  52. break;
  53. case "list-multi":
  54. $this->outList($element, "multiple");
  55. break;
  56. case "list-single":
  57. $this->outList($element, "");
  58. break;
  59. case "jid-multi":
  60. $this->outInput($element, "email", "multiple");
  61. break;
  62. case "jid-single":
  63. $this->outInput($element, "email", "");
  64. break;
  65. default:
  66. $this->html .= "";
  67. }
  68. if($element['type'] != 'hidden')
  69. $this->html .='</div>';
  70. break;
  71. case 'url':
  72. break;
  73. /*XML without <x> element*/
  74. case 'username':
  75. case 'email':
  76. case 'password':
  77. $this->html .='<div class="element">';
  78. $this->outGeneric($element->getName());
  79. $this->html .='</div>';
  80. break;
  81. default:
  82. $this->html .= "";
  83. }
  84. }
  85. if($this->fieldset>0){
  86. $this->html .= '</fieldset>';
  87. }
  88. }
  89. private function outGeneric($s){
  90. $this->html .= '
  91. <label for="'.$s.'">'.
  92. $s.'
  93. </label>
  94. <input id="'.$s.'" name="generic_'.$s.'" type="'.$s.'" required/>';
  95. }
  96. private function outTitle($s){
  97. $this->html .= '<h3>'.$s.'</h3>';
  98. }
  99. private function outP($s){
  100. $this->html .= '<p>'.$s.'</p>';
  101. }
  102. private function outUrl($s) {
  103. $this->html .= '<a href="'.$s->getName().'">'.$s->getName().'</a>';
  104. }
  105. private function outBold($s){
  106. if($this->fieldset > 0){
  107. $this->html .= '</fieldset>';
  108. }
  109. $this->html .= '<fieldset><legend>'.$s->value.'</legend><br />';
  110. $this->fieldset ++;
  111. }
  112. private function outCheckbox($s){
  113. $this->html .= '
  114. <label for="'.$s['var'].'">';
  115. if($s['label']==null){
  116. $this->html .= $s['var'];
  117. }
  118. else{
  119. $this->html .= $s['label'];
  120. }
  121. $this->html .= '
  122. </label>';
  123. $this->html .= '
  124. <input
  125. id="'.$s['var'].'"
  126. name="'.$s['var'].'"
  127. type="checkbox" '.$s->required;
  128. if($s->value == "true" || $s->value == "1")
  129. $this->html .= ' checked';
  130. $this->html .= '/>';
  131. }
  132. private function outTextarea($s){
  133. $this->html .= '<label for="'.$s["var"].'">'.$s["label"].'</label>
  134. <textarea id="'.$s["var"].'" name="'.$s["var"].'" required="'.$s->required.'">';
  135. foreach($s->children() as $value){
  136. if($value->getName() == "value"){
  137. $this->html .= $value;
  138. }
  139. }
  140. $this->html .= '</textarea>';
  141. }
  142. private function outInput($s, $type, $multiple){
  143. $this->html .= '
  144. <label for="'.$s["var"].'">'.$s["label"].'</label>
  145. <input id="'.$s["var"].'" name="'.$s["var"].'" value="';
  146. foreach($s->children() as $value){
  147. if($value->getName() == "value"){
  148. $this->html .= $value.' ';
  149. }
  150. }
  151. $this->html .= '" type="'.$type.'" title="'.$s->desc.'"
  152. '.$multiple.' '.$s->required.'/>';
  153. }
  154. private function outHiddeninput($s){
  155. $this->html .= '<input type="hidden" name="'.$s["var"].'" value="'.$s->value.'" />';
  156. }
  157. private function outList($s, $multiple){
  158. $this->html .= '<label for="'.$s["var"].'">'.$s["label"].'</label>
  159. <div class="select"><select id="'.$s["var"].'" name="'.$s['var'].'" '.$multiple.' '.$s->required.'>';
  160. if(count($s->xpath('option')) > 0){
  161. foreach($s->option as $option){
  162. $this->html .= '<option value="'.$option->value.'"';
  163. if(in_array((string)$option->value, $s->xpath('value')))
  164. $this->html .= ' selected';
  165. $this->html .= '>'.$option->value.'</option>';
  166. }
  167. }
  168. else{
  169. foreach($s->value as $option){
  170. $this->html .= '<option value="'.$option['label'].'" selected>'
  171. .$option.'</option>';
  172. }
  173. }
  174. $this->html .= '</select></div>';
  175. }
  176. }
  177. class FormtoXMPP{
  178. private $stream;
  179. private $inputs;
  180. private $dataform;
  181. public function __construct(){
  182. $this->stream = '';
  183. $this->inputs = array();
  184. $this->dataform = true;
  185. }
  186. public function getXMPP($stream, $inputs){
  187. $this->setXMPP($stream);
  188. $this->setInputs($inputs);
  189. $this->create();
  190. return $this->stream;
  191. }
  192. public function setXMPP($stream){
  193. $this->stream = new SimpleXMLElement($stream);
  194. }
  195. public function setInputs($inputs){
  196. $this->inputs = $inputs;
  197. }
  198. public function setDataformOff() {
  199. $this->dataform = false;
  200. }
  201. public function create(){
  202. switch($this->stream->getName()){
  203. case "stream":
  204. $node = $this->stream->iq->query;
  205. break;
  206. case "pubsub":
  207. $node = $this->stream->configure->x;
  208. break;
  209. }
  210. foreach($this->inputs as $key => $value) {
  211. if($value == '' && $this->stream->getName() == "stream") {
  212. RPC::call('movim_reload', BASE_URI."index.php?q=account&err=datamissing");
  213. RPC::commit();
  214. exit;
  215. } elseif(substr($key, 0, 8) == 'generic_') {
  216. $key = str_replace('generic_', '', $key);
  217. $node->addChild($key, $value);
  218. } else{
  219. $field = $node->addChild('field');
  220. if($value == 'true')
  221. $value = '1';
  222. if($value == 'false')
  223. $value = '0';
  224. $field->addChild('value', trim($value));
  225. $field->addAttribute('var', trim($key));
  226. }
  227. }
  228. }
  229. }
  230. ?>