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-10-29 19:40:18 -04:00
use Friendica\Core\Logger ;
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 ;
2021-01-16 19:00:32 -05:00
use Friendica\Model\Post ;
2018-07-19 22:18:02 -04:00
use Friendica\Model\User ;
2021-11-04 16:32:16 -04:00
use Friendica\Network\HTTPException\UnauthorizedException ;
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
2021-10-03 13:35:20 -04:00
Logger :: notice ( " installed 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' ]);
2020-01-18 10:54:49 -05:00
DI :: 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 ) {
2020-01-18 10:54:49 -05:00
DI :: pConfig () -> set ( local_user (), 'windowsphonepush' , 'counterunseen' , 0 );
2015-01-17 15:58:18 -05:00
}
2014-10-17 07:32:34 -04:00
2020-01-18 10:54:49 -05:00
DI :: pConfig () -> set ( local_user (), 'windowsphonepush' , 'senditemtext' , intval ( $_POST [ 'windowsphonepush-senditemtext' ]));
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 */
2019-12-30 15:53:43 -05:00
DI :: page ()[ 'htmlhead' ] .= '<link rel="stylesheet" type="text/css" href="' . DI :: baseUrl () -> get () . '/addon/windowsphonepush/windowsphonepush.css' . '" media="all" />' . " \r \n " ;
2014-10-17 07:32:34 -04:00
/* Get the current state of our config variables */
2020-01-18 10:50:56 -05:00
$enabled = DI :: pConfig () -> get ( local_user (), 'windowsphonepush' , 'enable' );
2014-10-17 07:32:34 -04:00
$checked_enabled = (( $enabled ) ? ' checked="checked" ' : '' );
2020-01-18 10:50:56 -05:00
$senditemtext = DI :: pConfig () -> get ( local_user (), 'windowsphonepush' , 'senditemtext' );
2014-10-17 07:32:34 -04:00
$checked_senditemtext = (( $senditemtext ) ? ' checked="checked" ' : '' );
2020-01-18 10:50:56 -05:00
$device_url = DI :: 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">' ;
2020-01-18 14:52:33 -05:00
$s .= '<h3>' . DI :: l10n () -> t ( 'WindowsPhonePush Settings' ) . '</h3>' ;
2014-10-17 07:32:34 -04:00
$s .= '<div id="windowsphonepush-enable-wrapper">' ;
2020-01-18 14:52:33 -05:00
$s .= '<label id="windowsphonepush-enable-label" for="windowsphonepush-enable-chk">' . DI :: 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">' ;
2020-01-18 14:52:33 -05:00
$s .= '<label id="windowsphonepush-senditemtext-label" for="windowsphonepush-senditemtext-chk">' . DI :: 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 */
2020-01-18 14:52:33 -05:00
$s .= '<div class="settings-submit-wrapper" ><input type="submit" id="windowsphonepush-submit" name="windowsphonepush-submit" class="settings-submit" value="' . DI :: 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
2021-10-03 14:58:52 -04:00
$pconfigs = DBA :: selectToArray ( 'pconfig' , [ 'uid' ], [ 'cat' => 'windowsphonepush' , 'k' => 'enable' , 'v' => true ]);
2021-10-03 13:35:20 -04:00
foreach ( $pconfigs as $rr ) {
// load stored information for the user-id of the current loop
$device_url = DI :: pConfig () -> get ( $rr [ 'uid' ], 'windowsphonepush' , 'device_url' );
$lastpushid = DI :: 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)
if (( $device_url == " " ) || ( $device_url == " NA " )) {
// no Device-URL for the user availabe, but addon is enabled --> write info to Logger
Logger :: notice ( " 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)
2021-10-07 15:30:10 -04:00
$count = DBA :: fetchFirst ( " SELECT count(`id`) AS count, max(`id`) AS max FROM `post-view` WHERE `unseen` AND `type` != ? AND `uid` = ? " , 'activity' , $rr [ 'uid' ]);
2021-10-03 13:35:20 -04: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
// user has loaded the timeline through app or website
2021-10-07 15:30:10 -04:00
$res_tile = send_tile_update ( $device_url , " " , $count [ 'count' ], " " );
2021-10-03 13:35:20 -04:00
switch ( trim ( $res_tile )) {
case " Received " :
// ok, count has been pushed, let's save it in personal settings
2021-10-07 15:30:10 -04:00
DI :: pConfig () -> set ( $rr [ 'uid' ], 'windowsphonepush' , 'counterunseen' , $count [ 'count' ]);
2021-10-03 13:35:20 -04:00
break ;
case " QueueFull " :
// maximum of 30 messages reached, server rejects any further push notification until device reconnects
Logger :: notice ( " INFO: Device-URL ' " . $device_url . " ' returns a QueueFull. " );
break ;
case " Suppressed " :
// notification received and dropped as something in app was not enabled
Logger :: notice ( " 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 :: notice ( " ERROR: specified Device-URL ' " . $device_url . " ' didn't produced any response. " );
}
2014-10-17 07:32:34 -04:00
2021-10-03 13:35:20 -04:00
// additionally user receives the text of the newest item (function checks against last successfully pushed item)
2021-10-07 15:30:10 -04:00
if ( intval ( $count [ 'max' ]) > intval ( $lastpushid )) {
2021-10-03 13:35:20 -04:00
// 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 = DI :: pConfig () -> get ( $rr [ 'uid' ], 'windowsphonepush' , 'senditemtext' );
if ( $senditemtext == 1 ) {
// load item with the max id
2021-10-07 15:30:10 -04:00
$item = Post :: selectFirst ([ 'author-name' , 'body' , 'uri-id' ], [ 'id' => $count [ 'max' ]]);
2021-10-03 13:35:20 -04:00
// as user allows to send the item, we want to show the sender of the item in the toast
// toasts are limited to one line, therefore place is limited - author shall be in
// max. 15 chars (incl. dots); author is displayed in bold font
$author = $item [ 'author-name' ];
$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
// show this in the toast (only test), therefore changing to an alternate text
// Otherwise BBcode-Tags will be eliminated and plain text cutted to 140 chars (incl. dots)
// BTW: information only possible in English
$body = $item [ 'body' ];
if ( substr ( $body , 0 , 4 ) == " [url " ) {
$body = " URL/Image ... " ;
2014-10-17 07:32:34 -04:00
} else {
2021-10-03 13:35:20 -04:00
$body = BBCode :: convertForUriId ( $item [ 'uri-id' ], $body , BBCode :: API );
$body = HTML :: toPlaintext ( $body , 0 );
$body = (( strlen ( $body ) > 137 ) ? substr ( $body , 0 , 137 ) . " ... " : $body );
2017-12-01 22:55:48 -05:00
}
2021-10-03 13:35:20 -04:00
} else {
// if user wishes higher privacy, we only display "Friendica - New timeline entry arrived"
$author = " Friendica " ;
$body = " New timeline entry arrived ... " ;
}
// only if toast push notification returns the Notification status "Received" we will update th settings with the
// new indicator max-id is checked against (QueueFull, Suppressed, N/A, Dropped shall qualify to resend
// the push notification some minutes later (BTW: if resulting in Expired for subscription status the
// 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' ) {
2021-10-07 15:30:10 -04:00
DI :: pConfig () -> set ( $rr [ 'uid' ], 'windowsphonepush' , 'lastpushid' , $count [ 'max' ]);
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 " ) {
2020-01-18 10:54:49 -05:00
DI :: pConfig () -> set ( local_user (), 'windowsphonepush' , 'device_url' , " " );
2021-10-03 13:35:20 -04:00
Logger :: notice ( " 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
2021-07-25 08:29:43 -04:00
$path = DI :: args () -> getArgv ()[ 0 ];
$path2 = DI :: args () -> getArgv ()[ 1 ];
2014-10-17 07:32:34 -04:00
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
2020-01-18 10:50:56 -05:00
$enable = DI :: pConfig () -> get ( local_user (), 'windowsphonepush' , 'enable' );
$device_url = DI :: pConfig () -> get ( local_user (), 'windowsphonepush' , 'device_url' );
$senditemtext = DI :: pConfig () -> get ( local_user (), 'windowsphonepush' , 'senditemtext' );
$lastpushid = DI :: pConfig () -> get ( local_user (), 'windowsphonepush' , 'lastpushid' );
$counterunseen = DI :: 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
2020-01-18 10:50:56 -05:00
$enable = DI :: 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 == " " ) {
2021-10-03 13:35:20 -04:00
Logger :: notice ( " 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)
2021-10-03 14:58:52 -04:00
$pconfigs = DBA :: selectToArray ( 'pconfig' , [ 'uid' ], [ " `uid` != ? AND `cat` = ? AND `k` = ? AND `v` = ? " , local_user (), 'windowsphonepush' , 'device_url' , $device_url ]);
2021-10-03 13:35:20 -04:00
foreach ( $pconfigs as $rr ) {
DI :: pConfig () -> set ( $rr [ 'uid' ], 'windowsphonepush' , 'device_url' , '' );
Logger :: notice ( " 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
}
2020-01-18 10:54:49 -05:00
DI :: 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
2021-10-03 13:35:20 -04:00
Logger :: notice ( " 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
2020-01-18 10:50:56 -05:00
$enable = DI :: 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 " ;
}
2020-01-18 10:54:49 -05:00
DI :: 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' ])) {
2021-10-03 13:35:20 -04:00
Logger :: info ( 'API_login: ' . print_r ( $_SERVER , true ));
2017-12-01 22:55:48 -05:00
header ( 'WWW-Authenticate: Basic realm="Friendica"' );
2021-11-04 16:32:16 -04:00
throw new UnauthorizedException ( 'This api requires login' );
2014-10-17 07:32:34 -04:00
}
2021-11-07 09:01:30 -05:00
try {
$user_id = User :: getIdFromPasswordAuthentication ( $_SERVER [ 'PHP_AUTH_USER' ], trim ( $_SERVER [ 'PHP_AUTH_PW' ]));
2021-11-07 10:45:27 -05:00
$record = DBA :: selectFirst ( 'user' , [], [ 'uid' => $user_id ]);
DI :: auth () -> setForUser ( $a , $record );
DI :: session () -> set ( 'allow_api' , true );
Hook :: callAll ( 'logged_in' , $record );
2021-11-07 09:01:30 -05:00
} catch ( Exception $ex ) {
2021-10-03 13:35:20 -04:00
Logger :: info ( 'API_login failure: ' . print_r ( $_SERVER , true ));
2017-12-01 22:55:48 -05:00
header ( 'WWW-Authenticate: Basic realm="Friendica"' );
2021-11-04 16:32:16 -04:00
throw new UnauthorizedException ( 'This api requires login' );
2014-10-17 07:32:34 -04:00
}
}