2 require_once("../config.php");
4 function zfill($n, $a) {
5 return str_repeat("0", max(0,$a-strlen($n))) . $n;
8 function named_records_sort($named_recs, $order_by, $reverse=false, $flags=0) {
10 foreach ($named_recs as $key => $fields)
11 $named_hash[$key] = $fields[$order_by];
14 arsort($named_hash,$flags=0) ;
16 asort($named_hash, $flags=0);
18 $sorted_records = array();
19 foreach($named_hash as $key => $val)$sorted_records[$key] = $named_recs[$key];
21 return $sorted_records;
24 function validate_user($username, $password, $salt) {
25 $creds['validated'] = false;
26 $query="SELECT * FROM users WHERE user_name = '".mysql_escape_string($username)."'";
27 $q_result = mysql_query($query);
28 while ($fetched_object = mysql_fetch_object($q_result)) {
29 if (md5($fetched_object->user_pass.$salt) == $password) {
31 $creds['user_id'] = $fetched_object->user_id;
32 $creds['user_name'] = $fetched_object->user_name;
33 $creds['user_level'] = $fetched_object->user_level;
34 $creds['validated'] = true;
35 $log_hash=str_repeat("0",32); // File ID is always empty on login
36 $q="INSERT INTO log (hash,action,user_id,user_name,ip,date) VALUES ('".mysql_escape_string($log_hash)."','login',".$creds['user_id'].",'".mysql_escape_string($creds['user_name'])."','".mysql_escape_string($_SERVER['REMOTE_ADDR'])."',NOW())";
40 $_SESSION['creds'] = $creds;
41 return $creds['validated'];
45 function get_smarty() {
46 require_once(SQUASHER_SMARTY_SOURCE); // See config.php
49 $smarty->setTemplateDir(SQUASHER_SMARTY_TEMPLATES)
50 ->setCompileDir(SQUASHER_SMARTY_TEMPLATES_C)
51 ->setCacheDir(SQUASHER_SMARTY_CACHE);
60 var $configs = array();
62 var $subfolders = array();
65 var $history = array();
67 function set_root($root) {
68 $this->basepath = $root;
71 function get_configs() {
72 return $this->configs;
75 function subfolders() {
76 return $this->subfolders;
79 function get_config($h) {
81 return $this->configs[$h];
84 function folderrights() {
85 return $this->folderrights;
88 function userrights() {
89 return $this->userrights;
92 function update_history() {
93 $q="SELECT md5_hash,file,completed,checked FROM file_hash";
95 while($o=mysql_fetch_object($r)) {
96 $this->history[$o->md5_hash]['file']=$o->file;
97 $this->history[$o->md5_hash]['completed']=$o->completed;
98 $this->history[$o->md5_hash]['checked']=$o->checked;
102 function get_users($user_level) {
103 $q = "SELECT * FROM users WHERE user_level < ".$user_level." ORDER BY user_name ASC";
104 $r = mysql_query($q);
106 while ($o = mysql_fetch_object($r)) {
107 $return[$o->user_id]['id'] = $o->user_id;
108 $return[$o->user_id]['name'] = $o->user_name;
109 $return[$o->user_id]['level'] = $o->user_level;
110 $return[$o->user_id]['enabled'] = ($o->user_pass == '') ? false : true ;
116 function get_logs($type='all') {
117 $q="SELECT log.* FROM log WHERE log.user_id != '1' and ip != '87.233.211.2' ";
118 if ($_SESSION['creds']['user_id'] == 1)
119 $q="SELECT log.* FROM log WHERE log.user_id != 'x' ";
122 $q.= " AND log.action = 'delete'";
125 $q.= " AND log.action = 'download'";
128 $q.= " AND log.action = 'login'";
131 $q.= " AND log.action = 'debug'";
138 $today =" AND date > date(date_add(now(), interval -0 day)) ";
139 $yesterday =" AND date < date(date_add(now(), interval -0 day)) AND date > date(date_add(now(), interval -1 day)) ";
140 $lastweek =" AND date < date(date_add(now(), interval -1 day)) AND date > date(date_add(now(), interval -6 day)) ";
141 $older =" AND date < date(date_add(now(), interval -7 day)) AND date > date(date_add(now(), interval -30 day)) ";
143 $order=" ORDER BY log.log_id desc ";
144 $r = mysql_query($q.$today.$order);
147 'yesterday'=>array(),
151 while($a = mysql_fetch_array($r)) {
152 $qu="SELECT users.user_name FROM users LEFT JOIN log ON users.user_id = log.user_id WHERE log.ip='".mysql_escape_string($a['ip'])."' GROUP BY users.user_name";
153 $ru = mysql_query($qu);
154 $a['users_from_ip'] = " | ";
155 while($au = mysql_fetch_array($ru))
156 $a['users_from_ip'] .= $au['user_name']." | ";
157 $return['today'][$a['log_id']] = $a;
159 $r = mysql_query($q.$yesterday.$order);
160 while($a = mysql_fetch_array($r)) {
161 $qu="SELECT users.user_name FROM users LEFT JOIN log ON users.user_id = log.user_id WHERE log.ip='".mysql_escape_string($a['ip'])."' GROUP BY users.user_name";
162 $ru = mysql_query($qu);
163 $a['users_from_ip'] = " | ";
164 while($au = mysql_fetch_array($ru))
165 $a['users_from_ip'] .= $au['user_name']." | ";
166 $return['yesterday'][$a['log_id']] = $a;
168 $r = mysql_query($q.$lastweek.$order);
169 while($a = mysql_fetch_array($r)) {
170 $qu="SELECT users.user_name FROM users LEFT JOIN log ON users.user_id = log.user_id WHERE log.ip='".mysql_escape_string($a['ip'])."' GROUP BY users.user_name";
171 $ru = mysql_query($qu);
172 $a['users_from_ip'] = " | ";
173 while($au = mysql_fetch_array($ru))
174 $a['users_from_ip'] .= $au['user_name']." | ";
175 $return['lastweek'][$a['log_id']] = $a;
177 $r = mysql_query($q.$older.$order);
178 while($a = mysql_fetch_array($r)) {
179 $qu="SELECT users.user_name FROM users LEFT JOIN log ON users.user_id = log.user_id WHERE log.ip='".mysql_escape_string($a['ip'])."' GROUP BY users.user_name";
180 $ru = mysql_query($qu);
181 $a['users_from_ip'] = " | ";
182 while($au = mysql_fetch_array($ru))
183 $a['users_from_ip'] .= $au['user_name']." | ";
184 $return['older'][$a['log_id']] = $a;
190 function insert_users($u, $admin_level) {
191 $user_name = $u['user_name'];
192 $user_pass = $u['user_pass'];
193 $user_level = (int)$u['user_level'];
195 $q = "INSERT INTO users (user_name,user_pass,user_level) VALUES ('".mysql_escape_string($user_name)."', '".mysql_escape_string(md5($user_pass))."', ".$user_level.")";
196 $r = mysql_query($q);
199 function update_users($u) {
200 $user_id = (int)$u['user_id'];
201 $user_name = @$u['user_name'];
202 $user_pass = @$u['user_pass'];
203 $user_level = (int)@$u['user_level'];
205 $q = "UPDATE users SET user_name = '".mysql_escape_string($user_name)."' WHERE user_id = ".$user_id;
206 $r = mysql_query($q);
209 $q = "UPDATE users SET user_pass = '".mysql_escape_string(md5($user_pass))."' WHERE user_id = ".$user_id;
210 $r = mysql_query($q);
213 $q = "UPDATE users SET user_level = ".$user_level." WHERE user_id = ".$user_id;
214 $r = mysql_query($q);
218 function disable_users($u) {
219 $user_id = (int)$u['user_id'];
220 $user_name = @$u['user_name'];
221 $user_level = (int)@$u['user_level'];
223 $q = "UPDATE users SET user_name = '".mysql_escape_string($user_name)."' WHERE user_id = ".$user_id;
224 $r = mysql_query($q);
226 $q = "UPDATE users SET user_pass = '' WHERE user_id = ".$user_id;
227 $r = mysql_query($q);
229 $q = "UPDATE users SET user_level = ".$user_level." WHERE user_id = ".$user_id;
230 $r = mysql_query($q);
232 $r = mysql_query($q);
235 function remove_users($u) {
236 $user_id = (int)$u['user_id'];
237 $q = "DELETE FROM users WHERE user_id = ".$user_id;
238 $r = mysql_query($q);
241 function get_rights($user_id) {
244 $q = "SELECT folder_path, access FROM user_rights WHERE user_id = ".(int)$user_id;
245 $r = mysql_query($q);
246 while ($o = mysql_fetch_object($r)) {
249 $arr_string = '$result';
251 //get foldernames from path
252 if ($o->folder_path != '/') {
253 $path = $o->folder_path;
255 $path=substr($path,1);
256 $folder_arr = explode('/',$path);
258 //create folder structure array
259 foreach ($folder_arr AS $key => $value) {
260 $value_escaped = str_replace("'", "\\'", $value);
261 $arr_string .= "['".$value_escaped."']";
264 $arr_string .= "['__access__']";
266 eval($arr_string." = '".$o->access."';");
271 function give_rights($user_id, $type='folderrights') {
272 if ($type=='folderrights')
273 $this->folderrights = $this->get_rights($user_id);
274 else // $type=='userrights'
275 $this->userrights = $this->get_rights($user_id);
278 function update_rights($edited_user, $m, $admin_level) {
279 $q = "SELECT count(*) result FROM users WHERE user_id = ".(int)$edited_user." AND user_level < ".(int)$admin_level;
280 $r = mysql_query($q);
281 $o = mysql_fetch_object($r);
284 foreach ($m AS $path => $access) {
285 $p_q = "SELECT count(*) result FROM user_rights WHERE user_id = ".(int)$edited_user." AND folder_path = '".mysql_escape_string($path)."'";
286 $p_r = mysql_query($p_q);
287 $p_o = mysql_fetch_object($p_r);
288 if ($p_o->result == 1)
289 mysql_query("UPDATE user_rights SET access = ".(int)$access." WHERE folder_path = '".mysql_escape_string($path)."' AND user_id = ".(int)$edited_user);
290 if ($p_o->result == 0)
291 mysql_query("INSERT INTO user_rights (user_id,folder_path,access) values (".(int)$edited_user.",'".mysql_escape_string($path)."',".(int)$access.") ");
297 function show_rights_tree($path, $depth=0, $userid=0) {
302 $this->give_rights($userid, 'userrights');
304 // access = 0 --deny-all
305 // access = 1 --allow-dir-only
306 // access = 2 --allow-inc-subs
311 for ($i=0; $i<$depth; $i++)
313 if ($dir = opendir($path)) {
314 $layout .= "<div style='clear:both;' >\n";
316 while (false !== ($file = readdir($dir))) {
317 $files_array[] = $file;
320 foreach ($files_array as $f_index => $file) {
321 if (($file{0} !== ".") && ($file !== ".."))
323 $filename = $path."/".$file;
324 if (!is_file($filename) && $this->got_rights_array($filename) > 0) {
326 if (substr($filename,0,strlen($this->basepath))==$this->basepath)
327 $name = substr($filename,strlen($this->basepath));
328 $check = $this->got_rights_array_admin($filename, $this->userrights);
334 $check_all = 'checked';
337 $check_allow = 'checked';
341 $check_deny = 'checked';
346 $style[$depth] = ".depth".$depth."{float:right;width:".(600-($depth*10))."px;border-left:2px solid #FFFFFF;border-top:1px solid #FFFFFF;background:#".dechex(14-$depth).dechex(14-$depth).dechex(14-$depth).dechex(14-$depth).dechex(14-$depth).dechex(14-$depth).";}";
348 $layout .= "<div class='depth".$depth."'>";
349 $layout .= "<div class='white_border' >".htmlspecialchars($file)."</div>\n";
350 $name_escaped = htmlspecialchars($name);
351 $layout .= '<div class="check_deny"><input name="m['.$name_escaped.']" value=0 type=radio '.$check_deny."></div>\n";
352 $layout .= '<div class="check_allow"><input name="m['.$name_escaped.']" value=1 type=radio '.$check_allow."></div>\n";
353 $layout .= '<div class="check_all"><input name="m['.$name_escaped.']" value=2 type=radio '.$check_all."></div>\n";
354 $sub_return = $this->show_rights_tree($filename,$depth+1,$userid);
355 if (is_array($style) && is_array($sub_return['style']))
356 $style = $style + $sub_return['style'];
357 $layout .= $sub_return['layout'];
363 $layout .= "</div>\n";
366 $return['style'] = $style;
367 $return['layout'] = $layout;
372 function got_rights_array($needle, $haystack='', $c=0) {
374 // - read_single_file
376 // - show_rights_tree
378 if (!is_array($haystack))
379 $haystack = $this->folderrights;
380 if (substr($needle, 0, strlen($this->basepath)) == $this->basepath)
381 $needle=substr($needle,strlen($this->basepath));
384 if ($needle{0} == '/' && @$haystack['__access__'] == 2 )
389 $needle = substr($needle, 1);
391 $needle_arr = explode('/', $needle);
392 $n = count($needle_arr);
395 foreach ($haystack as $k => $v) {
396 if ($needle_arr[$c] == $k) {
397 if (!is_array(@$v['__access__'])) {
398 if ($v['__access__'] == 2 ) return 2;
399 if ($v['__access__'] == 1 && $d == $n ) return 1;
400 if ($v['__access__'] == 0 && $d == $n ) return 0;
402 $return = $this->got_rights_array($needle, $v, $d);
409 function got_rights_array_admin($needle, $haystack='', $c=0) {
411 // - show_rights_tree
413 if (!is_array($haystack))
414 $haystack = $this->folderrights;
415 if (substr($needle, 0, strlen($this->basepath)) == $this->basepath)
416 $needle = substr($needle, strlen($this->basepath));
419 if ($needle{0} == '/' && @$haystack['__access__'] == 2 )
423 if ($needle{0} == '/')
424 $needle = substr($needle, 1);
426 $needle_arr = explode('/', $needle);
427 $n = count($needle_arr);
432 if (@$haystack['__access__'] == 2)
433 return $haystack['__access__'];
434 if (is_array($haystack[$needle_arr[$c]]))
435 $return = $this->got_rights_array_admin($needle, $haystack[$needle_arr[$c]], $d);
437 if (@$haystack['__access__'] > 0)
438 $return = $haystack['__access__'];
444 function got_rights_array_recursive($needle, $haystack='', $c=0) {
446 // - read_directory, for subfolders
448 if (!is_array($haystack))
449 $haystack = $this->folderrights;
450 if (substr($needle, 0, strlen($this->basepath)) == $this->basepath)
451 $needle=substr($needle, strlen($this->basepath));
454 if($needle{0}=='/' && @$haystack['__access__'] == 2 )
457 // check folder rights
459 $needle=substr($needle,1);
461 $needle_arr = explode('/', $needle);
462 $n = count($needle_arr);
466 foreach($haystack as $k => $v) {
467 if ($c < sizeof($needle_arr) && $needle_arr[$c] == $k) {
469 if ($v['__access__'] == 2)
470 $return = $return + $v['__access__'];
471 $return = $return + $this->got_rights_array_recursive($needle, $v, $d);
473 $return = $return + $this->in_array_recursive($v);
475 } elseif ($c == $n) {
476 $return = $return + $v['__access__'];
477 if($k != '__access__')$return = $return + $this->in_array_recursive($v);
484 function in_array_recursive($haystack) {
486 if (is_array($haystack)) {
487 foreach ($haystack as $key1 => $value1) {
488 if (is_array($value1)) {
489 $return = $return + $this->in_array_recursive($value1);
491 elseif ($value1 > 0) {
499 function read_single_file($path, $file) {
500 $filename = $path."/".$file;
502 if ($this->got_rights_array($path) > 0) {
503 if (is_file($filename.'.Completed'))
505 if (is_file($filename.'.InProgress'))
506 $file.='.InProgress';
507 if (is_file($filename.'.Starting'))
509 if (is_file($filename.'.Processed'))
511 $filename = $path . "/" . $file;
512 $handle = @fopen($filename, "rb");
513 if (strpos($file,'.Completed'))
515 if (strpos($file,'.InProgress'))
517 if (strpos($file,'.Starting'))
519 if (strpos($file,'.Processed'))
521 $sub_pos = strpos($file, $ext);
522 $base_name = substr($file, 0, $sub_pos);
523 $filecontent = @fread($handle, @filesize($filename));
524 $config[$i] = explode("\r\n", $filecontent);
533 * [6] -> CRC32 checksum
535 if (@filesize($filename) > 0) {
536 $h = md5($path."/".$config[$i][2]);
537 $this->configs[$h] = $config[$i];
538 $this->configs[$h]['squashed'] = true;
539 $this->configs[$h]['path'] = $path;
540 $this->configs[$h]['status'] = substr($ext, 1);
541 $this->configs[$h]['mime'] = $this->set_mime($this->configs[$h][2]);
542 $this->configs[$h]['hidden'] = (is_file($path.'/'.$base_name.'.hidden')) ? true : false ;
543 //to prevent dates of 1-1-1970 we set te dates of the config file
544 $this->configs[$h]['added'] = filectime($filename);
545 $this->configs[$h]['lastchange'] = filemtime($filename);
547 $this->populate_stats($path, $h);
549 #$this->update_hash($h,$path."/".$config[$i][2]);
551 $this->check_stats($h);
557 function read_directory($path, $getsubs=false, $getfirstfiles=true, $getdeepfiles=true, $populate=true) {
559 if ($dir = @opendir($path)) {
562 while (false !== ($file = readdir($dir))) {
563 if (($file{0} !== ".") && substr($file,0,1) !== "SQ") {
564 $filename = $path."/".$file;
565 if (!is_file($filename) && strpos($filename, './uploads/recieving')===false) {
567 if ($this->got_rights_array_recursive($filename) > 0) {
568 $key = substr($path, strlen($this->basepath)) . '/' . $file;
569 $this->subfolders[$key] = $file;
573 $this->read_directory($filename, false, $getdeepfiles, $getdeepfiles, $populate);
574 } elseif (strpos($filename,'./uploads/ftp')) { //ftp files
575 if ($this->got_rights_array($path) > 0 && !strpos($filename, '.hidden') ) {
577 $name_only = substr($filename, strlen($path)+1);
578 $file_structure = explode('.', $name_only);
579 $ext = array_pop($file_structure);
580 $base_name = array_pop($file_structure);
581 $this->configs[$h]['path'] = $path;
582 $this->configs[$h][0] = 'manual ftp';
583 $this->configs[$h][2] = $name_only;
584 $this->configs[$h][3] = filesize($filename);
585 $this->configs[$h]['added'] = filectime($filename);
586 $this->configs[$h]['lastchange'] = filemtime($filename);
587 $this->configs[$h]['status'] = 'unknown';
588 $this->configs[$h]['squashed'] = false;
589 $this->configs[$h]['mime'] = $this->set_mime($name_only);
590 $this->configs[$h]['hidden'] = (is_file($path.'/'.$base_name.'.hidden')) ? true : false ;
592 } elseif ($getfirstfiles) {
594 if ($this->got_rights_array($path) > 0) {
595 if (strpos($filename, '.Completed') || strpos($filename, '.InProgress') || strpos($filename, '.Starting') || strpos($filename, '.Processed')) {
597 $handle = @fopen($filename, "rb");
598 if (strpos($file,'.Completed'))
600 if (strpos($file,'.InProgress'))
602 if (strpos($file,'.Starting'))
604 if (strpos($file,'.Processed'))
606 $sub_pos = strpos($file, $ext);
607 $base_name = substr($file, 0, $sub_pos);
608 $filecontent = @fread($handle, @filesize($filename));
609 $config[$i] = explode("\r\n", $filecontent);
619 * [6] -> CRC32 checksum
622 if (@filesize($filename) > 0) {
623 $h = md5($path."/".$config[$i][2]);
624 $this->configs[$h] = $config[$i];
625 $this->configs[$h]['squashed'] = true;
626 $this->configs[$h]['path'] = $path;
627 $this->configs[$h]['status'] = substr($ext, 1);
628 $this->configs[$h]['mime'] = $this->set_mime($this->configs[$h][2]);
629 $this->configs[$h]['hidden'] = (is_file($path.'/'.$base_name.'.hidden')) ? true : false ;
630 //to prevent dates of 1-1-1970 we set te dates of the config file
631 $this->configs[$h]['added'] = filectime($filename);
632 $this->configs[$h]['lastchange'] = filemtime($filename);
634 $this->populate_stats($path, $h);
636 $this->update_hash($h, $path."/".$config[$i][2]);
638 $this->check_stats($h);
648 function check_stats($h) {
649 $config = $this->get_config($h);
650 $count = @array_sum($config['stats']);
651 if ($config['status']=='Completed' && $count != $config[5]) {
652 $filepath=$config['path'].'/'.$config[2];
653 if ($this->history[$h]['completed']=="1") {
654 //don't display broken file, remove it instead
655 unlink($config['path'].'/'.$config[2].'.Completed');
656 unset($this->configs[$h]);
660 $m_subject = "Squasher Debug: File Removed";
661 $m_body = "Upload removed: \n File: ".$config['path']."/".$config[2]." \n Status: ".$config['status']." \n Chunks: ".$count." out of ".$config[5];
662 mail('jasper@netformatie.nl', $m_subject, $m_body, "From: support@netformatie.nl");
663 $qlog = "INSERT INTO log (hash,file,action,user_id,user_name,ip,date) VALUES ('".mysql_escape_string($h)."','".mysql_escape_string($filepath)."','debug',-1,'squasher-web','cleanup',NOW())";
667 rename($config['path'].'/'.$config[2].'.Completed', $config['path'].'/'.$config[2].'.InProgress');
668 $this->configs[$h]['status'] = 'InProgress';
673 $m_subject = "Squasher Debug: Upload Error";
674 $m_body = "Upload error: \n File: ".$config['path']."/".$config[2]." \n Status: ".$config['status']." \n Chunks: ".$count." out of ".$config[5];
675 mail('support@netformatie.nl', $m_subject, $m_body, "From: squasher@netformatie.nl");
676 mail('jan@netformatie.nl', $m_subject, $m_body, "From: support@netformatie.nl");
677 mail('joop@netformatie.nl', $m_subject, $m_body, "From: support@netformatie.nl");
678 mail('jasper@netformatie.nl', $m_subject, $m_body, "From: support@netformatie.nl");
681 //wget -o/dev/null "http://www.mollie.nl/xml/sms/?username=netformatie&password=SMSdolsi&originator=Netformatie&recipients=${ENGINEER}&message=${CALLERID}";
684 $ship = explode('/',$config['path']);
685 if ($ship[2] == 'myas' || $ship[2] == 'myez' || $ship[2] == 'myrw')
686 mail('ro1@'.$ship[2].'.greenpeace.org','Squasher: '.$config[2].' resume request','The squasher server has detected an upload error. Please resume the squasher transmission for '.$config[2].' to correct this problem.',"From: support@netformatie.nl\nX-Priority: 1");
688 $qlog = "INSERT INTO log (hash,file,action,user_id,user_name,ip,date) VALUES ('".mysql_escape_string($h)."','".mysql_escape_string($filepath)."','debug',-1,'squasher-web','retry',NOW())";
692 } elseif ($config['status']=='Completed' && $count == $config[5]) {
693 if ($this->history[$h]['completed']==0)
694 mysql_query("UPDATE file_hash SET completed = 1 WHERE md5_hash = '".mysql_escape_string($h)."'");
698 function update_hash($hash, $path) {
699 $check_hash_query = "select * from file_hash where md5_hash = '".$hash."'";
700 $check_hash_result = mysql_query($check_hash_query);
701 if(mysql_num_rows($check_hash_result) == 0) {
702 $insert_hash_query = "INSERT INTO file_hash (md5_hash,file) values ('".mysql_escape_string($hash)."','".mysql_escape_string($path)."')";
703 mysql_query($insert_hash_query);
707 function path_to_arraystring($path, $arrayname) {
708 $path_values = explode('/', $path);
709 $return = $arrayname;
710 foreach ($path_values AS $key => $value) {
711 if ($value != '.' && $value != '')
712 $return.= "['".$value."']";
717 function populate_stats($path, $h) {
726 * [6] -> CRC32 checksum
728 $config = $this->configs[$h];
730 $q = "SELECT * FROM file_hash WHERE md5_hash = '".mysql_escape_string($h)."'";
731 $r = mysql_query($q);
732 $o = mysql_fetch_object($r);
733 $validated_chunks = $o->validated_chunks;
735 if ($this->history[$h]['completed']=="1") {
736 $file_part = $path."/SQ".zfill(1,6)."-".$config[2];
737 if (!is_file($file_part))
738 $file_part = $path."/SQ".zfill(1,3)."-".$config[2];
739 if (is_file($file_part)) {
740 $this->configs[$h]['added'] = filectime($file_part);
741 $file_part = $path."/SQ".zfill($config[5],6)."-".$config[2];
742 if (!is_file($file_part))
743 $file_part = $path."/SQ".zfill($config[5],3)."-".$config[2];
744 if (is_file($file_part))
745 $this->configs[$h]['lastchange'] = filemtime($file_part);
746 for ($i=1; $i<=$config[5]; $i++)
747 $this->configs[$h]['stats'][$i] = "1.00";
749 //failsafe voor verwijderde bestanden
750 mysql_query("UPDATE file_hash SET completed = 0 WHERE md5_hash = '".mysql_escape_string($h)."'");
753 $keep_validating = true;
754 for ($i=1; $i<=$config[5]; $i++) {
755 if ($validated_chunks > $i) {
756 $this->configs[$h]['stats'][$i]="1.00";
758 $file_part = $path."/SQ".zfill($i,6)."-".$config[2];
759 if (!is_file($file_part))
760 $file_part = $path."/SQ".zfill($i,3)."-".$config[2];
761 if (is_file($file_part)) {
762 $handle = fopen($file_part, "rb");
763 $size_this = filesize($file_part);
764 $added = filectime($file_part);
765 $last_changed = filemtime($file_part);
766 if ($this->configs[$h]['added'] > $added || !is_numeric($this->configs[$h]['added']))
767 $this->configs[$h]['added'] = $added;
768 if ($this->configs[$h]['lastchange'] < $last_changed)
769 $this->configs[$h]['lastchange'] = $last_changed;
770 if ($i != $config[5]) {
771 $this->configs[$h]['stats'][$i] = number_format((1/$config[4])*$size_this, 2, '.', '');
772 //number_format((100/$config[4])*$size_this, 2, '.', '')."%";
774 $this->configs[$h]['stats'][$i] = number_format((1/($config[3]-($config[4]*($config[5]-1))))*$size_this, 2, '.', '');
775 //number_format((100/($config[3]-($config[4]*($config[5]-1))))*$size_this, 2, '.', '')."%";
778 if ($config[4] == $size_this && $keep_validating) {
779 $validated_chunks = $i;
781 $keep_validating = false;
784 $this->configs[$h]['stats'][$i] = "0.00";
785 //$this->configs[$h]['stats'][$i]="0.00%";
790 mysql_query("UPDATE file_hash SET validated_chunks = '".mysql_escape_string($validated_chunks)."' WHERE md5_hash = '".mysql_escape_string($h)."'");
794 function read_config($path, $filename) {
795 if (is_file($path."/".$filename.".InProgress")) {
796 $config_handle = fopen($path."/".$filename.".InProgress", "r");
797 $conf_path=$path."/".$filename.".InProgress";
798 } elseif (is_file($path."/".$filename.".Completed" )) {
799 $config_handle = fopen($path."/".$filename.".Completed", "r");
800 $conf_path=$path."/".$filename.".Completed";
801 } elseif (is_file($path."/".$filename.".Starting" )) {
802 $config_handle = fopen($path."/".$filename.".Starting", "r");
803 $conf_path=$path."/".$filename.".Starting";
804 } elseif (is_file($path."/".$filename.".Processed" )) {
805 $config_handle = fopen($path."/".$filename.".Processed", "r");
806 $conf_path=$path."/".$filename.".Processed";
810 $config_content = fread($config_handle, filesize($conf_path));
811 fclose($config_handle);
812 $config = explode("\n", $config_content);
816 /* $file_count = $config[5];
818 for ($i=1; $i<=$file_count; $i++)
820 $file_part = $path."/SQ".zfill($i,6)."-".$filename;
821 if (is_file($file_part))
824 $handle = fopen($file_part, "rb");
825 $size_this = filesize($file_part);
826 //$stats[$i]=round((100/$config[4])*$size_this)."%";
827 if ((($size_this==$config[4] && ($last+1)==$i) )|| $i == $config[5]) {
828 $merged_file.=fread($handle, filesize($file_part));
838 function print_files($path, $filename, $tovar=false) {
839 if (strpos($path, './uploads/ftp')) {
840 $filestring = $path.'/'.$filename;
841 if (is_file($filestring)) {
842 $handle = fopen($filestring, "rb");
843 while (!feof($handle))
845 print(fread($handle, 1024));
851 if (is_file($path."/".$filename.".InProgress")) {
852 $config_handle = fopen($path."/".$filename.".InProgress", "r");
853 $conf_path=$path."/".$filename.".InProgress";
854 } elseif (is_file($path."/".$filename.".Completed" )) {
855 $config_handle = fopen($path."/".$filename.".Completed", "r");
856 $conf_path=$path."/".$filename.".Completed";
857 } elseif (is_file($path."/".$filename.".Starting" )) {
858 $config_handle = fopen($path."/".$filename.".Starting", "r");
859 $conf_path=$path."/".$filename.".Starting";
860 } elseif (is_file($path."/".$filename.".Processed" )) {
861 $config_handle = fopen($path."/".$filename.".Processed", "r");
862 $conf_path=$path."/".$filename.".Processed";
866 $config_content = fread($config_handle, filesize($conf_path));
867 fclose($config_handle);
868 $config = explode("\n",$config_content);
870 $file_count = $config[5];
872 $last_part_size = ( $config[3] - ( ( $config[5] -1 ) * $config[4] ) );
873 for ($i=0;$i<=$file_count;$i++)
875 $file_part = $path."/SQ".zfill($i,6)."-".$filename;
876 if (!is_file($file_part))
877 $file_part = $path."/SQ".zfill($i,3)."-".$filename;
878 if (is_file($file_part))
880 $handle = fopen($file_part, "rb");
881 $size_this = filesize($file_part);
882 if ( ( ( $size_this==$config[4] ) && ( ($last+1)==$i ) ) || ( ( $i == $config[5] ) && ( $size_this==$last_part_size ) && ( ($last+1)==$i ) ) ) {
884 $merged_file.=fread($handle, $size_this);
887 while (!feof($handle))
889 print(fread($handle, 4096));
904 function check_md5($h) {
906 $config = $this->configs[$h];
907 $var = $this->print_files($config['path'], $config[2], true);
909 if ($hash==$config[6])
915 function file_crc($file_string) {
916 //$file_string = file_get_contents($file);
918 $crc = crc32($file_string);
919 return sprintf("%u\n", $crc);
922 function file_crc_debug($file) {
923 $file_string = file_get_contents($file);
925 $crc = crc32($file_string);
926 return sprintf("%u\n", $crc);
929 function delete_file($h, $s) {
930 $request = $this->get_config($h);
931 $filepath=$request['path'].'/'.$request[2];
932 if (strpos($request['path'], './uploads/ftp')) {
934 if (is_file($filepath))
936 if (is_file($filepath.'.hidden'))
937 @unlink($filepath.'.hidden');
940 for ($i=0;$i<=$request[5];$i++) {
941 $part_six = $request['path']."/SQ".zfill($i,6)."-".$request[2];
942 $part_three = $request['path']."/SQ".zfill($i,3)."-".$request[2];
943 if (is_file($part_six))
945 if (is_file($part_three))
949 if (is_file($request['path'].'/'.$request[2].'.hidden')) @unlink($request['path'].'/'.$request[2].'.hidden');
950 if (is_file($request['path'].'/'.$request[2].'.Completed')) @unlink($request['path'].'/'.$request[2].'.Completed');
951 if (is_file($request['path'].'/'.$request[2].'.InProgress'))@unlink($request['path'].'/'.$request[2].'.InProgress');
952 if (is_file($request['path'].'/'.$request[2].'.Processed')) @unlink($request['path'].'/'.$request[2].'.Processed');
953 if (is_file($request['path'].'/'.$request[2].'.Starting')) @unlink($request['path'].'/'.$request[2].'.Starting');
957 $q = "DELETE FROM file_hash WHERE file_hash = '".mysql_escape_string($h)."'";
959 $q = "INSERT INTO log (hash,file,action,user_id,user_name,ip,date) VALUES ('".mysql_escape_string($h)."','".mysql_escape_string($filepath)."','delete',".(int)$s['user_id'].",'".mysql_escape_string($s['user_name'])."','".mysql_escape_string($_SERVER['REMOTE_ADDR'])."',NOW())";
963 $m_name = $s['user_name'];
964 $m_subject = "Squasher Debug: File Deleted by {$m_name}";
965 $m_body = "File Deleted: \n Requested by: {$m_name} \n File: {$filepath}";
966 mail('jasper@netformatie.nl', $m_subject, $m_body, "From: support@netformatie.nl");
969 /* unused debug function (echo / print_r)
970 function show_files() {
972 $path = "./uploads/";
974 if ($dir = opendir($path)) {
978 $files_merged = array();
979 while (false !== ($file = readdir($dir)))
981 if (($file !== ".") && ($file !== ".."))
983 $filename = $path.$file;
984 $handle = fopen($filename, "rb");
985 $size_this = filesize($filename);
987 $size_first = $size_this;
988 $filecontent = fread($handle, filesize($filename));
989 $files[$i++] = $filename;
990 $files_merged[$file_base][]=$filename;
997 print_r($files_merged);
1002 function set_mime($filename) {
1003 $ext_arr = explode('.', $filename);
1004 $ext = strtolower(array_pop($ext_arr));
1007 $mime = 'video/avi';
1011 $mime = 'video/mpeg'; //MPEG Video
1017 $mime = 'application/octet-stream';
1020 $mime = 'image/gif'; //GIF Image
1024 $mime = 'image/jpeg'; //JPEG Image
1027 $mime = 'image/png'; //PNG Image
1031 $mime = 'audio/wav'; //WAV Audio
1034 $mime = 'audio/mpeg'; //MP3 Audio
1037 $mime = 'video/mov'; //Quicktime Video
1040 $mime = 'video/x-ms-wmv'; //Windows WMV video
1043 $mime = 'audio/x-ms-wma'; //Windows WMA audio
1046 $mime = 'audio/x-realaudio'; //RealPlayer Audio/Video (.rm)
1049 $mime = 'audio/x-pn-realaudio'; //RealPlayer Audio/Video (.ram)
1052 $mime = 'application/pdf'; //PDF Document
1055 $mime = 'application/msword'; //MS Word .doc file
1058 $mime = 'application/zip'; //Zip File
1061 $mime = 'application/octet-stream';
1064 //$return['mime']=$mime;
1065 //$return['ext']=$ext;
1071 // vim: syntax=php ts=4 sw=4 sts=4 sr noet