/jesus/library.php
PHP | 672 lines | 367 code | 140 blank | 165 comment | 44 complexity | 444af6b4d80eb5dae93b0287bde33239 MD5 | raw file
- <?php
- function ceil_decimal( $number, $precision )
- {
- /*
- * CONCEPT:
- * ceil_decimal (rounds up) to the nearest decimal place.
- * Test:
- * $answer = ceil_decimal( 1.340000, 2 );
- * echo $answer;
- *
- * CAUTION:
- */
- $coefficient = pow( 10, $precision );
- $coefficient = strval($coefficient);
- return ceil( $number * $coefficient ) / $coefficient;
- }
- function decimal( $number, $precision )
- {
- /*
- * CONCEPT:
- * ceil_decimal (rounds up) to the nearest decimal place.
- * Test:
- * $answer = ceil_decimal( 1.340000, 2 );
- * echo $answer;
- *
- * CAUTION:
- */
- $coefficient = pow( 10, $precision );
- $coefficient = strval($coefficient);
- return round( $number * $coefficient ) / $coefficient;
- }
- function floor_decimal( $number, $precision )
- {
- /*
- * CONCEPT:
- * floor_decimal (rounds down) to the nearest decimal place.
- * Test:
- * $answer = floor_decimal( 1.340000, 2 );
- * echo $answer;
- *
- * CAUTION:
- */
- $coefficient = pow(10, $precision);
- return floor( $number * $coefficient ) / $coefficient;
- }
- function comment( $message, $debug_level = NULL )
- {
- /*
- * CONCEPT:
- * comment(), warning() and emergency() are the error handling functions.
- *
- * CAUTION:
- */
- // Instantiate the class that handles the settings
- $S = settings::getInstance();
- // Initialize var
- $referrer = NULL;
- // If the debug_level is not set, set it really high
- $debug_level = ( $debug_level === NULL ) ? 'set' : $debug_level;
- // If the $debug_level setting for the comment is greater than the debug_detail setting . . .
- if( $S->debug == 'yes' && is_numeric( $S->debug_detail ) )
- {
- if( $debug_level >= $S->debug_detail || $debug_level === 'set' )
- {
- // If the debug_level is a number . . .
- if( is_numeric( $debug_level ) )
- {
- // Add an "N" before it for css
- $debug_level = "N$debug_level";
- }
- // Get the referrer
- $referrer = referrer( 2 );
- echo "<div class=\"" . $debug_level . "\"><span class=\"header\">$referrer </span> $message</div>";
- }
- }
- // Send the email
- emailer( "$referrer comment", $message );
- }
- function display_tables( $open_or_close )
- {
- /*
- * CONCEPT:
- *
- * CAUTION:
- */
- // Instantiate the class that handles the settings
- $S = settings::getInstance();
- if( $S->debug_detail <= 10 && $S->debug == 'yes' )
- {
- $colors = array(1 => '009900', 'FFFF00', '0033FF', '009900', 'FFFF00', '0033FF');
- if( $open_or_close == 'open_account' )
- {
- echo "<P><P><TABLE WIDTH=100% BORDER=3 BORDERCOLOR=#000000><tr><td> ";
- }
- elseif( $open_or_close == 'open_port' )
- {
- echo "<P><TABLE WIDTH=100% BORDER=3 BORDERCOLOR=#" . $colors[$S->current_port] . "><tr><td>";
- comment( "Process " . $S->fullname . " [ port " . $S->current_port . " ] ", 10 );
- }
- elseif( $open_or_close == 'close_account' )
- {
- echo "</td></tr></table><p><br><br><br><br><br><p>";
- }
- else
- {
- echo "</td></tr></table>";
- }
- }
- }
- function emailer( $subject, $message )
- {
- /*
- * CONCEPT:
- *
- * CAUTION:
- * Do not put any calls to comment(), warning(), or emergency() in here because
- * it will create a nested loop.
- */
- // Instantiate the class that handles the settings
- $S = settings::getInstance();
- if( $S->email && $S->sendemail == 'yes' )
- {
- $emailheaders = 'MIME-Version: 1.0' . "\r\n";
- $emailheaders .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
- $emailheaders .= "From: \"jesus\" <drewbrownjr@gmail.com>\r\n";
- // Automatically send a BCC to the admin if it is not the admins account
- if( $S->email !== 'drewbrownjr@gmail.com' )
- {
- $emailheaders .= 'BCC: drewbrownjr@gmail.com' . "\r\n";
- }
- // Send the email
- // mail($S->email, $subject, $message, $emailheaders);
- }
- }
- function emergency( $message )
- {
- /*
- * CONCEPT:
- * emergency() shuts down jesus and sends an email to the administrator.
- * It is used in emergency_sql() and sql() which are used a lot in
- * most functions.
- *
- * CAUTION:
- * An error with this function will result in very Bad Things(TM). Pay
- * special attention to this function as it keeps god from running wild.
- */
- // Instantiate the class that handles the settings
- $S = settings::getInstance();
- list( $referrer, $trace ) = referrer( 'emergency' );
- $referrer = strtoupper( $referrer );
- $S->emergency = 'yes';
- // Mark the $port with an emergency() so jesus will wait for the admin to fix it
- mysql_query( "UPDATE `settings` SET value = 'yes' WHERE name = 'emergency' " );
- // Record the emergency() message in the `messages` table
- mysql_query( "INSERT INTO `messages` (`fullname`, `database`, `port`, `source`, `error_type`, `message`, `timestamp`)
- VALUES('" . $S->fullname . "', '" . $S->database . "', '" . $S->current_port . "',
- '" . $referrer . "', 'emergency', '" . $message . "', NOW() ) " );
- echo "<hr color=red><center><font color=red font size=2><b>$referrer() EMERGENCY SHUTDOWN:</b></font><br>
- <table><tr><td align=left><font color=red font size=2>$message<br><br>$trace</font></td></tr></table></center><hr color=red>";
- // Send the email
- emailer( "$referrer emergency", $message );
- throw new Exception( "emergency" );
- // trigger_error("emergency", E_USER_ERROR);
- // return;
- }
- function get_profit( $final_value, $beginning_value )
- {
- $profit = $final_value / $beginning_value;
- comment("[ \$$final_value ] final_value / [ \$$beginning_value ] beginning_value = [ $profit% ] profit ");
- $profit = floor_decimal( $profit, 2 );
- comment("profit = [ $profit% ] ");
- $profit = $profit * 100;
- comment("profit = [ $profit% ] ");
- return $profit;
- }
- function print_nice( $elem, $debug_level, $max_level=10, $print_nice_stack=array() )
- {
- /*
- * CONCEPT:
- *
- * CAUTION:
- * print_nice() either displays or does not display (based on the $debug_level).
- */
- // Instantiate the class that handles the settings
- $S = settings::getInstance();
- // If the debug_level is low enough, the debugging is turned on.
- if( ( $debug_level >= $S->debug_detail ) && $S->debug == 'yes' )
- {
- if( is_array( $elem ) || is_object( $elem ) )
- {
- if( in_array( $elem, $print_nice_stack, TRUE ) )
- {
- echo "<font color=red>RECURSION</font>";
- return;
- }
- $print_nice_stack[] = &$elem;
- if( $max_level < 1 )
- {
- echo "<font color=red>max level error</font>";
- return;
- }
- $max_level--;
- echo "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"100%\">";
- if( is_array( $elem ) )
- {
- echo '<tr><td colspan=2 style="background-color:#99CCFF;"><font color=grey></font></td></tr>';
- }
- else
- {
- echo '<tr><td colspan=2 style="background-color:#333333;"><strong>';
- echo '<font color=white>OBJECT Type: ' . get_class( $elem ) . '</font></strong></td></tr>';
- }
- $color = 0;
- foreach( $elem as $k => $v )
- {
- if( $max_level % 2 )
- {
- $rgb = ($color++ % 2) ? "#FFFFCC" : "#99CC00";
- }
- else
- {
- $rgb = ($color++ % 2) ? "#FF9933" : "#99CCFF";
- }
- echo '<tr><td NOWRAP valign=top style="width:40px;background-color:' . $rgb . ';">';
- echo " $k </td><td> ";
- print_nice( $v, $debug_level, $max_level, $print_nice_stack );
- echo "</td></tr>";
- }
- echo "</table>";
- return;
- }
- if( $elem === null )
- {
- echo "<font size=-2 color=green>NULL</font>";
- }
- elseif( $elem === 0 )
- {
- echo "0";
- }
- elseif( $elem === true )
- {
- echo "<font size=-1 color=green>TRUE</font>";
- }
- elseif( $elem === false )
- {
- echo "<font size=-1 color=green>FALSE</font>";
- }
- elseif( $elem === "" )
- {
- echo "[blank]";
- }
- else
- {
- echo str_replace( "\n", "<strong><font color=red>*</font></strong><br>\n", $elem );
- }
- }
- }
- function referrer( $level = NULL )
- {
- // Run the backtrace . . .
- $ref = debug_backtrace();
- // Initialize var
- $sp = 0;
- $trace = NULL;
- $referrer = NULL;
- if( $level === 'emergency' )
- {
- // $emergency_level = 6;
- // $file = str_replace('.php', '', basename($ref[1]['file']));
- // $class = $ref[$emergency_level]['class'];
- // $function = $ref[$emergency_level]['function'];
- // $referrer = "$class->$function";
- $ref = array_reverse( $ref );
- foreach( $ref AS $index => $info )
- {
- // $file = str_replace('.php', '', basename($info['file']));
- // $class = $info['class'];
- $class = ( isset( $info['class'] ) ) ? $info['class'] : NULL;
- $function = $info['function'];
- $line = $info['line'];
- if( $function !== 'referrer' && $function !== 'emergency' )
- {
- $trace .= str_repeat(" ", $sp++);
- if( $class )
- {
- $trace .= "$class->$function() ( line: $line )<br>";
- }
- else
- {
- $trace .= "$function() ( line: $line )<br>";
- }
- }
- }
- return array( $referrer, $trace );
- }
- elseif( is_numeric($level) )
- {
- // $file = str_replace('.php', '', basename($ref[1]['file']));
- if( isset( $ref[$level]['class'] ) )
- {
- $class = $ref[$level]['class'];
- }
- else
- {
- $class = 'missing method [fix this]';
- }
- $function = $ref[$level]['function'];
- // $line = $ref[$level]['line'];
- return "$class->$function() ";
- }
- else
- {
- echo "You need to be about setting the \$level for library.php->referrer<br>";
- }
- }
- function say( $input=NULL, $id=NULL )
- {
- /*
- * CONCEPT:
- * say() is a utility function for printing variables and arrays.
- * It saves lots of typing for quicker debugging.
- * To echo anything, simply use: say($input, 'identifier');
- *
- * CAUTION:
- */
- // Instantiate the class that handles the settings
- $S = settings::getInstance();
- // Get the referrer
- $referrer = referrer( 2 );
- // Increment the header value (to help identify outputs)
- $S->say_count++;
- // Begin the header
- echo "<hr>SAY{$S->say_count}+++ $referrer | id = [ $id ]<br>";
- // If the $input is a zero float
- if( !$input && !is_null($input) )
- {
- echo "input = [ $input ]";
- }
- elseif( is_null($input) )
- {
- echo "say() didn't get shit";
- }
- else
- {
- var_dump( $input );
- }
- echo "<hr>";
- }
- function sayd( $input=NULL, $id=NULL )
- {
- /*
- * CONCEPT:
- * sayd() is say() with a die()
- *
- * CAUTION:
- */
- // Instantiate the class that handles the settings
- $S = settings::getInstance();
- // Get the referrer
- $referrer = referrer( 2 );
- // Increment the header value (to help identify outputs)
- $S->say_count++;
- // Begin the header
- echo "<hr>SAYD{$S->say_count}+++ $referrer | id = [ $id ]<br>";
- // If the $input is a zero float
- if( !$input && !is_null($input) )
- {
- echo "input = [ $input ]";
- }
- elseif( is_null($input) )
- {
- echo "sayd() didn't get shit";
- }
- else
- {
- var_dump( $input );
- }
- echo "<hr>";
- die();
- }
- function search_array( $item, $multi_array, $partial_matches = FALSE, $search_keys = FALSE )
- {
- /*
- * CONCEPT:
- * search_array() recursively searches through an array for the $item specified.
- *
- * CAUTION:
- * Make sure to set $search_keys = TRUE. Consider setting $search_keys = FALSE if
- * searching for numbers.
- */
- if( !is_array( $multi_array ) )
- {
- comment( "Send an array to me, would ya?", 1 );
- return FALSE;
- }
- // Loop through the multideminsional array
- foreach( $multi_array AS $key => $value )
- {
- $what = ( $search_keys ) ? $key : $value;
- // If the $item is found
- if( $item === $what )
- {
- comment( "\$item [ $item ] found! ", 1 );
- return $item;
- }
- // If it is OK to find a partial match
- elseif( $partial_matches && @strpos( $what, $item ) !== FALSE )
- {
- comment( "partial match \$item [ $item ] found! ", 1 );
- return $item;
- }
- // If the value is an array and the $item is found
- elseif( is_array( $value ) && search_array( $item, $value, $partial_matches, $search_keys ) !== FALSE )
- {
- comment( "\$value \$item [ $item ] found! ", 1 );
- return $item;
- }
- }
- // $notfound = 'failz';
- return FALSE;
- }
- function sql( $query, $function, $database = NULL )
- {
- /*
- * CONCEPT:
- * sql() makes sure there are no MySQL errors on queries.
- * It also sends errors to emergency() to shut down jesus
- * and send an email to the admin. Use this liberally thru
- * the code. It saves time and reduces the amount of code.
- *
- * CAUTION:
- * This is a utility function. A mistake here will impact
- * every function. Test it well with satan.
- */
- // Instantiate the class that handles the settings
- $S = settings::getInstance();
- if( $database === NULL )
- {
- $database = $S->database;
- }
- elseif( $database === 'pastquotes' )
- {
- $database = 'pastquotes';
- }
- // Determine if the database was selected
- $db_selected = mysql_select_db($database);
- // Run the query
- $result = @mysql_query( $query );
- $total = @mysql_num_rows( $result );
- if( empty( $db_selected ) )
- {
- // Set the message formatting
- $format_1 = "<tr><td><font color=red><b>";
- $format_2 = "</b></b></font></td><td><font color=red>";
- $format_3 = "</font></font></td></tr>";
- // Define the detailed message
- $message = "<table>";
- $message .= "$format_1 Name: $format_2 " . $S->fullname . "$format_3";
- $message .= "$format_1 Database: $format_2 " . $database . " $format_3";
- $message .= "$format_1 Note: $format_2 You may need to manually create and populate the [ " . $database . " ] database. $format_3";
- $message .= "$format_1 Script: $format_2 " . $_SERVER["SCRIPT_NAME"] . " $format_3";
- $message .= "$format_1 Function: $format_2 " . $function . "() $format_3";
- $message .= "$format_1 MySQL_Query: $format_2 " . $query . " $format_3";
- $message .= "$format_1 Error Description: $format_2 " . @mysql_error() . " $format_3";
- $message .= "$format_1 Error Number and time: $format_2 " . @mysql_errno() . " " . date( "H:m:s, jS F, Y" ) . " $format_3";
- $message .= "$format_1 Total Rows: $format_2 [ " . $total . " ] $format_3";
- $result = @mysql_list_processes();
- $message .= " $format_3 </table>";
- // Free up the memory
- @mysql_free_result( $result );
- // Rollback any transaction - if this error is happening inside an
- // "START TRANSACTION" -> "COMMIT" block, the DB will be rolled back
- // to the data that was there at the "START TRANSATION" point.
- $rollback = mysql_query( "ROLLBACK" );
- // Send an emergency()
- emergency( $message, $function );
- }
- /*
- if( empty($db_selected) )
- {
- emergency("Database [ $database ] connection failed.
- You may need to manually create and populate the `pastquotes` database. ");
- }
- */
- // Switch back to the default database
- // $db_selected = mysql_select_db( $database );
- // If there was no error return the result (otherwise emergency() will hang jesus)
- return $result;
- }
- function warning( $message, $source, $no_exception = null )
- {
- /*
- * CONCEPT:
- * Send a warning to the admin. $no_exception argument determines whether we should stop the process or continue it.
- * If it is set to TRUE, warning() will send a notice to admin, display message (in debug mode) and return.
- * If it is not set, warning() will not return, but rather throw an exception that will be caught in the main port loop.
- * In other words, if you want to continue processing after a warning() like nothing happened, mark it TRUE
- *
- * CAUTION:
- */
- // Instantiate the class that handles the settings
- $S = settings::getInstance();
- // Record the warning() message in the `messages` table
- mysql_query( "INSERT INTO `messages` (`fullname`, `database`, `port`, `source`, `error_type`, `message`, `timestamp`)
- VALUES('" . $S->fullname . "', '" . $S->database . "', '" . $S->current_port . "',
- '" . $source . "', 'warning', '" . $message . "', NOW() ) " );
- // Put the error messages in descending order by time
- mysql_query( "ALTER TABLE `messages` ORDER BY `timestamp` DESC " );
- if( $S->debug == 'yes' )
- {
- echo "<hr color=#e77919><center><font color=#e77919 font size=2>$source() warning:<br>$message</font></center><hr color=#e77919>";
- }
- // Send the email
- emailer( "$source comment()", $message );
- // If the warning() is like so: warning($message, $source, TRUE); then jesus will NOT stop.
- if( !$no_exception )
- {
- // Flip the jesusrunning back to no
- mysql_query("UPDATE `settings` SET value = 'no' WHERE name = 'jesusrunning' ");
- throw new Exception( "warning" );
- }
- }
- ?>