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