simplenews.source.inc 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  1. <?php
  2. /**
  3. * @file
  4. * Contains SimplenewsSource interface and implementations.
  5. */
  6. /**
  7. * The source used to build a newsletter mail.
  8. *
  9. * @ingroup source
  10. */
  11. interface SimplenewsSourceInterface {
  12. /**
  13. * Returns the mail headers.
  14. *
  15. * @param $headers
  16. * The default mail headers.
  17. *
  18. * @return
  19. * Mail headers as an array.
  20. */
  21. function getHeaders(array $headers);
  22. /**
  23. * Returns the mail subject.
  24. */
  25. function getSubject();
  26. /**
  27. * Returns the mail body.
  28. *
  29. * The body should either be plaintext or html, depending on the format.
  30. */
  31. function getBody();
  32. /**
  33. * Returns the plaintext body.
  34. */
  35. function getPlainBody();
  36. /**
  37. * Returns the mail footer.
  38. *
  39. * The footer should either be plaintext or html, depending on the format.
  40. */
  41. function getFooter();
  42. /**
  43. * Returns the plain footer.
  44. */
  45. function getPlainFooter();
  46. /**
  47. * Returns the mail format.
  48. *
  49. * @return
  50. * The mail format as string, either 'plain' or 'html'.
  51. */
  52. function getFormat();
  53. /**
  54. * Returns the recipent of this newsletter mail.
  55. *
  56. * @return
  57. * The recipient mail address(es) of this newsletter as a string.
  58. */
  59. function getRecipient();
  60. /**
  61. * The language that should be used for this newsletter mail.
  62. */
  63. function getLanguage();
  64. /**
  65. * Returns an array of attachments for this newsletter mail.
  66. *
  67. * @return
  68. * An array of managed file objects with properties uri, filemime and so on.
  69. */
  70. function getAttachments();
  71. /**
  72. * Returns the token context to be used with token replacements.
  73. *
  74. * @return
  75. * An array of objects as required by token_replace().
  76. */
  77. function getTokenContext();
  78. /**
  79. * Returns the mail key to be used for drupal_mail().
  80. *
  81. * @return
  82. * The mail key, either test or node.
  83. */
  84. function getKey();
  85. /**
  86. * Returns the formatted from mail address.
  87. */
  88. function getFromFormatted();
  89. /**
  90. * Returns the plain mail address.
  91. */
  92. function getFromAddress();
  93. }
  94. /**
  95. * Source interface based on a node.
  96. *
  97. * This is the interface that needs to be implemented to be compatible with
  98. * the default simplenews spool implementation and therefore exposed in
  99. * hook_simplenews_source_cache_info().
  100. *
  101. * @ingroup source
  102. */
  103. interface SimplenewsSourceNodeInterface extends SimplenewsSourceInterface {
  104. /**
  105. * Create a source based on a node and subscriber.
  106. */
  107. function __construct($node, $subscriber);
  108. /**
  109. * Returns the actually used node of this source.
  110. */
  111. function getNode();
  112. /**
  113. * Returns the subscriber object.
  114. */
  115. function getSubscriber();
  116. }
  117. /**
  118. * Interface for a simplenews source cache implementation.
  119. *
  120. * This is only compatible with the SimplenewsSourceNodeInterface interface.
  121. *
  122. * @ingroup source
  123. */
  124. interface SimplenewsSourceCacheInterface {
  125. /**
  126. * Create a new instance, allows to initialize based on the used
  127. * source.
  128. */
  129. function __construct(SimplenewsSourceNodeInterface $source);
  130. /**
  131. * Return a cached element, if existing.
  132. *
  133. * Although group and key can be used to identify the requested cache, the
  134. * implementations are responsible to create a unique cache key themself using
  135. * the $source. For example based on the node id and the language.
  136. *
  137. * @param $group
  138. * Group of the cache key, which allows cache implementations to decide what
  139. * they want to cache. Currently used groups:
  140. * - data: Raw data, e.g. attachments.
  141. * - build: Built and themed content, before personalizations like tokens.
  142. * - final: The final returned data. Caching this means that newsletter
  143. * can not be personalized anymore.
  144. * @param $key
  145. * Identifies the requested element, e.g. body, footer or attachments.
  146. */
  147. function get($group, $key);
  148. /**
  149. * Write an element to the cache.
  150. *
  151. * Although group and key can be used to identify the requested cache, the
  152. * implementations are responsible to create a unique cache key themself using
  153. * the $source. For example based on the node id and the language.
  154. *
  155. * @param $group
  156. * Group of the cache key, which allows cache implementations to decide what
  157. * they want to cache. Currently used groups:
  158. * - data: Raw data, e.g. attachments.
  159. * - build: Built and themed content, before personalizations like tokens.
  160. * - final: The final returned data. Caching this means that newsletter
  161. * can not be personalized anymore.
  162. * @param $key
  163. * Identifies the requested element, e.g. body, footer or attachments.
  164. * @param $data
  165. * The data to be saved in the cache.
  166. */
  167. function set($group, $key, $data);
  168. }
  169. /**
  170. * A Simplenews spool implementation is a factory for Simplenews sources.
  171. *
  172. * Their main functionility is to return a number of sources based on the passed
  173. * in array of mail spool rows. Additionally, it needs to return the processed
  174. * mail rows after a source was sent.
  175. *
  176. * @todo: Move spool functions into this interface.
  177. *
  178. * @ingroup spool
  179. */
  180. interface SimplenewsSpoolInterface {
  181. /**
  182. * Initalizes the spool implementation.
  183. *
  184. * @param $spool_list
  185. * An array of rows from the {simplenews_mail_spool} table.
  186. */
  187. function __construct($pool_list);
  188. /**
  189. * Returns a Simplenews source to be sent.
  190. *
  191. * A single source may represent any number of mail spool rows, e.g. by
  192. * addressing them as BCC.
  193. */
  194. function nextSource();
  195. /**
  196. * Returns the processed mail spool rows, keyed by the msid.
  197. *
  198. * Only rows that were processed while preparing the previously returned
  199. * source must be returned.
  200. *
  201. * @return
  202. * An array of mail spool rows, keyed by the msid. Can optionally have set
  203. * the following additional properties.
  204. * - actual_nid: In case of content translation, the source node that was
  205. * used for this mail.
  206. * - error: FALSE if the prepration for this row failed. For example set
  207. * when the corresponding node failed to load.
  208. * - status: A simplenews spool status to indicate the status.
  209. */
  210. function getProcessed();
  211. }
  212. /**
  213. * Simplenews Spool implementation.
  214. *
  215. * @ingroup spool
  216. */
  217. class SimplenewsSpool implements SimplenewsSpoolInterface {
  218. /**
  219. * Array with mail spool rows being processed.
  220. *
  221. * @var array
  222. */
  223. protected $spool_list;
  224. /**
  225. * Array of the processed mail spool rows.
  226. */
  227. protected $processed = array();
  228. /**
  229. * Implements SimplenewsSpoolInterface::_construct($spool_list);
  230. */
  231. public function __construct($spool_list) {
  232. $this->spool_list = $spool_list;
  233. }
  234. /**
  235. * Implements SimplenewsSpoolInterface::nextSource();
  236. */
  237. public function nextSource() {
  238. // Get the current mail spool row and update the internal pointer to the
  239. // next row.
  240. $return = each($this->spool_list);
  241. // If we're done, return false.
  242. if (!$return) {
  243. return FALSE;
  244. }
  245. $spool_data = $return['value'];
  246. // Store this spool row as processed.
  247. $this->processed[$spool_data->msid] = $spool_data;
  248. $node = node_load($spool_data->nid);
  249. if (!$node) {
  250. // If node the load failed, set the processed status done and proceed with
  251. // the next mail.
  252. $this->processed[$spool_data->msid]->result = array(
  253. 'status' => SIMPLENEWS_SPOOL_DONE,
  254. 'error' => TRUE
  255. );
  256. return $this->nextSource();
  257. }
  258. if ($spool_data->data) {
  259. $subscriber = $spool_data->data;
  260. }
  261. else {
  262. $subscriber = simplenews_subscriber_load_by_mail($spool_data->mail);
  263. }
  264. if (!$subscriber) {
  265. // If loading the subscriber failed, set the processed status done and
  266. // proceed with the next mail.
  267. $this->processed[$spool_data->msid]->result = array(
  268. 'status' => SIMPLENEWS_SPOOL_DONE,
  269. 'error' => TRUE
  270. );
  271. return $this->nextSource();
  272. }
  273. $source_class = $this->getSourceImplementation($spool_data);
  274. $source = new $source_class($node, $subscriber);
  275. // Set which node is actually used. In case of a translation set, this might
  276. // not be the same node.
  277. $this->processed[$spool_data->msid]->actual_nid = $source->getNode()->nid;
  278. return $source;
  279. }
  280. /**
  281. * Implements SimplenewsSpoolInterface::getProcessed();
  282. */
  283. function getProcessed() {
  284. $processed = $this->processed;
  285. $this->processed = array();
  286. return $processed;
  287. }
  288. /**
  289. * Return the Simplenews source implementation for the given mail spool row.
  290. */
  291. protected function getSourceImplementation($spool_data) {
  292. return variable_get('simplenews_source', 'SimplenewsSourceNode');
  293. }
  294. }
  295. /**
  296. * Simplenews source implementation based on nodes for a single subscriber.
  297. *
  298. * @ingroup source
  299. */
  300. class SimplenewsSourceNode implements SimplenewsSourceNodeInterface {
  301. /**
  302. * The node object.
  303. */
  304. protected $node;
  305. /**
  306. * The cached build render array.
  307. */
  308. protected $build;
  309. /**
  310. * The newsletter category.
  311. */
  312. protected $category;
  313. /**
  314. * The subscriber and therefore recipient of this mail.
  315. */
  316. protected $subscriber;
  317. /**
  318. * The mail key used for drupal_mail().
  319. */
  320. protected $key = 'test';
  321. /**
  322. * The simplenews newsletter.
  323. */
  324. protected $newsletter;
  325. /**
  326. * Cache implementation used for this source.
  327. *
  328. * @var SimplenewsSourceCacheInterface
  329. */
  330. protected $cache;
  331. /**
  332. * Implements SimplenewsSourceInterface::_construct();
  333. */
  334. public function __construct($node, $subscriber) {
  335. $this->setSubscriber($subscriber);
  336. $this->setNode($node);
  337. $this->newsletter = simplenews_newsletter_load($node->nid);
  338. $this->category = simplenews_category_load($this->newsletter->tid);
  339. $this->initCache();
  340. }
  341. /**
  342. * Set the node of this source.
  343. *
  344. * If the node is part of a translation set, switch to the node for the
  345. * requested language, if existent.
  346. */
  347. public function setNode($node) {
  348. $langcode = $this->getLanguage();
  349. $nid = $node->nid;
  350. if (module_exists('translation')) {
  351. // If the node has translations and a translation is required
  352. // the equivalent of the node in the required language is used
  353. // or the base node (nid == tnid) is used.
  354. if ($tnid = $node->tnid) {
  355. if ($langcode != $node->language) {
  356. $translations = translation_node_get_translations($tnid);
  357. // A translation is available in the preferred language.
  358. if ($translation = $translations[$langcode]) {
  359. $nid = $translation->nid;
  360. $langcode = $translation->language;
  361. }
  362. else {
  363. // No translation found which matches the preferred language.
  364. foreach ($translations as $translation) {
  365. if ($translation->nid == $tnid) {
  366. $nid = $tnid;
  367. $langcode = $translation->language;
  368. break;
  369. }
  370. }
  371. }
  372. }
  373. }
  374. }
  375. // If a translation of the node is used, load this node.
  376. if ($nid != $node->nid) {
  377. $this->node = node_load($nid);
  378. }
  379. else {
  380. $this->node = $node;
  381. }
  382. }
  383. /**
  384. * Initialize the cache implementation.
  385. */
  386. protected function initCache() {
  387. $class = variable_get('simplenews_source_cache', 'SimplenewsSourceCacheBuild');
  388. $this->cache = new $class($this);
  389. }
  390. /**
  391. * Returns the corresponding category.
  392. */
  393. public function getCategory() {
  394. return $this->category;
  395. }
  396. /**
  397. * Set the active subscriber.
  398. */
  399. public function setSubscriber($subscriber) {
  400. $this->subscriber = $subscriber;
  401. }
  402. /**
  403. * Return the subscriber object.
  404. */
  405. public function getSubscriber() {
  406. return $this->subscriber;
  407. }
  408. /**
  409. * Implements SimplenewsSourceInterface::getHeaders().
  410. */
  411. public function getHeaders(array $headers) {
  412. // If receipt is requested, add headers.
  413. if ($this->category->receipt) {
  414. $headers['Disposition-Notification-To'] = $this->getFromAddress();
  415. $headers['X-Confirm-Reading-To'] = $this->getFromAddress();
  416. }
  417. // Add priority if set.
  418. switch ($this->category->priority) {
  419. case SIMPLENEWS_PRIORITY_HIGHEST:
  420. $headers['Priority'] = 'High';
  421. $headers['X-Priority'] = '1';
  422. $headers['X-MSMail-Priority'] = 'Highest';
  423. break;
  424. case SIMPLENEWS_PRIORITY_HIGH:
  425. $headers['Priority'] = 'urgent';
  426. $headers['X-Priority'] = '2';
  427. $headers['X-MSMail-Priority'] = 'High';
  428. break;
  429. case SIMPLENEWS_PRIORITY_NORMAL:
  430. $headers['Priority'] = 'normal';
  431. $headers['X-Priority'] = '3';
  432. $headers['X-MSMail-Priority'] = 'Normal';
  433. break;
  434. case SIMPLENEWS_PRIORITY_LOW:
  435. $headers['Priority'] = 'non-urgent';
  436. $headers['X-Priority'] = '4';
  437. $headers['X-MSMail-Priority'] = 'Low';
  438. break;
  439. case SIMPLENEWS_PRIORITY_LOWEST:
  440. $headers['Priority'] = 'non-urgent';
  441. $headers['X-Priority'] = '5';
  442. $headers['X-MSMail-Priority'] = 'Lowest';
  443. break;
  444. }
  445. // Add user specific header data.
  446. $headers['From'] = $this->getFromFormatted();
  447. $headers['List-Unsubscribe'] = '<' . token_replace('[simplenews-subscriber:unsubscribe-url]', $this->getTokenContext(), array('sanitize' => FALSE)) . '>';
  448. // Add general headers
  449. $headers['Precedence'] = 'bulk';
  450. return $headers;
  451. }
  452. /**
  453. * Implements SimplenewsSourceInterface::getTokenContext().
  454. */
  455. function getTokenContext() {
  456. return array(
  457. 'category' => $this->getCategory(),
  458. 'simplenews_subscriber' => $this->getSubscriber(),
  459. 'node' => $this->getNode(),
  460. );
  461. }
  462. /**
  463. * Set the mail key.
  464. */
  465. function setKey($key) {
  466. $this->key = $key;
  467. }
  468. /**
  469. * Implements SimplenewsSourceInterface::getKey().
  470. */
  471. function getKey() {
  472. return $this->key;
  473. }
  474. /**
  475. * Implements SimplenewsSourceInterface::getFromFormatted().
  476. */
  477. function getFromFormatted() {
  478. // Windows based PHP systems don't accept formatted email addresses.
  479. if (drupal_substr(PHP_OS, 0, 3) == 'WIN') {
  480. return $this->getFromAddress();
  481. }
  482. return '"' . addslashes(mime_header_encode($this->getCategory()->from_name)) . '" <' . $this->getFromAddress() . '>';
  483. }
  484. /**
  485. * Implements SimplenewsSourceInterface::getFromAddress().
  486. */
  487. function getFromAddress() {
  488. return $this->getCategory()->from_address;
  489. }
  490. /**
  491. * Implements SimplenewsSourceInterface::getRecipient().
  492. */
  493. function getRecipient() {
  494. return $this->getSubscriber()->mail;
  495. }
  496. /**
  497. * Implements SimplenewsSourceInterface::getFormat().
  498. */
  499. function getFormat() {
  500. return $this->getCategory()->format;
  501. }
  502. /**
  503. * Implements SimplenewsSourceInterface::getLanguage().
  504. */
  505. function getLanguage() {
  506. return $this->getSubscriber()->language;
  507. }
  508. /**
  509. * Implements SimplenewsSourceSpoolInterface::getNode().
  510. */
  511. function getNode() {
  512. return $this->node;
  513. }
  514. /**
  515. * Implements SimplenewsSourceInterface::getSubject().
  516. */
  517. function getSubject() {
  518. // Build email subject and perform some sanitizing.
  519. $langcode = $this->getLanguage();
  520. $language_list = language_list();
  521. // Use the requested language if enabled.
  522. $language = isset($language_list[$langcode]) ? $language_list[$langcode] : NULL;
  523. $subject = token_replace($this->getCategory()->email_subject, $this->getTokenContext(), array('sanitize' => FALSE, 'language' => $language));
  524. // Line breaks are removed from the email subject to prevent injection of
  525. // malicious data into the email header.
  526. $subject = str_replace(array("\r", "\n"), '', $subject);
  527. return $subject;
  528. }
  529. /**
  530. * Set up the necessary language and user context.
  531. */
  532. protected function setContext() {
  533. // Switch to the user
  534. if ($this->uid = $this->getSubscriber()->uid) {
  535. simplenews_impersonate_user($this->uid);
  536. }
  537. // Change language if the requested language is enabled.
  538. $language = $this->getLanguage();
  539. $languages = language_list();
  540. if (isset($languages[$language])) {
  541. $this->original_language = $GLOBALS['language'];
  542. $GLOBALS['language'] = $languages[$language];
  543. $GLOBALS['language_url'] = $languages[$language];
  544. // Overwrites the current content language for i18n_select.
  545. if (module_exists('i18n_select')) {
  546. $GLOBALS['language_content'] = $languages[$language];
  547. }
  548. }
  549. }
  550. /**
  551. * Reset the context.
  552. */
  553. protected function resetContext() {
  554. // Switch back to the previous user.
  555. if ($this->uid) {
  556. simplenews_revert_user();
  557. }
  558. // Switch language back.
  559. if (!empty($this->original_language)) {
  560. $GLOBALS['language'] = $this->original_language;
  561. $GLOBALS['language_url'] = $this->original_language;
  562. if (module_exists('i18n_select')) {
  563. $GLOBALS['language_content'] = $this->original_language;
  564. }
  565. }
  566. }
  567. /**
  568. * Build the node object.
  569. *
  570. * The resulting build array is cached as it is used in multiple places.
  571. * @param $format
  572. * (Optional) Override the default format. Defaults to getFormat().
  573. */
  574. protected function build($format = NULL) {
  575. if (empty($format)) {
  576. $format = $this->getFormat();
  577. }
  578. if (!empty($this->build[$format])) {
  579. return $this->build[$format];
  580. }
  581. // Build message body
  582. // Supported view modes: 'email_plain', 'email_html', 'email_textalt'
  583. $build = node_view($this->node, 'email_' . $format);
  584. unset($build['#theme']);
  585. foreach (field_info_instances('node', $this->node->type) as $field_name => $field) {
  586. if (isset($build[$field_name])) {
  587. $build[$field_name]['#theme'] = 'simplenews_field';
  588. }
  589. }
  590. $this->build[$format] = $build;
  591. return $this->build[$format];
  592. }
  593. /**
  594. * Build the themed newsletter body.
  595. *
  596. * @param $format
  597. * (Optional) Override the default format. Defaults to getFormat().
  598. */
  599. protected function buildBody($format = NULL) {
  600. if (empty($format)) {
  601. $format = $this->getFormat();
  602. }
  603. if ($cache = $this->cache->get('build', 'body:' . $format)) {
  604. return $cache;
  605. }
  606. $body = theme('simplenews_newsletter_body', array('build' => $this->build($format), 'category' => $this->getCategory(), 'language' => $this->getLanguage(), 'simplenews_subscriber' => $this->getSubscriber()));
  607. $this->cache->set('build', 'body:' . $format, $body);
  608. return $body;
  609. }
  610. /**
  611. * Implements SimplenewsSourceInterface::getBody().
  612. */
  613. public function getBody() {
  614. return $this->getBodyWithFormat($this->getFormat());
  615. }
  616. /**
  617. * Implements SimplenewsSourceInterface::getBody().
  618. */
  619. public function getPlainBody() {
  620. return $this->getBodyWithFormat('plain');
  621. }
  622. /**
  623. * Get the body with the requested format.
  624. *
  625. * @param $format
  626. * Either html or plain.
  627. *
  628. * @return
  629. * The rendered mail body as a string.
  630. */
  631. protected function getBodyWithFormat($format) {
  632. // Switch to correct user and language context.
  633. $this->setContext();
  634. if ($cache = $this->cache->get('final', 'body:' . $format)) {
  635. return $cache;
  636. }
  637. $body = $this->buildBody($format);
  638. // Build message body, replace tokens.
  639. $body = token_replace($body, $this->getTokenContext(), array('sanitize' => FALSE));
  640. if ($format == 'plain') {
  641. // Convert HTML to text if requested to do so.
  642. $body = simplenews_html_to_text($body, $this->getCategory()->hyperlinks);
  643. }
  644. $this->cache->set('final', 'body:' . $format, $body);
  645. $this->resetContext();
  646. return $body;
  647. }
  648. /**
  649. * Builds the themed footer.
  650. *
  651. * @param $format
  652. * (Optional) Set the format of this footer build, overrides the default
  653. * format.
  654. */
  655. protected function buildFooter($format = NULL) {
  656. if (empty($format)) {
  657. $format = $this->getFormat();
  658. }
  659. if ($cache = $this->cache->get('build', 'footer:' . $format)) {
  660. return $cache;
  661. }
  662. // Build and buffer message footer
  663. $footer = theme('simplenews_newsletter_footer', array(
  664. 'build' => $this->build($format),
  665. 'category' => $this->getCategory(),
  666. 'context' => $this->getTokenContext(),
  667. 'key' => $this->getKey(),
  668. 'language' => $this->getLanguage(),
  669. 'format' => $format,
  670. ));
  671. $this->cache->set('build', 'footer:' . $format, $footer);
  672. return $footer;
  673. }
  674. /**
  675. * Implements SimplenewsSourceInterface::getFooter().
  676. */
  677. public function getFooter() {
  678. return $this->getFooterWithFormat($this->getFormat());
  679. }
  680. /**
  681. * Implements SimplenewsSourceInterface::getPlainFooter().
  682. */
  683. public function getPlainFooter() {
  684. return $this->getFooterWithFormat('plain');
  685. }
  686. /**
  687. * Get the footer in the specified format.
  688. *
  689. * @param $format
  690. * Either html or plain.
  691. *
  692. * @return
  693. * The footer for the requested format.
  694. */
  695. protected function getFooterWithFormat($format) {
  696. // Switch to correct user and language context.
  697. $this->setContext();
  698. if ($cache = $this->cache->get('final', 'footer:' . $format)) {
  699. return $cache;
  700. }
  701. $final_footer = token_replace($this->buildFooter($format), $this->getTokenContext(), array('sanitize' => FALSE));
  702. $this->cache->set('final', 'footer:' . $format, $final_footer);
  703. $this->resetContext();
  704. return $final_footer;
  705. }
  706. /**
  707. * Implements SimplenewsSourceInterface::getAttachments().
  708. */
  709. function getAttachments() {
  710. if ($cache = $this->cache->get('data', 'attachments')) {
  711. return $cache;
  712. }
  713. $attachments = array();
  714. $build = $this->build();
  715. $fids = array();
  716. foreach (field_info_instances('node', $this->node->type) as $field_name => $field_instance) {
  717. // @todo: Find a better way to support more field types.
  718. // Only add fields of type file which are enabled for the current view
  719. // mode as attachments.
  720. $field = field_info_field($field_name);
  721. if ($field['type'] == 'file' && isset($build[$field_name])) {
  722. if ($items = field_get_items('node', $this->node, $field_name)) {
  723. foreach ($items as $item) {
  724. $fids[] = $item['fid'];
  725. }
  726. }
  727. }
  728. }
  729. if (!empty($fids)) {
  730. $attachments = file_load_multiple($fids);
  731. }
  732. $this->cache->set('data', 'attachments', $attachments);
  733. return $attachments;
  734. }
  735. }
  736. /**
  737. * Abstract implementation of the source caching that does static caching.
  738. *
  739. * Subclasses need to implement the abstract function isCacheable() to decide
  740. * what should be cached.
  741. *
  742. * @ingroup source
  743. */
  744. abstract class SimplenewsSourceCacheStatic implements SimplenewsSourceCacheInterface {
  745. /**
  746. * The simplenews source for which this cache is used.
  747. *
  748. * @var SimplenewsSourceNodeInterface
  749. */
  750. protected $source;
  751. /**
  752. * The cache identifier for the given source.
  753. */
  754. protected $cid;
  755. /**
  756. * The static cache.
  757. */
  758. protected static $cache = array();
  759. /**
  760. * Implements SimplenewsSourceNodeInterface::__construct().
  761. */
  762. public function __construct(SimplenewsSourceNodeInterface $source) {
  763. $this->source = $source;
  764. self::$cache = &drupal_static(__CLASS__, array());
  765. }
  766. /**
  767. * Returns the cache identifier for the current source.
  768. */
  769. protected function getCid() {
  770. if (empty($this->cid)) {
  771. $this->cid = $this->source->getNode()->nid . ':' . $this->source->getLanguage();
  772. }
  773. return $this->cid;
  774. }
  775. /**
  776. * Implements SimplenewsSourceNodeInterface::get().
  777. */
  778. public function get($group, $key) {
  779. if (!$this->isCacheable($group, $key)) {
  780. return;
  781. }
  782. if (isset(self::$cache[$this->getCid()][$group][$key])) {
  783. return self::$cache[$this->getCid()][$group][$key];
  784. }
  785. }
  786. /**
  787. * Implements SimplenewsSourceNodeInterface::set().
  788. */
  789. public function set($group, $key, $data) {
  790. if (!$this->isCacheable($group, $key)) {
  791. return;
  792. }
  793. self::$cache[$this->getCid()][$group][$key] = $data;
  794. }
  795. /**
  796. * Return if the requested element should be cached.
  797. *
  798. * @return
  799. * TRUE if it should be cached, FALSE otherwise.
  800. */
  801. abstract function isCacheable($group, $key);
  802. }
  803. /**
  804. * Cache implementation that does not cache anything at all.
  805. *
  806. * @ingroup source
  807. */
  808. class SimplenewsSourceCacheNone extends SimplenewsSourceCacheStatic {
  809. /**
  810. * Implements SimplenewsSourceCacheStatic::set().
  811. */
  812. public function isCacheable($group, $key) {
  813. return FALSE;
  814. }
  815. }
  816. /**
  817. * Source cache implementation that caches build and data element.
  818. *
  819. * @ingroup source
  820. */
  821. class SimplenewsSourceCacheBuild extends SimplenewsSourceCacheStatic {
  822. /**
  823. * Implements SimplenewsSourceCacheStatic::set().
  824. */
  825. function isCacheable($group, $key) {
  826. // Only cache for anon users.
  827. if (user_is_logged_in()) {
  828. return FALSE;
  829. }
  830. // Only cache data and build information.
  831. return in_array($group, array('data', 'build'));
  832. }
  833. }
  834. /**
  835. * Example source implementation used for tests.
  836. *
  837. * @ingroup source
  838. */
  839. class SimplenewsSourceTest implements SimplenewsSourceInterface {
  840. protected $format;
  841. public function __construct($format) {
  842. $this->format = $format;
  843. }
  844. public function getAttachments() {
  845. return array(
  846. array(
  847. 'uri' => 'example://test.png',
  848. 'filemime' => 'x-example',
  849. 'filename' => 'test.png',
  850. ),
  851. );
  852. }
  853. public function getBody() {
  854. return $this->getFormat() == 'plain' ? $this->getPlainBody() : 'the body';
  855. }
  856. public function getFooter() {
  857. return $this->getFormat() == 'plain' ? $this->getPlainFooter() : 'the footer';
  858. }
  859. public function getPlainFooter() {
  860. return 'the plain footer';
  861. }
  862. public function getFormat() {
  863. return $this->format;
  864. }
  865. public function getFromAddress() {
  866. return 'test@example.org';
  867. }
  868. public function getFromFormatted() {
  869. return 'Test <test@example.org>';
  870. }
  871. public function getHeaders(array $headers) {
  872. $headers['X-Simplenews-Test'] = 'OK';
  873. return $headers;
  874. }
  875. public function getKey() {
  876. return 'node';
  877. }
  878. public function getLanguage() {
  879. return 'en';
  880. }
  881. public function getPlainBody() {
  882. return 'the plain body';
  883. }
  884. public function getRecipient() {
  885. return 'recipient@example.org';
  886. }
  887. public function getSubject() {
  888. return 'the subject';
  889. }
  890. public function getTokenContext() {
  891. return array();
  892. }
  893. }