Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Title: Automation script for allocate web hosting users for INT-105 group project.
- * Author: Natthasak Vechprasit
- * Thanks: VestaCP API Documentation, StackOverflow, Ps.Suphanat (Collaborator)
- * WARNING: PLEASE RUN THIS SCRIPT ON 'CLI' ONLY. OUTPUT WILL NOT BE SHOW IF RUNNING THROUGH WEB SERVER AND MAY HAVE ERRORS OCCUR.
- * Requirements: MySQL Database with a table that match with the structure below
- * groups (groupname, groupalias, unixuser, password)
- * Coding Style: Object-Oriented Programming (OOP)
- */
- abstract class Credentials {
- /**
- * Server Credentials
- */
- private $vst_hostname = 'st.tni.ac.th';
- private $vst_username = 'username';
- private $vst_password = 'password';
- private $vst_returncode = 'yes';
- private $postvars;
- /**
- * Response Data
- */
- protected $what;
- protected $response;
- /**
- * Let's Create...
- */
- public function create(){
- // Send POST query via cURL
- $this->postvars['user'] = $this->vst_username;
- $this->postvars['password'] = $this->vst_password;
- $this->postvars['returncode'] = $this->vst_returncode;
- $postdata = http_build_query($this->postvars);
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, 'https://' . $this->vst_hostname . ':8083/api/');
- curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
- curl_setopt($curl, CURLOPT_POST, true);
- curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
- $this->response = curl_exec($curl);
- }
- public function getResponse(){
- echo ($this->response == 0) ? $this->what." has been successfuly created\n" : "Query returned error code: " .$this->what. "\n";
- }
- }
- class User extends Credentials {
- /**
- * Attributes for user creation
- */
- private $username;
- private $password;
- private $email;
- private $package;
- private $fist_name;
- private $last_name;
- /**
- * Construct a user information
- */
- public function __construct($username, $password, $email, $firstname, $lastname){
- $this->username = $username;
- $this->password = $password;
- $this->email = $email;
- $this->package = 'default';
- $this->first_name = $firstname;
- $this->last_name = $lastname;
- $this->what = 'User account';
- }
- /**
- * Prepare parameters for creation
- */
- public function preparePostQuery(){
- $this->postvars = array(
- 'cmd' => 'v-add-user',
- 'arg1' => $this->username,
- 'arg2' => $this->password,
- 'arg3' => $this->email,
- 'arg4' => $this->package,
- 'arg5' => $this->fist_name,
- 'arg6' => $this->last_name
- );
- }
- }
- class Domain extends Credentials {
- /**
- * Attributes for domain creation
- */
- public $username;
- public $domain = '.st.tni.ac.th';
- public function __construct($username, $groupname){
- $this->username = $username;
- $this->domain = "59-".$groupname.$this->domain;
- $this->what = 'Domain name';
- }
- /**
- * Prepare parameters for creation
- */
- public function preparePostQuery(){
- $this->postvars = array(
- 'cmd' => 'v-add-domain',
- 'arg1' => $this->username,
- 'arg2' => $this->domain
- );
- }
- }
- class Database extends Credentials {
- /**
- * Attributes for database creation
- */
- public $username;
- public $db_name;
- public $db_user;
- public $db_pass;
- /**
- * Prepare parameters for creation
- */
- public function __construct($username, $db_name, $db_user, $db_pass){
- $this->username = $username;
- $this->db_name = $db_name;
- $this->db_user = $db_user;
- $this->db_pass = $db_pass;
- $this->what = 'Database';
- }
- /**
- * Prepare parameters for creation
- */
- public function preparePostQuery(){
- $this->postvars = array(
- 'cmd' => 'v-add-database',
- 'arg1' => $this->username,
- 'arg2' => $this->db_name,
- 'arg3' => $this->db_user,
- 'arg4' => $this->db_pass,
- );
- }
- }
- // Main executable section begins here
- echo "\nJob started\n";
- echo date("Y/m/d H:i:s");
- echo "\n";
- /**
- * Instantiate a connection with database, Using MySQLi object for handle.
- */
- $mysqli = new mysqli('localhost', 'username', 'password', 'database_name');
- $mysqli->query('SET NAMES UTF8');
- /**
- * Get the information of all groups
- */
- $groups = $mysqli->query('SELECT * FROM groups');
- /**
- * Let's get into the loop, Create user for each fetched row
- */
- while($group = $groups->fetch_assoc()){
- echo "\n############## BEGIN (".$group['groupname'],") ##############\n";
- /**
- * Convert the variables into the correct format
- */
- $groupalias = clean(trim($group['groupalias'], ' '));
- $groupname = clean(trim($group['groupalias'], ' '));
- $unixuser = formatUser($group['unixuser']);
- /**
- * Create User
- * Get an information from database
- *
- * Expected Parameters
- * $unixuser - a username, 8 length character
- * $password - Don't you understand what the password is? Ok, It's just a random password.
- * $groupalias - Full name of group in lowercase alphabet format
- * $groupname - Full name of the group as firstname and lastname for the system
- */
- $user = new User($unixuser, $group['password'], $groupalias.'@st.tni.ac.th', $groupname, $groupname);
- $user->preparePostQuery();
- $user->create();
- /**
- * Display Parameters and Response
- */
- echo "Unix User: ".$unixuser."\n";
- echo "Password: ".$group['password']."\n";
- echo "E-Mail: ".$groupalias.'@st.tni.ac.th'."\n";
- echo "Firstname: ".$groupname."\n";
- echo "Lastname: ".$groupname."\n";
- echo "-------------\n";
- $user->showResponse();
- /**
- * Adding a domain name to recently created user.
- *
- * Expected Parameters
- * $unixuser
- * $groupalias
- */
- echo "Domain\n";
- $domain = new Domain($unixuser, $groupalias);
- $domain->preparePostQuery();
- $domain->create();
- /**
- * Display Parameters and Response
- */
- echo "Unix User: ".$unixuser."\n";
- echo "Group Name: ".$groupalias."\n";
- echo "-------------\n";
- $domain->showResponse();
- /**
- * Create a database for recently created user.
- */
- echo "Database\n";
- $db = new Database($unixuser, 'main', 'main', $group['password']);
- $db->preparePostQuery();
- $db->create();
- /**
- * Display Parameters and Response
- */
- echo "Unix User: ".$unixuser."\n";
- echo "DB Name: ".$unixuser."_main\n";
- echo "DB User: ".$unixuser."_main\n";
- echo "DB Pass: ".$group['password']."\n";
- $db->showResponse();
- echo "############## END ($groupname) ##############\n";
- }
- echo "\nJob Finished\n";
- echo date("Y/m/d H:i:s");
- echo "\n";
- // End main executation section
- /**
- * Convert String to the valid "domain name" format
- */
- function clean($string) {
- $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
- return strtolower(preg_replace('/[^A-Za-z0-9\-]/', '', $string)); // Removes special chars.
- }
- /**
- * Convert String to the valid "unixuser" format
- */
- function formatUser($unixuser){
- return strtolower(str_replace(' ', '', $unixuser)); // Replaces all spaces with hyphens.
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement