Application.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. /**
  3. * HSDN PHP Whois Server Daemon
  4. *
  5. * @author HSDN Team
  6. * @copyright (c) 2015, Information Networks Ltd.
  7. * @link http://www.hsdn.org
  8. */
  9. namespace pWhoisd;
  10. use pWhoisd\Console;
  11. use pWhoisd\Log;
  12. use pWhoisd\Config;
  13. use pWhoisd\Server;
  14. use RuntimeException;
  15. use ErrorException;
  16. define('PWHOISD_VERSION', '0.1.1b');
  17. /**
  18. * Application class.
  19. */
  20. class Application extends Daemon {
  21. /*
  22. * @var object Instance of Config class
  23. */
  24. public static $config;
  25. /*
  26. * @var object Instance of Log class
  27. */
  28. public static $log;
  29. /*
  30. * @var object Instance of Security class
  31. */
  32. public static $security;
  33. /*
  34. * @var object Instance of Server class
  35. */
  36. public static $server;
  37. /**
  38. * Returns instance of Application.
  39. *
  40. * @return Application
  41. */
  42. public static function factory()
  43. {
  44. return new self;
  45. }
  46. /**
  47. * Loads command-line arguments, configuration and Server class.
  48. *
  49. * @return void
  50. */
  51. public function __construct()
  52. {
  53. $this->initialize();
  54. self::$config = new Config;
  55. self::$log = new Log;
  56. self::$security = new Security;
  57. self::$server = new Server;
  58. if (!self::$arguments['daemon'])
  59. {
  60. Console::head();
  61. }
  62. self::$log->debug('Configuration loaded');
  63. }
  64. /**
  65. * Initialize application
  66. *
  67. * @return void
  68. */
  69. private function initialize()
  70. {
  71. set_time_limit(0);
  72. $this->set_exception_handlers();
  73. $this->test_dependencies();
  74. $this->assign_arguments();
  75. if (!self::$arguments['daemon'])
  76. {
  77. Console::help();
  78. }
  79. if (self::$arguments['help'])
  80. {
  81. exit;
  82. }
  83. $this->set_identifiers();
  84. $this->set_signal_handlers();
  85. }
  86. /**
  87. * Sets exception and error handlers
  88. *
  89. * @return void
  90. */
  91. private function set_exception_handlers()
  92. {
  93. set_exception_handler([$this, 'exception_handler']);
  94. set_error_handler([$this, 'error_handler']);
  95. }
  96. /**
  97. * Test application dependencies.
  98. *
  99. * @throws \RuntimeException If require dependence
  100. * @return void
  101. */
  102. private function test_dependencies()
  103. {
  104. version_compare(PHP_VERSION, '5.4', '<') and die('Requires PHP 5.4 or newer.');
  105. if (!extension_loaded('posix'))
  106. {
  107. throw new RuntimeException('Requires POSIX functions support (https://php.net/manual/en/posix.installation.php)');
  108. }
  109. if (!extension_loaded('pcntl'))
  110. {
  111. throw new RuntimeException('Requires PCNTL extension (http://www.php.net/manual/en/pcntl.installation.php)');
  112. }
  113. if (!extension_loaded('filter'))
  114. {
  115. throw new RuntimeException('Requires filter extension (http://www.php.net/manual/en/filter.installation.php)');
  116. }
  117. if (!extension_loaded('gmp') AND !extension_loaded('bcmath'))
  118. {
  119. throw new RuntimeException('Requires GMP or BCMATH extension (http://www.php.net/manual/en/gmp.installation.php)');
  120. }
  121. if (!extension_loaded('sockets'))
  122. {
  123. throw new RuntimeException('Requires sockets extension (http://www.php.net/manual/en/sockets.installation.php)');
  124. }
  125. }
  126. /**
  127. * Exception handler method.
  128. *
  129. * @return void
  130. */
  131. public function exception_handler(\Exception $exception)
  132. {
  133. $message = get_class($exception).': '.$exception->getMessage();
  134. $log = Application::$log;
  135. if (is_object($log))
  136. {
  137. $log->error($message);
  138. }
  139. else
  140. {
  141. Console::log($message, 'red');
  142. }
  143. // Terminate process if exception called before server loop
  144. if (is_null(Application::$server) OR Application::$server->listen_loop === FALSE)
  145. {
  146. Application::terminate(FALSE);
  147. }
  148. }
  149. /**
  150. * Error handler method.
  151. *
  152. * @return void
  153. */
  154. public function error_handler($severity, $message, $file, $line)
  155. {
  156. if (!(error_reporting() & $severity))
  157. {
  158. return;
  159. }
  160. throw new ErrorException('PHP Error: '.$message.'; file: '.$file.'; line: '.$line);
  161. }
  162. } // end of class Application