Packages.php 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278
  1. <?php
  2. /**
  3. * This file is the main Package Manager.
  4. *
  5. * Simple Machines Forum (SMF)
  6. *
  7. * @package SMF
  8. * @author Simple Machines http://www.simplemachines.org
  9. * @copyright 2011 Simple Machines
  10. * @license http://www.simplemachines.org/about/smf/license.php BSD
  11. *
  12. * @version 2.1 Alpha 1
  13. */
  14. if (!defined('SMF'))
  15. die('Hacking attempt...');
  16. /**
  17. * This is the notoriously defunct package manager..... :/.
  18. */
  19. function Packages()
  20. {
  21. global $txt, $scripturl, $sourcedir, $context;
  22. // @todo Remove this!
  23. if (isset($_GET['get']) || isset($_GET['pgdownload']))
  24. {
  25. require_once($sourcedir . '/PackageGet.php');
  26. return PackageGet();
  27. }
  28. isAllowedTo('admin_forum');
  29. // Load all the basic stuff.
  30. require_once($sourcedir . '/Subs-Package.php');
  31. loadLanguage('Packages');
  32. loadTemplate('Packages', 'admin');
  33. $context['page_title'] = $txt['package'];
  34. // Delegation makes the world... that is, the package manager go 'round.
  35. $subActions = array(
  36. 'browse' => 'PackageBrowse',
  37. 'remove' => 'PackageRemove',
  38. 'list' => 'PackageList',
  39. 'ftptest' => 'PackageFTPTest',
  40. 'install' => 'PackageInstallTest',
  41. 'install2' => 'PackageInstall',
  42. 'uninstall' => 'PackageInstallTest',
  43. 'uninstall2' => 'PackageInstall',
  44. 'installed' => 'InstalledList',
  45. 'options' => 'PackageOptions',
  46. 'perms' => 'PackagePermissions',
  47. 'flush' => 'FlushInstall',
  48. 'examine' => 'ExamineFile',
  49. 'showoperations' => 'ViewOperations',
  50. );
  51. // Work out exactly who it is we are calling.
  52. if (isset($_REQUEST['sa']) && isset($subActions[$_REQUEST['sa']]))
  53. $context['sub_action'] = $_REQUEST['sa'];
  54. else
  55. $context['sub_action'] = 'browse';
  56. // Set up some tabs...
  57. $context[$context['admin_menu_name']]['tab_data'] = array(
  58. 'title' => $txt['package_manager'],
  59. // @todo 'help' => 'registrations',
  60. 'description' => $txt['package_manager_desc'],
  61. 'tabs' => array(
  62. 'browse' => array(
  63. ),
  64. 'packageget' => array(
  65. 'description' => $txt['download_packages_desc'],
  66. ),
  67. 'installed' => array(
  68. 'description' => $txt['installed_packages_desc'],
  69. ),
  70. 'perms' => array(
  71. 'description' => $txt['package_file_perms_desc'],
  72. ),
  73. 'options' => array(
  74. 'description' => $txt['package_install_options_ftp_why'],
  75. ),
  76. ),
  77. );
  78. // Call the function we're handing control to.
  79. $subActions[$context['sub_action']]();
  80. }
  81. /**
  82. * Test install a package.
  83. */
  84. function PackageInstallTest()
  85. {
  86. global $boarddir, $txt, $context, $scripturl, $sourcedir, $modSettings, $smcFunc, $settings;
  87. // You have to specify a file!!
  88. if (!isset($_REQUEST['package']) || $_REQUEST['package'] == '')
  89. redirectexit('action=admin;area=packages');
  90. $context['filename'] = preg_replace('~[\.]+~', '.', $_REQUEST['package']);
  91. // Do we have an existing id, for uninstalls and the like.
  92. $context['install_id'] = isset($_REQUEST['pid']) ? (int) $_REQUEST['pid'] : 0;
  93. require_once($sourcedir . '/Subs-Package.php');
  94. // Load up the package FTP information?
  95. create_chmod_control();
  96. // Make sure temp directory exists and is empty.
  97. if (file_exists($boarddir . '/Packages/temp'))
  98. deltree($boarddir . '/Packages/temp', false);
  99. if (!mktree($boarddir . '/Packages/temp', 0755))
  100. {
  101. deltree($boarddir . '/Packages/temp', false);
  102. if (!mktree($boarddir . '/Packages/temp', 0777))
  103. {
  104. deltree($boarddir . '/Packages/temp', false);
  105. create_chmod_control(array($boarddir . '/Packages/temp/delme.tmp'), array('destination_url' => $scripturl . '?action=admin;area=packages;sa=' . $_REQUEST['sa'] . ';package=' . $_REQUEST['package'], 'crash_on_error' => true));
  106. deltree($boarddir . '/Packages/temp', false);
  107. if (!mktree($boarddir . '/Packages/temp', 0777))
  108. fatal_lang_error('package_cant_download', false);
  109. }
  110. }
  111. $context['uninstalling'] = $_REQUEST['sa'] == 'uninstall';
  112. // Change our last link tree item for more information on this Packages area.
  113. $context['linktree'][count($context['linktree']) - 1] = array(
  114. 'url' => $scripturl . '?action=admin;area=packages;sa=browse',
  115. 'name' => $context['uninstalling'] ? $txt['package_uninstall_actions'] : $txt['install_actions']
  116. );
  117. $context['page_title'] .= ' - ' . ($context['uninstalling'] ? $txt['package_uninstall_actions'] : $txt['install_actions']);
  118. $context['sub_template'] = 'view_package';
  119. if (!file_exists($boarddir . '/Packages/' . $context['filename']))
  120. {
  121. deltree($boarddir . '/Packages/temp');
  122. fatal_lang_error('package_no_file', false);
  123. }
  124. // Extract the files so we can get things like the readme, etc.
  125. if (is_file($boarddir . '/Packages/' . $context['filename']))
  126. {
  127. $context['extracted_files'] = read_tgz_file($boarddir . '/Packages/' . $context['filename'], $boarddir . '/Packages/temp');
  128. if ($context['extracted_files'] && !file_exists($boarddir . '/Packages/temp/package-info.xml'))
  129. foreach ($context['extracted_files'] as $file)
  130. if (basename($file['filename']) == 'package-info.xml')
  131. {
  132. $context['base_path'] = dirname($file['filename']) . '/';
  133. break;
  134. }
  135. if (!isset($context['base_path']))
  136. $context['base_path'] = '';
  137. }
  138. elseif (is_dir($boarddir . '/Packages/' . $context['filename']))
  139. {
  140. copytree($boarddir . '/Packages/' . $context['filename'], $boarddir . '/Packages/temp');
  141. $context['extracted_files'] = listtree($boarddir . '/Packages/temp');
  142. $context['base_path'] = '';
  143. }
  144. else
  145. fatal_lang_error('no_access', false);
  146. // Load up any custom themes we may want to install into...
  147. $request = $smcFunc['db_query']('', '
  148. SELECT id_theme, variable, value
  149. FROM {db_prefix}themes
  150. WHERE (id_theme = {int:default_theme} OR id_theme IN ({array_int:known_theme_list}))
  151. AND variable IN ({string:name}, {string:theme_dir})',
  152. array(
  153. 'known_theme_list' => explode(',', $modSettings['knownThemes']),
  154. 'default_theme' => 1,
  155. 'name' => 'name',
  156. 'theme_dir' => 'theme_dir',
  157. )
  158. );
  159. $theme_paths = array();
  160. while ($row = $smcFunc['db_fetch_assoc']($request))
  161. $theme_paths[$row['id_theme']][$row['variable']] = $row['value'];
  162. $smcFunc['db_free_result']($request);
  163. // Get the package info...
  164. $packageInfo = getPackageInfo($context['filename']);
  165. if (!is_array($packageInfo))
  166. fatal_lang_error($packageInfo);
  167. $packageInfo['filename'] = $context['filename'];
  168. $context['package_name'] = isset($packageInfo['name']) ? $packageInfo['name'] : $context['filename'];
  169. // Set the type of extraction...
  170. $context['extract_type'] = isset($packageInfo['type']) ? $packageInfo['type'] : 'modification';
  171. // The mod isn't installed.... unless proven otherwise.
  172. $context['is_installed'] = false;
  173. // See if it is installed?
  174. $request = $smcFunc['db_query']('', '
  175. SELECT version, themes_installed, db_changes
  176. FROM {db_prefix}log_packages
  177. WHERE package_id = {string:current_package}
  178. AND install_state != {int:not_installed}
  179. ORDER BY time_installed DESC
  180. LIMIT 1',
  181. array(
  182. 'not_installed' => 0,
  183. 'current_package' => $packageInfo['id'],
  184. )
  185. );
  186. while ($row = $smcFunc['db_fetch_assoc']($request))
  187. {
  188. $old_themes = explode(',', $row['themes_installed']);
  189. $old_version = $row['version'];
  190. $db_changes = empty($row['db_changes']) ? array() : unserialize($row['db_changes']);
  191. }
  192. $smcFunc['db_free_result']($request);
  193. $context['database_changes'] = array();
  194. if (!empty($db_changes))
  195. {
  196. foreach ($db_changes as $change)
  197. {
  198. if (isset($change[2]) && isset($txt['package_db_' . $change[0]]))
  199. $context['database_changes'][] = sprintf($txt['package_db_' . $change[0]], $change[1], $change[2]);
  200. elseif (isset($txt['package_db_' . $change[0]]))
  201. $context['database_changes'][] = sprintf($txt['package_db_' . $change[0]], $change[1]);
  202. else
  203. $context['database_changes'][] = $change[0] . '-' . $change[1] . (isset($change[2]) ? '-' . $change[2] : '');
  204. }
  205. }
  206. // Uninstalling?
  207. if ($context['uninstalling'])
  208. {
  209. // Wait, it's not installed yet!
  210. if (!isset($old_version) && $context['uninstalling'])
  211. {
  212. deltree($boarddir . '/Packages/temp');
  213. fatal_lang_error('package_cant_uninstall', false);
  214. }
  215. $actions = parsePackageInfo($packageInfo['xml'], true, 'uninstall');
  216. // Gadzooks! There's no uninstaller at all!?
  217. if (empty($actions))
  218. {
  219. deltree($boarddir . '/Packages/temp');
  220. fatal_lang_error('package_uninstall_cannot', false);
  221. }
  222. // Can't edit the custom themes it's edited if you're unisntalling, they must be removed.
  223. $context['themes_locked'] = true;
  224. // Only let them uninstall themes it was installed into.
  225. foreach ($theme_paths as $id => $data)
  226. if ($id != 1 && !in_array($id, $old_themes))
  227. unset($theme_paths[$id]);
  228. }
  229. elseif (isset($old_version) && $old_version != $packageInfo['version'])
  230. {
  231. // Look for an upgrade...
  232. $actions = parsePackageInfo($packageInfo['xml'], true, 'upgrade', $old_version);
  233. // There was no upgrade....
  234. if (empty($actions))
  235. $context['is_installed'] = true;
  236. else
  237. {
  238. // Otherwise they can only upgrade themes from the first time around.
  239. foreach ($theme_paths as $id => $data)
  240. if ($id != 1 && !in_array($id, $old_themes))
  241. unset($theme_paths[$id]);
  242. }
  243. }
  244. elseif (isset($old_version) && $old_version == $packageInfo['version'])
  245. $context['is_installed'] = true;
  246. if (!isset($old_version) || $context['is_installed'])
  247. $actions = parsePackageInfo($packageInfo['xml'], true, 'install');
  248. $context['actions'] = array();
  249. $context['ftp_needed'] = false;
  250. $context['has_failure'] = false;
  251. $chmod_files = array();
  252. if (empty($actions))
  253. return;
  254. // This will hold data about anything that can be installed in other themes.
  255. $themeFinds = array(
  256. 'candidates' => array(),
  257. 'other_themes' => array(),
  258. );
  259. // Now prepare things for the template.
  260. foreach ($actions as $action)
  261. {
  262. // Not failed until proven otherwise.
  263. $failed = false;
  264. if ($action['type'] == 'chmod')
  265. {
  266. $chmod_files[] = $action['filename'];
  267. continue;
  268. }
  269. elseif ($action['type'] == 'readme')
  270. {
  271. if (file_exists($boarddir . '/Packages/temp/' . $context['base_path'] . $action['filename']))
  272. $context['package_readme'] = htmlspecialchars(trim(file_get_contents($boarddir . '/Packages/temp/' . $context['base_path'] . $action['filename']), "\n\r"));
  273. elseif (file_exists($action['filename']))
  274. $context['package_readme'] = htmlspecialchars(trim(file_get_contents($action['filename']), "\n\r"));
  275. if (!empty($action['parse_bbc']))
  276. {
  277. require_once($sourcedir . '/Subs-Post.php');
  278. preparsecode($context['package_readme']);
  279. $context['package_readme'] = parse_bbc($context['package_readme']);
  280. }
  281. else
  282. $context['package_readme'] = nl2br($context['package_readme']);
  283. continue;
  284. }
  285. // Don't show redirects.
  286. elseif ($action['type'] == 'redirect')
  287. continue;
  288. elseif ($action['type'] == 'error')
  289. $context['has_failure'] = true;
  290. elseif ($action['type'] == 'modification')
  291. {
  292. if (!file_exists($boarddir . '/Packages/temp/' . $context['base_path'] . $action['filename']))
  293. {
  294. $context['has_failure'] = true;
  295. $context['actions'][] = array(
  296. 'type' => $txt['execute_modification'],
  297. 'action' => $smcFunc['htmlspecialchars'](strtr($action['filename'], array($boarddir => '.'))),
  298. 'description' => $txt['package_action_error'],
  299. 'failed' => true,
  300. );
  301. }
  302. if ($action['boardmod'])
  303. $mod_actions = parseBoardMod(@file_get_contents($boarddir . '/Packages/temp/' . $context['base_path'] . $action['filename']), true, $action['reverse'], $theme_paths);
  304. else
  305. $mod_actions = parseModification(@file_get_contents($boarddir . '/Packages/temp/' . $context['base_path'] . $action['filename']), true, $action['reverse'], $theme_paths);
  306. if (count($mod_actions) == 1 && isset($mod_actions[0]) && $mod_actions[0]['type'] == 'error' && $mod_actions[0]['filename'] == '-')
  307. $mod_actions[0]['filename'] = $action['filename'];
  308. foreach ($mod_actions as $key => $mod_action)
  309. {
  310. // Lets get the last section of the file name.
  311. if (isset($mod_action['filename']) && substr($mod_action['filename'], -13) != '.template.php')
  312. $actual_filename = strtolower(substr(strrchr($mod_action['filename'], '/'), 1) . '||' . $action['filename']);
  313. elseif (isset($mod_action['filename']) && preg_match('~([\w]*)/([\w]*)\.template\.php$~', $mod_action['filename'], $matches))
  314. $actual_filename = strtolower($matches[1] . '/' . $matches[2] . '.template.php' . '||' . $action['filename']);
  315. else
  316. $actual_filename = $key;
  317. if ($mod_action['type'] == 'opened')
  318. $failed = false;
  319. elseif ($mod_action['type'] == 'failure')
  320. {
  321. if (empty($mod_action['is_custom']))
  322. $context['has_failure'] = true;
  323. $failed = true;
  324. }
  325. elseif ($mod_action['type'] == 'chmod')
  326. {
  327. $chmod_files[] = $mod_action['filename'];
  328. }
  329. elseif ($mod_action['type'] == 'saved')
  330. {
  331. if (!empty($mod_action['is_custom']))
  332. {
  333. if (!isset($context['theme_actions'][$mod_action['is_custom']]))
  334. $context['theme_actions'][$mod_action['is_custom']] = array(
  335. 'name' => $theme_paths[$mod_action['is_custom']]['name'],
  336. 'actions' => array(),
  337. 'has_failure' => $failed,
  338. );
  339. else
  340. $context['theme_actions'][$mod_action['is_custom']]['has_failure'] |= $failed;
  341. $context['theme_actions'][$mod_action['is_custom']]['actions'][$actual_filename] = array(
  342. 'type' => $txt['execute_modification'],
  343. 'action' => $smcFunc['htmlspecialchars'](strtr($mod_action['filename'], array($boarddir => '.'))),
  344. 'description' => $failed ? $txt['package_action_failure'] : $txt['package_action_success'],
  345. 'failed' => $failed,
  346. );
  347. }
  348. elseif (!isset($context['actions'][$actual_filename]))
  349. {
  350. $context['actions'][$actual_filename] = array(
  351. 'type' => $txt['execute_modification'],
  352. 'action' => $smcFunc['htmlspecialchars'](strtr($mod_action['filename'], array($boarddir => '.'))),
  353. 'description' => $failed ? $txt['package_action_failure'] : $txt['package_action_success'],
  354. 'failed' => $failed,
  355. );
  356. }
  357. else
  358. {
  359. $context['actions'][$actual_filename]['failed'] |= $failed;
  360. $context['actions'][$actual_filename]['description'] = $context['actions'][$actual_filename]['failed'] ? $txt['package_action_failure'] : $txt['package_action_success'];
  361. }
  362. }
  363. elseif ($mod_action['type'] == 'skipping')
  364. {
  365. $context['actions'][$actual_filename] = array(
  366. 'type' => $txt['execute_modification'],
  367. 'action' => $smcFunc['htmlspecialchars'](strtr($mod_action['filename'], array($boarddir => '.'))),
  368. 'description' => $txt['package_action_skipping']
  369. );
  370. }
  371. elseif ($mod_action['type'] == 'missing' && empty($mod_action['is_custom']))
  372. {
  373. $context['has_failure'] = true;
  374. $context['actions'][$actual_filename] = array(
  375. 'type' => $txt['execute_modification'],
  376. 'action' => $smcFunc['htmlspecialchars'](strtr($mod_action['filename'], array($boarddir => '.'))),
  377. 'description' => $txt['package_action_missing'],
  378. 'failed' => true,
  379. );
  380. }
  381. elseif ($mod_action['type'] == 'error')
  382. $context['actions'][$actual_filename] = array(
  383. 'type' => $txt['execute_modification'],
  384. 'action' => $smcFunc['htmlspecialchars'](strtr($mod_action['filename'], array($boarddir => '.'))),
  385. 'description' => $txt['package_action_error'],
  386. 'failed' => true,
  387. );
  388. }
  389. // We need to loop again just to get the operations down correctly.
  390. foreach ($mod_actions as $operation_key => $mod_action)
  391. {
  392. // Lets get the last section of the file name.
  393. if (isset($mod_action['filename']) && substr($mod_action['filename'], -13) != '.template.php')
  394. $actual_filename = strtolower(substr(strrchr($mod_action['filename'], '/'), 1) . '||' . $action['filename']);
  395. elseif (isset($mod_action['filename']) && preg_match('~([\w]*)/([\w]*)\.template\.php$~', $mod_action['filename'], $matches))
  396. $actual_filename = strtolower($matches[1] . '/' . $matches[2] . '.template.php' . '||' . $action['filename']);
  397. else
  398. $actual_filename = $key;
  399. // We just need it for actual parse changes.
  400. if (!in_array($mod_action['type'], array('error', 'result', 'opened', 'saved', 'end', 'missing', 'skipping', 'chmod')))
  401. {
  402. if (empty($mod_action['is_custom']))
  403. $context['actions'][$actual_filename]['operations'][] = array(
  404. 'type' => $txt['execute_modification'],
  405. 'action' => $smcFunc['htmlspecialchars'](strtr($mod_action['filename'], array($boarddir => '.'))),
  406. 'description' => $mod_action['failed'] ? $txt['package_action_failure'] : $txt['package_action_success'],
  407. 'position' => $mod_action['position'],
  408. 'operation_key' => $operation_key,
  409. 'filename' => $action['filename'],
  410. 'is_boardmod' => $action['boardmod'],
  411. 'failed' => $mod_action['failed'],
  412. 'ignore_failure' => !empty($mod_action['ignore_failure']),
  413. );
  414. // Themes are under the saved type.
  415. if (isset($mod_action['is_custom']) && isset($context['theme_actions'][$mod_action['is_custom']]))
  416. $context['theme_actions'][$mod_action['is_custom']]['actions'][$actual_filename]['operations'][] = array(
  417. 'type' => $txt['execute_modification'],
  418. 'action' => $smcFunc['htmlspecialchars'](strtr($mod_action['filename'], array($boarddir => '.'))),
  419. 'description' => $mod_action['failed'] ? $txt['package_action_failure'] : $txt['package_action_success'],
  420. 'position' => $mod_action['position'],
  421. 'operation_key' => $operation_key,
  422. 'filename' => $action['filename'],
  423. 'is_boardmod' => $action['boardmod'],
  424. 'failed' => $mod_action['failed'],
  425. 'ignore_failure' => !empty($mod_action['ignore_failure']),
  426. );
  427. }
  428. }
  429. // Don't add anything else.
  430. $thisAction = array();
  431. }
  432. elseif ($action['type'] == 'code')
  433. $thisAction = array(
  434. 'type' => $txt['execute_code'],
  435. 'action' => $smcFunc['htmlspecialchars']($action['filename']),
  436. );
  437. elseif ($action['type'] == 'database')
  438. {
  439. $thisAction = array(
  440. 'type' => $txt['execute_database_changes'],
  441. 'action' => $smcFunc['htmlspecialchars']($action['filename']),
  442. );
  443. }
  444. elseif (in_array($action['type'], array('create-dir', 'create-file')))
  445. $thisAction = array(
  446. 'type' => $txt['package_create'] . ' ' . ($action['type'] == 'create-dir' ? $txt['package_tree'] : $txt['package_file']),
  447. 'action' => $smcFunc['htmlspecialchars'](strtr($action['destination'], array($boarddir => '.')))
  448. );
  449. elseif (in_array($action['type'], array('hook')))
  450. {
  451. $action['description'] = !isset($action['hook'], $action['function']) ? $txt['package_action_failure'] : $txt['package_action_success'];
  452. if (!isset($action['hook'], $action['function']))
  453. $context['has_failure'] = true;
  454. $thisAction = array(
  455. 'type' => $action['reverse'] ? $txt['execute_hook_remove'] : $txt['execute_hook_add'],
  456. 'action' => sprintf($txt['execute_hook_action'], $smcFunc['htmlspecialchars']($action['hook'])),
  457. );
  458. }
  459. elseif (in_array($action['type'], array('require-dir', 'require-file')))
  460. {
  461. // Do this one...
  462. $thisAction = array(
  463. 'type' => $txt['package_extract'] . ' ' . ($action['type'] == 'require-dir' ? $txt['package_tree'] : $txt['package_file']),
  464. 'action' => $smcFunc['htmlspecialchars'](strtr($action['destination'], array($boarddir => '.')))
  465. );
  466. // Could this be theme related?
  467. if (!empty($action['unparsed_destination']) && preg_match('~^\$(languagedir|languages_dir|imagesdir|themedir|themes_dir)~i', $action['unparsed_destination'], $matches))
  468. {
  469. // Is the action already stated?
  470. $theme_action = !empty($action['theme_action']) && in_array($action['theme_action'], array('no', 'yes', 'auto')) ? $action['theme_action'] : 'auto';
  471. // If it's not auto do we think we have something we can act upon?
  472. if ($theme_action != 'auto' && !in_array($matches[1], array('languagedir', 'languages_dir', 'imagesdir', 'themedir')))
  473. $theme_action = '';
  474. // ... or if it's auto do we even want to do anything?
  475. elseif ($theme_action == 'auto' && $matches[1] != 'imagesdir')
  476. $theme_action = '';
  477. // So, we still want to do something?
  478. if ($theme_action != '')
  479. $themeFinds['candidates'][] = $action;
  480. // Otherwise is this is going into another theme record it.
  481. elseif ($matches[1] == 'themes_dir')
  482. $themeFinds['other_themes'][] = strtolower(strtr(parse_path($action['unparsed_destination']), array('\\' => '/')) . '/' . basename($action['filename']));
  483. }
  484. }
  485. elseif (in_array($action['type'], array('move-dir', 'move-file')))
  486. $thisAction = array(
  487. 'type' => $txt['package_move'] . ' ' . ($action['type'] == 'move-dir' ? $txt['package_tree'] : $txt['package_file']),
  488. 'action' => $smcFunc['htmlspecialchars'](strtr($action['source'], array($boarddir => '.'))) . ' => ' . $smcFunc['htmlspecialchars'](strtr($action['destination'], array($boarddir => '.')))
  489. );
  490. elseif (in_array($action['type'], array('remove-dir', 'remove-file')))
  491. {
  492. $thisAction = array(
  493. 'type' => $txt['package_delete'] . ' ' . ($action['type'] == 'remove-dir' ? $txt['package_tree'] : $txt['package_file']),
  494. 'action' => $smcFunc['htmlspecialchars'](strtr($action['filename'], array($boarddir => '.')))
  495. );
  496. // Could this be theme related?
  497. if (!empty($action['unparsed_filename']) && preg_match('~^\$(languagedir|languages_dir|imagesdir|themedir|themes_dir)~i', $action['unparsed_filename'], $matches))
  498. {
  499. // Is the action already stated?
  500. $theme_action = !empty($action['theme_action']) && in_array($action['theme_action'], array('no', 'yes', 'auto')) ? $action['theme_action'] : 'auto';
  501. $action['unparsed_destination'] = $action['unparsed_filename'];
  502. // If it's not auto do we think we have something we can act upon?
  503. if ($theme_action != 'auto' && !in_array($matches[1], array('languagedir', 'languages_dir', 'imagesdir', 'themedir')))
  504. $theme_action = '';
  505. // ... or if it's auto do we even want to do anything?
  506. elseif ($theme_action == 'auto' && $matches[1] != 'imagesdir')
  507. $theme_action = '';
  508. // So, we still want to do something?
  509. if ($theme_action != '')
  510. $themeFinds['candidates'][] = $action;
  511. // Otherwise is this is going into another theme record it.
  512. elseif ($matches[1] == 'themes_dir')
  513. $themeFinds['other_themes'][] = strtolower(strtr(parse_path($action['unparsed_filename']), array('\\' => '/')) . '/' . basename($action['filename']));
  514. }
  515. }
  516. if (empty($thisAction))
  517. continue;
  518. // @todo None given?
  519. $thisAction['description'] = isset($action['description']) ? $action['description'] : '';
  520. $context['actions'][] = $thisAction;
  521. }
  522. // Have we got some things which we might want to do "multi-theme"?
  523. if (!empty($themeFinds['candidates']))
  524. {
  525. foreach ($themeFinds['candidates'] as $action_data)
  526. {
  527. // Get the part of the file we'll be dealing with.
  528. preg_match('~^\$(languagedir|languages_dir|imagesdir|themedir)(\\|/)*(.+)*~i', $action_data['unparsed_destination'], $matches);
  529. if ($matches[1] == 'imagesdir')
  530. $path = '/' . basename($settings['default_images_url']);
  531. elseif ($matches[1] == 'languagedir' || $matches[1] == 'languages_dir')
  532. $path = '/languages';
  533. else
  534. $path = '';
  535. if (!empty($matches[3]))
  536. $path .= $matches[3];
  537. if (!$context['uninstalling'])
  538. $path .= '/' . basename($action_data['filename']);
  539. // Loop through each custom theme to note it's candidacy!
  540. foreach ($theme_paths as $id => $theme_data)
  541. {
  542. if (isset($theme_data['theme_dir']) && $id != 1)
  543. {
  544. $real_path = $theme_data['theme_dir'] . $path;
  545. // Confirm that we don't already have this dealt with by another entry.
  546. if (!in_array(strtolower(strtr($real_path, array('\\' => '/'))), $themeFinds['other_themes']))
  547. {
  548. // Check if we will need to chmod this.
  549. if (!mktree(dirname($real_path), false))
  550. {
  551. $temp = dirname($real_path);
  552. while (!file_exists($temp) && strlen($temp) > 1)
  553. $temp = dirname($temp);
  554. $chmod_files[] = $temp;
  555. }
  556. if ($action_data['type'] == 'require-dir' && !is_writable($real_path) && (file_exists($real_path) || !is_writable(dirname($real_path))))
  557. $chmod_files[] = $real_path;
  558. if (!isset($context['theme_actions'][$id]))
  559. $context['theme_actions'][$id] = array(
  560. 'name' => $theme_data['name'],
  561. 'actions' => array(),
  562. );
  563. if ($context['uninstalling'])
  564. $context['theme_actions'][$id]['actions'][] = array(
  565. 'type' => $txt['package_delete'] . ' ' . ($action_data['type'] == 'require-dir' ? $txt['package_tree'] : $txt['package_file']),
  566. 'action' => strtr($real_path, array('\\' => '/', $boarddir => '.')),
  567. 'description' => '',
  568. 'value' => base64_encode(serialize(array('type' => $action_data['type'], 'orig' => $action_data['filename'], 'future' => $real_path, 'id' => $id))),
  569. 'not_mod' => true,
  570. );
  571. else
  572. $context['theme_actions'][$id]['actions'][] = array(
  573. 'type' => $txt['package_extract'] . ' ' . ($action_data['type'] == 'require-dir' ? $txt['package_tree'] : $txt['package_file']),
  574. 'action' => strtr($real_path, array('\\' => '/', $boarddir => '.')),
  575. 'description' => '',
  576. 'value' => base64_encode(serialize(array('type' => $action_data['type'], 'orig' => $action_data['destination'], 'future' => $real_path, 'id' => $id))),
  577. 'not_mod' => true,
  578. );
  579. }
  580. }
  581. }
  582. }
  583. }
  584. // Trash the cache... which will also check permissions for us!
  585. package_flush_cache(true);
  586. if (file_exists($boarddir . '/Packages/temp'))
  587. deltree($boarddir . '/Packages/temp');
  588. if (!empty($chmod_files))
  589. {
  590. $ftp_status = create_chmod_control($chmod_files);
  591. $context['ftp_needed'] = !empty($ftp_status['files']['notwritable']) && !empty($context['package_ftp']);
  592. }
  593. checkSubmitOnce('register');
  594. }
  595. /**
  596. * Apply another type of (avatar, language, etc.) package.
  597. */
  598. function PackageInstall()
  599. {
  600. global $boarddir, $txt, $context, $boardurl, $scripturl, $sourcedir, $modSettings;
  601. global $user_info, $smcFunc;
  602. // Make sure we don't install this mod twice.
  603. checkSubmitOnce('check');
  604. checkSession();
  605. // If there's no file, what are we installing?
  606. if (!isset($_REQUEST['package']) || $_REQUEST['package'] == '')
  607. redirectexit('action=admin;area=packages');
  608. $context['filename'] = $_REQUEST['package'];
  609. // If this is an uninstall, we'll have an id.
  610. $context['install_id'] = isset($_REQUEST['pid']) ? (int) $_REQUEST['pid'] : 0;
  611. require_once($sourcedir . '/Subs-Package.php');
  612. // @todo Perhaps do it in steps, if necessary?
  613. $context['uninstalling'] = $_REQUEST['sa'] == 'uninstall2';
  614. // Set up the linktree for other.
  615. $context['linktree'][count($context['linktree']) - 1] = array(
  616. 'url' => $scripturl . '?action=admin;area=packages;sa=browse',
  617. 'name' => $context['uninstalling'] ? $txt['uninstall'] : $txt['extracting']
  618. );
  619. $context['page_title'] .= ' - ' . ($context['uninstalling'] ? $txt['uninstall'] : $txt['extracting']);
  620. $context['sub_template'] = 'extract_package';
  621. if (!file_exists($boarddir . '/Packages/' . $context['filename']))
  622. fatal_lang_error('package_no_file', false);
  623. // Load up the package FTP information?
  624. create_chmod_control(array(), array('destination_url' => $scripturl . '?action=admin;area=packages;sa=' . $_REQUEST['sa'] . ';package=' . $_REQUEST['package']));
  625. // Make sure temp directory exists and is empty!
  626. if (file_exists($boarddir . '/Packages/temp'))
  627. deltree($boarddir . '/Packages/temp', false);
  628. else
  629. mktree($boarddir . '/Packages/temp', 0777);
  630. // Let the unpacker do the work.
  631. if (is_file($boarddir . '/Packages/' . $context['filename']))
  632. {
  633. $context['extracted_files'] = read_tgz_file($boarddir . '/Packages/' . $context['filename'], $boarddir . '/Packages/temp');
  634. if (!file_exists($boarddir . '/Packages/temp/package-info.xml'))
  635. foreach ($context['extracted_files'] as $file)
  636. if (basename($file['filename']) == 'package-info.xml')
  637. {
  638. $context['base_path'] = dirname($file['filename']) . '/';
  639. break;
  640. }
  641. if (!isset($context['base_path']))
  642. $context['base_path'] = '';
  643. }
  644. elseif (is_dir($boarddir . '/Packages/' . $context['filename']))
  645. {
  646. copytree($boarddir . '/Packages/' . $context['filename'], $boarddir . '/Packages/temp');
  647. $context['extracted_files'] = listtree($boarddir . '/Packages/temp');
  648. $context['base_path'] = '';
  649. }
  650. else
  651. fatal_lang_error('no_access', false);
  652. // Are we installing this into any custom themes?
  653. $custom_themes = array(1);
  654. $known_themes = explode(',', $modSettings['knownThemes']);
  655. if (!empty($_POST['custom_theme']))
  656. {
  657. foreach ($_POST['custom_theme'] as $tid)
  658. if (in_array($tid, $known_themes))
  659. $custom_themes[] = (int) $tid;
  660. }
  661. // Now load up the paths of the themes that we need to know about.
  662. $request = $smcFunc['db_query']('', '
  663. SELECT id_theme, variable, value
  664. FROM {db_prefix}themes
  665. WHERE id_theme IN ({array_int:custom_themes})
  666. AND variable IN ({string:name}, {string:theme_dir})',
  667. array(
  668. 'custom_themes' => $custom_themes,
  669. 'name' => 'name',
  670. 'theme_dir' => 'theme_dir',
  671. )
  672. );
  673. $theme_paths = array();
  674. $themes_installed = array(1);
  675. while ($row = $smcFunc['db_fetch_assoc']($request))
  676. $theme_paths[$row['id_theme']][$row['variable']] = $row['value'];
  677. $smcFunc['db_free_result']($request);
  678. // Are there any theme copying that we want to take place?
  679. $context['theme_copies'] = array(
  680. 'require-file' => array(),
  681. 'require-dir' => array(),
  682. );
  683. if (!empty($_POST['theme_changes']))
  684. {
  685. foreach ($_POST['theme_changes'] as $change)
  686. {
  687. if (empty($change))
  688. continue;
  689. $theme_data = unserialize(base64_decode($change));
  690. if (empty($theme_data['type']))
  691. continue;
  692. $themes_installed[] = $theme_data['id'];
  693. $context['theme_copies'][$theme_data['type']][$theme_data['orig']][] = $theme_data['future'];
  694. }
  695. }
  696. // Get the package info...
  697. $packageInfo = getPackageInfo($context['filename']);
  698. if (!is_array($packageInfo))
  699. fatal_lang_error($packageInfo);
  700. $packageInfo['filename'] = $context['filename'];
  701. // Set the type of extraction...
  702. $context['extract_type'] = isset($packageInfo['type']) ? $packageInfo['type'] : 'modification';
  703. // Create a backup file to roll back to! (but if they do this more than once, don't run it a zillion times.)
  704. if (!empty($modSettings['package_make_full_backups']) && (!isset($_SESSION['last_backup_for']) || $_SESSION['last_backup_for'] != $context['filename'] . ($context['uninstalling'] ? '$$' : '$')))
  705. {
  706. $_SESSION['last_backup_for'] = $context['filename'] . ($context['uninstalling'] ? '$$' : '$');
  707. // @todo Internationalize this?
  708. package_create_backup(($context['uninstalling'] ? 'backup_' : 'before_') . strtok($context['filename'], '.'));
  709. }
  710. // The mod isn't installed.... unless proven otherwise.
  711. $context['is_installed'] = false;
  712. // Is it actually installed?
  713. $request = $smcFunc['db_query']('', '
  714. SELECT version, themes_installed, db_changes
  715. FROM {db_prefix}log_packages
  716. WHERE package_id = {string:current_package}
  717. AND install_state != {int:not_installed}
  718. ORDER BY time_installed DESC
  719. LIMIT 1',
  720. array(
  721. 'not_installed' => 0,
  722. 'current_package' => $packageInfo['id'],
  723. )
  724. );
  725. while ($row = $smcFunc['db_fetch_assoc']($request))
  726. {
  727. $old_themes = explode(',', $row['themes_installed']);
  728. $old_version = $row['version'];
  729. $db_changes = empty($row['db_changes']) ? array() : unserialize($row['db_changes']);
  730. }
  731. $smcFunc['db_free_result']($request);
  732. // Wait, it's not installed yet!
  733. // @todo Replace with a better error message!
  734. if (!isset($old_version) && $context['uninstalling'])
  735. {
  736. deltree($boarddir . '/Packages/temp');
  737. fatal_error('Hacker?', false);
  738. }
  739. // Uninstalling?
  740. elseif ($context['uninstalling'])
  741. {
  742. $install_log = parsePackageInfo($packageInfo['xml'], false, 'uninstall');
  743. // Gadzooks! There's no uninstaller at all!?
  744. if (empty($install_log))
  745. fatal_lang_error('package_uninstall_cannot', false);
  746. // They can only uninstall from what it was originally installed into.
  747. foreach ($theme_paths as $id => $data)
  748. if ($id != 1 && !in_array($id, $old_themes))
  749. unset($theme_paths[$id]);
  750. }
  751. elseif (isset($old_version) && $old_version != $packageInfo['version'])
  752. {
  753. // Look for an upgrade...
  754. $install_log = parsePackageInfo($packageInfo['xml'], false, 'upgrade', $old_version);
  755. // There was no upgrade....
  756. if (empty($install_log))
  757. $context['is_installed'] = true;
  758. else
  759. {
  760. // Upgrade previous themes only!
  761. foreach ($theme_paths as $id => $data)
  762. if ($id != 1 && !in_array($id, $old_themes))
  763. unset($theme_paths[$id]);
  764. }
  765. }
  766. elseif (isset($old_version) && $old_version == $packageInfo['version'])
  767. $context['is_installed'] = true;
  768. if (!isset($old_version) || $context['is_installed'])
  769. $install_log = parsePackageInfo($packageInfo['xml'], false, 'install');
  770. $context['install_finished'] = false;
  771. // @todo Make a log of any errors that occurred and output them?
  772. if (!empty($install_log))
  773. {
  774. $failed_steps = array();
  775. $failed_count = 0;
  776. foreach ($install_log as $action)
  777. {
  778. $failed_count++;
  779. if ($action['type'] == 'modification' && !empty($action['filename']))
  780. {
  781. if ($action['boardmod'])
  782. $mod_actions = parseBoardMod(file_get_contents($boarddir . '/Packages/temp/' . $context['base_path'] . $action['filename']), false, $action['reverse'], $theme_paths);
  783. else
  784. $mod_actions = parseModification(file_get_contents($boarddir . '/Packages/temp/' . $context['base_path'] . $action['filename']), false, $action['reverse'], $theme_paths);
  785. // Any errors worth noting?
  786. foreach ($mod_actions as $key => $action)
  787. {
  788. if ($action['type'] == 'failure')
  789. $failed_steps[] = array(
  790. 'file' => $action['filename'],
  791. 'large_step' => $failed_count,
  792. 'sub_step' => $key,
  793. 'theme' => 1,
  794. );
  795. // Gather the themes we installed into.
  796. if (!empty($action['is_custom']))
  797. $themes_installed[] = $action['is_custom'];
  798. }
  799. }
  800. elseif ($action['type'] == 'code' && !empty($action['filename']))
  801. {
  802. // This is just here as reference for what is available.
  803. global $txt, $boarddir, $sourcedir, $modSettings, $context, $settings, $forum_version, $smcFunc;
  804. // Now include the file and be done with it ;).
  805. require($boarddir . '/Packages/temp/' . $context['base_path'] . $action['filename']);
  806. }
  807. elseif ($action['type'] == 'hook' && isset($action['hook'], $action['name']))
  808. {
  809. if ($action['reverse'])
  810. remove_integration_function($action['hook'], $action['function']);
  811. else
  812. add_integration_function($action['hook'], $action['function']);
  813. }
  814. // Only do the database changes on uninstall if requested.
  815. elseif ($action['type'] == 'database' && !empty($action['filename']) && (!$context['uninstalling'] || !empty($_POST['do_db_changes'])))
  816. {
  817. // These can also be there for database changes.
  818. global $txt, $boarddir, $sourcedir, $modSettings, $context, $settings, $forum_version, $smcFunc;
  819. global $db_package_log;
  820. // We'll likely want the package specific database functionality!
  821. db_extend('packages');
  822. // Let the file work its magic ;)
  823. require($boarddir . '/Packages/temp/' . $context['base_path'] . $action['filename']);
  824. }
  825. // Handle a redirect...
  826. elseif ($action['type'] == 'redirect' && !empty($action['redirect_url']))
  827. {
  828. $context['redirect_url'] = $action['redirect_url'];
  829. $context['redirect_text'] = !empty($action['filename']) && file_exists($boarddir . '/Packages/temp/' . $context['base_path'] . $action['filename']) ? file_get_contents($boarddir . '/Packages/temp/' . $context['base_path'] . $action['filename']) : ($context['uninstalling'] ? $txt['package_uninstall_done'] : $txt['package_installed_done']);
  830. $context['redirect_timeout'] = $action['redirect_timeout'];
  831. // Parse out a couple of common urls.
  832. $urls = array(
  833. '$boardurl' => $boardurl,
  834. '$scripturl' => $scripturl,
  835. '$session_var' => $context['session_var'],
  836. '$session_id' => $context['session_id'],
  837. );
  838. $context['redirect_url'] = strtr($context['redirect_url'], $urls);
  839. }
  840. }
  841. package_flush_cache();
  842. // First, ensure this change doesn't get removed by putting a stake in the ground (So to speak).
  843. package_put_contents($boarddir . '/Packages/installed.list', time());
  844. // See if this is already installed, and change it's state as required.
  845. $request = $smcFunc['db_query']('', '
  846. SELECT package_id, install_state, db_changes
  847. FROM {db_prefix}log_packages
  848. WHERE install_state != {int:not_installed}
  849. AND package_id = {string:current_package}
  850. ' . ($context['install_id'] ? ' AND id_install = {int:install_id} ' : '') . '
  851. ORDER BY time_installed DESC
  852. LIMIT 1',
  853. array(
  854. 'not_installed' => 0,
  855. 'install_id' => $context['install_id'],
  856. 'current_package' => $packageInfo['id'],
  857. )
  858. );
  859. $is_upgrade = false;
  860. while ($row = $smcFunc['db_fetch_assoc']($request))
  861. {
  862. // Uninstalling?
  863. if ($context['uninstalling'])
  864. {
  865. $smcFunc['db_query']('', '
  866. UPDATE {db_prefix}log_packages
  867. SET install_state = {int:not_installed}, member_removed = {string:member_name}, id_member_removed = {int:current_member},
  868. time_removed = {int:current_time}
  869. WHERE package_id = {string:package_id}
  870. AND id_install = {int:install_id}',
  871. array(
  872. 'current_member' => $user_info['id'],
  873. 'not_installed' => 0,
  874. 'current_time' => time(),
  875. 'package_id' => $row['package_id'],
  876. 'member_name' => $user_info['name'],
  877. 'install_id' => $context['install_id'],
  878. )
  879. );
  880. }
  881. // Otherwise must be an upgrade.
  882. else
  883. {
  884. $is_upgrade = true;
  885. $old_db_changes = empty($row['db_changes']) ? array() : unserialize($row['db_changes']);
  886. }
  887. }
  888. // Assuming we're not uninstalling, add the entry.
  889. if (!$context['uninstalling'])
  890. {
  891. // Any db changes from older version?
  892. if (!empty($old_db_changes))
  893. $db_package_log = empty($db_package_log) ? $old_db_changes : array_merge($old_db_changes, $db_package_log);
  894. // If there are some database changes we might want to remove then filter them out.
  895. if (!empty($db_package_log))
  896. {
  897. // We're really just checking for entries which are create table AND add columns (etc).
  898. $tables = array();
  899. function sort_table_first($a, $b)
  900. {
  901. if ($a[0] == $b[0])
  902. return 0;
  903. return $a[0] == 'remove_table' ? -1 : 1;
  904. }
  905. usort($db_package_log, 'sort_table_first');
  906. foreach ($db_package_log as $k => $log)
  907. {
  908. if ($log[0] == 'remove_table')
  909. $tables[] = $log[1];
  910. elseif (in_array($log[1], $tables))
  911. unset($db_package_log[$k]);
  912. }
  913. $db_changes = serialize($db_package_log);
  914. }
  915. else
  916. $db_changes = '';
  917. // What themes did we actually install?
  918. $themes_installed = array_unique($themes_installed);
  919. $themes_installed = implode(',', $themes_installed);
  920. // What failed steps?
  921. $failed_step_insert = serialize($failed_steps);
  922. $smcFunc['db_insert']('',
  923. '{db_prefix}log_packages',
  924. array(
  925. 'filename' => 'string', 'name' => 'string', 'package_id' => 'string', 'version' => 'string',
  926. 'id_member_installed' => 'int', 'member_installed' => 'string','time_installed' => 'int',
  927. 'install_state' => 'int', 'failed_steps' => 'string', 'themes_installed' => 'string',
  928. 'member_removed' => 'int', 'db_changes' => 'string',
  929. ),
  930. array(
  931. $packageInfo['filename'], $packageInfo['name'], $packageInfo['id'], $packageInfo['version'],
  932. $user_info['id'], $user_info['name'], time(),
  933. $is_upgrade ? 2 : 1, $failed_step_insert, $themes_installed,
  934. 0, $db_changes,
  935. ),
  936. array('id_install')
  937. );
  938. }
  939. $smcFunc['db_free_result']($request);
  940. $context['install_finished'] = true;
  941. }
  942. // If there's database changes - and they want them removed - let's do it last!
  943. if (!empty($db_changes) && !empty($_POST['do_db_changes']))
  944. {
  945. // We're gonna be needing the package db functions!
  946. db_extend('packages');
  947. foreach ($db_changes as $change)
  948. {
  949. if ($change[0] == 'remove_table' && isset($change[1]))
  950. $smcFunc['db_drop_table']($change[1]);
  951. elseif ($change[0] == 'remove_column' && isset($change[2]))
  952. $smcFunc['db_remove_column']($change[1], $change[2]);
  953. elseif ($change[0] == 'remove_index' && isset($change[2]))
  954. $smcFunc['db_remove_index']($change[1], $change[2]);
  955. }
  956. }
  957. // Clean house... get rid of the evidence ;).
  958. if (file_exists($boarddir . '/Packages/temp'))
  959. deltree($boarddir . '/Packages/temp');
  960. // Log what we just did.
  961. logAction($context['uninstalling'] ? 'uninstall_package' : (!empty($is_upgrade) ? 'upgrade_package' : 'install_package'), array('package' => $smcFunc['htmlspecialchars']($packageInfo['name']), 'version' => $smcFunc['htmlspecialchars']($packageInfo['version'])), 'admin');
  962. // Just in case, let's clear the whole cache to avoid anything going up the swanny.
  963. clean_cache();
  964. // Restore file permissions?
  965. create_chmod_control(array(), array(), true);
  966. }
  967. /**
  968. * List the files in a package.
  969. */
  970. function PackageList()
  971. {
  972. global $txt, $scripturl, $boarddir, $context, $sourcedir;
  973. require_once($sourcedir . '/Subs-Package.php');
  974. // No package? Show him or her the door.
  975. if (!isset($_REQUEST['package']) || $_REQUEST['package'] == '')
  976. redirectexit('action=admin;area=packages');
  977. $context['linktree'][] = array(
  978. 'url' => $scripturl . '?action=admin;area=packages;sa=list;package=' . $_REQUEST['package'],
  979. 'name' => $txt['list_file']
  980. );
  981. $context['page_title'] .= ' - ' . $txt['list_file'];
  982. $context['sub_template'] = 'list';
  983. // The filename...
  984. $context['filename'] = $_REQUEST['package'];
  985. // Let the unpacker do the work.
  986. if (is_file($boarddir . '/Packages/' . $context['filename']))
  987. $context['files'] = read_tgz_file($boarddir . '/Packages/' . $context['filename'], null);
  988. elseif (is_dir($boarddir . '/Packages/' . $context['filename']))
  989. $context['files'] = listtree($boarddir . '/Packages/' . $context['filename']);
  990. }
  991. /**
  992. * Display one of the files in a package.
  993. */
  994. function ExamineFile()
  995. {
  996. global $txt, $scripturl, $boarddir, $context, $sourcedir;
  997. require_once($sourcedir . '/Subs-Package.php');
  998. // No package? Show him or her the door.
  999. if (!isset($_REQUEST['package']) || $_REQUEST['package'] == '')
  1000. redirectexit('action=admin;area=packages');
  1001. // No file? Show him or her the door.
  1002. if (!isset($_REQUEST['file']) || $_REQUEST['file'] == '')
  1003. redirectexit('action=admin;area=packages');
  1004. $_REQUEST['package'] = preg_replace('~[\.]+~', '.', strtr($_REQUEST['package'], array('/' => '_', '\\' => '_')));
  1005. $_REQUEST['file'] = preg_replace('~[\.]+~', '.', $_REQUEST['file']);
  1006. if (isset($_REQUEST['raw']))
  1007. {
  1008. if (is_file($boarddir . '/Packages/' . $_REQUEST['package']))
  1009. echo read_tgz_file($boarddir . '/Packages/' . $_REQUEST['package'], $_REQUEST['file'], true);
  1010. elseif (is_dir($boarddir . '/Packages/' . $_REQUEST['package']))
  1011. echo file_get_contents($boarddir . '/Packages/' . $_REQUEST['package'] . '/' . $_REQUEST['file']);
  1012. obExit(false);
  1013. }
  1014. $context['linktree'][count($context['linktree']) - 1] = array(
  1015. 'url' => $scripturl . '?action=admin;area=packages;sa=list;package=' . $_REQUEST['package'],
  1016. 'name' => $txt['package_examine_file']
  1017. );
  1018. $context['page_title'] .= ' - ' . $txt['package_examine_file'];
  1019. $context['sub_template'] = 'examine';
  1020. // The filename...
  1021. $context['package'] = $_REQUEST['package'];
  1022. $context['filename'] = $_REQUEST['file'];
  1023. // Let the unpacker do the work.... but make sure we handle images properly.
  1024. if (in_array(strtolower(strrchr($_REQUEST['file'], '.')), array('.bmp', '.gif', '.jpeg', '.jpg', '.png')))
  1025. $context['filedata'] = '<img src="' . $scripturl . '?action=admin;area=packages;sa=examine;package=' . $_REQUEST['package'] . ';file=' . $_REQUEST['file'] . ';raw" alt="' . $_REQUEST['file'] . '" />';
  1026. else
  1027. {
  1028. if (is_file($boarddir . '/Packages/' . $_REQUEST['package']))
  1029. $context['filedata'] = htmlspecialchars(read_tgz_file($boarddir . '/Packages/' . $_REQUEST['package'], $_REQUEST['file'], true));
  1030. elseif (is_dir($boarddir . '/Packages/' . $_REQUEST['package']))
  1031. $context['filedata'] = htmlspecialchars(file_get_contents($boarddir . '/Packages/' . $_REQUEST['package'] . '/' . $_REQUEST['file']));
  1032. if (strtolower(strrchr($_REQUEST['file'], '.')) == '.php')
  1033. $context['filedata'] = highlight_php_code($context['filedata']);
  1034. }
  1035. }
  1036. /**
  1037. * List the installed packages.
  1038. */
  1039. function InstalledList()
  1040. {
  1041. global $txt, $scripturl, $context;
  1042. $context['page_title'] .= ' - ' . $txt['installed_packages'];
  1043. $context['sub_template'] = 'view_installed';
  1044. // Load the installed mods and send them to the template.
  1045. $context['installed_mods'] = loadInstalledPackages();
  1046. }
  1047. /**
  1048. * Empty out the installed list.
  1049. */
  1050. function FlushInstall()
  1051. {
  1052. global $boarddir, $sourcedir, $smcFunc;
  1053. // Always check the session.
  1054. checkSession('get');
  1055. include_once($sourcedir . '/Subs-Package.php');
  1056. // Record when we last did this.
  1057. package_put_contents($boarddir . '/Packages/installed.list', time());
  1058. // Set everything as uninstalled.
  1059. $smcFunc['db_query']('', '
  1060. UPDATE {db_prefix}log_packages
  1061. SET install_state = {int:not_installed}',
  1062. array(
  1063. 'not_installed' => 0,
  1064. )
  1065. );
  1066. redirectexit('action=admin;area=packages;sa=installed');
  1067. }
  1068. /**
  1069. * Delete a package.
  1070. */
  1071. function PackageRemove()
  1072. {
  1073. global $scripturl, $boarddir;
  1074. // Check it.
  1075. checkSession('get');
  1076. // Ack, don't allow deletion of arbitrary files here, could become a security hole somehow!
  1077. if (!isset($_GET['package']) || $_GET['package'] == 'index.php' || $_GET['package'] == 'installed.list' || $_GET['package'] == 'backups')
  1078. redirectexit('action=admin;area=packages;sa=browse');
  1079. $_GET['package'] = preg_replace('~[\.]+~', '.', strtr($_GET['package'], array('/' => '_', '\\' => '_')));
  1080. // Can't delete what's not there.
  1081. if (file_exists($boarddir . '/Packages/' . $_GET['package']) && (substr($_GET['package'], -4) == '.zip' || substr($_GET['package'], -4) == '.tgz' || substr($_GET['package'], -7) == '.tar.gz' || is_dir($boarddir . '/Packages/' . $_GET['package'])) && $_GET['package'] != 'backups' && substr($_GET['package'], 0, 1) != '.')
  1082. {
  1083. create_chmod_control(array($boarddir . '/Packages/' . $_GET['package']), array('destination_url' => $scripturl . '?action=admin;area=packages;sa=remove;package=' . $_GET['package'], 'crash_on_error' => true));
  1084. if (is_dir($boarddir . '/Packages/' . $_GET['package']))
  1085. deltree($boarddir . '/Packages/' . $_GET['package']);
  1086. else
  1087. {
  1088. @chmod($boarddir . '/Packages/' . $_GET['package'], 0777);
  1089. unlink($boarddir . '/Packages/' . $_GET['package']);
  1090. }
  1091. }
  1092. redirectexit('action=admin;area=packages;sa=browse');
  1093. }
  1094. /**
  1095. * Browse a list of installed packages.
  1096. */
  1097. function PackageBrowse()
  1098. {
  1099. global $txt, $boarddir, $scripturl, $context, $forum_version;
  1100. $context['page_title'] .= ' - ' . $txt['browse_packages'];
  1101. $context['sub_template'] = 'browse';
  1102. $context['forum_version'] = $forum_version;
  1103. $instmods = loadInstalledPackages();
  1104. $installed_mods = array();
  1105. // Look through the list of installed mods...
  1106. foreach ($instmods as $installed_mod)
  1107. $installed_mods[$installed_mod['package_id']] = array(
  1108. 'id' => $installed_mod['id'],
  1109. 'version' => $installed_mod['version'],
  1110. );
  1111. $the_version = strtr($forum_version, array('SMF ' => ''));
  1112. // Here we have a little code to help those who class themselves as something of gods, version emulation ;)
  1113. if (isset($_GET['version_emulate']))
  1114. {
  1115. if ($_GET['version_emulate'] === 0 && isset($_SESSION['version_emulate']))
  1116. unset($_SESSION['version_emulate']);
  1117. elseif ($_GET['version_emulate'] !== 0)
  1118. $_SESSION['version_emulate'] = strtr($_GET['version_emulate'], array('-' => ' ', '+' => ' ', 'SMF ' => ''));
  1119. }
  1120. if (!empty($_SESSION['version_emulate']))
  1121. {
  1122. $context['forum_version'] = 'SMF ' . $_SESSION['version_emulate'];
  1123. $the_version = $_SESSION['version_emulate'];
  1124. }
  1125. // Get a list of all the ids installed, so the latest packages won't include already installed ones.
  1126. $context['installed_mods'] = array_keys($installed_mods);
  1127. // Empty lists for now.
  1128. $context['available_mods'] = array();
  1129. $context['available_avatars'] = array();
  1130. $context['available_languages'] = array();
  1131. $context['available_other'] = array();
  1132. $context['available_all'] = array();
  1133. // We need the packages directory to be writable for this.
  1134. if (!@is_writable($boarddir . '/Packages'))
  1135. create_chmod_control(array($boarddir . '/Packages'), array('destination_url' => $scripturl . '?action=admin;area=packages', 'crash_on_error' => true));
  1136. if ($dir = @opendir($boarddir . '/Packages'))
  1137. {
  1138. $dirs = array();
  1139. while ($package = readdir($dir))
  1140. {
  1141. if ($package == '.' || $package == '..' || $package == 'temp' || (!(is_dir($boarddir . '/Packages/' . $package) && file_exists($boarddir . '/Packages/' . $package . '/package-info.xml')) && substr(strtolower($package), -7) != '.tar.gz' && substr(strtolower($package), -4) != '.tgz' && substr(strtolower($package), -4) != '.zip'))
  1142. continue;
  1143. // Skip directories or files that are named the same.
  1144. if (is_dir($boarddir . '/Packages/' . $package))
  1145. {
  1146. if (in_array($package, $dirs))
  1147. continue;
  1148. $dirs[] = $package;
  1149. }
  1150. elseif (substr(strtolower($package), -7) == '.tar.gz')
  1151. {
  1152. if (in_array(substr($package, 0, -7), $dirs))
  1153. continue;
  1154. $dirs[] = substr($package, 0, -7);
  1155. }
  1156. elseif (substr(strtolower($package), -4) == '.zip' || substr(strtolower($package), -4) == '.tgz')
  1157. {
  1158. if (in_array(substr($package, 0, -4), $dirs))
  1159. continue;
  1160. $dirs[] = substr($package, 0, -4);
  1161. }
  1162. $packageInfo = getPackageInfo($package);
  1163. if (!is_array($packageInfo))
  1164. continue;
  1165. $packageInfo['installed_id'] = isset($installed_mods[$packageInfo['id']]) ? $installed_mods[$packageInfo['id']]['id'] : 0;
  1166. $packageInfo['is_installed'] = isset($installed_mods[$packageInfo['id']]);
  1167. $packageInfo['is_current'] = $packageInfo['is_installed'] && ($installed_mods[$packageInfo['id']]['version'] == $packageInfo['version']);
  1168. $packageInfo['is_newer'] = $packageInfo['is_installed'] && ($installed_mods[$packageInfo['id']]['version'] > $packageInfo['version']);
  1169. $packageInfo['can_install'] = false;
  1170. $packageInfo['can_uninstall'] = false;
  1171. $packageInfo['can_upgrade'] = false;
  1172. // This package is currently NOT installed. Check if it can be.
  1173. if (!$packageInfo['is_installed'] && $packageInfo['xml']->exists('install'))
  1174. {
  1175. // Check if there's an install for *THIS* version of SMF.
  1176. $installs = $packageInfo['xml']->set('install');
  1177. foreach ($installs as $install)
  1178. {
  1179. if (!$install->exists('@for') || matchPackageVersion($the_version, $install->fetch('@for')))
  1180. {
  1181. // Okay, this one is good to go.
  1182. $packageInfo['can_install'] = true;
  1183. break;
  1184. }
  1185. }
  1186. }
  1187. // An already installed, but old, package. Can we upgrade it?
  1188. elseif ($packageInfo['is_installed'] && !$packageInfo['is_current'] && $packageInfo['xml']->exists('upgrade'))
  1189. {
  1190. $upgrades = $packageInfo['xml']->set('upgrade');
  1191. // First go through, and check against the current version of SMF.
  1192. foreach ($upgrades as $upgrade)
  1193. {
  1194. // Even if it is for this SMF, is it for the installed version of the mod?
  1195. if (!$upgrade->exists('@for') || matchPackageVersion($the_version, $upgrade->fetch('@for')))
  1196. if (!$upgrade->exists('@from') || matchPackageVersion($installed_mods[$packageInfo['id']]['version'], $upgrade->fetch('@from')))
  1197. {
  1198. $packageInfo['can_upgrade'] = true;
  1199. break;
  1200. }
  1201. }
  1202. }
  1203. // Note that it has to be the current version to be uninstallable. Shucks.
  1204. elseif ($packageInfo['is_installed'] && $packageInfo['is_current'] && $packageInfo['xml']->exists('uninstall'))
  1205. {
  1206. $uninstalls = $packageInfo['xml']->set('uninstall');
  1207. // Can we find any uninstallation methods that work for this SMF version?
  1208. foreach ($uninstalls as $uninstall)
  1209. if (!$uninstall->exists('@for') || matchPackageVersion($the_version, $uninstall->fetch('@for')))
  1210. {
  1211. $packageInfo['can_uninstall'] = true;
  1212. break;
  1213. }
  1214. }
  1215. // Store a complete list.
  1216. $context['available_all'][] = $packageInfo;
  1217. // Modification.
  1218. if ($packageInfo['type'] == 'modification' || $packageInfo['type'] == 'mod')
  1219. $context['available_mods'][] = $packageInfo;
  1220. // Avatar package.
  1221. elseif ($packageInfo['type'] == 'avatar')
  1222. $context['available_avatars'][] = $packageInfo;
  1223. // Language package.
  1224. elseif ($packageInfo['type'] == 'language')
  1225. $context['available_languages'][] = $packageInfo;
  1226. // Other stuff.
  1227. else
  1228. $context['available_other'][] = $packageInfo;
  1229. }
  1230. closedir($dir);
  1231. }
  1232. }
  1233. function PackageOptions()
  1234. {
  1235. global $txt, $scripturl, $context, $sourcedir, $modSettings, $smcFunc;
  1236. if (isset($_POST['submit']))
  1237. {
  1238. checkSession('post');
  1239. updateSettings(array(
  1240. 'package_server' => trim($smcFunc['htmlspecialchars']($_POST['pack_server'])),
  1241. 'package_port' => trim($smcFunc['htmlspecialchars']($_POST['pack_port'])),
  1242. 'package_username' => trim($smcFunc['htmlspecialchars']($_POST['pack_user'])),
  1243. 'package_make_backups' => !empty($_POST['package_make_backups']),
  1244. 'package_make_full_backups' => !empty($_POST['package_make_full_backups'])
  1245. ));
  1246. redirectexit('action=admin;area=packages;sa=options');
  1247. }
  1248. if (preg_match('~^/home\d*/([^/]+?)/public_html~', $_SERVER['DOCUMENT_ROOT'], $match))
  1249. $default_username = $match[1];
  1250. else
  1251. $default_username = '';
  1252. $context['page_title'] = $txt['package_settings'];
  1253. $context['sub_template'] = 'install_options';
  1254. $context['package_ftp_server'] = isset($modSettings['package_server']) ? $modSettings['package_server'] : 'localhost';
  1255. $context['package_ftp_port'] = isset($modSettings['package_port']) ? $modSettings['package_port'] : '21';
  1256. $context['package_ftp_username'] = isset($modSettings['package_username']) ? $modSettings['package_username'] : $default_username;
  1257. $context['package_make_backups'] = !empty($modSettings['package_make_backups']);
  1258. $context['package_make_full_backups'] = !empty($modSettings['package_make_full_backups']);
  1259. }
  1260. /**
  1261. * List operations
  1262. */
  1263. function ViewOperations()
  1264. {
  1265. global $context, $txt, $boarddir, $sourcedir, $smcFunc, $modSettings;
  1266. // Can't be in here buddy.
  1267. isAllowedTo('admin_forum');
  1268. // We need to know the operation key for the search and replace, mod file looking at, is it a board mod?
  1269. if (!isset($_REQUEST['operation_key'], $_REQUEST['filename']) && !is_numeric($_REQUEST['operation_key']))
  1270. fatal_lang_error('operation_invalid', 'general');
  1271. // Load the required file.
  1272. require_once($sourcedir . '/Subs-Package.php');
  1273. // Uninstalling the mod?
  1274. $reverse = isset($_REQUEST['reverse']) ? true : false;
  1275. // Get the base name.
  1276. $context['filename'] = preg_replace('~[\.]+~', '.', $_REQUEST['package']);
  1277. // We need to extract this again.
  1278. if (is_file($boarddir . '/Packages/' . $context['filename']))
  1279. {
  1280. $context['extracted_files'] = read_tgz_file($boarddir . '/Packages/' . $context['filename'], $boarddir . '/Packages/temp');
  1281. if ($context['extracted_files'] && !file_exists($boarddir . '/Packages/temp/package-info.xml'))
  1282. foreach ($context['extracted_files'] as $file)
  1283. if (basename($file['filename']) == 'package-info.xml')
  1284. {
  1285. $context['base_path'] = dirname($file['filename']) . '/';
  1286. break;
  1287. }
  1288. if (!isset($context['base_path']))
  1289. $context['base_path'] = '';
  1290. }
  1291. elseif (is_dir($boarddir . '/Packages/' . $context['filename']))
  1292. {
  1293. copytree($boarddir . '/Packages/' . $context['filename'], $boarddir . '/Packages/temp');
  1294. $context['extracted_files'] = listtree($boarddir . '/Packages/temp');
  1295. $context['base_path'] = '';
  1296. }
  1297. // Load up any custom themes we may want to install into...
  1298. $request = $smcFunc['db_query']('', '
  1299. SELECT id_theme, variable, value
  1300. FROM {db_prefix}themes
  1301. WHERE (id_theme = {int:default_theme} OR id_theme IN ({array_int:known_theme_list}))
  1302. AND variable IN ({string:name}, {string:theme_dir})',
  1303. array(
  1304. 'known_theme_list' => explode(',', $modSettings['knownThemes']),
  1305. 'default_theme' => 1,
  1306. 'name' => 'name',
  1307. 'theme_dir' => 'theme_dir',
  1308. )
  1309. );
  1310. $theme_paths = array();
  1311. while ($row = $smcFunc['db_fetch_assoc']($request))
  1312. $theme_paths[$row['id_theme']][$row['variable']] = $row['value'];
  1313. $smcFunc['db_free_result']($request);
  1314. // Boardmod?
  1315. if (isset($_REQUEST['boardmod']))
  1316. $mod_actions = parseBoardMod(@file_get_contents($boarddir . '/Packages/temp/' . $context['base_path'] . $_REQUEST['filename']), true, $reverse, $theme_paths);
  1317. else
  1318. $mod_actions = parseModification(@file_get_contents($boarddir . '/Packages/temp/' . $context['base_path'] . $_REQUEST['filename']), true, $reverse, $theme_paths);
  1319. // Ok lets get the content of the file.
  1320. $context['operations'] = array(
  1321. 'search' => strtr(htmlspecialchars($mod_actions[$_REQUEST['operation_key']]['search_original']), array('[' => '&#91;', ']' => '&#93;')),
  1322. 'replace' => strtr(htmlspecialchars($mod_actions[$_REQUEST['operation_key']]['replace_original']), array('[' => '&#91;', ']' => '&#93;')),
  1323. 'position' => $mod_actions[$_REQUEST['operation_key']]['position'],
  1324. );
  1325. // Let's do some formatting...
  1326. $operation_text = $context['operations']['position'] == 'replace' ? 'operation_replace' : ($context['operations']['position'] == 'before' ? 'operation_after' : 'operation_before');
  1327. $context['operations']['search'] = parse_bbc('[code=' . $txt['operation_find'] . ']' . ($context['operations']['position'] == 'end' ? '?&gt;' : $context['operations']['search']) . '[/code]');
  1328. $context['operations']['replace'] = parse_bbc('[code=' . $txt[$operation_text] . ']' . $context['operations']['replace'] . '[/code]');
  1329. // No layers
  1330. $context['template_layers'] = array();
  1331. $context['sub_template'] = 'view_operations';
  1332. }
  1333. /**
  1334. * Allow the admin to reset permissions on files.
  1335. */
  1336. function PackagePermissions()
  1337. {
  1338. global $context, $txt, $modSettings, $boarddir, $sourcedir, $cachedir, $smcFunc, $package_ftp;
  1339. // Let's try and be good, yes?
  1340. checkSession('get');
  1341. // If we're restoring permissions this is just a pass through really.
  1342. if (isset($_GET['restore']))
  1343. {
  1344. create_chmod_control(array(), array(), true);
  1345. fatal_lang_error('no_access', false);
  1346. }
  1347. // This is a memory eat.
  1348. @ini_set('memory_limit', '128M');
  1349. @set_time_limit(600);
  1350. // Load up some FTP stuff.
  1351. create_chmod_control();
  1352. if (empty($package_ftp) && !isset($_POST['skip_ftp']))
  1353. {
  1354. require_once($sourcedir . '/Class-Package.php');
  1355. $ftp = new ftp_connection(null);
  1356. list ($username, $detect_path, $found_path) = $ftp->detect_path($boarddir);
  1357. $context['package_ftp'] = array(
  1358. 'server' => isset($modSettings['package_server']) ? $modSettings['package_server'] : 'localhost',
  1359. 'port' => isset($modSettings['package_port']) ? $modSettings['package_port'] : '21',
  1360. 'username' => empty($username) ? (isset($modSettings['package_username']) ? $modSettings['package_username'] : '') : $username,
  1361. 'path' => $detect_path,
  1362. 'form_elements_only' => true,
  1363. );
  1364. }
  1365. else
  1366. $context['ftp_connected'] = true;
  1367. // Define the template.
  1368. $context['page_title'] = $txt['package_file_perms'];
  1369. $context['sub_template'] = 'file_permissions';
  1370. // Define what files we're interested in, as a tree.
  1371. $context['file_tree'] = array(
  1372. strtr($boarddir, array('\\' => '/')) => array(
  1373. 'type' => 'dir',
  1374. 'contents' => array(
  1375. 'agreement.txt' => array(
  1376. 'type' => 'file',
  1377. 'writable_on' => 'standard',
  1378. ),
  1379. 'Settings.php' => array(
  1380. 'type' => 'file',
  1381. 'writable_on' => 'restrictive',
  1382. ),
  1383. 'Settings_bak.php' => array(
  1384. 'type' => 'file',
  1385. 'writable_on' => 'restrictive',
  1386. ),
  1387. 'attachments' => array(
  1388. 'type' => 'dir',
  1389. 'writable_on' => 'restrictive',
  1390. ),
  1391. 'avatars' => array(
  1392. 'type' => 'dir',
  1393. 'writable_on' => 'standard',
  1394. ),
  1395. 'cache' => array(
  1396. 'type' => 'dir',
  1397. 'writable_on' => 'restrictive',
  1398. ),
  1399. 'custom_avatar_dir' => array(
  1400. 'type' => 'dir',
  1401. 'writable_on' => 'restrictive',
  1402. ),
  1403. 'Smileys' => array(
  1404. 'type' => 'dir_recursive',
  1405. 'writable_on' => 'standard',
  1406. ),
  1407. 'Sources' => array(
  1408. 'type' => 'dir',
  1409. 'list_contents' => true,
  1410. 'writable_on' => 'standard',
  1411. ),
  1412. 'Themes' => array(
  1413. 'type' => 'dir_recursive',
  1414. 'writable_on' => 'standard',
  1415. 'contents' => array(
  1416. 'default' => array(
  1417. 'type' => 'dir_recursive',
  1418. 'list_contents' => true,
  1419. 'contents' => array(
  1420. 'languages' => array(
  1421. 'type' => 'dir',
  1422. 'list_contents' => true,
  1423. ),
  1424. ),
  1425. ),
  1426. ),
  1427. ),
  1428. 'Packages' => array(
  1429. 'type' => 'dir',
  1430. 'writable_on' => 'standard',
  1431. 'contents' => array(
  1432. 'temp' => array(
  1433. 'type' => 'dir',
  1434. ),
  1435. 'backup' => array(
  1436. 'type' => 'dir',
  1437. ),
  1438. 'installed.list' => array(
  1439. 'type' => 'file',
  1440. 'writable_on' => 'standard',
  1441. ),
  1442. ),
  1443. ),
  1444. ),
  1445. ),
  1446. );
  1447. // Directories that can move.
  1448. if (substr($sourcedir, 0, strlen($boarddir)) != $boarddir)
  1449. {
  1450. unset($context['file_tree'][strtr($boarddir, array('\\' => '/'))]['contents']['Sources']);
  1451. $context['file_tree'][strtr($sourcedir, array('\\' => '/'))] = array(
  1452. 'type' => 'dir',
  1453. 'list_contents' => true,
  1454. 'writable_on' => 'standard',
  1455. );
  1456. }
  1457. // Moved the cache?
  1458. if (substr($cachedir, 0, strlen($boarddir)) != $boarddir)
  1459. {
  1460. unset($context['file_tree'][strtr($boarddir, array('\\' => '/'))]['contents']['cache']);
  1461. $context['file_tree'][strtr($cachedir, array('\\' => '/'))] = array(
  1462. 'type' => 'dir',
  1463. 'list_contents' => false,
  1464. 'writable_on' => 'restrictive',
  1465. );
  1466. }
  1467. // Are we using multiple attachment directories?
  1468. if (!empty($modSettings['currentAttachmentUploadDir']))
  1469. {
  1470. unset($context['file_tree'][strtr($boarddir, array('\\' => '/'))]['contents']['attachments']);
  1471. if (!is_array($modSettings['attachmentUploadDir']))
  1472. $modSettings['attachmentUploadDir'] = unserialize($modSettings['attachmentUploadDir']);
  1473. // @todo Should we suggest non-current directories be read only?
  1474. foreach ($modSettings['attachmentUploadDir'] as $dir)
  1475. $context['file_tree'][strtr($dir, array('\\' => '/'))] = array(
  1476. 'type' => 'dir',
  1477. 'writable_on' => 'restrictive',
  1478. );
  1479. }
  1480. elseif (substr($modSettings['attachmentUploadDir'], 0, strlen($boarddir)) != $boarddir)
  1481. {
  1482. unset($context['file_tree'][strtr($boarddir, array('\\' => '/'))]['contents']['attachments']);
  1483. $context['file_tree'][strtr($modSettings['attachmentUploadDir'], array('\\' => '/'))] = array(
  1484. 'type' => 'dir',
  1485. 'writable_on' => 'restrictive',
  1486. );
  1487. }
  1488. if (substr($modSettings['smileys_dir'], 0, strlen($boarddir)) != $boarddir)
  1489. {
  1490. unset($context['file_tree'][strtr($boarddir, array('\\' => '/'))]['contents']['Smileys']);
  1491. $context['file_tree'][strtr($modSettings['smileys_dir'], array('\\' => '/'))] = array(
  1492. 'type' => 'dir_recursive',
  1493. 'writable_on' => 'standard',
  1494. );
  1495. }
  1496. if (substr($modSettings['avatar_directory'], 0, strlen($boarddir)) != $boarddir)
  1497. {
  1498. unset($context['file_tree'][strtr($boarddir, array('\\' => '/'))]['contents']['avatars']);
  1499. $context['file_tree'][strtr($modSettings['avatar_directory'], array('\\' => '/'))] = array(
  1500. 'type' => 'dir',
  1501. 'writable_on' => 'standard',
  1502. );
  1503. }
  1504. if (isset($modSettings['custom_avatar_dir']) && substr($modSettings['custom_avatar_dir'], 0, strlen($boarddir)) != $boarddir)
  1505. {
  1506. unset($context['file_tree'][strtr($boarddir, array('\\' => '/'))]['contents']['custom_avatar_dir']);
  1507. $context['file_tree'][strtr($modSettings['custom_avatar_dir'], array('\\' => '/'))] = array(
  1508. 'type' => 'dir',
  1509. 'writable_on' => 'restrictive',
  1510. );
  1511. }
  1512. // Load up any custom themes.
  1513. $request = $smcFunc['db_query']('', '
  1514. SELECT value
  1515. FROM {db_prefix}themes
  1516. WHERE id_theme > {int:default_theme_id}
  1517. AND id_member = {int:guest_id}
  1518. AND variable = {string:theme_dir}
  1519. ORDER BY value ASC',
  1520. array(
  1521. 'default_theme_id' => 1,
  1522. 'guest_id' => 0,
  1523. 'theme_dir' => 'theme_dir',
  1524. )
  1525. );
  1526. while ($row = $smcFunc['db_fetch_assoc']($request))
  1527. {
  1528. if (substr(strtolower(strtr($row['value'], array('\\' => '/'))), 0, strlen($boarddir) + 7) == strtolower(strtr($boarddir, array('\\' => '/')) . '/Themes'))
  1529. $context['file_tree'][strtr($boarddir, array('\\' => '/'))]['contents']['Themes']['contents'][substr($row['value'], strlen($boarddir) + 8)] = array(
  1530. 'type' => 'dir_recursive',
  1531. 'list_contents' => true,
  1532. 'contents' => array(
  1533. 'languages' => array(
  1534. 'type' => 'dir',
  1535. 'list_contents' => true,
  1536. ),
  1537. ),
  1538. );
  1539. else
  1540. {
  1541. $context['file_tree'][strtr($row['value'], array('\\' => '/'))] = array(
  1542. 'type' => 'dir_recursive',
  1543. 'list_contents' => true,
  1544. 'contents' => array(
  1545. 'languages' => array(
  1546. 'type' => 'dir',
  1547. 'list_contents' => true,
  1548. ),
  1549. ),
  1550. );
  1551. }
  1552. }
  1553. $smcFunc['db_free_result']($request);
  1554. // If we're submitting then let's move on to another function to keep things cleaner..
  1555. if (isset($_POST['action_changes']))
  1556. return PackagePermissionsAction();
  1557. $context['look_for'] = array();
  1558. // Are we looking for a particular tree - normally an expansion?
  1559. if (!empty($_REQUEST['find']))
  1560. $context['look_for'][] = base64_decode($_REQUEST['find']);
  1561. // Only that tree?
  1562. $context['only_find'] = isset($_GET['xml']) && !empty($_REQUEST['onlyfind']) ? $_REQUEST['onlyfind'] : '';
  1563. if ($context['only_find'])
  1564. $context['look_for'][] = $context['only_find'];
  1565. // Have we got a load of back-catalogue trees to expand from a submit etc?
  1566. if (!empty($_GET['back_look']))
  1567. {
  1568. $potententialTrees = unserialize(base64_decode($_GET['back_look']));
  1569. foreach ($potententialTrees as $tree)
  1570. $context['look_for'][] = $tree;
  1571. }
  1572. // ... maybe posted?
  1573. if (!empty($_POST['back_look']))
  1574. $context['only_find'] = array_merge($context['only_find'], $_POST['back_look']);
  1575. $context['back_look_data'] = base64_encode(serialize(array_slice($context['look_for'], 0, 15)));
  1576. // Are we finding more files than first thought?
  1577. $context['file_offset'] = !empty($_REQUEST['fileoffset']) ? (int) $_REQUEST['fileoffset'] : 0;
  1578. // Don't list more than this many files in a directory.
  1579. $context['file_limit'] = 150;
  1580. // How many levels shall we show?
  1581. $context['default_level'] = empty($context['only_find']) ? 2 : 25;
  1582. // This will be used if we end up catching XML data.
  1583. $context['xml_data'] = array(
  1584. 'roots' => array(
  1585. 'identifier' => 'root',
  1586. 'children' => array(
  1587. array(
  1588. 'value' => preg_replace('~[^A-Za-z0-9_\-=:]~', ':-:', $context['only_find']),
  1589. ),
  1590. ),
  1591. ),
  1592. 'folders' => array(
  1593. 'identifier' => 'folder',
  1594. 'children' => array(),
  1595. ),
  1596. );
  1597. foreach ($context['file_tree'] as $path => $data)
  1598. {
  1599. // Run this directory.
  1600. if (file_exists($path) && (empty($context['only_find']) || substr($context['only_find'], 0, strlen($path)) == $path))
  1601. {
  1602. // Get the first level down only.
  1603. fetchPerms__recursive($path, $context['file_tree'][$path], 1);
  1604. $context['file_tree'][$path]['perms'] = array(
  1605. 'chmod' => @is_writable($path),
  1606. 'perms' => @fileperms($path),
  1607. );
  1608. }
  1609. else
  1610. unset($context['file_tree'][$path]);
  1611. }
  1612. // Is this actually xml?
  1613. if (isset($_GET['xml']))
  1614. {
  1615. loadTemplate('Xml');
  1616. $context['sub_template'] = 'generic_xml';
  1617. $context['template_layers'] = array();
  1618. }
  1619. }
  1620. function fetchPerms__recursive($path, &$data, $level)
  1621. {
  1622. global $context;
  1623. $isLikelyPath = false;
  1624. foreach ($context['look_for'] as $possiblePath)
  1625. if (substr($possiblePath, 0, strlen($path)) == $path)
  1626. $isLikelyPath = true;
  1627. // Is this where we stop?
  1628. if (isset($_GET['xml']) && !empty($context['look_for']) && !$isLikelyPath)
  1629. return;
  1630. elseif ($level > $context['default_level'] && !$isLikelyPath)
  1631. return;
  1632. // Are we actually interested in saving this data?
  1633. $save_data = empty($context['only_find']) || $context['only_find'] == $path;
  1634. // @todo Shouldn't happen - but better error message?
  1635. if (!is_dir($path))
  1636. fatal_lang_error('no_access', false);
  1637. // This is where we put stuff we've found for sorting.
  1638. $foundData = array(
  1639. 'files' => array(),
  1640. 'folders' => array(),
  1641. );
  1642. $dh = opendir($path);
  1643. while ($entry = readdir($dh))
  1644. {
  1645. // Some kind of file?
  1646. if (!is_dir($path . '/' . $entry))
  1647. {
  1648. // Are we listing PHP files in this directory?
  1649. if ($save_data && !empty($data['list_contents']) && substr($entry, -4) == '.php')
  1650. $foundData['files'][$entry] = true;
  1651. // A file we were looking for.
  1652. elseif ($save_data && isset($data['contents'][$entry]))
  1653. $foundData['files'][$entry] = true;
  1654. }
  1655. // It's a directory - we're interested one way or another, probably...
  1656. elseif ($entry != '.' && $entry != '..')
  1657. {
  1658. // Going further?
  1659. if ((!empty($data['type']) && $data['type'] == 'dir_recursive') || (isset($data['contents'][$entry]) && (!empty($data['contents'][$entry]['list_contents']) || (!empty($data['contents'][$entry]['type']) && $data['contents'][$entry]['type'] == 'dir_recursive'))))
  1660. {
  1661. if (!isset($data['contents'][$entry]))
  1662. $foundData['folders'][$entry] = 'dir_recursive';
  1663. else
  1664. $foundData['folders'][$entry] = true;
  1665. // If this wasn't expected inherit the recusiveness...
  1666. if (!isset($data['contents'][$entry]))
  1667. // We need to do this as we will be going all recursive.
  1668. $data['contents'][$entry] = array(
  1669. 'type' => 'dir_recursive',
  1670. );
  1671. // Actually do the recursive stuff...
  1672. fetchPerms__recursive($path . '/' . $entry, $data['contents'][$entry], $level + 1);
  1673. }
  1674. // Maybe it is a folder we are not descending into.
  1675. elseif (isset($data['contents'][$entry]))
  1676. $foundData['folders'][$entry] = true;
  1677. // Otherwise we stop here.
  1678. }
  1679. }
  1680. closedir($dh);
  1681. // Nothing to see here?
  1682. if (!$save_data)
  1683. return;
  1684. // Now actually add the data, starting with the folders.
  1685. ksort($foundData['folders']);
  1686. foreach ($foundData['folders'] as $folder => $type)
  1687. {
  1688. $additional_data = array(
  1689. 'perms' => array(
  1690. 'chmod' => @is_writable($path . '/' . $folder),
  1691. 'perms' => @fileperms($path . '/' . $folder),
  1692. ),
  1693. );
  1694. if ($type !== true)
  1695. $additional_data['type'] = $type;
  1696. // If there's an offset ignore any folders in XML mode.
  1697. if (isset($_GET['xml']) && $context['file_offset'] == 0)
  1698. {
  1699. $context['xml_data']['folders']['children'][] = array(
  1700. 'attributes' => array(
  1701. 'writable' => $additional_data['perms']['chmod'] ? 1 : 0,
  1702. 'permissions' => substr(sprintf('%o', $additional_data['perms']['perms']), -4),
  1703. 'folder' => 1,
  1704. 'path' => $context['only_find'],
  1705. 'level' => $level,
  1706. 'more' => 0,
  1707. 'offset' => $context['file_offset'],
  1708. 'my_ident' => preg_replace('~[^A-Za-z0-9_\-=:]~', ':-:', $context['only_find'] . '/' . $folder),
  1709. 'ident' => preg_replace('~[^A-Za-z0-9_\-=:]~', ':-:', $context['only_find']),
  1710. ),
  1711. 'value' => $folder,
  1712. );
  1713. }
  1714. elseif (!isset($_GET['xml']))
  1715. {
  1716. if (isset($data['contents'][$folder]))
  1717. $data['contents'][$folder] = array_merge($data['contents'][$folder], $additional_data);
  1718. else
  1719. $data['contents'][$folder] = $additional_data;
  1720. }
  1721. }
  1722. // Now we want to do a similar thing with files.
  1723. ksort($foundData['files']);
  1724. $counter = -1;
  1725. foreach ($foundData['files'] as $file => $dummy)
  1726. {
  1727. $counter++;
  1728. // Have we reached our offset?
  1729. if ($context['file_offset'] > $counter)
  1730. continue;
  1731. // Gone too far?
  1732. if ($counter > ($context['file_offset'] + $context['file_limit']))
  1733. continue;
  1734. $additional_data = array(
  1735. 'perms' => array(
  1736. 'chmod' => @is_writable($path . '/' . $file),
  1737. 'perms' => @fileperms($path . '/' . $file),
  1738. ),
  1739. );
  1740. // XML?
  1741. if (isset($_GET['xml']))
  1742. {
  1743. $context['xml_data']['folders']['children'][] = array(
  1744. 'attributes' => array(
  1745. 'writable' => $additional_data['perms']['chmod'] ? 1 : 0,
  1746. 'permissions' => substr(sprintf('%o', $additional_data['perms']['perms']), -4),
  1747. 'folder' => 0,
  1748. 'path' => $context['only_find'],
  1749. 'level' => $level,
  1750. 'more' => $counter == ($context['file_offset'] + $context['file_limit']) ? 1 : 0,
  1751. 'offset' => $context['file_offset'],
  1752. 'my_ident' => preg_replace('~[^A-Za-z0-9_\-=:]~', ':-:', $context['only_find'] . '/' . $file),
  1753. 'ident' => preg_replace('~[^A-Za-z0-9_\-=:]~', ':-:', $context['only_find']),
  1754. ),
  1755. 'value' => $file,
  1756. );
  1757. }
  1758. elseif ($counter != ($context['file_offset'] + $context['file_limit']))
  1759. {
  1760. if (isset($data['contents'][$file]))
  1761. $data['contents'][$file] = array_merge($data['contents'][$file], $additional_data);
  1762. else
  1763. $data['contents'][$file] = $additional_data;
  1764. }
  1765. }
  1766. }
  1767. /**
  1768. * Actually action the permission changes they want.
  1769. */
  1770. function PackagePermissionsAction()
  1771. {
  1772. global $context, $txt, $time_start, $package_ftp;
  1773. umask(0);
  1774. $timeout_limit = 5;
  1775. $context['method'] = $_POST['method'] == 'individual' ? 'individual' : 'predefined';
  1776. $context['sub_template'] = 'action_permissions';
  1777. $context['page_title'] = $txt['package_file_perms_applying'];
  1778. $context['back_look_data'] = isset($_POST['back_look']) ? $_POST['back_look'] : array();
  1779. // Skipping use of FTP?
  1780. if (empty($package_ftp))
  1781. $context['skip_ftp'] = true;
  1782. // We'll start off in a good place, security. Make sure that if we're dealing with individual files that they seem in the right place.
  1783. if ($context['method'] == 'individual')
  1784. {
  1785. // Only these path roots are legal.
  1786. $legal_roots = array_keys($context['file_tree']);
  1787. $context['custom_value'] = (int) $_POST['custom_value'];
  1788. // Continuing?
  1789. if (isset($_POST['toProcess']))
  1790. $_POST['permStatus'] = unserialize(base64_decode($_POST['toProcess']));
  1791. if (isset($_POST['permStatus']))
  1792. {
  1793. $context['to_process'] = array();
  1794. $validate_custom = false;
  1795. foreach ($_POST['permStatus'] as $path => $status)
  1796. {
  1797. // Nothing to see here?
  1798. if ($status == 'no_change')
  1799. continue;
  1800. $legal = false;
  1801. foreach ($legal_roots as $root)
  1802. if (substr($path, 0, strlen($root)) == $root)
  1803. $legal = true;
  1804. if (!$legal)
  1805. continue;
  1806. // Check it exists.
  1807. if (!file_exists($path))
  1808. continue;
  1809. if ($status == 'custom')
  1810. $validate_custom = true;
  1811. // Now add it.
  1812. $context['to_process'][$path] = $status;
  1813. }
  1814. $context['total_items'] = isset($_POST['totalItems']) ? (int) $_POST['totalItems'] : count($context['to_process']);
  1815. // Make sure the chmod status is valid?
  1816. if ($validate_custom)
  1817. {
  1818. if (preg_match('~^[4567][4567][4567]$~', $context['custom_value']) == false)
  1819. fatal_error($txt['chmod_value_invalid']);
  1820. }
  1821. // Nothing to do?
  1822. if (empty($context['to_process']))
  1823. redirectexit('action=admin;area=packages;sa=perms' . (!empty($context['back_look_data']) ? ';back_look=' . base64_encode(serialize($context['back_look_data'])) : '') . ';' . $context['session_var'] . '=' . $context['session_id']);
  1824. }
  1825. // Should never get here,
  1826. else
  1827. fatal_lang_error('no_access', false);
  1828. // Setup the custom value.
  1829. $custom_value = octdec('0' . $context['custom_value']);
  1830. // Start processing items.
  1831. foreach ($context['to_process'] as $path => $status)
  1832. {
  1833. if (in_array($status, array('execute', 'writable', 'read')))
  1834. package_chmod($path, $status);
  1835. elseif ($status == 'custom' && !empty($custom_value))
  1836. {
  1837. // Use FTP if we have it.
  1838. if (!empty($package_ftp) && !empty($_SESSION['pack_ftp']))
  1839. {
  1840. $ftp_file = strtr($path, array($_SESSION['pack_ftp']['root'] => ''));
  1841. $package_ftp->chmod($ftp_file, $custom_value);
  1842. }
  1843. else
  1844. @chmod($path, $custom_value);
  1845. }
  1846. // This fish is fried...
  1847. unset($context['to_process'][$path]);
  1848. // See if we're out of time?
  1849. if (time() - array_sum(explode(' ', $time_start)) > $timeout_limit)
  1850. return false;
  1851. }
  1852. }
  1853. // If predefined this is a little different.
  1854. else
  1855. {
  1856. $context['predefined_type'] = isset($_POST['predefined']) ? $_POST['predefined'] : 'restricted';
  1857. $context['total_items'] = isset($_POST['totalItems']) ? (int) $_POST['totalItems'] : 0;
  1858. $context['directory_list'] = isset($_POST['dirList']) ? unserialize(base64_decode($_POST['dirList'])) : array();
  1859. $context['file_offset'] = isset($_POST['fileOffset']) ? (int) $_POST['fileOffset'] : 0;
  1860. // Haven't counted the items yet?
  1861. if (empty($context['total_items']))
  1862. {
  1863. function count_directories__recursive($dir)
  1864. {
  1865. global $context;
  1866. $count = 0;
  1867. $dh = @opendir($dir);
  1868. while ($entry = readdir($dh))
  1869. {
  1870. if ($entry != '.' && $entry != '..' && is_dir($dir . '/' . $entry))
  1871. {
  1872. $context['directory_list'][$dir . '/' . $entry] = 1;
  1873. $count++;
  1874. $count += count_directories__recursive($dir . '/' . $entry);
  1875. }
  1876. }
  1877. closedir($dh);
  1878. return $count;
  1879. }
  1880. foreach ($context['file_tree'] as $path => $data)
  1881. {
  1882. if (is_dir($path))
  1883. {
  1884. $context['directory_list'][$path] = 1;
  1885. $context['total_items'] += count_directories__recursive($path);
  1886. $context['total_items']++;
  1887. }
  1888. }
  1889. }
  1890. // Have we built up our list of special files?
  1891. if (!isset($_POST['specialFiles']) && $context['predefined_type'] != 'free')
  1892. {
  1893. $context['special_files'] = array();
  1894. function build_special_files__recursive($path, &$data)
  1895. {
  1896. global $context;
  1897. if (!empty($data['writable_on']))
  1898. if ($context['predefined_type'] == 'standard' || $data['writable_on'] == 'restrictive')
  1899. $context['special_files'][$path] = 1;
  1900. if (!empty($data['contents']))
  1901. foreach ($data['contents'] as $name => $contents)
  1902. build_special_files__recursive($path . '/' . $name, $contents);
  1903. }
  1904. foreach ($context['file_tree'] as $path => $data)
  1905. build_special_files__recursive($path, $data);
  1906. }
  1907. // Free doesn't need special files.
  1908. elseif ($context['predefined_type'] == 'free')
  1909. $context['special_files'] = array();
  1910. else
  1911. $context['special_files'] = unserialize(base64_decode($_POST['specialFiles']));
  1912. // Now we definitely know where we are, we need to go through again doing the chmod!
  1913. foreach ($context['directory_list'] as $path => $dummy)
  1914. {
  1915. // Do the contents of the directory first.
  1916. $dh = @opendir($path);
  1917. $file_count = 0;
  1918. $dont_chmod = false;
  1919. while ($entry = readdir($dh))
  1920. {
  1921. $file_count++;
  1922. // Actually process this file?
  1923. if (!$dont_chmod && !is_dir($path . '/' . $entry) && (empty($context['file_offset']) || $context['file_offset'] < $file_count))
  1924. {
  1925. $status = $context['predefined_type'] == 'free' || isset($context['special_files'][$path . '/' . $entry]) ? 'writable' : 'execute';
  1926. package_chmod($path . '/' . $entry, $status);
  1927. }
  1928. // See if we're out of time?
  1929. if (!$dont_chmod && time() - array_sum(explode(' ', $time_start)) > $timeout_limit)
  1930. {
  1931. $dont_chmod = true;
  1932. // Don't do this again.
  1933. $context['file_offset'] = $file_count;
  1934. }
  1935. }
  1936. closedir($dh);
  1937. // If this is set it means we timed out half way through.
  1938. if ($dont_chmod)
  1939. {
  1940. $context['total_files'] = $file_count;
  1941. return false;
  1942. }
  1943. // Do the actual directory.
  1944. $status = $context['predefined_type'] == 'free' || isset($context['special_files'][$path]) ? 'writable' : 'execute';
  1945. package_chmod($path, $status);
  1946. // We've finished the directory so no file offset, and no record.
  1947. $context['file_offset'] = 0;
  1948. unset($context['directory_list'][$path]);
  1949. // See if we're out of time?
  1950. if (time() - array_sum(explode(' ', $time_start)) > $timeout_limit)
  1951. return false;
  1952. }
  1953. }
  1954. // If we're here we are done!
  1955. redirectexit('action=admin;area=packages;sa=perms' . (!empty($context['back_look_data']) ? ';back_look=' . base64_encode(serialize($context['back_look_data'])) : '') . ';' . $context['session_var'] . '=' . $context['session_id']);
  1956. }
  1957. /**
  1958. * Test an FTP connection.
  1959. */
  1960. function PackageFTPTest()
  1961. {
  1962. global $context, $txt, $package_ftp;
  1963. checkSession('get');
  1964. // Try to make the FTP connection.
  1965. create_chmod_control(array(), array('force_find_error' => true));
  1966. // Deal with the template stuff.
  1967. loadTemplate('Xml');
  1968. $context['sub_template'] = 'generic_xml';
  1969. $context['template_layers'] = array();
  1970. // Define the return data, this is simple.
  1971. $context['xml_data'] = array(
  1972. 'results' => array(
  1973. 'identifier' => 'result',
  1974. 'children' => array(
  1975. array(
  1976. 'attributes' => array(
  1977. 'success' => !empty($package_ftp) ? 1 : 0,
  1978. ),
  1979. 'value' => !empty($package_ftp) ? $txt['package_ftp_test_success'] : (isset($context['package_ftp'], $context['package_ftp']['error']) ? $context['package_ftp']['error'] : $txt['package_ftp_test_failed']),
  1980. ),
  1981. ),
  1982. ),
  1983. );
  1984. }
  1985. ?>