-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChecksumGenerator.php
More file actions
189 lines (175 loc) · 6.28 KB
/
Copy pathChecksumGenerator.php
File metadata and controls
189 lines (175 loc) · 6.28 KB
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
<?php
/**
* Checksum Generator
*
* @copyright Vavaballz
* @link https://github.com/vavaballz/ChecksumGenerator
*/
class ChecksumGenerator{
private $dir;
private $filename;
private $usedMethod;
private $wantedFields = [];
private $xml;
private $json = array();
private $array = array();
const AS_XML = 0;
const AS_JSON = 1;
const AS_ARRAY = 2;
public function __construct(){
$this->filename = str_replace(".php", "", str_replace(__DIR__ . DIRECTORY_SEPARATOR, "", __FILE__));
$this->wantedFields = ['path', 'size', 'md5', 'mtime'];
}
/**
* @param array $field contain the
* wanted fields
*/
public function setFields($field = []){
if(!empty($field)){
$this->wantedFields = $field;
}else{
$this->wantedFields = ['path', 'size', 'md5', 'mtime'];
}
}
/**
* Set the dir path
* @param String $dir
*/
public function setDir($dir){
if(substr($dir, strlen($dir)-1, strlen($dir)) == "/" && !empty($dir)){
$dir = substr($dir, 0, strlen($dir)-1);
}
$this->dir = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $dir);
}
/**
* Set the file name
* @param String $filename
*/
public function setFilename($filename){
$this->filename = $filename;
}
/**
* Set de wanted method
*
* @param integer $method
*/
public function setUsedMethod($method){
$this->usedMethod = $method;
}
/**
* This func is to generate a XML file
* which contain files path, md5 checksum
* and size.
*/
public function generate(){
if($this->usedMethod == self::AS_XML && $this->xml == null){
$this->xml = new SimpleXMLElement('<ListBucketResult/>');
}
$caller = debug_backtrace();
if(isset($caller[1]) && func_get_args()){
$args = func_get_args();
$dir = $args[0];
}else{
$dir = $this->dir;
}
if(is_dir($dir)){
if ($dh = opendir($dir)){
while( $file = readdir($dh) ) {
if ($file === '.' || $file === '..') continue;
$this->generate($dir.DIRECTORY_SEPARATOR.$file);
}
closedir($dh);
}
}else{
if(file_exists($dir)){
$fdir = str_replace($_SERVER['DOCUMENT_ROOT']."/", "", $dir);
$fdir = str_replace("..".DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR, $fdir);
if($this->usedMethod == self::AS_XML){
$f = $this->xml->addChild('Contents');
if(in_array("path", $this->wantedFields)){
$f->addChild('Key', str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $fdir));
}
if(in_array("size", $this->wantedFields)){
$f->addChild('Size', filesize($dir));
}
if(in_array("md5", $this->wantedFields)){
$f->addChild('ETag', "\"".md5_file($dir)."\"");
}
if(in_array("mtime", $this->wantedFields)){
$f->addChild('Mtime', filemtime($dir));
}
}else if($this->usedMethod == self::AS_JSON){
$json_array = ['file' => []];
if(in_array("path", $this->wantedFields)){
$json_array['file']['path'] = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $fdir);
}
if(in_array("size", $this->wantedFields)){
$json_array['file']['size'] = filesize($dir);
}
if(in_array("md5", $this->wantedFields)){
$json_array['file']['md5'] = md5_file($dir);
}
if(in_array("mtime", $this->wantedFields)){
$json_array['file']['mtime'] = filemtime($dir);
}
array_push($this->json, $json_array);
}else if($this->usedMethod == self::AS_ARRAY){
$file_array = ['file' => []];
if(in_array("path", $this->wantedFields)){
$file_array['file']['path'] = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $dir);
}
if(in_array("size", $this->wantedFields)){
$file_array['file']['size'] = filesize($dir);
}
if(in_array("md5", $this->wantedFields)){
$file_array['file']['md5'] = md5_file($dir);
}
if(in_array("mtime", $this->wantedFields)){
$file_array['file']['mtime'] = filemtime($dir);
}
array_push($this->array, $file_array);
}
}else{
echo "Unknow directory";
}
}
}
/**
* Save as file
* The file type is set according
* to the generating method used.
*/
public function save(){
if($this->usedMethod == self::AS_XML){
$this->xml->saveXML(__DIR__.DIRECTORY_SEPARATOR.$this->filename . ".xml");
$this->xml = null;
}else if($this->usedMethod == self::AS_JSON){
$json = json_encode($this->json);
file_put_contents(__DIR__.DIRECTORY_SEPARATOR.$this->filename.'.json', $json);
$this->array = null;
}else if($this->usedMethod == self::AS_ARRAY) {
echo "You can't use the save method for the <b>ARRAY</b> method";
$this->array = null;
}
}
/**
* Get the generated
* into a var
* @return mixed return a array, json or xml code
*/
public function get(){
if($this->usedMethod == self::AS_XML){
$xml = $this->xml;
$this->xml = null;
return $xml;
}else if($this->usedMethod == self::AS_JSON){
$json = json_encode($this->json);
$this->json = null;
return $json;
}else if($this->usedMethod == self::AS_ARRAY){
$array = $this->array;
$this->array = null;
return $array;
}
}
}