2014-10-03 10:34:50 +00:00
< ? php /** @file */
// import page design element
function impel_init ( & $a ) {
$ret = array ( 'success' => false );
2015-01-29 04:56:04 +00:00
if ( ! local_channel ())
2014-10-03 10:34:50 +00:00
json_return_and_die ( $ret );
2014-10-07 00:47:17 +00:00
logger ( 'impel: ' . print_r ( $_REQUEST , true ), LOGGER_DATA );
2014-10-03 10:34:50 +00:00
$elm = $_REQUEST [ 'element' ];
2014-10-07 00:47:17 +00:00
$x = base64url_decode ( $elm );
2014-10-03 10:34:50 +00:00
if ( ! $x )
json_return_and_die ( $ret );
$j = json_decode ( $x , true );
if ( ! $j )
json_return_and_die ( $ret );
2014-10-07 00:47:17 +00:00
$channel = $a -> get_channel ();
2014-10-03 10:34:50 +00:00
$arr = array ();
switch ( $j [ 'type' ]) {
case 'webpage' :
2015-01-29 22:51:41 +00:00
$arr [ 'item_type' ] = ITEM_TYPE_WEBPAGE ;
2014-10-03 23:51:22 +00:00
$namespace = 'WEBPAGE' ;
2014-10-07 00:47:17 +00:00
$installed_type = t ( 'webpage' );
2014-10-03 10:34:50 +00:00
break ;
case 'block' :
2015-01-29 22:51:41 +00:00
$arr [ 'item_type' ] = ITEM_TYPE_BLOCK ;
2014-10-03 23:51:22 +00:00
$namespace = 'BUILDBLOCK' ;
2014-10-07 00:47:17 +00:00
$installed_type = t ( 'block' );
2014-10-03 10:34:50 +00:00
break ;
case 'layout' :
2015-01-29 22:51:41 +00:00
$arr [ 'item_type' ] = ITEM_TYPE_PDL ;
2014-10-03 23:51:22 +00:00
$namespace = 'PDL' ;
2014-10-07 00:47:17 +00:00
$installed_type = t ( 'layout' );
2014-10-03 10:34:50 +00:00
break ;
default :
logger ( 'mod_impel: unrecognised element type' . print_r ( $j , true ));
break ;
}
2015-01-29 04:56:04 +00:00
$arr [ 'uid' ] = local_channel ();
2014-10-03 10:34:50 +00:00
$arr [ 'aid' ] = $channel [ 'channel_account_id' ];
$arr [ 'title' ] = $j [ 'title' ];
$arr [ 'body' ] = $j [ 'body' ];
$arr [ 'term' ] = $j [ 'term' ];
2014-10-07 00:47:17 +00:00
$arr [ 'created' ] = datetime_convert ( 'UTC' , 'UTC' , $j [ 'created' ]);
$arr [ 'edited' ] = datetime_convert ( 'UTC' , 'UTC' , $j [ 'edited' ]);
2014-10-03 10:34:50 +00:00
$arr [ 'owner_xchan' ] = get_observer_hash ();
2014-10-07 00:47:17 +00:00
$arr [ 'author_xchan' ] = (( $j [ 'author_xchan' ]) ? $j [ 'author_xchan' ] : get_observer_hash ());
2014-10-03 10:34:50 +00:00
$arr [ 'mimetype' ] = (( $j [ 'mimetype' ]) ? $j [ 'mimetype' ] : 'text/bbcode' );
2014-10-03 23:51:22 +00:00
if ( ! $j [ 'mid' ])
$j [ 'mid' ] = item_message_id ();
$arr [ 'mid' ] = $arr [ 'parent_mid' ] = $j [ 'mid' ];
if ( $j [ 'pagetitle' ]) {
require_once ( 'library/urlify/URLify.php' );
$pagetitle = strtolower ( URLify :: transliterate ( $j [ 'pagetitle' ]));
}
2014-10-03 10:34:50 +00:00
// Verify ability to use html or php!!!
$execflag = false ;
if ( $arr [ 'mimetype' ] === 'application/x-php' ) {
2015-01-14 02:30:30 +00:00
$z = q ( " select account_id, account_roles, channel_pageflags from account left join channel on channel_account_id = account_id where channel_id = %d limit 1 " ,
2015-01-29 04:56:04 +00:00
intval ( local_channel ())
2014-10-03 10:34:50 +00:00
);
2015-01-14 02:30:30 +00:00
if ( $z && (( $z [ 0 ][ 'account_roles' ] & ACCOUNT_ROLE_ALLOWCODE ) || ( $z [ 0 ][ 'channel_pageflags' ] & PAGE_ALLOWCODE ))) {
2014-10-03 10:34:50 +00:00
$execflag = true ;
}
}
2014-10-03 23:51:22 +00:00
$remote_id = 0 ;
2014-10-07 00:47:17 +00:00
$z = q ( " select * from item_id where sid = '%s' and service = '%s' and uid = %d limit 1 " ,
2014-10-03 23:51:22 +00:00
dbesc ( $pagetitle ),
dbesc ( $namespace ),
2015-01-29 04:56:04 +00:00
intval ( local_channel ())
2014-10-03 23:51:22 +00:00
);
2014-10-07 00:47:17 +00:00
$i = q ( " select id from item where mid = '%s' and uid = %d limit 1 " ,
2014-10-03 23:51:22 +00:00
dbesc ( $arr [ 'mid' ]),
2015-01-29 04:56:04 +00:00
intval ( local_channel ())
2014-10-03 23:51:22 +00:00
);
if ( $z && $i ) {
$remote_id = $z [ 0 ][ 'id' ];
$arr [ 'id' ] = $i [ 0 ][ 'id' ];
2014-10-07 00:47:17 +00:00
// don't update if it has the same timestamp as the original
if ( $arr [ 'edited' ] > $i [ 0 ][ 'edited' ])
$x = item_store_update ( $arr , $execflag );
2014-10-03 23:51:22 +00:00
}
else {
$x = item_store ( $arr , $execflag );
}
if ( $x [ 'success' ])
$item_id = $x [ 'item_id' ];
update_remote_id ( $channel , $item_id , $arr [ 'item_restrict' ], $pagetitle , $namespace , $remote_id , $arr [ 'mid' ]);
2014-10-03 10:34:50 +00:00
$ret [ 'success' ] = true ;
2014-10-07 00:47:17 +00:00
info ( sprintf ( t ( '%s element installed' ), $installed_type ));
2014-10-03 10:34:50 +00:00
json_return_and_die ( true );
}