-- mysql -u root -p < create_database.sql  

CREATE DATABASE IF NOT EXISTS webnetpagesdb;
CREATE USER 'webnetpages'@'localhost' IDENTIFIED BY 'jdkfjRGfdkE305!fklldgkl';
GRANT ALL ON webnetpagesdb.* TO 'webnetpages'@'localhost';

CREATE DATABASE IF NOT EXISTS roundcubedb;
CREATE USER 'roundcube'@'localhost' IDENTIFIED BY 'jdkfjRGfdkE305!fklldgkl';
GRANT ALL ON roundcubedb.* TO 'roundcube'@'localhost';

FLUSH PRIVILEGES; 
FLUSH HOSTS;

/* /home/appfactorystudio/websites/www/plugins/app/php/server/create_domains */
USE webnetpagesdb;

-- https://stackoverflow.com/questions/50093144/mysql-8-0-client-does-not-support-authentication-protocol-requested-by-server
ALTER USER 'webnetpages'@'localhost' IDENTIFIED WITH mysql_native_password BY 'jdkfjRGfdkE305!fklldgkl';

CREATE TABLE IF NOT EXISTS managed_domain (
	id INT AUTO_INCREMENT PRIMARY KEY,
	SubDomain VARCHAR(255) NULL,
	Domain VARCHAR(255) NULL,
	EmailUrl TEXT,
	EmailServer VARCHAR(255) NULL,
	AutoEmailLoginUrl VARCHAR(255) NULL,
	PaypayServiceUrl VARCHAR(255) NULL,
	AppfactoryUrl VARCHAR(255) NULL,
	Address VARCHAR(255) NULL,
	PayPalPort VARCHAR(255) NULL,
	PayPalEndPoint VARCHAR(255) NULL,
	CertPrivateKeyLocation VARCHAR(255) NULL,
	CertPublicKeyLocation VARCHAR(255) NULL,
	PaypalClientId VARCHAR(255) NULL,
	PaypalAppSecret VARCHAR(255) NULL,
	date TIMESTAMP DEFAULT CURRENT_TIMESTAMP()
);
INSERT INTO managed_domain \
(SubDomain,\
Domain,\
EmailUrl,\
EmailServer,\
AutoEmailLoginUrl,\
PaypayServiceUrl,\
AppfactoryUrl,\
Address,\
PaypalPort,\
PaypalEndPoint,\
CertPrivateKeyLocation,\
CertPublicKeyLocation,\
PaypalClientId,\
PaypalAppSecret) \
VALUES ( \
"www", \
"webnetpages.com", \
"https://www.webnetpages.com/portal/validation/email_account/normal/index.php", \
"server1.webnetpages.com", \
"https://www.webnetpages.com/portal/mailclient/1/RoundcubeAutoLogin.php", \
"https://www.webnetpages.com:9001/get_token", \
"http://live1.appfactory.studio/plugins/app/php/server/request.php",\
"https://www.webnetpages.com",\
"9011",\
"paypal_checkout",\
"/etc/letsencrypt/live/www.webnetpages.com/privkey.pem",\
"/etc/letsencrypt/live/www.webnetpages.com/cert.pem",\
"AbehsBM-Cg5UwIx6pAWObkiltltLPwN82QlhN-u4qwYMD7Fzly_mDmUGYnSJqHuMLIWjhBk9I5iKsvIN",\
"EIMOBpKnMGNlgaofARDgpuigkvpoSilNUJQSXSmXhNRwxrVosOVp1DCpb7Ky2OfjX7rR0SqszgRMyg1-"); 



CREATE TABLE IF NOT EXISTS email_users (
	id INT AUTO_INCREMENT PRIMARY KEY,
	username VARCHAR(255) NULL,
	password VARCHAR(255) NULL,
	domain VARCHAR(255) NULL,
	userid VARCHAR(255) NULL,
	home VARCHAR(255) NULL,
	type INT DEFAULT 1,
	level INT DEFAULT 0,
	category INT DEFAULT 0,
	verified INT DEFAULT 0,
	backupEmail VARCHAR(255) NULL,
	firstname VARCHAR(255) NULL,
	lastname VARCHAR(255) NULL,
	token VARCHAR(255) NULL,
	expiry TIMESTAMP NULL,
	uid VARCHAR(255) NULL,
	gid INT NULL,
	date TIMESTAMP DEFAULT CURRENT_TIMESTAMP()
);
CREATE TABLE IF NOT EXISTS email_users_copy (
	id INT AUTO_INCREMENT PRIMARY KEY,
	username VARCHAR(255) NULL,
	password VARCHAR(255) NULL,
	domain VARCHAR(255) NULL,
	userid VARCHAR(255) NULL,
	home VARCHAR(255) NULL,
	type INT DEFAULT 1,
	level INT DEFAULT 0,
	category INT DEFAULT 0,
	verified INT DEFAULT 0,
	backupEmail VARCHAR(255) NULL,
	firstname VARCHAR(255) NULL,
	lastname VARCHAR(255) NULL,
	token VARCHAR(255) NULL,
	expiry TIMESTAMP NULL,
	uid VARCHAR(255) NULL,
	gid INT NULL,
	date TIMESTAMP DEFAULT CURRENT_TIMESTAMP()
);


USE webnetpagesdb; 
CREATE TABLE IF NOT EXISTS admin_users (
	id INT AUTO_INCREMENT PRIMARY KEY,
	Username VARCHAR(255) NULL,
	Email VARCHAR(255) NULL,
	Password VARCHAR(255) NULL,
	date TIMESTAMP DEFAULT CURRENT_TIMESTAMP()
);
CREATE TABLE IF NOT EXISTS application_settings (
	id INT AUTO_INCREMENT PRIMARY KEY,
	category VARCHAR(255) NULL,
	json MEDIUMTEXT,
	date TIMESTAMP DEFAULT CURRENT_TIMESTAMP()
);
CREATE TABLE IF NOT EXISTS receipts (
	id INT AUTO_INCREMENT PRIMARY KEY,
	type VARCHAR(255) NULL,
	name VARCHAR(255) NULL,
	email VARCHAR(255) NULL,
	amount VARCHAR(255) NULL,
	processor VARCHAR(255) NULL,
	receipt MEDIUMTEXT,
	orderId VARCHAR(255) NULL,
	date TIMESTAMP DEFAULT CURRENT_TIMESTAMP()
);
CREATE TABLE IF NOT EXISTS subscribers (
	id INT AUTO_INCREMENT PRIMARY KEY,
	email VARCHAR(255) NULL,
	name INT DEFAULT 1,
	subscribed INT DEFAULT 1,
	subscriptions VARCHAR(255) NULL,
	assigned_from VARCHAR(255) NULL,
	date TIMESTAMP DEFAULT CURRENT_TIMESTAMP()
);
CREATE TABLE IF NOT EXISTS contact_us_form (
	id INT AUTO_INCREMENT PRIMARY KEY,
	name VARCHAR(255) NULL,
	email VARCHAR(255) NULL,
	message TEXT,
	assigned_from VARCHAR(255) NULL,
	date TIMESTAMP DEFAULT CURRENT_TIMESTAMP()
);
CREATE TABLE IF NOT EXISTS contact (
	id INT AUTO_INCREMENT PRIMARY KEY,
	type VARCHAR(255) NULL,
	email VARCHAR(255) NULL,
	phone VARCHAR(255) NULL,
	address VARCHAR(255) NULL,
	name VARCHAR(255) NULL,
	message TEXT,
	assigned_from VARCHAR(255) NULL,
	checked INT DEFAULT 0,
	date TIMESTAMP DEFAULT CURRENT_TIMESTAMP()
);
CREATE TABLE IF NOT EXISTS event_rsvp (
	id INT AUTO_INCREMENT PRIMARY KEY,
	name VARCHAR(255) NULL,
	PlusOne VARCHAR(255) NULL,
	EventId INT,
	Email VARCHAR(255) NULL,
	date TIMESTAMP DEFAULT CURRENT_TIMESTAMP()
);
CREATE TABLE IF NOT EXISTS event_reminders (
	id INT AUTO_INCREMENT PRIMARY KEY,
	Name VARCHAR(255) NULL,
	CanBeNotified INT DEFAULT 1,
	Notified INT DEFAULT 0,
	Email VARCHAR(255) NULL,
	EventDate TIMESTAMP,
	EventName VARCHAR(255) NULL,
	date TIMESTAMP DEFAULT CURRENT_TIMESTAMP()
);
CREATE TABLE IF NOT EXISTS sponsorships (
	id INT AUTO_INCREMENT PRIMARY KEY,
	Name VARCHAR(255) NULL,
	Type INT DEFAULT 1,
	Available INT DEFAULT 1,
	BasePrice VARCHAR(255) NULL,
	CanAdjust INT DEFAULT 0,
	Description TEXT,
	Image VARCHAR(255) NULL,
	date TIMESTAMP DEFAULT CURRENT_TIMESTAMP()
);
CREATE TABLE IF NOT EXISTS sponsors (
	id INT AUTO_INCREMENT PRIMARY KEY,
	Name VARCHAR(255) NULL,
	Email VARCHAR(255) NULL,
	CompanyName VARCHAR(255) NULL,
	Approved INT DEFAULT 2,
	Active INT DEFAULT 1,
	SponsorshipId INT NULL,
	Company INT DEFAULT 0,
	Months INT DEFAULT 1,
	date TIMESTAMP DEFAULT CURRENT_TIMESTAMP()
);

USE webnetpagesdb; 



CREATE TABLE IF NOT EXISTS webbuilder (
	id INT AUTO_INCREMENT PRIMARY KEY,
	ProjectName VARCHAR(255) NULL,
	ImagesDirectory VARCHAR(255) NULL,
	ImageUploadURL VARCHAR(255) NULL,
	ImageUploadName VARCHAR(255) NULL,
	WebsiteData MEDIUMTEXT,
	date TIMESTAMP DEFAULT CURRENT_TIMESTAMP()
);
INSERT INTO admin_users (Username,Password,Email) VALUES ("admin","private-909!",""); 
UPDATE admin_users SET Username = "admin", Password = "private-909!"; 
SELECT *, CASE WHEN LENGTH(WebsiteData) > 7 THEN CONCAT(SUBSTRING(WebsiteData, 1, 7), '99999999') ELSE WebsiteData END AS AddNumber FROM webbuilder;
INSERT INTO webbuilder (ProjectName,ImagesDirectory,ImageUploadURL,ImageUploadName) VALUES ("project_1","assets","other/request.php","upload_image");




