]> git.mxchange.org Git - friendica-addons.git/blobdiff - ifttt/ifttt.php
[pageheader] code cleanup
[friendica-addons.git] / ifttt / ifttt.php
index 199aace430630b53d3626d6fc0f0c556cc236cf1..3bb55f3568b99362a5585442e92da12f88ed4b71 100644 (file)
@@ -7,23 +7,26 @@
  * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
  */
 require_once 'mod/item.php';
-require_once 'include/items.php';
-require_once 'include/text.php';
-
 use Friendica\App;
+use Friendica\Core\Hook;
+use Friendica\Core\L10n;
+use Friendica\Core\Logger;
 use Friendica\Core\PConfig;
-use Friendica\Database\DBM;
+use Friendica\Core\Protocol;
+use Friendica\Database\DBA;
+use Friendica\Model\Item;
+use Friendica\Util\Strings;
 
 function ifttt_install()
 {
-       register_hook('connector_settings', 'addon/ifttt/ifttt.php', 'ifttt_settings');
-       register_hook('connector_settings_post', 'addon/ifttt/ifttt.php', 'ifttt_settings_post');
+       Hook::register('connector_settings', 'addon/ifttt/ifttt.php', 'ifttt_settings');
+       Hook::register('connector_settings_post', 'addon/ifttt/ifttt.php', 'ifttt_settings_post');
 }
 
 function ifttt_uninstall()
 {
-       unregister_hook('connector_settings', 'addon/ifttt/ifttt.php', 'ifttt_settings');
-       unregister_hook('connector_settings_post', 'addon/ifttt/ifttt.php', 'ifttt_settings_post');
+       Hook::unregister('connector_settings', 'addon/ifttt/ifttt.php', 'ifttt_settings');
+       Hook::unregister('connector_settings_post', 'addon/ifttt/ifttt.php', 'ifttt_settings_post');
 }
 
 function ifttt_module()
@@ -45,46 +48,46 @@ function ifttt_settings(App $a, &$s)
        $key = PConfig::get(local_user(), 'ifttt', 'key');
 
        if (!$key) {
-               $key = random_string(20);
+               $key = Strings::getRandomHex(20);
                PConfig::set(local_user(), 'ifttt', 'key', $key);
        }
 
        $s .= '<span id="settings_ifttt_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_ifttt_expanded\'); openClose(\'settings_ifttt_inflated\');">';
-       $s .= '<img class="connector" src="addon/ifttt/ifttt.png" /><h3 class="connector">' . t('IFTTT Mirror') . '</h3>';
+       $s .= '<img class="connector" src="addon/ifttt/ifttt.png" /><h3 class="connector">' . L10n::t('IFTTT Mirror') . '</h3>';
        $s .= '</span>';
        $s .= '<div id="settings_ifttt_expanded" class="settings-block" style="display: none;">';
        $s .= '<span class="fakelink" onclick="openClose(\'settings_ifttt_expanded\'); openClose(\'settings_ifttt_inflated\');">';
-       $s .= '<img class="connector" src="addon/ifttt/ifttt.png" /><h3 class="connector">' . t('IFTTT Mirror') . '</h3>';
+       $s .= '<img class="connector" src="addon/ifttt/ifttt.png" /><h3 class="connector">' . L10n::t('IFTTT Mirror') . '</h3>';
        $s .= '</span>';
 
        $s .= '<div id="ifttt-configuration-wrapper">';
-       $s .= '<p>' . t('Create an account at <a href="http://www.ifttt.com">IFTTT</a>. Create three Facebook recipes that are connected with <a href="https://ifttt.com/maker">Maker</a> (In the form "if Facebook then Maker") with the following parameters:') . '</p>';
+       $s .= '<p>' . L10n::t('Create an account at <a href="http://www.ifttt.com">IFTTT</a>. Create three Facebook recipes that are connected with <a href="https://ifttt.com/maker">Maker</a> (In the form "if Facebook then Maker") with the following parameters:') . '</p>';
        $s .= '<h4>URL</h4>';
-       $s .= '<p>' . $a->get_baseurl() . '/ifttt/' . $a->user['nickname'] . '</p>';
+       $s .= '<p>' . $a->getBaseURL() . '/ifttt/' . $a->user['nickname'] . '</p>';
        $s .= '<h4>Method</h4>';
        $s .= '<p>POST</p>';
        $s .= '<h4>Content Type</h4>';
        $s .= '<p>application/x-www-form-urlencoded</p>';
-       $s .= '<h4>' . t('Body for "new status message"') . '</h4>';
+       $s .= '<h4>' . L10n::t('Body for "new status message"') . '</h4>';
        $s .= '<p><code>' . htmlentities('key=' . $key . '&type=status&msg=<<<{{Message}}>>>&date=<<<{{UpdatedAt}}>>>&url=<<<{{PageUrl}}>>>') . '</code></p>';
-       $s .= '<h4>' . t('Body for "new photo upload"') . '</h4>';
+       $s .= '<h4>' . L10n::t('Body for "new photo upload"') . '</h4>';
        $s .= '<p><code>' . htmlentities('key=' . $key . '&type=photo&link=<<<{{Link}}>>>&image=<<<{{ImageSource}}>>>&msg=<<<{{Caption}}>>>&date=<<<{{CreatedAt}}>>>&url=<<<{{PageUrl}}>>>') . '</code></p>';
-       $s .= '<h4>' . t('Body for "new link post"') . '</h4>';
+       $s .= '<h4>' . L10n::t('Body for "new link post"') . '</h4>';
        $s .= '<p><code>' . htmlentities('key=' . $key . '&type=link&link=<<<{{Link}}>>>&title=<<<{{Title}}>>>&msg=<<<{{Message}}>>>&description=<<<{{Description}}>>>&date=<<<{{CreatedAt}}>>>&url=<<<{{PageUrl}}>>>') . '</code></p>';
        $s .= '</div><div class="clear"></div>';
 
        $s .= '<div id="ifttt-rekey-wrapper">';
-       $s .= '<label id="ifttt-rekey-label" for="ifttt-checkbox">' . t('Generate new key') . '</label>';
+       $s .= '<label id="ifttt-rekey-label" for="ifttt-checkbox">' . L10n::t('Generate new key') . '</label>';
        $s .= '<input id="ifttt-checkbox" type="checkbox" name="ifttt-rekey" value="1" />';
        $s .= '</div><div class="clear"></div>';
 
-       $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="ifttt-submit" class="settings-submit" value="' . t('Save Settings') . '" /></div>';
+       $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="ifttt-submit" class="settings-submit" value="' . L10n::t('Save Settings') . '" /></div>';
        $s .= '</div>';
 }
 
 function ifttt_settings_post()
 {
-       if (x($_POST, 'ifttt-submit') && isset($_POST['ifttt-rekey'])) {
+       if (!empty($_POST['ifttt-submit']) && isset($_POST['ifttt-rekey'])) {
                PConfig::delete(local_user(), 'ifttt', 'key');
        }
 }
@@ -97,18 +100,18 @@ function ifttt_post(App $a)
 
        $nickname = $a->argv[1];
 
-       $user = dba::selectFirst('user', ['uid'], ['nickname' => $nickname]);
-       if (!DBM::is_result($user)) {
-               logger('User ' . $nickname . ' not found.', LOGGER_DEBUG);
+       $user = DBA::selectFirst('user', ['uid'], ['nickname' => $nickname]);
+       if (!DBA::isResult($user)) {
+               Logger::log('User ' . $nickname . ' not found.', Logger::DEBUG);
                return;
        }
 
        $uid = $user['uid'];
 
-       logger('Received a post for user ' . $uid . ' from ifttt ' . print_r($_REQUEST, true), LOGGER_DEBUG);
+       Logger::log('Received a post for user ' . $uid . ' from ifttt ' . print_r($_REQUEST, true), Logger::DEBUG);
 
        if (!isset($_REQUEST['key'])) {
-               logger('No key found.');
+               Logger::log('No key found.');
                return;
        }
 
@@ -116,18 +119,18 @@ function ifttt_post(App $a)
 
        // Check the key
        if ($key != PConfig::get($uid, 'ifttt', 'key')) {
-               logger('Invalid key for user ' . $uid, LOGGER_DEBUG);
+               Logger::log('Invalid key for user ' . $uid, Logger::DEBUG);
                return;
        }
 
-       $item = array();
+       $item = [];
 
        if (isset($_REQUEST['type'])) {
                $item['type'] = $_REQUEST['type'];
        }
 
-       if (!in_array($item['type'], array('status', 'link', 'photo'))) {
-               logger('Unknown item type ' . $item['type'], LOGGER_DEBUG);
+       if (!in_array($item['type'], ['status', 'link', 'photo'])) {
+               Logger::log('Unknown item type ' . $item['type'], Logger::DEBUG);
                return;
        }
 
@@ -162,13 +165,12 @@ function ifttt_post(App $a)
 
 function ifttt_message($uid, $item)
 {
-       $a = get_app();
+       $a = \get_app();
 
        $_SESSION['authenticated'] = true;
        $_SESSION['uid'] = $uid;
 
        unset($_REQUEST);
-       $_REQUEST['type'] = 'wall';
        $_REQUEST['api_source'] = true;
        $_REQUEST['profile_uid'] = $uid;
        $_REQUEST['source'] = 'IFTTT';
@@ -177,10 +179,10 @@ function ifttt_message($uid, $item)
        //$_REQUEST['date'] = $item['date'];
        //$_REQUEST['uri'] = $item['url'];
 
-       if (strstr($item['url'], 'facebook.com')) {
-               $hash = hash('ripemd128', item['url']);
-               $_REQUEST['extid'] = NETWORK_FACEBOOK;
-               $_REQUEST['message_id'] = item_new_uri($a->get_hostname(), $uid, NETWORK_FACEBOOK . ':' . $hash);
+       if (!empty($item['url']) && strstr($item['url'], 'facebook.com')) {
+               $hash = hash('ripemd128', $item['url']);
+               $_REQUEST['extid'] = Protocol::FACEBOOK;
+               $_REQUEST['message_id'] = Item::newURI($uid, Protocol::FACEBOOK . ':' . $hash);
        }
 
        if ($item['type'] == 'link') {