Edit via SFTP
  1. <?php namespace ferret\classloader;
  2. /*
  3.   PURPOSE: include this file to enable Ferreteria functionality
  4.   INPUT:
  5.   $kfpConfigFerreteria must point to the local Ferreteria configuration folder (no trailing slash)
  6.   HISTORY:
  7.   2019-08-16 created in order to simplify things a bit, hopefully
  8.   2020-01-15 added fatal error handling
  9.   2021-03-18 ferret.data now depends on ferret.fields, so will include automatically
  10. */
  11. require('ClassLoader.php'); // invoke the Class Loader
  12. #\ferret\classloader\cLoader::SetDebugMode(TRUE);
  13.  
  14. if (php_sapi_name() == 'cli') {
  15. require 'base/env-cli.php';
  16. } else {
  17. require 'base/env-web.php';
  18. }
  19.  
  20. use ferret\cStackElement;
  21. use ferret\cStackTrace;
  22. use ferret\cEnv;
  23.  
  24. cLoader::SetBasePath(__DIR__);
  25. new cLibraryExec('ferret.base','base/0.php');
  26. new cLibraryExec('ferret.data','data/0.php');
  27. #new cLibraryExec('ferret.mw.core','mw/@lib.php'); // should only be added in MediaWiki environment
  28. #new cLibraryExec('ferret.forms','fields/0.php'); // replaces forms
  29. new cLibraryExec('ferret.login','user-access/0.php');
  30. new cLibraryExec('ferret.odata','odata/0.php');
  31.  
  32. //require('base/@lib.php'); // need the base library classes to access fcGlobals
  33. cLoader::LoadLibrary('ferret.base');
  34. require($kfpConfigFerreteria.'/portable/defaults.php'); // local config for fcGlobals
  35.  
  36. $ns = __NAMESPACE__.'\\';
  37. $ok = set_error_handler($ns."error_handler");
  38. register_shutdown_function($ns."shutdown_handler" );
  39.  
  40. function error_handler($errno,$errstr,$errfile,$errline,$errcontext=NULL) : bool {
  41. global $sDangerStatus; // set this whenever a possibly-dangerous operation needs to report what it was doing
  42.  
  43. #$error = error_get_last();
  44.  
  45. echo cEnv::HorizLine().cEnv::TitleIt('Fatal Error').cEnv::NewLine();
  46.  
  47. if( $errfile !== NULL) {
  48. #$errno = $error["type"];
  49. #$errfile = $error["file"];
  50. #$errline = $error["line"];
  51. #$errstr = $error["message"];
  52.  
  53. $oElem = new cStackElement;
  54. $oElem->FileSetup($errfile,$errline);
  55. #$htLinks = cCheckPoint::RenderTraceLinks($errfile,$errline);
  56. $htLinks = $oElem->RenderIDELinks();
  57. $CRLF = cEnv::NewLine();
  58.  
  59. echo
  60. cEnv::BoldIt('PHP ERROR')
  61. .' #'.cEnv::BoldIt($errno)
  62. .' in '.cEnv::BoldIt($errfile)
  63. .' line '.cEnv::BoldIt($errline)
  64. ." $htLinks:"
  65. .$CRLF
  66. .' - '.cEnv::ItalIt($errstr)
  67. ;
  68. if (!is_null($sDangerStatus)) {
  69. echo $CRLF.$sDangerStatus;
  70. }
  71. #$sContext = print_r($errcontext,TRUE);
  72. $sContext = \fcArray::Render($errcontext,['max'=>3]);
  73. echo $CRLF.'CONTEXT: '.$sContext.$CRLF.$CRLF;
  74.  
  75. // try to show a stack trace
  76. $oStack = new cStackTrace;
  77. echo $oStack->RenderTrace();
  78. }
  79. return TRUE; // no need for PHP to handle further
  80. }
  81.  
  82. function shutdown_handler() {
  83. global $sDangerStatus; // set this whenever a possibly-dangerous operation needs to report what it was doing
  84.  
  85. $errfile = "unknown file";
  86. $errstr = "shutdown";
  87. $errno = E_CORE_ERROR;
  88. $errline = 0;
  89.  
  90. $error = error_get_last();
  91.  
  92. if( $error !== NULL) {
  93. $CRLF = cEnv::NewLine();
  94.  
  95. $errno = $error["type"];
  96. $errfile = $error["file"];
  97. $errline = $error["line"];
  98. $errstr = $error["message"];
  99.  
  100. $oElem = new cStackElement;
  101. $oElem->FileSetup($errfile,$errline);
  102. $htLinks = $oElem->RenderIDELinks();
  103.  
  104. echo
  105. cEnv::TitleIt('Shutdown')
  106. .'Error #'.cEnv::BoldIt($errno)
  107. .' in '.cEnv::BoldIt($errfile)
  108. .' line '.cEnv::BoldIt($errline)
  109. .$htLinks.':'.$CRLF
  110. .' - '.cEnv::ItalIt("<pre>$errstr</pre>")
  111. ;
  112.  
  113. if (!is_null($sDangerStatus)) {
  114. echo $CRLF.$sDangerStatus;
  115. }
  116. }
  117. }
  118.