Advertisement
Guest User

Untitled

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