]> git.mxchange.org Git - friendica-addons.git/blobdiff - windowsphonepush/windowsphonepush.php
DE addon translation update
[friendica-addons.git] / windowsphonepush / windowsphonepush.php
index cba70617067a6f506cfb26caf99340a42d127620..8f289d124a1a1b1a7435936bcb535f91903be84d 100644 (file)
@@ -31,11 +31,13 @@ use Friendica\Content\Text\BBCode;
 use Friendica\Content\Text\HTML;
 use Friendica\Core\Hook;
 use Friendica\Core\Logger;
+use Friendica\Core\Renderer;
 use Friendica\Database\DBA;
 use Friendica\DI;
 use Friendica\Model\Item;
 use Friendica\Model\Post;
 use Friendica\Model\User;
+use Friendica\Network\HTTPException\UnauthorizedException;
 
 function windowsphonepush_install()
 {
@@ -85,48 +87,28 @@ function windowsphonepush_settings_post($a, $post)
 /* Called from the Addon Setting form.
  * Add our own settings info to the page.
  */
-function windowsphonepush_settings(&$a, &$s)
+function windowsphonepush_settings(App &$a, array &$data)
 {
        if (!local_user()) {
                return;
        }
 
-       /* Add our stylesheet to the page so we can make our settings look nice */
-       DI::page()['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . DI::baseUrl()->get() . '/addon/windowsphonepush/windowsphonepush.css' . '" media="all" />' . "\r\n";
-
-       /* Get the current state of our config variables */
        $enabled = DI::pConfig()->get(local_user(), 'windowsphonepush', 'enable');
-       $checked_enabled = (($enabled) ? ' checked="checked" ' : '');
-
        $senditemtext = DI::pConfig()->get(local_user(), 'windowsphonepush', 'senditemtext');
-       $checked_senditemtext = (($senditemtext) ? ' checked="checked" ' : '');
-
        $device_url = DI::pConfig()->get(local_user(), 'windowsphonepush', 'device_url');
 
-       /* Add some HTML to the existing form */
-       $s .= '<div class="settings-block">';
-       $s .= '<h3>' . DI::l10n()->t('WindowsPhonePush Settings') . '</h3>';
-
-       $s .= '<div id="windowsphonepush-enable-wrapper">';
-       $s .= '<label id="windowsphonepush-enable-label" for="windowsphonepush-enable-chk">' . DI::l10n()->t('Enable WindowsPhonePush Addon') . '</label>';
-       $s .= '<input id="windowsphonepush-enable-chk" type="checkbox" name="windowsphonepush" value="1" ' . $checked_enabled . '/>';
-       $s .= '</div><div class="clear"></div>';
-
-       $s .= '<div id="windowsphonepush-senditemtext-wrapper">';
-       $s .= '<label id="windowsphonepush-senditemtext-label" for="windowsphonepush-senditemtext-chk">' . DI::l10n()->t('Push text of new item') . '</label>';
-       $s .= '<input id="windowsphonepush-senditemtext-chk" type="checkbox" name="windowsphonepush-senditemtext" value="1" ' . $checked_senditemtext . '/>';
-       $s .= '</div><div class="clear"></div>';
-
-       /* provide a submit button - enable und senditemtext can be changed by the user */
-       $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="windowsphonepush-submit" name="windowsphonepush-submit" class="settings-submit" value="' . DI::l10n()->t('Save Settings') . '" /></div><div class="clear"></div>';
-
-       /* provide further read-only information concerning the addon (useful for */
-       $s .= '<div id="windowsphonepush-device_url-wrapper">';
-       $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>';
-
-       return;
+       $t    = Renderer::getMarkupTemplate('settings.tpl', 'addon/windowsphonepush/');
+       $html = Renderer::replaceMacros($t, [
+               '$enabled'   => ['windowsphonepush', DI::l10n()->t('Enable WindowsPhonePush Addon'), $enabled],
+               '$senditemtext'   => ['windowsphonepush-senditemtext', DI::l10n()->t('Push text of new item'), $senditemtext],
+               '$device_url'   => ['', DI::l10n()->t('Device URL'), $device_url, '', false, ' readonly'],
+       ]);
+
+       $data = [
+               'addon' => 'windowsphonepush',
+               'title' => DI::l10n()->t('WindowsPhonePush Settings'),
+               'html'  => $html,
+       ];
 }
 
 /* Cron function used to regularly check all users on the server with active windowsphonepushaddon and send
@@ -135,7 +117,7 @@ function windowsphonepush_settings(&$a, &$s)
 function windowsphonepush_cron()
 {
        // retrieve all UID's for which the addon windowsphonepush is enabled and loop through every user
-       $pconfigs = DBA::selectToArray('pconfig', ['cat' => 'windowsphonepush', 'k' => 'enable', 'v' => true]);
+       $pconfigs = DBA::selectToArray('pconfig', ['uid'], ['cat' => 'windowsphonepush', 'k' => 'enable', 'v' => true]);
        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');
@@ -149,16 +131,16 @@ function windowsphonepush_cron()
                } else {
                        // retrieve the number of unseen items and the id of the latest one (if there are more than
                        // one new entries since last poller run, only the latest one will be pushed)
-                       $count = q("SELECT count(`id`) as count, max(`id`) as max FROM `post-view` WHERE `unseen` = 1 AND `type` <> 'activity' AND `uid` = %d", intval($rr['uid']));
+                       $count = DBA::fetchFirst("SELECT count(`id`) AS count, max(`id`) AS max FROM `post-view` WHERE `unseen` AND `type` != ? AND `uid` = ?", 'activity', $rr['uid']);
 
                        // 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
-                       $res_tile = send_tile_update($device_url, "", $count[0]['count'], "");
+                       $res_tile = send_tile_update($device_url, "", $count['count'], "");
                        switch (trim($res_tile)) {
                                case "Received":
                                        // ok, count has been pushed, let's save it in personal settings
-                                       DI::pConfig()->set($rr['uid'], 'windowsphonepush', 'counterunseen', $count[0]['count']);
+                                       DI::pConfig()->set($rr['uid'], 'windowsphonepush', 'counterunseen', $count['count']);
                                        break;
                                case "QueueFull":
                                        // maximum of 30 messages reached, server rejects any further push notification until device reconnects
@@ -178,13 +160,13 @@ function windowsphonepush_cron()
                        }
 
                        // additionally user receives the text of the newest item (function checks against last successfully pushed item)
-                       if (intval($count[0]['max']) > intval($lastpushid)) {
+                       if (intval($count['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)
                                $senditemtext = DI::pConfig()->get($rr['uid'], 'windowsphonepush', 'senditemtext');
                                if ($senditemtext == 1) {
                                        // load item with the max id
-                                       $item = Post::selectFirst(['author-name', 'body', 'uri-id'], ['id' => $count[0]['max']]);
+                                       $item = Post::selectFirst(['author-name', 'body', 'uri-id'], ['id' => $count['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
@@ -216,7 +198,7 @@ function windowsphonepush_cron()
                                // 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') {
-                                       DI::pConfig()->set($rr['uid'], 'windowsphonepush', 'lastpushid', $count[0]['max']);
+                                       DI::pConfig()->set($rr['uid'], 'windowsphonepush', 'lastpushid', $count['max']);
                                }
                        }
                }
@@ -399,7 +381,7 @@ function windowsphonepush_updatesettings()
        // the user on the Windows Phone device and that device url is no longer true for the other user, so we
        // et the device_url for the OTHER user blank (should normally not occur as App should include User/server
        // in url request to Microsoft Push Notification server)
-       $pconfigs = DBA::selectToArray('pconfig', ["`uid` != ? AND `cat` = ? AND `k` = ? AND `v` = ?", local_user(), 'windowsphonepush', 'device_url', $device_url]);
+       $pconfigs = DBA::selectToArray('pconfig', ['uid'], ["`uid` != ? AND `cat` = ? AND `k` = ? AND `v` = ?", local_user(), 'windowsphonepush', 'device_url', $device_url]);
        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() . "'.");
@@ -436,22 +418,18 @@ function windowsphonepush_login(App $a)
        if (!isset($_SERVER['PHP_AUTH_USER'])) {
                Logger::info('API_login: ' . print_r($_SERVER, true));
                header('WWW-Authenticate: Basic realm="Friendica"');
-               header('HTTP/1.0 401 Unauthorized');
-               die('This api requires login');
+               throw new UnauthorizedException('This api requires login');
        }
 
-       $user_id = User::authenticate($_SERVER['PHP_AUTH_USER'], trim($_SERVER['PHP_AUTH_PW']));
-
-       if ($user_id) {
+       try {
+               $user_id = User::getIdFromPasswordAuthentication($_SERVER['PHP_AUTH_USER'], trim($_SERVER['PHP_AUTH_PW']));
                $record = DBA::selectFirst('user', [], ['uid' => $user_id]);
-       } else {
+               DI::auth()->setForUser($a, $record);
+               DI::session()->set('allow_api', true);
+               Hook::callAll('logged_in', $record);
+       } catch (Exception $ex) {
                Logger::info('API_login failure: ' . print_r($_SERVER, true));
                header('WWW-Authenticate: Basic realm="Friendica"');
-               header('HTTP/1.0 401 Unauthorized');
-               die('This api requires login');
+               throw new UnauthorizedException('This api requires login');
        }
-
-       DI::auth()->setForUser($a, $record);
-       DI::session()->set('allow_api', true);
-       Hook::callAll('logged_in', $record);
 }