-
Notifications
You must be signed in to change notification settings - Fork 310
/
samba.repo.php
248 lines (223 loc) · 8.07 KB
/
samba.repo.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php
/* This file is part of Jeedom.
*
* Jeedom is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Jeedom is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Jeedom. If not, see <http://www.gnu.org/licenses/>.
*/
/* * ***************************Includes********************************* */
require_once __DIR__ . '/../../core/php/core.inc.php';
class repo_samba {
/* * *************************Attributs****************************** */
public static $_name = 'Samba';
public static $_scope = array(
'plugin' => true,
'backup' => true,
'hasConfiguration' => true,
'core' => true,
'hasRetentionDay' => true,
'test' => true
);
/* * ***********************Méthodes statiques*************************** */
public static function getConfigurationOption() {
return array(
'parameters_for_add' => array(
'path' => array(
'name' => __('Chemin', __FILE__),
'type' => 'input',
),
),
'configuration' => array(
'backup::ip' => array(
'name' => __('[Backup] IP', __FILE__),
'type' => 'input',
),
'backup::username' => array(
'name' => __('[Backup] Utilisateur', __FILE__),
'type' => 'input',
),
'backup::password' => array(
'name' => __('[Backup] Mot de passe', __FILE__),
'type' => 'password',
),
'backup::share' => array(
'name' => __('[Backup] Partage', __FILE__),
'type' => 'input',
),
'backup::folder' => array(
'name' => __('[Backup] Chemin', __FILE__),
'type' => 'input',
),
),
);
}
public static function checkUpdate(&$_update) {
if (is_array($_update)) {
if (count($_update) < 1) {
return;
}
foreach ($_update as $update) {
self::checkUpdate($update);
}
return;
}
$file = self::ls(config::byKey('samba::plugin::folder') . '/' . $_update->getConfiguration('path'), 'plugin');
if (count($file) != 1) {
return;
}
if (!isset($file[0]['datetime'])) {
return;
}
$_update->setRemoteVersion($file[0]['datetime']);
if ($file[0]['datetime'] != $_update->getLocalVersion()) {
$_update->setStatus('update');
} else {
$_update->setStatus('ok');
}
$_update->save();
}
public static function deleteObjet($_update) {
}
public static function downloadObject($_update) {
$tmp_dir = jeedom::getTmpFolder('samba');
$tmp = $tmp_dir . '/' . $_update->getLogicalId() . '.zip';
if (file_exists($tmp)) {
unlink($tmp);
}
if (!is_writable($tmp_dir)) {
exec(system::getCmdSudo() . 'chmod 777 -R ' . $tmp);
}
if (!is_writable($tmp_dir)) {
throw new Exception(__('Impossible d\'écrire dans le répertoire :', __FILE__) . ' ' . $tmp . __('. Exécuter la commande suivante en SSH : sudo chmod 777 -R', __FILE__) . ' ' . $tmp_dir);
}
$cmd = 'cd ' . $tmp_dir . ';';
$cmd .= self::makeSambaCommand('cd ' . config::byKey('samba::plugin::folder') . ';get ' . $_update->getConfiguration('path'), 'plugin');
com_shell::execute($cmd);
$pathinfo = pathinfo($_update->getConfiguration('path'));
com_shell::execute('mv ' . $tmp_dir . '/' . $pathinfo['basename'] . ' ' . $tmp);
$file = self::ls(config::byKey('samba::plugin::folder') . '/' . $_update->getConfiguration('path'), 'plugin');
if (count($file) != 1 || !isset($file[0]['datetime'])) {
return array('path' => $tmp, 'localVersion' => date('Y-m-d H:i:s'));
}
return array('path' => $tmp, 'localVersion' => $file[0]['datetime']);
}
public static function objectInfo($_update) {
return array(
'doc' => '',
'changelog' => '',
);
}
public static function makeSambaCommand($_cmd, $_type = 'backup') {
return system::getCmdSudo() . 'smbclient -t 120 ' . config::byKey('samba::' . $_type . '::share') . ' -U "' . config::byKey('samba::' . $_type . '::username') . '%' . config::byKey('samba::' . $_type . '::password') . '" -I ' . config::byKey('samba::' . $_type . '::ip') . ' -c "' . $_cmd . '"';
}
public static function sortByDatetime($a, $b) {
if (strtotime($a['datetime']) == strtotime($b['datetime'])) {
return 0;
}
return (strtotime($a['datetime']) < strtotime($b['datetime'])) ? -1 : 1;
}
public static function ls($_dir = '', $_type = 'backup') {
$cmd = repo_samba::makeSambaCommand('cd ' . $_dir . ';ls', $_type);
$result = explode("\n", com_shell::execute($cmd));
$return = array();
for ($i = 2; $i < count($result) - 2; $i++) {
$line = array();
foreach (explode(" ", $result[$i]) as $value) {
if (trim($value) == '') {
continue;
}
$line[] = $value;
}
$file_info = array();
$file_info['filename'] = $line[0];
$file_info['size'] = $line[2];
$file_info['datetime'] = date('Y-m-d H:i:s', strtotime($line[5] . ' ' . $line[4] . ' ' . $line[7] . ' ' . $line[6]));
$return[] = $file_info;
}
usort($return, 'repo_samba::sortByDatetime');
return array_reverse($return);
}
public static function test() {
$cmd = repo_samba::makeSambaCommand('cd ' . config::byKey('samba::backup::folder') . ';ls', 'backup');
try {
$result = explode("\n", com_shell::execute($cmd));
return True;
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
}
public static function cleanBackupFolder() {
$timelimit = strtotime('-' . config::byKey('samba::keepDays') . ' days');
foreach (self::ls(config::byKey('samba::backup::folder')) as $file) {
if ($file['filename'] == '..' || $file['filename'] == '.') {
continue;
}
if ($timelimit > strtotime($file['datetime'])) {
echo 'Delete backup too old : ' . json_encode($file);
$cmd = self::makeSambaCommand('cd ' . config::byKey('samba::backup::folder') . ';del ' . $file['filename']);
com_shell::execute($cmd);
}
}
}
public static function backup_send($_path) {
$pathinfo = pathinfo($_path);
$cmd = 'cd ' . $pathinfo['dirname'] . ';';
$cmd .= self::makeSambaCommand('cd ' . config::byKey('samba::backup::folder') . ';put ' . $pathinfo['basename']);
com_shell::execute($cmd);
self::cleanBackupFolder();
}
public static function backup_list() {
$return = array();
foreach (self::ls(config::byKey('samba::backup::folder')) as $file) {
if (strpos($file['filename'], '.tar.gz') !== false) {
$return[] = $file['filename'];
}
}
return $return;
}
public static function backup_restore($_backup) {
$backup_dir = calculPath(config::byKey('backup::path'));
$cmd = 'cd ' . $backup_dir . ';';
$cmd .= self::makeSambaCommand('cd ' . config::byKey('samba::backup::folder') . ';get ' . $_backup);
com_shell::execute($cmd);
com_shell::execute(system::getCmdSudo() . 'chmod 777 -R ' . $backup_dir . '/*');
}
public static function downloadCore($_path) {
$pathinfo = pathinfo($_path);
$cmd = 'cd ' . $pathinfo['dirname'] . ';';
$cmd .= self::makeSambaCommand('get ' . config::byKey('samba::core::path') . '/jeedom.zip', 'plugin');
com_shell::execute($cmd);
com_shell::execute(system::getCmdSudo() . 'chmod 777 -R ' . $_path);
return;
}
public static function versionCore() {
try {
if (file_exists(jeedom::getTmpFolder('samba') . '/version')) {
com_shell::execute(system::getCmdSudo() . 'rm /tmp/jeedom_version');
}
$cmd = 'cd /tmp;';
$cmd .= self::makeSambaCommand('get ' . config::byKey('samba::core::path') . '/jeedom_version', 'plugin');
com_shell::execute($cmd);
if (!file_exists(jeedom::getTmpFolder('samba') . '/version')) {
return null;
}
$version = trim(file_get_contents(jeedom::getTmpFolder('samba') . '/version'));
com_shell::execute(system::getCmdSudo() . 'rm ' . jeedom::getTmpFolder('samba') . '/version');
return $version;
} catch (Exception $e) {
} catch (Error $e) {
}
return null;
}
/* * *********************Methode d'instance************************* */
/* * **********************Getteur Setteur*************************** */
}