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

Last change on this file since 3810 was 3810, checked in by Criss, 15 years ago

bug 0001150

Add e-mail address management

File size: 15.9 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
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    return $active;
327  }
328 
329  protected function send_message(&$infos) {
330    //redirect(make_index_url());
331//    include(PHPWG_ROOT_PATH . 'include/functions_mail.inc.php');
332
333    $admin_mails = $this->get_active_admin_emails();
334    if ( empty($admin_mails) or 
335        (empty($admin_mails['WEBMASTER']) and 
336         empty($admin_mails['ADMINS']))
337        ) {
338      // No admin mail...
339      array_push( $infos['infos'], l10n('cf_no_mail'));
340      return true;
341    }
342   
343    global $conf,$user;
344    cf_switch_to_default_lang();
345   
346    $from = format_email($infos['cf_from_name'], $infos['cf_from_mail']);
347    $subject_prefix = $this->config->get_value(CF_CFG_SUBJECT_PREFIX);
348    if (empty($subject_prefix)) {
349      $subject_prefix  = '['.CF_DEFAULT_PREFIX.'] ';
350    }
351    $subject  = '['.$subject_prefix.'] ';
352    $subject .= $infos['cf_subject'];
353    $content  = "\n\n".$infos['cf_message']."\n";
354    $content .= CF_SEPARATOR_PATTERN;
355    $mail_args = array(
356        'from' => $from,
357        'Bcc' => $admin_mails['ADMINS'],
358        'subject' => $subject,
359        'content' => $content,
360        'content_format' => 'text/plain',
361      );
362    add_event_handler('send_mail_content',             
363                      array(&$this, 'send_mail_content'));
364
365    $return = true;
366    $return = @pwg_mail(
367      $admin_mails['WEBMASTER'],
368      $mail_args
369    );
370
371    cf_switch_back_to_user_lang();
372    if (!$return) {
373      array_push( $infos['errors'], l10n('cf_error_sending_mail'));
374    } else {
375      array_push( $infos['infos'], l10n('cf_sending_mail_successful'));
376    }
377    return $return;
378  }
379 
380  protected function check_form_params(&$infos) {
381    // Include language advices
382    load_language('plugin.lang', CF_PATH);
383   
384    $infos = $this->create_infos_array(false);
385    $params_ok = true;
386    $return = '';
387    $value = '';
388    // Key
389    if ($this->check_key()) {
390      $infos['cf_id'] = trim( stripslashes($_POST['cf_id']));
391    } else {
392      $infos['cf_id'] = rand();
393      $params_ok = false;
394      array_push( $infos['errors'], l10n('cf_form_error'));
395    }
396    // From name
397    if (isset($_POST['cf_from_name'])) {
398      $value = trim( stripslashes($_POST['cf_from_name']) );
399      if (strlen($value) > 0) {
400        $infos['cf_from_name'] = $value;
401      } else {
402        array_push( $infos['errors'], l10n('cf_from_name_error'));
403        $params_ok = false;
404      }
405    } else {
406      array_push( $infos['errors'], l10n('cf_from_name_error'));
407      $params_ok = false;
408    }
409    // From mail
410    if (isset($_POST['cf_from_mail'])) {
411      $value = trim( stripslashes($_POST['cf_from_mail']) );
412      $return = cf_validate_mail_format($value);
413      if (null == $return) {
414        $infos['cf_from_mail'] = $value;
415      } else {
416        $params_ok = false;
417        array_push( $infos['errors'], $return);
418      }
419    } else {
420      array_push( $infos['errors'], l10n('cf_mail_format_error'));
421      $params_ok = false;
422    }
423    // Subject
424    if (isset($_POST['cf_subject'])) {
425      $value = trim( stripslashes($_POST['cf_subject']) );
426      if (strlen($value) > 0) {
427        $infos['cf_subject'] = $value;
428      } else {
429        array_push( $infos['errors'], l10n('cf_subject_error'));
430        $params_ok = false;
431      }
432    } else {
433      array_push( $infos['errors'], l10n('cf_subject_error'));
434      $params_ok = false;
435    }
436    // Message
437    if (isset($_POST['cf_message'])) {
438      $value = trim( stripslashes($_POST['cf_message']) );
439      if (strlen($value) > 0) {
440        $infos['cf_message'] = $value;
441      } else {
442        array_push( $infos['errors'], l10n('cf_message_error'));
443        $params_ok = false;
444      }
445    } else {
446      array_push( $infos['errors'], l10n('cf_message_error'));
447      $params_ok = false;
448    }
449    return $params_ok;
450  }
451 
452  protected function check_key() {
453    global $conf;
454    $key='';
455    $id=0;
456    if (isset($_POST['cf_key'])) {
457      $key = trim( stripslashes($_POST['cf_key']));
458    }
459    if (isset($_POST['cf_id'])) {
460      $id = trim( stripslashes($_POST['cf_id']));
461    }
462   
463    $key = explode( ':', $key );
464    if ( count($key)!=2
465          or $key[0]>time()-2 // page must have been retrieved more than 2 sec ago
466          or $key[0]<time()-3600 // 60 minutes expiration
467          or hash_hmac(
468                'md5', $key[0].':'.$id, $conf['secret_key']
469              ) != $key[1]
470        )
471    {
472      return false;
473    }
474    return true;
475  }
476 
477  protected function create_infos_array($fill=true) {
478    $infos = array(
479        'cf_id'         => '',
480        'cf_from_name'  => '',
481        'cf_from_mail'  => '',
482        'cf_subject'    => '',
483        'cf_message'    => '',
484        'errors'        => array(),
485        'infos'         => array(),
486      );
487    if ($fill) {
488      global $user;
489      // Include language advices
490      load_language('plugin.lang', CF_PATH);
491     
492      $infos['cf_id'] = rand();
493      $infos['cf_from_name'] = is_a_guest()?'':$user['username'];
494      $infos['cf_from_mail'] = $user['email'];
495      $infos['cf_subject'] = l10n('title_send_mail');
496    }
497    return $infos;
498  }
499  protected function check_menu_adding() {
500    return ($this->config->get_value(CF_CFG_MENU_LINK) and
501            $this->check_allowed());
502  }
503  protected function check_allowed() {
504    if (is_a_guest() and !$this->config->get_value(CF_CFG_ALLOW_GUEST)) {
505      // Not allowed
506      return false;
507    }
508    return true;
509  }
510  protected function get_plugin_admin_url() {
511    return get_admin_plugin_menu_link(CF_PATH . 'config.php');
512  }
513}
514?>
Note: See TracBrowser for help on using the repository browser.