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);
146 while($a = mysql_fetch_array($r)) {
147 $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";
148 $ru = mysql_query($qu);
149 $a['users_from_ip'] = " | ";
150 while($au = mysql_fetch_array($ru))
151 $a['users_from_ip'] .= $au['user_name']." | ";
152 $return['today'][$a['log_id']] = $a;
154 $r = mysql_query($q.$yesterday.$order);
155 while($a = mysql_fetch_array($r)) {
156 $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";
157 $ru = mysql_query($qu);
158 $a['users_from_ip'] = " | ";
159 while($au = mysql_fetch_array($ru))
160 $a['users_from_ip'] .= $au['user_name']." | ";
161 $return['yesterday'][$a['log_id']] = $a;
163 $r = mysql_query($q.$lastweek.$order);
164 while($a = mysql_fetch_array($r)) {
165 $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";
166 $ru = mysql_query($qu);
167 $a['users_from_ip'] = " | ";
168 while($au = mysql_fetch_array($ru))
169 $a['users_from_ip'] .= $au['user_name']." | ";
170 $return['lastweek'][$a['log_id']] = $a;
172 $r = mysql_query($q.$older.$order);
173 while($a = mysql_fetch_array($r)) {
174 $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";
175 $ru = mysql_query($qu);
176 $a['users_from_ip'] = " | ";
177 while($au = mysql_fetch_array($ru))
178 $a['users_from_ip'] .= $au['user_name']." | ";
179 $return['older'][$a['log_id']] = $a;
185 function insert_users($u, $admin_level) {
186 $user_name = $u['user_name'];
187 $user_pass = $u['user_pass'];
188 $user_level = (int)$u['user_level'];
190 $q = "INSERT INTO users (user_name,user_pass,user_level) VALUES ('".mysql_escape_string($user_name)."', '".mysql_escape_string(md5($user_pass))."', ".$user_level.")";
191 $r = mysql_query($q);
194 function update_users($u) {
195 $user_id = (int)$u['user_id'];
196 $user_name = @$u['user_name'];
197 $user_pass = @$u['user_pass'];
198 $user_level = (int)@$u['user_level'];
200 $q = "UPDATE users SET user_name = '".mysql_escape_string($user_name)."' WHERE user_id = ".$user_id;
201 $r = mysql_query($q);
204 $q = "UPDATE users SET user_pass = '".mysql_escape_string(md5($user_pass))."' WHERE user_id = ".$user_id;
205 $r = mysql_query($q);
208 $q = "UPDATE users SET user_level = ".$user_level." WHERE user_id = ".$user_id;
209 $r = mysql_query($q);
213 function disable_users($u) {
214 $user_id = (int)$u['user_id'];
215 $user_name = @$u['user_name'];
216 $user_level = (int)@$u['user_level'];
218 $q = "UPDATE users SET user_name = '".mysql_escape_string($user_name)."' WHERE user_id = ".$user_id;
219 $r = mysql_query($q);
221 $q = "UPDATE users SET user_pass = '' WHERE user_id = ".$user_id;
222 $r = mysql_query($q);
224 $q = "UPDATE users SET user_level = ".$user_level." WHERE user_id = ".$user_id;
225 $r = mysql_query($q);
227 $r = mysql_query($q);
230 function remove_users($u) {
231 $user_id = (int)$u['user_id'];
232 $q = "DELETE FROM users WHERE user_id = ".$user_id;
233 $r = mysql_query($q);
236 function get_rights($user_id) {
239 $q = "SELECT folder_path, access FROM user_rights WHERE user_id = ".(int)$user_id;
240 $r = mysql_query($q);
241 while ($o = mysql_fetch_object($r)) {
244 $arr_string = '$result';
246 //get foldernames from path
247 if ($o->folder_path != '/') {
248 $path = $o->folder_path;
250 $path=substr($path,1);
251 $folder_arr = explode('/',$path);
253 //create folder structure array
254 foreach ($folder_arr AS $key => $value) {
255 $value_escaped = str_replace("'", "\\'", $value);
256 $arr_string .= "['".$value_escaped."']";
259 $arr_string .= "['__access__']";
261 eval($arr_string." = '".$o->access."';");
266 function give_rights($user_id, $type='folderrights') {
267 if ($type=='folderrights')
268 $this->folderrights = $this->get_rights($user_id);
269 else // $type=='userrights'
270 $this->userrights = $this->get_rights($user_id);
273 function update_rights($edited_user, $m, $admin_level) {
274 $q = "SELECT count(*) result FROM users WHERE user_id = ".(int)$edited_user." AND user_level < ".(int)$admin_level;
275 $r = mysql_query($q);
276 $o = mysql_fetch_object($r);
279 foreach ($m AS $path => $access) {
280 $p_q = "SELECT count(*) result FROM user_rights WHERE user_id = ".(int)$edited_user." AND folder_path = '".mysql_escape_string($path)."'";
281 $p_r = mysql_query($p_q);
282 $p_o = mysql_fetch_object($p_r);
283 if ($p_o->result == 1)
284 mysql_query("UPDATE user_rights SET access = ".(int)$access." WHERE folder_path = '".mysql_escape_string($path)."' AND user_id = ".(int)$edited_user);
285 if ($p_o->result == 0)
286 mysql_query("INSERT INTO user_rights (user_id,folder_path,access) values (".(int)$edited_user.",'".mysql_escape_string($path)."',".(int)$access.") ");
292 function show_rights_tree($path, $depth=0, $userid=0) {
297 $this->give_rights($userid, 'userrights');
299 // access = 0 --deny-all
300 // access = 1 --allow-dir-only
301 // access = 2 --allow-inc-subs
306 for ($i=0; $i<$depth; $i++)
308 if ($dir = opendir($path)) {
309 $layout .= "<div style='clear:both;' >\n";
311 while (false !== ($file = readdir($dir))) {
312 $files_array[] = $file;
315 foreach ($files_array as $f_index => $file) {
316 if (($file{0} !== ".") && ($file !== ".."))
318 $filename = $path."/".$file;
319 if (!is_file($filename) && $this->got_rights_array($filename) > 0) {
321 if (substr($filename,0,strlen($this->basepath))==$this->basepath)
322 $name = substr($filename,strlen($this->basepath));
323 $check = $this->got_rights_array_admin($filename, $this->userrights);
329 $check_all = 'checked';
332 $check_allow = 'checked';
336 $check_deny = 'checked';
341 $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).";}";
343 $layout .= "<div class='depth".$depth."'>";
344 $layout .= "<div class='white_border' >".htmlspecialchars($file)."</div>\n";
345 $name_escaped = htmlspecialchars($name);
346 $layout .= '<div class="check_deny"><input name="m['.$name_escaped.']" value=0 type=radio '.$check_deny."></div>\n";
347 $layout .= '<div class="check_allow"><input name="m['.$name_escaped.']" value=1 type=radio '.$check_allow."></div>\n";
348 $layout .= '<div class="check_all"><input name="m['.$name_escaped.']" value=2 type=radio '.$check_all."></div>\n";
349 $sub_return = $this->show_rights_tree($filename,$depth+1,$userid);
350 if (is_array($style) && is_array($sub_return['style']))
351 $style = $style + $sub_return['style'];
352 $layout .= $sub_return['layout'];
358 $layout .= "</div>\n";
361 $return['style'] = $style;
362 $return['layout'] = $layout;
367 function got_rights_array($needle, $haystack='', $c=0) {
369 // - read_single_file
371 // - show_rights_tree
373 if (!is_array($haystack))
374 $haystack = $this->folderrights;
375 if (substr($needle, 0, strlen($this->basepath)) == $this->basepath)
376 $needle=substr($needle,strlen($this->basepath));
379 if ($needle{0} == '/' && @$haystack['__access__'] == 2 )
384 $needle = substr($needle, 1);
386 $needle_arr = explode('/', $needle);
387 $n = count($needle_arr);
390 foreach ($haystack as $k => $v) {
391 if ($needle_arr[$c] == $k) {
392 if (!is_array(@$v['__access__'])) {
393 if ($v['__access__'] == 2 ) return 2;
394 if ($v['__access__'] == 1 && $d == $n ) return 1;
395 if ($v['__access__'] == 0 && $d == $n ) return 0;
397 $return = $this->got_rights_array($needle, $v, $d);
404 function got_rights_array_admin($needle, $haystack='', $c=0) {
406 // - show_rights_tree
408 if (!is_array($haystack))
409 $haystack = $this->folderrights;
410 if (substr($needle, 0, strlen($this->basepath)) == $this->basepath)
411 $needle = substr($needle, strlen($this->basepath));
414 if ($needle{0} == '/' && @$haystack['__access__'] == 2 )
418 if ($needle{0} == '/')
419 $needle = substr($needle, 1);
421 $needle_arr = explode('/', $needle);
422 $n = count($needle_arr);
427 if (@$haystack['__access__'] == 2)
428 return $haystack['__access__'];
429 if (is_array($haystack[$needle_arr[$c]]))
430 $return = $this->got_rights_array_admin($needle, $haystack[$needle_arr[$c]], $d);
432 if (@$haystack['__access__'] > 0)
433 $return = $haystack['__access__'];
439 function got_rights_array_recursive($needle, $haystack='', $c=0) {
441 // - read_directory, for subfolders
443 if (!is_array($haystack))
444 $haystack = $this->folderrights;
445 if (substr($needle, 0, strlen($this->basepath)) == $this->basepath)
446 $needle=substr($needle, strlen($this->basepath));
449 if($needle{0}=='/' && @$haystack['__access__'] == 2 )
452 // check folder rights
454 $needle=substr($needle,1);
456 $needle_arr = explode('/', $needle);
457 $n = count($needle_arr);
461 foreach($haystack as $k => $v) {
462 if ($c < sizeof($needle_arr) && $needle_arr[$c] == $k) {
464 if ($v['__access__'] == 2)
465 $return = $return + $v['__access__'];
466 $return = $return + $this->got_rights_array_recursive($needle, $v, $d);
468 $return = $return + $this->in_array_recursive($v);
470 } elseif ($c == $n) {
471 $return = $return + $v['__access__'];
472 if($k != '__access__')$return = $return + $this->in_array_recursive($v);
479 function in_array_recursive($haystack) {
481 if (is_array($haystack)) {
482 foreach ($haystack as $key1 => $value1) {
483 if (is_array($value1)) {
484 $return = $return + $this->in_array_recursive($value1);
486 elseif ($value1 > 0) {
494 function read_single_file($path, $file) {
495 $filename = $path."/".$file;
497 if ($this->got_rights_array($path) > 0) {
498 if (is_file($filename.'.Completed'))
500 if (is_file($filename.'.InProgress'))
501 $file.='.InProgress';
502 if (is_file($filename.'.Starting'))
504 if (is_file($filename.'.Processed'))
506 $filename = $path . "/" . $file;
507 $handle = @fopen($filename, "rb");
508 if (strpos($file,'.Completed'))
510 if (strpos($file,'.InProgress'))
512 if (strpos($file,'.Starting'))
514 if (strpos($file,'.Processed'))
516 $sub_pos = strpos($file, $ext);
517 $base_name = substr($file, 0, $sub_pos);
518 $filecontent = @fread($handle, @filesize($filename));
519 $config[$i] = explode("\r\n", $filecontent);
528 * [6] -> CRC32 checksum
530 if (@filesize($filename) > 0) {
531 $h = md5($path."/".$config[$i][2]);
532 $this->configs[$h] = $config[$i];
533 $this->configs[$h]['squashed'] = true;
534 $this->configs[$h]['path'] = $path;
535 $this->configs[$h]['status'] = substr($ext, 1);
536 $this->configs[$h]['mime'] = $this->set_mime($this->configs[$h][2]);
537 $this->configs[$h]['hidden'] = (is_file($path.'/'.$base_name.'.hidden')) ? true : false ;
538 //to prevent dates of 1-1-1970 we set te dates of the config file
539 $this->configs[$h]['added'] = filectime($filename);
540 $this->configs[$h]['lastchange'] = filemtime($filename);
542 $this->populate_stats($path, $h);
544 #$this->update_hash($h,$path."/".$config[$i][2]);
546 $this->check_stats($h);
552 function read_directory($path, $getsubs=false, $getfirstfiles=true, $getdeepfiles=true, $populate=true) {
554 if ($dir = @opendir($path)) {
557 while (false !== ($file = readdir($dir))) {
558 if (($file{0} !== ".") && substr($file,0,1) !== "SQ") {
559 $filename = $path."/".$file;
560 if (!is_file($filename) && strpos($filename, './uploads/recieving')===false) {
562 if ($this->got_rights_array_recursive($filename) > 0) {
563 $key = substr($path, strlen($this->basepath)) . '/' . $file;
564 $this->subfolders[$key] = $file;
568 $this->read_directory($filename, false, $getdeepfiles, $getdeepfiles, $populate);
569 } elseif (strpos($filename,'./uploads/ftp')) { //ftp files
570 if ($this->got_rights_array($path) > 0 && !strpos($filename, '.hidden') ) {
572 $name_only = substr($filename, strlen($path)+1);
573 $file_structure = explode('.', $name_only);
574 $ext = array_pop($file_structure);
575 $base_name = array_pop($file_structure);
576 $this->configs[$h]['path'] = $path;
577 $this->configs[$h][0] = 'manual ftp';
578 $this->configs[$h][2] = $name_only;
579 $this->configs[$h][3] = filesize($filename);
580 $this->configs[$h]['added'] = filectime($filename);
581 $this->configs[$h]['lastchange'] = filemtime($filename);
582 $this->configs[$h]['status'] = 'unknown';
583 $this->configs[$h]['squashed'] = false;
584 $this->configs[$h]['mime'] = $this->set_mime($name_only);
585 $this->configs[$h]['hidden'] = (is_file($path.'/'.$base_name.'.hidden')) ? true : false ;
587 } elseif ($getfirstfiles) {
589 if ($this->got_rights_array($path) > 0) {
590 if (strpos($filename, '.Completed') || strpos($filename, '.InProgress') || strpos($filename, '.Starting') || strpos($filename, '.Processed')) {
592 $handle = @fopen($filename, "rb");
593 if (strpos($file,'.Completed'))
595 if (strpos($file,'.InProgress'))
597 if (strpos($file,'.Starting'))
599 if (strpos($file,'.Processed'))
601 $sub_pos = strpos($file, $ext);
602 $base_name = substr($file, 0, $sub_pos);
603 $filecontent = @fread($handle, @filesize($filename));
604 $config[$i] = explode("\r\n", $filecontent);
614 * [6] -> CRC32 checksum
617 if (@filesize($filename) > 0) {
618 $h = md5($path."/".$config[$i][2]);
619 $this->configs[$h] = $config[$i];
620 $this->configs[$h]['squashed'] = true;
621 $this->configs[$h]['path'] = $path;
622 $this->configs[$h]['status'] = substr($ext, 1);
623 $this->configs[$h]['mime'] = $this->set_mime($this->configs[$h][2]);
624 $this->configs[$h]['hidden'] = (is_file($path.'/'.$base_name.'.hidden')) ? true : false ;
625 //to prevent dates of 1-1-1970 we set te dates of the config file
626 $this->configs[$h]['added'] = filectime($filename);
627 $this->configs[$h]['lastchange'] = filemtime($filename);
629 $this->populate_stats($path, $h);
631 $this->update_hash($h, $path."/".$config[$i][2]);
633 $this->check_stats($h);
643 function check_stats($h) {
644 $config = $this->get_config($h);
645 $count = @array_sum($config['stats']);
646 if ($config['status']=='Completed' && $count != $config[5]) {
647 $filepath=$config['path'].'/'.$config[2];
648 if ($this->history[$h]['completed']=="1") {
649 //don't display broken file, remove it instead
650 unlink($config['path'].'/'.$config[2].'.Completed');
651 unset($this->configs[$h]);
655 $m_subject = "Squasher Debug: File Removed";
656 $m_body = "Upload removed: \n File: ".$config['path']."/".$config[2]." \n Status: ".$config['status']." \n Chunks: ".$count." out of ".$config[5];
657 mail('jasper@netformatie.nl', $m_subject, $m_body, "From: support@netformatie.nl");
658 $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())";
662 rename($config['path'].'/'.$config[2].'.Completed', $config['path'].'/'.$config[2].'.InProgress');
663 $this->configs[$h]['status'] = 'InProgress';
668 $m_subject = "Squasher Debug: Upload Error";
669 $m_body = "Upload error: \n File: ".$config['path']."/".$config[2]." \n Status: ".$config['status']." \n Chunks: ".$count." out of ".$config[5];
670 mail('support@netformatie.nl', $m_subject, $m_body, "From: squasher@netformatie.nl");
671 mail('jan@netformatie.nl', $m_subject, $m_body, "From: support@netformatie.nl");
672 mail('joop@netformatie.nl', $m_subject, $m_body, "From: support@netformatie.nl");
673 mail('jasper@netformatie.nl', $m_subject, $m_body, "From: support@netformatie.nl");
676 //wget -o/dev/null "http://www.mollie.nl/xml/sms/?username=netformatie&password=SMSdolsi&originator=Netformatie&recipients=${ENGINEER}&message=${CALLERID}";
679 $ship = explode('/',$config['path']);
680 if ($ship[2] == 'myas' || $ship[2] == 'myez' || $ship[2] == 'myrw')
681 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");
683 $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())";
687 } elseif ($config['status']=='Completed' && $count == $config[5]) {
688 if ($this->history[$h]['completed']==0)
689 mysql_query("UPDATE file_hash SET completed = 1 WHERE md5_hash = '".mysql_escape_string($h)."'");
693 function update_hash($hash, $path) {
694 $check_hash_query = "select * from file_hash where md5_hash = '".$hash."'";
695 $check_hash_result = mysql_query($check_hash_query);
696 if(mysql_num_rows($check_hash_result) == 0) {
697 $insert_hash_query = "INSERT INTO file_hash (md5_hash,file) values ('".mysql_escape_string($hash)."','".mysql_escape_string($path)."')";
698 mysql_query($insert_hash_query);
702 function path_to_arraystring($path, $arrayname) {
703 $path_values = explode('/', $path);
704 $return = $arrayname;
705 foreach ($path_values AS $key => $value) {
706 if ($value != '.' && $value != '')
707 $return.= "['".$value."']";
712 function populate_stats($path, $h) {
721 * [6] -> CRC32 checksum
723 $config = $this->configs[$h];
725 $q = "SELECT * FROM file_hash WHERE md5_hash = '".mysql_escape_string($h)."'";
726 $r = mysql_query($q);
727 $o = mysql_fetch_object($r);
728 $validated_chunks = $o->validated_chunks;
730 if ($this->history[$h]['completed']=="1") {
731 $file_part = $path."/SQ".zfill(1,6)."-".$config[2];
732 if (!is_file($file_part))
733 $file_part = $path."/SQ".zfill(1,3)."-".$config[2];
734 if (is_file($file_part)) {
735 $this->configs[$h]['added'] = filectime($file_part);
736 $file_part = $path."/SQ".zfill($config[5],6)."-".$config[2];
737 if (!is_file($file_part))
738 $file_part = $path."/SQ".zfill($config[5],3)."-".$config[2];
739 if (is_file($file_part))
740 $this->configs[$h]['lastchange'] = filemtime($file_part);
741 for ($i=1; $i<=$config[5]; $i++)
742 $this->configs[$h]['stats'][$i] = "1.00";
744 //failsafe voor verwijderde bestanden
745 mysql_query("UPDATE file_hash SET completed = 0 WHERE md5_hash = '".mysql_escape_string($h)."'");
748 $keep_validating = true;
749 for ($i=1; $i<=$config[5]; $i++) {
750 if ($validated_chunks > $i) {
751 $this->configs[$h]['stats'][$i]="1.00";
753 $file_part = $path."/SQ".zfill($i,6)."-".$config[2];
754 if (!is_file($file_part))
755 $file_part = $path."/SQ".zfill($i,3)."-".$config[2];
756 if (is_file($file_part)) {
757 $handle = fopen($file_part, "rb");
758 $size_this = filesize($file_part);
759 $added = filectime($file_part);
760 $last_changed = filemtime($file_part);
761 if ($this->configs[$h]['added'] > $added || !is_numeric($this->configs[$h]['added']))
762 $this->configs[$h]['added'] = $added;
763 if ($this->configs[$h]['lastchange'] < $last_changed)
764 $this->configs[$h]['lastchange'] = $last_changed;
765 if ($i != $config[5]) {
766 $this->configs[$h]['stats'][$i] = number_format((1/$config[4])*$size_this, 2, '.', '');
767 //number_format((100/$config[4])*$size_this, 2, '.', '')."%";
769 $this->configs[$h]['stats'][$i] = number_format((1/($config[3]-($config[4]*($config[5]-1))))*$size_this, 2, '.', '');
770 //number_format((100/($config[3]-($config[4]*($config[5]-1))))*$size_this, 2, '.', '')."%";
773 if ($config[4] == $size_this && $keep_validating) {
774 $validated_chunks = $i;
776 $keep_validating = false;
779 $this->configs[$h]['stats'][$i] = "0.00";
780 //$this->configs[$h]['stats'][$i]="0.00%";
785 mysql_query("UPDATE file_hash SET validated_chunks = '".mysql_escape_string($validated_chunks)."' WHERE md5_hash = '".mysql_escape_string($h)."'");
789 function read_config($path, $filename) {
790 if (is_file($path."/".$filename.".InProgress")) {
791 $config_handle = fopen($path."/".$filename.".InProgress", "r");
792 $conf_path=$path."/".$filename.".InProgress";
793 } elseif (is_file($path."/".$filename.".Completed" )) {
794 $config_handle = fopen($path."/".$filename.".Completed", "r");
795 $conf_path=$path."/".$filename.".Completed";
796 } elseif (is_file($path."/".$filename.".Starting" )) {
797 $config_handle = fopen($path."/".$filename.".Starting", "r");
798 $conf_path=$path."/".$filename.".Starting";
799 } elseif (is_file($path."/".$filename.".Processed" )) {
800 $config_handle = fopen($path."/".$filename.".Processed", "r");
801 $conf_path=$path."/".$filename.".Processed";
805 $config_content = fread($config_handle, filesize($conf_path));
806 fclose($config_handle);
807 $config = explode("\n", $config_content);
811 /* $file_count = $config[5];
813 for ($i=1; $i<=$file_count; $i++)
815 $file_part = $path."/SQ".zfill($i,6)."-".$filename;
816 if (is_file($file_part))
819 $handle = fopen($file_part, "rb");
820 $size_this = filesize($file_part);
821 //$stats[$i]=round((100/$config[4])*$size_this)."%";
822 if ((($size_this==$config[4] && ($last+1)==$i) )|| $i == $config[5]) {
823 $merged_file.=fread($handle, filesize($file_part));
833 function print_files($path, $filename, $tovar=false) {
834 if (strpos($path, './uploads/ftp')) {
835 $filestring = $path.'/'.$filename;
836 if (is_file($filestring)) {
837 $handle = fopen($filestring, "rb");
838 while (!feof($handle))
840 print(fread($handle, 1024));
846 if (is_file($path."/".$filename.".InProgress")) {
847 $config_handle = fopen($path."/".$filename.".InProgress", "r");
848 $conf_path=$path."/".$filename.".InProgress";
849 } elseif (is_file($path."/".$filename.".Completed" )) {
850 $config_handle = fopen($path."/".$filename.".Completed", "r");
851 $conf_path=$path."/".$filename.".Completed";
852 } elseif (is_file($path."/".$filename.".Starting" )) {
853 $config_handle = fopen($path."/".$filename.".Starting", "r");
854 $conf_path=$path."/".$filename.".Starting";
855 } elseif (is_file($path."/".$filename.".Processed" )) {
856 $config_handle = fopen($path."/".$filename.".Processed", "r");
857 $conf_path=$path."/".$filename.".Processed";
861 $config_content = fread($config_handle, filesize($conf_path));
862 fclose($config_handle);
863 $config = explode("\n",$config_content);
865 $file_count = $config[5];
867 $last_part_size = ( $config[3] - ( ( $config[5] -1 ) * $config[4] ) );
868 for ($i=0;$i<=$file_count;$i++)
870 $file_part = $path."/SQ".zfill($i,6)."-".$filename;
871 if (!is_file($file_part))
872 $file_part = $path."/SQ".zfill($i,3)."-".$filename;
873 if (is_file($file_part))
875 $handle = fopen($file_part, "rb");
876 $size_this = filesize($file_part);
877 if ( ( ( $size_this==$config[4] ) && ( ($last+1)==$i ) ) || ( ( $i == $config[5] ) && ( $size_this==$last_part_size ) && ( ($last+1)==$i ) ) ) {
879 $merged_file.=fread($handle, $size_this);
882 while (!feof($handle))
884 print(fread($handle, 4096));
899 function check_md5($h) {
901 $config = $this->configs[$h];
902 $var = $this->print_files($config['path'], $config[2], true);
904 if ($hash==$config[6])
910 function file_crc($file_string) {
911 //$file_string = file_get_contents($file);
913 $crc = crc32($file_string);
914 return sprintf("%u\n", $crc);
917 function file_crc_debug($file) {
918 $file_string = file_get_contents($file);
920 $crc = crc32($file_string);
921 return sprintf("%u\n", $crc);
924 function delete_file($h, $s) {
925 $request = $this->get_config($h);
926 $filepath=$request['path'].'/'.$request[2];
927 if (strpos($request['path'], './uploads/ftp')) {
929 if (is_file($filepath))
931 if (is_file($filepath.'.hidden'))
932 @unlink($filepath.'.hidden');
935 for ($i=0;$i<=$request[5];$i++) {
936 $part_six = $request['path']."/SQ".zfill($i,6)."-".$request[2];
937 $part_three = $request['path']."/SQ".zfill($i,3)."-".$request[2];
938 if (is_file($part_six))
940 if (is_file($part_three))
944 if (is_file($request['path'].'/'.$request[2].'.hidden')) @unlink($request['path'].'/'.$request[2].'.hidden');
945 if (is_file($request['path'].'/'.$request[2].'.Completed')) @unlink($request['path'].'/'.$request[2].'.Completed');
946 if (is_file($request['path'].'/'.$request[2].'.InProgress'))@unlink($request['path'].'/'.$request[2].'.InProgress');
947 if (is_file($request['path'].'/'.$request[2].'.Processed')) @unlink($request['path'].'/'.$request[2].'.Processed');
948 if (is_file($request['path'].'/'.$request[2].'.Starting')) @unlink($request['path'].'/'.$request[2].'.Starting');
952 $q = "DELETE FROM file_hash WHERE file_hash = '".mysql_escape_string($h)."'";
954 $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())";
958 $m_name = $s['user_name'];
959 $m_subject = "Squasher Debug: File Deleted by {$m_name}";
960 $m_body = "File Deleted: \n Requested by: {$m_name} \n File: {$filepath}";
961 mail('jasper@netformatie.nl', $m_subject, $m_body, "From: support@netformatie.nl");
964 function show_files() {
966 $path = "./uploads/";
968 if ($dir = opendir($path)) {
972 $files_merged = array();
973 while (false !== ($file = readdir($dir)))
975 if (($file !== ".") && ($file !== ".."))
977 $filename = $path.$file;
978 $handle = fopen($filename, "rb");
979 $size_this = filesize($filename);
981 $size_first = $size_this;
982 $filecontent = fread($handle, filesize($filename));
983 $files[$i++] = $filename;
984 $files_merged[$file_base][]=$filename;
991 print_r($files_merged);
995 function set_mime($filename) {
996 $ext_arr = explode('.', $filename);
997 $ext = strtolower(array_pop($ext_arr));
1000 $mime = 'video/avi';
1004 $mime = 'video/mpeg'; //MPEG Video
1010 $mime = 'application/octet-stream';
1013 $mime = 'image/gif'; //GIF Image
1017 $mime = 'image/jpeg'; //JPEG Image
1020 $mime = 'image/png'; //PNG Image
1024 $mime = 'audio/wav'; //WAV Audio
1027 $mime = 'audio/mpeg'; //MP3 Audio
1030 $mime = 'video/mov'; //Quicktime Video
1033 $mime = 'video/x-ms-wmv'; //Windows WMV video
1036 $mime = 'audio/x-ms-wma'; //Windows WMA audio
1039 $mime = 'audio/x-realaudio'; //RealPlayer Audio/Video (.rm)
1042 $mime = 'audio/x-pn-realaudio'; //RealPlayer Audio/Video (.ram)
1045 $mime = 'application/pdf'; //PDF Document
1048 $mime = 'application/msword'; //MS Word .doc file
1051 $mime = 'application/zip'; //Zip File
1054 $mime = 'application/octet-stream';
1057 //$return['mime']=$mime;
1058 //$return['ext']=$ext;
1064 // vim: syntax=php ts=4 sw=4 sts=4 sr noet