2015-10-11 19:07:40 +00:00
< ? php
2017-11-26 02:30:56 +00:00
2015-10-11 19:07:40 +00:00
/**
* Name : IFTTT Receiver
* Description : Receives a post from https :// ifttt . com / and distributes it .
* Version : 0.1
* Author : Michael Vogel < https :// pirati . ca / profile / heluecht >
*/
2017-11-26 02:30:56 +00:00
require_once 'mod/item.php' ;
require_once 'include/items.php' ;
require_once 'include/text.php' ;
2015-10-11 19:07:40 +00:00
2017-11-26 02:30:56 +00:00
use Friendica\App ;
2018-12-26 07:28:16 +00:00
use Friendica\Core\Hook ;
2018-01-22 19:03:11 +00:00
use Friendica\Core\L10n ;
2018-10-29 23:40:18 +00:00
use Friendica\Core\Logger ;
2017-11-06 23:55:24 +00:00
use Friendica\Core\PConfig ;
2018-08-11 20:40:48 +00:00
use Friendica\Core\Protocol ;
2018-07-20 12:20:48 +00:00
use Friendica\Database\DBA ;
2018-06-16 06:46:25 +00:00
use Friendica\Model\Item ;
2018-11-08 16:45:19 +00:00
use Friendica\Util\Strings ;
2017-11-06 23:55:24 +00:00
2017-11-26 02:30:56 +00:00
function ifttt_install ()
{
2018-12-26 07:28:16 +00:00
Hook :: register ( 'connector_settings' , 'addon/ifttt/ifttt.php' , 'ifttt_settings' );
Hook :: register ( 'connector_settings_post' , 'addon/ifttt/ifttt.php' , 'ifttt_settings_post' );
2015-10-11 19:07:40 +00:00
}
2017-11-26 02:30:56 +00:00
function ifttt_uninstall ()
{
2018-12-26 07:28:16 +00:00
Hook :: unregister ( 'connector_settings' , 'addon/ifttt/ifttt.php' , 'ifttt_settings' );
Hook :: unregister ( 'connector_settings_post' , 'addon/ifttt/ifttt.php' , 'ifttt_settings_post' );
2015-10-11 19:07:40 +00:00
}
2017-11-26 02:30:56 +00:00
function ifttt_module ()
{
2015-10-11 19:07:40 +00:00
}
2017-11-26 02:30:56 +00:00
function ifttt_content ()
{
2015-10-11 19:07:40 +00:00
2017-11-26 02:30:56 +00:00
}
function ifttt_settings ( App $a , & $s )
{
if ( ! local_user ()) {
return ;
}
2015-10-11 19:07:40 +00:00
2017-11-26 02:30:56 +00:00
$key = PConfig :: get ( local_user (), 'ifttt' , 'key' );
2015-10-11 19:07:40 +00:00
if ( ! $key ) {
2018-11-08 16:45:19 +00:00
$key = Strings :: getRandomHex ( 20 );
2017-11-26 02:30:56 +00:00
PConfig :: set ( local_user (), 'ifttt' , 'key' , $key );
2015-10-11 19:07:40 +00:00
}
$s .= '<span id="settings_ifttt_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_ifttt_expanded\'); openClose(\'settings_ifttt_inflated\');">' ;
2018-01-22 19:03:11 +00:00
$s .= '<img class="connector" src="addon/ifttt/ifttt.png" /><h3 class="connector">' . L10n :: t ( 'IFTTT Mirror' ) . '</h3>' ;
2017-11-26 02:30:56 +00:00
$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\');">' ;
2018-01-22 19:03:11 +00:00
$s .= '<img class="connector" src="addon/ifttt/ifttt.png" /><h3 class="connector">' . L10n :: t ( 'IFTTT Mirror' ) . '</h3>' ;
2017-11-26 02:30:56 +00:00
$s .= '</span>' ;
$s .= '<div id="ifttt-configuration-wrapper">' ;
2018-01-22 19:03:11 +00:00
$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>' ;
2015-10-11 19:07:40 +00:00
$s .= '<h4>URL</h4>' ;
2018-10-09 18:13:22 +00:00
$s .= '<p>' . $a -> getBaseURL () . '/ifttt/' . $a -> user [ 'nickname' ] . '</p>' ;
2015-10-11 19:07:40 +00:00
$s .= '<h4>Method</h4>' ;
$s .= '<p>POST</p>' ;
$s .= '<h4>Content Type</h4>' ;
$s .= '<p>application/x-www-form-urlencoded</p>' ;
2018-01-22 19:03:11 +00:00
$s .= '<h4>' . L10n :: t ( 'Body for "new status message"' ) . '</h4>' ;
2017-11-26 02:30:56 +00:00
$s .= '<p><code>' . htmlentities ( 'key=' . $key . '&type=status&msg=<<<{{Message}}>>>&date=<<<{{UpdatedAt}}>>>&url=<<<{{PageUrl}}>>>' ) . '</code></p>' ;
2018-01-22 19:03:11 +00:00
$s .= '<h4>' . L10n :: t ( 'Body for "new photo upload"' ) . '</h4>' ;
2017-11-26 02:30:56 +00:00
$s .= '<p><code>' . htmlentities ( 'key=' . $key . '&type=photo&link=<<<{{Link}}>>>&image=<<<{{ImageSource}}>>>&msg=<<<{{Caption}}>>>&date=<<<{{CreatedAt}}>>>&url=<<<{{PageUrl}}>>>' ) . '</code></p>' ;
2018-01-22 19:03:11 +00:00
$s .= '<h4>' . L10n :: t ( 'Body for "new link post"' ) . '</h4>' ;
2017-11-26 02:30:56 +00:00
$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">' ;
2018-01-22 19:03:11 +00:00
$s .= '<label id="ifttt-rekey-label" for="ifttt-checkbox">' . L10n :: t ( 'Generate new key' ) . '</label>' ;
2017-11-26 02:30:56 +00:00
$s .= '<input id="ifttt-checkbox" type="checkbox" name="ifttt-rekey" value="1" />' ;
$s .= '</div><div class="clear"></div>' ;
2015-10-11 19:07:40 +00:00
2018-01-22 19:03:11 +00:00
$s .= '<div class="settings-submit-wrapper" ><input type="submit" name="ifttt-submit" class="settings-submit" value="' . L10n :: t ( 'Save Settings' ) . '" /></div>' ;
2017-11-26 02:30:56 +00:00
$s .= '</div>' ;
2015-10-11 19:07:40 +00:00
}
2017-11-26 02:30:56 +00:00
function ifttt_settings_post ()
{
2018-11-30 14:11:56 +00:00
if ( ! empty ( $_POST [ 'ifttt-submit' ]) && isset ( $_POST [ 'ifttt-rekey' ])) {
2017-11-26 02:30:56 +00:00
PConfig :: delete ( local_user (), 'ifttt' , 'key' );
}
2015-10-11 19:07:40 +00:00
}
2017-11-26 02:30:56 +00:00
function ifttt_post ( App $a )
{
if ( $a -> argc != 2 ) {
2015-10-11 19:07:40 +00:00
return ;
2017-11-26 02:30:56 +00:00
}
2015-10-11 19:07:40 +00:00
2017-11-26 02:30:56 +00:00
$nickname = $a -> argv [ 1 ];
2015-10-11 19:07:40 +00:00
2018-07-20 12:20:48 +00:00
$user = DBA :: selectFirst ( 'user' , [ 'uid' ], [ 'nickname' => $nickname ]);
2018-07-21 12:46:13 +00:00
if ( ! DBA :: isResult ( $user )) {
2018-10-30 13:48:09 +00:00
Logger :: log ( 'User ' . $nickname . ' not found.' , Logger :: DEBUG );
2015-10-11 19:07:40 +00:00
return ;
}
2017-11-26 02:30:56 +00:00
$uid = $user [ 'uid' ];
2015-10-11 19:07:40 +00:00
2018-10-30 13:48:09 +00:00
Logger :: log ( 'Received a post for user ' . $uid . ' from ifttt ' . print_r ( $_REQUEST , true ), Logger :: DEBUG );
2015-10-11 19:07:40 +00:00
2017-11-26 02:30:56 +00:00
if ( ! isset ( $_REQUEST [ 'key' ])) {
2018-10-29 23:40:18 +00:00
Logger :: log ( 'No key found.' );
2015-10-11 19:07:40 +00:00
return ;
}
2017-11-26 02:30:56 +00:00
$key = $_REQUEST [ 'key' ];
2015-10-11 19:07:40 +00:00
// Check the key
2017-11-26 02:30:56 +00:00
if ( $key != PConfig :: get ( $uid , 'ifttt' , 'key' )) {
2018-10-30 13:48:09 +00:00
Logger :: log ( 'Invalid key for user ' . $uid , Logger :: DEBUG );
2015-10-11 19:07:40 +00:00
return ;
}
2018-01-15 13:15:33 +00:00
$item = [];
2015-10-11 19:07:40 +00:00
2017-11-26 02:30:56 +00:00
if ( isset ( $_REQUEST [ 'type' ])) {
$item [ 'type' ] = $_REQUEST [ 'type' ];
}
2015-10-11 19:07:40 +00:00
2018-01-15 13:15:33 +00:00
if ( ! in_array ( $item [ 'type' ], [ 'status' , 'link' , 'photo' ])) {
2018-10-30 13:48:09 +00:00
Logger :: log ( 'Unknown item type ' . $item [ 'type' ], Logger :: DEBUG );
2015-10-11 19:07:40 +00:00
return ;
}
2017-11-26 02:30:56 +00:00
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 );
}
2015-10-11 19:07:40 +00:00
ifttt_message ( $uid , $item );
}
2017-11-26 02:30:56 +00:00
function ifttt_message ( $uid , $item )
{
2015-10-11 19:07:40 +00:00
$a = get_app ();
2017-11-26 02:30:56 +00:00
$_SESSION [ 'authenticated' ] = true ;
$_SESSION [ 'uid' ] = $uid ;
2015-10-11 19:07:40 +00:00
unset ( $_REQUEST );
2017-11-26 02:30:56 +00:00
$_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'];
2018-08-29 18:04:03 +00:00
if ( ! empty ( $item [ 'url' ]) && strstr ( $item [ 'url' ], 'facebook.com' )) {
2018-08-14 01:53:30 +00:00
$hash = hash ( 'ripemd128' , $item [ 'url' ]);
2018-08-11 20:40:48 +00:00
$_REQUEST [ 'extid' ] = Protocol :: FACEBOOK ;
$_REQUEST [ 'message_id' ] = Item :: newURI ( $uid , Protocol :: FACEBOOK . ':' . $hash );
2016-11-15 06:36:52 +00:00
}
2015-10-11 19:07:40 +00:00
2017-11-26 02:30:56 +00:00
if ( $item [ 'type' ] == 'link' ) {
$data = query_page_info ( $item [ 'link' ]);
2015-10-11 19:07:40 +00:00
2017-11-26 02:30:56 +00:00
if ( isset ( $item [ 'title' ]) && ( trim ( $item [ 'title' ]) != '' )) {
$data [ 'title' ] = $item [ 'title' ];
}
2015-10-11 19:07:40 +00:00
2017-11-26 02:30:56 +00:00
if ( isset ( $item [ 'description' ]) && ( trim ( $item [ 'description' ]) != '' )) {
$data [ 'text' ] = $item [ 'description' ];
}
2015-10-11 19:07:40 +00:00
2017-11-26 02:30:56 +00:00
$_REQUEST [ 'body' ] .= add_page_info_data ( $data );
} elseif (( $item [ 'type' ] == 'photo' ) && ( $item [ 'image' ] != '' )) {
$_REQUEST [ 'body' ] .= " \n \n [img] " . $item [ 'image' ] . " [/img] \n " ;
}
2015-10-11 19:07:40 +00:00
item_post ( $a );
}