Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

262 lines
6.1 KiB

  1. <?php
  2. /* update friendika */
  3. define('APIBASE', 'http://github.com/api/v2/');
  4. define('F9KREPO', 'friendika/friendika');
  5. $up_totalfiles = 0;
  6. $up_countfiles = 0;
  7. $up_lastp = -1;
  8. function checkUpdate(){
  9. $r = fetch_url( APIBASE."json/repos/show/".F9KREPO."/tags" );
  10. $tags = json_decode($r);
  11. $tag = 0.0;
  12. foreach ($tags->tags as $i=>$v){
  13. $i = (float)$i;
  14. if ($i>$tag) $tag=$i;
  15. }
  16. if ($tag==0.0) return false;
  17. $f = fetch_url("https://raw.github.com/".F9KREPO."/".$tag."/boot.php","r");
  18. preg_match("|'FRIENDIKA_VERSION', *'([^']*)'|", $f, $m);
  19. $version = $m[1];
  20. $lv = explode(".", FRIENDIKA_VERSION);
  21. $rv = explode(".",$version);
  22. foreach($lv as $i=>$v){
  23. if ((int)$lv[$i] < (int)$rv[$i]) {
  24. return array($tag, $version, "https://github.com/friendika/friendika/zipball/".$tag);
  25. break;
  26. }
  27. }
  28. return false;
  29. }
  30. function canWeWrite(){
  31. $bd = dirname(dirname(__file__));
  32. return is_writable( $bd."/boot.php" );
  33. }
  34. function out($txt){ echo "§".$txt."§"; ob_end_flush(); flush();}
  35. function up_count($path){
  36. $file_count = 0;
  37. $dir_handle = opendir($path);
  38. if (!$dir_handle) return -1;
  39. while ($file = readdir($dir_handle)) {
  40. if ($file == '.' || $file == '..') continue;
  41. $file_count++;
  42. if (is_dir($path . $file)){
  43. $file_count += up_count($path . $file . DIRECTORY_SEPARATOR);
  44. }
  45. }
  46. closedir($dir_handle);
  47. return $file_count;
  48. }
  49. function up_unzip($file, $folder="/tmp"){
  50. $folder.="/";
  51. $zip = zip_open($file);
  52. if ($zip) {
  53. while ($zip_entry = zip_read($zip)) {
  54. $zip_entry_name = zip_entry_name($zip_entry);
  55. if (substr($zip_entry_name,strlen($zip_entry_name)-1,1)=="/"){
  56. mkdir($folder.$zip_entry_name,0777, true);
  57. } else {
  58. $fp = fopen($folder.$zip_entry_name, "w");
  59. if (zip_entry_open($zip, $zip_entry, "r")) {
  60. $buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
  61. fwrite($fp,"$buf");
  62. zip_entry_close($zip_entry);
  63. fclose($fp);
  64. }
  65. }
  66. }
  67. zip_close($zip);
  68. }
  69. }
  70. /**
  71. * Walk recoursively in a folder and call a callback function on every
  72. * dir entry.
  73. * args:
  74. * $dir string base dir to walk
  75. * $callback function callback function
  76. * $sort int 0: ascending, 1: descending
  77. * $cb_argv any extra value passed to callback
  78. *
  79. * callback signature:
  80. * function name($fn, $dir [, $argv])
  81. * $fn string full dir entry name
  82. * $dir string start dir path
  83. * $argv any user value to callback
  84. *
  85. */
  86. function up_walktree($dir, $callback=Null, $sort=0, $cb_argv=Null , $startdir=Null){
  87. if (is_null($callback)) return;
  88. if (is_null($startdir)) $startdir = $dir;
  89. $res = scandir($dir, $sort);
  90. foreach($res as $i=>$v){
  91. if ($v!="." && $v!=".."){
  92. $fn = $dir."/".$v;
  93. if ($sort==0) $callback($fn, $startdir, $cb_argv);
  94. if (is_dir($fn)) up_walktree($fn, $callback, $sort, $cb_argv, $startdir);
  95. if ($sort==1) $callback($fn, $startdir, $cb_argv);
  96. }
  97. }
  98. }
  99. function up_copy($fn, $dir){
  100. global $up_countfiles, $up_totalfiles, $up_lastp;
  101. $up_countfiles++; $prc=(int)(((float)$up_countfiles/(float)$up_totalfiles)*100);
  102. if (strpos($fn, ".gitignore")>-1 || strpos($fn, ".htaccess")>-1) return;
  103. $ddest = dirname(dirname(__file__));
  104. $fd = str_replace($dir, $ddest, $fn);
  105. if (is_dir($fn) && !is_dir($fd)) {
  106. $re=mkdir($fd,0777,true);
  107. }
  108. if (!is_dir($fn)){
  109. $re=copy($fn, $fd);
  110. }
  111. if ($re===false) {
  112. out("ERROR. Abort.");
  113. killme();
  114. }
  115. out("copy@Copy@$prc%");
  116. }
  117. function up_ftp($fn, $dir, $argv){
  118. global $up_countfiles, $up_totalfiles, $up_lastp;
  119. $up_countfiles++; $prc=(int)(((float)$up_countfiles/(float)$up_totalfiles)*100);
  120. if (strpos($fn, ".gitignore")>-1 || strpos($fn, ".htaccess")>-1) return;
  121. list($ddest, $conn_id) = $argv;
  122. $l = strlen($ddest)-1;
  123. if (substr($ddest,$l,1)=="/") $ddest = substr($ddest,0,$l);
  124. $fd = str_replace($dir, $ddest, $fn);
  125. if (is_dir($fn)){
  126. if (ftp_nlist($conn_id, $fd)===false) {
  127. $ret = ftp_mkdir($conn_id, $fd);
  128. } else {
  129. $ret=true;
  130. }
  131. } else {
  132. $ret = ftp_put($conn_id, $fd, $fn, FTP_BINARY);
  133. }
  134. if (!$ret) {
  135. out("ERROR. Abort.");
  136. killme();
  137. }
  138. out("copy@Copy@$prc%");
  139. }
  140. function up_rm($fn, $dir){
  141. if (is_dir($fn)){
  142. rmdir($fn);
  143. } else {
  144. unlink($fn);
  145. }
  146. }
  147. function up_dlfile($url, $file) {
  148. $in = fopen ($url, "r");
  149. $out = fopen ($file, "w");
  150. $fs = filesize($url);
  151. if (!$in || !$out) return false;
  152. $s=0; $count=0;
  153. while (!feof ($in)) {
  154. $line = fgets ($in, 1024);
  155. fwrite( $out, $line);
  156. $count++; $s += strlen($line);
  157. if ($count==50){
  158. $count=0;
  159. $sp=$s/1024.0; $ex="Kb";
  160. if ($sp>1024) { $sp=$sp/1024; $ex="Mb"; }
  161. if ($sp>1024) { $sp=$sp/1024; $ex="Gb"; }
  162. $sp = ((int)($sp*100))/100;
  163. out("dwl@Download@".$sp.$ex);
  164. }
  165. }
  166. fclose($in);
  167. return true;
  168. }
  169. function doUpdate($remotefile, $ftpdata=false){
  170. global $up_totalfiles;
  171. $localtmpfile = tempnam("/tmp", "fk");
  172. out("dwl@Download@starting...");
  173. $rt= up_dlfile($remotefile, $localtmpfile);
  174. if ($rt==false || filesize($localtmpfile)==0){
  175. out("dwl@Download@ERROR.");
  176. unlink($localtmpfile);
  177. return;
  178. }
  179. out("dwl@Download@Ok.");
  180. out("unzip@Unzip@");
  181. $tmpdirname = $localfile."ex";
  182. mkdir($tmpdirname);
  183. up_unzip($localtmpfile, $tmpdirname);
  184. $basedir = glob($tmpdirname."/*"); $basedir=$basedir[0];
  185. out ("unzip@Unzip@Ok.");
  186. $up_totalfiles = up_count($basedir."/");
  187. if (canWeWrite()){
  188. out("copy@Copy@");
  189. up_walktree($basedir, 'up_copy');
  190. }
  191. if ($ftpdata!==false && is_array($ftpdata) && $ftpdata['ftphost']!="" ){
  192. out("ftpcon@Connect to FTP@");
  193. $conn_id = ftp_connect($ftpdata['ftphost']);
  194. $login_result = ftp_login($conn_id, $ftpdata['ftpuser'], $ftpdata['ftppwd']);
  195. if ((!$conn_id) || (!$login_result)) {
  196. out("ftpcon@Connect to FTP@FAILED");
  197. up_clean($tmpdirname, $localtmpfile);
  198. return;
  199. } else {
  200. out("ftpcon@Connect to FTP@Ok.");
  201. }
  202. out("copy@Copy@");
  203. up_walktree($basedir, 'up_ftp', 0, array( $ftpdata['ftppath'], $conn_id));
  204. ftp_close($conn_id);
  205. }
  206. up_clean($tmpdirname, $localtmpfile);
  207. }
  208. function up_clean($tmpdirname, $localtmpfile){
  209. out("clean@Clean up@");
  210. unlink($localtmpfile);
  211. up_walktree($tmpdirname, 'up_rm', 1);
  212. rmdir($tmpdirname);
  213. out("clean@Clean up@Ok.");
  214. }