/src/classes/XLite/Model/MailImageParser.php

https://github.com/Koc/core · PHP · 215 lines · 75 code · 44 blank · 96 comment · 16 complexity · 528e6ca05f94d4896eebf5e2259539e6 MD5 · raw file

  1. <?php
  2. // vim: set ts=4 sw=4 sts=4 et:
  3. /**
  4. * LiteCommerce
  5. *
  6. * NOTICE OF LICENSE
  7. *
  8. * This source file is subject to the Open Software License (OSL 3.0)
  9. * that is bundled with this package in the file LICENSE.txt.
  10. * It is also available through the world-wide-web at this URL:
  11. * http://opensource.org/licenses/osl-3.0.php
  12. * If you did not receive a copy of the license and are unable to
  13. * obtain it through the world-wide-web, please send an email
  14. * to licensing@litecommerce.com so we can send you a copy immediately.
  15. *
  16. * PHP version 5.3.0
  17. *
  18. * @category LiteCommerce
  19. * @author Creative Development LLC <info@cdev.ru>
  20. * @copyright Copyright (c) 2011 Creative Development LLC <info@cdev.ru>. All rights reserved
  21. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  22. * @link http://www.litecommerce.com/
  23. * @see ____file_see____
  24. * @since 1.0.0
  25. */
  26. namespace XLite\Model;
  27. /**
  28. * Mail images parser
  29. * TODO: full refactoring is required
  30. *
  31. * @see ____class_see____
  32. * @since 1.0.0
  33. */
  34. class MailImageParser extends \XLite\Core\FlexyCompiler
  35. {
  36. /**
  37. * webdir
  38. *
  39. * @var string
  40. * @see ____var_see____
  41. * @since 1.0.0
  42. */
  43. public $webdir;
  44. /**
  45. * images
  46. *
  47. * @var array
  48. * @see ____var_see____
  49. * @since 1.0.0
  50. */
  51. public $images;
  52. /**
  53. * counter
  54. *
  55. * @var integer
  56. * @see ____var_see____
  57. * @since 1.0.0
  58. */
  59. public $counter;
  60. /**
  61. * Constructor
  62. * FIXME - we must found anoither way... now it is antipattern Public Morozov
  63. *
  64. * @return void
  65. * @see ____func_see____
  66. * @since 1.0.0
  67. */
  68. public function __construct()
  69. {
  70. parent::__construct();
  71. }
  72. /**
  73. * flexy
  74. *
  75. * @return void
  76. * @see ____func_see____
  77. * @since 1.0.0
  78. */
  79. public function flexy()
  80. {
  81. }
  82. /**
  83. * postprocess
  84. *
  85. * @return void
  86. * @see ____func_see____
  87. * @since 1.0.0
  88. */
  89. public function postprocess()
  90. {
  91. $this->images = array();
  92. $this->counter = 1;
  93. // find images, e.g. background=..., src=..., style="...url('...')"
  94. for ($i = 0; count($this->tokens) > $i; $i++) {
  95. $token = $this->tokens[$i];
  96. if ('attribute' == $token['type']) {
  97. $name = strtolower($token['name']);
  98. } elseif ('attribute-value' == $token['type']) {
  99. $val = $this->getTokenText($i);
  100. if ('style' == $name) {
  101. $pos = strpos($val, 'url(');
  102. if (false !== $pos) {
  103. $this->substImage(
  104. $pos + 5 + $token['start'],
  105. strpos($val, ')') + $token['start'] - 1
  106. );
  107. }
  108. } elseif ('background' == $name || 'src' == $name) {
  109. $this->substImage($token['start'], $token['end']);
  110. }
  111. $name = '';
  112. } else {
  113. $name = '';
  114. }
  115. }
  116. $this->result = $this->substitute();
  117. }
  118. /**
  119. * substImage
  120. *
  121. * @param mixed $start ____param_comment____
  122. * @param mixed $end ____param_comment____
  123. *
  124. * @return void
  125. * @see ____func_see____
  126. * @since 1.0.0
  127. */
  128. public function substImage($start, $end)
  129. {
  130. $img = substr($this->source, $start, $end-$start);
  131. if (strcasecmp(substr($img, 0, 5), 'http:')) {
  132. $img = $this->webdir . $img; // relative URL
  133. }
  134. $img = str_replace('&amp;', '&', $img);
  135. $img = str_replace(' ', '%20', $img);
  136. $this->subst($start, $end, $this->getImgSubstitution($img));
  137. }
  138. /**
  139. * getImgSubstitution
  140. *
  141. * @param mixed $img ____param_comment____
  142. *
  143. * @return void
  144. * @see ____func_see____
  145. * @since 1.0.0
  146. */
  147. public function getImgSubstitution($img)
  148. {
  149. if (!isset($this->images[$img])) {
  150. // fetch image
  151. if (($fd = @fopen($img, 'rb'))) {
  152. $image = '';
  153. while (!feof($fd)) {
  154. $image .= fgets($fd, 10000);
  155. }
  156. fclose($fd);
  157. $info = getimagesize($img);
  158. $this->images[$img] = array(
  159. 'name' => basename($img),
  160. 'data' => $image,
  161. 'mime' => $info['mime']
  162. );
  163. $this->counter++;
  164. } else {
  165. // can't fetch
  166. return $img;
  167. }
  168. }
  169. return 'cid:' . $this->images[$img]['name'] . '@mail.lc';
  170. }
  171. }