code.fastix.org

Dateiansicht:

Datei:Projekte -> PHP:use_strict -> use_strict.php
md5:f0ef45af65f493d4f891e1ffc0c4cbb2
sha1:70eb7d0219fe7e940d96ba734c51a11cb0832467
Download-Link:Download
  1. <?php
  2. /**
  3. * Author: Jörg Reinholz, fastix WebDesign & Consult, Kassel, Germany, http://www.fastix.org
  4. * This script comes with ABSOLUTELY NO WARRANTY!
  5. * This script is free software, and you are welcome to redistribute it under certain conditions:
  6. * Licence: GPL, Version 2
  7. * -- http://www.gnu.org/licenses/gpl-2.0.html.
  8. * -- http://www.gnu.de/documents/gpl-2.0.de.html ( deutsche Übersetzung )
  9. **/
  10.  
  11. /** Manual:
  12. * Usage:
  13. * <?php define( 'DEBUG', 2 ); require_once 'plugins/use_strict.php'; ?>
  14. * or:
  15. * <?php require_once 'plugins/use_strict.php'; ?> - in this smart & short case DEBUG will set to 1
  16.  
  17. * The constant DEBUG is also used in this ways:
  18. * DEBUG == 0 or smaller than 0:
  19. * --->The script will NOT break on notices or warnings but show them the  notices or warnings, not the source!
  20. * DEBUG == 1 or greater than 2:
  21. * --->The script will hard break on ALL notices and warnings, but not show the source of the errors... ( standard )
  22. * DEBUG == 2;
  23. * --->The script will show the erroreus source & break on ALL notices and warnings...
  24.  
  25. * Editors - If you use Windows and have Errors
  26. * Do not safe this with a BOM! Maybe your Skripts can not work fine if you do this.
  27. * Show the fine manual of your text-editor.
  28. **/
  29.  
  30. error_reporting( E_ALL );
  31. ini_set( "html_errors", 1 );
  32. set_error_handler( function( $errNo, $errStr, $errFile, $errLine ) {
  33.     if ( ! defined( 'DEBUG' ) ) { define( 'DEBUG', 1 ); }
  34.     if ( ! headers_sent( ) ) {
  35.          header_remove( );
  36.          header( "Cache-Control: no-store, no-cache, must-revalidate" );
  37.          header( $_SERVER['SERVER_PROTOCOL'].'/1.1 500' );
  38.          header( "Status: 500" );
  39.          header( 'Content-Type: text/html; charset=utf-8' );
  40.     }
  41.     $msg = "$errStr in $errFile on line $errLine";
  42.     if ( $errNo == E_NOTICE )           { $errType = "notice"; }
  43.     elseif ( $errNo == E_WARNING )      { $errType = "warning"; }
  44.     elseif ( $errNo == E_USER_WARNING ) { $errType = "self triggered warning"; }
  45.     elseif ( $errNo == E_USER_NOTICE )  { $errType = "self triggered notice"; }
  46.     elseif ( $errNo == E_USER_ERROR )   { $errType = "self triggered error"; }
  47.     else                              { $errType = "unknown"; }
  48.     if ( $errType ) {
  49.         if ( 2 == DEBUG )  {
  50.             $arErrFile = file( $errFile );
  51.             $errMin = $errLine - 4; if ( $errMin < 1 ) { $errMin = 1; }
  52.             $errMax = $errLine - 1; if ( $errMax >= count( $arErrFile ) ) { $errMax = count( $arErrFile ) - 1 ; }
  53.             for ( $i = $errMin; $i <= $errMax; $i++ ) {
  54.               if ( ( $i > 1 ) && ( isset( $arErrFile[$i] ) ) ) {
  55.                   if ( $i ==  $errLine - 1 ) {
  56.                       $arErrText[] = "<b>$i:</b>\t<span style = 'border:1px solid red;'>" . htmlspecialchars( rtrim( $arErrFile[$i] ) ). "</span>\n";
  57.                   } else {
  58.                       $arErrText[] = "<b>$i:</b>\t<span>" . htmlspecialchars( $arErrFile[$i] ) .  "<span>";
  59.                   }
  60.               }
  61.             }
  62.             unset( $arErrFile );
  63.         } else {
  64.             $arErrText[0] = "If you wish to see the relavant part of th source here, then define DEBUG und set this to 2.";
  65.         }
  66.         $my_php_error_msg_id = 'phperr_'.rand( );
  67.         $errStr = preg_replace( "/ \[<a href='.*'>.*<\/a>\]/", "", $errStr );
  68.         if ( 0 < DEBUG )  {
  69.            $errBreaked = "The execution of your fine script was hard breaked. Why?";
  70.            $errScript = '';
  71.         } else {
  72.            $errBreaked = '<button onclick="closer_'.$my_php_error_msg_id.'( );">close this message</button> --- ';
  73.            $errScript = "
  74.           <script type=\"text/javascript\">
  75.           if ( 'number' == typeof( phperr_zindex ) ) {
  76.               phperr_zindex = phperr_zindex - 1;
  77.           } else {
  78.               phperr_zindex = 950;
  79.           }
  80.           document.getElementById( '$my_php_error_msg_id' ).style.zIndex = phperr_zindex;
  81.           function closer_".$my_php_error_msg_id."( ) {
  82.             while( o = document.getElementById( '$my_php_error_msg_id' ) ) {
  83.                o.parentNode.removeChild( o );
  84.             }
  85.           }
  86.           </script>
  87.           ";
  88.         }
  89.         #The first line of the output is helpful if the calling script works in a special context. Naturally is this not valid html...
  90.        print "<!-- ]]\"' --></script></style></iframe></frame></frameset></noscript>
  91.        <div style = 'position:absolute;z-index:9999;top:0;left0;width:80%;border:1em solid red;padding:.5em;color:#008;background-color:#fff;display:block' id = '$my_php_error_msg_id'>
  92.           <h1>Error! ( Type: &quot;$errType&quot; )</h1>
  93.             <ul>
  94.               <li><strong>in file:</strong> <pre>" . htmlspecialchars( $errFile ) . "</pre></li>
  95.               <li><strong>in row:</strong> " . ( $errLine - 1 ) . "</li>
  96.               <li><strong>text: </strong> " . htmlspecialchars( $errStr ) . "</li>
  97.               <li><strong>the most relevant part of code:</strong>  <pre>" . implode( '', $arErrText ). "</pre></li>
  98.             </ul>
  99.           <p><strong>$errBreaked Often the results are not this what expected!</strong></p>
  100.        </div>
  101.        $errScript
  102.        ";
  103.         if ( 0 < DEBUG )  {
  104.            exit;
  105.         }
  106.  
  107.     } else {
  108.         echo $msg;
  109.     }
  110. }, E_ALL );