]> git.mxchange.org Git - friendica-addons.git/blobdiff - windowsphonepush/windowsphonepush.php
Merge branch '3.6-rc'
[friendica-addons.git] / windowsphonepush / windowsphonepush.php
index 77b0b46789d437f03a4152ed89ee1cba3726f277..9837e1fe573fbac0f6787d523e611639cfa29ba2 100644 (file)
@@ -2,45 +2,53 @@
 /**
  * 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>
  * 
  * 
  * Pre-requisite: Windows Phone mobile device (at least WP 7.0)
  *                Friendica mobile app on Windows Phone
  *
- * When plugin is installed, the system calls the plugin
+ * When addon is installed, the system calls the addon
  * name_install() function, located in 'addon/name/name.php',
  * where 'name' is the name of the addon.
  * 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)
  */
 
-
-function windowsphonepush_install() {
-
-       /**
-        * 
-        * Our plugin will attach in three places.
-        * The first is within cron - so the push notifications will be 
+use Friendica\App;
+use Friendica\Content\Text\BBCode;
+use Friendica\Core\Addon;
+use Friendica\Core\L10n;
+use Friendica\Core\PConfig;
+use Friendica\Model\User;
+
+function windowsphonepush_install()
+{
+       /* Our addon will attach in three places.
+        * The first is within cron - so the push notifications will be
         * sent every 10 minutes (or whatever is set in crontab).
         *
         */
+       Addon::registerHook('cron', 'addon/windowsphonepush/windowsphonepush.php', 'windowsphonepush_cron');
 
-       register_hook('cron', 'addon/windowsphonepush/windowsphonepush.php', 'windowsphonepush_cron');
-
-       /**
-        *
-        * Then we'll attach into the plugin settings page, and also the 
+       /* Then we'll attach into the addon settings page, and also the
         * settings post hook so that we can create and update
-        * user preferences. User shall be able to activate the plugin and 
+        * user preferences. User shall be able to activate the addon and
         * define whether he allows pushing first characters of item text
         *
         */
-
-       register_hook('plugin_settings', 'addon/windowsphonepush/windowsphonepush.php', 'windowsphonepush_settings');
-       register_hook('plugin_settings_post', 'addon/windowsphonepush/windowsphonepush.php', 'windowsphonepush_settings_post');
+       Addon::registerHook('addon_settings', 'addon/windowsphonepush/windowsphonepush.php', 'windowsphonepush_settings');
+       Addon::registerHook('addon_settings_post', 'addon/windowsphonepush/windowsphonepush.php', 'windowsphonepush_settings_post');
 
        logger("installed windowsphonepush");
 }
@@ -54,10 +62,9 @@ function windowsphonepush_uninstall() {
         * during install. Don't delete data in table `pconfig`.
         *
         */
-
-       unregister_hook('cron', 'addon/windowsphonepush/windowsphonepush.php', 'windowsphonepush_cron');
-       unregister_hook('plugin_settings', 'addon/windowsphonepush/windowsphonepush.php', 'windowsphonepush_settings');
-       unregister_hook('plugin_settings_post', 'addon/windowsphonepush/windowsphonepush.php', 'windowsphonepush_settings_post');
+       Addon::unregisterHook('cron', 'addon/windowsphonepush/windowsphonepush.php', 'windowsphonepush_cron');
+       Addon::unregisterHook('addon_settings', 'addon/windowsphonepush/windowsphonepush.php', 'windowsphonepush_settings');
+       Addon::unregisterHook('addon_settings_post', 'addon/windowsphonepush/windowsphonepush.php', 'windowsphonepush_settings_post');
 
        logger("removed windowsphonepush");
 }
@@ -78,17 +85,19 @@ function windowsphonepush_module() {}
 function windowsphonepush_settings_post($a,$post) {
        if(! local_user() || (! x($_POST,'windowsphonepush-submit')))
                return;
+       $enable = intval($_POST['windowsphonepush']);
+       set_pconfig(local_user(),'windowsphonepush','enable',$enable);
+
+       if($enable) {
+               set_pconfig(local_user(),'windowsphonepush','counterunseen', 0);
+       }
 
-       set_pconfig(local_user(),'windowsphonepush','enable',intval($_POST['windowsphonepush']));
        set_pconfig(local_user(),'windowsphonepush','senditemtext',intval($_POST['windowsphonepush-senditemtext']));
 
-       info( t('WindowsPhonePush settings updated.') . EOL);
+       info(L10n::t('WindowsPhonePush settings updated.') . EOL);
 }
 
-
-/**
- *
- * Called from the Plugin Setting form. 
+/* Called from the Addon Setting form.
  * Add our own settings info to the page.
  *
  */
@@ -111,20 +120,20 @@ function windowsphonepush_settings(&$a,&$s) {
 
        /* Add some HTML to the existing form */
        $s .= '<div class="settings-block">';
-       $s .= '<h3>' . t('WindowsPhonePush Settings') . '</h3>';
+       $s .= '<h3>' . L10n::t('WindowsPhonePush Settings') . '</h3>';
 
        $s .= '<div id="windowsphonepush-enable-wrapper">';
-       $s .= '<label id="windowsphonepush-enable-label" for="windowsphonepush-enable-chk">' . t('Enable WindowsPhonePush Plugin') . '</label>';
+       $s .= '<label id="windowsphonepush-enable-label" for="windowsphonepush-enable-chk">' . L10n::t('Enable WindowsPhonePush Addon') . '</label>';
        $s .= '<input id="windowsphonepush-enable-chk" type="checkbox" name="windowsphonepush" value="1" ' . $checked_enabled . '/>';
        $s .= '</div><div class="clear"></div>';
 
        $s .= '<div id="windowsphonepush-senditemtext-wrapper">';
-       $s .= '<label id="windowsphonepush-senditemtext-label" for="windowsphonepush-senditemtext-chk">' . t('Push text of new item') . '</label>';
+       $s .= '<label id="windowsphonepush-senditemtext-label" for="windowsphonepush-senditemtext-chk">' . L10n::t('Push text of new item') . '</label>';
        $s .= '<input id="windowsphonepush-senditemtext-chk" type="checkbox" name="windowsphonepush-senditemtext" value="1" ' . $checked_senditemtext . '/>';
        $s .= '</div><div class="clear"></div>';
 
-       /* provide a submit button - enable und senditemtext can be changed by the user*/
-       $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="windowsphonepush-submit" name="windowsphonepush-submit" class="settings-submit" value="' . t('Save Settings') . '" /></div><div class="clear"></div>';
+       /* provide a submit button - enable und senditemtext can be changed by the user */
+       $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="windowsphonepush-submit" name="windowsphonepush-submit" class="settings-submit" value="' . L10n::t('Save Settings') . '" /></div><div class="clear"></div>';
 
        /* provide further read-only information concerning the addon (useful for */
        $s .= '<div id="windowsphonepush-device_url-wrapper">';
@@ -136,16 +145,13 @@ function windowsphonepush_settings(&$a,&$s) {
 
 }
 
-
-/**
- *
- * Cron function used to regularly check all users on the server with active windowsphonepushplugin and send
+/* Cron function used to regularly check all users on the server with active windowsphonepushaddon and send
  * notifications to the Microsoft servers and consequently to the Windows Phone device
  *
  */
-
-function windowsphonepush_cron() {
-       // retrieve all UID's for which the plugin windowsphonepush is enabled and loop through every user
+function windowsphonepush_cron()
+{
+       // retrieve all UID's for which the addon windowsphonepush is enabled and loop through every user
        $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'windowsphonepush' AND `k` = 'enable' AND `v` = 1");
        if(count($r)) {
                foreach($r as $rr) {
@@ -155,13 +161,13 @@ function windowsphonepush_cron() {
 
                        // 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)
-                       if ( ( $device_url == "" ) || ( $device_url == "NA" ) ) {
-                               // no Device-URL for the user availabe, but plugin is enabled --> write info to Logger
+                       if (( $device_url == "" ) || ( $device_url == "NA" )) {
+                               // no Device-URL for the user availabe, but addon is enabled --> write info to Logger
                                logger("WARN: windowsphonepush is enable for user " . $rr['uid'] . ", but no Device-URL is specified for the user.");
                        } 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 +177,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 
+                                               set_pconfig($rr['uid'], 'windowsphonepush', 'counterunseen', $count[0]['count']);
                                                break;
                                        case "QueueFull":
                                                // maximum of 30 messages reached, server rejects any further push notification until device reconnects
@@ -214,10 +221,10 @@ function windowsphonepush_cron() {
                                                $body = $item[0]['body'];
                                                if (substr($body, 0, 4) == "[url") 
                                                        $body = "URL/Image ...";
-                                               else {
-                                                       require_once('include/bbcode.php');
+                                               } else {
                                                        require_once("include/html2plain.php");
-                                                       $body = bbcode($body, false, false, 2, true);
+
+                                                       $body = BBCode::convert($body, false, 2, true);
                                                        $body = html2plain($body, 0);
                                                        $body = ((strlen($body) > 137) ? substr($body, 0, 137) . "..." : $body);
                                                }
@@ -312,7 +319,7 @@ 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") {
@@ -339,6 +346,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 +358,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";
                }
@@ -376,6 +390,8 @@ function windowsphonepush_showsettings(&$a) {
        $device_url = get_pconfig(local_user(), 'windowsphonepush', 'device_url');
        $senditemtext = get_pconfig(local_user(), 'windowsphonepush', 'senditemtext');
        $lastpushid = get_pconfig(local_user(), 'windowsphonepush', 'lastpushid');
+       $counterunseen = get_pconfig(local_user(), 'windowsphonepush', 'counterunseen');
+       $addonversion = "2.0";
 
        if (!$device_url)
                $device_url = "";
@@ -388,7 +404,9 @@ function windowsphonepush_showsettings(&$a) {
                                'enable' => $enable, 
                                'device_url' => $device_url, 
                                'senditemtext' => $senditemtext,
-                               'lastpushid' => $lastpushid));
+                               'lastpushid' => $lastpushid, 
+                               'counterunseen' => $counterunseen, 
+                               'addonversion' => $addonversion));
 }
 
 /* 
@@ -400,9 +418,9 @@ function windowsphonepush_updatesettings(&$a) {
                return "Not Authenticated";
        }
 
-       // no updating if user hasn't enabled the plugin
-       $enable = get_pconfig(local_user(), 'windowsphonepush', 'enable');
-       if(! $enable) {
+       // no updating if user hasn't enabled the addon
+       $enable = PConfig::get(local_user(), 'windowsphonepush', 'enable');
+       if (!$enable) {
                return "Plug-in not enabled";
        }
 
@@ -434,6 +452,24 @@ function windowsphonepush_updatesettings(&$a) {
        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 addon
+       $enable = PConfig::get(local_user(), 'windowsphonepush', 'enable');
+       if (!$enable) {
+               return "Plug-in not enabled";
+       }
+
+       set_pconfig(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)
@@ -466,8 +502,9 @@ function windowsphonepush_login() {
            die('This api requires login');
        }
 
-       require_once('include/security.php');
-       authenticate_success($record); $_SESSION["allow_api"] = true;
-       call_hooks('logged_in', $a->user);
+       require_once 'include/security.php';
+       authenticate_success($record);
+       $_SESSION["allow_api"] = true;
+       Addon::callHooks('logged_in', $a->user);
 }