Example #1
0
/** Recursively copy a directory
 */
function recursive_copy($dirsourceparent, $dirdest, &$md5sums, $dirsource = "")
{
    global $dryrun;
    global $check_md5s;
    global $amp_conf;
    global $asterisk_conf;
    global $install_moh;
    global $make_links;
    $moh_subdir = isset($amp_conf['MOHDIR']) ? trim(trim($amp_conf['MOHDIR']), '/') : 'mohmp3';
    // total # files, # actually copied
    $num_files = $num_copied = 0;
    if ($dirsource && $dirsource[0] != "/") {
        $dirsource = "/" . $dirsource;
    }
    if (is_dir($dirsourceparent . $dirsource)) {
        $dir_handle = opendir($dirsourceparent . $dirsource);
    }
    /*
    echo "dirsourceparent: "; var_dump($dirsourceparent);
    echo "dirsource: "; var_dump($dirsource);
    echo "dirdest: "; var_dump($dirdest);
    */
    while (isset($dir_handle) && ($file = readdir($dir_handle))) {
        if ($file != "." && $file != ".." && $file != "CVS" && $file != ".svn") {
            $source = $dirsourceparent . $dirsource . "/" . $file;
            $destination = $dirdest . $dirsource . "/" . $file;
            if ($dirsource == "" && $file == "moh" && !$install_moh) {
                // skip to the next dir
                continue;
            }
            // configurable in amportal.conf
            if (strpos($destination, "htdocs_panel") == 1) {
                $destination = str_replace("/htdocs_panel", trim($amp_conf["FOPWEBROOT"]), $destination);
            } else {
                $destination = str_replace("/htdocs", trim($amp_conf["AMPWEBROOT"]), $destination);
            }
            $destination = str_replace("/htdocs_panel", trim($amp_conf["FOPWEBROOT"]), $destination);
            //			$destination=str_replace("/cgi-bin",trim($amp_conf["AMPCGIBIN"]),$destination);
            if (strpos($dirsource, 'modules') === false) {
                $destination = str_replace("/bin", trim($amp_conf["AMPBIN"]), $destination);
            }
            $destination = str_replace("/sbin", trim($amp_conf["AMPSBIN"]), $destination);
            // the following are configurable in asterisk.conf
            $destination = str_replace("/astetc", trim($asterisk_conf["astetcdir"]), $destination);
            $destination = str_replace("/moh", trim($asterisk_conf["astvarlibdir"]) . "/{$moh_subdir}", $destination);
            $destination = str_replace("/astvarlib", trim($asterisk_conf["astvarlibdir"]), $destination);
            if (strpos($dirsource, 'modules') === false) {
                $destination = str_replace("/agi-bin", trim($asterisk_conf["astagidir"]), $destination);
            }
            if (strpos($dirsource, 'modules') === false) {
                $destination = str_replace("/sounds", trim($asterisk_conf["astvarlibdir"]) . "/sounds", $destination);
            }
            // if this is a directory, ensure destination exists
            if (is_dir($source)) {
                if (!file_exists($destination)) {
                    if (!$dryrun && $destination != "") {
                        amp_mkdir($destination, "0750", true);
                    }
                }
            }
            //var_dump($md5sums);
            if (!is_dir($source)) {
                $md5_source = preg_replace("|^/?amp_conf/|", "/", $source);
                if ($check_md5s && file_exists($destination) && isset($md5sums[$md5_source]) && md5_file($destination) != $md5sums[$md5_source]) {
                    // double check using diff utility (and ignoring whitespace)
                    // This is a somewhat edge case (eg, the file doesn't match
                    // it's md5 sum from the previous version, but no substantial
                    // changes exist compared to the current version), but it
                    // pervents a useless prompt to the user.
                    if (checkDiff($source, $destination)) {
                        $overwrite = ask_overwrite($source, $destination);
                    } else {
                        debug("NOTE: MD5 for " . $destination . " was different, but `diff` did not detect any (non-whitespace) changes: overwriting");
                        $overwrite = true;
                    }
                } else {
                    $overwrite = true;
                }
                $num_files++;
                if ($overwrite) {
                    debug(($make_links ? "link" : "copy") . " " . $source . " -> " . $destination);
                    if (!$dryrun) {
                        if ($make_links) {
                            // symlink, unlike copy, doesn't overwrite - have to delete first
                            if (is_link($destination) || file_exists($destination)) {
                                unlink($destination);
                            }
                            symlink($_ENV["PWD"] . "/" . $source, $destination);
                        } else {
                            copy($source, $destination);
                        }
                        $num_copied++;
                    }
                } else {
                    debug("not overwriting " . $destination);
                }
            } else {
                //echo "recursive_copy($dirsourceparent, $dirdest, $md5sums, $dirsource/$file)";
                list($tmp_num_files, $tmp_num_copied) = recursive_copy($dirsourceparent, $dirdest, $md5sums, $dirsource . "/" . $file);
                $num_files += $tmp_num_files;
                $num_copied += $tmp_num_copied;
            }
        }
    }
    if (isset($dir_handle)) {
        closedir($dir_handle);
    }
    return array($num_files, $num_copied);
}
Example #2
0
/** Recursively copy a directory
 */
