1 | <?php |
---|
2 | if (!defined('CONTACT_FORM_PATH')) die('Hacking attempt!'); |
---|
3 | |
---|
4 | /** |
---|
5 | * define page section from url |
---|
6 | */ |
---|
7 | function contact_form_section_init() |
---|
8 | { |
---|
9 | global $tokens, $page, $conf; |
---|
10 | |
---|
11 | if ($tokens[0] == 'contact') |
---|
12 | { |
---|
13 | $page['section'] = 'contact'; |
---|
14 | $page['title'] = '<a href="'.get_absolute_root_url().'">'.l10n('Home').'</a>'.$conf['level_separator'].'<a href="'.CONTACT_FORM_PUBLIC.'">'.l10n('Contact').'</a>'; |
---|
15 | } |
---|
16 | } |
---|
17 | |
---|
18 | /** |
---|
19 | * contact page |
---|
20 | */ |
---|
21 | function contact_form_page() |
---|
22 | { |
---|
23 | global $page; |
---|
24 | |
---|
25 | if (isset($page['section']) and $page['section'] == 'contact') |
---|
26 | { |
---|
27 | include(CONTACT_FORM_PATH . '/include/contact_form.inc.php'); |
---|
28 | } |
---|
29 | } |
---|
30 | |
---|
31 | /** |
---|
32 | * public menu link |
---|
33 | */ |
---|
34 | function contact_form_applymenu($menu_ref_arr) |
---|
35 | { |
---|
36 | global $conf; |
---|
37 | |
---|
38 | if ( !$conf['ContactForm']['cf_menu_link'] ) return; |
---|
39 | if ( !is_classic_user() and !$conf['ContactForm']['cf_allow_guest'] ) return; |
---|
40 | if ( !count(get_contact_emails()) ) return; |
---|
41 | |
---|
42 | $menu = &$menu_ref_arr[0]; |
---|
43 | if (($block = $menu->get_block('mbMenu')) != null) |
---|
44 | { |
---|
45 | array_push($block->data, array( |
---|
46 | 'URL' => CONTACT_FORM_PUBLIC, |
---|
47 | 'NAME' => l10n('Contact'), |
---|
48 | )); |
---|
49 | } |
---|
50 | } |
---|
51 | |
---|
52 | /** |
---|
53 | * admin plugins menu link |
---|
54 | */ |
---|
55 | function contact_form_admin_menu($menu) |
---|
56 | { |
---|
57 | array_push($menu, array( |
---|
58 | 'URL' => CONTACT_FORM_ADMIN, |
---|
59 | 'NAME' => 'Contact Form', |
---|
60 | )); |
---|
61 | return $menu; |
---|
62 | } |
---|
63 | |
---|
64 | /** |
---|
65 | * change contact on link on footer |
---|
66 | */ |
---|
67 | function contact_form_footer_link($content, &$smarty) |
---|
68 | { |
---|
69 | $search = '<a href="mailto:{$CONTACT_MAIL}?subject={\'A comment on your site\'|@translate|@escape:url}">'; |
---|
70 | $replace = '<a href="'.CONTACT_FORM_PUBLIC.'">'; |
---|
71 | return str_replace($search, $replace, $content); |
---|
72 | } |
---|
73 | |
---|
74 | /** |
---|
75 | * init emails list |
---|
76 | */ |
---|
77 | function contact_form_initialize_emails() |
---|
78 | { |
---|
79 | global $conf; |
---|
80 | |
---|
81 | $query = ' |
---|
82 | SELECT |
---|
83 | u.'.$conf['user_fields']['username'].' AS username, |
---|
84 | u.'.$conf['user_fields']['email'].' AS email |
---|
85 | FROM '.USERS_TABLE.' AS u |
---|
86 | JOIN '.USER_INFOS_TABLE.' AS i |
---|
87 | ON i.user_id = u.'.$conf['user_fields']['id'].' |
---|
88 | WHERE i.status in (\'webmaster\', \'admin\') |
---|
89 | AND '.$conf['user_fields']['email'].' IS NOT NULL |
---|
90 | ORDER BY username |
---|
91 | ;'; |
---|
92 | $result = pwg_query($query); |
---|
93 | |
---|
94 | $emails = array(); |
---|
95 | while ($row = pwg_db_fetch_assoc($result)) |
---|
96 | { |
---|
97 | array_push($emails, array( |
---|
98 | 'name' => $row['username'], |
---|
99 | 'email' => $row['email'], |
---|
100 | 'active' => true, |
---|
101 | )); |
---|
102 | } |
---|
103 | |
---|
104 | $conf['ContactForm']['cf_admin_mails'] = $emails; |
---|
105 | $conf['ContactForm']['cf_must_initialize'] = false; |
---|
106 | conf_update_param('ContactForm', serialize($conf['ContactForm'])); |
---|
107 | } |
---|
108 | |
---|
109 | |
---|
110 | /** |
---|
111 | * Send comment to subscribers |
---|
112 | */ |
---|
113 | function send_contact_form(&$comm, $key) |
---|
114 | { |
---|
115 | global $conf, $page, $template, $conf_mail; |
---|
116 | |
---|
117 | if (!isset($conf_mail)) |
---|
118 | { |
---|
119 | $conf_mail = get_mail_configuration(); |
---|
120 | } |
---|
121 | |
---|
122 | $comm = array_merge($comm, |
---|
123 | array( |
---|
124 | 'ip' => $_SERVER['REMOTE_ADDR'], |
---|
125 | 'agent' => $_SERVER['HTTP_USER_AGENT'] |
---|
126 | ) |
---|
127 | ); |
---|
128 | |
---|
129 | $comment_action='validate'; |
---|
130 | |
---|
131 | // check key |
---|
132 | if (!verify_ephemeral_key(@$key)) |
---|
133 | { |
---|
134 | $comment_action='reject'; |
---|
135 | } |
---|
136 | |
---|
137 | // check author |
---|
138 | if ( $conf['ContactForm']['cf_mandatory_name'] and empty($comm['author']) ) |
---|
139 | { |
---|
140 | array_push($page['errors'], l10n('Please enter a name')); |
---|
141 | $comment_action='reject'; |
---|
142 | } |
---|
143 | |
---|
144 | // check email |
---|
145 | if ( $conf['ContactForm']['cf_mandatory_mail'] and empty($comm['email']) ) |
---|
146 | { |
---|
147 | array_push($page['errors'], l10n('Please enter an e-mail')); |
---|
148 | $comment_action='reject'; |
---|
149 | } |
---|
150 | else if ( !empty($comm['email']) and !check_email_validity($comm['email']) ) |
---|
151 | { |
---|
152 | array_push($page['errors'], l10n('mail address must be like xxx@yyy.eee (example : jack@altern.org)')); |
---|
153 | $comment_action='reject'; |
---|
154 | } |
---|
155 | |
---|
156 | // check subject |
---|
157 | if (empty($comm['subject'])) |
---|
158 | { |
---|
159 | array_push($page['errors'], l10n('Please enter a subject')); |
---|
160 | $comment_action='reject'; |
---|
161 | } |
---|
162 | else if (strlen($comm['subject']) < 4) |
---|
163 | { |
---|
164 | array_push($page['errors'], sprintf(l10n('%s must not be less than %d characters long'), l10n('Subject'), 4)); |
---|
165 | $comment_action='reject'; |
---|
166 | } |
---|
167 | else if (strlen($comm['subject']) > 100) |
---|
168 | { |
---|
169 | array_push($page['errors'], sprintf(l10n('%s must not be more than %d characters long'), l10n('Subject'), 100)); |
---|
170 | $comment_action='reject'; |
---|
171 | } |
---|
172 | |
---|
173 | // check content |
---|
174 | if (empty($comm['content'])) |
---|
175 | { |
---|
176 | array_push($page['errors'], l10n('Please enter a message')); |
---|
177 | $comment_action='reject'; |
---|
178 | } |
---|
179 | else if (strlen($comm['content']) < 20) |
---|
180 | { |
---|
181 | array_push($page['errors'], sprintf(l10n('%s must not be less than %d characters long'), l10n('Message'), 20)); |
---|
182 | $comment_action='reject'; |
---|
183 | } |
---|
184 | else if (strlen($comm['subject']) > 2000) |
---|
185 | { |
---|
186 | array_push($page['errors'], sprintf(l10n('%s must not be more than %d characters long'), l10n('Message'), 2000)); |
---|
187 | $comment_action='reject'; |
---|
188 | } |
---|
189 | |
---|
190 | include_once(PHPWG_ROOT_PATH.'include/functions_comment.inc.php'); |
---|
191 | include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php'); |
---|
192 | |
---|
193 | add_event_handler('contact_form_check', 'user_comment_check', |
---|
194 | EVENT_HANDLER_PRIORITY_NEUTRAL, 2); |
---|
195 | |
---|
196 | // perform more spam check |
---|
197 | $comment_action = trigger_event('contact_form_check', $comment_action, $comm); |
---|
198 | |
---|
199 | // get admin emails |
---|
200 | $emails = get_contact_emails(); |
---|
201 | if (!count($emails)) |
---|
202 | { |
---|
203 | array_push($page['errors'], l10n('Error while sending e-mail')); |
---|
204 | $comment_action='reject'; |
---|
205 | } |
---|
206 | |
---|
207 | if ($comment_action == 'validate') |
---|
208 | { |
---|
209 | // format subject |
---|
210 | $prefix = str_replace('%gallery_title%', $conf['gallery_title'], $conf['ContactForm']['cf_subject_prefix']); |
---|
211 | $subject = '['.$prefix.'] '.$comm['subject']; |
---|
212 | $subject = trim(preg_replace('#[\n\r]+#s', '', $subject)); |
---|
213 | $subject = encode_mime_header($subject); |
---|
214 | |
---|
215 | // format content |
---|
216 | if ($conf['ContactForm']['cf_mail_type'] == 'text/html') |
---|
217 | { |
---|
218 | $comm['content'] = nl2br($comm['content']); |
---|
219 | } |
---|
220 | |
---|
221 | // format expeditor |
---|
222 | if (empty($comm['email'])) |
---|
223 | { |
---|
224 | $args['from'] = $conf_mail['formated_email_webmaster']; |
---|
225 | } |
---|
226 | else |
---|
227 | { |
---|
228 | $args['from'] = format_email($comm['author'], $comm['email']); |
---|
229 | } |
---|
230 | |
---|
231 | // hearders |
---|
232 | $headers = 'From: '.$args['from']."\n"; |
---|
233 | $headers.= 'MIME-Version: 1.0'."\n"; |
---|
234 | $headers.= 'X-Mailer: Piwigo Mailer'."\n"; |
---|
235 | $headers.= 'Content-Transfer-Encoding: 8bit'."\n"; |
---|
236 | $headers.= 'Content-Type: '.$conf['ContactForm']['cf_mail_type'].'; charset="'.get_pwg_charset().'";'."\n"; |
---|
237 | |
---|
238 | set_make_full_url(); |
---|
239 | switch_lang_to(get_default_language()); |
---|
240 | load_language('plugin.lang', CONTACT_FORM_PATH); |
---|
241 | |
---|
242 | // template |
---|
243 | if ($conf['ContactForm']['cf_mail_type'] == 'text/html') |
---|
244 | { |
---|
245 | $mail_css = file_get_contents(dirname(__FILE__).'/../template/mail/style.css'); |
---|
246 | $template->set_filename('contact_mail', dirname(__FILE__).'/../template/mail/content_html.tpl'); |
---|
247 | } |
---|
248 | else |
---|
249 | { |
---|
250 | $mail_css = null; |
---|
251 | $template->set_filename('contact_mail', dirname(__FILE__).'/../template/mail/content_plain.tpl'); |
---|
252 | } |
---|
253 | |
---|
254 | $template->assign(array( |
---|
255 | 'cf_prefix' => $prefix, |
---|
256 | 'contact' => $comm, |
---|
257 | 'GALLERY_URL' => get_gallery_home_url(), |
---|
258 | 'PHPWG_URL' => PHPWG_URL, |
---|
259 | 'CONTACT_MAIL_CSS' => $mail_css, |
---|
260 | )); |
---|
261 | |
---|
262 | // mail content |
---|
263 | $content = $template->parse('contact_mail', true); |
---|
264 | $content = wordwrap($content, 70, "\n", true); |
---|
265 | |
---|
266 | // send mail |
---|
267 | $result = |
---|
268 | trigger_event('send_mail', |
---|
269 | false, /* Result */ |
---|
270 | trigger_event('send_mail_to', implode(';', $emails)), |
---|
271 | trigger_event('send_mail_subject', $subject), |
---|
272 | trigger_event('send_mail_content', $content), |
---|
273 | trigger_event('send_mail_headers', $headers), |
---|
274 | $args |
---|
275 | ); |
---|
276 | |
---|
277 | unset_make_full_url(); |
---|
278 | switch_lang_back(); |
---|
279 | load_language('plugin.lang', CONTACT_FORM_PATH); |
---|
280 | |
---|
281 | if ($result == false) |
---|
282 | { |
---|
283 | array_push($page['errors'], l10n('Error while sending e-mail')); |
---|
284 | $comment_action='reject'; |
---|
285 | } |
---|
286 | } |
---|
287 | |
---|
288 | return $comment_action; |
---|
289 | } |
---|
290 | |
---|
291 | /** |
---|
292 | * get contact emails |
---|
293 | */ |
---|
294 | function get_contact_emails() |
---|
295 | { |
---|
296 | global $conf; |
---|
297 | |
---|
298 | include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php'); |
---|
299 | |
---|
300 | $emails = array(); |
---|
301 | foreach ($conf['ContactForm']['cf_admin_mails'] as $data) |
---|
302 | { |
---|
303 | if ($data['active']) |
---|
304 | { |
---|
305 | array_push($emails, format_email($data['name'], $data['email'])); |
---|
306 | } |
---|
307 | } |
---|
308 | |
---|
309 | return $emails; |
---|
310 | } |
---|
311 | |
---|
312 | |
---|
313 | /** |
---|
314 | * check if email is valid |
---|
315 | */ |
---|
316 | function check_email_validity($mail_address) |
---|
317 | { |
---|
318 | if (version_compare(PHP_VERSION, '5.2.0') >= 0) |
---|
319 | { |
---|
320 | return filter_var($mail_address, FILTER_VALIDATE_EMAIL)!==false; |
---|
321 | } |
---|
322 | else |
---|
323 | { |
---|
324 | $atom = '[-a-z0-9!#$%&\'*+\\/=?^_`{|}~]'; // before arobase |
---|
325 | $domain = '([a-z0-9]([-a-z0-9]*[a-z0-9]+)?)'; // domain name |
---|
326 | $regex = '/^' . $atom . '+' . '(\.' . $atom . '+)*' . '@' . '(' . $domain . '{1,63}\.)+' . $domain . '{2,63}$/i'; |
---|
327 | |
---|
328 | return (bool)preg_match($regex, $mail_address); |
---|
329 | } |
---|
330 | } |
---|
331 | |
---|
332 | ?> |
---|