1
0
mirror of https://github.com/nerzhul/ocsms.git synced 2025-06-08 00:16:24 +00:00

Fix and use the new setting: country. International mapping can now be okay and will remove duplicates. We need a little more fix"

This commit is contained in:
Loic Blot 2015-01-09 18:02:41 +00:00
parent 74ed1e7527
commit 38d0cea3df
4 changed files with 42 additions and 21 deletions

View File

@ -123,6 +123,9 @@ class OcSmsApp extends App {
self::$contacts = array(); self::$contacts = array();
self::$contactsInverted = array(); self::$contactsInverted = array();
// Cache country because of loops
$configuredCountry = $this->c->query('ConfigMapper')->getCountry();
$cm = $this->c['ContactsManager']; $cm = $this->c['ContactsManager'];
if ($cm == null) { if ($cm == null) {
return; return;
@ -136,11 +139,11 @@ class OcSmsApp extends App {
if (is_array($phoneIds)) { if (is_array($phoneIds)) {
$countPhone = count($phoneIds); $countPhone = count($phoneIds);
for ($i=0; $i < $countPhone; $i++) { for ($i=0; $i < $countPhone; $i++) {
$this->pushPhoneNumberToCache($phoneIds[$i], $r["FN"]); $this->pushPhoneNumberToCache($phoneIds[$i], $r["FN"], $configuredCountry);
} }
} }
else { else {
$this->pushPhoneNumberToCache($phoneIds, $r["FN"]); $this->pushPhoneNumberToCache($phoneIds, $r["FN"], $configuredCountry);
} }
} }
@ -152,9 +155,9 @@ class OcSmsApp extends App {
} }
} }
private function pushPhoneNumberToCache($rawPhone, $contactName) { private function pushPhoneNumberToCache($rawPhone, $contactName, $country) {
$phoneNb = PhoneNumberFormatter::format($rawPhone); $phoneNb = PhoneNumberFormatter::format($country, $rawPhone);
self::$contacts[$phoneNb] = $contactName; self::$contacts[$phoneNb] = $contactName;
// Inverted contacts // Inverted contacts
if (!isset(self::$contactsInverted[$contactName])) { if (!isset(self::$contactsInverted[$contactName])) {

View File

@ -120,9 +120,12 @@ class SmsController extends Controller {
$contacts = array(); $contacts = array();
$photos = $this->app->getContactPhotos(); $photos = $this->app->getContactPhotos();
// Cache country because of loops
$configuredCountry = $this->configMapper->getCountry();
$countPhone = count($phoneList); $countPhone = count($phoneList);
foreach ($phoneList as $number => $ts) { foreach ($phoneList as $number => $ts) {
$fmtPN = PhoneNumberFormatter::format($number); $fmtPN = PhoneNumberFormatter::format($configuredCountry, $number);
if (isset($contactsSrc[$number])) { if (isset($contactsSrc[$number])) {
$contacts[$number] = $contactsSrc[$number]; $contacts[$number] = $contactsSrc[$number];
} elseif (isset($contactsSrc[$fmtPN])) { } elseif (isset($contactsSrc[$fmtPN])) {
@ -145,7 +148,11 @@ class SmsController extends Controller {
$contacts = $this->app->getContacts(); $contacts = $this->app->getContacts();
$iContacts = $this->app->getInvertedContacts(); $iContacts = $this->app->getInvertedContacts();
$contactName = ""; $contactName = "";
$fmtPN = PhoneNumberFormatter::format($phoneNumber);
// Cache country because of loops
$configuredCountry = $this->configMapper->getCountry();
$fmtPN = PhoneNumberFormatter::format($configuredCountry, $phoneNumber);
if (isset($contacts[$fmtPN])) { if (isset($contacts[$fmtPN])) {
$contactName = $contacts[$fmtPN]; $contactName = $contacts[$fmtPN];
} }
@ -159,7 +166,7 @@ class SmsController extends Controller {
foreach($iContacts[$contactName] as $cnumber) { foreach($iContacts[$contactName] as $cnumber) {
$messages = $messages + $this->smsMapper->getAllMessagesForPhoneNumber($this->userId, $cnumber, $lastDate); $messages = $messages + $this->smsMapper->getAllMessagesForPhoneNumber($this->userId, $cnumber, $lastDate);
$msgCount += $this->smsMapper->countMessagesForPhoneNumber($this->userId, $cnumber); $msgCount += $this->smsMapper->countMessagesForPhoneNumber($this->userId, $cnumber);
$phoneNumbers[] = PhoneNumberFormatter::format($cnumber); $phoneNumbers[] = PhoneNumberFormatter::format($configuredCountry, $cnumber);
} }
} }
else { else {
@ -171,7 +178,7 @@ class SmsController extends Controller {
$msgCount += $this->smsMapper->countMessagesForPhoneNumber($this->userId, $cnumber); $msgCount += $this->smsMapper->countMessagesForPhoneNumber($this->userId, $cnumber);
} }
} }
$phoneNumbers[] = PhoneNumberFormatter::format($phoneNumber); $phoneNumbers[] = PhoneNumberFormatter::format($configuredCountry, $phoneNumber);
} }
// Order by id (date) // Order by id (date)
ksort($messages); ksort($messages);

View File

@ -28,13 +28,13 @@ class ConfigMapper extends Mapper {
*/ */
private $crypto; private $crypto;
public function __construct(IDb $api, $user, $crypto){ public function __construct (IDb $api, $user, $crypto){
parent::__construct($api, 'ocsms_config'); parent::__construct($api, 'ocsms_config');
$this->user = $user; $this->user = $user;
$this->crypto = $crypto; $this->crypto = $crypto;
} }
public function set($key, $value){ public function set ($key, $value){
$value = $this->crypto->encrypt($value); $value = $this->crypto->encrypt($value);
if($this->hasKey($key, $value)){ if($this->hasKey($key, $value)){
$sql = "UPDATE `*PREFIX*ocsms_config` SET `value` = ? WHERE `user` = ? AND `key` = ?"; $sql = "UPDATE `*PREFIX*ocsms_config` SET `value` = ? WHERE `user` = ? AND `key` = ?";
@ -45,7 +45,7 @@ class ConfigMapper extends Mapper {
} }
} }
public function hasKey($key, $value){ public function hasKey ($key, $value){
try { try {
$sql = "SELECT key FROM `*PREFIX*ocsms_config` WHERE `key` = ? AND `user` = ?"; $sql = "SELECT key FROM `*PREFIX*ocsms_config` WHERE `key` = ? AND `user` = ?";
$this->findEntity($sql, array($key, $this->user)); $this->findEntity($sql, array($key, $this->user));
@ -55,18 +55,23 @@ class ConfigMapper extends Mapper {
} }
} }
public function getKey($key) { public function getKey ($key) {
try { try {
$sql = "SELECT key FROM `*PREFIX*ocsms_config` WHERE `key` = ? AND `user` = ?"; $query = \OCP\DB::prepare("SELECT value FROM `*PREFIX*ocsms_config` WHERE `key` = ? AND `user` = ?");
$result = $this->findEntity($sql, array($key, $this->user)); $result = $query->execute(array($key, $this->user));
foreach ($result as $r) { while($row = $result->fetchRow()) {
return $this->crypto->decrypt($r->getValue()); return $this->crypto->decrypt($row["value"]);
} }
return false; return false;
} catch (DoesNotExistException $e){ } catch (DoesNotExistException $e){
return false; return false;
} }
} }
/**
* Helpers for different config options
*/
public function getCountry () { return $this->getKey("country"); }
}; };
?> ?>

View File

@ -12,10 +12,10 @@
namespace OCA\OcSms\Lib; namespace OCA\OcSms\Lib;
use \OCA\OcSms\Lib\CountryCodes;
class PhoneNumberFormatter { class PhoneNumberFormatter {
public static function format ($pn) { public static $intlPhoneNumber_rxp = array( // match international numbers with 1,2,3 digits
$ipnrxp = array( // match international numbers with 1,2,3 digits
'#^(00|\+)(1\d\d\d)#', // NANP '#^(00|\+)(1\d\d\d)#', // NANP
'#^(00|\+)(2[1|2|3|4|5|6|8|9]\d)#', // +2(1|2|3|4|5|6|8|9)x '#^(00|\+)(2[1|2|3|4|5|6|8|9]\d)#', // +2(1|2|3|4|5|6|8|9)x
'#^(00|\+)(2[0|7])#', // +2x '#^(00|\+)(2[0|7])#', // +2x
@ -34,6 +34,12 @@ class PhoneNumberFormatter {
'#^(00|\+)(9[0|1|2|3|4|5|8])#' // +9x '#^(00|\+)(9[0|1|2|3|4|5|8])#' // +9x
); );
public static function format ($country, $pn) {
// If no country or country not found into mapper, return false
if ($country === false || !array_key_exists($country, CountryCodes::$codes)) {
return $pn;
}
$ignrxp = array( // match non digits and + $ignrxp = array( // match non digits and +
'#[^\d\+\(\)\[\]\{\}]#', // everything but digit, +, (), [] or {} '#[^\d\+\(\)\[\]\{\}]#', // everything but digit, +, (), [] or {}
'#(.+)([\(\[\{]\d*[\)\]\}])#', // braces inside the number: +49 (0) 123 456789 '#(.+)([\(\[\{]\d*[\)\]\}])#', // braces inside the number: +49 (0) 123 456789
@ -53,13 +59,13 @@ class PhoneNumberFormatter {
'#(^0)([^0])#' // in germany : 0-xx[x[x]]-123456 '#(^0)([^0])#' // in germany : 0-xx[x[x]]-123456
); // ); //
$lpnrpl = '+49$2'; // replace with +49 -xx[x[x]]-123456 $lpnrpl = '+'.CountryCodes::$codes[$country].'$2'; // replace with +{countryCode} -xx[x[x]]-123456
$tpn = trim($pn); $tpn = trim($pn);
if (preg_match('#^[\d\+\(\[\{].*#',$tpn)) { // start with digit, +, (, [ or { if (preg_match('#^[\d\+\(\[\{].*#',$tpn)) { // start with digit, +, (, [ or {
$fpn = preg_replace($ignrxp, $ignrpl, $tpn); // replace everything but digits/+ with '' $fpn = preg_replace($ignrxp, $ignrpl, $tpn); // replace everything but digits/+ with ''
$xpn = preg_replace($lpnrxp, $lpnrpl, $fpn); // replace local prenumbers $xpn = preg_replace($lpnrxp, $lpnrpl, $fpn); // replace local prenumbers
$ypn = preg_replace($ipnrxp, '+$2', $xpn); // format to international coding +x[x[x]]..... $ypn = preg_replace(PhoneNumberFormatter::$intlPhoneNumber_rxp, '+$2', $xpn); // format to international coding +x[x[x]].....
} else { } else {
$ypn = $tpn; // some SMS_adresses are strings $ypn = $tpn; // some SMS_adresses are strings
} }