Fix null pointer dereference when sending a mail to validate the subscription to...
[platal.git] / modules / email.php
index 73a22c7..e7f1e28 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2011 Polytechnique.org                              *
+ *  Copyright (C) 2003-2013 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -25,12 +25,12 @@ class EmailModule extends PLModule
     {
         return array(
             'emails'                  => $this->make_hook('emails',      AUTH_COOKIE, 'mail'),
-            'emails/alias'            => $this->make_hook('alias',       AUTH_MDP,    'mail'),
-            'emails/antispam'         => $this->make_hook('antispam',    AUTH_MDP,    'mail'),
-            'emails/broken'           => $this->make_hook('broken',      AUTH_COOKIE),
-            'emails/redirect'         => $this->make_hook('redirect',    AUTH_MDP,    'mail'),
-            'emails/send'             => $this->make_hook('send',        AUTH_MDP,    'mail'),
-            'emails/antispam/submit'  => $this->make_hook('submit',      AUTH_COOKIE),
+            'emails/alias'            => $this->make_hook('alias',       AUTH_PASSWD, 'mail'),
+            'emails/antispam'         => $this->make_hook('antispam',    AUTH_PASSWD, 'mail'),
+            'emails/broken'           => $this->make_hook('broken',      AUTH_COOKIE, 'user'),
+            'emails/redirect'         => $this->make_hook('redirect',    AUTH_PASSWD, 'mail'),
+            'emails/send'             => $this->make_hook('send',        AUTH_PASSWD, 'mail'),
+            'emails/antispam/submit'  => $this->make_hook('submit',      AUTH_COOKIE, 'user'),
             'emails/test'             => $this->make_hook('test',        AUTH_COOKIE, 'mail', NO_AUTH),
 
             'emails/rewrite/in'       => $this->make_hook('rewrite_in',  AUTH_PUBLIC),
@@ -38,10 +38,10 @@ class EmailModule extends PLModule
 
             'emails/imap/in'          => $this->make_hook('imap_in',     AUTH_PUBLIC),
 
-            'admin/emails/duplicated' => $this->make_hook('duplicated',  AUTH_MDP,    'admin'),
-            'admin/emails/watch'      => $this->make_hook('duplicated',  AUTH_MDP,    'admin'),
-            'admin/emails/lost'       => $this->make_hook('lost',        AUTH_MDP,    'admin'),
-            'admin/emails/broken'     => $this->make_hook('broken_addr', AUTH_MDP,    'admin'),
+            'admin/emails/duplicated' => $this->make_hook('duplicated',  AUTH_PASSWD, 'admin'),
+            'admin/emails/watch'      => $this->make_hook('duplicated',  AUTH_PASSWD, 'admin'),
+            'admin/emails/lost'       => $this->make_hook('lost',        AUTH_PASSWD, 'admin'),
+            'admin/emails/broken'     => $this->make_hook('broken_addr', AUTH_PASSWD, 'admin'),
         );
     }
 
@@ -152,7 +152,7 @@ class EmailModule extends PLModule
                                                  INNER JOIN  email_virtual_domains AS d ON (s.domain = d.id)
                                                       WHERE  s.uid = {?} AND s.type = \'alias_aux\'',
                                                     $user->id());
-        $visibility = $user->hasProfile() && ($user->profile(true)->alias_pub == 'public');
+        $visibility = $user->hasProfile() && $user->profile()->isVisible($user->profile()->alias_pub);
         $page->assign('current', $alias);
         $page->assign('user', $user);
         $page->assign('mail_public', $visibility);
@@ -163,20 +163,21 @@ class EmailModule extends PLModule
             // Retrieves user request.
             $new_alias  = Env::v('alias');
             $reason = Env::v('reason');
-            $public = (Env::v('public', 'off') == 'on') ? 'public' : 'private';
+            $public = (Env::v('public', 'off') == 'on') ? 'private' : 'hidden';
 
             $page->assign('r_alias', $new_alias);
             $page->assign('r_reason', $reason);
-            if ($public == 'public') {
+            if ($public == 'private') {
                 $page->assign('r_public', true);
             }
 
             // Checks special charaters in alias.
-            if (!preg_match("/^[a-zA-Z0-9\-.]{3,20}$/", $new_alias)) {
+            if (!preg_match("/^[a-zA-Z0-9\-.]{2,19}[a-zA-Z0-9\-]$/", $new_alias)) {
                 $page->trigError("L'adresse demandée n'est pas valide."
                             . " Vérifie qu'elle comporte entre 3 et 20 caractères"
                             . " et qu'elle ne contient que des lettres non accentuées,"
-                            . " des chiffres ou les caractères - et .");
+                            . " des chiffres ou les caractères '-' et '.'. De plus, elle ne"
+                            . " peut pas se terminer par un point.");
                 return;
             } else {
                 // Checks if the alias has already been given.
@@ -463,7 +464,7 @@ class EmailModule extends PLModule
                     foreach ($files as $name=>&$upload) {
                         $mymail->addUploadAttachment($upload, $name);
                     }
-                    if (Env::v('nowiki')) {
+                    if (Env::v('wiki') == 'text') {
                         $mymail->setTxtBody(wordwrap($txt, 78, "\n"));
                     } else {
                         $mymail->setWikiBody($txt);
@@ -498,6 +499,14 @@ class EmailModule extends PLModule
         $page->assign('contacts', $contacts);
         $page->assign('maxsize', ini_get('upload_max_filesize') . 'o');
         $page->assign('user', S::user());
+        $preferences = XDB::fetchOneAssoc('SELECT  from_email, from_format
+                                             FROM  accounts
+                                            WHERE  uid = {?}',
+                                          S::user()->id());
+        if ($preferences['from_email'] == '') {
+            $preferences['from_email'] = '"' . S::user()->fullName() . '" <' . S::user()->bestEmail() . '>';
+        }
+        $page->assign('preferences', $preferences);
     }
 
     function handler_test($page, $hruid = null)
@@ -652,6 +661,9 @@ class EmailModule extends PLModule
 
             if ($uid) {
                 $dest = User::getWithUID($uid);
+                $active = XDB::fetchOneCell('SELECT flags
+                                               FROM email_redirect_account
+                                              WHERE redirect = {?} AND uid = {?}', $email, $uid) == 'active';
 
                 $mail = new PlMailer('emails/broken-web.mail.tpl');
                 $mail->assign('email', $email);
@@ -879,8 +891,7 @@ class EmailModule extends PLModule
                 $corpsList = DirEnum::getOptions(DirEnum::CURRENTCORPS);
                 foreach ($broken_user_list as $uid => $mails) {
                     $profile = Profile::get($uid);
-                    $corps = $profile->getCorps();
-                    $current_corps = ($corps && $corps->current) ? $corpsList[$corps->current] : '';
+                    $current_corps = $profile->getCorpsName();
                     $jobs = $profile->getJobs();
                     $companies = array();
                     foreach ($jobs as $job) {