<?php 

function deactivate_post($args) {
	$post = new post;
	if(!isset($args['id'])) return ['status' => false, 'msg' => 'Oops, you cannot deactivate this post at the moment try again later.'];
	$res = $post->update_post_meta($args['id'], ['status' => 'inactive']);
	if($res) {
		return ['status' => true, 'msg' => 'Congratulations, you have successfully deactivated the post.'];
	} else return ['status' => false, 'msg' => 'Oops, you cannot deactivate this post at the moment try again later.'.$args['id']];
}

function activate_post($args) {
	$post = new post;
	if(!isset($args['id'])) return ['status' => false, 'msg' => 'Oops, you cannot activate this post at the moment try again later.'];
	$res = $post->update_post_meta($args['id'], ['status' => 'active']);
	if($res) {
		return ['status' => true, 'msg' => 'Congratulations, you have successfully activated the post.'];
	} else return ['status' => false, 'msg' => 'Oops, you cannot activate this post at the moment try again later.'.$args['id']];
}

function delete_post($args) {
	$post = new post;
	if(!isset($args['id'])) return;
	$post->drop_post($args['id']);
	return ['status' => true, 'msg' => 'Congratulations, you have successfully deleted this post.'];
}

function activate_user($args) {
	$user = new user;
	if(!isset($args['id'])) return;
	$user->update_user_meta($args['id'], ['status' => 'active']);
	return ['status' => true, 'msg' => 'Congratulations, you have successfully activated this user account.'];
}

function deactivate_user($args) {
	$user = new user;
	if(!isset($args['id'])) return;
	$user->update_user_meta($args['id'], ['status' => 'inactive']);
	return ['status' => true, 'msg' => 'Congratulations, you have successfully deactivated this user account.'];
}

function delete_user($args) {
	$user = new user;
	$session = Session::getInstance();
	if(!isset($args['id'])) return;
	if(!current_user_can("delete_user")) {
		return ['status' => false, 'msg' => 'Sorry, you cannot delete user account.'];
	}
	if($args['id'] == $session->id) {
		return ['status' => false, 'msg' => 'Sorry, you cannot delete your own account.'];
	}
	deleteDir("uploads/".$args['id']."/");
	$user->drop_user($args['id']);
	return ['status' => true, 'msg' => 'Congratulations, you have successfully deleted this user account.'];
}

function resend_verification_link ($args) {
	$user = new user;
	$user_meta = $user->get_user_meta($args['id']);
	$link = get_option('home_url').ACTIVATION_PAGE.'?action='.base64_encode('activate_account').'&id='.base64_encode($args["id"]).'&authe='.base64_encode($user_meta['activation']);
	$info = "<p>Welcome to ".get_option('company_name').". To verify your email your account, please follow this link:</p> <a title=\"activate account\" href=\"".$link."\">".$link."</a>";
	inform_user($user_meta, $info, "New Account Verification", $link, EMAIL, SMS);
	return ["msg" => "You have successfully resent your account verification link", "status" => true];
}

function login_as_user($args) {
	if(!current_user_can('login_as_user'))
		return;
	$user = new user;
	$session = Session::getInstance();
	$user_meta = $user->get_user_meta($args['id']);
	if($user_meta === false) {
		return;
	}
	$loggon_key = SHA1(time(). SALT);
	$login_meta = (isset($user_meta['login_meta'])) ? unserialize($user_meta['login_meta']) : array();
	$login_meta = [
		'last_login' => time(),
		'loggon_key' => $loggon_key,
		'state' => 'online'
	];
	$login_meta = serialize($login_meta);
	unset($session->menu);
	$user->update_user_meta($user_meta['id'], array('loggon_key' => $loggon_key, 'login_meta' => $login_meta));
	$session->id = $user_meta['id'];
	$session->key = $loggon_key;
}

