Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
freedom_import_tar.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 /**
7  * Import document descriptions
8  *
9  * @author Anakeen
10  * @version $Id: freedom_import_tar.php,v 1.3 2006/01/16 16:11:39 eric Exp $
11  * @package FDL
12  * @subpackage GED
13  */
14 /**
15  */
16 
17 include_once ("FDL/import_tar.php");
18 
20 {
21 
22  global $_FILES;
23 
24  $dirid = GetHttpVars("dirid"); // directory to place imported doc
25  $famid = GetHttpVars("famid"); // default import family
26  $onlycsv = (GetHttpVars("onlycsv") != ""); // only files described in fdl.csv files
27  $analyze = (GetHttpVars("analyze", "N") == "Y"); // just analyze
28  $uploaddir = getTarUploadDir($action);
29 
30  $err = '';
31  $fname = '';
32  $report = '';
33  $extract = '';
34  if ($_FILES['tar']['error'] != UPLOAD_ERR_OK) {
35  switch ($_FILES['tar']['error']) {
36  case UPLOAD_ERR_INI_SIZE:
37  $err = sprintf("The uploaded file exceeds the upload_max_filesize [%s bytes] directive in php.ini", ini_get('upload_max_filesize'));
38  break;
39 
40  case UPLOAD_ERR_FORM_SIZE:
41  $err = "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.";
42  break;
43 
44  case UPLOAD_ERR_PARTIAL:
45  $err = "The uploaded file was only partially uploaded.";
46  break;
47 
48  case UPLOAD_ERR_NO_FILE:
49  $err = "No file was uploaded.";
50  break;
51  }
52  if ($err != "") $action->exitError($err);
53  } else {
54 
55  system("mkdir -p " . escapeshellarg($uploaddir));
56  $fname = get_magic_quotes_gpc() ? stripslashes($_FILES['tar']['name']) : $_FILES['tar']['name'];
57  $uploadfile = $uploaddir . $fname;
58  if (move_uploaded_file($_FILES['tar']['tmp_name'], "$uploadfile")) {
59  $report = sprintf(_("File %s is valid, and was successfully uploaded.") , $fname);
60 
61  $untardir = getTarExtractDir($action, $fname);
62 
63  $status = extractTar($uploadfile, $untardir, $_FILES['tar']['type']);
64  if ($status === '') {
65  $extract = sprintf(_("The file %s has been correctly extracted") , $fname);
66  } else {
67  $extract = sprintf(_("The file %s cannot be extracted: %s") , $fname, $status);
68  }
69  } else {
70  $report = _("Possible file upload attack! Here's some debugging info:\n");
71  print_r2($_FILES);
72  }
73  }
74 
75  $action->lay->eset("filename", $fname);
76  $action->lay->eset("report", $report);
77  $action->lay->eset("extract", $extract);
78  $action->lay->eset("dirid", $dirid);
79 }
80 
81 function extractTar($tar, $untardir, $mime = "")
82 {
83  $tar = realpath($tar);
84  $mime = trim(shell_exec(sprintf("file -b %s", escapeshellarg($tar))));
85  $mime = substr($mime, 0, strpos($mime, " "));
86 
87  $err = '';
88  try {
89  switch ($mime) {
90  case "gzip":
91  case "application/x-compressed-tar":
92  case "application/x-gzip":
93  exec(sprintf("rm -rf %s 2>&1", escapeshellarg($untardir)) , $output, $status);
94  if ($status !== 0) {
95  throw new Exception(sprintf(_("Error deleting directory '%s': %s") , $untardir, join("\n", $output)));
96  }
97  exec(sprintf("mkdir -p %s 2>&1", escapeshellarg($untardir)) , $output, $status);
98  if ($status !== 0) {
99  throw new Exception(sprintf(_("Error creating directory '%s': %s") , $untardir, join("\n", $output)));
100  }
101  exec(sprintf("tar -C %s -zxf %s 2>&1", escapeshellarg($untardir) , escapeshellarg($tar)) , $output, $status);
102  if ($status !== 0) {
103  throw new Exception(sprintf(_("Error extracting archive '%s' in '%s': %s") , $tar, $untardir, join("\n", $output)));
104  }
105  break;
106 
107  case "bzip2":
108  exec(sprintf("rm -rf %s 2>&1", escapeshellarg($untardir)) , $output, $status);
109  if ($status !== 0) {
110  throw new Exception(sprintf(_("Error deleting directory '%s': %s") , $untardir, join("\n", $output)));
111  }
112  exec(sprintf("mkdir -p %s 2>&1", escapeshellarg($untardir)) , $output, $status);
113  if ($status !== 0) {
114  throw new Exception(sprintf(_("Error creating directory '%s': %s") , $untardir, join("\n", $output)));
115  }
116  exec(sprintf("tar -C %s -jxf %s 2>&1", escapeshellarg($untardir) , escapeshellarg($tar)) , $output, $status);
117  if ($status !== 0) {
118  throw new Exception(sprintf(_("Error extracting archive '%s' in '%s': %s") , $tar, $untardir, join("\n", $output)));
119  }
120  break;
121 
122  case "Zip":
123  case "application/x-zip-compressed":
124  case "application/x-zip":
125  exec(sprintf("rm -rf %s 2>&1", escapeshellarg($untardir)) , $output, $status);
126  if ($status !== 0) {
127  throw new Exception(sprintf(_("Error deleting directory '%s': %s") , $untardir, join("\n", $output)));
128  }
129  exec(sprintf("mkdir -p %s 2>&1", escapeshellarg($untardir)) , $output, $status);
130  if ($status !== 0) {
131  throw new Exception(sprintf(_("Error creating directory '%s': %s") , $untardir, join("\n", $output)));
132  }
133  exec(sprintf("unzip -d %s %s 2>&1", escapeshellarg($untardir) , escapeshellarg($tar)) , $output, $status);
134  if ($status !== 0) {
135  throw new Exception(sprintf(_("Error extracting archive '%s' in '%s': %s") , $tar, $untardir, join("\n", $output)));
136  }
137  break;
138 
139  default:
140  throw new Exception(sprintf(_("Unsupported archive format '%s' for archive '%s'.") , $mime, $tar));
141  }
142  }
143  catch(Exception $e) {
144  $err = $e->getMessage();
145  }
146  return $err;
147 }
$status
Definition: index.php:30
global $action
getTarExtractDir(Action &$action, $tar)
Definition: import_tar.php:34
freedom_import_tar(Action &$action)
exitError($texterr, $exit=true, $code="")
getTarUploadDir(Action &$action)
Definition: import_tar.php:24
print_r2($z, $ret=false)
Definition: Lib.Common.php:65
extractTar($tar, $untardir, $mime="")
if($file) if($subject==""&&$file) if($subject=="") $err
$analyze
← centre documentaire © anakeen