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

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

add button 'Send copy to my email'

File size: 9.3 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  }
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  mass_inserts(
93    CONTACT_FORM_TABLE,
94    array('name','email','active'),
95    $emails
96    );
97 
98  $conf['ContactForm']['cf_must_initialize'] = false;
99  conf_update_param('ContactForm', serialize($conf['ContactForm']));
100}
101
102
103/**
104 * Send comment to subscribers
105 */
106function send_contact_form(&$comm, $key)
107{
108  global $conf, $page, $template, $conf_mail;
109 
110  if (!isset($conf_mail))
111  {
112    $conf_mail = get_mail_configuration();
113  }
114 
115  $query = '
116SELECT DISTINCT group_name
117  FROM '. CONTACT_FORM_TABLE .'
118  ORDER BY group_name
119;';
120  $groups = array_from_query($query, 'group_name');
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']) > 100)
163  {
164    array_push($page['errors'], sprintf(l10n('%s must not be more than %d characters long'), l10n('Subject'), 100));
165    $comment_action='reject';
166  }
167 
168  // check group
169  if ( count($groups) > 1 and $comm['group'] == -1 )
170  {
171    $comm['group'] = true;
172    array_push($page['errors'], l10n('Please choose a category'));
173    $comment_action='reject';
174  }
175 
176  // check content
177  if (empty($comm['content']))
178  {
179    array_push($page['errors'], l10n('Please enter a message'));
180    $comment_action='reject';
181  }
182  else if (strlen($comm['subject']) > 2000)
183  {
184    array_push($page['errors'], sprintf(l10n('%s must not be more than %d characters long'), l10n('Message'), 2000));
185    $comment_action='reject';
186  }
187 
188  include_once(PHPWG_ROOT_PATH.'include/functions_comment.inc.php');
189  include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
190 
191  add_event_handler('contact_form_check', 'user_comment_check',
192    EVENT_HANDLER_PRIORITY_NEUTRAL, 2);
193 
194  // perform more spam check
195  $comment_action = trigger_event('contact_form_check', $comment_action, $comm);
196 
197  // get admin emails
198  $emails = get_contact_emails($comm['group']);
199  if (!count($emails))
200  {
201    array_push($page['errors'], l10n('Error while sending e-mail'));
202    $comment_action='reject';
203  }
204
205  if ($comment_action == 'validate')
206  {
207    // format subject
208    $prefix = str_replace('%gallery_title%', $conf['gallery_title'], $conf['ContactForm']['cf_subject_prefix']);
209    $subject = '['.$prefix.'] '.$comm['subject'];
210    $subject = trim(preg_replace('#[\n\r]+#s', '', $subject));
211    $subject = encode_mime_header($subject);
212   
213    // format content
214    if ($conf['ContactForm']['cf_mail_type'] == 'text/html')
215    {
216      $comm['content'] = nl2br($comm['content']);
217    }
218   
219    // format expeditor
220    if (empty($comm['email']))
221    {
222      $args['from'] = $conf_mail['formated_email_webmaster'];
223    }
224    else
225    {
226      $args['from'] = format_email($comm['author'], $comm['email']);
227    }
228   
229    // hearders
230    $headers = 'From: '.$args['from']."\n"; 
231    $headers.= 'MIME-Version: 1.0'."\n";
232    $headers.= 'X-Mailer: Piwigo Mailer'."\n";
233    $headers.= 'Content-Transfer-Encoding: 8bit'."\n";
234    $headers.= 'Content-Type: '.$conf['ContactForm']['cf_mail_type'].'; charset="'.get_pwg_charset().'";'."\n";
235   
236    set_make_full_url();
237    switch_lang_to(get_default_language());
238    load_language('plugin.lang', CONTACT_FORM_PATH);
239   
240    // template
241    if ($conf['ContactForm']['cf_mail_type'] == 'text/html')
242    {
243      $mail_css = file_get_contents(dirname(__FILE__).'/../template/mail/style.css');
244      $template->set_filename('contact_mail', dirname(__FILE__).'/../template/mail/content_html.tpl');
245    }
246    else
247    {
248      $mail_css = null;
249      $template->set_filename('contact_mail', dirname(__FILE__).'/../template/mail/content_plain.tpl');
250    }
251   
252    $comm['show_ip'] = isset($conf['contact_form_show_ip']) ? $conf['contact_form_show_ip'] : false;
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    if ( $comm['send_copy'] and !empty($comm['email']) )
278    {
279      trigger_event('send_mail',
280        false, /* Result */
281        trigger_event('send_mail_to', $args['from']),
282        trigger_event('send_mail_subject', $subject),
283        trigger_event('send_mail_content', $content),
284        trigger_event('send_mail_headers', $headers),
285        $args
286      );
287    }
288   
289    unset_make_full_url();
290    switch_lang_back();
291    load_language('plugin.lang', CONTACT_FORM_PATH);
292   
293    if ($result == false)
294    {
295      array_push($page['errors'], l10n('Error while sending e-mail'));
296      $comment_action='reject';
297    }
298  }
299 
300  return $comment_action;
301}
302
303/**
304 * get contact emails
305 * @param mixed group:
306 *    - bool true:    all emails
307 *    - empty string: emails without group
308 *    - string:       emails with the specified group
309 */
310function get_contact_emails($group=true)
311{
312  global $conf;
313 
314  include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
315 
316  $where = '1=1';
317  if ($group!==true)
318  {
319    if (empty($group))
320    {
321      $where = 'group_name IS NULL';
322    }
323    else
324    {
325      $where = 'group_name="'.$group.'"';
326    }
327  }
328 
329  $query = '
330SELECT *
331  FROM '. CONTACT_FORM_TABLE .'
332  WHERE
333    '.$where.'
334    AND active = "true"
335  ORDER BY name ASC
336';
337  $result = pwg_query($query);
338 
339  $emails = array();
340  while ($data = pwg_db_fetch_assoc($result))
341  {
342    array_push($emails, format_email($data['name'], $data['email']));
343  }
344 
345  return $emails;
346}
347
348
349/**
350 * check if email is valid
351 */
352function check_email_validity($mail_address)
353{
354  if (version_compare(PHP_VERSION, '5.2.0') >= 0)
355  {
356    return filter_var($mail_address, FILTER_VALIDATE_EMAIL)!==false;
357  }
358  else
359  {
360    $atom   = '[-a-z0-9!#$%&\'*+\\/=?^_`{|}~]';   // before  arobase
361    $domain = '([a-z0-9]([-a-z0-9]*[a-z0-9]+)?)'; // domain name
362    $regex = '/^' . $atom . '+' . '(\.' . $atom . '+)*' . '@' . '(' . $domain . '{1,63}\.)+' . $domain . '{2,63}$/i';
363
364    return (bool)preg_match($regex, $mail_address);
365  }
366}
367
368?>
Note: See TracBrowser for help on using the repository browser.