Some code factorization to be included in manageurs
authorx2000coic <x2000coic>
Thu, 11 Nov 2004 11:46:11 +0000 (11:46 +0000)
committerx2000coic <x2000coic>
Thu, 11 Nov 2004 11:46:11 +0000 (11:46 +0000)
htdocs/webservices/manageurs.php
include/webservices/manageurs.inc.php [new file with mode: 0644]

index 46631a8..74eed9a 100644 (file)
  *  Foundation, Inc.,                                                      *
  *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
  ***************************************************************************
-        $Id: manageurs.php,v 1.3 2004-11-11 11:06:28 x2000coic Exp $
+        $Id: manageurs.php,v 1.4 2004-11-11 11:46:15 x2000coic Exp $
  ***************************************************************************/
 
-require("auto.prepend.inc.php");
+require_once('auto.prepend.inc.php');
+require_once('webservices/manageurs.inc.php');
 
 
-$error_msg = "You didn't provide me with a valid matricule number...";
-
-function get_annuaire_infos($method, $params) { 
-    $cipher_key = "super toto !";
-    if (!empty($params[0])) { 
-        $res = mysql_query("SELECT nom AS nom, epouse AS nom_patro, prenom AS prenom, promo AS prenom, deces=0 AS decede, mobile AS cell FROM auth_user_md5 WHERE matricule = '".addslashes($params[0])."'");
-       if ($array = mysql_fetch_array($res)) {
-           // then it's perfectly fine ! we just have to use a good cypher...
-           $td = mcrypt_module_open('tripledes', '', 'ecb', '');
-           $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
-           mcrypt_generic_init($td, $cipher_key.$params[0], $iv);
-           foreach ( $array as $key => $value ) { 
-               $reply[base64_encode(mcrypt_generic($td, $key))] = base64_encode(mcrypt_generic($td, $value));
-           } 
-           mcrypt_generic_deinit($td);
-           mcrypt_module_close($td);
-       } else {
-            $args = array("faultCode" => 1, "faultString" => $error_msg);
-           $reply = xmlrpc_encode_request(NULL,$args);
-       }
-    } else {
-        $args = array("faultCode" => 1, "faultString" => $error_msg);
-       $reply = xmlrpc_encode_request(NULL,$args);
-    } 
-    return $reply; 
-} 
-
 $server = xmlrpc_server_create();
 
 xmlrpc_server_register_method($server, "getAnnuaireInfos", "get_annuaire_infos");
diff --git a/include/webservices/manageurs.inc.php b/include/webservices/manageurs.inc.php
new file mode 100644 (file)
index 0000000..536ebc9
--- /dev/null
@@ -0,0 +1,102 @@
+<?php
+/***************************************************************************
+ *  Copyright (C) 2003-2004 Polytechnique.org                              *
+ *  http://opensource.polytechnique.org/                                   *
+ *                                                                         *
+ *  This program is free software; you can redistribute it and/or modify   *
+ *  it under the terms of the GNU General Public License as published by   *
+ *  the Free Software Foundation; either version 2 of the License, or      *
+ *  (at your option) any later version.                                    *
+ *                                                                         *
+ *  This program is distributed in the hope that it will be useful,        *
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
+ *  GNU General Public License for more details.                           *
+ *                                                                         *
+ *  You should have received a copy of the GNU General Public License      *
+ *  along with this program; if not, write to the Free Software            *
+ *  Foundation, Inc.,                                                      *
+ *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
+ ***************************************************************************
+        $Id: manageurs.inc.php,v 1.1 2004-11-11 11:46:11 x2000coic Exp $
+ ***************************************************************************/
+
+$error_mat = "You didn't provide me with a valid matricule number...";
+$error_key = "You didn't provide me with a valid cipher key...";
+
+$tripledes = '';
+
+
+function manageurs_encrypt_init($id_assoce){
+  global $tripledes;
+  if(!$tripledes){
+    if(empty($GLOBALS['manageurs_cipher_key'])){
+      return 1;
+    }
+    $tripledes = mcrypt_module_open('tripledes', '', 'ecb', '');
+    $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($tripledes), MCRYPT_RAND);
+    mcrypt_generic_init($tripledes, $GLOBALS['manageurs_cipher_key'].$id_assoce, $iv);
+    return 0;
+  }
+}
+
+function manageurs_encrypt_close(){
+  global $tripledes;
+  if($tripledes){
+    mcrypt_generic_deinit($tripledes);
+    mcrypt_module_close($tripledes);
+  }
+}
+
+function manageurs_encrypt($message){
+  global $tripledes;
+  return base64_encode(mcrypt_generic($tripledes, $message));
+}
+
+function manageurs_decrypt($message){
+  global $tripledes;
+  return mdecrypt_generic($tripledes, base64_decode($message));
+}
+
+function manageurs_encrypt_array($array){
+  foreach($array as $key => $value){
+    $result[manageurs_encrypt($key)] = manageurs_encrypt($value);
+  }
+  return $result;
+}
+
+function manageurs_decrypt_array($array){
+  foreach($array as $key => $value){
+    $result[manageurs_decrypt($key)] = manageurs_decrypt($value);
+  }
+  return $result;
+}
+
+
+function get_annuaire_infos($method, $params) { 
+    if (!empty($params[0])) {
+        $res = mysql_query("SELECT nom AS nom, epouse AS nom_patro, prenom AS prenom, promo AS prenom, deces=0 AS decede, mobile AS cell FROM auth_user_md5 WHERE matricule = '".addslashes($params[0])."'");
+       if ($array = mysql_fetch_array($res)) {
+           // then it's perfectly fine ! we just have to use a good cypher...
+           
+           if(manageurs_encrypt_init($params[0]) == 1){
+             $args = array("faultCode" => 1, "faultString" => $error_key);
+              $reply = xmlrpc_encode_request(NULL,$args);
+           }
+           else{
+             $reply = manageurs_encrypt_array($array);
+
+             manageurs_encrypt_close();
+           }
+       } else {
+            $args = array("faultCode" => 1, "faultString" => $error_mat);
+           $reply = xmlrpc_encode_request(NULL,$args);
+       }
+    } else {
+        $args = array("faultCode" => 1, "faultString" => $error_mat);
+       $reply = xmlrpc_encode_request(NULL,$args);
+    } 
+    return $reply; 
+} 
+
+?>