Exemple #1
0
function net2ftp_module_printBody()
{
    // --------------
    // This function prints the edit screen
    // For screen == 1, the file is read from the FTP server
    // For screen == 2, the textarea is changed, the file is not read from the FTP server but comes from the HTML form
    // For screen == 3, the file is saved to the FTP server
    // --------------
    // -------------------------------------------------------------------------
    // Global variables
    // -------------------------------------------------------------------------
    global $net2ftp_settings, $net2ftp_globals, $net2ftp_messages, $net2ftp_result;
    if (isset($_POST["textareaType"]) == true) {
        $textareaType = validateTextareaType($_POST["textareaType"]);
    } else {
        $textareaType = "";
    }
    if (isset($_POST["text"]) == true) {
        $text = $_POST["text"];
    } else {
        $text = "";
    }
    if (isset($_POST["text_splitted"]) == true) {
        $text_splitted = $_POST["text_splitted"];
    } else {
        $text_splitted = "";
    }
    if (isset($_POST["encodingSelect"]) == true) {
        $encodingSelect = $_POST["encodingSelect"];
    } else {
        $encodingSelect = "";
    }
    if (isset($_POST["breakSelect"]) == true) {
        $breakSelect = $_POST["breakSelect"];
    } else {
        $breakSelect = "";
    }
    $text_encoding_selected = "";
    $line_break_selected = "";
    // -------------------------------------------------------------------------
    // Variables for all screens
    // -------------------------------------------------------------------------
    // Form name, back and forward buttons
    $formname = "EditForm";
    $back_onclick = "document.forms['" . $formname . "'].state.value='browse';document.forms['" . $formname . "'].state2.value='main';document.forms['" . $formname . "'].submit();";
    // Directory + file name
    $dirfilename = htmlEncode2(glueDirectories($net2ftp_globals["directory"], $net2ftp_globals["entry"]));
    // TextareaSelect onchange
    $onchange = "document.forms['EditForm'].screen.value=2;document.forms['EditForm'].textareaType.value=document.forms['EditForm'].textareaSelect.options[document.forms['EditForm'].textareaSelect.selectedIndex].value;document.forms['EditForm'].submit();";
    // Character encoding (requires multibyte string module to be installed)
    // With this, you can save a text with specified encoding and line break sequence
    // http://www.net2ftp.org/forums/viewtopic.php?id=2449
    if (($net2ftp_globals["language"] == "ja" || $net2ftp_globals["language"] == "tc" || $net2ftp_messages["iso-8859-1"] == "UTF-8") && function_exists("mb_detect_encoding") == true) {
        // $textarea_encodings is an array which contains the possible character encodings
        $textarea_encodings = getTextareaEncodingsArray();
        // $textarea_breaks is an array which contains the possible line breaks
        $textarea_breaks[] = "CRLF";
        $textarea_breaks[] = "CR";
        $textarea_breaks[] = "LF";
        // $text_encoding_old is the original encoding which is detected when the file is first read
        // $text_encoding_new is the requested encoding from the drop-down box
        // Default = encoding used for the page, which is defined by the language file in /languages/xx.inc.php
        // HTML uses BIG5, PHP uses BIG-5 (Traditional Chinese)
        // If the HTML encoding is not foreseen in the PHP function, set it to the default ISO-8859-1
        // $text_encoding is changed further on too
        if ($encodingSelect != "" && in_array($encodingSelect, $textarea_encodings)) {
            $text_encoding_new = $encodingSelect;
        } else {
            $text_encoding_new = "";
        }
        // $line_break_old is the original line break which is detected when the file is first read
        // $line_break is the requested line break from the drop-down box
        if ($breakSelect != "" && in_array($breakSelect, $textarea_breaks) == true) {
            $line_break_new = $breakSelect;
        } else {
            $line_break_new = "LF";
        }
    }
    // Programming language (for CodePress syntax highlighting)
    if ($textareaType == "codepress") {
        $filename_extension = get_filename_extension($net2ftp_globals["entry"]);
        if ($filename_extension == "asp") {
            $codepress_programming_language = "asp";
        } elseif ($filename_extension == "css") {
            $codepress_programming_language = "css";
        } elseif ($filename_extension == "cgi") {
            $codepress_programming_language = "perl";
        } elseif ($filename_extension == "htm") {
            $codepress_programming_language = "html";
        } elseif ($filename_extension == "html") {
            $codepress_programming_language = "html";
        } elseif ($filename_extension == "java") {
            $codepress_programming_language = "java";
        } elseif ($filename_extension == "js") {
            $codepress_programming_language = "javascript";
        } elseif ($filename_extension == "javascript") {
            $codepress_programming_language = "javascript";
        } elseif ($filename_extension == "pl") {
            $codepress_programming_language = "perl";
        } elseif ($filename_extension == "perl") {
            $codepress_programming_language = "perl";
        } elseif ($filename_extension == "php") {
            $codepress_programming_language = "php";
        } elseif ($filename_extension == "phps") {
            $codepress_programming_language = "php";
        } elseif ($filename_extension == "phtml") {
            $codepress_programming_language = "php";
        } elseif ($filename_extension == "ruby") {
            $codepress_programming_language = "ruby";
        } elseif ($filename_extension == "sql") {
            $codepress_programming_language = "sql";
        } elseif ($filename_extension == "txt") {
            $codepress_programming_language = "text";
        } else {
            $codepress_programming_language = "generic";
        }
        $codepress_onclick = "text.toggleEditor();";
    } else {
        $codepress_programming_language = "";
        $codepress_onclick = "";
    }
    // -------------------------------------------------------------------------
    // Variables for screen 1
    // Read the remote file (edit), or read the local template (new file)
    // -------------------------------------------------------------------------
    if ($net2ftp_globals["screen"] == 1) {
        // Template file
        $templatefile = $net2ftp_globals["application_rootdir"] . "/modules/edit/template.txt";
        // Edit: read the file from the FTP server
        if ($net2ftp_globals["state2"] == "") {
            $text = ftp_readfile("", $net2ftp_globals["directory"], $net2ftp_globals["entry"]);
            if ($net2ftp_result["success"] == false) {
                return false;
            }
            // Character encoding (requires multibyte string module to be installed)
            // Detect the original encoding of the text, and change the encoding of the text to the encoding of the page
            if (($net2ftp_globals["language"] == "ja" || $net2ftp_globals["language"] == "tc" || $net2ftp_messages["iso-8859-1"] == "UTF-8") && function_exists("mb_detect_encoding") == true) {
                // Detect original encoding
                $text_encoding_old = mb_detect_encoding($text, $textarea_encodings);
                $text_encoding_selected = $text_encoding_old;
                // If original encoding is detected and different from the page encoding, convert the text to the page encoding
                if ($text_encoding_old != "" && strcasecmp($text_encoding_old, $net2ftp_messages["iso-8859-1"]) != 0) {
                    $text = mb_convert_encoding($text, $net2ftp_messages["iso-8859-1"], $text_encoding_old);
                }
                // Detect original line break
                if (strpos($text, "\r\n") !== false) {
                    $line_break_old = "CRLF";
                } elseif (strpos($text, "\n") !== false) {
                    $line_break_old = "LF";
                } elseif (strpos($text, "\r") !== false) {
                    $line_break_old = "CR";
                } else {
                    $line_break_old = "LF";
                }
                $line_break_selected = $line_break_old;
            }
        } elseif ($net2ftp_globals["state2"] == "newfile") {
            $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 = trim(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);
        }
        // Save status
        $savestatus = __("Status: This file has not yet been saved");
        $savestatus_short = __("Not yet saved");
    } elseif ($net2ftp_globals["screen"] == 2) {
        // For HTML WYSIWYG editors, split the HTML
        if (($textareaType == "tinymce" || $textareaType == "ckeditor") && $text_splitted == "") {
            $text_splitted = splitHtml($text, $textareaType);
        } elseif (($textareaType == "plain" || $textareaType == "codepress") && $text == "" && isset($text_splitted["top"]) == true) {
            $text = $text_splitted["top"];
            $text .= $text_splitted["middle"];
            $text .= $text_splitted["bottom"];
        }
        // Save status
        $savestatus = __("Status: This file has not yet been saved");
        $savestatus_short = __("Not yet saved");
    } elseif ($net2ftp_globals["screen"] == 3) {
        // Check if a filename is specified
        if (strlen($net2ftp_globals["entry"]) < 1) {
            $errormessage = __("Please specify a filename");
            setErrorVars(false, $errormessage, debug_backtrace(), __FILE__, __LINE__);
            return false;
        }
        // For HTML WYSIWYG editors, join the HTML
        if ($textareaType == "tinymce" || $textareaType == "ckeditor") {
            $text = $text_splitted["top"];
            $text .= $text_splitted["middle"];
            $text .= $text_splitted["bottom"];
        }
        // $text_file contains the text which is written to the FTP server
        // It is equal to the text shown on screen, except if a different character encoding is chosen
        $text_file = $text;
        // Character encoding (requires multibyte string module to be installed)
        // Change the encoding of the text from the original or page encoding to the selected encoding
        if (($net2ftp_globals["language"] == "ja" || $net2ftp_globals["language"] == "tc" || $net2ftp_messages["iso-8859-1"] == "UTF-8") && function_exists("mb_detect_encoding") == true) {
            $break_map = array("CRLF" => "\r\n", "CR" => "\r", "LF" => "\n");
            if (isset($break_map[$line_break_new]) == true) {
                $text_file = preg_replace('/(\\r\\n)|\\r|\\n/', $break_map[$line_break_new], $text_file);
            }
            if ($text_encoding_new != "" && strcasecmp($text_encoding_new, $net2ftp_messages["iso-8859-1"]) != 0) {
                $text_file = mb_convert_encoding($text_file, $text_encoding_new, $net2ftp_messages["iso-8859-1"]);
            }
            $text_encoding_selected = $text_encoding_new;
            $line_break_selected = $line_break_new;
        }
        // Write the string to the FTP server
        // Note: this function also replaces CarriageReturn+LineFeed by LineFeed
        ftp_writefile("", $net2ftp_globals["directory"], $net2ftp_globals["entry"], $text_file);
        if ($net2ftp_result["success"] == false) {
            setErrorVars(true, "", "", "", "");
            // Continue anyway and print warning message
            $savestatus = __("Status: <b>This file could not be saved</b>");
            $savestatus_short = __("Could not be saved");
        } else {
            $mytime = mytime();
            $mytime_short = mytime_short();
            $ftpmode = ftpAsciiBinary($net2ftp_globals["entry"]);
            if ($ftpmode == FTP_ASCII) {
                $printftpmode = "FTP_ASCII";
            } elseif ($ftpmode == FTP_BINARY) {
                $printftpmode = "FTP_BINARY";
            }
            $savestatus = __("Status: Saved on <b>%1\$s</b> using mode %2\$s", $mytime, $printftpmode);
            $savestatus_short = __("Saved at %1\$s", $mytime_short);
        }
    }
    // -------------------------------------------------------------------------
    // Convert special characters to HTML entities
    // -------------------------------------------------------------------------
    // Plain textarea
    if ($textareaType == "" || $textareaType == "plain") {
        $text = htmlspecialchars($text, ENT_QUOTES);
    } elseif ($textareaType == "ckeditor") {
        $text_splitted["top"] = htmlspecialchars($text_splitted["top"], ENT_QUOTES);
        $text_splitted["bottom"] = htmlspecialchars($text_splitted["bottom"], ENT_QUOTES);
        // Do not encode the middle part, this is done by CKEditor itself
        //		$text_splitted["middle"] = htmlspecialchars($text_splitted["middle"], ENT_QUOTES);
    } elseif ($textareaType == "tinymce") {
        $text_splitted["top"] = htmlspecialchars($text_splitted["top"], ENT_QUOTES);
        $text_splitted["middle"] = htmlspecialchars($text_splitted["middle"], ENT_QUOTES);
        $text_splitted["bottom"] = htmlspecialchars($text_splitted["bottom"], ENT_QUOTES);
    } elseif ($textareaType == "codepress") {
        $text = htmlspecialchars($text, ENT_QUOTES);
    }
    // -------------------------------------------------------------------------
    // Print the output
    // -------------------------------------------------------------------------
    require_once $net2ftp_globals["application_skinsdir"] . "/" . $net2ftp_globals["skin"] . "/edit.template.php";
}
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";
}