PageRenderTime 38ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/webservice/rest/testclient/lib.php

https://github.com/nicolasconnault/moodle2.0
PHP | 226 lines | 135 code | 18 blank | 73 comment | 33 complexity | ddcd848d268e038bedfa0b261e612ef1 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, BSD-3-Clause
  1. <?php
  2. /**
  3. * Created on 10/17/2008
  4. *
  5. * Rest Test Client Suport Library
  6. *
  7. * @author David Castro Garcia
  8. * @author Ferran Recio Calderó
  9. */
  10. //--- interface helpers ---
  11. /**
  12. * starts the interface
  13. */
  14. function start_interface ($title="&nbsp;") {
  15. $title2 = ($title)?$title:"&nbsp;";
  16. echo '<html><head>';
  17. echo "<title>Moodle Webservice Rest Test Client</title>";
  18. echo '<link rel="stylesheet" href="style.css" type="text/css">';
  19. echo '</head><body>';
  20. echo '<div class="head"><h1>Moodle Webservice Rest Test Client</h1>';
  21. echo "<h3>$title2</h3>";
  22. echo '</div>';
  23. echo '<div class="content">';
  24. if ($title) echo '<p class="return"><a href="index.php"><img src="return.gif" border="0"/></a></p>';
  25. }
  26. /**
  27. * end interface
  28. *
  29. * @param bool $ret=true: show return button
  30. */
  31. function end_interface ($ret = true) {
  32. if ($ret) echo '<p class="return"><a href="index.php"><img src="return.gif" border="0"/></a></p>';
  33. echo '</div>';
  34. echo '<div class="footer">Created by David Castro i Ferran Recio for Moodle Webservices</div>';
  35. echo '</body></html>';
  36. }
  37. /**
  38. * print XML div area
  39. *
  40. * @param string $xml
  41. *
  42. */
  43. function show_xml ($xml) {
  44. echo '<div class="xmlshow">';
  45. echo '<a onClick="document.getElementById(\'toogleme\').style.display = ' .
  46. '(document.getElementById(\'toogleme\').style.display!=\'none\')?\'none\':\'\';">Hide/Show XML</a>';
  47. echo "<div style=\"display:none;\" id=\"toogleme\">";
  48. echo '<pre>';echo htmlentities($xml);echo '</pre>';
  49. echo "</div>";
  50. echo "</div>";
  51. }
  52. /**
  53. * format post data
  54. */
  55. function format_postdata ($data) {
  56. $o="";
  57. foreach ($data as $k=>$v) {
  58. $o.= "$k=".rawurlencode($v)."&";
  59. }
  60. $post_data=substr($o,0,-1);
  61. return $post_data;
  62. }
  63. /**
  64. * shows an object in list format
  65. *
  66. * @param mixed $obj
  67. * @param integer $cols: number of colums
  68. * @ string string $check=false: if this attribute is not present, the $obj is ans error
  69. *
  70. */
  71. function show_object ($obj,$cols=1,$check=false) {
  72. if (!is_array($obj)) $obj = array($obj);
  73. echo '<ul class="results">';
  74. foreach ($obj as $r) {
  75. if ($check && (!isset($r->$check) || $r->$check==-1)) {
  76. echo '<li class="error">';
  77. echo "EMPTY ROW!";
  78. } else {
  79. if (is_object($r)) {
  80. echo '<li class="element">';
  81. $text = array();
  82. $parts = get_object_vars($r);
  83. $num = 1;
  84. $currline = '';
  85. foreach ($parts as $key => $val) {
  86. $currline.= "<span class=\"resultval\"><b>$key:</b> <i>$val</i></span>, ";
  87. if ($num >= $cols) {
  88. $currline=substr($currline,0,-2);
  89. $text[] = $currline;
  90. $currline = '';
  91. $num = 0;
  92. }
  93. $num++;
  94. }
  95. echo implode('<br/>',$text);
  96. } else {
  97. if ($r==-1 || !$r) {
  98. echo '<li class="error">';
  99. echo "EMPTY ROW!";
  100. } else {
  101. echo '<li class="element">';
  102. echo "<span class=\"resultval\"><b>Returned Value:</b> <i>$r</i></span>";
  103. }
  104. }
  105. }
  106. echo '</li>';
  107. }
  108. echo '</ul>';
  109. }
  110. //---- XML simple parser ----
  111. //this code was donated by Ferran Recio
  112. /**
  113. * convert a simple xml into php object
  114. *
  115. * @author ferran recio
  116. *
  117. * @param String $xml
  118. *
  119. * @return mixed
  120. */
  121. function basicxml_xml_to_object ($xml) {
  122. $xml=utf8_encode($xml);
  123. //create the parser
  124. $parser = xml_parser_create ();
  125. xml_set_default_handler ($parser,'basicxml_xml_to_object_aux');
  126. $values = array();
  127. $index = array();
  128. xml_parse_into_struct($parser,$xml,$values,$index);
  129. //print_object($values);
  130. //print_object($index);
  131. //just simplexml tag (disabled)
  132. //if (strtolower($values[0]['tag']) != 'basicxml') return false;
  133. //if (strtolower($values[count($values)-1]['tag']) != 'basicxml') return false;
  134. $res = basicxml_xml_to_object_aux ($values);
  135. //omit the first tag
  136. $parts = array_keys(get_object_vars($res));
  137. $key = $parts[0];
  138. return $res->$key;
  139. }
  140. /**
  141. * auxiliar function to basicxml_xml_to_object
  142. *
  143. * @author ferran recio
  144. *
  145. * @param mixed $values
  146. *
  147. * @return mixed
  148. */
  149. function basicxml_xml_to_object_aux ($values) {
  150. if (!is_array($values)) return false;
  151. //print_object ($values);
  152. $currset = array();
  153. $search = false;
  154. foreach ($values as $value) {
  155. $tag = strtolower($value['tag']);
  156. //if we are acomulating, just acomulate it
  157. if ($search) {
  158. //if it closes a tag, we just stop searching
  159. if ($tag == $search && $value['type']=='close') {
  160. //recursivity
  161. $obj2 = basicxml_xml_to_object_aux ($currset);
  162. //search cleaning
  163. $search = false;
  164. //add to result
  165. if (isset($res->{$tag})){
  166. if (is_array($res->{$tag})){
  167. $res->{$tag}[] = $obj2;
  168. } else {
  169. $res->{$tag} = array($res->{$tag},$obj2);
  170. }
  171. } else {
  172. $res->{$tag} = $obj2;
  173. }
  174. } else {
  175. //we are searching. If it's cdada, pass it throw
  176. if ($value['type']=='cdata') continue;
  177. //if isn't cdata, put it in the set and continue searching
  178. //(because isn't the close we're searching)
  179. $currset[] = $value;
  180. }
  181. } else {
  182. //walking the xml
  183. if ($value['type']=='open'){
  184. //on open, let's search on it
  185. $currset = array();
  186. $search = $tag;
  187. } else {
  188. //if it's complete just save it
  189. if ($value['type']=='complete') {
  190. if (!empty($value['value'] )) {
  191. $val = html_entity_decode($value['value']);
  192. if (isset($res->{$tag})){
  193. if (is_array($res->{$tag})){
  194. $res->{$tag}[] = $val;
  195. } else {
  196. $res->{$tag} = array($res->{$tag},$val);
  197. }
  198. } else {
  199. $res->{$tag} = $val;
  200. }
  201. }
  202. }
  203. }
  204. }
  205. }
  206. return $res;
  207. }
  208. ?>