view e2gallerypro/e2upload/Backend/Assets/getid3/module.lib.data_hash.php @ 28:118cb614cf18 judyates tip

judy bio.
author Robert McIntyre <rlm@mit.edu>
date Tue, 29 Dec 2015 16:26:18 -0800
parents 3f6b44aa6b35
children
line wrap: on
line source
1 <?php
2 // +----------------------------------------------------------------------+
3 // | PHP version 5 |
4 // +----------------------------------------------------------------------+
5 // | Copyright (c) 2002-2006 James Heinrich, Allan Hansen |
6 // +----------------------------------------------------------------------+
7 // | This source file is subject to version 2 of the GPL license, |
8 // | that is bundled with this package in the file license.txt and is |
9 // | available through the world-wide-web at the following url: |
10 // | http://www.gnu.org/copyleft/gpl.html |
11 // +----------------------------------------------------------------------+
12 // | getID3() - http://getid3.sourceforge.net or http://www.getid3.org |
13 // +----------------------------------------------------------------------+
14 // | Authors: James Heinrich <infoØgetid3*org> |
15 // | Allan Hansen <ahØartemis*dk> |
16 // +----------------------------------------------------------------------+
17 // | module.lib.data-hash.php |
18 // | getID3() library file. |
19 // | dependencies: NONE. |
20 // +----------------------------------------------------------------------+
21 //
22 // $Id: module.lib.data_hash.php,v 1.5 2006/12/03 19:28:18 ah Exp $
26 class getid3_lib_data_hash
27 {
29 private $getid3;
32 // constructer - calculate md5/sha1 data
33 public function __construct(getID3 $getid3, $algorithm) {
35 $this->getid3 = $getid3;
37 // Check algorithm
38 if (!preg_match('/^(md5|sha1)$/', $algorithm)) {
39 throw new getid3_exception('Unsupported algorithm, "'.$algorithm.'", in GetHashdata()');
40 }
43 //// Handle ogg vorbis files
45 if ((@$getid3->info['fileformat'] == 'ogg') && (@$getid3->info['audio']['dataformat'] == 'vorbis')) {
47 // We cannot get an identical md5_data value for Ogg files where the comments
48 // span more than 1 Ogg page (compared to the same audio data with smaller
49 // comments) using the normal getID3() method of MD5'ing the data between the
50 // end of the comments and the end of the file (minus any trailing tags),
51 // because the page sequence numbers of the pages that the audio data is on
52 // do not match. Under normal circumstances, where comments are smaller than
53 // the nominal 4-8kB page size, then this is not a problem, but if there are
54 // very large comments, the only way around it is to strip off the comment
55 // tags with vorbiscomment and MD5 that file.
56 // This procedure must be applied to ALL Ogg files, not just the ones with
57 // comments larger than 1 page, because the below method simply MD5's the
58 // whole file with the comments stripped, not just the portion after the
59 // comments block (which is the standard getID3() method.
61 // The above-mentioned problem of comments spanning multiple pages and changing
62 // page sequence numbers likely happens for OggSpeex and OggFLAC as well, but
63 // currently vorbiscomment only works on OggVorbis files.
65 if ((bool)ini_get('safe_mode')) {
66 throw new getid3_exception('PHP running in Safe Mode - cannot make system call to vorbiscomment[.exe] needed for '.$algorithm.'_data.');
67 }
69 if (!preg_match('/^Vorbiscomment /', `vorbiscomment --version 2>&1`)) {
70 throw new getid3_exception('vorbiscomment[.exe] binary not found in path. UNIX: typically /usr/bin. Windows: typically c:\windows\system32.');
71 }
73 // Prevent user from aborting script
74 $old_abort = ignore_user_abort(true);
76 // Create empty file
77 $empty = tempnam('*', 'getID3');
78 touch($empty);
80 // Use vorbiscomment to make temp file without comments
81 $temp = tempnam('*', 'getID3');
83 $command_line = 'vorbiscomment -w -c '.escapeshellarg($empty).' '.escapeshellarg(realpath($getid3->filename)).' '.escapeshellarg($temp).' 2>&1';
85 // Error from vorbiscomment
86 if ($vorbis_comment_error = `$command_line`) {
87 throw new getid3_exception('System call to vorbiscomment[.exe] failed.');
88 }
90 // Get hash of newly created file
91 $hash_function = $algorithm . '_file';
92 $getid3->info[$algorithm.'_data'] = $hash_function($temp);
94 // Clean up
95 unlink($empty);
96 unlink($temp);
98 // Reset abort setting
99 ignore_user_abort($old_abort);
101 // Return success
102 return true;
103 }
105 //// Handle other file formats
107 // Get hash from part of file
108 if (@$getid3->info['avdataoffset'] || (@$getid3->info['avdataend'] && @$getid3->info['avdataend'] < $getid3->info['filesize'])) {
110 if ((bool)ini_get('safe_mode')) {
111 $getid3->warning('PHP running in Safe Mode - backtick operator not available, using slower non-system-call '.$algorithm.' algorithm.');
112 $hash_function = 'hash_file_partial_safe_mode';
113 }
114 else {
115 $hash_function = 'hash_file_partial';
116 }
118 $getid3->info[$algorithm.'_data'] = $this->$hash_function($getid3->filename, $getid3->info['avdataoffset'], $getid3->info['avdataend'], $algorithm);
119 }
121 // Get hash from whole file - use built-in md5_file() and sha1_file()
122 else {
123 $hash_function = $algorithm . '_file';
124 $getid3->info[$algorithm.'_data'] = $hash_function($getid3->filename);
125 }
126 }
130 // Return md5/sha1sum for a file from starting position to absolute end position
131 // Using windows system call
132 private function hash_file_partial($file, $offset, $end, $algorithm) {
134 // It seems that sha1sum.exe for Windows only works on physical files, does not accept piped data
135 // Fall back to create-temp-file method:
136 if ($algorithm == 'sha1' && strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {
137 return $this->hash_file_partial_safe_mode($file, $offset, $end, $algorithm);
138 }
140 // Check for presence of binaries and revert to safe mode if not found
141 if (!`head --version`) {
142 return $this->hash_file_partial_safe_mode($file, $offset, $end, $algorithm);
143 }
145 if (!`tail --version`) {
146 return $this->hash_file_partial_safe_mode($file, $offset, $end, $algorithm);
147 }
149 if (!`${algorithm}sum --version`) {
150 return $this->hash_file_partial_safe_mode($file, $offset, $end, $algorithm);
151 }
153 $size = $end - $offset;
154 $command_line = 'head -c'.$end.' '.escapeshellarg(realpath($file)).' | tail -c'.$size.' | '.$algorithm.'sum';
155 return substr(`$command_line`, 0, $algorithm == 'md5' ? 32 : 40);
156 }
160 // Return md5/sha1sum for a file from starting position to absolute end position
161 // Using slow safe_mode temp file
162 private function hash_file_partial_safe_mode($file, $offset, $end, $algorithm) {
164 // Attempt to create a temporary file in the system temp directory - invalid dirname should force to system temp dir
165 if (($data_filename = tempnam('*', 'getID3')) === false) {
166 throw new getid3_exception('Unable to create temporary file.');
167 }
169 // Init
170 $result = false;
172 // Copy parts of file
173 if ($fp = @fopen($file, 'rb')) {
175 if ($fp_data = @fopen($data_filename, 'wb')) {
177 fseek($fp, $offset, SEEK_SET);
178 $bytes_left_to_write = $end - $offset;
179 while (($bytes_left_to_write > 0) && ($buffer = fread($fp, getid3::FREAD_BUFFER_SIZE))) {
180 $bytes_written = fwrite($fp_data, $buffer, $bytes_left_to_write);
181 $bytes_left_to_write -= $bytes_written;
182 }
183 fclose($fp_data);
184 $hash_function = $algorithm . '_file';
185 $result = $hash_function($data_filename);
187 }
188 fclose($fp);
189 }
190 unlink($data_filename);
191 return $result;
192 }
194 }
196 ?>