function recursive_copy($dirsourceparent, $dirdest, &$md5sums, $dirsource = "")
{
    global $dryrun;
    global $check_md5s;
    global $amp_conf;
    global $asterisk_conf;
    global $install_moh;
    global $make_links;
    $overwrite = false;
    $moh_subdir = isset($amp_conf['MOHDIR']) ? trim(trim($amp_conf['MOHDIR']), '/') : 'mohmp3';
    // total # files, # actually copied
    $num_files = $num_copied = 0;
    if ($dirsource && $dirsource[0] != "/") {
        $dirsource = "/" . $dirsource;
    }
    if (is_dir($dirsourceparent . $dirsource)) {
        $dir_handle = opendir($dirsourceparent . $dirsource);
    }
    /*
    echo "dirsourceparent: "; var_dump($dirsourceparent);
    echo "dirsource: "; var_dump($dirsource);
    echo "dirdest: "; var_dump($dirdest);
    */
    while (isset($dir_handle) && ($file = readdir($dir_handle))) {
        if ($file != "." && $file != ".." && $file != "CVS" && $file != ".svn" && $file != ".git") {
            $source = $dirsourceparent . $dirsource . "/" . $file;
            $destination = $dirdest . $dirsource . "/" . $file;
            if ($dirsource == "" && $file == "moh" && !$install_moh) {
                // skip to the next dir
                continue;
            }
            // configurable in amportal.conf
            $destination = preg_replace("/^\\/htdocs\\//i", trim($amp_conf["AMPWEBROOT"]) . "/", $destination);
            if (strpos($dirsource, 'modules') === false) {
                $destination = str_replace("/bin", trim($amp_conf["AMPBIN"]), $destination);
            }
            $destination = str_replace("/sbin", trim($amp_conf["AMPSBIN"]), $destination);
            // the following are configurable in asterisk.conf
            $destination = str_replace("/astetc", trim($asterisk_conf["astetcdir"]), $destination);
            $destination = str_replace("/moh", trim($asterisk_conf["astvarlibdir"]) . "/{$moh_subdir}", $destination);
            $destination = str_replace("/astvarlib", trim($asterisk_conf["astvarlibdir"]), $destination);
            if (strpos($dirsource, 'modules') === false) {
                $destination = str_replace("/agi-bin", trim($asterisk_conf["astagidir"]), $destination);
            }
            if (strpos($dirsource, 'modules') === false) {
                $destination = str_replace("/sounds", trim($asterisk_conf["astvarlibdir"]) . "/sounds", $destination);
            }
            // if this is a directory, ensure destination exists
            if (is_dir($source)) {
                if (!file_exists($destination)) {
                    if (!$dryrun && $destination != "") {
                        amp_mkdir($destination, "0750", true);
                    }
                }
            }
            //var_dump($md5sums);
            if (!is_dir($source)) {
                $md5_source = preg_replace("|^/?amp_conf/|", "/", $source);
                if ($check_md5s && file_exists($destination) && isset($md5sums[$md5_source]) && md5_file($destination) != $md5sums[$md5_source]) {
                    // double check using diff utility (and ignoring whitespace)
                    // This is a somewhat edge case (eg, the file doesn't match
                    // it's md5 sum from the previous version, but no substantial
                    // changes exist compared to the current version), but it
                    // prevents a useless prompt to the user.
                    if (checkDiff($source, $destination)) {
                        $overwrite = ask_overwrite($source, $destination);
                    } else {
                        debug("NOTE: MD5 for " . $destination . " was different, but `diff` did not detect any (non-whitespace) changes: overwriting");
                        $overwrite = true;
                    }
                } else {
                    $overwrite = true;
                }
                // These are modified by apply_conf.sh, there may be others that fit in this category also. This keeps these from
                // being symlinked and then developers inadvertently checking in the changes when they should not have.
                //
                $never_symlink = array("cdr_mysql.conf", "manager.conf", "vm_email.inc", "modules.conf");
                $num_files++;
                if ($overwrite) {
                    debug(($make_links ? "link" : "copy") . " " . $source . " -> " . $destination);
                    if (!$dryrun) {
                        if ($make_links && !in_array(basename($source), $never_symlink)) {
                            // symlink, unlike copy, doesn't overwrite - have to delete first
                            if (is_link($destination) || file_exists($destination)) {
                                unlink($destination);
                            }
                            $links = recursive_readlink($source);
                            if (!empty($links)) {
                                @symlink(substitute_readlinks($source, $links), $destination);
                            } else {
                                if (file_exists(dirname(__FILE__) . "/" . $source)) {
                                    @symlink(dirname(__FILE__) . "/" . $source, $destination);
                                }
                            }
                        } else {
                            $ow = false;
                            if (file_exists($destination)) {
                                if (checkDiff($source, $destination) && !$make_links) {
                                    $ow = ask_overwrite($source, $destination);
                                } elseif ($make_links) {
                                    $ow = false;
                                }
                            } else {
                                $ow = true;
                            }
                            //ask_overwrite
                            if ($ow) {
                                //Copy will not overwrite a symlink, phpnesssss
                                if (file_exists($destination) && is_link($destination)) {
                                    unlink($destination);
                                }
                                copy($source, $destination);
                            } else {
                                continue;
                            }
                        }
                        $num_copied++;
                    }
                } else {
                    debug("not overwriting " . $destination);
                }
            } else {
                list($tmp_num_files, $tmp_num_copied) = recursive_copy($dirsourceparent, $dirdest, $md5sums, $dirsource . "/" . $file);
                $num_files += $tmp_num_files;
                $num_copied += $tmp_num_copied;
            }
        }
    }
    if (isset($dir_handle)) {
        closedir($dir_handle);
    }
    return array($num_files, $num_copied);
}