2015-10-11 15:07:40 -04:00
< ? php
2017-11-25 21:30:56 -05:00
2015-10-11 15:07:40 -04: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-25 21:30:56 -05:00
use Friendica\App ;
2020-07-14 10:17:17 -04:00
use Friendica\Content\PageInfo ;
2018-12-26 02:28:16 -05:00
use Friendica\Core\Hook ;
2018-10-29 19:40:18 -04:00
use Friendica\Core\Logger ;
2018-08-11 16:40:48 -04:00
use Friendica\Core\Protocol ;
2021-11-21 17:10:13 -05:00
use Friendica\Core\Renderer ;
2018-07-20 08:20:48 -04:00
use Friendica\Database\DBA ;
2019-12-15 19:05:14 -05:00
use Friendica\DI ;
2018-06-16 02:46:25 -04:00
use Friendica\Model\Item ;
2020-12-13 08:07:12 -05:00
use Friendica\Model\Post ;
2018-11-08 11:45:19 -05:00
use Friendica\Util\Strings ;
2017-11-06 18:55:24 -05:00
2017-11-25 21:30:56 -05:00
function ifttt_install ()
{
2018-12-26 02:28:16 -05: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 15:07:40 -04:00
}
2017-11-25 21:30:56 -05:00
function ifttt_module ()
{
2015-10-11 15:07:40 -04:00
}
2017-11-25 21:30:56 -05:00
function ifttt_content ()
{
2015-10-11 15:07:40 -04:00
2017-11-25 21:30:56 -05:00
}
2021-11-21 17:10:13 -05:00
function ifttt_settings ( App $a , array & $data )
2017-11-25 21:30:56 -05:00
{
if ( ! local_user ()) {
return ;
}
2015-10-11 15:07:40 -04:00
2020-01-18 10:50:56 -05:00
$key = DI :: pConfig () -> get ( local_user (), 'ifttt' , 'key' );
2015-10-11 15:07:40 -04:00
if ( ! $key ) {
2018-11-08 11:45:19 -05:00
$key = Strings :: getRandomHex ( 20 );
2020-01-18 10:54:49 -05:00
DI :: pConfig () -> set ( local_user (), 'ifttt' , 'key' , $key );
2015-10-11 15:07:40 -04:00
}
2021-11-21 17:10:13 -05:00
$t = Renderer :: getMarkupTemplate ( 'connector_settings.tpl' , 'addon/ifttt/' );
$html = Renderer :: replaceMacros ( $t , [
'$l10n' => [
'intro' => DI :: 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:' ),
'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' ),
];
2015-10-11 15:07:40 -04:00
}
2017-11-25 21:30:56 -05:00
function ifttt_settings_post ()
{
2021-11-21 17:10:13 -05:00
if ( ! empty ( $_POST [ 'ifttt-submit' ])) {
2020-01-18 10:56:45 -05:00
DI :: pConfig () -> delete ( local_user (), 'ifttt' , 'key' );
2017-11-25 21:30:56 -05:00
}
2015-10-11 15:07:40 -04:00
}
2017-11-25 21:30:56 -05:00
function ifttt_post ( App $a )
{
2021-07-25 08:29:43 -04:00
if ( DI :: args () -> getArgc () != 2 ) {
2015-10-11 15:07:40 -04:00
return ;
2017-11-25 21:30:56 -05:00
}
2015-10-11 15:07:40 -04:00
2021-07-25 08:29:43 -04:00
$nickname = DI :: args () -> getArgv ()[ 1 ];
2015-10-11 15:07:40 -04:00
2018-07-20 08:20:48 -04:00
$user = DBA :: selectFirst ( 'user' , [ 'uid' ], [ 'nickname' => $nickname ]);
2018-07-21 08:46:13 -04:00
if ( ! DBA :: isResult ( $user )) {
2021-10-21 02:04:27 -04:00
Logger :: info ( 'User ' . $nickname . ' not found.' );
2015-10-11 15:07:40 -04:00
return ;
}
2017-11-25 21:30:56 -05:00
$uid = $user [ 'uid' ];
2015-10-11 15:07:40 -04:00
2021-10-21 02:04:27 -04:00
Logger :: info ( 'Received a post for user ' . $uid . ' from ifttt ' . print_r ( $_REQUEST , true ));
2015-10-11 15:07:40 -04:00
2017-11-25 21:30:56 -05:00
if ( ! isset ( $_REQUEST [ 'key' ])) {
2021-10-21 02:04:27 -04:00
Logger :: notice ( 'No key found.' );
2015-10-11 15:07:40 -04:00
return ;
}
2017-11-25 21:30:56 -05:00
$key = $_REQUEST [ 'key' ];
2015-10-11 15:07:40 -04:00
// Check the key
2020-01-18 10:50:56 -05:00
if ( $key != DI :: pConfig () -> get ( $uid , 'ifttt' , 'key' )) {
2021-10-21 02:04:27 -04:00
Logger :: info ( 'Invalid key for user ' . $uid );
2015-10-11 15:07:40 -04:00
return ;
}
2018-01-15 08:15:33 -05:00
$item = [];
2015-10-11 15:07:40 -04:00
2017-11-25 21:30:56 -05:00
if ( isset ( $_REQUEST [ 'type' ])) {
$item [ 'type' ] = $_REQUEST [ 'type' ];
}
2015-10-11 15:07:40 -04:00
2018-01-15 08:15:33 -05:00
if ( ! in_array ( $item [ 'type' ], [ 'status' , 'link' , 'photo' ])) {
2021-10-21 02:04:27 -04:00
Logger :: info ( 'Unknown item type ' . $item [ 'type' ]);
2015-10-11 15:07:40 -04:00
return ;
}
2017-11-25 21:30:56 -05: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 15:07:40 -04:00
ifttt_message ( $uid , $item );
}
2017-11-25 21:30:56 -05:00
function ifttt_message ( $uid , $item )
{
2020-01-04 17:42:00 -05:00
$a = DI :: app ();
2015-10-11 15:07:40 -04:00
2020-12-13 08:07:12 -05:00
$post = [];
$post [ 'uid' ] = $uid ;
$post [ 'app' ] = 'IFTTT' ;
$post [ 'title' ] = '' ;
$post [ 'body' ] = $item [ 'msg' ];
//$post['date'] = $item['date'];
//$post['uri'] = $item['url'];
2017-11-25 21:30:56 -05:00
if ( $item [ 'type' ] == 'link' ) {
2020-12-13 08:07:12 -05:00
$link = $item [ 'link' ];
2020-07-14 10:17:17 -04:00
$data = PageInfo :: queryUrl ( $item [ 'link' ]);
2015-10-11 15:07:40 -04:00
2017-11-25 21:30:56 -05:00
if ( isset ( $item [ 'title' ]) && ( trim ( $item [ 'title' ]) != '' )) {
$data [ 'title' ] = $item [ 'title' ];
}
2015-10-11 15:07:40 -04:00
2017-11-25 21:30:56 -05:00
if ( isset ( $item [ 'description' ]) && ( trim ( $item [ 'description' ]) != '' )) {
$data [ 'text' ] = $item [ 'description' ];
}
2015-10-11 15:07:40 -04:00
2020-12-13 08:07:12 -05:00
$post [ 'body' ] .= " \n " . PageInfo :: getFooterFromData ( $data );
2017-11-25 21:30:56 -05:00
} elseif (( $item [ 'type' ] == 'photo' ) && ( $item [ 'image' ] != '' )) {
2020-12-13 08:07:12 -05:00
$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' ]);
2017-11-25 21:30:56 -05:00
}
2015-10-11 15:07:40 -04:00
2021-08-06 14:52:44 -04:00
Post\Delayed :: add ( $link , $post , PRIORITY_MEDIUM , Post\Delayed :: UNPREPARED );
2015-10-11 15:07:40 -04:00
}