XOOPS RMCommon Utilities  2.1.8.91RC
 All Classes Namespaces Files Functions Variables
updates.php
Go to the documentation of this file.
1 <?php
2 // $Id$
3 // --------------------------------------------------------------
4 // Red México Common Utilities
5 // A framework for Red México Modules
6 // Author: Eduardo Cortés <i.bitcero@gmail.com>
7 // Email: i.bitcero@gmail.com
8 // License: GPL 2.0
9 // --------------------------------------------------------------
10 
11 define('RMCLOCATION','updates');
12 include_once '../../include/cp_header.php';
13 
14 $updfile = XOOPS_CACHE_PATH.'/updates.chk';
15 $ftpConfig = new stdClass();
16 $runFiles = array();
17 
18 function jsonReturn($message, $error=1, $data=array()){
19 
20  $ret = array(
21  'message' => $message,
22  'error' => $error,
23  'data' => $data
24  );
25  echo json_encode($ret);
26  die();
27 
28 }
29 
32 
34  $rmUtil = RMUtilities::get();
35  $tf = new RMTimeFormatter('', '%T% %d%, %Y% at %h%:%i%');
36 
37  if(is_file($updfile))
38  $updates = unserialize(base64_decode(file_get_contents($updfile)));
39 
40  $rmFunc->create_toolbar();
41  $rmTpl->add_style('updates.css', 'rmcommon');
42  $rmTpl->add_local_script('updates.js', 'rmcommon', 'include');
43  $rmTpl->add_head_script('var xoToken = "'.$xoopsSecurity->createToken().'";');
44  $rmTpl->add_head_script('var langUpdated = "'.__('Item updated!','rmcommon').'";');
45 
46  $rmTpl->add_help(__('Updates Help','rmcommon'), 'http://www.xoopsmexico.net/docs/common-utilities/actualizaciones-automaticas/standalone/1/');
47 
48  $ftpserver = parse_url(XOOPS_URL);
49  $ftpserver = $ftpserver['host'];
50 
51  $pathinfo = parse_url(XOOPS_URL);
52  $ftpdir = str_replace($pathinfo['scheme'].'://'.$pathinfo['host'], '', XOOPS_URL);
53  unset($pathinfo);
54 
55  xoops_cp_header();
56  include $rmTpl->get_template('rmc_updates.php','module','rmcommon');
57  xoops_cp_footer();
58 
59 }
60 
64 function ajax_load_updates(){
65  global $rmTpl, $xoopsLogger, $updfile;
66 
67  $rmUtil = RMUtilities::get();
68 
69  $xoopsLogger->activated = false;
70  $updates = array();
71  if(is_file($updfile))
72  $updates = unserialize(base64_decode(file_get_contents($updfile)));
73 
74  include $rmTpl->get_template('ajax/rmc_updates_list.php','module','rmcommon');
75  die();
76 
77 }
78 
83  global $xoopsLogger, $rmTpl;
84 
85  $xoopsLogger->activated = false;
86 
87  $url = rmc_server_var($_GET, 'url', '');
88 
89  if($url=='')
90  jsonReturn(__('Invalid parameters!','rmcommon'));
91 
92  $data = json_decode(file_get_contents($url.'&action=update-details'), true);
93 
94  if($data['error']==1)
95  jsonReturn($data['message']);
96 
97  $rmUtil = RMUtilities::get();
98  $files = unserialize(base64_decode($data['data']['files']));
99  ob_start();
100  include $rmTpl->get_template('ajax/rmc_files_list.php','module','rmcommon');
101 
102  $ret = array(
103  'details' => $data['data']['details'],
104  'files' => ob_get_clean()
105  );
106 
107  jsonReturn(0,0,$ret);
108 
109  die();
110 }
111 
112 
113 function download_file(){
114  global $xoopsLogger, $rmTpl, $runFiles;
115 
116  $xoopsLogger->activated = false;
117 
118  $url = rmc_server_var($_POST, 'url', '');
119  $cred = rmc_server_var($_POST, 'credentials', '');
120  $type = rmc_server_var($_POST, 'type', '');
121  $dir = rmc_server_var($_POST, 'dir', '');
122  $ftpdata = rmc_server_var($_POST, 'ftp', '');
123 
124  if($url=='')
125  jsonReturn(__('Invalid parameters!','rmcommon'));
126 
127  // Request access
128  $response = json_decode(file_get_contents($url.'&action=login'.($cred!='' ? '&l='.$cred : '')), true);
129  if($response['error']==1)
130  jsonReturn($response['message']);
131 
132  //jsonReturn($response['data']['url']);
133 
134  if(!is_dir(XOOPS_CACHE_PATH.'/updates/'))
135  mkdir(XOOPS_CACHE_PATH.'/updates/', 511);
136 
137  if(!file_put_contents(XOOPS_CACHE_PATH.'/updates/'.$type.'-'.$dir.'.zip', file_get_contents($response['data']['url'])))
138  jsonReturn(__('Unable to download update file!','rmcommon'));
139 
140  // Extract files
141  $zip = new ZipArchive();
142  $res = $zip->open(XOOPS_CACHE_PATH.'/updates/'.$type.'-'.$dir.'.zip');
143  if($res!==TRUE)
144  jsonReturn(__('ERROR: unable to open downloaded zip file!','rmcommon'));
145 
146  $rmUtil = RMUtilities::get();
147  $source = XOOPS_CACHE_PATH.'/updates/'.$type.'-'.$dir;
148  if(is_dir($source))
149  $rmUtil->delete_directory($source);
150 
151  $zip->extractTo($source);
152  $zip->close();
153  // Delete downloaded zip
154  unlink(XOOPS_CACHE_PATH.'/updates/'.$type.'-'.$dir.'.zip');
155 
156  // Get files list
157  $details = json_decode(file_get_contents($url.'&action=update-details'), true);
158  if($details['error']==1)
159  jsonReturn($details['message']);
160 
161  $files = unserialize(base64_decode($details['data']['files']));
162 
163  // Prepare to copy files
164 
165  $target = XOOPS_ROOT_PATH.'/modules/';
166  if($type=='plugin')
167  $target .= 'rmcommon/plugins/'.$dir;
168  else
169  $target .= $dir;
170 
171  if(!is_dir($target))
172  jsonReturn(sprintf(__('Target path "%s" odes not exists!','rmcommon'), $target));
173 
174  // TODO 1: Verificar si existen permisos de escritura
175  if(is_writable($target)){
176 
177  foreach($files as $item){
178 
179  $fpath = $source . $item['path'] . ($item['path']!='/' ? '/' : '') . $item['name'];
180 
181  if($item['type']=='directory'){
182 
183  if($item['action']=='delete')
184  is_dir($target.$item['path'].'/'.$item['name']) ? $rmUtil->delete_directory(str_replace($source, $target, $fpath)) : null;
185  else
186  !is_dir($target.$item['path'].'/'.$item['name']) ? mkdir(str_replace($source, $target, $fpath)) : null;
187 
188 
189  } else {
190 
191  if($item['action']=='delete')
192  is_file($target.$item['path'].'/'.$item['name']) ? unlink(str_replace($source, $target, $fpath)) : null;
193  else
194  copy($fpath, str_replace($source, $target, $fpath));
195 
196  if($item['action']=='run')
197  $runFiles[] = str_replace(XOOPS_ROOT_PATH, XOOPS_URL, str_replace($source, $target, $fpath));
198 
199  }
200 
201  }
202 
203  } else {
204 
205  if($ftpdata=='')
206  jsonReturn(__('FTP configuration not specified!','rmcommon'));
207 
208  parse_str($ftpdata);
209  if($ftp_server=='' || $ftp_user=='' || $ftp_pass=='')
210  jsonReturn(__('FTP configuration not valid!','rmcommon'));
211 
212  global $ftpConfig;
213  $ftpConfig->server = $ftp_server;
214  $ftpConfig->user = $ftp_user;
215  $ftpConfig->pass = $ftp_pass;
216  $ftpConfig->dir = $ftp_dir;
217  $ftpConfig->port = $ftp_port>0 ? $ftp_port : 21;
218 
219  $ftp = new RMFtpClient($ftp_server, $ftp_port>0 ? $ftp_port : 21, $ftp_user, $ftp_pass);
220 
221  if(!$ftp->connect())
222  jsonReturn(sprintf(__('Unable to connect FTP server %s','rmcommon'), '<strong>'.$ftp_server.'</strong>'));
223 
224  $ftpConfig->base = $ftpConfig->dir .'/modules/'.($type=='plugin' ? 'rmcommon/plugins/' : '').$dir;
225  $ftpConfig->source = $source;
226  $ftpConfig->target = $target;
227 
228  foreach($files as $item){
229  processFile($item, $ftp);
230  }
231 
232  }
233 
234  // Update uploads file
235  $updates = unserialize(base64_decode(file_get_contents(XOOPS_CACHE_PATH.'/updates.chk')));
236  $new = array();
237  foreach($updates['updates'] as $upd){
238 
239  if($upd['data']['type'] == $type && $upd['data']['dir']==$dir) continue;
240  $new[] = $upd;
241 
242  }
243 
244  file_put_contents(XOOPS_CACHE_PATH.'/updates.chk', base64_encode(serialize(array('date'=>$updates['date'],'total'=>intval($updates['total'])-1,'updates'=>$new))));
245 
246  // Delete source folder
247  $rmUtil->delete_directory($source);
248 
249  if(!empty($runFiles))
250  jsonReturn(__('Executing files...','rmcommon'), 0, array('run'=>json_encode($runFiles)));
251  else
252  jsonReturn(sprintf(__('%s has been updated','rmcommon'), '<strong>'.$dir.'</strong>'), 0);
253 
254 }
255 
256 function processFile($file, $ftp){
257  global $ftpConfig, $runFiles;
258 
259  switch ($file['action']){
260  case 'update':
261  case 'run':
262 
263  if($file['type']=='directory' && $file['name']!=''){
264  $dirs = explode("/", $file['path'].'/'.$file['name']);
265  } else {
266  $dirs = explode("/", $file['path']);
267  $dirs = array_slice($dirs, 0, count($dirs)-1);
268  }
269 
270  if (count($dirs)>0) createDirs($dirs, $ftp);
271 
272  if($file['type']=='file')
273  putContents($ftpConfig->base.$file['path'].($file['path']!='/' ? '/' : '').$file['name'], $ftpConfig->source.$file['path'].($file['path']!='/' ? '/' : '').$file['name'], $ftp);
274 
275  chmodFile($ftpConfig->base.$file['path'].($file['path']!='/' ? '/' : '').$file['name'], $file['mode'], $ftp);
276 
277  // Almacenamos el archivo si se debe ejecutar
278  if ($file['action']=='run' && $file['type']=='file')
279  $runFiles[] = $ftpConfig->target.$file['path'].($file['path']!='/' ? '/' : '').$file['name'];
280 
281  break;
282 
283  case 'delete':
284 
285  if($file['type']=='directory')
286  deleteFTPDir($ftpConfig->base.$file['path'].($file['path']!='/' ? '/' : '').$file['name'], $ftp);
287  else
288  $ftp->delete($ftpConfig->base.$file['path'].($file['path']!='/' ? '/' : '').$file['name']);
289 
290  break;
291 
292  }
293 }
294 
295 // Create FTP firectories
296 function createDirs($dirs, RMFtpClient $ftp){
297  global $ftpConfig;
298 
299  $path = '';
300  $ftp->chdir($ftpConfig->base);
301  foreach ($dirs as $dir){
302  $path .= '/'.$dir;
303 
304  if (!$ftp->isDir($ftpConfig->base.$path))
305  $ftp->mkdir($ftpConfig->base.$path);
306 
307  }
308 }
309 
310 function chmodFile($file, $mode, $ftp){
311 
312  return $ftp->chmod($mode, $file);
313 
314 }
315 
316 function putContents($file, $source, $ftp){
317  global $updConfig;
318 
319  $res = $ftp->put($file, $source, FTP_BINARY);
320  return $res;
321 
322 }
323 
324 function deleteFTPDir($dir, $ftp){
325  global $ftpConfig;
326 
327  $list = $ftp->nlist($dir);
328  foreach($list as $item){
329  if ($item=='.' || $item=='..') continue;
330  if ($ftp->isDir($dir.$item)){
331  deleteFTPDir($dir.$item);
332  } else {
333  $ftp->delete($dir.$item);
334  }
335 
336  }
337 
338  $ftp->rmdir($dir);
339 
340 }
341 
342 
344  global $xoopsLogger;
345 
346  $xoopsLogger->activated = false;
347 
348  $url = rmc_server_var($_POST, 'url', '');
349  $cred = rmc_server_var($_POST, 'credentials', '');
350  $type = rmc_server_var($_POST, 'type', '');
351  $dir = rmc_server_var($_POST, 'dir', '');
352 
353  if($url=='')
354  jsonReturn(__('Invalid parameters!','rmcommon'));
355 
356  // Request access
357  $response = json_decode(file_get_contents($url.'&action=login'.($cred!='' ? '&l='.$cred : '')), true);
358  if($response['error']==1)
359  jsonReturn($response['message']);
360 
361  if(!is_dir(XOOPS_CACHE_PATH.'/updates/'))
362  mkdir(XOOPS_CACHE_PATH.'/updates/', 511);
363 
364  if(!file_put_contents(XOOPS_CACHE_PATH.'/updates/'.$type.'-'.$dir.'.zip', file_get_contents($response['data']['url'])))
365  jsonReturn(__('Unable to download update file!','rmcommon'));
366 
367  jsonReturn(__('Downloaded!', 'rmcommon'), 0, array(
368  'file' => $type.'-'.$dir.'.zip'
369  ));
370 }
371 
372 
376 function get_file_now(){
377 
378  global $xoopsSecurity;
379  $tfile = rmc_server_var($_GET, 'file', '');
380 
381  if($tfile=='')
382  redirectMsg('updates.php', __('File not found!','rmcommon'), RMMSG_ERROR);
383 
384  $tfile = str_replace(array("/","\\"), '', $tfile);
385 
386  $file = XOOPS_CACHE_PATH.'/updates/'.$tfile;
387  if(!is_file($file))
388  redirectMsg("updates.php", __('File not found!','rmcommon')." $tfile = $file", RMMSG_ERROR);
389 
390  header('Content-type: application/zip');
391  header('Cache-control: no-store');
392  header('Expires: 0');
393  header('Content-disposition: attachment; filename='.urlencode($tfile));
394  header('Content-Transfer-Encoding: binary');
395  header('Content-Lenght: '.filesize($file));
396  header('Last-Modified: '.gmdate("D, d M Y H:i:s",$file).'GMT');
397  ob_clean();
398  flush();
399  readfile($file);
400  unlink($file);
401  exit();
402 
403 }
404 
405 
406 $action = rmc_server_var($_REQUEST, 'action', '');
407 
408 switch($action){
409  case 'ajax-updates':
411  break;
412  case 'update-details':
414  break;
415  case 'first-step':
416  download_file();
417  break;
418  case 'later':
420  break;
421  case 'getfile':
422  get_file_now();
423  break;
424  default:
426  break;
427 
428 }
429