X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;ds=sidebyside;f=windowsphonepush%2Fwindowsphonepush.php;h=242fe96db217cd7a563924d328269b1ff7174330;hb=c9036cdc4d645988d9df591a780ec15c7dd1e7e8;hp=baa4c656e3beace9c1b015cfb223fd70edd4ea69;hpb=82e8e64d8832b588c4c7203896b46cd8e0ca3618;p=friendica-addons.git
diff --git a/windowsphonepush/windowsphonepush.php b/windowsphonepush/windowsphonepush.php
index baa4c656..242fe96d 100644
--- a/windowsphonepush/windowsphonepush.php
+++ b/windowsphonepush/windowsphonepush.php
@@ -10,7 +10,7 @@
* 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
@@ -25,27 +25,38 @@
* sets the counter back
* count only unseen elements which are not type=activity (likes and dislikes not seen as new elements)
*/
+
use Friendica\App;
+use Friendica\BaseObject;
+use Friendica\Content\Text\BBCode;
+use Friendica\Content\Text\HTML;
+use Friendica\App\Authentication;
+use Friendica\Core\Hook;
+use Friendica\Core\L10n;
+use Friendica\Core\Logger;
use Friendica\Core\PConfig;
+use Friendica\Core\Session;
+use Friendica\Database\DBA;
+use Friendica\Model\Item;
use Friendica\Model\User;
function windowsphonepush_install()
{
- /* Our plugin will attach in three places.
+ /* 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).
*/
- register_hook('cron', 'addon/windowsphonepush/windowsphonepush.php', 'windowsphonepush_cron');
+ Hook::register('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');
+ Hook::register('addon_settings', 'addon/windowsphonepush/windowsphonepush.php', 'windowsphonepush_settings');
+ Hook::register('addon_settings_post', 'addon/windowsphonepush/windowsphonepush.php', 'windowsphonepush_settings_post');
- logger("installed windowsphonepush");
+ Logger::log("installed windowsphonepush");
}
function windowsphonepush_uninstall()
@@ -53,11 +64,11 @@ function windowsphonepush_uninstall()
/* uninstall unregisters any hooks created with register_hook
* 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');
+ 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');
- logger("removed windowsphonepush");
+ Logger::log("removed windowsphonepush");
}
/* declare the windowsphonepush function so that /windowsphonepush url requests will land here */
@@ -73,7 +84,7 @@ function windowsphonepush_module()
*/
function windowsphonepush_settings_post($a, $post)
{
- if (!local_user() || (!x($_POST, 'windowsphonepush-submit'))) {
+ if (!local_user() || empty($_POST['windowsphonepush-submit'])) {
return;
}
$enable = intval($_POST['windowsphonepush']);
@@ -85,10 +96,10 @@ function windowsphonepush_settings_post($a, $post)
PConfig::set(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.
*/
function windowsphonepush_settings(&$a, &$s)
@@ -98,7 +109,7 @@ function windowsphonepush_settings(&$a, &$s)
}
/* Add our stylesheet to the page so we can make our settings look nice */
- $a->page['htmlhead'] .= '' . "\r\n";
+ $a->page['htmlhead'] .= '' . "\r\n";
/* Get the current state of our config variables */
$enabled = PConfig::get(local_user(), 'windowsphonepush', 'enable');
@@ -111,20 +122,20 @@ function windowsphonepush_settings(&$a, &$s)
/* Add some HTML to the existing form */
$s .= '
';
- $s .= '
' . t('WindowsPhonePush Settings') . '
';
+ $s .= '
' . L10n::t('WindowsPhonePush Settings') . '
';
$s .= '
';
- $s .= '';
+ $s .= '';
$s .= '';
$s .= '
';
$s .= '
';
- $s .= '';
+ $s .= '';
$s .= '';
$s .= '
';
/* provide a submit button - enable und senditemtext can be changed by the user */
- $s .= '';
+ $s .= '';
/* provide further read-only information concerning the addon (useful for */
$s .= '
';
@@ -135,12 +146,12 @@ function windowsphonepush_settings(&$a, &$s)
return;
}
-/* 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
+ // 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) {
@@ -151,8 +162,8 @@ 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
- logger("WARN: windowsphonepush is enable for user " . $rr['uid'] . ", but no Device-URL is specified for the user.");
+ // no Device-URL for the user availabe, but addon is enabled --> write info to Logger
+ Logger::log("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)
@@ -169,11 +180,11 @@ function windowsphonepush_cron()
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.");
+ Logger::log("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?");
+ Logger::log("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)
@@ -181,7 +192,7 @@ function windowsphonepush_cron()
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.");
+ Logger::log("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)
@@ -191,26 +202,24 @@ function windowsphonepush_cron()
$senditemtext = PConfig::get($rr['uid'], 'windowsphonepush', 'senditemtext');
if ($senditemtext == 1) {
// load item with the max id
- $item = q("SELECT `author-name` as author, `body` as body FROM `item` where `id` = %d", intval($count[0]['max']));
+ $item = Item::selectFirst(['author-name', 'body'], ['id' => $count[0]['max']]);
// 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[0]['author'];
+ $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[0]['body'];
+ $body = $item['body'];
if (substr($body, 0, 4) == "[url") {
$body = "URL/Image ...";
} else {
- require_once('include/bbcode.php');
- require_once("include/html2plain.php");
- $body = bbcode($body, false, false, 2, true);
- $body = html2plain($body, 0);
+ $body = BBCode::convert($body, false, 2, true);
+ $body = HTML::toPlaintext($body, 0);
$body = ((strlen($body) > 137) ? substr($body, 0, 137) . "..." : $body);
}
} else {
@@ -247,10 +256,10 @@ function send_tile_update($device_url, $image_url, $count, $title, $priority = 1
" " .
"";
- $result = send_push($device_url, array(
+ $result = send_push($device_url, [
'X-WindowsPhone-Target: token',
'X-NotificationClass: ' . $priority,
- ), $msg);
+ ], $msg);
return $result;
}
@@ -269,10 +278,10 @@ function send_toast($device_url, $title, $message, $priority = 2)
"" .
"";
- $result = send_push($device_url, array(
+ $result = send_push($device_url, [
'X-WindowsPhone-Target: toast',
'X-NotificationClass: ' . $priority,
- ), $msg);
+ ], $msg);
return $result;
}
@@ -284,11 +293,11 @@ function send_push($device_url, $headers, $msg)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HEADER, true);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $headers + array(
+ curl_setopt($ch, CURLOPT_HTTPHEADER, $headers + [
'Content-Type: text/xml',
'charset=utf-8',
'Accept: application/*',
- )
+ ]
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $msg);
@@ -300,7 +309,7 @@ function send_push($device_url, $headers, $msg)
$subscriptionStatus = get_header_value($output, 'X-SubscriptionStatus');
if ($subscriptionStatus == "Expired") {
PConfig::set(local_user(), 'windowsphonepush', 'device_url', "");
- logger("ERROR: the stored Device-URL " . $device_url . "returned an 'Expired' error, it has been deleted now.");
+ Logger::log("ERROR: the stored Device-URL " . $device_url . "returned an 'Expired' error, it has been deleted now.");
}
// the notification status shall be returned to windowsphonepush_cron (will
@@ -331,19 +340,19 @@ function windowsphonepush_content(App $a)
switch ($path2) {
case "show_settings":
windowsphonepush_showsettings($a);
- killme();
+ exit();
break;
case "update_settings":
$ret = windowsphonepush_updatesettings($a);
header("Content-Type: application/json; charset=utf-8");
- echo json_encode(array('status' => $ret));
- killme();
+ echo json_encode(['status' => $ret]);
+ exit();
break;
case "update_counterunseen":
$ret = windowsphonepush_updatecounterunseen();
header("Content-Type: application/json; charset=utf-8");
- echo json_encode(array('status' => $ret));
- killme();
+ echo json_encode(['status' => $ret]);
+ exit();
break;
default:
echo "Fehler";
@@ -374,13 +383,13 @@ function windowsphonepush_showsettings()
}
header("Content-Type: application/json");
- echo json_encode(array('uid' => local_user(),
+ echo json_encode(['uid' => local_user(),
'enable' => $enable,
'device_url' => $device_url,
'senditemtext' => $senditemtext,
'lastpushid' => $lastpushid,
'counterunseen' => $counterunseen,
- 'addonversion' => $addonversion));
+ 'addonversion' => $addonversion]);
}
/* update_settings is used to transfer the device_url from WP device to the Friendica server
@@ -392,7 +401,7 @@ function windowsphonepush_updatesettings()
return "Not Authenticated";
}
- // no updating if user hasn't enabled the plugin
+ // no updating if user hasn't enabled the addon
$enable = PConfig::get(local_user(), 'windowsphonepush', 'enable');
if (!$enable) {
return "Plug-in not enabled";
@@ -401,7 +410,7 @@ function windowsphonepush_updatesettings()
// 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 . "'");
+ Logger::log("ERROR: no valid Device-URL specified - client transferred '" . $device_url . "'");
return "No valid Device-URL specified";
}
@@ -416,13 +425,13 @@ function windowsphonepush_updatesettings()
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() . "'.");
+ 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() . "'.");
}
}
PConfig::set(local_user(), 'windowsphonepush', 'device_url', $device_url);
// output the successfull update of the device URL to the logger for error analysis if necessary
- logger("INFO: Device-URL for user '" . local_user() . "' has been updated with '" . $device_url . "'");
+ Logger::log("INFO: Device-URL for user '" . local_user() . "' has been updated with '" . $device_url . "'");
return "Device-URL updated successfully!";
}
@@ -433,7 +442,7 @@ function windowsphonepush_updatecounterunseen()
return "Not Authenticated";
}
- // no updating if user hasn't enabled the plugin
+ // no updating if user hasn't enabled the addon
$enable = PConfig::get(local_user(), 'windowsphonepush', 'enable');
if (!$enable) {
return "Plug-in not enabled";
@@ -449,7 +458,7 @@ function windowsphonepush_updatecounterunseen()
function windowsphonepush_login(App $a)
{
if (!isset($_SERVER['PHP_AUTH_USER'])) {
- logger('API_login: ' . print_r($_SERVER, true), LOGGER_DEBUG);
+ Logger::log('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');
@@ -458,16 +467,17 @@ function windowsphonepush_login(App $a)
$user_id = User::authenticate($_SERVER['PHP_AUTH_USER'], trim($_SERVER['PHP_AUTH_PW']));
if ($user_id) {
- $record = dba::select('user', [], ['uid' => $user_id], ['limit' => 1]);
+ $record = DBA::selectFirst('user', [], ['uid' => $user_id]);
} else {
- logger('API_login failure: ' . print_r($_SERVER, true), LOGGER_DEBUG);
+ Logger::log('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');
}
- require_once 'include/security.php';
- authenticate_success($record);
+ /** @var Authentication $authentication */
+ $authentication = BaseObject::getClass(Authentication::class);
+ $authentication->setForUser($a, $record);
$_SESSION["allow_api"] = true;
- call_hooks('logged_in', $a->user);
+ Hook::callAll('logged_in', $a->user);
}