Updates paypal and manageurs interaction files to use with the new profile_address...
authorStéphane Jacob <jacou@melix.net>
Tue, 17 Feb 2009 14:54:14 +0000 (15:54 +0100)
committerStéphane Jacob <jacou@melix.net>
Wed, 18 Feb 2009 18:13:59 +0000 (19:13 +0100)
include/geocoding.inc.php
include/webservices/manageurs.server.inc.php
modules/payment/money/paypal.inc.php

index 816afeb..9538faa 100644 (file)
@@ -52,6 +52,19 @@ abstract class Geocoder {
             }
         }
     }
+
+    // Returns the part of the text preceeding the line with the postal code
+    // and the city name, within the limit of $limit number of lines.
+    static public function getFirstLines($text, $postalCode, $limit)
+    {
+        $textArray  = explode("\n", $text);
+        for ($i = 0; $i < count($textArray); ++$i) {
+            if ($i > $limit || strpos($textLine, $postalCode) !== false) {
+                $limit = $i; break;
+            }
+        }
+        return implode("\n", array_slice($textArray, 0, $limit));
+    }
 }
 
 // Implementation of a Geocoder using the Google Maps API. Please refer to
index 89b1b59..562f402 100644 (file)
@@ -4,28 +4,33 @@ require_once('webservices/manageurs.inc.php');
 
 $error_mat = "You didn't provide me with a valid matricule number...";
 $error_key = "You didn't provide me with a valid cipher key...";
-/**
-  le premier parametre doit etre le matricule
-  le second parametre facultatif doit etre le numero de l'adresse voulue :
-    -1 => on ne veut pas d'adresse
-    0 => on veut toutes les adresses
-    n => on veut l'adresse numero n
-
-    IL NE FAUT PAS CHANGER LES NOMS DES CHAMPS DE ADRESSES
-    S'IL Y A DES MODIFS A FAIRE VOIR AVEC MANAGEURS admin@manageurs.com
-*/
+
+// The first parameter has to be the identification number (matricule).
+// The second parameter facultative has to be the number of the desired address:
+// -1 => we do not want any address;
+// 0  => we want all the addresses;
+// n  => we want the address number n.
+//
+// DO NOT CHANGE THE NAMES OF THE ADDRESSES' FIELDS.
+// IF YOU HAVE TO MAKE SOME MODIFICATION, FIRST CONTACT:
+// admin@manageurs.com
 function get_annuaire_infos($method, $params) {
+    require "geocoding.inc.php";
     global $error_mat, $error_key, $globals;
 
-    //verif du mdp
-    if(!isset($params[0]) || ($params[0] != $globals->manageurs->manageurs_pass)){return false;}
-    //si on a adresse == -1 => on ne recupère aucune adresse
-    if(isset($params[2]) && ($params[2] == -1)) unset($params[2]);
-
-    if( !empty($params[1]) ){ // on verifie qu'on a bien un matricule
+    // Password verification.
+    if(!isset($params[0]) || ($params[0] != $globals->manageurs->manageurs_pass)) {
+        return false;
+    }
+    // If address == -1, we do not retrieve any address.
+    if(isset($params[2]) && ($params[2] == -1)) {
+        unset($params[2]);
+    }
 
-        //on ne recupere pas les adresses inutilement
-        if(!isset($params[2])){
+    // We check we actually have an identification number.
+    if(!empty($params[1])) {
+        // We only retrieve addresses when required.
+        if(!isset($params[2])) {
             $res = XDB::iterRow(
                     "SELECT  ph.display_tel  AS cell, a.naissance AS age
                        FROM  auth_user_md5   AS a
@@ -33,30 +38,29 @@ function get_annuaire_infos($method, $params) {
                   LEFT JOIN  profile_phones  AS ph ON (ph.uid = a.user_id AND link_type='user' AND tel_type = 'mobile')
                       WHERE  a.matricule = {?} LIMIT 1", $params[1]);
             $array = $res->next();
-        }
-        else{
+        } else {
             $res = XDB::iterRow(
-                 "SELECT     a.naissance AS age,
-                             adr.adr1, adr.adr2, adr.adr3,
-                             adr.postcode, adr.city, adr.country,
-                             adr.uid, adr.adrid
-                       FROM  auth_user_md5 AS a
-                 INNER JOIN  auth_user_quick AS q USING (user_id)
-                  LEFT JOIN  adresses AS adr ON(adr.uid = a.user_id)
+                    "SELECT  a.naissance, addr.text, addr.postalCode,
+                             l.name, addr.countryId, addr.pid, addr.id
+                       FROM  auth_user_md5     AS a
+                 INNER JOIN  auth_user_quick   AS q    USING (user_id)
+                  LEFT JOIN  profile_addresses AS addr ON(adr.pid = a.user_id)
+                  LEFT JOIN  geoloc_localities AS l    ON (l.id = addr.localityId)
                       WHERE  a.matricule = {?} AND
                              NOT FIND_IN_SET('pro', adr.statut)
                    ORDER BY  NOT FIND_IN_SET('active', adr.statut),
                              FIND_IN_SET('res-secondaire', adr.statut),
                              NOT FIND_IN_SET('courrier', adr.statut)", $params[1]);
-            //traitement des adresses si necessaire
-            if(list($age, $adr['adr1'], $adr['adr2'], $adr['adr3'], $adr['cp'], $adr['ville'],
-                        $adr['pays'], $uid, $adr['adrid']) = $res->next())
-            {
-                $sql = XDB::query("SELECT display_tel
-                                     FROM profile_phones
-                                    WHERE uid ={?} AND link_type = 'user' AND tel_type = 'mobile'
-                                    LIMIT 1", $uid);
-                if ($sql->numRows()>0) {
+            // Process the addresses we got.
+            if(list($age, $text, $adr['cp'], $adr['ville'],
+                    $adr['pays'], $uid, $adr['adrid']) = $res->next()) {
+                list($adr['adr1'], $adr['adr2'], $adr['adr3']) =
+                    explode("\n", Geocoder::getFirstLines($text, $adr['cp'], 3));
+                $sql = XDB::query("SELECT  display_tel
+                                     FROM  profile_phones
+                                    WHERE  uid = {?} AND link_type = 'user' AND tel_type = 'mobile'
+                                    LIMIT  1", $uid);
+                if ($sql->numRows() > 0) {
                     $array['cell'] = $sql->fetchOneCell();
                 } else {
                     $array['cell'] ='';
@@ -65,86 +69,101 @@ function get_annuaire_infos($method, $params) {
                 $array['adresse'][] = $adr;
 
 
-                //on clamp le numero au nombre d'adresses dispo
+                // We limit the address number by the number of available addresses.
                 $adresse = min((int) $params[2], $res->total());
 
-                if ($adresse != 1) { //on ne veut pas la premiere adresse
+                if ($adresse != 1) { // We don't want the first address.
                     $i = 2;
-                    while(list($age, $adr['adr1'], $adr['adr2'], $adr['adr3'], $adr['cp'], $adr['ville'],
-                                $adr['pays'], , $adr['adrid']) = $res->next())
-                    {
-                        if($adresse == $i){//si on veut cette adresse en particulier
+                    while(list($age, $text, $adr['cp'], $adr['ville'],
+                                $adr['pays'], , $adr['adrid']) = $res->next()) {
+                        list($adr['adr1'], $adr['adr2'], $adr['adr3']) =
+                            explode("\n", Geocoder::getFirstLines($text, $adr['cp'], 3));
+                        if ($adresse == $i) {
+                            // If we want this particular address.
                             $array['adresse'][0] = $adr;
                             //$res->free();
                             break;
-                        }
-                        elseif($adresse == 0){//si on veut toutes les adresses
+                        } elseif ($adresse == 0) {
+                            // If we want every address.
                             $array['adresse'][] = $adr;
                         }
                         $i++;
                     }
                 }
 
-                // on rajoute les numéros de tél
+                // We add the phone numbers.
                 $adrid_index = array();
-                foreach ($array['adresse'] as $i => $a) $adrid_index[$a['adrid']] = $i;
-                // on rajoute les numéros de tels
+                foreach ($array['adresse'] as $i => $a) {
+                    $adrid_index[$a['adrid']] = $i;
+                }
                 $restel = XDB::iterator(
-                    "SELECT t.display_tel AS tel, t.tel_type, t.link_id as adrid
-                       FROM profile_phones AS t
-                 INNER JOIN adresses AS a ON (t.link_id = a.adrid AND t.uid = a.uid)
-                      WHERE t.uid = {?} AND t.link_type = 'address' AND NOT FIND_IN_SET('pro', a.statut)", $uid);
-                while ($tel = $restel->next()) $array['adresse'][$adrid_index[$tel['adrid']]]['tels'][] = $tel;
+                           "SELECT  t.display_tel AS tel, t.tel_type, t.link_id as adrid
+                              FROM  profile_phones    AS t
+                        INNER JOIN  profile_addresses AS a ON (t.link_id = a.id AND t.uid = a.pid)
+                             WHERE  t.uid = {?} AND t.link_type = 'address'
+                                    AND NOT FIND_IN_SET('pro', a.statut)", $uid);
+                while ($tel = $restel->next()) {
+                    $array['adresse'][$adrid_index[$tel['adrid']]]['tels'][] = $tel;
+                }
                 foreach ($array['adresse'] as $i => $adr) {
                     unset($lasttel);
-                    foreach($adr['tels'] as $j => $t){
-                        if (!isset($array['adresse'][$i]['tel']) && (strpos($t['tel_type'], 'Tél') === 0)) $array['adresse'][$i]['tel'] = $t['tel'];
-                        elseif (!isset($array['adresse'][$i]['fax']) && (strpos($t['tel_type'], 'Fax') === 0)) $array['adresse'][$i]['fax'] = $t['tel'];
-                        else $lasttel = $t['tel'];
-                        if (isset($array['adresse'][$i]['tel']) && isset($array['adresse'][$i]['fax'])) break;
+                    foreach ($adr['tels'] as $j => $t) {
+                        if (!isset($array['adresse'][$i]['tel']) && (strpos($t['tel_type'], 'Tél') === 0)) {
+                            $array['adresse'][$i]['tel'] = $t['tel'];
+                        } elseif (!isset($array['adresse'][$i]['fax'])
+                                  && (strpos($t['tel_type'], 'Fax') === 0)) {
+                            $array['adresse'][$i]['fax'] = $t['tel'];
+                        } else {
+                            $lasttel = $t['tel'];
+                        }
+                        if (isset($array['adresse'][$i]['tel']) && isset($array['adresse'][$i]['fax'])) {
+                            break;
+                        }
                     }
-                    if (!isset($array['adresse'][$i]['tel']) && isset($lasttel))
+                    if (!isset($array['adresse'][$i]['tel']) && isset($lasttel)) {
                         $array['adresse'][$i]['tel'] = $lasttel;
-                    elseif (!isset($array['adresse'][$i]['fax']) && isset($lasttel))
+                    } elseif (!isset($array['adresse'][$i]['fax']) && isset($lasttel)) {
                         $array['adresse'][$i]['fax'] = $lasttel;
+                    }
                     unset($array['adresse'][$i]['adrid']);
                     unset($array['adresse'][$i]['tels']);
                 }
-            }
-            else{
+            } else {
                 $array = false;
             }
         }
 
-        if ($array) { // on a bien eu un résultat : le matricule etait bon
+        if ($array) { // We did get a result: the identification number was rigth.
 
-            //on n'envoit que l'age à manageurs le format est YYYY-MM-DD 0123-56-89
-            $year  = (int) substr($array['age'],0,4);
-            $month = (int) substr($array['age'],5,2);
-            $day   = (int) substr($array['age'],8,2);
+            // We only send the age to manageurs.com; the format is YYYY-MM-DD 0123-56-89.
+            $year  = (int) substr($array['age'], 0, 4);
+            $month = (int) substr($array['age'], 5, 2);
+            $day   = (int) substr($array['age'], 8, 2);
             $age   = (int) date('Y') - $year - 1;
             if(( $month < (int)date('m')) ||
-                    (($month == (int)date('m')) && ($day >= (int)date('d'))))
-            {
+                    (($month == (int)date('m')) && ($day >= (int)date('d')))) {
                 $age += 1;
             }
             $array['age'] = $age;
 
-            //on commence le cryptage des donnees
-            if (manageurs_encrypt_init($params[1]) == 1) {//on a pas trouve la cle pour crypter
+            // We start the encryption of the data.
+            if (manageurs_encrypt_init($params[1]) == 1) {
+                // We did not find the key to encryptthe data.
                 $args  = array("erreur" => 3, "erreurstring" => $error_key);
                 $reply = xmlrpc_encode_request(NULL,$args);
             } else {
                 $reply = manageurs_encrypt_array($array);
                 manageurs_encrypt_close();
             }
-        } else {//le matricule n'etait pas valide
+        } else {
+            // The identification number was not valid.
             $args  = array("erreur" => 2, "erreurstring" => $erreur_mat);
             $reply = xmlrpc_encode_request(NULL,$args);
         }
-    } else {//le matricule n'etait pas en argument
+    } else {
+        // The identification number was not in argument.
         $args  = array("erreur" => 1, "erreurstring" => $error_mat);
-        $reply = xmlrpc_encode_request(NULL,$args);
+        $reply = xmlrpc_encode_request(NULL, $args);
     }
 
     return $reply;
@@ -152,35 +171,36 @@ function get_annuaire_infos($method, $params) {
 
 function get_nouveau_infos($method, $params) {
     global $error_mat, $error_key, $globals;
-    //verif du mdp
-    if(!isset($params[0]) || ($params[0] != $globals->manageurs->manageurs_pass)){return false;}
-    if( !empty($params[1]) ){ // on verifie qu'on a bien un matricule
-
+    // Password verification.
+    if(!isset($params[0]) || ($params[0] != $globals->manageurs->manageurs_pass)) {
+        return false;
+    }
+    // We check we actually have an identification number.
+    if(!empty($params[1])) {
         $res = XDB::query(
-            "SELECT  a.nom, a.nom_usage,a.prenom, FIND_IN_SET('femme', a.flags) as femme ,a.deces!= 0 as decede ,
-            a.naissance, a.promo, concat(al.alias, '@m4x.org') as mail
-            FROM  auth_user_md5 AS a
-            INNER JOIN aliases as al ON a.user_id=al.id
-            WHERE al.flags='bestalias' and  a.matricule = {?}",$params[1]);
+                "SELECT  a.nom, a.nom_usage,a.prenom, FIND_IN_SET('femme', a.flags) as femme,
+                         a.deces!= 0 as decede, a.naissance, a.promo, concat(al.alias, '@m4x.org') as mail
+                   FROM  auth_user_md5 AS a
+             INNER JOIN  aliases       AS al ON (a.user_id = al.id)
+                  WHERE  al.flags = 'bestalias' AND a.matricule = {?}",$params[1]);
         $data=$res->fetchOneAssoc();
         //$data['mail'].='@polytechnique.org';
 
 
-        //on commence le cryptage des donnees
-        if (manageurs_encrypt_init($params[1]) == 1) {//on a pas trouve la cle pour crypter
+        // We start the encryption of the data.
+        if (manageurs_encrypt_init($params[1]) == 1) {
+            // We did not find the key to encryptthe data.
             $args  = array("erreur" => 3, "erreurstring" => $error_key);
-            $reply = xmlrpc_encode_request(NULL,$args);
+            $reply = xmlrpc_encode_request(NULL, $args);
         } else {
             $reply = manageurs_encrypt_array($data);
             manageurs_encrypt_close();
         }
 
+    } else {
+        $reply = false;
     }
-    else{
-    $reply=false;
-    }
-        return $reply;
-
+    return $reply;
 }
 
 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
index 13d1796..08d83f4 100644 (file)
@@ -21,8 +21,6 @@
 
 class PayPal
 {
-    // {{{ properties
-
     var $val_number;
     var $urlform;
     var $nomsite = "PayPal";
@@ -30,79 +28,81 @@ class PayPal
 
     var $infos;
 
-    // }}}
-    // {{{ constructor
-
     function PayPal($val)
     {
         $this->val_number = $val;
     }
 
-    // }}}
-    // {{{ function form()
-
     function prepareform(&$pay)
     {
-        // toute la doc sur :
-        // https://www.paypal.com/fr_FR/pdf/integration_guide.pdf
-        // attention : le renvoi automatique ne fonctionne que si
-        // on oblige les gens à créer un compte paypal
-        // nous ne l'utilisons pas ; il faut donc que l'utilisateur
-        // revienne sur le site
+        // Documentation:
+        // https://www.paypal.com/developer
+        // Warning: the automatic return only works if we force the
+        // users to create a paypal account. We do not use it; thus
+        // the user must come back on the site.
         global $globals, $platal;
 
-        $this->urlform = 'https://'.$globals->money->paypal_site.'/cgi-bin/webscr';
+        $this->urlform = 'https://' . $globals->money->paypal_site . '/cgi-bin/webscr';
         $req = XDB::query("SELECT  IF(nom_usage!='', nom_usage, nom) AS nom
                              FROM  auth_user_md5
-                            WHERE  user_id = {?}",S::v('uid'));
+                            WHERE  user_id = {?}", S::v('uid'));
         $name = $req->fetchOneCell();
 
         $roboturl = str_replace("https://","http://",$globals->baseurl)
-            . '/' . $platal->ns . "payment/paypal_return/".S::v('uid')."?comment=".urlencode(Env::v('comment'));
+                  . '/' . $platal->ns . "payment/paypal_return/" . S::v('uid')
+                  . "?comment=" . urlencode(Env::v('comment'));
 
-        $this->infos = Array();
+        $this->infos = array();
 
-        $this->infos['commercant'] = Array(
+        $this->infos['commercant'] = array(
             'business'    => $globals->money->paypal_compte,
-            'rm'        => 2,
+            'rm'          => 2,
             'return'      => $roboturl,
-            'cn'        => 'Commentaires',
+            'cn'          => 'Commentaires',
             'no_shipping' => 1,
             'cbt'         => empty($GLOBALS['IS_XNET_SITE']) ?
-            'Revenir sur polytechnique.org' :
-            'Revenir sur polytechnique.net');
+            'Revenir sur polytechnique.org.' :
+            'Revenir sur polytechnique.net.'
+        );
 
-        $info_client = Array(
+        $info_client = array(
             'first_name' => S::v('prenom'),
             'last_name'  => $name,
-            'email'      => S::user()->bestEmail());
+            'email'      => S::user()->bestEmail()
+        );
 
         $res = XDB::query(
-            "SELECT a.adr1 AS address1, a.adr2 AS address2,
-                    a.city, a.postcode AS zip, a.country,
-                    IF(t1.display_tel != '', t1.display_tel, t2.display_tel) AS night_phone_b
-               FROM auth_user_quick AS q
-          LEFT JOIN adresses  AS a ON (q.user_id = a.uid AND FIND_IN_SET('active', a.statut))
-          LEFT JOIN profile_phones AS t1 ON (t1.uid = a.uid AND t1.link_type = 'address' AND t1.link_id = a.adrid)
-          LEFT JOIN profile_phones AS t2 ON (t2.uid = a.uid AND t2.link_type = 'user' AND t2.link_id = 0)
-              WHERE q.user_id = {?}
-              LIMIT 1", S::v('uid'));
+                "SELECT  a.text, l.name AS city, a.postalCode AS zip, a.countryiId AS country,
+                         IF(t1.display_tel != '', t1.display_tel, t2.display_tel) AS night_phone_b
+                   FROM  auth_user_quick   AS q
+              LEFT JOIN  profile_addresses AS a  ON (q.user_id = a.pid AND FIND_IN_SET('current', a.flags))
+              LEFT JOIN  profile_phones    AS t1 ON (t1.uid = a.uid AND t1.link_type = 'address'
+                                                     AND t1.link_id = a.adrid)
+              LEFT JOIN  profile_phones    AS t2 ON (t2.uid = a.uid AND t2.link_type = 'user'
+                                                     AND t2.link_id = 0)
+              LEFT JOIN  geoloc_localities AS l  ON (l.id = a.localityId)
+                  WHERE  q.user_id = {?}
+                  LIMIT  1",
+                S::v('uid'));
         $this->infos['client'] = array_map('replace_accent', array_merge($info_client, $res->fetchOneAssoc()));
-
-        // on constuit la reference de la transaction
-        $prefix = ($pay->flags->hasflag('unique')) ? str_pad("",15,"0") : rand_url_id();
-        $fullref = substr("$prefix-xorg-{$pay->id}",-15);
-
-        $this->infos['commande'] = Array(
-            'item_name' => replace_accent($pay->text),
-            'amount'  => $this->val_number,
+        list($this->infos['client']['address1'], $this->infos['client']['address2']) =
+            explode("\n", Geocoder::getFirstLines($this->infos['client']['text'],
+                                                  $this->infos['client']['zip'], 2));
+        unset($this->infos['client']['text']);
+
+        // We build the transaction's reference
+        $prefix = ($pay->flags->hasflag('unique')) ? str_pad("", 15, "0") : rand_url_id();
+        $fullref = substr("$prefix-xorg-{$pay->id}", -15);
+
+        $this->infos['commande'] = array(
+            'item_name'     => replace_accent($pay->text),
+            'amount'        => $this->val_number,
             'currency_code' => 'EUR',
-            'custom'  => $fullref);
+            'custom'        => $fullref
+        );
 
-        $this->infos['divers'] = Array('cmd' => '_xclick');
+        $this->infos['divers'] = array('cmd' => '_xclick');
     }
-
-    // }}}
 }
 
 $api = 'PayPal';