Add option THIRDPARTY_LOGO_ALLOW_EXTERNAL_DOWNLOAD for logo indexing

This commit is contained in:
Laurent Destailleur 2018-02-23 19:55:15 +01:00
parent 7cf7bf6e45
commit d8bf553ea9
4 changed files with 140 additions and 41 deletions

View File

@ -4116,6 +4116,7 @@ abstract class CommonObject
* @param int $hideref 1 to hide product reference. 0 by default
* @param null|array $moreparams Array to provide more information
* @return int >0 if OK, <0 if KO
* @see addFileIntoDatabaseIndex
*/
protected function commonGenerateDocument($modelspath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams=null)
{
@ -4354,6 +4355,7 @@ abstract class CommonObject
/**
* Build thumb
* @TODO Move this into files.lib.php
*
* @param string $file Path file in UTF8 to original file to create thumbs from.
* @return void

View File

@ -950,7 +950,7 @@ function dolCheckVirus($src_file)
* Note:
* - This function can be used only into a HTML page context. Use dol_move if you are outside.
* - Test on antivirus is always done (if antivirus set).
* - Database of files is NOT updated.
* - Database of files is NOT updated (this is done by dol_add_file_process() that calls this function).
*
* @param string $src_file Source full path filename ($_FILES['field']['tmp_name'])
* @param string $dest_file Target full path filename ($_FILES['field']['name'])
@ -1554,28 +1554,10 @@ function dol_add_file_process($upload_dir, $allowoverwrite=0, $donotupdatesessio
// Update table of files
if ($donotupdatesession)
{
$rel_dir = preg_replace('/^'.preg_quote(DOL_DATA_ROOT,'/').'/', '', $upload_dir);
if (! preg_match('/[\\/]temp[\\/]|[\\/]thumbs|\.meta$/', $rel_dir)) // If not a tmp dir
$result = addFileIntoDatabaseIndex($upload_dir, basename($destfile), $TFile['name'][$i], 'uploaded', 0);
if ($result < 0)
{
$filename = basename($destfile);
$rel_dir = preg_replace('/[\\/]$/', '', $rel_dir);
$rel_dir = preg_replace('/^[\\/]/', '', $rel_dir);
include_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmfiles.class.php';
$ecmfile=new EcmFiles($db);
$ecmfile->filepath = $rel_dir;
$ecmfile->filename = $filename;
$ecmfile->label = md5_file(dol_osencode($destfull)); // MD5 of file content
$ecmfile->fullpath_orig = $TFile['name'][$i];
$ecmfile->gen_or_uploaded = 'uploaded';
$ecmfile->description = ''; // indexed content
$ecmfile->keyword = ''; // keyword content
$result = $ecmfile->create($user);
if ($result < 0)
{
setEventMessages($ecmfile->error, $ecmfile->errors, 'warnings');
}
setEventMessages('FailedToAddFileIntoDatabaseIndex', '', 'warnings');
}
}
@ -1679,6 +1661,114 @@ function dol_remove_file_process($filenb,$donotupdatesession=0,$donotdeletefile=
}
}
/**
* Add a file into database index.
* Called by dol_add_file_process when uploading a file and on other cases.
* See also commonGenerateDocument that also add/update database index when a file is generated.
*
* @param string $dir Directory name (full real path without ending /)
* @param string $file File name
* @param string $fullpathorig Full path of origin for file (can be '')
* @param string $mode How file was created ('uploaded', 'generated', ...)
* @param int $setsharekey Set also the share key
* @return int <0 if KO, 0 if nothing done, >0 if OK
*/
function addFileIntoDatabaseIndex($dir, $file, $fullpathorig='', $mode='uploaded', $setsharekey=0)
{
global $db, $user;
$result = 0;
$rel_dir = preg_replace('/^'.preg_quote(DOL_DATA_ROOT,'/').'/', '', $dir);
if (! preg_match('/[\\/]temp[\\/]|[\\/]thumbs|\.meta$/', $rel_dir)) // If not a tmp dir
{
$filename = basename($file);
$rel_dir = preg_replace('/[\\/]$/', '', $rel_dir);
$rel_dir = preg_replace('/^[\\/]/', '', $rel_dir);
include_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmfiles.class.php';
$ecmfile=new EcmFiles($db);
$ecmfile->filepath = $rel_dir;
$ecmfile->filename = $filename;
$ecmfile->label = md5_file(dol_osencode($dir.'/'.$file)); // MD5 of file content
$ecmfile->fullpath_orig = $fullpathorig;
$ecmfile->gen_or_uploaded = $mode;
$ecmfile->description = ''; // indexed content
$ecmfile->keyword = ''; // keyword content
if ($setsharekey)
{
require_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
$ecmfile->share = getRandomPassword(true);
}
$result = $ecmfile->create($user);
if ($result < 0)
{
dol_syslog($ecmfile->error);
}
}
return $result;
}
/**
* Delete files into database index using search criterias.
*
* @param string $dir Directory name (full real path without ending /)
* @param string $file File name
* @param string $mode How file was created ('uploaded', 'generated', ...)
* @return int <0 if KO, 0 if nothing done, >0 if OK
*/
function deleteFilesIntoDatabaseIndex($dir, $file, $mode='uploaded')
{
global $conf, $db, $user;
$error = 0;
if (empty($dir))
{
dol_syslog("deleteFilesIntoDatabaseIndex: dir parameter can't be empty", LOG_ERR);
return -1;
}
$db->begin();
$rel_dir = preg_replace('/^'.preg_quote(DOL_DATA_ROOT,'/').'/', '', $dir);
$filename = basename($file);
$rel_dir = preg_replace('/[\\/]$/', '', $rel_dir);
$rel_dir = preg_replace('/^[\\/]/', '', $rel_dir);
if (! $error)
{
$sql = 'DELETE FROM ' . MAIN_DB_PREFIX . 'ecm_files';
$sql.= ' WHERE entity = '.$conf->entity;
$sql.= " AND filepath = '" . $db->escape($rel_dir) . "'";
if ($file) $sql.= " AND filename = '" . $db->escape($file) . "'";
if ($mode) $sql.= " AND gen_or_uploaded = '" . $db->escape($mode) . "'";
$resql = $db->query($sql);
if (!$resql)
{
$error++;
dol_syslog(__METHOD__ . ' ' . $db->lasterror(), LOG_ERR);
}
}
// Commit or rollback
if ($error) {
$db->rollback();
return - 1 * $error;
} else {
$db->commit();
return 1;
}
}
/**
* Convert an image file into another format.
* This need Imagick php extension.

View File

@ -713,13 +713,18 @@ if (empty($reshook))
}
else
{
// Create small thumbs for company (Ratio is near 16/9)
// Used on logon for example
$imgThumbSmall = vignette($newfile, $maxwidthsmall, $maxheightsmall, '_small', $quality);
// Create thumbs
$object->addThumbs($newfile);
// Create mini thumbs for company (Ratio is near 16/9)
// Used on menu or for setup page for example
$imgThumbMini = vignette($newfile, $maxwidthmini, $maxheightmini, '_mini', $quality);
// Index file in database
if (! empty($conf->global->THIRDPARTY_LOGO_ALLOW_EXTERNAL_DOWNLOAD))
{
require_once DOL_DOCUMENT_ROOT .'/core/lib/files.lib.php';
// the dir dirname($newfile) is directory of logo, so we should have only one file at once into index, so we delete indexes for the dir
deleteFilesIntoDatabaseIndex(dirname($newfile), '', '', 'uploaded', 1);
// now we index the uploaded logo file
addFileIntoDatabaseIndex(dirname($newfile), basename($newfile), '', 'uploaded', 1);
}
}
}
}

View File

@ -1121,10 +1121,11 @@ class Societe extends CommonObject
* @param string $idprof4 Prof id 4 of third party (Warning, this can return several records)
* @param string $idprof5 Prof id 5 of third party (Warning, this can return several records)
* @param string $idprof6 Prof id 6 of third party (Warning, this can return several records)
* @param string $email Email (Warning, this can return several records)
* @param string $email Email of third party (Warning, this can return several records)
* @param string $ref_alias Name_alias of third party (Warning, this can return several records)
* @return int >0 if OK, <0 if KO or if two records found for same ref or idprof, 0 if not found.
*/
function fetch($rowid, $ref='', $ref_ext='', $ref_int='', $idprof1='',$idprof2='',$idprof3='',$idprof4='',$idprof5='',$idprof6='', $email='')
function fetch($rowid, $ref='', $ref_ext='', $ref_int='', $idprof1='',$idprof2='',$idprof3='',$idprof4='',$idprof5='',$idprof6='', $email='', $ref_alias='')
{
global $langs;
global $conf;
@ -1166,17 +1167,18 @@ class Societe extends CommonObject
$sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_incoterms as i ON s.fk_incoterms = i.rowid';
$sql .= ' WHERE s.entity IN ('.getEntity($this->element).')';
if ($rowid) $sql .= ' AND s.rowid = '.$rowid;
if ($ref) $sql .= " AND s.nom = '".$this->db->escape($ref)."'";
if ($ref_ext) $sql .= " AND s.ref_ext = '".$this->db->escape($ref_ext)."'";
if ($ref_int) $sql .= " AND s.ref_int = '".$this->db->escape($ref_int)."'";
if ($idprof1) $sql .= " AND s.siren = '".$this->db->escape($idprof1)."'";
if ($idprof2) $sql .= " AND s.siret = '".$this->db->escape($idprof2)."'";
if ($idprof3) $sql .= " AND s.ape = '".$this->db->escape($idprof3)."'";
if ($idprof4) $sql .= " AND s.idprof4 = '".$this->db->escape($idprof4)."'";
if ($idprof5) $sql .= " AND s.idprof5 = '".$this->db->escape($idprof5)."'";
if ($idprof6) $sql .= " AND s.idprof6 = '".$this->db->escape($idprof6)."'";
if ($email) $sql .= " AND s.email = '".$this->db->escape($email)."'";
if ($rowid) $sql .= ' AND s.rowid = '.$rowid;
if ($ref) $sql .= " AND s.nom = '".$this->db->escape($ref)."'";
if ($ref_alias) $sql .= " AND s.nom_alias = '".$this->db->escape($nom_alias)."'";
if ($ref_ext) $sql .= " AND s.ref_ext = '".$this->db->escape($ref_ext)."'";
if ($ref_int) $sql .= " AND s.ref_int = '".$this->db->escape($ref_int)."'";
if ($idprof1) $sql .= " AND s.siren = '".$this->db->escape($idprof1)."'";
if ($idprof2) $sql .= " AND s.siret = '".$this->db->escape($idprof2)."'";
if ($idprof3) $sql .= " AND s.ape = '".$this->db->escape($idprof3)."'";
if ($idprof4) $sql .= " AND s.idprof4 = '".$this->db->escape($idprof4)."'";
if ($idprof5) $sql .= " AND s.idprof5 = '".$this->db->escape($idprof5)."'";
if ($idprof6) $sql .= " AND s.idprof6 = '".$this->db->escape($idprof6)."'";
if ($email) $sql .= " AND s.email = '".$this->db->escape($email)."'";
$resql=$this->db->query($sql);
if ($resql)