summaryrefslogtreecommitdiff
path: root/upload_file.php
blob: acf8c894c574b0fd6c294f350deadf77549c49c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php include("config.php"); ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Upload result</title>
</head>
<body>
<p>
<?php
function size_readable($size, $max = null, $system = "si", $retstring = "%01.2f %s") {
  // Pick units
  $systems['si']['prefix'] = array('B', 'K', 'MB', 'GB', 'TB', 'PB');
  $systems['si']['size']   = 1000;
  $systems['bi']['prefix'] = array('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB');
  $systems['bi']['size']   = 1024;
  $sys = isset($systems[$system]) ? $systems[$system] : $systems['si'];

  // Max unit to display
  $depth = count($sys['prefix']) - 1;
  if ($max && false !== $d = array_search($max, $sys['prefix'])) {
    $depth = $d;
  }

  // Loop
  $i = 0;
  while ($size >= $sys['size'] && $i < $depth) {
    $size /= $sys['size'];
    $i++;
  }

  return sprintf($retstring, $size, $sys['prefix'][$i]);
}

// Respect HTTPS or not
if (isset($_SERVER['HTTPS'])) {
  $url = preg_replace("/^http:\/\//", "https://", $url);
} else {
  $url = preg_replace("/^https:\/\//", "http://", $url);
}

if ($_FILES["file"]["error"] > 0) {
  echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
} else {
  if (file_exists($_FILES["file"]["name"])) {
    echo "Error: " . $_FILES["file"]["name"] . " already exists. ";
  } else {
    move_uploaded_file($_FILES["file"]["tmp_name"], "./" . $_FILES["file"]["name"]);
    echo "Uploaded: " . $_FILES["file"]["name"] . "<br>";
    $fileurl = "$url" . rawurlencode($_FILES["file"]["name"]);
    echo "URL: <a href=\"$fileurl\">$fileurl</a><br>";
    echo "Size: " . size_readable($_FILES["file"]["size"], null, "bi");
    if (isset($posturl)) {
      send_upload_notification($fileurl, $posturl, $postchannel);
    }
  }
}

function send_upload_notification($fileurl, $posturl, $channel) {
  $data = array('url' => $fileurl, 'channel' => $channel);

  $options = array(
    'http' => array(
      'header'  => "Content-type: application/x-www-form-urlencoded\r\n" .
                   "X-Blaears-Event: blaupload\r\n",
      'method'  => 'POST',
      'content' => http_build_query($data),
    ),
  );
  $context  = stream_context_create($options);
  file_get_contents($posturl, false, $context);
}
?>
</p>
<p>
<a href="<?php echo $url; ?>">Back to index</a>
</p>
</body>
</html>