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

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

change $BODY_ID

File size: 9.7 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}
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    if ($conf['ContactForm']['cf_mail_type'] == 'text/html')
226    {
227      $comm['content'] = nl2br($comm['content']);
228    }
229   
230    // format expeditor
231    if (empty($comm['email']))
232    {
233      $args['from'] = $conf_mail['formated_email_webmaster'];
234    }
235    else
236    {
237      $args['from'] = format_email($comm['author'], $comm['email']);
238    }
239   
240    // hearders
241    $headers = 'From: '.$args['from']."\n"; 
242    $headers.= 'MIME-Version: 1.0'."\n";
243    $headers.= 'X-Mailer: Piwigo Mailer'."\n";
244    $headers.= 'Content-Transfer-Encoding: 8bit'."\n";
245    $headers.= 'Content-Type: '.$conf['ContactForm']['cf_mail_type'].'; charset="'.get_pwg_charset().'";'."\n";
246   
247    set_make_full_url();
248    switch_lang_to(get_default_language());
249    load_language('plugin.lang', CONTACT_FORM_PATH);
250   
251    // template
252    if ($conf['ContactForm']['cf_mail_type'] == 'text/html')
253    {
254      $mail_css = file_get_contents(dirname(__FILE__).'/../template/mail/style.css');
255      $template->set_filename('contact_mail', dirname(__FILE__).'/../template/mail/content_html.tpl');
256    }
257    else
258    {
259      $mail_css = null;
260      $template->set_filename('contact_mail', dirname(__FILE__).'/../template/mail/content_plain.tpl');
261    }
262   
263    $comm['show_ip'] = isset($conf['contact_form_show_ip']) ? $conf['contact_form_show_ip'] : false;
264   
265    $template->assign(array(
266      'cf_prefix' => $prefix,
267      'contact' => $comm,
268      'GALLERY_URL' => get_gallery_home_url(),
269      'PHPWG_URL' => PHPWG_URL,
270      'CONTACT_MAIL_CSS' => $mail_css,
271      ));
272   
273    // mail content
274    $content = $template->parse('contact_mail', true);
275    $content = wordwrap($content, 70, "\n", true);
276   
277    // send mail
278    $result =
279      trigger_event('send_mail',
280        false, /* Result */
281        trigger_event('send_mail_to', implode(',', $emails)),
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    if ( $comm['send_copy'] and !empty($comm['email']) )
289    {
290      trigger_event('send_mail',
291        false, /* Result */
292        trigger_event('send_mail_to', $args['from']),
293        trigger_event('send_mail_subject', $subject),
294        trigger_event('send_mail_content', $content),
295        trigger_event('send_mail_headers', $headers),
296        $args
297      );
298    }
299   
300    unset_make_full_url();
301    switch_lang_back();
302    load_language('plugin.lang', CONTACT_FORM_PATH);
303   
304    if ($result == false)
305    {
306      array_push($page['errors'], l10n('Error while sending e-mail'));
307      $comment_action='reject';
308    }
309  }
310 
311  return $comment_action;
312}
313
314/**
315 * get contact emails
316 * @param mixed group:
317 *    - bool true:    all emails
318 *    - empty string: emails without group
319 *    - string:       emails with the specified group
320 */
321function get_contact_emails($group=true)
322{
323  global $conf;
324 
325  include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
326 
327  $where = '1=1';
328  if ($group!==true)
329  {
330    if (empty($group))
331    {
332      $where = 'group_name IS NULL';
333    }
334    else
335    {
336      $where = 'group_name="'.$group.'"';
337    }
338  }
339 
340  $query = '
341SELECT *
342  FROM '. CONTACT_FORM_TABLE .'
343  WHERE
344    '.$where.'
345    AND active = "true"
346  ORDER BY name ASC
347';
348  $result = pwg_query($query);
349 
350  $emails = array();
351  while ($data = pwg_db_fetch_assoc($result))
352  {
353    array_push($emails, format_email($data['name'], $data['email']));
354  }
355 
356  return $emails;
357}
358
359
360/**
361 * check if email is valid
362 */
363function check_email_validity($mail_address)
364{
365  if (function_exists('email_check_format'))
366  {
367    return email_check_format($mail_address); // Piwigo 2.5
368  }
369  else if (version_compare(PHP_VERSION, '5.2.0') >= 0)
370  {
371    return filter_var($mail_address, FILTER_VALIDATE_EMAIL)!==false;
372  }
373  else
374  {
375    $atom   = '[-a-z0-9!#$%&\'*+\\/=?^_`{|}~]';   // before  arobase
376    $domain = '([a-z0-9]([-a-z0-9]*[a-z0-9]+)?)'; // domain name
377    $regex = '/^' . $atom . '+' . '(\.' . $atom . '+)*' . '@' . '(' . $domain . '{1,63}\.)+' . $domain . '{2,63}$/i';
378
379    return (bool)preg_match($regex, $mail_address);
380  }
381}
382
383?>
Note: See TracBrowser for help on using the repository browser.