Packages.php 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292
  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. array(
  883. 'current_member' => $user_info['id'],
  884. 'not_installed' => 0,
  885. 'current_time' => time(),
  886. 'package_id' => $row['package_id'],
  887. 'member_name' => $user_info['name'],
  888. )
  889. );
  890. }
  891. // Otherwise must be an upgrade.
  892. else
  893. {
  894. $is_upgrade = true;
  895. $old_db_changes = empty($row['db_changes']) ? array() : unserialize($row['db_changes']);
  896. }
  897. }
  898. // Assuming we're not uninstalling, add the entry.
  899. if (!$context['uninstalling'])
  900. {
  901. // Any db changes from older version?
  902. if (!empty($old_db_changes))
  903. $db_package_log = empty($db_package_log) ? $old_db_changes : array_merge($old_db_changes, $db_package_log);
  904. // If there are some database changes we might want to remove then filter them out.
  905. if (!empty($db_package_log))
  906. {
  907. // We're really just checking for entries which are create table AND add columns (etc).
  908. $tables = array();
  909. function sort_table_first($a, $b)
  910. {
  911. if ($a[0] == $b[0])
  912. return 0;
  913. return $a[0] == 'remove_table' ? -1 : 1;
  914. }
  915. usort($db_package_log, 'sort_table_first');
  916. foreach ($db_package_log as $k => $log)
  917. {
  918. if ($log[0] == 'remove_table')
  919. $tables[] = $log[1];
  920. elseif (in_array($log[1], $tables))
  921. unset($db_package_log[$k]);
  922. }
  923. $db_changes = serialize($db_package_log);
  924. }
  925. else
  926. $db_changes = '';
  927. // What themes did we actually install?
  928. $themes_installed = array_unique($themes_installed);
  929. $themes_installed = implode(',', $themes_installed);
  930. // What failed steps?
  931. $failed_step_insert = serialize($failed_steps);
  932. $smcFunc['db_insert']('',
  933. '{db_prefix}log_packages',
  934. array(
  935. 'filename' => 'string', 'name' => 'string', 'package_id' => 'string', 'version' => 'string',
  936. 'id_member_installed' => 'int', 'member_installed' => 'string','time_installed' => 'int',
  937. 'install_state' => 'int', 'failed_steps' => 'string', 'themes_installed' => 'string',
  938. 'member_removed' => 'int', 'db_changes' => 'string',
  939. ),
  940. array(
  941. $packageInfo['filename'], $packageInfo['name'], $packageInfo['id'], $packageInfo['version'],
  942. $user_info['id'], $user_info['name'], time(),
  943. $is_upgrade ? 2 : 1, $failed_step_insert, $themes_installed,
  944. 0, $db_changes,
  945. ),
  946. array('id_install')
  947. );
  948. }
  949. $smcFunc['db_free_result']($request);
  950. $context['install_finished'] = true;
  951. }
  952. // If there's database changes - and they want them removed - let's do it last!
  953. if (!empty($db_changes) && !empty($_POST['do_db_changes']))
  954. {
  955. // We're gonna be needing the package db functions!
  956. db_extend('packages');
  957. foreach ($db_changes as $change)
  958. {
  959. if ($change[0] == 'remove_table' && isset($change[1]))
  960. $smcFunc['db_drop_table']($change[1]);
  961. elseif ($change[0] == 'remove_column' && isset($change[2]))
  962. $smcFunc['db_remove_column']($change[1], $change[2]);
  963. elseif ($change[0] == 'remove_index' && isset($change[2]))
  964. $smcFunc['db_remove_index']($change[1], $change[2]);
  965. }
  966. }
  967. // Clean house... get rid of the evidence ;).
  968. if (file_exists($boarddir . '/Packages/temp'))
  969. deltree($boarddir . '/Packages/temp');
  970. // Log what we just did.
  971. logAction($context['uninstalling'] ? 'uninstall_package' : (!empty($is_upgrade) ? 'upgrade_package' : 'install_package'), array('package' => $smcFunc['htmlspecialchars']($packageInfo['name']), 'version' => $smcFunc['htmlspecialchars']($packageInfo['version'])), 'admin');
  972. // Just in case, let's clear the whole cache to avoid anything going up the swanny.
  973. clean_cache();
  974. // Restore file permissions?
  975. create_chmod_control(array(), array(), true);
  976. }
  977. /**
  978. * List the files in a package.
  979. */
  980. function PackageList()
  981. {
  982. global $txt, $scripturl, $boarddir, $context, $sourcedir;
  983. require_once($sourcedir . '/Subs-Package.php');
  984. // No package? Show him or her the door.
  985. if (!isset($_REQUEST['package']) || $_REQUEST['package'] == '')
  986. redirectexit('action=admin;area=packages');
  987. $context['linktree'][] = array(
  988. 'url' => $scripturl . '?action=admin;area=packages;sa=list;package=' . $_REQUEST['package'],
  989. 'name' => $txt['list_file']
  990. );
  991. $context['page_title'] .= ' - ' . $txt['list_file'];
  992. $context['sub_template'] = 'list';
  993. // The filename...
  994. $context['filename'] = $_REQUEST['package'];
  995. // Let the unpacker do the work.
  996. if (is_file($boarddir . '/Packages/' . $context['filename']))
  997. $context['files'] = read_tgz_file($boarddir . '/Packages/' . $context['filename'], null);
  998. elseif (is_dir($boarddir . '/Packages/' . $context['filename']))
  999. $context['files'] = listtree($boarddir . '/Packages/' . $context['filename']);
  1000. }
  1001. /**
  1002. * Display one of the files in a package.
  1003. */
  1004. function ExamineFile()
  1005. {
  1006. global $txt, $scripturl, $boarddir, $context, $sourcedir;
  1007. require_once($sourcedir . '/Subs-Package.php');
  1008. // No package? Show him or her the door.
  1009. if (!isset($_REQUEST['package']) || $_REQUEST['package'] == '')
  1010. redirectexit('action=admin;area=packages');
  1011. // No file? Show him or her the door.
  1012. if (!isset($_REQUEST['file']) || $_REQUEST['file'] == '')
  1013. redirectexit('action=admin;area=packages');
  1014. $_REQUEST['package'] = preg_replace('~[\.]+~', '.', strtr($_REQUEST['package'], array('/' => '_', '\\' => '_')));
  1015. $_REQUEST['file'] = preg_replace('~[\.]+~', '.', $_REQUEST['file']);
  1016. if (isset($_REQUEST['raw']))
  1017. {
  1018. if (is_file($boarddir . '/Packages/' . $_REQUEST['package']))
  1019. echo read_tgz_file($boarddir . '/Packages/' . $_REQUEST['package'], $_REQUEST['file'], true);
  1020. elseif (is_dir($boarddir . '/Packages/' . $_REQUEST['package']))
  1021. echo file_get_contents($boarddir . '/Packages/' . $_REQUEST['package'] . '/' . $_REQUEST['file']);
  1022. obExit(false);
  1023. }
  1024. $context['linktree'][count($context['linktree']) - 1] = array(
  1025. 'url' => $scripturl . '?action=admin;area=packages;sa=list;package=' . $_REQUEST['package'],
  1026. 'name' => $txt['package_examine_file']
  1027. );
  1028. $context['page_title'] .= ' - ' . $txt['package_examine_file'];
  1029. $context['sub_template'] = 'examine';
  1030. // The filename...
  1031. $context['package'] = $_REQUEST['package'];
  1032. $context['filename'] = $_REQUEST['file'];
  1033. // Let the unpacker do the work.... but make sure we handle images properly.
  1034. if (in_array(strtolower(strrchr($_REQUEST['file'], '.')), array('.bmp', '.gif', '.jpeg', '.jpg', '.png')))
  1035. $context['filedata'] = '<img src="' . $scripturl . '?action=admin;area=packages;sa=examine;package=' . $_REQUEST['package'] . ';file=' . $_REQUEST['file'] . ';raw" alt="' . $_REQUEST['file'] . '" />';
  1036. else
  1037. {
  1038. if (is_file($boarddir . '/Packages/' . $_REQUEST['package']))
  1039. $context['filedata'] = htmlspecialchars(read_tgz_file($boarddir . '/Packages/' . $_REQUEST['package'], $_REQUEST['file'], true));
  1040. elseif (is_dir($boarddir . '/Packages/' . $_REQUEST['package']))
  1041. $context['filedata'] = htmlspecialchars(file_get_contents($boarddir . '/Packages/' . $_REQUEST['package'] . '/' . $_REQUEST['file']));
  1042. if (strtolower(strrchr($_REQUEST['file'], '.')) == '.php')
  1043. $context['filedata'] = highlight_php_code($context['filedata']);
  1044. }
  1045. }
  1046. /**
  1047. * List the installed packages.
  1048. */
  1049. function InstalledList()
  1050. {
  1051. global $txt, $scripturl, $context;
  1052. $context['page_title'] .= ' - ' . $txt['installed_packages'];
  1053. $context['sub_template'] = 'view_installed';
  1054. // Load the installed mods and send them to the template.
  1055. $context['installed_mods'] = loadInstalledPackages();
  1056. }
  1057. /**
  1058. * Empty out the installed list.
  1059. */
  1060. function FlushInstall()
  1061. {
  1062. global $boarddir, $sourcedir, $smcFunc;
  1063. // Always check the session.
  1064. checkSession('get');
  1065. include_once($sourcedir . '/Subs-Package.php');
  1066. // Record when we last did this.
  1067. package_put_contents($boarddir . '/Packages/installed.list', time());
  1068. // Set everything as uninstalled.
  1069. $smcFunc['db_query']('', '
  1070. UPDATE {db_prefix}log_packages
  1071. SET install_state = {int:not_installed}',
  1072. array(
  1073. 'not_installed' => 0,
  1074. )
  1075. );
  1076. redirectexit('action=admin;area=packages;sa=installed');
  1077. }
  1078. /**
  1079. * Delete a package.
  1080. */
  1081. function PackageRemove()
  1082. {
  1083. global $scripturl, $boarddir;
  1084. // Check it.
  1085. checkSession('get');
  1086. // Ack, don't allow deletion of arbitrary files here, could become a security hole somehow!
  1087. if (!isset($_GET['package']) || $_GET['package'] == 'index.php' || $_GET['package'] == 'installed.list' || $_GET['package'] == 'backups')
  1088. redirectexit('action=admin;area=packages;sa=browse');
  1089. $_GET['package'] = preg_replace('~[\.]+~', '.', strtr($_GET['package'], array('/' => '_', '\\' => '_')));
  1090. // Can't delete what's not there.
  1091. 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)
  1092. {
  1093. 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));
  1094. if (is_dir($boarddir . '/Packages/' . $_GET['package']))
  1095. deltree($boarddir . '/Packages/' . $_GET['package']);
  1096. else
  1097. {
  1098. @chmod($boarddir . '/Packages/' . $_GET['package'], 0777);
  1099. unlink($boarddir . '/Packages/' . $_GET['package']);
  1100. }
  1101. }
  1102. redirectexit('action=admin;area=packages;sa=browse');
  1103. }
  1104. /**
  1105. * Browse a list of installed packages.
  1106. */
  1107. function PackageBrowse()
  1108. {
  1109. global $txt, $boarddir, $scripturl, $context, $forum_version;
  1110. $context['page_title'] .= ' - ' . $txt['browse_packages'];
  1111. $context['sub_template'] = 'browse';
  1112. $context['forum_version'] = $forum_version;
  1113. $instmods = loadInstalledPackages();
  1114. $installed_mods = array();
  1115. // Look through the list of installed mods...
  1116. foreach ($instmods as $installed_mod)
  1117. $installed_mods[$installed_mod['package_id']] = array(
  1118. 'id' => $installed_mod['id'],
  1119. 'version' => $installed_mod['version'],
  1120. );
  1121. $the_version = strtr($forum_version, array('SMF ' => ''));
  1122. // Here we have a little code to help those who class themselves as something of gods, version emulation ;)
  1123. if (isset($_GET['version_emulate']))
  1124. {
  1125. if ($_GET['version_emulate'] === 0 && isset($_SESSION['version_emulate']))
  1126. unset($_SESSION['version_emulate']);
  1127. elseif ($_GET['version_emulate'] !== 0)
  1128. $_SESSION['version_emulate'] = strtr($_GET['version_emulate'], array('-' => ' ', '+' => ' ', 'SMF ' => ''));
  1129. }
  1130. if (!empty($_SESSION['version_emulate']))
  1131. {
  1132. $context['forum_version'] = 'SMF ' . $_SESSION['version_emulate'];
  1133. $the_version = $_SESSION['version_emulate'];
  1134. }
  1135. // Get a list of all the ids installed, so the latest packages won't include already installed ones.
  1136. $context['installed_mods'] = array_keys($installed_mods);
  1137. // Empty lists for now.
  1138. $context['available_mods'] = array();
  1139. $context['available_avatars'] = array();
  1140. $context['available_languages'] = array();
  1141. $context['available_other'] = array();
  1142. $context['available_all'] = array();
  1143. // We need the packages directory to be writable for this.
  1144. if (!@is_writable($boarddir . '/Packages'))
  1145. create_chmod_control(array($boarddir . '/Packages'), array('destination_url' => $scripturl . '?action=admin;area=packages', 'crash_on_error' => true));
  1146. if ($dir = @opendir($boarddir . '/Packages'))
  1147. {
  1148. $dirs = array();
  1149. while ($package = readdir($dir))
  1150. {
  1151. 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'))
  1152. continue;
  1153. // Skip directories or files that are named the same.
  1154. if (is_dir($boarddir . '/Packages/' . $package))
  1155. {
  1156. if (in_array($package, $dirs))
  1157. continue;
  1158. $dirs[] = $package;
  1159. }
  1160. elseif (substr(strtolower($package), -7) == '.tar.gz')
  1161. {
  1162. if (in_array(substr($package, 0, -7), $dirs))
  1163. continue;
  1164. $dirs[] = substr($package, 0, -7);
  1165. }
  1166. elseif (substr(strtolower($package), -4) == '.zip' || substr(strtolower($package), -4) == '.tgz')
  1167. {
  1168. if (in_array(substr($package, 0, -4), $dirs))
  1169. continue;
  1170. $dirs[] = substr($package, 0, -4);
  1171. }
  1172. $packageInfo = getPackageInfo($package);
  1173. if (!is_array($packageInfo))
  1174. continue;
  1175. $packageInfo['installed_id'] = isset($installed_mods[$packageInfo['id']]) ? $installed_mods[$packageInfo['id']]['id'] : 0;
  1176. $packageInfo['is_installed'] = isset($installed_mods[$packageInfo['id']]);
  1177. $packageInfo['is_current'] = $packageInfo['is_installed'] && ($installed_mods[$packageInfo['id']]['version'] == $packageInfo['version']);
  1178. $packageInfo['is_newer'] = $packageInfo['is_installed'] && ($installed_mods[$packageInfo['id']]['version'] > $packageInfo['version']);
  1179. $packageInfo['can_install'] = false;
  1180. $packageInfo['can_uninstall'] = false;
  1181. $packageInfo['can_upgrade'] = false;
  1182. // This package is currently NOT installed. Check if it can be.
  1183. if (!$packageInfo['is_installed'] && $packageInfo['xml']->exists('install'))
  1184. {
  1185. // Check if there's an install for *THIS* version of SMF.
  1186. $installs = $packageInfo['xml']->set('install');
  1187. foreach ($installs as $install)
  1188. {
  1189. if (!$install->exists('@for') || matchPackageVersion($the_version, $install->fetch('@for')))
  1190. {
  1191. // Okay, this one is good to go.
  1192. $packageInfo['can_install'] = true;
  1193. break;
  1194. }
  1195. }
  1196. }
  1197. // An already installed, but old, package. Can we upgrade it?
  1198. elseif ($packageInfo['is_installed'] && !$packageInfo['is_current'] && $packageInfo['xml']->exists('upgrade'))
  1199. {
  1200. $upgrades = $packageInfo['xml']->set('upgrade');
  1201. // First go through, and check against the current version of SMF.
  1202. foreach ($upgrades as $upgrade)
  1203. {
  1204. // Even if it is for this SMF, is it for the installed version of the mod?
  1205. if (!$upgrade->exists('@for') || matchPackageVersion($the_version, $upgrade->fetch('@for')))
  1206. if (!$upgrade->exists('@from') || matchPackageVersion($installed_mods[$packageInfo['id']]['version'], $upgrade->fetch('@from')))
  1207. {
  1208. $packageInfo['can_upgrade'] = true;
  1209. break;
  1210. }
  1211. }
  1212. }
  1213. // Note that it has to be the current version to be uninstallable. Shucks.
  1214. elseif ($packageInfo['is_installed'] && $packageInfo['is_current'] && $packageInfo['xml']->exists('uninstall'))
  1215. {
  1216. $uninstalls = $packageInfo['xml']->set('uninstall');
  1217. // Can we find any uninstallation methods that work for this SMF version?
  1218. foreach ($uninstalls as $uninstall)
  1219. if (!$uninstall->exists('@for') || matchPackageVersion($the_version, $uninstall->fetch('@for')))
  1220. {
  1221. $packageInfo['can_uninstall'] = true;
  1222. break;
  1223. }
  1224. }
  1225. // Store a complete list.
  1226. $context['available_all'][] = $packageInfo;
  1227. // Modification.
  1228. if ($packageInfo['type'] == 'modification' || $packageInfo['type'] == 'mod')
  1229. $context['available_mods'][] = $packageInfo;
  1230. // Avatar package.
  1231. elseif ($packageInfo['type'] == 'avatar')
  1232. $context['available_avatars'][] = $packageInfo;
  1233. // Language package.
  1234. elseif ($packageInfo['type'] == 'language')
  1235. $context['available_languages'][] = $packageInfo;
  1236. // Other stuff.
  1237. else
  1238. $context['available_other'][] = $packageInfo;
  1239. }
  1240. closedir($dir);
  1241. }
  1242. }
  1243. function PackageOptions()
  1244. {
  1245. global $txt, $scripturl, $context, $sourcedir, $modSettings;
  1246. if (isset($_POST['submit']))
  1247. {
  1248. checkSession('post');
  1249. updateSettings(array(
  1250. 'package_server' => trim($smcFunc['htmlspecialchars']($_POST['pack_server'])),
  1251. 'package_port' => trim($smcFunc['htmlspecialchars']($_POST['pack_port'])),
  1252. 'package_username' => trim($smcFunc['htmlspecialchars']($_POST['pack_user'])),
  1253. 'package_make_backups' => !empty($_POST['package_make_backups']),
  1254. 'package_make_full_backups' => !empty($_POST['package_make_full_backups'])
  1255. ));
  1256. redirectexit('action=admin;area=packages;sa=options');
  1257. }
  1258. if (preg_match('~^/home\d*/([^/]+?)/public_html~', $_SERVER['DOCUMENT_ROOT'], $match))
  1259. $default_username = $match[1];
  1260. else
  1261. $default_username = '';
  1262. $context['page_title'] = $txt['package_settings'];
  1263. $context['sub_template'] = 'install_options';
  1264. $context['package_ftp_server'] = isset($modSettings['package_server']) ? $modSettings['package_server'] : 'localhost';
  1265. $context['package_ftp_port'] = isset($modSettings['package_port']) ? $modSettings['package_port'] : '21';
  1266. $context['package_ftp_username'] = isset($modSettings['package_username']) ? $modSettings['package_username'] : $default_username;
  1267. $context['package_make_backups'] = !empty($modSettings['package_make_backups']);
  1268. $context['package_make_full_backups'] = !empty($modSettings['package_make_full_backups']);
  1269. }
  1270. /**
  1271. * List operations
  1272. */
  1273. function ViewOperations()
  1274. {
  1275. global $context, $txt, $boarddir, $sourcedir, $smcFunc, $modSettings;
  1276. // Can't be in here buddy.
  1277. isAllowedTo('admin_forum');
  1278. // We need to know the operation key for the search and replace, mod file looking at, is it a board mod?
  1279. if (!isset($_REQUEST['operation_key'], $_REQUEST['filename']) && !is_numeric($_REQUEST['operation_key']))
  1280. fatal_lang_error('operation_invalid', 'general');
  1281. // Load the required file.
  1282. require_once($sourcedir . '/Subs-Package.php');
  1283. // Uninstalling the mod?
  1284. $reverse = isset($_REQUEST['reverse']) ? true : false;
  1285. // Get the base name.
  1286. $context['filename'] = preg_replace('~[\.]+~', '.', $_REQUEST['package']);
  1287. // We need to extract this again.
  1288. if (is_file($boarddir . '/Packages/' . $context['filename']))
  1289. {
  1290. $context['extracted_files'] = read_tgz_file($boarddir . '/Packages/' . $context['filename'], $boarddir . '/Packages/temp');
  1291. if ($context['extracted_files'] && !file_exists($boarddir . '/Packages/temp/package-info.xml'))
  1292. foreach ($context['extracted_files'] as $file)
  1293. if (basename($file['filename']) == 'package-info.xml')
  1294. {
  1295. $context['base_path'] = dirname($file['filename']) . '/';
  1296. break;
  1297. }
  1298. if (!isset($context['base_path']))
  1299. $context['base_path'] = '';
  1300. }
  1301. elseif (is_dir($boarddir . '/Packages/' . $context['filename']))
  1302. {
  1303. copytree($boarddir . '/Packages/' . $context['filename'], $boarddir . '/Packages/temp');
  1304. $context['extracted_files'] = listtree($boarddir . '/Packages/temp');
  1305. $context['base_path'] = '';
  1306. }
  1307. // Load up any custom themes we may want to install into...
  1308. $request = $smcFunc['db_query']('', '
  1309. SELECT id_theme, variable, value
  1310. FROM {db_prefix}themes
  1311. WHERE (id_theme = {int:default_theme} OR id_theme IN ({array_int:known_theme_list}))
  1312. AND variable IN ({string:name}, {string:theme_dir})',
  1313. array(
  1314. 'known_theme_list' => explode(',', $modSettings['knownThemes']),
  1315. 'default_theme' => 1,
  1316. 'name' => 'name',
  1317. 'theme_dir' => 'theme_dir',
  1318. )
  1319. );
  1320. $theme_paths = array();
  1321. while ($row = $smcFunc['db_fetch_assoc']($request))
  1322. $theme_paths[$row['id_theme']][$row['variable']] = $row['value'];
  1323. $smcFunc['db_free_result']($request);
  1324. // Boardmod?
  1325. if (isset($_REQUEST['boardmod']))
  1326. $mod_actions = parseBoardMod(@file_get_contents($boarddir . '/Packages/temp/' . $context['base_path'] . $_REQUEST['filename']), true, $reverse, $theme_paths);
  1327. else
  1328. $mod_actions = parseModification(@file_get_contents($boarddir . '/Packages/temp/' . $context['base_path'] . $_REQUEST['filename']), true, $reverse, $theme_paths);
  1329. // Ok lets get the content of the file.
  1330. $context['operations'] = array(
  1331. 'search' => strtr(htmlspecialchars($mod_actions[$_REQUEST['operation_key']]['search_original']), array('[' => '&#91;', ']' => '&#93;')),
  1332. 'replace' => strtr(htmlspecialchars($mod_actions[$_REQUEST['operation_key']]['replace_original']), array('[' => '&#91;', ']' => '&#93;')),
  1333. 'position' => $mod_actions[$_REQUEST['operation_key']]['position'],
  1334. );
  1335. // Let's do some formatting...
  1336. $operation_text = $context['operations']['position'] == 'replace' ? 'operation_replace' : ($context['operations']['position'] == 'before' ? 'operation_after' : 'operation_before');
  1337. $context['operations']['search'] = parse_bbc('[code=' . $txt['operation_find'] . ']' . ($context['operations']['position'] == 'end' ? '?&gt;' : $context['operations']['search']) . '[/code]');
  1338. $context['operations']['replace'] = parse_bbc('[code=' . $txt[$operation_text] . ']' . $context['operations']['replace'] . '[/code]');
  1339. // No layers
  1340. $context['template_layers'] = array();
  1341. $context['sub_template'] = 'view_operations';
  1342. }
  1343. /**
  1344. * Allow the admin to reset permissions on files.
  1345. */
  1346. function PackagePermissions()
  1347. {
  1348. global $context, $txt, $modSettings, $boarddir, $sourcedir, $cachedir, $smcFunc, $package_ftp;
  1349. // Let's try and be good, yes?
  1350. checkSession('get');
  1351. // If we're restoring permissions this is just a pass through really.
  1352. if (isset($_GET['restore']))
  1353. {
  1354. create_chmod_control(array(), array(), true);
  1355. fatal_lang_error('no_access', false);
  1356. }
  1357. // This is a memory eat.
  1358. @ini_set('memory_limit', '128M');
  1359. @set_time_limit(600);
  1360. // Load up some FTP stuff.
  1361. create_chmod_control();
  1362. if (empty($package_ftp) && !isset($_POST['skip_ftp']))
  1363. {
  1364. loadClassFile('Class-Package.php');
  1365. $ftp = new ftp_connection(null);
  1366. list ($username, $detect_path, $found_path) = $ftp->detect_path($boarddir);
  1367. $context['package_ftp'] = array(
  1368. 'server' => isset($modSettings['package_server']) ? $modSettings['package_server'] : 'localhost',
  1369. 'port' => isset($modSettings['package_port']) ? $modSettings['package_port'] : '21',
  1370. 'username' => empty($username) ? (isset($modSettings['package_username']) ? $modSettings['package_username'] : '') : $username,
  1371. 'path' => $detect_path,
  1372. 'form_elements_only' => true,
  1373. );
  1374. }
  1375. else
  1376. $context['ftp_connected'] = true;
  1377. // Define the template.
  1378. $context['page_title'] = $txt['package_file_perms'];
  1379. $context['sub_template'] = 'file_permissions';
  1380. // Define what files we're interested in, as a tree.
  1381. $context['file_tree'] = array(
  1382. strtr($boarddir, array('\\' => '/')) => array(
  1383. 'type' => 'dir',
  1384. 'contents' => array(
  1385. 'agreement.txt' => array(
  1386. 'type' => 'file',
  1387. 'writable_on' => 'standard',
  1388. ),
  1389. 'Settings.php' => array(
  1390. 'type' => 'file',
  1391. 'writable_on' => 'restrictive',
  1392. ),
  1393. 'Settings_bak.php' => array(
  1394. 'type' => 'file',
  1395. 'writable_on' => 'restrictive',
  1396. ),
  1397. 'attachments' => array(
  1398. 'type' => 'dir',
  1399. 'writable_on' => 'restrictive',
  1400. ),
  1401. 'avatars' => array(
  1402. 'type' => 'dir',
  1403. 'writable_on' => 'standard',
  1404. ),
  1405. 'cache' => array(
  1406. 'type' => 'dir',
  1407. 'writable_on' => 'restrictive',
  1408. ),
  1409. 'custom_avatar_dir' => array(
  1410. 'type' => 'dir',
  1411. 'writable_on' => 'restrictive',
  1412. ),
  1413. 'Smileys' => array(
  1414. 'type' => 'dir_recursive',
  1415. 'writable_on' => 'standard',
  1416. ),
  1417. 'Sources' => array(
  1418. 'type' => 'dir',
  1419. 'list_contents' => true,
  1420. 'writable_on' => 'standard',
  1421. ),
  1422. 'Themes' => array(
  1423. 'type' => 'dir_recursive',
  1424. 'writable_on' => 'standard',
  1425. 'contents' => array(
  1426. 'default' => array(
  1427. 'type' => 'dir_recursive',
  1428. 'list_contents' => true,
  1429. 'contents' => array(
  1430. 'languages' => array(
  1431. 'type' => 'dir',
  1432. 'list_contents' => true,
  1433. ),
  1434. ),
  1435. ),
  1436. ),
  1437. ),
  1438. 'Packages' => array(
  1439. 'type' => 'dir',
  1440. 'writable_on' => 'standard',
  1441. 'contents' => array(
  1442. 'temp' => array(
  1443. 'type' => 'dir',
  1444. ),
  1445. 'backup' => array(
  1446. 'type' => 'dir',
  1447. ),
  1448. 'installed.list' => array(
  1449. 'type' => 'file',
  1450. 'writable_on' => 'standard',
  1451. ),
  1452. ),
  1453. ),
  1454. ),
  1455. ),
  1456. );
  1457. // Directories that can move.
  1458. if (substr($sourcedir, 0, strlen($boarddir)) != $boarddir)
  1459. {
  1460. unset($context['file_tree'][strtr($boarddir, array('\\' => '/'))]['contents']['Sources']);
  1461. $context['file_tree'][strtr($sourcedir, array('\\' => '/'))] = array(
  1462. 'type' => 'dir',
  1463. 'list_contents' => true,
  1464. 'writable_on' => 'standard',
  1465. );
  1466. }
  1467. // Moved the cache?
  1468. if (substr($cachedir, 0, strlen($boarddir)) != $boarddir)
  1469. {
  1470. unset($context['file_tree'][strtr($boarddir, array('\\' => '/'))]['contents']['cache']);
  1471. $context['file_tree'][strtr($cachedir, array('\\' => '/'))] = array(
  1472. 'type' => 'dir',
  1473. 'list_contents' => false,
  1474. 'writable_on' => 'restrictive',
  1475. );
  1476. }
  1477. // Are we using multiple attachment directories?
  1478. if (!empty($modSettings['currentAttachmentUploadDir']))
  1479. {
  1480. unset($context['file_tree'][strtr($boarddir, array('\\' => '/'))]['contents']['attachments']);
  1481. if (!is_array($modSettings['attachmentUploadDir']))
  1482. $modSettings['attachmentUploadDir'] = unserialize($modSettings['attachmentUploadDir']);
  1483. /**
  1484. * @todo Should we suggest non-current directories be read only?
  1485. */
  1486. foreach ($modSettings['attachmentUploadDir'] as $dir)
  1487. $context['file_tree'][strtr($dir, array('\\' => '/'))] = array(
  1488. 'type' => 'dir',
  1489. 'writable_on' => 'restrictive',
  1490. );
  1491. }
  1492. elseif (substr($modSettings['attachmentUploadDir'], 0, strlen($boarddir)) != $boarddir)
  1493. {
  1494. unset($context['file_tree'][strtr($boarddir, array('\\' => '/'))]['contents']['attachments']);
  1495. $context['file_tree'][strtr($modSettings['attachmentUploadDir'], array('\\' => '/'))] = array(
  1496. 'type' => 'dir',
  1497. 'writable_on' => 'restrictive',
  1498. );
  1499. }
  1500. if (substr($modSettings['smileys_dir'], 0, strlen($boarddir)) != $boarddir)
  1501. {
  1502. unset($context['file_tree'][strtr($boarddir, array('\\' => '/'))]['contents']['Smileys']);
  1503. $context['file_tree'][strtr($modSettings['smileys_dir'], array('\\' => '/'))] = array(
  1504. 'type' => 'dir_recursive',
  1505. 'writable_on' => 'standard',
  1506. );
  1507. }
  1508. if (substr($modSettings['avatar_directory'], 0, strlen($boarddir)) != $boarddir)
  1509. {
  1510. unset($context['file_tree'][strtr($boarddir, array('\\' => '/'))]['contents']['avatars']);
  1511. $context['file_tree'][strtr($modSettings['avatar_directory'], array('\\' => '/'))] = array(
  1512. 'type' => 'dir',
  1513. 'writable_on' => 'standard',
  1514. );
  1515. }
  1516. if (isset($modSettings['custom_avatar_dir']) && substr($modSettings['custom_avatar_dir'], 0, strlen($boarddir)) != $boarddir)
  1517. {
  1518. unset($context['file_tree'][strtr($boarddir, array('\\' => '/'))]['contents']['custom_avatar_dir']);
  1519. $context['file_tree'][strtr($modSettings['custom_avatar_dir'], array('\\' => '/'))] = array(
  1520. 'type' => 'dir',
  1521. 'writable_on' => 'restrictive',
  1522. );
  1523. }
  1524. // Load up any custom themes.
  1525. $request = $smcFunc['db_query']('', '
  1526. SELECT value
  1527. FROM {db_prefix}themes
  1528. WHERE id_theme > {int:default_theme_id}
  1529. AND id_member = {int:guest_id}
  1530. AND variable = {string:theme_dir}
  1531. ORDER BY value ASC',
  1532. array(
  1533. 'default_theme_id' => 1,
  1534. 'guest_id' => 0,
  1535. 'theme_dir' => 'theme_dir',
  1536. )
  1537. );
  1538. while ($row = $smcFunc['db_fetch_assoc']($request))
  1539. {
  1540. if (substr(strtolower(strtr($row['value'], array('\\' => '/'))), 0, strlen($boarddir) + 7) == strtolower(strtr($boarddir, array('\\' => '/')) . '/Themes'))
  1541. $context['file_tree'][strtr($boarddir, array('\\' => '/'))]['contents']['Themes']['contents'][substr($row['value'], strlen($boarddir) + 8)] = array(
  1542. 'type' => 'dir_recursive',
  1543. 'list_contents' => true,
  1544. 'contents' => array(
  1545. 'languages' => array(
  1546. 'type' => 'dir',
  1547. 'list_contents' => true,
  1548. ),
  1549. ),
  1550. );
  1551. else
  1552. {
  1553. $context['file_tree'][strtr($row['value'], array('\\' => '/'))] = array(
  1554. 'type' => 'dir_recursive',
  1555. 'list_contents' => true,
  1556. 'contents' => array(
  1557. 'languages' => array(
  1558. 'type' => 'dir',
  1559. 'list_contents' => true,
  1560. ),
  1561. ),
  1562. );
  1563. }
  1564. }
  1565. $smcFunc['db_free_result']($request);
  1566. // If we're submitting then let's move on to another function to keep things cleaner..
  1567. if (isset($_POST['action_changes']))
  1568. return PackagePermissionsAction();
  1569. $context['look_for'] = array();
  1570. // Are we looking for a particular tree - normally an expansion?
  1571. if (!empty($_REQUEST['find']))
  1572. $context['look_for'][] = base64_decode($_REQUEST['find']);
  1573. // Only that tree?
  1574. $context['only_find'] = isset($_GET['xml']) && !empty($_REQUEST['onlyfind']) ? $_REQUEST['onlyfind'] : '';
  1575. if ($context['only_find'])
  1576. $context['look_for'][] = $context['only_find'];
  1577. // Have we got a load of back-catalogue trees to expand from a submit etc?
  1578. if (!empty($_GET['back_look']))
  1579. {
  1580. $potententialTrees = unserialize(base64_decode($_GET['back_look']));
  1581. foreach ($potententialTrees as $tree)
  1582. $context['look_for'][] = $tree;
  1583. }
  1584. // ... maybe posted?
  1585. if (!empty($_POST['back_look']))
  1586. $context['only_find'] = array_merge($context['only_find'], $_POST['back_look']);
  1587. $context['back_look_data'] = base64_encode(serialize(array_slice($context['look_for'], 0, 15)));
  1588. // Are we finding more files than first thought?
  1589. $context['file_offset'] = !empty($_REQUEST['fileoffset']) ? (int) $_REQUEST['fileoffset'] : 0;
  1590. // Don't list more than this many files in a directory.
  1591. $context['file_limit'] = 150;
  1592. // How many levels shall we show?
  1593. $context['default_level'] = empty($context['only_find']) ? 2 : 25;
  1594. // This will be used if we end up catching XML data.
  1595. $context['xml_data'] = array(
  1596. 'roots' => array(
  1597. 'identifier' => 'root',
  1598. 'children' => array(
  1599. array(
  1600. 'value' => preg_replace('~[^A-Za-z0-9_\-=:]~', ':-:', $context['only_find']),
  1601. ),
  1602. ),
  1603. ),
  1604. 'folders' => array(
  1605. 'identifier' => 'folder',
  1606. 'children' => array(),
  1607. ),
  1608. );
  1609. foreach ($context['file_tree'] as $path => $data)
  1610. {
  1611. // Run this directory.
  1612. if (file_exists($path) && (empty($context['only_find']) || substr($context['only_find'], 0, strlen($path)) == $path))
  1613. {
  1614. // Get the first level down only.
  1615. fetchPerms__recursive($path, $context['file_tree'][$path], 1);
  1616. $context['file_tree'][$path]['perms'] = array(
  1617. 'chmod' => @is_writable($path),
  1618. 'perms' => @fileperms($path),
  1619. );
  1620. }
  1621. else
  1622. unset($context['file_tree'][$path]);
  1623. }
  1624. // Is this actually xml?
  1625. if (isset($_GET['xml']))
  1626. {
  1627. loadTemplate('Xml');
  1628. $context['sub_template'] = 'generic_xml';
  1629. $context['template_layers'] = array();
  1630. }
  1631. }
  1632. function fetchPerms__recursive($path, &$data, $level)
  1633. {
  1634. global $context;
  1635. $isLikelyPath = false;
  1636. foreach ($context['look_for'] as $possiblePath)
  1637. if (substr($possiblePath, 0, strlen($path)) == $path)
  1638. $isLikelyPath = true;
  1639. // Is this where we stop?
  1640. if (isset($_GET['xml']) && !empty($context['look_for']) && !$isLikelyPath)
  1641. return;
  1642. elseif ($level > $context['default_level'] && !$isLikelyPath)
  1643. return;
  1644. // Are we actually interested in saving this data?
  1645. $save_data = empty($context['only_find']) || $context['only_find'] == $path;
  1646. /**
  1647. * @todo Shouldn't happen - but better error message?
  1648. */
  1649. if (!is_dir($path))
  1650. fatal_lang_error('no_access', false);
  1651. // This is where we put stuff we've found for sorting.
  1652. $foundData = array(
  1653. 'files' => array(),
  1654. 'folders' => array(),
  1655. );
  1656. $dh = opendir($path);
  1657. while ($entry = readdir($dh))
  1658. {
  1659. // Some kind of file?
  1660. if (!is_dir($path . '/' . $entry))
  1661. {
  1662. // Are we listing PHP files in this directory?
  1663. if ($save_data && !empty($data['list_contents']) && substr($entry, -4) == '.php')
  1664. $foundData['files'][$entry] = true;
  1665. // A file we were looking for.
  1666. elseif ($save_data && isset($data['contents'][$entry]))
  1667. $foundData['files'][$entry] = true;
  1668. }
  1669. // It's a directory - we're interested one way or another, probably...
  1670. elseif ($entry != '.' && $entry != '..')
  1671. {
  1672. // Going further?
  1673. 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'))))
  1674. {
  1675. if (!isset($data['contents'][$entry]))
  1676. $foundData['folders'][$entry] = 'dir_recursive';
  1677. else
  1678. $foundData['folders'][$entry] = true;
  1679. // If this wasn't expected inherit the recusiveness...
  1680. if (!isset($data['contents'][$entry]))
  1681. // We need to do this as we will be going all recursive.
  1682. $data['contents'][$entry] = array(
  1683. 'type' => 'dir_recursive',
  1684. );
  1685. // Actually do the recursive stuff...
  1686. fetchPerms__recursive($path . '/' . $entry, $data['contents'][$entry], $level + 1);
  1687. }
  1688. // Maybe it is a folder we are not descending into.
  1689. elseif (isset($data['contents'][$entry]))
  1690. $foundData['folders'][$entry] = true;
  1691. // Otherwise we stop here.
  1692. }
  1693. }
  1694. closedir($dh);
  1695. // Nothing to see here?
  1696. if (!$save_data)
  1697. return;
  1698. // Now actually add the data, starting with the folders.
  1699. ksort($foundData['folders']);
  1700. foreach ($foundData['folders'] as $folder => $type)
  1701. {
  1702. $additional_data = array(
  1703. 'perms' => array(
  1704. 'chmod' => @is_writable($path . '/' . $folder),
  1705. 'perms' => @fileperms($path . '/' . $folder),
  1706. ),
  1707. );
  1708. if ($type !== true)
  1709. $additional_data['type'] = $type;
  1710. // If there's an offset ignore any folders in XML mode.
  1711. if (isset($_GET['xml']) && $context['file_offset'] == 0)
  1712. {
  1713. $context['xml_data']['folders']['children'][] = array(
  1714. 'attributes' => array(
  1715. 'writable' => $additional_data['perms']['chmod'] ? 1 : 0,
  1716. 'permissions' => substr(sprintf('%o', $additional_data['perms']['perms']), -4),
  1717. 'folder' => 1,
  1718. 'path' => $context['only_find'],
  1719. 'level' => $level,
  1720. 'more' => 0,
  1721. 'offset' => $context['file_offset'],
  1722. 'my_ident' => preg_replace('~[^A-Za-z0-9_\-=:]~', ':-:', $context['only_find'] . '/' . $folder),
  1723. 'ident' => preg_replace('~[^A-Za-z0-9_\-=:]~', ':-:', $context['only_find']),
  1724. ),
  1725. 'value' => $folder,
  1726. );
  1727. }
  1728. elseif (!isset($_GET['xml']))
  1729. {
  1730. if (isset($data['contents'][$folder]))
  1731. $data['contents'][$folder] = array_merge($data['contents'][$folder], $additional_data);
  1732. else
  1733. $data['contents'][$folder] = $additional_data;
  1734. }
  1735. }
  1736. // Now we want to do a similar thing with files.
  1737. ksort($foundData['files']);
  1738. $counter = -1;
  1739. foreach ($foundData['files'] as $file => $dummy)
  1740. {
  1741. $counter++;
  1742. // Have we reached our offset?
  1743. if ($context['file_offset'] > $counter)
  1744. continue;
  1745. // Gone too far?
  1746. if ($counter > ($context['file_offset'] + $context['file_limit']))
  1747. continue;
  1748. $additional_data = array(
  1749. 'perms' => array(
  1750. 'chmod' => @is_writable($path . '/' . $file),
  1751. 'perms' => @fileperms($path . '/' . $file),
  1752. ),
  1753. );
  1754. // XML?
  1755. if (isset($_GET['xml']))
  1756. {
  1757. $context['xml_data']['folders']['children'][] = array(
  1758. 'attributes' => array(
  1759. 'writable' => $additional_data['perms']['chmod'] ? 1 : 0,
  1760. 'permissions' => substr(sprintf('%o', $additional_data['perms']['perms']), -4),
  1761. 'folder' => 0,
  1762. 'path' => $context['only_find'],
  1763. 'level' => $level,
  1764. 'more' => $counter == ($context['file_offset'] + $context['file_limit']) ? 1 : 0,
  1765. 'offset' => $context['file_offset'],
  1766. 'my_ident' => preg_replace('~[^A-Za-z0-9_\-=:]~', ':-:', $context['only_find'] . '/' . $file),
  1767. 'ident' => preg_replace('~[^A-Za-z0-9_\-=:]~', ':-:', $context['only_find']),
  1768. ),
  1769. 'value' => $file,
  1770. );
  1771. }
  1772. elseif ($counter != ($context['file_offset'] + $context['file_limit']))
  1773. {
  1774. if (isset($data['contents'][$file]))
  1775. $data['contents'][$file] = array_merge($data['contents'][$file], $additional_data);
  1776. else
  1777. $data['contents'][$file] = $additional_data;
  1778. }
  1779. }
  1780. }
  1781. /**
  1782. * Actually action the permission changes they want.
  1783. */
  1784. function PackagePermissionsAction()
  1785. {
  1786. global $context, $txt, $time_start, $package_ftp;
  1787. umask(0);
  1788. $timeout_limit = 5;
  1789. $context['method'] = $_POST['method'] == 'individual' ? 'individual' : 'predefined';
  1790. $context['sub_template'] = 'action_permissions';
  1791. $context['page_title'] = $txt['package_file_perms_applying'];
  1792. $context['back_look_data'] = isset($_POST['back_look']) ? $_POST['back_look'] : array();
  1793. // Skipping use of FTP?
  1794. if (empty($package_ftp))
  1795. $context['skip_ftp'] = true;
  1796. // 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.
  1797. if ($context['method'] == 'individual')
  1798. {
  1799. // Only these path roots are legal.
  1800. $legal_roots = array_keys($context['file_tree']);
  1801. $context['custom_value'] = (int) $_POST['custom_value'];
  1802. // Continuing?
  1803. if (isset($_POST['toProcess']))
  1804. $_POST['permStatus'] = unserialize(base64_decode($_POST['toProcess']));
  1805. if (isset($_POST['permStatus']))
  1806. {
  1807. $context['to_process'] = array();
  1808. $validate_custom = false;
  1809. foreach ($_POST['permStatus'] as $path => $status)
  1810. {
  1811. // Nothing to see here?
  1812. if ($status == 'no_change')
  1813. continue;
  1814. $legal = false;
  1815. foreach ($legal_roots as $root)
  1816. if (substr($path, 0, strlen($root)) == $root)
  1817. $legal = true;
  1818. if (!$legal)
  1819. continue;
  1820. // Check it exists.
  1821. if (!file_exists($path))
  1822. continue;
  1823. if ($status == 'custom')
  1824. $validate_custom = true;
  1825. // Now add it.
  1826. $context['to_process'][$path] = $status;
  1827. }
  1828. $context['total_items'] = isset($_POST['totalItems']) ? (int) $_POST['totalItems'] : count($context['to_process']);
  1829. // Make sure the chmod status is valid?
  1830. if ($validate_custom)
  1831. {
  1832. if (preg_match('~^[4567][4567][4567]$~', $context['custom_value']) == false)
  1833. fatal_error($txt['chmod_value_invalid']);
  1834. }
  1835. // Nothing to do?
  1836. if (empty($context['to_process']))
  1837. 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']);
  1838. }
  1839. // Should never get here,
  1840. else
  1841. fatal_lang_error('no_access', false);
  1842. // Setup the custom value.
  1843. $custom_value = octdec('0' . $context['custom_value']);
  1844. // Start processing items.
  1845. foreach ($context['to_process'] as $path => $status)
  1846. {
  1847. if (in_array($status, array('execute', 'writable', 'read')))
  1848. package_chmod($path, $status);
  1849. elseif ($status == 'custom' && !empty($custom_value))
  1850. {
  1851. // Use FTP if we have it.
  1852. if (!empty($package_ftp) && !empty($_SESSION['pack_ftp']))
  1853. {
  1854. $ftp_file = strtr($path, array($_SESSION['pack_ftp']['root'] => ''));
  1855. $package_ftp->chmod($ftp_file, $custom_value);
  1856. }
  1857. else
  1858. @chmod($path, $custom_value);
  1859. }
  1860. // This fish is fried...
  1861. unset($context['to_process'][$path]);
  1862. // See if we're out of time?
  1863. if (time() - array_sum(explode(' ', $time_start)) > $timeout_limit)
  1864. return false;
  1865. }
  1866. }
  1867. // If predefined this is a little different.
  1868. else
  1869. {
  1870. $context['predefined_type'] = isset($_POST['predefined']) ? $_POST['predefined'] : 'restricted';
  1871. $context['total_items'] = isset($_POST['totalItems']) ? (int) $_POST['totalItems'] : 0;
  1872. $context['directory_list'] = isset($_POST['dirList']) ? unserialize(base64_decode($_POST['dirList'])) : array();
  1873. $context['file_offset'] = isset($_POST['fileOffset']) ? (int) $_POST['fileOffset'] : 0;
  1874. // Haven't counted the items yet?
  1875. if (empty($context['total_items']))
  1876. {
  1877. function count_directories__recursive($dir)
  1878. {
  1879. global $context;
  1880. $count = 0;
  1881. $dh = @opendir($dir);
  1882. while ($entry = readdir($dh))
  1883. {
  1884. if ($entry != '.' && $entry != '..' && is_dir($dir . '/' . $entry))
  1885. {
  1886. $context['directory_list'][$dir . '/' . $entry] = 1;
  1887. $count++;
  1888. $count += count_directories__recursive($dir . '/' . $entry);
  1889. }
  1890. }
  1891. closedir($dh);
  1892. return $count;
  1893. }
  1894. foreach ($context['file_tree'] as $path => $data)
  1895. {
  1896. if (is_dir($path))
  1897. {
  1898. $context['directory_list'][$path] = 1;
  1899. $context['total_items'] += count_directories__recursive($path);
  1900. $context['total_items']++;
  1901. }
  1902. }
  1903. }
  1904. // Have we built up our list of special files?
  1905. if (!isset($_POST['specialFiles']) && $context['predefined_type'] != 'free')
  1906. {
  1907. $context['special_files'] = array();
  1908. function build_special_files__recursive($path, &$data)
  1909. {
  1910. global $context;
  1911. if (!empty($data['writable_on']))
  1912. if ($context['predefined_type'] == 'standard' || $data['writable_on'] == 'restrictive')
  1913. $context['special_files'][$path] = 1;
  1914. if (!empty($data['contents']))
  1915. foreach ($data['contents'] as $name => $contents)
  1916. build_special_files__recursive($path . '/' . $name, $contents);
  1917. }
  1918. foreach ($context['file_tree'] as $path => $data)
  1919. build_special_files__recursive($path, $data);
  1920. }
  1921. // Free doesn't need special files.
  1922. elseif ($context['predefined_type'] == 'free')
  1923. $context['special_files'] = array();
  1924. else
  1925. $context['special_files'] = unserialize(base64_decode($_POST['specialFiles']));
  1926. // Now we definitely know where we are, we need to go through again doing the chmod!
  1927. foreach ($context['directory_list'] as $path => $dummy)
  1928. {
  1929. // Do the contents of the directory first.
  1930. $dh = @opendir($path);
  1931. $file_count = 0;
  1932. $dont_chmod = false;
  1933. while ($entry = readdir($dh))
  1934. {
  1935. $file_count++;
  1936. // Actually process this file?
  1937. if (!$dont_chmod && !is_dir($path . '/' . $entry) && (empty($context['file_offset']) || $context['file_offset'] < $file_count))
  1938. {
  1939. $status = $context['predefined_type'] == 'free' || isset($context['special_files'][$path . '/' . $entry]) ? 'writable' : 'execute';
  1940. package_chmod($path . '/' . $entry, $status);
  1941. }
  1942. // See if we're out of time?
  1943. if (!$dont_chmod && time() - array_sum(explode(' ', $time_start)) > $timeout_limit)
  1944. {
  1945. $dont_chmod = true;
  1946. // Don't do this again.
  1947. $context['file_offset'] = $file_count;
  1948. }
  1949. }
  1950. closedir($dh);
  1951. // If this is set it means we timed out half way through.
  1952. if ($dont_chmod)
  1953. {
  1954. $context['total_files'] = $file_count;
  1955. return false;
  1956. }
  1957. // Do the actual directory.
  1958. $status = $context['predefined_type'] == 'free' || isset($context['special_files'][$path]) ? 'writable' : 'execute';
  1959. package_chmod($path, $status);
  1960. // We've finished the directory so no file offset, and no record.
  1961. $context['file_offset'] = 0;
  1962. unset($context['directory_list'][$path]);
  1963. // See if we're out of time?
  1964. if (time() - array_sum(explode(' ', $time_start)) > $timeout_limit)
  1965. return false;
  1966. }
  1967. }
  1968. // If we're here we are done!
  1969. 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']);
  1970. }
  1971. /**
  1972. * Test an FTP connection.
  1973. */
  1974. function PackageFTPTest()
  1975. {
  1976. global $context, $txt, $package_ftp;
  1977. checkSession('get');
  1978. // Try to make the FTP connection.
  1979. create_chmod_control(array(), array('force_find_error' => true));
  1980. // Deal with the template stuff.
  1981. loadTemplate('Xml');
  1982. $context['sub_template'] = 'generic_xml';
  1983. $context['template_layers'] = array();
  1984. // Define the return data, this is simple.
  1985. $context['xml_data'] = array(
  1986. 'results' => array(
  1987. 'identifier' => 'result',
  1988. 'children' => array(
  1989. array(
  1990. 'attributes' => array(
  1991. 'success' => !empty($package_ftp) ? 1 : 0,
  1992. ),
  1993. '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']),
  1994. ),
  1995. ),
  1996. ),
  1997. );
  1998. }
  1999. ?>