[pumpio] Rename Item::delete to Item::markForDeletion
[friendica-addons.git/.git] / windowsphonepush / windowsphonepush.php
index 2950119..af68711 100644 (file)
 use Friendica\App;
 use Friendica\Content\Text\BBCode;
 use Friendica\Content\Text\HTML;
-use Friendica\Core\Addon;
-use Friendica\Core\Authentication;
 use Friendica\Core\Hook;
-use Friendica\Core\L10n;
 use Friendica\Core\Logger;
-use Friendica\Core\PConfig;
 use Friendica\Database\DBA;
+use Friendica\DI;
 use Friendica\Model\Item;
 use Friendica\Model\User;
 
@@ -45,15 +42,15 @@ function windowsphonepush_install()
         * The first is within cron - so the push notifications will be
         * sent every 10 minutes (or whatever is set in crontab).
         */
-       Addon::registerHook('cron', 'addon/windowsphonepush/windowsphonepush.php', 'windowsphonepush_cron');
+       Hook::register('cron', 'addon/windowsphonepush/windowsphonepush.php', 'windowsphonepush_cron');
 
        /* 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 addon and
         * define whether he allows pushing first characters of item text
         */
-       Addon::registerHook('addon_settings', 'addon/windowsphonepush/windowsphonepush.php', 'windowsphonepush_settings');
-       Addon::registerHook('addon_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::log("installed windowsphonepush");
 }
@@ -63,9 +60,9 @@ function windowsphonepush_uninstall()
        /* uninstall unregisters any hooks created with register_hook
         * during install. Don't delete data in table `pconfig`.
         */
-       Addon::unregisterHook('cron', 'addon/windowsphonepush/windowsphonepush.php', 'windowsphonepush_cron');
-       Addon::unregisterHook('addon_settings', 'addon/windowsphonepush/windowsphonepush.php', 'windowsphonepush_settings');
-       Addon::unregisterHook('addon_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::log("removed windowsphonepush");
 }
@@ -87,15 +84,15 @@ function windowsphonepush_settings_post($a, $post)
                return;
        }
        $enable = intval($_POST['windowsphonepush']);
-       PConfig::set(local_user(), 'windowsphonepush', 'enable', $enable);
+       DI::pConfig()->set(local_user(), 'windowsphonepush', 'enable', $enable);
 
        if ($enable) {
-               PConfig::set(local_user(), 'windowsphonepush', 'counterunseen', 0);
+               DI::pConfig()->set(local_user(), 'windowsphonepush', 'counterunseen', 0);
        }
 
-       PConfig::set(local_user(), 'windowsphonepush', 'senditemtext', intval($_POST['windowsphonepush-senditemtext']));
+       DI::pConfig()->set(local_user(), 'windowsphonepush', 'senditemtext', intval($_POST['windowsphonepush-senditemtext']));
 
-       info(L10n::t('WindowsPhonePush settings updated.') . EOL);
+       info(DI::l10n()->t('WindowsPhonePush settings updated.') . EOL);
 }
 
 /* Called from the Addon Setting form.
@@ -108,33 +105,33 @@ function windowsphonepush_settings(&$a, &$s)
        }
 
        /* 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->getBaseURL() . '/addon/windowsphonepush/windowsphonepush.css' . '" media="all" />' . "\r\n";
+       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 = PConfig::get(local_user(), 'windowsphonepush', 'enable');
+       $enabled = DI::pConfig()->get(local_user(), 'windowsphonepush', 'enable');
        $checked_enabled = (($enabled) ? ' checked="checked" ' : '');
 
-       $senditemtext = PConfig::get(local_user(), 'windowsphonepush', 'senditemtext');
+       $senditemtext = DI::pConfig()->get(local_user(), 'windowsphonepush', 'senditemtext');
        $checked_senditemtext = (($senditemtext) ? ' checked="checked" ' : '');
 
-       $device_url = PConfig::get(local_user(), 'windowsphonepush', 'device_url');
+       $device_url = DI::pConfig()->get(local_user(), 'windowsphonepush', 'device_url');
 
        /* Add some HTML to the existing form */
        $s .= '<div class="settings-block">';
-       $s .= '<h3>' . L10n::t('WindowsPhonePush Settings') . '</h3>';
+       $s .= '<h3>' . DI::l10n()->t('WindowsPhonePush Settings') . '</h3>';
 
        $s .= '<div id="windowsphonepush-enable-wrapper">';
