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

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

missing file in previous commit

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    add_event_handler('loc_begin_page_header', 'contact_form_header');
14   
15    $page['section'] = 'contact';
16    $page['is_homepage'] = false;
17   
18    $page['title'] = l10n('Contact');
19    $page['section_title'] = $page['section_title'] = '<a href="'.get_absolute_root_url().'">'.l10n('Home').'</a>'.$conf['level_separator'].'<a href="'.CONTACT_FORM_PUBLIC.'">'.l10n('Contact').'</a>'; 
20  }
21}
22function contact_form_header()
23{
24  global $page;
25  $page['body_id'] = 'theContactPage';
26}
27
28/**
29 * contact page
30 */
31function contact_form_page()
32{
33  global $page;
34
35  if (isset($page['section']) and $page['section'] == 'contact')
36  {
37    include(CONTACT_FORM_PATH . '/include/contact_form.inc.php');
38  }
39}
40
41/**
42 * public menu link
43 */
44function contact_form_applymenu($menu_ref_arr)
45{
46  global $conf;
47 
48  if ( !$conf['ContactForm']['cf_menu_link'] ) return;
49  if ( !is_classic_user() and !$conf['ContactForm']['cf_allow_guest'] ) return;
50  if ( !count(get_contact_emails()) ) return;
51
52  $menu = &$menu_ref_arr[0];
53  if (($block = $menu->get_block('mbMenu')) != null)
54  {
55    array_push($block->data, array(
56      'URL' => CONTACT_FORM_PUBLIC,
57      'NAME' => l10n('Contact'),
58    ));
59  }
60}
61
62/**
63 * change contact on link on footer
64 */
65function contact_form_footer_link($content, &$smarty)
66{
67  $search = '<a href="mailto:{$CONTACT_MAIL}?subject={\'A comment on your site\'|@translate|@escape:url}">';
68  $replace = '<a href="'.CONTACT_FORM_PUBLIC.'">';
69  return str_replace($search, $replace, $content);
70}
71
72/**
73 * init emails list
74 */
75function contact_form_initialize_emails()
76{
77  global $conf;
78 
79  $query = '
80SELECT
81    u.'.$conf['user_fields']['username'].' AS username,
82    u.'.$conf['user_fields']['email'].' AS email
83  FROM '.USERS_TABLE.' AS u
84    JOIN '.USER_INFOS_TABLE.' AS i
85    ON i.user_id =  u.'.$conf['user_fields']['id'].'
86  WHERE i.status in (\'webmaster\',  \'admin\')
87    AND '.$conf['user_fields']['email'].' IS NOT NULL
88  ORDER BY username
89;';
90  $result = pwg_query($query);
91 
92  $emails = array();
93  while ($row = pwg_db_fetch_assoc($result))
94  {
95    array_push($emails, array(
96      'name' => $row['username'],
97      'email' => $row['email'],
98      'active' => 'true',
99      ));
100  }
101 
102  mass_inserts(
103    CONTACT_FORM_TABLE,
104    array('name','email','active'),
105    $emails
106    );
107 
108  $conf['ContactForm']['cf_must_initialize'] = false;
109  conf_update_param('ContactForm', serialize($conf['ContactForm']));
110}
111
112
113/**
114 * Send comment to subscribers
115 */
116function send_contact_form(&$comm, $key)
117{
118  global $conf, $page, $template, $conf_mail;
119 
120  if (!isset($conf_mail))
121  {
122    $conf_mail = get_mail_configuration();
123  }
124 
125  $query = '
126SELECT DISTINCT group_name
127  FROM '. CONTACT_FORM_TABLE .'
128  ORDER BY group_name
129;';
130  $groups = array_from_query($query, 'group_name');
131 
132  $comm = array_merge($comm,
133    array(
134      'ip' => $_SERVER['REMOTE_ADDR'],
135      'agent' => $_SERVER['HTTP_USER_AGENT']
136    )
137   );
138
139  $comment_action='validate';
140 
141  // check key
142  if (!verify_ephemeral_key(@$key))
143  {
144    $comment_action='reject';
145  }
146
147  // check author
148  if ( $conf['ContactForm']['cf_mandatory_name'] and empty($comm['author']) )
149  {
150    array_push($page['errors'], l10n('Please enter a name'));
151    $comment_action='reject';
152  }
153 
154  // check email
155  if ( $conf['ContactForm']['cf_mandatory_mail'] and empty($comm['email']) )
156  {
157    array_push($page['errors'], l10n('Please enter an e-mail'));
158    $comment_action='reject';
159  }
160  else if ( !empty($comm['email']) and !check_email_validity($comm['email']) )
161  {
162    array_push($page['errors'], l10n('mail address must be like xxx@yyy.eee (example : jack@altern.org)'));
163    $comment_action='reject';
164  }
165
166  // check subject
167  if (empty($comm['subject']))
168  {
169    array_push($page['errors'], l10n('Please enter a subject'));
170    $comment_action='reject';
171  }
172  else if (strlen($comm['subject']) > 100)
173  {
174    array_push($page['errors'], sprintf(l10n('%s must not be more than %d characters long'), l10n('Subject'), 100));
175    $comment_action='reject';
176  }
177 
178  // check group
179  if ( count($groups) > 1 and $comm['group'] == -1 )
180  {
181    $comm['group'] = true;
182    array_push($page['errors'], l10n('Please choose a category'));
183    $comment_action='reject';
184  }
185 
186  // check content
187  if (empty($comm['content']))
188  {
189    array_push($page['errors'], l10n('Please enter a message'));
190    $comment_action='reject';
191  }
192  else if (strlen($comm['subject']) > 2000)
193  {
194    array_push($page['errors'], sprintf(l10n('%s must not be more than %d characters long'), l10n('Message'), 2000));
195    $comment_action='reject';
196  }
197 
198  include_once(PHPWG_ROOT_PATH.'include/functions_comment.inc.php');
199  include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
200 
201  add_event_handler('contact_form_check', 'user_comment_check',
202    EVENT_HANDLER_PRIORITY_NEUTRAL, 2);
203 
204  // perform more spam check
205  $comment_action = trigger_event('contact_form_check', $comment_action, $comm);
206 
207  // get admin emails
208  $emails = get_contact_emails($comm['group']);
209  if (!count($emails))
210  {
211    array_push($page['errors'], l10n('Error while sending e-mail'));
212    $comment_action='reject';
213  }
214
215  if ($comment_action == 'validate')
216  {
217    // format subject
218    $prefix = str_replace('%gallery_title%', $conf['gallery_title'], $conf['ContactForm']['cf_subject_prefix']);
219    $subject = '['.$prefix.'] '.$comm['subject'];
220    $subject = trim(preg_replace('#[\n\r]+#s', '', $subject));
221    $subject = encode_mime_header($subject);
222   
223    // format content
224    $comm['content'] = trigger_event('render_contact_content', $comm['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-'.$conf['ContactForm']['cf_theme'].'.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.