-
Notifications
You must be signed in to change notification settings - Fork 0
/
TorrentSave.php.script
747 lines (600 loc) · 21.6 KB
/
TorrentSave.php.script
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
#!/usr/bin/php
<?php
/**
* PHP Version 7
* Verif Class Doc Comment
*
* @category Class
* @package Package
* @author SCHRODER Bastien <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
* @link http://10.0.10.145/infoDisk.php
*/
/**
* Command Class Doc Comment
*
* @category Class
* @package Package
* @author SCHRODER Bastien <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
* @link http://10.0.10.145/infoDisk.php
*/
Class Commands
{
private static $_keyLength = 50 ;
//private static $_linkInfoDisk = "http://10.0.10.145/infoDisk.php";
/**
* Verify Args
*
* @param Array $argc number of array line
* @param Array $argv array of data
*
* @return boolean
**/
public function verifArgs($argc,$argv)
{
$config=self::_getIdInfo();
if (file_exists(self::_getUserDir()) && isset($config['server'])) {
switch(true)
{
case $argc > 2 && in_array($argv[1], array('--addDisk', '-add')):
self::_addDisk($argv[2]);
break;
case $argc > 2 && in_array($argv[1], array('--removeDisk', '-rm')):
self::_removeDisk($argv[2]);
break;
case $argc > 2 && in_array($argv[1], array('--link', '-l')):
self::_linkServer($argv[2]);
break;
case $argc > 2 && in_array($argv[1], array('--save', '-save')):
self::_save($argv[2]);
break;
case $argc = 2 && in_array($argv[1], array('--unLink', '-ul')):
self::_unLinkServer();
break;
case $argc = 2 && in_array($argv[1], array('--send', '-send')):
echo self::_updateDisk();
break;
case $argc = 2 && in_array($argv[1], array('--infoDisk', '-i')):
echo file_get_contents($config['server']."?info&key=".$config['key']."");
break;
case $argc = 2 && in_array($argv[1], array('--uninstall')):
echo self::_uninstall();
break;
case $argc <= 2 || in_array($argv[1], array('--help', '-h', '-?')):
echo self::_help($argv);
break;
}
} else {
switch(true)
{
case $argc > 2 && in_array($argv[1], array('--link', '-l')):
self::_linkServer($argv[2]);
break;
case $argc <= 2 && in_array($argv[1], array('--help', '-h', '-?')):
echo self::_help($argv);
break;
default :
echo "\nVous devez lier un serveur avant de commencer tout autre action,\n
utilisez l'option --link http://www.exemple.com/exemple \n\n";
}
}
}
/**
* Get the name of current user
*
* @return string
**/
private static function _getUserDir()
{
$currUser = get_current_user();
return "/home/$currUser/.config/torrentSave/Id.txt";
}
/**
* Get Id info
*
* @return array
**/
private function _getIdInfo()
{
if (file_exists(self::_getUserDir())) {
$data = file_get_contents(self::_getUserDir());
$dataArray = json_decode($data, true);
return $dataArray;
}
return null;
}
/**
* Check if key exist
*
* @return boolean
**/
private function _keyCheck()
{
$config=self::_getIdInfo();
if (isset($config['key'])) {
echo "\nclé existante\n";
return true;
} else {
$config['key'] = self::_random();
if (file_put_contents(self::_getUserDir(), json_encode($config))) {
echo "\nouvelle clé\n";
return true;
}
}
return fasle;
}
/**
* Add Disk
*
* @param Array $argv array of data
*
* @return boolean
**/
private function _addDisk($argv)
{
if (self::_keyCheck()) {
$config=self::_getIdInfo();
$fsName= explode("\n", shell_exec("df --output=source $argv"));
$i = self::_diskCheck($argv);
if ($i != false || $i === 0) {
echo "\nDisk already exist\n";
} else {
$config['disk'][]= array(
"diskKey" => self::_diskKey($argv),
"total" => disk_total_space("$argv"),
"dir" => $argv,
"fsName" => $fsName[1],
);
file_put_contents(self::_getUserDir(), json_encode($config));
echo "\nAdded disk\n";
}
} else {
echo "erreur";
}
}
/**
* Update Disk
*
* @return boolean
**/
private function _updateDisk()
{
$idInfo = self::_getIdInfo();
$count = count($idInfo['disk']);
for ($i = 0; $i < $count; $i++) {
$ss=$idInfo['disk'][$i]['dir'];
echo "\nLe répertoire $ss ";
if(!file_exists($ss."/save")) {
shell_exec("sudo mkdir ".$ss."/save");
}
$saveSpace = explode("\t", shell_exec("du -sb ".$ss."/save"));
$usedSpace = explode("\t", shell_exec("du -sb $ss"));
$hostname =gethostname();
$diskFreeSpace = disk_free_space($idInfo['disk'][$i]['dir']);
$res = file_get_contents(
$idInfo['server']."?update&key=".$idInfo['key'].
"&dir=".$idInfo['disk'][$i]['dir'].
"&total=".$idInfo['disk'][$i]['total'].
"&diskKey=".$idInfo['disk'][$i]['diskKey'].
"&saveSpace=".$saveSpace[0].
"&usedSpace=".$usedSpace[0].
"&hostname=".$hostname.
"&freeSpace=".$diskFreeSpace."", "r"
);
if ($res != false) {
$count = count($idInfo['disk']);
$freeSp = 0;
for ($x = 0; $x < $count; $x++) {
$dFS = disk_free_space($idInfo['disk'][$x]['dir']);
if ($freeSp < $dFS ){
$freeSp=$dFS;
$diskSave = $idInfo['disk'][$x]['dir'];
}
}
$res2 = file_get_contents(
$idInfo['server']."?getTorrent&key=".$idInfo['key'].
"&libelle=".$res."", "r"
);
//var_dump($res2);
if (!file_exists($diskSave."/save")) {
shell_exec("sudo mkdir ".$diskSave."/save");
shell_exec("sudo chmod 777 ".$diskSave."/save");
}
if ($res2 != false) {
$torrentName = basename($res);
file_put_contents($idInfo['disk'][$i]['dir']."/save/".$torrentName, $res2);
shell_exec("transmission-remote -n 'transmission:transmission' -a ".$idInfo['disk'][$i]['dir']."/save/".$torrentName.
" -w ".$idInfo['disk'][$i]['dir']."/save/");
if (file_exists($idInfo['disk'][$i]['dir']."/save/".$torrentName)) {
echo file_get_contents(
$idInfo['server']."?TorrentIsOk&key=".$idInfo['key'].
"&torrentName=".$res."", "r"
);
}
}
} else {
echo "à envoyé ses données au serveur avec succès. \n";
}
}
}
/**
* Remove Disk
*
* @param Array $argv array of data
*
* @return boolean
**/
private function _removeDisk($argv)
{
$config=self::_getIdInfo();
$i = self::_diskCheck($argv);
var_dump($i);
if ($i != false || $i === 0) {
unset($config['disk'][$i]);
file_put_contents(self::_getUserDir(), json_encode($config));
echo "Removed disk";
} else {
echo "Disk doesn't exist";
}
}
/**
* Check if disk exist
*
* @param Array $argv array of data
*
* @return integer
**/
private function _diskCheck($argv)
{
$idInfo = self::_getIdInfo();
if (isset($idInfo['disk'])) {
$idInfo2 = $idInfo['disk'];
$count = count($idInfo2);
} else {
return false;
}
$x=false;
for ($i = 0; $i < $count; $i++) {
if ($idInfo['disk'][$i]['diskKey'] == self::_diskKey($argv)) {
$x=$i;
}
}
return $x;
}
/**
* Generate a randomKey
*
* @return string
**/
private function _random()
{
$characters='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$random = '';
for ($i = 0; $i < self::$_keyLength; $i++) {
$random .= $characters[rand(0, $charactersLength - 1)];
}
$randomKey = hash('sha256', $random);
return $randomKey;
}
/**
* Generate a new key
*
* @param Array $argv array of data
*
* @return string
**/
private function _diskKey($argv)
{
$fsName= explode("\n", shell_exec("df --output=source $argv"));
$blk= explode("\n", shell_exec("sudo blkid -o export $fsName[1]"));
foreach ($blk as $blkLine) {
$split = explode("=", $blkLine);
if ($split[0] === "UUID") {
$uuid = $split[1];
}
}
$diskKey = hash('sha256', $uuid);
return $diskKey;
}
/**
* Link the server address
*
* @return boolean
**/
private function _linkServer($address)
{
if (!file_exists(self::_getUserDir())) {
$link = array("server" => "$address");
if (file_put_contents(self::_getUserDir(), json_encode($link))) {
echo "\nServeur lié\n\n";
return true;
}
} elseif (file_exists(self::_getUserDir()) && !isset($config['server'])) {
$config=self::_getIdInfo();
self::_unLinkServer();
$link = array("server" => "$address");
if (file_put_contents(self::_getUserDir(), json_encode($link))) {
echo "\nServeur lié.\n\n";
return true;
} else {
echo "\nUn serveur est déjà lié \n\n";
return true;
}
return fasle;
}
}
/**
* unLink the server address
*
* @return boolean
**/
private function _unLinkServer()
{
if (file_exists(self::_getUserDir())) {
echo "\nSi vous continuez, le serveur ne sera plus lié et
les information sur les disque dur \n
ainsi que la clé d'identification seront detruite.
Pour continuer tapez 'yes' : ";
$handle = fopen ("php://stdin","r");
$line = fgets($handle);
if(strtolower(trim($line)) != 'yes'){
echo "\nAction annulé !\n\n";
exit;
}
fclose($handle);
echo "\n";
echo "Le serveur n'est plus lié,
les informations des disques dur et
la clé d'identification ont été supprimer aussi\n \n";
if (unlink(self::_getUserDir())) {
return true;
} else {
echo "\nune erreur est survenue \n\n";
return false;
}
} else {
echo "\nAucun lien serveur à supprimer \n\n";
return true;
}
return fasle;
}
/**
* Convert octets
*
* @param Array $number int
*
* @return string
**/
private function _convert($number)
{
if (is_numeric($number)) {
switch(true) {
case $number > 1000000000000:
$number = intval($number/1000000000000);
return "$number To";
break;
case $number > 1000000000 && $number < 1000000000000:
$number = intval($number/1000000000);
return "$number Go";
break;
case $number < 1000000000 && $number > 1000000:
$number = intval($number/1000000);
return "$number Mo";
break;
case $number < 1000000:
return "$number Octets";
break;
}
} else {
return null;
}
}
/**
* All torrents info
*
* @return Array
**/
private function _infoTorrents()
{
$req = shell_exec("transmission-remote -n 'test:test' -t all -i");
//echo $test;
$torrentArray = explode("\nNAME", $req);
$allTorrents = array();
foreach ($torrentArray as $key => $value) {
$lignes = explode("\n", $value);
$allTorrents[$key] = array();
foreach ($lignes as $key2 => $value2) {
if (strpos($value2, ': ') !== false) {
$param = explode(': ', $value2);
$allTorrents[$key][trim($param[0])]= trim($param[1]);
}
}
}
return $allTorrents;
}
/**
* Save Torrent
*
* @param Array $arg array of data
*
* @return boolean
**/
private function _save($file)
{
$idInfo = self::_getIdInfo();
if (!file_exists($file)) {
echo "Le fichier ou répertoire n'existe pas\n";
return;
}
$filesize = intval(filesize($file)/1000000000);
$check = file_get_contents(
$idInfo['server']."?check&key=".$idInfo['key'].
"&size=".$filesize."", "r"
);
if (!$check) {
echo " Il n'y a pas assez de place\n";
return;
}
$trackerTemp = parse_url($idInfo['server']);
$tracker = $trackerTemp["host"];
$baseName = basename($file);
if (!isset($baseName) || !isset($tracker) && !isset($file)) {
echo "Impossible de créer le torrent car il manque des informations.";
return;
}
echo shell_exec("transmission-create -o ~/".$baseName.
".torrent -t http://".$tracker."/announce.php $file");
$currUser = get_current_user();
$torrentFile = "/home/$currUser/".$baseName.".torrent";
if (!file_exists($torrentFile)) {
echo "Erreur lors de la création du torrent\n";
return;
}
$count = count($idInfo['disk']);
$freeSp = 0;
for ($i = 0; $i < $count; $i++) {
$dFS = disk_free_space($idInfo['disk'][$i]['dir']);
if ($freeSp < $dFS ){
$freeSp=$dFS;
$diskSave = $idInfo['disk'][$i]['dir'];
}
}
if (!file_exists($diskSave."/save")) {
shell_exec("sudo mkdir ".$diskSave."/save");
shell_exec("sudo chmod 755 ".$diskSave."/save");
}
if (rename($file, $diskSave."/save/".$baseName)) {
echo "Ajout du fichier réussi\n";
}
shell_exec("transmission-remote -n 'test:test' -a /home/$currUser/$baseName.torrent --find $diskSave/save");
$torrentsInfo = self::_infoTorrents();
foreach ($torrentsInfo as $key => $value) {
foreach ($value as $key2 => $value2) {
if ($value2 === $baseName ) {
$hash = $torrentsInfo[$key]["Hash"];
}
}
}
$key = $idInfo['key'];
$ch=curl_init();
$mime = mime_content_type("$diskSave/save/$baseName");
$cfile = curl_file_create('/home/'.$currUser.'/'.$baseName.'.torrent',$mime,$baseName);
$data = array(
'key' => $key,
'size' => filesize("$diskSave/save/$baseName"),
'hash' => $hash,
'fileToUpload' => $cfile
);
curl_setopt($ch, CURLOPT_URL, $idInfo['server']);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
if ($result === "false") {
echo "Le fichier existe déjà\n";
return;
}
echo $result;
}
/**
* TorrentSave Uninstall
*
* @return Array
**/
private function _uninstall()
{
$idInfo = self::_getIdInfo();
echo "\nPour supprimer Uniquement TorrentSave : '1',
Pour supprimer TorrentSave ainsi que les sauvegardes associées : '2',
Pour interrompre l'opération : '0'";
$handle = fopen ("php://stdin","r");
$line = fgets($handle);
switch(strtolower(trim($line)))
{
case '0':
echo "\nAction annulé !\n\n";
exit;
break;
case '1':
$uninstall = file_get_contents(
$idInfo['server']."?uninstall&key=".$idInfo['key']."", "r"
);
$hash = explode("\n", $uninstall);
$count = count($hash);
for ($i = 0; $i < $count; $i++) {
shell_exec("sudo transmission-remote -n 'transmission:transmission' -t ".$hash[$i]." -rad");
}
$currUser = get_current_user();
shell_exec("sudo rm -r /home/$currUser/.config/torrentSave");
shell_exec("sudo rm /usr/local/bin/torrentSave");
echo "\n Désinstallation terminé";
exit;
break;
case '2':
$uninstall = file_get_contents(
$idInfo['server']."?uninstall&key=".$idInfo['key']."", "r"
);
$hash = explode("\n", $uninstall);
$count = count($hash);
for ($i = 0; $i < $count; $i++) {
shell_exec("sudo transmission-remote -n 'transmission:transmission' -t ".$hash[$i]." -rad");
}
$countd = count($idInfo['disk']);
for ($i = 0; $i < $countd; $i++) {
$dir=$idInfo['disk'][$i]['dir']."/save";
shell_exec("sudo rm -r $dir");
}
$currUser = get_current_user();
shell_exec("sudo rm -r /home/$currUser/.config/torrentSave");
shell_exec("sudo rm /usr/local/bin/torrentSave");
echo "\n Désinstallation terminé";
exit;
break;
}
fclose($handle);
}
/**
* Command help
*
* @param Array $argv array of data
*
* @return string
**/
private function _help($argv)
{
$help =
"
NAME
--addDisk, -add ----> used to add a disk.
--removeDisk, -rm ----> used to remove disk.
--save, -save ----> used to create a new save/torrent
--send, -s ----> used to send key and disk data to server
--infoDisk, -i ----> used to get disk info from server
--link, -l ----> used to link server address
--unLink, -ul ----> used to unlink server address
--help, -h, -? ----> used to show this help.
SYNOPSIS
$argv[0] <option> <dir>
^ ^
| |
(add, remove or help) (Disk directory)
DESCRIPTION
1 - You need to link the server address ( --link ).
2 - Add one Disk or more.
3 -
EXAMPLES
$argv[0] --addDisk /disk1
$argv[0] --removeDisk /disk1
$argv[0] --save /folder1/folder2/.../file.exemple
$argv[0] --send
$argv[0] --infoDisk
$argv[0] --link http://wwww.exemple.com/exemple
$argv[0] --unLink
$argv[0] -help\n";
return $help;
}
}
Commands::verifArgs($argc,$argv);