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

Last change on this file since 17662 was 17662, checked in by mistic100, 12 years ago

correct multi-recipient syntax,
remove user agent display,
advanced param 'contact_form_show_ip' to remove IP display

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