PageRenderTime 25ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Utils/StringEscape.php

https://gitlab.com/cpteam/core
PHP | 231 lines | 166 code | 29 blank | 36 comment | 16 complexity | 95fb4bbbe520f94f09226a3cf59a17dc MD5 | raw file
  1. <?php
  2. namespace CPTeam\Utils;
  3. /**
  4. * Class String Escape
  5. *
  6. * @author Lukas "Arzi" Klima
  7. * @version 0.2
  8. * @license MIT
  9. */
  10. class StringEscape
  11. {
  12. const LENGTH_SHORT = 520;
  13. /** Escape Js, divneho paragrafu, whitespaces, double enter, make url, make <br>
  14. *
  15. * @param $text
  16. * @return mixed|string
  17. * @deprecated
  18. */
  19. public static function basic($text, $lenght = self::LENGTH_SHORT)
  20. {
  21. $text = self::escapeHtml($text);
  22. if ($lenght == false) {
  23. $text = self::short($text, $lenght);
  24. }
  25. $text = self::char($text);
  26. $text = self::trim($text);
  27. $text = str_ireplace("\n", "\n<br>", $text);
  28. return $text;
  29. }
  30. public static function escapeHtml($text)
  31. {
  32. return htmlspecialchars($text, ENT_COMPAT | ENT_HTML401, 'ISO-8859-1');
  33. }
  34. public static function short($text, $length = self::LENGTH_SHORT)
  35. {
  36. $textLenght = mb_strlen($text);
  37. if ($textLenght < $length) {
  38. return $text;
  39. }
  40. $space = false;
  41. for ($i = $length; $i >= 0; $i--) {
  42. if (in_array(mb_substr($text, $i, 2), [". ", "! ", "? ", " ", "\n"])) {
  43. return mb_substr($text, 0, $i + 1) . " <a href='' onClick='$(this).next().show(); $(this).remove();return false;'>Zobrazit vše</a><div style='display: none'>" . mb_substr($text, $i + 2) . "</div>";
  44. }
  45. }
  46. }
  47. /**
  48. * @param $text
  49. * @return mixed
  50. */
  51. public static function char($text)
  52. {
  53. $a = [
  54. '`' => "'",
  55. ];
  56. foreach ($a as $b => $c) {
  57. $text = str_replace($b, $c, $text);
  58. }
  59. return $text;
  60. }
  61. public static function trim($text)
  62. {
  63. $text = trim($text);
  64. return self::doubleLine($text);
  65. }
  66. private static function doubleLine($text)
  67. {
  68. return preg_replace("/^(\s*(\r\n|\n|\r)){2,}/m", "\n", $text);
  69. }
  70. /** @deprecated */
  71. public static function toDatabase($text)
  72. {
  73. $text = self::escapeHtml($text);
  74. $text = self::char($text);
  75. $text = self::trim($text);
  76. return $text;
  77. }
  78. public static function preview($text, $length = self::LENGTH_SHORT)
  79. {
  80. $textLenght = mb_strlen($text);
  81. if ($textLenght < $length) {
  82. return $text;
  83. }
  84. $space = false;
  85. for ($i = $length; $i >= 0; $i--) {
  86. if (mb_substr($text, $i, 1) == " ") {
  87. $space = true;
  88. } elseif ($space == true && (mb_substr($text, $i, 1) == "." || mb_substr($text, $i, 1) == "!" || mb_substr($text, $i, 1) == "?")) {
  89. return mb_substr($text, 0, $i + 1) . " [...]";
  90. } else {
  91. $space = false;
  92. }
  93. }
  94. }
  95. public static function fromTextarea($text)
  96. {
  97. $text = self::escapeHtml($text);
  98. $text = self::char($text);
  99. $text = self::trim($text);
  100. $text = self::url($text);
  101. $text = self::createList($text);
  102. return str_ireplace("\n", "<br />", $text);
  103. }
  104. /**
  105. * @param $url
  106. * @return mixed
  107. */
  108. public static function url($url)
  109. {
  110. $apostrof = "'";
  111. $reg_exUrl = ('/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;"' . $apostrof . '()]*[-a-z0-9+&@#\/%=~"_|()]/im');
  112. $url = preg_replace('/^\b((?!(?:https?|ftp):\/\/)(www\.))/im', 'http://www.', $url);
  113. return preg_replace($reg_exUrl, '<i><a href="$0" target="_blank" rel="nofollow">$0</a></i>', $url);
  114. }
  115. public static function createList($text)
  116. {
  117. $text = preg_replace("/\*+(.*)?/i", "<ul><li>$1</li></ul>", $text);
  118. $text = preg_replace("/(\<\/ul\>\n(.*)\<ul\>*)+/", "", $text);
  119. return $text;
  120. }
  121. public static function seo($data)
  122. {
  123. $transfer = ["á" => "a", "ä" => "a", "č" => "c", "ď" => "d", "é" => "e", "ě" => "e", "ë" => "e", "í" => "i", "&#239;" => "i", "ň" => "n", "ó" => "o", "ō" => "ou", "Ō" => "Oo", "ö" => "o", "ř" => "r", "š" => "s", "ť" => "t", "ú" => "u", "ů" => "u", "ü" => "u", "ý" => "y", "&#255;" => "y", "ž" => "z", "Á" => "A", "Ä" => "A", "Č" => "C", "Ď" => "D", "É" => "E", "Ě" => "E", "Ë" => "E", "Í" => "I", "&#207;" => "I", "Ň" => "N", "Ó" => "O", "Ö" => "O", "Ř" => "R", "Š" => "S", "Ť" => "T", "Ú" => "U", "Ů" => "U", "Ü" => "U", "Ý" => "Y", "&#376;" => "Y", "Ž" => "Z"];
  124. $data = strtr($data, $transfer);
  125. $data = strtolower($data);
  126. $data = preg_replace("/[^[:alpha:][:digit:]]/", "-", $data);
  127. $data = trim($data, "-");
  128. $data = preg_replace("/[-]+/", "-", $data);
  129. return $data;
  130. }
  131. /**
  132. * @param $url
  133. * @return mixed
  134. */
  135. public static function getYoutubeUrl($text)
  136. {
  137. if (preg_match('/youtube\.com\/watch\?v=([^\&\?\/]+)/', $text, $id)) {
  138. return $id[1];
  139. } else if (preg_match('/youtube\.com\/embed\/([^\&\?\/]+)/', $text, $id)) {
  140. return $id[1];
  141. } else if (preg_match('/youtube\.com\/v\/([^\&\?\/]+)/', $text, $id)) {
  142. return $id[1];
  143. } else if (preg_match('/youtu\.be\/([^\&\?\/]+)/', $text, $id)) {
  144. return $id[1];
  145. } else if (preg_match('/youtube\.com\/verify_age\?next_url=\/watch%3Fv%3D([^\&\?\/]+)/', $text, $id)) {
  146. return $id[1];
  147. }
  148. return false;
  149. }
  150. public static $rules = [
  151. '/(#+)(.*)/' => 'self::header', // headers
  152. /* '/\[([^\[]+)\]\(([^\)]+)\)/' => '<a href=\'\2\'>\1</a>', // links
  153. '/(\*\*|__)(.*?)\1/' => '<strong>\2</strong>', // bold
  154. '/(\*|_)(.*?)\1/' => '<em>\2</em>', // emphasis
  155. '/\~\~(.*?)\~\~/' => '<del>\1</del>', // del
  156. '/\:\"(.*?)\"\:/' => '<q>\1</q>', // quote
  157. '/`(.*?)`/' => '<code>\1</code>', // inline code
  158. '/\n\*(.*)/' => 'self::ul_list', // ul lists
  159. '/\n[0-9]+\.(.*)/' => 'self::ol_list', // ol lists
  160. '/\n(&gt;|\>)(.*)/' => 'self::blockquote ', // blockquotes
  161. '/\n-{5,}/' => "\n<hr />", // horizontal rule
  162. '/\n([^\n]+)\n/' => 'self::para', // add paragraphs
  163. '/<\/ul>\s?<ul>/' => '', // fix extra ul
  164. '/<\/ol>\s?<ol>/' => '', // fix extra ol
  165. '/<\/blockquote><blockquote>/' => "\n" // fix extra blockquote
  166. */
  167. ];
  168. public static function markDown($text)
  169. {
  170. foreach (self::$rules as $regex => $replacement) {
  171. if (is_callable($replacement)) {
  172. $text = preg_replace_callback($regex, $replacement, $text);
  173. } else {
  174. $text = preg_replace($regex, $replacement, $text);
  175. }
  176. }
  177. return $text;
  178. }
  179. private static function header($regs)
  180. {
  181. list ($tmp, $chars, $header) = $regs;
  182. $level = strlen($chars);
  183. return sprintf('<h%d>%s</h%d>', $level, trim($header), $level);
  184. }
  185. }