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

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

"undefined index cf_ready" when config is reloaded (main configuration page)

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