X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=ifttt%2Fifttt.php;h=5962e2ae94b28ee71a6b3a785f1e76acbe9651e9;hb=14124f49e7f2057146212a124563a7ee8f0c20dc;hp=5021a63ed60cdd5482d7600e0505a27113bc2254;hpb=203672cf27fc45e264dd0d6dd507ffc4da837e4d;p=friendica-addons.git diff --git a/ifttt/ifttt.php b/ifttt/ifttt.php index 5021a63e..5962e2ae 100644 --- a/ifttt/ifttt.php +++ b/ifttt/ifttt.php @@ -6,15 +6,16 @@ * Version: 0.1 * Author: Michael Vogel */ -require_once 'mod/item.php'; use Friendica\App; use Friendica\Content\PageInfo; use Friendica\Core\Hook; use Friendica\Core\Logger; use Friendica\Core\Protocol; +use Friendica\Core\Renderer; use Friendica\Database\DBA; use Friendica\DI; use Friendica\Model\Item; +use Friendica\Model\Post; use Friendica\Util\Strings; function ifttt_install() @@ -33,79 +34,71 @@ function ifttt_content() } -function ifttt_settings(App $a, &$s) +function ifttt_settings(App $a, array &$data) { if (!local_user()) { return; } $key = DI::pConfig()->get(local_user(), 'ifttt', 'key'); - if (!$key) { $key = Strings::getRandomHex(20); DI::pConfig()->set(local_user(), 'ifttt', 'key', $key); } - $s .= ''; - $s .= '

' . DI::l10n()->t('IFTTT Mirror') . '

'; - $s .= '
'; - $s .= ''; + $t = Renderer::getMarkupTemplate('connector_settings.tpl', 'addon/ifttt/'); + $html = Renderer::replaceMacros($t, [ + '$l10n' => [ + 'intro' => DI::l10n()->t('Create an account at IFTTT. Create three Facebook recipes that are connected with Maker (In the form "if Facebook then Maker") with the following parameters:'), + 'url' => DI::l10n()->t('URL'), + 'method' => DI::l10n()->t('Method'), + 'content_type' => DI::l10n()->t('Content Type'), + 'new_status_message_body' => DI::l10n()->t('Body for "new status message"'), + 'new_photo_upload_body' => DI::l10n()->t('Body for "new photo upload"'), + 'new_link_post_body' => DI::l10n()->t('Body for "new link post"'), + ], + '$url' => DI::baseUrl()->get() . '/ifttt/' . $a->getLoggedInUserNickname(), + '$new_status_message_body' => 'key=' . $key . '&type=status&msg=<<<{{Message}}>>>&date=<<<{{UpdatedAt}}>>>&url=<<<{{PageUrl}}>>>', + '$new_photo_upload_body' => 'key=' . $key . '&type=photo&link=<<<{{Link}}>>>&image=<<<{{ImageSource}}>>>&msg=<<<{{Caption}}>>>&date=<<<{{CreatedAt}}>>>&url=<<<{{PageUrl}}>>>', + '$new_link_post_body' => 'key=' . $key . '&type=link&link=<<<{{Link}}>>>&title=<<<{{Title}}>>>&msg=<<<{{Message}}>>>&description=<<<{{Description}}>>>&date=<<<{{CreatedAt}}>>>&url=<<<{{PageUrl}}>>>', + ]); + + $data = [ + 'connector' => 'ifttt', + 'title' => DI::l10n()->t('IFTTT Mirror'), + 'image' => 'addon/ifttt/ifttt.png', + 'html' => $html, + 'submit' => DI::l10n()->t('Generate new key'), + ]; } function ifttt_settings_post() { - if (!empty($_POST['ifttt-submit']) && isset($_POST['ifttt-rekey'])) { + if (!empty($_POST['ifttt-submit'])) { DI::pConfig()->delete(local_user(), 'ifttt', 'key'); } } function ifttt_post(App $a) { - if ($a->argc != 2) { + if (DI::args()->getArgc() != 2) { return; } - $nickname = $a->argv[1]; + $nickname = DI::args()->getArgv()[1]; $user = DBA::selectFirst('user', ['uid'], ['nickname' => $nickname]); if (!DBA::isResult($user)) { - Logger::log('User ' . $nickname . ' not found.', Logger::DEBUG); + Logger::info('User ' . $nickname . ' not found.'); return; } $uid = $user['uid']; - Logger::log('Received a post for user ' . $uid . ' from ifttt ' . print_r($_REQUEST, true), Logger::DEBUG); + Logger::info('Received a post for user ' . $uid . ' from ifttt ' . print_r($_REQUEST, true)); if (!isset($_REQUEST['key'])) { - Logger::log('No key found.'); + Logger::notice('No key found.'); return; } @@ -113,7 +106,7 @@ function ifttt_post(App $a) // Check the key if ($key != DI::pConfig()->get($uid, 'ifttt', 'key')) { - Logger::log('Invalid key for user ' . $uid, Logger::DEBUG); + Logger::info('Invalid key for user ' . $uid); return; } @@ -124,7 +117,7 @@ function ifttt_post(App $a) } if (!in_array($item['type'], ['status', 'link', 'photo'])) { - Logger::log('Unknown item type ' . $item['type'], Logger::DEBUG); + Logger::info('Unknown item type ' . $item['type']); return; } @@ -161,25 +154,16 @@ function ifttt_message($uid, $item) { $a = DI::app(); - $_SESSION['authenticated'] = true; - $_SESSION['uid'] = $uid; - - unset($_REQUEST); - $_REQUEST['api_source'] = true; - $_REQUEST['profile_uid'] = $uid; - $_REQUEST['source'] = 'IFTTT'; - $_REQUEST['title'] = ''; - $_REQUEST['body'] = $item['msg']; - //$_REQUEST['date'] = $item['date']; - //$_REQUEST['uri'] = $item['url']; - - 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); - } + $post = []; + $post['uid'] = $uid; + $post['app'] = 'IFTTT'; + $post['title'] = ''; + $post['body'] = $item['msg']; + //$post['date'] = $item['date']; + //$post['uri'] = $item['url']; if ($item['type'] == 'link') { + $link = $item['link']; $data = PageInfo::queryUrl($item['link']); if (isset($item['title']) && (trim($item['title']) != '')) { @@ -190,10 +174,15 @@ function ifttt_message($uid, $item) $data['text'] = $item['description']; } - $_REQUEST['body'] .= "\n" . PageInfo::getFooterFromData($data); + $post['body'] .= "\n" . PageInfo::getFooterFromData($data); } elseif (($item['type'] == 'photo') && ($item['image'] != '')) { - $_REQUEST['body'] .= "\n\n[img]" . $item['image'] . "[/img]\n"; + $link = $item['image']; + $post['body'] .= "\n\n[img]" . $item['image'] . "[/img]\n"; + } elseif (!empty($item['url'])) { + $link = $item['url']; + } else { + $link = hash('ripemd128', $item['msg']); } - item_post($a); + Post\Delayed::add($link, $post, PRIORITY_MEDIUM, Post\Delayed::UNPREPARED); }