-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathextension.driver.php
287 lines (214 loc) · 11 KB
/
extension.driver.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<?php
require_once(dirname(__FILE__) . '/lib/class.email.php');
Class extension_SMTP_Email_Library extends Extension{
const HTACCESS_PEAR_INCLUDE = "## EMAIL EXTENSION PEAR LIBRARY\nphp_value include_path \"extensions/smtp_email_library/lib/pear:.\"";
public function about(){
return array('name' => 'SMTP Email Library',
'version' => '1.1',
'release-date' => '2010-02-12',
'author' => array('name' => 'Alistair Kearney',
'website' => 'http://www.pointybeard.com',
'email' => 'alistair@symphony-cms.com')
);
}
public function getSubscribedDelegates(){
return array(
array(
'page' => '/system/preferences/',
'delegate' => 'AddCustomPreferenceFieldsets',
'callback' => 'cbAppendPreferences'
),
array(
'page' => '/system/preferences/',
'delegate' => 'Save',
'callback' => 'cbSavePreferences'
),
array(
'page' => '/blueprints/events/new/',
'delegate' => 'AppendEventFilter',
'callback' => 'cbAddFilterToEventEditor'
),
array(
'page' => '/blueprints/events/edit/',
'delegate' => 'AppendEventFilter',
'callback' => 'cbAddFilterToEventEditor'
),
array(
'page' => '/blueprints/events/new/',
'delegate' => 'AppendEventFilterDocumentation',
'callback' => 'cbAppendEventFilterDocumentation'
),
array(
'page' => '/blueprints/events/edit/',
'delegate' => 'AppendEventFilterDocumentation',
'callback' => 'cbAppendEventFilterDocumentation'
),
array(
'page' => '/frontend/',
'delegate' => 'EventPostSaveFilter',
'callback' => 'cbSendEmailSMTPFilter'
),
);
}
public function uninstall(){
self::__update_htaccess(true);
Symphony::Configuration()->remove('smtp_email_library');
$this->_Parent->saveConfig();
}
public function cbAppendPreferences($context){
$group = new XMLElement('fieldset');
$group->setAttribute('class', 'settings');
$group->appendChild(new XMLElement('legend', __('SMTP Email Library')));
$div = new XMLElement('div');
$div->setAttribute('class', 'group');
$label = Widget::Label(__('Host'));
$label->appendChild(Widget::Input('settings[smtp_email_library][host]', Symphony::Configuration()->get('host', 'smtp_email_library')));
$div->appendChild($label);
$label = Widget::Label(__('Port'));
$label->appendChild(Widget::Input('settings[smtp_email_library][port]', Symphony::Configuration()->get('port', 'smtp_email_library')));
$div->appendChild($label);
$group->appendChild($div);
$label = Widget::Label();
$input = Widget::Input('settings[smtp_email_library][auth]', '1', 'checkbox');
if(Symphony::Configuration()->get('auth', 'smtp_email_library') == '1') $input->setAttribute('checked', 'checked');
$label->setValue($input->generate() . ' Requires authentication');
$group->appendChild($label);
$group->appendChild(new XMLElement('p', 'Some SMTP connections require authentication. If that is the case, enter the username/password combination below.', array('class' => 'help')));
$div = new XMLElement('div');
$div->setAttribute('class', 'group');
$label = Widget::Label(__('Username'));
$label->appendChild(Widget::Input('settings[smtp_email_library][username]', Symphony::Configuration()->get('username', 'smtp_email_library')));
$div->appendChild($label);
$label = Widget::Label(__('Password'));
$label->appendChild(Widget::Input('settings[smtp_email_library][password]', Symphony::Configuration()->get('password', 'smtp_email_library')));
$div->appendChild($label);
$group->appendChild($div);
$context['wrapper']->appendChild($group);
}
public function cbSavePreferences($context){
if(!isset($context['settings']['smtp_email_library']['auth'])) $context['settings']['smtp_email_library']['auth'] = '0';
}
public function enable(){
return self::__update_htaccess();
}
public function disable(){
return self::__update_htaccess(true);
}
public function install(){
return self::__update_htaccess();
}
private static function __update_htaccess($removing=false){
$htaccess = @file_get_contents(DOCROOT . '/.htaccess');
if($htaccess === false) return false;
## Remove existing rules
$htaccess = str_replace(self::HTACCESS_PEAR_INCLUDE, NULL, $htaccess);
if($removing == false){
$htaccess = preg_replace(
'/### Symphony 2\.0\.x ###\n*/i',
"### Symphony 2.0.x ###\n\n" . self::HTACCESS_PEAR_INCLUDE . "\n\n",
$htaccess
);
}
else{
//clean up the extra new line characters
$htaccess = preg_replace(
'/### Symphony 2\.0\.x ###\n*/i',
"### Symphony 2.0.x ###\n",
$htaccess
);
}
return @file_put_contents(DOCROOT . '/.htaccess', $htaccess);
}
private function __sendEmailFindFormValue($needle, $haystack, $discard_field_name=true, $default=NULL, $collapse=true){
if(preg_match('/^(fields\[[^\]]+\],?)+$/i', $needle)){
$parts = preg_split('/\,/i', $needle, -1, PREG_SPLIT_NO_EMPTY);
$parts = array_map('trim', $parts);
$stack = array();
foreach($parts as $p){
$field = str_replace(array('fields[', ']'), '', $p);
($discard_field_name ? $stack[] = $haystack[$field] : $stack[$field] = $haystack[$field]);
}
if(is_array($stack) && !empty($stack)) return ($collapse ? implode(' ', $stack) : $stack);
else $needle = NULL;
}
$needle = trim($needle);
if(empty($needle)) return $default;
return $needle;
}
public function cbAppendEventFilterDocumentation(array $context=array()){
if(!@in_array('smtp-email-library-send-email-filter', $context['selected'])) return;
$context['documentation'][] = new XMLElement('h3', __('Send Email via Direct SMTP Connection'));
$context['documentation'][] = new XMLElement('p', __('The send email filter, upon the event successfully saving the entry, takes input from the form and send an email to the desired recipient. <b>This filter currently does not work with the "Allow Multiple" option.</b> The following are the recognised fields:'));
$context['documentation'][] = contentBlueprintsEvents::processDocumentationCode(
'send-email[sender-email] // '.__('Optional').self::CRLF.
'send-email[sender-name] // '.__('Optional').self::CRLF.
'send-email[subject] // '.__('Optional').self::CRLF.
'send-email[body]'.self::CRLF.
'send-email[recipient] // '.__('comma separated list of author usernames.'));
$context['documentation'][] = new XMLElement('p', __('All of these fields can be set dynamically using the exact field name of another field in the form as shown below in the example form:'));
$context['documentation'][] = contentBlueprintsEvents::processDocumentationCode('<form action="" method="post">
<fieldset>
<label>'.__('Name').' <input type="text" name="fields[author]" value="" /></label>
<label>'.__('Email').' <input type="text" name="fields[email]" value="" /></label>
<label>'.__('Message').' <textarea name="fields[message]" rows="5" cols="21"></textarea></label>
<input name="send-email[sender-email]" value="fields[email]" type="hidden" />
<input name="send-email[sender-name]" value="fields[author]" type="hidden" />
<input name="send-email[subject]" value="You are being contacted" type="hidden" />
<input name="send-email[body]" value="fields[message]" type="hidden" />
<input name="send-email[recipient]" value="fred" type="hidden" />
<input id="submit" type="submit" name="action[save-contact-form]" value="Send" />
</fieldset>
</form>');
}
public function cbSendEmailSMTPFilter(array $context=array()){
if(!@in_array('smtp-email-library-send-email-filter', $context['event']->eParamFILTERS)) return;
$fields = $_POST['send-email'];
$fields['recipient'] = $this->__sendEmailFindFormValue($fields['recipient'], $_POST['fields'], true);
$fields['recipient'] = preg_split('/\,/i', $fields['recipient'], -1, PREG_SPLIT_NO_EMPTY);
$fields['recipient'] = array_map('trim', $fields['recipient']);
$fields['recipient'] = Symphony::Database()->fetch("SELECT `email`, CONCAT(`first_name`, ' ', `last_name`) AS `name` FROM `tbl_authors` WHERE `username` IN ('".@implode("', '", $fields['recipient'])."') ");
$fields['subject'] = $this->__sendEmailFindFormValue($fields['subject'], $context['fields'], true, __('[Symphony] A new entry was created on %s', array(Symphony::Configuration()->get('sitename', 'general'))));
$fields['body'] = $this->__sendEmailFindFormValue($fields['body'], $context['fields'], false, NULL, false);
$fields['sender-email'] = $this->__sendEmailFindFormValue($fields['sender-email'], $context['fields'], true, 'noreply@' . parse_url(URL, PHP_URL_HOST));
$fields['sender-name'] = $this->__sendEmailFindFormValue($fields['sender-name'], $context['fields'], true, 'Symphony');
$fields['from'] = $this->__sendEmailFindFormValue($fields['from'], $context['fields'], true, $fields['sender-email']);
$section = Symphony::Database()->fetchRow(0, "SELECT * FROM `tbl_sections` WHERE `id` = ".$context['event']->getSource()." LIMIT 1");
$edit_link = URL.'/symphony/publish/'.$section['handle'].'/edit/'.$context['entry_id'].'/';
$body = __('Dear <!-- RECIPIENT NAME -->,') . General::CRLF . General::CRLF . __('This is a courtesy email to notify you that an entry was created on the %1$s section. You can edit the entry by going to: %2$s', array($section['name'], $edit_link)). General::CRLF . General::CRLF;
if(is_array($fields['body'])){
foreach($fields['body'] as $field_handle => $value){
$body .= "=== $field_handle ===" . General::CRLF . General::CRLF . $value . General::CRLF . General::CRLF;
}
}
else $body .= $fields['body'];
$errors = array();
if(!is_array($fields['recipient']) || empty($fields['recipient'])){
$context['messages'][] = array('smtp-email-library-send-email-filter', false, __('No valid recipients found. Check send-email[recipient] field.'));
}
else{
foreach($fields['recipient'] as $r){
$email = new LibraryEmail;
$email->to = vsprintf('%2$s <%1$s>', array_values($r));
$email->from = sprintf('%s <%s>', $fields['sender-name'], $fields['sender-email']);
$email->subject = $fields['subject'];
$email->message = str_replace('<!-- RECIPIENT NAME -->', $r['name'], $body);
$email->setHeader('Reply-To', $fields['from']);
try{
$email->send();
}
catch(Exception $e){
$errors[] = $email;
}
}
if(!empty($errors)){
$context['messages'][] = array('smtp-email-library-send-email-filter', false, 'The following email addresses were problematic: ' . General::sanitize(implode(', ', $errors)));
}
else $context['messages'][] = array('smtp-email-library-send-email-filter', true);
}
}
public function cbAddFilterToEventEditor($context){
$context['options'][] = array(
'smtp-email-library-send-email-filter', @in_array('smtp-email-library-send-email-filter', $context['selected']), 'Send Email via Direct SMTP Connection'
);
}
}