source: extensions/ContactForm/include/functions.inc.php @ 20795

Last change on this file since 20795 was 20795, checked in by mistic100, 11 years ago

add trigger for content render

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