-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssh2_crontab_manager.php
134 lines (102 loc) · 4.1 KB
/
ssh2_crontab_manager.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
use phpseclib3\Net\SSH2;
Class Ssh2_crontab_manager {
private $connection;
private $path;
private $handle;
private $cron_file;
public function __construct($host = null, $port = null, $username = null, $password = null)
{
$path_length = strrpos(__FILE__, "/");
$this->path = substr(__FILE__, 0, $path_length) . '/';
$this->handle = 'crontab.txt';
$this->cron_file = "{$this->path}{$this->handle}";
try {
if (is_null($host) || is_null($port) || is_null($username) || is_null($password)) {
throw new Exception("Please specify the host, port, username, and password!");
}
$this->connection = new SSH2($host, $port);
if (!$this->connection->login($username, $password)) {
throw new Exception("Could not authenticate '{$username}' using password: '{$password}'.");
}
} catch (Exception $e) {
$this->error_message($e->getMessage());
}
}
public function exec()
{
$argument_count = func_num_args();
try {
if (!$argument_count) {
throw new Exception("There is nothing to execute, no arguments specified.");
}
$arguments = func_get_args();
$command_string = ($argument_count > 1) ? implode(" && ", $arguments) : $arguments[0];
$stream = $this->connection->exec($command_string);
if (!$stream) {
throw new Exception("Unable to execute the specified commands: <br />{$command_string}");
}
} catch (Exception $e) {
$this->error_message($e->getMessage());
}
return $this;
}
public function write_to_file($path=NULL, $handle=NULL)
{
if (! $this->crontab_file_exists())
{
$this->handle = (is_null($handle)) ? $this->handle : $handle;
$this->path = (is_null($path)) ? $this->path : $path;
$this->cron_file = "{$this->path}{$this->handle}";
$init_cron = "crontab -l > {$this->cron_file} && [ -f {$this->cron_file} ] || > {$this->cron_file}";
$this->exec($init_cron);
}
return $this;
}
public function remove_file()
{
if ($this->crontab_file_exists()) $this->exec("rm {$this->cron_file}");
return $this;
}
public function append_cronjob($cron_jobs=NULL)
{
if (is_null($cron_jobs)) $this->error_message("Nothing to append! Please specify a cron job or an array of cron jobs.");
$append_cronfile = "echo '";
$append_cronfile .= (is_array($cron_jobs)) ? implode("\\n", $cron_jobs) : $cron_jobs;
$append_cronfile .= "' >> {$this->cron_file}";
$install_cron = "crontab {$this->cron_file}";
$this->write_to_file()->exec($append_cronfile, $install_cron)->remove_file();
return $this;
}
public function remove_cronjob($cron_jobs=NULL)
{
if (is_null($cron_jobs)) $this->error_message("Nothing to remove! Please specify a cron job or an array of cron jobs.");
$this->write_to_file();
$cron_array = file($this->cron_file, FILE_IGNORE_NEW_LINES);
if (empty($cron_array)) $this->error_message("Nothing to remove! The cronTab is already empty.");
$original_count = count($cron_array);
if (is_array($cron_jobs))
{
foreach ($cron_jobs as $cron_regex) $cron_array = preg_grep($cron_regex, $cron_array, PREG_GREP_INVERT);
}
else
{
$cron_array = preg_grep($cron_jobs, $cron_array, PREG_GREP_INVERT);
}
return ($original_count === count($cron_array)) ? $this->remove_file() : $this->remove_crontab()->append_cronjob($cron_array);
}
public function remove_crontab()
{
$this->exec("crontab -r")->remove_file();
return $this;
}
private function crontab_file_exists()
{
return file_exists($this->cron_file);
}
private function error_message($error)
{
die("<pre style='color:#EE2711'>ERROR: {$error}</pre>");
}
}
?>