2020-11-29 01:55:31 +01:00
< ? php
// This file runs maintenance scripts and should be executed by cron regularly
2023-09-04 15:06:43 +02:00
include " config.inc.php " ;
include " newsportal.php " ;
include $config_dir . " /scripts/rslight-lib.php " ;
include $config_dir . " /gpg.conf " ;
2020-11-29 01:55:31 +01:00
2023-09-10 15:29:34 +02:00
$pid = getmypid ();
$logfile = $logdir . '/cron.log' ;
2023-09-17 14:56:24 +02:00
if ( file_exists ( $config_dir . '/cron.disable' ) || file_exists ( $spooldir . '/cron.disable' )) {
2023-09-17 15:27:37 +02:00
file_put_contents ( $logfile , " \n " . date ( 'M d H:i:s' ) . " " . $config_name . " cron.php disabled by semaphore: cron.disable Exiting... " , FILE_APPEND );
2023-09-10 15:29:34 +02:00
chown ( $logfile , $CONFIG [ 'webserver_user' ]);
exit ();
} else {
2024-06-11 12:19:26 +02:00
file_put_contents ( $logfile , " \n " . date ( 'M d H:i:s' ) . " " . $config_name . " cron " . $pid . " started... " , FILE_APPEND );
2023-09-10 15:29:34 +02:00
chown ( $logfile , $CONFIG [ 'webserver_user' ]);
2023-09-04 15:06:43 +02:00
}
2024-10-17 15:13:09 +02:00
$menulist = get_section_menu_array ();
2020-11-29 01:55:31 +01:00
# Start or verify NNTP server
2023-09-04 15:06:43 +02:00
if ( isset ( $CONFIG [ 'enable_nntp' ]) && $CONFIG [ 'enable_nntp' ] == true ) {
2020-11-29 01:55:31 +01:00
# Create group list for nntp.php
2023-09-04 15:06:43 +02:00
$fp1 = $spooldir . " / " . $config_name . " /groups.txt " ;
2023-04-14 03:52:30 +02:00
unlink ( $fp1 );
touch ( $fp1 );
2024-10-14 12:33:43 +02:00
$group_exists = array ();
2023-09-04 15:06:43 +02:00
foreach ( $menulist as $menu ) {
$menuitem = explode ( ':' , $menu );
if ( $menuitem [ 2 ] == '1' ) {
$in_gl = file ( $config_dir . $menuitem [ 0 ] . " /groups.txt " );
foreach ( $in_gl as $ok_group ) {
if (( $ok_group [ 0 ] == ':' ) || ( trim ( $ok_group ) == " " )) {
continue ;
}
2024-10-14 12:33:43 +02:00
$entry = preg_split ( " /( | \t )/ " , trim ( $ok_group ), 2 );
if ( ! isset ( $group_exists [ $entry [ 0 ]])) {
file_put_contents ( $fp1 , $entry [ 0 ] . " \n " , FILE_APPEND );
}
$group_exists [ $entry [ 0 ]] = true ;
2023-09-04 15:06:43 +02:00
}
}
2020-11-29 01:55:31 +01:00
}
2024-06-11 12:19:26 +02:00
2023-12-12 13:55:31 +01:00
$disabled_php = ini_get ( 'disable_functions' );
echo $disabled_php ;
2024-06-11 12:19:26 +02:00
if ( strpos ( $disabled_php , 'pcntl_fork' ) !== false ) {
2023-12-12 13:55:31 +01:00
echo " \n ERROR: pcntl_fork() disabled in php ini file, cannot fork (nntp server will not start). " ;
file_put_contents ( $logfile , " \n " . format_log_date () . " ERROR: pcntl_fork() disabled in php ini file, cannot fork (nntp server will not start). " , FILE_APPEND );
} else {
exec ( $CONFIG [ 'php_exec' ] . " " . $config_dir . " /scripts/nntp.php > /dev/null 2>&1 " );
if ( is_numeric ( $CONFIG [ 'local_ssl_port' ])) {
2024-06-11 12:19:26 +02:00
exec ( $CONFIG [ 'php_exec' ] . " " . $config_dir . " /scripts/nntp-ssl.php > /dev/null 2>&1 " );
2023-12-12 13:55:31 +01:00
}
2020-11-29 01:55:31 +01:00
}
2023-09-04 15:06:43 +02:00
}
2024-07-01 11:12:12 +02:00
// Create paths in $config_dir/scripts path file
$config_path_file = $config_dir . '/scripts/paths.inc.php' ;
2024-10-06 15:07:43 +02:00
if ( ! file_exists ( $config_path_file )) {
file_put_contents ( $config_path_file , '<?php' . " \n " );
file_put_contents ( $config_path_file , '$spoolnews_path = "' . getcwd () . '";' , FILE_APPEND );
2024-07-01 11:12:12 +02:00
}
2021-04-30 01:16:32 +02:00
# Generate user count file (must be root)
2023-09-04 15:06:43 +02:00
exec ( $CONFIG [ 'php_exec' ] . " " . $config_dir . " /scripts/count_users.php " );
echo " Updated user count \n " ;
$uinfo = posix_getpwnam ( $CONFIG [ 'webserver_user' ]);
$cwd = getcwd ();
2024-03-08 18:27:35 +01:00
// Check permissions on some files
2023-09-04 15:06:43 +02:00
$webtmp = preg_replace ( '/spoolnews/' , 'tmp/' , $cwd );
$keydir = preg_replace ( '/spoolnews/' , 'pubkey/' , $cwd );
2021-10-29 07:58:58 +02:00
2024-07-02 15:43:14 +02:00
$banfile = $config_dir . '/banned_users.conf' ;
@ chown ( $banfile , $uinfo [ " uid " ]);
2023-09-04 15:06:43 +02:00
@ mkdir ( $webtmp , 0755 , 'recursive' );
@ chown ( $webtmp , $uinfo [ " uid " ]);
@ chgrp ( $webtmp , $uinfo [ " gid " ]);
@ mkdir ( $keydir , 0755 , 'recursive' );
@ chown ( $keydir , $uinfo [ " uid " ]);
@ chgrp ( $keydir , $uinfo [ " gid " ]);
@ mkdir ( $ssldir , 0755 );
@ chown ( $ssldir , $uinfo [ " uid " ]);
@ chgrp ( $ssldir , $uinfo [ " gid " ]);
2021-10-29 07:58:58 +02:00
2024-03-08 18:27:35 +01:00
$alias_file = $config_dir . '/aliases.conf' ;
2024-06-11 12:19:26 +02:00
if ( ! file_exists ( $alias_file )) {
2024-03-08 18:27:35 +01:00
touch ( $alias_file );
}
@ chown ( $alias_file , $uinfo [ " uid " ]);
@ chgrp ( $alias_file , $uinfo [ " gid " ]);
2023-09-04 15:06:43 +02:00
$pemfile = $ssldir . '/server.pem' ;
create_node_ssl_cert ( $pemfile );
2023-01-08 18:12:27 +01:00
2023-09-04 15:06:43 +02:00
$overview = $spooldir . '/articles-overview.db3' ;
touch ( $overview );
@ chown ( $overview , $uinfo [ " uid " ]);
@ chgrp ( $overview , $uinfo [ " gid " ]);
if ( $rslight_gpg [ 'enable' ] == '1' ) {
2023-07-24 19:33:13 +02:00
$gnupg = $rslight_gpg [ 'gnupghome' ];
2023-09-04 15:06:43 +02:00
if ( ! is_dir ( $gnupg )) {
mkdir ( $gnupg , 0700 );
chown ( $gnupg , $uinfo [ " uid " ]);
chgrp ( $gnupg , $uinfo [ " gid " ]);
2023-07-24 19:33:13 +02:00
}
2023-09-04 15:06:43 +02:00
}
2020-11-29 01:55:31 +01:00
/* Change to non root user */
2023-09-04 15:06:43 +02:00
change_identity ( $uinfo [ " uid " ], $uinfo [ " gid " ]);
2020-11-29 01:55:31 +01:00
/* Everything below runs as $CONFIG['webserver_user'] */
2023-09-04 15:06:43 +02:00
@ mkdir ( $logdir , 0755 , 'recursive' );
@ mkdir ( $lockdir , 0755 , 'recursive' );
2023-04-14 03:52:30 +02:00
2023-09-04 15:06:43 +02:00
if ( isset ( $CONFIG [ 'enable_nocem' ]) && $CONFIG [ 'enable_nocem' ] == true ) {
@ mkdir ( $spooldir . " nocem " , 0755 , 'recursive' );
exec ( $CONFIG [ 'php_exec' ] . " " . $config_dir . " /scripts/nocem.php " );
2020-11-29 01:55:31 +01:00
}
2023-07-24 19:33:13 +02:00
// Set up server gpg keys
2023-09-04 15:06:43 +02:00
if ( $rslight_gpg [ 'enable' ] == '1' ) {
if ( ! is_file ( $keydir . '/server_pubkey.txt' )) {
$domain = 'rslight@' . $rslight_gpg [ 'domain_name' ];
$pubkey = $keydir . '/server_pubkey.txt' ;
$fingerprint = $keydir . '/server_fingerprint.txt' ;
$create_gpg_keys = $config_dir . '/scripts/create_gpg_keys.sh "' . $gnupg . '" "' . $pubkey . '" "' . $fingerprint . '" "' . $domain . '"' ;
exec ( $create_gpg_keys );
}
exec ( $CONFIG [ 'php_exec' ] . " " . $config_dir . " /scripts/interBBS_mail.php " );
2023-07-24 19:33:13 +02:00
}
2020-11-29 01:55:31 +01:00
2024-10-06 15:07:43 +02:00
check_disk_space ();
2020-11-29 01:55:31 +01:00
reset ( $menulist );
2023-09-04 15:06:43 +02:00
foreach ( $menulist as $menu ) {
if (( $menu [ 0 ] == '#' ) || ( trim ( $menu ) == " " )) {
continue ;
}
$menuitem = explode ( ':' , $menu );
chdir ( " ../ " . $menuitem [ 0 ]);
2024-10-15 15:15:37 +02:00
$this_config_name = $menuitem [ 0 ];
if ( file_exists ( $config_dir . $this_config_name . '.inc.php' )) {
$config_file = $config_dir . $this_config_name . '.inc.php' ;
} else {
$config_file = $config_dir . 'rslight.inc.php' ;
}
$this_CONFIG = include ( $config_file );
file_put_contents ( $debug_log , " \n " . format_log_date () . " Using " . $config_file . " for " . $menuitem [ 0 ], FILE_APPEND );
if ( $this_CONFIG [ 'remote_server' ] !== '' ) {
2023-09-04 15:06:43 +02:00
# Send articles
echo " Sending articles \n " ;
echo exec ( $CONFIG [ 'php_exec' ] . " " . $config_dir . " /scripts/send.php " );
# Refresh spool
if ( isset ( $spoolnews ) && ( $spoolnews == true )) {
exec ( $CONFIG [ 'php_exec' ] . " " . $config_dir . " /scripts/spoolnews.php " );
2023-11-03 15:23:55 +01:00
echo " \n Refreshed spoolnews \n " ;
2023-09-04 15:06:43 +02:00
}
2024-10-15 15:15:37 +02:00
} else {
file_put_contents ( $debug_log , " \n " . format_log_date () . " Remote disabled for " . $menuitem [ 0 ] . " (no remote server) " , FILE_APPEND );
2023-09-04 15:06:43 +02:00
}
# Expire articles
exec ( $CONFIG [ 'php_exec' ] . " " . $config_dir . " /scripts/expire.php " );
echo " Expired articles \n " ;
2020-11-29 01:55:31 +01:00
}
2023-11-03 15:23:55 +01:00
2024-06-07 14:43:52 +02:00
# Expire diskcache
if ( file_exists ( $config_dir . '/cache.inc.php' )) {
include $config_dir . '/cache.inc.php' ;
2024-06-11 12:19:26 +02:00
if ( $enable_cache == 'diskcache' ) {
prune_dir_by_days ( $cache_dir , $cache_ttl / 86400 );
}
2024-06-07 14:43:52 +02:00
}
2021-12-02 05:02:31 +01:00
# Run RSS Feeds
2023-09-04 15:06:43 +02:00
exec ( $CONFIG [ 'php_exec' ] . " " . $config_dir . " /scripts/rss-feeds.php " );
echo " RSS Feeds updated \n " ;
2024-03-08 18:27:35 +01:00
# Reload grouplist
2024-06-11 12:19:26 +02:00
if (( filemtime ( $grouplist_cache_filename ) < ( time () - ( $grouplist_cache_time - 600 )) || ! file_exists ( $grouplist_cache_filename ))) {
2024-03-08 18:27:35 +01:00
exec ( $CONFIG [ 'php_exec' ] . " ../common/grouplist.php .RELOAD " );
echo " Refreshed grouplist \n " ;
}
2020-11-29 01:55:31 +01:00
# Rotate log files
2023-09-04 15:06:43 +02:00
log_rotate ();
echo " Log files rotated \n " ;
2021-06-11 09:50:54 +02:00
# Rotate keys
2023-09-04 15:06:43 +02:00
rotate_keys ();
echo " Keys rotated \n " ;
2023-11-03 15:23:55 +01:00
# Expire files
expire_files ();
echo " Removed old files \n " ;
2024-06-11 12:19:26 +02:00
file_put_contents ( $logfile , " \n " . date ( 'M d H:i:s' ) . " " . $config_name . " cron " . $pid . " completed... " , FILE_APPEND );
2020-11-29 01:55:31 +01:00
2024-10-06 15:07:43 +02:00
function check_disk_space ()
{
2024-10-09 14:59:10 +02:00
global $CONFIG , $OVERRIDES , $logdir , $spooldir ;
2024-10-06 15:07:43 +02:00
global $low_spool_disk_space , $min_spool_disk_space , $free_spool_disk_space ;
$logfile = $logdir . '/spoolnews.log' ;
$warning_spool_disk_space = $min_spool_disk_space * 1.1 ;
if ( $free_spool_disk_space < $warning_spool_disk_space ) {
$nearing_low_spool_disk_space = true ;
} else {
$nearing_low_spool_disk_space = false ;
}
if ( $nearing_low_spool_disk_space ) {
if ( $low_spool_disk_space ) { // Disk space low. Spooling will pause
print " Low Disk Space (less than " . $min_spool_disk_space . " available) \n " ;
file_put_contents ( $logfile , " \n " . format_log_date () . " " . $config_name . " Low Disk Space (less than " . $min_spool_disk_space . " Gb available for spool). Pausing spoolnews " , FILE_APPEND );
$subject = " LOW DISK SPACE ON " . $CONFIG [ 'server_path' ];
$body = " LOW DISK SPACE ON " . $CONFIG [ 'server_path' ] . " \n " ;
$body .= " Space has fallen below " . $min_spool_disk_space . " GB \n " ;
$body .= " Space remaining: " . round ( $free_spool_disk_space ) . " GB \n " ;
$timer_type = " low_spool_disk_space " ;
} else { // Disk space approaching low (within 10%) This is a warning only
print " Nearing Low Disk Space (less than " . round ( $warning_spool_disk_space , 1 ) . " Gb available for spool) \n " ;
file_put_contents ( $logfile , " \n " . format_log_date () . " Nearing Low Disk Space (less than " . round ( $warning_spool_disk_space , 1 ) . " Gb available for spool) " , FILE_APPEND );
$subject = " NEARING LOW DISK SPACE ON " . $CONFIG [ 'server_path' ];
$body = " NEARING LOW DISK SPACE ON " . $CONFIG [ 'server_path' ] . " \n " ;
$body .= " Space has fallen below " . round ( $warning_spool_disk_space ) . " GB \n " ;
$body .= " Space remaining: " . round ( $free_spool_disk_space ) . " GB \n \n " ;
$body .= " Spooling will pause when space below " . $min_spool_disk_space . " GB \n " ;
$timer_type = " nearing_low_spool_disk_space " ;
}
2024-10-09 14:59:10 +02:00
if ( $OVERRIDES [ 'send_admin_debug_messages' ] === true ) {
$date_window = 86400 ;
$send_email_timer_file = $spooldir . '/email_send_timer.dat' ;
if ( file_exists ( $send_email_timer_file )) {
$send_email_timer = unserialize ( file_get_contents ( $send_email_timer_file ));
2024-10-06 15:07:43 +02:00
} else {
2024-10-09 14:59:10 +02:00
$send_email_timer = array ();
}
if ( ! isset ( $send_email_timer [ $timer_type ])) {
$send_email_timer [ $timer_type ] = 0 ;
}
if ( $send_email_timer [ $timer_type ] < ( time () - $date_window )) {
if ( $send_email_timer [ $timer_type ] != 0 ) {
$send_email_timer [ $timer_type ] = 0 ;
} else {
$send_email_timer [ $timer_type ] = time ();
}
send_internet_email ( $subject , $body );
2024-10-06 15:07:43 +02:00
}
2024-10-09 14:59:10 +02:00
file_put_contents ( $send_email_timer_file , serialize ( $send_email_timer ));
2024-10-06 15:07:43 +02:00
}
}
}
2024-06-11 12:19:26 +02:00
function expire_files ()
{
2024-10-06 15:07:43 +02:00
global $spooldir , $logdir , $uinfo ;
2023-11-03 15:23:55 +01:00
$now = time ();
// Days to prune
$nocemdays = 7 ;
// Days to seconds from now
$nocem = $now - ( $nocemdays * 86400 );
// Dirs to prune
$nocem_processed = $spooldir . " /nocem/processed/ " ;
$nocem_failed = $spooldir . " /nocem/failed/ " ;
2024-06-11 12:19:26 +02:00
if ( ! is_dir ( $nocem_processed )) {
2023-12-19 12:50:41 +01:00
@ mkdir ( $nocem_processed , 0755 , 'recursive' );
@ chown ( $nocem_processed , $uinfo [ " uid " ]);
@ chgrp ( $nocem_processed , $uinfo [ " gid " ]);
}
2024-06-11 12:19:26 +02:00
if ( ! is_dir ( $nocem_failed )) {
2023-12-19 12:50:41 +01:00
@ mkdir ( $nocem_failed , 0755 , 'recursive' );
@ chown ( $nocem_failed , $uinfo [ " uid " ]);
@ chgrp ( $nocem_failed , $uinfo [ " gid " ]);
}
2024-06-11 12:19:26 +02:00
2023-11-03 15:23:55 +01:00
// $nocem_processed
$filenames = array_diff ( scandir ( $nocem_processed ), array (
'..' ,
'.'
));
2024-06-11 12:19:26 +02:00
foreach ( $filenames as $one ) {
if ( filemtime ( $nocem_processed . $one ) < $nocem ) {
unlink ( $nocem_processed . $one );
2023-11-03 15:23:55 +01:00
}
}
2024-06-11 12:19:26 +02:00
2023-11-03 15:23:55 +01:00
// $nocem_failed
$filenames = array_diff ( scandir ( $nocem_failed ), array (
'..' ,
'.'
));
2024-06-11 12:19:26 +02:00
foreach ( $filenames as $one ) {
if ( filemtime ( $nocem_failed . $one ) < $nocem ) {
unlink ( $nocem_failed . $one );
2023-11-03 15:23:55 +01:00
}
}
}
2023-09-04 15:06:43 +02:00
function log_rotate ()
{
global $logdir ;
$rotate = filemtime ( $logdir . '/rotate' );
if (( time () - $rotate ) > 86400 ) {
$log_files = scandir ( $logdir );
foreach ( $log_files as $logfile ) {
2024-10-06 15:07:43 +02:00
if ( substr ( $logfile , - 4 ) != '.log' ) {
2023-09-04 15:06:43 +02:00
continue ;
}
$logfile = $logdir . '/' . $logfile ;
@ unlink ( $logfile . '.5' );
@ rename ( $logfile . '.4' , $logfile . '.5' );
@ rename ( $logfile . '.3' , $logfile . '.4' );
@ rename ( $logfile . '.2' , $logfile . '.3' );
@ rename ( $logfile . '.1' , $logfile . '.2' );
2024-05-13 15:41:02 +02:00
file_put_contents ( $logfile , " \n Log file rotated " , FILE_APPEND );
2023-09-04 15:06:43 +02:00
@ rename ( $logfile , $logfile . '.1' );
echo 'Rotated: ' . $logfile . " \n " ;
}
unlink ( $logdir . '/rotate' );
touch ( $logdir . '/rotate' );
2020-11-29 01:55:31 +01:00
}
}
2023-09-04 15:06:43 +02:00
function rotate_keys ()
{
global $spooldir ;
$keyfile = $spooldir . '/keys.dat' ;
$newkeys = array ();
if ( filemtime ( $keyfile ) + 14400 > time ()) {
return ;
2021-06-11 09:50:54 +02:00
} else {
2023-09-04 15:06:43 +02:00
$new = true ;
if ( is_file ( $keyfile )) {
$keys = unserialize ( file_get_contents ( $keyfile ));
$new = false ;
}
if ( $new !== true ) {
$newkeys [ 0 ] = base64_encode ( openssl_random_pseudo_bytes ( 44 ));
$newkeys [ 1 ] = $keys [ 0 ];
} else {
$newkeys [ 0 ] = base64_encode ( openssl_random_pseudo_bytes ( 44 ));
$newkeys [ 1 ] = base64_encode ( openssl_random_pseudo_bytes ( 44 ));
}
2021-06-11 09:50:54 +02:00
}
2023-09-04 15:06:43 +02:00
file_put_contents ( $keyfile , serialize ( $newkeys ));
touch ( $keyfile );
2021-06-11 09:50:54 +02:00
}