-       $s .= '<label id="windowsphonepush-enable-label" for="windowsphonepush-enable-chk">' . L10n::t('Enable WindowsPhonePush Addon') . '</label>';
+       $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">' . L10n::t('Push text of new item') . '</label>';
+       $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="' . L10n::t('Save Settings') . '" /></div><div class="clear"></div>';
+       $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">';
@@ -155,8 +152,8 @@ function windowsphonepush_cron()
        if (count($r)) {
                foreach ($r as $rr) {
                        // load stored information for the user-id of the current loop
-                       $device_url = PConfig::get($rr['uid'], 'windowsphonepush', 'device_url');
-                       $lastpushid = PConfig::get($rr['uid'], 'windowsphonepush', 'lastpushid');
+                       $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)
@@ -175,7 +172,7 @@ function windowsphonepush_cron()
                                switch (trim($res_tile)) {
                                        case "Received":
                                                // ok, count has been pushed, let's save it in personal settings
-                                               PConfig::set($rr['uid'], 'windowsphonepush', 'counterunseen', $count[0]['count']);
+                                               DI::pConfig()->set($rr['uid'], 'windowsphonepush', 'counterunseen', $count[0]['count']);
                                                break;
                                        case "QueueFull":
                                                // maximum of 30 messages reached, server rejects any further push notification until device reconnects
@@ -198,7 +195,7 @@ function windowsphonepush_cron()
                                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)
-                                       $senditemtext = PConfig::get($rr['uid'], 'windowsphonepush', 'senditemtext');
+                                       $senditemtext = DI::pConfig()->get($rr['uid'], 'windowsphonepush', 'senditemtext');
                                        if ($senditemtext == 1) {
                                                // load item with the max id
                                                $item = Item::selectFirst(['author-name', 'body'], ['id' => $count[0]['max']]);
@@ -233,7 +230,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') {
-                                               PConfig::set($rr['uid'], 'windowsphonepush', 'lastpushid', $count[0]['max']);
+                                               DI::pConfig()->set($rr['uid'], 'windowsphonepush', 'lastpushid', $count[0]['max']);
                                        }
                                }
                        }
@@ -307,7 +304,7 @@ function send_push($device_url, $headers, $msg)
        // and log this fact
        $subscriptionStatus = get_header_value($output, 'X-SubscriptionStatus');
        if ($subscriptionStatus == "Expired") {
-               PConfig::set(local_user(), 'windowsphonepush', 'device_url', "");
+               DI::pConfig()->set(local_user(), 'windowsphonepush', 'device_url', "");
                Logger::log("ERROR: the stored Device-URL " . $device_url . "returned an 'Expired' error, it has been deleted now.");
        }
 
@@ -366,11 +363,11 @@ function windowsphonepush_showsettings()
                return;
        }
 
-       $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');
+       $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');
        $addonversion = "2.0";
 
        if (!$device_url) {
@@ -401,7 +398,7 @@ function windowsphonepush_updatesettings()
        }
 
        // no updating if user hasn't enabled the addon
-       $enable = PConfig::get(local_user(), 'windowsphonepush', 'enable');
+       $enable = DI::pConfig()->get(local_user(), 'windowsphonepush', 'enable');
        if (!$enable) {
                return "Plug-in not enabled";
        }
@@ -423,12 +420,12 @@ function windowsphonepush_updatesettings()
                                                `v` = '" . $device_url . "'");
        if (count($r)) {
                foreach ($r as $rr) {
-                       PConfig::set($rr['uid'], 'windowsphonepush', 'device_url', '');
+                       DI::pConfig()->set($rr['uid'], 'windowsphonepush', 'device_url', '');
                        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);
+       DI::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::log("INFO: Device-URL for user '" . local_user() . "' has been updated with '" . $device_url . "'");
        return "Device-URL updated successfully!";
@@ -442,12 +439,12 @@ function windowsphonepush_updatecounterunseen()
        }
 
        // no updating if user hasn't enabled the addon
-       $enable = PConfig::get(local_user(), 'windowsphonepush', 'enable');
+       $enable = DI::pConfig()->get(local_user(), 'windowsphonepush', 'enable');
        if (!$enable) {
                return "Plug-in not enabled";
        }
 
-       PConfig::set(local_user(), 'windowsphonepush', 'counterunseen', 0);
+       DI::pConfig()->set(local_user(), 'windowsphonepush', 'counterunseen', 0);
        return "Counter set to zero";
 }
 
@@ -474,7 +471,7 @@ function windowsphonepush_login(App $a)
                die('This api requires login');
        }
 
-       Authentication::setAuthenticatedSessionForUser($record);
-       $_SESSION["allow_api"] = true;
+       DI::auth()->setForUser($a, $record);
+       DI::session()->set('allow_api', true);
        Hook::callAll('logged_in', $a->user);
 }