/modules/services/controllers/services.php
PHP | 219 lines | 166 code | 37 blank | 16 comment | 33 complexity | e308d7c0eb26e44b0b3eafdc105e1ace MD5 | raw file
Possible License(s): BSD-3-Clause
- <?php
- /**
- * Created by eoanLabs.
- * User: Rein
- * Date: 7/16/11
- * Time: 1:48 PM
- */
-
- class Services extends Controller
- {
- function __construct()
- {
- parent::__construct();
- $this->load->helper('url');
- $this->prefix = config_item('ws_prefix');
- }
-
- public function index()
- {
- redirect(BASE_URL . strtolower(__CLASS__) . '/documentation/');
- }
-
- public function documentation($method = '')
- {
- $_service = array();
- $_service['soap_wsdl'] = BASE_URL . strtolower(__CLASS__) . '/soap/wsdl/';
- $function = '';
- $_module = '';
- $_service['MODULE_ASSETS_PATH'] = APP_ASSETS_PATH . 'modules/' . strtolower(__CLASS__) . '/';
-
- if(!empty($method))
- {
- $_tmp = explode(':', $method);
- $_module = isset($_tmp[0]) ? $_tmp[0] : '';
- $function = isset($_tmp[1]) ? $_tmp[1] : '';
-
- }
-
- // if type and/org method is not defined, then will just show the user the default SERVICE doc list
- if (empty($function) || !$method) {
- $counter = 0;
- $skip = false;
-
- // get the list of modules
- foreach (get_modules() as $_mod)
- {
- $_xml_path = BASE_URL . strtolower(__CLASS__) . '/get_config_xml/' . strtolower($_mod['name']) . '/';
- $_service_path = getcwd() . '/../modules/' . strtolower($_mod['name']) . '/models/';
- if ($handle = opendir($_service_path)) {
- $file_exemption_list = (array)config_item('file_search_exemption');
- while (false !== ($file = readdir($handle))) {
- if (!in_array($file,$file_exemption_list))
- {
- // skip any file that have no prefix
- if(!strstr($file,$this->prefix))
- {
- continue;
- }
-
- if (!empty($_module) && empty($function) && strtolower($_module) != strtolower($_mod['name'])) {
- $_service['active_module'] = $_module;
- continue;
- }
-
-
- // if method file and config.xml does not exist, then skip it.
- if(!file_exists($_service_path . $file))
- {
- $skip = true;
- log_message('error','missing config : ' . strtolower($_mod['name']) . '/' . $file . '.');
- }
-
- if(!file_exists($_service_path . $file))
- {
- $skip = true;
- log_message('error','missing method : ' . strtolower($_mod['name']) . '/' . $file . '.');
-
- $skip = true;
- }
-
-
- // checkpoint #1
- if($skip)
- {
- $skip = false;
- continue;
- }
- $config = xml_to_assoc($_xml_path . current(explode(EXT,$file)),true);
- $config_response = is_config_valid($_mod['name'],$file);
- if($config_response['status']==200)
- {
- $skip = false;
- }
-
-
- // now make sure that method declaration exists
-
- $_method_declaration_file = file_get_contents($_service_path . $file);
- if(!strpos($_method_declaration_file,'function ' . next(explode($this->prefix,current(explode(EXT,$file)))) . '('))
- {
-
- log_message('error','method declaration not found or invalid : ' . strtolower($_mod['name']) . '/' . next(explode($this->prefix,$file)). '.');
- $skip = true;
-
- }
-
-
-
- // checkpoint #2
- if($skip)
- {
- $skip = false;
- continue;
- }
-
-
-
- $config['module'] = strtolower($_mod['name']);
- if ((strtoupper($config['active']) == 'TRUE' || $config['active'] == '1') && strtoupper($config['public']) == 'TRUE') {
- $_service['SERVICE'][$counter] = $config;
- $counter++;
- }
-
-
- }
- }
- closedir($handle);
- }
-
- }
- $_service['modules'] = get_modules();
- $this->load->view(strtolower(__FUNCTION__) . '_view', $_service);
- }
- else
- {
- // get the list of modules
- list($_module, $function) = explode(':', $method);
- $_xml_path = BASE_URL . strtolower(__CLASS__) . '/get_config_xml/' . $_module . '/';
- $_service_path = getcwd() . '/../modules/' . $_module . '/models/' . $this->prefix.$function.EXT;
-
- if (!file_exists($_service_path)) {
- log_message('error','SERVICE /' . $_module . ':' . $function . '/ does not exist.');
- }
-
- //$config = xml_to_assoc($_service_path . 'config.xml');
- $config = xml_to_assoc($_xml_path . $function,true);
- if ((strtoupper($config['active']) == 'TRUE' || $config['active'] == '1') && strtoupper($config['public']) == 'TRUE') {
-
-
- $data['module'] = $_module;
- $data['method'] = $function;
- $data['info'] = $config;
- $data['MODULE_ASSETS_PATH'] = APP_ASSETS_PATH . 'modules/' . strtolower(__CLASS__) . '/';
- $data['soap_wsdl'] = $_service['soap_wsdl'];
- $data['service_url'] = BASE_URL . strtolower(__CLASS__) . '/';
- $this->load->view(strtolower(__FUNCTION__) . '_method_view', $data);
- return;
- }
- {
- show_404();
- return;
- }
- }
- }
- // http://localhost/core/services/validate/services:greet/output/xsd
-
- public function validate($function,$key,$type = 'xsd')
- {
- header('Content-type: text/xml');
- switch($type)
- {
- default :
- echo $this->_internal_xsd();
- }
- }
-
- private function _xsd($function,$key)
- {
- list($module,$method) = explode(':',$function);
- return file_get_contents(getcwd() . '/../modules/' . $module . '/models/'.$method.'/'.$key.'.xsd');
- }
-
- private function _internal_xsd()
- {
- echo $xsd = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>
- <xs:schema elementFormDefault=\"qualified\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">
- <xs:element name=\"xml\">
- <xs:complexType>
- <xs:sequence>
- <xs:element name=\"status\" type=\"xs:integer\" />
- <xs:element name=\"response\" type=\"xs:anyType\" />
- </xs:sequence>
- </xs:complexType>
- </xs:element>
- </xs:schema>
- ";
- }
-
- private function _get_config_data($module,$method)
- {
- return get_config_data($module,$method);
- }
-
- public function get_config_xml($module,$method, $security = false)
- {
- echo header('content-type: text/xml');
- if(empty($module) || empty($method) || !$security)
- {
- echo $xsd = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>
- <xml>
- <status>".STATUS_FORBIDDEN."</status>
- <status>Access denied</status>
- </xml>
- ";
- return false;
- }
- echo get_config_xml($module,$method);
- }
- }