-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchange_pass_ftp_user.php
91 lines (88 loc) · 2.53 KB
/
change_pass_ftp_user.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
require 'config.php';
$parser = new PEAR2\Console\CommandLine(array(
'description' => 'Creates new FTP user',
'version' => '0.0.1', // the version of your program
));
$parser->addOption(
'interactive',
array(
'short_name' => '-i',
'long_name' => '--interactive',
'description' => 'Run interactively',
'action' => 'StoreTrue'
)
);
$parser->addOption(
'name',
array(
'short_name' => '-n',
'long_name' => '--name',
'description' => 'ftp user name',
'action' => 'StoreString',
'help_name' => 'ftp_user_name'
)
);
$parser->addOption(
'password',
array(
'short_name' => '-p',
'long_name' => '--password',
'description' => 'database user name password',
'action' => 'Password',
'help_name' => 'ftp_user_name_password'
)
);
try {
$result = $parser->parse();
if ($result->options["interactive"]){
//TODO Запуск в интерактивном режиме
}
if (empty(array_filter($result->options))){
$parser->displayUsage();
}
if ($result->options["password"]){
$user_password = $result->options["password"];
} else {
$user_password = random_str(8);
}
$temp = tmpfile();
$tmp_pwd = $user_password . "\n" . $user_password . "\n";
$metaDatas = stream_get_meta_data($temp);
$tmpFilename = $metaDatas['uri'];
try {
if (fwrite($temp,$tmp_pwd ) === FALSE) {
throw new \Exception ("Can't write temporary file");
}
} catch (Exception $ex) {
echo 'Error: ', $ex->getMessage(), "\n";
fclose($temp);
unset($tmp_pwd);
unset($user_password);
exit(1);
}
try {
$cmd = escapeshellcmd('pure-pw passwd ' . $result->options["name"] . ' -m ');
$cmd = $cmd . "< " . $tmpFilename;
unset($aResult);
exec($cmd . " 2>&1",$aResult, $return_val);
if ($return_val<>0)
throw new \Exception ("Can't run pure-pw passwd command");
echo "Successfully changed ftp user: " . $result->options["name"] . " password to " . $user_password . "\n";
exit(0);
} catch (Exception $ex) {
echo 'Error: ', $ex->getMessage(), "\n";
fclose($temp);
unset($tmp_pwd);
unset($user_password);
exit(1);
}
}
catch (Exception $exc) {
$parser->displayError($exc->getMessage());
}