Advertisement
Guest User

Untitled

a guest
Aug 28th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.56 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Title: Automation script for allocate web hosting users for INT-105 group project.
  5. * Author: Natthasak Vechprasit
  6. * Thanks: VestaCP API Documentation, StackOverflow
  7. * WARNING: PLEASE RUN THIS SCRIPT ON 'CLI' ONLY. OUTPUT WILL NOT BE SHOW IF RUNNING THROUGH WEB SERVER AND MAY HAVE ERRORS OCCUR.
  8. */
  9.  
  10. abstract class Credentials {
  11.  
  12. /**
  13. * Server Credentials
  14. */
  15. private $vst_hostname = 'st.tni.ac.th';
  16. private $vst_username = 'admin';
  17. private $vst_password = 'latte1996';
  18. private $vst_returncode = 'yes';
  19. private $postvars;
  20.  
  21. /**
  22. * Response Data
  23. */
  24. protected $what;
  25. protected $response;
  26.  
  27. /**
  28. * Create...
  29. */
  30. public function create(){
  31. // Send POST query via cURL
  32. $this->postvars['user'] = $this->vst_username;
  33. $this->postvars['password'] = $this->vst_password;
  34. $this->postvars['returncode'] = $this->vst_returncode;
  35. $postdata = http_build_query($this->postvars);
  36. $curl = curl_init();
  37. curl_setopt($curl, CURLOPT_URL, 'https://' . $this->vst_hostname . ':8083/api/');
  38. curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
  39. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  40. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  41. curl_setopt($curl, CURLOPT_POST, true);
  42. curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
  43.  
  44. $this->response = curl_exec($curl);
  45. }
  46.  
  47. public function getResponse(){
  48. echo ($this->response == 0) ? $this->what." has been successfuly created\n" : "Query returned error code: " .$answer. "\n";
  49. }
  50. }
  51.  
  52. class User extends Credentials {
  53. /**
  54. * Attributes for user creation
  55. */
  56. private $username;
  57. private $password;
  58. private $email;
  59. private $package;
  60. private $fist_name;
  61. private $last_name;
  62.  
  63. /**
  64. * Construct a user information
  65. */
  66. public function __construct($username, $password, $email, $firstname, $lastname){
  67. $this->username = $username;
  68. $this->password = $password;
  69. $this->email = $email;
  70. $this->package = 'default';
  71. $this->first_name = $firstname;
  72. $this->last_name = $lastname;
  73. $this->what = 'User account';
  74. }
  75.  
  76. /**
  77. * Prepare parameters for creation
  78. */
  79. public function preparePostQuery(){
  80. $this->postvars = array(
  81. 'cmd' => 'v-add-user',
  82. 'arg1' => $this->username,
  83. 'arg2' => $this->password,
  84. 'arg3' => $this->email,
  85. 'arg4' => $this->package,
  86. 'arg5' => $this->fist_name,
  87. 'arg6' => $this->last_name
  88. );
  89. }
  90. }
  91.  
  92. class Domain extends Credentials {
  93. /**
  94. * Attributes for domain creation
  95. */
  96. public $username;
  97. public $domain = '.st.tni.ac.th';
  98.  
  99. public function __construct($username, $groupname){
  100. $this->username = $username;
  101. $this->domain = "59-".$groupname.$this->domain;
  102. $this->what = 'Domain name';
  103. }
  104.  
  105. /**
  106. * Prepare parameters for creation
  107. */
  108. public function preparePostQuery(){
  109. $this->postvars = array(
  110. 'cmd' => 'v-add-domain',
  111. 'arg1' => $this->username,
  112. 'arg2' => $this->domain
  113. );
  114. }
  115. }
  116.  
  117. class Database extends Credentials {
  118. /**
  119. * Attributes for database creation
  120. */
  121. public $username;
  122. public $db_name;
  123. public $db_user;
  124. public $db_pass;
  125.  
  126. /**
  127. * Prepare parameters for creation
  128. */
  129. public function __construct($username, $db_name, $db_user, $db_pass){
  130. $this->username = $username;
  131. $this->db_name = $db_name;
  132. $this->db_user = $db_user;
  133. $this->db_pass = $db_pass;
  134. $this->what = 'Database';
  135. }
  136.  
  137. /**
  138. * Prepare parameters for creation
  139. */
  140. public function preparePostQuery(){
  141. $this->postvars = array(
  142. 'cmd' => 'v-add-database',
  143. 'arg1' => $this->username,
  144. 'arg2' => $this->db_name,
  145. 'arg3' => $this->db_user,
  146. 'arg4' => $this->db_pass,
  147. );
  148. }
  149. }
  150.  
  151. // Main executable section begins here
  152. echo "\nJob started\n";
  153. echo date("Y/m/d H:i:s");
  154. echo "\n";
  155.  
  156. /**
  157. * Instantiate a connection with database, Using MySQLi object for handle.
  158. */
  159. $mysqli = new mysqli('localhost', 'username', 'password', 'database_name');
  160. $mysqli->query('SET NAMES UTF8');
  161.  
  162. /**
  163. * Get the information of all groups
  164. */
  165. $groups = $mysqli->query('SELECT * FROM groups');
  166.  
  167. /**
  168. * Let's get into the loop, Create user for each fetched row
  169. */
  170. while($group = $groups->fetch_assoc()){
  171.  
  172. echo "\n############## BEGIN (".$group['groupname'],") ##############\n";
  173.  
  174. /**
  175. * Convert the variables into the correct format
  176. */
  177. $groupalias = clean(trim($group['groupalias'], ' '));
  178. $groupname = clean(trim($group['groupalias'], ' '));
  179. $unixuser = formatUser($group['unixuser']);
  180.  
  181. /**
  182. * Create User
  183. * Get an information from database
  184. *
  185. * Expected Parameters
  186. * $unixuser - a username, 8 length character
  187. * $password - Don't you understand what the password is? Ok, It's just a random password.
  188. * $groupalias - Full name of group in lowercase alphabet format
  189. * $groupname - Full name of the group as firstname and lastname for the system
  190. */
  191. $user = new User($unixuser, $group['password'], $groupalias.'@st.tni.ac.th', $groupname, $groupname);
  192. $user->preparePostQuery();
  193. $user->create();
  194.  
  195. /**
  196. * Display Parameters and Response
  197. */
  198. echo "Unix User: ".$unixuser."\n";
  199. echo "Password: ".$group['password']."\n";
  200. echo "E-Mail: ".$groupalias.'@st.tni.ac.th'."\n";
  201. echo "Firstname: ".$groupname."\n";
  202. echo "Lastname: ".$groupname."\n";
  203. echo "-------------\n";
  204. $user->showResponse();
  205.  
  206. /**
  207. * Adding a domain name to recently created user.
  208. *
  209. * Expected Parameters
  210. * $unixuser
  211. * $groupalias
  212. */
  213. echo "Domain\n";
  214. $domain = new Domain($unixuser, $groupalias);
  215. $domain->preparePostQuery();
  216. $domain->create();
  217.  
  218. /**
  219. * Display Parameters and Response
  220. */
  221. echo "Unix User: ".$unixuser."\n";
  222. echo "Group Name: ".$groupalias."\n";
  223. echo "-------------\n";
  224. $domain->showResponse();
  225.  
  226. /**
  227. * Create a database for recently created user.
  228. */
  229. echo "Database\n";
  230. $db = new Database($unixuser, 'main', 'main', $group['password']);
  231. $db->preparePostQuery();
  232. $db->create();
  233.  
  234. /**
  235. * Display Parameters and Response
  236. */
  237. echo "Unix User: ".$unixuser."\n";
  238. echo "DB Name: ".$unixuser."_main\n";
  239. echo "DB User: ".$unixuser."_main\n";
  240. echo "DB Pass: ".$group['password']."\n";
  241. $db->showResponse();
  242.  
  243.  
  244. echo "############## END ($groupname) ##############\n";
  245. }
  246.  
  247. echo "\nJob Finished\n";
  248. echo date("Y/m/d H:i:s");
  249. echo "\n";
  250. // End main executation section
  251.  
  252. /**
  253. * Convert String to the valid "domain name" format
  254. */
  255. function clean($string) {
  256. $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
  257. return strtolower(preg_replace('/[^A-Za-z0-9\-]/', '', $string)); // Removes special chars.
  258. }
  259.  
  260. /**
  261. * Convert String to the valid "unixuser" format
  262. */
  263. function formatUser($unixuser){
  264. return strtolower(str_replace(' ', '', $unixuser)); // Replaces all spaces with hyphens.
  265. }
  266.  
  267. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement