2014-10-17 13:32:34 +02:00
< ? php
2017-12-02 04:55:48 +01:00
2014-10-17 13:32:34 +02:00
/**
* 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 )
2015-01-17 21:58:18 +01:00
* Version : 2.0
2014-10-17 13:32:34 +02:00
* Author : Gerhard Seeber < http :// friendica . seeber . at / profile / admin >
2017-12-02 04:55:48 +01:00
*
*
2014-10-17 13:32:34 +02:00
* Pre - requisite : Windows Phone mobile device ( at least WP 7.0 )
* Friendica mobile app on Windows Phone
*
2018-01-20 14:57:41 +01:00
* When addon is installed , the system calls the addon
2014-10-17 13:32:34 +02:00
* name_install () function , located in 'addon/name/name.php' ,
* where 'name' is the name of the addon .
2017-12-02 04:55:48 +01:00
* If the addon is removed from the configuration list , the
2014-10-17 13:32:34 +02:00
* system will call the name_uninstall () function .
*
2014-12-14 22:46:07 +01:00
* Version history :
2017-12-02 04:55:48 +01:00
* 1.1 : addon crashed on php versions >= 5.4 as of removed deprecated call - time
2014-12-14 22:46:07 +01:00
* pass - by - reference used in function calls within function windowsphonepush_content
2015-01-17 21:58:18 +01:00
* 2.0 : adaption for supporting emphasizing new entries in app ( count on tile cannot be read out ,
2017-12-02 04:55:48 +01:00
* so we need to retrieve counter through show_settings secondly ) . Provide new function for
2015-01-17 21:58:18 +01:00
* 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 )
2014-10-17 13:32:34 +02:00
*/
2017-12-02 04:55:48 +01:00
use Friendica\App ;
2018-01-17 01:51:12 +01:00
use Friendica\Core\Addon ;
2018-01-22 20:03:11 +01:00
use Friendica\Core\L10n ;
2017-11-07 00:55:24 +01:00
use Friendica\Core\PConfig ;
2017-12-02 04:55:48 +01:00
use Friendica\Model\User ;
2014-10-17 13:32:34 +02:00
2017-12-02 04:55:48 +01:00
function windowsphonepush_install ()
{
2018-01-20 14:57:41 +01:00
/* Our addon will attach in three places .
2017-12-02 04:55:48 +01:00
* The first is within cron - so the push notifications will be
2014-10-17 13:32:34 +02:00
* sent every 10 minutes ( or whatever is set in crontab ) .
*/
2018-01-17 01:51:12 +01:00
Addon :: registerHook ( 'cron' , 'addon/windowsphonepush/windowsphonepush.php' , 'windowsphonepush_cron' );
2014-10-17 13:32:34 +02:00
2018-01-20 14:57:41 +01:00
/* Then we ' ll attach into the addon settings page , and also the
2014-10-17 13:32:34 +02:00
* settings post hook so that we can create and update
2018-01-20 14:57:41 +01:00
* user preferences . User shall be able to activate the addon and
2014-10-17 13:32:34 +02:00
* define whether he allows pushing first characters of item text
*/
2018-01-20 14:57:41 +01:00
Addon :: registerHook ( 'addon_settings' , 'addon/windowsphonepush/windowsphonepush.php' , 'windowsphonepush_settings' );
Addon :: registerHook ( 'addon_settings_post' , 'addon/windowsphonepush/windowsphonepush.php' , 'windowsphonepush_settings_post' );
2014-10-17 13:32:34 +02:00
logger ( " installed windowsphonepush " );
}
2017-12-02 04:55:48 +01:00
function windowsphonepush_uninstall ()
{
/* uninstall unregisters any hooks created with register_hook
2014-10-17 13:32:34 +02:00
* during install . Don ' t delete data in table `pconfig` .
*/
2018-01-17 01:51:12 +01:00
Addon :: unregisterHook ( 'cron' , 'addon/windowsphonepush/windowsphonepush.php' , 'windowsphonepush_cron' );
2018-01-20 14:57:41 +01:00
Addon :: unregisterHook ( 'addon_settings' , 'addon/windowsphonepush/windowsphonepush.php' , 'windowsphonepush_settings' );
Addon :: unregisterHook ( 'addon_settings_post' , 'addon/windowsphonepush/windowsphonepush.php' , 'windowsphonepush_settings_post' );
2014-10-17 13:32:34 +02:00
logger ( " removed windowsphonepush " );
}
/* declare the windowsphonepush function so that /windowsphonepush url requests will land here */
2017-12-02 04:55:48 +01:00
function windowsphonepush_module ()
{
2014-10-17 13:32:34 +02:00
2017-12-02 04:55:48 +01:00
}
2014-10-17 13:32:34 +02:00
2017-12-02 04:55:48 +01:00
/* Callback from the settings post function .
2014-10-17 13:32:34 +02:00
* $post contains the $_POST array .
* We will make sure we ' ve got a valid user account
* and if so set our configuration setting for this person .
*/
2017-12-02 04:55:48 +01:00
function windowsphonepush_settings_post ( $a , $post )
{
if ( ! local_user () || ( ! x ( $_POST , 'windowsphonepush-submit' ))) {
2014-10-17 13:32:34 +02:00
return ;
2017-12-02 04:55:48 +01:00
}
2015-01-17 21:58:18 +01:00
$enable = intval ( $_POST [ 'windowsphonepush' ]);
2017-12-02 04:55:48 +01:00
PConfig :: set ( local_user (), 'windowsphonepush' , 'enable' , $enable );
2015-01-17 21:58:18 +01:00
2017-12-02 04:55:48 +01:00
if ( $enable ) {
PConfig :: set ( local_user (), 'windowsphonepush' , 'counterunseen' , 0 );
2015-01-17 21:58:18 +01:00
}
2014-10-17 13:32:34 +02:00
2017-12-02 04:55:48 +01:00
PConfig :: set ( local_user (), 'windowsphonepush' , 'senditemtext' , intval ( $_POST [ 'windowsphonepush-senditemtext' ]));
2014-10-17 13:32:34 +02:00
2018-01-24 04:59:20 +01:00
info ( L10n :: t ( 'WindowsPhonePush settings updated.' ) . EOL );
2014-10-17 13:32:34 +02:00
}
2018-01-20 14:57:41 +01:00
/* Called from the Addon Setting form .
2014-10-17 13:32:34 +02:00
* Add our own settings info to the page .
*/
2017-12-02 04:55:48 +01:00
function windowsphonepush_settings ( & $a , & $s )
{
if ( ! local_user ()) {
2014-10-17 13:32:34 +02:00
return ;
2017-12-02 04:55:48 +01:00
}
2014-10-17 13:32:34 +02:00
/* Add our stylesheet to the page so we can make our settings look nice */
$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 */
2017-12-02 04:55:48 +01:00
$enabled = PConfig :: get ( local_user (), 'windowsphonepush' , 'enable' );
2014-10-17 13:32:34 +02:00
$checked_enabled = (( $enabled ) ? ' checked="checked" ' : '' );
2017-11-07 00:55:24 +01:00
$senditemtext = PConfig :: get ( local_user (), 'windowsphonepush' , 'senditemtext' );
2014-10-17 13:32:34 +02:00
$checked_senditemtext = (( $senditemtext ) ? ' checked="checked" ' : '' );
2017-11-07 00:55:24 +01:00
$device_url = PConfig :: get ( local_user (), 'windowsphonepush' , 'device_url' );
2014-10-17 13:32:34 +02:00
/* Add some HTML to the existing form */
$s .= '<div class="settings-block">' ;
2018-01-22 20:03:11 +01:00
$s .= '<h3>' . L10n :: t ( 'WindowsPhonePush Settings' ) . '</h3>' ;
2014-10-17 13:32:34 +02:00
$s .= '<div id="windowsphonepush-enable-wrapper">' ;
2018-01-22 20:03:11 +01:00
$s .= '<label id="windowsphonepush-enable-label" for="windowsphonepush-enable-chk">' . L10n :: t ( 'Enable WindowsPhonePush Addon' ) . '</label>' ;
2014-10-17 13:32:34 +02:00
$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">' ;
2018-01-22 20:03:11 +01:00
$s .= '<label id="windowsphonepush-senditemtext-label" for="windowsphonepush-senditemtext-chk">' . L10n :: t ( 'Push text of new item' ) . '</label>' ;
2014-10-17 13:32:34 +02:00
$s .= '<input id="windowsphonepush-senditemtext-chk" type="checkbox" name="windowsphonepush-senditemtext" value="1" ' . $checked_senditemtext . '/>' ;
$s .= '</div><div class="clear"></div>' ;
2017-12-02 04:55:48 +01:00
/* provide a submit button - enable und senditemtext can be changed by the user */
2018-01-22 20:03:11 +01:00
$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>' ;
2014-10-17 13:32:34 +02:00
/* provide further read-only information concerning the addon (useful for */
$s .= '<div id="windowsphonepush-device_url-wrapper">' ;
$s .= '<label id="windowsphonepush-device_url-label" for="windowsphonepush-device_url-text">Device-URL</label>' ;
$s .= '<input id="windowsphonepush-device_url-text" type="text" readonly value=' . $device_url . '/>' ;
$s .= '</div><div class="clear"></div></div>' ;
2017-12-02 04:55:48 +01:00
return ;
2014-10-17 13:32:34 +02:00
}
2018-01-20 14:57:41 +01:00
/* Cron function used to regularly check all users on the server with active windowsphonepushaddon and send
2014-10-17 13:32:34 +02:00
* notifications to the Microsoft servers and consequently to the Windows Phone device
*/
2017-12-02 04:55:48 +01:00
function windowsphonepush_cron ()
{
2018-01-20 14:57:41 +01:00
// retrieve all UID's for which the addon windowsphonepush is enabled and loop through every user
2014-10-17 13:32:34 +02:00
$r = q ( " SELECT * FROM `pconfig` WHERE `cat` = 'windowsphonepush' AND `k` = 'enable' AND `v` = 1 " );
2017-12-02 04:55:48 +01:00
if ( count ( $r )) {
foreach ( $r as $rr ) {
2014-10-17 13:32:34 +02:00
// load stored information for the user-id of the current loop
2017-11-07 00:55:24 +01:00
$device_url = PConfig :: get ( $rr [ 'uid' ], 'windowsphonepush' , 'device_url' );
$lastpushid = PConfig :: get ( $rr [ 'uid' ], 'windowsphonepush' , 'lastpushid' );
2014-10-17 13:32:34 +02:00
2017-12-02 04:55:48 +01:00
// pushing only possible if device_url (the URI on Microsoft server) is available or not "NA" (which will be sent
2014-10-17 13:32:34 +02:00
// by app if user has switched the server setting in app - sending blank not possible as this would return an update error)
2017-12-02 04:55:48 +01:00
if (( $device_url == " " ) || ( $device_url == " NA " )) {
2018-01-20 14:57:41 +01:00
// no Device-URL for the user availabe, but addon is enabled --> write info to Logger
2014-10-17 13:32:34 +02:00
logger ( " WARN: windowsphonepush is enable for user " . $rr [ 'uid' ] . " , but no Device-URL is specified for the user. " );
} else {
2017-12-02 04:55:48 +01:00
// retrieve the number of unseen items and the id of the latest one (if there are more than
2014-10-17 13:32:34 +02:00
// one new entries since last poller run, only the latest one will be pushed)
2017-12-02 04:55:48 +01:00
$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' ]));
2014-10-17 13:32:34 +02:00
2017-12-02 04:55:48 +01:00
// send number of unseen items to the device (the number will be displayed on Start screen until
// App will be started by user) - this update will be sent every 10 minutes to update the number to 0 if
2014-10-17 13:32:34 +02:00
// user has loaded the timeline through app or website
$res_tile = send_tile_update ( $device_url , " " , $count [ 0 ][ 'count' ], " " );
switch ( trim ( $res_tile )) {
case " Received " :
2017-12-02 04:55:48 +01:00
// ok, count has been pushed, let's save it in personal settings
2017-11-07 00:55:24 +01:00
PConfig :: set ( $rr [ 'uid' ], 'windowsphonepush' , 'counterunseen' , $count [ 0 ][ 'count' ]);
2014-10-17 13:32:34 +02:00
break ;
case " QueueFull " :
// maximum of 30 messages reached, server rejects any further push notification until device reconnects
logger ( " INFO: Device-URL ' " . $device_url . " ' returns a QueueFull. " );
break ;
case " Suppressed " :
// notification received and dropped as something in app was not enabled
logger ( " WARN. Device-URL ' " . $device_url . " ' returns a Suppressed. Unexpected error in Mobile App? " );
break ;
case " Dropped " :
// mostly combines with Expired, in that case Device-URL will be deleted from pconfig (function send_push)
break ;
default :
// error, mostly called by "" which means that the url (not "" which has been checked)
// didn't not received Microsoft Notification Server -> wrong url
logger ( " ERROR: specified Device-URL ' " . $device_url . " ' didn't produced any response. " );
}
// additionally user receives the text of the newest item (function checks against last successfully pushed item)
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)
2017-11-07 00:55:24 +01:00
$senditemtext = PConfig :: get ( $rr [ 'uid' ], 'windowsphonepush' , 'senditemtext' );
2014-10-17 13:32:34 +02:00
if ( $senditemtext == 1 ) {
// load item with the max id
2017-12-02 04:55:48 +01:00
$item = q ( " SELECT `author-name` as author, `body` as body FROM `item` where `id` = %d " , intval ( $count [ 0 ][ 'max' ]));
2014-10-17 13:32:34 +02:00
// as user allows to send the item, we want to show the sender of the item in the toast
2017-12-02 04:55:48 +01:00
// toasts are limited to one line, therefore place is limited - author shall be in
2014-10-17 13:32:34 +02:00
// max. 15 chars (incl. dots); author is displayed in bold font
$author = $item [ 0 ][ 'author' ];
$author = (( strlen ( $author ) > 12 ) ? substr ( $author , 0 , 12 ) . " ... " : $author );
// normally we show the body of the item, however if it is an url or an image we cannot
2017-12-02 04:55:48 +01:00
// show this in the toast (only test), therefore changing to an alternate text
2014-10-17 13:32:34 +02:00
// Otherwise BBcode-Tags will be eliminated and plain text cutted to 140 chars (incl. dots)
// BTW: information only possible in English
$body = $item [ 0 ][ 'body' ];
2017-12-02 04:55:48 +01:00
if ( substr ( $body , 0 , 4 ) == " [url " ) {
2014-10-17 13:32:34 +02:00
$body = " URL/Image ... " ;
2017-12-02 04:55:48 +01:00
} else {
2014-10-17 13:32:34 +02:00
require_once ( 'include/bbcode.php' );
require_once ( " include/html2plain.php " );
$body = bbcode ( $body , false , false , 2 , true );
$body = html2plain ( $body , 0 );
$body = (( strlen ( $body ) > 137 ) ? substr ( $body , 0 , 137 ) . " ... " : $body );
}
} else {
2017-12-02 04:55:48 +01:00
// if user wishes higher privacy, we only display "Friendica - New timeline entry arrived"
2014-10-17 13:32:34 +02:00
$author = " Friendica " ;
$body = " New timeline entry arrived ... " ;
}
2017-12-02 04:55:48 +01:00
// only if toast push notification returns the Notification status "Received" we will update th settings with the
2014-10-17 13:32:34 +02:00
// new indicator max-id is checked against (QueueFull, Suppressed, N/A, Dropped shall qualify to resend
2017-12-02 04:55:48 +01:00
// the push notification some minutes later (BTW: if resulting in Expired for subscription status the
2014-10-17 13:32:34 +02:00
// device_url will be deleted (no further try on this url, see send_push)
// 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' ) {
2017-11-07 00:55:24 +01:00
PConfig :: set ( $rr [ 'uid' ], 'windowsphonepush' , 'lastpushid' , $count [ 0 ][ 'max' ]);
2017-12-02 04:55:48 +01:00
}
2014-10-17 13:32:34 +02:00
}
}
}
}
}
2017-12-02 04:55:48 +01:00
/* Tile push notification change the number in the icon of the App in Start Screen of
2014-10-17 13:32:34 +02:00
* a Windows Phone Device , Image could be changed , not used for App " Friendica Mobile "
*/
2017-12-02 04:55:48 +01:00
function send_tile_update ( $device_url , $image_url , $count , $title , $priority = 1 )
{
2014-10-17 13:32:34 +02:00
$msg = " <?xml version= \" 1.0 \" encoding= \" utf-8 \" ?> " .
" <wp:Notification xmlns:wp= \" WPNotification \" > " .
2017-12-02 04:55:48 +01:00
" <wp:Tile> " .
" <wp:BackgroundImage> " . $image_url . " </wp:BackgroundImage> " .
" <wp:Count> " . $count . " </wp:Count> " .
" <wp:Title> " . $title . " </wp:Title> " .
" </wp:Tile> " .
2014-10-17 13:32:34 +02:00
" </wp:Notification> " ;
2018-01-15 14:15:33 +01:00
$result = send_push ( $device_url , [
2014-10-17 13:32:34 +02:00
'X-WindowsPhone-Target: token' ,
'X-NotificationClass: ' . $priority ,
2018-01-15 14:15:33 +01:00
], $msg );
2014-10-17 13:32:34 +02:00
return $result ;
}
2017-12-02 04:55:48 +01:00
/* Toast push notification send information to the top of the display
2014-10-17 13:32:34 +02:00
* if the user is not currently using the Friendica Mobile App , however
* there is only one line for displaying the information
*/
2017-12-02 04:55:48 +01:00
function send_toast ( $device_url , $title , $message , $priority = 2 )
{
$msg = " <?xml version= \" 1.0 \" encoding= \" utf-8 \" ?> " .
2014-10-17 13:32:34 +02:00
" <wp:Notification xmlns:wp= \" WPNotification \" > " .
2017-12-02 04:55:48 +01:00
" <wp:Toast> " .
" <wp:Text1> " . $title . " </wp:Text1> " .
" <wp:Text2> " . $message . " </wp:Text2> " .
" <wp:Param></wp:Param> " .
" </wp:Toast> " .
2014-10-17 13:32:34 +02:00
" </wp:Notification> " ;
2018-01-15 14:15:33 +01:00
$result = send_push ( $device_url , [
2014-10-17 13:32:34 +02:00
'X-WindowsPhone-Target: toast' ,
2017-12-02 04:55:48 +01:00
'X-NotificationClass: ' . $priority ,
2018-01-15 14:15:33 +01:00
], $msg );
2014-10-17 13:32:34 +02:00
return $result ;
}
2017-12-02 04:55:48 +01:00
// General function to send the push notification via cURL
function send_push ( $device_url , $headers , $msg )
{
2014-10-17 13:32:34 +02:00
$ch = curl_init ();
curl_setopt ( $ch , CURLOPT_URL , $device_url );
curl_setopt ( $ch , CURLOPT_RETURNTRANSFER , 1 );
curl_setopt ( $ch , CURLOPT_POST , true );
2017-12-02 04:55:48 +01:00
curl_setopt ( $ch , CURLOPT_HEADER , true );
2018-01-15 14:15:33 +01:00
curl_setopt ( $ch , CURLOPT_HTTPHEADER , $headers + [
2017-12-02 04:55:48 +01:00
'Content-Type: text/xml' ,
'charset=utf-8' ,
'Accept: application/*' ,
2018-01-15 14:15:33 +01:00
]
2017-12-02 04:55:48 +01:00
);
2014-10-17 13:32:34 +02:00
curl_setopt ( $ch , CURLOPT_POSTFIELDS , $msg );
$output = curl_exec ( $ch );
curl_close ( $ch );
2014-12-14 22:46:07 +01:00
// if we received "Expired" from Microsoft server we will delete the obsolete device-URL
2014-10-17 13:32:34 +02:00
// and log this fact
$subscriptionStatus = get_header_value ( $output , 'X-SubscriptionStatus' );
if ( $subscriptionStatus == " Expired " ) {
2017-12-02 04:55:48 +01:00
PConfig :: set ( local_user (), 'windowsphonepush' , 'device_url' , " " );
2014-10-17 13:32:34 +02:00
logger ( " ERROR: the stored Device-URL " . $device_url . " returned an 'Expired' error, it has been deleted now. " );
}
2017-12-02 04:55:48 +01:00
// the notification status shall be returned to windowsphonepush_cron (will
2014-10-17 13:32:34 +02:00
// update settings if 'Received' otherwise keep old value in settings (on QueuedFull. Suppressed, N/A, Dropped)
$notificationStatus = get_header_value ( $output , 'X-NotificationStatus' );
return $notificationStatus ;
2017-12-02 04:55:48 +01:00
}
2014-10-17 13:32:34 +02:00
2017-12-02 04:55:48 +01:00
// helper function to receive statuses from webresponse of Microsoft server
function get_header_value ( $content , $header )
{
2014-10-17 13:32:34 +02:00
return preg_match_all ( " / $header : (.*)/i " , $content , $match ) ? $match [ 1 ][ 0 ] : " " ;
}
2017-12-02 04:55:48 +01:00
/* reading information from url and deciding which function to start
2014-10-17 13:32:34 +02:00
* show_settings = delivering settings to check
* update_settings = set the device_url
2015-01-17 21:58:18 +01:00
* update_counterunseen = set counter for unseen elements to zero
2014-10-17 13:32:34 +02:00
*/
2017-12-02 04:55:48 +01:00
function windowsphonepush_content ( App $a )
{
2014-10-17 13:32:34 +02:00
// Login with the specified Network credentials (like in api.php)
2017-12-02 04:55:48 +01:00
windowsphonepush_login ( $a );
2014-10-17 13:32:34 +02:00
$path = $a -> argv [ 0 ];
$path2 = $a -> argv [ 1 ];
if ( $path == " windowsphonepush " ) {
switch ( $path2 ) {
case " show_settings " :
2014-12-14 22:46:07 +01:00
windowsphonepush_showsettings ( $a );
2014-10-17 13:32:34 +02:00
killme ();
break ;
case " update_settings " :
2014-12-14 22:46:07 +01:00
$ret = windowsphonepush_updatesettings ( $a );
2017-12-02 04:55:48 +01:00
header ( " Content-Type: application/json; charset=utf-8 " );
2018-01-15 14:15:33 +01:00
echo json_encode ([ 'status' => $ret ]);
2017-12-02 04:55:48 +01:00
killme ();
2014-10-17 13:32:34 +02:00
break ;
2015-01-17 21:58:18 +01:00
case " update_counterunseen " :
$ret = windowsphonepush_updatecounterunseen ();
header ( " Content-Type: application/json; charset=utf-8 " );
2018-01-15 14:15:33 +01:00
echo json_encode ([ 'status' => $ret ]);
2015-01-17 21:58:18 +01:00
killme ();
break ;
2014-10-17 13:32:34 +02:00
default :
echo " Fehler " ;
}
}
}
2017-12-02 04:55:48 +01:00
// return settings for windowsphonepush addon to be able to check them in WP app
function windowsphonepush_showsettings ()
{
if ( ! local_user ()) {
2014-10-17 13:32:34 +02:00
return ;
2017-12-02 04:55:48 +01:00
}
2014-10-17 13:32:34 +02:00
2017-11-07 00:55:24 +01:00
$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' );
2015-01-17 21:58:18 +01:00
$addonversion = " 2.0 " ;
2014-10-17 13:32:34 +02:00
2017-12-02 04:55:48 +01:00
if ( ! $device_url ) {
2014-10-17 13:32:34 +02:00
$device_url = " " ;
2017-12-02 04:55:48 +01:00
}
2014-10-17 13:32:34 +02:00
2017-12-02 04:55:48 +01:00
if ( ! $lastpushid ) {
2014-10-17 13:32:34 +02:00
$lastpushid = 0 ;
2017-12-02 04:55:48 +01:00
}
2014-10-17 13:32:34 +02:00
2017-12-02 04:55:48 +01:00
header ( " Content-Type: application/json " );
2018-01-15 14:15:33 +01:00
echo json_encode ([ 'uid' => local_user (),
2017-12-02 04:55:48 +01:00
'enable' => $enable ,
'device_url' => $device_url ,
'senditemtext' => $senditemtext ,
'lastpushid' => $lastpushid ,
'counterunseen' => $counterunseen ,
2018-01-15 14:15:33 +01:00
'addonversion' => $addonversion ]);
2014-10-17 13:32:34 +02:00
}
2017-12-02 04:55:48 +01:00
/* update_settings is used to transfer the device_url from WP device to the Friendica server
2014-10-17 13:32:34 +02:00
* return the status of the operation to the server
*/
2017-12-02 04:55:48 +01:00
function windowsphonepush_updatesettings ()
{
if ( ! local_user ()) {
2014-10-17 13:32:34 +02:00
return " Not Authenticated " ;
}
2018-01-20 14:57:41 +01:00
// no updating if user hasn't enabled the addon
2017-11-07 00:55:24 +01:00
$enable = PConfig :: get ( local_user (), 'windowsphonepush' , 'enable' );
2017-12-02 04:55:48 +01:00
if ( ! $enable ) {
2014-10-17 13:32:34 +02:00
return " Plug-in not enabled " ;
}
// check if sent url is empty - don't save and send return code to app
$device_url = $_POST [ 'deviceurl' ];
if ( $device_url == " " ) {
logger ( " ERROR: no valid Device-URL specified - client transferred ' " . $device_url . " ' " );
return " No valid Device-URL specified " ;
}
2017-12-02 04:55:48 +01:00
// check if sent url is already stored in database for another user, we assume that there was a change of
2014-10-17 13:32:34 +02:00
// the user on the Windows Phone device and that device url is no longer true for the other user, so we
2017-12-02 04:55:48 +01:00
// et the device_url for the OTHER user blank (should normally not occur as App should include User/server
2014-10-17 13:32:34 +02:00
// in url request to Microsoft Push Notification server)
2017-12-02 04:55:48 +01:00
$r = q ( " SELECT * FROM `pconfig` WHERE `uid` <> " . local_user () . " AND
`cat` = 'windowsphonepush' AND
`k` = 'device_url' AND
2014-10-17 13:32:34 +02:00
`v` = '" . $device_url . "' " );
2017-12-02 04:55:48 +01:00
if ( count ( $r )) {
foreach ( $r as $rr ) {
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 () . " '. " );
2014-10-17 13:32:34 +02:00
}
}
2017-12-02 04:55:48 +01:00
PConfig :: set ( local_user (), 'windowsphonepush' , 'device_url' , $device_url );
2014-10-17 13:32:34 +02:00
// 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! " ;
}
2017-12-02 04:55:48 +01:00
// update_counterunseen is used to reset the counter to zero from Windows Phone app
function windowsphonepush_updatecounterunseen ()
{
if ( ! local_user ()) {
2015-01-17 21:58:18 +01:00
return " Not Authenticated " ;
}
2018-01-20 14:57:41 +01:00
// no updating if user hasn't enabled the addon
2017-11-07 00:55:24 +01:00
$enable = PConfig :: get ( local_user (), 'windowsphonepush' , 'enable' );
2017-12-02 04:55:48 +01:00
if ( ! $enable ) {
2015-01-17 21:58:18 +01:00
return " Plug-in not enabled " ;
}
2017-12-02 04:55:48 +01:00
PConfig :: set ( local_user (), 'windowsphonepush' , 'counterunseen' , 0 );
2015-01-17 21:58:18 +01:00
return " Counter set to zero " ;
}
2017-12-02 04:55:48 +01:00
/* helper function to login to the server with the specified Network credentials
2014-10-17 13:32:34 +02:00
* ( mainly copied from api . php )
*/
2017-12-02 04:55:48 +01:00
function windowsphonepush_login ( App $a )
{
2014-10-17 13:32:34 +02:00
if ( ! isset ( $_SERVER [ 'PHP_AUTH_USER' ])) {
2017-12-02 04:55:48 +01:00
logger ( 'API_login: ' . print_r ( $_SERVER , true ), LOGGER_DEBUG );
header ( 'WWW-Authenticate: Basic realm="Friendica"' );
header ( 'HTTP/1.0 401 Unauthorized' );
die ( 'This api requires login' );
2014-10-17 13:32:34 +02:00
}
2017-12-02 05:03:49 +01:00
$user_id = User :: authenticate ( $_SERVER [ 'PHP_AUTH_USER' ], trim ( $_SERVER [ 'PHP_AUTH_PW' ]));
2014-10-17 13:32:34 +02:00
2017-12-02 05:03:49 +01:00
if ( $user_id ) {
2018-01-10 14:37:11 +01:00
$record = dba :: selectFirst ( 'user' , [], [ 'uid' => $user_id ]);
2014-10-17 13:32:34 +02:00
} else {
2017-12-02 04:55:48 +01:00
logger ( 'API_login failure: ' . print_r ( $_SERVER , true ), LOGGER_DEBUG );
header ( 'WWW-Authenticate: Basic realm="Friendica"' );
header ( 'HTTP/1.0 401 Unauthorized' );
die ( 'This api requires login' );
2014-10-17 13:32:34 +02:00
}
2017-12-02 04:55:48 +01:00
require_once 'include/security.php' ;
authenticate_success ( $record );
$_SESSION [ " allow_api " ] = true ;
2018-01-21 06:11:18 +01:00
Addon :: callHooks ( 'logged_in' , $a -> user );
2014-10-17 13:32:34 +02:00
}