PageRenderTime 57ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/php/Sources/Subs-Package.php

https://github.com/dekoza/openshift-smf-2.0.7
PHP | 2995 lines | 2198 code | 388 blank | 409 comment | 621 complexity | b48c36b73bc696160c5570a0bd92e8cc MD5 | raw file
Possible License(s): BSD-3-Clause

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * Simple Machines Forum (SMF)
  4. *
  5. * @package SMF
  6. * @author Simple Machines http://www.simplemachines.org
  7. * @copyright 2011 Simple Machines
  8. * @license http://www.simplemachines.org/about/smf/license.php BSD
  9. *
  10. * @version 2.0.1
  11. */
  12. if (!defined('SMF'))
  13. die('Hacking attempt...');
  14. /* This file's central purpose of existence is that of making the package
  15. manager work nicely. It contains functions for handling tar.gz and zip
  16. files, as well as a simple xml parser to handle the xml package stuff.
  17. Not to mention a few functions to make file handling easier.
  18. array read_tgz_file(string filename, string destination,
  19. bool single_file = false, bool overwrite = false, array files_to_extract = null)
  20. - reads a .tar.gz file, filename, in and extracts file(s) from it.
  21. - essentially just a shortcut for read_tgz_data().
  22. array read_tgz_data(string data, string destination,
  23. bool single_file = false, bool overwrite = false, array files_to_extract = null)
  24. - extracts a file or files from the .tar.gz contained in data.
  25. - detects if the file is really a .zip file, and if so returns the
  26. result of read_zip_data
  27. - if destination is null, returns a list of files in the archive.
  28. - if single_file is true, returns the contents of the file specified
  29. by destination, if it exists, or false.
  30. - if single_file is true, destination can start with * and / to
  31. signify that the file may come from any directory.
  32. - destination should not begin with a / if single_file is true.
  33. - overwrites existing files with newer modification times if and
  34. only if overwrite is true.
  35. - creates the destination directory if it doesn't exist, and is
  36. is specified.
  37. - requires zlib support be built into PHP.
  38. - returns an array of the files extracted.
  39. - if files_to_extract is not equal to null only extracts file within this array.
  40. array read_zip_data(string data, string destination,
  41. bool single_file = false, bool overwrite = false, array files_to_extract = null)
  42. - extracts a file or files from the .zip contained in data.
  43. - if destination is null, returns a list of files in the archive.
  44. - if single_file is true, returns the contents of the file specified
  45. by destination, if it exists, or false.
  46. - if single_file is true, destination can start with * and / to
  47. signify that the file may come from any directory.
  48. - destination should not begin with a / if single_file is true.
  49. - overwrites existing files with newer modification times if and
  50. only if overwrite is true.
  51. - creates the destination directory if it doesn't exist, and is
  52. is specified.
  53. - requires zlib support be built into PHP.
  54. - returns an array of the files extracted.
  55. - if files_to_extract is not equal to null only extracts file within this array.
  56. bool url_exists(string url)
  57. - checks to see if url is valid, and returns a 200 status code.
  58. - will return false if the file is "moved permanently" or similar.
  59. - returns true if the remote url exists.
  60. array loadInstalledPackages()
  61. - loads and returns an array of installed packages.
  62. - gets this information from Packages/installed.list.
  63. - returns the array of data.
  64. array getPackageInfo(string filename)
  65. - loads a package's information and returns a representative array.
  66. - expects the file to be a package in Packages/.
  67. - returns a error string if the package-info is invalid.
  68. - returns a basic array of id, version, filename, and similar
  69. information.
  70. - in the array returned, an xmlArray is available in 'xml'.
  71. void packageRequireFTP(string destination_url, array files = none, bool return = false)
  72. // !!!
  73. array parsePackageInfo(xmlArray &package, bool testing_only = true,
  74. string method = 'install', string previous_version = '')
  75. - parses the actions in package-info.xml files from packages.
  76. - package should be an xmlArray with package-info as its base.
  77. - testing_only should be true if the package should not actually be
  78. applied.
  79. - method is upgrade, install, or uninstall. Its default is install.
  80. - previous_version should be set to the previous installed version
  81. of this package, if any.
  82. - does not handle failure terribly well; testing first is always
  83. better.
  84. - returns an array of those changes made.
  85. bool matchPackageVersion(string version, string versions)
  86. - checks if version matches any of the versions in versions.
  87. - supports comma separated version numbers, with or without
  88. whitespace.
  89. - supports lower and upper bounds. (1.0-1.2)
  90. - returns true if the version matched.
  91. int compareVersions(string version1, string version2)
  92. - compares two versions.
  93. - returns 0 if version1 is equal to version2.
  94. - returns -1 if version1 is lower than version2.
  95. - returns 1 if version1 is higher than version2.
  96. string parse_path(string path)
  97. - parses special identifiers out of the specified path.
  98. - returns the parsed path.
  99. void deltree(string path, bool delete_directory = true)
  100. - deletes a directory, and all the files and direcories inside it.
  101. - requires access to delete these files.
  102. bool mktree(string path, int mode)
  103. - creates the specified tree structure with the mode specified.
  104. - creates every directory in path until it finds one that already
  105. exists.
  106. - returns true if successful, false otherwise.
  107. void copytree(string source, string destination)
  108. - copies one directory structure over to another.
  109. - requires the destination to be writable.
  110. void listtree(string path, string sub_path = none)
  111. // !!!
  112. array parseModification(string file, bool testing = true, bool undo = false, array theme_paths = array())
  113. - parses a xml-style modification file (file).
  114. - testing tells it the modifications shouldn't actually be saved.
  115. - undo specifies that the modifications the file requests should be
  116. undone; this doesn't work with everything (regular expressions.)
  117. - returns an array of those changes made.
  118. array parseBoardMod(string file, bool testing = true, bool undo = false, array theme_paths = array())
  119. - parses a boardmod-style modification file (file).
  120. - testing tells it the modifications shouldn't actually be saved.
  121. - undo specifies that the modifications the file requests should be
  122. undone.
  123. - returns an array of those changes made.
  124. // !!!
  125. int package_put_contents(string filename, string data)
  126. - writes data to a file, almost exactly like the file_put_contents()
  127. function.
  128. - uses FTP to create/chmod the file when necessary and available.
  129. - uses text mode for text mode file extensions.
  130. - returns the number of bytes written.
  131. void package_chmod(string filename)
  132. // !!!
  133. string package_crypt(string password)
  134. // !!!
  135. string fetch_web_data(string url, string post_data = '',
  136. bool keep_alive = false)
  137. // !!!
  138. Creating your own package server:
  139. ---------------------------------------------------------------------------
  140. // !!!
  141. Creating your own package:
  142. ---------------------------------------------------------------------------
  143. // !!!
  144. */
  145. // Get the data from the file and extract it.
  146. function read_tgz_file($gzfilename, $destination, $single_file = false, $overwrite = false, $files_to_extract = null)
  147. {
  148. if (substr($gzfilename, 0, 7) == 'http://')
  149. {
  150. $data = fetch_web_data($gzfilename);
  151. if ($data === false)
  152. return false;
  153. }
  154. else
  155. {
  156. $data = @file_get_contents($gzfilename);
  157. if ($data === false)
  158. return false;
  159. }
  160. return read_tgz_data($data, $destination, $single_file, $overwrite, $files_to_extract);
  161. }
  162. // Extract tar.gz data. If destination is null, return a listing.
  163. function read_tgz_data($data, $destination, $single_file = false, $overwrite = false, $files_to_extract = null)
  164. {
  165. // Make sure we have this loaded.
  166. loadLanguage('Packages');
  167. // This function sorta needs gzinflate!
  168. if (!function_exists('gzinflate'))
  169. fatal_lang_error('package_no_zlib', 'critical');
  170. umask(0);
  171. if (!$single_file && $destination !== null && !file_exists($destination))
  172. mktree($destination, 0777);
  173. // No signature?
  174. if (strlen($data) < 2)
  175. return false;
  176. $id = unpack('H2a/H2b', substr($data, 0, 2));
  177. if (strtolower($id['a'] . $id['b']) != '1f8b')
  178. {
  179. // Okay, this ain't no tar.gz, but maybe it's a zip file.
  180. if (substr($data, 0, 2) == 'PK')
  181. return read_zip_data($data, $destination, $single_file, $overwrite, $files_to_extract);
  182. else
  183. return false;
  184. }
  185. $flags = unpack('Ct/Cf', substr($data, 2, 2));
  186. // Not deflate!
  187. if ($flags['t'] != 8)
  188. return false;
  189. $flags = $flags['f'];
  190. $offset = 10;
  191. $octdec = array('mode', 'uid', 'gid', 'size', 'mtime', 'checksum', 'type');
  192. // "Read" the filename and comment. // !!! Might be mussed.
  193. if ($flags & 12)
  194. {
  195. while ($flags & 8 && $data{$offset++} != "\0")
  196. continue;
  197. while ($flags & 4 && $data{$offset++} != "\0")
  198. continue;
  199. }
  200. $crc = unpack('Vcrc32/Visize', substr($data, strlen($data) - 8, 8));
  201. $data = @gzinflate(substr($data, $offset, strlen($data) - 8 - $offset));
  202. // smf_crc32 and crc32 may not return the same results, so we accept either.
  203. if ($crc['crc32'] != smf_crc32($data) && $crc['crc32'] != crc32($data))
  204. return false;
  205. $blocks = strlen($data) / 512 - 1;
  206. $offset = 0;
  207. $return = array();
  208. while ($offset < $blocks)
  209. {
  210. $header = substr($data, $offset << 9, 512);
  211. $current = unpack('a100filename/a8mode/a8uid/a8gid/a12size/a12mtime/a8checksum/a1type/a100linkname/a6magic/a2version/a32uname/a32gname/a8devmajor/a8devminor/a155path', $header);
  212. // Blank record? This is probably at the end of the file.
  213. if (empty($current['filename']))
  214. {
  215. $offset += 512;
  216. continue;
  217. }
  218. if ($current['type'] == 5 && substr($current['filename'], -1) != '/')
  219. $current['filename'] .= '/';
  220. foreach ($current as $k => $v)
  221. {
  222. if (in_array($k, $octdec))
  223. $current[$k] = octdec(trim($v));
  224. else
  225. $current[$k] = trim($v);
  226. }
  227. $checksum = 256;
  228. for ($i = 0; $i < 148; $i++)
  229. $checksum += ord($header{$i});
  230. for ($i = 156; $i < 512; $i++)
  231. $checksum += ord($header{$i});
  232. if ($current['checksum'] != $checksum)
  233. break;
  234. $size = ceil($current['size'] / 512);
  235. $current['data'] = substr($data, ++$offset << 9, $current['size']);
  236. $offset += $size;
  237. // Not a directory and doesn't exist already...
  238. if (substr($current['filename'], -1, 1) != '/' && !file_exists($destination . '/' . $current['filename']))
  239. $write_this = true;
  240. // File exists... check if it is newer.
  241. elseif (substr($current['filename'], -1, 1) != '/')
  242. $write_this = $overwrite || filemtime($destination . '/' . $current['filename']) < $current['mtime'];
  243. // Folder... create.
  244. elseif ($destination !== null && !$single_file)
  245. {
  246. // Protect from accidental parent directory writing...
  247. $current['filename'] = strtr($current['filename'], array('../' => '', '/..' => ''));
  248. if (!file_exists($destination . '/' . $current['filename']))
  249. mktree($destination . '/' . $current['filename'], 0777);
  250. $write_this = false;
  251. }
  252. else
  253. $write_this = false;
  254. if ($write_this && $destination !== null)
  255. {
  256. if (strpos($current['filename'], '/') !== false && !$single_file)
  257. mktree($destination . '/' . dirname($current['filename']), 0777);
  258. // Is this the file we're looking for?
  259. if ($single_file && ($destination == $current['filename'] || $destination == '*/' . basename($current['filename'])))
  260. return $current['data'];
  261. // If we're looking for another file, keep going.
  262. elseif ($single_file)
  263. continue;
  264. // Looking for restricted files?
  265. elseif ($files_to_extract !== null && !in_array($current['filename'], $files_to_extract))
  266. continue;
  267. package_put_contents($destination . '/' . $current['filename'], $current['data']);
  268. }
  269. if (substr($current['filename'], -1, 1) != '/')
  270. $return[] = array(
  271. 'filename' => $current['filename'],
  272. 'md5' => md5($current['data']),
  273. 'preview' => substr($current['data'], 0, 100),
  274. 'size' => $current['size'],
  275. 'skipped' => false
  276. );
  277. }
  278. if ($destination !== null && !$single_file)
  279. package_flush_cache();
  280. if ($single_file)
  281. return false;
  282. else
  283. return $return;
  284. }
  285. // Extract zip data. If destination is null, return a listing.
  286. function read_zip_data($data, $destination, $single_file = false, $overwrite = false, $files_to_extract = null)
  287. {
  288. umask(0);
  289. if ($destination !== null && !file_exists($destination) && !$single_file)
  290. mktree($destination, 0777);
  291. // Look for the PK header...
  292. if (substr($data, 0, 2) != 'PK')
  293. return false;
  294. // Find the central whosamawhatsit at the end; if there's a comment it's a pain.
  295. if (substr($data, -22, 4) == 'PK' . chr(5) . chr(6))
  296. $p = -22;
  297. else
  298. {
  299. // Have to find where the comment begins, ugh.
  300. for ($p = -22; $p > -strlen($data); $p--)
  301. {
  302. if (substr($data, $p, 4) == 'PK' . chr(5) . chr(6))
  303. break;
  304. }
  305. }
  306. $return = array();
  307. // Get the basic zip file info.
  308. $zip_info = unpack('vfiles/Vsize/Voffset', substr($data, $p + 10, 10));
  309. $p = $zip_info['offset'];
  310. for ($i = 0; $i < $zip_info['files']; $i++)
  311. {
  312. // Make sure this is a file entry...
  313. if (substr($data, $p, 4) != 'PK' . chr(1) . chr(2))
  314. return false;
  315. // Get all the important file information.
  316. $file_info = unpack('Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len/vcomment_len/vdisk/vinternal/Vexternal/Voffset', substr($data, $p + 16, 30));
  317. $file_info['filename'] = substr($data, $p + 46, $file_info['filename_len']);
  318. // Skip all the information we don't care about anyway.
  319. $p += 46 + $file_info['filename_len'] + $file_info['extra_len'] + $file_info['comment_len'];
  320. // If this is a file, and it doesn't exist.... happy days!
  321. if (substr($file_info['filename'], -1, 1) != '/' && !file_exists($destination . '/' . $file_info['filename']))
  322. $write_this = true;
  323. // If the file exists, we may not want to overwrite it.
  324. elseif (substr($file_info['filename'], -1, 1) != '/')
  325. $write_this = $overwrite;
  326. // This is a directory, so we're gonna want to create it. (probably...)
  327. elseif ($destination !== null && !$single_file)
  328. {
  329. // Just a little accident prevention, don't mind me.
  330. $file_info['filename'] = strtr($file_info['filename'], array('../' => '', '/..' => ''));
  331. if (!file_exists($destination . '/' . $file_info['filename']))
  332. mktree($destination . '/' . $file_info['filename'], 0777);
  333. $write_this = false;
  334. }
  335. else
  336. $write_this = false;
  337. // Check that the data is there and does exist.
  338. if (substr($data, $file_info['offset'], 4) != 'PK' . chr(3) . chr(4))
  339. return false;
  340. // Get the actual compressed data.
  341. $file_info['data'] = substr($data, $file_info['offset'] + 30 + $file_info['filename_len'], $file_info['compressed_size']);
  342. // Only inflate it if we need to ;).
  343. if ($file_info['compressed_size'] != $file_info['size'])
  344. $file_info['data'] = @gzinflate($file_info['data']);
  345. // Okay! We can write this file, looks good from here...
  346. if ($write_this && $destination !== null)
  347. {
  348. if (strpos($file_info['filename'], '/') !== false && !$single_file)
  349. mktree($destination . '/' . dirname($file_info['filename']), 0777);
  350. // If we're looking for a specific file, and this is it... ka-bam, baby.
  351. if ($single_file && ($destination == $file_info['filename'] || $destination == '*/' . basename($file_info['filename'])))
  352. return $file_info['data'];
  353. // Oh? Another file. Fine. You don't like this file, do you? I know how it is. Yeah... just go away. No, don't apologize. I know this file's just not *good enough* for you.
  354. elseif ($single_file)
  355. continue;
  356. // Don't really want this?
  357. elseif ($files_to_extract !== null && !in_array($file_info['filename'], $files_to_extract))
  358. continue;
  359. package_put_contents($destination . '/' . $file_info['filename'], $file_info['data']);
  360. }
  361. if (substr($file_info['filename'], -1, 1) != '/')
  362. $return[] = array(
  363. 'filename' => $file_info['filename'],
  364. 'md5' => md5($file_info['data']),
  365. 'preview' => substr($file_info['data'], 0, 100),
  366. 'size' => $file_info['size'],
  367. 'skipped' => false
  368. );
  369. }
  370. if ($destination !== null && !$single_file)
  371. package_flush_cache();
  372. if ($single_file)
  373. return false;
  374. else
  375. return $return;
  376. }
  377. // Checks the existence of a remote file since file_exists() does not do remote.
  378. function url_exists($url)
  379. {
  380. $a_url = parse_url($url);
  381. if (!isset($a_url['scheme']))
  382. return false;
  383. // Attempt to connect...
  384. $temp = '';
  385. $fid = fsockopen($a_url['host'], !isset($a_url['port']) ? 80 : $a_url['port'], $temp, $temp, 8);
  386. if (!$fid)
  387. return false;
  388. fputs($fid, 'HEAD ' . $a_url['path'] . ' HTTP/1.0' . "\r\n" . 'Host: ' . $a_url['host'] . "\r\n\r\n");
  389. $head = fread($fid, 1024);
  390. fclose($fid);
  391. return preg_match('~^HTTP/.+\s+(20[01]|30[127])~i', $head) == 1;
  392. }
  393. // Load the installed packages.
  394. function loadInstalledPackages()
  395. {
  396. global $boarddir, $smcFunc;
  397. // First, check that the database is valid, installed.list is still king.
  398. $install_file = implode('', file($boarddir . '/Packages/installed.list'));
  399. if (trim($install_file) == '')
  400. {
  401. $smcFunc['db_query']('', '
  402. UPDATE {db_prefix}log_packages
  403. SET install_state = {int:not_installed}',
  404. array(
  405. 'not_installed' => 0,
  406. )
  407. );
  408. // Don't have anything left, so send an empty array.
  409. return array();
  410. }
  411. // Load the packages from the database - note this is ordered by install time to ensure latest package uninstalled first.
  412. $request = $smcFunc['db_query']('', '
  413. SELECT id_install, package_id, filename, name, version
  414. FROM {db_prefix}log_packages
  415. WHERE install_state != {int:not_installed}
  416. ORDER BY time_installed DESC',
  417. array(
  418. 'not_installed' => 0,
  419. )
  420. );
  421. $installed = array();
  422. $found = array();
  423. while ($row = $smcFunc['db_fetch_assoc']($request))
  424. {
  425. // Already found this? If so don't add it twice!
  426. if (in_array($row['package_id'], $found))
  427. continue;
  428. $found[] = $row['package_id'];
  429. $installed[] = array(
  430. 'id' => $row['id_install'],
  431. 'name' => $row['name'],
  432. 'filename' => $row['filename'],
  433. 'package_id' => $row['package_id'],
  434. 'version' => $row['version'],
  435. );
  436. }
  437. $smcFunc['db_free_result']($request);
  438. return $installed;
  439. }
  440. function getPackageInfo($gzfilename)
  441. {
  442. global $boarddir;
  443. // Extract package-info.xml from downloaded file. (*/ is used because it could be in any directory.)
  444. if (strpos($gzfilename, 'http://') !== false)
  445. $packageInfo = read_tgz_data(fetch_web_data($gzfilename, '', true), '*/package-info.xml', true);
  446. else
  447. {
  448. if (!file_exists($boarddir . '/Packages/' . $gzfilename))
  449. return 'package_get_error_not_found';
  450. if (is_file($boarddir . '/Packages/' . $gzfilename))
  451. $packageInfo = read_tgz_file($boarddir . '/Packages/' . $gzfilename, '*/package-info.xml', true);
  452. elseif (file_exists($boarddir . '/Packages/' . $gzfilename . '/package-info.xml'))
  453. $packageInfo = file_get_contents($boarddir . '/Packages/' . $gzfilename . '/package-info.xml');
  454. else
  455. return 'package_get_error_missing_xml';
  456. }
  457. // Nothing?
  458. if (empty($packageInfo))
  459. return 'package_get_error_is_zero';
  460. // Parse package-info.xml into an xmlArray.
  461. loadClassFile('Class-Package.php');
  462. $packageInfo = new xmlArray($packageInfo);
  463. // !!! Error message of some sort?
  464. if (!$packageInfo->exists('package-info[0]'))
  465. return 'package_get_error_packageinfo_corrupt';
  466. $packageInfo = $packageInfo->path('package-info[0]');
  467. $package = $packageInfo->to_array();
  468. $package['xml'] = $packageInfo;
  469. $package['filename'] = $gzfilename;
  470. if (!isset($package['type']))
  471. $package['type'] = 'modification';
  472. return $package;
  473. }
  474. // Create a chmod control for chmoding files.
  475. function create_chmod_control($chmodFiles = array(), $chmodOptions = array(), $restore_write_status = false)
  476. {
  477. global $context, $modSettings, $package_ftp, $boarddir, $txt, $sourcedir, $scripturl;
  478. // If we're restoring the status of existing files prepare the data.
  479. if ($restore_write_status && isset($_SESSION['pack_ftp']) && !empty($_SESSION['pack_ftp']['original_perms']))
  480. {
  481. function list_restoreFiles($dummy1, $dummy2, $dummy3, $do_change)
  482. {
  483. global $txt;
  484. $restore_files = array();
  485. foreach ($_SESSION['pack_ftp']['original_perms'] as $file => $perms)
  486. {
  487. // Check the file still exists, and the permissions were indeed different than now.
  488. $file_permissions = @fileperms($file);
  489. if (!file_exists($file) || $file_permissions == $perms)
  490. {
  491. unset($_SESSION['pack_ftp']['original_perms'][$file]);
  492. continue;
  493. }
  494. // Are we wanting to change the permission?
  495. if ($do_change && isset($_POST['restore_files']) && in_array($file, $_POST['restore_files']))
  496. {
  497. // Use FTP if we have it.
  498. if (!empty($package_ftp))
  499. {
  500. $ftp_file = strtr($file, array($_SESSION['pack_ftp']['root'] => ''));
  501. $package_ftp->chmod($ftp_file, $perms);
  502. }
  503. else
  504. @chmod($file, $perms);
  505. $new_permissions = @fileperms($file);
  506. $result = $new_permissions == $perms ? 'success' : 'failure';
  507. unset($_SESSION['pack_ftp']['original_perms'][$file]);
  508. }
  509. elseif ($do_change)
  510. {
  511. $new_permissions = '';
  512. $result = 'skipped';
  513. unset($_SESSION['pack_ftp']['original_perms'][$file]);
  514. }
  515. // Record the results!
  516. $restore_files[] = array(
  517. 'path' => $file,
  518. 'old_perms_raw' => $perms,
  519. 'old_perms' => substr(sprintf('%o', $perms), -4),
  520. 'cur_perms' => substr(sprintf('%o', $file_permissions), -4),
  521. 'new_perms' => isset($new_permissions) ? substr(sprintf('%o', $new_permissions), -4) : '',
  522. 'result' => isset($result) ? $result : '',
  523. 'writable_message' => '<span style="color: ' . (@is_writable($file) ? 'green' : 'red') . '">' . (@is_writable($file) ? $txt['package_file_perms_writable'] : $txt['package_file_perms_not_writable']) . '</span>',
  524. );
  525. }
  526. return $restore_files;
  527. }
  528. $listOptions = array(
  529. 'id' => 'restore_file_permissions',
  530. 'title' => $txt['package_restore_permissions'],
  531. 'get_items' => array(
  532. 'function' => 'list_restoreFiles',
  533. 'params' => array(
  534. !empty($_POST['restore_perms']),
  535. ),
  536. ),
  537. 'columns' => array(
  538. 'path' => array(
  539. 'header' => array(
  540. 'value' => $txt['package_restore_permissions_filename'],
  541. ),
  542. 'data' => array(
  543. 'db' => 'path',
  544. 'class' => 'smalltext',
  545. ),
  546. ),
  547. 'old_perms' => array(
  548. 'header' => array(
  549. 'value' => $txt['package_restore_permissions_orig_status'],
  550. ),
  551. 'data' => array(
  552. 'db' => 'old_perms',
  553. 'class' => 'smalltext',
  554. ),
  555. ),
  556. 'cur_perms' => array(
  557. 'header' => array(
  558. 'value' => $txt['package_restore_permissions_cur_status'],
  559. ),
  560. 'data' => array(
  561. 'function' => create_function('$rowData', '
  562. global $txt;
  563. $formatTxt = $rowData[\'result\'] == \'\' || $rowData[\'result\'] == \'skipped\' ? $txt[\'package_restore_permissions_pre_change\'] : $txt[\'package_restore_permissions_post_change\'];
  564. return sprintf($formatTxt, $rowData[\'cur_perms\'], $rowData[\'new_perms\'], $rowData[\'writable_message\']);
  565. '),
  566. 'class' => 'smalltext',
  567. ),
  568. ),
  569. 'check' => array(
  570. 'header' => array(
  571. 'value' => '<input type="checkbox" onclick="invertAll(this, this.form);" class="input_check" />',
  572. ),
  573. 'data' => array(
  574. 'sprintf' => array(
  575. 'format' => '<input type="checkbox" name="restore_files[]" value="%1$s" class="input_check" />',
  576. 'params' => array(
  577. 'path' => false,
  578. ),
  579. ),
  580. 'style' => 'text-align: center',
  581. ),
  582. ),
  583. 'result' => array(
  584. 'header' => array(
  585. 'value' => $txt['package_restore_permissions_result'],
  586. ),
  587. 'data' => array(
  588. 'function' => create_function('$rowData', '
  589. global $txt;
  590. return $txt[\'package_restore_permissions_action_\' . $rowData[\'result\']];
  591. '),
  592. 'class' => 'smalltext',
  593. ),
  594. ),
  595. ),
  596. 'form' => array(
  597. 'href' => !empty($chmodOptions['destination_url']) ? $chmodOptions['destination_url'] : $scripturl . '?action=admin;area=packages;sa=perms;restore;' . $context['session_var'] . '=' . $context['session_id'],
  598. ),
  599. 'additional_rows' => array(
  600. array(
  601. 'position' => 'below_table_data',
  602. 'value' => '<input type="submit" name="restore_perms" value="' . $txt['package_restore_permissions_restore'] . '" class="button_submit" />',
  603. 'class' => 'titlebg',
  604. 'style' => 'text-align: right;',
  605. ),
  606. array(
  607. 'position' => 'after_title',
  608. 'value' => '<span class="smalltext">' . $txt['package_restore_permissions_desc'] . '</span>',
  609. 'class' => 'windowbg2',
  610. ),
  611. ),
  612. );
  613. // Work out what columns and the like to show.
  614. if (!empty($_POST['restore_perms']))
  615. {
  616. $listOptions['additional_rows'][1]['value'] = sprintf($txt['package_restore_permissions_action_done'], $scripturl . '?action=admin;area=packages;sa=perms;' . $context['session_var'] . '=' . $context['session_id']);
  617. unset($listOptions['columns']['check'], $listOptions['form'], $listOptions['additional_rows'][0]);
  618. $context['sub_template'] = 'show_list';
  619. $context['default_list'] = 'restore_file_permissions';
  620. }
  621. else
  622. {
  623. unset($listOptions['columns']['result']);
  624. }
  625. // Create the list for display.
  626. require_once($sourcedir . '/Subs-List.php');
  627. createList($listOptions);
  628. // If we just restored permissions then whereever we are, we are now done and dusted.
  629. if (!empty($_POST['restore_perms']))
  630. obExit();
  631. }
  632. // Otherwise, it's entirely irrelevant?
  633. elseif ($restore_write_status)
  634. return true;
  635. // This is where we report what we got up to.
  636. $return_data = array(
  637. 'files' => array(
  638. 'writable' => array(),
  639. 'notwritable' => array(),
  640. ),
  641. );
  642. // If we have some FTP information already, then let's assume it was required and try to get ourselves connected.
  643. if (!empty($_SESSION['pack_ftp']['connected']))
  644. {
  645. // Load the file containing the ftp_connection class.
  646. loadClassFile('Class-Package.php');
  647. $package_ftp = new ftp_connection($_SESSION['pack_ftp']['server'], $_SESSION['pack_ftp']['port'], $_SESSION['pack_ftp']['username'], package_crypt($_SESSION['pack_ftp']['password']));
  648. }
  649. // Just got a submission did we?
  650. if (empty($package_ftp) && isset($_POST['ftp_username']))
  651. {
  652. loadClassFile('Class-Package.php');
  653. $ftp = new ftp_connection($_POST['ftp_server'], $_POST['ftp_port'], $_POST['ftp_username'], $_POST['ftp_password']);
  654. // We're connected, jolly good!
  655. if ($ftp->error === false)
  656. {
  657. // Common mistake, so let's try to remedy it...
  658. if (!$ftp->chdir($_POST['ftp_path']))
  659. {
  660. $ftp_error = $ftp->last_message;
  661. $ftp->chdir(preg_replace('~^/home[2]?/[^/]+?~', '', $_POST['ftp_path']));
  662. }
  663. if (!in_array($_POST['ftp_path'], array('', '/')))
  664. {
  665. $ftp_root = strtr($boarddir, array($_POST['ftp_path'] => ''));
  666. if (substr($ftp_root, -1) == '/' && ($_POST['ftp_path'] == '' || substr($_POST['ftp_path'], 0, 1) == '/'))
  667. $ftp_root = substr($ftp_root, 0, -1);
  668. }
  669. else
  670. $ftp_root = $boarddir;
  671. $_SESSION['pack_ftp'] = array(
  672. 'server' => $_POST['ftp_server'],
  673. 'port' => $_POST['ftp_port'],
  674. 'username' => $_POST['ftp_username'],
  675. 'password' => package_crypt($_POST['ftp_password']),
  676. 'path' => $_POST['ftp_path'],
  677. 'root' => $ftp_root,
  678. 'connected' => true,
  679. );
  680. if (!isset($modSettings['package_path']) || $modSettings['package_path'] != $_POST['ftp_path'])
  681. updateSettings(array('package_path' => $_POST['ftp_path']));
  682. // This is now the primary connection.
  683. $package_ftp = $ftp;
  684. }
  685. }
  686. // Now try to simply make the files writable, with whatever we might have.
  687. if (!empty($chmodFiles))
  688. {
  689. foreach ($chmodFiles as $k => $file)
  690. {
  691. // Sometimes this can somehow happen maybe?
  692. if (empty($file))
  693. unset($chmodFiles[$k]);
  694. // Already writable?
  695. elseif (@is_writable($file))
  696. $return_data['files']['writable'][] = $file;
  697. else
  698. {
  699. // Now try to change that.
  700. $return_data['files'][package_chmod($file, 'writable', true) ? 'writable' : 'notwritable'][] = $file;
  701. }
  702. }
  703. }
  704. // Have we still got nasty files which ain't writable? Dear me we need more FTP good sir.
  705. if (empty($package_ftp) && (!empty($return_data['files']['notwritable']) || !empty($chmodOptions['force_find_error'])))
  706. {
  707. if (!isset($ftp) || $ftp->error !== false)
  708. {
  709. if (!isset($ftp))
  710. {
  711. loadClassFile('Class-Package.php');
  712. $ftp = new ftp_connection(null);
  713. }
  714. elseif ($ftp->error !== false && !isset($ftp_error))
  715. $ftp_error = $ftp->last_message === null ? '' : $ftp->last_message;
  716. list ($username, $detect_path, $found_path) = $ftp->detect_path($boarddir);
  717. if ($found_path)
  718. $_POST['ftp_path'] = $detect_path;
  719. elseif (!isset($_POST['ftp_path']))
  720. $_POST['ftp_path'] = isset($modSettings['package_path']) ? $modSettings['package_path'] : $detect_path;
  721. if (!isset($_POST['ftp_username']))
  722. $_POST['ftp_username'] = $username;
  723. }
  724. $context['package_ftp'] = array(
  725. 'server' => isset($_POST['ftp_server']) ? $_POST['ftp_server'] : (isset($modSettings['package_server']) ? $modSettings['package_server'] : 'localhost'),
  726. 'port' => isset($_POST['ftp_port']) ? $_POST['ftp_port'] : (isset($modSettings['package_port']) ? $modSettings['package_port'] : '21'),
  727. 'username' => isset($_POST['ftp_username']) ? $_POST['ftp_username'] : (isset($modSettings['package_username']) ? $modSettings['package_username'] : ''),
  728. 'path' => $_POST['ftp_path'],
  729. 'error' => empty($ftp_error) ? null : $ftp_error,
  730. 'destination' => !empty($chmodOptions['destination_url']) ? $chmodOptions['destination_url'] : '',
  731. );
  732. // Which files failed?
  733. if (!isset($context['notwritable_files']))
  734. $context['notwritable_files'] = array();
  735. $context['notwritable_files'] = array_merge($context['notwritable_files'], $return_data['files']['notwritable']);
  736. // Sent here to die?
  737. if (!empty($chmodOptions['crash_on_error']))
  738. {
  739. $context['page_title'] = $txt['package_ftp_necessary'];
  740. $context['sub_template'] = 'ftp_required';
  741. obExit();
  742. }
  743. }
  744. return $return_data;
  745. }
  746. function packageRequireFTP($destination_url, $files = null, $return = false)
  747. {
  748. global $context, $modSettings, $package_ftp, $boarddir, $txt;
  749. // Try to make them writable the manual way.
  750. if ($files !== null)
  751. {
  752. foreach ($files as $k => $file)
  753. {
  754. // If this file doesn't exist, then we actually want to look at the directory, no?
  755. if (!file_exists($file))
  756. $file = dirname($file);
  757. // This looks odd, but it's an attempt to work around PHP suExec.
  758. if (!@is_writable($file))
  759. @chmod($file, 0755);
  760. if (!@is_writable($file))
  761. @chmod($file, 0777);
  762. if (!@is_writable(dirname($file)))
  763. @chmod($file, 0755);
  764. if (!@is_writable(dirname($file)))
  765. @chmod($file, 0777);
  766. $fp = is_dir($file) ? @opendir($file) : @fopen($file, 'rb');
  767. if (@is_writable($file) && $fp)
  768. {
  769. unset($files[$k]);
  770. if (!is_dir($file))
  771. fclose($fp);
  772. else
  773. closedir($fp);
  774. }
  775. }
  776. // No FTP required!
  777. if (empty($files))
  778. return array();
  779. }
  780. // They've opted to not use FTP, and try anyway.
  781. if (isset($_SESSION['pack_ftp']) && $_SESSION['pack_ftp'] == false)
  782. {
  783. if ($files === null)
  784. return array();
  785. foreach ($files as $k => $file)
  786. {
  787. // This looks odd, but it's an attempt to work around PHP suExec.
  788. if (!file_exists($file))
  789. {
  790. mktree(dirname($file), 0755);
  791. @touch($file);
  792. @chmod($file, 0755);
  793. }
  794. if (!@is_writable($file))
  795. @chmod($file, 0777);
  796. if (!@is_writable(dirname($file)))
  797. @chmod(dirname($file), 0777);
  798. if (@is_writable($file))
  799. unset($files[$k]);
  800. }
  801. return $files;
  802. }
  803. elseif (isset($_SESSION['pack_ftp']))
  804. {
  805. // Load the file containing the ftp_connection class.
  806. loadClassFile('Class-Package.php');
  807. $package_ftp = new ftp_connection($_SESSION['pack_ftp']['server'], $_SESSION['pack_ftp']['port'], $_SESSION['pack_ftp']['username'], package_crypt($_SESSION['pack_ftp']['password']));
  808. if ($files === null)
  809. return array();
  810. foreach ($files as $k => $file)
  811. {
  812. $ftp_file = strtr($file, array($_SESSION['pack_ftp']['root'] => ''));
  813. // This looks odd, but it's an attempt to work around PHP suExec.
  814. if (!file_exists($file))
  815. {
  816. mktree(dirname($file), 0755);
  817. $package_ftp->create_file($ftp_file);
  818. $package_ftp->chmod($ftp_file, 0755);
  819. }
  820. if (!@is_writable($file))
  821. $package_ftp->chmod($ftp_file, 0777);
  822. if (!@is_writable(dirname($file)))
  823. $package_ftp->chmod(dirname($ftp_file), 0777);
  824. if (@is_writable($file))
  825. unset($files[$k]);
  826. }
  827. return $files;
  828. }
  829. if (isset($_POST['ftp_none']))
  830. {
  831. $_SESSION['pack_ftp'] = false;
  832. $files = packageRequireFTP($destination_url, $files, $return);
  833. return $files;
  834. }
  835. elseif (isset($_POST['ftp_username']))
  836. {
  837. loadClassFile('Class-Package.php');
  838. $ftp = new ftp_connection($_POST['ftp_server'], $_POST['ftp_port'], $_POST['ftp_username'], $_POST['ftp_password']);
  839. if ($ftp->error === false)
  840. {
  841. // Common mistake, so let's try to remedy it...
  842. if (!$ftp->chdir($_POST['ftp_path']))
  843. {
  844. $ftp_error = $ftp->last_message;
  845. $ftp->chdir(preg_replace('~^/home[2]?/[^/]+?~', '', $_POST['ftp_path']));
  846. }
  847. }
  848. }
  849. if (!isset($ftp) || $ftp->error !== false)
  850. {
  851. if (!isset($ftp))
  852. {
  853. loadClassFile('Class-Package.php');
  854. $ftp = new ftp_connection(null);
  855. }
  856. elseif ($ftp->error !== false && !isset($ftp_error))
  857. $ftp_error = $ftp->last_message === null ? '' : $ftp->last_message;
  858. list ($username, $detect_path, $found_path) = $ftp->detect_path($boarddir);
  859. if ($found_path)
  860. $_POST['ftp_path'] = $detect_path;
  861. elseif (!isset($_POST['ftp_path']))
  862. $_POST['ftp_path'] = isset($modSettings['package_path']) ? $modSettings['package_path'] : $detect_path;
  863. if (!isset($_POST['ftp_username']))
  864. $_POST['ftp_username'] = $username;
  865. $context['package_ftp'] = array(
  866. 'server' => isset($_POST['ftp_server']) ? $_POST['ftp_server'] : (isset($modSettings['package_server']) ? $modSettings['package_server'] : 'localhost'),
  867. 'port' => isset($_POST['ftp_port']) ? $_POST['ftp_port'] : (isset($modSettings['package_port']) ? $modSettings['package_port'] : '21'),
  868. 'username' => isset($_POST['ftp_username']) ? $_POST['ftp_username'] : (isset($modSettings['package_username']) ? $modSettings['package_username'] : ''),
  869. 'path' => $_POST['ftp_path'],
  870. 'error' => empty($ftp_error) ? null : $ftp_error,
  871. 'destination' => $destination_url,
  872. );
  873. // If we're returning dump out here.
  874. if ($return)
  875. return $files;
  876. $context['page_title'] = $txt['package_ftp_necessary'];
  877. $context['sub_template'] = 'ftp_required';
  878. obExit();
  879. }
  880. else
  881. {
  882. if (!in_array($_POST['ftp_path'], array('', '/')))
  883. {
  884. $ftp_root = strtr($boarddir, array($_POST['ftp_path'] => ''));
  885. if (substr($ftp_root, -1) == '/' && ($_POST['ftp_path'] == '' || substr($_POST['ftp_path'], 0, 1) == '/'))
  886. $ftp_root = substr($ftp_root, 0, -1);
  887. }
  888. else
  889. $ftp_root = $boarddir;
  890. $_SESSION['pack_ftp'] = array(
  891. 'server' => $_POST['ftp_server'],
  892. 'port' => $_POST['ftp_port'],
  893. 'username' => $_POST['ftp_username'],
  894. 'password' => package_crypt($_POST['ftp_password']),
  895. 'path' => $_POST['ftp_path'],
  896. 'root' => $ftp_root,
  897. );
  898. if (!isset($modSettings['package_path']) || $modSettings['package_path'] != $_POST['ftp_path'])
  899. updateSettings(array('package_path' => $_POST['ftp_path']));
  900. $files = packageRequireFTP($destination_url, $files, $return);
  901. }
  902. return $files;
  903. }
  904. // Parses a package-info.xml file - method can be 'install', 'upgrade', or 'uninstall'.
  905. function parsePackageInfo(&$packageXML, $testing_only = true, $method = 'install', $previous_version = '')
  906. {
  907. global $boarddir, $forum_version, $context, $temp_path, $language;
  908. // Mayday! That action doesn't exist!!
  909. if (empty($packageXML) || !$packageXML->exists($method))
  910. return array();
  911. // We haven't found the package script yet...
  912. $script = false;
  913. $the_version = strtr($forum_version, array('SMF ' => ''));
  914. // Emulation support...
  915. if (!empty($_SESSION['version_emulate']))
  916. $the_version = $_SESSION['version_emulate'];
  917. // Get all the versions of this method and find the right one.
  918. $these_methods = $packageXML->set($method);
  919. foreach ($these_methods as $this_method)
  920. {
  921. // They specified certain versions this part is for.
  922. if ($this_method->exists('@for'))
  923. {
  924. // Don't keep going if this won't work for this version of SMF.
  925. if (!matchPackageVersion($the_version, $this_method->fetch('@for')))
  926. continue;
  927. }
  928. // Upgrades may go from a certain old version of the mod.
  929. if ($method == 'upgrade' && $this_method->exists('@from'))
  930. {
  931. // Well, this is for the wrong old version...
  932. if (!matchPackageVersion($previous_version, $this_method->fetch('@from')))
  933. continue;
  934. }
  935. // We've found it!
  936. $script = $this_method;
  937. break;
  938. }
  939. // Bad news, a matching script wasn't found!
  940. if ($script === false)
  941. return array();
  942. // Find all the actions in this method - in theory, these should only be allowed actions. (* means all.)
  943. $actions = $script->set('*');
  944. $return = array();
  945. $temp_auto = 0;
  946. $temp_path = $boarddir . '/Packages/temp/' . (isset($context['base_path']) ? $context['base_path'] : '');
  947. $context['readmes'] = array();
  948. // This is the testing phase... nothing shall be done yet.
  949. foreach ($actions as $action)
  950. {
  951. $actionType = $action->name();
  952. if ($actionType == 'readme' || $actionType == 'code' || $actionType == 'database' || $actionType == 'modification' || $actionType == 'redirect')
  953. {
  954. // Allow for translated readme files.
  955. if ($actionType == 'readme')
  956. {
  957. if ($action->exists('@lang'))
  958. {
  959. // Auto-select a readme language based on either request variable or current language.
  960. if ((isset($_REQUEST['readme']) && $action->fetch('@lang') == $_REQUEST['readme']) || (!isset($_REQUEST['readme']) && $action->fetch('@lang') == $language))
  961. {
  962. // In case the user put the readme blocks in the wrong order.
  963. if (isset($context['readmes']['selected']) && $context['readmes']['selected'] == 'default')
  964. $context['readmes'][] = 'default';
  965. $context['readmes']['selected'] = htmlspecialchars($action->fetch('@lang'));
  966. }
  967. else
  968. {
  969. // We don't want this readme now, but we'll allow the user to select to read it.
  970. $context['readmes'][] = htmlspecialchars($action->fetch('@lang'));
  971. continue;
  972. }
  973. }
  974. // Fallback readme. Without lang parameter.
  975. else
  976. {
  977. // Already selected a readme.
  978. if (isset($context['readmes']['selected']))
  979. {
  980. $context['readmes'][] = 'default';
  981. continue;
  982. }
  983. else
  984. $context['readmes']['selected'] = 'default';
  985. }
  986. }
  987. // !!! TODO: Make sure the file actually exists? Might not work when testing?
  988. if ($action->exists('@type') && $action->fetch('@type') == 'inline')
  989. {
  990. $filename = $temp_path . '$auto_' . $temp_auto++ . ($actionType == 'readme' || $actionType == 'redirect' ? '.txt' : ($actionType == 'code' || $actionType == 'database' ? '.php' : '.mod'));
  991. package_put_contents($filename, $action->fetch('.'));
  992. $filename = strtr($filename, array($temp_path => ''));
  993. }
  994. else
  995. $filename = $action->fetch('.');
  996. $return[] = array(
  997. 'type' => $actionType,
  998. 'filename' => $filename,
  999. 'description' => '',
  1000. 'reverse' => $action->exists('@reverse') && $action->fetch('@reverse') == 'true',
  1001. 'boardmod' => $action->exists('@format') && $action->fetch('@format') == 'boardmod',
  1002. 'redirect_url' => $action->exists('@url') ? $action->fetch('@url') : '',
  1003. 'redirect_timeout' => $action->exists('@timeout') ? (int) $action->fetch('@timeout') : '',
  1004. 'parse_bbc' => $action->exists('@parsebbc') && $action->fetch('@parsebbc') == 'true',
  1005. 'language' => ($actionType == 'readme' && $action->exists('@lang') && $action->fetch('@lang') == $language) ? $language : '',
  1006. );
  1007. continue;
  1008. }
  1009. elseif ($actionType == 'error')
  1010. {
  1011. $return[] = array(
  1012. 'type' => 'error',
  1013. );
  1014. }
  1015. $this_action = &$return[];
  1016. $this_action = array(
  1017. 'type' => $actionType,
  1018. 'filename' => $action->fetch('@name'),
  1019. 'description' => $action->fetch('.')
  1020. );
  1021. // If there is a destination, make sure it makes sense.
  1022. if (substr($actionType, 0, 6) != 'remove')
  1023. {
  1024. $this_action['unparsed_destination'] = $action->fetch('@destination');
  1025. $this_action['destination'] = parse_path($action->fetch('@destination')) . '/' . basename($this_action['filename']);
  1026. }
  1027. else
  1028. {
  1029. $this_action['unparsed_filename'] = $this_action['filename'];
  1030. $this_action['filename'] = parse_path($this_action['filename']);
  1031. }
  1032. // If we're moving or requiring (copying) a file.
  1033. if (substr($actionType, 0, 4) == 'move' || substr($actionType, 0, 7) == 'require')
  1034. {
  1035. if ($action->exists('@from'))
  1036. $this_action['source'] = parse_path($action->fetch('@from'));
  1037. else
  1038. $this_action['source'] = $temp_path . $this_action['filename'];
  1039. }
  1040. // Check if these things can be done. (chmod's etc.)
  1041. if ($actionType == 'create-dir')
  1042. {
  1043. if (!mktree($this_action['destination'], false))
  1044. {
  1045. $temp = $this_action['destination'];
  1046. while (!file_exists($temp) && strlen($temp) > 1)
  1047. $temp = dirname($temp);
  1048. $return[] = array(
  1049. 'type' => 'chmod',
  1050. 'filename' => $temp
  1051. );
  1052. }
  1053. }
  1054. elseif ($actionType == 'create-file')
  1055. {
  1056. if (!mktree(dirname($this_action['destination']), false))
  1057. {
  1058. $temp = dirname($this_action['destination']);
  1059. while (!file_exists($temp) && strlen($temp) > 1)
  1060. $temp = dirname($temp);
  1061. $return[] = array(
  1062. 'type' => 'chmod',
  1063. 'filename' => $temp
  1064. );
  1065. }
  1066. if (!is_writable($this_action['destination']) && (file_exists($this_action['destination']) || !is_writable(dirname($this_action['destination']))))
  1067. $return[] = array(
  1068. 'type' => 'chmod',
  1069. 'filename' => $this_action['destination']
  1070. );
  1071. }
  1072. elseif ($actionType == 'require-dir')
  1073. {
  1074. if (!mktree($this_action['destination'], false))
  1075. {
  1076. $temp = $this_action['destination'];
  1077. while (!file_exists($temp) && strlen($temp) > 1)
  1078. $temp = dirname($temp);
  1079. $return[] = array(
  1080. 'type' => 'chmod',
  1081. 'filename' => $temp
  1082. );
  1083. }
  1084. }
  1085. elseif ($actionType == 'require-file')
  1086. {
  1087. if ($action->exists('@theme'))
  1088. $this_action['theme_action'] = $action->fetch('@theme');
  1089. if (!mktree(dirname($this_action['destination']), false))
  1090. {
  1091. $temp = dirname($this_action['destination']);
  1092. while (!file_exists($temp) && strlen($temp) > 1)
  1093. $temp = dirname($temp);
  1094. $return[] = array(
  1095. 'type' => 'chmod',
  1096. 'filename' => $temp
  1097. );
  1098. }
  1099. if (!is_writable($this_action['destination']) && (file_exists($this_action['destination']) || !is_writable(dirname($this_action['destination']))))
  1100. $return[] = array(
  1101. 'type' => 'chmod',
  1102. 'filename' => $this_action['destination']
  1103. );
  1104. }
  1105. elseif ($actionType == 'move-dir' || $actionType == 'move-file')
  1106. {
  1107. if (!mktree(dirname($this_action['destination']), false))
  1108. {
  1109. $temp = dirname($this_action['destination']);
  1110. while (!file_exists($temp) && strlen($temp) > 1)
  1111. $temp = dirname($temp);
  1112. $return[] = array(
  1113. 'type' => 'chmod',
  1114. 'filename' => $temp
  1115. );
  1116. }
  1117. if (!is_writable($this_action['destination']) && (file_exists($this_action['destination']) || !is_writable(dirname($this_action['destination']))))
  1118. $return[] = array(
  1119. 'type' => 'chmod',
  1120. 'filename' => $this_action['destination']
  1121. );
  1122. }
  1123. elseif ($actionType == 'remove-dir')
  1124. {
  1125. if (!is_writable($this_action['filename']) && file_exists($this_action['destination']))
  1126. $return[] = array(
  1127. 'type' => 'chmod',
  1128. 'filename' => $this_action['filename']
  1129. );
  1130. }
  1131. elseif ($actionType == 'remove-file')
  1132. {
  1133. if (!is_writable($this_action['filename']) && file_exists($this_action['filename']))
  1134. $return[] = array(
  1135. 'type' => 'chmod',
  1136. 'filename' => $this_action['filename']
  1137. );
  1138. }
  1139. }
  1140. // Only testing - just return a list of things to be done.
  1141. if ($testing_only)
  1142. return $return;
  1143. umask(0);
  1144. $failure = false;
  1145. $not_done = array(array('type' => '!'));
  1146. foreach ($return as $action)
  1147. {
  1148. if ($action['type'] == 'modification' || $action['type'] == 'code' || $action['type'] == 'database' || $action['type'] == 'redirect')
  1149. $not_done[] = $action;
  1150. if ($action['type'] == 'create-dir')
  1151. {
  1152. if (!mktree($action['destination'], 0755) || !is_writable($action['destination']))
  1153. $failure |= !mktree($action['destination'], 0777);
  1154. }
  1155. elseif ($action['type'] == 'create-file')
  1156. {
  1157. if (!mktree(dirname($action['destination']), 0755) || !is_writable(dirname($action['destination'])))
  1158. $failure |= !mktree(dirname($action['destination']), 0777);
  1159. // Create an empty file.
  1160. package_put_contents($action['destination'], package_get_contents($action['source']), $testing_only);
  1161. if (!file_exists($action['destination']))
  1162. $failure = true;
  1163. }
  1164. elseif ($action['type'] == 'require-dir')
  1165. {
  1166. copytree($action['source'], $action['destination']);
  1167. // Any other theme folders?
  1168. if (!empty($context['theme_copies']) && !empty($context['theme_copies'][$action['type']][$action['destination']]))
  1169. foreach ($context['theme_copies'][$action['type']][$action['destination']] as $theme_destination)
  1170. copytree($action['source'], $theme_destination);
  1171. }
  1172. elseif ($action['type'] == 'require-file')
  1173. {
  1174. if (!mktree(dirname($action['destination']), 0755) || !is_writable(dirname($action['destination'])))
  1175. $failure |= !mktree(dirname($action['destination']), 0777);
  1176. package_put_contents($action['destination'], package_get_contents($action['source']), $testing_only);
  1177. $failure |= !copy($action['source'], $action['destination']);
  1178. // Any other theme files?
  1179. if (!empty($context['theme_copies']) && !empty($context['theme_copies'][$action['type']][$action['destination']]))
  1180. foreach ($context['theme_copies'][$action['type']][$action['destination']] as $theme_destination)
  1181. {
  1182. if (!mktree(dirname($theme_destination), 0755) || !is_writable(dirname($theme_destination)))
  1183. $failure |= !mktree(dirname($theme_destination), 0777);
  1184. package_put_contents($theme_destination, package_get_contents($action['source']), $testing_only);
  1185. $failure |= !copy($action['source'], $theme_destination);
  1186. }
  1187. }
  1188. elseif ($action['type'] == 'move-file')
  1189. {
  1190. if (!mktree(dirname($action['destination']), 0755) || !is_writable(dirname($action['destination'])))
  1191. $failure |= !mktree(dirname($action['destination']), 0777);
  1192. $failure |= !rename($action['source'], $action['destination']);
  1193. }
  1194. elseif ($action['type'] == 'move-dir')
  1195. {
  1196. if (!mktree($action['destination'], 0755) || !is_writable($action['destination']))
  1197. $failure |= !mktree($action['destination'], 0777);
  1198. $failure |= !rename($action['source'], $action['destination']);
  1199. }
  1200. elseif ($action['type'] == 'remove-dir')
  1201. {
  1202. deltree($action['filename']);
  1203. // Any other theme folders?
  1204. if (!empty($context['theme_copies']) && !empty($context['theme_copies'][$action['type']][$action['filename']]))
  1205. foreach ($context['theme_copies'][$action['type']][$action['filename']] as $theme_destination)
  1206. deltree($theme_destination);
  1207. }
  1208. elseif ($action['type'] == 'remove-file')
  1209. {
  1210. // Make sure the file exists before deleting it.
  1211. if (file_exists($action['filename']))
  1212. {
  1213. package_chmod($action['filename']);
  1214. $failure |= !unlink($action['filename']);
  1215. }
  1216. // The file that was supposed to be deleted couldn't be found.
  1217. else
  1218. $failure = true;
  1219. // Any other theme folders?
  1220. if (!empty($context['theme_copies']) && !empty($context['theme_copies'][$action['type']][$action['filename']]))
  1221. foreach ($context['theme_copies'][$action['type']][$action['filename']] as $theme_destination)
  1222. if (file_exists($theme_destination))
  1223. $failure |= !unlink($theme_destination);
  1224. else
  1225. $failure = true;
  1226. }
  1227. }
  1228. return $not_done;
  1229. }
  1230. // This function tries to match $version into any of the ranges given in $versions
  1231. function matchPackageVersion($version, $versions)
  1232. {
  1233. // Make sure everything is lowercase and clean of spaces and unpleasant history.
  1234. $version = str_replace(array(' ', '2.0rc1-1'), array('', '2.0rc1.1'), strtolower($version));
  1235. $versions = explode(',', str_replace(array(' ', '2.0rc1-1'), array('', '2.0rc1.1'), strtolower($versions)));
  1236. // Perhaps we do accept anything?
  1237. if (in_array('all', $versions))
  1238. return true;
  1239. // Loop through each version.
  1240. foreach ($versions as $for)
  1241. {
  1242. // Wild card spotted?
  1243. if (strpos($for, '*') !== false)
  1244. $for = str_replace('*', '0dev0', $for) . '-' . str_replace('*', '999', $for);
  1245. // Do we have a range?
  1246. if (strpos($for, '-') !== false)
  1247. {
  1248. list ($lower, $upper) = explode('-', $for);
  1249. // Compare the version against lower and upper bounds.
  1250. if (compareVersions($version, $lower) > -1 && compareVersions($version, $upper) < 1)
  1251. return true;
  1252. }
  1253. // Otherwise check if they are equal...
  1254. elseif (compareVersions($version, $for) === 0)
  1255. return true;
  1256. }
  1257. return false;
  1258. }
  1259. // The geek version of versioning checks for dummies, which basically compares two versions.
  1260. function compareVersions($version1, $version2)
  1261. {
  1262. static $categories;
  1263. $versions = array();
  1264. foreach (array(1 => $version1, $version2) as $id => $version)
  1265. {
  1266. // Clean the version and extract the version parts.
  1267. $clean = str_replace(array(' ', '2.0rc1-1'), array('', '2.0rc1.1'), strtolower($version));
  1268. preg_match('~(\d+)(?:\.(\d+|))?(?:\.)?(\d+|)(?:(alpha|beta|rc)(\d+|)(?:\.)?(\d+|))?(?:(dev))?(\d+|)~', $clean, $parts);
  1269. // Build an array of parts.
  1270. $versions[$id] = array(
  1271. 'major' => (int) $parts[1],
  1272. 'minor' => !empty($parts[2]) ? (int) $parts[2] : 0,
  1273. 'patch' => !empty($parts[3]) ? (int) $parts[3] : 0,
  1274. 'type' => empty($parts[4]) ? 'stable' : $parts[4],
  1275. 'type_major' => !empty($parts[6]) ? (int) $parts[5] : 0,
  1276. 'type_minor' => !empty($parts[6]) ? (int) $parts[6] : 0,
  1277. 'dev' => !empty($parts[7]),
  1278. );
  1279. }
  1280. // Are they the same, perhaps?
  1281. if ($versions[1] ===

Large files files are truncated, but you can click here to view the full file