source: extensions/ContactForm/classes/cf_plugin.class.php @ 7197

Last change on this file since 7197 was 7197, checked in by plg, 14 years ago

feature 1897 added: the core "Contact Webmaster" link in footer is replaced by
a link to the ContactForm page. It works with a smarty prefilter, so it should
work with all themes.

The configurable template variable name was removed. It is hard coded as
$ContactFormLink. I've seen no example using another variable name. It was
hard to understand for users in the administration panel and not very useful.
The corresponding language keys were removed from all languages.

bug 1916 fixed: apply the same rule for link display in footer as in menu. If
the administrator has allowed the ContactForm page for guests, let's display
it in the menu + footer.

File size: 16.4 KB
Line 
1<?php
2if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
3
4/**
5 * CF_Plugin class
6 */
7class CF_Plugin {
8  protected $plugin_id;
9  protected $plugin_title;
10  protected $config;
11  protected $debug=array();
12 
13  /* ************************ */
14  /* ** Constructor        ** */
15  /* ************************ */
16
17  function CF_Plugin($plugin_id, $title=CF_TITLE) {
18    $this->plugin_id = $plugin_id;
19    $this->plugin_title = $title;
20    $this->config = new CF_Config();
21    $this->config->set_db_key(CF_CFG_DB_KEY);
22    $this->config->load_config();
23    $this->config->set_value(CF_CFG_COMMENT, CF_CFG_DB_COMMENT);
24    CF_Log::add_debug($this->config, 'CF_Plugin');
25  }
26 
27  /* ************************ */
28  /* ** Trigger management ** */
29  /* ************************ */
30 
31  function get_admin_plugin_menu_links($menu) {
32    array_push(
33        $menu,
34        array(
35            'NAME' => $this->get_title(),
36            'URL'  => $this->get_plugin_admin_url()
37        )
38    );
39    return $menu;
40  }
41 
42  function loc_begin_index() {
43    $this->display_message();
44  }
45  function loc_begin_page_header() {
46    global $template;
47   
48    $template->set_prefilter('tail', 'contactForm_prefilter');
49
50    if (!$this->check_allowed()) {
51      return;
52    }
53   
54    $cf_values = array(
55        'TEXT'  => $this->config->get_lang_value('contact_form_link'),
56        'URL'   => make_index_url(array('section' => CF_URL_PARAMETER)),
57      );
58    $template->assign('CF_FOOTER_VALUES', $cf_values);
59
60    $template->assign('ContactFormLink', $this->get_html_contact_form_link());
61  }
62 
63  function blockmanager_apply($aMenuRefArray) {
64    if (!$this->check_menu_adding()) {
65      return;
66    }
67    $menu = &$aMenuRefArray[0];
68    $block_mbMenu = $menu->get_block('mbMenu');
69    if (null == $block_mbMenu) {
70      return;
71    }
72    // Include language advices
73    load_language('plugin.lang', CF_PATH);
74   
75    if (!isset($block_mbMenu->data[CF_MENUBAR_KEY])) {
76      $contact_form_menu = array(
77          'TITLE' => $this->config->get_lang_value('contact_form_title'),
78          'NAME'  => $this->config->get_lang_value('contact_form'),
79          'URL'   => make_index_url(array('section' => CF_URL_PARAMETER)),
80        );
81      $block_mbMenu->data[CF_MENUBAR_KEY] = $contact_form_menu;
82    }
83   
84  }
85 
86  function loc_end_index() {
87    if ('index' != script_basename()) {
88      return;
89    }
90    if (!$this->check_allowed()) {
91      return;
92    }
93    global $tokens;
94    $form_requested = false;
95    foreach($tokens as $token) {
96      if ($token == CF_URL_PARAMETER) {
97        $form_requested = true;
98      }
99    }
100    if ($form_requested) {
101      if (isset($_POST['cf_key'])) {
102        $this->valid_form();
103      } else {
104        $this->display_form($this->create_infos_array());
105      }
106    }
107  }
108
109  function send_mail_content($mail_content) {
110    remove_event_handler('send_mail_content',             
111                          array(&$this, 'send_mail_content'));
112    global $user,$conf_mail,$conf;
113    if (!isset($conf_mail)) {
114      $conf_mail = get_mail_configuration();
115    }
116    $keyargs_content_admin_info = array(
117      get_l10n_args('Connected user: %s', $user['username']),
118      get_l10n_args('IP: %s', $_SERVER['REMOTE_ADDR']),
119      get_l10n_args('Browser: %s', $_SERVER['HTTP_USER_AGENT'])
120    );
121    $newline = "\n";
122    $separator  = $newline;
123    $separator .= str_repeat($this->config->get_value(CF_CFG_SEPARATOR),
124                             $this->config->get_value(CF_CFG_SEPARATOR_LEN));
125    $separator .= $newline;
126    if ('text/html' == $conf_mail['default_email_format']) {
127      $newline = "<br/>";
128      $separator .= '<hr style="width: ';
129      $separator .= $this->config->get_value(CF_CFG_SEPARATOR_LEN);
130      $separator .= 'em; text-align: left;" />';
131    }
132    $footer  = $newline;
133    $footer .= $separator;
134    $footer .= l10n_args($keyargs_content_admin_info, $newline);
135    $footer .= $separator;
136   
137    $mail_content = str_replace(CF_SEPARATOR_PATTERN, $footer, $mail_content);
138    return $mail_content;
139  }
140 
141  function loc_end_page_tail() {
142    CF_Log::show_debug();
143  }
144 
145  /* ************************ */
146  /* ** Accessors          ** */
147  /* ************************ */
148
149  function get_config() {
150    return $this->config;
151  }
152
153  function get_version() {
154    return $this->config->get_version();
155  }
156 
157  function get_title() {
158    // Include language advices
159    load_language('plugin.lang', CF_PATH);
160   
161    return l10n($this->plugin_title);
162  }
163 
164  /* ************************ */
165  /* ** Private functions  ** */
166  /* ************************ */
167 
168  protected function get_html_contact_form_link() {
169    global $template;
170    $cf_link = array(
171        'TEXT'  => $this->config->get_lang_value('contact_form_link'),
172        'URL'   => make_index_url(array('section' => CF_URL_PARAMETER)),
173      );
174    $template->set_filenames(array(
175        'contact_form_link' => realpath(cf_get_template('cf_link.tpl')),
176      ));
177    $template->assign('CF_LINK', $cf_link);
178   
179    $link = $template->parse('contact_form_link', true);
180    return $link;
181  }
182 
183  protected function display_form($infos) {
184    global $template,$user;
185    $template->set_extent(realpath(cf_get_template('cf_index.tpl')), 'index');
186    $template->set_filenames(array(
187        'index'       => realpath(cf_get_template('cf_index.tpl')),
188        'cf_title'    => realpath(cf_get_template('cf_title.tpl')),
189        'cf_form'     => realpath(cf_get_template('cf_form.tpl')),
190        'cf_messages' => realpath(cf_get_template('cf_messages.tpl')),
191      ));
192    $template->block_html_head( '',
193              '<link rel="stylesheet" type="text/css" '.
194              'href="' . CF_INCLUDE . 'contactform.css' . '">',
195              $smarty, $repeat);
196    $template->block_html_head( '',
197              '<script type="text/javascript" '.
198              'src="' . CF_INCLUDE . 'contactform.js' . '">'.
199              '</script>',
200              $smarty, $repeat);
201    $cf = array(
202        'TITLE'     => 'contact_form_title',
203        'NEED_NAME' => $this->config->get_value(CF_CFG_NAME_MANDATORY),
204        'NEED_MAIL' => $this->config->get_value(CF_CFG_MAIL_MANDATORY),
205        'F_ACTION'  => make_index_url(array('section' => CF_URL_PARAMETER)),
206        'LOGGED'    => !is_a_guest(),
207        'ID'        => $infos['cf_id'],
208        'EMAIL'     => $infos['cf_from_mail'],
209        'NAME'      => $infos['cf_from_name'],
210        'SUBJECT'   => $infos['cf_subject'],
211        'MESSAGE'   => $infos['cf_message'],
212        'KEY'       => get_comment_post_key($infos['cf_id']),
213      );
214    if (!empty($infos['errors'])) {
215      $template->assign('errors', $infos['errors']);
216    }
217    if (!empty($infos['infos'])) {
218      $template->assign('infos', $infos['infos']);
219    }
220    $template->assign('CF', $cf);
221    $template->assign_var_from_handle('CF_TITLE', 'cf_title');
222    $template->assign_var_from_handle('CF_MESSAGES', 'cf_messages');
223    $template->assign_var_from_handle('CF_FORM', 'cf_form');
224  }
225
226  protected function redirect($redirect_url, $infos) {
227    global $template;
228    $template->set_filenames(array(
229        'cf_redirect' => realpath(cf_get_template('cf_redirect.tpl')),
230        'cf_title'    => realpath(cf_get_template('cf_title.tpl')),
231        'cf_messages' => realpath(cf_get_template('cf_messages.tpl')),
232      ));
233     
234    $template->block_html_head( '',
235              '<link rel="stylesheet" type="text/css" '.
236              'href="' . CF_INCLUDE . 'contactform.css' . '">',
237              $smarty, $repeat);
238    $cf = array(
239        'TITLE'     => 'contact_redirect_title',
240        'CSS'       => '<link rel="stylesheet" type="text/css" '.
241                       'href="' . CF_INCLUDE . 'contactform.css' . '">'
242      );
243             
244    if (!empty($infos['infos'])) {
245      $template->assign('infos', $infos['infos']);
246    }
247    $template->assign('CF', $cf);
248    $template->assign_var_from_handle('CF_TITLE', 'cf_title');
249    $template->assign_var_from_handle('CF_MESSAGES', 'cf_messages');
250    $redirect_msg = $template->parse('cf_redirect', true);
251    $redirect_delay = $this->config->get_value(CF_CFG_REDIRECT_DELAY);
252//    redirect($redirect_url, $redirect_msg, $redirect_delay);
253    redirect($redirect_url);
254  }
255 
256  protected function display_message() {
257    $infos = pwg_get_session_var('cf_infos');
258    pwg_unset_session_var('cf_infos');
259    if ( null == $infos or
260        (empty($infos['infos']) and
261         empty($infos['errors']))
262        ) {
263      return;
264    }
265    global $template;
266    $template->set_filenames(array(
267        'cf_index'    => realpath(cf_get_template('cf_messages_index.tpl')),
268        'cf_title'    => realpath(cf_get_template('cf_title.tpl')),
269        'cf_button'   => realpath(cf_get_template('cf_button.tpl')),
270        'cf_messages' => realpath(cf_get_template('cf_messages.tpl')),
271      ));
272     
273    $template->block_html_head( '',
274              '<link rel="stylesheet" type="text/css" '.
275              'href="' . CF_INCLUDE . 'contactform.css' . '">',
276              $smarty, $repeat);
277    $cf = array(
278        'TITLE'     => 'contact_form_title',
279      );
280    if (!empty($infos['errors'])) {
281      $template->assign('errors', $infos['errors']);
282    }
283    if (!empty($infos['infos'])) {
284      $template->assign('infos', $infos['infos']);
285    }
286    $template->assign('CF', $cf);
287    $template->assign_var_from_handle('CF_TITLE', 'cf_title');
288    $template->assign_var_from_handle('CF_MESSAGES', 'cf_messages');
289    $template->assign_var_from_handle('CF_BUTTON', 'cf_button');
290   
291    $begin = 'PLUGIN_INDEX_CONTENT_BEFORE';
292    $old_begin = $template->get_template_vars($begin);
293    $template->assign_var_from_handle($begin, 'cf_index');
294    $template->concat($begin, $old_begin);
295  }
296 
297  protected function valid_form() {
298    if ($this->check_form_params($infos)) {
299      global $template;
300      if (!$this->send_message($infos)) {
301        // Include language advices
302        load_language('plugin.lang', CF_PATH);
303        $this->display_form($infos);
304      } else {
305        pwg_set_session_var('cf_infos', array(
306            'infos'  => $infos['infos'],
307            'errors' => $infos['errors'],
308          ));
309        redirect(make_index_url());
310        //$this->redirect(make_index_url(),$infos);
311      }
312    } else {
313      $this->display_form($infos);
314    }
315  }
316 
317  protected function get_active_admin_emails() {
318    //$cf_emails = $cf_config->get_value(CF_CFG_ADMIN_MAILS);
319    $all_mails = $this->config->get_value(CF_CFG_ADMIN_MAILS);
320    $active = array('WEBMASTER' => null, 'ADMINS' => array());
321    foreach($all_mails as $email => $values) {
322      if (1 == $values['ACTIVE']) {
323        if (1 == $values['WEBMASTER']) {
324          $active['WEBMASTER'] = $values['EMAILSTR'];
325        } else {
326          array_push($active['ADMINS'], $values['EMAILSTR']);
327        }
328      }
329    }
330
331    if (empty($all_mails)) {
332      $webmaster_email = get_webmaster_mail_address();
333      $active = array(
334        'WEBMASTER' => $webmaster_email,
335        'ADMINS' => cf_get_admins_emails($webmaster_email),
336        );
337    }
338   
339    return $active;
340  }
341 
342  protected function send_message(&$infos) {
343    //redirect(make_index_url());
344//    include(PHPWG_ROOT_PATH . 'include/functions_mail.inc.php');
345
346    $admin_mails = $this->get_active_admin_emails();
347    if ( empty($admin_mails) or 
348        (empty($admin_mails['WEBMASTER']) and 
349         empty($admin_mails['ADMINS']))
350        ) {
351      // No admin mail...
352      array_push( $infos['infos'], l10n('cf_no_mail'));
353      return true;
354    }
355   
356    global $conf,$user;
357    cf_switch_to_default_lang();
358   
359    $from = format_email($infos['cf_from_name'], $infos['cf_from_mail']);
360    $subject_prefix = $this->config->get_value(CF_CFG_SUBJECT_PREFIX);
361    if (empty($subject_prefix)) {
362      $subject_prefix  = '['.CF_DEFAULT_PREFIX.'] ';
363    }
364    $subject  = '['.$subject_prefix.'] ';
365    $subject .= $infos['cf_subject'];
366    $content  = "\n\n".$infos['cf_message']."\n";
367    $content .= CF_SEPARATOR_PATTERN;
368    $mail_args = array(
369        'from' => $from,
370        'Bcc' => $admin_mails['ADMINS'],
371        'subject' => $subject,
372        'content' => $content,
373        'content_format' => 'text/plain',
374      );
375    add_event_handler('send_mail_content',             
376                      array(&$this, 'send_mail_content'));
377
378    $return = true;
379    $return = @pwg_mail(
380      $admin_mails['WEBMASTER'],
381      $mail_args
382    );
383
384    cf_switch_back_to_user_lang();
385    if (!$return) {
386      array_push( $infos['errors'], l10n('cf_error_sending_mail'));
387    } else {
388      array_push( $infos['infos'], l10n('cf_sending_mail_successful'));
389    }
390    return $return;
391  }
392 
393  protected function check_form_params(&$infos) {
394    // Include language advices
395    load_language('plugin.lang', CF_PATH);
396   
397    $infos = $this->create_infos_array(false);
398    $params_ok = true;
399    $return = '';
400    $value = '';
401    // Key
402    if ($this->check_key()) {
403      $infos['cf_id'] = trim( stripslashes($_POST['cf_id']));
404    } else {
405      $infos['cf_id'] = rand();
406      $params_ok = false;
407      array_push( $infos['errors'], l10n('cf_form_error'));
408    }
409    // From name
410    if (isset($_POST['cf_from_name'])) {
411      $value = trim( stripslashes($_POST['cf_from_name']) );
412      if (strlen($value) > 0) {
413        $infos['cf_from_name'] = $value;
414      } else {
415        array_push( $infos['errors'], l10n('cf_from_name_error'));
416        $params_ok = false;
417      }
418    } else {
419      array_push( $infos['errors'], l10n('cf_from_name_error'));
420      $params_ok = false;
421    }
422    // From mail
423    if (isset($_POST['cf_from_mail'])) {
424      $value = trim( stripslashes($_POST['cf_from_mail']) );
425      $return = cf_validate_mail_format($value);
426      if (null == $return) {
427        $infos['cf_from_mail'] = $value;
428      } else {
429        $params_ok = false;
430        array_push( $infos['errors'], $return);
431      }
432    } else {
433      array_push( $infos['errors'], l10n('cf_mail_format_error'));
434      $params_ok = false;
435    }
436    // Subject
437    if (isset($_POST['cf_subject'])) {
438      $value = trim( stripslashes($_POST['cf_subject']) );
439      if (strlen($value) > 0) {
440        $infos['cf_subject'] = $value;
441      } else {
442        array_push( $infos['errors'], l10n('cf_subject_error'));
443        $params_ok = false;
444      }
445    } else {
446      array_push( $infos['errors'], l10n('cf_subject_error'));
447      $params_ok = false;
448    }
449    // Message
450    if (isset($_POST['cf_message'])) {
451      $value = trim( stripslashes($_POST['cf_message']) );
452      if (strlen($value) > 0) {
453        $infos['cf_message'] = $value;
454      } else {
455        array_push( $infos['errors'], l10n('cf_message_error'));
456        $params_ok = false;
457      }
458    } else {
459      array_push( $infos['errors'], l10n('cf_message_error'));
460      $params_ok = false;
461    }
462    return $params_ok;
463  }
464 
465  protected function check_key() {
466    global $conf;
467    $key='';
468    $id=0;
469    if (isset($_POST['cf_key'])) {
470      $key = trim( stripslashes($_POST['cf_key']));
471    }
472    if (isset($_POST['cf_id'])) {
473      $id = trim( stripslashes($_POST['cf_id']));
474    }
475   
476    $key = explode( ':', $key );
477    if ( count($key)!=2
478          or $key[0]>time()-2 // page must have been retrieved more than 2 sec ago
479          or $key[0]<time()-3600 // 60 minutes expiration
480          or hash_hmac(
481                'md5', $key[0].':'.$id, $conf['secret_key']
482              ) != $key[1]
483        )
484    {
485      return false;
486    }
487    return true;
488  }
489 
490  protected function create_infos_array($fill=true) {
491    $infos = array(
492        'cf_id'         => '',
493        'cf_from_name'  => '',
494        'cf_from_mail'  => '',
495        'cf_subject'    => '',
496        'cf_message'    => '',
497        'errors'        => array(),
498        'infos'         => array(),
499      );
500    if ($fill) {
501      global $user;
502      // Include language advices
503      load_language('plugin.lang', CF_PATH);
504     
505      $infos['cf_id'] = rand();
506      $infos['cf_from_name'] = is_a_guest()?'':$user['username'];
507      $infos['cf_from_mail'] = $user['email'];
508
509      $l10n_key = 'title_send_mail';
510      $infos['cf_subject'] = l10n($l10n_key);
511      if ($l10n_key == $infos['cf_subject']) {
512        $infos['cf_subject'] = l10n('A comment on your site');
513      }
514    }
515    return $infos;
516  }
517  protected function check_menu_adding() {
518    return ($this->config->get_value(CF_CFG_MENU_LINK) and
519            $this->check_allowed());
520  }
521  protected function check_allowed() {
522    if (is_a_guest() and !$this->config->get_value(CF_CFG_ALLOW_GUEST)) {
523      // Not allowed
524      return false;
525    }
526    return true;
527  }
528  protected function get_plugin_admin_url() {
529    return get_admin_plugin_menu_link(CF_PATH . 'config.php');
530  }
531}
532?>
Note: See TracBrowser for help on using the repository browser.