Packages.php 89 KB

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