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

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

use trigger_change

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