3 define('APIBASE', 'http://github.com/api/v2/');
4 define('F9KREPO', 'friendika/friendika');
10 function checkUpdate(){
11 $r = fetch_url( APIBASE."json/repos/show/".F9KREPO."/tags" );
12 $tags = json_decode($r);
15 foreach ($tags->tags as $i=>$v){
20 if ($tag==0.0) return false;
21 $f = fetch_url("https://raw.github.com/".F9KREPO."/".$tag."/boot.php","r");
22 preg_match("|'FRIENDIKA_VERSION', *'([^']*)'|", $f, $m);
25 $lv = explode(".", FRIENDIKA_VERSION);
26 $rv = explode(".",$version);
27 foreach($lv as $i=>$v){
28 if ((int)$lv[$i] < (int)$rv[$i]) {
29 return array($tag, $version, "https://github.com/friendika/friendika/zipball/".$tag);
35 function canWeWrite(){
36 $bd = dirname(dirname(__file__));
37 return is_writable( $bd."/boot.php" );
40 function out($txt){ echo "§".$txt."§"; ob_end_flush(); flush();}
42 function up_count($path){
46 $dir_handle = opendir($path);
48 if (!$dir_handle) return -1;
50 while ($file = readdir($dir_handle)) {
52 if ($file == '.' || $file == '..') continue;
55 if (is_dir($path . $file)){
56 $file_count += up_count($path . $file . DIRECTORY_SEPARATOR);
61 closedir($dir_handle);
68 function up_unzip($file, $folder="/tmp"){
70 $zip = zip_open($file);
72 while ($zip_entry = zip_read($zip)) {
73 $zip_entry_name = zip_entry_name($zip_entry);
74 if (substr($zip_entry_name,strlen($zip_entry_name)-1,1)=="/"){
75 mkdir($folder.$zip_entry_name,0777, true);
77 $fp = fopen($folder.$zip_entry_name, "w");
78 if (zip_entry_open($zip, $zip_entry, "r")) {
79 $buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
81 zip_entry_close($zip_entry);
91 * Walk recoursively in a folder and call a callback function on every
94 * $dir string base dir to walk
95 * $callback function callback function
96 * $sort int 0: ascending, 1: descending
97 * $cb_argv any extra value passed to callback
100 * function name($fn, $dir [, $argv])
101 * $fn string full dir entry name
102 * $dir string start dir path
103 * $argv any user value to callback
106 function up_walktree($dir, $callback=Null, $sort=0, $cb_argv=Null , $startdir=Null){
107 if (is_null($callback)) return;
108 if (is_null($startdir)) $startdir = $dir;
109 $res = scandir($dir, $sort);
110 foreach($res as $i=>$v){
111 if ($v!="." && $v!=".."){
113 if ($sort==0) $callback($fn, $startdir, $cb_argv);
114 if (is_dir($fn)) up_walktree($fn, $callback, $sort, $cb_argv, $startdir);
115 if ($sort==1) $callback($fn, $startdir, $cb_argv);
121 function up_copy($fn, $dir){
122 global $up_countfiles, $up_totalfiles, $up_lastp;
123 $up_countfiles++; $prc=(int)(((float)$up_countfiles/(float)$up_totalfiles)*100);
125 if (strpos($fn, ".gitignore")>-1 || strpos($fn, ".htaccess")>-1) return;
126 $ddest = dirname(dirname(__file__));
127 $fd = str_replace($dir, $ddest, $fn);
129 if (is_dir($fn) && !is_dir($fd)) {
130 $re=mkdir($fd,0777,true);
137 out("ERROR. Abort.");
140 out("copy@Copy@$prc%");
143 function up_ftp($fn, $dir, $argv){
144 global $up_countfiles, $up_totalfiles, $up_lastp;
145 $up_countfiles++; $prc=(int)(((float)$up_countfiles/(float)$up_totalfiles)*100);
147 if (strpos($fn, ".gitignore")>-1 || strpos($fn, ".htaccess")>-1) return;
149 list($ddest, $conn_id) = $argv;
150 $l = strlen($ddest)-1;
151 if (substr($ddest,$l,1)=="/") $ddest = substr($ddest,0,$l);
152 $fd = str_replace($dir, $ddest, $fn);
155 if (ftp_nlist($conn_id, $fd)===false) {
156 $ret = ftp_mkdir($conn_id, $fd);
161 $ret = ftp_put($conn_id, $fd, $fn, FTP_BINARY);
164 out("ERROR. Abort.");
167 out("copy@Copy@$prc%");
170 function up_rm($fn, $dir){
178 function up_dlfile($url, $file) {
179 $in = fopen ($url, "r");
180 $out = fopen ($file, "w");
182 $fs = filesize($url);
185 if (!$in || !$out) return false;
188 while (!feof ($in)) {
189 $line = fgets ($in, 1024);
190 fwrite( $out, $line);
192 $count++; $s += strlen($line);
195 $sp=$s/1024.0; $ex="Kb";
196 if ($sp>1024) { $sp=$sp/1024; $ex="Mb"; }
197 if ($sp>1024) { $sp=$sp/1024; $ex="Gb"; }
198 $sp = ((int)($sp*100))/100;
199 out("dwl@Download@".$sp.$ex);
206 function doUpdate($remotefile, $ftpdata=false){
207 global $up_totalfiles;
210 $localtmpfile = tempnam("/tmp", "fk");
211 out("dwl@Download@starting...");
212 $rt= up_dlfile($remotefile, $localtmpfile);
213 if ($rt==false || filesize($localtmpfile)==0){
214 out("dwl@Download@ERROR.");
215 unlink($localtmpfile);
218 out("dwl@Download@Ok.");
221 $tmpdirname = $localfile."ex";
223 up_unzip($localtmpfile, $tmpdirname);
224 $basedir = glob($tmpdirname."/*"); $basedir=$basedir[0];
225 out ("unzip@Unzip@Ok.");
227 $up_totalfiles = up_count($basedir."/");
231 up_walktree($basedir, 'up_copy');
233 if ($ftpdata!==false && is_array($ftpdata) && $ftpdata['ftphost']!="" ){
234 out("ftpcon@Connect to FTP@");
235 $conn_id = ftp_connect($ftpdata['ftphost']);
236 $login_result = ftp_login($conn_id, $ftpdata['ftpuser'], $ftpdata['ftppwd']);
238 if ((!$conn_id) || (!$login_result)) {
239 out("ftpcon@Connect to FTP@FAILED");
240 up_clean($tmpdirname, $localtmpfile);
243 out("ftpcon@Connect to FTP@Ok.");
246 up_walktree($basedir, 'up_ftp', 0, array( $ftpdata['ftppath'], $conn_id));
251 up_clean($tmpdirname, $localtmpfile);
255 function up_clean($tmpdirname, $localtmpfile){
256 out("clean@Clean up@");
257 unlink($localtmpfile);
258 up_walktree($tmpdirname, 'up_rm', 1);
260 out("clean@Clean up@Ok.");