Advertisement
Guest User

Untitled

a guest
Sep 9th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.71 KB | None | 0 0
  1. <?php
  2. error_reporting(0);
  3.  
  4. $output = array();
  5. $port = $_POST['port'];
  6. $username = $_POST['username'];
  7. $password = $_POST['password'];
  8.  
  9. if(isset($port) && isset($username) && isset($password)){
  10. try{
  11. startSetup($port, $username, $password);
  12. }catch(Exception $ex){
  13. $error = $ex->getMessage();
  14. }
  15. }
  16.  
  17. function startSetup($port, $username, $password){
  18. $ftp = ftpConnectLoginPassive($port, $username, $password);
  19. $config = getConfig();
  20.  
  21. foreach($config as $cfg){
  22. $path = $cfg[0];
  23. $mode = $cfg[1];
  24. $recursive = $cfg[2];
  25.  
  26. if($recursive){
  27. output('- - - Processing <b>recursive</b> command - - -');
  28. ftpChmodRecursive($ftp, $mode, $path);
  29. output('- - - <b>Finished</b> recursive execution - - -');
  30. }else{
  31. ftpChmod($ftp, $mode, $path);
  32. }
  33. }
  34.  
  35. ftp_close($ftp);
  36. output('Closed FTP connection');
  37. }
  38.  
  39. function ftpConnectLoginPassive($port, $username, $password){
  40. $ftp = ftp_ssl_connect('localhost', $port, 3);
  41.  
  42. if(!$ftp){
  43. throw new Exception('Could not connect to the local FTP server!');
  44. }
  45.  
  46. output('Connected to the local FTP server on port <b>'.$port.'</b>');
  47.  
  48. if(!ftp_login($ftp, $username, $password)){
  49. throw new Exception('Incorrect login credentials!');
  50. }
  51.  
  52. output('Logged in as <b>'.$username.'</b>');
  53.  
  54. if(!ftp_pasv($ftp, true)){
  55. throw new Exception('Could not enable passive mode!');
  56. }
  57.  
  58. output('<b>Passive</b> mode enabled');
  59.  
  60. return $ftp;
  61. }
  62.  
  63. function ftpChmod($ftp, $mode, $path){
  64. if(ftp_chmod($ftp, $mode, $path)){
  65. output('Changed permissions of <b>'.$path.'</b> to <b>'.decoct($mode).'</b>');
  66. }else{
  67. output('Could not change permissions of <b>'.$path.'</b> to <b>'.decoct($mode).'</b>');
  68. }
  69. }
  70.  
  71. function ftpChmodRecursive($ftp, $mode, $path){
  72. $pwd = ftp_pwd($ftp);
  73.  
  74. ftpChmod($ftp, $mode, $path);
  75.  
  76. if(@ftp_chdir($ftp, $path)){
  77. output('Changed directory to <b>'.$path.'</b>');
  78.  
  79. $files = ftp_nlist($ftp, '.');
  80.  
  81. ftp_chdir($ftp, $pwd);
  82. output('Changed back to <b>'.$pwd.'</b>');
  83.  
  84. foreach($files as $file){
  85. if($file !== '.' && $file !== '..'){
  86. output('Found nested file <b>'.$file.'</b>');
  87. ftpChmodRecursive($ftp, $mode, $path.'/'.$file);
  88. }
  89. }
  90. }
  91. }
  92.  
  93. function getConfig(){
  94. $text = file_get_contents('config.txt');
  95.  
  96. if(!$text){
  97. throw new Exception('Could not read config file!');
  98. }
  99.  
  100. output('Read config file');
  101.  
  102. $lines = preg_split('/\r\n|\n|\r/', $text);
  103.  
  104. output('Config has <b>'.sizeof($lines).'</b> lines');
  105.  
  106. for($i = 0; $i < sizeof($lines); $i++){
  107. $explode = explode(';', $lines[$i]);
  108. $path = $explode[0];
  109. $mode = $explode[1];
  110. $mode = octdec(str_pad($mode, 4, '0', STR_PAD_LEFT));
  111. $recursive = sizeof($explode) === 3 && $explode[2] === 'r';
  112. $lines[$i] = array($path, $mode, $recursive);
  113. }
  114.  
  115. return $lines;
  116. }
  117.  
  118. function output($message){
  119. global $output;
  120. $output[]= $message;
  121. }
  122. ?>
  123. <!doctype>
  124. <html>
  125. <head>
  126. <meta charset="utf8">
  127. <title>Chmod Setup</title>
  128. <link href="//maxcdn.bootstrapcdn.com/bootswatch/3.3.2/paper/bootstrap.min.css" rel="stylesheet">
  129. <style>
  130. .center-block {
  131. float: none;
  132. }
  133.  
  134. pre {
  135. white-space: normal;
  136. }
  137. </style>
  138. </head>
  139. <body>
  140. <br>
  141. <div class="container">
  142. <div class="row">
  143. <div class="col-md-4">
  144. <form method="POST">
  145. <fieldset>
  146. <legend>FTP Settings</legend>
  147. <div class="form-group">
  148. <label for="port">Port</label>
  149. <input name="port" type="number" class="form-control" value="21">
  150. </div>
  151. <div class="form-group">
  152. <label for="username">Username</label>
  153. <input name="username" type="text" class="form-control">
  154. </div>
  155. <div class="form-group">
  156. <label for="password">Password</label>
  157. <input name="password" type="password" class="form-control">
  158. </div>
  159. <button type="submit" class="btn btn-primary">Start setup</button>
  160. </fieldset>
  161. </form>
  162. </div>
  163. <div class="col-md-6">
  164. <?php if($error){ ?>
  165. <div class="alert alert-danger">
  166. <?php echo $error; ?>
  167. </div>
  168. <?php }?>
  169. <?php if(sizeof($output) > 0){ ?>
  170. <pre>
  171. <?php foreach($output as $msg){ echo $msg.'<br>'; } ?>
  172. </pre>
  173. <?php } ?>
  174. </div>
  175. </div>
  176. </div>
  177. <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
  178. </body>
  179. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement