2
0

Packages.php 93 KB

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