Packages.php 91 KB

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