PageRenderTime 44ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/database.inc.php

https://github.com/jlahm/Belize-openSIS
PHP | 450 lines | 204 code | 32 blank | 214 comment | 18 complexity | b2801512662a879e2fffdcc6e728071e MD5 | raw file
  1. <?php
  2. #**************************************************************************
  3. # openSIS is a free student information system for public and non-public
  4. # schools from Open Solutions for Education, Inc. web: www.os4ed.com
  5. #
  6. # openSIS is web-based, open source, and comes packed with features that
  7. # include student demographic info, scheduling, grade book, attendance,
  8. # report cards, eligibility, transcripts, parent portal,
  9. # student portal and more.
  10. #
  11. # Visit the openSIS web site at http://www.opensis.com to learn more.
  12. # If you have question regarding this system or the license, please send
  13. # an email to info@os4ed.com.
  14. #
  15. # This program is released under the terms of the GNU General Public License as
  16. # published by the Free Software Foundation, version 2 of the License.
  17. # See license.txt.
  18. #
  19. # This program is distributed in the hope that it will be useful,
  20. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. # GNU General Public License for more details.
  23. #
  24. # You should have received a copy of the GNU General Public License
  25. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  26. #
  27. #***************************************************************************************
  28. // Establish MySQL DB connection.
  29. include('Redirect_root.php');
  30. function db_start()
  31. { global $DatabaseServer,$DatabaseUsername,$DatabasePassword,$DatabaseName,$DatabasePort,$DatabaseType;
  32. switch($DatabaseType)
  33. {
  34. case 'mysql':
  35. $connection = mysql_connect($DatabaseServer,$DatabaseUsername,$DatabasePassword);
  36. mysql_select_db($DatabaseName);
  37. break;
  38. }
  39. // Error code for both.
  40. if($connection === false)
  41. {
  42. switch($DatabaseType)
  43. {
  44. case 'mysql':
  45. $errormessage = mysql_error($connection);
  46. break;
  47. }
  48. db_show_error("","Could not Connect to Database: $DatabaseServer",$errstring);
  49. }
  50. return $connection;
  51. }
  52. // This function connects, and does the passed query, then returns a connection identifier.
  53. // Not receiving the return == unusable search.
  54. // ie, $processable_results = DBQuery("select * from students");
  55. function DBQuery($sql)
  56. { global $DatabaseType,$_openSIS;
  57. $connection = db_start();
  58. switch($DatabaseType)
  59. {
  60. case 'mysql':
  61. $sql = str_replace('&amp;', "", $sql);
  62. $sql = str_replace('&quot', "", $sql);
  63. $sql = str_replace('&#039;', "", $sql);
  64. $sql = str_replace('&lt;', "", $sql);
  65. $sql = str_replace('&gt;', "", $sql);
  66. $sql = ereg_replace("([,\(=])[\r\n\t ]*''",'\\1NULL',$sql);
  67. if(preg_match_all("/'(\d\d-[A-Za-z]{3}-\d{2,4})'/",$sql,$matches))
  68. {
  69. foreach($matches[1] as $match)
  70. {
  71. $dt = date('Y-m-d',strtotime($match));
  72. $sql = preg_replace("/'$match'/","'$dt'",$sql);
  73. }
  74. }
  75. if(substr($sql,0,6)=="BEGIN;")
  76. {
  77. $array = explode( ";", $sql );
  78. foreach( $array as $value )
  79. {
  80. if($value!="")
  81. {
  82. $result = mysql_query($value);
  83. if(!$result)
  84. {
  85. mysql_query("ROLLBACK");
  86. die(db_show_error($sql,"DB Execute Failed.",mysql_error()));
  87. }
  88. }
  89. }
  90. }
  91. else
  92. {
  93. $result = mysql_query($sql) or die(db_show_error($sql,"DB Execute Failed.",mysql_error()));
  94. }
  95. break;
  96. }
  97. return $result;
  98. }
  99. // return next row.
  100. function db_fetch_row($result)
  101. { global $DatabaseType;
  102. switch($DatabaseType)
  103. {
  104. case 'mysql':
  105. $return = mysql_fetch_array($result);
  106. if(is_array($return))
  107. {
  108. foreach($return as $key => $value)
  109. {
  110. if(is_int($key))
  111. unset($return[$key]);
  112. }
  113. }
  114. break;
  115. }
  116. return @array_change_key_case($return,CASE_UPPER);
  117. }
  118. // returns code to go into SQL statement for accessing the next value of a sequence function db_seq_nextval($seqname)
  119. function db_seq_nextval($seqname)
  120. { global $DatabaseType;
  121. if($DatabaseType=='mysql')
  122. $seq="fn_".strtolower($seqname)."()";
  123. return $seq;
  124. }
  125. // start transaction
  126. /*
  127. function db_trans_start($connection)
  128. { global $DatabaseType;
  129. if($DatabaseType=='postres')
  130. db_trans_query($connection,"BEGIN WORK");
  131. }
  132. */
  133. // run query on transaction -- if failure, runs rollback.
  134. /*
  135. function db_trans_query($connection,$sql)
  136. { global $DatabaseType;
  137. if($DatabaseType=='oracle')
  138. {
  139. $parse = ociparse($connection,$sql);
  140. if($parse===false)
  141. {
  142. db_trans_rollback($connection);
  143. db_show_error($sql,"DB Transaction Parse Failed.");
  144. }
  145. $result=OciExecute($parse,OCI_DEFAULT);
  146. if ($result===false)
  147. {
  148. db_trans_rollback($connection);
  149. db_show_error($sql,"DB Transaction Execute Failed.");
  150. }
  151. $result=$parse;
  152. }
  153. elseif($DatabaseType=='postgres')
  154. {
  155. $sql = ereg_replace("([,\(=])[\r\n\t ]*''",'\\1NULL',$sql);
  156. $result = pg_query($connection,$sql);
  157. if($result===false)
  158. {
  159. db_trans_rollback($connection);
  160. db_show_error($sql,"DB Transaction Execute Failed.");
  161. }
  162. }
  163. return $result;
  164. }
  165. */
  166. // rollback commands.
  167. /*
  168. function db_trans_rollback($connection)
  169. { global $DatabaseType;
  170. if($DatabaseType=='oracle')
  171. OCIRollback($connection);
  172. elseif($DatabaseType=='postgres')
  173. pg_query($connection,"ROLLBACK");
  174. }
  175. */
  176. // commit changes.
  177. /*
  178. function db_trans_commit($connection)
  179. { global $DatabaseType;
  180. if($DatabaseType=='oracle')
  181. OCICommit($connection);
  182. elseif($DatabaseType=='postgres')
  183. pg_query($connection,"COMMIT");
  184. }
  185. */
  186. // keyword mapping.
  187. /*
  188. if($DatabaseType=='oracle')
  189. define("FROM_DUAL"," FROM DUAL ");
  190. else
  191. define("FROM_DUAL"," ");
  192. */
  193. // DECODE and CASE-WHEN support
  194. function db_case($array)
  195. { global $DatabaseType;
  196. $counter=0;
  197. if($DatabaseType=='mysql')
  198. {
  199. $array_count=count($array);
  200. $string = " CASE WHEN $array[0] =";
  201. $counter++;
  202. $arr_count = count($array);
  203. for($i=1;$i<$arr_count;$i++)
  204. {
  205. $value = $array[$i];
  206. if($value=="''" && substr($string,-1)=='=')
  207. {
  208. $value = ' IS NULL';
  209. $string = substr($string,0,-1);
  210. }
  211. $string.="$value";
  212. if($counter==($array_count-2) && $array_count%2==0)
  213. $string.=" ELSE ";
  214. elseif($counter==($array_count-1))
  215. $string.=" END ";
  216. elseif($counter%2==0)
  217. $string.=" WHEN $array[0]=";
  218. elseif($counter%2==1)
  219. $string.=" THEN ";
  220. $counter++;
  221. }
  222. }
  223. /*
  224. else
  225. {
  226. $string=" decode( ";
  227. foreach($array as $value)
  228. $string.="$value,";
  229. $string[strlen($string)-1]=")";
  230. $string.=" ";
  231. }
  232. */
  233. return $string;
  234. }
  235. // String position.
  236. /*
  237. function db_strpos($args)
  238. { global $DatabaseType;
  239. if($DatabaseType=='postgres')
  240. $ret = 'strpos(';
  241. else
  242. $ret = 'instr(';
  243. foreach($args as $value)
  244. $ret .= $value . ',';
  245. $ret = substr($ret,0,-1) . ')';
  246. return $ret;
  247. }
  248. */
  249. // CONVERT VARCHAR TO NUMERIC
  250. /*
  251. function db_to_number($text)
  252. { global $DatabaseType;
  253. if($DatabaseType=='postgres')
  254. return '('.$text.')::text::float::numeric';
  255. else
  256. return 'to_number('.$text.')';
  257. }
  258. */
  259. // returns an array with the field names for the specified table as key with subkeys
  260. // of SIZE, TYPE, SCALE and NULL. TYPE: varchar, numeric, etc.
  261. function db_properties($table)
  262. { global $DatabaseType,$DatabaseUsername;
  263. switch($DatabaseType)
  264. {
  265. case 'mysql':
  266. $result = DBQuery("SHOW COLUMNS FROM $table");
  267. while($row = db_fetch_row($result))
  268. {
  269. $properties[strtoupper($row['FIELD'])]['TYPE'] = strtoupper($row['TYPE'],strpos($row['TYPE'],'('));
  270. if(!$pos = strpos($row['TYPE'],','))
  271. $pos = strpos($row['TYPE'],')');
  272. else
  273. $properties[strtoupper($row['FIELD'])]['SCALE'] = substr($row['TYPE'],$pos+1);
  274. $properties[strtoupper($row['FIELD'])]['SIZE'] = substr($row['TYPE'],strpos($row['TYPE'],'(')+1,$pos);
  275. if($row['NULL']!='')
  276. $properties[strtoupper($row['FIELD'])]['NULL'] = "Y";
  277. else
  278. $properties[strtoupper($row['FIELD'])]['NULL'] = "N";
  279. }
  280. break;
  281. }
  282. return $properties;
  283. }
  284. function db_show_error($sql,$failnote,$additional='')
  285. { global $openSISTitle,$openSISVersion,$openSISNotifyAddress;
  286. PopTable('header','Error');
  287. $tb = debug_backtrace();
  288. $error = $tb[1]['file'] . " at " . $tb[1]['line'];
  289. echo "
  290. <TABLE CELLSPACING=10 BORDER=0>
  291. <TD align=right><b>Date:</TD>
  292. <TD><pre>".date("m/d/Y h:i:s")."</pre></TD>
  293. </TR><TR>
  294. <TD align=right><b>Failure Notice:</b></TD>
  295. <TD><pre> $failnote </pre></TD>
  296. </TR><TR>
  297. <TD align=right><b>SQL:</b></TD>
  298. <TD>$sql</TD>
  299. </TR>
  300. </TR><TR>
  301. <TD align=right><b>Traceback:</b></TD>
  302. <TD>$error</TD>
  303. </TR>
  304. </TR><TR>
  305. <TD align=right><b>Additional Information:</b></TD>
  306. <TD>$additional</TD>
  307. </TR>
  308. </TABLE>";
  309. echo "
  310. <TABLE CELLSPACING=10 BORDER=0>
  311. <TR><TD align=right><b>Date:</TD>
  312. <TD><pre>".date("m/d/Y h:i:s")."</pre></TD>
  313. </TR><TR>
  314. <TD align=right></TD>
  315. <TD>openSIS has encountered an error that could have resulted from any of the following:
  316. <br/>
  317. <ul>
  318. <li>Invalid data input</li>
  319. <li>Database SQL error</li>
  320. <li>Program error</li>
  321. </ul>
  322. Please take this screen shot and send it to your openSIS representative for debugging and resolution.
  323. </TD>
  324. </TR>
  325. </TABLE>";
  326. //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.
  327. PopTable('footer');
  328. echo "<!-- SQL STATEMENT: \n\n $sql \n\n -->";
  329. /*if(function_exists('mysql_query'))
  330. {
  331. $link = @mysql_connect('os4ed.com','openSIS_log','openSIS_log');
  332. @mysql_select_db('openSIS_log');
  333. @mysql_query("INSERT INTO SQL_ERROR_LOG (HOST_NAME,IP_ADDRESS,LOGIN_DATE,VERSION,PHP_SELF,DOCUMENT_ROOT,SCRIPT_NAME,MODNAME,USERNAME,SQL,REQUEST) values('$_SERVER[SERVER_NAME]','$_SERVER[SERVER_ADDR]','".date('Y-m-d')."','$openSISVersion','$_SERVER[PHP_SELF]','$_SERVER[DOCUMENT_ROOT]','$_SERVER[SCRIPT_NAME]','$_REQUEST[modname]','".User('USERNAME')."','$sql','".ShowVar($_REQUEST,'Y', 'N')."')");
  334. @mysql_close($link);
  335. }*/
  336. if($openSISNotifyAddress)
  337. {
  338. $message = "System: $openSISTitle \n";
  339. $message .= "Date: ".date("m/d/Y h:i:s")."\n";
  340. $message .= "Page: ".$_SERVER['PHP_SELF'].' '.ProgramTitle()." \n\n";
  341. $message .= "Failure Notice: $failnote \n";
  342. $message .= "Additional Info: $additional \n";
  343. $message .= "\n $sql \n";
  344. $message .= "Request Array: \n".ShowVar($_REQUEST,'Y', 'N');
  345. $message .= "\n\nSession Array: \n".ShowVar($_SESSION,'Y', 'N');
  346. mail($openSISNotifyAddress,'openSIS Database Error',$message);
  347. }
  348. die();
  349. }
  350. /*
  351. function Version()
  352. {
  353. $query = DBQuery("select value from APP where name='version'");
  354. $sql = mysql_fetch_assoc($query);
  355. return($sql['value']);
  356. }
  357. function BuildDate()
  358. {
  359. $query = DBQuery("select value from APP where name='build'");
  360. $build = mysql_fetch_assoc($query);
  361. $month = substr($build['value'],0,-9);
  362. $day = substr($build['value'],2,-7);
  363. $year = substr($build['value'],4,-3);
  364. switch($month)
  365. {
  366. case '01':
  367. $month = 'January';
  368. break;
  369. case '02':
  370. $month = 'February';
  371. break;
  372. case '03':
  373. $month = 'March';
  374. break;
  375. case '04':
  376. $month = 'April';
  377. break;
  378. case '05':
  379. $month = 'May';
  380. break;
  381. case '06':
  382. $month = 'June';
  383. break;
  384. case '07':
  385. $month = 'July';
  386. break;
  387. case '08':
  388. $month = 'August';
  389. break;
  390. case '09':
  391. $month = 'September';
  392. break;
  393. case '10':
  394. $month = 'October';
  395. break;
  396. case '11':
  397. $month = 'November';
  398. break;
  399. case '12':
  400. $month = 'December';
  401. break;
  402. }
  403. $build_date = $month.'&nbsp;'.$day.',&nbsp;'.$year;
  404. return($build_date);
  405. }
  406. */
  407. ?>