// copy the current user_id to the $file array for the hook
        $file["user_id"]=$PHORUM["user"]["user_id"];

        // Run the before_attach hook.
        list($message, $file) =
            phorum_hook("before_attach", array($message, $file));

        // Add the file to the database. We add it using message_id
        // 0 (zero). Only when the message gets saved definitely,
        // the message_id will be updated to link the file to the
        // forum message. This is mainly done so we can support
        // attachments for new messages, which do not yet have
        // a message_id assigned.
        $file_id = phorum_db_file_save(
            $PHORUM["user"]["user_id"],
            $file["name"], $file["size"],
            $file["data"], 0, PHORUM_LINK_EDITOR
        );

        // Create new attachment information.
        $new_attachment = array(
            "file_id" => $file_id,
            "name"    => $file["name"],
            "size"    => $file["size"],
            "keep"    => true,
            "linked"  => false,
        );

        // Run the after_attach hook.
        list($message, $new_attachment) =
            phorum_hook("after_attach", array($message, $new_attachment));
Example #2
0
/**
* Store or update a file.
*
* @example file_store.php Store a personal file.
*
* @param array $file
*     An array, containing information for the file.
*     This array has to contain the following fields:
*     <ul>
*     <li>filename: The name of the file.</li>
*     <li>file_data: The file data.</li>
*     <li>filesize: The size of the file data in bytes.</li>
*     <li>link: A value describing to what type of entity the file is
*         linked. The following values are available:
*         <ul>
*         <li>PHORUM_LINK_USER</li>
*         <li>PHORUM_LINK_MESSAGE</li>
*         <li>PHORUM_LINK_EDITOR</li>
*         <li>PHORUM_LINK_TEMPFILE</li>
*         </ul>
*     </li>
*     <li>user_id: The user to link a file to. If none is provided, then
          the user_id of the active Phorum user will be used.</li>
*     <li>message_id: The message to link a file to or 0 if it's no
*         message attachment.</li>
*     </ul>
*
*     Additionally, the "file_id" field can be set. If it is set,
*     then the existing file will be updated. If it is not set,
*     a new file will be created.
*
* @return mixed
*     On error, this function will return FALSE. The functions
*     {@link phorum_api_strerror()} and {@link phorum_api_errno()} can
*     be used to retrieve information about the error which occurred.
*
*     On success, an array containing the data for the stored file
*     will be returned. If the function is called with no "file_id"
*     in the {@link $file} argument (when a new file is stored),
*     then the new "file_id" and "add_datetime" fields will be
*     included in the return variable as well.
*/
function phorum_api_file_store($file)
{
    $PHORUM = $GLOBALS["PHORUM"];
    // Check if we really got an array argument for $file.
    if (!is_array($file)) {
        trigger_error("phorum_api_file_store(): \$file parameter must be an array.", E_USER_ERROR);
    }
    // Check and preprocess the data from the $file argument.
    // First we create a new empty file structure to fill.
    $checkfile = array("file_id" => NULL, "user_id" => NULL, "filename" => NULL, "filesize" => NULL, "file_data" => NULL, "message_id" => NULL, "link" => NULL);
    // Go over all fields in the $file argument and add these to
    // the $checkfile array.
    foreach ($file as $k => $v) {
        switch ($k) {
            case "file_id":
            case "user_id":
            case "message_id":
            case "filesize":
                if ($v !== NULL) {
                    settype($v, "int");
                }
                $checkfile[$k] = $v;
                break;
            case "filename":
                $v = basename($v);
                $checkfile[$k] = $v;
                break;
            case "link":
            case "file_data":
                $checkfile[$k] = $v;
                break;
            case "add_datetime":
            case "result":
            case "mime_type":
                // These are some dynamic fields which might be pressent
                // in the data, when storing file data that was returned
                // by the file retrieve function. We simply skip these here.
                break;
            default:
                trigger_error("phorum_api_file_store(): \$file parameter contains " . 'an illegal field "' . htmlspecialchars($k) . '".', E_USER_ERROR);
        }
    }
    // Force the message_id and user_id to 0, depending on the
    // link type. Also check if the required id field (user or
    // message) is set for the used link type.
    switch ($checkfile["link"]) {
        case PHORUM_LINK_EDITOR:
            $checkfile["message_id"] = 0;
            $checkfile["user_id"] = 0;
            break;
        case PHORUM_LINK_USER:
            $checkfile["message_id"] = 0;
            if (empty($checkfile["user_id"])) {
                $checkfile["user_id"] = $PHORUM["user"]["user_id"];
            }
            if (empty($checkfile["user_id"])) {
                trigger_error("phorum_api_file_store(): \$file set the link type to " . "PHORUM_LINK_USER, but the user_id was not set.", E_USER_ERROR);
            }
            break;
        case PHORUM_LINK_MESSAGE:
            $checkfile["user_id"] = 0;
            if (empty($checkfile["message_id"])) {
                trigger_error("phorum_api_file_store(): \$file set the link type to " . "PHORUM_LINK_MESSAGE, but the message_id was not set.", E_USER_ERROR);
            }
            break;
        default:
            if (empty($checkfile["message_id"])) {
                $checkfile["message_id"] = 0;
            }
            if (empty($checkfile["user_id"])) {
                $checkfile["user_id"] = 0;
            }
            break;
    }
    // See if all required values are set.
    foreach ($checkfile as $k => $v) {
        if ($k == 'file_id') {
            continue;
        }
        // is NULL for new files.
        if ($v === NULL) {
            trigger_error("phorum_api_file_store(): \$file parameter misses the " . '"' . htmlspecialchars($k) . '" field.', E_USER_ERROR);
        }
    }
    // All data was checked, so now we can continue with the checked data.
    $file = $checkfile;
    // New files need a file_id.
    $created_skeleton_file = FALSE;
    if (empty($file["file_id"])) {
        // Insert a skeleton file record in the database. We do this, to
        // get hold of a new file_id. That file_id can be passed on to
        // the hook below, so alternative storage systems know directly
        // for what file_id they will have to store data, without having
        // to store the full data in the database already.
        $file_id = phorum_db_file_save(array("filename" => $file["filename"], "filesize" => 0, "file_data" => "", "user_id" => 0, "message_id" => 0, "link" => PHORUM_LINK_TEMPFILE));
        $file["file_id"] = $file_id;
        $created_skeleton_file = TRUE;
    }
    // Allow modules to handle file data storage. If a module implements
    // a different data storage method, it can store the file data in its
    // own way and set the "file_data" field to an empty string in $file
    // (it is not mandatory to do so, but it is adviceable, since it
    // would make no sense to store the file data both in an alternative
    // storage and the database at the same time).
    // The hook can use phorum_api_error_set() to return an error.
    // Hooks should be aware that their input might not be $file, but
    // FALSE instead, in which case they should immediately return
    // FALSE themselves.
    if (isset($PHORUM["hooks"]["file_store"])) {
        $hook_result = phorum_hook("file_store", $file);
        // Return if a module returned an error.
        if ($hook_result === FALSE) {
            // Cleanup the skeleton file from the database.
            if ($created_skeleton_file) {
                phorum_db_file_delete($file["file_id"]);
                $file["file_id"] = NULL;
            }
            return FALSE;
        }
        $file = $hook_result;
    }
    // Phorum stores the files in base64 format in the database, to
    // prevent problems with upgrading and migrating database servers.
    // The ASCII representation for the files will always be safe to dump
    // and restore. So here we will base64 encode the file data.
    //
    // If the file_data field is an empty string by now, then either the
    // file data was really empty to start with or a module handled the
    // storage. In both cases it's fine to keep the data field empty.
    if ($file["file_data"] != '') {
        $file["file_data"] = base64_encode($file["file_data"]);
    }
    // Update the (skeleton) file record to match the real file data.
    // This acts like a commit action for the file storage.
    phorum_db_file_save($file);
    return $file;
}
Example #3
0
            }
        }

        if($PHORUM["file_space_quota"]>0 && phorum_db_get_user_filesize_total($PHORUM["user"]["user_id"])+$_FILES["newfile"]["size"]>=$PHORUM["file_space_quota"]*1024){
            $error_msg = true;
            $PHORUM["DATA"]["MESSAGE"] = $PHORUM["DATA"]["LANG"]["FileOverQuota"];
        }

        if(empty($error_msg)){

            // read in the file
            $fp=fopen($_FILES["newfile"]["tmp_name"], "r");
            $buffer=base64_encode(fread($fp, $_FILES["newfile"]["size"]));
            fclose($fp);

            $file_id=phorum_db_file_save($PHORUM["user"]["user_id"], $_FILES["newfile"]["name"], $_FILES["newfile"]["size"], $buffer);

        }

    } elseif(!empty($_POST["delete"])) {

        foreach($_POST["delete"] as $file_id){

            phorum_db_file_delete($file_id);

        }                

    }

    $files = phorum_db_get_user_file_list($PHORUM["user"]["user_id"]);
