Beispiel #1
0
function net2ftp_module_printBody()
{
    // --------------
    // This function prints the chmod screen
    // --------------
    // -------------------------------------------------------------------------
    // Global variables
    // -------------------------------------------------------------------------
    global $net2ftp_settings, $net2ftp_globals, $net2ftp_messages, $net2ftp_result, $net2ftp_output;
    if (isset($_POST["list"]) == true) {
        $list = getSelectedEntries($_POST["list"]);
    } else {
        $list = "";
    }
    // -------------------------------------------------------------------------
    // Variables for all screens
    // -------------------------------------------------------------------------
    // Title
    $title = __("Install software packages");
    // Form name, back and forward buttons
    $formname = "InstallForm";
    $back_onclick = "document.forms['" . $formname . "'].state.value='browse';document.forms['" . $formname . "'].state2.value='main';document.forms['" . $formname . "'].submit();";
    // -------------------------------------------------------------------------
    // Screen 1
    // -------------------------------------------------------------------------
    if ($net2ftp_globals["screen"] == 1) {
        // ----------------------------------------------
        // Read the net2ftp installer script template $text
        // ----------------------------------------------
        $templatefile = $net2ftp_globals["application_rootdir"] . "/modules/install/net2ftp_installer.txt";
        $handle = fopen($templatefile, "r");
        // Open the local template file for reading only
        if ($handle == false) {
            $errormessage = __("Unable to open the template file");
            setErrorVars(false, $errormessage, debug_backtrace(), __FILE__, __LINE__);
            return false;
        }
        clearstatcache();
        // for filesize
        $text = fread($handle, filesize($templatefile));
        if ($text == false) {
            $errormessage = __("Unable to read the template file");
            setErrorVars(false, $errormessage, debug_backtrace(), __FILE__, __LINE__);
            return false;
        }
        @fclose($handle);
        // ----------------------------------------------
        // Read the list of packages
        // ----------------------------------------------
        $packagelistfile_net2ftp = "http://www.net2ftp.com/package_list.txt";
        $packagelistfile_local = $net2ftp_globals["application_rootdir"] . "/modules/install/package_list.txt";
        // Get the list of packages from net2ftp.com
        $handle_net2ftp = @fopen($packagelistfile_net2ftp, "r");
        clearstatcache();
        // for filesize
        $packagelist_net2ftp = @fread($handle_net2ftp, filesize($packagelistfile_net2ftp));
        @fclose($handle_net2ftp);
        // If net2ftp.com can't be reached, get it from the local installation
        if ($packagelist_net2ftp == false) {
            $handle_local = @fopen($packagelistfile_local, "r");
            clearstatcache();
            // for filesize
            $packagelist_local = @fread($handle_local, filesize($packagelistfile_local));
            @fclose($handle_local);
        }
        // Issue an error message if no list could be read
        if ($packagelist_net2ftp != "") {
            $packagelist = $packagelist_net2ftp;
        } elseif ($packagelist_local != "") {
            $packagelist = $packagelist_local;
        } else {
            $errormessage = __("Unable to get the list of packages");
            setErrorVars(false, $errormessage, debug_backtrace(), __FILE__, __LINE__);
            return false;
        }
        // ----------------------------------------------
        // Security code
        // Random key generator by goochivasquez -at- gmail (15-Apr-2005 11:53)
        // ----------------------------------------------
        // Random key generator
        $keychars = "abcdefghijklmnopqrstuvwxyz0123456789";
        $length = 20;
        $security_code = "";
        for ($i = 0; $i < $length; $i++) {
            $security_code .= substr($keychars, rand(1, strlen($keychars)), 1);
        }
        // Random key generator
        $keychars = "abcdefghijklmnopqrstuvwxyz0123456789";
        $length = 5;
        $tempdir_extension = "";
        for ($i = 0; $i < $length; $i++) {
            $tempdir_extension .= substr($keychars, rand(1, strlen($keychars)), 1);
        }
        $tempdir_ftp = glueDirectories($net2ftp_globals["directory"], "net2ftp_temp_") . $tempdir_extension;
        // ----------------------------------------------
        // Replace certain values
        // ----------------------------------------------
        $text = str_replace("NET2FTP_SECURITY_CODE", $security_code, $text);
        $text = str_replace("NET2FTP_TEMPDIR_EXTENSION", $tempdir_extension, $text);
        $text = str_replace("NET2FTP_PACKAGELIST", $packagelist, $text);
        $text = str_replace("NET2FTP_FTP_SERVER", $net2ftp_globals["ftpserver"], $text);
        $text = str_replace("NET2FTP_FTPSERVER_PORT", $net2ftp_globals["ftpserverport"], $text);
        $text = str_replace("NET2FTP_USERNAME", $net2ftp_globals["username"], $text);
        $text = str_replace("NET2FTP_DIRECTORY", $net2ftp_globals["directory"], $text);
        // ----------------------------------------------
        // Open connection
        // ----------------------------------------------
        setStatus(2, 10, __("Connecting to the FTP server"));
        $conn_id = ftp_openconnection();
        if ($conn_id == false) {
            return false;
        }
        // ----------------------------------------------
        // Create temporary /net2ftp_temp directory
        // ----------------------------------------------
        setStatus(4, 10, __("Creating a temporary directory on the FTP server"));
        ftp_newdirectory($conn_id, $tempdir_ftp);
        if ($net2ftp_result["success"] == false) {
            setErrorVars(true, "", "", "", "");
        }
        // ----------------------------------------------
        // Chmodding the temporary /net2ftp_temp directory to 777
        // ----------------------------------------------
        setStatus(6, 10, __("Setting the permissions of the temporary directory"));
        $sitecommand = "chmod 0777 " . $tempdir_ftp;
        $ftp_site_result = @ftp_site($conn_id, $sitecommand);
        // ----------------------------------------------
        // Put a .htaccess in the /net2ftp_temp directory to avoid anyone else reading the contents it
        // (Works only for Apache web servers...)
        // ----------------------------------------------
        ftp_writefile($conn_id, $tempdir_ftp, ".htaccess", "deny from all");
        if ($net2ftp_result["success"] == false) {
            setErrorVars(true, "", "", "", "");
        }
        // ----------------------------------------------
        // Write the net2ftp installer script to the FTP server
        // ----------------------------------------------
        setStatus(8, 10, __("Copying the net2ftp installer script to the FTP server"));
        ftp_writefile($conn_id, $net2ftp_globals["directory"], "net2ftp_installer.php", $text);
        if ($net2ftp_result["success"] == false) {
            return false;
        }
        // ----------------------------------------------
        // Close connection
        // ----------------------------------------------
        ftp_closeconnection($conn_id);
        // ----------------------------------------------
        // Variables for screen 1
        // ----------------------------------------------
        // URL to the installer script
        $list_files[1]["dirfilename_js"] = "net2ftp_installer.php?security_code=" . $security_code;
        $ftp2http_result = ftp2http($net2ftp_globals["directory"], $list_files, "no");
        $net2ftp_installer_url = $ftp2http_result[1];
    }
    // end if
    // -------------------------------------------------------------------------
    // Print the output
    // -------------------------------------------------------------------------
    require_once $net2ftp_globals["application_skinsdir"] . "/" . $net2ftp_globals["skin"] . "/manage.template.php";
}
function ftp_unziptransferfiles($archivesArray)
{
    // --------------
    // Extract the directories and files from the archive to a temporary directory on the web server, and
    // then create the directories and put the files on the FTP server
    // --------------
    // -------------------------------------------------------------------------
    // Global variables
    // -------------------------------------------------------------------------
    global $net2ftp_globals, $net2ftp_result, $net2ftp_output;
    // -------------------------------------------------------------------------
    // Open connection
    // -------------------------------------------------------------------------
    $conn_id = ftp_openconnection();
    if ($net2ftp_result["success"] == false) {
        for ($archive_nr = 1; $archive_nr <= sizeof($archivesArray); $archive_nr++) {
            @unlink($archivesArray[$archive_nr]["tmp_name"]);
        }
        return false;
    }
    // -------------------------------------------------------------------------
    // For each archive...
    // -------------------------------------------------------------------------
    for ($archive_nr = 1; $archive_nr <= sizeof($archivesArray); $archive_nr++) {
        // Set status
        setStatus($archive_nr, sizeof($archivesArray), __("Decompressing archives and transferring files"));
        // -------------------------------------------------------------------------
        // Determine the type of archive depending on the filename extension
        // -------------------------------------------------------------------------
        $archive_name = $archivesArray[$archive_nr]["name"];
        $archive_file = $archivesArray[$archive_nr]["tmp_name"];
        $archivename_without_dottext = substr($archivesArray[$archive_nr]["tmp_name"], 0, strlen($archive) - 4);
        $archive_type = get_filename_extension($archivename_without_dottext);
        $net2ftp_output["ftp_unziptransferfiles"][] = __("Processing archive nr %1\$s: <b>%2\$s</b>", $archive_nr, $archive_name);
        $net2ftp_output["ftp_unziptransferfiles"][] = "<ul>";
        if ($archive_type != "zip" && $archive_type != "tar" && $archive_type != "tgz" && $archive_type != "gz") {
            $net2ftp_output["ftp_unziptransferfiles"][] = __("Archive <b>%1\$s</b> was not processed because its filename extension was not recognized. Only zip, tar, tgz and gz archives are supported at the moment.", $archive_name);
            continue;
        }
        // -------------------------------------------------------------------------
        // Extract directories and files
        // -------------------------------------------------------------------------
        // ------------------------------
        // Check list of files to see if there are any malicious filenames
        // ------------------------------
        if ($archive_type == "zip") {
            $zip = new PclZip($archive_file);
            $list_to_check = $zip->listContent();
        } elseif ($archive_type == "tar" || $archive_type == "tgz" || $archive_type == "gz") {
            $list_to_check = PclTarList($archive_file);
        }
        if ($list_to_check <= 0) {
            $net2ftp_output["ftp_unziptransferfiles"][] = __("Unable to extract the files and directories from the archive");
            continue;
        }
        for ($i = 0; $i < sizeof($list_to_check); $i++) {
            $source = trim($list_to_check[$i]["filename"]);
            if (strpos($source, "../") !== false || strpos($source, "..\\") !== false) {
                $errormessage = __("Archive contains filenames with ../ or ..\\ - aborting the extraction");
                setErrorVars(false, $errormessage, debug_backtrace(), __FILE__, __LINE__);
                return false;
            }
        }
        // ------------------------------
        // Generate random directory
        // ------------------------------
        $tempdir = tempdir2($net2ftp_globals["application_tempdir"], "unzip__", "");
        if ($net2ftp_result["success"] == false) {
            return false;
        }
        registerTempfile("register", "{$tempdir}");
        // ------------------------------
        // Extract
        // ------------------------------
        if ($archive_type == "zip") {
            $zip = new PclZip($archive_file);
            $list = $zip->extract($p_path = $tempdir);
        } elseif ($archive_type == "tar" || $archive_type == "tgz" || $archive_type == "gz") {
            $list = PclTarExtract($archive_file, $tempdir);
        }
        // This code is not needed any more - see above: if ($list_to_check <= 0)
        if ($list <= 0) {
            //			$net2ftp_output["ftp_unziptransferfiles"][] = __("Unable to extract the files and directories from the archive");
            continue;
        }
        // ------------------------------
        // Create the directories and put the files on the FTP server
        // ------------------------------
        for ($i = 0; $i < sizeof($list); $i++) {
            $source = trim($list[$i]["filename"]);
            $unzip_status = trim($list[$i]["status"]);
            $target_relative = substr($source, strlen($tempdir));
            $target = $net2ftp_globals["directory"] . $target_relative;
            $ftpmode = ftpAsciiBinary($source);
            if ($unzip_status != "ok") {
                $net2ftp_output["ftp_unziptransferfiles"][] = __("Could not unzip entry %1\$s (error code %2\$s)", $target_relative, $unzip_status);
                setErrorVars(true, "", "", "", "");
                continue;
            }
            // Directory entry in the archive: create the directory
            if (is_dir($source) == true) {
                ftp_newdirectory($conn_id, $target);
                if ($net2ftp_result["success"] == true) {
                    $net2ftp_output["ftp_unziptransferfiles"][] = __("Created directory %1\$s", $target);
                } else {
                    $net2ftp_output["ftp_unziptransferfiles"][] = __("Could not create directory %1\$s", $target);
                    setErrorVars(true, "", "", "", "");
                }
            } elseif (is_file($source) == true) {
                ftp_putfile($conn_id, dirname($source), basename($source), dirname($target), basename($target), $ftpmode, "move");
                if ($net2ftp_result["success"] == true) {
                    $net2ftp_output["ftp_unziptransferfiles"][] = __("Copied file %1\$s", $target);
                } else {
                    setErrorVars(true, "", "", "", "");
                    $target_relative_parts = explode("/", str_replace("\\", "/", dirname($target_relative)));
                    $directory_to_create = $net2ftp_globals["directory"];
                    for ($j = 0; $j < sizeof($target_relative_parts); $j = $j + 1) {
                        $directory_to_create = $directory_to_create . "/" . $target_relative_parts[$j];
                        $ftp_chdir_result = @ftp_chdir($conn_id, $directory_to_create);
                        if ($ftp_chdir_result == false) {
                            ftp_newdirectory($conn_id, $directory_to_create);
                            if ($net2ftp_result["success"] == true) {
                                $net2ftp_output["ftp_unziptransferfiles"][] = __("Created directory %1\$s", $directory_to_create);
                            } else {
                                setErrorVars(true, "", "", "", "");
                            }
                        }
                        // end if
                    }
                    // end for
                    ftp_putfile($conn_id, dirname($source), basename($source), dirname($target), basename($target), $ftpmode, "copy");
                    if ($net2ftp_result["success"] == true) {
                        $net2ftp_output["ftp_unziptransferfiles"][] = __("Copied file %1\$s", $target);
                    } else {
                        setErrorVars(true, "", "", "", "");
                        $net2ftp_output["ftp_unziptransferfiles"][] = __("Could not copy file %1\$s", $target);
                    }
                }
            }
            // end elseif file
        }
        // end for
        // -------------------------------------------------------------------------
        // Delete the uploaded archive and the temporary files
        // -------------------------------------------------------------------------
        // Delete the temporary directory and its contents
        $delete_dirorfile_result = delete_dirorfile($tempdir);
        if ($delete_dirorfile_result == false) {
            $net2ftp_output["ftp_unziptransferfiles"][] = __("Unable to delete the temporary directory");
        } else {
            registerTempfile("unregister", "{$tempdir}");
        }
        // Delete the archive
        $unlink_result = @unlink($archive_file);
        if ($unlink_result == false) {
            $net2ftp_output["ftp_unziptransferfiles"][] = __("Unable to delete the temporary file %1\$s", $archive_file);
        } else {
            registerTempfile("unregister", "{$archive_file}");
        }
        $net2ftp_output["ftp_unziptransferfiles"][] = "</ul>";
    }
    // End for
    // -------------------------------------------------------------------------
    // Close connection
    // -------------------------------------------------------------------------
    ftp_closeconnection($conn_id);
}
function net2ftp_module_printBody()
{
    // --------------
    // This function prints the new directory screen
    // --------------
    // -------------------------------------------------------------------------
    // Global variables
    // -------------------------------------------------------------------------
    global $net2ftp_settings, $net2ftp_globals, $net2ftp_messages, $net2ftp_result, $net2ftp_output;
    if (isset($_POST["newNames"]) == true) {
        $newNames = validateEntry($_POST["newNames"]);
    } else {
        $newNames = "";
    }
    // -------------------------------------------------------------------------
    // Variables for all screens
    // -------------------------------------------------------------------------
    // Title
    $title = __("Create new directories");
    // Form name, back and forward buttons
    $formname = "NewDirForm";
    $back_onclick = "document.forms['" . $formname . "'].state.value='browse';document.forms['" . $formname . "'].state2.value='main';document.forms['" . $formname . "'].submit();";
    $forward_onclick = "document.forms['" . $formname . "'].submit();";
    // -------------------------------------------------------------------------
    // Variables for screen 1
    // -------------------------------------------------------------------------
    if ($net2ftp_globals["screen"] == 1) {
        // Next screen
        $nextscreen = 2;
    } elseif ($net2ftp_globals["screen"] == 2) {
        // Open connection
        setStatus(2, 10, __("Connecting to the FTP server"));
        $conn_id = ftp_openconnection();
        if ($net2ftp_result["success"] == false) {
            return false;
        }
        // Create new directories
        setStatus(4, 10, __("Processing the entries"));
        for ($k = 1; $k <= sizeof($newNames); $k++) {
            if (strlen($newNames[$k]) > 0) {
                $newsubdir = glueDirectories($net2ftp_globals["directory"], $newNames[$k]);
                ftp_newdirectory($conn_id, $newsubdir);
                if ($net2ftp_result["success"] == false) {
                    setErrorVars(true, "", "", "", "");
                    // Continue anyway
                    $net2ftp_output["newdir"][] = __("Directory <b>%1\$s</b> could not be created.", htmlEncode2($newNames[$k]));
                } else {
                    $net2ftp_output["newdir"][] = __("Directory <b>%1\$s</b> was successfully created.", htmlEncode2($newNames[$k]));
                }
            }
            // End if
        }
        // End for
        // Close connection
        ftp_closeconnection($conn_id);
    }
    // end elseif
    // -------------------------------------------------------------------------
    // Print the output
    // -------------------------------------------------------------------------
    require_once $net2ftp_globals["application_skinsdir"] . "/" . $net2ftp_globals["skin"] . "/manage.template.php";
}