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

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

update for 2.6

File size: 7.6 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    )
163   );
164
165  $comment_action='validate';
166
167  // check key
168  if (!verify_ephemeral_key(@$key))
169  {
170    $comment_action='reject';
171  }
172
173  // check author
174  if ($conf['ContactForm']['cf_mandatory_name'] and empty($comm['author']))
175  {
176    $page['errors'][] = l10n('Please enter a name');
177    $comment_action='reject';
178  }
179
180  // check email
181  if ($conf['ContactForm']['cf_mandatory_mail'] and empty($comm['email']))
182  {
183    $page['errors'][] = l10n('Please enter an e-mail');
184    $comment_action='reject';
185  }
186  else if (!empty($comm['email']) and !email_check_format($comm['email']))
187  {
188    $page['errors'][] = l10n('mail address must be like xxx@yyy.eee (example : jack@altern.org)');
189    $comment_action='reject';
190  }
191
192  // check subject
193  if (empty($comm['subject']))
194  {
195    $page['errors'][] = l10n('Please enter a subject');
196    $comment_action='reject';
197  }
198  else if (strlen($comm['subject']) > 100)
199  {
200    $page['errors'][] = sprintf(l10n('%s must not be more than %d characters long'), l10n('Subject'), 100);
201    $comment_action='reject';
202  }
203
204  // check group
205  if (count($groups) > 1 and $comm['group'] == -1)
206  {
207    $comm['group'] = true;
208    $page['errors'][] = l10n('Please choose a category');
209    $comment_action='reject';
210  }
211
212  // check content
213  if (empty($comm['content']))
214  {
215    $page['errors'][] = l10n('Please enter a message');
216    $comment_action='reject';
217  }
218  else if (strlen($comm['subject']) > 2000)
219  {
220    $page['errors'][] = sprintf(l10n('%s must not be more than %d characters long'), l10n('Message'), 2000);
221    $comment_action='reject';
222  }
223
224  include_once(PHPWG_ROOT_PATH.'include/functions_comment.inc.php');
225  include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
226
227  add_event_handler('contact_form_check', 'user_comment_check',
228    EVENT_HANDLER_PRIORITY_NEUTRAL, 2);
229
230  // perform more spam check
231  $comment_action = trigger_event('contact_form_check', $comment_action, $comm);
232
233  // get admin emails
234  $emails = get_contact_emails($comm['group']);
235  if (!count($emails))
236  {
237    $page['errors'][] = l10n('Error while sending e-mail');
238    $comment_action='reject';
239  }
240
241  if ($comment_action == 'validate')
242  {
243    $prefix = str_replace('%gallery_title%', $conf['gallery_title'], $conf['ContactForm']['cf_subject_prefix']);
244
245    $from = $Cc = null;
246    if (!empty($comm['email']))
247    {
248      $from = array(
249        'name' => $comm['author'],
250        'email' => $comm['email'],
251        );
252      if ($comm['send_copy'])
253      {
254        $Cc = $from;
255      }
256    }
257
258    switch_lang_to(get_default_language());
259    load_language('plugin.lang', CONTACT_FORM_PATH);
260
261    $result = pwg_mail(
262      $emails,
263      array(
264        'subject' => '['.$prefix.'] '.$comm['subject'],
265        'mail_title' => $prefix,
266        'mail_subtitle' => $comm['subject'],
267        'content_format' => 'text/html',
268        'from' => $from,
269        'Cc' => $Cc,
270        ),
271      array(
272        'filename' => 'mail',
273        'dirname' => realpath(CONTACT_FORM_PATH . 'template'),
274        'assign' => array(
275          'CONTACT' => $comm,
276          ),
277        )
278      );
279
280    switch_lang_back();
281    load_language('plugin.lang', CONTACT_FORM_PATH);
282
283    if ($result == false)
284    {
285      $page['errors'][] = l10n('Error while sending e-mail');
286      $comment_action='reject';
287    }
288  }
289
290  return $comment_action;
291}
292
293/**
294 * get contact emails
295 * @param mixed group:
296 *    - bool true:    all emails
297 *    - empty string: emails without group
298 *    - string:       emails with the specified group
299 */
300function get_contact_emails($group=true)
301{
302  global $conf;
303
304  include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
305
306  $where = '1=1';
307  if ($group!==true)
308  {
309    if (empty($group))
310    {
311      $where = 'group_name IS NULL';
312    }
313    else
314    {
315      $where = 'group_name="'.$group.'"';
316    }
317  }
318
319  $query = '
320SELECT name, email
321  FROM '. CONTACT_FORM_TABLE .'
322  WHERE
323    '.$where.'
324    AND active = "true"
325  ORDER BY name ASC
326';
327  $emails = array_from_query($query);
328
329  return $emails;
330}
Note: See TracBrowser for help on using the repository browser.