function backup_file($filename, $date)
{
    $sgcp = new CP_Update();
    $bkup_dir = "tmp_update/backup";
    $dstdir = $bkup_dir . "/" . $date;
    // Check & Create dirs
    $sgcp->list_create_dir($dstdir . "/" . $filename);
    if (!($handle = fopen($dstdir . "/" . $filename, 'w'))) {
        //echo "Cannot write file ($filename)<br />\n";
    } else {
        if ($contents = file_get_contents($filename)) {
            if (fwrite($handle, $contents) === FALSE) {
                //echo "Cannot write to file ($filename)";
            }
        }
        fclose($handle);
    }
}
function do_cp_update()
{
    global $CP;
    if (checkprivilege_action($CP[login_id], g_view_lastestcp)) {
        $sgcp = new CP_Update();
        $version = cp_current_version();
        $cp_version = $sgcp->version_data($version, 0);
        $cp_release = $sgcp->version_data($version, 1);
        $list_version = cp_list_version();
        if (!is_dir("tmp_update")) {
            if (!mkdir("tmp_update")) {
                exit;
            }
        }
        $count_files = $sgcp->count_list_update_file($cp_version, $cp_release, $list_version) * 2;
        $date = date("Y-m-d_H_i_s");
        $count_success = 0;
        cp_update_set_msg(0, 0);
        cp_update_set_msg(3, 0);
        for ($i = 0; $i < count($list_version); $i++) {
            $load_version = $sgcp->version_data($list_version[$i], 0);
            $load_release = $sgcp->version_data($list_version[$i], 1);
            if ($load_version && cp_check_version($cp_version, $cp_release, $load_version, $load_release)) {
                $list_update_file = file_get_result("bin/" . $list_version[$i] . "/list.sgcp", 1);
                $list_update_file = $sgcp->true_list_update_file($list_update_file);
                $list_download_file = $sgcp->true_list_download_file($list_update_file, $list_version[$i]);
                $tmp_list_update_file[$i] = $list_update_file;
                for ($j = 0; $j < count($list_download_file); $j++) {
                    cp_update_set_msg(1, $list_download_file[$j]);
                    if (!($tmp_update_result[$i][$j] = file_get_result($list_download_file[$j]))) {
                        cp_update_set_msg(4, $list_download_file[$j]);
                        exit;
                    } else {
                        $count_success++;
                        $percent = $count_success / $count_files * 100;
                        $percent = (int) $percent;
                        cp_update_set_msg(3, $percent);
                    }
                }
            }
        }
        for ($i = 0; $i < count($list_version); $i++) {
            $load_version = $sgcp->version_data($list_version[$i], 0);
            $load_release = $sgcp->version_data($list_version[$i], 1);
            if ($load_version && cp_check_version($cp_version, $cp_release, $load_version, $load_release)) {
                $list_update_file = $tmp_list_update_file[$i];
                $list_download_file = $sgcp->true_list_download_file($list_update_file, $list_version[$i]);
                for ($j = 0; $j < count($list_download_file); $j++) {
                    // Check & Create dirs
                    $sgcp->list_create_dir($list_update_file[$j]);
                    // Backup files
                    backup_file($list_update_file[$j], $date);
                    if (!($handle = fopen($list_update_file[$j], 'w+'))) {
                        cp_update_set_msg(5, $list_update_file[$j]);
                        exit;
                    } else {
                        cp_update_set_msg(2, $list_update_file[$j]);
                        if (fwrite($handle, $tmp_update_result[$i][$j]) === FALSE) {
                            cp_update_set_msg(5, $list_update_file[$j]);
                            exit;
                        } else {
                            $count_success++;
                            $percent = $count_success / $count_files * 100;
                            $percent = (int) $percent;
                            cp_update_set_msg(3, $percent);
                        }
                        fclose($handle);
                    }
                }
            }
        }
        cp_update_set_msg(0, 3);
    }
}