1 | <?php |
---|
2 | |
---|
3 | if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!'); |
---|
4 | |
---|
5 | $service = &$arr[0]; |
---|
6 | $service->addMethod('pwg.piwecard.previewEmail', 'piwecard_ws_previewEmail', |
---|
7 | array( |
---|
8 | 'format_message'=>array(), |
---|
9 | 'message'=>array(), |
---|
10 | ), |
---|
11 | 'Preview the email' |
---|
12 | ); |
---|
13 | |
---|
14 | /** |
---|
15 | * Function called by Javascript |
---|
16 | * @param Array Parameters passed through Javascript |
---|
17 | * @param |
---|
18 | * @return String the preview of the email |
---|
19 | */ |
---|
20 | function piwecard_ws_previewEmail($params, $service) { |
---|
21 | $format_message = $params['format_message']; |
---|
22 | $message = $params['message']; |
---|
23 | |
---|
24 | return previewEmail($format_message, $message); |
---|
25 | } |
---|
26 | |
---|
27 | /** |
---|
28 | * Construct the preview of the email |
---|
29 | * @param String Email format (text or html) |
---|
30 | * @param String the email message |
---|
31 | * @return String the preview of the email |
---|
32 | */ |
---|
33 | function previewEmail($format_message, $message) { |
---|
34 | $piwecard = new Piwecard(); |
---|
35 | |
---|
36 | $template_mail = new Template(PIWECARD_MAIL_PATH.'template'); |
---|
37 | $smarty = $template_mail->smarty; |
---|
38 | |
---|
39 | if ($format_message == 'text') { |
---|
40 | $message_output = nl2br($piwecard->get_text_message(stripslashes(parse($message)), $smarty)); |
---|
41 | $style = ''; |
---|
42 | } elseif ($format_message == 'html') { |
---|
43 | $output = $piwecard->get_html_message(stripslashes(parse($message)), $smarty); |
---|
44 | $dom_document = new DOMDocument(); |
---|
45 | $dom_document->loadHTML($output); |
---|
46 | $styles = piwecard_create_style_array($dom_document->getElementsByTagName('style')->item(0)->nodeValue); |
---|
47 | $message_output = $dom_document->getElementsByTagName('body')->item(0); |
---|
48 | $email_body = $dom_document->createElement('div'); |
---|
49 | $email_body->setAttribute('id', 'email_body'); |
---|
50 | $message_output->parentNode->insertBefore($email_body, $message_output); |
---|
51 | $email_body->appendChild($message_output); |
---|
52 | foreach ($styles as $id => $style) { |
---|
53 | $dom_document->getElementById(str_replace('#', '', $id))->setAttribute("style", $style); |
---|
54 | } |
---|
55 | $message_output = $dom_document->saveHTML(); |
---|
56 | } |
---|
57 | |
---|
58 | return array('message' => $message_output); |
---|
59 | } |
---|
60 | |
---|
61 | /** |
---|
62 | * Get style from the email template |
---|
63 | * @param String The value of the node "Style" |
---|
64 | * @return Array the styles of the email |
---|
65 | */ |
---|
66 | function piwecard_create_style_array($style) { |
---|
67 | $style = str_replace(array("\n", "\t"), array('', ''), $style); |
---|
68 | $tok = strtok($style, "{}"); |
---|
69 | |
---|
70 | $sarray = array(); |
---|
71 | while ($tok !== false) { |
---|
72 | array_push($sarray, trim($tok)); |
---|
73 | $tok = strtok("{}"); |
---|
74 | } |
---|
75 | |
---|
76 | $styles = array(); |
---|
77 | |
---|
78 | for($i = 0; $i<count($sarray)-2; $i+=2){ |
---|
79 | $styles[$sarray[$i]] = $sarray[$i+1]; |
---|
80 | } |
---|
81 | |
---|
82 | return $styles; |
---|
83 | } |
---|
84 | |
---|
85 | /** |
---|
86 | * Parse the email message |
---|
87 | * @param String message to parse |
---|
88 | * @return String parsed message |
---|
89 | */ |
---|
90 | function parse($data) { |
---|
91 | global $conf; |
---|
92 | |
---|
93 | set_make_full_url(); |
---|
94 | $ecard_parse = array( |
---|
95 | '%yourname%' => 'Your Name', |
---|
96 | '%youremail%' => 'Your email', |
---|
97 | '%recipientname%' => 'Recipient name', |
---|
98 | '%recipientemail%' => 'Recipient email', |
---|
99 | '%website%' => isset($conf['gallery_title']) ? $conf['gallery_title'] : '' , |
---|
100 | '%websiteurl%' => get_absolute_root_url() , |
---|
101 | '%ecardurl%' => '', |
---|
102 | '%ecardtitle%' => 'Ecard title', |
---|
103 | '%ecardmessage%' => 'The message of the ecard', |
---|
104 | '%pictureurl%' => get_root_url().PIWECARD_PATH.'admin/images/image_thumb.jpg', |
---|
105 | '%pictureinfos%' => '<small>Image infos</small>', |
---|
106 | ); |
---|
107 | unset_make_full_url(); |
---|
108 | |
---|
109 | $patterns = array(); |
---|
110 | $replacements = array(); |
---|
111 | foreach ($ecard_parse as $key => $value) { |
---|
112 | array_push($patterns, $key); |
---|
113 | array_push($replacements, $value); |
---|
114 | } |
---|
115 | |
---|
116 | return str_replace($patterns, $replacements, $data); |
---|
117 | } |
---|