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

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

restore 'render_contact_content' deleted in a previous commit

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    $comm['content'] = trigger_change('render_contact_content', $comm['content']);
244
245    $prefix = str_replace('%gallery_title%', $conf['gallery_title'], $conf['ContactForm']['cf_subject_prefix']);
246
247    $from = $Cc = null;
248    if (!empty($comm['email']))
249    {
250      $from = array(
251        'name' => $comm['author'],
252        'email' => $comm['email'],
253        );
254      if ($comm['send_copy'])
255      {
256        $Cc = $from;
257      }
258    }
259
260    switch_lang_to(get_default_language());
261    load_language('plugin.lang', CONTACT_FORM_PATH);
262
263    $result = pwg_mail(
264      $emails,
265      array(
266        'subject' => '['.$prefix.'] '.$comm['subject'],
267        'mail_title' => $prefix,
268        'mail_subtitle' => $comm['subject'],
269        'content_format' => 'text/html',
270        'from' => $from,
271        'Cc' => $Cc,
272        ),
273      array(
274        'filename' => 'mail',
275        'dirname' => realpath(CONTACT_FORM_PATH . 'template'),
276        'assign' => array(
277          'CONTACT' => $comm,
278          ),
279        )
280      );
281
282    switch_lang_back();
283    load_language('plugin.lang', CONTACT_FORM_PATH);
284
285    if ($result == false)
286    {
287      $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 * @param mixed group:
298 *    - bool true:    all emails
299 *    - empty string: emails without group
300 *    - string:       emails with the specified group
301 */
302function get_contact_emails($group=true)
303{
304  global $conf;
305
306  include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
307
308  $where = '1=1';
309  if ($group!==true)
310  {
311    if (empty($group))
312    {
313      $where = 'group_name IS NULL';
314    }
315    else
316    {
317      $where = 'group_name="'.$group.'"';
318    }
319  }
320
321  $query = '
322SELECT name, email
323  FROM '. CONTACT_FORM_TABLE .'
324  WHERE
325    '.$where.'
326    AND active = "true"
327  ORDER BY name ASC
328';
329  $emails = array_from_query($query);
330
331  return $emails;
332}
Note: See TracBrowser for help on using the repository browser.