2016-04-19 03:38:38 +00:00
< ? php
namespace Zotlabs\Module ;
2019-04-03 01:53:43 +00:00
use App ;
use Zotlabs\Web\Controller ;
2016-05-22 23:54:30 +00:00
require_once ( 'include/channel.php' );
2016-04-19 03:38:38 +00:00
require_once ( 'include/permissions.php' );
2019-04-03 01:53:43 +00:00
class New_channel extends Controller {
2016-04-19 03:38:38 +00:00
function init () {
2017-10-20 01:48:11 +00:00
2016-04-19 03:38:38 +00:00
$cmd = (( argc () > 1 ) ? argv ( 1 ) : '' );
if ( $cmd === 'autofill.json' ) {
$result = array ( 'error' => false , 'message' => '' );
$n = trim ( $_REQUEST [ 'name' ]);
2018-03-13 03:54:55 +00:00
$x = false ;
if ( get_config ( 'system' , 'unicode_usernames' )) {
$x = punify ( mb_strtolower ( $n ));
}
if (( ! $x ) || strlen ( $x ) > 64 )
$x = strtolower ( \URLify :: transliterate ( $n ));
2016-04-19 03:38:38 +00:00
$test = array ();
// first name
if ( strpos ( $x , ' ' ))
$test [] = legal_webbie ( substr ( $x , 0 , strpos ( $x , ' ' )));
if ( $test [ 0 ]) {
// first name plus first initial of last
$test [] = (( strpos ( $x , ' ' )) ? $test [ 0 ] . legal_webbie ( trim ( substr ( $x , strpos ( $x , ' ' ), 2 ))) : '' );
// first name plus random number
$test [] = $test [ 0 ] . mt_rand ( 1000 , 9999 );
}
// fullname
$test [] = legal_webbie ( $x );
// fullname plus random number
$test [] = legal_webbie ( $x ) . mt_rand ( 1000 , 9999 );
2018-05-22 03:17:17 +00:00
2016-04-19 03:38:38 +00:00
json_return_and_die ( check_webbie ( $test ));
}
if ( $cmd === 'checkaddr.json' ) {
$result = array ( 'error' => false , 'message' => '' );
$n = trim ( $_REQUEST [ 'nick' ]);
2018-05-22 03:17:17 +00:00
if ( ! $n ) {
$n = trim ( $_REQUEST [ 'name' ]);
}
2018-03-13 03:54:55 +00:00
$x = false ;
if ( get_config ( 'system' , 'unicode_usernames' )) {
$x = punify ( mb_strtolower ( $n ));
}
if (( ! $x ) || strlen ( $x ) > 64 )
$x = strtolower ( \URLify :: transliterate ( $n ));
2018-05-22 03:17:17 +00:00
2016-04-19 03:38:38 +00:00
$test = array ();
2018-05-22 03:17:17 +00:00
// first name
if ( strpos ( $x , ' ' ))
$test [] = legal_webbie ( substr ( $x , 0 , strpos ( $x , ' ' )));
if ( $test [ 0 ]) {
// first name plus first initial of last
$test [] = (( strpos ( $x , ' ' )) ? $test [ 0 ] . legal_webbie ( trim ( substr ( $x , strpos ( $x , ' ' ), 2 ))) : '' );
// first name plus random number
$test [] = $test [ 0 ] . mt_rand ( 1000 , 9999 );
}
2016-04-19 03:38:38 +00:00
$n = legal_webbie ( $x );
if ( strlen ( $n )) {
$test [] = $n ;
$test [] = $n . mt_rand ( 1000 , 9999 );
}
for ( $y = 0 ; $y < 100 ; $y ++ )
$test [] = 'id' . mt_rand ( 1000 , 9999 );
json_return_and_die ( check_webbie ( $test ));
}
}
2016-06-27 23:15:47 +00:00
function post () {
2016-04-19 03:38:38 +00:00
$arr = $_POST ;
2019-04-03 01:53:43 +00:00
$acc = App :: get_account ();
if ( local_channel ()) {
$parent_channel = App :: get_channel ();
if ( $parent_channel ) {
$arr [ 'parent_hash' ] = $parent_channel [ 'channel_hash' ];
}
}
2016-04-19 03:38:38 +00:00
$arr [ 'account_id' ] = get_account_id ();
// prevent execution by delegated channels as well as those not logged in.
// get_account_id() returns the account_id from the session. But \App::$account
// may point to the original authenticated account.
if (( ! $acc ) || ( $acc [ 'account_id' ] != $arr [ 'account_id' ])) {
notice ( t ( 'Permission denied.' ) . EOL );
return ;
}
$result = create_identity ( $arr );
if ( ! $result [ 'success' ]) {
notice ( $result [ 'message' ]);
return ;
}
$newuid = $result [ 'channel' ][ 'channel_id' ];
change_channel ( $result [ 'channel' ][ 'channel_id' ]);
2018-01-28 23:56:31 +00:00
$next_page = get_config ( 'system' , 'workflow_channel_next' , 'profiles' );
2016-04-19 03:38:38 +00:00
goaway ( z_root () . '/' . $next_page );
}
2016-06-27 23:15:47 +00:00
function get () {
2016-04-19 03:38:38 +00:00
2019-04-03 01:53:43 +00:00
$acc = App :: get_account ();
2016-04-19 03:38:38 +00:00
if (( ! $acc ) || $acc [ 'account_id' ] != get_account_id ()) {
notice ( t ( 'Permission denied.' ) . EOL );
return ;
}
$default_role = '' ;
$aid = get_account_id ();
if ( $aid ) {
$r = q ( " select count(channel_id) as total from channel where channel_account_id = %d " ,
intval ( $aid )
);
if ( $r && ( ! intval ( $r [ 0 ][ 'total' ]))) {
2018-05-08 00:31:53 +00:00
$default_role = get_config ( 'system' , 'default_permissions_role' , 'social' );
2016-04-19 03:38:38 +00:00
}
$limit = account_service_class_fetch ( get_account_id (), 'total_identities' );
if ( $r && ( $limit !== false )) {
$channel_usage_message = sprintf ( t ( " You have created %1 $ .0f of %2 $ .0f allowed channels. " ), $r [ 0 ][ 'total' ], $limit );
}
else {
$channel_usage_message = '' ;
}
}
2018-05-08 00:31:53 +00:00
2018-05-18 19:30:07 +00:00
$name_help = '<span id="name_help_loading" style="display:none">' . t ( 'Loading' ) . '</span><span id="name_help_text">' ;
$name_help .= (( $default_role )
2018-05-08 00:31:53 +00:00
? t ( 'Your real name is recommended.' )
: t ( 'Examples: "Bob Jameson", "Lisa and her Horses", "Soccer", "Aviation Group"' )
2018-05-18 19:30:07 +00:00
);
$name_help .= '</span>' ;
2018-05-08 00:31:53 +00:00
2018-05-18 19:30:07 +00:00
$nick_help = '<span id="nick_help_loading" style="display:none">' . t ( 'Loading' ) . '</span><span id="nick_help_text">' ;
$nick_help .= t ( 'This will be used to create a unique network address (like an email address).' );
2018-05-08 00:31:53 +00:00
if ( ! get_config ( 'system' , 'unicode_usernames' )) {
$nick_help .= ' ' . t ( 'Allowed characters are a-z 0-9, - and _' );
}
2018-05-18 19:30:07 +00:00
$nick_help .= '<span>' ;
2018-05-08 00:31:53 +00:00
2016-09-01 02:55:45 +00:00
$privacy_role = (( x ( $_REQUEST , 'permissions_role' )) ? $_REQUEST [ 'permissions_role' ] : " " );
$perm_roles = \Zotlabs\Access\PermissionRoles :: roles ();
2018-05-08 00:31:53 +00:00
$name = array ( 'name' , t ( 'Channel name' ), (( x ( $_REQUEST , 'name' )) ? $_REQUEST [ 'name' ] : '' ), $name_help , " * " );
2016-04-19 03:38:38 +00:00
$nickhub = '@' . \App :: get_hostname ();
2018-05-08 00:31:53 +00:00
$nickname = array ( 'nickname' , t ( 'Choose a short nickname' ), (( x ( $_REQUEST , 'nickname' )) ? $_REQUEST [ 'nickname' ] : '' ), $nick_help , " * " );
$role = array ( 'permissions_role' , t ( 'Channel role and privacy' ), ( $privacy_role ) ? $privacy_role : 'social' , t ( 'Select a channel permission role compatible with your usage needs and privacy requirements.' ) . '<br>' . '<a href="help/member/member_guide#Channel_Permission_Roles" target="_blank">' . t ( 'Read more about channel permission roles' ) . '</a>' , $perm_roles );
2016-04-19 03:38:38 +00:00
$o = replace_macros ( get_markup_template ( 'new_channel.tpl' ), array (
2018-05-08 00:31:53 +00:00
'$title' => t ( 'Create a Channel' ),
'$desc' => t ( 'A channel is a unique network identity. It can represent a person (social network profile), a forum (group), a business or celebrity page, a newsfeed, and many other things.' ) ,
2016-04-19 03:38:38 +00:00
'$label_import' => t ( 'or <a href="import">import an existing channel</a> from another location.' ),
'$name' => $name ,
2018-04-20 02:05:13 +00:00
'$role' => $role ,
2016-04-19 03:38:38 +00:00
'$default_role' => $default_role ,
'$nickname' => $nickname ,
2018-02-20 04:12:58 +00:00
'$validate' => t ( 'Validate' ),
2016-04-19 03:38:38 +00:00
'$submit' => t ( 'Create' ),
'$channel_usage_message' => $channel_usage_message
));
return $o ;
}
}