You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
3.9 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3. /**
  4. *
  5. * HookPostControllerConstructor.php
  6. *
  7. * 컨트롤러가 인스턴스화 직후 가동되는 후킹 클래스.
  8. *
  9. */
  10. class HookDisplayOverride {
  11. function init()
  12. {
  13. $this->CI =& get_instance();
  14. $output = !( $this->CI->uri->segment(1) == 'rss' OR strpos($this->CI->uri->segment(1), "sitemap") !== FALSE ) ? $this->set_layout( $this->CI->output->get_output() ) : $this->CI->output->get_output();
  15. $this->CI->output->_display($output);
  16. }
  17. function set_layout($output)
  18. {
  19. if( PAGE_AJAX OR PAGE_INSTALL OR $this->CI->theme === FALSE ) return $output;
  20. // Script Tag를 모두 가져와서 body 밑으로
  21. preg_match_all("/<script\\b[^>]*>([\\s\\S]*?)<\\/script>/", $output, $matches);
  22. $output = preg_replace("/<script\\b[^>]*>([\\s\\S]*?)<\\/script>/","", $output);
  23. $foot = "";
  24. $foot .= $this->CI->site->display_js() . PHP_EOL;
  25. foreach($matches[0] as $match) $foot .= $match;
  26. // 구글애널리틱스 코드가 있다면?
  27. //if( IS_AJAX_REQUEST && ! IS_ADMIN_PAGES && ! $this->CI->agent->is_robot() && ! $this->CI->member->is_super() )
  28. if( ! PAGE_AJAX && ! PAGE_ADMIN && ! $this->CI->agent->is_robot() )
  29. {
  30. $foot .= (! empty($this->CI->site->config('extra_tag_script')) ) ? $this->CI->site->config('extra_tag_script').PHP_EOL : '';
  31. }
  32. // 사이트 채널 연동
  33. $channel = array();
  34. $channel_type_array = array('facebook','instagram','itunes','naver_blog','naver_cafe','naver_pholar','naver_post','naver_storefarm','playstore');
  35. $channel_type = $this->CI->site->config('channel_type') == 'Organization' ? 'Organization' : 'Person';
  36. foreach($channel_type_array as $c)
  37. {
  38. if( $this->CI->site->config('channel_'.$c) ) $channel[] = $this->CI->site->config('channel_'.$c);
  39. }
  40. if( count($channel) > 0 )
  41. {
  42. $channel_arr = array(
  43. "@context" => "http://schema.org",
  44. "@type" => $channel_type,
  45. "name" => $this->CI->site->config('site_title'),
  46. "url" => base_url(),
  47. "sameAS" => $channel
  48. );
  49. $foot .= '<script type="application/ld+json">'.PHP_EOL;
  50. $foot .= json_encode($channel_arr, JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE).PHP_EOL;
  51. $foot .= '</script>';
  52. }
  53. $foot .= "<script src='".base_url('helptool/lang')."'></script>";
  54. $foot .= '</body>'.PHP_EOL;
  55. $foot .= '</html>';
  56. $output = str_replace("</body>", $foot.PHP_EOL."</body>", $output);
  57. // Html minify
  58. ini_set("pcre.recursion_limit", "16777");
  59. $re = '%# Collapse whitespace everywhere but in blacklisted elements.
  60. (?> # Match all whitespans other than single space.
  61. [^\S ]\s* # Either one [\t\r\n\f\v] and zero or more ws,
  62. | \s{2,} # or two or more consecutive-any-whitespace.
  63. ) # Note: The remaining regex consumes no text at all...
  64. (?= # Ensure we are not in a blacklist tag.
  65. [^<]*+ # Either zero or more non-"<" {normal*}
  66. (?: # Begin {(special normal*)*} construct
  67. < # or a < starting a non-blacklist tag.
  68. (?!/?(?:textarea|pre|script)\b)
  69. [^<]*+ # more non-"<" {normal*}
  70. )*+ # Finish "unrolling-the-loop"
  71. (?: # Begin alternation group.
  72. < # Either a blacklist start tag.
  73. (?>textarea|pre|script)\b
  74. | \z # or end of file.
  75. ) # End alternation group.
  76. ) # If we made it here, we are not in a blacklist tag.
  77. %Six';
  78. $newOutput = preg_replace($re, "", $output);
  79. if( $newOutput === null) {
  80. $newOutput = $output;
  81. }
  82. return $newOutput;
  83. }
  84. }