TestingBehavior.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace app\admin\behavior;
  3. /**
  4. * 系统行为扩展:入口index.php的检测自动隐藏
  5. */
  6. class TestingBehavior {
  7. protected static $actionName;
  8. protected static $controllerName;
  9. protected static $moduleName;
  10. /**
  11. * 构造方法
  12. * @param Request $request Request对象
  13. * @access public
  14. */
  15. public function __construct()
  16. {
  17. }
  18. // 行为扩展的执行入口必须是run
  19. public function run(&$params){
  20. self::$actionName = request()->action();
  21. self::$controllerName = request()->controller();
  22. self::$moduleName = request()->module();
  23. // file_put_contents ( DATA_PATH."log.txt", date ( "Y-m-d H:i:s" ) . " " . var_export('admin_TestingBehavior',true) . "\r\n", FILE_APPEND );
  24. $this->_initialize();
  25. }
  26. protected function _initialize() {
  27. $this->checkInlet();
  28. }
  29. /**
  30. * 检测url入口index.php是否被重写隐藏
  31. * @access public
  32. */
  33. static public function checkInlet() {
  34. /*不在以下相应的控制器和操作名里不往下执行,以便提高性能*/
  35. $ctlActArr = array(
  36. 'Index@index',
  37. 'System@clearCache',
  38. );
  39. $ctlActStr = self::$controllerName.'@'.self::$actionName;
  40. if (!in_array($ctlActStr, $ctlActArr)) {
  41. return false;
  42. }
  43. /*--end*/
  44. $now_seo_inlet = 0; // 默认不隐藏入口
  45. /*检测是否支持URL重写隐藏应用的入口文件index.php*/
  46. try {
  47. $response = false;
  48. $url = SITE_URL.'/api/Rewrite/testing.html';
  49. $context = stream_context_set_default(array('http' => array('timeout' => 3,'method'=>'GET')));
  50. $response = @file_get_contents($url,false,$context);
  51. /* $ch = curl_init($url);
  52. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  53. curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
  54. curl_setopt($ch, CURLOPT_TIMEOUT, 3); // 设置cURL允许执行的最长秒数
  55. $response = curl_exec ($ch);
  56. curl_close ($ch); */
  57. if ($response == 'ok') {
  58. $now_seo_inlet = 1;
  59. }
  60. } catch (Exception $e) {}
  61. /*--end*/
  62. $seo_inlet = tpCache('seo.seo_inlet');
  63. if ($seo_inlet != $now_seo_inlet) {
  64. tpCache('seo', array('seo_inlet'=>$now_seo_inlet));
  65. }
  66. }
  67. /**
  68. * 根据IP判断是否本地局域网访问
  69. * @access public
  70. */
  71. static protected function is_local($ip){
  72. if(preg_match('/^(localhost|127\.|192\.)/', $ip) === 1){
  73. return true;
  74. }else{
  75. return false;
  76. }
  77. }
  78. }