PHP; Upload SCP

I’ve written a small tool that allows you to download a file over scp. Scp stands for secure copy and uses the ssh protocol.
This can help you to copy for example an IOS of cisco from the router/switch towards the server.
It’s written with php and except, and specially in such a way that users can take advantage of a web interface…

php_pscp_return.sh
#!/usr/bin/ex_pect -fset username [lrange $argv 0 0]
set password [lrange $argv 1 1]
set hostname [lrange $argv 2 2]
set filename [lrange $argv 3 3]
set filename2 [lrange $argv 4 4]
set timeout -1
log_user 1

> spawn pscp -l $username -pw $password -scp $hostname:/$filename /tftpboot/$filename2expect {
>         -re ".*es.*o.*" {
>                 exp_send "yes\r"
>                 exp_continue
>                 }
>         -re ".*sword.*" {
>                 exp_send "$password\r"
>                 }
>         }
> php_pscp_return.php
> <?php
>     $formatted = "";<br />    $output = array();<br />    $command = "";<br />    $today = date ("Ymd");<br />    if (!empty($_POST['filename'])) {<br />        $case_insensitive = false;<br />        if (!empty($_POST['case_insensitive'])) {<br />            $case_insensitive = true;<br />        }<br />        $filename       = $_POST['filename'];<br />        $filename2      = $_POST['filename2'];<br />        $username       = $_POST['username'];<br />        $password       = $_POST['password'];<br />        $passwordDebug  = "******";<br />        $hostname       = $_POST['hostname'];<br />        $commandRun     = "/var/www/html/custom/php_pscp_return.sh";<br />        $command        = " " . $commandRun . " " . $username . " " . $password . " " . $hostname . " " . $filename . " " . $filename2 . " ";
>         $result = -1;
>         $return_code = -1;
>         $result = exec($command, $output, $return_code);
>         foreach($output as $line) {
>             $grep_parts = explode(":", $line, 3); / split into three parts
>             $filepath = $grep_parts[0];
>             $lineno = $grep_parts[1];
>             $matched_line = $grep_parts[2];$formatted .= "<span class=\"match_component\"><pre class=\"brush:php; first-line: \">" . htmlspecialchars($matched_line) . "</pre></span>";
>         }
>     }
> ?>
> <!DOCTYPE HTML PUBLIC "-/W3C/DTD HTML 4.01 Transitional/EN"
>     "http://www.w3.org/TR/html4/loose.dtd">
> <html>
>     <head>
>         <meta http-equiv="Content-type" content="text/html; charset=utf-8"><br />        <title>Launch a SCP</title><br />        <style type="text/css" media="screen"><br />            .filename {<br />                font-family: Helvetica;<br />                font-size: 10pt;<br />            }<br />        </style><br />        <script type="text/javascript" src="/intranet/xregexp-min.js"></script><br />        <script type="text/javascript" src="/intranet/shCore.js"></script><br />        <script type="text/javascript" src="/intranet/shBrushPhp.js"></script><br />        <link href="/intranet/shCore.css" rel="stylesheet" type="text/css" />
>         <link href="/intranet/shThemeDefault.css" rel="stylesheet" type="text/css" />
>     </head>
>     <body id="" onload=""><br />        <h1><u><font color="red">Launch a secure copy (SCP)</font></u></h1><br />        With this little tool, you can copy <font color="red"><b>(download)</b></font> a file to a remote host with scp (Secure Copy).<br><br />        For more information, read <a href="/net/index.php/procedures/90-launch-a-secure-copy-scp" target="_blank">this</a>.<br />        <hr><br />        <h2><font color="green">Perform a copy (download)</font></h2><br />        <form action="php_pscp_return.php" method="post" accept-charset="utf-8"><br />        <table><br />                <tr valign="top"><br />                        <td><label for="filename">Remote filename:<br><small>The filename on the remote node</small></label></td><br />                        <td><input type="text" name="filename" id="username" value="" size="80"></td><br />                </tr><br />                <tr><td><br></td><td><br></td></tr><br />                <tr valign="top"><br />                        <td><label for="filename">Local filename:<br><small>The filename you want on the server</small></label></td><br />                        <td><input type="text" name="filename2" id="username2" value="" size="80"></td><br />                </tr><br />                <tr><td><br></td><td><br></td></tr><br />                <tr valign="top"><br />                        <td><label for="username">Your username: </label></td><br />                        <td><input type="text" name="username" id="username" value="" size="10"></td><br />                </tr><br />                <tr><td><br></td><td><br></td></tr><br />                <tr valign="top"><br />                        <td><label for="password">Your password: </label></td><br />                        <td><input type="password" name="password" id="password" value="" size="20"></td><br />                </tr><br />                <tr><td><br></td><td><br></td></tr><br />                <tr valign="top"><br />                        <td><label for="hostname">The hostname (FQDN) or IP: </label></td><br />                        <td><input type="text" name="hostname" id="hostname" value="" size="50"></td><br />                </tr><br />        </table><br />                <center><p><br />                        <input type="submit" value="... Launch your download ..."><br />                        <input type="reset" value="... Reset ..."><br />                </p></center><br />        </form><br />        <hr><br />        <h2><font color="green">Debug</font></h2><br />        Here some debug results, you never know you can use them...<br><br><br />        <ul><br />                <li>The Command you ran: <i><code><?php echo "$commandRun $username $passwordDebug $hostname $filename"; ?></code></i><br><br></li><br />                <li>The output of the files that are in the folder /tftpboot/. This folder is used as the source to store all files. This way, the files can <br />be used for scp, tftp, sftp...<br>If you want to download the file, simple click it...<ol><?php<br />                                if ($handle = opendir('./tftpboot/')) {<br />                                        while (false !== ($entry = readdir($handle))) {<br />                                                if ($entry != "." && $entry != "..") {<br />                                                        echo "<li><a href=\"/tftpboot/$entry\" target=\"_blank\">$entry</a></li>";
>                                                         }
>                                                 }
>                                         closedir($handle);
>                                 }?></ol><br><br></li>
>         <li>The complete copy output, together with the RSA key will appear hereunder (after you started it of course):<?php echo $formatted ?></li>    </body>    
> </html>