@@ -996,4 +996,130 @@ private function checkTokenValidity($token_record)
996996
997997 return $ is_not_active ;
998998 }
999+
1000+ /**
1001+ * Memorizza l'URL di destinazione nella sessione per il redirect post-login.
1002+ *
1003+ * @param string $url URL di destinazione
1004+ */
1005+ public function setIntendedUrl ($ url )
1006+ {
1007+ if ($ this ->isValidInternalUrl ($ url )) {
1008+ $ _SESSION ['intended_url ' ] = $ url ;
1009+ }
1010+ }
1011+
1012+ /**
1013+ * Recupera l'URL di destinazione dalla sessione.
1014+ *
1015+ * @return string|null
1016+ */
1017+ public function getIntendedUrl ()
1018+ {
1019+ return $ _SESSION ['intended_url ' ] ?? null ;
1020+ }
1021+
1022+ /**
1023+ * Verifica se esiste un URL di destinazione memorizzato.
1024+ *
1025+ * @return bool
1026+ */
1027+ public function hasIntendedUrl ()
1028+ {
1029+ return !empty ($ _SESSION ['intended_url ' ]);
1030+ }
1031+
1032+ /**
1033+ * Pulisce l'URL di destinazione dalla sessione.
1034+ */
1035+ public function clearIntendedUrl ()
1036+ {
1037+ unset($ _SESSION ['intended_url ' ]);
1038+ }
1039+
1040+ /**
1041+ * Verifica se l'utente ha i permessi per accedere all'URL intended.
1042+ *
1043+ * @return bool
1044+ */
1045+ public function canAccessIntendedUrl ()
1046+ {
1047+ if (!$ this ->hasIntendedUrl ()) {
1048+ return false ;
1049+ }
1050+
1051+ $ url = $ this ->getIntendedUrl ();
1052+
1053+ // Estrae l'id_module dall'URL
1054+ if (preg_match ('/[?&]id_module=(\d+)/ ' , $ url , $ matches )) {
1055+ $ id_module = $ matches [1 ];
1056+
1057+ // Verifica i permessi per il modulo
1058+ $ permission = \Modules::getPermission ($ id_module );
1059+ return in_array ($ permission , ['r ' , 'rw ' ]);
1060+ }
1061+
1062+ return true ; // Per URL senza modulo specifico
1063+ }
1064+
1065+ /**
1066+ * Valida che l'URL sia interno al sistema e sicuro.
1067+ *
1068+ * @param string $url URL da validare
1069+ *
1070+ * @return bool
1071+ */
1072+ private function isValidInternalUrl ($ url )
1073+ {
1074+ if (empty ($ url )) {
1075+ return false ;
1076+ }
1077+
1078+ // Verifica che non contenga protocolli pericolosi
1079+ if (strpos ($ url , 'javascript: ' ) !== false || strpos ($ url , 'data: ' ) !== false ) {
1080+ return false ;
1081+ }
1082+
1083+ // Verifica che non sia un URL esterno (con protocollo http/https)
1084+ if (strpos ($ url , 'http:// ' ) === 0 || strpos ($ url , 'https:// ' ) === 0 ) {
1085+ return false ;
1086+ }
1087+
1088+ $ base_path = base_path ();
1089+
1090+ // L'URL deve iniziare con il base_path del sistema o essere relativo
1091+ if (strpos ($ url , $ base_path ) === 0 || strpos ($ url , '/ ' ) === 0 ) {
1092+ return true ;
1093+ }
1094+
1095+ return false ;
1096+ }
1097+
1098+ /**
1099+ * Metodi statici per l'accesso ai metodi di intended URL.
1100+ */
1101+ public static function setIntended ($ url )
1102+ {
1103+ return self ::getInstance ()->setIntendedUrl ($ url );
1104+ }
1105+
1106+ public static function getIntended ()
1107+ {
1108+ return self ::getInstance ()->getIntendedUrl ();
1109+ }
1110+
1111+ public static function hasIntended ()
1112+ {
1113+ return self ::getInstance ()->hasIntendedUrl ();
1114+ }
1115+
1116+ public static function clearIntended ()
1117+ {
1118+ return self ::getInstance ()->clearIntendedUrl ();
1119+ }
1120+
1121+ public static function canAccessIntended ()
1122+ {
1123+ return self ::getInstance ()->canAccessIntendedUrl ();
1124+ }
9991125}
0 commit comments