forked from francoisjacquet/rosariosis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.inc.php
692 lines (614 loc) · 16.8 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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
<?php
/**
* Database functions
*
* FJ remove DatabaseType (oracle and mysql cases)
*
* @package RosarioSIS
*/
/**
* Establish DB connection
*
* @global $DatabaseServer Database server hostname
* @global $DatabaseUsername Database username
* @global $DatabasePassword Database password
* @global $DatabaseName Database name
* @global $DatabasePort Database port
* @see config.inc.php file for globals definitions
*
* @return PostgreSQL connection resource
*/
function db_start()
{
global $DatabaseServer,
$DatabaseUsername,
$DatabasePassword,
$DatabaseName,
$DatabasePort;
/**
* Fix pg_connect(): Unable to connect to PostgreSQL server:
* could not connect to server:
* No such file or directory Is the server running locally
* and accepting connections on Unix domain socket "/tmp/.s.PGSQL.5432"
*
* Always set host, force TCP.
*
* @since 3.5.2
*/
$connectstring = 'host=' . $DatabaseServer . ' ';
if ( $DatabasePort !== '5432' )
{
$connectstring .= 'port=' . $DatabasePort . ' ';
}
$connectstring .= 'dbname=' . $DatabaseName . ' user=' . $DatabaseUsername;
if ( $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;
}
/**
* Execute DB query
* pg_exec wrapper, dies on error.
*
* @since 5.1
* @since 5.2 Add $show_error optional param.
*
* @uses db_start()
* @uses db_show_error()
*
* @param string $sql SQL statement.
* @param bool $show_error Show error and die. Optional, defaults to true.
*
* @return resource PostgreSQL result resource.
*/
function db_query( $sql, $show_error = true )
{
static $connection;
if ( ! isset( $connection ) )
{
$connection = db_start();
}
$result = @pg_exec( $connection, $sql );
if ( $result === false
&& $show_error )
{
// TRANSLATION: do NOT translate these since error messages need to stay in English for technical support.
db_show_error( $sql, 'DB Execute Failed.', pg_last_error( $connection ) );
}
return $result;
}
/**
* SQL query filter
* Replace empty strings ('') with NULL values:
* - Check for ( or , character before empty string ''.
* - Check for <> or = character before empty string ''.
*
* @since 5.2
*
* @param string $sql SQL queries.
* @return string Filtered SQL queries.
*/
function db_sql_filter( $sql )
{
// Replace empty strings ('') with NULL values.
if ( stripos( $sql, 'INSERT INTO ' ) !== false )
{
// Check for ( or , character before empty string ''.
$sql = preg_replace( "/([,\(])[\r\n\t ]*''(?!')/", '\\1NULL', $sql );
}
// Check for <> or = character before empty string ''.
$sql = preg_replace( "/(<>|=)[\r\n\t ]*''(?!')/", '\\1NULL', $sql );
/**
* IS NOT NULL cases
*
* Replace <>NULL & !=NULL with IS NOT NULL
*
* @link http://www.postgresql.org/docs/current/static/functions-comparison.html
*/
$sql = str_replace(
array( '<>NULL', '!=NULL' ),
array( ' IS NOT NULL', ' IS NOT NULL' ),
$sql
);
return $sql;
}
/**
* This function connects, and does the passed query, then returns a result resource
* Not receiving the return == unusable search.
*
* @example $processable_results = DBQuery( "SELECT * FROM students" );
*
* @uses db_sql_filter()
* @uses db_query()
* @see DBGet()
*
* @since 3.7 INSERT INTO case to Replace empty strings ('') with NULL values.
* @since 4.3 Do DBQuery after action hook.
*
* @param string $sql SQL statement.
* @return resource PostgreSQL result resource
*/
function DBQuery( $sql )
{
$sql = db_sql_filter( $sql );
$result = db_query( $sql );
// Do DBQuery after action hook.
do_action( 'database.inc.php|dbquery_after', array( $sql, $result ) );
return $result;
}
/**
* Return next row
*
* @param resource PostgreSQL result resource $result Result.
* @return array Next row in result set.
*/
function db_fetch_row( $result )
{
$return = @pg_fetch_array( $result, null, PGSQL_ASSOC );
return is_array( $return ) ? @array_change_key_case( $return, CASE_UPPER ) : $return;
}
/**
* Returns code to go into SQL statement for accessing the next value of a sequence
*
* @param string $seqname PostgreSQL sequence name.
* @return sting nextval code
*/
function db_seq_nextval( $seqname )
{
// @deprecated Please update your sequence name!
$seqname = DBSeqConvertSerialName( $seqname );
return "nextval('" . DBEscapeString( $seqname ) . "')";
}
/**
* DB Sequence Next ID
*
* @example $id = DBSeqNextID( 'people_person_id_seq' );
*
* @param string $seqname Sequence name.
*
* @return int Next ID.
*/
function DBSeqNextID( $seqname )
{
// @deprecated Please update your sequence name!
$seqname = DBSeqConvertSerialName( $seqname );
$QI = DBQuery( "SELECT " . db_seq_nextval( $seqname ) . ' AS ID' );
$seq_next_RET = db_fetch_row( $QI );
return $seq_next_RET['ID'];
}
/**
* DB Sequence Convert to new based serial name
* Compatibility with old sequence names before RosarioSIS 5.0.
* Should be updated
* @see _update50beta()
*
* @since 5.0
*
* @deprecated Please update your sequence name! Remove in 6.0.
*
* @param string $seqname (Old) Sequence name.
*
* @return string New/default sequence name based on serial.
*/
function DBSeqConvertSerialName( $seqname )
{
$old_seqnames = array(
'user_profiles_seq',
'students_join_people_seq',
'students_join_address_seq',
'students_seq',
'student_report_card_grades_seq',
'student_medical_visits_seq',
'student_medical_alerts_seq',
'student_medical_seq',
'student_field_categories_seq',
'student_enrollment_codes_seq',
'student_enrollment_seq',
'staff_fields_seq',
'staff_field_categories_seq',
'staff_seq',
'school_periods_seq',
'schools_seq',
'school_gradelevels_seq',
'school_fields_seq',
'schedule_requests_seq',
'resources_seq',
'report_card_grades_seq',
'report_card_grade_scales_seq',
'report_card_comments_seq',
'report_card_comment_codes_seq',
'report_card_comment_code_scales_seq',
'report_card_comment_categories_seq',
'portal_polls_seq',
'portal_poll_questions_seq',
'portal_notes_seq',
'people_join_contacts_seq',
'people_fields_seq',
'people_field_categories_seq',
'people_seq',
'marking_period_seq',
'gradebook_assignments_seq',
'gradebook_assignment_types_seq',
'food_service_transactions_seq',
'food_service_staff_transactions_seq',
'food_service_menus_seq',
'food_service_menu_items_seq',
'food_service_items_seq',
'food_service_categories_seq',
'eligibility_activities_seq',
'discipline_referrals_seq',
'discipline_fields_seq',
'discipline_field_usage_seq',
'custom_seq',
'course_subjects_seq',
'course_period_school_periods_seq',
'courses_seq',
'course_periods_seq',
'calendar_events_seq',
'billing_payments_seq',
'billing_fees_seq',
'attendance_codes_seq',
'attendance_code_categories_seq',
'calendars_seq',
'address_fields_seq',
'address_field_categories_seq',
'address_seq',
'accounting_payments_seq',
'accounting_salaries_seq',
'accounting_incomes_seq',
'billing_fees_monthly_seq',
'school_inventory_categories_seq',
'school_inventory_items_seq',
'saved_reports_seq',
'saved_calculations_seq',
'messages_seq',
);
$new_seqnames = array(
'user_profiles_id_seq',
'students_join_people_id_seq',
'students_join_address_id_seq',
'students_student_id_seq',
'student_report_card_grades_id_seq',
'student_medical_visits_id_seq',
'student_medical_alerts_id_seq',
'student_medical_id_seq',
'student_field_categories_id_seq',
'student_enrollment_codes_id_seq',
'student_enrollment_id_seq',
'staff_fields_id_seq',
'staff_field_categories_id_seq',
'staff_staff_id_seq',
'school_periods_period_id_seq',
'schools_id_seq',
'school_gradelevels_id_seq',
'school_fields_id_seq',
'schedule_requests_request_id_seq',
'resources_id_seq',
'report_card_grades_id_seq',
'report_card_grade_scales_id_seq',
'report_card_comments_id_seq',
'report_card_comment_codes_id_seq',
'report_card_comment_code_scales_id_seq',
'report_card_comment_categories_id_seq',
'portal_polls_id_seq',
'portal_poll_questions_id_seq',
'portal_notes_id_seq',
'people_join_contacts_id_seq',
'people_fields_id_seq',
'people_field_categories_id_seq',
'people_person_id_seq',
'school_marking_periods_marking_period_id_seq',
'gradebook_assignments_assignment_id_seq',
'gradebook_assignment_types_assignment_type_id_seq',
'food_service_transactions_transaction_id_seq',
'food_service_staff_transactions_transaction_id_seq',
'food_service_menus_menu_id_seq',
'food_service_menu_items_menu_item_id_seq',
'food_service_items_item_id_seq',
'food_service_categories_category_id_seq',
'eligibility_activities_id_seq',
'discipline_referrals_id_seq',
'discipline_fields_id_seq',
'discipline_field_usage_id_seq',
'custom_fields_id_seq',
'course_subjects_subject_id_seq',
'course_period_school_periods_course_period_school_periods_id_seq',
'courses_course_id_seq',
'course_periods_course_period_id_seq',
'calendar_events_id_seq',
'billing_payments_id_seq',
'billing_fees_id_seq',
'attendance_codes_id_seq',
'attendance_code_categories_id_seq',
'attendance_calendars_calendar_id_seq',
'address_fields_id_seq',
'address_field_categories_id_seq',
'address_address_id_seq',
'accounting_payments_id_seq',
'accounting_salaries_id_seq',
'accounting_incomes_id_seq',
'billing_fees_monthly_id_seq',
'school_inventory_categories_category_id_seq',
'school_inventory_items_item_id_seq',
'saved_reports_id_seq',
'saved_calculations_id_seq',
'messages_message_id_seq',
);
return str_ireplace(
$old_seqnames,
$new_seqnames,
$seqname
);
}
/**
* Start transaction
*
* @deprecated $connection param since 5.2
*
* @param PostgreSQL connection resource $connection Connection. DEPRECATED.
* @return void
*/
function db_trans_start( $connection = false )
{
db_query( 'BEGIN TRANSACTION;' );
}
/**
* Run query on transaction -- if failure, runs rollback
*
* @since 5.2 $connection param removed.
*
* @param string $sql SQL statement.
* @return PostgreSQL result resource
*/
function db_trans_query( $sql, $show_error = true )
{
$sql = db_sql_filter( $sql );
$result = db_query( $sql, $show_error );
if ( $result === false )
{
// Rollback commands.
db_trans_rollback();
}
return $result;
}
/**
* Commit changes
*
* @deprecated $connection param since 5.2
*
* @param PostgreSQL connection resource $connection Connection. DEPRECATED.
* @return void
*/
function db_trans_commit( $connection = false )
{
db_query( 'COMMIT TRANSACTION;' );
}
/**
* Rollback changes
*
* @since 5.2
*
* @return void
*/
function db_trans_rollback()
{
db_query( 'ROLLBACK TRANSACTION;' );
}
/**
* Dry run query on transaction -- rollback anyway
* Useful to check first if foreign key constraints are preventing DELETE.
*
* @since 5.2
*
* @example $can_delete = DBTransDryRun( UserDeleteSQL( UserStaffID() ) );
*
* @param string $sql SQL statement.
* @return PostgreSQL result resource
*/
function DBTransDryRun( $sql )
{
db_trans_start();
$result = db_trans_query( $sql, false );
if ( $result !== false )
{
// Rollback transaction anyway.
db_trans_rollback();
}
return $result;
}
/**
* Generate CASE-WHEN condition
*
* @example db_case( array( 'FAILED_LOGIN', "''", '1', 'FAILED_LOGIN+1' ) )
* will return ' CASE WHEN FAILED_LOGIN IS NULL THEN 1 ELSE FAILED_LOGIN+1 END '
*
* @param array $array array( Column, IS, THEN, ELSE ).
* @return string CASE-WHEN condition
*/
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;
}
/**
* 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.
*
* @param string $table DB Table.
* @return array Table properties
*/
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( DBEscapeString( $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;
}
/**
* Show SQL error message
* Send notification email if `$RosarioNotifyAddress` or `$RosarioErrorsAddress` set
*
* @global string $RosarioNotifyAddress or $RosarioErrorsAddress email set in config.inc.php file
* @since 4.0 Uses ErrorSendEmail()
* @since 4.6 Show SQL query.
*
* @param string $sql SQL statement.
* @param string $failnote Failure Notice.
* @param string $additional Additional Information.
*/
function db_show_error( $sql, $failnote, $additional = '' )
{
global $RosarioNotifyAddress,
$RosarioErrorsAddress;
// TRANSLATION: do NOT translate these since error messages need to stay in English for technical support.
?>
<br />
<table class="postbox cellspacing-0">
<thead><tr><th class="center">
<h3><?php echo function_exists( '_' ) ?
_( 'We have a problem, please contact technical support ...' ) :
// PHP gettext extension not loaded, and polyfill either (PHPCompatibility functions not loaded yet).
'We have a problem, please contact technical support ...'; ?></h3>
</th></tr></thead>
<tbody><tr><td class="popTable">
<table>
<tr>
<td><?php echo date( 'm/d/Y H:i:s' ); ?><br />
<span class="legend-gray">Date</span></td>
</tr>
<tr>
<td><?php echo $failnote; ?> <?php echo $additional; ?><br />
<span class="legend-gray">Failure Notice</span></td>
</tr>
<tr>
<td><pre class="size-1" style="max-width: 65vw; overflow: auto;"><?php echo str_replace( "\t\t", '', $sql ); ?></pre>
<span class="legend-gray">SQL query</span></td>
</tr>
</table>
</td></tr></tbody></table>
<?php
// Send notification email if $RosarioNotifyAddress set & functions loaded.
$db_error_email = ! empty( $RosarioErrorsAddress ) ? $RosarioErrorsAddress : $RosarioNotifyAddress;
if ( function_exists( 'ErrorSendEmail' ) )
{
$db_error = array(
'Failure Notice: ' . $failnote,
'Additional Info: ' . $additional,
$sql,
);
ErrorSendEmail( $db_error, 'Database Error' );
}
die();
}
/**
* Escapes single quotes by using two for every one.
*
* @example $safe_string = DBEscapeString( $string );
*
* @param string $input Input string.
* @return string escaped string
*/
function DBEscapeString( $input )
{
// return str_replace("'","''",$input);
return pg_escape_string( $input );
}
/**
* Escapes identifiers (table, column) using double quotes.
* Security function for
* when you HAVE to use a variable as an identifier.
*
* @example $safe_sql = "SELECT COLUMN FROM " . DBEscapeIdentifier( $table ) . " WHERE " . DBEscapeIdentifier( $column ) . "='Y'";
* @uses pg_escape_identifier(), requires PHP 5.4.4+
* @since 3.0
*
* @param string $identifier SQL identifier (table, column).
* @return string Escaped identifier.
*/
function DBEscapeIdentifier( $identifier )
{
$identifier = mb_strtolower( $identifier );
if ( ! function_exists( 'pg_escape_identifier' ) )
{
return '"' . $identifier . '"';
}
return pg_escape_identifier( $identifier );
}