static function ExecuteAddDomain($uid, $domain, $destination, $autohome)
 {
     global $zdbh;
     $retval = FALSE;
     runtime_hook::Execute('OnBeforeAddDomain');
     $currentuser = ctrl_users::GetUserDetail($uid);
     $domain = strtolower(str_replace(' ', '', $domain));
     if (!fs_director::CheckForEmptyValue(self::CheckCreateForErrors($domain))) {
         //** New Home Directory **//
         if ($autohome == 1) {
             $destination = "/" . str_replace(".", "_", $domain);
             $vhost_path = ctrl_options::GetSystemOption('hosted_dir') . $currentuser['username'] . "/public_html/" . $destination . "/";
             fs_director::CreateDirectory($vhost_path);
             fs_director::SetFileSystemPermissions($vhost_path, 0777);
             //** Existing Home Directory **//
         } else {
             $destination = "/" . $destination;
             $vhost_path = ctrl_options::GetSystemOption('hosted_dir') . $currentuser['username'] . "/public_html/" . $destination . "/";
         }
         // Error documents:- Error pages are added automatically if they are found in the _errorpages directory
         // and if they are a valid error code, and saved in the proper format, i.e. <error_number>.html
         fs_director::CreateDirectory($vhost_path . "/_errorpages/");
         $errorpages = ctrl_options::GetSystemOption('static_dir') . "/errorpages/";
         if (is_dir($errorpages)) {
             if ($handle = @opendir($errorpages)) {
                 while (($file = @readdir($handle)) !== false) {
                     if ($file != "." && $file != "..") {
                         $page = explode(".", $file);
                         if (!fs_director::CheckForEmptyValue(self::CheckErrorDocument($page[0]))) {
                             fs_filehandler::CopyFile($errorpages . $file, $vhost_path . '/_errorpages/' . $file);
                         }
                     }
                 }
                 closedir($handle);
             }
         }
         // Lets copy the default welcome page across...
         if (!file_exists($vhost_path . "/index.html") && !file_exists($vhost_path . "/index.php") && !file_exists($vhost_path . "/index.htm")) {
             fs_filehandler::CopyFileSafe(ctrl_options::GetSystemOption('static_dir') . "pages/welcome.html", $vhost_path . "/index.html");
         }
         // If all has gone well we need to now create the domain in the database...
         $sql = $zdbh->prepare("INSERT INTO x_vhosts (vh_acc_fk,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t vh_name_vc,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t vh_directory_vc,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t vh_type_in,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t vh_created_ts) VALUES (\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t :userid,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t :domain,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t :destination,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t 1,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t :time)");
         //CLEANER FUNCTION ON $domain and $homedirectory_to_use (Think I got it?)
         $time = time();
         $sql->bindParam(':time', $time);
         $sql->bindParam(':userid', $currentuser['userid']);
         $sql->bindParam(':domain', $domain);
         $sql->bindParam(':destination', $destination);
         $sql->execute();
         // Only run if the Server platform is Windows.
         if (sys_versions::ShowOSPlatformVersion() == 'Windows') {
             if (ctrl_options::GetSystemOption('disable_hostsen') == 'false') {
                 // Lets add the hostname to the HOSTS file so that the server can view the domain immediately...
                 @exec("C:/Sentora/bin/zpss/setroute.exe " . $domain . "");
                 @exec("C:/Sentora/bin/zpss/setroute.exe www." . $domain . "");
             }
         }
         self::SetWriteApacheConfigTrue();
         $retval = TRUE;
         runtime_hook::Execute('OnAfterAddDomain');
         return $retval;
     }
 }
echo "END Calculating disk usage" . fs_filehandler::NewLine();
/*
 * Calculate the bandwidth used for each user.
 */
$checksql = $zdbh->query("SELECT COUNT(*) AS total FROM x_vhosts WHERE vh_deleted_ts IS NULL")->fetch();
echo fs_filehandler::NewLine() . "START Calculating bandwidth usage for all client accounts.." . fs_filehandler::NewLine();
if ($checksql['total'] > 0) {
    $domainssql = $zdbh->query("SELECT vh_acc_fk, vh_name_vc FROM x_vhosts WHERE vh_deleted_ts IS NULL");
    while ($domain = $domainssql->fetch()) {
        $domainowner = ctrl_users::GetUserDetail($domain['vh_acc_fk']);
        $bandwidthlog = ctrl_options::GetSystemOption('log_dir') . 'domains/' . $domainowner['username'] . '/' . $domain['vh_name_vc'] . '-bandwidth.log';
        $snapshotfile = ctrl_options::GetSystemOption('log_dir') . 'domains/' . $domainowner['username'] . '/' . $domain['vh_name_vc'] . '-snapshot.bw';
        $bandwidth = 0;
        echo "Processing domain \"" . $domain['vh_name_vc'] . "\"" . fs_filehandler::NewLine();
        if (fs_director::CheckFileExists($bandwidthlog)) {
            fs_filehandler::CopyFile($bandwidthlog, $snapshotfile);
            if (fs_director::CheckFileExists($snapshotfile)) {
                fs_filehandler::ResetFile($bandwidthlog);
                echo "Generating bandwidth.. " . fs_filehandler::NewLine();
                $bandwidth = sys_bandwidth::CalculateFromApacheLog($snapshotfile);
                if (file_exists($snapshotfile)) {
                    @unlink($snapshotfile);
                    echo "usage: " . $bandwidth . " (" . fs_director::ShowHumanFileSize($bandwidth) . ")" . fs_filehandler::NewLine();
                }
            }
        }
        if (!fs_director::CheckForEmptyValue($bandwidth)) {
            //$zdbh->query("UPDATE x_bandwidth SET bd_transamount_bi=(bd_transamount_bi+" . $bandwidth . ") WHERE bd_acc_fk = " . $domain['vh_acc_fk'] . " AND bd_month_in = " . date("Ym") . "");
            $numrows = $zdbh->prepare("UPDATE x_bandwidth SET bd_transamount_bi=(bd_transamount_bi+:bandwidth) WHERE bd_acc_fk = :vh_acc_fk AND bd_month_in = :date");
            $numrows->bindParam(':bandwidth', $bandwidth);
            $date = date("Ym");