summaryrefslogtreecommitdiff
path: root/src/attachment_writer.php
blob: bd00b142f9af5fbe3cb5ebfe5566df47395b3a9c (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
<?php

if (!array_key_exists('password', $_POST)) {
  die('No password given');
}

if ($_POST['password'] != 'foo') {
  die('Incorrect password');
}

$attach_dir = '/var/www/forum/files';

foreach ($_FILES as $file) {
  # Check for errors
  switch ($file['error']) {
  case UPLOAD_ERR_OK:
    break;
  case UPLOAD_ERR_INI_SIZE:
    die('Error UPLOAD_ERR_INI_SIZE: ' . $file['name']);
  case UPLOAD_ERR_FORM_SIZE:
    die('Error UPLOAD_ERR_FORM_SIZE: ' . $file['name']);
  case UPLOAD_ERR_PARTIAL:
    die('Error UPLOAD_ERR_PARTIAL: ' . $file['name']);
  case UPLOAD_ERR_NO_FILE:
    die('Error UPLOAD_ERR_NO_FILE: ' . $file['name']);
  case UPLOAD_ERR_NO_TMP_DIR:
    die('Error UPLOAD_ERR_NO_TMP_DIR: ' . $file['name']);
  case UPLOAD_ERR_CANT_WRITE:
    die('Error UPLOAD_ERR_CANT_WRITE: ' . $file['name']);
  case UPLOAD_ERR_EXTENSION:
    die('Error UPLOAD_ERR_EXTENSION: ' . $file['name']);
  default:
    die('Unrecognized error code: ' . $file['error'] . ' ' $file['name']);
  }

  # Don't continue if the name isn't what phpBB expects
  if (preg_match('/^\d+_[0-9a-f]{32}$/', $file['name']) != 1) {
    die('Bad destination filename: ' . $file['name']);
  }

  $src = $file['tmp_name'];
  $dst = $attach_dir . '/' . $file['name'];

  # Move temp file to attachments dir
  if (!move_uploaded_file($src, $dst)) {
    die("Failed to move $src to $dst.");
  }
}

return 1;

?>