1
0

errorhandling.inc.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. // -------------------------------------------------------------------------------
  3. // | net2ftp: a web based FTP client |
  4. // | Copyright (c) 2003-2013 by David Gartner |
  5. // | |
  6. // | This program is free software; you can redistribute it and/or |
  7. // | modify it under the terms of the GNU General Public License |
  8. // | as published by the Free Software Foundation; either version 2 |
  9. // | of the License, or (at your option) any later version. |
  10. // | |
  11. // -------------------------------------------------------------------------------
  12. /* -------------------------------------------------------------------------
  13. This is how error-handing works within net2ftp
  14. -------------------------------------------------------------------------
  15. There are 3 global variables:
  16. - $net2ftp_result["success"], which is true or false
  17. - $net2ftp_result["errormessage"], which contains an error message
  18. - $net2ftp_result["debug_backtrace"], which contains the debugging backtrace (to indicate *where* the error happened)
  19. ---------------------------------
  20. Low-level function executes a standard PHP function
  21. - If everything goes OK, the low-level function simply returns its $finalresult
  22. - If there is an error, the global variable $net2ftp_result["success"] is set to false, and
  23. $net2ftp_result["errormessage"] will be filled with the error message
  24. ---------------------------------
  25. function low_level {
  26. $result = php_function();
  27. if ($result == false) { setErrorVars(false, "errormessage", debug_backtrace(), __FILE, __LINE__); return false; }
  28. ...
  29. return $finalresult;
  30. }
  31. ---------------------------------
  32. Middle-level function executes a low-level function (it may also execute standard PHP functions)
  33. - If everything goes OK, the middle-level function simply returns its $finalresult
  34. - If there is an error, the function can either return to its parent, or continue
  35. ---------------------------------
  36. function middle_level {
  37. global $net2ftp_result;
  38. $result = low_level();
  39. // Return to its parent, leave the error message as is:
  40. if ($net2ftp_result["success"] == false) { return false; }
  41. // Return to its parent, change the error message (leave the debug backtrace as is):
  42. if ($net2ftp_result["success"] == false) { setErrorVars(false, "errormessage2", $net2ftp_result["debug_backtrace"], __FILE, __LINE__); return false; }
  43. // Reset the error variables and continue:
  44. if ($net2ftp_result["success"] == false) { setErrorVars(true, "", "", "", ""); }
  45. ...
  46. return $finalresult;
  47. // Print error message and exit -- THIS IS NOT DONE ANY MORE, as exit() calls must be avoided at all cost to
  48. // keep net2ftp integrateable within other web applications.
  49. // This case is replaced by case 1: return to the parent function, and leave the error message as is.
  50. //// if ($net2ftp_result["success"] == false) { printErrorMessage(); }
  51. }
  52. ---------------------------------
  53. High-level function executes a middle-level function (it may also execute standard PHP functions)
  54. - If everything goes OK, the high-level function simply returns its $finalresult
  55. - If there is an error, the function returs to its parent (the script which called the net2ftp() function). It is
  56. up to the parent to see if and how an error message should be printed -- see index.php for an example.
  57. ---------------------------------
  58. function high_level {
  59. global $net2ftp_result;
  60. $result = middle_level();
  61. if ($net2ftp_result["success"] == false) { return false; }
  62. ...
  63. return $finalresult;
  64. }
  65. ------------------------------------------------------------------------- */
  66. // **************************************************************************************
  67. // **************************************************************************************
  68. // ** **
  69. // ** **
  70. function setErrorVars($success, $errormessage, $debug_backtrace, $file, $line) {
  71. // --------------
  72. // This function modifies the 3 global error-handling variables
  73. // --------------
  74. // -------------------------------------------------------------------------
  75. // Global variables
  76. // -------------------------------------------------------------------------
  77. global $net2ftp_result, $net2ftp_settings;
  78. // -------------------------------------------------------------------------
  79. // Set the error-handling variables
  80. // -------------------------------------------------------------------------
  81. $net2ftp_result["success"] = $success;
  82. $net2ftp_result["errormessage"] = $errormessage;
  83. $net2ftp_result["debug_backtrace"] = $debug_backtrace;
  84. $net2ftp_result["file"] = $file;
  85. $net2ftp_result["line"] = $line;
  86. // -------------------------------------------------------------------------
  87. // Log the error if an error occured ($success == false)
  88. // If the error vars are set to true again ($success == true), don't log the error once more
  89. // -------------------------------------------------------------------------
  90. // DON'T LOG THE ERROR HERE, AS THE FUNCTION logError() MAY CALL setErrorVars() AGAIN,
  91. // CAUSING AN INFINITE LOOP!
  92. // if ($success == false) {
  93. // logError();
  94. // }
  95. } // end setErrorVars
  96. // ** **
  97. // ** **
  98. // **************************************************************************************
  99. // **************************************************************************************
  100. // **************************************************************************************
  101. // **************************************************************************************
  102. // ** **
  103. // ** **
  104. function net2ftpErrorHandler($errno, $errmsg, $file, $line, $vars) {
  105. // --------------
  106. // This function processes PHP notices, warnings and errors
  107. // --------------
  108. // -------------------------------------------------------------------------
  109. // Global variables
  110. // -------------------------------------------------------------------------
  111. global $net2ftp_output;
  112. // -------------------------------------------------------------------------
  113. // Put error details in variable
  114. // -------------------------------------------------------------------------
  115. if ($errno == E_USER_ERROR || $errno == E_ERROR || $errno == E_PARSE) {
  116. $net2ftp_output["php_error"][] = "Error [$errno] $errstr in file $file on line $line";
  117. echo "Error [$errno] $errstr in file $file on line $line";
  118. exit();
  119. }
  120. elseif ($errorno == E_USER_WARNING || $errno == E_WARNING) {
  121. $net2ftp_output["php_warning"][] = "Warning [$errno] $errstr in file $file on line $line";
  122. }
  123. elseif ($errorno == E_USER_NOTICE) {
  124. $net2ftp_output["php_notice"][] = "Notice [$errno] $errstr in file $file on line $line";
  125. }
  126. else {
  127. $net2ftp_output["php_error"][] = "Unknown error type [$errno] $errstr in file $file on line $line";
  128. }
  129. } // end setErrorVars
  130. // ** **
  131. // ** **
  132. // **************************************************************************************
  133. // **************************************************************************************
  134. ?>