Exemplo n.º 1
0
 /**
  * @todo better transaction
  * @see FileInterface::update_file()
  * @param array $file_array
  * @param integer $previous_version_id
  * @param bool $major
  * @param bool $current
  * @return integer
  */
 public function update_file($file_array, $previous_version_id, $major, $current)
 {
     global $user, $session, $transaction;
     if ($this->file and $this->file_id and $file_array) {
         $transaction_id = $transaction->begin();
         $user_data = new DataUserData($user->get_user_id());
         $folder = Folder::get_instance($this->get_parent_folder_id());
         $folder_path = $folder->get_path();
         if ($folder->is_write_access() == true) {
             $target = constant("BASE_DIR") . "/" . $folder_path . "/" . $file_array['name'];
             if (!empty($file_array['name'])) {
                 $file_name_array = explode(".", $file_array['name']);
                 $file_name_array_length = substr_count($file_array['name'], ".");
                 // Forbidden file-types
                 if ($file_name_array[$file_name_array_length] == "php" or $file_name_array[$file_name_array_length] == "php3" or $file_name_array[$file_name_array_length] == "php4" or $file_name_array[$file_name_array_length] == "php5" or $file_name_array[$file_name_array_length] == "phtml" or $file_name_array[$file_name_array_length] == "phtm" or $file_name_array[$file_name_array_length] == "html" or $file_name_array[$file_name_array_length] == "htm" or $file_name_array[$file_name_array_length] == "cgi" or $file_name_array[$file_name_array_length] == "pl" or $file_name_array[$file_name_array_length] == "asp") {
                     if ($transaction_id != null) {
                         $transaction->rollback($transaction_id);
                     }
                     return 7;
                 } else {
                     // Upload new file
                     move_uploaded_file($file_array['tmp_name'], $target);
                     if (($file_upload_error_no = $file_array['error']) == 0) {
                         $file_size = filesize($target);
                         $checksum = md5_file($target);
                         if ($this->compare_with_current_version($checksum) == false) {
                             $file_upload_precheck_event = new FileUploadPrecheckEvent($this->get_parent_folder_id(), $file_size);
                             $event_handler = new EventHandler($file_upload_precheck_event);
                             if ($event_handler->get_success() == false) {
                                 if ($transaction_id != null) {
                                     $transaction->rollback($transaction_id);
                                 }
                                 return 6;
                             }
                             if ($folder->get_quota_access($user->get_user_id(), $file_size) == true) {
                                 $folder->increase_filesize($user->get_user_id(), $file_size);
                                 $file_upload_event = new FileUploadEvent($this->get_parent_folder_id(), $file_size);
                                 $event_handler = new EventHandler($file_upload_event);
                                 if ($event_handler->get_success() == false) {
                                     // Nothing happens
                                 }
                                 // Rename Old File
                                 $current_file_version_id = FileVersion_Access::get_current_entry_by_toid($this->file_id);
                                 $current_file_version = new FileVersion_Access($current_file_version_id);
                                 $extension_array = explode(".", $current_file_version->get_name());
                                 $extension_array_length = substr_count($current_file_version->get_name(), ".");
                                 $new_internal_revision = $current_file_version->get_internal_revision() + 1;
                                 if ($major == true) {
                                     if ($previous_version_id == null) {
                                         $new_version = $current_file_version->get_version() + 1;
                                         $previous_version_pk_id = null;
                                     } else {
                                         $major_file_version_id = FileVersion_Access::get_entry_by_toid_and_internal_revision($this->file_id, $previous_version_id);
                                         $major_file_version = new FileVersion_Access($major_file_version_id);
                                         if ($major_file_version->get_previous_version_id() == $major_file_version->get_id()) {
                                             $previous_version_pk_id = null;
                                         } else {
                                             $previous_version_pk_id = $major_file_version->get_previous_version_id();
                                         }
                                         $major_file_version_id = FileVersion_Access::get_highest_major_version_entry_by_toid_and_previous_version_id($major_file_version->get_toid(), $previous_version_pk_id);
                                         $major_file_version = new FileVersion_Access($major_file_version_id);
                                         $new_version = $major_file_version->get_version() + 1;
                                     }
                                 } else {
                                     $major_file_version_id = FileVersion_Access::get_entry_by_toid_and_internal_revision($this->file_id, $previous_version_id);
                                     $current_minor_version_id = FileVersion_Access::get_highest_minor_version_entry_by_id($major_file_version_id);
                                     if ($current_minor_version_id) {
                                         $current_minor_version = new FileVersion_Access($current_minor_version_id);
                                         $new_version = $current_minor_version->get_version() + 1;
                                     } else {
                                         $new_version = 1;
                                     }
                                     $previous_version_pk_id = $major_file_version_id;
                                 }
                                 if ($current == true) {
                                     $file_version = new FileVersion_Access(null);
                                     $file_version->create($this->file_id, $file_array['name'], $new_version, $file_size, $checksum, null, $previous_version_pk_id, $new_internal_revision, true, $user->get_user_id());
                                     $current_file_version->set_current(false);
                                 } else {
                                     $file_version = new FileVersion_Access(null);
                                     $file_version->create($this->file_id, $file_array['name'], $new_version, $file_size, $checksum, null, $previous_version_pk_id, $new_internal_revision, false, $user->get_user_id());
                                 }
                                 // Rename File
                                 $extension_array = explode(".", $target);
                                 $extension_array_length = substr_count($target, ".");
                                 if ($extension_array_length == 0) {
                                     $extension_array[0] = "";
                                 }
                                 $new_filename = constant("BASE_DIR") . "/" . $folder_path . "/" . $this->data_entity_id . "-" . $new_internal_revision . "." . $extension_array[$extension_array_length];
                                 // Rename file with the object id
                                 if (rename($target, $new_filename) == true) {
                                     if ($transaction_id != null) {
                                         $transaction->commit($transaction_id);
                                     }
                                     return 1;
                                 } else {
                                     if ($transaction_id != null) {
                                         $transaction->rollback($transaction_id);
                                     }
                                     return 2;
                                 }
                             } else {
                                 // Delete File - Overquota
                                 if (unlink($target)) {
                                     if ($transaction_id != null) {
                                         $transaction->rollback($transaction_id);
                                     }
                                     return 6;
                                 } else {
                                     if ($transaction_id != null) {
                                         $transaction->rollback($transaction_id);
                                     }
                                     // Write Log - Delete Error (nach Rollback)
                                     return 6;
                                 }
                             }
                         } else {
                             // Delete File - Equals previous version
                             if (unlink($target)) {
                                 if ($transaction_id != null) {
                                     $transaction->rollback($transaction_id);
                                 }
                                 return 5;
                             } else {
                                 if ($transaction_id != null) {
                                     $transaction->rollback($transaction_id);
                                 }
                                 // Write Log - Delete Error
                                 return 5;
                             }
                         }
                     } else {
                         if ($file_upload_error_no == 1 or $file_upload_error_no == 2) {
                             if ($transaction_id != null) {
                                 $transaction->rollback($transaction_id);
                             }
                             return 4;
                         } else {
                             if ($transaction_id != null) {
                                 $transaction->rollback($transaction_id);
                             }
                             return 2;
                         }
                     }
                 }
                 // End If/Else - Forbidden File-Types
             } else {
                 if ($transaction_id != null) {
                     $transaction->rollback($transaction_id);
                 }
                 return 3;
             }
         } else {
             if ($transaction_id != null) {
                 $transaction->rollback($transaction_id);
             }
             return 8;
         }
     } else {
         return 3;
     }
 }