Fix simple document upload using the resource node #2326

pull/2715/head
Julio Montoya 7 years ago
parent 27f65c2ac6
commit 7cc9b16fe0
  1. 27
      main/document/upload.php
  2. 47
      main/inc/lib/fileUpload.lib.php
  3. 2
      public/courses.php

@ -2,33 +2,6 @@
/* For licensing terms, see /license.txt */ /* For licensing terms, see /license.txt */
/** /**
* Main script for the documents tool.
*
* This script allows the user to manage files and directories on a remote http server.
*
* The user can : - navigate through files and directories.
* - upload a file
* - delete, copy a file or a directory
* - edit properties & content (name, comments, html content)
*
* The script is organised in four sections.
*
* 1) Execute the command called by the user
* Note: somme commands of this section are organised in two steps.
* The script always begins with the second step,
* so it allows to return more easily to the first step.
*
* Note (March 2004) some editing functions (renaming, commenting)
* are moved to a separate page, edit_document.php. This is also
* where xml and other stuff should be added.
*
* 2) Define the directory to display
*
* 3) Read files and directories from the directory defined in part 2
* 4) Display all of that on an HTML page
*
* @todo eliminate code duplication between
* document/document.php, scormdocument.php
* *
* @package chamilo.document * @package chamilo.document
*/ */

@ -2,6 +2,7 @@
/* For licensing terms, see /license.txt */ /* For licensing terms, see /license.txt */
use Chamilo\CourseBundle\Entity\CDocument; use Chamilo\CourseBundle\Entity\CDocument;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/** /**
* FILE UPLOAD LIBRARY. * FILE UPLOAD LIBRARY.
@ -316,7 +317,7 @@ function handle_uploaded_document(
$whereToSave = $documentDir.$uploadPath; $whereToSave = $documentDir.$uploadPath;
// At least if the directory doesn't exist, tell so // At least if the directory doesn't exist, tell so
if (!is_dir($whereToSave)) { /*if (!is_dir($whereToSave)) {
if (!mkdir($whereToSave, api_get_permissions_for_new_directories())) { if (!mkdir($whereToSave, api_get_permissions_for_new_directories())) {
if ($output) { if ($output) {
Display::addFlash( Display::addFlash(
@ -329,7 +330,7 @@ function handle_uploaded_document(
return false; return false;
} }
} }*/
// Just upload the file "as is" // Just upload the file "as is"
if ($onlyUploadFile) { if ($onlyUploadFile) {
@ -564,8 +565,12 @@ function handle_uploaded_document(
$fullPath = $whereToSave.$fileSystemName; $fullPath = $whereToSave.$fileSystemName;
$filePath = $uploadPath.$fileSystemName; $filePath = $uploadPath.$fileSystemName;
if (moveUploadedFile($uploadedFile, $fullPath)) { $request = \Chamilo\CoreBundle\Framework\Container::getRequest();
chmod($fullPath, $filePermissions); $content = $request->files->get('file');
//if (moveUploadedFile($uploadedFile, $fullPath)) {
if (true) {
///chmod($fullPath, $filePermissions);
// Put the document data in the database // Put the document data in the database
$documentId = add_document( $documentId = add_document(
$courseInfo, $courseInfo,
@ -577,12 +582,15 @@ function handle_uploaded_document(
0, // read only 0, // read only
true, // save visibility true, // save visibility
$groupId, $groupId,
$sessionId $sessionId,
0,
true,
$content
); );
if ($documentId) { if ($documentId) {
// Update document item_property // Update document item_property
api_item_property_update( /*api_item_property_update(
$courseInfo, $courseInfo,
TOOL_DOCUMENT, TOOL_DOCUMENT,
$documentId, $documentId,
@ -593,14 +601,14 @@ function handle_uploaded_document(
null, null,
null, null,
$sessionId $sessionId
); );*/
// Redo visibility // Redo visibility
api_set_default_visibility($documentId, TOOL_DOCUMENT, null, $courseInfo); //api_set_default_visibility($documentId, TOOL_DOCUMENT, null, $courseInfo);
} }
// If the file is in a folder, we need to update all parent folders // If the file is in a folder, we need to update all parent folders
item_property_update_on_folder($courseInfo, $uploadPath, $userId); //item_property_update_on_folder($courseInfo, $uploadPath, $userId);
// Display success message to user // Display success message to user
if ($output) { if ($output) {
@ -616,7 +624,7 @@ function handle_uploaded_document(
return $filePath; return $filePath;
} else { } else {
if ($output) { /*if ($output) {
Display::addFlash( Display::addFlash(
Display::return_message( Display::return_message(
get_lang('UplUnableToSaveFile'), get_lang('UplUnableToSaveFile'),
@ -626,7 +634,7 @@ function handle_uploaded_document(
); );
} }
return false; return false;*/
} }
break; break;
case 'nothing': case 'nothing':
@ -653,8 +661,9 @@ function handle_uploaded_document(
); );
} }
} else { } else {
if (moveUploadedFile($uploadedFile, $fullPath)) { if (moveUploadedFile($uploadedFile, $fullPath)) {
chmod($fullPath, $filePermissions); //chmod($fullPath, $filePermissions);
// Put the document data in the database // Put the document data in the database
$documentId = add_document( $documentId = add_document(
@ -1384,12 +1393,14 @@ function add_document(
$media->setProviderName($provider); $media->setProviderName($provider);
$media->setEnabled(true); $media->setEnabled(true);
if ($content instanceof UploadedFile) {
$handle = tmpfile(); $file = $content;
fwrite($handle, $content); } else {
$file = new \Sonata\MediaBundle\Extra\ApiMediaFile($handle); $handle = tmpfile();
$file->setMimetype($media->getContentType()); fwrite($handle, $content);
$file = new \Sonata\MediaBundle\Extra\ApiMediaFile($handle);
$file->setMimetype($media->getContentType());
}
$media->setBinaryContent($file); $media->setBinaryContent($file);

@ -11,7 +11,7 @@ $publicPath = api_get_path(WEB_PUBLIC_PATH);
// http://localhost/chamilo2/courses/ABC/document/aa.html // http://localhost/chamilo2/courses/ABC/document/aa.html
$courseCode = Security::remove_XSS($_GET['courseCode']); $courseCode = Security::remove_XSS($_GET['courseCode']);
$path = Security::remove_XSS($_GET['url']); $path = Security::remove_XSS($_GET['url']);
$type = Security::remove_XSS($_GET['type']) ?? 'show'; $type = Security::remove_XSS($_GET['type'] ?? 'show') ;
$url = $publicPath."courses/$courseCode/document/$path?type=$type"; $url = $publicPath."courses/$courseCode/document/$path?type=$type";
header("Location: $url"); header("Location: $url");
exit; exit;

Loading…
Cancel
Save