2013-07-25 18:35:48 +02:00
< ? php
2013-09-18 14:58:26 +02:00
/* Copyright ( C ) 2013 Cédric Salvador < csalvador @ gpcsolutions . fr >
2015-07-07 09:40:23 +02:00
* Copyright ( C ) 2015 Marcos García < marcosgdf @ gmail . com >
2015-11-18 13:59:25 +01:00
* Copyright ( C ) 2015 Ferran Marcet < fmarcet @ 2 byte . es >
2013-09-18 14:58:26 +02:00
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < http :// www . gnu . org / licenses />.
* or see http :// www . gnu . org /
*/
2017-11-18 15:41:30 +01:00
// Variable $upload_dir must be defined when entering here.
2015-11-02 17:56:00 +01:00
// Variable $upload_dirold may also exists.
2017-11-18 15:41:30 +01:00
// Variable $confirm must be defined.
2013-09-18 14:58:26 +02:00
2016-12-31 03:43:22 +01:00
//var_dump($upload_dir);
//var_dump($upload_dirold);
2017-11-18 15:41:30 +01:00
2016-08-05 04:48:30 +02:00
// Submit file/link
2019-01-27 11:55:16 +01:00
if ( GETPOST ( 'sendit' , 'alpha' ) && ! empty ( $conf -> global -> MAIN_UPLOAD_DOC ))
2014-07-19 22:05:35 +02:00
{
2019-03-07 20:53:03 +01:00
if ( ! empty ( $_FILES ))
2017-11-18 15:41:30 +01:00
{
if ( is_array ( $_FILES [ 'userfile' ][ 'tmp_name' ])) $userfiles = $_FILES [ 'userfile' ][ 'tmp_name' ];
else $userfiles = array ( $_FILES [ 'userfile' ][ 'tmp_name' ]);
foreach ( $userfiles as $key => $userfile )
{
if ( empty ( $_FILES [ 'userfile' ][ 'tmp_name' ][ $key ]))
{
$error ++ ;
if ( $_FILES [ 'userfile' ][ 'error' ][ $key ] == 1 || $_FILES [ 'userfile' ][ 'error' ][ $key ] == 2 ){
setEventMessages ( $langs -> trans ( 'ErrorFileSizeTooLarge' ), null , 'errors' );
}
else {
setEventMessages ( $langs -> trans ( " ErrorFieldRequired " , $langs -> transnoentitiesnoconv ( " File " )), null , 'errors' );
}
}
}
if ( ! $error )
{
2018-10-19 12:47:59 +02:00
// Define if we have to generate thumbs or not
$generatethumbs = 1 ;
if ( GETPOST ( 'section_dir' )) $generatethumbs = 0 ;
2017-11-18 15:41:30 +01:00
if ( ! empty ( $upload_dirold ) && ! empty ( $conf -> global -> PRODUCT_USE_OLD_PATH_FOR_PHOTO ))
{
2018-10-19 12:47:59 +02:00
$result = dol_add_file_process ( $upload_dirold , 0 , 1 , 'userfile' , GETPOST ( 'savingdocmask' , 'alpha' ), null , '' , $generatethumbs );
2017-11-18 15:41:30 +01:00
}
elseif ( ! empty ( $upload_dir ))
{
2018-10-19 12:47:59 +02:00
$result = dol_add_file_process ( $upload_dir , 0 , 1 , 'userfile' , GETPOST ( 'savingdocmask' , 'alpha' ), null , '' , $generatethumbs );
2017-11-18 15:41:30 +01:00
}
}
}
2014-07-19 22:05:35 +02:00
}
2019-01-27 11:55:16 +01:00
elseif ( GETPOST ( 'linkit' , 'none' ) && ! empty ( $conf -> global -> MAIN_UPLOAD_DOC ))
2014-07-19 22:05:35 +02:00
{
2017-11-18 15:41:30 +01:00
$link = GETPOST ( 'link' , 'alpha' );
if ( $link )
2014-07-19 22:05:35 +02:00
{
2017-11-18 15:41:30 +01:00
if ( substr ( $link , 0 , 7 ) != 'http://' && substr ( $link , 0 , 8 ) != 'https://' && substr ( $link , 0 , 7 ) != 'file://' ) {
$link = 'http://' . $link ;
2013-07-25 18:35:48 +02:00
}
2018-10-19 12:47:59 +02:00
dol_add_file_process ( $upload_dir , 0 , 1 , 'userfile' , null , $link , '' , 0 );
2013-07-29 16:37:49 +02:00
}
2013-07-25 18:35:48 +02:00
}
2013-07-30 16:46:41 +02:00
// Delete file/link
2013-11-18 00:20:05 +01:00
if ( $action == 'confirm_deletefile' && $confirm == 'yes' )
2013-11-15 11:43:16 +01:00
{
2018-10-19 10:59:34 +02:00
$urlfile = GETPOST ( 'urlfile' , 'alpha' , 0 , null , null , 1 ); // Do not use urldecode here ($_GET and $_REQUEST are already decoded by PHP).
if ( GETPOST ( 'section' , 'alpha' )) // For a delete from the ECM module, upload_dir is ECM root dir and urlfile contains relative path from upload_dir
{
$file = $upload_dir . ( preg_match ( '/\/$/' , $upload_dir ) ? '' : '/' ) . $urlfile ;
}
else // For a delete from the file manager into another module, or from documents pages, upload_dir contains already path to file from module dir, so we clean path into urlfile.
2014-01-19 22:43:36 +01:00
{
$urlfile = basename ( $urlfile );
2018-10-19 10:59:34 +02:00
$file = $upload_dir . ( preg_match ( '/\/$/' , $upload_dir ) ? '' : '/' ) . $urlfile ;
2015-11-02 17:56:00 +01:00
if ( ! empty ( $upload_dirold )) $fileold = $upload_dirold . " / " . $urlfile ;
2014-01-19 22:43:36 +01:00
}
2018-10-19 10:59:34 +02:00
$linkid = GETPOST ( 'linkid' , 'int' );
2014-01-19 22:43:36 +01:00
2018-10-19 10:59:34 +02:00
if ( $urlfile ) // delete of a file
2013-11-15 11:43:16 +01:00
{
2018-10-19 10:59:34 +02:00
$dir = dirname ( $file ) . '/' ; // Chemin du dossier contenant l'image d'origine
$dirthumb = $dir . '/thumbs/' ; // Chemin du dossier contenant la vignette (if file is an image)
2015-07-07 09:40:23 +02:00
2017-11-18 15:41:30 +01:00
$ret = dol_delete_file ( $file , 0 , 0 , 0 , ( is_object ( $object ) ? $object : null ));
if ( ! empty ( $fileold )) dol_delete_file ( $fileold , 0 , 0 , 0 , ( is_object ( $object ) ? $object : null )); // Delete file using old path
2017-06-18 21:52:51 +02:00
2015-07-07 09:40:23 +02:00
// Si elle existe, on efface la vignette
2019-01-27 11:55:16 +01:00
if ( preg_match ( '/(\.jpg|\.jpeg|\.bmp|\.gif|\.png|\.tiff)$/i' , $file , $regs ))
2015-07-07 09:40:23 +02:00
{
2019-01-27 11:55:16 +01:00
$photo_vignette = basename ( preg_replace ( '/' . $regs [ 0 ] . '/i' , '' , $file ) . '_small' . $regs [ 0 ]);
2015-07-07 09:40:23 +02:00
if ( file_exists ( dol_osencode ( $dirthumb . $photo_vignette )))
{
dol_delete_file ( $dirthumb . $photo_vignette );
}
2019-01-27 11:55:16 +01:00
$photo_vignette = basename ( preg_replace ( '/' . $regs [ 0 ] . '/i' , '' , $file ) . '_mini' . $regs [ 0 ]);
2015-07-07 09:40:23 +02:00
if ( file_exists ( dol_osencode ( $dirthumb . $photo_vignette )))
{
dol_delete_file ( $dirthumb . $photo_vignette );
}
}
2015-11-19 13:59:58 +01:00
if ( $ret ) setEventMessages ( $langs -> trans ( " FileWasRemoved " , $urlfile ), null , 'mesgs' );
else setEventMessages ( $langs -> trans ( " ErrorFailToDeleteFile " , $urlfile ), null , 'errors' );
2013-11-18 00:20:05 +01:00
}
2018-10-19 10:59:34 +02:00
elseif ( $linkid ) // delete of external link
2013-11-15 11:43:16 +01:00
{
2013-11-18 00:20:05 +01:00
require_once DOL_DOCUMENT_ROOT . '/core/class/link.class.php' ;
2013-07-30 16:46:41 +02:00
$link = new Link ( $db );
$link -> id = $linkid ;
$link -> fetch ();
$res = $link -> delete ( $user );
2014-12-23 11:39:43 +01:00
2013-07-30 17:47:24 +02:00
$langs -> load ( 'link' );
2014-12-23 11:39:43 +01:00
if ( $res > 0 ) {
2015-11-19 13:59:58 +01:00
setEventMessages ( $langs -> trans ( " LinkRemoved " , $link -> label ), null , 'mesgs' );
2013-07-30 16:46:41 +02:00
} else {
2014-12-23 11:39:43 +01:00
if ( count ( $link -> errors )) {
setEventMessages ( '' , $link -> errors , 'errors' );
} else {
2015-11-19 13:59:58 +01:00
setEventMessages ( $langs -> trans ( " ErrorFailedToDeleteLink " , $link -> label ), null , 'errors' );
2014-12-23 11:39:43 +01:00
}
2013-07-30 16:46:41 +02:00
}
2013-07-29 16:37:49 +02:00
}
2017-11-18 15:41:30 +01:00
if ( is_object ( $object ) && $object -> id > 0 )
{
if ( $backtopage )
{
header ( 'Location: ' . $backtopage );
exit ;
}
else
{
2019-01-27 11:55:16 +01:00
header ( 'Location: ' . $_SERVER [ " PHP_SELF " ] . '?id=' . $object -> id . ( GETPOST ( 'section_dir' , 'alpha' ) ? '§ion_dir=' . urlencode ( GETPOST ( 'section_dir' , 'alpha' )) : '' ) . ( ! empty ( $withproject ) ? '&withproject=1' : '' ));
2017-11-18 15:41:30 +01:00
exit ;
}
}
2013-11-18 00:20:05 +01:00
}
2019-01-27 11:55:16 +01:00
elseif ( $action == 'confirm_updateline' && GETPOST ( 'save' , 'alpha' ) && GETPOST ( 'link' , 'alpha' ))
2013-11-15 11:43:16 +01:00
{
2013-11-18 00:20:05 +01:00
require_once DOL_DOCUMENT_ROOT . '/core/class/link.class.php' ;
2013-07-31 16:21:24 +02:00
$langs -> load ( 'link' );
$link = new Link ( $db );
$link -> id = GETPOST ( 'linkid' , 'int' );
$f = $link -> fetch ();
2013-11-18 00:20:05 +01:00
if ( $f )
2013-11-15 11:43:16 +01:00
{
2013-07-31 16:21:24 +02:00
$link -> url = GETPOST ( 'link' , 'alpha' );
2016-10-22 01:57:25 +02:00
if ( substr ( $link -> url , 0 , 7 ) != 'http://' && substr ( $link -> url , 0 , 8 ) != 'https://' && substr ( $link -> url , 0 , 7 ) != 'file://' )
2013-11-15 11:43:16 +01:00
{
2013-07-31 16:21:24 +02:00
$link -> url = 'http://' . $link -> url ;
}
$link -> label = GETPOST ( 'label' , 'alpha' );
$res = $link -> update ( $user );
2013-11-18 00:20:05 +01:00
if ( ! $res )
2013-11-15 11:43:16 +01:00
{
2015-11-19 13:59:58 +01:00
setEventMessages ( $langs -> trans ( " ErrorFailedToUpdateLink " , $link -> label ), null , 'mesgs' );
2013-07-31 16:21:24 +02:00
}
2013-11-18 00:20:05 +01:00
}
else
2013-11-15 11:43:16 +01:00
{
2013-07-31 16:21:24 +02:00
//error fetching
}
2013-07-25 18:35:48 +02:00
}
2019-01-27 11:55:16 +01:00
elseif ( $action == 'renamefile' && GETPOST ( 'renamefilesave' , 'alpha' ))
2016-09-07 01:54:16 +02:00
{
2018-03-06 09:20:45 +01:00
// For documents pages, upload_dir contains already path to file from module dir, so we clean path into urlfile.
2018-03-02 17:05:52 +01:00
if ( ! empty ( $upload_dir ))
{
2019-01-27 11:55:16 +01:00
$filenamefrom = dol_sanitizeFileName ( GETPOST ( 'renamefilefrom' , 'alpha' ), '_' , 0 ); // Do not remove accents
$filenameto = dol_sanitizeFileName ( GETPOST ( 'renamefileto' , 'alpha' ), '_' , 0 ); // Do not remove accents
2017-11-18 15:41:30 +01:00
2018-02-13 13:55:36 +01:00
if ( $filenamefrom != $filenameto )
2016-09-07 01:54:16 +02:00
{
2018-02-13 13:55:36 +01:00
// Security:
// Disallow file with some extensions. We rename them.
// Because if we put the documents directory into a directory inside web root (very bad), this allows to execute on demand arbitrary code.
2019-05-21 15:13:25 +02:00
if ( isAFileWithExecutableContent ( $filenameto ) && empty ( $conf -> global -> MAIN_DOCUMENT_IS_OUTSIDE_WEBROOT_SO_NOEXE_NOT_REQUIRED ))
2018-02-13 13:55:36 +01:00
{
$filenameto .= '.noexe' ;
}
2017-06-18 21:52:51 +02:00
2018-02-13 13:55:36 +01:00
if ( $filenamefrom && $filenameto )
{
$srcpath = $upload_dir . '/' . $filenamefrom ;
$destpath = $upload_dir . '/' . $filenameto ;
2018-03-06 09:20:45 +01:00
$reshook = $hookmanager -> initHooks ( array ( 'actionlinkedfiles' ));
$parameters = array ( 'filenamefrom' => $filenamefrom , 'filenameto' => $filenameto , 'upload_dir' => $upload_dir );
$reshook = $hookmanager -> executeHooks ( 'renameUploadedFile' , $parameters , $object );
if ( empty ( $reshook ))
2018-02-13 13:55:36 +01:00
{
2018-03-06 09:20:45 +01:00
if ( ! file_exists ( $destpath ))
2018-02-13 13:55:36 +01:00
{
2018-03-06 09:20:45 +01:00
$result = dol_move ( $srcpath , $destpath );
if ( $result )
{
2018-10-19 12:47:59 +02:00
// Define if we have to generate thumbs or not
$generatethumbs = 1 ;
2018-12-17 20:19:11 +01:00
// When we rename a file from the file manager in ecm, we must not regenerate thumbs (not a problem, we do pass here)
// When we rename a file from the website module, we must not regenerate thumbs (module = medias in such a case)
// but when we rename from a tab "Documents", we must regenerate thumbs
if ( GETPOST ( 'modulepart' ) == 'medias' ) $generatethumbs = 0 ;
2018-10-19 12:47:59 +02:00
if ( $generatethumbs )
2018-03-06 09:20:45 +01:00
{
2018-12-17 20:19:11 +01:00
if ( $object -> id )
2018-10-19 12:47:59 +02:00
{
$object -> addThumbs ( $destpath );
}
2018-02-13 13:55:36 +01:00
2018-10-19 12:47:59 +02:00
// TODO Add revert function of addThumbs to remove thumbs with old name
//$object->delThumbs($srcpath);
}
2018-02-13 13:55:36 +01:00
2018-03-06 09:20:45 +01:00
setEventMessages ( $langs -> trans ( " FileRenamed " ), null );
}
else
{
$langs -> load ( " errors " ); // key must be loaded because we can't rely on loading during output, we need var substitution to be done now.
setEventMessages ( $langs -> trans ( " ErrorFailToRenameFile " , $filenamefrom , $filenameto ), null , 'errors' );
}
}
else
{
$langs -> load ( " errors " ); // key must be loaded because we can't rely on loading during output, we need var substitution to be done now.
setEventMessages ( $langs -> trans ( " ErrorDestinationAlreadyExists " , $filenameto ), null , 'errors' );
}
2018-02-13 13:55:36 +01:00
}
}
2016-09-07 01:54:16 +02:00
}
}
2018-02-13 13:55:36 +01:00
// Update properties in ECM table
if ( GETPOST ( 'ecmfileid' , 'int' ) > 0 )
{
$shareenabled = GETPOST ( 'shareenabled' , 'alpha' );
include_once DOL_DOCUMENT_ROOT . '/ecm/class/ecmfiles.class.php' ;
$ecmfile = new EcmFiles ( $db );
$result = $ecmfile -> fetch ( GETPOST ( 'ecmfileid' , 'int' ));
if ( $result > 0 )
{
if ( $shareenabled )
{
if ( empty ( $ecmfile -> share ))
{
require_once DOL_DOCUMENT_ROOT . '/core/lib/security2.lib.php' ;
$ecmfile -> share = getRandomPassword ( true );
}
}
else
{
$ecmfile -> share = '' ;
}
$result = $ecmfile -> update ( $user );
if ( $result < 0 )
{
setEventMessages ( $ecmfile -> error , $ecmfile -> errors , 'warnings' );
}
}
}
2016-09-07 01:54:16 +02:00
}