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

Last change on this file since 12925 was 12925, checked in by plg, 12 years ago

compatible with multisite for loading local language files

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