Advertisement
Guest User

Untitled

a guest
Aug 28th, 2015
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.18 KB | None | 0 0
  1. <?php
  2.  
  3. // Set these dependant on your BB credentials
  4. $username = '';
  5. $password = '';
  6.  
  7. // your Bitbucket repo name
  8. $reponame = "";
  9.  
  10. // extract to
  11. $dest = "./"; // leave ./ for relative destination
  12.  
  13.  
  14. //Exclusion list
  15. $exc = array("deploy.php", ".htaccess");
  16.  
  17. // Grab the data from BB's POST service and decode
  18. $json = stripslashes($_POST['payload']);
  19. $data = json_decode($json);
  20.  
  21. // set higher script timeout (for large repo's or slow servers)
  22. set_time_limit(5000);
  23.  
  24. // Set some parameters to fetch the correct files
  25. $uri = $data->repository->absolute_url;
  26. $node = $data->commits[0]->node;
  27. $files = $data->commits[0]->files;
  28.  
  29. //Clear Root
  30. rmdir_recursively(".");
  31.  
  32. // download the repo zip file
  33. $fp = fopen("tip.zip", 'w');
  34.  
  35. $ch = curl_init("https://bitbucket.org/$username/$reponame/get/$node.zip");
  36. curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
  37. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  38. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  39. curl_setopt($ch, CURLOPT_FILE, $fp);
  40.  
  41. $data = curl_exec($ch);
  42.  
  43. curl_close($ch);
  44. fclose($fp);
  45.  
  46. // unzip
  47. $zip = new ZipArchive;
  48. $res = $zip->open('tip.zip');
  49. if ($res === TRUE) {
  50. $zip->extractTo('./');
  51. $zip->close();
  52. } else {
  53. die('ZIP not supported on this server!');
  54. }
  55.  
  56. // function to delete all files in a directory recursively
  57. function rmdir_recursively($dir) {
  58. global $exc;
  59. if(in_array($dir,$exc)) return false;
  60. if (!is_dir($dir) || is_link($dir)) return unlink($dir);
  61. foreach (scandir($dir) as $file) {
  62. if ($file == '.' || $file == '..') continue;
  63. if (!rmdir_recursively($dir . DIRECTORY_SEPARATOR . $file)) {
  64. chmod($dir . DIRECTORY_SEPARATOR . $file, 0777);
  65. if (!rmdir_recursively($dir . DIRECTORY_SEPARATOR . $file)) return false;
  66. };
  67. }
  68. return rmdir($dir);
  69. }
  70.  
  71. // function to recursively copy the files
  72. function copy_recursively($src, $dest) {
  73. if (is_dir($src)) {
  74. if ($dest != "./")
  75. rmdir_recursively($dest);
  76. @mkdir($dest);
  77. $files = scandir($src);
  78. foreach ($files as $file)
  79. if ($file != "." && $file != "..")
  80. copy_recursively("$src/$file", "$dest/$file");
  81. }
  82. else if (file_exists($src))
  83. copy($src, $dest);
  84. rmdir_recursively($src);
  85. }
  86.  
  87. // start copying the files from extracted repo and delete the old directory recursively
  88. copy_recursively("$username-$reponame-$node", $dest);
  89.  
  90. // delete the repo zip file
  91. unlink("tip.zip");
  92. ?>
  93.  
  94. <?php
  95.  
  96. // set higher script timeout (for large repo's or slow servers)
  97. $timeLimit = 5000;
  98.  
  99. ///////////////////////////////////////////////////////////////////////////////////////
  100. $mode = intval(isset($_POST['payload']));
  101.  
  102. if (isset($_GET['commit']))
  103. $mode = 2;
  104.  
  105. $force = isset($_GET['force']);
  106. $owner = (isset($owner)) ? $owner : $username; // if user is owner
  107. $repo = $reponame;
  108. $response = "";
  109.  
  110. if ($mode == 0) { // manual deploy
  111.  
  112. function callback($url, $chunk) {
  113. global $response;
  114. $response .= $chunk;
  115. return strlen($chunk);
  116. }
  117.  
  118. ;
  119.  
  120. $ch = curl_init("https://api.bitbucket.org/1.0/repositories/$owner/$repo/changesets?limit=1");
  121.  
  122. curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
  123.  
  124. curl_setopt($ch, CURLOPT_HEADER, 0);
  125. curl_setopt($ch, CURLOPT_HTTPHEADER, array('User-Agent:Mozilla/5.0'));
  126. curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'callback');
  127. curl_exec($ch);
  128. curl_close($ch);
  129.  
  130. $changesets = json_decode($response, true);
  131. $node = $changesets['changesets'][0]['node'];
  132. $raw_node = $changesets['changesets'][0]['raw_node'];
  133. } else if ($mode == 1) { // auto deploy
  134. $json = stripslashes($_POST['payload']);
  135. $data = json_decode($json);
  136. // Set some parameters to fetch the correct files
  137. $uri = $data->repository->absolute_url;
  138. $node = $data->commits[0]->node;
  139. echo $node;
  140. $files = $data->commits[0]->files;
  141. } else if ($mode == 2) { // deploy with hash code
  142. $node = $_GET['commit'];
  143. $node = substr($node, 0, 12);
  144. echo 'commit: ' . $node . "n";
  145. }
  146. // Check last commit hash
  147.  
  148. if (isset($_GET['updated'])) {
  149. echo "n<br>Bitbucket Deploy Updated<br>n";
  150. }
  151.  
  152. set_time_limit($timeLimit);
  153.  
  154. // Grab the data from BB's POST service and decode
  155. // Clear Root
  156. // download the repo zip file
  157.  
  158. if (!$force && file_exists('lastcommit.hash')) {
  159. $lastcommit = file_get_contents('lastcommit.hash');
  160. if ($lastcommit == $node)
  161. die('Project is already up to date');
  162. }
  163.  
  164. file_put_contents('lastcommit.hash', $node);
  165.  
  166.  
  167. $fp = fopen("tip.zip", 'w');
  168.  
  169. $ch = curl_init("https://bitbucket.org/$owner/$reponame/get/$node.zip");
  170. curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
  171. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  172. //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  173. curl_setopt($ch, CURLOPT_FILE, $fp);
  174.  
  175. $data = curl_exec($ch);
  176.  
  177. curl_close($ch);
  178. fclose($fp);
  179.  
  180. $exc["files"][] = realpath("tip.zip");
  181.  
  182. $tipsize = filesize("tip.zip");
  183.  
  184. if ($tipsize < 50) {
  185. die("Commit not found");
  186. }
  187.  
  188. if ($autoUpdate)
  189. updateDeploy();
  190.  
  191. //var_dump($exc);
  192. //die();
  193.  
  194.  
  195. RemoveDir(realpath($dest), true, $exc);
  196.  
  197.  
  198. // unzip
  199. $zip = new ZipArchive;
  200. $res = $zip->open('tip.zip');
  201. if ($res !== TRUE) {
  202. die('ZIP not supported on this server!');
  203. }
  204.  
  205. $zip->extractTo("$dest/");
  206. $zip->close();
  207.  
  208. copy_recursively("$owner-$reponame-$node", $dest);
  209.  
  210. RemoveDir(realpath("$owner-$reponame-$node"), false);
  211. @rmdir("$owner-$reponame-$node");
  212. // Delete the repo zip file
  213. unlink("tip.zip");
  214.  
  215. // function to delete all files in a directory recursively
  216.  
  217. function updateDeploy() {
  218. global $force;
  219. global $dest;
  220. global $mode;
  221. $updated = isset($_GET['updated']);
  222. //var_dump($_GET);
  223. if ($updated)
  224. return true;
  225.  
  226. $response = "";
  227.  
  228. $response = file_get_contents("https://api.bitbucket.org/1.0/repositories/codearts/bitbucket-deploy/changesets?limit=1");
  229.  
  230. $changesets = json_decode($response, true);
  231. $node = $changesets['changesets'][0]['node'];
  232. $raw_node = $changesets['changesets'][0]['raw_node'];
  233.  
  234. $lastcommit = file_get_contents('data.hash');
  235. if (file_exists('data.hash')) { // if (!$force && file_exists('data.hash')) {
  236. $lastcommit = file_get_contents('data.hash');
  237. if ($lastcommit == $node)
  238. return;
  239. }
  240. file_put_contents('data.hash', $node);
  241. $deployLink = "https://bitbucket.org/codearts/bitbucket-deploy/get/$node.zip";
  242. $deploy = file_get_contents($deployLink);
  243.  
  244. $f = fopen("deploy.zip", "w");
  245. fwrite($f, $deploy);
  246. fclose($f);
  247. $zip = new ZipArchive;
  248. $res = $zip->open('deploy.zip');
  249. if ($res !== TRUE) {
  250. die('ZIP not supported on this server!');
  251. }
  252. $zip->extractTo("$dest/");
  253. $zip->close();
  254. unlink('deploy.php');
  255. copy("codearts-bitbucket-deploy-$node/deploy.php", 'deploy.php');
  256. //unlink("codearts-bitbucket-deploy-$node/deploy.php");
  257. RemoveDir(realpath("codearts-bitbucket-deploy-$node"), false);
  258. @rmdir(realpath("codearts-bitbucket-deploy-$node"));
  259.  
  260. $url = "http://" . $_SERVER['HTTP_HOST'] . "/deploy.php?updated" . (($force) ? '&force' : '');
  261.  
  262. header("Location:" . $url);
  263. die();
  264.  
  265. // if($mode != 1) echo "n<br>Bitbucket Deploy Updated<br>n";
  266. }
  267.  
  268. // Deleting with exclude list
  269.  
  270.  
  271. function checkExcluding($path, $excludinglist) {
  272. if (!isset($excludinglist["files"]))
  273. return false;
  274. if (!is_dir($path)) {
  275. return in_array($path, $excludinglist["files"]);
  276. }
  277. else
  278. return in_array($path, $excludinglist["dirs"]);
  279. }
  280.  
  281. function RemoveDir($dir, $exclude = false, $excludelist = array()) {
  282. $it = new RecursiveDirectoryIterator($dir);
  283. $files = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::CHILD_FIRST);
  284. // var_dump($files);
  285. foreach ($files as $file) {
  286. if ($exclude && checkExcluding($file->getRealPath(), $excludelist)) {
  287. // echo 'Excluding: ' . $file->getRealPath() . '<br>';
  288. continue;
  289. }
  290.  
  291. if ($file->isDir()) {
  292. @rmdir($file->getRealPath());
  293. //echo 'DIR: ' . $file->getRealPath() . '<br>';
  294. } else {
  295. @unlink($file->getRealPath());
  296. //echo 'FILE: ' . $file->getRealPath() . '<br>';
  297. }
  298. }
  299. if (file_exists($dir))
  300. @rmdir($dir);
  301. }
  302.  
  303. function copy_recursively($src, $dest) {
  304. //var_dump($src);
  305. global $exc;
  306. $excludeDirsNames = array();
  307. $excludeFileNames = $exc["files"];
  308. // var_dump( $excludeFileNames );
  309.  
  310. if (is_dir('' . $src)) {
  311. // var_dump($src);
  312. // if ($dest != "./")
  313. // rmdir_recursively($dest);
  314. @mkdir($dest);
  315. $files = scandir($src);
  316.  
  317.  
  318.  
  319. // var_dump( $excludeFileNames );
  320.  
  321. foreach ($files as $file) {
  322. if (!in_array($file, $excludeDirsNames)) {
  323.  
  324. if ($file != "." && $file != "..")
  325. copy_recursively("$src/$file", "$dest/$file");
  326. }
  327. }
  328. }
  329. else if (file_exists($src)) {
  330.  
  331. $filename = $src;
  332. $filename = end(explode("/", $src));
  333. //$filename = $filename[count( $filename)-2];
  334. if (!in_array($filename, $excludeFileNames)) {
  335. //var_dump($filename);
  336. // var_dump(in_array( $filename, $excludeDirsNames));
  337. copy($src, $dest);
  338. }
  339. }
  340. // rmdir_recursively($src);
  341. }
  342.  
  343. if ($mode != 1)
  344. echo '<br>Done';
  345. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement