Packages.php 94 KB

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