ftimme
2012-10-19 1ef48604d2474dfbfe23645f5fa5bb0b5ea0fadc
server/cron_daily.php
@@ -39,7 +39,7 @@
// Load required base-classes
$app->uses('ini_parser,file,services,getconf');
$app->uses('ini_parser,file,services,getconf,system');
#######################################################################################################
@@ -113,7 +113,7 @@
}
$sql = "SELECT domain_id, domain, document_root FROM web_domain WHERE stats_type = 'webalizer' AND server_id = ".$conf['server_id'];
$sql = "SELECT domain_id, domain, document_root, web_folder, type FROM web_domain WHERE stats_type = 'webalizer' AND server_id = ".$conf['server_id'];
$records = $app->db->queryAllRecords($sql);
foreach($records as $rec) {
@@ -128,7 +128,7 @@
   }
   $domain = escapeshellcmd($rec['domain']);
   $statsdir = escapeshellcmd($rec['document_root'].'/web/stats');
   $statsdir = escapeshellcmd($rec['document_root'].'/'.($rec['type'] == 'vhostsubdomain' ? $rec['web_folder'] : 'web').'/stats');
   $webalizer = '/usr/bin/webalizer';
   $webalizer_conf_main = '/etc/webalizer/webalizer.conf';
   $webalizer_conf = escapeshellcmd($rec['document_root'].'/log/webalizer.conf');
@@ -154,7 +154,7 @@
// Create awstats statistics
#######################################################################################################
$sql = "SELECT domain_id, domain, document_root, system_user, system_group FROM web_domain WHERE stats_type = 'awstats' AND server_id = ".$conf['server_id'];
$sql = "SELECT domain_id, domain, document_root, web_folder, type, system_user, system_group FROM web_domain WHERE stats_type = 'awstats' AND server_id = ".$conf['server_id'];
$records = $app->db->queryAllRecords($sql);
$web_config = $app->getconf->get_server_config($conf['server_id'], 'web');
@@ -169,9 +169,9 @@
         continue;
      }
   }
    $web_folder = ($rec['type'] == 'vhostsubdomain' ? $rec['web_folder'] : 'web');
   $domain = escapeshellcmd($rec['domain']);
   $statsdir = escapeshellcmd($rec['document_root'].'/web/stats');
   $statsdir = escapeshellcmd($rec['document_root'].'/'.$web_folder.'/stats');
   $awstats_pl = $web_config['awstats_pl'];
   $awstats_buildstaticpages_pl = $web_config['awstats_buildstaticpages_pl'];
@@ -213,7 +213,7 @@
      }
   }
   // awstats_buildstaticpages.pl -update -config=mydomain.com -lang=en -dir=/var/www/domain.com/web/stats -awstatsprog=/path/to/awstats.pl
   // awstats_buildstaticpages.pl -update -config=mydomain.com -lang=en -dir=/var/www/domain.com/'.$web_folder.'/stats -awstatsprog=/path/to/awstats.pl
   // $command = "$awstats_buildstaticpages_pl -update -config='$domain' -lang=".$conf['language']." -dir='$statsdir' -awstatsprog='$awstats_pl'";
   $command = "$awstats_buildstaticpages_pl -month='$awmonth' -year='$awyear' -update -config='$domain' -lang=".$conf['language']." -dir='$statsdir' -awstatsprog='$awstats_pl'";
@@ -236,18 +236,24 @@
   if($awstats_pl != '' && $awstats_buildstaticpages_pl != '' && fileowner($awstats_pl) == 0 && fileowner($awstats_buildstaticpages_pl) == 0) {
      exec($command);
      if(is_file($rec['document_root'].'/web/stats/index.html')) unlink($rec['document_root'].'/web/stats/index.html');
      rename($rec['document_root'].'/web/stats/awstats.'.$domain.'.html',$rec['document_root'].'/web/stats/awsindex.html');
      if(!is_file($rec['document_root']."/web/stats/index.php")) copy("/usr/local/ispconfig/server/conf/awstats_index.php.master",$rec['document_root']."/web/stats/index.php");
      if(is_file($rec['document_root'].'/'.$web_folder.'/stats/index.html')) unlink($rec['document_root'].'/'.$web_folder.'/stats/index.html');
      rename($rec['document_root'].'/'.$web_folder.'/stats/awstats.'.$domain.'.html',$rec['document_root'].'/'.$web_folder.'/stats/awsindex.html');
      if(!is_file($rec['document_root']."/".$web_folder."/stats/index.php")) {
         if(file_exists("/usr/local/ispconfig/server/conf-custom/awstats_index.php.master")) {
            copy("/usr/local/ispconfig/server/conf-custom/awstats_index.php.master",$rec['document_root']."/".$web_folder."/stats/index.php");
         } else {
            copy("/usr/local/ispconfig/server/conf/awstats_index.php.master",$rec['document_root']."/".$web_folder."/stats/index.php");
         }
      }
      $app->log('Created awstats statistics with command: '.$command,LOGLEVEL_DEBUG);
   } else {
      $app->log("No awstats statistics created. Either $awstats_pl or $awstats_buildstaticpages_pl is not owned by root user.",LOGLEVEL_WARN);
   }
   if(is_file($rec['document_root']."/web/stats/index.php")) {
      chown($rec['document_root']."/web/stats/index.php",$rec['system_user']);
      chgrp($rec['document_root']."/web/stats/index.php",$rec['system_group']);
   if(is_file($rec['document_root']."/".$web_folder."/stats/index.php")) {
      chown($rec['document_root']."/".$web_folder."/stats/index.php",$rec['system_user']);
      chgrp($rec['document_root']."/".$web_folder."/stats/index.php",$rec['system_group']);
   }
}
@@ -447,10 +453,12 @@
if ($app->dbmaster == $app->db) {
   $global_config = $app->getconf->get_global_config('mail');
   $current_month = date('Y-m');
   //* Check website traffic quota
   $sql = "SELECT sys_groupid,domain_id,domain,traffic_quota,traffic_quota_lock FROM web_domain WHERE (traffic_quota > 0 or traffic_quota_lock = 'y') and type = 'vhost'";
   $sql = "SELECT sys_groupid,domain_id,domain,traffic_quota,traffic_quota_lock FROM web_domain WHERE (traffic_quota > 0 or traffic_quota_lock = 'y') and (type = 'vhost' OR type = 'vhostsubdomain')";
   $records = $app->db->queryAllRecords($sql);
   if(is_array($records)) {
      foreach($records as $rec) {
@@ -478,12 +486,63 @@
            ($reseller_traffic_quota > 0 && $web_traffic > $reseller_traffic_quota)) {*/
         if($web_traffic_quota > 0 && $web_traffic > $web_traffic_quota) {
            $app->dbmaster->datalogUpdate('web_domain', "traffic_quota_lock = 'y',active = 'n'", 'domain_id', $rec['domain_id']);
            $app->log('Traffic quota for '.$rec['domain_id'].' exceeded. Disabling website.',LOGLEVEL_DEBUG);
            $app->log('Traffic quota for '.$rec['domain'].' exceeded. Disabling website.',LOGLEVEL_DEBUG);
            //* Send traffic notifications
            if($web_config['overtraffic_notify_admin'] == 'y' || $web_config['overtraffic_notify_client'] == 'y') {
               if(file_exists($conf['rootpath'].'/conf-custom/mail/web_traffic_notification_'.$conf['language'].'.txt')) {
                  $lines = file($conf['rootpath'].'/conf-custom/mail/web_traffic_notification_'.$conf['language'].'.txt');
               } elseif(file_exists($conf['rootpath'].'/conf-custom/mail/web_traffic_notification_en.txt')) {
                  $lines = file($conf['rootpath'].'/conf-custom/mail/web_traffic_notification_en.txt');
               } elseif(file_exists($conf['rootpath'].'/conf/mail/web_traffic_notification_'.$conf['language'].'.txt')) {
                  $lines = file($conf['rootpath'].'/conf/mail/web_traffic_notification_'.$conf['language'].'.txt');
               } else {
                  $lines = file($conf['rootpath'].'/conf/mail/web_traffic_notification_en.txt');
               }
               //* Get subject
               $parts = explode(':',trim($lines[0]));
               unset($parts[0]);
               $traffic_mail_subject  = implode(':',$parts);
               unset($lines[0]);
               //* Get message
               $traffic_mail_message = trim(implode($lines));
               unset($tmp);
               //* Replace placeholders
               $traffic_mail_message = str_replace('{domain}',$rec['domain'],$traffic_mail_message);
               $mailHeaders      = "MIME-Version: 1.0" . "\n";
               $mailHeaders     .= "Content-type: text/plain; charset=utf-8" . "\n";
               $mailHeaders     .= "Content-Transfer-Encoding: 8bit" . "\n";
               $mailHeaders     .= "From: ". $global_config['admin_mail'] . "\n";
               $mailHeaders     .= "Reply-To: ". $global_config['admin_mail'] . "\n";
               $mailSubject      = "=?utf-8?B?".base64_encode($traffic_mail_subject)."?=";
               //* send email to admin
               if($global_config['admin_mail'] != '' && $web_config['overtraffic_notify_admin'] == 'y') {
                  mail($global_config['admin_mail'], $mailSubject, $traffic_mail_message, $mailHeaders);
               }
               //* Send email to client
               if($web_config['overtraffic_notify_admin'] == 'y') {
                  $client_group_id = $rec["sys_groupid"];
                  $client = $app->db->queryOneRecord("SELECT client.email FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = $client_group_id");
                  if($client['email'] != '') {
                     mail($client['email'], $mailSubject, $traffic_mail_message, $mailHeaders);
                  }
               }
            }
         } else {
            //* unlock the website, if traffic is lower then quota
            if($rec['traffic_quota_lock'] == 'y') {
               $app->dbmaster->datalogUpdate('web_domain', "traffic_quota_lock = 'n',active = 'y'", 'domain_id', $rec['domain_id']);
               $app->log('Traffic quota for '.$rec['domain_id'].' ok again. Re-enabling website.',LOGLEVEL_DEBUG);
               $app->log('Traffic quota for '.$rec['domain'].' ok again. Re-enabling website.',LOGLEVEL_DEBUG);
            }
         }
      }
@@ -539,7 +598,7 @@
      chmod(escapeshellcmd($backup_dir), $backup_dir_permissions);
   }
   $sql = "SELECT * FROM web_domain WHERE server_id = '".$conf['server_id']."' AND type = 'vhost' AND backup_interval != 'none'";
   $sql = "SELECT * FROM web_domain WHERE server_id = '".$conf['server_id']."' AND (type = 'vhost' OR type = 'vhostsubdomain') AND backup_interval != 'none'";
   $records = $app->db->queryAllRecords($sql);
   if(is_array($records)) {
      foreach($records as $rec) {
@@ -576,9 +635,12 @@
            chmod($web_backup_dir.'/'.$web_backup_file, 0750);
            //* Insert web backup record in database
            $insert_data = "(server_id,parent_domain_id,backup_type,backup_mode,tstamp,filename) VALUES (".$conf['server_id'].",".$web_id.",'web','".$backup_mode."',".time().",'".$app->db->quote($web_backup_file)."')";
            $app->dbmaster->datalogInsert('web_backup', $insert_data, 'backup_id');
            //$insert_data = "(server_id,parent_domain_id,backup_type,backup_mode,tstamp,filename) VALUES (".$conf['server_id'].",".$web_id.",'web','".$backup_mode."',".time().",'".$app->db->quote($web_backup_file)."')";
            //$app->dbmaster->datalogInsert('web_backup', $insert_data, 'backup_id');
            $sql = "INSERT INTO web_backup (server_id,parent_domain_id,backup_type,backup_mode,tstamp,filename) VALUES (".$conf['server_id'].",".$web_id.",'web','".$backup_mode."',".time().",'".$app->db->quote($web_backup_file)."')";
            $app->db->query($sql);
            $app->dbmaster->query($sql);
            //* Remove old backups
            $backup_copies = intval($rec['backup_copies']);
@@ -598,7 +660,10 @@
                  unlink($web_backup_dir.'/'.$files[$n]);
                  $sql = "SELECT backup_id FROM web_backup WHERE server_id = ".$conf['server_id']." AND parent_domain_id = $web_id AND filename = '".$app->db->quote($files[$n])."'";
                  $tmp = $app->dbmaster->queryOneRecord($sql);
                  $app->dbmaster->datalogDelete('web_backup', 'backup_id', $tmp['backup_id']);
                  //$app->dbmaster->datalogDelete('web_backup', 'backup_id', $tmp['backup_id']);
                  $sql = "DELETE FROM web_backup WHERE backup_id = ".intval($tmp['backup_id']);
                  $app->db->query($sql);
                  $app->dbmaster->query($sql);
               }
            }
@@ -606,6 +671,9 @@
            unset($dir_handle);
            //* Remove backupdir symlink and create as directory instead
            $app->uses('system');
            $app->system->web_folder_protection($web_path,false);
            if(is_link($web_path.'/backup')) {
               unlink($web_path.'/backup');
            }
@@ -614,6 +682,8 @@
               chown($web_path.'/backup', $rec['system_user']);
               chgrp($web_path.'/backup', $rec['system_group']);
            }
            $app->system->web_folder_protection($web_path,true);
         }
@@ -662,8 +732,11 @@
            chgrp($db_backup_dir.'/'.$db_backup_file.'.gz', filegroup($db_backup_dir));
            //* Insert web backup record in database
            $insert_data = "(server_id,parent_domain_id,backup_type,backup_mode,tstamp,filename) VALUES (".$conf['server_id'].",$web_id,'mysql','sqlgz',".time().",'".$app->db->quote($db_backup_file).".gz')";
            $app->dbmaster->datalogInsert('web_backup', $insert_data, 'backup_id');
            //$insert_data = "(server_id,parent_domain_id,backup_type,backup_mode,tstamp,filename) VALUES (".$conf['server_id'].",$web_id,'mysql','sqlgz',".time().",'".$app->db->quote($db_backup_file).".gz')";
            //$app->dbmaster->datalogInsert('web_backup', $insert_data, 'backup_id');
            $sql = "INSERT INTO web_backup (server_id,parent_domain_id,backup_type,backup_mode,tstamp,filename) VALUES (".$conf['server_id'].",$web_id,'mysql','sqlgz',".time().",'".$app->db->quote($db_backup_file).".gz')";
            $app->db->query($sql);
            $app->dbmaster->query($sql);
            //* Remove the uncompressed file
            unlink($db_backup_dir.'/'.$db_backup_file);
@@ -687,7 +760,10 @@
                  unlink($db_backup_dir.'/'.$files[$n]);
                  $sql = "SELECT backup_id FROM web_backup WHERE server_id = ".$conf['server_id']." AND parent_domain_id = $web_id AND filename = '".$app->db->quote($files[$n])."'";
                  $tmp = $app->dbmaster->queryOneRecord($sql);
                  $app->dbmaster->datalogDelete('web_backup', 'backup_id', $tmp['backup_id']);
                  //$app->dbmaster->datalogDelete('web_backup', 'backup_id', $tmp['backup_id']);
                  $sql = "DELETE FROM web_backup WHERE backup_id = ".intval($tmp['backup_id']);
                  $app->db->query($sql);
                  $app->dbmaster->query($sql);
               }
            }