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

Last change on this file since 8887 was 8887, checked in by patdenice, 13 years ago

Add two triggers for captcha plugin.
Language key corrected.

File size: 16.2 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    trigger_action('display_contactform');
186    $template->set_extent(realpath(cf_get_template('cf_index.tpl')), 'index');
187    $template->set_filenames(array(
188        'index'       => realpath(cf_get_template('cf_index.tpl')),
189        'cf_title'    => realpath(cf_get_template('cf_title.tpl')),
190        'cf_form'     => realpath(cf_get_template('cf_form.tpl')),
191        'cf_messages' => realpath(cf_get_template('cf_messages.tpl')),
192      ));
193    $template->block_html_head( '',
194              '<link rel="stylesheet" type="text/css" '.
195              'href="' . CF_INCLUDE . 'contactform.css' . '">',
196              $smarty, $repeat);
197    $template->block_html_head( '',
198              '<script type="text/javascript" '.
199              'src="' . CF_INCLUDE . 'contactform.js' . '">'.
200              '</script>',
201              $smarty, $repeat);
202    $cf = array(
203        'TITLE'     => 'contact_form_title',
204        'NEED_NAME' => $this->config->get_value(CF_CFG_NAME_MANDATORY),
205        'NEED_MAIL' => $this->config->get_value(CF_CFG_MAIL_MANDATORY),
206        'F_ACTION'  => make_index_url(array('section' => CF_URL_PARAMETER)),
207        'LOGGED'    => !is_a_guest(),
208        'ID'        => $infos['cf_id'],
209        'EMAIL'     => $infos['cf_from_mail'],
210        'NAME'      => $infos['cf_from_name'],
211        'SUBJECT'   => $infos['cf_subject'],
212        'MESSAGE'   => $infos['cf_message'],
213        'KEY'       => get_comment_post_key($infos['cf_id']),
214      );
215    if (!empty($infos['errors'])) {
216      $template->assign('errors', $infos['errors']);
217    }
218    if (!empty($infos['infos'])) {
219      $template->assign('infos', $infos['infos']);
220    }
221    $template->assign('CF', $cf);
222    $template->assign_var_from_handle('CF_TITLE', 'cf_title');
223    $template->assign_var_from_handle('CF_MESSAGES', 'cf_messages');
224    $template->assign_var_from_handle('CF_FORM', 'cf_form');
225  }
226
227  protected function redirect($redirect_url, $infos) {
228    global $template;
229    $template->set_filenames(array(
230        'cf_redirect' => realpath(cf_get_template('cf_redirect.tpl')),
231        'cf_title'    => realpath(cf_get_template('cf_title.tpl')),
232        'cf_messages' => realpath(cf_get_template('cf_messages.tpl')),
233      ));
234     
235    $template->block_html_head( '',
236              '<link rel="stylesheet" type="text/css" '.
237              'href="' . CF_INCLUDE . 'contactform.css' . '">',
238              $smarty, $repeat);
239    $cf = array(
240        'TITLE'     => 'contact_redirect_title',
241        'CSS'       => '<link rel="stylesheet" type="text/css" '.
242                       'href="' . CF_INCLUDE . 'contactform.css' . '">'
243      );
244             
245    if (!empty($infos['infos'])) {
246      $template->assign('infos', $infos['infos']);
247    }
248    $template->assign('CF', $cf);
249    $template->assign_var_from_handle('CF_TITLE', 'cf_title');
250    $template->assign_var_from_handle('CF_MESSAGES', 'cf_messages');
251    $redirect_msg = $template->parse('cf_redirect', true);
252    $redirect_delay = $this->config->get_value(CF_CFG_REDIRECT_DELAY);
253//    redirect($redirect_url, $redirect_msg, $redirect_delay);
254    redirect($redirect_url);
255  }
256 
257  protected function display_message() {
258    $infos = pwg_get_session_var('cf_infos');
259    pwg_unset_session_var('cf_infos');
260    if ( null == $infos or
261        (empty($infos['infos']) and
262         empty($infos['errors']))
263        ) {
264      return;
265    }
266    global $template;
267    $template->set_filenames(array(
268        'cf_index'    => realpath(cf_get_template('cf_messages_index.tpl')),
269        'cf_title'    => realpath(cf_get_template('cf_title.tpl')),
270        'cf_button'   => realpath(cf_get_template('cf_button.tpl')),
271        'cf_messages' => realpath(cf_get_template('cf_messages.tpl')),
272      ));
273     
274    $template->block_html_head( '',
275              '<link rel="stylesheet" type="text/css" '.
276              'href="' . CF_INCLUDE . 'contactform.css' . '">',
277              $smarty, $repeat);
278    $cf = array(
279        'TITLE'     => 'contact_form_title',
280      );
281    if (!empty($infos['errors'])) {
282      $template->assign('errors', $infos['errors']);
283    }
284    if (!empty($infos['infos'])) {
285      $template->assign('infos', $infos['infos']);
286    }
287    $template->assign('CF', $cf);
288    $template->assign_var_from_handle('CF_TITLE', 'cf_title');
289    $template->assign_var_from_handle('CF_MESSAGES', 'cf_messages');
290    $template->assign_var_from_handle('CF_BUTTON', 'cf_button');
291   
292    $begin = 'PLUGIN_INDEX_CONTENT_BEFORE';
293    $old_begin = $template->get_template_vars($begin);
294    $template->assign_var_from_handle($begin, 'cf_index');
295    $template->concat($begin, $old_begin);
296  }
297 
298  protected function valid_form() {
299    if ($this->check_form_params($infos)) {
300      global $template;
301      if (!$this->send_message($infos)) {
302        // Include language advices
303        load_language('plugin.lang', CF_PATH);
304        $this->display_form($infos);
305      } else {
306        pwg_set_session_var('cf_infos', array(
307            'infos'  => $infos['infos'],
308            'errors' => $infos['errors'],
309          ));
310        redirect(make_index_url());
311        //$this->redirect(make_index_url(),$infos);
312      }
313    } else {
314      $this->display_form($infos);
315    }
316  }
317 
318  protected function get_active_admin_emails() {
319    //$cf_emails = $cf_config->get_value(CF_CFG_ADMIN_MAILS);
320    $all_mails = $this->config->get_value(CF_CFG_ADMIN_MAILS);
321    $active = array('WEBMASTER' => null, 'ADMINS' => array());
322    foreach($all_mails as $email => $values) {
323      if (1 == $values['ACTIVE']) {
324        if (1 == $values['WEBMASTER']) {
325          $active['WEBMASTER'] = $values['EMAILSTR'];
326        } else {
327          array_push($active['ADMINS'], $values['EMAILSTR']);
328        }
329      }
330    }
331
332    if (empty($all_mails)) {
333      $webmaster_email = get_webmaster_mail_address();
334      $active = array(
335        'WEBMASTER' => $webmaster_email,
336        'ADMINS' => cf_get_admins_emails($webmaster_email),
337        );
338    }
339   
340    return $active;
341  }
342 
343  protected function send_message(&$infos) {
344    //redirect(make_index_url());
345//    include(PHPWG_ROOT_PATH . 'include/functions_mail.inc.php');
346
347    $admin_mails = $this->get_active_admin_emails();
348    if ( empty($admin_mails) or 
349        (empty($admin_mails['WEBMASTER']) and 
350         empty($admin_mails['ADMINS']))
351        ) {
352      // No admin mail...
353      array_push( $infos['infos'], l10n('cf_no_mail'));
354      return true;
355    }
356   
357    global $conf,$user;
358    cf_switch_to_default_lang();
359   
360    $from = format_email($infos['cf_from_name'], $infos['cf_from_mail']);
361    $subject_prefix = $this->config->get_value(CF_CFG_SUBJECT_PREFIX);
362    if (empty($subject_prefix)) {
363      $subject_prefix  = '['.CF_DEFAULT_PREFIX.'] ';
364    }
365    $subject  = '['.$subject_prefix.'] ';
366    $subject .= $infos['cf_subject'];
367    $content  = "\n\n".$infos['cf_message']."\n";
368    $content .= CF_SEPARATOR_PATTERN;
369    $mail_args = array(
370        'from' => $from,
371        'Bcc' => $admin_mails['ADMINS'],
372        'subject' => $subject,
373        'content' => $content,
374        'content_format' => 'text/plain',
375      );
376    add_event_handler('send_mail_content',             
377                      array(&$this, 'send_mail_content'));
378
379    $return = true;
380    $return = @pwg_mail(
381      $admin_mails['WEBMASTER'],
382      $mail_args
383    );
384
385    cf_switch_back_to_user_lang();
386    if (!$return) {
387      array_push( $infos['errors'], l10n('cf_error_sending_mail'));
388    } else {
389      array_push( $infos['infos'], l10n('cf_sending_mail_successful'));
390    }
391    return $return;
392  }
393 
394  protected function check_form_params(&$infos) {
395    // Include language advices
396    load_language('plugin.lang', CF_PATH);
397   
398    $infos = $this->create_infos_array(false);
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      array_push( $infos['errors'], l10n('cf_form_error'));
407    }
408    // From name
409    if (isset($_POST['cf_from_name'])) {
410      $value = trim( stripslashes($_POST['cf_from_name']) );
411      if (strlen($value) > 0) {
412        $infos['cf_from_name'] = $value;
413      } else {
414        array_push( $infos['errors'], l10n('cf_from_name_error'));
415      }
416    } else {
417      array_push( $infos['errors'], l10n('cf_from_name_error'));
418    }
419    // From mail
420    if (isset($_POST['cf_from_mail'])) {
421      $value = trim( stripslashes($_POST['cf_from_mail']) );
422      $return = cf_validate_mail_format($value);
423      if (null == $return) {
424        $infos['cf_from_mail'] = $value;
425      } else {
426        array_push( $infos['errors'], $return);
427      }
428    } else {
429      array_push( $infos['errors'], l10n('cf_mail_format_error'));
430    }
431    // Subject
432    if (isset($_POST['cf_subject'])) {
433      $value = trim( stripslashes($_POST['cf_subject']) );
434      if (strlen($value) > 0) {
435        $infos['cf_subject'] = $value;
436      } else {
437        array_push( $infos['errors'], l10n('cf_subject_error'));
438      }
439    } else {
440      array_push( $infos['errors'], l10n('cf_subject_error'));
441    }
442    // Message
443    if (isset($_POST['cf_message'])) {
444      $value = trim( stripslashes($_POST['cf_message']) );
445      if (strlen($value) > 0) {
446        $infos['cf_message'] = $value;
447      } else {
448        array_push( $infos['errors'], l10n('cf_message_error'));
449      }
450    } else {
451      array_push( $infos['errors'], l10n('cf_message_error'));
452    }
453
454    $infos = trigger_event('check_contactform_params', $infos);
455
456    return empty($infos['errors']);
457  }
458 
459  protected function check_key() {
460    global $conf;
461    $key='';
462    $id=0;
463    if (isset($_POST['cf_key'])) {
464      $key = trim( stripslashes($_POST['cf_key']));
465    }
466    if (isset($_POST['cf_id'])) {
467      $id = trim( stripslashes($_POST['cf_id']));
468    }
469   
470    $key = explode( ':', $key );
471    if ( count($key)!=2
472          or $key[0]>time()-2 // page must have been retrieved more than 2 sec ago
473          or $key[0]<time()-3600 // 60 minutes expiration
474          or hash_hmac(
475                'md5', $key[0].':'.$id, $conf['secret_key']
476              ) != $key[1]
477        )
478    {
479      return false;
480    }
481    return true;
482  }
483 
484  protected function create_infos_array($fill=true) {
485    $infos = array(
486        'cf_id'         => '',
487        'cf_from_name'  => '',
488        'cf_from_mail'  => '',
489        'cf_subject'    => '',
490        'cf_message'    => '',
491        'errors'        => array(),
492        'infos'         => array(),
493      );
494    if ($fill) {
495      global $user;
496      // Include language advices
497      load_language('plugin.lang', CF_PATH);
498     
499      $infos['cf_id'] = rand();
500      $infos['cf_from_name'] = is_a_guest()?'':$user['username'];
501      $infos['cf_from_mail'] = $user['email'];
502
503      $l10n_key = 'title_send_mail';
504      $infos['cf_subject'] = l10n($l10n_key);
505      if ($l10n_key == $infos['cf_subject']) {
506        $infos['cf_subject'] = l10n('A comment on your site');
507      }
508    }
509    return $infos;
510  }
511  protected function check_menu_adding() {
512    return ($this->config->get_value(CF_CFG_MENU_LINK) and
513            $this->check_allowed());
514  }
515  protected function check_allowed() {
516    if (is_a_guest() and !$this->config->get_value(CF_CFG_ALLOW_GUEST)) {
517      // Not allowed
518      return false;
519    }
520    return true;
521  }
522  protected function get_plugin_admin_url() {
523    return get_admin_plugin_menu_link(CF_PATH . 'config.php');
524  }
525}
526?>
Note: See TracBrowser for help on using the repository browser.