475 lines
15 KiB
PHP
475 lines
15 KiB
PHP
<?php
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// session_start();
|
|
// date_default_timezone_set('America/Chicago');
|
|
// // /home/cradle2careertxxyz/websites/www15/portal/dashboard/services/api/php
|
|
// require "init.php";
|
|
// require "functions.php";
|
|
|
|
// if(Input::get("admin_login")){
|
|
// admin_login();
|
|
// }
|
|
// if(Input::get("admin_logout")){
|
|
// admin_logout();
|
|
// }
|
|
// if(Input::get('initialize_dashboard')){
|
|
// initialize_dashboard();
|
|
// }
|
|
// if(Input::get('create_email_account')){
|
|
// create_email_account();
|
|
// }
|
|
// if(Input::get('get_all_email_accounts')){
|
|
// get_all_email_accounts();
|
|
// }
|
|
// if(Input::get('check_username')){
|
|
// check_username();
|
|
// }
|
|
// if(Input::get("validate_new_user")){
|
|
// validate_new_user();
|
|
// }
|
|
// if(Input::get('update_user_email_data')){
|
|
// update_user_email_data();
|
|
// }
|
|
// if(Input::get('send_subscribed_confirmation_email')){
|
|
// send_subscribed_confirmation_email();
|
|
// }
|
|
// if(Input::get('contact_us')){
|
|
// contact_us();
|
|
// }
|
|
// if(Input::get('afs_plugin_loader')){
|
|
// afs_plugin_loader();
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// tail -f -n50 /var/log/maillog
|
|
// tail -f -n50 /var/log/php-fpm/www-error.log
|
|
// tail -f -n50 /var/log/mylog.txt
|
|
// tail -f -n50 /home/webnetpagescom/websites/www/portal/dashboard/services/server/create_domains/output
|
|
|
|
|
|
function initialize_dashboard(){
|
|
$response = array("accounts" => "");
|
|
// $db = new DB();
|
|
// $rows = $db->query("SELECT * FROM managed_domain");
|
|
// if($rows->count() > 0){
|
|
// $response["domain"] = $rows->first()->Domain;
|
|
// $response["email_url"] = $rows->first()->EmailUrl;
|
|
// $response["server"] = $rows->first()->EmailServer;
|
|
// $response['auto_login_url'] = $rows->first()->AutoEmailLoginUrl;
|
|
// }
|
|
// $db = null;
|
|
// echo json_encode($response);
|
|
|
|
|
|
$subdomain = file_get_contents("../../portal/admin/type.txt");
|
|
$subdomain = str_replace("\n", "", $subdomain);
|
|
$subdomain = trim($subdomain, " ");
|
|
|
|
$db = new DB();
|
|
$rows = $db->query("SELECT * FROM managed_domain WHERE SubDomain='".$subdomain."'");
|
|
$managed_domain = array('access' => false);
|
|
if($rows->count()){
|
|
$response['managed_domain']['access'] = true;
|
|
$response['managed_domain']['SubDomain'] = $rows->first()->SubDomain;
|
|
$response['managed_domain']['Domain'] = $rows->first()->Domain;
|
|
$response['managed_domain']['EmailUrl'] = $rows->first()->EmailUrl;
|
|
$response['managed_domain']['EmailServer'] = $rows->first()->EmailServer;
|
|
$response['managed_domain']['AutoEmailLoginUrl'] = $rows->first()->AutoEmailLoginUrl;
|
|
$response['managed_domain']['PaypayServiceUrl'] = $rows->first()->PaypayServiceUrl;
|
|
$response['managed_domain']['AppfactoryUrl'] = $rows->first()->AppfactoryUrl;
|
|
|
|
$response['managed_domain']['Address'] = $rows->first()->Address;
|
|
$response['managed_domain']['PaypalPort'] = $rows->first()->PaypalPort;
|
|
$response['managed_domain']['PaypalEndPoint'] = $rows->first()->PaypalEndPoint;
|
|
$response['managed_domain']['CertPrivateKeyLocation'] = $rows->first()->CertPrivateKeyLocation;
|
|
$response['managed_domain']['CertPublicKeyLocation'] = $rows->first()->CertPublicKeyLocation;
|
|
$response['managed_domain']['PaypalClientId'] = $rows->first()->PaypalClientId;
|
|
$response['managed_domain']['PaypalAppSecret'] = $rows->first()->PaypalAppSecret;
|
|
}
|
|
$db = null;
|
|
|
|
echo json_encode($response);
|
|
}
|
|
function check_username(){
|
|
$db = new DB();
|
|
$username = Input::get('username');
|
|
$rows = $db->query("SELECT * FROM email_users WHERE username='" . $username . "'");
|
|
$response = array("available" => true);
|
|
if($rows->count() > 0){
|
|
$response["available"] = false;
|
|
}
|
|
echo json_encode($response);
|
|
}
|
|
function get_all_email_accounts(){
|
|
|
|
$response = _get_all_accounts();
|
|
echo json_encode($response);
|
|
|
|
}
|
|
function _get_all_accounts(){
|
|
$response = array();
|
|
$db = new DB();
|
|
$rows = $db->query("SELECT * FROM email_users");
|
|
if($rows->count() > 0){
|
|
for($i=0; $i < $rows->count(); $i++){
|
|
$response[$i]["verified"]["id"] = $rows->results()[$i]->id;
|
|
$response[$i]["verified"]["username"] = $rows->results()[$i]->username;
|
|
$response[$i]["verified"]["password"] = str_replace("{plain}","",$rows->results()[$i]->password);
|
|
$response[$i]["verified"]["domain"] = $rows->results()[$i]->domain;
|
|
$response[$i]["verified"]["userid"] = $rows->results()[$i]->userid;
|
|
$response[$i]["verified"]["home"] = $rows->results()[$i]->home;
|
|
$response[$i]["verified"]["type"] = $rows->results()[$i]->type;
|
|
$response[$i]["verified"]["level"] = $rows->results()[$i]->level;
|
|
$response[$i]["verified"]["category"] = $rows->results()[$i]->category;
|
|
$response[$i]["verified"]["verified"] = $rows->results()[$i]->verified;
|
|
$response[$i]["verified"]["backupEmail"] = $rows->results()[$i]->backupEmail;
|
|
$response[$i]["verified"]["firstname"] = $rows->results()[$i]->firstname;
|
|
$response[$i]["verified"]["lastname"] = $rows->results()[$i]->lastname;
|
|
$response[$i]["verified"]["token"] = $rows->results()[$i]->token;
|
|
$response[$i]["verified"]["expiry"] = $rows->results()[$i]->expiry;
|
|
$response[$i]["verified"]["date"] = $rows->results()[$i]->date;
|
|
}
|
|
}
|
|
$db = null;
|
|
|
|
$db = new DB();
|
|
$rows = $db->query("SELECT * FROM email_users_copy WHERE verified=0");
|
|
if($rows->count() > 0){
|
|
for($i=0; $i < $rows->count(); $i++){
|
|
$response[$i]["unverified"]["id"] = $rows->results()[$i]->id;
|
|
$response[$i]["unverified"]["username"] = $rows->results()[$i]->username;
|
|
$response[$i]["unverified"]["password"] = $rows->results()[$i]->password;
|
|
$response[$i]["unverified"]["domain"] = $rows->results()[$i]->domain;
|
|
$response[$i]["unverified"]["userid"] = $rows->results()[$i]->userid;
|
|
$response[$i]["unverified"]["home"] = $rows->results()[$i]->home;
|
|
$response[$i]["unverified"]["type"] = $rows->results()[$i]->type;
|
|
$response[$i]["unverified"]["level"] = $rows->results()[$i]->level;
|
|
$response[$i]["unverified"]["category"] = $rows->results()[$i]->category;
|
|
$response[$i]["unverified"]["verified"] = $rows->results()[$i]->verified;
|
|
$response[$i]["unverified"]["backupEmail"] = $rows->results()[$i]->backupEmail;
|
|
$response[$i]["unverified"]["firstname"] = $rows->results()[$i]->firstname;
|
|
$response[$i]["unverified"]["lastname"] = $rows->results()[$i]->lastname;
|
|
$response[$i]["unverified"]["token"] = $rows->results()[$i]->token;
|
|
$response[$i]["unverified"]["expiry"] = $rows->results()[$i]->expiry;
|
|
$response[$i]["unverified"]["date"] = $rows->results()[$i]->date;
|
|
}
|
|
}
|
|
$db = null;
|
|
return $response;
|
|
}
|
|
function update_user_email_data(){
|
|
$data = json_decode(Input::get("data"));
|
|
$db = new DB();
|
|
$db->updateFields("email_users",array(
|
|
"firstname" => $data->firstname,
|
|
"lastname" => $data->lastname,
|
|
"category" => $data->category,
|
|
"level" => $data->level
|
|
),array(
|
|
"userid" => $data->username
|
|
));
|
|
$db = null;
|
|
echo "success " . $data->category;
|
|
}
|
|
|
|
function send_invite_email($fields){
|
|
|
|
// joseph@imminentimagination.com
|
|
|
|
$email_invite_template = file_get_contents("email_invite_template.html");
|
|
$domain = $fields["domain"];
|
|
$token = $fields["token"];
|
|
|
|
// https://www2.cradle2careertx.xyz/validation/email_account/normal/index.php?action=activate&domain=cradle2careertx.xyz&token=5555
|
|
$url = _getURL() . "?action=activate&domain=$domain&token=$token";
|
|
$body = str_replace("{url}", $url, $email_invite_template);
|
|
|
|
$db = new DB();
|
|
$rows = $db->query("SELECT * FROM email_users WHERE userid='".$fields["sender"]."'");
|
|
if($rows->count() > 0){
|
|
$obj = array(
|
|
'send_email' => true,
|
|
'to' => $fields["backupEmail"],
|
|
'from' => $fields["sender"] . "@" . $fields["domain"],
|
|
'subject' => "Activate Email Account",
|
|
// TODO: resplace hard coded domain
|
|
'server' => $fields["server"],
|
|
'password' => str_replace("{plain}","",$rows->first()->password),
|
|
'body' => $body
|
|
);
|
|
|
|
// TODO: resplace hard coded domain
|
|
$requestUrl = "http://live1.appfactory.studio/plugins/app/php/server/request.php";
|
|
|
|
// use key 'http' even if you send the request to https://...
|
|
$options = array(
|
|
'http' => array(
|
|
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
|
|
'method' => 'POST',
|
|
'content' => http_build_query($obj)
|
|
)
|
|
);
|
|
$context = stream_context_create($options);
|
|
$result = file_get_contents($requestUrl, false, $context);
|
|
if ($result === FALSE) {
|
|
$result = array("status" => "failure");
|
|
}else{
|
|
|
|
}
|
|
echo json_encode($result);
|
|
}
|
|
|
|
}
|
|
function _getURL(){
|
|
$url = "";
|
|
$db2 = new DB();
|
|
$rows = $db2->query("SELECT * FROM managed_domain");
|
|
if($rows->count() > 0){
|
|
$url = $rows->first()->EmailUrl;
|
|
}
|
|
$db2 = null;
|
|
return $url;
|
|
}
|
|
|
|
// http://live1.appfactory.studio/plugins/app/php/server/request.php?create_email=true&username=testing13&password=mypass84&domain=cradle2careertx.xyz&type=1&level=0&category=1&backupEmail=my@backup.com
|
|
// https://www2.cradle2careertx.xyz/admin/core/request.php?create_email_account=true&username=testing14&password=mypass84&domain=cradle2careertx.xyz&type=1&level=0&category=1&backupEmail=my@backup.com&firstname=james&lastname=mitchell&token=sdjhfkjdhfadf
|
|
function create_email_account(){
|
|
$expiry = Input::get("expiry").":00";
|
|
$fields = array();
|
|
|
|
// portal/validation/email_account/normal/main.js
|
|
// portal/admin/core/js/components/create_email_components.js
|
|
|
|
// echo json_encode(array("status" => true));
|
|
// return;
|
|
|
|
// The state is is the phase at which the email is being created
|
|
// 0 = admin initiated
|
|
// 1 = user verified
|
|
$state = Input::get("state");
|
|
$data = json_decode(Input::get("data"));
|
|
$fields = array();
|
|
if($state==0){
|
|
$token = generate_uuid();
|
|
$fields = array(
|
|
"state" => $state,
|
|
"username" => strtolower($data->username),
|
|
"userid" => strtolower($data->userid),
|
|
"password" => $data->password,
|
|
"firstname" => ($data->firstname=="") ? "_not_set_" : $data->firstname,
|
|
"lastname" => ($data->lastname=="") ? "_not_set_" : $data->lastname,
|
|
"level" => $data->level,
|
|
"type" => $data->type,
|
|
"category" => $data->category,
|
|
"backupEmail" => $data->backupEmail,
|
|
"domain" => $data->domain,
|
|
"verified" => $data->verified,
|
|
"token" => $token,
|
|
"sender" => $data->sender,
|
|
"server" => $data->server,
|
|
"expiry" => date("Y-m-d H:i:s", strtotime(sprintf("+%d hours", $data->expiry))) //hoursToMinutes($expiry)
|
|
);
|
|
send_invite_email($fields);
|
|
//echo "<br>\n\r " . $data->domain;
|
|
}else{
|
|
$fields = array(
|
|
"state" => $state,
|
|
"username" => strtolower($data->username),
|
|
"userid" => strtolower($data->userid),
|
|
"password" => $data->password,
|
|
"firstname" => ($data->firstname=="" || $data->firstname==null) ? "_not_set_" : $data->firstname,
|
|
"lastname" => ($data->lastname=="" || $data->lastname==null) ? "_not_set_" : $data->lastname,
|
|
"level" => $data->level,
|
|
"type" => $data->type,
|
|
"category" => $data->category,
|
|
"backupEmail" => $data->backupEmail,
|
|
"domain" => $data->domain,
|
|
"verified" => $data->verified,
|
|
"token" => ($data->token == "") ? generate_uuid() : $data->token,
|
|
"expiry" => "null",//date("Y-m-d H:i:s", strtotime(sprintf("+%d hours", Input::get("expiry")))) //hoursToMinutes($expiry)
|
|
);
|
|
}
|
|
|
|
create_email2($fields);
|
|
|
|
//echo json_encode($fields);
|
|
|
|
|
|
// $apple = new AppfactoryStudioAPI();
|
|
// echo $apple->create_email(json_encode($fields),$state);
|
|
}
|
|
|
|
// https://stackoverflow.com/questions/46358550/convert-hours-and-minutes-to-minutes-using-php
|
|
// Transform hours like "1:45" into the total number of minutes, "105".
|
|
function hoursToMinutes($hours)
|
|
{
|
|
$minutes = 0;
|
|
if (strpos($hours, ':') !== false)
|
|
{
|
|
// Split hours and minutes.
|
|
list($hours, $minutes) = explode(':', $hours);
|
|
}
|
|
return (intval($hours) * 60 + $minutes);
|
|
}
|
|
// Transform minutes like "105" into hours like "1:45".
|
|
function minutesToHours($minutes)
|
|
{
|
|
$hours = (int)($minutes / 60);
|
|
$minutes -= $hours * 60;
|
|
return sprintf("%d:%02.0f", $hours, $minutes);
|
|
}
|
|
|
|
|
|
|
|
function admin_logout(){
|
|
session_unset();// remove all session variables
|
|
session_destroy();// destroy the session
|
|
echo "logged out";
|
|
}
|
|
function admin_login(){
|
|
$username = Input::get('username');
|
|
$password = Input::get('password');
|
|
$db = new DB();
|
|
$rows = $db->query("SELECT * FROM admin_users WHERE Username='" . $username . "'");
|
|
$response = array('status' => false, 'message' => "");
|
|
if($rows->count() > 0){
|
|
$passwd = $rows->first()->Password;
|
|
if($passwd == $password){
|
|
$response["status"] = true;
|
|
$response["message"] = "success";
|
|
}else{
|
|
$response["message"] = "incorrect password";
|
|
}
|
|
}else{
|
|
$response["message"] = "Username and/or password incorrect";
|
|
}
|
|
$_SESSION['username'] = $username;
|
|
echo json_encode($response);
|
|
}
|
|
|
|
|
|
function generateRandomString($length = 10) {
|
|
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
$charactersLength = strlen($characters);
|
|
$randomString = '';
|
|
for ($i = 0; $i < $length; $i++) {
|
|
$randomString .= $characters[rand(0, $charactersLength - 1)];
|
|
}
|
|
return $randomString;
|
|
}
|
|
function generate_uuid() {
|
|
return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
|
|
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
|
|
mt_rand( 0, 0xffff ),
|
|
mt_rand( 0, 0x0C2f ) | 0x4000,
|
|
mt_rand( 0, 0x3fff ) | 0x8000,
|
|
mt_rand( 0, 0x2Aff ), mt_rand( 0, 0xffD3 ), mt_rand( 0, 0xff4B )
|
|
);
|
|
}
|
|
|
|
|
|
function validate_new_user(){
|
|
//strtotime('+5 hours');
|
|
//strtotime("+{$hours} hours");
|
|
//strtotime(sprintf("+%d hours", $hours));
|
|
|
|
/*
|
|
$minutes_to_add = $data["expiry"];//hoursToMinutes(":00");
|
|
//$minutes_to_add = 15;
|
|
$lastCheckInDate = new DateTime("2023-02-01 12:49:04");
|
|
//$lastCheckInDate = new DateTime("2023-02-01 11:59:32");
|
|
$lastCheckInDate->add(new DateInterval('PT' . $minutes_to_add . 'M'));
|
|
$logoutTime = $lastCheckInDate->format('Y-m-d H:i:s');
|
|
*/
|
|
|
|
// $dbtimestamp = strtotime($logoutTime);
|
|
// $time = time();
|
|
// if (time() - $dbtimestamp > $minutes_to_add * 60) {
|
|
// // 15 mins has passed
|
|
// echo "yes";
|
|
// }else{
|
|
// echo "no";
|
|
// }
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function contact_us(){
|
|
|
|
}
|
|
function send_subscribed_confirmation_email(){
|
|
|
|
// joseph@imminentimagination.com
|
|
$fields = array();
|
|
$email_invite_template = file_get_contents("templates/email/subscribed_confirmation_email_template.html");
|
|
$domain = $fields["domain"];
|
|
$token = $fields["token"];
|
|
|
|
// https://www2.cradle2careertx.xyz/validation/email_account/normal/index.php?action=activate&domain=cradle2careertx.xyz&token=5555
|
|
$url = _getURL() . "?action=activate&domain=$domain&token=$token";
|
|
$body = str_replace("{url}", $url, $email_invite_template);
|
|
|
|
$db = new DB();
|
|
$rows = $db->query("SELECT * FROM email_users WHERE userid='".$fields["sender"]."'");
|
|
if($rows->count() > 0){
|
|
$obj = array(
|
|
'send_email' => true,
|
|
'to' => $fields["backupEmail"],
|
|
'from' => $fields["sender"] . "@" . $fields["domain"],
|
|
'subject' => "Activate Email Account",
|
|
'server' => $fields["server"],
|
|
'password' => str_replace("{plain}","",$rows->first()->password),
|
|
'body' => $body
|
|
);
|
|
|
|
// TODO: resplace hard coded domain
|
|
$requestUrl = "http://live1.appfactory.studio/plugins/app/php/server/request.php";
|
|
|
|
// use key 'http' even if you send the request to https://...
|
|
$options = array(
|
|
'http' => array(
|
|
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
|
|
'method' => 'POST',
|
|
'content' => http_build_query($obj)
|
|
)
|
|
);
|
|
$context = stream_context_create($options);
|
|
$result = file_get_contents($requestUrl, false, $context);
|
|
if ($result === FALSE) {
|
|
$result = array("status" => "failure");
|
|
}else{
|
|
|
|
}
|
|
echo json_encode($result);
|
|
}
|
|
|
|
}
|
|
|
|
|