27require_once NOALYSS_INCLUDE .
'/constant.php';
28require_once NOALYSS_INCLUDE .
'/lib/ac_common.php';
59 function __construct($p_user, $p_password, $p_dbname, $p_host, $p_port)
61 $this->
db = @pg_connect(
"dbname=$p_dbname host='$p_host' user='$p_user'
62 password='$p_password' port=$p_port");
63 if ($this->
db ==
false) {
64 if ( DEBUGNOALYSS > 0 ) {
66 echo
'<h2 class="error">'._(
'Impossible de se connecter à postgreSql').
'</h2>';
68 echo
_(
"Vos paramètres sont incorrectes").
": <br>";
70 printf (
_(
"base de donnée = %s"), $p_dbname).
"<br>";
71 printf (
_(
"Port %s"),$p_port ).
"<br>";
72 printf (
_(
"Utilisateur : %s"),$p_user ).
"<br>";
77 echo
'<h2 class="error">' .
_(
'Erreur de connexion !') .
'</h2>';
78 $this->is_open =
false;
79 throw new Exception(
_(
'Erreur Connexion'));
82 $this->dbport=$p_port;
83 $this->dbname=$p_dbname;
84 $this->dbhost=$p_host;
85 $this->dbuser=$p_user;
86 $this->is_open = TRUE;
111 return pg_dbname($this->
db);
121 pg_set_client_encoding($this->
db, $p_charset);
126 return pg_client_encoding($this->
db);
206 if (!$this->is_open)
throw new Exception(
' Database is closed');
207 $this->sql = $p_string;
211 if ( DEBUGNOALYSS == 0 )
212 $this->ret = pg_query($this->
db, $p_string);
214 $this->ret = @pg_query($this->
db, $p_string);
218 throw new Exception(
_(
"Erreur : exec_sql attend un array"));
220 if ( DEBUGNOALYSS == 0 )
221 $this->ret =@pg_query_params($this->
db, $p_string,
$p_array);
223 $this->ret = pg_query_params($this->
db, $p_string,
$p_array);
225 if ($this->ret ==
false) {
226 $str_error = pg_last_error($this->
db) ;
227 throw new Exception(
" SQL ERROR $p_string " . $str_error, 1);
229 }
catch (Exception
$a) {
230 if ( DEBUGNOALYSS > 0 ) {
233 echo
$a->getMessage();
234 echo
$a->getTraceAsString();
235 echo pg_last_error($this->
db);
242 throw new \Exception(
"exec_sql fails",242,
$a);
258 return pg_num_rows($r_sql);
277 $Res = $this->
exec_sql(
"select nextval('$p_seq') as seq");
297 if (!$this->is_open)
return;
306 if (!$this->is_open)
return;
319 $Res = $this->
exec_sql(
"alter sequence $p_name restart $min");
330 if ( DEBUGNOALYSS == 0 ) {
333 $debug = fopen(
"/tmp/debug_execute_script".uniqid().
".log",
"w+");
337 throw new Exception (
'Ne peut ouvrir ' .
$script);
339 printf (
" open %s <br>",
$script);
341 $flag_function =
false;
343 $buffer = fgets($hf);
344 $buffer = str_replace(
'$BODY$',
'$_$', $buffer);
345 print $buffer .
"<br>";
347 if (substr($buffer, 0, 2) ==
"--") {
352 If (Strlen($buffer) == 0) {
356 if (strpos(strtolower($buffer),
"create function") === 0) {
357 echo
"found a function";
358 $flag_function =
true;
362 if (strpos(strtolower($buffer),
"create or replace function") === 0) {
363 echo
"found a function";
364 $flag_function =
true;
369 if ($flag_function ==
false && strpos($buffer,
';') ==
false) {
373 if ($flag_function) {
374 if (strpos(strtolower($buffer),
"$$;") ===
false &&
375 strpos(strtolower($buffer),
'$_$;') ===
false &&
376 strpos(strtolower($buffer),
'$function$;') ===
false &&
377 strpos(strtolower($buffer),
'language plpgsql;') ===
false &&
378 strpos(strtolower($buffer),
'language plpgsql ;') ===
false
385 $buffer = str_replace(
';',
'', $buffer);
388 if ( DEBUGNOALYSS > 0 ) fwrite($debug,
$sql);
392 if ( DEBUGNOALYSS == 0 )
394 print "ERROR : $sql";
395 throw new Exception(
"ERROR : $sql");
398 $flag_function =
false;
402 if ( DEBUGNOALYSS == 0 )
413 function fetch($p_indice,$p_mode= PGSQL_ASSOC)
415 if ($this->ret ==
false)
416 throw new Exception(
'this->ret is empty');
417 return pg_fetch_array($this->ret, $p_indice,$p_mode);
432 return pg_num_rows($this->ret);
434 return pg_num_rows($p_ret);
443 return $this->
size($p_ret);
464 $r = pg_num_rows($this->ret);
468 $array = pg_fetch_all($this->ret);
469 throw new Exception(
"Attention $p_sql retourne " . pg_num_rows($this->ret) .
" valeurs " .
470 var_export(
$p_array,
true) .
" values=" . var_export(
$array,
true));
472 $r = pg_fetch_row($this->ret, 0);
475 }
catch (Exception
$ex) {
501 if (pg_num_rows(
$r) == 0)
517 if (empty(
$array))
return null;
519 throw new Exception(
_(
"Database:get_row retourne trop de lignes"), 100);
531 $sql =
"create sequence " . $p_name .
" minvalue $min";
542 $r = $this->
count_sql(
"select relname from pg_class where relname=lower($1)", array($p_name));
557 $r = $this->
count_sql(
"select table_name from information_schema.tables where table_schema=$1 and table_name=lower($2)", array($p_schema, $p_name));
572 $r = $this->
get_value(
'select count(*) from information_schema.columns where table_name=lower($1) and column_name=lower($2) and table_schema=lower($3)', array($col,
$table, $schema));
586 $database_exist = $this->
get_value(
'select count(*)
587 from pg_catalog.pg_database where datname = lower($1)', array($p_name));
588 return $database_exist;
598 $r = $this->
get_value(
'select count(*) from pg_largeobject_metadata where oid=$1'
613 $r = $this->
count_sql(
"select viewname from pg_views where viewname=lower($1)", array($p_name));
626 $r = $this->
count_sql(
"select nspname from pg_namespace where nspname=lower($1)", array($p_name));
648 $aIdx = array_keys($aArray[0]);
700 if (
$max == 0 && $p_null == 0)
710 $r[
$i][
'value'] =
$r[
$i - 1][
'value'];
711 $r[
$i][
'label'] =
$r[
$i - 1][
'label'];
714 $r[0][
'label'] =
" ";
733 function upload($p_name,$only_oid =
false)
738 if ( $this->
status() !== PGSQL_TRANSACTION_INTRANS ) {
744 if ($_FILES[$p_name][
"error"] == UPLOAD_ERR_NO_FILE) {
745 \record_log(
"DC759: error upload file".var_export($_FILES,
true));
750 $new_name = tempnam($_ENV[
'TMP'], $p_name);
751 if ($_FILES[$p_name][
"error"] > 0) {
752 \record_log(
"DC740: error upload file".var_export($_FILES,
true));
756 if (strlen($_FILES[$p_name][
'tmp_name']) != 0) {
757 if (move_uploaded_file($_FILES[$p_name][
'tmp_name'],
$new_name)) {
761 \record_log(
"DC747: error upload file".var_export($_FILES,
true).
"SQL MESSAGE". pg_last_error($this->
db));
769 return [
"oid"=>$oid,
'filename'=>
$new_name];
772 \record_log(
"DC754: move_uploaded fails".var_export($_FILES,
true));
778 \record_log(
"DC576: Files error names empty".var_export($_FILES,
true));
792 if ( $this->
status() !== PGSQL_TRANSACTION_INTRANS ) {
797 $oid= pg_lo_create($this->
db);
799 if ( ($handle=pg_lo_open($this->
db,$oid,
"w")) ==
false ) {
return false ;}
800 pg_lo_write($handle, $binary_data);
801 pg_lo_close($handle);
815 if ( $this->
status() !== PGSQL_TRANSACTION_INTRANS ) {
820 $handle=pg_lo_open($this->
db,$oid,
"r");
821 if ( $handle ==
false ) {
return false ;}
823 pg_lo_seek($handle, 0, PGSQL_SEEK_END);
825 $size= pg_lo_tell($handle);
827 pg_lo_seek($handle, 0, PGSQL_SEEK_SET);
829 $binary_data = pg_lo_read($handle,
$size );
830 pg_lo_close($handle);
842 if ($this->
status() !== PGSQL_TRANSACTION_INTRANS) {
846 $handle = pg_lo_open($this->
db, $oid,
"w");
847 if ( $handle ==
false ) {
return false ;}
848 pg_lo_truncate($handle, 0);
849 pg_lo_write($handle, $binary_data);
850 pg_lo_close($handle);
865 return pg_num_rows(
$ret);
878 return pg_fetch_array(
$ret, $p_indice,$p_mode);
890 return pg_fetch_all(
$ret,$p_mode);
914 return pg_fetch_row(
$ret, $p_row);
926 return pg_lo_unlink($this->
db, $p_oid);
937 return pg_prepare($this->
db, $p_string, $p_sql);
950 $this->ret = pg_execute($this->
db, $p_string,
$p_array);
963 return pg_lo_export($this->
db, $p_oid, $tmp_file);
975 return pg_lo_import($this->
db, $p_filename);
988 return pg_escape_string(
$cn->db,$p_string);
997 if ($this->is_open) pg_close($this->
db);
998 $this->is_open = FALSE;
1032 return pg_transaction_status($this->
db);
1044 select table_schema,table_name,column_name
1046 information_schema.columns
1047 where table_schema not in ('information_schema','pg_catalog')
1048 and data_type='oid'";
1050 select oid,'N' as used from pg_largeobject_metadata
1054 if ($a_table ==
false || $a_lob ==
false)
return;
1056 $nb_lob =
count($a_lob);
1057 $nb_table =
count($a_table);
1058 for (
$i = 0;
$i < $nb_lob;
$i++) {
1059 $lob = $a_lob[
$i][
'oid'];
1060 if ($a_lob[
$i][
'used'] ==
'Y')
continue;
1061 for ($j = 0; $j < $nb_table; $j++) {
1062 if ($a_lob[
$i][
'used'] ==
'Y')
continue;
1064 $a_table[$j][
'table_schema'] .
"." . $a_table[$j][
'table_name'] .
1066 $a_table[$j][
'column_name'] .
"=$1", array($lob));
1068 $a_lob[
$i][
'used'] =
'Y';
1072 for (
$i = 0;
$i < $nb_lob;
$i++) {
1073 if ($a_lob[
$i][
'used'] ==
'Y')
continue;
1085 $nb_prepared = $this->
get_value(
"select count(*) from pg_prepared_statements where name=$1", [$query_name]);
1086 if ($nb_prepared == 0)
return FALSE;
1104 $a_header[] = $aheader[
$i][
'title'];
1106 $csv->write_header($a_header);
1114 foreach (
$row as $row_item){
1115 if (
$e >=
count($a_header))
break;
1116 switch ($aheader[
$e][
'type']) {
1118 $csv->add($row_item,
"number");
1121 $csv->add($row_item);
1133 return pg_num_fields($p_ret);
1143 $forbid_sql=array(
"update",
"delete",
"truncate",
"insert");
1145 foreach ($forbid_sql as $forbid_key) {
1146 if (stripos($p_sql,$forbid_key) !==
false)
1148 throw new Exception(
_(
"Possible SQL inject"),
EXC_INVALID);
1173 pg_exec($this->
db,
'DEALLOCATE ALL');
record_log($p_message)
Record an error message into the log file of the server or in the log folder of NOALYSS Record also t...
catch(Exception $exc) if(! $g_user->can_write_action($ag_id)) $r
h( $row[ 'oa_description'])
_("actif, passif,charge,...")
This class allow you to connect to the postgresql database, execute sql, retrieve data.
lo_read($oid)
read a Large object with data content in a binary
commit()
Commit the transaction.
start()
: start a transaction
close()
wrapper for the function pg_close
make_list($sql, $p_array=null)
create a string containing the value separated by comma for use in a SQL in statement
alter_seq($p_name, $min)
alter the sequence value
static escape_string($p_string)
wrapper for the function pg_escape_string
exist_column($col, $table, $schema)
Check if a column exists in a table.
lo_export($p_oid, $tmp_file)
wrapper for the function pg_lo_export
$dbhost
!< $dbport (int) Database port
static fetch_result($ret, $p_row=0, $p_col=0)
wrapper for the function pg_fetch_all
$sql
last SQL stmt executed
execute($p_string, $p_array)
wrapper for the function pg_execute
rollback()
rollback the current transaction
is_prepare($query_name)
Check if a prepared statement already exists or not.
get_value($p_sql, $p_array=null)
return the value of the sql, the sql will return only one value with the value
size($p_ret=null)
return the number of rows found by the last query, or the number of rows from $p_ret
get_sql()
last SQL stmt executed
count($p_ret=null)
synomym for size()
exist_sequence($p_name)
test if a sequence exist
get_next_seq($p_seq)
get the next sequence value
fetch($p_indice, $p_mode=PGSQL_ASSOC)
fetch the $p_indice array from the last query
clear_all_prepare()
clear all prepare stmt
make_array($p_sql, $p_null=0, $p_array=null)
make a array with the sql.
exist_blob($p_oid)
check if the large object exists
static fetch_row($ret, $p_row)
wrapper for the function pg_fetch_row
clear_prepare($sql_name)
clear a prepare stmt
lo_import($p_filename)
wrapper for the function pg_lo_export
search_sql_inject($p_sql)
FInd if a SQL Select has a SQL stmt to inject or damage Data When a SELECT SQL string is build,...
upload($p_name, $only_oid=false)
Save one or several documents into the database , it just puts the file in the database and returns t...
static fetch_array($ret, $p_indice=0, $p_mode=PGSQL_ASSOC)
wrapper for the function pg_fetch_array
status()
get the transaction status : The status can be
get_affected()
return the number of rows affected by the previous query
$dbuser
!< $dbhost(string) Database host
get_current_seq($p_seq)
get the current sequence value
create_sequence($p_name, $min=1)
Create a sequence.
exec_sql($p_string, $p_array=null)
send a sql string to the database
lo_unlink($p_oid)
wrapper for the function pg_lo_unlink
set_sql($sql)
last SQL stmt executed
clean_orphan_lob()
Find all lob and remove those which are not used by any tables.
__construct($p_user, $p_password, $p_dbname, $p_host, $p_port)
!< $dbuser(string) Database user
lo_write($binary_data)
large_object writee: create a Large object if oid is not given with data content in a binaray
static fetch_all($ret, $p_mode=PGSQL_ASSOC)
wrapper for the function pg_fetch_all
get_array($p_sql, $p_array=null, $p_mode=PGSQL_ASSOC)
purpose return the result of a sql statment in a array
static num_row($ret)
wrapper for the function pg_num_rows
get_name()
return the name of the current database
execute_script($script)
Execute a sql script.
exist_database($p_name)
Count the database name in a system view.
query_to_csv($ret, $aheader)
with the handle of a successull query, echo each row into CSV and send it directly to output.
exist_schema($p_name)
test if a schema exists
$dbport
!< $dbname (string) Database name
count_sql($p_sql, $p_array=null)
Count the number of row returned by a sql statement.
lo_replace($binary_data, $oid)
replace a Large object with data content in a binary
exist_table($p_name, $p_schema='public')
test if a table exist
prepare($p_string, $p_sql)
wrapper for the function pg_prepare
static nb_column($p_ret)
Returns the number of columns in a ret.
get_row($p_sql, $p_array=NULL)
Returns only one row from a query.
contains the class for connecting to Noalyss
Manage the CSV : manage files and write CSV record.
if(in_array( $type, array( 'CHA', 'ACT', 'PASINV', 'PROINV')) &&$tot_deb< $tot_cred) if(in_array($type, array('PRO', 'PAS', 'ACTINV', 'CHAINV')) && $tot_deb > $tot_cred) $size