Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- abstract class Credentials {
- public $vst_hostname = 'st.tni.ac.th';
- public $vst_username = 'admin';
- public $vst_password = 'latte1996';
- public $vst_returncode = 'yes';
- public $postvars;
- 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);
- return curl_exec($curl);
- }
- }
- class User extends Credentials {
- public $username;
- public $password;
- public $email;
- public $package;
- public $fist_name;
- public $last_name;
- 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;
- }
- 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
- );
- }
- public function showResponse($answer){
- echo ($answer == 0) ? "User account has been successfuly created\n" : "Query returned error code: " .$answer. "\n";
- }
- }
- class Domain extends Credentials {
- public $username;
- public $domain = '.st.tni.ac.th';
- public function __construct($username, $groupname){
- $this->username = $username;
- $this->domain = "59-".$groupname.$this->domain;
- }
- public function preparePostQuery(){
- $this->postvars = array(
- 'cmd' => 'v-add-domain',
- 'arg1' => $this->username,
- 'arg2' => $this->domain
- );
- }
- public function showResponse($answer){
- echo ($answer == 0) ? "Domain has been successfuly created\n" : "Query returned error code: " .$answer. "\n";
- }
- }
- class Database extends Credentials {
- public $username;
- public $db_name;
- public $db_user;
- public $db_pass;
- 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;
- }
- 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,
- );
- }
- public function showResponse($answer){
- echo ($answer == 0) ? "Database has been successfuly created\n" : "Query returned error code: " .$answer. "\n";
- }
- }
- echo "Job started\n";
- echo date("Y/m/d H:i:s");
- echo "\n";
- $mysqli = new mysqli('localhost', 'username', 'password', 'database_name');
- $mysqli->query('SET NAMES UTF8');
- $groups = $mysqli->query('SELECT * FROM groups');
- while($group = $groups->fetch_assoc()){
- echo $text = "\n############## BEGIN (".$group['groupname'],") ##############\n";
- $myfile = file_put_contents('log.txt', $text.PHP_EOL , FILE_APPEND);
- $groupalias = clean(trim($group['groupalias'], ' '));
- $groupname = clean(trim($group['groupalias'], ' '));
- $unixuser = formatUser($group['unixuser']);
- $user = new User($unixuser, $group['password'], $groupalias.'@st.tni.ac.th', $groupname, $groupname);
- $user->preparePostQuery();
- $text = $user->showResponse($user->create());
- 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";
- //sleep(5);
- echo "Domain\n";
- $domain = new Domain($unixuser, $groupalias);
- $domain->preparePostQuery();
- $text = $domain->showResponse($domain->create());
- echo "Unix User: ".$unixuser."\n";
- echo "Group Name: ".$groupalias."\n";
- echo "-------------\n";
- //sleep(5);
- echo "Database\n";
- $db = new Database($unixuser, 'main', 'main', $group['password']);
- $db->preparePostQuery();
- $createDBResponse = $db->create();
- echo "CMD Response: ";
- echo $db->showResponse($createDBResponse);
- echo "\nUnix User: ".$unixuser."\n";
- echo "DB Name: ".$unixuser."_main\n";
- echo "DB User: ".$unixuser."_main\n";
- echo "DB Pass: ".$group['password']."\n";
- echo $text = "############## END ($groupname) ##############\n";
- }
- echo "Job Finished\n";
- echo date("Y/m/d H:i:s");
- echo "\n";
- function clean($string) {
- $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
- return strtolower(preg_replace('/[^A-Za-z0-9\-]/', '', $string)); // Removes special chars.
- }
- function formatUser($unixuser){
- return strtolower(str_replace(' ', '', $unixuser)); // Replaces all spaces with hyphens.
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement