[ Index ]
 

Code source de Phorum 5.1.25

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/include/posting/ -> action_attachments.php (source)

   1  <?php
   2  
   3  ////////////////////////////////////////////////////////////////////////////////
   4  //                                                                            //
   5  //   Copyright (C) 2006  Phorum Development Team                              //
   6  //   http://www.phorum.org                                                    //
   7  //                                                                            //
   8  //   This program is free software. You can redistribute it and/or modify     //
   9  //   it under the terms of either the current Phorum License (viewable at     //
  10  //   phorum.org) or the Phorum License that was distributed with this file    //
  11  //                                                                            //
  12  //   This program is distributed in the hope that it will be useful,          //
  13  //   but WITHOUT ANY WARRANTY, without even the implied warranty of           //
  14  //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                     //
  15  //                                                                            //
  16  //   You should have received a copy of the Phorum License                    //
  17  //   along with this program.                                                 //
  18  ////////////////////////////////////////////////////////////////////////////////
  19  
  20  if(!defined("PHORUM")) return;
  21  
  22  if ($do_detach)
  23  {
  24      // Find the message to detach.
  25      foreach ($message["attachments"] as $id => $info)
  26      {
  27          if ($info["file_id"] == $do_detach && $info["keep"])
  28          {
  29              // Attachments which are not yet linked to a message
  30              // can be deleted immediately. Linked attachments should
  31              // be kept in the db, in case the users clicks "Cancel".
  32              if (! $info["linked"]) {
  33                  phorum_db_file_delete($info["file_id"]);
  34                  unset($message["attachments"][$id]);
  35              } else {
  36                  $message["attachments"][$id]["keep"] = false;
  37              }
  38  
  39              // Run the after_detach hook.
  40              list($message,$info) =
  41                  phorum_hook("after_detach", array($message,$info));
  42  
  43              $attach_count--;
  44  
  45              break;
  46          }
  47      }
  48  }
  49  
  50  // Attachment(s) uploaded.
  51  elseif ($do_attach && ! empty($_FILES))
  52  {
  53      // find the maximum allowed attachment size.
  54      require_once ('./include/upload_functions.php');
  55      $system_max_upload = phorum_get_system_max_upload();
  56      if($PHORUM["max_attachment_size"]==0) $PHORUM["max_attachment_size"]=$system_max_upload[0]/1024;
  57      $PHORUM["max_attachment_size"] = min($PHORUM["max_attachment_size"],$system_max_upload[0]/1024);
  58  
  59      // The editor template that I use only supports one upload
  60      // at a time. This code supports multiple uploads.
  61      $attached = 0;
  62      foreach ($_FILES as $file)
  63      {
  64          // Not too many attachments?
  65          if ($attach_count >= $PHORUM["max_attachments"]) break;
  66  
  67          // PHP 4.2.0 and later can set an error field for the file
  68          // upload, indicating a specific error. In Phorum 5.1, we only
  69          // have an error message for too large uploads. Other error
  70          // messages will get a generic file upload error.
  71          $file_too_large = false;
  72          if (isset($file["error"]) && $file["error"]) {
  73              if ($file["error"] == UPLOAD_ERR_INI_SIZE ||
  74                  $file["error"] == UPLOAD_ERR_FORM_SIZE) {
  75                  // File too large. Just pass it on to the 
  76                  // following code to handle the error message.
  77                  $file_too_large = true;
  78              } else {
  79                  // Make sure that a generic error will be shown.
  80                  $file["size"] = 0;
  81              }
  82          }
  83  
  84          // Isn't the attachment too large?
  85          if ($file_too_large || ($PHORUM["max_attachment_size"] > 0 && $file["size"] > $PHORUM["max_attachment_size"] * 1024)) {
  86              $PHORUM["DATA"]["ERROR"] = str_replace(
  87                  '%size%',
  88                  phorum_filesize($PHORUM["max_attachment_size"] * 1024),
  89                  $PHORUM["DATA"]["LANG"]["AttachFileSize"]
  90              );
  91              phorum_filesize($PHORUM["max_attachment_size"] * 1024);
  92              $error_flag = true;
  93              break;
  94          }
  95  
  96          // Some problems in uploading result in files which are
  97          // zero in size. We asume that people who upload zero byte
  98          // files will almost always have problems uploading.
  99          if ($file["size"] == 0) continue;
 100  
 101          // Check if the tempfile is an uploaded file?
 102          if (! is_uploaded_file($file["tmp_name"])) continue;
 103  
 104          // Isn't the total attachment size too large?
 105          if ($PHORUM["max_totalattachment_size"] > 0 &&
 106              ($file["size"] + $attach_totalsize) > $PHORUM["max_totalattachment_size"]*1024) {
 107              $PHORUM["DATA"]["ERROR"] = str_replace(
 108                  '%size%',
 109                  phorum_filesize($PHORUM["max_totalattachment_size"] * 1024),
 110                  $PHORUM["DATA"]["LANG"]["AttachTotalFileSize"]
 111              );
 112              $error_flag = true;
 113              break;
 114          }
 115  
 116          // Is the type of file acceptable?
 117          if(! empty($PHORUM["allow_attachment_types"]))
 118          {
 119              $ext=substr($file["name"], strrpos($file["name"], ".")+1);
 120              $allowed_exts=explode(";", $PHORUM["allow_attachment_types"]);
 121              if (! in_array(strtolower($ext), $allowed_exts)) {
 122                  $PHORUM["DATA"]["ERROR"] =
 123                      $PHORUM["DATA"]["LANG"]["AttachInvalidType"] . " ".
 124                      str_replace('%types%', str_replace(";", ", ", $PHORUM["allow_attachment_types"]), $PHORUM["DATA"]["LANG"]["AttachFileTypes"]);
 125                  $error_flag = true;
 126                  break;
 127              }
 128          }
 129  
 130          // Read in the file.
 131          $file["data"] = base64_encode(file_get_contents($file["tmp_name"]));
 132  
 133          // copy the current user_id to the $file array for the hook
 134          $file["user_id"]=$PHORUM["user"]["user_id"];
 135  
 136          // Run the before_attach hook.
 137          list($message, $file) =
 138              phorum_hook("before_attach", array($message, $file));
 139  
 140          // Add the file to the database. We add it using message_id
 141          // 0 (zero). Only when the message gets saved definitely,
 142          // the message_id will be updated to link the file to the
 143          // forum message. This is mainly done so we can support
 144          // attachments for new messages, which do not yet have
 145          // a message_id assigned.
 146          $file_id = phorum_db_file_save(
 147              $PHORUM["user"]["user_id"],
 148              $file["name"], $file["size"],
 149              $file["data"], 0, PHORUM_LINK_EDITOR
 150          );
 151  
 152          // Create new attachment information.
 153          $new_attachment = array(
 154              "file_id" => $file_id,
 155              "name"    => $file["name"],
 156              "size"    => $file["size"],
 157              "keep"    => true,
 158              "linked"  => false,
 159          );
 160  
 161          // Run the after_attach hook.
 162          list($message, $new_attachment) =
 163              phorum_hook("after_attach", array($message, $new_attachment));
 164  
 165          // Add the attachment to the message.
 166          $message['attachments'][] = $new_attachment;
 167          $attach_totalsize += $new_attachment["size"];
 168          $attach_count++;
 169          $attached++;
 170      }
 171  
 172      // Show a generic error message if nothing was attached and
 173      // no specific message was set.
 174      if (! $error_flag && ! $attached) {
 175          $PHORUM["DATA"]["ERROR"] =
 176              $PHORUM["DATA"]["LANG"]["AttachmentsMissing"];
 177          $error_flag = true;
 178      }
 179  
 180      // Show a success message in case an attachment is added.
 181      if (! $error_flag && $attached) {
 182          $PHORUM["DATA"]["OKMSG"] = $PHORUM["DATA"]["LANG"]["AttachmentAdded"];
 183  
 184      }
 185  }
 186  ?>


Généré le : Thu Nov 29 12:22:27 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics