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

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

bug 1895 fixed: use a default list of target emails when no manual selection available

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