From: Michael Vogel Date: Sun, 11 Oct 2015 19:07:40 +0000 (+0200) Subject: IFTTT: New plugin X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=b8a720775332c0a80a909bb11d6a7f5ee234e9a2;p=friendica-addons.git IFTTT: New plugin --- diff --git a/ifttt/ifttt.php b/ifttt/ifttt.php new file mode 100644 index 00000000..3203fec4 --- /dev/null +++ b/ifttt/ifttt.php @@ -0,0 +1,177 @@ + + */ + +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'); +} + +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_content(&$a) { +} + +function ifttt_settings(&$a,&$s) { + + if(! local_user()) + return; + + $key = get_pconfig(local_user(),'ifttt','key'); + + if (!$key) { + $key = substr(random_string(),0,20); + set_pconfig(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_post(&$a) { + if ($a->argc != 2) + return; + + $user = $a->argv[1]; + + $r = q("SELECT `uid` FROM `user` WHERE `nickname` = '%s' LIMIT 1", dbesc($user)); + if (!$r) { + logger("User ".$user." not found.", LOGGER_DEBUG); + return; + } + + $uid = $r[0]["uid"]; + + logger("Received a post for user ".$uid." from ifttt ".print_r($_REQUEST, true), LOGGER_DEBUG); + + if (!isset($_REQUEST["key"])) { + logger("No key found."); + return; + } + + $key = $_REQUEST["key"]; + + // Check the key + if ($key != get_pconfig($uid,'ifttt','key')) { + logger("Invalid key for user ".$uid, LOGGER_DEBUG); + return; + } + + $item = array(); + + 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); + 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) == "<<<") AND (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")) + $_REQUEST["extid"] = NETWORK_FACEBOOK; + + if ($item["type"] == "link") { + $data = query_page_info($item["link"]); + + if (isset($item["title"]) AND (trim($item["title"]) != "")) + $data["title"] = $item["title"]; + + if (isset($item["description"]) AND (trim($item["description"]) != "")) + $data["text"] = $item["description"]; + + $_REQUEST["body"] .= add_page_info_data($data); + } elseif (($item["type"] == "photo") AND ($item["image"] != "")) + $_REQUEST["body"] .= "\n\n[img]".$item["image"]."[/img]\n"; + + //print_r($_REQUEST); + item_post($a); +} +?> diff --git a/ifttt/ifttt.png b/ifttt/ifttt.png new file mode 100644 index 00000000..364edb08 Binary files /dev/null and b/ifttt/ifttt.png differ