Packages.php 90 KB

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