Completed
Push — master ( 4fc9f8...d0e06e )
by Julito
12:04
created

authenticate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 15
nc 3
nop 2
dl 0
loc 22
rs 9.7666
c 0
b 0
f 0
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
ini_set('log_errors_max_len', 0);
6
ini_set('soap.wsdl_cache_enabled', '0');
7
ini_set('soap.wsdl_cache_ttl', '0');
8
9
require_once '../../../main/inc/global.inc.php';
10
require_once '../../../vendor/autoload.php';
11
12
ini_set('soap.wsdl_cache_enabled', 0);
13
$libpath = api_get_path(LIBRARY_PATH);
14
require_once api_get_path(SYS_PLUGIN_PATH).'sepe/ws/Sepe.php';
15
16
require_once $libpath.'nusoap/class.nusoap_base.php';
17
require_once api_get_path(SYS_PLUGIN_PATH).'sepe/src/wsse/soap-server-wsse.php';
18
//require_once api_get_path(SYS_PLUGIN_PATH).'sepe/src/wsse/soap-wsse.php';
19
20
$ns = api_get_path(WEB_PLUGIN_PATH).'sepe/ws/ProveedorCentroTFWS.wsdl';
21
$wsdl = api_get_path(SYS_PLUGIN_PATH).'sepe/ws/ProveedorCentroTFWS.wsdl';
22
$serviceUrl = api_get_path(WEB_PLUGIN_PATH).'sepe/ws/service.php';
23
24
/**
25
 * Class CustomServer.
26
 */
27
class CustomServer extends Zend\Soap\Server
28
{
29
    public function __construct($wsdl = null, array $options = null)
30
    {
31
        parent::__construct($wsdl, $options);
32
33
        // Response of handle will always be returned
34
        $this->setReturnResponse(true);
35
    }
36
37
    public function handle($request = null)
38
    {
39
        $response = parent::handle($request);
40
        $response = str_replace(
41
            'xmlns:ns1="http://impl.ws.application.proveedorcentro.meyss.spee.es"',
42
            '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/"',
43
            $response
44
        );
45
46
        $response = $this->addNamespaceToTag($response, 'RESPUESTA_DATOS_CENTRO', 'sal');
47
        $response = $this->addNamespaceToTag($response, 'RESPUESTA_OBT_LISTA_ACCIONES', 'sal');
48
        $response = $this->addNamespaceToTag($response, 'RESPUESTA_ELIMINAR_ACCION', 'sal');
49
        $response = $this->addNamespaceToTag($response, 'RESPUESTA_OBT_ACCION', 'sal');
50
51
        $response = $this->addNamespaceToTag($response, 'ACCION_FORMATIVA', 'ent');
52
        $response = $this->addNamespaceToTag($response, 'ID_ACCION', 'ent');
53
        $response = $this->addNamespaceToTag($response, 'DATOS_IDENTIFICATIVOS', 'ent');
54
55
        // Dentro de ACCION_FORMATIVA no hay ent:ID_ACCION
56
        $response = str_replace(
57
            '<ent:ACCION_FORMATIVA><ent:ID_ACCION>',
58
            '<ent:ACCION_FORMATIVA><ID_ACCION>',
59
            $response
60
        );
61
62
        $response = str_replace(
63
            '</ent:ID_ACCION><SITUACION>',
64
            '</ID_ACCION><SITUACION>',
65
            $response
66
        );
67
68
        //$response = file_get_contents('/tmp/log4.xml');
69
        header('Content-Length:'.strlen($response));
70
        echo $response;
71
        exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
72
    }
73
74
    private function addNamespaceToTag($response, $tag, $namespace)
75
    {
76
        return str_replace(
77
            $tag,
78
            $namespace.':'.$tag,
79
            $response
80
        );
81
    }
82
}
83
84
function authenticate($WSUser, $WSKey)
85
{
86
    $tUser = Database::get_main_table(TABLE_MAIN_USER);
87
    $tApi = Database::get_main_table(TABLE_MAIN_USER_API_KEY);
88
    $login = Database::escape_string($WSUser);
89
    $sql = "SELECT u.user_id, u.status FROM $tUser u, $tApi a 
90
            WHERE 
91
                u.username='".$login."' AND  
92
                u.user_id = a.user_id AND 
93
                a.api_service = 'dokeos' AND 
94
                a.api_key='".$WSKey."'";
95
    $result = Database::query($sql);
96
97
    if (Database::num_rows($result) > 0) {
98
        $row = Database::fetch_row($result);
99
        if ('4' == $row[1]) { //UserManager::is_admin($row[0])) {
100
            return true;
101
        } else {
102
            return false;
103
        }
104
    } else {
105
        return false;
106
    }
107
}
108
109
$doc = new DOMDocument();
110
$post = file_get_contents('php://input');
111
if (!empty($post)) {
112
    $doc->loadXML($post);
113
114
    $WSUser = $doc->getElementsByTagName('Username')->item(0)->nodeValue;
115
    $WSKey = $doc->getElementsByTagName('Password')->item(0)->nodeValue;
116
117
    $s = new WSSESoapServer($doc);
118
    if (!empty($WSUser) && !empty($WSKey)) {
119
        if (authenticate($WSUser, $WSKey)) {
120
            // pointing to the current file here
121
            $options = [
122
                'soap_version' => SOAP_1_1,
123
            ];
124
            $soap = new CustomServer($wsdl, $options);
125
            $soap->setObject(new Sepe());
126
127
            if ($s->process()) {
128
                $xml = $s->saveXML();
129
                //header('Content-type: application/xml');
130
                $soap->handle($xml);
131
                exit;
132
            } else {
133
                error_log('not processed');
134
            }
135
        } else {
136
            error_log('Claves incorrectas');
137
        }
138
    } else {
139
        error_log('not processed');
140
    }
141
} else {
142
    $contents = file_get_contents($wsdl);
143
    header('Content-type: application/xml');
144
    echo $contents;
145
    exit;
146
}
147
exit;
148