diff --git a/appinfo/ocsmsapp.php b/appinfo/ocsmsapp.php index fef3576..9932f00 100644 --- a/appinfo/ocsmsapp.php +++ b/appinfo/ocsmsapp.php @@ -123,6 +123,9 @@ class OcSmsApp extends App { self::$contacts = array(); self::$contactsInverted = array(); + // Cache country because of loops + $configuredCountry = $this->c->query('ConfigMapper')->getCountry(); + $cm = $this->c['ContactsManager']; if ($cm == null) { return; @@ -136,11 +139,11 @@ class OcSmsApp extends App { if (is_array($phoneIds)) { $countPhone = count($phoneIds); for ($i=0; $i < $countPhone; $i++) { - $this->pushPhoneNumberToCache($phoneIds[$i], $r["FN"]); + $this->pushPhoneNumberToCache($phoneIds[$i], $r["FN"], $configuredCountry); } } 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; // Inverted contacts if (!isset(self::$contactsInverted[$contactName])) { diff --git a/controller/smscontroller.php b/controller/smscontroller.php index 085aa46..4dcccbc 100644 --- a/controller/smscontroller.php +++ b/controller/smscontroller.php @@ -120,9 +120,12 @@ class SmsController extends Controller { $contacts = array(); $photos = $this->app->getContactPhotos(); + // Cache country because of loops + $configuredCountry = $this->configMapper->getCountry(); + $countPhone = count($phoneList); foreach ($phoneList as $number => $ts) { - $fmtPN = PhoneNumberFormatter::format($number); + $fmtPN = PhoneNumberFormatter::format($configuredCountry, $number); if (isset($contactsSrc[$number])) { $contacts[$number] = $contactsSrc[$number]; } elseif (isset($contactsSrc[$fmtPN])) { @@ -145,7 +148,11 @@ class SmsController extends Controller { $contacts = $this->app->getContacts(); $iContacts = $this->app->getInvertedContacts(); $contactName = ""; - $fmtPN = PhoneNumberFormatter::format($phoneNumber); + + // Cache country because of loops + $configuredCountry = $this->configMapper->getCountry(); + + $fmtPN = PhoneNumberFormatter::format($configuredCountry, $phoneNumber); if (isset($contacts[$fmtPN])) { $contactName = $contacts[$fmtPN]; } @@ -159,7 +166,7 @@ class SmsController extends Controller { foreach($iContacts[$contactName] as $cnumber) { $messages = $messages + $this->smsMapper->getAllMessagesForPhoneNumber($this->userId, $cnumber, $lastDate); $msgCount += $this->smsMapper->countMessagesForPhoneNumber($this->userId, $cnumber); - $phoneNumbers[] = PhoneNumberFormatter::format($cnumber); + $phoneNumbers[] = PhoneNumberFormatter::format($configuredCountry, $cnumber); } } else { @@ -171,7 +178,7 @@ class SmsController extends Controller { $msgCount += $this->smsMapper->countMessagesForPhoneNumber($this->userId, $cnumber); } } - $phoneNumbers[] = PhoneNumberFormatter::format($phoneNumber); + $phoneNumbers[] = PhoneNumberFormatter::format($configuredCountry, $phoneNumber); } // Order by id (date) ksort($messages); diff --git a/db/configmapper.php b/db/configmapper.php index 837d201..126f2ae 100644 --- a/db/configmapper.php +++ b/db/configmapper.php @@ -28,13 +28,13 @@ class ConfigMapper extends Mapper { */ private $crypto; - public function __construct(IDb $api, $user, $crypto){ + public function __construct (IDb $api, $user, $crypto){ parent::__construct($api, 'ocsms_config'); $this->user = $user; $this->crypto = $crypto; } - public function set($key, $value){ + public function set ($key, $value){ $value = $this->crypto->encrypt($value); if($this->hasKey($key, $value)){ $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 { $sql = "SELECT key FROM `*PREFIX*ocsms_config` WHERE `key` = ? AND `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 { - $sql = "SELECT key FROM `*PREFIX*ocsms_config` WHERE `key` = ? AND `user` = ?"; - $result = $this->findEntity($sql, array($key, $this->user)); - foreach ($result as $r) { - return $this->crypto->decrypt($r->getValue()); + $query = \OCP\DB::prepare("SELECT value FROM `*PREFIX*ocsms_config` WHERE `key` = ? AND `user` = ?"); + $result = $query->execute(array($key, $this->user)); + while($row = $result->fetchRow()) { + return $this->crypto->decrypt($row["value"]); } return false; } catch (DoesNotExistException $e){ return false; } } + + /** + * Helpers for different config options + */ + public function getCountry () { return $this->getKey("country"); } }; ?> diff --git a/lib/phonenumberformatter.php b/lib/phonenumberformatter.php index 61c379e..f0cd54f 100644 --- a/lib/phonenumberformatter.php +++ b/lib/phonenumberformatter.php @@ -12,10 +12,10 @@ namespace OCA\OcSms\Lib; +use \OCA\OcSms\Lib\CountryCodes; class PhoneNumberFormatter { - public static function format ($pn) { - $ipnrxp = array( // match international numbers with 1,2,3 digits + public static $intlPhoneNumber_rxp = array( // match international numbers with 1,2,3 digits '#^(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[0|7])#', // +2x @@ -32,7 +32,13 @@ class PhoneNumberFormatter { '#^(00|\+)(8[1|2|3|4|6])#', // +8x '#^(00|\+)(9[6|7|9]\d)#', // +9(6|7|9)x '#^(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 + '#[^\d\+\(\)\[\]\{\}]#', // everything but digit, +, (), [] or {} @@ -53,13 +59,13 @@ class PhoneNumberFormatter { '#(^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); if (preg_match('#^[\d\+\(\[\{].*#',$tpn)) { // start with digit, +, (, [ or { $fpn = preg_replace($ignrxp, $ignrpl, $tpn); // replace everything but digits/+ with '' $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 { $ypn = $tpn; // some SMS_adresses are strings }