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

Last change on this file since 28324 was 28324, checked in by mistic100, 10 years ago

add $confcontact_form_show_ip option

File size: 7.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'] = l10n('Contact');
15    $page['body_id'] = 'theContactPage';
16    $page['is_external'] = true;
17    $page['is_homepage'] = false;
18
19    $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}
22
23/**
24 * contact page
25 */
26function contact_form_page()
27{
28  global $page;
29
30  if (isset($page['section']) and $page['section'] == 'contact')
31  {
32    include(CONTACT_FORM_PATH . 'include/contact_form.inc.php');
33  }
34}
35
36/**
37 * public menu link
38 */
39function contact_form_applymenu($menu_ref_arr)
40{
41  global $conf;
42
43  if (!$conf['ContactForm']['cf_menu_link']) return;
44  if (!is_classic_user() and !$conf['ContactForm']['cf_allow_guest']) return;
45  if (!$conf['ContactForm_ready']) return;
46
47  $menu = &$menu_ref_arr[0];
48  if (($block = $menu->get_block('mbMenu')) != null)
49  {
50    $block->data[] = array(
51      'URL' => CONTACT_FORM_PUBLIC,
52      'NAME' => l10n('Contact'),
53      );
54  }
55}
56
57/**
58 * change contact link in footer
59 */
60function contact_form_footer_link($content, &$smarty)
61{
62  $search = '<a href="mailto:{$CONTACT_MAIL}?subject={\'A comment on your site\'|@translate|@escape:url}">';
63  $replace = '<a href="{$CONTACT_FORM_PUBLIC}">';
64  return str_replace($search, $replace, $content);
65}
66
67/**
68 * change contact link in mail footer
69 */
70function contact_form_mail_template($cache_key, $content_type)
71{
72  global $conf_mail;
73
74  $template = &$conf_mail[$cache_key]['theme'];
75  $template->assign('CONTACT_FORM_PUBLIC', CONTACT_FORM_PUBLIC);
76
77  if ($content_type == 'text/html')
78  {
79    $template->set_prefilter('mail_footer', 'contact_form_mail_template_html');
80  }
81  else
82  {
83    $template->set_prefilter('mail_footer', 'contact_form_mail_template_plain');
84  }
85}
86function contact_form_mail_template_html($content)
87{
88  return str_replace(
89    'mailto:{$CONTACT_MAIL}?subject={\'A comment on your site\'|translate|escape:url}',
90    '{$CONTACT_FORM_PUBLIC}',
91    $content
92    );
93}
94function contact_form_mail_template_plain($content)
95{
96  return str_replace(
97    '{$CONTACT_MAIL}',
98    '{$CONTACT_FORM_PUBLIC}',
99    $content
100    );
101}
102
103/**
104 * init emails list
105 */
106function contact_form_initialize_emails()
107{
108  global $conf;
109
110  $query = '
111SELECT
112    u.'.$conf['user_fields']['username'].' AS username,
113    u.'.$conf['user_fields']['email'].' AS email
114  FROM '.USERS_TABLE.' AS u
115    JOIN '.USER_INFOS_TABLE.' AS i
116    ON i.user_id =  u.'.$conf['user_fields']['id'].'
117  WHERE i.status IN (\'webmaster\',  \'admin\')
118    AND '.$conf['user_fields']['email'].' IS NOT NULL
119  ORDER BY username
120;';
121  $result = pwg_query($query);
122
123  $emails = array();
124  while ($row = pwg_db_fetch_assoc($result))
125  {
126    $emails[] = array(
127      'name' => $row['username'],
128      'email' => $row['email'],
129      'active' => 'true',
130      );
131  }
132
133  mass_inserts(
134    CONTACT_FORM_TABLE,
135    array('name','email','active'),
136    $emails
137    );
138
139  $conf['ContactForm']['cf_must_initialize'] = false;
140  conf_update_param('ContactForm', serialize($conf['ContactForm']));
141}
142
143
144/**
145 * Send comment to subscribers
146 */
147function send_contact_form(&$comm, $key)
148{
149  global $conf, $page, $template;
150
151  $query = '
152SELECT DISTINCT group_name
153  FROM '. CONTACT_FORM_TABLE .'
154  ORDER BY group_name
155;';
156  $groups = array_from_query($query, 'group_name');
157
158  $comm = array_merge($comm,
159    array(
160      'ip' => $_SERVER['REMOTE_ADDR'],
161      'agent' => $_SERVER['HTTP_USER_AGENT'],
162      'show_ip' => isset($conf['contact_form_show_ip']) && $conf['contact_form_show_ip'],
163    )
164   );
165
166  $comment_action='validate';
167
168  // check key
169  if (!verify_ephemeral_key(@$key))
170  {
171    $comment_action='reject';
172  }
173
174  // check author
175  if ($conf['ContactForm']['cf_mandatory_name'] and empty($comm['author']))
176  {
177    $page['errors'][] = l10n('Please enter a name');
178    $comment_action='reject';
179  }
180
181  // check email
182  if ($conf['ContactForm']['cf_mandatory_mail'] and empty($comm['email']))
183  {
184    $page['errors'][] = l10n('Please enter an e-mail');
185    $comment_action='reject';
186  }
187  else if (!empty($comm['email']) and !email_check_format($comm['email']))
188  {
189    $page['errors'][] = l10n('mail address must be like xxx@yyy.eee (example : jack@altern.org)');
190    $comment_action='reject';
191  }
192
193  // check subject
194  if (empty($comm['subject']))
195  {
196    $page['errors'][] = l10n('Please enter a subject');
197    $comment_action='reject';
198  }
199  else if (strlen($comm['subject']) > 100)
200  {
201    $page['errors'][] = sprintf(l10n('%s must not be more than %d characters long'), l10n('Subject'), 100);
202    $comment_action='reject';
203  }
204
205  // check group
206  if (count($groups) > 1 and $comm['group'] == -1)
207  {
208    $comm['group'] = true;
209    $page['errors'][] = l10n('Please choose a category');
210    $comment_action='reject';
211  }
212
213  // check content
214  if (empty($comm['content']))
215  {
216    $page['errors'][] = l10n('Please enter a message');
217    $comment_action='reject';
218  }
219  else if (strlen($comm['subject']) > 2000)
220  {
221    $page['errors'][] = sprintf(l10n('%s must not be more than %d characters long'), l10n('Message'), 2000);
222    $comment_action='reject';
223  }
224
225  include_once(PHPWG_ROOT_PATH.'include/functions_comment.inc.php');
226  include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
227
228  add_event_handler('contact_form_check', 'user_comment_check',
229    EVENT_HANDLER_PRIORITY_NEUTRAL, 2);
230
231  // perform more spam check
232  $comment_action = trigger_event('contact_form_check', $comment_action, $comm);
233
234  // get admin emails
235  $emails = get_contact_emails($comm['group']);
236  if (!count($emails))
237  {
238    $page['errors'][] = l10n('Error while sending e-mail');
239    $comment_action='reject';
240  }
241
242  if ($comment_action == 'validate')
243  {
244    $comm['content'] = trigger_event('render_contact_content', $comm['content']);
245
246    $prefix = str_replace('%gallery_title%', $conf['gallery_title'], $conf['ContactForm']['cf_subject_prefix']);
247
248    $from = $Cc = null;
249    if (!empty($comm['email']))
250    {
251      $from = array(
252        'name' => $comm['author'],
253        'email' => $comm['email'],
254        );
255      if ($comm['send_copy'])
256      {
257        $Cc = $from;
258      }
259    }
260
261    switch_lang_to(get_default_language());
262    load_language('plugin.lang', CONTACT_FORM_PATH);
263
264    $result = pwg_mail(
265      $emails,
266      array(
267        'subject' => '['.$prefix.'] '.$comm['subject'],
268        'mail_title' => $prefix,
269        'mail_subtitle' => $comm['subject'],
270        'content_format' => 'text/html',
271        'from' => $from,
272        'Cc' => $Cc,
273        ),
274      array(
275        'filename' => 'mail',
276        'dirname' => realpath(CONTACT_FORM_PATH . 'template'),
277        'assign' => array(
278          'CONTACT' => $comm,
279          ),
280        )
281      );
282
283    switch_lang_back();
284    load_language('plugin.lang', CONTACT_FORM_PATH);
285
286    if ($result == false)
287    {
288      $page['errors'][] = l10n('Error while sending e-mail');
289      $comment_action='reject';
290    }
291  }
292
293  return $comment_action;
294}
295
296/**
297 * get contact emails
298 * @param mixed group:
299 *    - bool true:    all emails
300 *    - empty string: emails without group
301 *    - string:       emails with the specified group
302 */
303function get_contact_emails($group=true)
304{
305  global $conf;
306
307  include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
308
309  $where = '1=1';
310  if ($group!==true)
311  {
312    if (empty($group))
313    {
314      $where = 'group_name IS NULL';
315    }
316    else
317    {
318      $where = 'group_name="'.$group.'"';
319    }
320  }
321
322  $query = '
323SELECT name, email
324  FROM '. CONTACT_FORM_TABLE .'
325  WHERE
326    '.$where.'
327    AND active = "true"
328  ORDER BY name ASC
329';
330  $emails = array_from_query($query);
331
332  return $emails;
333}
Note: See TracBrowser for help on using the repository browser.