function activate_account($args) {
	ignore_user_abort(true); // just to be safe
	$session = Session::getInstance();
	$id = base64_decode($args['id']);
	$authe = base64_decode($args['authe']);
	$user = new user;
	$umeta = $user->get_user_meta($id);
	if($authe === $umeta['activation'] && $umeta['activation'] != '') {
		$settings = ['status' => 'active', 'activation' => ''];
		if($umeta['role'] === 'unverified') {
			$settings['role'] = ($option = get_option('default_user_role')) ? $option : DEFAULT_USER_ROLE;
			$settings['role'] = (isset($umeta['temp_role'])) ? $umeta['temp_role'] : $settings['role'];
			$settings['role'] = (trim($settings['role']) == '') ? DEFAULT_USER_ROLE : $settings['role'];
			$settings['role'] = apply_filters("user_role_at_activation", $settings['role'], $umeta);
		}
		$user->update_user_meta($id, $settings);
	}
}

function delete_outward_site_binding($args) {
	$post = new post;
	$meta = $post->get_post_meta($args['id']);
	$cURLConnection = curl_init($meta['url']);
	if($cURLConnection) {
		curl_setopt($cURLConnection, CURLOPT_POSTFIELDS, [
			'binding_id' => $meta['binding_id'],
			'action' => 'delete_site_binding'
		]);
		curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true);
		$res = curl_exec($cURLConnection);
		curl_close($cURLConnection);
		$res = json_decode($res);
	}
	$post->drop_post($meta['id']);
	return ['status' => true, 'msg' => 'Done'];
}

function delete_inward_site_binding($args) {
	$post = new post;
	$meta = $post->get_post_meta($args['id']);
	$post->drop_post($meta['id']);
	return ['status' => true, 'msg' => 'Done'];
}

function approve_inward_site_binding($args) {
	$post = new post;
	$session = Session::getInstance();
	$meta = $post->get_post_meta($args['id']);
	$post->update_post_meta($meta['id'], ['status' => 'active', 'approved_by' => $session->id, 'approved_on' => time()]);
	$url = trim($meta['ref_url']);
	$url .= (substr($url, -1) === "/" || substr($url, -1) === "\\") ? "" : "/";
	$url .= "app/includes/ajax.php";
	$cURLConnection = curl_init($url);
	if($cURLConnection) {
		$args = ["action" => "notify_binding", "msg" => "Congratulations, your request to bind with ".$meta['site_name']." was approved by the site admin.", "subject" => "BINDING REQUEST APPROVED"];
		curl_setopt($cURLConnection, CURLOPT_POSTFIELDS, $args);
		curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true);
		$res = curl_exec($cURLConnection);
		curl_close($cURLConnection);
	}
	return ['status' => true, 'msg' => 'Done'];
}

function disallow_inward_site_binding($args) {
	$post = new post;
	$session = Session::getInstance();
	$meta = $post->get_post_meta($args['id']);
	$post->update_post_meta($meta['id'], ['status' => 'disallowed', 'disallowed_by' => $session->id, 'disallowed_on' => time()]);
	$url = trim($meta['ref_url']);
	$url .= (substr($url, -1) === "/" || substr($url, -1) === "\\") ? "" : "/";
	$url .= "app/includes/ajax.php";
	$cURLConnection = curl_init($url);
	if($cURLConnection) {
		$args = ["action" => "notify_binding", "msg" => "Sorry, your request to bind with ".$meta['site_name']." was disallowed by the site admin.", "subject" => "BINDING REQUEST DISALLOWED"];
		curl_setopt($cURLConnection, CURLOPT_POSTFIELDS, $args);
		curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true);
		$res = curl_exec($cURLConnection);
		curl_close($cURLConnection);
	}
	return ['status' => true, 'msg' => 'Done'];
}

function make_cron_active($args) {
	$query = new query;
	$query->update("crontabs",
		["status" => "active"],
		["id" => $args['id']]
	);
	return ['status' => true, 'msg' => 'Done'];
}

function delete_cron($args) {
	$query = new query;
	$query->delete("crontabs",
		["id" => $args['id']]
	);
	return ['status' => true, 'msg' => 'Done'];
}

function cancel_cron($args) {
	$query = new query;
	$query->update("crontabs",
		["status" => "cancelled"],
		["id" => $args['id']]
	);
	return ['status' => true, 'msg' => 'Done'];
}

function run_cron_now($args) {
	$cronjobs = new cronjobs;
	$cron_meta = db_item_unique(['id' => $args['id']], "crontabs");
	if($cron_meta) {
		$cronjobs->execute($cron_meta);
		return ['status' => true, 'msg' => 'Done'];
	}
	return ['status' => false, 'msg' => 'Cron tab not found'];
}