service.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.webservices
  5. */
  6. ini_set('log_errors_max_len', 0);
  7. ini_set('soap.wsdl_cache_enabled', '0');
  8. ini_set('soap.wsdl_cache_ttl', '0');
  9. require_once '../../../main/inc/global.inc.php';
  10. require_once '../../../vendor/autoload.php';
  11. ini_set("soap.wsdl_cache_enabled", 0);
  12. $libpath = api_get_path(LIBRARY_PATH);
  13. require_once api_get_path(SYS_PLUGIN_PATH).'sepe/ws/Sepe.php';
  14. require_once $libpath.'nusoap/class.nusoap_base.php';
  15. require_once api_get_path(SYS_PLUGIN_PATH).'sepe/src/wsse/soap-server-wsse.php';
  16. //require_once api_get_path(SYS_PLUGIN_PATH).'sepe/src/wsse/soap-wsse.php';
  17. $ns = api_get_path(WEB_PLUGIN_PATH)."sepe/ws/ProveedorCentroTFWS.wsdl";
  18. $wsdl = api_get_path(SYS_PLUGIN_PATH)."sepe/ws/ProveedorCentroTFWS.wsdl";
  19. $serviceUrl = api_get_path(WEB_PLUGIN_PATH).'sepe/ws/service.php';
  20. /**
  21. * Class CustomServer.
  22. */
  23. class CustomServer extends Zend\Soap\Server
  24. {
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public function __construct($wsdl = null, array $options = null)
  29. {
  30. parent::__construct($wsdl, $options);
  31. // Response of handle will always be returned
  32. $this->setReturnResponse(true);
  33. }
  34. public function handle($request = null)
  35. {
  36. $response = parent::handle($request);
  37. $response = str_replace(
  38. 'xmlns:ns1="http://impl.ws.application.proveedorcentro.meyss.spee.es"',
  39. 'xmlns:ns1="http://impl.ws.application.proveedorcentro.meyss.spee.es" xmlns:impl="http://impl.ws.application.proveedorcentro.meyss.spee.es" xmlns:sal="http://salida.bean.domain.common.proveedorcentro.meyss.spee.es" xmlns:ent="http://entsal.bean.domain.common.proveedorcentro.meyss.spee.es" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"',
  40. $response
  41. );
  42. $response = $this->addNamespaceToTag($response, 'RESPUESTA_DATOS_CENTRO', 'sal');
  43. $response = $this->addNamespaceToTag($response, 'RESPUESTA_OBT_LISTA_ACCIONES', 'sal');
  44. $response = $this->addNamespaceToTag($response, 'RESPUESTA_ELIMINAR_ACCION', 'sal');
  45. $response = $this->addNamespaceToTag($response, 'RESPUESTA_OBT_ACCION', 'sal');
  46. $response = $this->addNamespaceToTag($response, 'ACCION_FORMATIVA', 'ent');
  47. $response = $this->addNamespaceToTag($response, 'ID_ACCION', 'ent');
  48. $response = $this->addNamespaceToTag($response, 'DATOS_IDENTIFICATIVOS', 'ent');
  49. // Dentro de ACCION_FORMATIVA no hay ent:ID_ACCION
  50. $response = str_replace(
  51. '<ent:ACCION_FORMATIVA><ent:ID_ACCION>',
  52. '<ent:ACCION_FORMATIVA><ID_ACCION>',
  53. $response
  54. );
  55. $response = str_replace(
  56. '</ent:ID_ACCION><SITUACION>',
  57. '</ID_ACCION><SITUACION>',
  58. $response
  59. );
  60. //$response = file_get_contents('/tmp/log4.xml');
  61. header('Content-Length:'.strlen($response));
  62. echo $response;
  63. exit;
  64. }
  65. private function addNamespaceToTag($response, $tag, $namespace)
  66. {
  67. return str_replace(
  68. $tag,
  69. $namespace.":".$tag,
  70. $response
  71. );
  72. }
  73. }
  74. function authenticate($WSUser, $WSKey)
  75. {
  76. $tUser = Database::get_main_table(TABLE_MAIN_USER);
  77. $tApi = Database::get_main_table(TABLE_MAIN_USER_API_KEY);
  78. $login = Database::escape_string($WSUser);
  79. $sql = "SELECT u.user_id, u.status FROM $tUser u, $tApi a
  80. WHERE
  81. u.username='".$login."' AND
  82. u.user_id = a.user_id AND
  83. a.api_service = 'dokeos' AND
  84. a.api_key='".$WSKey."'";
  85. $result = Database::query($sql);
  86. if (Database::num_rows($result) > 0) {
  87. $row = Database::fetch_row($result);
  88. if ($row[1] == '4') { //UserManager::is_admin($row[0])) {
  89. return true;
  90. } else {
  91. return false;
  92. }
  93. } else {
  94. return false;
  95. }
  96. }
  97. $doc = new DOMDocument();
  98. $post = file_get_contents('php://input');
  99. if (!empty($post)) {
  100. $doc->loadXML($post);
  101. $WSUser = $doc->getElementsByTagName('Username')->item(0)->nodeValue;
  102. $WSKey = $doc->getElementsByTagName('Password')->item(0)->nodeValue;
  103. $s = new WSSESoapServer($doc);
  104. if (!empty($WSUser) && !empty($WSKey)) {
  105. if (authenticate($WSUser, $WSKey)) {
  106. // pointing to the current file here
  107. $options = [
  108. 'soap_version' => SOAP_1_1,
  109. ];
  110. $soap = new CustomServer($wsdl, $options);
  111. $soap->setObject(new Sepe());
  112. if ($s->process()) {
  113. $xml = $s->saveXML();
  114. //header('Content-type: application/xml');
  115. $soap->handle($xml);
  116. exit;
  117. } else {
  118. error_log('not processed');
  119. }
  120. } else {
  121. error_log('Claves incorrectas');
  122. }
  123. } else {
  124. error_log('not processed');
  125. }
  126. } else {
  127. $contents = file_get_contents($wsdl);
  128. header('Content-type: application/xml');
  129. echo $contents;
  130. exit;
  131. }
  132. exit;