PageRenderTime 57ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/services/controllers/services.php

https://bitbucket.org/eoanlabs/core
PHP | 219 lines | 166 code | 37 blank | 16 comment | 33 complexity | e308d7c0eb26e44b0b3eafdc105e1ace MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Created by eoanLabs.
  4. * User: Rein
  5. * Date: 7/16/11
  6. * Time: 1:48 PM
  7. */
  8. class Services extends Controller
  9. {
  10. function __construct()
  11. {
  12. parent::__construct();
  13. $this->load->helper('url');
  14. $this->prefix = config_item('ws_prefix');
  15. }
  16. public function index()
  17. {
  18. redirect(BASE_URL . strtolower(__CLASS__) . '/documentation/');
  19. }
  20. public function documentation($method = '')
  21. {
  22. $_service = array();
  23. $_service['soap_wsdl'] = BASE_URL . strtolower(__CLASS__) . '/soap/wsdl/';
  24. $function = '';
  25. $_module = '';
  26. $_service['MODULE_ASSETS_PATH'] = APP_ASSETS_PATH . 'modules/' . strtolower(__CLASS__) . '/';
  27. if(!empty($method))
  28. {
  29. $_tmp = explode(':', $method);
  30. $_module = isset($_tmp[0]) ? $_tmp[0] : '';
  31. $function = isset($_tmp[1]) ? $_tmp[1] : '';
  32. }
  33. // if type and/org method is not defined, then will just show the user the default SERVICE doc list
  34. if (empty($function) || !$method) {
  35. $counter = 0;
  36. $skip = false;
  37. // get the list of modules
  38. foreach (get_modules() as $_mod)
  39. {
  40. $_xml_path = BASE_URL . strtolower(__CLASS__) . '/get_config_xml/' . strtolower($_mod['name']) . '/';
  41. $_service_path = getcwd() . '/../modules/' . strtolower($_mod['name']) . '/models/';
  42. if ($handle = opendir($_service_path)) {
  43. $file_exemption_list = (array)config_item('file_search_exemption');
  44. while (false !== ($file = readdir($handle))) {
  45. if (!in_array($file,$file_exemption_list))
  46. {
  47. // skip any file that have no prefix
  48. if(!strstr($file,$this->prefix))
  49. {
  50. continue;
  51. }
  52. if (!empty($_module) && empty($function) && strtolower($_module) != strtolower($_mod['name'])) {
  53. $_service['active_module'] = $_module;
  54. continue;
  55. }
  56. // if method file and config.xml does not exist, then skip it.
  57. if(!file_exists($_service_path . $file))
  58. {
  59. $skip = true;
  60. log_message('error','missing config : ' . strtolower($_mod['name']) . '/' . $file . '.');
  61. }
  62. if(!file_exists($_service_path . $file))
  63. {
  64. $skip = true;
  65. log_message('error','missing method : ' . strtolower($_mod['name']) . '/' . $file . '.');
  66. $skip = true;
  67. }
  68. // checkpoint #1
  69. if($skip)
  70. {
  71. $skip = false;
  72. continue;
  73. }
  74. $config = xml_to_assoc($_xml_path . current(explode(EXT,$file)),true);
  75. $config_response = is_config_valid($_mod['name'],$file);
  76. if($config_response['status']==200)
  77. {
  78. $skip = false;
  79. }
  80. // now make sure that method declaration exists
  81. $_method_declaration_file = file_get_contents($_service_path . $file);
  82. if(!strpos($_method_declaration_file,'function ' . next(explode($this->prefix,current(explode(EXT,$file)))) . '('))
  83. {
  84. log_message('error','method declaration not found or invalid : ' . strtolower($_mod['name']) . '/' . next(explode($this->prefix,$file)). '.');
  85. $skip = true;
  86. }
  87. // checkpoint #2
  88. if($skip)
  89. {
  90. $skip = false;
  91. continue;
  92. }
  93. $config['module'] = strtolower($_mod['name']);
  94. if ((strtoupper($config['active']) == 'TRUE' || $config['active'] == '1') && strtoupper($config['public']) == 'TRUE') {
  95. $_service['SERVICE'][$counter] = $config;
  96. $counter++;
  97. }
  98. }
  99. }
  100. closedir($handle);
  101. }
  102. }
  103. $_service['modules'] = get_modules();
  104. $this->load->view(strtolower(__FUNCTION__) . '_view', $_service);
  105. }
  106. else
  107. {
  108. // get the list of modules
  109. list($_module, $function) = explode(':', $method);
  110. $_xml_path = BASE_URL . strtolower(__CLASS__) . '/get_config_xml/' . $_module . '/';
  111. $_service_path = getcwd() . '/../modules/' . $_module . '/models/' . $this->prefix.$function.EXT;
  112. if (!file_exists($_service_path)) {
  113. log_message('error','SERVICE /' . $_module . ':' . $function . '/ does not exist.');
  114. }
  115. //$config = xml_to_assoc($_service_path . 'config.xml');
  116. $config = xml_to_assoc($_xml_path . $function,true);
  117. if ((strtoupper($config['active']) == 'TRUE' || $config['active'] == '1') && strtoupper($config['public']) == 'TRUE') {
  118. $data['module'] = $_module;
  119. $data['method'] = $function;
  120. $data['info'] = $config;
  121. $data['MODULE_ASSETS_PATH'] = APP_ASSETS_PATH . 'modules/' . strtolower(__CLASS__) . '/';
  122. $data['soap_wsdl'] = $_service['soap_wsdl'];
  123. $data['service_url'] = BASE_URL . strtolower(__CLASS__) . '/';
  124. $this->load->view(strtolower(__FUNCTION__) . '_method_view', $data);
  125. return;
  126. }
  127. {
  128. show_404();
  129. return;
  130. }
  131. }
  132. }
  133. // http://localhost/core/services/validate/services:greet/output/xsd
  134. public function validate($function,$key,$type = 'xsd')
  135. {
  136. header('Content-type: text/xml');
  137. switch($type)
  138. {
  139. default :
  140. echo $this->_internal_xsd();
  141. }
  142. }
  143. private function _xsd($function,$key)
  144. {
  145. list($module,$method) = explode(':',$function);
  146. return file_get_contents(getcwd() . '/../modules/' . $module . '/models/'.$method.'/'.$key.'.xsd');
  147. }
  148. private function _internal_xsd()
  149. {
  150. echo $xsd = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>
  151. <xs:schema elementFormDefault=\"qualified\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">
  152. <xs:element name=\"xml\">
  153. <xs:complexType>
  154. <xs:sequence>
  155. <xs:element name=\"status\" type=\"xs:integer\" />
  156. <xs:element name=\"response\" type=\"xs:anyType\" />
  157. </xs:sequence>
  158. </xs:complexType>
  159. </xs:element>
  160. </xs:schema>
  161. ";
  162. }
  163. private function _get_config_data($module,$method)
  164. {
  165. return get_config_data($module,$method);
  166. }
  167. public function get_config_xml($module,$method, $security = false)
  168. {
  169. echo header('content-type: text/xml');
  170. if(empty($module) || empty($method) || !$security)
  171. {
  172. echo $xsd = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>
  173. <xml>
  174. <status>".STATUS_FORBIDDEN."</status>
  175. <status>Access denied</status>
  176. </xml>
  177. ";
  178. return false;
  179. }
  180. echo get_config_xml($module,$method);
  181. }
  182. }