Remove deprecated file upload in justification

Replace code with PersonalFile #3792
pull/3904/head
Julio Montoya 4 years ago
parent 56fe63bb98
commit 6dde830929
  1. 58
      public/main/auth/justification.php
  2. 4
      public/main/inc/lib/TicketManager.php
  3. 136
      public/main/inc/lib/api.lib.php
  4. 6
      public/plugin/customcertificate/src/index.php
  5. 15
      public/plugin/justification/justification_by_user.php
  6. 3
      public/plugin/whispeakauth/Controller/BaseController.php
  7. 80
      src/CoreBundle/Framework/Container.php

@ -1,6 +1,10 @@
<?php
/* For licensing terms, see /license.txt */
use Chamilo\CoreBundle\Entity\PersonalFile;
use Chamilo\CoreBundle\Framework\Container;
$cidReset = true;
require_once __DIR__.'/../inc/global.inc.php';
@ -11,8 +15,9 @@ $allowJustification = 'true' === api_get_plugin_setting('justification', 'tool_e
if (!$allowJustification) {
api_not_allowed(true);
}
$user_data = api_get_user_info(api_get_user_id());
$personalRepo = Container::getPersonalFileRepository();
$userId = api_get_user_id();
$user = api_get_user_entity();
$justification = '';
$plugin = Justification::create();
@ -34,8 +39,10 @@ if ($formValidator->validate() && isset($_FILES)) {
$fieldId = $field['id'];
$days = $field['validity_duration'];
$fileNameKey = '';
if (isset($_FILES[$field['code'].'_file']) && !empty($_FILES[$field['code'].'_file']['tmp_name'])) {
$file = $_FILES[$field['code'].'_file'];
$fileNameKey = $field['code'].'_file';
} else {
continue;
}
@ -60,7 +67,9 @@ if ($formValidator->validate() && isset($_FILES)) {
$id = Database::insert('justification_document_rel_users', $params);
if ($id) {
api_upload_file('justification', $file, $id);
$personalFile = new PersonalFile();
$personalFile->setTitle($fileName);
$personalRepo->addFileFromFileRequest($personalFile, $fileNameKey, true);
Display::addFlash(Display::return_message($plugin->get_lang('JustificationSaved')));
}
}
@ -71,7 +80,7 @@ if ($formValidator->validate() && isset($_FILES)) {
$userJustifications = $plugin->getUserJustificationList(api_get_user_id());
$userJustificationList = '';
$action = isset($_REQUEST['a']) ? $_REQUEST['a'] : '';
$action = $_REQUEST['a'] ?? '';
$justificationContent = '';
switch ($action) {
@ -82,7 +91,11 @@ switch ($action) {
if (0 == $justification['date_manual_on']) {
api_not_allowed(true);
}
$formEdit = new FormValidator('edit', 'post', api_get_self().'?a=edit_justification&justification_id='.$justificationId);
$formEdit = new FormValidator(
'edit',
'post',
api_get_self().'?a=edit_justification&justification_id='.$justificationId
);
$formEdit->addHeader($justification['name']);
$element = $formEdit->addDatePicker('date_validity', $plugin->get_lang('ValidityDate'));
$element->setValue($userJustification['date_validity']);
@ -93,7 +106,7 @@ switch ($action) {
$values = $formEdit->getSubmitValues();
$date = Database::escape_string($values['date_validity']);
$sql = "UPDATE justification_document_rel_users SET date_validity = '$date'
WHERE id = $justificationId AND user_id = ".$user_data['id'];
WHERE id = $justificationId AND user_id = ".$userId;
Database::query($sql);
Display::addFlash(Display::return_message(get_lang('Updated')));
header('Location: '.api_get_self());
@ -104,9 +117,17 @@ switch ($action) {
$justificationId = isset($_REQUEST['justification_id']) ? (int) $_REQUEST['justification_id'] : '';
$userJustification = $plugin->getUserJustification($justificationId);
if ($userJustification && $userJustification['user_id'] == api_get_user_id()) {
api_remove_uploaded_file_by_id('justification', $justificationId, $userJustification['file_path']);
$personalFile = $personalRepo->getResourceByCreatorFromTitle(
$userJustification['file_path'],
$user,
$user->getResourceNode()
);
if (null !== $personalFile) {
$personalRepo->delete($personalFile);
}
$sql = "DELETE FROM justification_document_rel_users
WHERE id = $justificationId AND user_id = ".$user_data['id'];
WHERE id = $justificationId AND user_id = ".$userId;
Database::query($sql);
Display::addFlash(Display::return_message(get_lang('Deleted')));
}
@ -134,7 +155,14 @@ if (!empty($userJustifications)) {
$row = 1;
foreach ($userJustifications as $userJustification) {
$justification = $plugin->getJustification($userJustification['justification_document_id']);
$url = api_get_uploaded_web_url('justification', $userJustification['id'], $userJustification['file_path']);
$personalFile = $personalRepo->findResourceByTitle(
$userJustification['file_path'],
$user->getResourceNode()
);
$url = $personalRepo->getResourceFileUrl($personalFile);
//$url = api_get_uploaded_web_url('justification', $userJustification['id'], $userJustification['file_path']);
$link = Display::url($userJustification['file_path'], $url);
$col = 0;
$table->setCellContents($row, $col++, $justification['name']);
@ -147,9 +175,17 @@ if (!empty($userJustifications)) {
$actions = '';
if (1 == $justification['date_manual_on']) {
$actions .= Display::url(get_lang('Edit'), api_get_self().'?a=edit_justification&justification_id='.$userJustification['id'], ['class' => 'btn btn-primary']);
$actions .= Display::url(
get_lang('Edit'),
api_get_self().'?a=edit_justification&justification_id='.$userJustification['id'],
['class' => 'btn btn-primary']
);
}
$actions .= '&nbsp;'.Display::url(get_lang('Delete'), api_get_self().'?a=delete_justification&justification_id='.$userJustification['id'], ['class' => 'btn btn-danger']);
$actions .= '&nbsp;'.Display::url(
get_lang('Delete'),
api_get_self().'?a=delete_justification&justification_id='.$userJustification['id'],
['class' => 'btn btn-danger']
);
$table->setCellContents($row, $col++, $actions);
$row++;
}

@ -741,7 +741,9 @@ class TicketManager
'error'
);
} else {
$result = api_upload_file('ticket_attachment', $file_attach, $ticketId);
throw new Exception('@todo file upload ticket_attachment');
//$result = api_upload_file('ticket_attachment', $file_attach, $ticketId);
if ($result) {
$safe_file_name = Database::escape_string($new_file_name);
$safe_new_file_name = Database::escape_string($result['path_to_save']);

@ -7930,138 +7930,6 @@ function api_is_student_view_active()
return 'studentview' === $studentView;
}
/**
* Adds a file inside the upload/$type/id.
*
* @param string $type
* @param array $file
* @param int $itemId
* @param string $cropParameters
*
* @return array|bool
*/
function api_upload_file($type, $file, $itemId, $cropParameters = '')
{
throw new Exception('api_upload_file not implemented');
$upload = process_uploaded_file($file);
if ($upload) {
$name = api_replace_dangerous_char($file['name']);
// No "dangerous" files
$name = disable_dangerous_file($name);
$pathId = '/'.substr((string) $itemId, 0, 1).'/'.$itemId.'/';
$path = api_get_path(SYS_UPLOAD_PATH).$type.$pathId;
if (!is_dir($path)) {
mkdir($path, api_get_permissions_for_new_directories(), true);
}
$pathToSave = $path.$name;
$result = moveUploadedFile($file, $pathToSave);
if ($result) {
if (!empty($cropParameters)) {
$image = new Image($pathToSave);
$image->crop($cropParameters);
}
return ['path_to_save' => $pathId.$name];
}
}
return false;
}
/**
* @param string $type
* @param int $itemId
* @param string $file
*
* @return bool
*/
function api_get_uploaded_web_url($type, $itemId, $file)
{
return api_get_uploaded_file($type, $itemId, $file, true);
}
/**
* @param string $type
* @param int $itemId
* @param string $file
* @param bool $getUrl
*
* @return bool
*/
function api_get_uploaded_file($type, $itemId, $file, $getUrl = false)
{
$itemId = (int) $itemId;
$pathId = '/'.substr((string) $itemId, 0, 1).'/'.$itemId.'/';
$path = api_get_path(SYS_UPLOAD_PATH).$type.$pathId;
$file = basename($file);
$file = $path.'/'.$file;
if (Security::check_abs_path($file, $path) && is_file($file) && file_exists($file)) {
if ($getUrl) {
return str_replace(api_get_path(SYS_UPLOAD_PATH), api_get_path(WEB_UPLOAD_PATH), $file);
}
return $file;
}
return false;
}
/**
* @param string $type
* @param int $itemId
* @param string $file
* @param string $title
*/
function api_download_uploaded_file($type, $itemId, $file, $title = '')
{
$file = api_get_uploaded_file($type, $itemId, $file);
if ($file) {
if (Security::check_abs_path($file, api_get_path(SYS_UPLOAD_PATH).$type)) {
DocumentManager::file_send_for_download($file, true, $title);
exit;
}
}
api_not_allowed(true);
}
/**
* @param string $type
* @param string $file
*/
function api_remove_uploaded_file($type, $file)
{
$typePath = api_get_path(SYS_UPLOAD_PATH).$type;
$path = $typePath.'/'.$file;
if (Security::check_abs_path($path, $typePath) && file_exists($path) && is_file($path)) {
unlink($path);
}
}
/**
* @param string $type
* @param int $itemId
* @param string $file
*
* @return bool
*/
function api_remove_uploaded_file_by_id($type, $itemId, $file)
{
$file = api_get_uploaded_file($type, $itemId, $file, false);
$typePath = api_get_path(SYS_UPLOAD_PATH).$type;
if (Security::check_abs_path($file, $typePath) && file_exists($file) && is_file($file)) {
unlink($file);
return true;
}
return false;
}
/**
* Converts string value to float value.
*
@ -8076,9 +7944,7 @@ function api_remove_uploaded_file_by_id($type, $itemId, $file)
*/
function api_float_val($number)
{
$number = (float) str_replace(',', '.', trim($number));
return $number;
return (float) str_replace(',', '.', trim($number));
}
/**

@ -84,7 +84,7 @@ $htmlHeadXtra[] = '<script>
success: function (response) {
window.location.reload();
}
});
});
}
});
@ -172,6 +172,8 @@ if ($form->validate()) {
);
}
throw new Exception('@todo custom certificate upload');
if ($_FILES[$field]['size']) {
$newPicture = api_upload_file(
'certificates',
@ -431,7 +433,7 @@ $form->addHtml('<div class="form-group" style="padding-top: 10px;">
<div class="col-sm-10">
<div class="radio" style="margin-top: -25px;">
<span style="margin: 0 10px; font-style: italic;">'.get_lang('From').'</span>
<input
<input
size="20"
autofocus="autofocus"
class="form-control-cert text-center datepicker"

@ -1,6 +1,8 @@
<?php
/* For license terms, see /license.txt */
use Chamilo\CoreBundle\Framework\Container;
require_once __DIR__.'/../../main/inc/global.inc.php';
api_protect_admin_script();
@ -30,7 +32,9 @@ if ($form->validate()) {
$userId = $form->getSubmitValue('user_id');
}
$personalRepo = Container::getPersonalFileRepository();
if ($userId) {
$user = api_get_user_entity($userId);
$tpl->assign('user_info', api_get_user_info($userId));
$list = $plugin->getUserJustificationList($userId);
if ($list) {
@ -39,9 +43,18 @@ if ($userId) {
$item['date_validity'] = Display::label($item['date_validity'], 'warning');
}
$item['justification'] = $plugin->getJustification($item['justification_document_id']);
$personalFile = $personalRepo->getResourceByCreatorFromTitle(
$item['file_path'],
$user,
$user->getResourceNode()
);
$url = $personalRepo->getResourceFileUrl($personalFile);
$item['file_path'] = Display::url(
$item['file_path'],
api_get_uploaded_web_url('justification', $item['id'], $item['file_path']),
$url,
['target' => '_blank']
);
}

@ -39,9 +39,10 @@ abstract class BaseController
*/
protected function uploadAudioFile(User $user)
{
throw new Exception('@todo uploadAudioFile');
$pluginName = $this->plugin->get_name();
$path = api_upload_file($pluginName, $_FILES['audio'], $user->getId());
//$path = api_upload_file($pluginName, $_FILES['audio'], $user->getId());
if (false === $path) {
throw new \Exception(get_lang('UploadError'));

@ -14,6 +14,7 @@ use Chamilo\CoreBundle\Repository\Node\AccessUrlRepository;
use Chamilo\CoreBundle\Repository\Node\CourseRepository;
use Chamilo\CoreBundle\Repository\Node\IllustrationRepository;
use Chamilo\CoreBundle\Repository\Node\MessageAttachmentRepository;
use Chamilo\CoreBundle\Repository\Node\PersonalFileRepository;
use Chamilo\CoreBundle\Repository\Node\UsergroupRepository;
use Chamilo\CoreBundle\Repository\Node\UserRepository;
use Chamilo\CoreBundle\Repository\SequenceRepository;
@ -242,6 +243,11 @@ class Container
return Database::getManager();
}
public static function getAssetRepository(): AssetRepository
{
return self::$container->get(AssetRepository::class);
}
public static function getAttendanceRepository(): CAttendanceRepository
{
return self::$container->get(CAttendanceRepository::class);
@ -262,14 +268,14 @@ class Container
return self::$container->get(CAnnouncementAttachmentRepository::class);
}
public static function getCourseRepository(): CourseRepository
public static function getBlogRepository(): CBlogRepository
{
return self::$container->get(CourseRepository::class);
return self::$container->get(CBlogRepository::class);
}
public static function getSessionRepository(): SessionRepository
public static function getCourseRepository(): CourseRepository
{
return self::$container->get(SessionRepository::class);
return self::$container->get(CourseRepository::class);
}
public static function getCourseCategoryRepository(): CourseCategoryRepository
@ -282,11 +288,6 @@ class Container
return self::$container->get(CCourseDescriptionRepository::class);
}
public static function getGlossaryRepository(): CGlossaryRepository
{
return self::$container->get(CGlossaryRepository::class);
}
public static function getCalendarEventRepository(): CCalendarEventRepository
{
return self::$container->get(CCalendarEventRepository::class);
@ -302,14 +303,29 @@ class Container
return self::$container->get(CDocumentRepository::class);
}
public static function getQuizRepository(): CQuizRepository
public static function getExerciseCategoryRepository(): CExerciseCategoryRepository
{
return self::$container->get(CQuizRepository::class);
return self::$container->get(CExerciseCategoryRepository::class);
}
public static function getExerciseCategoryRepository(): CExerciseCategoryRepository
public static function getGlossaryRepository(): CGlossaryRepository
{
return self::$container->get(CExerciseCategoryRepository::class);
return self::$container->get(CGlossaryRepository::class);
}
public static function getGradeBookCategoryRepository(): GradeBookCategoryRepository
{
return self::$container->get(GradeBookCategoryRepository::class);
}
public static function getGroupRepository(): CGroupRepository
{
return self::$container->get(CGroupRepository::class);
}
public static function getGroupCategoryRepository(): CGroupCategoryRepository
{
return self::$container->get(CGroupCategoryRepository::class);
}
public static function getForumRepository(): CForumRepository
@ -337,19 +353,14 @@ class Container
return self::$container->get(CForumThreadRepository::class);
}
public static function getGradeBookCategoryRepository(): GradeBookCategoryRepository
{
return self::$container->get(GradeBookCategoryRepository::class);
}
public static function getGroupRepository(): CGroupRepository
public static function getIllustrationRepository(): IllustrationRepository
{
return self::$container->get(CGroupRepository::class);
return self::$container->get(IllustrationRepository::class);
}
public static function getGroupCategoryRepository(): CGroupCategoryRepository
public static function getQuizRepository(): CQuizRepository
{
return self::$container->get(CGroupCategoryRepository::class);
return self::$container->get(CQuizRepository::class);
}
public static function getQuestionRepository(): CQuizQuestionRepository
@ -397,6 +408,11 @@ class Container
return self::$container->get(CNotebookRepository::class);
}
public static function getPersonalFileRepository(): PersonalFileRepository
{
return self::$container->get(PersonalFileRepository::class);
}
public static function getUserRepository(): UserRepository
{
return self::$container->get(UserRepository::class);
@ -407,11 +423,6 @@ class Container
return self::$container->get(UsergroupRepository::class);
}
public static function getIllustrationRepository(): IllustrationRepository
{
return self::$container->get(IllustrationRepository::class);
}
public static function getShortcutRepository(): CShortcutRepository
{
return self::$container->get(CShortcutRepository::class);
@ -447,6 +458,11 @@ class Container
return self::$container->get(SequenceRepository::class);
}
public static function getSessionRepository(): SessionRepository
{
return self::$container->get(SessionRepository::class);
}
public static function getSurveyRepository(): CSurveyRepository
{
return self::$container->get(CSurveyRepository::class);
@ -472,11 +488,6 @@ class Container
return self::$container->get(CThematicAdvanceRepository::class);
}
public static function getBlogRepository(): CBlogRepository
{
return self::$container->get(CBlogRepository::class);
}
public static function getWikiRepository(): CWikiRepository
{
return self::$container->get(CWikiRepository::class);
@ -506,11 +517,6 @@ class Container
return self::$container->get(ToolChain::class);
}
public static function getAssetRepository(): AssetRepository
{
return self::$container->get(AssetRepository::class);
}
public static function setLegacyServices(ContainerInterface $container, bool $setSession = true): void
{
Database::setConnection($container->get('doctrine.dbal.default_connection'));

Loading…
Cancel
Save