2019-05-04 02:03:42 +02:00
< ? php
2020-09-07 15:04:06 +02:00
/*
* OpenSTAManager : il software gestionale open source per l ' assistenza tecnica e la fatturazione
2021-01-20 15:08:51 +01:00
* Copyright ( C ) DevCode s . r . l .
2020-09-07 15:04:06 +02:00
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < https :// www . gnu . org / licenses />.
*/
2019-05-04 02:03:42 +02:00
include_once __DIR__ . '/../../core.php' ;
2020-11-27 17:11:18 +01:00
use API\Services ;
use Models\Cache ;
2021-09-03 20:14:30 +02:00
use Models\Module ;
use Models\Plugin ;
2019-05-04 02:03:42 +02:00
use Util\FileSystem ;
$id = post ( 'id' );
switch ( filter ( 'op' )) {
2021-09-03 20:14:30 +02:00
case 'rimuovi-modulo' :
$id = filter ( 'id' );
$is_plugin = filter ( 'tipo' ) == 'plugin' ;
if ( empty ( $id )) {
echo json_encode ([]);
}
2019-05-04 02:03:42 +02:00
2021-09-03 20:14:30 +02:00
// Ricerca del modulo/plugin tra quelli disponibili
if ( ! $is_plugin ) {
$struttura = Module :: where ( 'default' , '=' , 0 ) -> find ( $id );
} else {
$struttura = Plugin :: where ( 'default' , '=' , 0 ) -> find ( $id );
}
2019-05-04 02:03:42 +02:00
2021-09-03 20:14:30 +02:00
// Modulo/plugin non trovato
if ( empty ( $struttura )) {
echo json_encode ([]);
}
2019-05-04 02:03:42 +02:00
2021-09-03 20:14:30 +02:00
// Eliminazione del modulo/plugin dal sistema di navigazione
$struttura -> delete ();
2019-05-04 02:03:42 +02:00
2021-09-03 20:14:30 +02:00
// Esecuzione dello script di disinstallazione (se presente)
$uninstall_script = $struttura -> path . '/update/uninstall.php' ;
if ( file_exists ( $uninstall_script )) {
include_once $uninstall_script ;
2019-05-04 02:03:42 +02:00
}
2021-09-03 20:14:30 +02:00
// Eliminazione dei file del modulo/plugin
delete ( $struttura -> path );
// Messaggio informativo
if ( ! $is_plugin ) {
flash () -> info ( tr ( 'Modulo "_NAME_" disinstallato!' , [
'_NAME_' => $struttura -> title ,
]));
} else {
flash () -> info ( tr ( 'Plugin "_NAME_" disinstallato!' , [
'_NAME_' => $struttura -> title ,
]));
}
2019-05-04 02:03:42 +02:00
2021-09-03 20:14:30 +02:00
echo json_encode ([]);
2019-05-04 02:03:42 +02:00
break ;
2021-09-03 20:14:30 +02:00
case 'disabilita-modulo' :
$id = filter ( 'id' );
$is_plugin = filter ( 'tipo' ) == 'plugin' ;
// Disabilitazione del modulo indicato
$database -> table ( $is_plugin ? 'zz_plugins' : 'zz_modules' )
-> where ( 'id' , '=' , $id )
-> update ([ 'enabled' => 0 ]);
// Cascata in tutti i sotto-moduli
if ( ! $is_plugin ) {
$moduli_interessati = collect ([ $id ]);
while ( ! $moduli_interessati -> isEmpty ()) {
$id_modulo = $moduli_interessati -> pop ();
// Disabilitazione dei sotto-moduli
$database -> table ( 'zz_modules' )
-> where ( 'parent' , '=' , $id_modulo )
-> update ([ 'enabled' => 0 ]);
// Ricerca sotto-moduli
$sotto_moduli = $database -> table ( 'zz_modules' )
-> where ( 'parent' , '=' , $id_modulo )
-> select ( 'id' )
-> get () -> pluck ( 'id' );
$moduli_interessati -> concat ( $sotto_moduli );
}
}
2019-05-04 02:03:42 +02:00
2021-09-03 20:14:30 +02:00
// Disabilitazione modulo/plugin indicato
$moduli_sempre_attivi = [ 'Utenti e permessi' , 'Stato dei servizi' ];
$database -> table ( 'zz_modules' )
-> whereIn ( 'name' , $moduli_sempre_attivi )
-> update ([ 'enabled' => 1 ]);
// Messaggio informativo
$struttura = $is_plugin ? Plugin :: find ( $id ) : Module :: find ( $id );
if ( ! $is_plugin ) {
flash () -> info ( tr ( 'Modulo "_NAME_" disabilitato!' , [
'_NAME_' => $struttura -> title ,
]));
} else {
flash () -> info ( tr ( 'Plugin "_NAME_" disabilitato!' , [
'_NAME_' => $struttura -> title ,
]));
}
echo json_encode ([]);
2019-05-04 02:03:42 +02:00
break ;
2021-09-03 20:14:30 +02:00
case 'abilita-sotto-modulo' :
$id = filter ( 'id' );
// Cascata in tutti i sotto-moduli
$moduli_interessati = collect ([ $id ]);
while ( ! $moduli_interessati -> isEmpty ()) {
$id_modulo = $moduli_interessati -> pop ();
// Disabilitazione dei sotto-moduli
$database -> table ( 'zz_modules' )
-> where ( 'parent' , '=' , $id_modulo )
-> update ([ 'enabled' => 1 ]);
// Ricerca sotto-moduli
$sotto_moduli = $database -> table ( 'zz_modules' )
-> where ( 'parent' , '=' , $id_modulo )
-> select ( 'id' )
-> get () -> pluck ( 'id' );
$moduli_interessati -> concat ( $sotto_moduli );
}
2019-05-04 02:03:42 +02:00
2021-09-03 20:14:30 +02:00
// no break
case 'abilita-modulo' :
$id = filter ( 'id' );
$is_plugin = filter ( 'tipo' ) == 'plugin' ;
// Abilitazione del modulo/plugin indicato
$database -> table ( $is_plugin ? 'zz_plugins' : 'zz_modules' )
-> where ( 'id' , '=' , $id )
-> update ([ 'enabled' => 1 ]);
// Messaggio informativo
$struttura = $is_plugin ? Plugin :: find ( $id ) : Module :: find ( $id );
if ( ! isset ( $moduli_interessati )) {
if ( ! $is_plugin ) {
flash () -> info ( tr ( 'Modulo "_NAME_" abilitato!' , [
'_NAME_' => $struttura -> title ,
]));
} else {
flash () -> info ( tr ( 'Plugin "_NAME_" abilitato!' , [
'_NAME_' => $struttura -> title ,
]));
}
} else {
2021-02-20 13:31:05 +01:00
$modulo = module ( $id );
2021-09-03 20:14:30 +02:00
flash () -> info ( tr ( 'Moduli sotto a "_NAME_" abilitati!' , [
'_NAME_' => $struttura -> title ,
]));
}
2019-05-04 02:03:42 +02:00
2021-09-03 20:14:30 +02:00
echo json_encode ([]);
2019-05-04 02:03:42 +02:00
break ;
2021-09-03 20:14:30 +02:00
case 'disabilita-widget' :
$id = filter ( 'id' );
2019-05-04 02:03:42 +02:00
2021-09-03 20:14:30 +02:00
// Abilitazione del widget indicato
$database -> table ( 'zz_widgets' )
-> where ( 'id' , '=' , $id )
-> update ([ 'enabled' => 0 ]);
2019-05-04 02:03:42 +02:00
2021-09-03 20:14:30 +02:00
// Messaggio informativo
$widget = $database -> table ( 'zz_widgets' )
-> where ( 'id' , '=' , $id )
-> first ();
flash () -> info ( tr ( 'Widget "_NAME_" disabilitato!' , [
'_NAME_' => $widget -> name ,
2019-05-04 02:03:42 +02:00
]));
2021-09-03 20:14:30 +02:00
echo json_encode ([]);
2019-05-04 02:03:42 +02:00
break ;
2021-09-03 20:14:30 +02:00
case 'abilita-widget' :
$id = filter ( 'id' );
2019-05-04 02:03:42 +02:00
2021-09-03 20:14:30 +02:00
// Abilitazione del widget indicato
$database -> table ( 'zz_widgets' )
-> where ( 'id' , '=' , $id )
-> update ([ 'enabled' => 1 ]);
2019-05-04 02:03:42 +02:00
2021-09-03 20:14:30 +02:00
// Messaggio informativo
$widget = $database -> table ( 'zz_widgets' )
-> where ( 'id' , '=' , $id )
-> first ();
flash () -> info ( tr ( 'Widget "_NAME_" abilitato!' , [
'_NAME_' => $widget -> name ,
2019-05-04 02:03:42 +02:00
]));
2021-09-03 20:14:30 +02:00
echo json_encode ([]);
2019-05-04 02:03:42 +02:00
break ;
2021-09-03 20:14:30 +02:00
case 'sposta-widget' :
$id = filter ( 'id' );
// Individuazione widget
$widget = $database -> table ( 'zz_widgets' )
-> where ( 'id' , '=' , $id )
-> first ();
if ( empty ( $widget )) {
echo json_encode ([]);
}
2019-05-04 02:03:42 +02:00
2021-09-03 20:14:30 +02:00
// Individuazione dello spostamento da effettuare
$pieces = explode ( '_' , $widget -> location );
$location = $pieces [ 0 ] . '_' . ( $pieces [ 1 ] == 'right' ? 'top' : 'right' );
2019-05-04 02:03:42 +02:00
2021-09-03 20:14:30 +02:00
// Abilitazione del widget indicato
$database -> table ( 'zz_widgets' )
-> where ( 'id' , '=' , $id )
-> update ([ 'location' => $location ]);
// Messaggio informativo
flash () -> info ( tr ( 'Posizione del widget "_NAME_" aggiornata!' , [
'_NAME_' => $widget -> name ,
2019-05-04 02:03:42 +02:00
]));
2021-09-03 20:14:30 +02:00
echo json_encode ([]);
2019-05-04 02:03:42 +02:00
break ;
// Ordinamento moduli di primo livello
2019-07-26 17:40:52 +02:00
case 'sort_modules' :
2021-07-20 20:48:02 +02:00
$order = explode ( ',' , post ( 'order' , true ));
2019-05-04 02:03:42 +02:00
2021-07-20 20:48:02 +02:00
foreach ( $order as $i => $id ) {
$dbo -> query ( 'UPDATE zz_modules SET `order`=' . prepare ( $i ) . ' WHERE id=' . prepare ( $id ));
2019-05-04 02:03:42 +02:00
}
break ;
2019-07-26 17:40:52 +02:00
case 'sort_widgets' :
2021-07-20 20:48:02 +02:00
$order = explode ( ',' , post ( 'order' , true ));
2019-05-04 02:03:42 +02:00
2021-07-20 20:48:02 +02:00
foreach ( $order as $i => $id ) {
$dbo -> query ( 'UPDATE zz_widgets SET `order`=' . prepare ( $i ) . ' WHERE id=' . prepare ( $id ));
2019-05-04 02:03:42 +02:00
}
2021-07-20 20:48:02 +02:00
2019-05-04 02:03:42 +02:00
break ;
case 'sizes' :
$results = [];
2020-12-31 16:13:28 +01:00
$backup_dir = AppLegacy :: getConfig ()[ 'backup_dir' ];
2019-05-04 02:03:42 +02:00
$dirs = [
$backup_dir => tr ( 'Backup' ),
2020-09-23 13:36:37 +02:00
base_dir () . '/files' => tr ( 'Allegati' ),
base_dir () . '/logs' => tr ( 'Logs' ),
2019-05-04 02:03:42 +02:00
];
foreach ( $dirs as $dir => $description ) {
2020-06-26 16:26:42 +02:00
$size = FileSystem :: folderSize ( $dir , [ 'htaccess' ]);
2019-07-19 15:23:00 +02:00
2019-05-04 02:03:42 +02:00
$results [] = [
'description' => $description ,
'size' => $size ,
'formattedSize' => FileSystem :: formatBytes ( $size ),
2020-08-31 16:30:54 +02:00
'count' => FileSystem :: fileCount ( $dir , [ 'htaccess' ]) ? : 0 ,
'dbSize' => ( $description == 'Allegati' ) ? $dbo -> fetchOne ( 'SELECT SUM(`size`) AS dbsize FROM zz_files' )[ 'dbsize' ] : '' ,
'dbCount' => ( $description == 'Allegati' ) ? $dbo -> fetchOne ( 'SELECT COUNT(`id`) AS dbcount FROM zz_files' )[ 'dbcount' ] : '' ,
'dbExtensions' => ( $description == 'Allegati' ) ? $dbo -> fetchArray ( " SELECT SUBSTRING_INDEX(filename, '.', -1) AS extension, COUNT(*) AS num FROM zz_files GROUP BY extension ORDER BY num DESC LIMIT 10 " ) : '' ,
2019-05-04 02:03:42 +02:00
];
}
echo json_encode ( $results );
break ;
2020-11-27 17:11:18 +01:00
case 'informazioni-fe' :
$info = Cache :: pool ( 'Informazioni su spazio FE' );
if ( ! $info -> isValid ()) {
$response = Services :: request ( 'POST' , 'informazioni_fe' );
$response = Services :: responseBody ( $response );
$info -> set ( $response [ 'result' ]);
}
$informazioni = $info -> content ;
2021-09-17 17:18:17 +02:00
$spazio_totale = floatval ( $informazioni [ 'maxSize' ]) * ( 1024 ** 2 );
$avviso_spazio = ! empty ( $spazio_totale ) && floatval ( $informazioni [ 'size' ]) > 0.9 * $spazio_totale ;
2021-09-03 20:14:30 +02:00
// Restrizione storico agli ultimi 3 anni
2021-03-01 16:41:52 +01:00
$history = ( array ) $informazioni [ 'history' ];
2021-09-03 20:14:30 +02:00
$history = array_slice ( $history , 0 , 3 );
2021-10-11 19:16:56 +02:00
$max_number = $informazioni [ 'maxNumber' ];
$avviso_numero = ! empty ( $max_number ) && floatval ( $history [ 0 ][ 'number' ]) > 0.9 * $max_number ;
2021-09-03 20:14:30 +02:00
// Formattazione dei contenuti dello storico
2021-03-01 16:41:52 +01:00
foreach ( $history as $key => $value ) {
2021-10-18 10:23:39 +02:00
$history [ $key ][ 'size' ] = (( $history [ $key ][ 'size' ]) ? Filesystem :: formatBytes ( $value [ 'size' ]) : '-' );
2021-10-11 19:16:56 +02:00
//$history[$key]['invoices_size'] = Filesystem::formatBytes($value['invoices_size']);
//$history[$key]['notifies_size'] = Filesystem::formatBytes($value['notifies_size']);
2021-03-01 16:41:52 +01:00
}
2021-09-03 20:14:30 +02:00
// Formattazione dei contenuti generici
2020-11-27 17:11:18 +01:00
echo json_encode ([
2021-10-11 19:16:56 +02:00
//'invoices_size' => Filesystem::formatBytes($informazioni['invoices_size']),
//'notifies_size' => Filesystem::formatBytes($informazioni['notifies_size']),
2021-09-17 17:18:17 +02:00
2021-10-11 19:16:56 +02:00
'invoice_number' => $informazioni [ 'invoice_number' ],
'maxNumber' => $max_number ,
'avviso_numero' => $avviso_numero ,
2021-09-17 17:18:17 +02:00
'avviso_spazio' => $avviso_spazio ,
'spazio_totale' => Filesystem :: formatBytes ( $spazio_totale ),
'spazio_occupato' => Filesystem :: formatBytes ( $informazioni [ 'size' ]),
2021-03-01 16:47:01 +01:00
'history' => $history ,
2020-11-27 17:11:18 +01:00
]);
break ;
2019-05-04 02:03:42 +02:00
}