]> git.mxchange.org Git - friendica-addons.git/blobdiff - windowsphonepush/windowsphonepush.php
Issue 3873
[friendica-addons.git] / windowsphonepush / windowsphonepush.php
index 77b0b46789d437f03a4152ed89ee1cba3726f277..53bf83b87d1863b93780733b67be68d529cbc53f 100644 (file)
@@ -2,7 +2,7 @@
 /**
  * Name: WindowsPhonePush
  * Description: Enable push notification to send information to Friendica Mobile app on Windows phone (count of unread timeline entries, text of last posting - if wished by user)
- * Version: 1.0
+ * Version: 2.0
  * Author: Gerhard Seeber <http://friendica.seeber.at/profile/admin>
  * 
  * 
  * If the addon is removed from the configuration list, the 
  * system will call the name_uninstall() function.
  *
+ * Version history:
+ * 1.1  : addon crashed on php versions >= 5.4 as of removed deprecated call-time 
+ *        pass-by-reference used in function calls within function windowsphonepush_content
+ * 2.0  : adaption for supporting emphasizing new entries in app (count on tile cannot be read out,
+ *        so we need to retrieve counter through show_settings secondly). Provide new function for 
+ *        calling from app to set the counter back after start (if user starts again before cronjob
+ *        sets the counter back
+ *        count only unseen elements which are not type=activity (likes and dislikes not seen as new elements)
  */
 
