From 5eca5d441e427192b449ed30a3543dfae79e2384 Mon Sep 17 00:00:00 2001 From: Daniel Hansson Date: Sun, 2 Nov 2014 03:07:05 +0100 Subject: [PATCH 01/21] Change from Oc Sms ---> SMS Looks cleaner and more like the design. --- appinfo/app.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/appinfo/app.php b/appinfo/app.php index 52fd4a4..4fcf5ed 100644 --- a/appinfo/app.php +++ b/appinfo/app.php @@ -29,5 +29,5 @@ namespace OCA\OcSms\AppInfo; // the title of your application. This will be used in the // navigation or on the settings page of your app - 'name' => \OCP\Util::getL10N('ocsms')->t('Oc Sms') + 'name' => \OCP\Util::getL10N('ocsms')->t('SMS') )); From 63f477e10f0e651a0ece7eb6411d3f235f42dbdc Mon Sep 17 00:00:00 2001 From: Daniel Hansson Date: Sun, 2 Nov 2014 03:47:31 +0100 Subject: [PATCH 02/21] Update README.md Made improvements --- README.md | 45 +++++++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 572b686..c28e4a5 100644 --- a/README.md +++ b/README.md @@ -1,49 +1,58 @@ -# Oc Sms +## ownCloud SMS -## Introduction -Oc Sms provides a webinterface to display your SMS conversations. +### Introduction +ownCloud SMS provides a webinterface to display your SMS conversations. -SMS conversations are pushed by your Android devices using ownCloud Sms app, available on Google Play Store +SMS conversations are pushed by your Android devices using ownCloud SMS app, available on Google Play Store. Android download link: https://play.google.com/store/apps/details?id=fr.unix_experience.owncloud_sms ownCloud SMS Android App sources are partially available here: https://github.com/nerzhul/ownCloud-SMS-App -## Licence -OcSMS web application is currently under AGPL licence but this free licence can be modificated to a better free licence. +### Licence +ownCloud SMS (OcSMS) web application is currently under AGPL licence but this free licence can be modificated to a better free licence. -## Requirements + +### Requirements - An ownCloud instance +- An Andoid phone -## Installation -Place this app in **owncloud/apps/** +### Installation +1. Download the [latest release](https://github.com/nerzhul/ocsms/releases) +2. Place this app in **owncloud/apps/** and unpack it +3. Activate the app in https://your-owncloud/index.php/settings/apps?installed +4. Download the Android client from [Google Play](https://play.google.com/store/apps/details?id=fr.unix_experience.owncloud_sms) +5. Activate the Android client by adding an account in your phone settings -## ocsms app issues - -Please create your issues here: +### Owncloud SMS core issues +*Please create your core issues here:* https://github.com/nerzhul/ocsms/issues -Server + +### Issue template +**Server** - ownCloud version: X.X.X - PHP version: X.X - HTTPd server: - HTTPS: -Client +**Client** - Android version: X.X.X - Phone: - ownCloud SMS app version: X.X.X -Please also include extract of owncloud.log +**Content of /owncloud/data/owncloud.log** +``` +Place content of your owncloud.log here +``` -## Android client issues - -Please create your issues for the Android client here: +### Android client issues +*Please create your issues for the Android client here:* https://github.com/nerzhul/ownCloud-SMS-App/issues From 3fc0aab3469fc8664baa6e5b5f12ae9a86869bbc Mon Sep 17 00:00:00 2001 From: Daniel Hansson Date: Sun, 2 Nov 2014 03:54:28 +0100 Subject: [PATCH 03/21] Changed from Oc SMS to ownCloud SMS ...and updated description. --- appinfo/info.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/appinfo/info.xml b/appinfo/info.xml index 4c1ff67..baa8c40 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -1,8 +1,8 @@ ocsms - Oc Sms - Owncloud SMS app + ownCloud SMS + A app to sync SMS with your ownCloud AGPL Loic Blot 1.2.3 From 66859a49409b37a16d17daad46056af20b4df164 Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Wed, 5 Nov 2014 11:08:59 +0000 Subject: [PATCH 04/21] pushPhoneNumberToCache. This permit to fix problems with phone numbers with dashes and parenthesis --- appinfo/ocsmsapp.php | 58 ++++++++++++++++++++++++++++++++++-- controller/smscontroller.php | 14 ++++----- 2 files changed, 63 insertions(+), 9 deletions(-) diff --git a/appinfo/ocsmsapp.php b/appinfo/ocsmsapp.php index 9cea17e..a7b7cfb 100644 --- a/appinfo/ocsmsapp.php +++ b/appinfo/ocsmsapp.php @@ -134,19 +134,73 @@ class OcSmsApp extends App { } private function pushPhoneNumberToCache($rawPhone, $contactName) { + // We try to add many combinaisons $phoneNb = preg_replace("#[ ]#", "/", $rawPhone); - $phoneNbNoSpaces = 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 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])) { self::$contactsInverted[$contactName] = array(); } + array_push(self::$contactsInverted[$contactName], $phoneNb); - if ($phoneNb != $phoneNbNoSpaces) { + 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 78b8a7d..1bb0f7f 100644 --- a/controller/smscontroller.php +++ b/controller/smscontroller.php @@ -127,7 +127,7 @@ class SmsController extends Controller { $msgCount = 0; // This table will be used to avoid duplicates - $noSpacesPhones = array(); + $cleanedPhones = array(); // Contact resolved if ($contactName != "" && isset($iContacts[$contactName])) { @@ -143,10 +143,10 @@ class SmsController extends Controller { $msgCount += $this->smsMapper->countMessagesForPhoneNumber($this->userId, $fmtPN); - $fmtPNNoSpaces = preg_replace("#[ ]#","", $fmtPN); - if (!in_array($fmtPNNoSpaces, $noSpacesPhones)) { + $fmtPNCleaned = preg_replace("#[ ]|-|\(|\)]#","", $fmtPN); + if (!in_array($fmtPNCleaned, $cleanedPhones)) { $phoneNumbers[] = $fmtPN; - $noSpacesPhones[] = $fmtPNNoSpaces; + $cleanedPhones[] = $fmtPNCleaned; } } } @@ -157,10 +157,10 @@ class SmsController extends Controller { $messages = $this->smsMapper->getAllMessagesForPhoneNumber($this->userId, $fmtPN, $lastDate); $msgCount = $this->smsMapper->countMessagesForPhoneNumber($this->userId, $fmtPN); - $fmtPNNoSpaces = preg_replace("#[ ]#","", $fmtPN); - if (!in_array($fmtPNNoSpaces, $noSpacesPhones)) { + $fmtPNCleaned = preg_replace("#[ ]|-|\(|\)]#","", $fmtPN); + if (!in_array($fmtPNCleaned, $cleanedPhones)) { $phoneNumbers[] = $fmtPN; - $noSpacesPhones[] = $fmtPNNoSpaces; + $cleanedPhones[] = $fmtPNCleaned; } } From e655024521609de9a54490d75f3bec0d5d3c8c38 Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Wed, 5 Nov 2014 11:11:22 +0000 Subject: [PATCH 05/21] Fix regex and then duplicate numbers into conversation --- controller/smscontroller.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/controller/smscontroller.php b/controller/smscontroller.php index 1bb0f7f..87f8a63 100644 --- a/controller/smscontroller.php +++ b/controller/smscontroller.php @@ -143,7 +143,7 @@ class SmsController extends Controller { $msgCount += $this->smsMapper->countMessagesForPhoneNumber($this->userId, $fmtPN); - $fmtPNCleaned = preg_replace("#[ ]|-|\(|\)]#","", $fmtPN); + $fmtPNCleaned = preg_replace("#[ ]|[-]|[(]|[)]#","", $fmtPN); if (!in_array($fmtPNCleaned, $cleanedPhones)) { $phoneNumbers[] = $fmtPN; $cleanedPhones[] = $fmtPNCleaned; From 0ad0164fe5a18223766bf5cc92b7c03e3ebe4b02 Mon Sep 17 00:00:00 2001 From: Ner'zhul Date: Wed, 19 Nov 2014 10:48:36 +0100 Subject: [PATCH 06/21] Fix CSRF problem on push function CSRF is not needed on push because user is not calling other pages. This will fix issues https://github.com/nerzhul/ocsms/issues/25 and https://github.com/nerzhul/ownCloud-SMS-App/issues/8 --- controller/smscontroller.php | 1 + 1 file changed, 1 insertion(+) diff --git a/controller/smscontroller.php b/controller/smscontroller.php index 87f8a63..f2967ea 100644 --- a/controller/smscontroller.php +++ b/controller/smscontroller.php @@ -204,6 +204,7 @@ class SmsController extends Controller { /** * @NoAdminRequired + * @NoCSRFRequired */ public function push ($smsCount, $smsDatas) { if ($this->checkPushStructure($smsCount, $smsDatas, true) === false) { From 7be2d756c94a389de16fa17879543f347e81e73d Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Wed, 19 Nov 2014 10:22:28 +0000 Subject: [PATCH 07/21] Tag 1.3.1 --- appinfo/info.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/appinfo/info.xml b/appinfo/info.xml index 45220af..9184e3f 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -5,6 +5,6 @@ A app to sync SMS with your ownCloud AGPL Loic Blot - 1.3.0 + 1.3.1 7 From 3cd85810eb05f30821a010b7cce62c995669fa82 Mon Sep 17 00:00:00 2001 From: Ner'zhul Date: Wed, 19 Nov 2014 15:40:59 +0100 Subject: [PATCH 08/21] Add comment --- controller/smscontroller.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/controller/smscontroller.php b/controller/smscontroller.php index f2967ea..a1b3322 100644 --- a/controller/smscontroller.php +++ b/controller/smscontroller.php @@ -68,6 +68,11 @@ class SmsController extends Controller { /** * @NoAdminRequired * @NoCSRFRequired + * + * This function is used by API v1 + * Phone will compare its own message list with this + * message list and send the missing messages + * This call will remain as secure slow sync mode (1 per hour) */ public function retrieveAllIds () { $smsList = $this->smsMapper->getAllIds($this->userId); From 4519f0088de4310de490843d6d748c68c9fb064e Mon Sep 17 00:00:00 2001 From: stagprom Date: Mon, 24 Nov 2014 15:54:10 +0100 Subject: [PATCH 09/21] new file: appinfo/formatphonenumber.php modified: appinfo/ocsmsapp.php modified: controller/smscontroller.php modified: db/smsmapper.php A lot of changes in Code dealing with phonenumbers --- appinfo/formatphonenumber.php | 56 ++++++++++++++++++++++++++ appinfo/ocsmsapp.php | 74 ++++------------------------------- controller/smscontroller.php | 67 ++++++++++++------------------- db/smsmapper.php | 67 ++++++++++++++++++++++++------- 4 files changed, 141 insertions(+), 123 deletions(-) create mode 100644 appinfo/formatphonenumber.php 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 ' . From 405ee9af6fddf2f80b92e48d0b55aa907075c23d Mon Sep 17 00:00:00 2001 From: stagprom Date: Tue, 25 Nov 2014 13:28:43 +0100 Subject: [PATCH 10/21] Added many comments to sourcees modified: appinfo/formatphonenumber.php modified: appinfo/ocsmsapp.php modified: controller/smscontroller.php modified: db/smsmapper.php --- appinfo/formatphonenumber.php | 43 +++++++++++++++++++---------------- appinfo/ocsmsapp.php | 7 +++++- controller/smscontroller.php | 1 + db/smsmapper.php | 8 +++---- 4 files changed, 34 insertions(+), 25 deletions(-) diff --git a/appinfo/formatphonenumber.php b/appinfo/formatphonenumber.php index 876b7ec..f64224f 100644 --- a/appinfo/formatphonenumber.php +++ b/appinfo/formatphonenumber.php @@ -14,16 +14,16 @@ namespace OCA\OcSms\AppInfo; class FormatPhoneNumber { public static function formatPhoneNumber($pn) { - $ipnrxp = array( + $ipnrxp = 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 - '#^(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|\+)(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|\+)(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 @@ -31,26 +31,29 @@ class FormatPhoneNumber { '#^(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( // match non digits and + + '#\(\d*\)|[^\d\+]#', + ); - $ignrxp = array( - '#\(\d*\)|[^\d\+]#', - ); + /* + ToDo : make local settings in web-page + */ + $lpnrxp = array( // match local numbers + '#(^0)([^0])#', // in germany : 0-xx[x[x]]-123456 + ); // + $lpnrpl = '+49$2'; // replace with +49 -xx[x[x]]-123456 $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); // + $fpn = preg_replace($ignrxp, '', $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]]..... } else - $ypn = $tpn; + $ypn = $tpn; // some SMS_adresses are strings return $ypn; } } diff --git a/appinfo/ocsmsapp.php b/appinfo/ocsmsapp.php index 425a0e9..633b80d 100644 --- a/appinfo/ocsmsapp.php +++ b/appinfo/ocsmsapp.php @@ -27,6 +27,9 @@ class OcSmsApp extends App { /** * @var array used to cache the parsed contacts for every request */ + /* + caching dosn´t work because on every call all will be reinstantiated + */ private static $contacts; // dosn´t work private static $contactsInverted; // dosn´t work @@ -135,6 +138,9 @@ class OcSmsApp extends App { } } + /* + all numbers will be formatted + */ private function pushPhoneNumberToCache($rawPhone, $contactName) { $phoneNb = FormatPhoneNumber::formatPhoneNumber($rawPhone); @@ -143,6 +149,5 @@ class OcSmsApp extends App { if (!isset(self::$contactsInverted[$contactName])) self::$contactsInverted[$contactName] = array(); array_push(self::$contactsInverted[$contactName], $phoneNb); - } } diff --git a/controller/smscontroller.php b/controller/smscontroller.php index dc6eadc..21039a2 100644 --- a/controller/smscontroller.php +++ b/controller/smscontroller.php @@ -133,6 +133,7 @@ class SmsController extends Controller { $msgCount = 0; // Contact resolved if ($contactName != "" && isset($iContacts[$contactName])) { + // forall numbers in iContacts foreach($iContacts[$contactName] as $cnumber) { $messages = $messages + $this->smsMapper->getAllMessagesForPhoneNumber($this->userId, $cnumber, $lastDate); $msgCount += $this->smsMapper->countMessagesForPhoneNumber($this->userId, $cnumber); diff --git a/db/smsmapper.php b/db/smsmapper.php index dff4021..0a5f7c2 100644 --- a/db/smsmapper.php +++ b/db/smsmapper.php @@ -86,7 +86,10 @@ class SmsMapper extends Mapper { } return $phoneList; } - + + /* + get all possible SMS_adresses for a given formated phonenumber + */ public function getAllPhoneNumbersForFPN ($userId,$phoneNumber) { $query = \OCP\DB::prepare('SELECT sms_address FROM ' . '*PREFIX*ocsms_smsdatas WHERE user_id = ? AND sms_mailbox IN (?,?)'); @@ -98,7 +101,6 @@ class SmsMapper extends Mapper { if (!isset($phoneList[$fmtPN])) { $phoneList[$fmtPN] = array(); } - //if (!in_array($pn, $phoneList[$fmtPN])) { if(!isset($phoneList[$fmtPN][$pn])) { $phoneList[$fmtPN][$pn] = 0; } @@ -163,8 +165,6 @@ class SmsMapper extends Mapper { $phoneList = array(); while ($row = $result->fetchRow()) { - //$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"]; From 6816a92b89e1741cab7713572825e551eba0d174 Mon Sep 17 00:00:00 2001 From: stagprom Date: Tue, 25 Nov 2014 16:55:15 +0100 Subject: [PATCH 11/21] changed tabs and braces modified: appinfo/formatphonenumber.php modified: appinfo/ocsmsapp.php modified: controller/smscontroller.php modified: db/smsmapper.php --- appinfo/formatphonenumber.php | 84 +++++++++++++++++------------------ appinfo/ocsmsapp.php | 19 ++++---- controller/smscontroller.php | 6 +-- db/smsmapper.php | 7 +-- 4 files changed, 58 insertions(+), 58 deletions(-) diff --git a/appinfo/formatphonenumber.php b/appinfo/formatphonenumber.php index f64224f..2a12da4 100644 --- a/appinfo/formatphonenumber.php +++ b/appinfo/formatphonenumber.php @@ -8,52 +8,50 @@ * @author Loic Blot * @copyright Loic Blot 2014 */ - namespace OCA\OcSms\AppInfo; class FormatPhoneNumber { - public static function formatPhoneNumber($pn) { - $ipnrxp = 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 - '#^(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 - ); - - $ignrxp = array( // match non digits and + - '#\(\d*\)|[^\d\+]#', - ); - - /* - ToDo : make local settings in web-page - */ - $lpnrxp = array( // match local numbers - '#(^0)([^0])#', // in germany : 0-xx[x[x]]-123456 - ); // - $lpnrpl = '+49$2'; // replace with +49 -xx[x[x]]-123456 - - $tpn = trim($pn); - $fpn = ''; - $xpn = ''; - if( preg_match('#^[\d\+].*#',$tpn)) { // start with digit or + - $fpn = preg_replace($ignrxp, '', $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]]..... - } else - $ypn = $tpn; // some SMS_adresses are strings - return $ypn; + public static function formatPhoneNumber($pn) { + $ipnrxp = 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 + '#^(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 + ); + + $ignrxp = array( // match non digits and + + '#\(\d*\)|[^\d\+]#', + ); + + /* + ToDo : make local settings in web-page + */ + $lpnrxp = array( // match local numbers + '#(^0)([^0])#', // in germany : 0-xx[x[x]]-123456 + ); // + $lpnrpl = '+49$2'; // replace with +49 -xx[x[x]]-123456 + + $tpn = trim($pn); + if( preg_match('#^[\d\+].*#',$tpn)) { // start with digit or + + $fpn = preg_replace($ignrxp, '', $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]]..... + } else { + $ypn = $tpn; // some SMS_adresses are strings + } + return $ypn; } } diff --git a/appinfo/ocsmsapp.php b/appinfo/ocsmsapp.php index 633b80d..bc42d79 100644 --- a/appinfo/ocsmsapp.php +++ b/appinfo/ocsmsapp.php @@ -30,10 +30,10 @@ class OcSmsApp extends App { /* caching dosn´t work because on every call all will be reinstantiated */ - private static $contacts; // dosn´t work - + private static $contacts; // dosn´t work + private static $contactsInverted; // dosn´t work - + private $c; public function __construct (array $urlParams=array()) { @@ -42,7 +42,7 @@ class OcSmsApp extends App { $container = $this->getContainer(); $this->c = $container; $app = $this; - + /** * Controllers */ @@ -91,7 +91,7 @@ class OcSmsApp extends App { } return self::$contacts; } - + public function getInvertedContacts() { // Only load contacts if they aren't in the buffer // dosn´t work @@ -100,7 +100,7 @@ class OcSmsApp extends App { } return self::$contactsInverted; } - + /** * Partially importe this function from owncloud Chat app * https://github.com/owncloud/chat/blob/master/app/chat.php @@ -108,12 +108,12 @@ class OcSmsApp extends App { private function loadContacts() { self::$contacts = array(); self::$contactsInverted = array(); - + $cm = $this->c['ContactsManager']; if ($cm == null) { return; } - + $result = array(); try { $result = $cm->search('',array('FN')); @@ -146,8 +146,9 @@ class OcSmsApp extends App { $phoneNb = FormatPhoneNumber::formatPhoneNumber($rawPhone); self::$contacts[$phoneNb] = $contactName; // Inverted contacts - if (!isset(self::$contactsInverted[$contactName])) + if (!isset(self::$contactsInverted[$contactName])) { self::$contactsInverted[$contactName] = array(); + } array_push(self::$contactsInverted[$contactName], $phoneNb); } } diff --git a/controller/smscontroller.php b/controller/smscontroller.php index 21039a2..078c5a7 100644 --- a/controller/smscontroller.php +++ b/controller/smscontroller.php @@ -26,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; @@ -145,7 +145,7 @@ class SmsController extends Controller { $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); + $messages = $messages + $this->smsMapper->getAllMessagesForPhoneNumber($this->userId, $cnumber, $lastDate); $msgCount += $this->smsMapper->countMessagesForPhoneNumber($this->userId, $cnumber); } } @@ -153,7 +153,7 @@ class SmsController extends Controller { } // Order by id (date) ksort($messages); - + // Set the last read message for the conversation (all phone numbers) if (count($messages) > 0) { $maxDate = max(array_keys($messages)); diff --git a/db/smsmapper.php b/db/smsmapper.php index 0a5f7c2..d1f4a79 100644 --- a/db/smsmapper.php +++ b/db/smsmapper.php @@ -100,7 +100,7 @@ class SmsMapper extends Mapper { $fmtPN = FormatPhoneNumber::formatPhoneNumber($pn); if (!isset($phoneList[$fmtPN])) { $phoneList[$fmtPN] = array(); - } + } if(!isset($phoneList[$fmtPN][$pn])) { $phoneList[$fmtPN][$pn] = 0; } @@ -110,12 +110,13 @@ class SmsMapper extends Mapper { if(isset($phoneList[$fpn])){ return $phoneList[$fpn]; } - else + 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 ' . From b19146be11149f12d08e17ad92df514c0cc44dfe Mon Sep 17 00:00:00 2001 From: stagprom Date: Thu, 27 Nov 2014 13:35:41 +0100 Subject: [PATCH 12/21] modified: appinfo/formatphonenumber.php Numbers will be formatted as in this examples: 00xx 1234567... => +xx1234567... (xxx)-yyy 1234567... => xxxyyy1234567... +xx (0) yyy 1234567... => +xxyyy1234567... --- appinfo/formatphonenumber.php | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/appinfo/formatphonenumber.php b/appinfo/formatphonenumber.php index 2a12da4..c601a28 100644 --- a/appinfo/formatphonenumber.php +++ b/appinfo/formatphonenumber.php @@ -10,9 +10,13 @@ */ namespace OCA\OcSms\AppInfo; +error_log("You messed up!", 3, "/var/tmp/my-errors.log"); + class FormatPhoneNumber { + public static function formatPhoneNumber($pn) { + $ipnrxp = 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 @@ -33,20 +37,26 @@ class FormatPhoneNumber { ); $ignrxp = array( // match non digits and + - '#\(\d*\)|[^\d\+]#', + '#[^\d\+\(\)\[\]\{\}]#', // everything but digit, +, (), [] or {} + '#(.+)([\(\[\{]\d*[\)\]\}])#', // braces inside the number: +49 (0) 123 456789 + '#[^\d\+]#' // everything but digits and + ); - + $ignrpl = array( // replacements + '', + '$1', + '' + ); + /* ToDo : make local settings in web-page */ $lpnrxp = array( // match local numbers - '#(^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 - $tpn = trim($pn); - if( preg_match('#^[\d\+].*#',$tpn)) { // start with digit or + - $fpn = preg_replace($ignrxp, '', $tpn); // replace everything but digits/+ with '' + 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]]..... } else { From 9f621e4df859b2223f1f7e7a73676a3bb64d212a Mon Sep 17 00:00:00 2001 From: stagprom Date: Thu, 27 Nov 2014 13:35:41 +0100 Subject: [PATCH 13/21] modified: appinfo/formatphonenumber.php Numbers will be formatted as in this examples: 00xx 1234567... => +xx1234567... (xxx)-yyy 1234567... => xxxyyy1234567... +xx (0) yyy 1234567... => +xxyyy1234567... --- appinfo/formatphonenumber.php | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/appinfo/formatphonenumber.php b/appinfo/formatphonenumber.php index 2a12da4..c601a28 100644 --- a/appinfo/formatphonenumber.php +++ b/appinfo/formatphonenumber.php @@ -10,9 +10,13 @@ */ namespace OCA\OcSms\AppInfo; +error_log("You messed up!", 3, "/var/tmp/my-errors.log"); + class FormatPhoneNumber { + public static function formatPhoneNumber($pn) { + $ipnrxp = 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 @@ -33,20 +37,26 @@ class FormatPhoneNumber { ); $ignrxp = array( // match non digits and + - '#\(\d*\)|[^\d\+]#', + '#[^\d\+\(\)\[\]\{\}]#', // everything but digit, +, (), [] or {} + '#(.+)([\(\[\{]\d*[\)\]\}])#', // braces inside the number: +49 (0) 123 456789 + '#[^\d\+]#' // everything but digits and + ); - + $ignrpl = array( // replacements + '', + '$1', + '' + ); + /* ToDo : make local settings in web-page */ $lpnrxp = array( // match local numbers - '#(^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 - $tpn = trim($pn); - if( preg_match('#^[\d\+].*#',$tpn)) { // start with digit or + - $fpn = preg_replace($ignrxp, '', $tpn); // replace everything but digits/+ with '' + 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]]..... } else { From b45854abd368917be012a4fcf7bb230788d2467a Mon Sep 17 00:00:00 2001 From: stagprom Date: Thu, 27 Nov 2014 14:00:26 +0100 Subject: [PATCH 14/21] modified: appinfo/formatphonenumber.php Numbers will be formatted as in this examples: 00xx 1234567... => +xx1234567... (xxx)-yyy 1234567... => xxxyyy1234567... +xx (0) yyy 1234567... => +xxyyy1234567... --- appinfo/formatphonenumber.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/appinfo/formatphonenumber.php b/appinfo/formatphonenumber.php index 744e168..719d6b2 100644 --- a/appinfo/formatphonenumber.php +++ b/appinfo/formatphonenumber.php @@ -8,10 +8,11 @@ * @author Loic Blot * @copyright Loic Blot 2014 */ + namespace OCA\OcSms\AppInfo; -class FormatPhoneNumber { +class FormatPhoneNumber { public static function formatPhoneNumber($pn) { From 5f0263f71e07390827a8b45e8e5869d968567ae0 Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Thu, 27 Nov 2014 14:04:25 +0000 Subject: [PATCH 15/21] Move formatphonenumber.php to proper dir and rename. * fix some coding styles * Add contributor --- appinfo/ocsmsapp.php | 4 ++-- controller/smscontroller.php | 10 +++++----- db/smsmapper.php | 6 +++--- .../phonenumberformatter.php | 19 +++++++++++-------- 4 files changed, 21 insertions(+), 18 deletions(-) rename appinfo/formatphonenumber.php => lib/phonenumberformatter.php (88%) diff --git a/appinfo/ocsmsapp.php b/appinfo/ocsmsapp.php index bc42d79..2867c2a 100644 --- a/appinfo/ocsmsapp.php +++ b/appinfo/ocsmsapp.php @@ -20,7 +20,7 @@ use \OCA\OcSms\Controller\SmsController; use \OCA\OcSms\Db\Sms; use \OCA\OcSms\Db\SmsMapper; -use \OCA\OcSms\AppInfo\FormatPhoneNumber; +use \OCA\OcSms\Lib\PhoneNumberFormatter; class OcSmsApp extends App { @@ -143,7 +143,7 @@ class OcSmsApp extends App { */ private function pushPhoneNumberToCache($rawPhone, $contactName) { - $phoneNb = FormatPhoneNumber::formatPhoneNumber($rawPhone); + $phoneNb = PhoneNumberFormatter::format($rawPhone); self::$contacts[$phoneNb] = $contactName; // Inverted contacts if (!isset(self::$contactsInverted[$contactName])) { diff --git a/controller/smscontroller.php b/controller/smscontroller.php index 078c5a7..7dea05d 100644 --- a/controller/smscontroller.php +++ b/controller/smscontroller.php @@ -18,7 +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; +use \OCA\OcSms\Lib\PhoneNumberFormatter; class SmsController extends Controller { @@ -100,7 +100,7 @@ class SmsController extends Controller { $countPhone = count($phoneList); foreach ($phoneList as $number => $ts) { - $fmtPN = FormatPhoneNumber::formatPhoneNumber($number); + $fmtPN = PhoneNumberFormatter::format($number); if (isset($contactsSrc[$number])) { $contacts[$number] = $contactsSrc[$number]; } elseif (isset($contactsSrc[$fmtPN])) { @@ -123,7 +123,7 @@ class SmsController extends Controller { $contacts = $this->app->getContacts(); $iContacts = $this->app->getInvertedContacts(); $contactName = ""; - $fmtPN = FormatPhoneNumber::formatPhoneNumber($phoneNumber); + $fmtPN = PhoneNumberFormatter::format($phoneNumber); if (isset($contacts[$fmtPN])) { $contactName = $contacts[$fmtPN]; } @@ -137,7 +137,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[] = FormatPhoneNumber::formatPhoneNumber($cnumber); + $phoneNumbers[] = PhoneNumberFormatter::format($cnumber); } } else { @@ -149,7 +149,7 @@ class SmsController extends Controller { $msgCount += $this->smsMapper->countMessagesForPhoneNumber($this->userId, $cnumber); } } - $phoneNumbers[] = FormatPhoneNumber::formatPhoneNumber($phoneNumber); + $phoneNumbers[] = PhoneNumberFormatter::format($phoneNumber); } // Order by id (date) ksort($messages); diff --git a/db/smsmapper.php b/db/smsmapper.php index d1f4a79..c01ad4f 100644 --- a/db/smsmapper.php +++ b/db/smsmapper.php @@ -15,7 +15,7 @@ use \OCP\IDb; use \OCP\AppFramework\Db\Mapper; use \OCA\OcSms\AppInfo\OcSmsApp; -use \OCA\OcSms\AppInfo\FormatPhoneNumber; +use \OCA\OcSms\Lib\PhoneNumberFormatter; class SmsMapper extends Mapper { /* @@ -97,7 +97,7 @@ class SmsMapper extends Mapper { $phoneList = array(); while($row = $result->fetchRow()) { $pn = $row["sms_address"]; - $fmtPN = FormatPhoneNumber::formatPhoneNumber($pn); + $fmtPN = PhoneNumberFormatter::format($pn); if (!isset($phoneList[$fmtPN])) { $phoneList[$fmtPN] = array(); } @@ -106,7 +106,7 @@ class SmsMapper extends Mapper { } $phoneList[$fmtPN][$pn] += 1; } - $fpn = FormatPhoneNumber::formatPhoneNumber($phoneNumber); + $fpn = PhoneNumberFormatter::format($phoneNumber); if(isset($phoneList[$fpn])){ return $phoneList[$fpn]; } diff --git a/appinfo/formatphonenumber.php b/lib/phonenumberformatter.php similarity index 88% rename from appinfo/formatphonenumber.php rename to lib/phonenumberformatter.php index 719d6b2..0d813a2 100644 --- a/appinfo/formatphonenumber.php +++ b/lib/phonenumberformatter.php @@ -6,16 +6,15 @@ * later. See the COPYING file. * * @author Loic Blot + * @contributor: stagprom * @copyright Loic Blot 2014 */ -namespace OCA\OcSms\AppInfo; +namespace OCA\OcSms\Lib; -class FormatPhoneNumber { - - public static function formatPhoneNumber($pn) { - +class PhoneNumberFormatter { + public static function format ($pn) { $ipnrxp = 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 @@ -40,6 +39,7 @@ class FormatPhoneNumber { '#(.+)([\(\[\{]\d*[\)\]\}])#', // braces inside the number: +49 (0) 123 456789 '#[^\d\+]#' // everything but digits and + ); + $ignrpl = array( // replacements '', '$1', @@ -47,20 +47,23 @@ class FormatPhoneNumber { ); /* - ToDo : make local settings in web-page + @TODO: make local settings in web-page */ $lpnrxp = array( // match local numbers '#(^0)([^0])#' // in germany : 0-xx[x[x]]-123456 ); // + $lpnrpl = '+49$2'; // replace with +49 -xx[x[x]]-123456 $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 '' $xpn = preg_replace($lpnrxp, $lpnrpl, $fpn); // replace local prenumbers $ypn = preg_replace($ipnrxp, '+$2', $xpn); // format to international coding +x[x[x]]..... } else { $ypn = $tpn; // some SMS_adresses are strings } + return $ypn; } -} +}; From f89a23c20558a4cd05beec10beb1660b396bd2c0 Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Sun, 30 Nov 2014 18:05:20 +0000 Subject: [PATCH 16/21] Prepare an APIv2 call to improve sync performance on phone (battery) --- controller/smscontroller.php | 14 ++++++++++++++ db/smsmapper.php | 14 +++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/controller/smscontroller.php b/controller/smscontroller.php index 7dea05d..d36acbc 100644 --- a/controller/smscontroller.php +++ b/controller/smscontroller.php @@ -80,6 +80,20 @@ class SmsController extends Controller { return new JSONResponse(array("smslist" => $smsList)); } + /** + * @NoAdminRequired + * @NoCSRFRequired + * + * This function is used by API v2 + * Phone will get this ID to push recent messages + * This call will be used combined with retrieveAllIds + * but will be used more times + */ + public function retrieveLastTimestamp () { + $ts = $this->smsMapper->getLastTimestamp($this->userId); + return new JSONResponse(array("timestamp" => $ts)); + } + /** * @NoAdminRequired * @NoCSRFRequired diff --git a/db/smsmapper.php b/db/smsmapper.php index c01ad4f..928e570 100644 --- a/db/smsmapper.php +++ b/db/smsmapper.php @@ -72,6 +72,18 @@ class SmsMapper extends Mapper { return $smsList; } + public function getLastTimestamp ($userId) { + $query = \OCP\DB::prepare('SELECT max(sms_date) as mx FROM ' . + '*PREFIX*ocsms_smsdatas WHERE user_id = ?'); + $result = $query->execute(array($userId)); + + if ($row = $result->fetchRow()) { + return $row["mx"]; + } + + return 0; + } + public function getAllPeersPhoneNumbers ($userId) { $query = \OCP\DB::prepare('SELECT sms_address FROM ' . '*PREFIX*ocsms_smsdatas WHERE user_id = ? AND sms_mailbox IN (?,?)'); @@ -144,7 +156,7 @@ class SmsMapper extends Mapper { '*PREFIX*ocsms_smsdatas WHERE user_id = ? AND sms_address = ? ' . 'AND sms_mailbox IN (?,?)'); - foreach( $phlst as $pn => $val) { + foreach($phlst as $pn => $val) { $result = $query->execute(array($userId, $pn, 0, 1)); if ($row = $result->fetchRow()) $cnt += $row["ct"]; From df95014fc61177f5147f1af913bde210106e1f37 Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Sun, 30 Nov 2014 19:19:13 +0000 Subject: [PATCH 17/21] Add route for previous commit --- appinfo/routes.php | 1 + 1 file changed, 1 insertion(+) diff --git a/appinfo/routes.php b/appinfo/routes.php index 69176ef..aa342d2 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -19,6 +19,7 @@ $application->registerRoutes($this, array('routes' => array( array('name' => 'sms#replace', 'url' => '/replace', 'verb' => 'POST'), array('name' => 'sms#retrieve_all_ids', 'url' => '/get/smsidlist', 'verb' => 'GET'), array('name' => 'sms#retrieve_all_ids_with_status', 'url' => '/get/smsidstate', 'verb' => 'GET'), + array('name' => 'sms#retrieve_last_timestamp', 'url' => '/get/lastmsgtime', 'verb' => 'GET'), array('name' => 'sms#retrieve_all_peers', 'url' => '/get/peerlist', 'verb' => 'GET'), array('name' => 'sms#get_conversation', 'url' => '/get/conversation', 'verb' => 'GET'), array('name' => 'sms#check_new_messages', 'url' => '/get/new_messages', 'verb' => 'GET'), From 7c0bd22385b568b8a7d3b2ac2742d193643dcc16 Mon Sep 17 00:00:00 2001 From: Ner'zhul Date: Tue, 9 Dec 2014 16:26:53 +0100 Subject: [PATCH 18/21] Remove integer cast on date. This fix an integer overflow on 32 bits systems --- db/smsmapper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/db/smsmapper.php b/db/smsmapper.php index 928e570..4b78d21 100644 --- a/db/smsmapper.php +++ b/db/smsmapper.php @@ -260,7 +260,7 @@ class SmsMapper extends Mapper { '(?,?,?,?,?,?,?,?,?,?)'); $result = $query->execute(array( $userId, "NOW()", "NOW()", $smsFlags, - (int) $sms["date"], (int) $sms["_id"], + $sms["date"], (int) $sms["_id"], $sms["address"], $sms["body"], (int) $sms["mbox"], (int) $sms["type"] )); From 0cc56794fda881a45dcf7baa3df292ef1aecca21 Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Tue, 9 Dec 2014 16:30:07 +0100 Subject: [PATCH 19/21] tag 1.3.2 --- appinfo/info.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/appinfo/info.xml b/appinfo/info.xml index 9184e3f..e6dbc8f 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -5,6 +5,6 @@ A app to sync SMS with your ownCloud AGPL Loic Blot - 1.3.1 + 1.3.2 7 From d87e245236eabb7dc77df7893ca63fab17931bf0 Mon Sep 17 00:00:00 2001 From: Ner'zhul Date: Mon, 22 Dec 2014 10:58:39 +0100 Subject: [PATCH 20/21] Temporary Fix https://github.com/nerzhul/ownCloud-SMS-App/issues/10 We disable HTML 5 notification is there isn't browser support (IE for example) We need a portable library later. --- js/script.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/js/script.js b/js/script.js index dab293c..bafcb0b 100644 --- a/js/script.js +++ b/js/script.js @@ -303,6 +303,10 @@ function fetchInitialPeerList(jsondata) { } function initDesktopNotifies() { + if (!("Notification" in window)) { + return; + } + Notification.requestPermission(function (permission) { if(!('permission' in Notification)) { Notification.permission = permission; From 8dae05cc225b283b01a64137c9286c601e397042 Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Mon, 22 Dec 2014 16:45:30 +0000 Subject: [PATCH 21/21] Remove useless try catch --- appinfo/ocsmsapp.php | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/appinfo/ocsmsapp.php b/appinfo/ocsmsapp.php index 2867c2a..4ebad1a 100644 --- a/appinfo/ocsmsapp.php +++ b/appinfo/ocsmsapp.php @@ -114,13 +114,7 @@ class OcSmsApp extends App { return; } - $result = array(); - try { - $result = $cm->search('',array('FN')); - } catch (Exception $e) { - // If contact manager failed, avoid the issue - return; - } + $result = $cm->search('',array('FN')); foreach ($result as $r) { if (isset ($r["TEL"])) {