2019-11-19 03:36:39 +00:00
< ? php
namespace Application\Controllers ;
use Application\HTTP\Request ;
use Application\HTTP\Response ;
use Application\Services\ServiceContainer ;
use Application\Models\Category ;
use Application\Models\Post ;
use Application\Models\Thread ;
use Application\Models\UserAction ;
2019-11-20 05:19:02 +00:00
use Application\Models\UserReport ;
2019-11-21 16:28:57 +00:00
use Application\Models\User ;
2019-11-19 03:36:39 +00:00
use Application\Foundations\QueryBuilder ;
2019-11-20 05:19:02 +00:00
use Application\Foundations\MailBuilder ;
2019-11-19 03:36:39 +00:00
class ForumThreadController {
public function __construct () {
2019-11-20 05:19:02 +00:00
}
public function getBans () {
$bans = [];
if ( ServiceContainer :: Authentication () -> isLoggedIn ()) {
$where = new QueryBuilder ();
$where = $where -> where ( 'user_id' , ServiceContainer :: Session () -> get ( 'user_id' )) -> where ( 'expired_at' , '>' , date ( 'Y-m-d H:i:s' )) -> where ( 'action_type' , 'ban' ) -> orderBy ( 'expired_at' , 'desc' );
$actions = UserAction :: select ( $where );
$bans = array_map ( function ( $action ) {
return ( int ) $action -> category_id ;
}, $actions );
}
return $bans ;
2019-11-19 03:36:39 +00:00
}
public function forum ( Request $request , Response $response ) {
$thread = Thread :: find ( $request -> id );
2019-11-20 05:19:02 +00:00
$bans = $this -> getBans ();
if ( in_array ( $thread -> category_id , $bans ) ) {
return $response -> body ( " " );
}
$thread -> update ([ " view_count " => $thread -> view_count + 1 ]);
2019-11-19 03:36:39 +00:00
return $response -> view ( 'thread' , [ 'thread' => $thread ] );
}
public function process ( Request $request , Response $response ) {
$user = ServiceContainer :: Authentication () -> user ();
$eligible = true ;
if ( $user -> is_confirmed == 0 ) {
$query = new QueryBuilder ();
$query = $query -> where ( " user_id " , $user -> id ) -> where ( " created_at " , " > " , date ( " Y-m-d H:i:s " , strtotime ( " - 24 hours " )));
$posts = Post :: select ( $query );
if ( count ( $posts ) < 0 ) {
$eligible = false ;
}
}
$category = Category :: find ( $request -> category );
$thread = Thread :: find ( $request -> thread );
$reply = Post :: find ( $request -> reply );
$edit = Post :: find ( $request -> edit );
2019-11-20 05:19:02 +00:00
$delete = Post :: find ( $request -> delete );
$report = Post :: find ( $request -> report );
$time_stamp = false ;
if ( isset ( $edit )) {
$time_stamp = ( time () - strtotime ( $edit -> created_at )) > 300 ;
} else if ( isset ( $delete )) {
$time_stamp = ( time () - strtotime ( $delete -> created_at )) > 300 ;
}
if ( $time_stamp ) {
return $response -> redirect ( '/?thread=' . ( $edit -> id ? ? $delete -> id ));
}
2019-11-19 03:36:39 +00:00
if ( isset ( $edit )) {
$edit -> update ([ 'post' => $request -> content , 'updated_at' => date ( " Y-m-d H:i:s " ) ]);
2019-11-20 05:19:02 +00:00
return $response -> redirect ( '/?thread=' . $thread -> id );
} else if ( isset ( $delete )) {
$cat_id = $delete -> thread_id ;
$delete -> delete ();
return $response -> redirect ( '/?thread=' . $cat_id );
} else if ( isset ( $report )) {
$query = new QueryBuilder ();
$query -> where ( 'user_id' , $user -> id ) -> where ( 'report_date' , '>' , date ( " Y-m-d H:i:s " , strtotime ( " - 1 day " )) );
$reports = UserReport :: select ( $query );
if ( count ( $reports ) > 0 ) return $response -> body ( 'You cannot report again for a 24 hour period' );
$category = Post :: find ( $request -> report ) -> thread () -> category ();
UserReport :: create ([
'user_id' => $user -> id ,
'reason' => $request -> content ,
'post_id' => $request -> report ,
'report_date' => date ( 'Y-m-d H:i:s' ),
]);
2019-11-21 16:28:57 +00:00
if ( $report -> user () -> didIModerateThis ( $category -> id )) {
2019-11-20 05:19:02 +00:00
$query = new QueryBuilder ();
$query = $query -> where ( 'role' , '>=' , '100000' );
$admins = User :: select ( $query );
foreach ( $admins as $moderator ) {
$email = new MailBuilder ();
$body = " Dear " . $moderator -> username . " \n " ;
2019-11-21 16:28:57 +00:00
$body .= $user -> username . " reported " . $report -> user () -> username . " , a moderator for " . $category -> category_name . " for alleged violation of the rules. \n \n " ;
2019-11-20 05:19:02 +00:00
$body .= " Reason: \n \n " ;
$body .= $request -> content . " \n \n " ;
$body .= " Please resolve this via the moderation interface. \n " ;
2019-11-21 16:28:57 +00:00
$email -> from ( " metaforums@nanao.moe " ) -> to ( $moderator -> email ) -> subject ( " New report: " . $report -> user () -> username ) -> body ( $body );
2019-11-20 05:19:02 +00:00
ServiceContainer :: Email () -> send ( $email );
}
} else {
$moderators = Post :: find ( $request -> report ) -> thread () -> category () -> moderators ;
foreach ( $moderators as $moderator ) {
$email = new MailBuilder ();
$body = " Dear " . $moderator -> username . " \n " ;
$body .= " Someone reported " . $report -> user () -> username . " for alleged violation of the rules. \n \n " ;
$body .= " Reason: \n \n " ;
$body .= $request -> content . " \n \n " ;
$body .= " Please resolve this via the moderation interface. \n " ;
$email -> from ( " metaforums@nanao.moe " ) -> to ( $moderator -> email ) -> subject ( " New report: " . $user -> username ) -> body ( $body );
ServiceContainer :: Email () -> send ( $email );
}
}
return $response -> redirect ( '/?thread=' . $thread -> id );
2019-11-19 03:36:39 +00:00
} else if ( isset ( $thread ) && isset ( $reply )) {
$title = $reply -> title ;
if ( strpos ( $reply -> title , " Re: " ) != 0 ) {
$title .= " Re: " . $reply -> title ;
}
if ( $eligible ) {
Post :: create ([
'thread_id' => $thread -> id ,
'user_id' => $user -> id ,
'title' => $title ,
'post' => $request -> content ,
'created_at' => date ( " Y-m-d H:i:s " ),
'updated_at' => date ( " Y-m-d H:i:s " ),
]);
}
2019-11-20 05:19:02 +00:00
return $response -> redirect ( '/?thread=' . $thread -> id );
2019-11-19 03:36:39 +00:00
} else if ( isset ( $category )) {
$title = $request -> title ;
$thread = Thread :: create ([
'category_id' => $category -> id ,
'title' => $title ,
'author' => $user -> id ,
'created_at' => date ( " Y-m-d H:i:s " ),
'updated_at' => date ( " Y-m-d H:i:s " ),
]);
Post :: create ([
'thread_id' => $thread -> id ,
'user_id' => $user -> id ,
'title' => $title ,
'post' => $request -> content ,
'created_at' => date ( " Y-m-d H:i:s " ),
'updated_at' => date ( " Y-m-d H:i:s " ),
]);
2019-11-20 05:19:02 +00:00
return $response -> redirect ( '/?thread=' . $thread -> id );
2019-11-19 03:36:39 +00:00
}
}
public function editor ( Request $request , Response $response ) {
$title = " " ;
$category = Category :: find ( $request -> category );
$thread = Thread :: find ( $request -> thread );
$reply = Post :: find ( $request -> reply );
$edit = Post :: find ( $request -> edit );
2019-11-20 05:19:02 +00:00
$delete = Post :: find ( $request -> delete );
$report = Post :: find ( $request -> report );
2019-11-19 03:36:39 +00:00
if ( isset ( $edit )) {
$title = " Editing post " ;
2019-11-20 05:19:02 +00:00
} else if ( isset ( $report )) {
$title = " Reporting abuse by " . $report -> user () -> username ;
} else if ( isset ( $delete )) {
$title = " Are you sure to delete this post? " ;
2019-11-19 03:36:39 +00:00
} else if ( isset ( $thread ) && isset ( $reply ) && $thread -> main_post -> id == $reply -> id ) {
$title = " Replying to Main Post " ;
} else if ( isset ( $thread ) && isset ( $reply )) {
$title = " Replying to " . $reply -> user () -> username ;
} else if ( isset ( $category )) {
2019-11-20 05:19:02 +00:00
$title = " Creating Thread in " . $category -> category_name ;
}
return $response -> view ( 'editor' , [ 'title' => $title , 'category' => $category , 'thread' => $thread , 'edit' => $edit , 'reply' => $reply , 'delete' => $delete , " report " => $report , 'thread_post' => $thread ] );
}
public function moderating_editor ( Request $request , Response $response ) {
$post = Post :: find ( $request -> id );
$title = " Moderating " ;
return $response -> view ( 'moderating-editor' , [ 'title' => $title , 'post' => $post ]);
}
public function moderate ( Request $request , Response $response ) {
$user = ServiceContainer :: Authentication () -> user ();
$post = Post :: find ( $request -> post );
$thread = Thread :: find ( $request -> thread );
$category = Category :: find ( $request -> category );
$action = $request -> action ;
UserAction :: create ([
'user_id' => $post -> user () -> id ,
'thread_id' => $thread -> id ? ? 0 ,
'category_id' => $category -> id ? ? 0 ,
'action_type' => $action ,
'reason' => $request -> content ,
'action_at' => date ( 'Y-m-d H:i:s' ),
'expired_at' => date ( 'Y-m-d H:i:s' , strtotime ( $request -> duration )) ,
]);
if ( $action == " lock " ) {
Post :: create ([
'thread_id' => $thread -> id ,
'user_id' => $user -> id ,
'title' => " Locking " . $thread -> title ,
'post' => $request -> content ,
'created_at' => date ( " Y-m-d H:i:s " ),
'updated_at' => date ( " Y-m-d H:i:s " ),
]);
} else {
$offending_user = $post -> user ();
$email = new MailBuilder ();
$body = " A moderator, " . $user -> username . " has decided to apply a " . $action . " to your account. \n " ;
$body .= " Reason: \n \n " ;
$body .= $request -> content . " \n \n " ;
$body .= " To appeal this decision, contact the moderator who applied the " . $action . " . \n " ;
$email -> from ( " metaforums@nanao.moe " ) -> to ( $offending_user -> email ) -> subject ( " A " . $action . " was applied to your account " ) -> body ( $body );
ServiceContainer :: Email () -> send ( $email );
}
return $response -> redirect ( '/?thread=' . $thread -> id );
}
public function unlock ( Request $request , Response $response ) {
$thread = Thread :: find ( $request -> id );
if ( $thread -> lock ()) {
$thread -> lock () -> delete ();
2019-11-19 03:36:39 +00:00
}
2019-11-20 05:19:02 +00:00
return $response -> redirect ( '/?thread=' . $thread -> id );
2019-11-19 03:36:39 +00:00
}
}