Example #4
0
function phorum_convert_getNextMessage($res,$table_name) {
      global $CONVERT;

      // fetching the message from the database
      $mdata = mysql_fetch_assoc($res);
      if(!$mdata) {
            return false;
      }
      $max_id= $CONVERT['max_id'];

      $id=$mdata['id'];
      if($mdata['closed'])
            $closed=1;
      else
            $closed=0;

      if($mdata['approved'] != "Y")
            $post_status=PHORUM_STATUS_HOLD;
      else
            $post_status=PHORUM_STATUS_APPROVED;

      $post_sort=PHORUM_SORT_DEFAULT;

      $parentid=($mdata['parent']>0)?($mdata['parent']+$max_id):0;

      if(!get_magic_quotes_runtime()){
            $mdata['author'] = $mdata['author'];
            $mdata['subject'] = $mdata['subject'];
            $mdata['body'] = $mdata['body'];
            $mdata['email'] = $mdata['email'];
      }

      //find [%sig%] and cut it
      if (preg_match ("/\[%sig%\]/", $mdata['body'])) {
      	$mdata['body'] = preg_replace ( "/\[%sig%\]/", "", $mdata['body']);
      	$add_signature = true;
      } else {
        $add_signature = false;
      }

      // bah, there are really people trying to upgrade from 3.2.x ;)
      $userid = (isset($mdata['userid']) ? $mdata['userid'] : 0);


      // building the new message
      $newmessage = array(
          'message_id'=> $mdata['id']+$max_id,
          'forum_id'  => $CONVERT['forum_id'],
          'datestamp' => $mdata['unixtime'],
          'thread'    => ($mdata['thread']+$max_id),
          'parent_id' => $parentid,
          'author'    => $mdata['author'],
          'subject'   => $mdata['subject'],
          'email'     => $mdata['email'],
          'ip'        => $mdata['host'],
          'user_id'   => $userid,
          'moderator_post' => 0,
          'status'    => $post_status,
          'sort'      => $post_sort,
          'msgid'     => $mdata['msgid'],
          'closed'    => $closed,
          'body'      => $mdata['body']
      );

      if($add_signature) {
          $newmessage["meta"]["show_signature"]=1;
      }
      if(isset($mdata['viewcount'])) {
          $newmessage['viewcount']=$mdata['viewcount'];
      }
      $newmessage['viewcount'] = (isset($mdata['viewcount']) ? $mdata['viewcount'] : 0);
      // converting attachments if needed
      $inserted_files=array();
      if (isset($CONVERT['attachments'][$mdata['id']]) && count($CONVERT['attachments'][$mdata['id']])) {
          foreach($CONVERT['attachments'][$mdata['id']] as $attachment) {
              $filename = $CONVERT['attachmentdir']."/".$table_name."/".$attachment['id'].strtolower(strrchr($attachment['filename'], "."));
              if(file_exists($filename) && filesize($filename)>0) {
                  $fp=fopen($filename, "r");
                  $buffer=base64_encode(fread($fp, filesize($filename)));
                  fclose($fp);
                  $file_id = phorum_db_file_save($userid, $attachment['filename'], filesize($filename), $buffer, $newmessage['message_id']);
                  unset($buffer); // free that large buffer
                  $inserted_files[]=array("file_id"=>$file_id, "name"=>$attachment['filename'], "size"=>filesize($filename));
              }
          }
      }
      if(count($inserted_files)) {
          $newmessage["meta"]["attachments"]=$inserted_files;
      }


      return $newmessage;
}