From 3bea51e7b8317b9f697c169d44ed0a22e305267a Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sun, 25 Sep 2016 12:29:59 +0200 Subject: [PATCH] Suppression du journal d'appel dans l'upload (API) --- manager/module/call_log.php | 261 ------------------------------------ manager/module/upload.php | 181 +------------------------ 2 files changed, 4 insertions(+), 438 deletions(-) delete mode 100644 manager/module/call_log.php diff --git a/manager/module/call_log.php b/manager/module/call_log.php deleted file mode 100644 index 16bd90b..0000000 --- a/manager/module/call_log.php +++ /dev/null @@ -1,261 +0,0 @@ - Numéro de téléphone du propriétaire du journal d'appel - * - * @return logs Retourne un tableau associatif les logs - * @return directory Retourne un tableau associatif contenant l'annuaire des contacts - * - */ - public static function unserialize($params){ - extract($params); - - - /* [0] On récupère dans la config le chemin du fichier - =========================================================*/ - /* (1) On récupère le fichier */ - $uploadAuth = file_get_contents(__ROOT__.'/config/upload-auth.json'); - - /* (2) Si une erreur pour le fichier de conf */ - if( $uploadAuth === false ) - return ManagerError::UnreachableResource; - - /* (3) On récupère la config sous forme de tableau */ - $uploadAuth = json_decode( $uploadAuth, true ); - - /* (4) Si erreur de PARSAGE */ - if( !is_array($uploadAuth) ) - return ManagerError::ParsingFailed; - - /* (5) On construit le chemin du fichier */ - $prefix = 'call_log'; $extension = 'xml'; - $path = __ROOT__.$uploadAuth['root']."/$prefix/".$_SESSION['username'].".$extension"; - - - - /* [1] On récupère le dictionnaire des valeurs - =========================================================*/ - /* (1) On récupère le fichier */ - $dict = file_get_contents(__ROOT__.'/src/dynamic/dictionary.json'); - - /* (2) Si une erreur pour le fichier de conf */ - if( $dict === false ) - return ManagerError::UnreachableResource; - - /* (3) On récupère la config sous forme de tableau */ - $dict = json_decode( $dict, true ); - - /* (4) Si erreur de PARSAGE */ - if( !is_array($dict) ) - return ManagerError::ParsingFailed; - - - - /* [2] Fonction pour mettre la clé associée dans la dictionnaire - =========================================================*/ - function dParse($dict, $path, $value){ - // {0} On met en forme la clé // - $path = explode(':', $path); - - // {1} Si mauvaise taille, on quitte // - if( count($path) != 2 ) - return $value; - - // {2} Si l'entrée n'existe pas, on retourne la valeur // - if( !isset($dict[$path[0]][$path[1]]) ) - return $value; - - // {3} On charche parmi les valeurs possibles // - foreach($dict[$path[0]][$path[1]] as $k=>$v) - if( $v == $value ) // Si on trouve la valeur, on retourne la clé associée - return $k; - - - // {4} On retourne la valeur tel quel si on ne trouve pas // - return $value; - }; - - /* [3] On parse/récupère le xml - =========================================================*/ - // Si le fichier n'existe pas, on quitte - if( !file_exists($path) ) - return ['ModuleError' => ManagerError::UnreachableResource]; - - $file_content = file_get_contents($path); - - if( $file_content === false ) - return ['ModuleError' => ManagerError::UnreachableResource]; - - $xml = simplexml_load_string( $file_content ); - - // Si le format XML n'est pas bon, on retourne une erreur - if( $xml === false ) - return ['ModuleError' => ManagerError::ParsingFailed]; - - - /* [4] On lit chaque élément - =========================================================*/ - $id = []; // Contiendra les correspondances numéro->id - $phone_directory = []; // Contiendra les données des contacts (par id) - $call_logs = []; // Contiendra nos logs (appels/sms) - - foreach($xml->Item as $log){ - /* (1) On formatte le numéro */ - $number = Database::formatNumber($log['Number']); - - /* (2) On enregistre le contact dans l'annuaire s'il y est pas déjà */ - if( !isset($id[$number]) ){ - $id[$number] = count($id); - $cur_id = $id[$number]; - - $phone_directory[$cur_id] = [ - 'id' => $cur_id, - 'number' => $number, - 'name' => (string) $log['Name'], - 'call' => 0, - 'sms' => 0 - ]; - } - - // On récupère l'id courant - $cur_id = $id[$number]; - - - /* (3) Si c'est un appel, on incrémente le compteur pour ce numéro */ - if( strtolower($log['Type']) == 'phone' ) $phone_directory[$cur_id]['call']++; - - /* (4) Si c'est un sms, on incrémente le compteur pour ce numéro */ - else $phone_directory[$cur_id]['sms']++; - - - /* (5) On complète le log */ - $call_log = [ - 'id' => $cur_id, - 'direction' => dParse($dict, 'logs:direction', $log['Direction']), - 'type' => dParse($dict, 'logs:type', strtoupper($log['Type'])), - 'date' => strtotime( str_replace('T', ' ', $log['Date']) ), - 'duration' => (int) $log['Duration'] - ]; - array_push($call_logs, $call_log); - } - - - /* [5] On trie les contacts par nombre d'apparition d'APPEL - =========================================================*/ - $tmp = $phone_directory; // Permet de ne pas efface $phone_directory - $maxId = -1; // Contiendra l'id du plus gros - $maxVal = null; // Contiendra la valeur max (total calls) - $call_sorted = []; // Contiendra l'annuaire trié par nombre d'interraction - - /* (1) Tant qu'on a pas tout trié */ - while( count($call_sorted) < 10 ){ - $maxId = -1; - $maxVal = null; - - /* (2) On parcours toutes les entrées puor trouver le plus proche */ - foreach($tmp as $cur_id=>$data) - if( $data['call'] > $maxVal || is_null($maxVal) ){ - // On met à jour la valeur max - $maxVal = $data['call']; - // On met à jour l'indice - $maxId = $cur_id; - } - - // Si on a rien trouvé, on arrête - if( $maxId == -1 ) - break; - - /* (3) On supprime le plus proche qu'on a trouvé et on l'ajoute au tableau trié */ - array_push($call_sorted, $maxId); - - unset($tmp[$maxId]); - } - - - /* [6] On trie les contacts par nombre d'apparition de SMS/MMS - =========================================================*/ - $tmp = $phone_directory; // Permet de ne pas efface $phone_directory - $maxId = -1; // Contiendra le numéro du plus gros - $maxVal = null; // Contiendra la valeur max (total calls) - $sms_sorted = []; // Contiendra l'annuaire trié par nombre d'interraction - - /* (1) Tant qu'on a pas tout trié */ - while( count($sms_sorted) < 10 ){ - $maxId = -1; - $maxVal = null; - - /* (2) On parcours toutes les entrées puor trouver le plus proche */ - foreach($tmp as $cur_id=>$data) - if( $data['sms'] > $maxVal || is_null($maxVal) ){ - // On met à jour la valeur max - $maxVal = $data['sms']; - // On met à jour l'indice - $maxId = $cur_id; - } - - // Si on a rien trouvé, on arrête - if( $maxId == -1 ) - break; - - /* (3) On supprime le plus proche qu'on a trouvé et on l'ajoute au tableau trié */ - array_push($sms_sorted, $maxId); - - unset($tmp[$maxId]); - - } - - - /* [7] Enregistrement du journal d'appel au format de sortie - =========================================================*/ - // on cržée un id unique pour cet utilisateur - $subject_id = sessionManager::sha1(uniqid()); - - /* (1) On construit le contenu */ - $phone_storage = [ - 'logs' => $call_logs, // Le journal d'appel - 'subject' => '', // sera complété lors de l'envoi du formulaire (numéro de tel) - 'contacts' => [] // sera complété lors de l'envoi du formulaire - ]; - - /* (2) On met le contenu en json */ - $phone_storage = json_encode($phone_storage); - - /* (3) On l'enregistre dans le fichier */ - $written = file_put_contents( __ROOT__.'/tmp/phone_'.$subject_id.'.json', $phone_storage); - - // Si erreur d'écriture - if( $written === false ) - return [ 'ModuleError' => ManagerError::ModuleError ]; - - - - - /* [8] Gestion du retour - =========================================================*/ - return [ - 'ModuleError' => ManagerError::Success, - 'tmp_id' => $subject_id, - 'directory' => $phone_directory, - 'call' => $call_sorted, - 'sms' => $sms_sorted - ]; - - } - - - - - } - - -?> diff --git a/manager/module/upload.php b/manager/module/upload.php index ba1f8dc..f946519 100644 --- a/manager/module/upload.php +++ b/manager/module/upload.php @@ -136,83 +136,6 @@ - /* IMPORT D'UN JOURNAL D'APPEL - * - * @file Pointeur vers $_FILES[''] - * - */ - public static function call_log($params){ - extract($params); - - /* [1] Gestion de l'upload du fichier et de la vžérification du format - =========================================================*/ - $uploadError = self::simpleFile( - 'call_log', // nom du dossier d'upload - 'xml', // format du fichier - $file, // Fichier lui-même - function($content){ // Vérification du format du fichier - /* (1) Vérification du format XML */ - $xml = simplexml_load_string($content); - if( $xml === false ) return false; // Si erreur de parsage, on retourne une erreur - - /* (2) Vérification du contenu (balises) */ - // Doit avoir des Item(s) - if( !isset($xml->Item) ) - return false; - - // Vérification de tous les champs - foreach($xml->Item as $log){ - $checkAttributes = isset($log['Number']); - $checkAttributes = $checkAttributes && isset($log['Name']); - $checkAttributes = $checkAttributes && isset($log['Date']); - $checkAttributes = $checkAttributes && isset($log['Duration']); - $checkAttributes = $checkAttributes && isset($log['Direction']); - $checkAttributes = $checkAttributes && isset($log['Type']); - - // Si on a pas tout les champs, on retourne une erreur - if( !$checkAttributes ) - return false; - } - - /* (3) Si tout s'est bien passé, le format est bon */ - return true; - } - ); - - - /* [2] Gestion du retour (unserialize) - =========================================================*/ - /* (1) Si erreur d'upload, on la renvoie */ - if( $uploadError != ManagerError::Success ) - return [ 'ModuleError' => $uploadError ]; - - /* (2) Gestion du parsage (unserialize) du journal d'appel */ - $request = new ModuleRequest('call_log/unserialize', [] ); - $response = $request->dispatch(); - - /* (3) Restitution du retour de `unserialize` */ - return array_merge( - [ 'ModuleError' => $response->error ], - $response->getAll() - ); - } - - - - - - - - - - - - - - - - - @@ -262,10 +185,10 @@ foreach($json['contacts'] as $contact){ $checkContact = isset($contact['uid']) && is_numeric($contact['uid']); $checkContact = $checkContact && isset($contact['username']) && is_string($contact['username']); - $checkContact = $checkContact && isset($contact['sms']) && is_numeric($contact['sms']); - $checkContact = $checkContact && isset($contact['call']) && is_numeric($contact['call']); - $checkContact = $checkContact && isset($contact['countsms']) && is_numeric($contact['countsms']); - $checkContact = $checkContact && isset($contact['countcall']) && is_numeric($contact['countcall']); + // $checkContact = $checkContact && isset($contact['sms']) && is_numeric($contact['sms']); + // $checkContact = $checkContact && isset($contact['call']) && is_numeric($contact['call']); + // $checkContact = $checkContact && isset($contact['countsms']) && is_numeric($contact['countsms']); + // $checkContact = $checkContact && isset($contact['countcall']) && is_numeric($contact['countcall']); // Si erreur des attributs du contact incorrects ou manquants if( !$checkContact ) @@ -387,102 +310,6 @@ - - - - - - - - - - - /* CONVERTISSEUR .txt de iExplorer vers .xml de Call Log - * - * @file Pointeur vers $_FILES[''] - * - */ - public static function iexplorer_convert($params){ - extract($params); - - /* [1] Gestion de l'upload du fichier et de la vžérification du format - =========================================================*/ - $uploadError = self::simpleFile( - 'convert_iexplorer',// nom du dossier d'upload - 'txt', // format du fichier - $file, // Fichier lui-même - function($content){ - $lines = explode("\n", $content); - - foreach($lines as $l=>$line) - if( strlen($line) > 0 && !preg_match('/^([\w\(\):\.@-]+(?: {1,3}[\w\(\):\.@-]+)*) +([\w\(\):\.@-]+(?: {1,3}[\w\(\):\.@-]+)*) +([\w\(\):\.@-]+(?: {1,3}[\w\(\):\.@-]+)*) +([\w\(\):\.@-]+(?: {1,3}[\w\(\):\.@-]+)*) *$/u', $line) ) - return false; - - return true; - } - ); - - /* (1) Si erreur d'upload, on la renvoie */ - if( $uploadError != ManagerError::Success ) - return [ 'ModuleError' => $uploadError ]; - - - - /* [2] On récupère les colonnes avec la premiére ligne - =========================================================*/ - /* (1) On met un handler sur le fichier pour le lire */ - $path = __ROOT__."/src/upload/convert_iexplorer/".$_SESSION['username'].".txt"; - - $handler = new \SplFileObject($path, 'r'); - - /* (2) On récupère pour chaque colonne, la position dans le chaine, et la valeur */ - $data = []; - $lineCount = 0; - while( !$handler->eof() ){ - $line = $handler->fgets(); - - /* (1) Si première ligne, c'est les intitulés */ - if( $lineCount == 0 ){ - // Si erreur dans les intitulés, on retourne une erreur - if( !preg_match('/^([\w\(\):\.@-]+(?: [\w\(\):\.@-]+)*) +([\w\(\):\.@-]+(?: [\w\(\):\.@-]+)*) +([\w\(\):\.@-]+(?: [\w\(\):\.@-]+)*) +([\w\(\):\.@-]+(?: [\w\(\):\.@-]+)*) *$/u', $line, $matches) ) - return [ 'ModuleError' => ManagerError::FormatError ]; - - // On récupère les colonnes - $columns = array_slice($matches, 1); - - // On récupére les positions des colonnes - $colpos = []; - foreach($columns as $column) - array_push($colpos, strpos($line, $column)); - - } - - - - /* (2) S'il reconnait pas la ligne, on passe à la suivante */ - if( !preg_match('/^([\w\(\):\.@-]+(?: {1,3}[\w\(\):\.@-]+)*) +([\w\(\):\.@-]+(?: {1,3}[\w\(\):\.@-]+)*) +([\w\(\):\.@-]+(?: {1,3}[\w\(\):\.@-]+)*) +([\w\(\):\.@-]+(?: {1,3}[\w\(\):\.@-]+)*) *$/u', $line, $matches) ) - continue; - - /* (3) On enregistre les données sinon */ - $cur = []; - foreach($columns as $c=>$column) - $cur[$column] = $matches[$c+1]; - - array_push($data, $cur); - - - $lineCount++; - } - - - $handler = null; - - /* (3) Restitution du retour de `unserialize` */ - return [ - 'ModuleError' => ManagerError::Success, - 'data' => $data - ]; - } }