Advertisement
Guest User

Untitled

a guest
Aug 28th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.72 KB | None | 0 0
  1. <?php
  2.  
  3. abstract class Credentials {
  4. public $vst_hostname = 'st.tni.ac.th';
  5. public $vst_username = 'admin';
  6. public $vst_password = 'latte1996';
  7. public $vst_returncode = 'yes';
  8. public $postvars;
  9.  
  10. public function create(){
  11. // Send POST query via cURL
  12. $this->postvars['user'] = $this->vst_username;
  13. $this->postvars['password'] = $this->vst_password;
  14. $this->postvars['returncode'] = $this->vst_returncode;
  15. $postdata = http_build_query($this->postvars);
  16. $curl = curl_init();
  17. curl_setopt($curl, CURLOPT_URL, 'https://' . $this->vst_hostname . ':8083/api/');
  18. curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
  19. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  20. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  21. curl_setopt($curl, CURLOPT_POST, true);
  22. curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
  23.  
  24. return curl_exec($curl);
  25. }
  26. }
  27.  
  28. class User extends Credentials {
  29. public $username;
  30. public $password;
  31. public $email;
  32. public $package;
  33. public $fist_name;
  34. public $last_name;
  35.  
  36. public function __construct($username, $password, $email, $firstname, $lastname){
  37. $this->username = $username;
  38. $this->password = $password;
  39. $this->email = $email;
  40. $this->package = 'default';
  41. $this->first_name = $firstname;
  42. $this->last_name = $lastname;
  43. }
  44.  
  45. public function preparePostQuery(){
  46. $this->postvars = array(
  47. 'cmd' => 'v-add-user',
  48. 'arg1' => $this->username,
  49. 'arg2' => $this->password,
  50. 'arg3' => $this->email,
  51. 'arg4' => $this->package,
  52. 'arg5' => $this->fist_name,
  53. 'arg6' => $this->last_name
  54. );
  55. }
  56.  
  57. public function showResponse($answer){
  58. echo ($answer == 0) ? "User account has been successfuly created\n" : "Query returned error code: " .$answer. "\n";
  59. }
  60. }
  61.  
  62. class Domain extends Credentials {
  63. public $username;
  64. public $domain = '.st.tni.ac.th';
  65.  
  66. public function __construct($username, $groupname){
  67. $this->username = $username;
  68. $this->domain = "59-".$groupname.$this->domain;
  69. }
  70.  
  71. public function preparePostQuery(){
  72. $this->postvars = array(
  73. 'cmd' => 'v-add-domain',
  74. 'arg1' => $this->username,
  75. 'arg2' => $this->domain
  76. );
  77. }
  78.  
  79. public function showResponse($answer){
  80. echo ($answer == 0) ? "Domain has been successfuly created\n" : "Query returned error code: " .$answer. "\n";
  81. }
  82. }
  83.  
  84. class Database extends Credentials {
  85. public $username;
  86. public $db_name;
  87. public $db_user;
  88. public $db_pass;
  89.  
  90. public function __construct($username, $db_name, $db_user, $db_pass){
  91. $this->username = $username;
  92. $this->db_name = $db_name;
  93. $this->db_user = $db_user;
  94. $this->db_pass = $db_pass;
  95. }
  96.  
  97. public function preparePostQuery(){
  98. $this->postvars = array(
  99. 'cmd' => 'v-add-database',
  100. 'arg1' => $this->username,
  101. 'arg2' => $this->db_name,
  102. 'arg3' => $this->db_user,
  103. 'arg4' => $this->db_pass,
  104. );
  105. }
  106.  
  107. public function showResponse($answer){
  108. echo ($answer == 0) ? "Database has been successfuly created\n" : "Query returned error code: " .$answer. "\n";
  109. }
  110. }
  111.  
  112. echo "Job started\n";
  113. echo date("Y/m/d H:i:s");
  114. echo "\n";
  115.  
  116. $mysqli = new mysqli('localhost', 'username', 'password', 'database_name');
  117. $mysqli->query('SET NAMES UTF8');
  118.  
  119. $groups = $mysqli->query('SELECT * FROM groups');
  120.  
  121. while($group = $groups->fetch_assoc()){
  122.  
  123. echo $text = "\n############## BEGIN (".$group['groupname'],") ##############\n";
  124. $myfile = file_put_contents('log.txt', $text.PHP_EOL , FILE_APPEND);
  125.  
  126. $groupalias = clean(trim($group['groupalias'], ' '));
  127. $groupname = clean(trim($group['groupalias'], ' '));
  128. $unixuser = formatUser($group['unixuser']);
  129.  
  130. $user = new User($unixuser, $group['password'], $groupalias.'@st.tni.ac.th', $groupname, $groupname);
  131. $user->preparePostQuery();
  132. $text = $user->showResponse($user->create());
  133.  
  134. echo "Unix User: ".$unixuser."\n";
  135. echo "Password: ".$group['password']."\n";
  136. echo "E-Mail: ".$groupalias.'@st.tni.ac.th'."\n";
  137. echo "Firstname: ".$groupname."\n";
  138. echo "Lastname: ".$groupname."\n";
  139. echo "-------------\n";
  140.  
  141. //sleep(5);
  142.  
  143. echo "Domain\n";
  144. $domain = new Domain($unixuser, $groupalias);
  145. $domain->preparePostQuery();
  146. $text = $domain->showResponse($domain->create());
  147. echo "Unix User: ".$unixuser."\n";
  148. echo "Group Name: ".$groupalias."\n";
  149. echo "-------------\n";
  150.  
  151. //sleep(5);
  152.  
  153. echo "Database\n";
  154. $db = new Database($unixuser, 'main', 'main', $group['password']);
  155. $db->preparePostQuery();
  156. $createDBResponse = $db->create();
  157. echo "CMD Response: ";
  158. echo $db->showResponse($createDBResponse);
  159. echo "\nUnix User: ".$unixuser."\n";
  160. echo "DB Name: ".$unixuser."_main\n";
  161. echo "DB User: ".$unixuser."_main\n";
  162. echo "DB Pass: ".$group['password']."\n";
  163.  
  164. echo $text = "############## END ($groupname) ##############\n";
  165. }
  166.  
  167. echo "Job Finished\n";
  168. echo date("Y/m/d H:i:s");
  169. echo "\n";
  170.  
  171. function clean($string) {
  172. $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
  173. return strtolower(preg_replace('/[^A-Za-z0-9\-]/', '', $string)); // Removes special chars.
  174. }
  175.  
  176. function formatUser($unixuser){
  177. return strtolower(str_replace(' ', '', $unixuser)); // Replaces all spaces with hyphens.
  178. }
  179.  
  180. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement