27require_once NOALYSS_INCLUDE .
'/constant.php';
28require_once NOALYSS_INCLUDE .
'/lib/ac_common.php';
54 function __construct($p_user, $p_password, $p_dbname, $p_host, $p_port)
56 $this->
db = @pg_connect(
"dbname=$p_dbname host='$p_host' user='$p_user'
57 password='$p_password' port=$p_port");
58 if ($this->
db ==
false) {
59 if ( DEBUGNOALYSS > 0 ) {
61 echo
'<h2 class="error">'._(
'Impossible de se connecter à postgreSql').
'</h2>';
63 echo _(
"Vos paramètres sont incorrectes").
": <br>";
65 printf (_(
"base de donnée = %s"), $p_dbname).
"<br>";
66 printf (_(
"Port %s"),$p_port ).
"<br>";
67 printf ( _(
"Utilisateur : %s"),$p_user ).
"<br>";
72 echo
'<h2 class="error">' . _(
'Erreur de connexion !') .
'</h2>';
73 $this->is_open =
false;
74 throw new Exception(_(
'Erreur Connexion'));
78 $this->is_open = TRUE;
88 return pg_dbname($this->ret);
98 pg_set_client_encoding($this->
db, $p_charset);
103 return pg_client_encoding($this->
db);
183 if (!$this->is_open)
throw new Exception(
' Database is closed');
184 $this->sql = $p_string;
188 if ( DEBUGNOALYSS == 0 )
189 $this->ret = pg_query($this->
db, $p_string);
191 $this->ret = @pg_query($this->
db, $p_string);
195 throw new Exception(_(
"Erreur : exec_sql attend un array"));
197 if ( DEBUGNOALYSS == 0 )
198 $this->ret =@pg_query_params($this->
db, $p_string,
$p_array);
200 $this->ret = pg_query_params($this->
db, $p_string,
$p_array);
202 if ($this->ret ==
false) {
203 $str_error = pg_last_error($this->
db) ;
204 throw new Exception(
" SQL ERROR $p_string " . $str_error, 1);
206 }
catch (Exception
$a) {
207 if ( DEBUGNOALYSS > 0 ) {
210 echo
$a->getMessage();
211 echo
$a->getTraceAsString();
212 echo pg_last_error($this->
db);
235 return pg_num_rows($r_sql);
254 $Res = $this->
exec_sql(
"select nextval('$p_seq') as seq");
274 if (!$this->is_open)
return;
283 if (!$this->is_open)
return;
296 $Res = $this->
exec_sql(
"alter sequence $p_name restart $min");
307 if ( DEBUGNOALYSS == 0 ) {
310 $debug = fopen(
"/tmp/debug_execute_script".uniqid().
".log",
"w+");
314 throw new Exception (
'Ne peut ouvrir ' .
$script);
316 printf (
" open %s <br>",
$script);
318 $flag_function =
false;
320 $buffer = fgets($hf);
321 $buffer = str_replace(
'$BODY$',
'$_$', $buffer);
322 print $buffer .
"<br>";
324 if (substr($buffer, 0, 2) ==
"--") {
329 If (Strlen($buffer) == 0) {
333 if (strpos(strtolower($buffer),
"create function") === 0) {
334 echo
"found a function";
335 $flag_function =
true;
339 if (strpos(strtolower($buffer),
"create or replace function") === 0) {
340 echo
"found a function";
341 $flag_function =
true;
346 if ($flag_function ==
false && strpos($buffer,
';') ==
false) {
350 if ($flag_function) {
351 if (strpos(strtolower($buffer),
"$$;") ===
false &&
352 strpos(strtolower($buffer),
'$_$;') ===
false &&
353 strpos(strtolower($buffer),
'$function$;') ===
false &&
354 strpos(strtolower($buffer),
'language plpgsql;') ===
false &&
355 strpos(strtolower($buffer),
'language plpgsql ;') ===
false
362 $buffer = str_replace(
';',
'', $buffer);
365 if ( DEBUGNOALYSS > 0 ) fwrite($debug,
$sql);
369 if ( DEBUGNOALYSS == 0 )
371 print "ERROR : $sql";
372 throw new Exception(
"ERROR : $sql");
375 $flag_function =
false;
379 if ( DEBUGNOALYSS == 0 )
391 if ($this->ret ==
false)
392 throw new Exception(
'this->ret is empty');
393 return pg_fetch_array($this->ret, $p_indice,PGSQL_ASSOC);
408 return pg_num_rows($this->ret);
410 return pg_num_rows($p_ret);
419 return $this->
size($p_ret);
440 $r = pg_num_rows($this->ret);
444 $array = pg_fetch_all($this->ret);
445 throw new Exception(
"Attention $p_sql retourne " . pg_num_rows($this->ret) .
" valeurs " .
446 var_export(
$p_array,
true) .
" values=" . var_export(
$array,
true));
448 $r = pg_fetch_row($this->ret, 0);
451 }
catch (Exception
$ex) {
476 if (pg_num_rows(
$r) == 0)
492 if (empty(
$array))
return null;
494 throw new Exception(_(
"Database:get_row retourne trop de lignes"), 100);
506 $sql =
"create sequence " . $p_name .
" minvalue $min";
517 $r = $this->
count_sql(
"select relname from pg_class where relname=lower($1)", array($p_name));
532 $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));
547 $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));
561 $database_exist = $this->
get_value(
'select count(*)
562 from pg_catalog.pg_database where datname = lower($1)', array($p_name));
563 return $database_exist;
573 $r = $this->
get_value(
'select count(*) from pg_largeobject_metadata where oid=$1'
588 $r = $this->
count_sql(
"select viewname from pg_views where viewname=lower($1)", array($p_name));
601 $r = $this->
count_sql(
"select nspname from pg_namespace where nspname=lower($1)", array($p_name));
623 $aIdx = array_keys($aArray[0]);
675 if (
$max == 0 && $p_null == 0)
685 $r[
$i][
'value'] =
$r[
$i - 1][
'value'];
686 $r[
$i][
'label'] =
$r[
$i - 1][
'label'];
689 $r[0][
'label'] =
" ";
709 if ($_FILES[$p_name][
"error"] == UPLOAD_ERR_NO_FILE) {
713 $new_name = tempnam($_ENV[
'TMP'], $p_name);
714 if ($_FILES[$p_name][
"error"] > 0) {
716 echo_error(__FILE__ .
":" . __LINE__ .
"Error: " . $_FILES[$p_name][
"error"]);
719 if (strlen($_FILES[$p_name][
'tmp_name']) != 0) {
720 if (move_uploaded_file($_FILES[$p_name][
'tmp_name'],
$new_name)) {
724 echo_error(__FILE__, __LINE__,
"cannot upload document");
730 echo
"<H1>Error</H1>";
746 return pg_num_rows(
$ret);
759 return pg_fetch_array(
$ret, $p_indice,$p_mode);
770 return pg_fetch_all(
$ret,PGSQL_ASSOC);
794 return pg_fetch_row(
$ret, $p_row);
806 return pg_lo_unlink($this->
db, $p_oid);
817 return pg_prepare($this->
db, $p_string, $p_sql);
830 $this->ret = pg_execute($this->
db, $p_string,
$p_array);
843 return pg_lo_export($this->
db, $p_oid, $tmp_file);
855 return pg_lo_import($this->
db, $p_filename);
868 return pg_escape_string(
$cn->db,$p_string);
877 if ($this->is_open) pg_close($this->
db);
878 $this->is_open = FALSE;
912 return pg_transaction_status($this->
db);
924 select table_schema,table_name,column_name
926 information_schema.columns
927 where table_schema not in ('information_schema','pg_catalog')
928 and data_type='oid'";
930 select oid,'N' as used from pg_largeobject_metadata
934 if ($a_table ==
false || $a_lob ==
false)
return;
936 $nb_lob =
count($a_lob);
937 $nb_table =
count($a_table);
938 for (
$i = 0;
$i < $nb_lob;
$i++) {
939 $lob = $a_lob[
$i][
'oid'];
940 if ($a_lob[
$i][
'used'] ==
'Y')
continue;
941 for ($j = 0; $j < $nb_table; $j++) {
942 if ($a_lob[
$i][
'used'] ==
'Y')
continue;
944 $a_table[$j][
'table_schema'] .
"." . $a_table[$j][
'table_name'] .
946 $a_table[$j][
'column_name'] .
"=$1", array($lob));
948 $a_lob[
$i][
'used'] =
'Y';
952 for (
$i = 0;
$i < $nb_lob;
$i++) {
953 if ($a_lob[
$i][
'used'] ==
'Y')
continue;
965 $nb_prepared = $this->
get_value(
"select count(*) from pg_prepared_statements where name=$1", [$query_name]);
966 if ($nb_prepared == 0)
return FALSE;
982 $a_header[] = $aheader[
$i][
'title'];
984 $csv->write_header($a_header);
991 switch ($aheader[
$e][
'type']) {
1007 return pg_num_fields($p_ret);
1017 $forbid_sql=array(
"update",
"delete",
"truncate",
"insert");
1019 foreach ($forbid_sql as $forbid_key) {
1020 if (stripos($p_sql,$forbid_key) !==
false)
1022 throw new Exception(_(
"Possible SQL inject"),
EXC_INVALID);
1047 pg_exec($this->
db,
'DEALLOCATE ALL');
echo_error($p_log, $p_line="", $p_message="")
log error into the /tmp/noalyss_error.log it doesn't work on windows
record_log($p_message)
Record an error message into the log file of the server.
catch(Exception $exc) if(! $g_user->can_write_action($ag_id)) $r
h( $row[ 'oa_description'])
This class allow you to connect to the postgresql database, execute sql, retrieve data.
commit()
Commit the transaction.
start()
: start a transaction
close()
wrapper for the function pg_close
get_array($p_sql, $p_array=null)
purpose return the result of a sql statment in a array
make_list($sql, $p_array=null)
create a string containing the value separated by comma for use in a SQL in statement
static fetch_all($ret)
wrapper for the function pg_fetch_all
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
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
fetch($p_indice)
fetch the $p_indice array from the last query
get_next_seq($p_seq)
get the next sequence value
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,...
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
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)
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
count_sql($p_sql, $p_array=null)
Count the number of row returned by a sql statement.
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.