Fixes
[platal.git] / modules / payment.php
index 30313d0..9de57d1 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2006 Polytechnique.org                              *
+ *  Copyright (C) 2003-2008 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
 
 /* sort en affichant une erreur */
 function cb_erreur($text) {
+    global $globals;
     $mymail = new PlMailer();
-    $mymail->addTo("telepaiement@polytechnique.org");
-    $mymail->setFrom("webmaster@polytechnique.org");
-    $mymail->setSubject("erreur lors d'un télépaiement (CyberPaiement)");
+    $mymail->addTo($globals->money->email);
+    $mymail->setFrom("webmaster@" . $globals->mail->domain);
+    $mymail->setSubject("erreur lors d'un télépaiement (CyberPaiement)");
     $mymail->setTxtBody("\n\n".var_export($_REQUEST,true));
     $mymail->send();
     exit;
 }
 
 /* sort en affichant une erreur */
-function paypal_erreur($text, $send=true) {
-    global $page, $erreur;
+function paypal_erreur($text, $send=true)
+{
+    global $page, $erreur, $globals;
     if ($erreur) return;
     $erreur = $text;
     if (!$send) return;
 
     $mymail = new PlMailer();
-    $mymail->addTo("telepaiement@polytechnique.org");
-    $mymail->setFrom("webmaster@polytechnique.org");
-    $mymail->setSubject("erreur lors d'un télépaiement (PayPal)");
+    $mymail->addTo($globals->money->email);
+    $mymail->setFrom("webmaster@" . $globals->mail->domain);
+    $mymail->setSubject("erreur lors d'un télépaiement (PayPal)");
     $mymail->setTxtBody("\n\n".var_export($_REQUEST,true));
     $mymail->send();
 
-    $page->trig($text);
+    $page->trigError($text);
 }
 
 /* http://fr.wikipedia.org/wiki/Formule_de_Luhn */
@@ -58,7 +60,7 @@ function luhn($nombre) {
     return $sum % 10;
 }
 
-/* calcule la clé d'acceptation a partir de 5 champs */
+/* calcule la clé d'acceptation a partir de 5 champs */
 function cle_accept($d1,$d2,$d3,$d4,$d5)
 {
     $m1 = luhn($d1.$d5);
@@ -70,6 +72,16 @@ function cle_accept($d1,$d2,$d3,$d4,$d5)
     return $alpha{$n-1}.$m1.$m2.$m3.$m4;
 }
 
+/* decode the comment */
+function comment_decode($comment) {
+    $comment = urldecode($comment);
+    if (is_utf8($comment)) {
+        return $comment;
+    } else {
+        return utf8_encode($comment);
+    }
+}
+
 
 class PaymentModule extends PLModule
 {
@@ -84,7 +96,7 @@ class PaymentModule extends PLModule
             '%grp/payment/cyber_return'  => $this->make_hook('cyber_return', AUTH_PUBLIC),
             '%grp/payment/paypal_return' => $this->make_hook('paypal_return', AUTH_PUBLIC),
             'admin/payments'        => $this->make_hook('admin', AUTH_MDP, 'admin'),
-        
+
         );
     }
 
@@ -106,11 +118,9 @@ class PaymentModule extends PLModule
             if (!$res->numRows()) {
                 return PL_FORBIDDEN;
             }
-            new_group_page('payment/index.tpl');
-        } else {
-            $page->changeTpl('payment/index.tpl');
-            $page->assign('xorg_title','Polytechnique.org - Télépaiements');
         }
+        $page->changeTpl('payment/index.tpl');
+        $page->assign('xorg_title','Polytechnique.org - Télépaiements');
 
         // initialisation
         $op   = Env::v('op', 'select');
@@ -118,13 +128,13 @@ class PaymentModule extends PLModule
         $pay  = new Payment($ref);
 
         if($pay->flags->hasflag('old')){
-            $page->trig("La transaction selectionnée est périmée.");
+            $page->trigError("La transaction selectionnée est périmée.");
             $pay = new Payment();
         }
         $val = Env::v('montant') != 0 ? Env::v('montant') : $pay->montant_def;
 
         if (($e = $pay->check($val)) !== true) {
-            $page->trig($e);
+            $page->trigError($e);
         }
 
         if ($op=='submit') {
@@ -154,24 +164,24 @@ class PaymentModule extends PLModule
     function handler_cyber_return(&$page, $uid = null)
     {
         /* reference banque (numero de transaction) */
-        $champ901 = clean_request('CHAMP901');
+        $champ901 = Env::s('CHAMP901');
         /* cle d'acceptation */
-        $champ905 = clean_request('CHAMP905');
+        $champ905 = Env::s('CHAMP905');
         /* code retour */
-        $champ906 = clean_request('CHAMP906');
+        $champ906 = Env::s('CHAMP906');
         /* email renvoye par la banque */
-        $champ104 = clean_request('CHAMP104');
+        $champ104 = Env::s('CHAMP104');
         /* reference complete de la commande */
-        $champ200 = clean_request('CHAMP200');
+        $champ200 = Env::s('CHAMP200');
         /* montant de la transaction */
-        $champ201 = clean_request('CHAMP201');
+        $champ201 = Env::s('CHAMP201');
         /* devise */
-        $champ202 = clean_request('CHAMP202');
+        $champ202 = Env::s('CHAMP202');
         $montant = "$champ201 $champ202";
 
         /* on extrait les informations sur l'utilisateur */
         $res = XDB::query("
-            SELECT  a.prenom,a.nom,a.promo,l.alias,FIND_IN_SET(a.flags,'femme')
+            SELECT  a.prenom,a.nom,a.promo,l.alias,FIND_IN_SET('femme', a.flags)
               FROM  auth_user_md5 AS a
         INNER JOIN  aliases       AS l ON (a.user_id=l.id AND type!='homonyme')
              WHERE  a.user_id={?}", $uid);
@@ -182,7 +192,7 @@ class PaymentModule extends PLModule
 
         /* on extrait la reference de la commande */
         if (!ereg('-xorg-([0-9]+)$',$champ200,$matches)) {
-            cb_erreur("référence de commande invalide");
+            cb_erreur("référence de commande invalide");
         }
 
         echo ($ref = $matches[1]);
@@ -190,7 +200,7 @@ class PaymentModule extends PLModule
                              FROM  paiement.paiements
                             WHERE  id={?}", $ref);
         if (!list($conf_mail,$conf_title,$conf_text) = $res->fetchOneRow()) {
-            cb_erreur("référence de commande inconnue");
+            cb_erreur("référence de commande inconnue");
         }
 
         /* on extrait le code de retour */
@@ -201,7 +211,7 @@ class PaymentModule extends PLModule
                                 WHERE  rcb.id='$champ906'");
             if (list($rcb_text, $c_id, $c_text) = $res->fetchOneRow()) {
                 cb_erreur("erreur lors du paiement : $c_text ($c_id)");
-            } else{ 
+            } else{
                 cb_erreur("erreur inconnue lors du paiement");
             }
         }
@@ -216,21 +226,22 @@ class PaymentModule extends PLModule
         $conf_text = str_replace("<nom>",$nom,$conf_text);
         $conf_text = str_replace("<promo>",$promo,$conf_text);
         $conf_text = str_replace("<montant>",$montant,$conf_text);
-        $conf_text = str_replace("<salutation>",$femme ? "Chère" : "Cher",$conf_text);
-        $conf_text = str_replace("<cher>",$femme ? "Chère" : "Cher",$conf_text);
+        $conf_text = str_replace("<salutation>",$femme ? "Chère" : "Cher",$conf_text);
+        $conf_text = str_replace("<cher>",$femme ? "Chère" : "Cher",$conf_text);
 
+        global $globals;
         $mymail = new PlMailer();
         $mymail->setFrom($conf_mail);
-        $mymail->addTo("\"$prenom $nom\" <$forlife@polytechnique.org>");
+        $mymail->addTo("\"$prenom $nom\" <$forlife@" . $globals->mail->domain . '>');
         $mymail->addCc($conf_mail);
         $mymail->setSubject($conf_title);
-        $mymail->setTxtBody($conf_text);
-        $mymail->send();
+        $mymail->setWikiBody($conf_text);
+        $mymail->send(S::v('mail_fmt') == 'html');
 
-        /* on envoie les details de la transaction à telepaiement@ */
+        /* on envoie les details de la transaction à telepaiement@ */
         $mymail = new PlMailer();
-        $mymail->setFrom("webmaster@polytechnique.org");
-        $mymail->addTo("telepaiement@staff.polytechnique.org");
+        $mymail->setFrom("webmaster@" . $globals->mail->domain);
+        $mymail->addTo($globals->money->email);
         $mymail->setSubject($conf_title);
         $msg = "utilisateur : $prenom $nom ($uid)\n".
                "mail : $forlife@polytechnique.org\n\n".
@@ -246,26 +257,22 @@ class PaymentModule extends PLModule
 
     function handler_paypal_return(&$page, $uid = null)
     {
-        if (!empty($GLOBALS['IS_XNET_SITE'])) {
-            new_group_open_page('payment/retour_paypal.tpl');
-        } else {
-            $page->changeTpl('payment/retour_paypal.tpl');
-        }
+        $page->changeTpl('payment/retour_paypal.tpl');
 
         /* reference banque (numero de transaction) */
-        $no_transaction = clean_request('tx');
+        $no_transaction = Env::s('tx');
         /* token a renvoyer pour avoir plus d'information */
-        $clef = clean_request('sig');
+        $clef = Env::s('sig');
         /* code retour */
-        $status = clean_request('st');
+        $status = Env::s('st');
         /* raison */
-        $reason = ($status == 'Pending')?clean_request('pending_reason'):clean_request('reason_code');
+        $reason = ($status == 'Pending')? Env::s('pending_reason'): Env::s('reason_code');
         /* reference complete de la commande */
-        $fullref = clean_request('cm');
+        $fullref = Env::s('cm');
         /* montant de la transaction */
-        $montant_nb = clean_request('amt');
+        $montant_nb = Env::s('amt');
         /* devise */
-        $montant_dev = clean_request('cc');
+        $montant_dev = Env::s('cc');
         $montant = "$montant_nb $montant_dev";
 
         /* on extrait le code de retour */
@@ -273,12 +280,12 @@ class PaymentModule extends PLModule
             if ($status)
                 paypal_erreur("erreur lors du paiement : $status - $reason");
             else
-                paypal_erreur("Paiement annulé", false);
+                paypal_erreur("Paiement annulé", false);
         }
 
         /* on extrait les informations sur l'utilisateur */
         $res = XDB::query("
-            SELECT  a.prenom,a.nom,a.promo,l.alias,FIND_IN_SET(a.flags,'femme')
+            SELECT  a.prenom,a.nom,a.promo,l.alias,FIND_IN_SET('femme', a.flags)
               FROM  auth_user_md5 AS a
         INNER JOIN  aliases       AS l ON (a.user_id=l.id AND type!='homonyme')
              WHERE  a.user_id={?}", $uid);
@@ -288,7 +295,7 @@ class PaymentModule extends PLModule
 
         /* on extrait la reference de la commande */
         if (!ereg('-xorg-([0-9]+)$',$fullref,$matches)) {
-            paypal_erreur("référence de commande invalide");
+            paypal_erreur("référence de commande invalide");
         }
 
         $ref = $matches[1];
@@ -296,7 +303,7 @@ class PaymentModule extends PLModule
                              FROM  paiement.paiements
                             WHERE  id={?}", $ref);
         if (!list($conf_mail,$conf_title,$conf_text) = $res->fetchOneRow()) {
-            paypal_erreur("référence de commande inconnue");
+            paypal_erreur("référence de commande inconnue");
         }
 
         /* on fait l'insertion en base de donnees */
@@ -309,21 +316,22 @@ class PaymentModule extends PLModule
         $conf_text = str_replace("<nom>",$nom,$conf_text);
         $conf_text = str_replace("<promo>",$promo,$conf_text);
         $conf_text = str_replace("<montant>",$montant,$conf_text);
-        $conf_text = str_replace("<salutation>",$femme ? "Chère" : "Cher",$conf_text);
-        $conf_text = str_replace("<cher>",$femme ? "Chère" : "Cher",$conf_text);
+        $conf_text = str_replace("<salutation>",$femme ? "Chère" : "Cher",$conf_text);
+        $conf_text = str_replace("<cher>",$femme ? "Chère" : "Cher",$conf_text);
 
+        global $globals;
         $mymail = new PlMailer();
         $mymail->setFrom($conf_mail);
-        $mymail->addTo("\"$prenom $nom\" <$forlife@polytechnique.org>");
+        $mymail->addTo("\"$prenom $nom\" <$forlife@" . $globals->mail->domain . '>');
         $mymail->addCc($conf_mail);
         $mymail->setSubject($conf_title);
-        $mymail->setTxtBody($conf_text);
-        $mymail->send();
+        $mymail->setWikiBody($conf_text);
+        $mymail->send(S::v('mail_fmt') == 'html');
 
-        /* on envoie les details de la transaction à telepaiement@ */
+        /* on envoie les details de la transaction à telepaiement@ */
         $mymail = new PlMailer();
-        $mymail->setFrom("webmaster@polytechnique.org");
-        $mymail->addTo("telepaiement@polytechnique.org");
+        $mymail->setFrom("webmaster@" . $globals->mail->domain);
+        $mymail->addTo($globals->money->email);
         $mymail->setSubject($conf_title);
         $msg = "utilisateur : $prenom $nom ($uid)\n".
                "mail : $forlife@polytechnique.org\n\n".
@@ -342,16 +350,31 @@ class PaymentModule extends PLModule
     function handler_xnet_payment(&$page, $pid = null)
     {
         global $globals;
-        
+
+        $perms = S::v('perms');
+        if (!$perms->hasFlag('groupmember')) {
+            if (is_null($pid)) {
+                return PL_FORBIDDEN;
+            }
+            $res = XDB::query("SELECT  1
+                                 FROM  groupex.evenements AS e
+                           INNER JOIN  groupex.evenements_participants AS ep ON (ep.eid = e.eid AND uid = {?})
+                                WHERE  e.paiement_id = {?} AND e.asso_id = {?}",
+                              S::i('uid'), $pid, $globals->asso('id'));
+            if ($res->numRows() == 0) {
+                return PL_FORBIDDEN;
+            }
+        }
+
         if (!is_null($pid)) {
             return  $this->handler_payment($page, $pid);
         }
-        new_group_page('payment/xnet.tpl');
-        
+        $page->changeTpl('payment/xnet.tpl');
+
         $res = XDB::query(
                 "SELECT  id, text, url
                    FROM  {$globals->money->mpay_tprefix}paiements
-                  WHERE  asso_id = {?} AND NOT FIND_IN_SET(flags, 'old')
+                  WHERE  asso_id = {?} AND NOT FIND_IN_SET('old', flags)
                ORDER BY  id DESC", $globals->asso('id'));
         $tit = $res->fetchAllAssoc();
         $page->assign('titres', $tit);
@@ -404,10 +427,10 @@ class PaymentModule extends PLModule
                     $sum = 0;
                     foreach ($trans[$pid] as $i => $t) {
                         $sum += strtr(substr($t['montant'], 0, strpos($t['montant'], 'EUR')), ',', '.');
-                        $trans[$pid][$i]['montant'] = str_replace('EUR', '¤', $t['montant']);
+                        $trans[$pid][$i]['montant'] = str_replace('EUR', '', $t['montant']);
                     }
                     $trans[$pid][] = array('nom' => 'somme totale',
-                                           'montant' => strtr($sum, '.', ',').' ¤');
+                                           'montant' => strtr($sum, '.', ',').' ');
             }
             $res = XDB::iterRow("SELECT e.eid, e.short_name, e.intitule, ep.nb, ei.montant, ep.paid
                                    FROM groupex.evenements AS e
@@ -438,27 +461,29 @@ class PaymentModule extends PLModule
                 $event[$pid]['paid'] += trim($p);
             }
         }
+        $page->register_modifier('decode_comment', 'decode_comment');
         $page->assign('trans', $trans);
         $page->assign('event', $event);
     }
-    
+
     function handler_admin(&$page, $action = 'list', $id = null) {
         $page->assign('xorg_title','Polytechnique.org - Administration - Paiements');
-        $page->assign('title', 'Gestion des télépaiements');
+        $page->assign('title', 'Gestion des télépaiements');
         $table_editor = new PLTableEditor('admin/payments','paiement.paiements','id');
         $table_editor->add_join_table('paiement.transactions','ref',true);
         $table_editor->add_sort_field('flags');
         $table_editor->add_sort_field('id', true, true);
-        $table_editor->on_delete("UPDATE paiement.paiements SET flags = 'old' WHERE id = {?}", "Le paiement a été archivé");
-        $table_editor->describe('text','intitulé',true);
+        $table_editor->on_delete("UPDATE paiement.paiements SET flags = 'old' WHERE id = {?}", "Le paiement a été archivé");
+        $table_editor->describe('text','intitulé',true);
         $table_editor->describe('url','site web',false);
-        $table_editor->describe('montant_def','montant par défaut',false);
+        $table_editor->describe('montant_def','montant par défaut',false);
         $table_editor->describe('montant_min','montant minimum',false);
         $table_editor->describe('montant_max','montant maximum',false);
         $table_editor->describe('mail','email contact',true);
         $table_editor->describe('confirmation','message confirmation',false);
         $table_editor->apply($page, $action, $id);
-    }  
+    }
 }
 
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
 ?>