phpDocumentor VFront
Administration
[ class tree: VFront ] [ index: VFront ] [ all elements ]

Source for file log.php

Documentation is available at log.php

  1. <?php
  2. /**
  3.  * Il questo file sono presenti funzioni e procedure per la gestione del log.
  4.  * E' possibile vedere la tabella dei log, impostare i filtri, eseguire operazioni di rollback, ecc.
  5.  * 
  6.  * @desc File di gestione dei log
  7.  * @package VFront
  8.  * @subpackage Administration
  9.  * @author M.Marcello Verona
  10.  * @copyright 2007 M.Marcello Verona
  11.  * @version 0.90
  12.  * @license http://www.gnu.org/licenses/gpl.html GNU Public License
  13.  */
  14.  
  15.  
  16. include("../inc/conn.php");
  17. include("../inc/layouts.php");
  18. include("../inc/func.comuni.php");
  19. include("../inc/func.frontend.php");
  20.  
  21.  proteggi(2);
  22.  
  23.  
  24.  
  25.  
  26. /**
  27.  * Funzione di ripristino.
  28.  * Mediante questa funzione è possibile eseguire rollback di operazioni di DELETE e UPDATE
  29.  *
  30.  * @todo Fare verifiche sul corretto funzionamento in ambito Postgres
  31.  * @param int $id_log ID dell'operazione nel log su cui operare
  32.  */
  33. function ripristina($id_log){
  34.     
  35.     global $link,$db1;
  36.     
  37.     // prendi il log
  38.     $q_log=vmsql_query("SELECT op,storico_pre,storico_post,tabella,id_record FROM {$db1['frontend']}.log WHERE id_log=$id_log",$link);
  39.     
  40.     if(vmsql_num_rows($q_log)!=1){
  41.         openErrorGenerico("Nessun riferimento nell'operazione di ripristino");
  42.         exit;
  43.     }
  44.     
  45.     
  46.     list($op,$storico_pre,$storico_post,$tabella,$id_record)=vmsql_fetch_row($q_log);
  47.     
  48.     ##############################
  49.     #    
  50.     #    RIPRISTINO UPDATE
  51.     #
  52.         
  53.     if($op=='update'){
  54.         
  55.         $array_pre unserialize($storico_pre);
  56.         
  57.         
  58.         
  59.         if(is_array($array_pre)){
  60.             
  61.             $sql_update="UPDATE $tabella SET ";
  62.             
  63.             // PRENDI LA DOCUMENTAZIONE DELLA TABELLA (serve per esprimere i valori null in caso di int o float o double
  64.             list($info_column_name,$info_data_type)=prendi_colonne_frontend($tabella,"column_name,data_type",false,0);
  65.             
  66.             $info_cols=array();
  67.             
  68.             for($i=0;$i<count($info_data_type);$i++){
  69.                 $info_cols[$info_column_name[$i]]=$info_data_type[$i];
  70.             }
  71.             
  72.     
  73.             
  74.             
  75.             // ciclo sui valori
  76.             foreach($array_pre as $campo=>$val){
  77.                 
  78.                 if$info_cols[$campo]=='int' || 
  79.                     $info_cols[$campo]=='tinyint' ||  
  80.                     $info_cols[$campo]=='mediumint' || 
  81.                     $info_cols[$campo]=='double' ||
  82.                     $info_cols[$campo]=='float' 
  83.                     ){
  84.                         
  85.                         if($val=='' || $val==null){
  86.                             $valore="NULL";
  87.                         }
  88.                         else{
  89.                             $valore="'$val'";
  90.                         }
  91.                         
  92.                     }
  93.                     else{
  94.                         $valore="'".str_replace("'","\'",stripslashes($val))."'";
  95.                     }
  96.                 
  97.                 
  98.                 $sql_update.=" $campo=$valore,";
  99.             }
  100.             
  101.             // condizione
  102.             $campo_pk=prendi_PK($tabella);
  103.             
  104.             if($campo_pk==null){
  105.                 openErrorGenerico("Eccezione nella procedura: impossibile completare il ripristino (1)");
  106.                 exit;
  107.             }
  108.             
  109.             $sql_update=substr($sql_update,0,-1);
  110.             
  111.             $sql_update.= " WHERE $campo_pk='".addslashes($id_record)."'";
  112.             
  113.             
  114.             $q_rip=vmsql_query($sql_update,$link);
  115.             if(vmsql_affected_rows($link,$q_rip)==1){
  116.                 
  117.                 
  118.                 // INSERISCI NEL LOG
  119.                 rpc_log('ripristino',$tabella,$_SESSION['user']['uid'],$_SESSION['gid'],$id_record,true);
  120.                 header("Location: ".$_SERVER['PHP_SELF']."?id_record=$id_record&feed=ok");
  121.                 exit;
  122.             }
  123.             else{
  124.                 header("Location: ".$_SERVER['PHP_SELF']."?id_record=$id_record&feed=ko");
  125.                 exit;
  126.             }
  127.             
  128.             
  129.             
  130.         }
  131.         else{
  132.             openErrorGenerico("Eccezione nella procedura: impossibile completare il ripristino (2)");
  133.             exit;
  134.             
  135.         }
  136.         
  137.     // -- FIne ripristino UPDATE
  138.  
  139.     else if($op=='delete'){
  140.         
  141.         $array_pre unserialize($storico_pre);
  142.         
  143.         // verifica se esiste un record con il codice del record.
  144.         // In pratica il record è rispristinabile se l'ID è libero (ad es. caso autoincrement)
  145.         
  146.         // condizione
  147.         $campo_pk=prendi_PK($tabella);
  148.         
  149.         $q_test vmsql_query("SELECT * FROM $tabella WHERE $campo_pk='$id_record'",$link);
  150.         
  151.         // se esiste esce con errore
  152.         if(vmsql_num_rows($q_test)>0){
  153.             openErrorGenerico("Impossibile ripristinare il record. La chiave primaria &egrave; usata da un altro record");
  154.             exit;
  155.         }
  156.         
  157.         
  158.         $sql_insert="INSERT INTO $tabella ";
  159.         
  160.         $sql_campi="";
  161.         $sql_valori="";
  162.         
  163.         // PRENDI LA DOCUMENTAZIONE DELLA TABELLA (serve per esprimere i valori null in caso di int o float o double
  164.         list($info_column_name,$info_data_type)=prendi_colonne_frontend($tabella,"column_name,data_type",false,0);
  165.         
  166.         $info_cols=array();
  167.         
  168.         for($i=0;$i<count($info_data_type);$i++){
  169.             $info_cols[$info_column_name[$i]]=$info_data_type[$i];
  170.         }
  171.     
  172.             
  173.         
  174.             // ciclo sui valori
  175.             foreach($array_pre as $campo=>$val){
  176.                 
  177.                 if$info_cols[$campo]=='int' || 
  178.                     $info_cols[$campo]=='tinyint' ||  
  179.                     $info_cols[$campo]=='mediumint' || 
  180.                     $info_cols[$campo]=='double' ||
  181.                     $info_cols[$campo]=='float' ||
  182.                     $info_cols[$campo]=='date' ||
  183.                     $info_cols[$campo]=='datetime' ||
  184.                     $info_cols[$campo]=='timestamp' 
  185.                     ){
  186.                         
  187.                         if($val=='' || $val==null){
  188.                             $valore="NULL";
  189.                         }
  190.                         else{
  191.                             $valore="'$val'";
  192.                         }
  193.                         
  194.                     }
  195.                     else{
  196.                         $valore="'".str_replace("'","\'",stripslashes($val))."'";
  197.                     }
  198.                 
  199.                 
  200.                 $sql_campi.=$campo.",";
  201.                 $sql_valori.=$valore.",";
  202.             }
  203.         
  204.         
  205.         
  206.         $sql_campi=substr($sql_campi,0,-1);
  207.         $sql_valori=substr($sql_valori,0,-1);
  208.         
  209.         $sql_insert.= "($sql_campiVALUES ($sql_valori)";
  210.         
  211.         
  212.         // INIZIO TRANSAZIONE
  213.             
  214.             $q_rip=vmsql_query($sql_insert,$link);
  215.             if(vmsql_affected_rows($link,$q_rip)==1){
  216.                 
  217.                 
  218.                 // INSERISCI NEL LOG
  219.                 rpc_log('ripristino',$tabella,$_SESSION['user']['uid'],$_SESSION['gid'],$id_record,true);
  220.                 header("Location: ".$_SERVER['PHP_SELF']."?id_record=$id_record&feed=ok");
  221.                 exit;
  222.             }
  223.             else{
  224.                 header("Location: ".$_SERVER['PHP_SELF']."?id_record=$id_record&feed=ko");
  225.                 exit;
  226.             }
  227.     }
  228.     
  229.     
  230.     
  231.     
  232. }
  233.  
  234.  
  235. /**
  236.  * @desc Funzione che mostra la pagina con il log
  237.  *
  238.  */
  239. function mostra_log(){
  240.     
  241.     global $link,$db1;
  242.  
  243.     $files=array("sty/admin.css","sty/tabelle.css","js/mostra_nascondi_id.js","sty/log.css");
  244.     
  245.     $files[]="js/jscalendar/calendar.js";
  246.     $files[]="js/jscalendar/lang/calendar-it.js";
  247.     $files[]="js/jscalendar/calendar-setup.js";
  248.     $files[]="sty/jscalendar/calendar-win2k-cold-1.css";
  249.  
  250.      echo openLayout1("Log Database"$files);
  251.      
  252.     
  253.      echo "<div id=\"briciole\"><a href=\"index.php\">home amministrazione</a> &raquo; log database</div>\n";
  254.     
  255.      echo "<h1>Log operazioni sul database</h1>\n";
  256.     
  257.      $ORDER= isset($_GET['or']? (int) $_GET['or']"data";
  258.      
  259.      $SORT= isset($_GET['s']$_GET['or']"DESC";
  260.      
  261.      
  262.      
  263.      $PASSO 100;
  264.      
  265.      $colore_tab =" class=\"arancio\"";
  266.      
  267.      
  268.           
  269.               
  270.      $QS="";
  271.      
  272.      $val_op=array('insert'=>0,'update'=>0,'delete'=>0,'ripristino'=>0,'duplicazione'=>0);
  273.      
  274.      // Impostazioni per i filtraggi SQL 
  275.      if(isset($_GET['uid'])  && $_GET['uid']!=''){
  276.           $clausola_uid="AND log.uid='".intval($_GET['uid'])."'";
  277.           $class_uid=$colore_tab;
  278.           $val_uid=(int) $_GET['uid'];
  279.           $QS.="&uid=$val_uid";
  280.      else {
  281.           $clausola_uid='';  $class_uid='';  $val_uid='';
  282.      }
  283.     
  284.      if(isset($_GET['op']&& $_GET['op']!=''){
  285.           $clausola_op="AND log.op='".addslashes($_GET['op'])."'";
  286.           $class_op=$colore_tab;
  287.           $val_op[$_GET['op']]=1;
  288.           $QS.="&op=".$_GET['op'];
  289.      else {
  290.           $clausola_op=''$class_op='';   }
  291.      
  292.      
  293.      if(isset($_GET['data_dal']&& $_GET['data_dal']!='Tutte' && $_GET['data_dal']!=''){
  294.           $clausola_data1="AND log.data>'".addslashes($_GET['data_dal'])."'";
  295.           $class_data=$colore_tab;
  296.           $val_data1$_GET['data_dal'];
  297.           $QS.="&data_dal=".$_GET['data_dal'];
  298.      else {
  299.           $clausola_data1='';  $class_data=''$val_data1'Tutte';
  300.      }
  301.     
  302.      
  303.      if(isset($_GET['data_al']&& $_GET['data_al']!='Tutte' && $_GET['data_al']!=''){
  304.           $clausola_data2="AND log.data<'".addslashes($_GET['data_al'])."'";
  305.           $class_data=$colore_tab;
  306.           $val_data2$_GET['data_al'];
  307.           $QS.="&data_al=".$_GET['data_al'];
  308.      else {
  309.           $clausola_data2=''$class_data=''$val_data2='Tutte';
  310.      }
  311.     
  312.      
  313.      
  314.      if(isset($_GET['tabella']&& $_GET['tabella']!=''{
  315.           $clausola_tabella="AND log.tabella='".addslashes($_GET['tabella'])."'";
  316.           $class_tabella $colore_tab;
  317.           $val_tabella=$_GET['tabella'];
  318.           $QS.="&tabella=".$_GET['tabella'];
  319.      else$clausola_tabella=''$class_tabella=""$val_tabella='';  }
  320.      
  321.      // se non ci sono filtri nascondi le opzioni filtri mostra
  322.      $mostra_filtri ($clausola_uid $clausola_op $clausola_data1 $clausola_data2 $clausola_tabella == ''
  323.                      "display:none" "display:";
  324.          
  325.          
  326.      
  327.      
  328.      $clausola_istituto='';
  329.      
  330.      $LIMIT= isset($_GET['l']?   (int) $_GET['l']$PASSO;
  331.      $OFFSET= isset($_GET['of']? (int) $_GET['of']0;
  332.      
  333.      if($OFFSET<0$OFFSET=0;
  334.      
  335.      
  336.      
  337.      // Query Log
  338.      
  339.           $q_log_count=vmsql_query("SELECT count(*) 
  340.                                       FROM {$db1['frontend']}.log 
  341.                                       INNER JOIN {$db1['frontend']}.utente AS u ON u.id_utente=log.uid
  342.                                      INNER JOIN {$db1['frontend']}.gruppo AS g ON u.gid=g.gid
  343.                                       WHERE 1=1
  344.                                       $clausola_uid
  345.                                      $clausola_op
  346.                                      $clausola_data1
  347.                                      $clausola_data2
  348.                                      $clausola_tabella
  349.                                      ",$link);
  350.           list($TOTvmsql_fetch_row($q_log_count);
  351.      
  352.           
  353.          $LIMIT_SYNTAX limit_sintax($LIMIT,$OFFSET);
  354.           
  355.          $sql_log "SELECT log.id_log,
  356.                             log.op,
  357.                             log.tabella,
  358.                             log.data,
  359.                             log.uid,
  360.                             ".concat_sintax("log.gid, ' (',g.nome_gruppo,')'"'gruppo_desc').",
  361.                             log.id_record,
  362.                             log.fonte,
  363.                             ".concat_sintax("u.nome, ' ',u.cognome",  'nomecognome')."
  364.                             
  365.                                 
  366.                      FROM {$db1['frontend']}.log 
  367.                      INNER JOIN {$db1['frontend']}.utente AS u ON u.id_utente=log.uid
  368.                      INNER JOIN {$db1['frontend']}.gruppo AS g ON u.gid=g.gid
  369.                      
  370.                  WHERE 1=1
  371.                  $clausola_uid
  372.                  $clausola_op
  373.                  $clausola_data1
  374.                  $clausola_data2
  375.                  $clausola_tabella
  376.                  ORDER BY $ORDER $SORT
  377.                 $LIMIT_SYNTAX
  378.                  ";
  379.          
  380.          $q_log=vmsql_query($sql_log,$link);
  381.          
  382.          $inizio_set $OFFSET 1;
  383.          $fine_set=(($OFFSET+$LIMIT)<$TOT$OFFSET+$LIMIT $TOT;
  384.          
  385.          if($OFFSET>$TOT$OFFSET=$TOT;
  386.          
  387.      $str_filtrato (trim($clausola_uid.
  388.                          $clausola_op.
  389.                          $clausola_data1.
  390.                          $clausola_data2.
  391.                          $clausola_tabella.
  392.                          $clausola_istituto)!=''"<span class=\"grigio\">(filtrati)</span>" "";
  393.          
  394.      echo "<p>Operazioni $inizio_set - $fine_set di <strong>$TOT</strong$str_filtrato</p>\n";
  395.          
  396.      
  397.      $PAG"<div id=\"paginazione\">\n";
  398.  
  399.      
  400.      
  401.      
  402.      if($OFFSET-$PASSO >= 0){
  403.          
  404.          
  405.          
  406.           $PAG.= "<a href=\"".$_SERVER['PHP_SELF']."?of=".($OFFSET-$PASSO)$QS ."\">&lt; &lt; indietro</a>\n | ";
  407.      }
  408.      else{
  409.          
  410.           $PAG.= "<span class=\"pag\">&lt; &lt; indietro</span>\n | ";
  411.      }
  412.      
  413.      $n_pagine ceil($TOT/$PASSO);
  414.      
  415.      if($n_pagine>$PASSO){
  416.          $n_pagine=$PASSO;         
  417.      }
  418.      
  419.      if($n_pagine>1){
  420.      
  421.          for($i=0;$i<$n_pagine;$i++){
  422.              
  423.              if($OFFSET==$PASSO*$i){
  424.                 
  425.                  $PAG.= " ".($i+1)." \n | ";    
  426.              }
  427.              else{
  428.                  $PAG.= " <a href=\"".$_SERVER['PHP_SELF']."?of=".($PASSO*$i)$QS ."\">".($i+1)."</a>\n | ";             
  429.              }
  430.  
  431.              
  432.          }
  433.      
  434.      }
  435.      
  436.      if($OFFSET+$PASSO>= $TOT){
  437.          
  438.           $PAG.= "<span class=\"pag\">avanti &gt; &gt; </span>\n | ";
  439.      }
  440.      else{
  441.          $PAG.= "<a href=\"".$_SERVER['PHP_SELF']."?of=".($OFFSET+$PASSO).  $QS ."\">avanti &gt; &gt; </a>\n | ";
  442.           
  443.      }
  444.      
  445.      
  446.      $PAG=substr($PAG,0,-2);
  447.      
  448.      $PAG.= "</div><br />\n";
  449.      
  450.      
  451.      
  452.     
  453.      
  454.      
  455.      
  456.      // FILTRI
  457.      
  458.      // PRENDI LE TABELLE ESISTENTI SUL LOG
  459.      $q_tab_log=vmsql_query("SELECT DISTINCT tabella FROM {$db1['frontend']}.log ORDER BY tabella",$link);
  460.      
  461.      list($tabelle)=vmsql_fetch_row_all($q_tab_log,true);
  462.      // -- fine tabelle
  463.      
  464.       // PRENDI GLI UTENTI ESISTENTI SUL LOG
  465.      $q_tab_ut=vmsql_query("SELECT DISTINCT log.uid, ".concat_sintax("log.uid,' - ',u.cognome,' ',u.nome"'uidnomecognome')."
  466.                              FROM {$db1['frontend']}.log , {$db1['frontend']}.utente as u
  467.                              WHERE u.id_utente=log.uid 
  468.                              ORDER BY uidnomecognome",$link);
  469.      
  470.      list($id_utenti$utenti)=vmsql_fetch_row_all($q_tab_ut,true);
  471.      // -- fine tabelle
  472.      
  473.      
  474.      $FILTRI"<p><span class=\"fakelink\" onclick=\"mostra_nascondi('filtri_log');\"><strong>Filtri sul log</strong></span></p>\n";
  475.      
  476.      
  477.      
  478.      
  479.      
  480.       $FILTRI.= "
  481.          <div id=\"filtri_log\" style=\"$mostra_filtri;\">
  482.      
  483.          <form action=\"".$_SERVER['PHP_SELF']."\" method=\"get\">
  484.              <fieldset style=\"margin:5px 20px 20px 0px; width:60%;\">
  485.                  <label for=\"op\">Tipo di operazione:</label>
  486.                  <select name=\"op\" id=\"op\">
  487.                      <option value=\"\">Tutte le operazioni</option>\n";
  488.       
  489.         $ar_op=array_keys($val_op);
  490.       
  491.           for($i=0;$i<count($ar_op);$i++){
  492.               
  493.               $sel_op ($val_op[$ar_op[$i]]==1"selected=\"selected\"" "";
  494.               
  495.                   $FILTRI.= "
  496.                   <option value=\"".$ar_op[$i]."\" $sel_op>".$ar_op[$i]."</option>
  497.                   ";
  498.           }
  499.  
  500.           $FILTRI.=  "    
  501.                  </select>
  502.                  
  503.                  <br /><br />
  504.                  
  505.                  <label for=\"op\">Tabella:</label>
  506.                  <select name=\"tabella\" id=\"tabella\">
  507.                      <option value=\"\">Tutte le tabelle</option>
  508.                  ";
  509.      
  510.          for($i=0;$i<count($tabelle);$i++){
  511.              
  512.              $sel_tabella ($tabelle[$i]==$val_tabella" selected=\"selected\"" "";
  513.              
  514.              $FILTRI.= "<option value=\"".$tabelle[$i]."\" $sel_tabella>".$tabelle[$i]."</option>\n";
  515.          }
  516.  
  517.          $FILTRI.= "
  518.                  </select>
  519.      
  520.              <br /><br />
  521.              
  522.              
  523.              
  524.                  
  525.                  <label for=\"uid\">Utente:</label>
  526.                  <select name=\"uid\" id=\"uid\">
  527.                      <option value=\"\">Tutti gli utenti</option>
  528.                  ";
  529.      
  530.          for($i=0;$i<count($utenti);$i++){
  531.              
  532.              $sel_utenti ($id_utenti[$i]==$val_uid" selected=\"selected\"" "";
  533.              
  534.              $FILTRI.= "<option value=\"".$id_utenti[$i]."\" $sel_utenti>".$utenti[$i]."</option>\n";
  535.          }
  536.  
  537.          $FILTRI.= "
  538.                  </select>
  539.      
  540.              <br /><br />
  541.              
  542.              <label>Data:</label><br />
  543.              dal: <input type=\"text\" name=\"data_dal\" id=\"data_dal\" value=\"$val_data1\" /> al <input type=\"text\" name=\"data_al\"  id=\"data_al\" value=\"$val_data2\" />
  544.              
  545.               <script type=\"text/javascript\">
  546.     
  547.               
  548.           
  549.                Calendar.setup({
  550.                     inputField     :    \"data_dal\",   // id of the input field
  551.                     firstDay       :    1,
  552.                     ifFormat       :    \"%Y-%m-%d %H:%M\",       // format of the input field
  553.                     showsTime      :    true,
  554.                     timeFormat     :    \"24\"
  555.                 });    
  556.                 
  557.                Calendar.setup({
  558.                     inputField     :    \"data_al\",   // id of the input field
  559.                     firstDay       :    1,
  560.                     ifFormat       :    \"%Y-%m-%d %H:%M\",       // format of the input field
  561.                     showsTime      :    true,
  562.                     timeFormat     :    \"24\"
  563.                 });    
  564.                 
  565.             
  566.                 </script>
  567.              
  568.              <br /><br />
  569.                  
  570.              
  571.              
  572.              <input type=\"button\" onclick=\"submit();\" name=\"filtra\" value=\" Filtra i log \" />
  573.              &nbsp;&nbsp;&nbsp;&nbsp;
  574.              <input type=\"button\" onclick=\"reset();getElementById('tabella').options[0].selected=true;getElementById('op').options[0].selected=true;getElementById('uid').options[0].selected=true;submit();\" name=\"rimuovi\" value=\" Rimuovi tutti i filtri\" />
  575.                  
  576.              </fieldset>
  577.          </form>
  578.          </div>\n";
  579.      
  580.          
  581.          
  582.          
  583.          
  584.          
  585.          
  586.          
  587.          
  588.          
  589.          
  590.      #########################################################################
  591.      #
  592.      #    CONTINUA A STAMPARE
  593.      #
  594.          
  595.      echo $FILTRI;
  596.      
  597.      echo $PAG;
  598.          
  599.          
  600.      echo "<table class=\"tab-color\" summary=\"Tabella Log\">
  601.     
  602.          <tr>
  603.             <th$class_data>data</th>
  604.             <th$class_op>operazione</th>
  605.             <th$class_tabella>tabella</th>
  606.             <th>utente</th>
  607.             <th>gruppo</th>
  608.             <th>id_record</th>
  609.             <th>fonte</th>
  610.             <th>dettaglio</th>
  611.             <th>storico</th>
  612.         </tr>
  613.     
  614.         ";
  615.      
  616.      while($RSlog=vmsql_fetch_assoc($q_log)){
  617.      
  618.          switch($RSlog['op']){
  619.              case 'insert' $colore="#EFFFEF"break;
  620.              case 'update' $colore="#FFFBEF"break;
  621.              case 'delete' $colore="#FFEFEF"break;
  622.          }
  623.          
  624.          $data dataISO2ITA($RSlog['data'],true);
  625.          
  626.          echo "
  627.          <tr class=\"colore-".$RSlog['op']."\" >
  628.             <td>".$data['ita']."</td>
  629.             <td>".$RSlog['op']."</td>
  630.             <td>".$RSlog['tabella']."</td>
  631.             <td>".$RSlog['nomecognome']."</td>
  632.             <td>".$RSlog['gruppo_desc']."</td>
  633.             <td>".$RSlog['id_record']."</td>
  634.             <td>".$RSlog['fonte']."</td>
  635.             <td><a href=\"log.php?dettaglio=".$RSlog['id_log']."\">dettaglio</a></td>
  636.             <td><a href=\"log.php?id_record=".$RSlog['id_record']."\">storico</a></td>
  637.         </tr>
  638.          ";
  639.      }
  640.      
  641.      echo "</table>\n";
  642.      
  643.      echo closeLayout1();
  644.  
  645. }
  646.  
  647.  
  648. /**
  649.  * Qualora il record sia stato creato e gestito dalle maschere,
  650.  * viene riportata tutta la sua storia: inserimento, modifiche, etc.
  651.  * 
  652.  * @desc Mostra lo storico di un dato record.
  653.  */
  654. function mostra_storico(){
  655.     
  656.     global $link,$db1;
  657.  
  658.     $files=array("sty/admin.css","sty/tabelle.css","js/mostra_nascondi_id.js","sty/log.css");
  659.     
  660.     
  661.     
  662.     $ID_RECORD = (int) $_GET['id_record'];
  663.     
  664.      echo openLayout1("Storico record Database"$files);
  665.      
  666.      echo "<div id=\"briciole\"><a href=\"index.php\">home amministrazione</a> &raquo; <a href=\"log.php\">log database</a> &raquo; storico record</div>\n";
  667.     
  668.      echo "<h1>Storico del record</h1>\n";
  669.     
  670.      
  671.      // Query Storico
  672.      
  673.      
  674.          $sql_log "SELECT log.id_log,
  675.                             log.op,
  676.                             log.tabella,
  677.                             log.data,
  678.                             log.uid,
  679.                             ".concat_sintax("log.gid, ' (',g.nome_gruppo,')'"'gruppo_desc').",
  680.                             log.id_record,
  681.                             log.fonte,
  682.                             ".concat_sintax("u.nome, ' ',u.cognome"'nomecognome')."
  683.                             
  684.                                 
  685.                      FROM {$db1['frontend']}.log 
  686.                      INNER JOIN {$db1['frontend']}.utente AS u ON u.id_utente=log.uid
  687.                      INNER JOIN {$db1['frontend']}.gruppo AS g ON u.gid=g.gid
  688.                      
  689.                  WHERE 1=1
  690.                  AND u.gid=g.gid
  691.                  AND log.id_record=$ID_RECORD
  692.                  ORDER BY log.data ASC
  693.                  ";
  694.          $q_log=vmsql_query($sql_log,$link);
  695.          
  696.      
  697.  
  698.           
  699.      #########################################################################
  700.      #
  701.      #    CONTINUA A STAMPARE
  702.      #
  703.          
  704.      
  705.          
  706.          
  707.      echo "<table class=\"tab-color\" summary=\"Tabella Log\">
  708.     
  709.          <tr>
  710.             <th class=\"grigia\">data</th>
  711.             <th class=\"grigia\">operazione</th>
  712.             <th class=\"grigia\">tabella</th>
  713.             <th class=\"grigia\">utente</th>
  714.             <th class=\"grigia\">gruppo</th>
  715.             <th class=\"grigia\">id_record</th>
  716.             <th class=\"grigia\">fonte</th>
  717.             <th class=\"grigia\">dettaglio</th>
  718.         </tr>
  719.     
  720.         ";
  721.      
  722.      while($RSlog=vmsql_fetch_assoc($q_log)){
  723.      
  724.          $data dataISO2ITA($RSlog['data'],true);
  725.          
  726.          echo "
  727.          <tr class=\"colore-".$RSlog['op']."\" >
  728.             <td>".$data['ita']."</td>
  729.             <td>".$RSlog['op']."</td>
  730.             <td>".$RSlog['tabella']."</td>
  731.             <td>".$RSlog['nomecognome']."</td>
  732.             <td>".$RSlog['gruppo_desc']."</td>
  733.             <td>".$RSlog['id_record']."</td>
  734.             <td>".$RSlog['fonte']."</td>
  735.             <td><a href=\"log.php?dettaglio=".$RSlog['id_log']."\">dettaglio</a></td>
  736.         </tr>
  737.          ";
  738.      }
  739.      
  740.      echo "</table>\n";
  741.      
  742.      echo closeLayout1();
  743.  
  744. }
  745.  
  746.  
  747.  
  748.  
  749. /**
  750.  * Funzione di parsing delle istruzioni UPDATE
  751.  * Restituisce un array con chiave tabella e un array modifiche con campo=>valore inserito
  752.  *
  753.  * @param string $sql SQL da analizzare
  754.  * @return array Array con frammenti di SQL
  755.  */
  756. function parser_sql_update($sql){
  757.     
  758.     $sql=str_replace(array("\n","\r")," ",$sql);
  759.     
  760.     $sql=preg_replace("|LIMIT .*$|i",'',$sql);
  761.     
  762.     preg_match("# *UPDATE +([a-z_]+) +SET(.+?) WHERE +(.+) *#i",$sql,$sql_frag);
  763.     
  764.     $out=array();
  765.     
  766.     if(count($sql_frag)==4){
  767.         
  768.         //1 - tabella
  769.         //2 - modifiche
  770.         //3 - condizioni
  771.         
  772.         $out['tabella']=trim($sql_frag[1]);
  773.         
  774.         // MODIFICA DI COMODO PER IL PARSING CORRETTO DELLA VIRGOLA
  775.         $sql_frag[2]=preg_replace("|([\w.-])', '?|","$1[@]",$sql_frag[2])
  776.         
  777.         $modifiche explode("[@]",trim($sql_frag[2]));
  778.         
  779.         for($i=0;$i<count($modifiche);$i++){
  780.             
  781.             $t_modifiche=explode("=",$modifiche[$i],2);
  782.             
  783.             $t_modifiche[1]=trim($t_modifiche[1]);
  784.             
  785.             // tolgo gli apici
  786.             if(substr($t_modifiche[1],0,1)=="'"){
  787.                 $t_modifiche[1]=substr($t_modifiche[1],1);
  788.             }
  789.             
  790.             if(substr($t_modifiche[1],-1,1)=="'"){
  791.                 $t_modifiche[1]=substr($t_modifiche[1],0,-1);
  792.             }
  793.             
  794.             
  795.             $arr_modifiche[trim($t_modifiche[0])]=$t_modifiche[1];
  796.         }
  797.         
  798.         $out['modifiche']=$arr_modifiche;
  799.         
  800.         
  801.     }
  802.     
  803.     
  804.     return $out;
  805.     
  806. }
  807.  
  808.  
  809.  
  810.  
  811.  
  812. /**
  813.  * Funzione di parsing delle istruzioni INSERT
  814.  * Restituisce un array con chiave tabella e un array campo=>valore
  815.  *
  816.  * @param string $sql SQL da analizzare
  817.  * @return array Array con associazione di campi e valori
  818.  */
  819. function parser_sql_insert($sql){
  820.     
  821.     $sql=str_replace(array("\n","\r"),"",$sql);
  822.     
  823.     preg_match("| *INSERT +INTO +([a-z_]+) +\((.+?)\) VALUES +\('?(.+?)'?\)|i",$sql,$sql_frag);
  824.     
  825.     $out=array();
  826.     
  827.     if(count($sql_frag)==4){
  828.         
  829.         //1 - tabella
  830.         //2 - campi
  831.         //3 - valori
  832.         
  833.         $out['tabella']=trim($sql_frag[1]);
  834.         
  835.         $campi explode(",",trim($sql_frag[2]));
  836.         $valori explode("','",trim($sql_frag[3]));
  837.         
  838.         $out['campi']=$campi;
  839.         $out['valori']=$valori;
  840.         
  841.         
  842.     }
  843.     
  844.     return $out;
  845.     
  846. }
  847.  
  848.  
  849.  
  850.  
  851.  
  852. /**
  853.  * Funzione per identificare se un dato record è presente in tabella
  854.  *
  855.  * @param int $id_record 
  856.  * @param string $tabella 
  857.  * @todo Funzione non sviluppata qui
  858.  */
  859. function in_tabella($id_record$tabella){
  860.     
  861.     global $link;
  862.     
  863.     
  864.     $q=vmsql_query("SELECT * FROM $tabella WHERE $campo_pk='$id_record'");
  865.     
  866.     
  867. }
  868.  
  869.  
  870.  
  871.  
  872.  
  873. /**
  874.  * Raccoglie informazioni sull'operazione
  875.  * presente nel log e restituisce un array con la tabella HTML generata
  876.  * e una variabile boolean per la reversibilità dell'operazione
  877.  *
  878.  * @param resource $RS Recordset
  879.  * @return array Array con la tabella HTML generata e una variabile boolean per la reversibilità dell'operazione
  880.  * @see function mostra_dettaglio_log
  881.  */
  882. function info_tabella_operazione($RS){
  883.     
  884.     global $link;
  885.     
  886.     $presenza_id (intval($RS['id_record'])>|| strlen($RS['id_record'])>2);
  887.     
  888.     $fonte ($RS['fonte']=="m"'maschera':'sottomaschera';
  889.     
  890.     switch($RS['op']){
  891.         
  892.         
  893.         
  894.         case 'update'
  895.         
  896.                     $storico_pre is_array(unserialize($RS['storico_pre']));
  897.                     $storico_post count(parser_sql_update($RS['storico_post']))==2;
  898.                     
  899.                     
  900.                         if($storico_pre && $storico_post && $presenza_id){
  901.  
  902.                             $campo_pk prendi_PK($RS['tabella']);
  903.                             
  904.                             if(vmsql_test_id($link,$campo_pk,$RS['id_record'],$RS['tabella'],'',true)){
  905.                                 $reversibile="<span class=\"verde\">reversibile</span>\n";
  906.                                 $is_reversibile=true;
  907.                             }
  908.                             else{
  909.                                 $reversibile="<span class=\"rosso\">irreversibile</span> (<strong>il dato &egrave; stato eliminato</strong>, ripristinare prima l'operazione di DELETE)\n";
  910.                                  $is_reversibile=false;
  911.                             }
  912.                          }
  913.                          else{
  914.                              $reversibile="<span class=\"rosso\">irreversibile</span>\n";
  915.                              $is_reversibile=false;
  916.                          }
  917.         break;
  918.         
  919.         
  920.         
  921.         
  922.         case 'insert':
  923.         
  924.                     $storico_post count(parser_sql_insert($RS['storico_post']))==2;
  925.                     
  926.  
  927.                             $reversibile="<span class=\"verde\">reversibile (&egrave; sufficiente eliminare il record)</span>\n";
  928.                             $is_reversibile=true;
  929.                        
  930.         break;    
  931.                 
  932.         case 'delete':
  933.                     $storico_pre is_array(unserialize($RS['storico_pre']));
  934.                     
  935.                     
  936.                         if($storico_pre){
  937.  
  938.                             $reversibile="<span class=\"verde\">reversibile (reinserimento)</span>\n";
  939.                             $is_reversibile=true;
  940.                          }
  941.                          else{
  942.                              $reversibile="<span class=\"rosso\">irreversibile</span>\n";
  943.                              $is_reversibile=false;
  944.                          }
  945.         break;        
  946.         
  947.     }
  948.     
  949.     
  950.             $info_operazione="
  951.             
  952.             <table id=\"info_log\" summary=\"informazioni sull'operazione\" border=\"1\">
  953.                 <tr>
  954.                     <th colspan=\"2\" style=\"text-align:left\"><h3>Riepilogo <span class=\"var\">".strtoupper($RS['op'])."</span></h3></th>
  955.                 </tr>            
  956.                 <tr>
  957.                     <th>id_log</th>
  958.                     <td>".$RS['id_log']."</td>
  959.                 </tr>
  960.                 <tr>
  961.                     <th>data/ora</th>
  962.                     <td>".$RS['data']."</td>
  963.                 </tr>
  964.                 <tr>
  965.                     <th>tabella</th>
  966.                     <td>".$RS['tabella']."</td>
  967.                 </tr>
  968.                 <tr>
  969.                     <th>id_record</th>
  970.                     <td>".$RS['id_record']."</td>
  971.                 </tr>
  972.                 <tr>
  973.                     <th>tipo operazione</th>
  974.                     <td>".$RS['op']."</td>
  975.                 </tr>
  976.                 <tr>
  977.                     <th>reversibilit&agrave;</th>
  978.                     <td>".$reversibile."</td>
  979.                 </tr>
  980.                 <tr>
  981.                     <th>autore operazione</th>
  982.                     <td>".$RS['uid']."</td>
  983.                 </tr>
  984.                 <tr>
  985.                     <th>gruppo autore</th>
  986.                     <td>".$RS['gid']."</td>
  987.                 </tr>            
  988.                 
  989.                 <tr>
  990.                     <th>fonte modifica</th>
  991.                     <td>".$fonte."</td>
  992.                 </tr>        
  993.                         
  994.                 <tr>
  995.                     <th>informazioni browser</th>
  996.                     <td>".htmlentities(stripslashes($RS['info_browser']),ENT_QUOTES)."</td>
  997.                 </tr>
  998.                 
  999.             </table>        
  1000.                     
  1001.             
  1002.             ";
  1003.             
  1004.         return array('table'=>$info_operazione,'rev'=>$is_reversibile);
  1005.             
  1006. }
  1007.  
  1008. /**
  1009.  * @desc Funzione che mostra la pagina di dettaglio per una operazione di log
  1010.  * @param int $id_log ID dell'operazione da mostrare
  1011.  */
  1012. function mostra_dettaglio_log($id_log){
  1013.     
  1014.     global $link,$db1;
  1015.     
  1016.     // PRENDI IL DETTAGLIO DEL LOG
  1017.     
  1018.     $q_log=vmsql_query("SELECT log.* 
  1019.                         FROM {$db1['frontend']}.log 
  1020.                         INNER JOIN {$db1['frontend']}.utente ON log.uid=utente.id_utente
  1021.                         WHERE id_log=$id_log",$link);
  1022.     
  1023.     $RS=vmsql_fetch_assoc($q_log);
  1024.     
  1025.     
  1026.     $OUT"";
  1027.     
  1028.     // CASO QUERY DI MODIFICA
  1029.     if($RS['op']=='update'){
  1030.         
  1031.         $storico_pre unserialize($RS['storico_pre']);
  1032.         
  1033.         $parse_sql parser_sql_update($RS['storico_post']);
  1034.         
  1035.         if(count($parse_sql)==0){
  1036.             
  1037.             $OUT.= "Errore di lettura del record";
  1038.         }
  1039.         else{
  1040.             
  1041.             $info_op info_tabella_operazione($RS);
  1042.             
  1043.             $OUT.=$info_op['table'];
  1044.             
  1045.             // Tabella di comparazione:
  1046.             
  1047.             $OUT.="<br /><br />
  1048.             <h2>Tabella di comparazione record</h2>
  1049.             
  1050.             <p>Sono evidenziati in giallo i campi che hanno subito modifiche in questa operazione.</p>
  1051.             <table border=\"1\" summary=\"tabella comparazione\" id=\"tabella-comparazione\">\n";
  1052.             
  1053.             $OUT.="
  1054.             <tr>
  1055.                 <th>campo</th>
  1056.                 <th>valore modificato</th>
  1057.                 <th>valore attuale</th>
  1058.             </tr>
  1059.             ";
  1060.             
  1061.             foreach($storico_pre as $campo=>$valore_old){
  1062.  
  1063.                 $valore_old=htmlentities($valore_old,ENT_QUOTES);
  1064.                 
  1065.                 if($valore_old=='' || $valore_old==null$valore_old "<em class=\"null_old\">Null</em>";
  1066.                 
  1067.                 if(isset($parse_sql['modifiche'][$campo])){
  1068.                     
  1069.                     $valore_new "<span class=\"modificato_new\">".htmlentities(stripslashes($parse_sql['modifiche'][$campo]),ENT_QUOTES)."</span>";
  1070.                     $classe_new '';
  1071.                     $valore_old "<span class=\"modificato_old\">".stripslashes($valore_old)."</span>";
  1072.                     $classe_tr=" class=\"evidenza\"";
  1073.                 }
  1074.                 else{
  1075.                     $valore_new $valore_old;
  1076.                     $classe_new " class=\"intatto\"";
  1077.                     $valore_old "<span class=\"intatto_old\">".$valore_old."</span>";
  1078.                     $classe_tr="";
  1079.                 }
  1080.                 
  1081.                 
  1082.                 $OUT.= "<tr $classe_tr>\n";            
  1083.                 
  1084.                 $OUT.= "<td class=\"campo\">".$campo."</td>\n";
  1085.                                 
  1086.                 $OUT.= "<td>".$valore_old."</td>\n";
  1087.                 
  1088.                 $OUT.= "<td $classe_new>".$valore_new."</td>\n";
  1089.                 
  1090.             }
  1091.             
  1092.             $OUT.="</table>\n";
  1093.             
  1094.             
  1095.             
  1096.             // PROCEDURA DI RIPRISTINO IN CASO UPDATE
  1097.             if($info_op['rev']){
  1098.                 
  1099.                 $OUT.= "<br /><form action=\"".$_SERVER['PHP_SELF']."?ripristino=1&amp;type=update\" method=\"post\">
  1100.                     
  1101.                     <input type=\"hidden\" name=\"id_log\" value=\"$id_log\" />
  1102.                     <input type=\"button\" onclick=\"submit();\" name=\"ripristino_op\" value=\" Ripristina questa operazione \" />
  1103.                     
  1104.                     </form>\n";
  1105.             }
  1106.             
  1107.             
  1108.         }
  1109.         
  1110.         
  1111.         
  1112.     // CASO QUERY DI INSERT
  1113.     else if($RS['op']=='insert'){
  1114.         
  1115.         $parse_sql parser_sql_insert($RS['storico_post']);
  1116.         
  1117.         $info_op info_tabella_operazione($RS);
  1118.             
  1119.         $OUT.=$info_op['table'];
  1120.         
  1121.         
  1122.         // Tabella di comparazione:
  1123.             
  1124.             $OUT.="<br /><br />
  1125.             <h2>Record inserito</h2>
  1126.             
  1127.             <table border=\"1\" summary=\"tabella comparazione\" id=\"tabella-comparazione\">\n";
  1128.             
  1129.             $OUT.="
  1130.             <tr>
  1131.                 <th>campo</th>
  1132.                 <th>valore</th>
  1133.             </tr>
  1134.             ";
  1135.             
  1136.             for($i=0;$i<count($parse_sql['campi']);$i++){
  1137.             
  1138.                 $valore_new=htmlentities($parse_sql['valori'][$i]);
  1139.                 
  1140.                 if($valore_new=='' || $valore_new==null$valore_new "<em class=\"null_old\">Null</em>";
  1141.                 
  1142.                 $valore "<span class=\"intatto_old\">".$valore_new."</span>";
  1143.                 $classe_tr="";
  1144.                 
  1145.                 
  1146.                 $OUT.= "<tr $classe_tr>\n";            
  1147.                 
  1148.                 $OUT.= "<td class=\"campo\">".$parse_sql['campi'][$i]."</td>\n";
  1149.                                 
  1150.                 $OUT.= "<td>".$valore."</td>\n";
  1151.                 
  1152.                 
  1153.             }
  1154.             
  1155.             $OUT.="</table>\n";
  1156.             
  1157.             
  1158.             
  1159.         
  1160.     }
  1161.     // CASO QUERY DI DELETE
  1162.     else if($RS['op']=='delete'){
  1163.         
  1164.         $storico_pre unserialize($RS['storico_pre']);
  1165.  
  1166.         $info_op info_tabella_operazione($RS);
  1167.         
  1168.             $OUT.=$info_op['table'];
  1169.         
  1170.             // Tabella di comparazione:
  1171.             
  1172.             $OUT.="<br /><br />
  1173.             <h2>Record eliminato</h2>
  1174.             
  1175.             <table border=\"1\" summary=\"tabella comparazione\" id=\"tabella-comparazione\">\n";
  1176.             
  1177.             $OUT.="
  1178.             <tr>
  1179.                 <th>campo</th>
  1180.                 <th>valore</th>
  1181.             </tr>
  1182.             ";
  1183.             
  1184.             foreach($storico_pre as $campo=>$valore_old){
  1185.  
  1186.                 $valore_old=htmlentities($valore_old);
  1187.                 
  1188.                 if($valore_old=='' || $valore_old==null$valore_old "<em class=\"null_old\">Null</em>";
  1189.                 
  1190.                 if(isset($parse_sql['modifiche'][$campo])){
  1191.                     
  1192.                     $valore_new "<span class=\"modificato_new\">".htmlentities($parse_sql['modifiche'][$campo])."</span>";
  1193.                     $classe_new '';
  1194.                     $valore_old "<span class=\"modificato_old\">".$valore_old."</span>";
  1195.                     $classe_tr=" class=\"evidenza\"";
  1196.                 }
  1197.                 else{
  1198.                     $valore_new $valore_old;
  1199.                     $classe_new " class=\"intatto\"";
  1200.                     $valore_old "<span class=\"intatto_old\">".$valore_old."</span>";
  1201.                     $classe_tr="";
  1202.                 }
  1203.                 
  1204.                 
  1205.                 $OUT.= "<tr $classe_tr>\n";            
  1206.                 
  1207.                 $OUT.= "<td class=\"campo\">".$campo."</td>\n";
  1208.                                 
  1209.                 $OUT.= "<td>".$valore_old."</td>\n";
  1210.                 
  1211.                 
  1212.             }
  1213.             
  1214.             $OUT.="</table>\n";
  1215.             
  1216.     
  1217.         
  1218.         // PROCEDURA DI RIPRISTINO IN CASO DELETE
  1219.         if($info_op['rev']){
  1220.                 
  1221.             $OUT.= "<br /><form action=\"".$_SERVER['PHP_SELF']."?ripristino=1&amp;type=delete\" method=\"post\">
  1222.                 
  1223.                     <input type=\"hidden\" name=\"id_log\" value=\"$id_log\" />
  1224.                     <input type=\"button\" onclick=\"submit();\" name=\"ripristino_op\" value=\" Ripristina questa operazione \" />
  1225.                     
  1226.                     </form>\n";
  1227.             }
  1228.             
  1229.     }
  1230.     // CASO QUERY DI DELETE
  1231.     else if($RS['op']=='duplicazione'){
  1232.     
  1233.         $info_op info_tabella_operazione($RS);
  1234.             
  1235.         $OUT.=$info_op['table'];
  1236.         
  1237.         
  1238.         // Tabella di comparazione:
  1239.             
  1240.             $OUT.="<br /><br />
  1241.             <h2>Record duplicato</h2>
  1242.             ";
  1243.             
  1244.             // prendi le informazioni
  1245.             
  1246.             $info_duplicazione=str_replace("DUPLICAZIONE ","",$RS['storico_post']);
  1247.             
  1248.             
  1249.             $OUT.="<p>Duplicazione del record della tabella ".str_replace(":",", ID:",$info_duplicazione)."</p>";
  1250.             
  1251.     }
  1252.     
  1253.      echo openLayout1("Dettaglio log Database"array("sty/admin.css","sty/tabelle.css","sty/log.css"));
  1254.      
  1255.     
  1256.      echo "<div id=\"briciole\"><a href=\"index.php\">home amministrazione</a> &raquo; <a href=\"log.php\">log database</a> &raquo; dettaglio</div>\n";
  1257.     
  1258.      echo "<h1>Dettaglio log </h1>\n";
  1259.      
  1260.      
  1261.      
  1262.      
  1263.      echo $OUT;
  1264.      
  1265.      
  1266.      echo closeLayout1();
  1267. }
  1268.  
  1269.  
  1270.  
  1271.  
  1272.  
  1273.  
  1274.  
  1275.  
  1276. if(isset($_POST['id_log']&& isset($_GET['ripristino'])){
  1277.     
  1278.     $id_log = (int)$_POST['id_log'];
  1279.     if($id_log>0){
  1280.         ripristina($id_log);
  1281.     }
  1282.     else{
  1283.         openErrorGenerico("Nessun riferimento per l'operazione da ripristinare");
  1284.         exit;
  1285.     }
  1286. }
  1287.  
  1288. else if(isset($_GET['dettaglio']&& (intval($_GET['dettaglio'])>0)){
  1289.     
  1290.     
  1291.     mostra_dettaglio_log($_GET['dettaglio']);
  1292. }
  1293. else if(isset($_GET['id_record']&& (intval($_GET['id_record'])>0)){
  1294.     
  1295.     mostra_storico();
  1296. }
  1297. else{
  1298.     
  1299.     mostra_log();
  1300. }
  1301.  
  1302.  
  1303.  
  1304.  
  1305.  
  1306.  
  1307.  
  1308. ?>

Documentation generated on Sat, 22 Sep 2007 11:51:00 +0200 by phpDocumentor 1.4.0a2