Packages.php 91 KB

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