Packages.php 78 KB

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