[ Index ]
 

Code source de Drupal 5.3

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/modules/contact/ -> contact.module (source)

   1  <?php
   2  // $Id: contact.module,v 1.74.2.1 2007/06/05 07:18:05 drumm Exp $
   3  
   4  /**
   5   * @file
   6   * Enables the use of personal and site-wide contact forms.
   7   */
   8  
   9  /**
  10   * Implementation of hook_help().
  11   */
  12  function contact_help($section) {
  13    switch ($section) {
  14      case 'admin/help#contact':
  15        $output = '<p>'. t('The contact module enables the use of both personal and site-wide contact forms, thereby facilitating easy communication within the community. While personal contact forms allow users to contact each other by e-mail, site-wide forms allow community members to contact the site administration from a central location. Users can specify a subject and message in the contact form, and also request that a copy of the e-mail be sent to their own address.') .'</p>';
  16        $output .= '<p>'. t("Users can activate/deactivate their personal contact forms in their account settings. Upon activation, a contact tab will appear in their user profiles. Privileged users such as site administrators are able to contact users even if they have chosen not to enable this feature.") .'</p>';
  17        $output .= '<p>'. t("Note that the contact tab will not appear when a user views his or her own profile; only when viewing another user's profile, if that user's contact form is enabled.") .'</p>';
  18        $output .= '<p>'. t('If the menu module is enabled, a menu item linking to the site-wide contact page is added to the navigation block. It is disabled by default, but can be enabled via the <a href="@menu-module">menu management</a> page. Links to the contact page may also be added to the primary and secondary links using the same page.', array('@menu-module' => url('admin/build/menu'))) .'</p>';
  19        $output .= '<p>'. t('For more information, please read the configuration and customization handbook page for the <a href="@contact">contact module</a>.', array('@contact' => url('http://drupal.org/handbook/modules/contact/', NULL, NULL, TRUE))) .'</p>';
  20        return $output;
  21      case 'admin/build/contact':
  22        $output = '<p>'. t('This page lets you setup <a href="@form">your site-wide contact form</a>. To do so, add one or more categories. You can associate different recipients with each category to route e-mails to different people. For example, you can route website feedback to the webmaster and direct product information requests to the sales department. On the <a href="@settings">settings page</a>, you can customize the information shown above the contact form. This can be useful to provide additional contact information such as your postal address and telephone number.', array('@settings' => url('admin/build/contact/settings'), '@form' => url('contact'))) .'</p>';
  23        if (!module_exists('menu')) {
  24          $menu_note = t('The menu item can be customized and configured only once the menu module has been <a href="@modules-page">enabled</a>.', array('@modules-page' => url('admin/settings/modules')));
  25        }
  26        else {
  27          $menu_note = '';
  28        }
  29        $output .= '<p>'. t('The contact module also adds a <a href="@menu-settings">menu item</a> (disabled by default) to the navigation block.', array('@menu-settings' => url('admin/build/menu'))) .' '. $menu_note .'</p>';
  30        return($output);
  31    }
  32  }
  33  
  34  /**
  35   * Implementation of hook_perm
  36   */
  37  function contact_perm() {
  38    return array('access site-wide contact form');
  39  }
  40  /**
  41   * Implementation of hook_menu().
  42   */
  43  function contact_menu($may_cache) {
  44    $items = array();
  45    if ($may_cache) {
  46      $items[] = array('path' => 'admin/build/contact',
  47        'title' => t('Contact form'),
  48        'description' => t('Create a system contact form and set up categories for the form to use.'),
  49        'callback' => 'contact_admin_categories',
  50        'access' => user_access('administer site configuration'),
  51      );
  52      $items[] = array('path' => 'admin/build/contact/list',
  53        'title' => t('List'),
  54        'callback' => 'contact_admin_categories',
  55        'access' => user_access('administer site configuration'),
  56        'type' => MENU_DEFAULT_LOCAL_TASK,
  57      );
  58      $items[] = array('path' => 'admin/build/contact/add',
  59        'title' => t('Add category'),
  60        'callback' => 'drupal_get_form',
  61        'callback arguments' => array('contact_admin_edit'),
  62        'access' => user_access('administer site configuration'),
  63        'type' => MENU_LOCAL_TASK,
  64        'weight' => 1,
  65      );
  66      $items[] = array('path' => 'admin/build/contact/edit',
  67        'title' => t('Edit contact category'),
  68        'callback' => 'drupal_get_form',
  69        'callback arguments' => array('contact_admin_edit'),
  70        'access' => user_access('administer site configuration'),
  71        'type' => MENU_CALLBACK,
  72      );
  73      $items[] = array('path' => 'admin/build/contact/delete',
  74        'title' => t('Delete contact'),
  75        'callback' => 'drupal_get_form',
  76        'callback arguments' => array('contact_admin_delete'),
  77        'access' => user_access('administer site configuration'),
  78        'type' => MENU_CALLBACK,
  79      );
  80      $items[] = array('path' => 'admin/build/contact/settings',
  81        'title' => t('Settings'),
  82        'callback' => 'drupal_get_form',
  83        'callback arguments' => array('contact_admin_settings'),
  84        'access' => user_access('administer site configuration'),
  85        'type' => MENU_LOCAL_TASK,
  86        'weight' => 2,
  87      );
  88      $items[] = array('path' => 'contact',
  89        'title' => t('Contact'),
  90        'callback' => 'contact_site_page',
  91        'access' => user_access('access site-wide contact form'),
  92        'type' => MENU_SUGGESTED_ITEM,
  93      );
  94    }
  95    else {
  96      if (arg(0) == 'user' && is_numeric(arg(1))) {
  97        global $user;
  98        $account = user_load(array('uid' => arg(1)));
  99        if (($user->uid != $account->uid && $account->contact) || user_access('administer users')) {
 100          $items[] = array('path' => 'user/'. arg(1) .'/contact',
 101            'title' => t('Contact'),
 102            'callback' => 'contact_user_page',
 103            'type' => MENU_LOCAL_TASK,
 104            'access' => $user->uid,
 105            'weight' => 2,
 106          );
 107        }
 108      }
 109    }
 110  
 111    return $items;
 112  }
 113  
 114  /**
 115   * Implementation of hook_user().
 116   *
 117   * Allows the user the option of enabling/disabling his personal contact form.
 118   */
 119  function contact_user($type, &$edit, &$user, $category = NULL) {
 120    if ($type == 'form' && $category == 'account') {
 121      $form['contact'] = array('#type' => 'fieldset',
 122        '#title' => t('Contact settings'),
 123        '#weight' => 5,
 124        '#collapsible' => TRUE,
 125      );
 126      $form['contact']['contact'] = array('#type' => 'checkbox',
 127        '#title' => t('Personal contact form'),
 128        '#default_value' => $edit['contact'],
 129        '#description' => t('Allow other users to contact you by e-mail via <a href="@url">your personal contact form</a>. Note that while your e-mail address is not made public to other members of the community, privileged users such as site administrators are able to contact you even if you choose not to enable this feature.', array('@url' => url("user/$user->uid/contact"))),
 130      );
 131      return $form;
 132    }
 133    elseif ($type == 'validate') {
 134      return array('contact' => $edit['contact']);
 135    }
 136    elseif ($type == 'insert') {
 137      $edit['contact'] = variable_get('contact_default_status', 1);
 138    }
 139  }
 140  
 141  /**
 142   * Categories/list tab.
 143   */
 144  function contact_admin_categories() {
 145    $result = db_query('SELECT cid, category, recipients, selected FROM {contact} ORDER BY weight, category');
 146    $rows = array();
 147    while ($category = db_fetch_object($result)) {
 148      $rows[] = array($category->category, $category->recipients, ($category->selected ? t('Yes') : t('No')), l(t('edit'), 'admin/build/contact/edit/'. $category->cid), l(t('delete'), 'admin/build/contact/delete/'. $category->cid));
 149    }
 150    $header = array(t('Category'), t('Recipients'), t('Selected'), array('data' => t('Operations'), 'colspan' => 2));
 151  
 152    return theme('table', $header, $rows);
 153  }
 154  
 155  /**
 156   * Category edit page.
 157   */
 158  function contact_admin_edit($cid = NULL) {
 159    if (arg(3) == "edit" && $cid > 0) {
 160      $edit = db_fetch_array(db_query("SELECT * FROM {contact} WHERE cid = %d", $cid));
 161    }
 162    $form['category'] = array('#type' => 'textfield',
 163      '#title' => t('Category'),
 164      '#maxlength' => 255,
 165      '#default_value' => $edit['category'],
 166      '#description' => t("Example: 'website feedback' or 'product information'."),
 167      '#required' => TRUE,
 168    );
 169    $form['recipients'] = array('#type' => 'textarea',
 170      '#title' => t('Recipients'),
 171      '#default_value' => $edit['recipients'],
 172      '#description' => t("Example: 'webmaster@example.com' or 'sales@example.com,support@example.com'. To specify multiple recipients, separate each e-mail address with a comma."),
 173      '#required' => TRUE,
 174    );
 175    $form['reply'] = array('#type' => 'textarea',
 176      '#title' => t('Auto-reply'),
 177      '#default_value' => $edit['reply'],
 178      '#description' => t('Optional auto-reply. Leave empty if you do not want to send the user an auto-reply message.'),
 179    );
 180    $form['weight'] = array('#type' => 'weight',
 181      '#title' => t('Weight'),
 182      '#default_value' => $edit['weight'],
 183      '#description' => t('When listing categories, those with lighter (smaller) weights get listed before categories with heavier (larger) weights. Categories with equal weights are sorted alphabetically.'),
 184    );
 185    $form['selected'] = array('#type' => 'select',
 186      '#title' => t('Selected'),
 187      '#options' => array('0' => t('No'), '1' => t('Yes')),
 188      '#default_value' => $edit['selected'],
 189      '#description' => t('Set this to <em>Yes</em> if you would like this category to be selected by default.'),
 190    );
 191    $form['cid'] = array('#type' => 'value',
 192      '#value' => $edit['cid'],
 193    );
 194    $form['submit'] = array('#type' => 'submit',
 195      '#value' => t('Submit'),
 196    );
 197  
 198    return $form;
 199  }
 200  
 201  /**
 202   * Validate the contact category edit page form submission.
 203   */
 204  function contact_admin_edit_validate($form_id, $form_values) {
 205    if (empty($form_values['category'])) {
 206      form_set_error('category', t('You must enter a category.'));
 207    }
 208    if (empty($form_values['recipients'])) {
 209      form_set_error('recipients', t('You must enter one or more recipients.'));
 210    }
 211    else {
 212      $recipients = explode(',', $form_values['recipients']);
 213      foreach ($recipients as $recipient) {
 214        if (!valid_email_address(trim($recipient))) {
 215          form_set_error('recipients', t('%recipient is an invalid e-mail address.', array('%recipient' => $recipient)));
 216        }
 217      }
 218    }
 219  }
 220  
 221  /**
 222   * Process the contact category edit page form submission.
 223   */
 224  function contact_admin_edit_submit($form_id, $form_values) {
 225    if ($form_values['selected']) {
 226      // Unselect all other contact categories.
 227      db_query('UPDATE {contact} SET selected = 0');
 228    }
 229    $recipients = explode(',', $form_values['recipients']);
 230    foreach ($recipients as $key=>$recipient) {
 231      // E-mail address validation has already been done in _validate.
 232      $recipients[$key] = trim($recipient);
 233    }
 234    $form_values['recipients'] = implode(',', $recipients);
 235    if (arg(3) == 'add') {
 236      db_query("INSERT INTO {contact} (category, recipients, reply, weight, selected) VALUES ('%s', '%s', '%s', %d, %d)", $form_values['category'], $form_values['recipients'], $form_values['reply'], $form_values['weight'], $form_values['selected']);
 237      drupal_set_message(t('Category %category has been added.', array('%category' => $form_values['category'])));
 238      watchdog('mail', t('Contact form: category %category added.', array('%category' => $form_values['category'])), WATCHDOG_NOTICE, l(t('view'), 'admin/build/contact'));
 239  
 240    }
 241    else {
 242      db_query("UPDATE {contact} SET category = '%s', recipients = '%s', reply = '%s', weight = %d, selected = %d WHERE cid = %d", $form_values['category'], $form_values['recipients'], $form_values['reply'], $form_values['weight'], $form_values['selected'], $form_values['cid']);
 243      drupal_set_message(t('Category %category has been updated.', array('%category' => $form_values['category'])));
 244      watchdog('mail', t('Contact form: category %category updated.', array('%category' => $form_values['category'])), WATCHDOG_NOTICE, l(t('view'), 'admin/build/contact'));
 245    }
 246  
 247    return 'admin/build/contact';
 248  }
 249  
 250  /**
 251   * Category delete page.
 252   */
 253  function contact_admin_delete($cid = NULL) {
 254    if ($info = db_fetch_object(db_query("SELECT category FROM {contact} WHERE cid = %d", $cid))) {
 255      $form['category'] = array('#type' => 'value',
 256        '#value' => $info->category,
 257      );
 258  
 259      return confirm_form($form, t('Are you sure you want to delete %category?', array('%category' => $info->category)), 'admin/build/contact', t('This action cannot be undone.'), t('Delete'), t('Cancel'));
 260    }
 261    else {
 262      drupal_set_message(t('Category not found.'), 'error');
 263      drupal_goto('admin/build/contact');
 264    }
 265  }
 266  
 267  /**
 268   * Process category delete form submission.
 269   */
 270  function contact_admin_delete_submit($form_id, $form_values) {
 271    db_query("DELETE FROM {contact} WHERE cid = %d", arg(4));
 272    drupal_set_message(t('Category %category has been deleted.', array('%category' => $form_values['category'])));
 273    watchdog('mail', t('Contact form: category %category deleted.', array('%category' => $form_values['category'])), WATCHDOG_NOTICE);
 274  
 275    return 'admin/build/contact';
 276  }
 277  
 278  function contact_admin_settings() {
 279    $form['contact_form_information'] = array('#type' => 'textarea',
 280      '#title' => t('Additional information'),
 281      '#default_value' => variable_get('contact_form_information', t('You can leave a message using the contact form below.')),
 282      '#description' => t('Information to show on the <a href="@form">contact page</a>. Can be anything from submission guidelines to your postal address or telephone number.', array('@form' => url('contact'))),
 283    );
 284    $form['contact_hourly_threshold'] = array('#type' => 'select',
 285      '#title' => t('Hourly threshold'),
 286      '#options' => drupal_map_assoc(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40, 50)),
 287      '#default_value' => variable_get('contact_hourly_threshold', 3),
 288      '#description' => t('The maximum number of contact form submissions a user can perform per hour.'),
 289    );
 290    $form['contact_default_status'] = array(
 291      '#type' => 'checkbox',
 292      '#title' => t('Enable personal contact form by default'),
 293      '#default_value' => variable_get('contact_default_status', 1),
 294      '#description' => t('Default status of the personal contact form for new users.'),
 295    );
 296    return system_settings_form($form);
 297  }
 298  
 299  /**
 300   * Personal contact page.
 301   */
 302  function contact_user_page() {
 303    global $user;
 304  
 305    if ($account = user_load(array('uid' => arg(1)))) {
 306      if (!valid_email_address($user->mail)) {
 307        $output = t('You need to provide a valid e-mail address to contact other users. Please update your <a href="@url">user information</a> and try again.', array('@url' => url("user/$user->uid/edit")));
 308      }
 309      else if (!flood_is_allowed('contact', variable_get('contact_hourly_threshold', 3))) {
 310        $output = t('You cannot contact more than %number users per hour. Please try again later.', array('%number' => variable_get('contact_hourly_threshold', 3)));
 311      }
 312      else {
 313        drupal_set_title(check_plain($account->name));
 314        $output = drupal_get_form('contact_mail_user', $account);
 315      }
 316  
 317      return $output;
 318    }
 319    else {
 320      drupal_not_found();
 321    }
 322  }
 323  
 324  function contact_mail_user($recipient) {
 325    global $user;
 326    $form['#token'] = $user->name . $user->mail;
 327    $form['from'] = array('#type' => 'item',
 328      '#title' => t('From'),
 329      '#value' => check_plain($user->name) .' &lt;'. check_plain($user->mail) .'&gt;',
 330    );
 331    $form['to'] = array('#type' => 'item',
 332      '#title' => t('To'),
 333      '#value' => check_plain($recipient->name),
 334    );
 335    $form['subject'] = array('#type' => 'textfield',
 336      '#title' => t('Subject'),
 337      '#maxlength' => 50,
 338      '#required' => TRUE,
 339    );
 340    $form['message'] = array('#type' => 'textarea',
 341      '#title' => t('Message'),
 342      '#rows' => 15,
 343      '#required' => TRUE,
 344    );
 345    $form['copy'] = array('#type' => 'checkbox',
 346      '#title' => t('Send yourself a copy.'),
 347    );
 348    $form['submit'] = array('#type' => 'submit',
 349      '#value' => t('Send e-mail'),
 350    );
 351    return $form;
 352  }
 353  
 354  /**
 355   * Process the personal contact page form submission.
 356   */
 357  function contact_mail_user_submit($form_id, $form_values) {
 358    global $user;
 359  
 360    $account = user_load(array('uid' => arg(1), 'status' => 1));
 361    // Compose the body:
 362    $message[] = "$account->name,";
 363    $message[] = t("!name (!name-url) has sent you a message via your contact form (!form-url) at !site.", array('!name' => $user->name, '!name-url' => url("user/$user->uid", NULL, NULL, TRUE), '!form-url' => url($_GET['q'], NULL, NULL, TRUE), '!site' => variable_get('site_name', 'Drupal')));
 364    $message[] = t("If you don't want to receive such e-mails, you can change your settings at !url.", array('!url' => url("user/$account->uid", NULL, NULL, TRUE)));
 365    $message[] = t('Message:');
 366    $message[] = $form_values['message'];
 367  
 368    // Tidy up the body:
 369    foreach ($message as $key => $value) {
 370      $message[$key] = wordwrap($value);
 371    }
 372  
 373    // Prepare all fields:
 374    $to = $account->mail;
 375    $from = $user->mail;
 376  
 377    // Format the subject:
 378    $subject = '['. variable_get('site_name', 'Drupal') .'] '. $form_values['subject'];
 379  
 380    // Prepare the body:
 381    $body = implode("\n\n", $message);
 382  
 383    // Send the e-mail:
 384    drupal_mail('contact-user-mail', $to, $subject, $body, $from);
 385  
 386    // Send a copy if requested:
 387    if ($form_values['copy']) {
 388      drupal_mail('contact-user-copy', $from, $subject, $body, $from);
 389    }
 390  
 391    // Log the operation:
 392    flood_register_event('contact');
 393    watchdog('mail', t('%name-from sent %name-to an e-mail.', array('%name-from' => $user->name, '%name-to' => $account->name)));
 394  
 395    // Set a status message:
 396    drupal_set_message(t('The message has been sent.'));
 397  
 398    // Jump to the user's profile page:
 399    return "user/$account->uid";
 400  }
 401  
 402  /**
 403   * Site-wide contact page
 404   */
 405  function contact_site_page() {
 406    global $user;
 407  
 408    if (!flood_is_allowed('contact', variable_get('contact_hourly_threshold', 3))) {
 409      $output = t("You cannot send more than %number messages per hour. Please try again later.", array('%number' => variable_get('contact_hourly_threshold', 3)));
 410    }
 411    else {
 412      $output = drupal_get_form('contact_mail_page');
 413    }
 414  
 415    return $output;
 416  }
 417  
 418  function contact_mail_page() {
 419    global $user;
 420  
 421    $result = db_query('SELECT cid, category, selected FROM {contact} ORDER BY weight, category');
 422    while ($category = db_fetch_object($result)) {
 423      $categories[$category->cid] = $category->category;
 424      if ($category->selected) {
 425        $default_category = $category->cid;
 426      }
 427    }
 428  
 429    if (count($categories) > 0) {
 430      $form['#token'] = $user->name . $user->mail;
 431      $form['contact_information'] = array('#value' => filter_xss_admin(variable_get('contact_form_information', t('You can leave a message using the contact form below.'))));
 432      $form['name'] = array('#type' => 'textfield',
 433        '#title' => t('Your name'),
 434        '#maxlength' => 255,
 435        '#default_value' => $user->uid ? $user->name : '',
 436        '#required' => TRUE,
 437      );
 438      $form['mail'] = array('#type' => 'textfield',
 439        '#title' => t('Your e-mail address'),
 440        '#maxlength' => 255,
 441        '#default_value' => $user->uid ? $user->mail : '',
 442        '#required' => TRUE,
 443      );
 444      $form['subject'] = array('#type' => 'textfield',
 445        '#title' => t('Subject'),
 446        '#maxlength' => 255,
 447        '#required' => TRUE,
 448      );
 449      if (count($categories) > 1) {
 450        // If there is more than one category available and no default category has been selected,
 451        // prepend a default placeholder value.
 452        if (!isset($default_category)) {
 453          $categories = array(t('--')) + $categories;
 454        }
 455        $form['cid'] = array('#type' => 'select',
 456          '#title' => t('Category'),
 457          '#default_value' => $default_category,
 458          '#options' => $categories,
 459          '#required' => TRUE,
 460        );
 461      }
 462      else {
 463        // If there is only one category, store its cid.
 464        $category_keys = array_keys($categories);
 465        $form['cid'] = array('#type' => 'value',
 466          '#value' => array_shift($category_keys),
 467        );
 468      }
 469      $form['message'] = array('#type' => 'textarea',
 470        '#title' => t('Message'),
 471        '#required' => TRUE,
 472      );
 473      // We do not allow anonymous users to send themselves a copy
 474      // because it can be abused to spam people.
 475      if ($user->uid) {
 476        $form['copy'] = array('#type' => 'checkbox',
 477          '#title' => t('Send yourself a copy.'),
 478        );
 479      }
 480      $form['submit'] = array('#type' => 'submit',
 481        '#value' => t('Send e-mail'),
 482      );
 483    }
 484    else {
 485      $form['#error'] = array('#value' => t('The contact form has not been configured.'));
 486    }
 487    return $form;
 488  }
 489  
 490  /**
 491   * Validate the site-wide contact page form submission.
 492   */
 493  function contact_mail_page_validate($form_id, $form_values) {
 494    if (!$form_values['cid']) {
 495      form_set_error('category', t('You must select a valid category.'));
 496    }
 497    if (!valid_email_address($form_values['mail'])) {
 498      form_set_error('mail', t('You must enter a valid e-mail address.'));
 499    }
 500  }
 501  
 502  /**
 503   * Process the site-wide contact page form submission.
 504   */
 505  function contact_mail_page_submit($form_id, $form_values) {
 506  
 507    // E-mail address of the sender: as the form field is a text field,
 508    // all instances of \r and \n have been automatically stripped from it.
 509    $from = $form_values['mail'];
 510  
 511    // Compose the body:
 512    $message[] = t("!name sent a message using the contact form at !form.", array('!name' => $form_values['name'], '!form' => url($_GET['q'], NULL, NULL, TRUE)));
 513    $message[] = $form_values['message'];
 514  
 515    // Tidy up the body:
 516    foreach ($message as $key => $value) {
 517      $message[$key] = wordwrap($value);
 518    }
 519  
 520    // Load the category information:
 521    $contact = db_fetch_object(db_query("SELECT * FROM {contact} WHERE cid = %d", $form_values['cid']));
 522  
 523    // Format the category:
 524    $subject = t('[!category] !subject', array('!category' => $contact->category, '!subject' => $form_values['subject']));
 525  
 526    // Prepare the body:
 527    $body = implode("\n\n", $message);
 528  
 529    // Send the e-mail to the recipients:
 530    drupal_mail('contact-page-mail', $contact->recipients, $subject, $body, $from);
 531  
 532    // If the user requests it, send a copy.
 533    if ($form_values['copy']) {
 534      drupal_mail('contact-page-copy', $from, $subject, $body, $from);
 535    }
 536  
 537    // Send an auto-reply if necessary:
 538    if ($contact->reply) {
 539      drupal_mail('contact-page-autoreply', $from, $subject, wordwrap($contact->reply), $contact->recipients);
 540    }
 541  
 542    // Log the operation:
 543    flood_register_event('contact');
 544    watchdog('mail', t('%name-from sent an e-mail regarding %category.', array('%name-from' => $form_values['name'] ." <$from>", '%category' => $contact->category)));
 545  
 546    // Update user:
 547    drupal_set_message(t('Your message has been sent.'));
 548  
 549    // Jump to home page rather than back to contact page to avoid contradictory messages if flood control has been activated.
 550    return('');
 551  }
 552  


Généré le : Fri Nov 30 16:20:15 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics