function glueDirectories($part1, $part2)
{
    // --------------
    // Returns the 2 dirs glued together in the format
    //    /home/dh1234/test                (leading /, NO trailing /)
    //    \\server\web\mysite.com\net2ftp
    // --------------
    // Strip leading and trailing / and \
    // Take Windows shared directory into account \\server\web\mysite.com\net2ftp
    if (substr($part1, 0, 2) == "\\\\") {
        $part1 = "\\\\" . stripDirectory($part1);
    } else {
        $part1 = stripDirectory($part1);
    }
    if (substr($part2, 0, 2) == "\\\\") {
        $part2 = "\\\\" . stripDirectory($part2);
    } else {
        $part2 = stripDirectory($part2);
    }
    // Length
    $part1_len = strlen($part1);
    $part2_len = strlen($part2);
    // Check if Unix or Windows style directories are used
    if ($part1_len > 1 && (substr($part1, 1, 1) == ":" || substr($part1, 0, 2) == "\\\\")) {
        $system = "windows";
    } elseif ($part2_len > 1 && (substr($part2, 1, 1) == ":" || substr($part1, 0, 2) == "\\\\")) {
        $system = "windows";
    } else {
        $system = "unix";
    }
    // Glue the 2 parts together
    if ($part1_len > 0 && $part2_len > 0) {
        if ($system == "windows") {
            return $part1 . "\\" . $part2;
        } else {
            return "/" . $part1 . "/" . $part2;
        }
    } elseif (($part1_len == 0 || $part1 == "/" || $part1 == "\\") && $part2_len > 0) {
        if ($system == "windows") {
            return $part2;
        } else {
            return "/" . $part2;
        }
    } elseif (($part2_len == 0 || $part2 == "/" || $part2 == "\\") && $part1_len > 0) {
        if ($system == "windows") {
            return $part1;
        } else {
            return "/" . $part1;
        }
    } else {
        return "";
    }
}
function net2ftp_module_printBody()
{
    // --------------
    // This function prints the browse screen ($state2=="main") or the directory popup screen ($state2=="popup")
    // For the browse screen ($state2=="main"), 2 template files are called
    // --------------
    // -------------------------------------------------------------------------
    // Global variables
    // -------------------------------------------------------------------------
    global $net2ftp_settings, $net2ftp_globals, $net2ftp_messages, $net2ftp_result;
    // -------------------------------------------------------------------------
    // Check if the directory name contains \' and if it does, print an error message
    // Note: these directories cannot be browsed, but can be deleted
    // -------------------------------------------------------------------------
    //	if (strstr($directory, "\'") != false) {
    //		$errormessage = __("Directories with names containing \' cannot be displayed correctly. They can only be deleted. Please go back and select another subdirectory.");
    //		setErrorVars(false, $errormessage, debug_backtrace(), __FILE__, __LINE__);
    //		return false;
    //	}
    // -------------------------------------------------------------------------
    // Variables
    // With status update if $state2=="main"
    // -------------------------------------------------------------------------
    // ------------------------------------
    // Open connection
    // ------------------------------------
    if ($net2ftp_globals["state2"] == "main") {
        setStatus(2, 10, __("Connecting to the FTP server"));
    }
    $conn_id = ftp_openconnection();
    if ($net2ftp_result["success"] == false) {
        return false;
    }
    // ------------------------------------
    // Get raw list of directories and files; parse the raw list and return a nice list
    // This function may change the current $directory; a warning message is returned in that case
    // ------------------------------------
    if ($net2ftp_globals["state2"] == "main") {
        setStatus(4, 10, __("Getting the list of directories and files"));
    }
    $list = ftp_getlist($conn_id, $net2ftp_globals["directory"]);
    if ($net2ftp_result["success"] == false) {
        return false;
    }
    // ------------------------------------
    // Close connection
    // ------------------------------------
    ftp_closeconnection($conn_id);
    // ------------------------------------
    // Sort the list
    // ------------------------------------
    $list_directories = sort_list($list["directories"]);
    $list_files = sort_list($list["files"]);
    $list_symlinks = sort_list($list["symlinks"]);
    $list_unrecognized = sort_list($list["unrecognized"]);
    $warning_directory = $list["stats"]["warnings"];
    $directory = $list["stats"]["newdirectory"];
    $directory_html = htmlEncode2($directory);
    $directory_url = urlEncode2($directory);
    $directory_js = javascriptEncode2($directory);
    $updirectory = upDir($directory);
    $updirectory_html = htmlEncode2($updirectory);
    $updirectory_url = urlEncode2($updirectory);
    $updirectory_js = javascriptEncode2($updirectory);
    // ------------------------------------
    // Calculate the list of HTTP URLs
    // ------------------------------------
    if ($net2ftp_globals["state2"] == "main") {
        $list_links_js = ftp2http($net2ftp_globals["directory"], $list_files, "no");
        $list_links_url = ftp2http($net2ftp_globals["directory"], $list_files, "yes");
    }
    // ------------------------------------
    // Consumption message
    // ------------------------------------
    $warning_consumption = "";
    if (checkConsumption() == false) {
        $warning_consumption .= "<b>" . __("Daily limit reached: you will not be able to transfer data") . "</b><br /><br />\n";
        $warning_consumption .= __("In order to guarantee the fair use of the web server for everyone, the data transfer volume and script execution time are limited per user, and per day. Once this limit is reached, you can still browse the FTP server but not transfer data to/from it.") . "<br /><br />\n";
        $warning_consumption .= __("If you need unlimited usage, please install net2ftp on your own web server.") . "<br />\n";
    }
    // ------------------------------------
    // Browse message
    // ------------------------------------
    if ($net2ftp_settings["message_browse"] != "" && $net2ftp_settings["message_browse"] != "Setting message_browse does not exist") {
        $warning_message = $net2ftp_settings["message_browse"];
    }
    // ------------------------------------
    // Directory tree
    // ------------------------------------
    $directory_exploded = explode("/", stripDirectory($directory));
    if ($directory != "/" && checkAuthorizedDirectory("/") == true) {
        $directory_tree = "<a href=\"javascript:submitBrowseForm('/','','browse','main');\">root</a> ";
    } else {
        $directory_tree = "root ";
    }
    $directory_goto = "";
    for ($i = 0; $i < sizeof($directory_exploded) - 1; $i++) {
        $directory_goto = glueDirectories($directory_goto, $directory_exploded[$i]);
        $directory_goto_url = urlEncode2($directory_goto);
        if (checkAuthorizedDirectory($directory_goto) == true) {
            $directory_tree .= "/<a href=\"javascript:submitBrowseForm('" . $directory_goto_url . "','','browse','main');\">" . htmlEncode2($directory_exploded[$i]) . "</a> ";
        } else {
            $directory_tree .= "/" . $directory_exploded[$i] . " ";
        }
    }
    $directory_tree .= "/" . $directory_exploded[sizeof($directory_exploded) - 1];
    // ------------------------------------
    // Language
    // ------------------------------------
    $language_onchange = "document.BrowseForm.language.value=document.forms['BrowseForm'].language2.options[document.forms['BrowseForm'].language2.selectedIndex].value; submitBrowseForm('{$directory_js}', '', 'browse', 'main');";
    // ------------------------------------
    // Skin
    // ------------------------------------
    $skin_onchange = "document.BrowseForm.skin.value=document.forms['BrowseForm'].skin2.options[document.forms['BrowseForm'].skin2.selectedIndex].value; submitBrowseForm('{$directory_js}', '', 'browse', 'main');";
    // ------------------------------------
    // $rowcounter counts the total nr of rows
    // ------------------------------------
    $rowcounter = 0;
    // ------------------------------------
    // Column spans
    // ------------------------------------
    $action_colspan = 1;
    if ($net2ftp_settings["functionuse_view"] == "yes") {
        $action_colspan++;
    }
    if ($net2ftp_settings["functionuse_edit"] == "yes") {
        $action_colspan++;
    }
    if ($net2ftp_settings["functionuse_update"] == "yes") {
        $action_colspan++;
    }
    // Total nr of columns
    $total_colspan = $action_colspan + 9;
    // ------------------------------------
    // Name, Type, Size, ...
    // Determine the sort criteria and direction (ascending/descending)
    // ------------------------------------
    $sortArray["dirfilename"]["text"] = __("Name");
    $sortArray["type"]["text"] = __("Type");
    $sortArray["size"]["text"] = __("Size");
    $sortArray["owner"]["text"] = __("Owner");
    $sortArray["group"]["text"] = __("Group");
    $sortArray["permissions"]["text"] = __("Perms");
    $sortArray["mtime"]["text"] = __("Mod Time");
    $icon_directory = $net2ftp_globals["application_rootdir_url"] . "/skins/" . $net2ftp_globals["skin"] . "/images/mime";
    // Loop over all the sort possibilities
    while (list($key, $value) = each($sortArray)) {
        // The list is sorted by the current $key
        // Print the icon representing the current sortorder
        // Print the link to sort using the other sortorder
        if ($net2ftp_globals["sort"] == $key) {
            // Ascending
            if ($net2ftp_globals["sortorder"] == "ascending") {
                $sortArray[$key]["title"] = __("Click to sort by %1\$s in descending order", $value["text"]);
                $sortArray[$key]["onclick"] = "do_sort('" . $key . "','descending');";
                $icon = "ascend.png";
                $alt = __("Ascending order");
            } else {
                $sortArray[$key]["title"] = __("Click to sort by %1\$s in ascending order", $value["text"]);
                $sortArray[$key]["onclick"] = "do_sort('" . $key . "','ascending');";
                $icon = "descend.png";
                $alt = __("Descending order");
            }
        } else {
            $sortArray[$key]["title"] = __("Click to sort by %1\$s in ascending order", $value["text"]);
            $sortArray[$key]["onclick"] = "do_sort('" . $key . "','ascending');";
            $icon = "";
            $alt = "";
        }
        // The icon to be printed is determined above
        // Now, print the full HTML depending on the browser agent, version and platform
        if ($icon != "") {
            if ($net2ftp_globals["browser_agent"] == "IE" && ($net2ftp_globals["browser_version"] == "5.5" || $net2ftp_globals["browser_version"] == "6") && $net2ftp_globals["browser_platform"] == "Win") {
                $sortArray[$key]["icon"] = "<img src=\"{$icon_directory}/spacer.gif\"   alt=\"{$alt}\" style=\"border: 0px; width: 16px; height: 16px; filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='{$icon_directory}/{$icon}', sizingMethod='scale');\" />\n";
            } else {
                $sortArray[$key]["icon"] = "<img src=\"{$icon_directory}/{$icon}\"        alt=\"{$alt}\" style=\"border: 0px; width: 16px; height: 16px;\" />\n";
            }
        } else {
            $sortArray[$key]["icon"] = "";
        }
    }
    // ------------------------------------
    // popup - FormAndFieldname
    // ------------------------------------
    if (isset($_POST["FormAndFieldName"]) == true) {
        $FormAndFieldName = validateGenericInput($_POST["FormAndFieldName"]);
    } else {
        $FormAndFieldName = "";
    }
    // ------------------------------------
    // Action URL
    // Used for Up, Subdirectories, Files (download + actions)
    // ------------------------------------
    $action_url = printPHP_SELF("actions");
    // ------------------------------------
    // Data transfer statistics
    // Print this only if the consumption statistics are available (logging must be on, using a MySQL database)
    // ------------------------------------
    if (isset($net2ftp_globals["consumption_ipaddress_datatransfer"]) == true || isset($net2ftp_globals["consumption_ftpserver_datatransfer"]) == true) {
        $print_consumption = true;
        $consumption_ipaddress_datatransfer = formatFilesize($net2ftp_globals["consumption_ipaddress_datatransfer"]);
        $consumption_ftpserver_datatransfer = formatFilesize($net2ftp_globals["consumption_ftpserver_datatransfer"]);
    } else {
        $print_consumption = false;
    }
    // ------------------------------------
    // HTTP URL
    // ------------------------------------
    $list_files_tmp[1]["dirfilename_url"] = "";
    $httplink = ftp2http($directory, $list_files_tmp, "no");
    // -------------------------------------------------------------------------
    // Print the output - part 2
    // -------------------------------------------------------------------------
    if ($net2ftp_globals["state2"] == "main") {
        setStatus(6, 10, __("Printing the list of directories and files"));
        require_once $net2ftp_globals["application_skinsdir"] . "/" . $net2ftp_globals["skin"] . "/browse_main.template.php";
    } elseif ($net2ftp_globals["state2"] == "popup") {
        require_once $net2ftp_globals["application_skinsdir"] . "/" . $net2ftp_globals["skin"] . "/browse_popup.template.php";
    }
}
function validateDirectory($directory)
{
    // --------------
    // Input: "/dir1/dir2/dir3/../../dir4/dir5"
    // Output: "/dir1/dir4/dir5"
    // Remove the following characters \/:*?"<>|
    // --------------
    // -------------------------------------------------------------------------
    // Nothing to do if the directory is the root directory
    // -------------------------------------------------------------------------
    if ($directory == "") {
        return "";
    } elseif ($directory == "/") {
        return "/";
    }
    // -------------------------------------------------------------------------
    // Remove XSS code
    // -------------------------------------------------------------------------
    //	$directory = RemoveXSS($directory);
    // -------------------------------------------------------------------------
    // Check if the directory contains ".."
    // -------------------------------------------------------------------------
    if (strpos($directory, "..") === false) {
        $directory = "/" . stripDirectory($directory);
    } else {
        $directory = stripDirectory($directory);
        // Split down into parts
        // directoryparts[0] contains the first part, directoryparts[1] the second,...
        $directoryparts = explode("/", $directory);
        // Start from the end
        // If you encounter N times a "..", do not take into account the next N parts which are not ".."
        // Example: "/dir1/dir2/dir3/../../dir4/dir5"  ---->  "/dir1/dir4/dir5"
        $doubledotcounter = 0;
        $newdirectory = "";
        $sizeof_directoryparts = sizeof($directoryparts);
        for ($i = $sizeof_directoryparts - 1; $i >= 0; $i = $i - 1) {
            if ($directoryparts[$i] == "..") {
                $doubledotcounter = $doubledotcounter + 1;
            } else {
                if ($doubledotcounter == 0) {
                    $newdirectory = $directoryparts[$i] . "/" . $newdirectory;
                } elseif ($doubledotcounter > 0) {
                    $doubledotcounter = $doubledotcounter - 1;
                }
                // Don't add the part, and reduce the counter by 1
            }
        }
        // end for
        $directory = "/" . stripDirectory($newdirectory);
    }
    // end if else
    // Remove : * ? " < > |
    //	$directory = preg_replace("/[\\:\\*\\?\\\"\\<\\>\\|]/", "", $directory);
    // Remove : * ? < > |
    $directory = preg_replace("/[\\:\\*\\?\\<\\>\\|]/", "", $directory);
    return $directory;
}
function isSubdirectory($parentdir, $childdir)
{
    // --------------
    // Returns true if the childdir is a subdirectory of the parentdir
    // --------------
    // If the parentdir is empty or the root directory, then the childdir is
    // a the same as or a subdirectory of the parentdir
    if ($parentdir == "" || $parentdir == "/" || $parentdir == "\\") {
        return true;
    }
    // Strip the directories of leading and trailing slashes
    $parentdir = stripDirectory($parentdir);
    $childdir = stripDirectory($childdir);
    $parentdir_length = strlen($parentdir);
    // Check if the first characters of the childdir are different from the
    // parentdir. Example:
    //    parentdir: /home/abc
    //    childdir:  /home/blabla ==> false
    //    childdir:  /home/abcd    ==> continue further checks
    //    childdir:  /home/abc/xyz ==> continue further checks
    $childdir_firstchars = substr($childdir, 0, $parentdir_length);
    if ($childdir_firstchars != $parentdir) {
        return false;
    }
    // If the first characters of the childdir are identical to the parentdir,
    // check if the first next character of the childdir name is different.
    // Example:
    //    parentdir: /home/abc
    //    childdir:  /home/abcd    ==> false
    //    childdir:  /home/abc/xyz ==> true
    $childdir_nextchar = substr($childdir, $parentdir_length, 1);
    if ($childdir_nextchar != "/" && $childdir_nextchar != "\\") {
        return false;
    }
    return true;
}
function ftp_getlist($conn_id, $directory)
{
    // --------------
    // This function connects to the FTP server and returns an array with a list of directories and files.
    // One row per directory or file, with rows from index 1 to n
    //
    // Step 1: send ftp_rawlist request to the FTP server; this returns a string
    // Step 2: parse that string and get a first array ($templist)
    // Step 3: move the rows to another array, to index 1 to n ($list)
    //
    // This function is used in all functions which process directories recursively.
    // --------------
    // -------------------------------------------------------------------------
    // Global variables
    // -------------------------------------------------------------------------
    global $net2ftp_globals, $net2ftp_settings;
    // -------------------------------------------------------------------------
    // Initialization
    // -------------------------------------------------------------------------
    $warnings = "";
    // -------------------------------------------------------------------------
    // Step 1: Chdir to the directory and get the current directory
    // -------------------------------------------------------------------------
    // ----------------------------------------------
    // Step 1a - Directory is "/"
    // Chdir to the directory because otherwise the ftp_rawlist does not work on some FTP servers
    // ----------------------------------------------
    if ($directory == "/") {
        $result1a = @ftp_chdir($conn_id, $directory);
    } elseif ($directory == "") {
        $result1b = @ftp_chdir($conn_id, $directory);
        $directory = @ftp_pwd($conn_id);
    } else {
        // 1c1 - Replace \' by \\' to be able to delete directories with names containing \'
        $directory1 = str_replace("\\'", "\\\\'", $directory);
        // 1c2 - Chdir to the directory
        // This is to check if the directory exists, but also because otherwise
        // the ftp_rawlist does not work on some FTP servers.
        $result1c = @ftp_chdir($conn_id, $directory1);
        // 1c3 - If the first ftp_chdir returns false, try a second time without the leading /
        // Some Windows FTP servers do not work when you use a leading /
        if ($result1c == false) {
            $directory2 = stripDirectory($directory1);
            $result2 = @ftp_chdir($conn_id, $directory2);
            // 1c3 - If the second ftp_chdir also does not work:
            //           For the Browse screen ==> go to the user's root directory
            //           For all other screens ==> return error
            if ($result2 == false) {
                if ($net2ftp_globals["state"] == "browse") {
                    $rootdirectory = getRootdirectory();
                    // User's root directory is different from the current directory, so switch to it
                    if ($directory != $rootdirectory) {
                        $warnings .= __("The directory <b>%1\$s</b> does not exist or could not be selected, so the directory <b>%2\$s</b> is shown instead.", $directory, $rootdirectory);
                        $directory = $rootdirectory;
                        $result3 = @ftp_chdir($conn_id, $directory);
                    } else {
                        $errormessage = __("Your root directory <b>%1\$s</b> does not exist or could not be selected.", $directory);
                        setErrorVars(false, $errormessage, debug_backtrace(), __FILE__, __LINE__);
                    }
                } else {
                    $errormessage = __("The directory <b>%1\$s</b> could not be selected - you may not have sufficient rights to view this directory, or it may not exist.", $directory);
                    setErrorVars(false, $errormessage, debug_backtrace(), __FILE__, __LINE__);
                }
            }
            // end if result2
        }
        // end if result1
    }
    // end if / or "" or else
    // -------------------------------------------------------------------------
    // Step 2 - Get list of directories and files
    // The -a option is used to show the hidden files as well on some FTP servers
    // Some servers do not return anything when using -a, so in that case try again without the -a option
    // -------------------------------------------------------------------------
    $rawlist = ftp_rawlist($conn_id, "-a");
    if (sizeof($rawlist) <= 1) {
        $rawlist = ftp_rawlist($conn_id, "");
    }
    // -------------------------------------------------------------------------
    // Step 3 - Parse the raw list
    // -------------------------------------------------------------------------
    // ----------------------------------------------
    // Initialize variables
    // ----------------------------------------------
    $list["directories"] = array();
    $list["files"] = array();
    $list["symlinks"] = array();
    $list["unrecognized"] = array();
    $directory_index = 1;
    $file_index = 1;
    $symlink_index = 1;
    $unrecognized_index = 1;
    $list["stats"]["directories"]["total_number"] = 0;
    $list["stats"]["directories"]["total_size"] = 0;
    $list["stats"]["directories"]["total_skipped"] = 0;
    $list["stats"]["files"]["total_number"] = 0;
    $list["stats"]["files"]["total_size"] = 0;
    $list["stats"]["files"]["total_skipped"] = 0;
    $list["stats"]["symlinks"]["total_number"] = 0;
    $list["stats"]["symlinks"]["total_size"] = 0;
    $list["stats"]["symlinks"]["total_skipped"] = 0;
    $list["stats"]["unrecognized"]["total_number"] = 0;
    $list["stats"]["unrecognized"]["total_size"] = 0;
    $list["stats"]["unrecognized"]["total_skipped"] = 0;
    // ----------------------------------------------
    // Loop over the raw list lines
    // ----------------------------------------------
    $nr_entries_banned_keyword = 0;
    $nr_entries_too_big = 0;
    for ($i = 0; $i < sizeof($rawlist); $i++) {
        // ----------------------------------------------
        // Scan each line
        // ----------------------------------------------
        $listline = ftp_scanline($directory, $rawlist[$i]);
        // If $listline is empty (e.g. if it contained ".."), continue to the next line
        if ($listline == "") {
            continue;
        }
        // Encode the name for HTML and Javascript
        if (isset($listline["dirfilename"])) {
            $listline["dirfilename_html"] = htmlEncode2($listline["dirfilename"]);
            $listline["dirfilename_url"] = urlEncode2($listline["dirfilename"]);
            $listline["dirfilename_js"] = javascriptEncode2($listline["dirfilename"]);
        }
        // Check if the filename contains a forbidden keyword
        // If it does, then this line will not be selectable on the Browse screen
        // Note: even if "selectable" is set to true here, it can still be set to false just below if the filesize is too big
        if (checkAuthorizedName($listline["dirfilename"]) == true) {
            $listline["selectable"] = "ok";
        } else {
            $listline["selectable"] = "banned_keyword";
            $nr_entries_banned_keyword++;
        }
        // Check if the filesize is bigger than the maximum authorized filesize
        if ($listline["dirorfile"] == "-" && isset($listline["size"]) && is_numeric($listline["size"])) {
            if ($listline["selectable"] == "ok" && $listline["size"] > $net2ftp_settings["max_filesize"]) {
                $listline["selectable"] = "too_big";
                $nr_entries_too_big++;
            }
        }
        // Form the new directory filename and encode it too
        if ($listline["dirorfile"] == "d") {
            $listline["newdir"] = glueDirectories($directory, $listline["dirfilename"]);
            $listline["newdir_html"] = htmlEncode2($listline["newdir"]);
            $listline["newdir_url"] = urlEncode2($listline["newdir"]);
            $listline["newdir_js"] = javascriptEncode2($listline["newdir"]);
        }
        // ----------------------------------------------
        // Depending on if the line contained a directory/file/symlink/unrecognized
        // row, store the result in different variables
        // ----------------------------------------------
        if ($listline["dirorfile"] == "d") {
            $list["directories"][$directory_index] = $listline;
            $directory_index++;
            if (isset($listline["size"]) && is_numeric($listline["size"])) {
                $list["stats"]["directories"]["total_size"] = $list["stats"]["directories"]["total_size"] + $listline["size"];
            } else {
                $list["stats"]["directories"]["total_skipped"] = $list["stats"]["directories"]["total_skipped"] + 1;
            }
        } elseif ($listline["dirorfile"] == "-") {
            $list["files"][$file_index] = $listline;
            $file_index++;
            if (isset($listline["size"]) && is_numeric($listline["size"])) {
                $list["stats"]["files"]["total_size"] = $list["stats"]["files"]["total_size"] + $listline["size"];
            } else {
                $list["stats"]["files"]["total_skipped"] = $list["stats"]["files"]["total_skipped"] + 1;
            }
        } elseif ($listline["dirorfile"] == "l") {
            $list["symlinks"][$symlink_index] = $listline;
            $symlink_index++;
        } elseif ($listline["dirorfile"] == "u") {
            $list["unrecognized"][$unrecognized_index] = $listline;
            $unrecognized_index++;
        }
        // end elseif
    }
    // end for
    // Print a warning message if some directories, files or symlinks contain a banned keyword or if a file is
    // too big to be downloaded
    if ($nr_entries_banned_keyword > 0) {
        $warnings .= __("Entries which contain banned keywords can't be managed using net2ftp. This is to avoid Paypal or Ebay scams from being uploaded through net2ftp.");
        $warnings .= "<br />\n";
    }
    if ($nr_entries_too_big > 0) {
        $warnings .= __("Files which are too big can't be downloaded, uploaded, copied, moved, searched, zipped, unzipped, viewed or edited; they can only be renamed, chmodded or deleted.");
        $warnings .= "<br />\n";
    }
    // Store the warnings and new directory in $list["stats"]
    if (isset($warnings) == true) {
        $list["stats"]["warnings"] = $warnings;
    } else {
        $list["stats"]["warnings"] = "";
    }
    $list["stats"]["newdirectory"] = $directory;
    // Store the statistics
    $list["stats"]["directories"]["total_size_formated"] = formatFilesize($list["stats"]["directories"]["total_size"]);
    $list["stats"]["files"]["total_size_formated"] = formatFilesize($list["stats"]["files"]["total_size"]);
    $list["stats"]["directories"]["total_number"] = $directory_index - 1;
    $list["stats"]["files"]["total_number"] = $file_index - 1;
    $list["stats"]["symlinks"]["total_number"] = $symlink_index - 1;
    $list["stats"]["unrecognized"]["total_number"] = $unrecognized_index - 1;
    // Put everything together in $list["all"]
    $list["all"] = $list["directories"] + $list["files"] + $list["symlinks"] + $list["unrecognized"];
    // -------------------------------------------------------------------------
    // Step 4 - Return the result
    // -------------------------------------------------------------------------
    return $list;
    // -------------------------------------------------------------------------
    // Some documentation:
    // 1 - Some FTP servers return a total on the first line
    // 2 - Some FTP servers return . and .. in their list of directories
    // ftp_scanline does not return those entries.
    // -------------------------------------------------------------------------
    // 1 - After doing some tests on different public FTP servers, it appears that
    // they reply differently to the ftp_rawlist request:
    //      - some FTP servers, like ftp.belnet.be, start with a line summarizing how
    //        many subdirectories and files there are in the current directory. The
    //        real list of subdirectories and files starts on the second line.
    //               [0] => total 15
    //               [1] => drwxr-xr-x 11 BELNET Archive 512 Feb 6 2000 BELNET
    //               [2] => drwxr-xr-x 2 BELNET Archive 512 Oct 29 2001 FVD-SFI
    //      - some other FTP servers, like ftp.redhat.com/pub, start directly with the
    //        list of subdirectories and files.
    //               [0] => drwxr-xr-x 9 ftp ftp 4096 Jan 11 06:34 contrib
    //               [1] => drwxr-xr-x 13 ftp ftp 4096 Jan 29 21:59 redhat
    //               [2] => drwxrwsr-x 6 ftp ftp 4096 Jun 05 2002 up2date
    // 2 - Some FTP servers return "." and ".." as directories. These fake entries
    // have to be eliminated!
    // They would cause infinite loops in the copy/move/delete functions.
    //               [0] => drwxr-xr-x 5 80 www 512 Apr 10 09:39 .
    //               [1] => drwxr-xr-x 16 80 www 512 Apr 9 08:51 ..
    //               [2] => -rw-r--r-- 1 80 www 5647 Apr 9 08:12 _CHANGES_v0.5
    //               [3] => -rw-r--r-- 1 80 www 1239 Apr 9 08:12 _CREDITS_v0.5
}
Example #6
0
function glueDirectories($part1, $part2)
{
    // --------------
    // Returns the 2 dirs glued together in the format /home/dh1234/test (leading /, NO trailing /)
    // --------------
    // Strip leading and trailing / and \
    $part1 = stripDirectory($part1);
    $part2 = stripDirectory($part2);
    // Length
    $part1_len = strlen($part1);
    $part2_len = strlen($part2);
    // Check if Unix or Windows style directories are used
    if ($part1_len > 1 && substr($part1, 1, 1) == ":") {
        $system = "windows";
    } elseif ($part2_len > 1 && substr($part2, 1, 1) == ":") {
        $system = "windows";
    } else {
        $system = "unix";
    }
    // Glue the 2 parts together
    if ($part1_len > 0 && $part2_len > 0) {
        if ($system == "windows") {
            return $part1 . "\\" . $part2;
        } else {
            return "/" . $part1 . "/" . $part2;
        }
    } elseif (($part1_len == 0 || $part1 == "/" || $part1 == "\\") && $part2_len > 0) {
        if ($system == "windows") {
            return $part2;
        } else {
            return "/" . $part2;
        }
    } elseif (($part2_len == 0 || $part2 == "/" || $part2 == "\\") && $part1_len > 0) {
        if ($system == "windows") {
            return $part1;
        } else {
            return "/" . $part1;
        }
    } else {
        return "";
    }
}