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 ;
2023-08-04 14:54:28 +02:00
use Carbon\Carbon ;
2020-11-27 17:11:18 +01:00
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' )) {
2023-03-09 18:12:38 +01:00
case 'cambia-dimensione' :
$result = $dbo -> update ( 'zz_widgets' , [
2023-08-04 14:54:28 +02:00
'class' => post ( 'valore' ),
2023-03-09 18:12:38 +01:00
], [
2023-08-04 14:54:28 +02:00
'id' => post ( 'id' ),
2023-03-09 18:12:38 +01:00
]);
2023-08-04 14:54:28 +02:00
2023-03-09 18:12:38 +01:00
echo json_encode ([
'result' => $result ,
]);
if ( $result ) {
flash () -> info ( 'Impostazione modificata con successo!' );
} else {
flash () -> error ( 'Errore durante il salvataggio!' );
}
break ;
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!' , [
2024-03-20 11:13:28 +01:00
'_NAME_' => $struttura -> getTranslation ( 'title' ),
2021-09-03 20:14:30 +02:00
]));
} else {
flash () -> info ( tr ( 'Plugin "_NAME_" disinstallato!' , [
2024-03-20 11:13:28 +01:00
'_NAME_' => $struttura -> getTranslation ( 'title' ),
2021-09-03 20:14:30 +02:00
]));
}
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
2024-03-27 16:59:46 +01:00
$moduli_sempre_attivi = [( new Module ()) -> getByField ( 'name' , 'Utenti e permessi' , Models\Locale :: getPredefined () -> id ) -> id_record , ( new Module ()) -> getByField ( 'name' , 'Stato dei servizi' , Models\Locale :: getPredefined () -> id )];
2021-09-03 20:14:30 +02:00
$database -> table ( 'zz_modules' )
2024-03-05 16:01:45 +01:00
-> whereIn ( 'id' , $moduli_sempre_attivi )
2021-09-03 20:14:30 +02:00
-> update ([ 'enabled' => 1 ]);
// Messaggio informativo
$struttura = $is_plugin ? Plugin :: find ( $id ) : Module :: find ( $id );
if ( ! $is_plugin ) {
flash () -> info ( tr ( 'Modulo "_NAME_" disabilitato!' , [
2024-03-20 11:13:28 +01:00
'_NAME_' => $struttura -> getTranslation ( 'title' ),
2021-09-03 20:14:30 +02:00
]));
} else {
flash () -> info ( tr ( 'Plugin "_NAME_" disabilitato!' , [
2024-03-20 11:13:28 +01:00
'_NAME_' => $struttura -> getTranslation ( 'title' ),
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-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!' , [
2024-03-20 11:13:28 +01:00
'_NAME_' => $struttura -> getTranslation ( 'title' ),
2021-09-03 20:14:30 +02:00
]));
} else {
flash () -> info ( tr ( 'Plugin "_NAME_" abilitato!' , [
2024-03-20 11:13:28 +01:00
'_NAME_' => $struttura -> getTranslation ( 'title' ),
2021-09-03 20:14:30 +02:00
]));
}
} else {
2024-03-05 16:01:45 +01:00
$modulo = Module :: find ( $id );
2021-09-03 20:14:30 +02:00
flash () -> info ( tr ( 'Moduli sotto a "_NAME_" abilitati!' , [
2024-03-20 11:13:28 +01:00
'_NAME_' => $struttura -> getTranslation ( 'title' ),
2021-09-03 20:14:30 +02:00
]));
}
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!' , [
2024-03-20 11:13:28 +01:00
'_NAME_' => $widget -> getTranslation ( '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!' , [
2024-03-20 11:13:28 +01:00
'_NAME_' => $widget -> getTranslation ( '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!' , [
2024-03-20 11:13:28 +01:00
'_NAME_' => $widget -> getTranslation ( '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 ;
2024-01-15 15:30:45 +01:00
// 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 ) {
2024-03-05 16:01:45 +01:00
$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 ;
2023-01-25 19:29:38 +01:00
case 'svuota-cache-hooks' :
// Svuota cache hooks
$database -> table ( 'zz_cache' )
-> update ([ 'expire_at' => Carbon :: now () -> subMinutes ( 1 )]);
// Messaggio informativo
flash () -> info ( tr ( 'Cache hooks svuotata!' , []));
echo json_encode ([]);
break ;
2023-08-04 14:54:28 +02:00
2022-10-06 17:08:18 +02:00
case 'disabilita-hook' :
$id = filter ( 'id' );
// Abilitazione del widget indicato
$database -> table ( 'zz_hooks' )
-> where ( 'id' , '=' , $id )
-> update ([ 'enabled' => 0 ]);
// Messaggio informativo
$hook = $database -> table ( 'zz_hooks' )
-> where ( 'id' , '=' , $id )
-> first ();
flash () -> info ( tr ( 'Hook "_NAME_" disabilitato!' , [
2024-03-20 11:13:28 +01:00
'_NAME_' => $hook -> getTranslation ( 'name' ),
2022-10-06 17:08:18 +02:00
]));
echo json_encode ([]);
break ;
2023-08-04 14:54:28 +02:00
2022-10-06 17:08:18 +02:00
case 'abilita-hook' :
$id = filter ( 'id' );
// Abilitazione del widget indicato
$database -> table ( 'zz_hooks' )
-> where ( 'id' , '=' , $id )
-> update ([ 'enabled' => 1 ]);
// Messaggio informativo
$hook = $database -> table ( 'zz_hooks' )
-> where ( 'id' , '=' , $id )
-> first ();
flash () -> info ( tr ( 'Hook "_NAME_" abilitato!' , [
2024-03-20 11:13:28 +01:00
'_NAME_' => $hook -> getTranslation ( 'name' ),
2022-10-06 17:08:18 +02:00
]));
echo json_encode ([]);
break ;
2019-05-04 02:03:42 +02:00
case 'sizes' :
$results = [];
$backup_dir = App :: getConfig ()[ 'backup_dir' ];
$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 ) {
2023-08-04 14:54:28 +02:00
$excluded_extensions = [ 'htaccess' , 'gitkeep' ];
2024-01-15 15:30:45 +01:00
// Tutte le cartelle che non prevedono log in zz_files
2022-10-07 12:01:13 +02:00
$excluded_dir = [ DOCROOT . '\files\impianti' , DOCROOT . '\files\importFE' , DOCROOT . '\files\exportFE' , DOCROOT . '\files\receiptFE' , DOCROOT . '\files\temp' ];
2023-08-04 14:54:28 +02:00
$size = FileSystem :: folderSize ( $dir , array_merge ( $excluded_extensions , $excluded_dir ));
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 ),
2023-08-04 14:54:28 +02:00
'count' => FileSystem :: fileCount ( $dir , array_merge ( $excluded_extensions , $excluded_dir )) ? : 0 ,
2022-10-06 13:30:07 +02:00
'dbSize' => ( $description == 'Allegati' ) ? $dbo -> fetchOne ( 'SELECT SUM(`size`) AS dbsize FROM zz_files' )[ 'dbsize' ] : 0 ,
'dbCount' => ( $description == 'Allegati' ) ? $dbo -> fetchOne ( 'SELECT COUNT(`id`) AS dbcount FROM zz_files' )[ 'dbcount' ] : 0 ,
'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 " ) : 0 ,
2019-05-04 02:03:42 +02:00
];
}
echo json_encode ( $results );
break ;
2020-11-27 17:11:18 +01:00
case 'informazioni-fe' :
2024-03-27 16:59:46 +01:00
$info = Cache :: find (( new Cache ()) -> getByField ( 'name' , 'Informazioni su spazio FE' , Models\Locale :: getPredefined () -> id ));
2020-11-27 17:11:18 +01:00
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 ) {
2024-01-31 14:23:46 +01:00
$history [ $key ][ 'size' ] = (( $history [ $key ][ 'size' ]) ? FileSystem :: formatBytes ( $value [ 'size' ]) : '-' );
2024-01-15 15:30:45 +01: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 ([
2024-01-15 15:30:45 +01: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 ,
2024-01-31 14:23:46 +01:00
'spazio_totale' => FileSystem :: formatBytes ( $spazio_totale ),
'spazio_occupato' => FileSystem :: formatBytes ( $informazioni [ 'size' ]),
2021-09-17 17:18:17 +02:00
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
}