Packages.php 93 KB

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