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

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

compatibility with Piwigo 2.2 (remove "adviser", get_comment_post_key()
replaced by get_ephemeral_key(), known_script replaced by combine_script
and use of combine_css)

textarea is no more "resizable" (useless and break CSS rules)

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