From a2a31c8b3fc7e03af446c1679373aa8aa685b0f8 Mon Sep 17 00:00:00 2001 From: sparkyx Date: Fri, 29 Jan 2021 10:50:34 +0100 Subject: [PATCH] =?UTF-8?q?New=20:=200001890:=20Avertissement=20si=20la=20?= =?UTF-8?q?document=20=C3=A0=20t=C3=A9l=C3=A9charger=20est=20trop=20gros,?= =?UTF-8?q?=20on=20ne=20peut=20soumettre=20le=20fichier?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- html/js/scripts.js | 45 +++++++++++++++++ include/action.common.inc.php | 3 +- include/ajax/ajax_ledger.php | 56 ++++++++++----------- include/class/acc_ledger_fin.class.php | 2 +- include/class/acc_ledger_purchase.class.php | 1 + include/class/acc_ledger_sold.class.php | 1 + include/class/fiche.class.php | 2 +- include/class/follow_up.class.php | 1 + include/config.inc.example | 2 + include/constant.php | 7 ++- include/lib/ac_common.php | 2 + include/lib/ifile.class.php | 46 ++++++++++++++++- include/lib/message_javascript.php | 2 + include/operation_ods_confirm.inc.php | 1 + include/template/detail-action.php | 9 ++++ scenario/HtmlInput.test.php | 34 +++++++++++++ 16 files changed, 180 insertions(+), 34 deletions(-) diff --git a/html/js/scripts.js b/html/js/scripts.js index d068eaf04..04cc00b1c 100644 --- a/html/js/scripts.js +++ b/html/js/scripts.js @@ -3955,3 +3955,48 @@ var operation_tag = function (p_div) }); }; }; + +/** + * Check the sum of size of all the FILES to upload + * @param p_object the form DOM object, + * @param p_max_size MAX_FILE_SIZE constant (see config.inc.php or constant.php) + * @returns true if the sum of filesize is greater than the limit + */ +function check_file_size(p_object,p_max_size) +{ + var sum_file=0; + for(var i=0;i p_max_size) {alert_box(content[78]);return false;} + return true; +} + +/** + * Check that the receipt file is not too big + * @see ajax_ledger.php , ledger_detail_file + * @param int p_max_size maximum size + * @param p_info name of the waiting box + * @returns true if file size is less than the maximum + */ +function check_receipt_size(p_max_size,p_info) +{ + document.getElementById(p_info).style.display="inline"; + console.debug ("param p_max_file_size"+p_max_size); + var f=document.getElementById("receipt_id"); + if ( f && f.files[0] && f.files[0].size > parseFloat(p_max_size)) { + document.getElementById("receipt_info_id").innerHTML=content[78]; + document.getElementById(p_info).style.display="none"; + return false; + } + document.getElementById("receipt_info_id").innerHTML=""; + document.getElementById("form_file").submit(); + return true; +} \ No newline at end of file diff --git a/include/action.common.inc.php b/include/action.common.inc.php index 4e568e55d..9c5a25e47 100644 --- a/include/action.common.inc.php +++ b/include/action.common.inc.php @@ -225,7 +225,8 @@ if ($sub_action == 'detail') if ($g_user->can_write_action($ag_id) == true) { - echo '
'; + printf( '',MAX_FILE_SIZE); echo $supl_hidden; echo HtmlInput::hidden('ac', $http->request('ac')); echo dossier::hidden(); diff --git a/include/ajax/ajax_ledger.php b/include/ajax/ajax_ledger.php index 40cd7c486..d9ac3ec99 100644 --- a/include/ajax/ajax_ledger.php +++ b/include/ajax/ajax_ledger.php @@ -224,8 +224,7 @@ case 'file': $obj=$op->get_quant(); /* return an obj. ACH / FIN or VEN or null if nothing is found*/ $repo = new Database(); - $theme = $repo->get_value("select the_filestyle from theme where the_name=$1", array($_SESSION[SESSION_KEY.'g_theme'])); - html_min_page_start($theme); + html_min_page_start($_SESSION[SESSION_KEY.'g_theme']); // if there is a receipt document if ( $obj->det->jr_pj_name=='') @@ -238,7 +237,9 @@ case 'file': } if ( $access=='W') { - echo ''; + $check_receipt=sprintf("check_receipt_size('%s','file%s')", + MAX_FILE_SIZE,$div); + echo ''; $sp=new ISpan('file'.$div); $sp->style="display:none;background-color:red;color:white;font-size:12px"; @@ -248,8 +249,10 @@ case 'file': echo dossier::hidden(); echo HtmlInput::hidden('jr_id',$jr_id); echo HtmlInput::hidden('div',$div); + echo ''; + + echo '

'; - echo ''; echo '
'; } else @@ -285,17 +288,20 @@ case 'file': { // Not possible to remove the file thanks a modal dialog box, // because of the frameset - $x=sprintf(''."x".'', - $gDossier,$div,$jr_id); - + + $x=Icon_Action::trash(uniqid(), + sprintf("if (confirm(content[47])) {document.location.href='ajax_misc.php?op=ledger&gDossier=%d&div=%s&jr_id=%s&act=rmf'}", + $gDossier,$div,$jr_id)); + } $filename= $obj->det->jr_pj_name; - if ( strlen($obj->det->jr_pj_name) > 20 ) + if ( strlen($obj->det->jr_pj_name) > 60 ) { - $filename=mb_substr($obj->det->jr_pj_name,0,23); + $filename=mb_substr($obj->det->jr_pj_name,0,60); } echo HtmlInput::show_receipt_document($jr_id,h($filename)); echo $x; + echo '

'; echo ''; echo ''; exit(); @@ -314,29 +320,24 @@ case 'loadfile': // Show a link to the new file $op->get(); $obj=$op->get_quant(); /* return an obj. ACH / FIN or VEN or null if nothing is found*/ - - echo ""; - $repo=new Database(); - $theme=$repo->get_value("select the_filestyle from theme where the_name=$1",array($_SESSION[SESSION_KEY.'g_theme'])); - echo " "; - echo ""; + html_min_page_start($_SESSION[SESSION_KEY.'g_theme']); if ( ! isset($_REQUEST['ajax']) ) echo ""; else echo ""; - echo "

"._("Document")."

"; echo '
'; - + $x=""; // check if the user can remove a document if ($g_user->check_action (RMRECEIPT) == 1) { // Not possible to remove the file thanks a modal dialog box, // because of the frameset - $x=sprintf(''.SMALLX.'', - $gDossier,$div,$jr_id); - echo $x; + $x=Icon_Action::trash(uniqid(), + sprintf("if (confirm(content[47])) {document.location.href='ajax_misc.php?op=ledger&gDossier=%d&div=%s&jr_id=%s&act=rmf'}", + $gDossier,$div,$jr_id)); } $filename= $obj->det->jr_pj_name; echo HtmlInput::show_receipt_document($jr_id,h($filename)); + echo $x; echo '
'; - + echo ''; } exit(); ///////////////////////////////////////////////////////////////////////////// @@ -345,16 +346,14 @@ case 'loadfile': case 'rmf': if ( $access == 'W' && $g_user->check_action (RMRECEIPT) == 1) { - echo ""; $repo=new Database(); - $theme=$repo->get_value("select the_filestyle from theme where the_name=$1",array($_SESSION[SESSION_KEY.'g_theme'])); - echo " "; - echo ""; - echo "

"._("Document")."

"; + html_min_page_start($_SESSION[SESSION_KEY.'g_theme']); echo '
'; + $check_receipt=sprintf("check_receipt_size('%s','file%s')", + MAX_FILE_SIZE,$div); echo '
'; $sp=new ISpan('file'.$div); - $sp->style="display:none;width:155;height:15;background-color:red;color:white;font-size:10"; + $sp->style="display:none;width:155px;height:15px;background-color:red;color:white;font-size:10px"; $sp->value=_("Chargement"); echo $sp->input(); @@ -363,7 +362,8 @@ case 'rmf': echo HtmlInput::hidden('jr_id',$jr_id); echo HtmlInput::hidden('div',$div); - echo ''; + echo ''; + echo '

'; echo '
'; $ret=$cn->exec_sql("select jr_pj from jrn where jr_id=$1",array($jr_id)); if (Database::num_row($ret) != 0) diff --git a/include/class/acc_ledger_fin.class.php b/include/class/acc_ledger_fin.class.php index 50db84369..de48fcb16 100644 --- a/include/class/acc_ledger_fin.class.php +++ b/include/class/acc_ledger_fin.class.php @@ -648,7 +648,7 @@ class Acc_Ledger_Fin extends Acc_Ledger $r.=''; // check for upload piece $file = new IFile(); - + $file->setAlertOnSize(true); $r.="
"._("Ajoutez une pièce justificative")." "; $r.=$file->input("pj", ""); diff --git a/include/class/acc_ledger_purchase.class.php b/include/class/acc_ledger_purchase.class.php index d624af02c..a20209d4e 100644 --- a/include/class/acc_ledger_purchase.class.php +++ b/include/class/acc_ledger_purchase.class.php @@ -1752,6 +1752,7 @@ EOF; $r.='

'; // check for upload piece $file=new IFile(); + $file->setAlertOnSize(true); $file->table=0; $r.=_("Ajoutez une pièce justificative "); $r.=$file->input("pj",""); diff --git a/include/class/acc_ledger_sold.class.php b/include/class/acc_ledger_sold.class.php index 27ec56adf..3074f76b2 100644 --- a/include/class/acc_ledger_sold.class.php +++ b/include/class/acc_ledger_sold.class.php @@ -988,6 +988,7 @@ EOF; // check for upload piece $file = new IFile(); $file->table = 0; + $file->setAlertOnSize(true); $r.='

'; $r.=_("Ajoutez une pièce justificative "); $r.=$file->input("pj", ""); diff --git a/include/class/fiche.class.php b/include/class/fiche.class.php index 12f7828e6..ba01e63a5 100644 --- a/include/class/fiche.class.php +++ b/include/class/fiche.class.php @@ -1899,7 +1899,7 @@ class Fiche $odd=""; $odd = ($i % 2 == 0 ) ? ' odd ': ' even '; $accounting=$tiers->strAttribut(ATTR_DEF_ACCOUNT); - if ( $p_action == 'bank' && $amount['debit'] < $amount['credit'] ){ + if ( ! empty($accounting) && $p_action == 'bank' && $amount['debit'] < $amount['credit'] ){ if ( strpos($accounting,$bank->p_value)===0 || strpos($accounting,$cash->p_value)===0 || strpos($accounting,$cc->p_value)===0){ //put in red if c>d $odd.=" notice "; diff --git a/include/class/follow_up.class.php b/include/class/follow_up.class.php index 1be594160..e05570689 100644 --- a/include/class/follow_up.class.php +++ b/include/class/follow_up.class.php @@ -401,6 +401,7 @@ class Follow_Up /* for new files */ $upload=new IFile(); $upload->name="file_upload[]"; + $upload->setAlertOnSize(true); $upload->readOnly=$readonly; $upload->value=""; $aAttachedFile=$this->db->get_array('select d_id,d_filename,d_description,d_mimetype,'. diff --git a/include/config.inc.example b/include/config.inc.example index e1be7d1be..56bbdff52 100644 --- a/include/config.inc.example +++ b/include/config.inc.example @@ -72,3 +72,5 @@ define ("dbname",""); // $pdftk = /snap/bin/pdftk // Define a random session key if you work with different version of NOALYSS // define ('SESSION_KEY','abcde'); +// Max size is defined by default to 2MB, it could be also needed to modify PHP Ini file +// define ("MAX_FILE_SIZE",2097152); \ No newline at end of file diff --git a/include/constant.php b/include/constant.php index 5e9302f8a..a76070aef 100644 --- a/include/constant.php +++ b/include/constant.php @@ -228,8 +228,11 @@ define ("FICHE_TYPE_ACH_SER",3); define ("FICHE_TYPE_ACH_MAT",7); define ("FICHE_TYPE_PROJET",26); define ("FICHE_TYPE_MATERIAL",7); - -/** +// Max size is defined by default to 2MB, +if ( ! defined("MAX_FILE_SIZE")) { + define ("MAX_FILE_SIZE",2097152); +} +/** * -- pour utiliser unoconv démarrer un server libreoffice * commande * libreoffice --headless --accept="socket,host=127.0.0.1,port=2002;urp;" --nofirststartwizard diff --git a/include/lib/ac_common.php b/include/lib/ac_common.php index e4a09c26e..6f27cdf30 100644 --- a/include/lib/ac_common.php +++ b/include/lib/ac_common.php @@ -421,9 +421,11 @@ function html_min_page_start($p_theme="", $p_script="", $p_script2="") " . $p_script2 . " + "; + include_once NOALYSS_INCLUDE.'/lib/message_javascript.php'; echo ' '; diff --git a/include/lib/ifile.class.php b/include/lib/ifile.class.php index f5e8a3766..b952f49f4 100644 --- a/include/lib/ifile.class.php +++ b/include/lib/ifile.class.php @@ -25,13 +25,57 @@ require_once NOALYSS_INCLUDE.'/lib/html_input.class.php'; class IFile extends HtmlInput { + // if true , the size is tested and a box is displaid + private $alert_on_size; + function __construct($p_name = "", $p_value = "", $p_id = "") + { + parent::__construct($p_name, $p_value, $p_id); + $this->alert_on_size=false; + } + + /** + * @return false + */ + public function getAlertOnSize(): bool + { + return $this->alert_on_size; + } + + /** + * if true , the size is tested and a box is displaid + * @param false $alert_on_size + */ + public function setAlertOnSize(bool $alert_on_size): void + { + $this->alert_on_size = $alert_on_size; + } + /*!\brief show the html input of the widget*/ public function input($p_name=null,$p_value=null) { $this->name=($p_name==null)?$this->name:$p_name; $this->value=($p_value==null)?$this->value:$p_value; if ( $this->readOnly==true) return $this->display(); - $r=''; + if ($this->id=="") $this->id=uniqid("file_"); + $r=sprintf('', + $this->name, + $this->id, + $this->value); + if ( $this->alert_on_size) + { + $max_size=MAX_FILE_SIZE; + $max_size_mb=round($max_size / 1024 /1024,2); + $too_large=h(_("Fichier trop grand(max = $max_size_mb MB)")); + + $js_check_size=sprintf(' + document.getElementById("%s").addEventListener("change",function () + { + var fFile=document.getElementById("%s"); + if (fFile.files[0] && fFile.files[0].size>%s) { smoke.alert("%s");} + });',$this->id,$this->id,$max_size,$too_large); + $r.=create_script($js_check_size); + + } return $r; } diff --git a/include/lib/message_javascript.php b/include/lib/message_javascript.php index 74d816f30..75dd82dd6 100644 --- a/include/lib/message_javascript.php +++ b/include/lib/message_javascript.php @@ -109,4 +109,6 @@ content[74]=""; content[76]=""; content[77]=""; + +content[78]=""; diff --git a/include/operation_ods_confirm.inc.php b/include/operation_ods_confirm.inc.php index e29348e5a..666d7059a 100644 --- a/include/operation_ods_confirm.inc.php +++ b/include/operation_ods_confirm.inc.php @@ -71,6 +71,7 @@ echo $ledger->confirm($_POST,false);