5 ) die ("-->Dossier invalide [$p_database_id]"); $phpcompta_user=phpcompta_user; $password=phpcompta_password; $port=phpcompta_psql_port; $host=( ! defined ("phpcompta_psql_host"))?'127.0.0.1':phpcompta_psql_host; if ( $p_database_id == 0 ) { /* connect to the repository */ $l_dossier=sprintf("%saccount_repository",strtolower(domaine)); } else if ( $p_type == 'dos') { /* connect to a folder (dossier) */ $l_dossier=sprintf("%sdossier%d",strtolower(domaine),$p_database_id); } else if ($p_type=='mod') { /* connect to a template (modele) */ $l_dossier=sprintf("%smod%d",strtolower(domaine),$p_database_id); } else if ($p_type=='template') { $l_dossier='template1'; } else { throw new Exception ('Connection invalide'); } ob_start(); $a=pg_connect("dbname=$l_dossier host='$host' user='$phpcompta_user' password='$password' port=$port"); if ( $a == false ) { ob_end_clean(); echo '

Impossible de se connecter à postgreSql !

'; echo '

'; echo "Vos paramètres sont incorrectes :
"; echo "
"; echo "base de donnée : $l_dossier
"; echo "Domaine : ".domaine."
"; echo "Port $port
"; echo "Utilisateur : $phpcompta_user
"; echo '

'; exit ("Connection impossible : vérifiez vos paramètres de base de données"); } $this->db=$a; ob_end_clean(); } public function verify() { // Verify that the elt we want to add is correct } function set_encoding($p_charset) { pg_set_client_encoding($this->db,$p_charset); } /*! * \brief send a sql string to the database * \param $p_string sql string * \param $p_array array for the SQL string (see pg_query_params) * \return false if error otherwise true */ function exec_sql( $p_string,$p_array=null) { try { if ( $p_array==null ) { $this->ret=pg_query($this->db,$p_string); } else { $this->ret=pg_query_params($this->db,$p_string,$p_array); } if ( ! $this->ret ) { throw new Exception (" SQL ERROR $p_string ",1);} }catch (Exception $a) { if (DEBUG) { print_r ($p_string); print_r($p_array); echo $a->getMessage(); echo $a->getTrace(); } throw ($a); } return $this->ret; } /*! \brief Count the number of row returned by a sql statement * * \param $p_sql sql string * \param $p_array if not null we use the safer pg_query_params */ function count_sql($p_sql,$p_array=null) { $r_sql=$this->exec_sql($p_sql,$p_array); return pg_NumRows($r_sql); } /*!\brief get the current sequence value */ function get_current_seq($p_seq) { $Res=$this->get_value("select currval('$p_seq') as seq"); return $Res; } /*!\brief get the next sequence value */ function get_next_seq($p_seq) { $Res=$this->exec_sql("select nextval('$p_seq') as seq"); $seq=pg_fetch_array($Res,0); return $seq['seq']; } /** * @ brief : start a transaction * */ function start() { $Res=$this->exec_sql("start transaction"); } /** * Commit the transaction * */ function commit() { $Res=$this->exec_sql("commit"); } /** * rollback the current transaction */ function rollback() { $Res=$this->exec_sql("rollback"); } /** * @brief alter the sequence value * @param $p_name name of the sequence * @param $min the start value of the sequence */ function alter_seq($p_name,$min) { if ($min < 1) $min=1; $Res=$this->exec_sql("alter sequence $p_name restart $min"); } /*! * \brief Execute a sql script * \param $script script name */ function execute_script($script) { if ( DEBUG=='false' ) ob_start(); $hf=fopen($script,'r'); if ( $hf == false ) { echo 'Ne peut ouvrir '.$script; exit(); } $sql=""; $flag_function=false; while (!feof($hf)) { $buffer=fgets($hf); $buffer=str_replace ("$","\$",$buffer); print $buffer."
"; // comment are not execute if ( substr($buffer,0,2) == "--" ) { //echo "comment $buffer"; continue; } // Blank Lines Are Skipped If ( Strlen($buffer)==0) { //echo "Blank $buffer"; Continue; } if ( strpos(strtolower($buffer),"create function")===0 ) { echo "found a function"; $flag_function=true; $sql=$buffer; continue; } if ( strpos(strtolower($buffer),"create or replace function")===0 ) { echo "found a function"; $flag_function=true; $sql=$buffer; continue; } // No semi colon -> multiline command if ( $flag_function== false && strpos($buffer,';') == false ) { $sql.=$buffer; continue; } if ( $flag_function ) { if ( strpos(strtolower($buffer), "language plpgsql") === false && strpos(strtolower($buffer), "language 'plpgsql'") === false ) { $sql.=$buffer; continue; } } else { // cut the semi colon $buffer=str_replace (';','',$buffer); } $sql.=$buffer; echo_debug('setup.php',__LINE__,"Execute sql $sql"); if ( $this->exec_sql($sql) == false ) { $this->rollback(); if ( DEBUG=='false' ) ob_end_clean(); print "ERROR : $sql"; exit(); } $sql=""; $flag_function=false; print "
"; } // while (feof) fclose($hf); if ( DEBUG=='false' ) ob_end_clean(); } /*! * \brief Get version of a database, the content of the * table version * * \return version number * */ function get_version() { $Res=$this->get_value("select val from version"); return $Res; } /** *@brief fetch the $p_indice array from the last query *@param $p_indice index * */ function fetch($p_indice) { if ( $this->ret == false ) throw new Exception ('this->ret is empty'); return pg_fetch_array($this->ret ) ; } /*!@brief return the number of rows found by the last query, synomym for count()*/ function size() { return pg_NumRows($this->ret); } /*!@brief return the number of rows found by the last query, synomym for size()*/ function count() { return pg_NumRows($this->ret); } /*!\brief loop to apply all the path to a folder or * a template * \param $p_name database name * \param $from_setup == 1 if called from setup.php * */ function apply_patch($p_name,$from_setup=1) { $MaxVersion=DBVERSION-1; echo ''; } /*!\brief return the value of the sql, the sql will return only one value * with the value * \param $p_sql the sql stmt example :select s_value from document_state where s_id=2 *\param $p_array if array is not null we use the ExecSqlParm (safer) *\see exec_sql *\note print a warning if several value are found, if only the first value is needed * consider using a LIMIT clause * \return only the first value or an empty string if nothing is found */ function get_value($p_sql,$p_array=null) { $this->ret=$this->exec_sql($p_sql,$p_array); if ( pg_NumRows($this->ret) == 0 ) return ""; if ( pg_NumRows($this->ret) > 1 ) echo "Attention $p_sql retourne plusieurs valeurs"; $r=pg_fetch_row($this->ret,0); return $r[0]; } /*!\brief purpose return the result of a sql statment * in a array * \param $p_sql sql query * \param $p_array if not null we use ExecSqlParam *\return an empty array if nothing is found */ function get_array($p_sql,$p_array=null) { $r=$this->exec_sql($p_sql,$p_array); if ( ($Max= pg_NumRows($r)) == 0 ) return array(); $array=pg_fetch_all($r); return $array; } function create_sequence($p_name,$min=1) { if ($min < 1) $min=1; $sql="create sequence ".$p_name." minvalue $min"; $this->exec_sql($sql); } /*!\brief test if a sequence exist */ /* \return true if the seq. exist otherwise false */ function exist_sequence($p_name) { $r=$this->count_sql("select relname from pg_class where relname=lower($1)",array($p_name)); if ( $r==0) return false; return true; } /*!\brief test if a table exist * \return true if a table exist otherwise false */ function exist_table($p_name) { $r=$this->count_sql("select tablename from pg_tables where tablename=lower($1)", array($p_name)); if ( $r==0) return false; return true; } /* *!\brief test if a view exist * \return true if the view. exist otherwise false */ function exist_view($p_name) { $r=$this->count_sql("select viewname from pg_views where viewname=lower($1)",array($p_name)); if ( $r==0) return false; return true; } /*! *\brief create a string containing the value separated by comma * for use in a SQL in statement *\return the string or empty if nothing is found *\see fid_card.php */ function make_list($sql,$p_array=null) { if ( $p_array == null ) { $aArray=$this->get_array($sql); } else { $aArray=$this->get_array($sql,$p_array); } if (empty ($aArray) ) return ""; $aIdx=array_keys($aArray[0]); $idx=$aIdx[0]; $ret="";$f=""; for ($i = 0; $i < count($aArray);$i++) { $row=$aArray[$i]; $ret.=$f.$aArray[$i][$idx]; $f=','; } return $ret; } /*! * \brief make a array with the sql * * \param $p_sql sql statement, only the first two column will be returned in * an array. The first col. is the label and the second the value * \param $p_null if the array start with a null value *\note this function is used with ISelect when it is needed to have a list of * options * \return: a double array like \verbatim Array ( [0] => Array ( [value] => 1 [label] => Marchandise A ) [1] => Array ( [value] => 2 [label] => Marchandise B ) [2] => Array ( [value] => 3 [label] => Marchandise C ) ) \endverbatim * \see ISelect */ function make_array($p_sql,$p_null=0) { $a=$this->exec_sql($p_sql); $max=pg_NumRows($a); if ( $max==0) return null; for ($i=0;$i<$max;$i++) { $row=pg_fetch_row($a); $r[$i]['value']=$row[0]; $r[$i]['label']=$row[1]; } // add a blank item ? if ( $p_null == 1 ) { for ($i=$max;$i!=0;$i--) { $r[$i]['value']= $r[$i-1]['value']; $r[$i]['label']= $r[$i-1]['label']; } $r[0]['value']=-1; $r[0]['label']=" "; } // if ( $p_null == 1 ) return $r; } /*! * \brief Save a "piece justificative" * * \param $seq jr_grpt_id * \return $oid of the lob file if success * null if a error occurs * */ function save_upload_document ($seq) { /* there is no file to upload */ if ($_FILES["pj"]["error"] == UPLOAD_ERR_NO_FILE ){ return;} $new_name=tempnam($_ENV['TMP'],'pj'); echo_debug(__FILE__,__LINE__,"new name=".$new_name); echo_debug(__FILE__.":".__LINE__.$_FILES); if ($_FILES["pj"]["error"] > 0) { print_r($_FILES); echo_error(__FILE__.":".__LINE__."Error: " . $_FILES["pj"]["error"] ); } if ( strlen ($_FILES['pj']['tmp_name']) != 0 ) { echo_debug(__FILE__.":".__LINE__.'_FILE is'.$_FILES['pj']['tmp_name']); if (move_uploaded_file($_FILES['pj']['tmp_name'], $new_name)) { // echo "Image saved"; echo_debug(__FILE__.":".__LINE__."Doc saved"); $oid= pg_lo_import($this->db,$new_name); if ( $oid == false ) { echo_error('postgres.php',__LINE__,"cannot upload document"); $this->rollback(); return; } echo_debug(__FILE__,__LINE__,"Loading document"); // Remove old document $ret=$this->exec_sql("select jr_pj from jrn where jr_grpt_id=$seq"); if (pg_num_rows($ret) != 0) { $r=pg_fetch_array($ret,0); $old_oid=$r['jr_pj']; if (strlen($old_oid) != 0) pg_lo_unlink($cn,$old_oid); } // Load new document $this->exec_sql("update jrn set jr_pj=".$oid.", jr_pj_name='".$_FILES['pj']['name']."', ". "jr_pj_type='".$_FILES['pj']['type']."' where jr_grpt_id=$seq"); return $oid; } else { echo "

Error

"; $this->rollback(); } } return 0; } /*!\brief wrapper for the function pg_NumRows *\param $ret is the result of a exec_sql *\return number of line affected */ static function num_row($ret) { return pg_NumRows($ret); } /*!\brief wrapper for the function pg_fetch_array *\param $ret is the result of a pg_exec *\param $p_indice is the index *\return $array of column */ static function fetch_array($ret,$p_indice=0) { return pg_fetch_array($ret,$p_indice); } /*!\brief wrapper for the function pg_fetch_all *\param $ret is the result of pg_exec (exec_sql) *\return double array (row x col ) */ static function fetch_all($ret) { return pg_fetch_all($ret); } /*!\brief wrapper for the function pg_fetch_all *\param $ret is the result of pg_exec (exec_sql) *\param $p_row is the indice of the row *\param $p_col is the indice of the col *\return a string or an integer */ static function fetch_result($ret,$p_row=0,$p_col=0) { return pg_fetch_result($ret,$p_row,$p_col); } /*!\brief wrapper for the function pg_lo_unlink *\param $p_oid is the of oid *\return return the result of the operation */ function lo_unlink($p_oid) { return pg_lo_unlink($this->db,$p_oid); } /*!\brief wrapper for the function pg_lo_unlink *\param $p_string string name for pg_prepare function *\param $p_sql is the sql to prepare *\return return the result of the operation */ function prepare($p_string,$p_sql) { return pg_prepare($this->db,$p_string,$p_sql); } /*!\brief wrapper for the function pg_lo_unlink *\param $p_string string name for pg_prepare function *\param $p_array contains the variables *\return return the result of the operation */ function execute($p_string,$p_array) { return pg_execute($this->db,$p_string,$p_array); } /*!\brief wrapper for the function pg_lo_export *\param $p_oid is the oid of the log *\param $tmp is the file *\return result of the operation */ function lo_export($p_oid,$tmp) { return pg_lo_export($this->db,$p_oid,$tmp); } /*!\brief wrapper for the function pg_lo_export *\param $p_oid is the oid of the log *\param $tmp is the file *\return result of the operation */ function lo_import($p_oid) { return pg_lo_import($this->db,$p_oid); } /*!\brief wrapper for the function pg_escape_string *\param $p_string is the string to escape *\return escaped string */ static function escape_string($p_string) { return pg_escape_string($p_string); } /*!\brief wrapper for the function pg_close */ function close() { pg_close($this->db);} /*!\brief *\param *\return *\note *\see */ function __toString() { return "database "; } static function test_me() { } } /* test::test_me(); */