PageRenderTime 67ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/system/nnframework/helpers/tags.php

https://bitbucket.org/organicdevelopment/joomla-2.5
PHP | 134 lines | 96 code | 12 blank | 26 comment | 18 complexity | 79590754a78fa294aff9a5c60579df6d MD5 | raw file
Possible License(s): LGPL-3.0, GPL-2.0, MIT, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /**
  3. * NoNumber Framework Helper File: Tags
  4. *
  5. * @package NoNumber Framework
  6. * @version 12.8.2
  7. *
  8. * @author Peter van Westen <peter@nonumber.nl>
  9. * @link http://www.nonumber.nl
  10. * @copyright Copyright Š 2012 NoNumber All Rights Reserved
  11. * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
  12. */
  13. // No direct access
  14. defined('_JEXEC') or die;
  15. /**
  16. * Functions
  17. */
  18. class NNTags
  19. {
  20. public static $_version = '12.8.2';
  21. public static function getTagValues($str = '', $keys = array('title'), $separator = '|', $equal = ':', $limit = 0)
  22. {
  23. $s = '[[S]]';
  24. $e = '[[E]]';
  25. $t1 = '[[T]]';
  26. $t2 = '[[/T]]';
  27. // protect all html tags
  28. if (preg_match_all('#</?[a-z][^>]*>#si', $str, $matches, PREG_SET_ORDER) > 0) {
  29. foreach ($matches as $match) {
  30. $str = str_replace($match['0'], $t1.base64_encode($match['0']).$t2, $str);
  31. }
  32. }
  33. // replace separators and equal signs with special markup
  34. $str = str_replace(array($separator, $equal), array($s, $e), $str);
  35. // replace protected separators and equal signs back to original
  36. $str = str_replace(array('\\'.$s, '\\'.$e), array($separator, $equal), $str);
  37. // split string into array
  38. if ($limit) {
  39. $vals = explode($s, $str, (int) $limit);
  40. } else {
  41. $vals = explode($s, $str);
  42. }
  43. // initialize return vars
  44. $t = new stdClass();
  45. $t->params = array();
  46. // loop through splits
  47. foreach($vals as $i => $val) {
  48. // spit part into key and val by equal sign
  49. $keyval = explode($e, $val, 2);
  50. // unprotect tags in key and val
  51. foreach($keyval as $k => $v) {
  52. if (preg_match_all('#'.preg_quote($t1, '#').'(.*?)'.preg_quote($t2, '#').'#si', $v, $matches, PREG_SET_ORDER) > 0) {
  53. foreach ($matches as $match) {
  54. $v = str_replace($match['0'], base64_decode($match['1']), $v);
  55. }
  56. $keyval[$k] = $v;
  57. }
  58. }
  59. if (isset($keys[$i])) {
  60. // if value is in the keys array add as defined in keys array
  61. // ignore equal sign
  62. $t->{$keys[$i]} = implode($equal, $keyval);
  63. unset($keys[$i]);
  64. } else {
  65. // else add as defined in the string
  66. if (isset($keyval['1'])) {
  67. $t->{$keyval['0']} = $keyval['1'];
  68. } else {
  69. $t->params[] = implode($equal, $keyval);
  70. }
  71. }
  72. }
  73. return $t;
  74. }
  75. public static function setSurroundingTags($pre, $post, $tags = 0)
  76. {
  77. if ($tags == 0) {
  78. $tags = array('div', 'p', 'span', 'pre', 'a',
  79. 'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
  80. 'strong', 'b', 'em', 'i', 'u', 'big', 'small', 'font'
  81. );
  82. }
  83. $a = explode('<', $pre);
  84. $b = explode('</', $post);
  85. if (count($b) > 1 && count($a) > 1) {
  86. $a = array_reverse($a);
  87. $a_pre = array_pop($a);
  88. $b_pre = array_shift($b);
  89. $a_tags = $a;
  90. foreach ($a_tags as $i => $a_tag) {
  91. $a[$i] = '<'.trim($a_tag);
  92. $a_tags[$i] = preg_replace('#^([a-z0-9]+).*$#', '\1', trim($a_tag));
  93. }
  94. $b_tags = $b;
  95. foreach ($b_tags as $i => $b_tag) {
  96. $b[$i] = '</'.trim($b_tag);
  97. $b_tags[$i] = preg_replace('#^([a-z0-9]+).*$#', '\1', trim($b_tag));
  98. }
  99. foreach ($b_tags as $i => $b_tag) {
  100. if ($b_tag && in_array($b_tag, $tags)) {
  101. foreach ($a_tags as $j => $a_tag) {
  102. if ($b_tag == $a_tag) {
  103. $a_tags[$i] = '';
  104. $b[$i] = trim(preg_replace('#^</'.$b_tag.'.*?>#', '', $b[$i]));
  105. $a[$j] = trim(preg_replace('#^<'.$a_tag.'.*?>#', '', $a[$j]));
  106. break;
  107. }
  108. }
  109. }
  110. }
  111. foreach ($a_tags as $i => $tag) {
  112. if ($tag && in_array($tag, $tags)) {
  113. array_unshift($b, trim($a[$i]));
  114. $a[$i] = '';
  115. }
  116. }
  117. $a = array_reverse($a);
  118. list($pre, $post) = array(implode('', $a), implode('', $b));
  119. }
  120. return array(trim($pre), trim($post));
  121. }
  122. }