Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- error_reporting(0);
- $output = array();
- $port = $_POST['port'];
- $username = $_POST['username'];
- $password = $_POST['password'];
- if(isset($port) && isset($username) && isset($password)){
- try{
- startSetup($port, $username, $password);
- }catch(Exception $ex){
- $error = $ex->getMessage();
- }
- }
- function startSetup($port, $username, $password){
- $ftp = ftpConnectLoginPassive($port, $username, $password);
- $config = getConfig();
- foreach($config as $cfg){
- $path = $cfg[0];
- $mode = $cfg[1];
- $recursive = $cfg[2];
- if($recursive){
- output('- - - Processing <b>recursive</b> command - - -');
- ftpChmodRecursive($ftp, $mode, $path);
- output('- - - <b>Finished</b> recursive execution - - -');
- }else{
- ftpChmod($ftp, $mode, $path);
- }
- }
- ftp_close($ftp);
- output('Closed FTP connection');
- }
- function ftpConnectLoginPassive($port, $username, $password){
- $ftp = ftp_ssl_connect('localhost', $port, 3);
- if(!$ftp){
- throw new Exception('Could not connect to the local FTP server!');
- }
- output('Connected to the local FTP server on port <b>'.$port.'</b>');
- if(!ftp_login($ftp, $username, $password)){
- throw new Exception('Incorrect login credentials!');
- }
- output('Logged in as <b>'.$username.'</b>');
- if(!ftp_pasv($ftp, true)){
- throw new Exception('Could not enable passive mode!');
- }
- output('<b>Passive</b> mode enabled');
- return $ftp;
- }
- function ftpChmod($ftp, $mode, $path){
- if(ftp_chmod($ftp, $mode, $path)){
- output('Changed permissions of <b>'.$path.'</b> to <b>'.decoct($mode).'</b>');
- }else{
- output('Could not change permissions of <b>'.$path.'</b> to <b>'.decoct($mode).'</b>');
- }
- }
- function ftpChmodRecursive($ftp, $mode, $path){
- $pwd = ftp_pwd($ftp);
- ftpChmod($ftp, $mode, $path);
- if(@ftp_chdir($ftp, $path)){
- output('Changed directory to <b>'.$path.'</b>');
- $files = ftp_nlist($ftp, '.');
- ftp_chdir($ftp, $pwd);
- output('Changed back to <b>'.$pwd.'</b>');
- foreach($files as $file){
- if($file !== '.' && $file !== '..'){
- output('Found nested file <b>'.$file.'</b>');
- ftpChmodRecursive($ftp, $mode, $path.'/'.$file);
- }
- }
- }
- }
- function getConfig(){
- $text = file_get_contents('config.txt');
- if(!$text){
- throw new Exception('Could not read config file!');
- }
- output('Read config file');
- $lines = preg_split('/\r\n|\n|\r/', $text);
- output('Config has <b>'.sizeof($lines).'</b> lines');
- for($i = 0; $i < sizeof($lines); $i++){
- $explode = explode(';', $lines[$i]);
- $path = $explode[0];
- $mode = $explode[1];
- $mode = octdec(str_pad($mode, 4, '0', STR_PAD_LEFT));
- $recursive = sizeof($explode) === 3 && $explode[2] === 'r';
- $lines[$i] = array($path, $mode, $recursive);
- }
- return $lines;
- }
- function output($message){
- global $output;
- $output[]= $message;
- }
- ?>
- <!doctype>
- <html>
- <head>
- <meta charset="utf8">
- <title>Chmod Setup</title>
- <link href="//maxcdn.bootstrapcdn.com/bootswatch/3.3.2/paper/bootstrap.min.css" rel="stylesheet">
- <style>
- .center-block {
- float: none;
- }
- pre {
- white-space: normal;
- }
- </style>
- </head>
- <body>
- <br>
- <div class="container">
- <div class="row">
- <div class="col-md-4">
- <form method="POST">
- <fieldset>
- <legend>FTP Settings</legend>
- <div class="form-group">
- <label for="port">Port</label>
- <input name="port" type="number" class="form-control" value="21">
- </div>
- <div class="form-group">
- <label for="username">Username</label>
- <input name="username" type="text" class="form-control">
- </div>
- <div class="form-group">
- <label for="password">Password</label>
- <input name="password" type="password" class="form-control">
- </div>
- <button type="submit" class="btn btn-primary">Start setup</button>
- </fieldset>
- </form>
- </div>
- <div class="col-md-6">
- <?php if($error){ ?>
- <div class="alert alert-danger">
- <?php echo $error; ?>
- </div>
- <?php }?>
- <?php if(sizeof($output) > 0){ ?>
- <pre>
- <?php foreach($output as $msg){ echo $msg.'<br>'; } ?>
- </pre>
- <?php } ?>
- </div>
- </div>
- </div>
- <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement