Packages.php 79 KB

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