|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
ÁTICA - Aplicación web para la gestión documental de centros educativos |
|
4
|
|
|
|
|
5
|
|
|
Copyright (C) 2015-2017: Luis Ramón López López |
|
6
|
|
|
|
|
7
|
|
|
This program is free software: you can redistribute it and/or modify |
|
8
|
|
|
it under the terms of the GNU Affero General Public License as published by |
|
9
|
|
|
the Free Software Foundation, either version 3 of the License, or |
|
10
|
|
|
(at your option) any later version. |
|
11
|
|
|
|
|
12
|
|
|
This program is distributed in the hope that it will be useful, |
|
13
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15
|
|
|
GNU Affero General Public License for more details. |
|
16
|
|
|
|
|
17
|
|
|
You should have received a copy of the GNU Affero General Public License |
|
18
|
|
|
along with this program. If not, see [http://www.gnu.org/licenses/]. |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace Atica\Service; |
|
22
|
|
|
|
|
23
|
|
|
class SenecaAuthenticatorService |
|
24
|
|
|
{ |
|
25
|
|
|
/** @var string */ |
|
26
|
|
|
private $url; |
|
27
|
|
|
|
|
28
|
|
|
/** @var boolean */ |
|
29
|
|
|
private $forceSecurity; |
|
30
|
|
|
|
|
31
|
|
|
/** @var boolean */ |
|
32
|
|
|
private $enabled; |
|
33
|
|
|
|
|
34
|
|
|
public function __construct($url, $forceSecurity, $enabled) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->url = $url; |
|
37
|
|
|
$this->forceSecurity = $forceSecurity; |
|
38
|
|
|
$this->enabled = $enabled; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param string $user |
|
43
|
|
|
* @param string $password |
|
44
|
|
|
* @return bool |
|
45
|
|
|
*/ |
|
46
|
|
|
public function checkUserCredentials($user, $password) |
|
47
|
|
|
{ |
|
48
|
|
|
// devolver error si no está habilitado |
|
49
|
|
|
if (false === $this->enabled) { |
|
50
|
|
|
return null; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
// obtener URL de entrada |
|
54
|
|
|
$str = $this->getUrl($this->url, $this->forceSecurity); |
|
55
|
|
|
if (!$str) { |
|
56
|
|
|
return null; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$dom = new \DOMDocument(); |
|
60
|
|
|
libxml_use_internal_errors(true); |
|
61
|
|
|
$dom->loadHTML($str); |
|
62
|
|
|
$xpath = new \DOMXPath($dom); |
|
63
|
|
|
$form = $xpath->query('//form')->item(0); |
|
64
|
|
|
$hidden = $xpath->query('//input[@name="N_V_"]')->item(0); |
|
65
|
|
|
|
|
66
|
|
|
if (!$form || !$hidden) { |
|
67
|
|
|
return null; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
// enviar datos del formulario |
|
71
|
|
|
$postUrl = $form->getAttribute('action'); |
|
72
|
|
|
$hiddenValue = $hidden->getAttribute('value'); |
|
73
|
|
|
|
|
74
|
|
|
$fields = array( |
|
75
|
|
|
'USUARIO' => urlencode($user), |
|
76
|
|
|
'CLAVE' => urlencode($password), |
|
77
|
|
|
'N_V_' => urlencode($hiddenValue) |
|
78
|
|
|
); |
|
79
|
|
|
|
|
80
|
|
|
$str = $this->postToUrl($fields, $postUrl, $this->url, $this->forceSecurity); |
|
81
|
|
|
|
|
82
|
|
|
if (!$str) { |
|
83
|
|
|
return null; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$dom = new \DOMDocument(); |
|
87
|
|
|
libxml_use_internal_errors(true); |
|
88
|
|
|
$dom->loadHTML($str); |
|
89
|
|
|
$xpath = new \DOMXPath($dom); |
|
90
|
|
|
$nav = $xpath->query('//nav'); |
|
91
|
|
|
$error = $xpath->query('//p[@class="text-danger"]'); |
|
92
|
|
|
|
|
93
|
|
|
return $nav->length === 1 && $error->length === 0; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Get URL contents |
|
98
|
|
|
* |
|
99
|
|
|
* @param string $url |
|
100
|
|
|
* @param boolean $forceSecurity |
|
101
|
|
|
* @return string |
|
102
|
|
|
*/ |
|
103
|
|
|
private function getUrl($url, $forceSecurity) |
|
104
|
|
|
{ |
|
105
|
|
|
$curl = curl_init(); |
|
106
|
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, $forceSecurity); |
|
107
|
|
|
curl_setopt($curl, CURLOPT_HEADER, false); |
|
108
|
|
|
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); |
|
109
|
|
|
curl_setopt($curl, CURLOPT_MAXREDIRS, 2); |
|
110
|
|
|
curl_setopt($curl, CURLOPT_URL, $url); |
|
111
|
|
|
curl_setopt($curl, CURLOPT_REFERER, $url); |
|
112
|
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); |
|
113
|
|
|
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.125 Safari/533.4"); |
|
114
|
|
|
$str = curl_exec($curl); |
|
115
|
|
|
curl_close($curl); |
|
116
|
|
|
return $str === false ? '' : (string) $str; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Gets the content after POSTing into an URL |
|
121
|
|
|
* |
|
122
|
|
|
* @param array $fields |
|
123
|
|
|
* @param string $postUrl |
|
124
|
|
|
* @param string $refererUrl |
|
125
|
|
|
* @param boolean $forceSecurity |
|
126
|
|
|
* @return string |
|
127
|
|
|
*/ |
|
128
|
|
|
private function postToUrl($fields, $postUrl, $refererUrl, $forceSecurity) |
|
129
|
|
|
{ |
|
130
|
|
|
$fieldsString = ''; |
|
131
|
|
|
foreach ($fields as $key => $value) { |
|
132
|
|
|
$fieldsString .= $key.'='.$value.'&'; |
|
133
|
|
|
} |
|
134
|
|
|
$fieldsString = rtrim($fieldsString, '&'); |
|
135
|
|
|
|
|
136
|
|
|
$curl = curl_init(); |
|
137
|
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, $forceSecurity); |
|
138
|
|
|
curl_setopt($curl, CURLOPT_HEADER, false); |
|
139
|
|
|
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); |
|
140
|
|
|
curl_setopt($curl, CURLOPT_URL, $postUrl); |
|
141
|
|
|
curl_setopt($curl, CURLOPT_REFERER, $refererUrl); |
|
142
|
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); |
|
143
|
|
|
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.125 Safari/533.4"); |
|
144
|
|
|
curl_setopt($curl, CURLOPT_POST, count($fields)); |
|
145
|
|
|
curl_setopt($curl, CURLOPT_POSTFIELDS, $fieldsString); |
|
146
|
|
|
$str = curl_exec($curl); |
|
147
|
|
|
curl_close($curl); |
|
148
|
|
|
return $str === false ? '' : (string) $str; |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|