-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnrar.php
137 lines (116 loc) · 3.05 KB
/
Unrar.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
#!/usr/bin/php
<?php
namespace Synology\DirectoryUtility;
class Unrar
{
private $indexFilePattern = ".sfv";
private $isComplete = false;
private $dirToScan;
private $indexFile;
private $rarFiles = [];
public function __construct($dirToScan)
{
$this->dirToScan = rtrim($dirToScan, DIRECTORY_SEPARATOR);
$this->checkDir();
}
private function checkDir()
{
if (!is_dir($this->dirToScan)) {
throw new \Exception(sprintf('\'%s\' not is a valid dir', $this->dirToScan));
}
$dirIterator = new \DirectoryIterator($this->dirToScan);
$sfvFound = false;
foreach ($dirIterator as $item) {
if ($item->isDot() || $item->isDir()) continue;
if (strpos($item->getFilename(), $this->indexFilePattern) !== false) {
$sfvFound = true;
$this->indexFile = $item->getPathName();
break;
}
}
if ($sfvFound) {
//var_dump($this->indexFile);
$temp = explode("\n", file_get_contents($this->indexFile));
//preg_match("/^(?P<file>.*\.r\d{1,3})\s(?P<hash>[a-h0-9]*)$/", $input_line, $output_array);
foreach ($temp as $line) {
if (strlen(trim($line)) == 0 ) continue;
$detail = (explode(" ", $line));
$this->rarFiles[trim($detail[1])] = $this->dirToScan.DIRECTORY_SEPARATOR.trim($detail[0]);
}
}
if (count($this->rarFiles) == 0) {
$this->isComplete = false;
return;
}
$fileExistence = true;
foreach ($this->rarFiles as $crcHex => $file) {
//echo "$file\n";
if (!is_file($file)) {
var_dump("xxx-->".$file);
$fileExistence = false;
break;
}
}
$isComplete = true;
if ($fileExistence) {
foreach ($this->rarFiles as $crcHex => $file) {
if (!$this->crcIsValid($crcHex, $file)) {
$isComplete = false;
break;
}
}
}
//var_dump($fileExistence);
//var_dump($isComplete);
$this->isComplete = $fileExistence && $isComplete;
}
private function crcIsValid($hexa, $file)
{
echo "file: $file\n";
$founded = str_pad ( \dechex($this->getCrc($file)), 8, $pad_string = "0", STR_PAD_LEFT);
echo sprintf("found :%s = %s", $founded, $hexa)."\n";
return $founded == $hexa;
}
private function getCrc($file)
{
$file_string = file_get_contents($file);
$crc = crc32($file_string);
unset($file_string);
return sprintf("%s", $crc);
}
public function isComplete() {
return $this->isComplete;
}
public function run()
{
$file = end($this->rarFiles);
$cmd = sprintf("unrar x -y %s %s > /dev/null", $file, $this->dirToScan);
$return_var;
echo $cmd."\n";
$buff = system ( $cmd, $return_var );
if( $return_var == 0) {
foreach ($this->rarFiles as $file) {
echo "Remove $file \n";
unlink($file);
}
unlink($this->indexFile);
}
}
public function containRar()
{
return count($this->rarFiles) > 0;
}
}
$cdw = is_dir($argv[1]) ? $argv[1] : dirname(__FILE__);
ini_set('memory_limit', '-1');
$unraredDir = new Unrar($cdw);
if ($unraredDir->containRar()) {
if ($unraredDir->isComplete()) {
$unraredDir->run();
} else {
echo "$cdw is not complete\n";
}
} else {
//echo "$cdw Doesn't contain rar files\n";
}
//$history->write();