X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=ifttt%2Fifttt.php;h=593c0e08640cd798e34710d5fbf986ccf22b50dd;hb=08c64387d702ce52471482c530a0069aa1412476;hp=e5428e82b45b08935cdb0571e86098e018fec26f;hpb=8fa3f2415a3f3906f5fdadd1601ec6830ecce3c9;p=friendica-addons.git diff --git a/ifttt/ifttt.php b/ifttt/ifttt.php index e5428e82..593c0e08 100644 --- a/ifttt/ifttt.php +++ b/ifttt/ifttt.php @@ -1,179 +1,195 @@ */ - -require_once("mod/item.php"); -require_once("include/items.php"); - -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'); +use Friendica\App; +use Friendica\Content\PageInfo; +use Friendica\Core\Hook; +use Friendica\Core\Logger; +use Friendica\Core\Protocol; +use Friendica\Database\DBA; +use Friendica\DI; +use Friendica\Model\Item; +use Friendica\Model\Post; +use Friendica\Util\Strings; + +function ifttt_install() +{ + 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'); -} +function ifttt_module() +{ -function ifttt_module() { } -function ifttt_content(&$a) { -} +function ifttt_content() +{ -function ifttt_settings(&$a,&$s) { +} - if(! local_user()) - return; +function ifttt_settings(App $a, &$s) +{ + if (!local_user()) { + return; + } - $key = get_pconfig(local_user(),'ifttt','key'); + $key = DI::pConfig()->get(local_user(), 'ifttt', 'key'); if (!$key) { - $key = substr(random_string(),0,20); - set_pconfig(local_user(),'ifttt','key', $key); + $key = Strings::getRandomHex(20); + DI::pConfig()->set(local_user(), 'ifttt', 'key', $key); } $s .= ''; - $s .= '

'. t('IFTTT Mirror').'

'; - $s .= '
'; - $s .= ''; } -function ifttt_settings_post(&$a,&$b) { - - if(x($_POST,'ifttt-submit')) - if (isset($_POST['ifttt-rekey'])) - del_pconfig(local_user(), 'ifttt', 'key'); +function ifttt_settings_post() +{ + if (!empty($_POST['ifttt-submit']) && isset($_POST['ifttt-rekey'])) { + DI::pConfig()->delete(local_user(), 'ifttt', 'key'); + } } -function ifttt_post(&$a) { - if ($a->argc != 2) +function ifttt_post(App $a) +{ + if (DI::args()->getArgc() != 2) { return; + } - $user = $a->argv[1]; + $nickname = DI::args()->getArgv()[1]; - $r = q("SELECT `uid` FROM `user` WHERE `nickname` = '%s' LIMIT 1", dbesc($user)); - if (!$r) { - logger("User ".$user." 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 = $r[0]["uid"]; + $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."); + if (!isset($_REQUEST['key'])) { + Logger::log('No key found.'); return; } - $key = $_REQUEST["key"]; + $key = $_REQUEST['key']; // Check the key - if ($key != get_pconfig($uid,'ifttt','key')) { - logger("Invalid key for user ".$uid, LOGGER_DEBUG); + if ($key != DI::pConfig()->get($uid, 'ifttt', 'key')) { + Logger::log('Invalid key for user ' . $uid, Logger::DEBUG); return; } - $item = array(); + $item = []; - if (isset($_REQUEST["type"])) - $item["type"] = $_REQUEST["type"]; + 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; } - if (isset($_REQUEST["link"])) - $item["link"] = trim($_REQUEST["link"]); - if (isset($_REQUEST["image"])) - $item["image"] = trim($_REQUEST["image"]); - if (isset($_REQUEST["title"])) - $item["title"] = trim($_REQUEST["title"]); - if (isset($_REQUEST["msg"])) - $item["msg"] = trim($_REQUEST["msg"]); - if (isset($_REQUEST["description"])) - $item["description"] = trim($_REQUEST["description"]); - if (isset($_REQUEST["date"])) - $item["date"] = date("c", strtotime($date = str_replace(" at ", ", ", $_REQUEST["date"]))); - if (isset($_REQUEST["url"])) - $item["url"] = trim($_REQUEST["url"]); - - if ((substr($item["msg"], 0, 3) == "<<<") && (substr($item["msg"], -3, 3) == ">>>")) - $item["msg"] = substr($item["msg"], 3, -3); + if (isset($_REQUEST['link'])) { + $item['link'] = trim($_REQUEST['link']); + } + if (isset($_REQUEST['image'])) { + $item['image'] = trim($_REQUEST['image']); + } + if (isset($_REQUEST['title'])) { + $item['title'] = trim($_REQUEST['title']); + } + if (isset($_REQUEST['msg'])) { + $item['msg'] = trim($_REQUEST['msg']); + } + if (isset($_REQUEST['description'])) { + $item['description'] = trim($_REQUEST['description']); + } + if (isset($_REQUEST['date'])) { + $item['date'] = date('c', strtotime($date = str_replace(' at ', ', ', $_REQUEST['date']))); + } + if (isset($_REQUEST['url'])) { + $item['url'] = trim($_REQUEST['url']); + } + + if ((substr($item['msg'], 0, 3) == '<<<') && (substr($item['msg'], -3, 3) == '>>>')) { + $item['msg'] = substr($item['msg'], 3, -3); + } ifttt_message($uid, $item); } -function ifttt_message($uid, $item) { - - $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"; - $_REQUEST["title"] = ""; - $_REQUEST["body"] = $item["msg"]; - //$_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); +function ifttt_message($uid, $item) +{ + $a = DI::app(); + + $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']) != '')) { + $data['title'] = $item['title']; + } + + if (isset($item['description']) && (trim($item['description']) != '')) { + $data['text'] = $item['description']; + } + + $post['body'] .= "\n" . PageInfo::getFooterFromData($data); + } elseif (($item['type'] == 'photo') && ($item['image'] != '')) { + $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']); } - if ($item["type"] == "link") { - $data = query_page_info($item["link"]); - - if (isset($item["title"]) && (trim($item["title"]) != "")) - $data["title"] = $item["title"]; - - if (isset($item["description"]) && (trim($item["description"]) != "")) - $data["text"] = $item["description"]; - - $_REQUEST["body"] .= add_page_info_data($data); - } elseif (($item["type"] == "photo") && ($item["image"] != "")) - $_REQUEST["body"] .= "\n\n[img]".$item["image"]."[/img]\n"; - - //print_r($_REQUEST); - item_post($a); + Post\Delayed::add($link, $post, PRIORITY_MEDIUM, true); }