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

Last change on this file since 9070 was 9070, checked in by plg, 13 years ago

feature 2180: remove the localization configuration tab

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