-
Notifications
You must be signed in to change notification settings - Fork 9
/
sfvinfo.php
167 lines (148 loc) · 3.6 KB
/
sfvinfo.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
<?php
require_once dirname(__FILE__).'/archivereader.php';
/**
* SfvInfo class.
*
* A simple class for inspecting SFV file data and listing information about the
* contents. Data can be streamed from a file or loaded directly from memory.
*
* Example usage:
*
* <code>
*
* // Load the SFV file or data
* $sfv = new SfvInfo;
* $sfv->open('./foo.sfv'); // or $sfv->setData($data);
* if ($sfv->error) {
* echo "Error: {$sfv->error}\n";
* exit;
* }
*
* // Process the file list
* $files = $sfv->getFileList();
* foreach ($files as $file) {
* echo $file['name'].' - '.$file['checksum'];
* }
*
* </code>
*
* @author Hecks
* @copyright (c) 2010-2013 Hecks
* @license Modified BSD
* @version 2.1
*/
class SfvInfo extends ArchiveReader
{
/**
* SFV file comments.
* @var string
*/
public $comments = '';
/**
* Convenience method that outputs a summary list of the SFV file records,
* useful for pretty-printing.
*
* @param boolean $basenames don't include full file paths?
* @return array file record summary
*/
public function getSummary($full=false, $basenames=false)
{
$summary = array(
'file_name' => $this->file,
'file_size' => $this->fileSize,
'data_size' => $this->dataSize,
'use_range' => "{$this->start}-{$this->end}",
'file_count' => $this->fileCount,
);
if ($full) {
$summary['file_list'] = $this->getFileList($basenames);
}
if ($this->error) {
$summary['error'] = $this->error;
}
return $summary;
}
/**
* Returns a list of file records with checksums from the source SFV file.
*
* @param boolean $basenames don't include full file paths?
* @return array list of file records, empty if none are available
*/
public function getFileList($basenames=false)
{
if ($basenames) {
$ret = array();
foreach ($this->fileList as $item) {
$item['name'] = pathinfo($item['name'], PATHINFO_BASENAME);
$ret[] = $item;
}
return $ret;
}
return $this->fileList;
}
/**
* Returns the position of the archive marker/signature.
*
* @return mixed Marker position, or false if none found
*/
public function findMarker()
{
return $this->markerPosition = 0;
}
/**
* The parsed file list with checksum info.
* @var array
*/
protected $fileList = array();
/**
* Parses the source data and stores a list of valid file records locally.
*
* @return boolean false if parsing fails
*/
protected function analyze()
{
// Get the available data up to the maximum allowed
try {
$data = $this->read(min($this->length, $this->maxReadBytes));
} catch (Exception $e) {
$this->close();
return false;
}
// Split on all line ending types
foreach (preg_split('/\R/', $data, -1, PREG_SPLIT_NO_EMPTY) as $line) {
// Store comment lines
if (strpos($line, ';') === 0) {
$this->comments .= trim(substr($line, 1))."\n";
continue;
}
if (preg_match('/^(.+?)\s+([[:xdigit:]]{2,8})$/', trim($line), $matches)) {
// Store the file record locally
$this->fileList[] = array(
'name' => $matches[1],
'checksum' => $matches[2]
);
// Increment the filecount
$this->fileCount++;
} else {
// Contains invalid chars, so assume this isn't an SFV
$this->error = 'Not a valid SFV file';
$this->close();
return false;
}
}
// Analysis was successful
$this->close();
return true;
}
/**
* Resets the instance variables before parsing new data.
*
* @return void
*/
protected function reset()
{
parent::reset();
$this->fileList = array();
$this->comments = '';
}
} // End SfvInfo class