41 function filterArrayMatches($regex, $list)
45 foreach ($list as $item) {
47 if (preg_match($regex, $item, $matches)) {
48 array_push($ret, $matches);
63 function endsWith($main, $suffix)
65 $len = strlen($suffix);
66 return substr_compare($main, $suffix, -$len, $len) === 0;
82 while (
count($queue) > 0) {
83 $current = array_shift($queue);
85 foreach (scandir($current) as $item) {
86 if ($item[0] !=
'.') {
87 $filename =
"$current/$item";
89 switch (filetype($filename))
92 array_push($files, $filename);
95 array_push($queue, $filename);
112 function rrmdir($dir)
115 $objects = scandir($dir);
116 foreach ($objects as $object) {
117 if ($object !=
"." && $object !=
"..") {
118 if (filetype($dir .
"/" . $object) ==
"dir") {
119 rrmdir($dir .
"/" . $object);
121 unlink($dir .
"/" . $object);
139 function tmpdir($prefix=
'bag')
141 $dir = tempnam(sys_get_temp_dir(), $prefix);
155 function seenAtKey($array, $key, $item)
157 $keys = array_keys($array);
158 for ($x=0, $len=
count($keys); $x<$len; $x++) {
159 $sub = $array[$keys[$x]];
160 if (array_key_exists($key, $sub) && $sub[$key] == $item) {
175 function saveUrl($url, $filename)
177 $curl = curl_init($url);
178 $file = fopen($filename,
'w');
180 curl_setopt($curl, CURLOPT_FILE, $file);
181 curl_setopt($curl, CURLOPT_HEADER, 0);
197 function findFirstExisting($fileNames, $default=
null)
199 foreach ($fileNames as $fileName) {
200 if (file_exists($fileName)) {
217 function readFileText($fileName, $fileEncoding)
219 $data = iconv($fileEncoding,
'UTF-8', file_get_contents($fileName));
233 function readLines($fileName, $fileEncoding)
235 $data = readFileText($fileName, $fileEncoding);
236 $lines = preg_split(
'/[\n\r]+/', $data,
null, PREG_SPLIT_NO_EMPTY);
250 function writeFileText($fileName, $fileEncoding, $data)
252 file_put_contents($fileName, iconv(
'UTF-8', $fileEncoding, $data));
262 function BagIt_sanitizeFileName($filename)
265 $filename = preg_replace(
'/\s+/',
'_', $filename);
268 $filename = preg_replace(
269 '/\.{2}|[~\^@!#%&\*\/:\'?\"<>\|]/',
274 $forbidden =
'/^(CON|PRN|AUX|NUL|COM1|COM2|COM3|COM4|COM5| ' .
275 'COM6|COM7|COM8|COM9|LPT1|LPT2|LPT3|LPT4|LPT5|LPT6|' .
278 if (preg_match($forbidden, $filename)) {
279 $filename = strtolower($filename);
280 $suffix = substr(str_shuffle(
'abcdefghijklmnopqrstuvwxyz'), 0, 12);
281 $filename =
"{$filename}_{$suffix}";
294 function BagIt_readBagItFile($filename)
298 if (file_exists($filename)) {
299 $data = readFileText($filename,
'UTF-8');
301 $versions = BagIt_parseVersionString($data);
302 if ($versions ===
null) {
306 'Error reading version information from bagit.txt file.')
310 $fileEncoding = BagIt_parseEncodingString($data);
313 $versions = array(
'major' => 0,
'minor' => 96);
314 $fileEncoding =
'UTF-8';
317 return array($versions, $fileEncoding, $errors);
328 function BagIt_parseVersionString($bagitFileData)
331 $success = preg_match(
332 "/BagIt-Version: (\d+)\.(\d+)/",
338 $major = (int)$matches[1];
339 $minor = (int)$matches[2];
340 if ($major ===
null || $minor ===
null) {
341 throw new Exception(
"Invalid bagit version: '{$matches[0]}'.");
343 return array(
'major' => $major,
'minor' => $minor);
356 function BagIt_parseEncodingString($bagitFileData)
359 $success = preg_match(
360 '/Tag-File-Character-Encoding: (.*)/',
379 function BagIt_uncompressBag($compressedFile)
382 $dir = tempnam(sys_get_temp_dir(),
'bagit_');
388 $success = preg_match(
389 '/^(.*)\.(zip|tar\.gz|tgz)$/',
390 basename($compressedFile),
394 throw new ErrorException(
"File not compressed: $compressedFile.");
397 $bagBase = $matches[1];
402 $zip =
new ZipArchive();
403 $zip->open($compressedFile);
404 $zip->extractTo($dir);
406 $datadir = $dir .
'/' . $bagBase .
'/data';
408 if (!file_exists($datadir)) {
409 mkdir($datadir, 0700);
412 }
else if ($ext ==
'tgz' || $ext ==
'tar.gz') {
419 return "$dir/$bagBase";
431 function BagIt_compressBag($dirname, $output, $method=
'tgz')
433 $base = basename($dirname);
434 $stripLen = strlen($dirname) - strlen($base);
436 if ($method ==
'zip') {
437 $zip =
new ZipArchive();
438 $zip->open($output, ZIPARCHIVE::CREATE);
440 foreach (rls($dirname) as $file) {
441 $zip->addFile($file, substr($file, $stripLen));
446 }
else if ($method ==
'tgz') {
448 $tar->createModify($dirname, $base, $dirname);
462 function BagIt_validateExists($filename, &$errors)
464 if (! file_exists($filename)) {
465 $basename = basename($filename);
468 array($basename,
"$basename does not exist.")
482 function BagIt_parseBagInfo($lines)
487 foreach ($lines as $line) {
488 if (strlen($line) <= 1) {
490 }
else if ($line[0] ==
' ' || $line[0] ==
"\t") {
492 $val = $bagInfo[$prevKey];
493 if (is_array($val)) {
494 $val[
count($val) - 1] .=
' '. trim($line);
496 $val .=
' ' . trim($line);
498 $bagInfo[$prevKey] = $val;
500 list($key, $val) = preg_split(
'/:\s*/', $line, 2);
503 $bagInfo[$prevKey] = BagIt_getAccumulatedValue(
504 $bagInfo, $prevKey, $val
527 function BagIt_getAccumulatedValue($map, $key, $val)
529 if (array_key_exists($key, $map)) {
531 if (is_array($pval)) {
534 $pval = array( $pval, $val );