Advertisement
Guest User

Untitled

a guest
Sep 28th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.93 KB | None | 0 0
  1. <?
  2.  
  3. /*
  4. ==========================================================================================
  5. SETTINGS
  6. ==========================================================================================
  7. */
  8.  
  9. ini_set('memory_limit', '2048M');
  10. set_time_limit ( 0 );
  11. $config_file = 'config.php';
  12. $update_dir = './updates'; // the number will automatically be added from your config. Like: ./updates434
  13.  
  14. /*
  15. ==========================================================================================
  16. DO NOT CHANGE ANYTHING BELOW THIS LINE
  17. ==========================================================================================
  18. */
  19.  
  20. $db = false;
  21. $last_applied = array();
  22. $total_new = 0;
  23.  
  24. // Load config
  25. if(file_exists($config_file)){
  26.  
  27. require_once($config_file);
  28. if(!isset($dbs)){ die('Config file does not contain server information! ($dbs variable)'); }
  29.  
  30. forEach($dbs as $version=>$details){
  31.  
  32. // Connect to this specific server
  33. $db_host = $details[0];
  34. $db_user = $details[1];
  35. $db_pass = $details[2];
  36. $db_name = $details[3];
  37. $db = new Database($db_host, $db_user, $db_pass, $db_name);
  38.  
  39. // Load any previously applied updates
  40. $already_applied__updates = array();
  41. $tmp = $db->query("SELECT * FROM `updates`")->resultSet();
  42. if($db->rowCount()>0){
  43. foreach($tmp as $previous_update){
  44. $already_applied__updates[$version][] = strtolower(trim($previous_update['name']));
  45. }
  46. }
  47.  
  48. // Scan update files in the right folder
  49. $update_dir_for_version = $update_dir.$version;
  50. if(file_exists($update_dir_for_version)){
  51. $update_files = preg_grep('~\.(sql|txt)$~', scandir($update_dir_for_version));
  52. // Sort files naturally and case insensitive by filename
  53. natcasesort($update_files);
  54. }else{
  55. die('Update folder not found! ('.$update_dir_for_version.')');
  56. }
  57.  
  58. // Apply update files to this server.
  59. $new_updates = 0;
  60. forEach($update_files as $update_file){
  61.  
  62. // Already applied?
  63. $update_filename = strtolower(trim($update_file));
  64. if(!in_array($update_filename,$already_applied__updates[$version])){
  65.  
  66. // New update. Let's apply
  67. $total_new++;
  68. $new_updates++;
  69. $sql = file_get_contents( $update_dir_for_version.'/'.$update_file );
  70. $r = $db->query($sql)->execute();
  71. if($r){
  72. $db->query("INSERT INTO updates (`name`,`hash`,`state`,`timestamp`,`speed`)VALUES(:name,:hash,:state,NOW(),:speed)");
  73. $db->bind(":name", $update_file); // file
  74. $db->bind(":hash", strtoupper(sha1($update_file))); // hash
  75. $db->bind(":state", 'RELEASED'); // state
  76. $db->bind(":speed", 0); // query speed in milliseconds. set to zero, we don't measure.
  77. $db->execute();
  78. }else{
  79. echo 'Update "'.$update_file.'" in version '.version($version).' failed. Please fix the file and refresh the page..<br>';
  80. exit();
  81. }
  82.  
  83. }else{
  84. // Already applied
  85. }
  86. }
  87. echo 'Version '.version($version).' already has '.count($already_applied__updates[$version]).' update(s) applied to it..<br>';
  88. echo 'Just now applied '.$new_updates.' new update(s) to version '.version($version).'..<br>';
  89. echo '<hr>';
  90.  
  91. // Grab latest update applied for this version
  92. $tmp = $db->query("SELECT `name` FROM `updates` ORDER BY `timestamp` DESC LIMIT 1")->single();
  93. $last_applied[$version] = $tmp['name'];
  94. }
  95.  
  96. // If any new updates were applied, remember them.
  97. if($total_new<1){
  98. echo "You are already up to date. Nothing to apply..<br>";
  99. }
  100.  
  101. }else{
  102.  
  103. die('Config file not found!');
  104.  
  105. }
  106.  
  107. /*
  108. ==========================================================================================
  109. ALL DONE
  110. ==========================================================================================
  111. */
  112.  
  113. forEach($last_applied as $version=>$filename){
  114. echo "Last update applied to version ".version($version).": ".$filename.'<br>';
  115. }
  116. echo "<strong>All done!</strong>";
  117.  
  118. /*
  119. ==========================================================================================
  120. HELPERS
  121. ==========================================================================================
  122. */
  123.  
  124. function version($without_dots){
  125. return implode('.',str_split($without_dots));
  126. }
  127.  
  128. /*
  129. ==========================================================================================
  130. SMALL PDO DATABASE CLASS
  131. ==========================================================================================
  132. */
  133.  
  134. class Database {
  135.  
  136. private $dbh;
  137. private $stmt;
  138.  
  139. public function __construct($host, $user, $pass, $name) {
  140. // Set DSN
  141. $dsn = 'mysql:host=' . $host . ';dbname=' . $name . ';charset=utf8';
  142.  
  143. // Set options
  144. $options = array(
  145. PDO::ATTR_PERSISTENT => true,
  146. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
  147. );
  148.  
  149. // Attempt new instance
  150. try {
  151. $this->dbh = new PDO($dsn, $user, $pass, $options);
  152. } catch (PDOException $e) {
  153. $this->error = $e->getMessage();
  154. die('A database connection could not be established: '.$this->error);
  155. }
  156. }
  157.  
  158. public function query($query) {
  159. $this->stmt = $this->dbh->prepare($query);
  160. return $this;
  161. }
  162.  
  163. public function bind($pos, $value, $type = null) {
  164.  
  165. if( is_null($type) ) {
  166. switch( true ) {
  167. case is_int($value):
  168. $type = PDO::PARAM_INT;
  169. break;
  170. case is_bool($value):
  171. $type = PDO::PARAM_BOOL;
  172. break;
  173. case is_null($value):
  174. $type = PDO::PARAM_NULL;
  175. break;
  176. default:
  177. $type = PDO::PARAM_STR;
  178. }
  179. }
  180.  
  181. $this->stmt->bindValue($pos, $value, $type);
  182. return $this;
  183. }
  184.  
  185.  
  186. public function execute() {
  187. return $this->stmt->execute();
  188. }
  189.  
  190. public function rowCount(){
  191. return $this->stmt->rowCount();
  192. }
  193.  
  194. public function lastInsertId(){
  195. return $this->dbh->lastInsertId();
  196. }
  197.  
  198. public function resultSet() {
  199. $this->execute();
  200. return $this->stmt->fetchAll();
  201. }
  202.  
  203. public function single() {
  204. $this->execute();
  205. return $this->stmt->fetch();
  206. }
  207. }
  208. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement