Packages.php 89 KB

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