PageRenderTime 80ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/jesus/library.php

https://bitbucket.org/samuelbond/god
PHP | 672 lines | 367 code | 140 blank | 165 comment | 44 complexity | 444af6b4d80eb5dae93b0287bde33239 MD5 | raw file
  1. <?php
  2. function ceil_decimal( $number, $precision )
  3. {
  4. /*
  5. * CONCEPT:
  6. * ceil_decimal (rounds up) to the nearest decimal place.
  7. * Test:
  8. * $answer = ceil_decimal( 1.340000, 2 );
  9. * echo $answer;
  10. *
  11. * CAUTION:
  12. */
  13. $coefficient = pow( 10, $precision );
  14. $coefficient = strval($coefficient);
  15. return ceil( $number * $coefficient ) / $coefficient;
  16. }
  17. function decimal( $number, $precision )
  18. {
  19. /*
  20. * CONCEPT:
  21. * ceil_decimal (rounds up) to the nearest decimal place.
  22. * Test:
  23. * $answer = ceil_decimal( 1.340000, 2 );
  24. * echo $answer;
  25. *
  26. * CAUTION:
  27. */
  28. $coefficient = pow( 10, $precision );
  29. $coefficient = strval($coefficient);
  30. return round( $number * $coefficient ) / $coefficient;
  31. }
  32. function floor_decimal( $number, $precision )
  33. {
  34. /*
  35. * CONCEPT:
  36. * floor_decimal (rounds down) to the nearest decimal place.
  37. * Test:
  38. * $answer = floor_decimal( 1.340000, 2 );
  39. * echo $answer;
  40. *
  41. * CAUTION:
  42. */
  43. $coefficient = pow(10, $precision);
  44. return floor( $number * $coefficient ) / $coefficient;
  45. }
  46. function comment( $message, $debug_level = NULL )
  47. {
  48. /*
  49. * CONCEPT:
  50. * comment(), warning() and emergency() are the error handling functions.
  51. *
  52. * CAUTION:
  53. */
  54. // Instantiate the class that handles the settings
  55. $S = settings::getInstance();
  56. // Initialize var
  57. $referrer = NULL;
  58. // If the debug_level is not set, set it really high
  59. $debug_level = ( $debug_level === NULL ) ? 'set' : $debug_level;
  60. // If the $debug_level setting for the comment is greater than the debug_detail setting . . .
  61. if( $S->debug == 'yes' && is_numeric( $S->debug_detail ) )
  62. {
  63. if( $debug_level >= $S->debug_detail || $debug_level === 'set' )
  64. {
  65. // If the debug_level is a number . . .
  66. if( is_numeric( $debug_level ) )
  67. {
  68. // Add an "N" before it for css
  69. $debug_level = "N$debug_level";
  70. }
  71. // Get the referrer
  72. $referrer = referrer( 2 );
  73. echo "<div class=\"" . $debug_level . "\"><span class=\"header\">$referrer &nbsp;</span> $message</div>";
  74. }
  75. }
  76. // Send the email
  77. emailer( "$referrer comment", $message );
  78. }
  79. function display_tables( $open_or_close )
  80. {
  81. /*
  82. * CONCEPT:
  83. *
  84. * CAUTION:
  85. */
  86. // Instantiate the class that handles the settings
  87. $S = settings::getInstance();
  88. if( $S->debug_detail <= 10 && $S->debug == 'yes' )
  89. {
  90. $colors = array(1 => '009900', 'FFFF00', '0033FF', '009900', 'FFFF00', '0033FF');
  91. if( $open_or_close == 'open_account' )
  92. {
  93. echo "<P><P><TABLE WIDTH=100% BORDER=3 BORDERCOLOR=#000000><tr><td> ";
  94. }
  95. elseif( $open_or_close == 'open_port' )
  96. {
  97. echo "<P><TABLE WIDTH=100% BORDER=3 BORDERCOLOR=#" . $colors[$S->current_port] . "><tr><td>";
  98. comment( "Process " . $S->fullname . " [ port " . $S->current_port . " ] ", 10 );
  99. }
  100. elseif( $open_or_close == 'close_account' )
  101. {
  102. echo "</td></tr></table><p><br><br><br><br><br><p>";
  103. }
  104. else
  105. {
  106. echo "</td></tr></table>";
  107. }
  108. }
  109. }
  110. function emailer( $subject, $message )
  111. {
  112. /*
  113. * CONCEPT:
  114. *
  115. * CAUTION:
  116. * Do not put any calls to comment(), warning(), or emergency() in here because
  117. * it will create a nested loop.
  118. */
  119. // Instantiate the class that handles the settings
  120. $S = settings::getInstance();
  121. if( $S->email && $S->sendemail == 'yes' )
  122. {
  123. $emailheaders = 'MIME-Version: 1.0' . "\r\n";
  124. $emailheaders .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
  125. $emailheaders .= "From: \"jesus\" <drewbrownjr@gmail.com>\r\n";
  126. // Automatically send a BCC to the admin if it is not the admins account
  127. if( $S->email !== 'drewbrownjr@gmail.com' )
  128. {
  129. $emailheaders .= 'BCC: drewbrownjr@gmail.com' . "\r\n";
  130. }
  131. // Send the email
  132. // mail($S->email, $subject, $message, $emailheaders);
  133. }
  134. }
  135. function emergency( $message )
  136. {
  137. /*
  138. * CONCEPT:
  139. * emergency() shuts down jesus and sends an email to the administrator.
  140. * It is used in emergency_sql() and sql() which are used a lot in
  141. * most functions.
  142. *
  143. * CAUTION:
  144. * An error with this function will result in very Bad Things(TM). Pay
  145. * special attention to this function as it keeps god from running wild.
  146. */
  147. // Instantiate the class that handles the settings
  148. $S = settings::getInstance();
  149. list( $referrer, $trace ) = referrer( 'emergency' );
  150. $referrer = strtoupper( $referrer );
  151. $S->emergency = 'yes';
  152. // Mark the $port with an emergency() so jesus will wait for the admin to fix it
  153. mysql_query( "UPDATE `settings` SET value = 'yes' WHERE name = 'emergency' " );
  154. // Record the emergency() message in the `messages` table
  155. mysql_query( "INSERT INTO `messages` (`fullname`, `database`, `port`, `source`, `error_type`, `message`, `timestamp`)
  156. VALUES('" . $S->fullname . "', '" . $S->database . "', '" . $S->current_port . "',
  157. '" . $referrer . "', 'emergency', '" . $message . "', NOW() ) " );
  158. echo "<hr color=red><center><font color=red font size=2><b>$referrer() EMERGENCY SHUTDOWN:</b></font><br>
  159. <table><tr><td align=left><font color=red font size=2>$message<br><br>$trace</font></td></tr></table></center><hr color=red>";
  160. // Send the email
  161. emailer( "$referrer emergency", $message );
  162. throw new Exception( "emergency" );
  163. // trigger_error("emergency", E_USER_ERROR);
  164. // return;
  165. }
  166. function get_profit( $final_value, $beginning_value )
  167. {
  168. $profit = $final_value / $beginning_value;
  169. comment("[ \$$final_value ] final_value / [ \$$beginning_value ] beginning_value = [ $profit% ] profit ");
  170. $profit = floor_decimal( $profit, 2 );
  171. comment("profit = [ $profit% ] ");
  172. $profit = $profit * 100;
  173. comment("profit = [ $profit% ] ");
  174. return $profit;
  175. }
  176. function print_nice( $elem, $debug_level, $max_level=10, $print_nice_stack=array() )
  177. {
  178. /*
  179. * CONCEPT:
  180. *
  181. * CAUTION:
  182. * print_nice() either displays or does not display (based on the $debug_level).
  183. */
  184. // Instantiate the class that handles the settings
  185. $S = settings::getInstance();
  186. // If the debug_level is low enough, the debugging is turned on.
  187. if( ( $debug_level >= $S->debug_detail ) && $S->debug == 'yes' )
  188. {
  189. if( is_array( $elem ) || is_object( $elem ) )
  190. {
  191. if( in_array( $elem, $print_nice_stack, TRUE ) )
  192. {
  193. echo "<font color=red>RECURSION</font>";
  194. return;
  195. }
  196. $print_nice_stack[] = &$elem;
  197. if( $max_level < 1 )
  198. {
  199. echo "<font color=red>max level error</font>";
  200. return;
  201. }
  202. $max_level--;
  203. echo "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"100%\">";
  204. if( is_array( $elem ) )
  205. {
  206. echo '<tr><td colspan=2 style="background-color:#99CCFF;"><font color=grey></font></td></tr>';
  207. }
  208. else
  209. {
  210. echo '<tr><td colspan=2 style="background-color:#333333;"><strong>';
  211. echo '<font color=white>OBJECT Type: ' . get_class( $elem ) . '</font></strong></td></tr>';
  212. }
  213. $color = 0;
  214. foreach( $elem as $k => $v )
  215. {
  216. if( $max_level % 2 )
  217. {
  218. $rgb = ($color++ % 2) ? "#FFFFCC" : "#99CC00";
  219. }
  220. else
  221. {
  222. $rgb = ($color++ % 2) ? "#FF9933" : "#99CCFF";
  223. }
  224. echo '<tr><td NOWRAP valign=top style="width:40px;background-color:' . $rgb . ';">';
  225. echo " $k </td><td>&nbsp; ";
  226. print_nice( $v, $debug_level, $max_level, $print_nice_stack );
  227. echo "</td></tr>";
  228. }
  229. echo "</table>";
  230. return;
  231. }
  232. if( $elem === null )
  233. {
  234. echo "<font size=-2 color=green>NULL</font>";
  235. }
  236. elseif( $elem === 0 )
  237. {
  238. echo "0";
  239. }
  240. elseif( $elem === true )
  241. {
  242. echo "<font size=-1 color=green>TRUE</font>";
  243. }
  244. elseif( $elem === false )
  245. {
  246. echo "<font size=-1 color=green>FALSE</font>";
  247. }
  248. elseif( $elem === "" )
  249. {
  250. echo "[blank]";
  251. }
  252. else
  253. {
  254. echo str_replace( "\n", "<strong><font color=red>*</font></strong><br>\n", $elem );
  255. }
  256. }
  257. }
  258. function referrer( $level = NULL )
  259. {
  260. // Run the backtrace . . .
  261. $ref = debug_backtrace();
  262. // Initialize var
  263. $sp = 0;
  264. $trace = NULL;
  265. $referrer = NULL;
  266. if( $level === 'emergency' )
  267. {
  268. // $emergency_level = 6;
  269. // $file = str_replace('.php', '', basename($ref[1]['file']));
  270. // $class = $ref[$emergency_level]['class'];
  271. // $function = $ref[$emergency_level]['function'];
  272. // $referrer = "$class->$function";
  273. $ref = array_reverse( $ref );
  274. foreach( $ref AS $index => $info )
  275. {
  276. // $file = str_replace('.php', '', basename($info['file']));
  277. // $class = $info['class'];
  278. $class = ( isset( $info['class'] ) ) ? $info['class'] : NULL;
  279. $function = $info['function'];
  280. $line = $info['line'];
  281. if( $function !== 'referrer' && $function !== 'emergency' )
  282. {
  283. $trace .= str_repeat("&nbsp;", $sp++);
  284. if( $class )
  285. {
  286. $trace .= "$class->$function() ( line: $line )<br>";
  287. }
  288. else
  289. {
  290. $trace .= "$function() ( line: $line )<br>";
  291. }
  292. }
  293. }
  294. return array( $referrer, $trace );
  295. }
  296. elseif( is_numeric($level) )
  297. {
  298. // $file = str_replace('.php', '', basename($ref[1]['file']));
  299. if( isset( $ref[$level]['class'] ) )
  300. {
  301. $class = $ref[$level]['class'];
  302. }
  303. else
  304. {
  305. $class = 'missing method [fix this]';
  306. }
  307. $function = $ref[$level]['function'];
  308. // $line = $ref[$level]['line'];
  309. return "$class->$function() ";
  310. }
  311. else
  312. {
  313. echo "You need to be about setting the \$level for library.php->referrer<br>";
  314. }
  315. }
  316. function say( $input=NULL, $id=NULL )
  317. {
  318. /*
  319. * CONCEPT:
  320. * say() is a utility function for printing variables and arrays.
  321. * It saves lots of typing for quicker debugging.
  322. * To echo anything, simply use: say($input, 'identifier');
  323. *
  324. * CAUTION:
  325. */
  326. // Instantiate the class that handles the settings
  327. $S = settings::getInstance();
  328. // Get the referrer
  329. $referrer = referrer( 2 );
  330. // Increment the header value (to help identify outputs)
  331. $S->say_count++;
  332. // Begin the header
  333. echo "<hr>SAY{$S->say_count}+++ $referrer &nbsp; | &nbsp; id = [ $id ]<br>";
  334. // If the $input is a zero float
  335. if( !$input && !is_null($input) )
  336. {
  337. echo "input = [ $input ]";
  338. }
  339. elseif( is_null($input) )
  340. {
  341. echo "say() didn't get shit";
  342. }
  343. else
  344. {
  345. var_dump( $input );
  346. }
  347. echo "<hr>";
  348. }
  349. function sayd( $input=NULL, $id=NULL )
  350. {
  351. /*
  352. * CONCEPT:
  353. * sayd() is say() with a die()
  354. *
  355. * CAUTION:
  356. */
  357. // Instantiate the class that handles the settings
  358. $S = settings::getInstance();
  359. // Get the referrer
  360. $referrer = referrer( 2 );
  361. // Increment the header value (to help identify outputs)
  362. $S->say_count++;
  363. // Begin the header
  364. echo "<hr>SAYD{$S->say_count}+++ $referrer &nbsp; | &nbsp; id = [ $id ]<br>";
  365. // If the $input is a zero float
  366. if( !$input && !is_null($input) )
  367. {
  368. echo "input = [ $input ]";
  369. }
  370. elseif( is_null($input) )
  371. {
  372. echo "sayd() didn't get shit";
  373. }
  374. else
  375. {
  376. var_dump( $input );
  377. }
  378. echo "<hr>";
  379. die();
  380. }
  381. function search_array( $item, $multi_array, $partial_matches = FALSE, $search_keys = FALSE )
  382. {
  383. /*
  384. * CONCEPT:
  385. * search_array() recursively searches through an array for the $item specified.
  386. *
  387. * CAUTION:
  388. * Make sure to set $search_keys = TRUE. Consider setting $search_keys = FALSE if
  389. * searching for numbers.
  390. */
  391. if( !is_array( $multi_array ) )
  392. {
  393. comment( "Send an array to me, would ya?", 1 );
  394. return FALSE;
  395. }
  396. // Loop through the multideminsional array
  397. foreach( $multi_array AS $key => $value )
  398. {
  399. $what = ( $search_keys ) ? $key : $value;
  400. // If the $item is found
  401. if( $item === $what )
  402. {
  403. comment( "\$item [ $item ] found! ", 1 );
  404. return $item;
  405. }
  406. // If it is OK to find a partial match
  407. elseif( $partial_matches && @strpos( $what, $item ) !== FALSE )
  408. {
  409. comment( "partial match \$item [ $item ] found! ", 1 );
  410. return $item;
  411. }
  412. // If the value is an array and the $item is found
  413. elseif( is_array( $value ) && search_array( $item, $value, $partial_matches, $search_keys ) !== FALSE )
  414. {
  415. comment( "\$value \$item [ $item ] found! ", 1 );
  416. return $item;
  417. }
  418. }
  419. // $notfound = 'failz';
  420. return FALSE;
  421. }
  422. function sql( $query, $function, $database = NULL )
  423. {
  424. /*
  425. * CONCEPT:
  426. * sql() makes sure there are no MySQL errors on queries.
  427. * It also sends errors to emergency() to shut down jesus
  428. * and send an email to the admin. Use this liberally thru
  429. * the code. It saves time and reduces the amount of code.
  430. *
  431. * CAUTION:
  432. * This is a utility function. A mistake here will impact
  433. * every function. Test it well with satan.
  434. */
  435. // Instantiate the class that handles the settings
  436. $S = settings::getInstance();
  437. if( $database === NULL )
  438. {
  439. $database = $S->database;
  440. }
  441. elseif( $database === 'pastquotes' )
  442. {
  443. $database = 'pastquotes';
  444. }
  445. // Determine if the database was selected
  446. $db_selected = mysql_select_db($database);
  447. // Run the query
  448. $result = @mysql_query( $query );
  449. $total = @mysql_num_rows( $result );
  450. if( empty( $db_selected ) )
  451. {
  452. // Set the message formatting
  453. $format_1 = "<tr><td><font color=red><b>";
  454. $format_2 = "</b></b></font></td><td><font color=red>";
  455. $format_3 = "</font></font></td></tr>";
  456. // Define the detailed message
  457. $message = "<table>";
  458. $message .= "$format_1 Name: $format_2 " . $S->fullname . "$format_3";
  459. $message .= "$format_1 Database: $format_2 " . $database . " $format_3";
  460. $message .= "$format_1 Note: $format_2 You may need to manually create and populate the [ " . $database . " ] database. $format_3";
  461. $message .= "$format_1 Script: $format_2 " . $_SERVER["SCRIPT_NAME"] . " $format_3";
  462. $message .= "$format_1 Function: $format_2 " . $function . "() $format_3";
  463. $message .= "$format_1 MySQL_Query: $format_2 " . $query . " $format_3";
  464. $message .= "$format_1 Error Description: $format_2 " . @mysql_error() . " $format_3";
  465. $message .= "$format_1 Error Number and time: $format_2 " . @mysql_errno() . " " . date( "H:m:s, jS F, Y" ) . " $format_3";
  466. $message .= "$format_1 Total Rows: $format_2 [ " . $total . " ] $format_3";
  467. $result = @mysql_list_processes();
  468. $message .= " $format_3 </table>";
  469. // Free up the memory
  470. @mysql_free_result( $result );
  471. // Rollback any transaction - if this error is happening inside an
  472. // "START TRANSACTION" -> "COMMIT" block, the DB will be rolled back
  473. // to the data that was there at the "START TRANSATION" point.
  474. $rollback = mysql_query( "ROLLBACK" );
  475. // Send an emergency()
  476. emergency( $message, $function );
  477. }
  478. /*
  479. if( empty($db_selected) )
  480. {
  481. emergency("Database [ $database ] connection failed.
  482. You may need to manually create and populate the `pastquotes` database. ");
  483. }
  484. */
  485. // Switch back to the default database
  486. // $db_selected = mysql_select_db( $database );
  487. // If there was no error return the result (otherwise emergency() will hang jesus)
  488. return $result;
  489. }
  490. function warning( $message, $source, $no_exception = null )
  491. {
  492. /*
  493. * CONCEPT:
  494. * Send a warning to the admin. $no_exception argument determines whether we should stop the process or continue it.
  495. * If it is set to TRUE, warning() will send a notice to admin, display message (in debug mode) and return.
  496. * If it is not set, warning() will not return, but rather throw an exception that will be caught in the main port loop.
  497. * In other words, if you want to continue processing after a warning() like nothing happened, mark it TRUE
  498. *
  499. * CAUTION:
  500. */
  501. // Instantiate the class that handles the settings
  502. $S = settings::getInstance();
  503. // Record the warning() message in the `messages` table
  504. mysql_query( "INSERT INTO `messages` (`fullname`, `database`, `port`, `source`, `error_type`, `message`, `timestamp`)
  505. VALUES('" . $S->fullname . "', '" . $S->database . "', '" . $S->current_port . "',
  506. '" . $source . "', 'warning', '" . $message . "', NOW() ) " );
  507. // Put the error messages in descending order by time
  508. mysql_query( "ALTER TABLE `messages` ORDER BY `timestamp` DESC " );
  509. if( $S->debug == 'yes' )
  510. {
  511. echo "<hr color=#e77919><center><font color=#e77919 font size=2>$source() warning:<br>$message</font></center><hr color=#e77919>";
  512. }
  513. // Send the email
  514. emailer( "$source comment()", $message );
  515. // If the warning() is like so: warning($message, $source, TRUE); then jesus will NOT stop.
  516. if( !$no_exception )
  517. {
  518. // Flip the jesusrunning back to no
  519. mysql_query("UPDATE `settings` SET value = 'no' WHERE name = 'jesusrunning' ");
  520. throw new Exception( "warning" );
  521. }
  522. }
  523. ?>