Guest User

Untitled

a guest
Jun 22nd, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1. <?php
  2. /**
  3. * @author Philip Sturgeon
  4. * @created 09/02/2009
  5. */
  6.  
  7. class OutputBuffer
  8. {
  9. private static $replace_from = array();
  10. private static $replace_to = array();
  11.  
  12. public static $title_separator = ' | ';
  13.  
  14. /**
  15. * Override the title at any point
  16. *
  17. * @param string $new_title The new title to output
  18. */
  19. public static function set_title($title_parts = array())
  20. {
  21. if(!is_array($title_parts))
  22. {
  23. $title_parts =& func_get_args();
  24. }
  25.  
  26. $title = htmlspecialchars(trim(implode(self::$title_separator, $title_parts)));
  27.  
  28. self::set_regex('#<title>(.+)</title>#', '<title>'.$title.'</title>');
  29. }
  30.  
  31.  
  32. /**
  33. * Override head tag (or add it if missing)
  34. *
  35. * @param string $new_title The new title to output
  36. */
  37. public static function set_head($content = '')
  38. {
  39. self::set_regex('#</head>#', "\t".$content."\n</head>");
  40. }
  41.  
  42.  
  43. /**
  44. * Override meta data (or add it if missing)
  45. *
  46. * @param string $new_title The new title to output
  47. */
  48. public static function set_metadata($name = '')
  49. {
  50. // Accepts unlimited arguments (2nd+)
  51. $args =& func_get_args();
  52. unset($args[0]);
  53.  
  54. // Remove HTML and join the arguments
  55. $content = strip_tags( implode(' ', $args) );
  56.  
  57. // Keyword specific replacing
  58. if($name == 'keywords')
  59. {
  60. $content = str_replace(array('_', '-', '(', ')', '!', '?', ':', '"', ',', '.'), '', $content);
  61. $content = str_replace(array(' ', ',of,', ',and,', ',in,'), ',', $content);
  62. }
  63.  
  64. // If this meta data exists, replace it
  65. if(strpos(ob_get_contents(), '<meta name="'.$name.'"'))
  66. {
  67. self::set_regex('#<meta name="'.$name.'" content="([^"]+)?"#', '<meta name="'.$name.'" content="'.trim($content).'"');
  68. }
  69.  
  70. // Otherwise, add it after the </title> tag
  71. else
  72. {
  73. self::set_regex('#</title>#', '</title>'."\n\t".'<meta name="'.$name.'" content="'.trim($content).'"/>');
  74. }
  75. }
  76.  
  77. /**
  78. * Override meta data (or add it if missing)
  79. *
  80. * @param string $new_title The new title to output
  81. */
  82. public static function set_canonical($link = '')
  83. {
  84. global $smarty;
  85.  
  86. $url = smarty_outputfilter_linkparser($link, $smarty);
  87.  
  88. // If canonical tag exists, replace it
  89. if(strpos(ob_get_contents(), '<link rel="canonical"'))
  90. {
  91. self::set_regex('#<link rel="canonical" href="([^"]+)?"#', '<link rel="canonical" href="'.trim($url).'"');
  92. }
  93.  
  94. // Otherwise, add it after the </title> tag
  95. else
  96. {
  97. self::set_regex('#</title>#', '</title>'."\n\t".'<link rel="canonical" href="'.trim($url).'"/>');
  98. }
  99. }
  100.  
  101. /**
  102. * Override the title at any point
  103. *
  104. * @param string $new_title The new title to output
  105. */
  106. public static function set_regex($from, $to)
  107. {
  108. self::$replace_from[] = $from;
  109.  
  110. self::$replace_to[] = str_replace('$', '\$', $to);
  111. }
  112.  
  113.  
  114. /**
  115. * Override the title at any point
  116. *
  117. * @param string $new_title The new title to output
  118. */
  119. public static function replace()
  120. {
  121. // Get, clean and end the current buffer
  122. $current_buffer = ob_get_clean();
  123.  
  124. // Start a new one
  125. ob_start();
  126.  
  127. // Output the old buffer with the changes as the new buffer
  128. echo preg_replace(
  129. self::$replace_from,
  130. self::$replace_to,
  131. $current_buffer);
  132.  
  133. // Reset the replace pieces in case this is randomly called again
  134. self::$replace_from = array();
  135. self::$replace_to = array();
  136.  
  137. }
  138.  
  139. }
  140. ?>
Add Comment
Please, Sign In to add comment