Packages.php 89 KB

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