+use Friendica\Core\PConfig;
 
 function windowsphonepush_install() {
 
@@ -78,9 +87,14 @@ function windowsphonepush_module() {}
 function windowsphonepush_settings_post($a,$post) {
        if(! local_user() || (! x($_POST,'windowsphonepush-submit')))
                return;
+       $enable = intval($_POST['windowsphonepush']);
+       PConfig::set(local_user(),'windowsphonepush','enable',$enable);
 
-       set_pconfig(local_user(),'windowsphonepush','enable',intval($_POST['windowsphonepush']));
-       set_pconfig(local_user(),'windowsphonepush','senditemtext',intval($_POST['windowsphonepush-senditemtext']));
+       if($enable) {
+               PConfig::set(local_user(),'windowsphonepush','counterunseen', 0);
+       }
+
+       PConfig::set(local_user(),'windowsphonepush','senditemtext',intval($_POST['windowsphonepush-senditemtext']));
 
        info( t('WindowsPhonePush settings updated.') . EOL);
 }
@@ -101,13 +115,13 @@ function windowsphonepush_settings(&$a,&$s) {
        $a->page['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . $a->get_baseurl() . '/addon/windowsphonepush/windowsphonepush.css' . '" media="all" />' . "\r\n";
 
        /* Get the current state of our config variables */
-       $enabled = get_pconfig(local_user(),'windowsphonepush','enable');
+       $enabled = PConfig::get(local_user(),'windowsphonepush','enable');
        $checked_enabled = (($enabled) ? ' checked="checked" ' : '');
 
-       $senditemtext = get_pconfig(local_user(), 'windowsphonepush', 'senditemtext');
+       $senditemtext = PConfig::get(local_user(), 'windowsphonepush', 'senditemtext');
        $checked_senditemtext = (($senditemtext) ? ' checked="checked" ' : '');
 
-       $device_url = get_pconfig(local_user(), 'windowsphonepush', 'device_url');
+       $device_url = PConfig::get(local_user(), 'windowsphonepush', 'device_url');
 
        /* Add some HTML to the existing form */
        $s .= '<div class="settings-block">';
@@ -150,8 +164,8 @@ function windowsphonepush_cron() {
        if(count($r)) {
                foreach($r as $rr) {
                        // load stored information for the user-id of the current loop
-                       $device_url = get_pconfig($rr['uid'], 'windowsphonepush', 'device_url');
-                       $lastpushid = get_pconfig($rr['uid'], 'windowsphonepush', 'lastpushid');
+                       $device_url = PConfig::get($rr['uid'], 'windowsphonepush', 'device_url');
+                       $lastpushid = PConfig::get($rr['uid'], 'windowsphonepush', 'lastpushid');
 
                        // pushing only possible if device_url (the URI on Microsoft server) is available or not "NA" (which will be sent 
                        // by app if user has switched the server setting in app - sending blank not possible as this would return an update error)
@@ -161,7 +175,7 @@ function windowsphonepush_cron() {
                        } else {
                                // retrieve the number of unseen items and the id of the latest one (if there are more than 
                                // one new entries since last poller run, only the latest one will be pushed)
-                               $count = q("SELECT count(`id`) as count, max(`id`) as max FROM `item` WHERE `unseen` = 1 AND `uid` = %d",
+                               $count = q("SELECT count(`id`) as count, max(`id`) as max FROM `item` WHERE `unseen` = 1 AND `type` <> 'activity' AND `uid` = %d",
                                        intval($rr['uid'])
                                );
 
@@ -171,7 +185,8 @@ function windowsphonepush_cron() {
                                $res_tile = send_tile_update($device_url, "", $count[0]['count'], "");
                                switch (trim($res_tile)) {
                                        case "Received":
-                                               // ok, count has been pushed
+                                               // ok, count has been pushed, let's save it in personal settings 
+                                               PConfig::set($rr['uid'], 'windowsphonepush', 'counterunseen', $count[0]['count']);
                                                break;
                                        case "QueueFull":
                                                // maximum of 30 messages reached, server rejects any further push notification until device reconnects
@@ -194,7 +209,7 @@ function windowsphonepush_cron() {
                                if (intval($count[0]['max']) > intval($lastpushid)) {
                                        // user can define if he wants to see the text of the item in the push notification
                                        // this has been implemented as the device_url is not a https uri (not so secure)
-                                       $senditemtext = get_pconfig($rr['uid'], 'windowsphonepush', 'senditemtext');
+                                       $senditemtext = PConfig::get($rr['uid'], 'windowsphonepush', 'senditemtext');
                                        if ($senditemtext == 1) {
                                                // load item with the max id
                                                $item = q("SELECT `author-name` as author, `body` as body FROM `item` where `id` = %d",
@@ -233,7 +248,7 @@ function windowsphonepush_cron() {
                                        // further log information done on count pushing with send_tile (see above)
                                        $res_toast = send_toast($device_url, $author, $body);
                                        if (trim($res_toast) === 'Received') {
-                                               set_pconfig($rr['uid'], 'windowsphonepush', 'lastpushid', $count[0]['max']);
+                                               PConfig::set($rr['uid'], 'windowsphonepush', 'lastpushid', $count[0]['max']);
                                        }                               
                                }
                        }
@@ -312,11 +327,11 @@ function send_push($device_url, $headers, $msg) {
        $output = curl_exec($ch);
        curl_close($ch);
 
-       // if we received "Expired" from Novartis server we will delete the obsolete device-URL
+       // if we received "Expired" from Microsoft server we will delete the obsolete device-URL
        // and log this fact
        $subscriptionStatus = get_header_value($output, 'X-SubscriptionStatus');
        if ($subscriptionStatus == "Expired") {
-               set_pconfig(local_user(),'windowsphonepush','device_url', "");
+               PConfig::set(local_user(),'windowsphonepush','device_url', "");
                logger("ERROR: the stored Device-URL " . $device_url . "returned an 'Expired' error, it has been deleted now.");
        }
 
@@ -339,6 +354,7 @@ function get_header_value($content, $header) {
  * reading information from url and deciding which function to start
  * show_settings = delivering settings to check
  * update_settings = set the device_url
+ * update_counterunseen = set counter for unseen elements to zero
  *
  */
 function windowsphonepush_content(&$a) {       
@@ -350,15 +366,21 @@ function windowsphonepush_content(&$a) {
        if ($path == "windowsphonepush") {
                switch ($path2) {
                        case "show_settings":
-                               windowsphonepush_showsettings(&$a);
+                               windowsphonepush_showsettings($a);
                                killme();
                                break;
                        case "update_settings":
-                               $ret = windowsphonepush_updatesettings(&$a);
+                               $ret = windowsphonepush_updatesettings($a);
                                header("Content-Type: application/json; charset=utf-8");        
                                echo json_encode(array('status' => $ret));
                                killme();                               
                                break;
+                       case "update_counterunseen":
+                               $ret = windowsphonepush_updatecounterunseen();
+                               header("Content-Type: application/json; charset=utf-8");
+                               echo json_encode(array('status' => $ret));
+                               killme();
+                               break;
                        default:
                                echo "Fehler";
                }
@@ -372,10 +394,12 @@ function windowsphonepush_showsettings(&$a) {
        if(! local_user())
                return;
 
-       $enable = get_pconfig(local_user(), 'windowsphonepush', 'enable');
-       $device_url = get_pconfig(local_user(), 'windowsphonepush', 'device_url');
-       $senditemtext = get_pconfig(local_user(), 'windowsphonepush', 'senditemtext');
-       $lastpushid = get_pconfig(local_user(), 'windowsphonepush', 'lastpushid');
+       $enable = PConfig::get(local_user(), 'windowsphonepush', 'enable');
+       $device_url = PConfig::get(local_user(), 'windowsphonepush', 'device_url');
+       $senditemtext = PConfig::get(local_user(), 'windowsphonepush', 'senditemtext');
+       $lastpushid = PConfig::get(local_user(), 'windowsphonepush', 'lastpushid');
+       $counterunseen = PConfig::get(local_user(), 'windowsphonepush', 'counterunseen');
+       $addonversion = "2.0";
 
        if (!$device_url)
                $device_url = "";
@@ -388,7 +412,9 @@ function windowsphonepush_showsettings(&$a) {
                                'enable' => $enable, 
                                'device_url' => $device_url, 
                                'senditemtext' => $senditemtext,
-                               'lastpushid' => $lastpushid));
+                               'lastpushid' => $lastpushid, 
+                               'counterunseen' => $counterunseen, 
+                               'addonversion' => $addonversion));
 }
 
 /* 
@@ -401,7 +427,7 @@ function windowsphonepush_updatesettings(&$a) {
        }
 
        // no updating if user hasn't enabled the plugin
-       $enable = get_pconfig(local_user(), 'windowsphonepush', 'enable');
+       $enable = PConfig::get(local_user(), 'windowsphonepush', 'enable');
        if(! $enable) {
                return "Plug-in not enabled";
        }
@@ -423,17 +449,35 @@ function windowsphonepush_updatesettings(&$a) {
                                                `v` = '" . $device_url . "'");
        if(count($r)) {
                foreach($r as $rr) {
-               set_pconfig($rr['uid'], 'windowsphonepush', 'device_url', '');
+               PConfig::set($rr['uid'], 'windowsphonepush', 'device_url', '');
                logger("WARN: the sent URL was already registered with user '" . $rr['uid'] . "'. Deleted for this user as we expect to be correct now for user '" . local_user() . "'.");
                }
        }
 
-       set_pconfig(local_user(),'windowsphonepush','device_url', $device_url);
+       PConfig::set(local_user(),'windowsphonepush','device_url', $device_url);
        // output the successfull update of the device URL to the logger for error analysis if necessary
        logger("INFO: Device-URL for user '" . local_user() . "' has been updated with '" . $device_url . "'");
        return "Device-URL updated successfully!";
 }
 
+/* 
+ * update_counterunseen is used to reset the counter to zero from Windows Phone app 
+ */
+function windowsphonepush_updatecounterunseen() {
+       if(! local_user()) {  
+               return "Not Authenticated";
+       }
+
+       // no updating if user hasn't enabled the plugin
+       $enable = PConfig::get(local_user(), 'windowsphonepush', 'enable');
+       if(! $enable) {
+               return "Plug-in not enabled";
+       }
+
+       PConfig::set(local_user(),'windowsphonepush','counterunseen', 0);
+       return "Counter set to zero";
+}
+
 /*
  * helper function to login to the server with the specified Network credentials
  * (mainly copied from api.php)