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

Last change on this file since 14428 was 14428, checked in by ddtddt, 12 years ago

[extensions] - ContactForm - add perso text on form

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