diff --git a/appinfo/formatphonenumber.php b/appinfo/formatphonenumber.php new file mode 100644 index 0000000..876b7ec --- /dev/null +++ b/appinfo/formatphonenumber.php @@ -0,0 +1,56 @@ + + * @copyright Loic Blot 2014 + */ + +namespace OCA\OcSms\AppInfo; + +class FormatPhoneNumber { + + public static function formatPhoneNumber($pn) { + $ipnrxp = array( + '#^(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 + '#^(00|\+)(3[5|7|8]\d)#', // +3(5|7|8)x + '#^(00|\+)(3[0|1|2|3|4|6|9])#', // +3x + '#^(00|\+)(4[2]\d)#', // +4(2)x + '#^(00|\+)(4[0|1|3|4|5|6|7|8|9])#', // +4x + '#^(00|\+)(5[0|9]\d)#', // +5(0|9)x + '#^(00|\+)(5[1|2|3|4|5|6|7|8])#', // +5x + '#^(00|\+)(6[7|8|9]\d)#', // +6(7|8|9)x + '#^(00|\+)(6[0|1|2|3|4|5|6])#', // +6x + '#^(00|\+)(7)#', // +7 + '#^(00|\+)(8[5|7|8|9]\d)#', // +8(5|7|8|9)x + '#^(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 + ); + + $lpnrxp = array( + '#(^0)([^0])#', + ); + $lpnrpl = '+49$2'; + + $ignrxp = array( + '#\(\d*\)|[^\d\+]#', + ); + + $tpn = trim($pn); + $fpn = ''; + $xpn = ''; + if( preg_match('#^[\d\+].*#',$tpn)) { // start with digit or + + $fpn = preg_replace($ignrxp, '', $tpn); // replace with '' + $xpn = preg_replace($lpnrxp, $lpnrpl, $fpn); // + $ypn = preg_replace($ipnrxp, '+$2', $xpn); // + } else + $ypn = $tpn; + return $ypn; + } +} diff --git a/appinfo/ocsmsapp.php b/appinfo/ocsmsapp.php index a7b7cfb..425a0e9 100644 --- a/appinfo/ocsmsapp.php +++ b/appinfo/ocsmsapp.php @@ -9,6 +9,7 @@ * @copyright Loic Blot 2014 */ + namespace OCA\OcSms\AppInfo; @@ -19,15 +20,16 @@ use \OCA\OcSms\Controller\SmsController; use \OCA\OcSms\Db\Sms; use \OCA\OcSms\Db\SmsMapper; +use \OCA\OcSms\AppInfo\FormatPhoneNumber; class OcSmsApp extends App { /** * @var array used to cache the parsed contacts for every request */ - private static $contacts; + private static $contacts; // dosn´t work - private static $contactsInverted; + private static $contactsInverted; // dosn´t work private $c; @@ -80,19 +82,19 @@ class OcSmsApp extends App { public function getContacts() { // Only load contacts if they aren't in the buffer + // dosn´t work if(count(self::$contacts) == 0) { $this->loadContacts(); } - return self::$contacts; } public function getInvertedContacts() { // Only load contacts if they aren't in the buffer + // dosn´t work if(count(self::$contactsInverted) == 0) { $this->loadContacts(); } - return self::$contactsInverted; } @@ -134,73 +136,13 @@ class OcSmsApp extends App { } private function pushPhoneNumberToCache($rawPhone, $contactName) { - // We try to add many combinaisons - $phoneNb = preg_replace("#[ ]#", "/", $rawPhone); - /* - * At this point, spaces are slashes. - */ - - // Spaces removed - $phoneNbNoSpaces = preg_replace("#[/]#", "", $phoneNb); - // Parenthesis removed - $phoneNbNoParenthesis = preg_replace("#[(]|[)]#", "", $phoneNb); - // Dashes removed - $phoneNbNoDashes = preg_replace("#[-]#", "", $phoneNb); - - // Spaces and parenthesis - $phoneNbNoSpacesParenthesis = preg_replace("#[/]|[(]|[)]#", "", $phoneNb); - // Spaces and dashes - $phoneNbNoSpacesDashes = preg_replace("#[/]|[-]#", "", $phoneNb); - // parenthesis and dashes - $phoneNbNoDashesParenthesis = preg_replace("#[-]|[(]|[)]#", "", $phoneNb); - - // Nothing - $phoneNbNothing = preg_replace("#[/]|[(]|[)]|[-]#", "", $phoneNb); - - // Contacts + $phoneNb = FormatPhoneNumber::formatPhoneNumber($rawPhone); self::$contacts[$phoneNb] = $contactName; - self::$contacts[$phoneNbNoSpaces] = $contactName; - self::$contacts[$phoneNbNoParenthesis] = $contactName; - self::$contacts[$phoneNbNoDashes] = $contactName; - self::$contacts[$phoneNbNoSpacesParenthesis] = $contactName; - self::$contacts[$phoneNbNoSpacesDashes] = $contactName; - self::$contacts[$phoneNbNoDashesParenthesis] = $contactName; - self::$contacts[$phoneNbNothing] = $contactName; - // Inverted contacts - if (!isset(self::$contactsInverted[$contactName])) { + if (!isset(self::$contactsInverted[$contactName])) self::$contactsInverted[$contactName] = array(); - } - array_push(self::$contactsInverted[$contactName], $phoneNb); - if (!in_array($phoneNbNoSpaces, self::$contactsInverted[$contactName])) { - array_push(self::$contactsInverted[$contactName], $phoneNbNoSpaces); - } - - if (!in_array($phoneNbNoParenthesis, self::$contactsInverted[$contactName])) { - array_push(self::$contactsInverted[$contactName], $phoneNbNoParenthesis); - } - - if (!in_array($phoneNbNoDashes, self::$contactsInverted[$contactName])) { - array_push(self::$contactsInverted[$contactName], $phoneNbNoDashes); - } - - if (!in_array($phoneNbNoSpacesParenthesis, self::$contactsInverted[$contactName])) { - array_push(self::$contactsInverted[$contactName], $phoneNbNoSpacesParenthesis); - } - - if (!in_array($phoneNbNoSpacesDashes, self::$contactsInverted[$contactName])) { - array_push(self::$contactsInverted[$contactName], $phoneNbNoSpacesDashes); - } - - if (!in_array($phoneNbNoDashesParenthesis, self::$contactsInverted[$contactName])) { - array_push(self::$contactsInverted[$contactName], $phoneNbNoDashesParenthesis); - } - - if (!in_array($phoneNbNothing, self::$contactsInverted[$contactName])) { - array_push(self::$contactsInverted[$contactName], $phoneNbNothing); - } } } diff --git a/controller/smscontroller.php b/controller/smscontroller.php index a1b3322..dc6eadc 100644 --- a/controller/smscontroller.php +++ b/controller/smscontroller.php @@ -18,6 +18,7 @@ use \OCP\AppFramework\Controller; use \OCP\AppFramework\Http\JSONResponse; use \OCA\OcSms\AppInfo\OcSmsApp; use \OCA\OcSms\Db\SmsMapper; +use \OCA\OcSms\AppInfo\FormatPhoneNumber; class SmsController extends Controller { @@ -25,7 +26,7 @@ class SmsController extends Controller { private $userId; private $smsMapper; private $errorMsg; - + public function __construct ($appName, IRequest $request, $userId, SmsMapper $mapper, OcSmsApp $app){ parent::__construct($appName, $request); $this->app = $app; @@ -99,16 +100,18 @@ class SmsController extends Controller { $countPhone = count($phoneList); foreach ($phoneList as $number => $ts) { - $fmtPN = preg_replace("#[ ]#","/", $number); - if (isset($contactsSrc[$fmtPN])) { - $fmtPN2 = preg_replace("#\/#","", $fmtPN); - $contacts[$fmtPN] = $contactsSrc[$fmtPN]; - $contacts[$fmtPN2] = $contactsSrc[$fmtPN]; + $fmtPN = FormatPhoneNumber::formatPhoneNumber($number); + if (isset($contactsSrc[$number])) { + $contacts[$number] = $contactsSrc[$number]; + } elseif (isset($contactsSrc[$fmtPN])) { + $contacts[$number] = $contactsSrc[$fmtPN]; + } elseif (isset($contacts[$fmtPN])) { + $contacts[$number] = $fmtPN; + } else { + $contacts[$number] = $fmtPN; } } - $lastRead = $this->smsMapper->getLastReadDate($this->userId); - return new JSONResponse(array("phonelist" => $phoneList, "contacts" => $contacts, "lastRead" => $lastRead)); } @@ -120,9 +123,7 @@ class SmsController extends Controller { $contacts = $this->app->getContacts(); $iContacts = $this->app->getInvertedContacts(); $contactName = ""; - - // Add slashes to index properly - $fmtPN = preg_replace("#[ ]#","/", $phoneNumber); + $fmtPN = FormatPhoneNumber::formatPhoneNumber($phoneNumber); if (isset($contacts[$fmtPN])) { $contactName = $contacts[$fmtPN]; } @@ -130,45 +131,25 @@ class SmsController extends Controller { $messages = array(); $phoneNumbers = array(); $msgCount = 0; - - // This table will be used to avoid duplicates - $cleanedPhones = array(); - // Contact resolved if ($contactName != "" && isset($iContacts[$contactName])) { - $ctPn = count($iContacts[$contactName]); - - // We merge each message list into global messagelist - for ($i=0; $i < $ctPn; $i++) { - // Remove slashes - $fmtPN = preg_replace("#[/]#"," ", $iContacts[$contactName][$i]); - - $messages = $messages + - $this->smsMapper->getAllMessagesForPhoneNumber($this->userId, $fmtPN, $lastDate); - - $msgCount += $this->smsMapper->countMessagesForPhoneNumber($this->userId, $fmtPN); - - $fmtPNCleaned = preg_replace("#[ ]|[-]|[(]|[)]#","", $fmtPN); - if (!in_array($fmtPNCleaned, $cleanedPhones)) { - $phoneNumbers[] = $fmtPN; - $cleanedPhones[] = $fmtPNCleaned; - } + foreach($iContacts[$contactName] as $cnumber) { + $messages = $messages + $this->smsMapper->getAllMessagesForPhoneNumber($this->userId, $cnumber, $lastDate); + $msgCount += $this->smsMapper->countMessagesForPhoneNumber($this->userId, $cnumber); + $phoneNumbers[] = FormatPhoneNumber::formatPhoneNumber($cnumber); } } else { - // remove slashes - $fmtPN = preg_replace("#[/]#"," ", $phoneNumber); - - $messages = $this->smsMapper->getAllMessagesForPhoneNumber($this->userId, $fmtPN, $lastDate); - $msgCount = $this->smsMapper->countMessagesForPhoneNumber($this->userId, $fmtPN); - - $fmtPNCleaned = preg_replace("#[ ]|-|\(|\)]#","", $fmtPN); - if (!in_array($fmtPNCleaned, $cleanedPhones)) { - $phoneNumbers[] = $fmtPN; - $cleanedPhones[] = $fmtPNCleaned; + $messages = $this->smsMapper->getAllMessagesForPhoneNumber($this->userId, $phoneNumber, $lastDate); + $msgCount = $this->smsMapper->countMessagesForPhoneNumber($this->userId, $phoneNumber); + if(isset($peerNumber[$fmtPN])) { + foreach($peerNumber[$fmtPN] as $cnumber) { + $messages = $messages + $this->smsMapper->getAllMessagesForPhoneNumber($this->userId, $cnumber, $lastDate); + $msgCount += $this->smsMapper->countMessagesForPhoneNumber($this->userId, $cnumber); + } } + $phoneNumbers[] = FormatPhoneNumber::formatPhoneNumber($phoneNumber); } - // Order by id (date) ksort($messages); diff --git a/db/smsmapper.php b/db/smsmapper.php index 16db0f3..dff4021 100644 --- a/db/smsmapper.php +++ b/db/smsmapper.php @@ -14,6 +14,8 @@ namespace OCA\OcSms\Db; use \OCP\IDb; use \OCP\AppFramework\Db\Mapper; +use \OCA\OcSms\AppInfo\OcSmsApp; +use \OCA\OcSms\AppInfo\FormatPhoneNumber; class SmsMapper extends Mapper { /* @@ -77,38 +79,74 @@ class SmsMapper extends Mapper { $phoneList = array(); while($row = $result->fetchRow()) { - if (!in_array($row["sms_address"], $phoneList)) { - array_push($phoneList, $row["sms_address"]); + $pn = $row["sms_address"]; + if (!in_array($pn, $phoneList)) { + array_push($phoneList, $pn); } } return $phoneList; } + + public function getAllPhoneNumbersForFPN ($userId,$phoneNumber) { + $query = \OCP\DB::prepare('SELECT sms_address FROM ' . + '*PREFIX*ocsms_smsdatas WHERE user_id = ? AND sms_mailbox IN (?,?)'); + $result = $query->execute(array($userId, 0, 1)); + $phoneList = array(); + while($row = $result->fetchRow()) { + $pn = $row["sms_address"]; + $fmtPN = FormatPhoneNumber::formatPhoneNumber($pn); + if (!isset($phoneList[$fmtPN])) { + $phoneList[$fmtPN] = array(); + } + //if (!in_array($pn, $phoneList[$fmtPN])) { + if(!isset($phoneList[$fmtPN][$pn])) { + $phoneList[$fmtPN][$pn] = 0; + } + $phoneList[$fmtPN][$pn] += 1; + } + $fpn = FormatPhoneNumber::formatPhoneNumber($phoneNumber); + if(isset($phoneList[$fpn])){ + return $phoneList[$fpn]; + } + else + return array(); + } public function getAllMessagesForPhoneNumber ($userId, $phoneNumber, $minDate = 0) { + + $phlst = $this->getAllPhoneNumbersForFPN ($userId,$phoneNumber); + $messageList = array(); $query = \OCP\DB::prepare('SELECT sms_date, sms_msg, sms_type FROM ' . '*PREFIX*ocsms_smsdatas WHERE user_id = ? AND sms_address = ? ' . 'AND sms_mailbox IN (?,?) AND sms_date > ?'); - $result = $query->execute(array($userId, $phoneNumber, 0, 1, $minDate)); - $messageList = array(); - while ($row = $result->fetchRow()) { - $messageList[$row["sms_date"]] = array( - "msg" => $row["sms_msg"], - "type" => $row["sms_type"] - ); + foreach( $phlst as $pn => $val) { + $result = $query->execute(array($userId, $pn, 0, 1, $minDate)); + + while ($row = $result->fetchRow()) { + $messageList[$row["sms_date"]] = array( + "msg" => $row["sms_msg"], + "type" => $row["sms_type"] + ); + } } return $messageList; } public function countMessagesForPhoneNumber ($userId, $phoneNumber) { + $cnt = 0; + $phlst = $this->getAllPhoneNumbersForFPN ($userId,$phoneNumber); + $query = \OCP\DB::prepare('SELECT count(sms_date) as ct FROM ' . '*PREFIX*ocsms_smsdatas WHERE user_id = ? AND sms_address = ? ' . 'AND sms_mailbox IN (?,?)'); - $result = $query->execute(array($userId, $phoneNumber, 0, 1)); - if ($row = $result->fetchRow()) { - return $row["ct"]; + foreach( $phlst as $pn => $val) { + $result = $query->execute(array($userId, $pn, 0, 1)); + if ($row = $result->fetchRow()) + $cnt += $row["ct"]; } + return $cnt; } public function getLastMessageTimestampForAllPhonesNumbers ($userId, $order = true) { @@ -125,7 +163,9 @@ class SmsMapper extends Mapper { $phoneList = array(); while ($row = $result->fetchRow()) { - $phoneNumber = preg_replace("#[ ]#", "/", $row["sms_address"]); + //$phoneNumber = preg_replace("#[ ]#", "/", $row["sms_address"]); + //$phoneNumber = \OCA\OcSms\AppInfo\FormatPhoneNumber::formatPhoneNumber($row["sms_address"]); + $phoneNumber = $row["sms_address"]; if (!in_array($phoneNumber, $phoneList)) { $phoneList[$phoneNumber] = $row["mx"]; } @@ -201,7 +241,6 @@ class SmsMapper extends Mapper { $userId, (int) $sms["_id"] )); } - $query = \OCP\DB::prepare('INSERT INTO *PREFIX*ocsms_smsdatas ' . '(user_id, added, lastmodified, sms_flags, sms_date, sms_id,' . 'sms_address, sms_msg, sms_mailbox, sms_type) VALUES ' .