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

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

fix bug introduced by Piwigo 2.4.2

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