PageRenderTime 65ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/local/AW/Helpdeskultimate/Model/Data/Parser/Text.php

https://bitbucket.org/spenna/alexoo_produzione
PHP | 289 lines | 168 code | 23 blank | 98 comment | 19 complexity | ff4614abe61171f85f467e8d53cf3984 MD5 | raw file
  1. <?php
  2. /**
  3. * aheadWorks Co.
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the EULA
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://ecommerce.aheadworks.com/AW-LICENSE-COMMUNITY.txt
  11. *
  12. * =================================================================
  13. * MAGENTO EDITION USAGE NOTICE
  14. * =================================================================
  15. * This package designed for Magento COMMUNITY edition
  16. * aheadWorks does not guarantee correct work of this extension
  17. * on any other Magento edition except Magento COMMUNITY edition.
  18. * aheadWorks does not provide extension support in case of
  19. * incorrect edition usage.
  20. * =================================================================
  21. *
  22. * @category AW
  23. * @package AW_Helpdeskultimate
  24. * @version 2.9.3
  25. * @copyright Copyright (c) 2010-2012 aheadWorks Co. (http://www.aheadworks.com)
  26. * @license http://ecommerce.aheadworks.com/AW-LICENSE-COMMUNITY.txt
  27. */
  28. /**
  29. * aheadWorks Co.
  30. *
  31. * NOTICE OF LICENSE
  32. *
  33. * This source file is subject to the EULA
  34. * that is bundled with this package in the file LICENSE.txt.
  35. * It is also available through the world-wide-web at this URL:
  36. * http://ecommerce.aheadworks.com/LICENSE-M1.txt
  37. *
  38. * @category AW
  39. * @package AW_Helpdeskultimate
  40. * @copyright Copyright (c) 2009-2010 aheadWorks Co. (http://www.aheadworks.com)
  41. * @license http://ecommerce.aheadworks.com/LICENSE-M1.txt
  42. * @version 1.0
  43. */
  44. class AW_Helpdeskultimate_Model_Data_Parser_Text extends AW_Helpdeskultimate_Model_Data_Parser_Abstract {
  45. /**
  46. * They expect this function to do something but its truly excessive
  47. */
  48. public function clear() {
  49. $__text = htmlspecialchars($this->getText());
  50. $this->setText($__text);
  51. return $this;
  52. }
  53. /**
  54. * They expect this function to do something but its truly excessive
  55. */
  56. public function convertToQuoteAsHtml() {
  57. $text = $this->clear()->getText();
  58. $text = $this->__microparseBlockquotes(html_entity_decode($text));
  59. //wrap line in <p></p>
  60. $text = preg_replace("/([^\n\r$]+)[^>][\n\r$]+/", "<p>$1</p>", $text);
  61. //remove \n \r
  62. $text = preg_replace("/([\n\r$]+)/", "", $text);
  63. $text = "<blockquote>" . $text . "</blockquote>";
  64. $this->setText($text);
  65. return $this;
  66. }
  67. /**
  68. * They expect this function to do something but its truly excessive
  69. */
  70. public function convertToQuoteAsText() {
  71. //$text = strip_tags($this->getText());
  72. $text = preg_replace("/(([^\n\r]*)([\n\r]+))/", ">$0", $this->getText() . "\n\r");
  73. $this->setText($text);
  74. return $this;
  75. }
  76. public function convertToQuote() {
  77. return $this->convertToQuoteAsText();
  78. }
  79. /**
  80. *
  81. */
  82. public function prepareToDisplay($isStripAttributes = true) {
  83. $__raw = $this->clear()->getText();
  84. $__raw = $this->__microparseBlockquotes(html_entity_decode($__raw));
  85. $this->__pool = $__raw;
  86. $this->__inserts = array();
  87. $__changesCount = 1;
  88. while ($__changesCount > 0) {
  89. $__changesCount = 0;
  90. $__changesCount += $this->__microparseLinks();
  91. }
  92. /* $__changesCount = 1;
  93. while ($__changesCount > 0) {
  94. $__changesCount = 0;
  95. $__changesCount += $this->__microparseBoldtext();
  96. $__changesCount += $this->__microparseItalictext();
  97. $__changesCount += $this->__microparseUnderlinedtext();
  98. }*/
  99. $__changesCount = 1;
  100. while ($__changesCount > 0) {
  101. $__changesCount = 0;
  102. $__changesCount += $this->__microparseNewlinesymbols();
  103. }
  104. // $__lines[$__key] = preg_replace('/<(http|https):\/\/([\w\.]*\w+\.)([\w-]{3})([\w\/\.\?\&-]*)>/', '<a href="$1://$2$3$4"><img alt="&raquo;" src="" /></a>', $__lines[$__key]);
  105. // $__lines[$__key] = preg_replace('/\*(\w[\w]*\w)\*/', '<b>$1</b>', $__lines[$__key]);
  106. // $__lines[$__key] = preg_replace('/\/(\w[\w]*\w)\//', '<i>$1</i>', $__lines[$__key]);
  107. // $__lines[$__key] = preg_replace('/_(\w[\w]*\w)_/', '<u>$1</u>', $__lines[$__key]);
  108. $__point = 0;
  109. $__text = '';
  110. foreach ($this->__inserts as $__position => $__contentItems) {
  111. $__text .= substr($this->__pool, $__point, $__position - $__point);
  112. foreach ($__contentItems as $__item) $__text .= $__item;
  113. $__point = $__position;
  114. }
  115. $__text .= substr($this->__pool, $__point, strlen($this->__pool));
  116. //convert #AAA-11111 to link to ticket
  117. $smartText = Mage::helper('helpdeskultimate/parser')->getSmartText($__text);
  118. $this->setText($smartText);
  119. return $this;
  120. }
  121. /**
  122. *
  123. */
  124. private $__pool;
  125. private $__inserts;
  126. /**
  127. *
  128. */
  129. private function __removeFromPool($index, $count) {
  130. $this->__pool = substr_replace($this->__pool, '', $index, $count);
  131. foreach ($this->__inserts as $__position => $__contentItems) {
  132. if ($__position > $index) {
  133. if (!isset($this->__inserts[$__position - $count])) $this->__inserts[$__position - $count] = array();
  134. foreach ($this->__inserts[$__position] as $__item) {
  135. array_push($this->__inserts[$__position - $count], $__item);
  136. }
  137. unset($this->__inserts[$__position]);
  138. }
  139. }
  140. ksort($this->__inserts);
  141. }
  142. /**
  143. *
  144. */
  145. private function __insertIntoPool($position, $content) {
  146. if (!isset($this->__inserts[$position])) $this->__inserts[$position] = array();
  147. array_push($this->__inserts[$position], $content);
  148. }
  149. /**
  150. *
  151. */
  152. private function __microparseLinks() {
  153. $__changesCount = 0;
  154. while (preg_match('/<(http|https):\/\/([\w\.]*\w+\.)([\w-]{3})([\w\/\.\?\&-]*)>/U', $this->__pool, $__matches, PREG_OFFSET_CAPTURE)) {
  155. $__changesCount++;
  156. $this->__removeFromPool($__matches[0][1], strlen($__matches[0][0]));
  157. $this->__insertIntoPool($__matches[0][1], '<a href="' . $__matches[1][0] . '://' . $__matches[2][0] . $__matches[3][0] . $__matches[4][0] . '">' . $__matches[1][0] . '://' . $__matches[2][0] . $__matches[3][0] . $__matches[4][0] . '</a>');
  158. }
  159. return $__changesCount;
  160. }
  161. /**
  162. *
  163. */
  164. private function __microparseBoldtext() {
  165. $__changesCount = 0;
  166. while (preg_match('|(\*)([\w][^\n]*[\w])(\*)|U', $this->__pool, $__matches, PREG_OFFSET_CAPTURE)) {
  167. $__changesCount++;
  168. $this->__removeFromPool($__matches[1][1], 1);
  169. $this->__insertIntoPool($__matches[1][1], '<b>');
  170. $this->__removeFromPool($__matches[3][1] - 1, 1);
  171. $this->__insertIntoPool($__matches[3][1] - 1, '</b>');
  172. }
  173. return $__changesCount;
  174. }
  175. /**
  176. *
  177. */
  178. private function __microparseItalictext() {
  179. $__changesCount = 0;
  180. while (preg_match('|(\/)([\w][^\n]*[\w])(\/)|U', $this->__pool, $__matches, PREG_OFFSET_CAPTURE)) {
  181. $__changesCount++;
  182. $this->__removeFromPool($__matches[1][1], 1);
  183. $this->__insertIntoPool($__matches[1][1], '<i>');
  184. $this->__removeFromPool($__matches[3][1] - 1, 1);
  185. $this->__insertIntoPool($__matches[3][1] - 1, '</i>');
  186. }
  187. return $__changesCount;
  188. }
  189. /**
  190. *
  191. */
  192. private function __microparseUnderlinedtext() {
  193. $__changesCount = 0;
  194. while (preg_match('|(_)([\w][^\n]*[\w])(_)|U', $this->__pool, $__matches, PREG_OFFSET_CAPTURE)) {
  195. $__changesCount++;
  196. $this->__removeFromPool($__matches[1][1], 1);
  197. $this->__insertIntoPool($__matches[1][1], '<u>');
  198. $this->__removeFromPool($__matches[3][1] - 1, 1);
  199. $this->__insertIntoPool($__matches[3][1] - 1, '</u>');
  200. }
  201. return $__changesCount;
  202. }
  203. /**
  204. *
  205. */
  206. private function __microparseNewlinesymbols() {
  207. $__changesCount = 0;
  208. while (preg_match('|(\n)|U', $this->__pool, $__matches, PREG_OFFSET_CAPTURE)) {
  209. $__changesCount++;
  210. $this->__removeFromPool($__matches[1][1], 1);
  211. $this->__insertIntoPool($__matches[1][1], "<br />\n");
  212. }
  213. return $__changesCount;
  214. }
  215. private function __microparseBlockquotes($text) {
  216. $text = htmlentities($text, ENT_QUOTES, 'UTF-8');
  217. $__depth = 15;
  218. $__exit = false;
  219. while (!$__exit && $__depth > 0) {
  220. $__exit = true;
  221. $__depth--;
  222. //inline fragmentation
  223. preg_match_all("/([^\n\r]*)[\n\r]{0,2}/", $text, $matches, PREG_OFFSET_CAPTURE);
  224. //found blockquotes
  225. $replacementArray = array();
  226. $_currentQuote = null;
  227. $_currentQuotedText = "";
  228. foreach($matches[1] as $quote) {
  229. preg_match("/^[\d]*(&gt;)(.*)/", $quote[0], $isFounded, PREG_OFFSET_CAPTURE);
  230. if (!isset($isFounded[2]) || is_null($isFounded[2])) {
  231. if (!is_null($_currentQuote)) {
  232. $__exit = false;
  233. $replacementArray[] = array('from' => $_currentQuote, 'to' => $quote[1], 'text' => $_currentQuotedText);
  234. $_currentQuote = null;
  235. $_currentQuotedText = "";
  236. }
  237. }
  238. else {
  239. if (is_null($_currentQuote)) {
  240. $_currentQuote = $quote[1];
  241. }
  242. if (isset($isFounded[2])) {
  243. $_currentQuotedText .= $isFounded[2][0] . "\n\r";
  244. }
  245. else {
  246. $_currentQuotedText .= "\n\r";
  247. }
  248. }
  249. }
  250. //create new line with blockquote tag
  251. $__newText = '';
  252. $__offset = 0;
  253. foreach ($replacementArray as $rule) {
  254. $__newText .= substr($text, $__offset, $rule['from'] - $__offset);
  255. $__newText .= "<blockquote>\n" . $rule['text'] . "</blockquote>\n";
  256. $__offset = $rule['to'];
  257. }
  258. $__newText .= substr($text, $__offset);
  259. $text = $__newText;
  260. //repeat if replacementArray not empty
  261. }
  262. //remove \n from tag end
  263. $text = preg_replace("/(blockquote>\n)/", "blockquote>", $text);
  264. return $text;
  265. }
  266. }