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

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