GlisWeb framework
_210.auth.php
Vai alla documentazione di questo file.
1 <?php
2 
105  // libreria
106  // use \Firebase\JWT\JWT;
107 
108  // debug
109  // print_r( $_SESSION );
110 
111  // stato del login
112  $cf['auth']['status'] = NULL;
113 
114  // intercetto eventuali richieste di autenticazione HTTP
115  if( ! empty( $_SERVER['PHP_AUTH_USER'] ) && ! empty( $_SERVER['PHP_AUTH_PW'] ) ) {
116  $_REQUEST['__login__']['user'] = $_SERVER['PHP_AUTH_USER'];
117  $_REQUEST['__login__']['pasw'] = $_SERVER['PHP_AUTH_PW'];
118  }
119 
120  // intercetto eventuali tentativi di login in corso
121  if( isset( $_REQUEST['__login__'] ) && is_array( $_REQUEST['__login__'] ) ) {
122 
123  // log
124  logWrite( 'tentativo di login in corso per ' . $_REQUEST['__login__']['user'], 'auth', LOG_INFO );
125 
126  // verifico che sia stato inviato un modulo di login compilato
127  if( isset( $_REQUEST['__login__']['user'] ) && isset( $_REQUEST['__login__']['pasw'] ) ) {
128 
129  // ricalcolo la password
130  $_REQUEST['__login__']['pasw'] = md5( $_REQUEST['__login__']['pasw'] );
131 
132  // login con gli utenti del framework
133  if( in_array( $_REQUEST['__login__']['user'], array_keys( $cf['auth']['accounts'] ) ) ) {
134 
135  if( $cf['auth']['accounts'][ $_REQUEST['__login__']['user'] ]['password'] == $_REQUEST['__login__']['pasw'] ) {
136 
137  // attribuzione dell'account
138  $_SESSION['account'] = &$cf['auth']['accounts'][ $_REQUEST['__login__']['user'] ];
139 
140  // attribuzione dei gruppi e dei privilegi di gruppo
141  foreach( $_SESSION['account']['gruppi'] as $gr ) {
142  $_SESSION['groups'][ $gr ] = &$cf['auth']['groups'][ $gr ];
143  if( isset( $cf['auth']['groups'][ $gr ]['privilegi'] ) ) {
144  foreach( $cf['auth']['groups'][ $gr ]['privilegi'] as $pr ) {
145  if( ! in_array( $pr, $_SESSION['account']['privilegi'] ) ) {
146  $_SESSION['account']['privilegi'][] = $pr;
147  }
148  }
149  }
150  }
151 
152  // attribuzione dei privilegi utente
153  foreach( $_SESSION['account']['privilegi'] as $pr ) {
154  $_SESSION['privilegi'][ $pr ] = &$cf['auth']['privileges'][ $pr ];
155  }
156 
157  // debug
158  // print_r( $_SESSION['privilegi'] );
159 
160  // status
161  $cf['auth']['status'] = LOGIN_SUCCESS;
162 
163  // log
164  logWrite( 'login effettuato correttamente per ' . $_REQUEST['__login__']['user'], 'auth', LOG_DEBUG );
165 
166  } else {
167 
168  // status
169  $cf['auth']['status'] = LOGIN_ERR_WRONG_PW;
170 
171  // log
172  logWrite( 'password errata per ' . $_REQUEST['__login__']['user'], 'auth', LOG_ERR );
173  logWrite( 'mancata corrispondenza degli hash per ' . $_REQUEST['__login__']['user'] . ': ' . $cf['auth']['accounts'][ $_REQUEST['__login__']['user'] ]['password'] . '/' . $_REQUEST['__login__']['pasw'], 'auth', LOG_ERR );
174 
175  }
176 
177  } else {
178 
179  // status
180  $cf['auth']['status'] = LOGIN_ERR_NO_USER;
181 
182  // log
183  logWrite( $_REQUEST['__login__']['user'] . ' non presente nei file di configurazione', 'auth', LOG_INFO );
184 
185  }
186 
187  // speed
188  timerCheck( $cf['speed'], ' -> fine login via framework' );
189 
190  // debug
191  // echo 'status: ' . $cf['auth']['status'] . '/' . LOGIN_SUCCESS . PHP_EOL;
192 
193  // login su database MySQL
194  if( $cf['auth']['status'] != LOGIN_SUCCESS ) {
195  if( ! empty( $cf['mysql']['connection'] ) ) {
196 
197  // log
198  logWrite( 'fallback al login su database', 'auth', LOG_DEBUG );
199 
200  // query di login
201  $r = mysqlSelectRow(
202  $cf['mysql']['connection'],
203  'SELECT * FROM account_view WHERE username = ? AND password = ?',
204  array(
205  array( 's' => $_REQUEST['__login__']['user'] ),
206  array( 's' => $_REQUEST['__login__']['pasw'] )
207  )
208  );
209 
210  // controllo il risultato
211  if( count( $r ) && $r['se_attivo'] == true ) {
212 
213  // verifica se si tratta del primo accesso e in tal caso valorizza il campo che andrĂ  in $_SESSION['account']['first_access']
214  if( is_null($r['timestamp_login']) ){ $r['first_access'] = true;
215  } else { $r['last_access'] = $r['timestamp_login']; }
216 
217  // TODO CHIARA
218  // qui aggiornare la colonna timestamp_login dell'account connesso
219  $login = mysqlQuery($cf['mysql']['connection'],
220  'UPDATE account SET timestamp_login = ? WHERE id = ? ',
221  array(
222  array( 's' => time() ),
223  array( 's' => $r['id'] ) ) );
224 
225  $_SESSION['account'] = $r;
226  string2array( $_SESSION['account']['gruppi'] );
227  string2array( $_SESSION['account']['id_gruppi'] );
228  string2array( $_SESSION['account']['categorie'] );
229  string2array( $_SESSION['account']['id_categorie'] );
230 
231  $_SESSION['account']['permissions'] = array();
232 
233  foreach( $_SESSION['account']['id_gruppi'] as $g ) {
235  $cf['memcache']['connection'],
236  $cf['mysql']['connection'],
237  'SELECT * FROM gruppi_view WHERE id = ?',
238  array( array( 's' => $g ) )
239  );
240  if( isset( $cf['auth']['groups'][ $r['nome'] ] ) ) {
241  $cf['auth']['groups'][ $r['nome'] ] = array_replace_recursive( $r, $cf['auth']['groups'][ $r['nome'] ] );
242  } else {
243  $cf['auth']['groups'][ $r['nome'] ] = $r;
244  }
245  $_SESSION['groups'][ $r['nome'] ] = &$cf['auth']['groups'][ $r['nome'] ];
246  }
247 
248  // dati anagrafici
249  $_SESSION['account'] = array_replace_recursive( $_SESSION['account'], mysqlSelectRow(
250  $cf['mysql']['connection'],
251  'SELECT se_collaboratore, se_cliente, se_fornitore, se_agente, se_amministrazione FROM anagrafica_view WHERE id = ?',
252  array( array( 's' => $_SESSION['account']['id_anagrafica'] ) )
253  ) );
254 
255  // attribuzione dei gruppi e dei privilegi di gruppo
256  foreach( $_SESSION['groups'] as $gr ) {
257  if( isset( $cf['auth']['groups'][ $gr['nome'] ]['privilegi'] ) ) {
258  foreach( $cf['auth']['groups'][ $gr['nome'] ]['privilegi'] as $pr ) {
259  if( ! isset( $_SESSION['account']['privilegi'] ) || ! in_array( $pr, $_SESSION['account']['privilegi'] ) ) {
260  $_SESSION['privilegi'][] = $pr;
261  }
262  }
263  }
264  }
265 
266  // gruppi di attribuzione automatica dell'utente
267  if( ! empty( $_SESSION['account']['id_gruppi_attribuzione'] ) ) {
268  $aGroups = explode( '|', $_SESSION['account']['id_gruppi_attribuzione'] );
269  $_SESSION['account']['id_gruppi_attribuzione'] = array();
270  foreach( $aGroups as $aGroup ) {
271  $aGrData = explode( '#', $aGroup );
272  $_SESSION['account']['id_gruppi_attribuzione'][ $aGrData[0] ][] = $aGrData[1];
273  }
274  }
275 
276  // debug
277  // print_r( $_SESSION );
278 
279 /*
280  // attribuzione dei privilegi utente
281  foreach( $_SESSION['account']['privilegi'] as $pr ) {
282  $_SESSION['privilegi'][ $pr ] = &$cf['auth']['privileges'][ $pr ];
283  }
284 */
285 // $_SESSION['account']['gruppi'] =
286 
287 /*
288  $_SESSION['account'] = array(
289  'id' => $rsLogin[0]['id'],
290  'user' => array(
291  'id' => $rsLogin[0]['id_anagrafica'],
292  'nome' => $rsLogin[0]['nome'],
293  'cognome' => $rsLogin[0]['cognome'],
294  'denominazione' => $rsLogin[0]['denominazione'],
295  ),
296  'username' => $rsLogin[0]['username'],
297  'password' => $rsLogin[0]['password'],
298  'groups' => array(
299  'name' => array_map( 'trim', explode( ',', $rsLogin[0]['gruppi'] ) ),
300  'id' => array_map( 'trim', explode( ',', $rsLogin[0]['id_gruppi'] ) )
301  )
302  );
303  $_SESSION['account']['logged'] = true;
304  $_SESSION['groups'] = &$_SESSION['account']['groups'];
305 */
306  // status
307  $cf['auth']['status'] = LOGIN_SUCCESS;
308 
309  // log
310  logWrite( 'login effettuato correttamente via database per ' . $_REQUEST['__login__']['user'], 'auth', LOG_DEBUG );
311 
312  } elseif( count( $r ) && $r['se_attivo'] != true ) {
313 
314  // status
315  $cf['auth']['status'] = LOGIN_ERR_INACTIVE;
316 
317  // log
318  logWrite( 'UTENTE INATTIVO, impossibile effettuare il login via database per ' . $_REQUEST['__login__']['user'] . '/' . $_REQUEST['__login__']['pasw'], 'auth', LOG_INFO );
319 
320  } else {
321 
322  // status
323  $cf['auth']['status'] = LOGIN_ERR_WRONG_PW;
324 
325  // log
326  logWrite( 'impossibile effettuare il login via database per ' . $_REQUEST['__login__']['user'] . '/' . $_REQUEST['__login__']['pasw'], 'auth', LOG_ERR );
327 /*
328 TODO il login custom va semplicemente definito nei file locali
329  // log
330  logFactory( 'fallback al login con factory custom', 'auth', LOG_DEBUG );
331 
332  // ricerca factory custom
333  $factoryCustomStandard = glob( DIRECTORY_BASE . '_src/_bin/_auth.*.php' );
334  $factoryCustomLocali = glob( DIRECTORY_BASE . 'src/bin/auth.*.php' );
335  $factoryCustom = array_merge( $factoryCustomStandard , $factoryCustomLocali );
336 
337  // tentativo di login
338  foreach( $factoryCustom as $factory ) {
339  if( ! isset( $_SESSION['account'] ) || $_SESSION['account']['userId'] == NULL ) {
340  require $factory;
341  }
342  }
343 */
344  }
345 
346  } else {
347 
348  // status
349  $cf['auth']['status'] = LOGIN_ERR_NO_CONNECTION;
350 
351  // log
352  logWrite( 'login sul database per ' . $_REQUEST['__login__']['user'] . ' impossibile per mancanza di connessione', 'auth', LOG_DEBUG );
353 
354  }
355 
356  }
357 
358  // speed
359  timerCheck( $cf['speed'], ' -> fine login via database' );
360 
361  } else {
362 
363  // status
364  $cf['auth']['status'] = LOGIN_ERR_NO_DATA;
365 
366  // log
367  logWrite( 'dati di login insufficienti per ' . $_REQUEST['__login__']['user'], 'auth', LOG_DEBUG );
368 
369  }
370 
371 # // elimino il blocco dati di login
372 # unset( $_REQUEST['login'] );
373 
374  }
375 /*
376  // autenticazione JWT
377  if( $cf['auth']['status'] == LOGIN_SUCCESS && $cf['auth']['jwt']['secret'] ) {
378 
379  // creazione token
380  $cf['auth']['jwt']['token'] = JWT::encode(
381  $cf['auth'],
382  base64_decode(
383  strtr( $cf['auth']['jwt']['secret'], '-_', '+/' )
384  ),
385  'HS256'
386  );
387 
388  // debug
389  // echo 'JWT: ' . print_r( $cf['auth']['jwt']['token'], true );
390 
391  } else {
392 
393  // debug
394  // print_r( $cf['auth'] );
395  // echo 'duh?';
396 
397  }
398 */
399  // debug
400  // print_r( $cf['localization']['language'] );
401  // print_r( $_SESSION );
402 
403 ?>
const LOGIN_ERR_NO_DATA
Definition: _200.auth.php:65
mysqlSelectRow( $c, $q, $p=false, &$e=array())
string2array(&$s, $c=ARRAY_SEPARATOR)
$cf['auth']['status']
Definition: _210.auth.php:112
logWrite( $m, $f='site', $l=LOG_NOTICE, $d=DIRECTORY_LOG, $t=CURRENT_LOG_LEVEL, $s=SITE_STATUS)
scrive un messaggio nei log del sito
Definition: _log.utils.php:48
const LOGIN_ERR_NO_USER
Definition: _200.auth.php:67
mysqlQuery( $c, $q, $p=false, &$e=array())
const LOGIN_ERR_NO_CONNECTION
Definition: _200.auth.php:66
$r
Definition: _osm.php:25
if(isset( $_REQUEST['amazonCheckoutSessionId'])) $_SESSION['carrello']
if(! isset( $_REQUEST['__view__'][ $ct['view']['id']]['__extra__']['assegnato'])|| $_REQUEST['__view__'][ $ct['view']['id']]['__extra__']['assegnato']=='__me__') elseif($_REQUEST[ '__view__'][$ct[ 'view'][ 'id']][ '__extra__'][ 'assegnato']=='__nessuno__')
const LOGIN_SUCCESS
Definition: _200.auth.php:70
mysqlSelectCachedRow( $m, $c, $q, $p=false, $t=MEMCACHE_DEFAULT_TTL, &$e=array())
timerCheck(&$a, $c)
const LOGIN_ERR_INACTIVE
Definition: _200.auth.php:69
const LOGIN_ERR_WRONG_PW
Definition: _200.auth.php:68
$_REQUEST['__view__'][ $ct['view']['id']]['__restrict__']['id_progetto']['EQ']