Пример #1
0
function ftp_copymovedelete($conn_id_source, $conn_id_target, $list, $copymovedelete, $divelevel)
{
    // --------------
    // This function copies/moves/deletes directories and files from an FTP server to the same
    // or another FTP server.
    //
    // Note there is a difference between move on the same FTP server, and move to another FTP
    // server. Move on the same FTP server is done by renaming the directories and files because
    // this is much faster.
    //
    // For copy (on same or another FTP server) and move (to another FTP server), files
    // are first transferred from the source FTP server to the webserver, and then transferred
    // to the target FTP server.
    //
    // $list[$i]["dirorfile"] contains d or - which indicates if its a directory or a file
    // $list[$i]["dirfilename"] contains the entry name
    // $list[$i]["sourcedirectory"] contains the source directory
    // $list[$i]["targetdirectory"] contains the target directory
    // $list[$i]["newname"] contains the new name if divelevel = 0; for deeper levels the newname is the entry name itself
    // --------------
    // -------------------------------------------------------------------------
    // Global variables
    // -------------------------------------------------------------------------
    global $net2ftp_globals, $net2ftp_settings, $net2ftp_result, $net2ftp_output;
    // -------------------------------------------------------------------------
    // Initialization
    // -------------------------------------------------------------------------
    if ($divelevel == 0) {
        $net2ftp_output["ftp_copymovedelete"][] = "<ul>";
    }
    // Total number of directories and files on level 0 (chosen by the user)
    $total_dirs_files = $list["stats"]["directories"]["total_number"] + $list["stats"]["files"]["total_number"];
    // -------------------------------------------------------------------------
    // For all directories
    // -------------------------------------------------------------------------
    for ($i = 1; $i <= $list["stats"]["directories"]["total_number"]; $i = $i + 1) {
        // ------------------------------
        // Set the status
        // ------------------------------
        $message = __("Processing entry %1\$s", javascriptEncode2($list["directories"][$i]["dirfilename"])) . " ({$i}/{$total_dirs_files})";
        setStatus($i, $total_dirs_files, $message);
        // ------------------------------
        // Source and target
        // ------------------------------
        $source = glueDirectories($list["directories"][$i]["sourcedirectory"], $list["directories"][$i]["dirfilename"]);
        if ($copymovedelete == "copy" || $copymovedelete == "move") {
            if ($divelevel > 0) {
                $target = glueDirectories($list["directories"][$i]["targetdirectory"], $list["directories"][$i]["dirfilename"]);
            } else {
                $target = glueDirectories($list["directories"][$i]["targetdirectory"], $list["directories"][$i]["newname"]);
            }
            // First-level user-selected directories can have been renamed
        } else {
            $target = "";
        }
        // ------------------------------
        // Print starting message
        // ------------------------------
        $net2ftp_output["ftp_copymovedelete"][] = __("Processing directory <b>%1\$s</b>", $source);
        $net2ftp_output["ftp_copymovedelete"][] = "<ul>";
        // ------------------------------
        // Check that the targetdirectory is not a subdirectory of the sourcedirectory
        // ------------------------------
        if ($conn_id_source == $conn_id_target && $copymovedelete != "delete" && isSubdirectory($source, $target) == true) {
            $net2ftp_output["ftp_copymovedelete"][] = __("The target directory <b>%1\$s</b> is the same as or a subdirectory of the source directory <b>%2\$s</b>, so this directory will be skipped", $target, $source);
            $net2ftp_output["ftp_copymovedelete"][] = "</ul>";
            continue;
        }
        // ------------------------------
        // Check if the directory contains a banned keyword
        // ------------------------------
        // If banned keyword - copy: continue
        // If banned keyword - move: abort
        if ($list["directories"][$i]["selectable"] == "banned_keyword") {
            if ($copymovedelete == "copy") {
                $net2ftp_output["ftp_copymovedelete"][] = __("The directory <b>%1\$s</b> contains a banned keyword, so this directory will be skipped", $source);
                $net2ftp_output["ftp_copymovedelete"][] = "</ul>";
                continue;
            } elseif ($copymovedelete == "move") {
                $net2ftp_output["ftp_copymovedelete"][] = __("The directory <b>%1\$s</b> contains a banned keyword, aborting the move", $source);
                $net2ftp_output["ftp_copymovedelete"][] = "</ul>";
                return false;
            }
        }
        // ------------------------------
        // For copy (to the same or another FTP server), move (to another FTP server) or delete
        // ------------------------------
        if ($copymovedelete == "copy" || $copymovedelete == "move" && $conn_id_source != $conn_id_target || $copymovedelete == "delete") {
            // Create the targetdirectory
            if ($copymovedelete == "copy" || $copymovedelete == "move") {
                $success1 = ftp_mkdir($conn_id_target, $target);
                if ($success1 == false) {
                    $net2ftp_output["ftp_copymovedelete"][] = __("Unable to create the subdirectory <b>%1\$s</b>. It may already exist. Continuing the copy/move process...", $target);
                } else {
                    $net2ftp_output["ftp_copymovedelete"][] = __("Created target subdirectory <b>%1\$s</b>", $target);
                }
            }
            // Get a new list
            $newlist = ftp_getlist($conn_id_source, $source);
            if ($net2ftp_result["success"] == false) {
                $net2ftp_output["ftp_copymovedelete"][] = __("The directory <b>%1\$s</b> could not be selected, so this directory will be skipped", $source);
                $net2ftp_output["ftp_copymovedelete"][] = "</ul>";
                setErrorVars(true, "", "", "", "");
                continue;
            }
            // Add information to the list
            for ($j = 1; $j <= $newlist["stats"]["directories"]["total_number"]; $j++) {
                $newlist["directories"][$j]["sourcedirectory"] = $source;
                $newlist["directories"][$j]["targetdirectory"] = $target;
            }
            for ($j = 1; $j <= $newlist["stats"]["files"]["total_number"]; $j++) {
                $newlist["files"][$j]["sourcedirectory"] = $source;
                $newlist["files"][$j]["targetdirectory"] = $target;
            }
            // Call the function recursively
            $newdivelevel = $divelevel + 1;
            $ftp_copymovedelete_result = ftp_copymovedelete($conn_id_source, $conn_id_target, $newlist, $copymovedelete, $newdivelevel);
            // Delete the source directory
            // (Only if there were no problems in the recursive call to ftp_copymovedelete() above)
            if ($ftp_copymovedelete_result == true && ($copymovedelete == "move" || $copymovedelete == "delete")) {
                ftp_rmdir2($conn_id_source, $source);
                if ($net2ftp_result["success"] == false) {
                    setErrorVars(true, "", "", "", "");
                    $net2ftp_output["ftp_copymovedelete"][] = __("Unable to delete the subdirectory <b>%1\$s</b> - it may not be empty", $source);
                } else {
                    $net2ftp_output["ftp_copymovedelete"][] = __("Deleted subdirectory <b>%1\$s</b>", $source);
                }
            }
        }
        // end if copy, move to another FTP server, delete
        // ------------------------------
        // For move (to the same FTP server)
        // ------------------------------
        if ($copymovedelete == "move" && $conn_id_source == $conn_id_target) {
            ftp_rename3($conn_id_source, $source, $target);
            if ($net2ftp_result["success"] == false) {
                setErrorVars(true, "", "", "", "");
                $net2ftp_output["ftp_copymovedelete"][] = __("Unable to move the directory <b>%1\$s</b>", $source);
                $net2ftp_output["ftp_copymovedelete"][] = "</ul>";
                continue;
            } else {
                $net2ftp_output["ftp_copymovedelete"][] = __("Moved directory <b>%1\$s</b>", $source);
            }
        }
        // end if move to the same FTP server
        // ------------------------------
        // Print ending message
        // ------------------------------
        $net2ftp_output["ftp_copymovedelete"][] = __("Processing of directory <b>%1\$s</b> completed", $source);
        $net2ftp_output["ftp_copymovedelete"][] = "</ul>";
    }
    // end for list_directories
    // -------------------------------------------------------------------------
    // Process the files
    // -------------------------------------------------------------------------
    for ($i = 1; $i <= $list["stats"]["files"]["total_number"]; $i = $i + 1) {
        // ------------------------------
        // Set the status
        // ------------------------------
        $j = $list["stats"]["directories"]["total_number"] + $i;
        $message = __("Processing entry %1\$s", javascriptEncode2($list["files"][$i]["dirfilename"])) . " ({$j}/{$total_dirs_files})";
        setStatus($j, $total_dirs_files, $message);
        // ------------------------------
        // Source and target
        // ------------------------------
        $source = glueDirectories($list["files"][$i]["sourcedirectory"], $list["files"][$i]["dirfilename"]);
        if ($copymovedelete == "copy" || $copymovedelete == "move") {
            if (isset($list["files"][$i]["newname"])) {
                $target = glueDirectories($list["files"][$i]["targetdirectory"], $list["files"][$i]["newname"]);
            } else {
                $target = glueDirectories($list["files"][$i]["targetdirectory"], $list["files"][$i]["dirfilename"]);
            }
        } else {
            $target = "";
        }
        // ------------------------------
        // Check that the target is not the same as the source file
        // ------------------------------
        if ($conn_id_source == $conn_id_target && $target == $source) {
            $net2ftp_output["ftp_copymovedelete"][] = __("The target for file <b>%1\$s</b> is the same as the source, so this file will be skipped", $source);
            continue;
        }
        // ------------------------------
        // Check if the file contains a banned keyword, and if it is not bigger than the limit
        // ------------------------------
        // If banned keyword or too big - copy: continue with the other files
        // If banned keyword or too big - move: abort
        if ($list["files"][$i]["selectable"] == "banned_keyword") {
            if ($copymovedelete == "copy") {
                $net2ftp_output["ftp_copymovedelete"][] = __("The file <b>%1\$s</b> contains a banned keyword, so this file will be skipped", $source);
                continue;
            } elseif ($copymovedelete == "move") {
                $net2ftp_output["ftp_copymovedelete"][] = __("The file <b>%1\$s</b> contains a banned keyword, aborting the move", $source);
                return false;
            }
        } elseif ($list["files"][$i]["selectable"] == "too_big") {
            if ($copymovedelete == "copy") {
                $net2ftp_output["ftp_copymovedelete"][] = __("The file <b>%1\$s</b> is too big to be copied, so this file will be skipped", $source);
                continue;
            } elseif ($copymovedelete == "move") {
                $net2ftp_output["ftp_copymovedelete"][] = __("The file <b>%1\$s</b> is too big to be moved, aborting the move", $source);
                return false;
            }
        }
        // ------------------------------------
        // For copy (to the same or another FTP server) or move (to another FTP server)
        // ------------------------------------
        if ($copymovedelete == "copy" || $copymovedelete == "move" && $conn_id_source != $conn_id_target) {
            // Get file from remote sourcedirectory to local temp directory
            // Don't delete the source file yet
            $localtargetdir = $net2ftp_globals["application_tempdir"];
            $localtargetfile = $list["files"][$i]["dirfilename"] . ".txt";
            $remotesourcedir = $list["files"][$i]["sourcedirectory"];
            $remotesourcefile = $list["files"][$i]["dirfilename"];
            $ftpmode = ftpAsciiBinary($list["files"][$i]["dirfilename"]);
            $copymove = "copy";
            ftp_getfile($conn_id_source, $localtargetdir, $localtargetfile, $remotesourcedir, $remotesourcefile, $ftpmode, $copymove);
            if ($net2ftp_result["success"] == false) {
                setErrorVars(true, "", "", "", "");
                if ($copymovedelete == "copy") {
                    $net2ftp_output["ftp_copymovedelete"][] = __("Unable to copy the file <b>%1\$s</b>", $list["files"][$i]["dirfilename"]);
                    continue;
                } elseif ($copymovedelete == "move") {
                    $net2ftp_output["ftp_copymovedelete"][] = __("Unable to move the file <b>%1\$s</b>, aborting the move", $list["files"][$i]["dirfilename"]);
                    return false;
                }
            }
            // Put file from local temp directory to remote targetdirectory
            // Delete the temporary file
            $localsourcedir = $net2ftp_globals["application_tempdir"];
            $localsourcefile = $list["files"][$i]["dirfilename"] . ".txt";
            $remotetargetdir = $list["files"][$i]["targetdirectory"];
            if (isset($list["files"][$i]["newname"])) {
                $remotetargetfile = $list["files"][$i]["newname"];
            } else {
                $remotetargetfile = $list["files"][$i]["dirfilename"];
            }
            $copymove = "move";
            ftp_putfile($conn_id_target, $localsourcedir, $localsourcefile, $remotetargetdir, $remotetargetfile, $ftpmode, $copymove);
            if ($net2ftp_result["success"] == false) {
                setErrorVars(true, "", "", "", "");
                if ($copymovedelete == "copy") {
                    $net2ftp_output["ftp_copymovedelete"][] = __("Unable to copy the file <b>%1\$s</b>", $list["files"][$i]["dirfilename"]);
                    continue;
                } elseif ($copymovedelete == "move") {
                    $net2ftp_output["ftp_copymovedelete"][] = __("Unable to move the file <b>%1\$s</b>, aborting the move", $list["files"][$i]["dirfilename"]);
                    return false;
                }
            } elseif ($net2ftp_result["success"] == true && $copymovedelete == "copy") {
                $net2ftp_output["ftp_copymovedelete"][] = __("Copied file <b>%1\$s</b>", $list["files"][$i]["dirfilename"]);
            } elseif ($copymovedelete == "move") {
                $remotesource = glueDirectories($list["files"][$i]["sourcedirectory"], $list["files"][$i]["dirfilename"]);
                ftp_delete2($conn_id_source, $remotesource);
                if ($net2ftp_result["success"] == false) {
                    setErrorVars(true, "", "", "", "");
                    $net2ftp_output["ftp_copymovedelete"][] = __("Unable to move the file <b>%1\$s</b>", $list["files"][$i]["dirfilename"]);
                } else {
                    $net2ftp_output["ftp_copymovedelete"][] = __("Moved file <b>%1\$s</b>", $list["files"][$i]["dirfilename"]);
                }
            }
        } elseif ($copymovedelete == "move" && $conn_id_source == $conn_id_target) {
            ftp_rename3($conn_id_source, $source, $target);
            if ($net2ftp_result["success"] == false) {
                setErrorVars(true, "", "", "", "");
                $net2ftp_output["ftp_copymovedelete"][] = __("Unable to move the file <b>%1\$s</b>", $source);
            } else {
                $net2ftp_output["ftp_copymovedelete"][] = __("Moved file <b>%1\$s</b>", $source);
            }
        } elseif ($copymovedelete == "delete") {
            $remotesource = glueDirectories($list["files"][$i]["sourcedirectory"], $list["files"][$i]["dirfilename"]);
            ftp_delete2($conn_id_source, $remotesource);
            if ($net2ftp_result["success"] == false) {
                setErrorVars(true, "", "", "", "");
                $net2ftp_output["ftp_copymovedelete"][] = __("Unable to delete the file <b>%1\$s</b>", $list["files"][$i]["dirfilename"]);
                continue;
            } else {
                $net2ftp_output["ftp_copymovedelete"][] = __("Deleted file <b>%1\$s</b>", $list["files"][$i]["dirfilename"]);
            }
        }
        // end delete
    }
    // end for list_files
    if ($divelevel == 0) {
        $net2ftp_output["ftp_copymovedelete"][] = "</ul>";
    }
    // Print message
    if ($divelevel == 0) {
        $net2ftp_output["ftp_copymovedelete"][] = __("All the selected directories and files have been processed.");
    }
    return true;
}
Пример #2
0
function checkAuthorizedDirectory($directory)
{
    // --------------
    // 1 - This function checks whether the current $directory name contains a banned
    // keyword.
    // 2 - It also checks if the current $directory is a subdirectory of the
    // homedirectory. The rootdirectory is first checked for the current user;
    // if this is not set, the default rootdirectory is checked.
    // --------------
    // -------------------------------------------------------------------------
    // Global variables
    // -------------------------------------------------------------------------
    global $net2ftp_globals, $net2ftp_settings, $net2ftp_result;
    // -------------------------------------------------------------------------
    // 1 - Check if the directory name contains a banned keyword
    // -------------------------------------------------------------------------
    if (checkAuthorizedName($directory) == false) {
        return false;
    }
    // -------------------------------------------------------------------------
    // 2 - Check if the directory is a subdirectory of the homedirectory (set in the DB)
    // -------------------------------------------------------------------------
    // ----------------------------------------------
    // Initial checks
    // ----------------------------------------------
    if ($net2ftp_settings["use_database"] != "yes" || $net2ftp_settings["check_homedirectory"] != "yes") {
        return true;
    }
    // ----------------------------------------------
    // Get the homedirectory from the database, then store it in a global
    // variable, and from then on, don't access the database any more
    // ----------------------------------------------
    $net2ftp_globals["homedirectory"] = getRootdirectory();
    // ----------------------------------------------
    // Check if the current directory is a subdirectory of the homedirectory
    // ----------------------------------------------
    if (isSubdirectory($net2ftp_globals["homedirectory"], $directory) == false) {
        return false;
    } else {
        return true;
    }
}