]> git.mxchange.org Git - friendica.git/blob - include/remoteupdate.php
Bugfix: Warning because of undefined constant
[friendica.git] / include / remoteupdate.php
1 <?php
2 /* update friendica */
3 define('APIBASE', 'http://github.com/api/v2/');
4 define('F9KREPO', 'friendica/friendica');
5
6 $up_totalfiles = 0;
7 $up_countfiles = 0;
8 $up_lastp = -1;
9
10 function checkUpdate(){
11         $r = fetch_url( APIBASE."json/repos/show/".F9KREPO."/tags" );
12         $tags = json_decode($r);
13         
14         $tag = 0.0;
15         foreach ($tags->tags as $i=>$v){
16                 $i = (float)$i;
17                 if ($i>$tag) $tag=$i;
18         }
19         
20         if ($tag==0.0) return false;
21         $f = fetch_url("https://raw.github.com/".F9KREPO."/".$tag."/boot.php","r");
22         preg_match("|'FRIENDICA_VERSION', *'([^']*)'|", $f, $m);
23         $version =  $m[1];
24         
25         $lv = explode(".", FRIENDICA_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/friendica/friendica/zipball/".$tag);
30                         break;
31                 }
32         }
33         return false;
34 }
35 function canWeWrite(){
36         $bd = dirname(dirname(__file__));
37         return is_writable( $bd."/boot.php" );
38 }
39
40 function out($txt){ echo "§".$txt."§"; ob_end_flush(); flush();}
41
42 function up_count($path){
43  
44     $file_count = 0;
45  
46     $dir_handle = opendir($path);
47  
48     if (!$dir_handle) return -1;
49  
50     while ($file = readdir($dir_handle)) {
51  
52         if ($file == '.' || $file == '..') continue;
53                 $file_count++;
54         
55         if (is_dir($path . $file)){      
56             $file_count += up_count($path . $file . DIRECTORY_SEPARATOR);
57         }
58         
59     }
60  
61     closedir($dir_handle);
62  
63     return $file_count;
64 }
65
66
67
68 function up_unzip($file, $folder="/tmp"){
69         $folder.="/";
70         $zip = zip_open($file);
71         if ($zip) {
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);
76                 } else {
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));
80                           fwrite($fp,"$buf");
81                           zip_entry_close($zip_entry);
82                           fclose($fp);
83                         }
84                 }
85           }
86           zip_close($zip);
87         }       
88 }
89
90 /**
91  * Walk recoursively in a folder and call a callback function on every
92  * dir entry.
93  * args:
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
98  * 
99  * callback signature:
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
104  * 
105  */
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!=".."){
112                         $fn = $dir."/".$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);      
116                 }
117         }
118         
119 }
120
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);
124         
125         if (strpos($fn, ".gitignore")>-1 || strpos($fn, ".htaccess")>-1) return;  
126         $ddest = dirname(dirname(__file__));
127         $fd = str_replace($dir, $ddest, $fn);
128         
129         if (is_dir($fn) && !is_dir($fd)) {
130                 $re=mkdir($fd,0777,true);
131         }
132         if (!is_dir($fn)){
133                 $re=copy($fn, $fd);
134         }
135         
136         if ($re===false) { 
137                 out("ERROR. Abort."); 
138                 killme();
139         }
140         out("copy@Copy@$prc%");
141 }
142
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);
146
147         if (strpos($fn, ".gitignore")>-1 || strpos($fn, ".htaccess")>-1) return;
148         
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);
153         
154         if (is_dir($fn)){
155                 if (ftp_nlist($conn_id, $fd)===false) { 
156                         $ret = ftp_mkdir($conn_id, $fd);
157                 } else {
158                         $ret=true;
159                 }
160         } else {
161                 $ret = ftp_put($conn_id, $fd, $fn, FTP_BINARY); 
162         }
163         if (!$ret) { 
164                 out("ERROR. Abort."); 
165                 killme();
166         } 
167         out("copy@Copy@$prc%");
168 }
169
170 function up_rm($fn, $dir){
171         if (is_dir($fn)){
172                 rmdir($fn);
173         } else {
174                 unlink($fn);
175         }
176 }
177
178 function up_dlfile($url, $file) {
179         $in = fopen ($url, "r");
180         $out = fopen ($file, "w");
181         
182         $fs = filesize($url);
183         
184
185         if (!$in || !$out) return false;
186         
187         $s=0; $count=0;
188         while (!feof ($in)) {
189                 $line = fgets ($in, 1024);
190                 fwrite( $out, $line);
191                 
192                 $count++; $s += strlen($line);
193                 if ($count==50){
194                         $count=0;
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);
200                 }
201         }
202         fclose($in);
203         return true;
204 }
205
206 function doUpdate($remotefile, $ftpdata=false){
207         global $up_totalfiles;
208         
209         
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);
216                 return;
217         }
218         out("dwl@Download@Ok.");
219         
220         out("unzip@Unzip@");
221         $tmpdirname = $localfile."ex";
222         mkdir($tmpdirname);
223         up_unzip($localtmpfile, $tmpdirname);
224         $basedir = glob($tmpdirname."/*"); $basedir=$basedir[0];
225         out ("unzip@Unzip@Ok.");
226         
227         $up_totalfiles = up_count($basedir."/");
228         
229         if (canWeWrite()){
230                 out("copy@Copy@");
231                 up_walktree($basedir, 'up_copy');
232         }
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']);
237                 
238                 if ((!$conn_id) || (!$login_result)) { 
239                         out("ftpcon@Connect to FTP@FAILED");
240                         up_clean($tmpdirname, $localtmpfile); 
241                         return;
242                 } else {
243                         out("ftpcon@Connect to FTP@Ok.");
244                 }
245                 out("copy@Copy@");
246                 up_walktree($basedir, 'up_ftp', 0, array( $ftpdata['ftppath'], $conn_id));
247  
248                 ftp_close($conn_id);
249         }
250
251         up_clean($tmpdirname, $localtmpfile); 
252         
253 }
254
255 function up_clean($tmpdirname, $localtmpfile){
256         out("clean@Clean up@");
257         unlink($localtmpfile);
258         up_walktree($tmpdirname, 'up_rm', 1);
259         rmdir($tmpdirname);
260         out("clean@Clean up@Ok.");
261 }