#!/bin/sh
eval 'if [ -x /usr/local/cpanel/3rdparty/bin/perl ]; then exec /usr/local/cpanel/3rdparty/bin/perl -x -- $0 ${1+"$@"}; else exec /usr/bin/perl -x $0 ${1+"$@"}; fi;'
  if 0;

#!/usr/bin/perl
#WHMADDON:cronconfig:Configure cPanel Cron Times
# cpanel - addon_cronconfig.cgi                   Copyright(c) 2011 cPanel, Inc.
#                                                           All rights Reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cpanel license. Unauthorized copying is prohibited

BEGIN {
    unshift @INC, '/usr/local/cpanel', '/usr/local/cpanel/whostmgr/docroot/cgi';
}

use whmlib;
use SafeFile;
require "parseform.pl";

our %ACL;
my ( $header, $footer ) = undef;

my %FORM = parseform();
print "Content-type: text/html\n\n";
defheader("Configure cPanel Cron");

if ( !$ACL{all} ) {
    print "You do not have access to modify Cron Settings. If you believe this is an error please contact your system administrator or hosting company.\n";
    exit();
}

$header = "<div align=\"center\"><table align=\"center\" width=\"40%\"><tr><td><fieldset>\n";
$footer = "</fieldset></td></tr></table></div>\n";

if ( $FORM{'action'} eq "" ) {
    $FORM{'action'} = "main";
    main();
}
if ( $FORM{'action'} eq "changecron" ) {
    changecron();
}

sub main {
    print $header;
    print "<legend>Main Menu</legend>";
    print "<table align=\"center\" width=\"30%\"><tr>\n";
    print "<td align=\"center\" width=\"14%\">Command</td>\n";
    print "<td align=\"center\" width=\"14%\">Minute</td>\n";
    print "<td align=\"center\" width=\"14%\">Hour</td>\n";
    print "<td align=\"center\" width=\"14%\">Day</td>\n";
    print "<td align=\"center\" width=\"14%\">Month</td>\n";
    print "<td align=\"center\" width=\"14%\">Weekday</td>\n";
    print "<td align=\"center\" width=\"14%\"></td>\n";
    checkcron("upcp");
    checkcron("cpbackup");
    print "</tr></table>\n";
    upcpinfo();
    cpbackupinfo();
    croninfo();
    print $footer;
}

sub croninfo {
    print "</fieldset><fieldset><legend>Cron Information</legend>\n";
    print
      "Cron jobs are jobs that are set to run at a certain time based on the minute, hour, day, month, and weekday given to them. The Hour is always set in military time and based on the time that your server is set to. Day of the month you want something ran on (15 would be the 15th day of the month). Month is the month you want the cronjob to run in 1=January and 12=December. Weekday is the day of the week you want it to run on 0=Sunday and 6=Saturday. * means to run it at every instance of this section, so a * in day would mean to run the job everyday, while a * in Minute would run the job every minute.<br>Example: Minute = 15; Hour = 21; Day = *; Month = *; Weekday = *. This process would run everyday at 2115, or in 12hr format 9:15pm\n";
}

sub upcpinfo {
    print "</fieldset><fieldset><legend>Command: upcp</legend>\n";
    print "upcp is the program that updates cPanel. We recommend running this script once a day, it has, by default, been set to a random time between 9pm and 6am local time. You may set this to whatever time you wish, however we recommend setting it to run during off peak hours.\n";
}

sub cpbackupinfo {
    print "</fieldset><fieldset><legend>Command: cpbackup</legend>\n";
    print "cpbackup is the program used to run backups on your server. It is, by default, set to run at 1am as this is an off peak time for most servers. We recommend setting this to an off peak time early enough to allow your server to finish backups before entering peak time again. If you have backups disabled then the program will immediately exit after it is run\n";
}

sub checkcron {
    my $cron    = shift;
    my $cronadd = 0;
    my @CL      = split( /\n/, `crontab -l` );
    my ( $minute, $hour, $day, $month, $weekday, $command ) = undef;
    foreach my $cronjob (@CL) {
        if ( $cronjob =~ /$cron/ ) {
            ( $minute, $hour, $day, $month, $weekday, $command ) = split( / /, $cronjob );
            $cronadd++;
        }
        elsif ( $cronadd eq "0" && $cron eq "cpbackup" ) {
            ( $minute, $hour, $day, $month, $weekday, $command ) = ( "0", "1", "*", "*", "*", "/scripts/cpbackup" );
        }
        elsif ( $cronadd eq "0" && $cron eq "upcp" ) {
            ( $minute, $hour, $day, $month, $weekday, $command ) = ( int( rand(60) ), randtime(), "*", "*", "*", "/scripts/upcp" );
        }
    }
    print "<tr>\n";
    print "<form name=\"$cron\" action=\"addon_cronconfig.cgi\" method=\"post\">\n";
    print "<td>$cron</td>\n";
    print "<td align=\"center\"><input type=\"text\" name=\"minute\" value=\"$minute\" size=\"2\"></td>\n";
    print "<td align=\"center\"><input type=\"text\" name=\"hour\" value=\"$hour\" size=\"2\"></td>\n";
    print "<td align=\"center\"><input type=\"text\" name=\"day\" value=\"$day\" size=\"2\"></td>\n";
    print "<td align=\"center\"><input type=\"text\" name=\"month\" value=\"$month\" size=\"2\"></td>\n";
    print "<td align=\"center\"><input type=\"text\" name=\"weekday\" value=\"$weekday\" size=\"2\"></td>\n";
    print "<td align=\"center\"><input type=\"hidden\" name=\"action\" value=\"changecron\"></td>\n";
    print "<td align=\"center\"><input type=\"hidden\" name=\"script\" value=\"$cron\"></td>\n";
    print "<td align=\"center\"><input type=\"hidden\" name=\"command\" value=\"$command\"></td>\n";
    print "<td align=\"center\"><input type=\"submit\" value=\"Commit\" class=\"submit\"><br></td>\n";
    print "</form></tr>\n";

}

sub changecron {
    my $script = $FORM{'script'};
    my @CL = split( /\n/, `crontab -l` );
    @CL = grep( !/$script/, @CL );
    push( @CL, "$FORM{'minute'} $FORM{'hour'} $FORM{'day'} $FORM{'month'} $FORM{'weekday'} $FORM{'command'}" );
    open( CR, ">/scripts/.crontab" );
    foreach my $line (@CL) {
        print CR "$line\n";
    }
    close(CR);
    system("crontab /scripts/.crontab");
    my $success = !$?;
    unlink("/scripts/.crontab");
    print $header;
    if ($success) {
        print "<legend>Cron Changed</legend>\n";
        print "The cron job for <b>$script</b> has been changed to run at:<br>\n";
        print "Hour: $FORM{'hour'}<br>Minute: $FORM{'minute'}<br>\n";

        if ( $FORM{'day'} ne "*" ) {
            print "$FORM{'day'} day of ";
        }
        else { print "Every day of "; }
        if ( $FORM{'month'} ne "*" ) {
            print "the $FORM{'month'} month";
        }
        else { print "every month"; }
        if ( $FORM{'weekday'} eq "*" ) {
            print ".<br>\n";
        }
        else { print " and on the $FORM{'weekday'} weekday (remember 0=sunday and 6=saturday).<br>\n"; }
    }
    else {
        print "<legend>Cron Not Changed</legend>\n";
        print "Please check that you entered only digits and the * character.\n";
    }
    print $footer;
}

sub randtime {
    my $time = int( rand(9) );
    if ( $time <= 5 ) {
        return ($time);
    }
    else {
        $time = $time % 5 + 20;
        return $time;
    }
}

