-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.inc.php
257 lines (221 loc) · 7.16 KB
/
database.inc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<?php
//modif Francois: remove DatabaseType (oracle and mysql cases)
// Establish DB connection.
function db_start()
{ global $DatabaseServer,$DatabaseUsername,$DatabasePassword,$DatabaseName,$DatabasePort;
$connectstring = '';
if($DatabaseServer!='localhost')
$connectstring = "host=$DatabaseServer ";
if($DatabasePort!='5432')
$connectstring .= "port=$DatabasePort ";
$connectstring .= "dbname=$DatabaseName user=$DatabaseUsername";
if(!empty($DatabasePassword))
$connectstring.=" password=$DatabasePassword";
$connection = pg_connect($connectstring);
// Error code for both.
if($connection===false)
{
// TRANSLATION: do NOT translate these since error messages need to stay in English for technical support
db_show_error("",sprintf('Could not Connect to Database Server \'%s\'',$DatabaseServer),pg_last_error());
}
return $connection;
}
// This function connects, and does the passed query, then returns a connection identifier.
// Not receiving the return == unusable search.
// ie, $processable_results = DBQuery("select * from students");
function DBQuery($sql)
{
$connection = db_start();
// TRANSLATION: do NOT translate these since error messages need to stay in English for technical support
$sql = preg_replace("/([,\(=])[\r\n\t ]*''[\r\n\t]*(?!')/",'\\1NULL',$sql);
$result = @pg_exec($connection,$sql);
if($result===false)
{
$errstring = pg_last_error($connection);
db_show_error($sql,"DB Execute Failed.",$errstring);
}
return $result;
}
// return next row.
function db_fetch_row($result)
{
$return = @pg_fetch_array($result);
if(is_array($return))
{
foreach($return as $key => $value)
{
if(is_int($key))
unset($return[$key]);
}
}
return @array_change_key_case($return,CASE_UPPER);
}
// returns code to go into SQL statement for accessing the next value of a sequenc function db_seq_nextval($seqname)
function db_seq_nextval($seqname)
{
$seq = "nextval('".$seqname."')";
return $seq;
}
// start transaction
function db_trans_start($connection)
{
db_trans_query($connection,"BEGIN WORK");
}
// run query on transaction -- if failure, runs rollback.
function db_trans_query($connection,$sql)
{
// TRANSLATION: do NOT translate these since error messages need to stay in English for technical support
$sql = preg_replace("/([,\(=])[\r\n\t ]*''/",'\\1NULL',$sql);
$result = pg_query($connection,$sql);
if($result===false)
{
db_trans_rollback($connection);
db_show_error($sql,"DB Transaction Execute Failed.");
}
return $result;
}
// rollback commands.
function db_trans_rollback($connection)
{
pg_query($connection,"ROLLBACK");
}
// commit changes.
function db_trans_commit($connection)
{
pg_query($connection,"COMMIT");
}
// keyword mapping.
define("FROM_DUAL"," ");
// DECODE and CASE-WHEN support
function db_case($array)
{
$counter=0;
$array_count=count($array);
$string = " CASE WHEN $array[0] =";
$counter++;
$arr_count = count($array);
for($i=1;$i<$arr_count;$i++)
{
$value = $array[$i];
if($value=="''" && mb_substr($string,-1)=='=')
{
$value = ' IS NULL';
$string = mb_substr($string,0,-1);
}
$string.="$value";
if($counter==($array_count-2) && $array_count%2==0)
$string.=" ELSE ";
elseif($counter==($array_count-1))
$string.=" END ";
elseif($counter%2==0)
$string.=" WHEN $array[0]=";
elseif($counter%2==1)
$string.=" THEN ";
$counter++;
}
return $string;
}
// String position.
function db_strpos($args)
{
$ret = 'strpos(';
foreach($args as $value)
$ret .= $value . ',';
$ret = mb_substr($ret,0,-1) . ')';
return $ret;
}
// CONVERT VARCHAR TO NUMERIC
function db_to_number($text)
{
return '('.$text.')::text::float::numeric';
}
// greatest/least - builtin to postgres 8 but not 7
function db_greatest($a,$b)
{
return "greatest($a,$b)";
}
function db_least($a,$b)
{
return "least($a,$b)";
}
// returns an array with the field names for the specified table as key with subkeys
// of SIZE, TYPE, SCALE and NULL. TYPE: varchar, numeric, etc.
function db_properties($table)
{
$sql = "SELECT a.attnum,a.attname AS field,t.typname AS type,
a.attlen AS length,a.atttypmod AS lengthvar,
a.attnotnull AS notnull
FROM pg_class c, pg_attribute a, pg_type t
WHERE c.relname = '".mb_strtolower($table)."'
and a.attnum > 0 and a.attrelid = c.oid
and a.atttypid = t.oid ORDER BY a.attnum";
$result = DBQuery($sql);
while($row = db_fetch_row($result))
{
$properties[mb_strtoupper($row['FIELD'])]['TYPE'] = mb_strtoupper($row['TYPE']);
if(mb_strtoupper($row['TYPE'])=="NUMERIC")
{
$properties[mb_strtoupper($row['FIELD'])]['SIZE'] = ($row['LENGTHVAR'] >> 16) & 0xffff;
$properties[mb_strtoupper($row['FIELD'])]['SCALE'] = ($row['LENGTHVAR'] -4) & 0xffff;
}
else
{
if($row['LENGTH']>0)
$properties[mb_strtoupper($row['FIELD'])]['SIZE'] = $row['LENGTH'];
elseif($row['LENGTHVAR']>0)
$properties[mb_strtoupper($row['FIELD'])]['SIZE'] = $row['LENGTHVAR']-4;
}
if ($row['NOTNULL']=='t')
$properties[mb_strtoupper($row['FIELD'])]['NULL'] = "N";
else
$properties[mb_strtoupper($row['FIELD'])]['NULL'] = "Y";
}
return $properties;
}
function db_show_error($sql,$failnote,$additional='')
{ global $RosarioVersion,$RosarioNotifyAddress;
echo '<BR />';
PopTable('header',_('We have a problem, please contact technical support ...'));
// TRANSLATION: do NOT translate these since error messages need to stay in English for technical support
echo '
<TABLE style="border-collapse:separate; border-spacing:10px;">
<TR>
<TD style="text-align:right"><b>Date:</b></TD>
<TD><pre>'.date("m/d/Y h:i:s").'</pre></TD>
</TR><TR>
<TD style="text-align:right"><b>Failure Notice:</b></TD>
<TD><pre> '.$failnote.' </pre></TD>
</TR><TR>
<TD style="text-align:right"><b>Additional Information:</b></TD>
<TD>'.$additional.'</TD>
</TR>
</TABLE>';
//Something you have asked the system to do has thrown a database error. A system administrator has been notified, and the problem will be fixed as soon as possible. It might be that changing the input parameters sent to this program will cause it to run properly. Thanks for your patience.
PopTable('footer');
echo "<!-- SQL STATEMENT: \n\n $sql \n\n -->";
if($RosarioNotifyAddress)
{
$message = "System: ".ParseMLField(Config('TITLE'))." \n";
$message .= "Date: ".date("m/d/Y h:i:s")."\n";
$message .= "Page: ".$_SERVER['PHP_SELF'].' '.ProgramTitle()." \n\n";
$message .= "Failure Notice: $failnote \n";
$message .= "Additional Info: $additional \n";
$message .= "\n $sql \n";
$message .= "Request Array: \n".print_r($_REQUEST, true);
$message .= "\n\nSession Array: \n".print_r($_SESSION, true);
//modif Francois: add email headers
$headers = 'From:'.$RosarioNotifyAddress."\r\n";
$headers .= 'Return-Path:'.$RosarioNotifyAddress."\r\n";
$headers .= 'Reply-To:'.$RosarioNotifyAddress . "\r\n" . 'X-Mailer: PHP/' . phpversion();
$params = '-f '.$RosarioNotifyAddress;
mail($RosarioNotifyAddress,'Rosario Database Error',utf8_decode($message),$headers, $params);
}
die();
}
// $safe_string = DBEscapeString($string). Escapes single quotes by using two for every one.
function DBEscapeString($input)
{
return pg_escape_string($input);
//return str_replace("'","''",$input);
}
?>