admin.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. <?php
  2. /**
  3. * Elgg admin functions.
  4. *
  5. * Admin menu items
  6. * Elgg has a convenience function for adding menu items to the sidebar of the
  7. * admin area. @see elgg_register_admin_menu_item()
  8. *
  9. * Admin pages
  10. * Plugins no not need to provide their own page handler to add a page to the
  11. * admin area. A view placed at admin/<section>/<subsection> can be access
  12. * at http://example.org/admin/<section>/<subsection>. The title of the page
  13. * will be elgg_echo('admin:<section>:<subsection>'). For an example of how to
  14. * add a page to the admin area, see the diagnostics plugin.
  15. *
  16. * Admin notices
  17. * System messages (success and error messages) are used in both the main site
  18. * and the admin area. There is a special presistent message for the admin area
  19. * called an admin notice. It should be used when a plugin requires an
  20. * administrator to take an action. An example is the categories plugin
  21. * requesting that the administrator set site categories after the plugin has
  22. * been activated. @see elgg_add_admin_notice()
  23. *
  24. *
  25. * @package Elgg.Core
  26. * @subpackage Admin
  27. */
  28. /**
  29. * Get the admin users
  30. *
  31. * @param array $options Options array, @see elgg_get_entities() for parameters
  32. *
  33. * @return mixed Array of admin users or false on failure. If a count, returns int.
  34. * @since 1.8.0
  35. */
  36. function elgg_get_admins(array $options = array()) {
  37. global $CONFIG;
  38. if (isset($options['joins'])) {
  39. if (!is_array($options['joins'])) {
  40. $options['joins'] = array($options['joins']);
  41. }
  42. $options['joins'][] = "join {$CONFIG->dbprefix}users_entity u on e.guid=u.guid";
  43. } else {
  44. $options['joins'] = array("join {$CONFIG->dbprefix}users_entity u on e.guid=u.guid");
  45. }
  46. if (isset($options['wheres'])) {
  47. if (!is_array($options['wheres'])) {
  48. $options['wheres'] = array($options['wheres']);
  49. }
  50. $options['wheres'][] = "u.admin = 'yes'";
  51. } else {
  52. $options['wheres'][] = "u.admin = 'yes'";
  53. }
  54. return elgg_get_entities($options);
  55. }
  56. /**
  57. * Write a persistent message to the admin view.
  58. * Useful to alert the admin to take a certain action.
  59. * The id is a unique ID that can be cleared once the admin
  60. * completes the action.
  61. *
  62. * eg: add_admin_notice('twitter_services_no_api',
  63. * 'Before your users can use Twitter services on this site, you must set up
  64. * the Twitter API key in the <a href="link">Twitter Services Settings</a>');
  65. *
  66. * @param string $id A unique ID that your plugin can remember
  67. * @param string $message Body of the message
  68. *
  69. * @return bool
  70. * @since 1.8.0
  71. */
  72. function elgg_add_admin_notice($id, $message) {
  73. return _elgg_services()->adminNotices->add($id, $message);
  74. }
  75. /**
  76. * Remove an admin notice by ID.
  77. *
  78. * eg In actions/twitter_service/save_settings:
  79. * if (is_valid_twitter_api_key()) {
  80. * delete_admin_notice('twitter_services_no_api');
  81. * }
  82. *
  83. * @param string $id The unique ID assigned in add_admin_notice()
  84. *
  85. * @return bool
  86. * @since 1.8.0
  87. */
  88. function elgg_delete_admin_notice($id) {
  89. return _elgg_services()->adminNotices->delete($id);
  90. }
  91. /**
  92. * Get admin notices. An admin must be logged in since the notices are private.
  93. *
  94. * @param int $limit Limit
  95. *
  96. * @return array Array of admin notices
  97. * @since 1.8.0
  98. */
  99. function elgg_get_admin_notices($limit = 10) {
  100. return _elgg_services()->adminNotices->find($limit);
  101. }
  102. /**
  103. * Check if an admin notice is currently active.
  104. *
  105. * @param string $id The unique ID used to register the notice.
  106. *
  107. * @return bool
  108. * @since 1.8.0
  109. */
  110. function elgg_admin_notice_exists($id) {
  111. return _elgg_services()->adminNotices->exists($id);
  112. }
  113. /**
  114. * Add an admin area section or child section.
  115. * This is a wrapper for elgg_register_menu_item().
  116. *
  117. * Used in conjuction with http://elgg.org/admin/section_id/child_section style
  118. * page handler. See the documentation at the top of this file for more details
  119. * on that.
  120. *
  121. * The text of the menu item is obtained from elgg_echo(admin:$parent_id:$menu_id)
  122. *
  123. * This function handles registering the parent if it has not been registered.
  124. *
  125. * @param string $section The menu section to add to
  126. * @param string $menu_id The unique ID of section
  127. * @param string $parent_id If a child section, the parent section id
  128. * @param int $priority The menu item priority
  129. *
  130. * @return bool
  131. * @since 1.8.0
  132. */
  133. function elgg_register_admin_menu_item($section, $menu_id, $parent_id = null, $priority = 100) {
  134. // make sure parent is registered
  135. if ($parent_id && !elgg_is_menu_item_registered('page', $parent_id)) {
  136. elgg_register_admin_menu_item($section, $parent_id);
  137. }
  138. // in the admin section parents never have links
  139. if ($parent_id) {
  140. $href = "admin/$parent_id/$menu_id";
  141. } else {
  142. $href = null;
  143. }
  144. $name = $menu_id;
  145. if ($parent_id) {
  146. $name = "$parent_id:$name";
  147. }
  148. return elgg_register_menu_item('page', array(
  149. 'name' => $name,
  150. 'href' => $href,
  151. 'text' => elgg_echo("admin:$name"),
  152. 'context' => 'admin',
  153. 'parent_name' => $parent_id,
  154. 'priority' => $priority,
  155. 'section' => $section
  156. ));
  157. }
  158. /**
  159. * Add an admin notice when a new \ElggUpgrade object is created.
  160. *
  161. * @param string $event
  162. * @param string $type
  163. * @param \ElggObject $object
  164. * @access private
  165. */
  166. function _elgg_create_notice_of_pending_upgrade($event, $type, $object) {
  167. if ($object instanceof \ElggUpgrade) {
  168. // Link to the Upgrades section
  169. $link = elgg_view('output/url', array(
  170. 'href' => 'admin/upgrades',
  171. 'text' => elgg_echo('admin:view_upgrades'),
  172. ));
  173. $message = elgg_echo('admin:pending_upgrades');
  174. elgg_add_admin_notice('pending_upgrades', "$message $link");
  175. }
  176. }
  177. /**
  178. * Initialize the admin backend.
  179. * @return void
  180. * @access private
  181. */
  182. function _elgg_admin_init() {
  183. elgg_register_event_handler('pagesetup', 'system', '_elgg_admin_pagesetup', 1000);
  184. // maintenance mode
  185. if (elgg_get_config('elgg_maintenance_mode', null)) {
  186. elgg_register_plugin_hook_handler('route', 'all', '_elgg_admin_maintenance_handler');
  187. elgg_register_plugin_hook_handler('action', 'all', '_elgg_admin_maintenance_action_check');
  188. elgg_register_css('maintenance', elgg_get_simplecache_url('css', 'maintenance'));
  189. elgg_register_menu_item('topbar', array(
  190. 'name' => 'maintenance_mode',
  191. 'href' => 'admin/administer_utilities/maintenance',
  192. 'text' => elgg_echo('admin:maintenance_mode:indicator_menu_item'),
  193. 'priority' => 900,
  194. ));
  195. }
  196. elgg_register_action('admin/user/ban', '', 'admin');
  197. elgg_register_action('admin/user/unban', '', 'admin');
  198. elgg_register_action('admin/user/delete', '', 'admin');
  199. elgg_register_action('admin/user/resetpassword', '', 'admin');
  200. elgg_register_action('admin/user/makeadmin', '', 'admin');
  201. elgg_register_action('admin/user/removeadmin', '', 'admin');
  202. elgg_register_action('admin/site/update_basic', '', 'admin');
  203. elgg_register_action('admin/site/update_advanced', '', 'admin');
  204. elgg_register_action('admin/site/flush_cache', '', 'admin');
  205. elgg_register_action('admin/site/unlock_upgrade', '', 'admin');
  206. elgg_register_action('admin/site/set_robots', '', 'admin');
  207. elgg_register_action('admin/site/set_maintenance_mode', '', 'admin');
  208. elgg_register_action('admin/upgrades/upgrade_comments', '', 'admin');
  209. elgg_register_action('admin/upgrades/upgrade_datadirs', '', 'admin');
  210. elgg_register_action('admin/upgrades/upgrade_discussion_replies', '', 'admin');
  211. elgg_register_action('admin/upgrades/upgrade_comments_access', '', 'admin');
  212. elgg_register_action('admin/site/regenerate_secret', '', 'admin');
  213. elgg_register_action('admin/menu/save', '', 'admin');
  214. elgg_register_action('admin/delete_admin_notice', '', 'admin');
  215. elgg_register_action('profile/fields/reset', '', 'admin');
  216. elgg_register_action('profile/fields/add', '', 'admin');
  217. elgg_register_action('profile/fields/edit', '', 'admin');
  218. elgg_register_action('profile/fields/delete', '', 'admin');
  219. elgg_register_action('profile/fields/reorder', '', 'admin');
  220. elgg_register_simplecache_view('css/admin');
  221. $url = elgg_get_simplecache_url('js', 'admin');
  222. elgg_register_js('elgg.admin', $url);
  223. elgg_register_js('elgg.upgrades', 'js/lib/upgrades.js');
  224. elgg_register_js('jquery.jeditable', 'vendors/jquery/jquery.jeditable.mini.js');
  225. // administer
  226. // dashboard
  227. elgg_register_menu_item('page', array(
  228. 'name' => 'dashboard',
  229. 'href' => 'admin/dashboard',
  230. 'text' => elgg_echo('admin:dashboard'),
  231. 'context' => 'admin',
  232. 'priority' => 10,
  233. 'section' => 'administer'
  234. ));
  235. // statistics
  236. elgg_register_admin_menu_item('administer', 'statistics', null, 20);
  237. elgg_register_admin_menu_item('administer', 'overview', 'statistics');
  238. elgg_register_admin_menu_item('administer', 'server', 'statistics');
  239. //utilities
  240. elgg_register_admin_menu_item('administer', 'maintenance', 'administer_utilities');
  241. // users
  242. elgg_register_admin_menu_item('administer', 'users', null, 20);
  243. elgg_register_admin_menu_item('administer', 'online', 'users', 10);
  244. elgg_register_admin_menu_item('administer', 'admins', 'users', 20);
  245. elgg_register_admin_menu_item('administer', 'newest', 'users', 30);
  246. elgg_register_admin_menu_item('administer', 'add', 'users', 40);
  247. // configure
  248. // upgrades
  249. elgg_register_menu_item('page', array(
  250. 'name' => 'upgrades',
  251. 'href' => 'admin/upgrades',
  252. 'text' => elgg_echo('admin:upgrades'),
  253. 'context' => 'admin',
  254. 'priority' => 10,
  255. 'section' => 'configure'
  256. ));
  257. // plugins
  258. elgg_register_menu_item('page', array(
  259. 'name' => 'plugins',
  260. 'href' => 'admin/plugins',
  261. 'text' => elgg_echo('admin:plugins'),
  262. 'context' => 'admin',
  263. 'priority' => 75,
  264. 'section' => 'configure'
  265. ));
  266. // settings
  267. elgg_register_admin_menu_item('configure', 'appearance', null, 50);
  268. elgg_register_admin_menu_item('configure', 'settings', null, 100);
  269. elgg_register_admin_menu_item('configure', 'basic', 'settings', 10);
  270. elgg_register_admin_menu_item('configure', 'advanced', 'settings', 20);
  271. // plugin settings are added in _elgg_admin_add_plugin_settings_menu() via the admin page handler
  272. // for performance reasons.
  273. // appearance
  274. elgg_register_admin_menu_item('configure', 'menu_items', 'appearance', 30);
  275. elgg_register_admin_menu_item('configure', 'profile_fields', 'appearance', 40);
  276. // default widgets is added via an event handler elgg_default_widgets_init() in widgets.php
  277. // because it requires additional setup.
  278. // configure utilities
  279. elgg_register_admin_menu_item('configure', 'robots', 'configure_utilities');
  280. // we want plugin settings menu items to be sorted alphabetical
  281. if (elgg_in_context('admin')) {
  282. elgg_register_plugin_hook_handler('prepare', 'menu:page', '_elgg_admin_sort_page_menu');
  283. }
  284. if (elgg_is_admin_logged_in()) {
  285. elgg_register_menu_item('topbar', array(
  286. 'name' => 'administration',
  287. 'href' => 'admin',
  288. 'text' => elgg_view_icon('settings') . elgg_echo('admin'),
  289. 'priority' => 100,
  290. 'section' => 'alt',
  291. ));
  292. }
  293. // widgets
  294. $widgets = array('online_users', 'new_users', 'content_stats', 'banned_users', 'admin_welcome', 'control_panel', 'cron_status');
  295. foreach ($widgets as $widget) {
  296. elgg_register_widget_type(
  297. $widget,
  298. elgg_echo("admin:widget:$widget"),
  299. elgg_echo("admin:widget:$widget:help"),
  300. array('admin')
  301. );
  302. }
  303. // automatic adding of widgets for admin
  304. elgg_register_event_handler('make_admin', 'user', '_elgg_add_admin_widgets');
  305. // Add notice about pending upgrades
  306. elgg_register_event_handler('create', 'object', '_elgg_create_notice_of_pending_upgrade');
  307. elgg_register_page_handler('admin', '_elgg_admin_page_handler');
  308. elgg_register_page_handler('admin_plugin_screenshot', '_elgg_admin_plugin_screenshot_page_handler');
  309. elgg_register_page_handler('admin_plugin_text_file', '_elgg_admin_markdown_page_handler');
  310. elgg_register_page_handler('robots.txt', '_elgg_robots_page_handler');
  311. }
  312. /**
  313. * Handles any set up required for administration pages
  314. *
  315. * @return void
  316. * @access private
  317. */
  318. function _elgg_admin_pagesetup() {
  319. if (elgg_in_context('admin')) {
  320. $url = elgg_get_simplecache_url('css', 'admin');
  321. elgg_register_css('elgg.admin', $url);
  322. elgg_load_css('elgg.admin');
  323. elgg_unregister_css('elgg');
  324. $admin = elgg_get_logged_in_user_entity();
  325. // setup header menu
  326. elgg_register_menu_item('admin_header', array(
  327. 'name' => 'admin_logout',
  328. 'href' => 'action/logout',
  329. 'text' => elgg_echo('logout'),
  330. 'is_trusted' => true,
  331. 'priority' => 1000,
  332. ));
  333. elgg_register_menu_item('admin_header', array(
  334. 'name' => 'view_site',
  335. 'href' => elgg_get_site_url(),
  336. 'text' => elgg_echo('admin:view_site'),
  337. 'is_trusted' => true,
  338. 'priority' => 900,
  339. ));
  340. elgg_register_menu_item('admin_header', array(
  341. 'name' => 'admin_profile',
  342. 'href' => false,
  343. 'text' => elgg_echo('admin:loggedin', array($admin->name)),
  344. 'priority' => 800,
  345. ));
  346. if (elgg_get_config('elgg_maintenance_mode', null)) {
  347. elgg_register_menu_item('admin_header', array(
  348. 'name' => 'maintenance',
  349. 'href' => 'admin/administer_utilities/maintenance',
  350. 'text' => elgg_echo('admin:administer_utilities:maintenance'),
  351. 'link_class' => 'elgg-maintenance-mode-warning',
  352. 'priority' => 700,
  353. ));
  354. }
  355. // setup footer menu
  356. elgg_register_menu_item('admin_footer', array(
  357. 'name' => 'faq',
  358. 'text' => elgg_echo('admin:footer:faq'),
  359. 'href' => 'http://learn.elgg.org/en/stable/appendix/faqs.html',
  360. ));
  361. elgg_register_menu_item('admin_footer', array(
  362. 'name' => 'manual',
  363. 'text' => elgg_echo('admin:footer:manual'),
  364. 'href' => 'http://learn.elgg.org/en/stable/admin/index.html',
  365. ));
  366. elgg_register_menu_item('admin_footer', array(
  367. 'name' => 'community_forums',
  368. 'text' => elgg_echo('admin:footer:community_forums'),
  369. 'href' => 'http://community.elgg.org/groups/all/',
  370. ));
  371. elgg_register_menu_item('admin_footer', array(
  372. 'name' => 'blog',
  373. 'text' => elgg_echo('admin:footer:blog'),
  374. 'href' => 'https://community.elgg.org/blog/all',
  375. ));
  376. }
  377. }
  378. /**
  379. * Create the plugin settings page menu.
  380. *
  381. * This is done in a separate function called from the admin
  382. * page handler because of performance concerns.
  383. *
  384. * @return void
  385. * @access private
  386. * @since 1.8.0
  387. */
  388. function _elgg_admin_add_plugin_settings_menu() {
  389. $active_plugins = elgg_get_plugins('active');
  390. if (!$active_plugins) {
  391. // nothing added because no items
  392. return;
  393. }
  394. foreach ($active_plugins as $plugin) {
  395. $plugin_id = $plugin->getID();
  396. $settings_view_old = 'settings/' . $plugin_id . '/edit';
  397. $settings_view_new = 'plugins/' . $plugin_id . '/settings';
  398. if (elgg_view_exists($settings_view_new) || elgg_view_exists($settings_view_old)) {
  399. elgg_register_menu_item('page', array(
  400. 'name' => $plugin_id,
  401. 'href' => "admin/plugin_settings/$plugin_id",
  402. 'text' => $plugin->getManifest()->getName(),
  403. 'parent_name' => 'settings',
  404. 'context' => 'admin',
  405. 'section' => 'configure',
  406. ));
  407. }
  408. }
  409. }
  410. /**
  411. * Sort the plugin settings menu items
  412. *
  413. * @param string $hook
  414. * @param string $type
  415. * @param array $return
  416. * @param array $params
  417. *
  418. * @return void
  419. * @since 1.8.0
  420. * @access private
  421. */
  422. function _elgg_admin_sort_page_menu($hook, $type, $return, $params) {
  423. $configure_items = $return['configure'];
  424. if (is_array($configure_items)) {
  425. /* @var \ElggMenuItem[] $configure_items */
  426. foreach ($configure_items as $menu_item) {
  427. if ($menu_item->getName() == 'settings') {
  428. $settings = $menu_item;
  429. }
  430. }
  431. if (!empty($settings) && $settings instanceof \ElggMenuItem) {
  432. // keep the basic and advanced settings at the top
  433. /* @var \ElggMenuItem $settings */
  434. $children = $settings->getChildren();
  435. $site_settings = array_splice($children, 0, 2);
  436. usort($children, array('\ElggMenuBuilder', 'compareByText'));
  437. array_splice($children, 0, 0, $site_settings);
  438. $settings->setChildren($children);
  439. }
  440. }
  441. }
  442. /**
  443. * Handle admin pages. Expects corresponding views as admin/section/subsection
  444. *
  445. * @param array $page Array of pages
  446. *
  447. * @return bool
  448. * @access private
  449. */
  450. function _elgg_admin_page_handler($page) {
  451. elgg_admin_gatekeeper();
  452. _elgg_admin_add_plugin_settings_menu();
  453. elgg_set_context('admin');
  454. elgg_unregister_css('elgg');
  455. elgg_load_js('elgg.admin');
  456. elgg_load_js('jquery.jeditable');
  457. // default to dashboard
  458. if (!isset($page[0]) || empty($page[0])) {
  459. $page = array('dashboard');
  460. }
  461. // was going to fix this in the page_handler() function but
  462. // it's commented to explicitly return a string if there's a trailing /
  463. if (empty($page[count($page) - 1])) {
  464. array_pop($page);
  465. }
  466. $vars = array('page' => $page);
  467. // special page for plugin settings since we create the form for them
  468. if ($page[0] == 'plugin_settings') {
  469. if (isset($page[1]) && (elgg_view_exists("settings/{$page[1]}/edit") ||
  470. elgg_view_exists("plugins/{$page[1]}/settings"))) {
  471. $view = 'admin/plugin_settings';
  472. $plugin = elgg_get_plugin_from_id($page[1]);
  473. $vars['plugin'] = $plugin;
  474. $title = elgg_echo("admin:{$page[0]}");
  475. } else {
  476. forward('', '404');
  477. }
  478. } else {
  479. $view = 'admin/' . implode('/', $page);
  480. $title = elgg_echo("admin:{$page[0]}");
  481. if (count($page) > 1) {
  482. $title .= ' : ' . elgg_echo('admin:' . implode(':', $page));
  483. }
  484. }
  485. // gets content and prevents direct access to 'components' views
  486. if ($page[0] == 'components' || !($content = elgg_view($view, $vars))) {
  487. $title = elgg_echo('admin:unknown_section');
  488. $content = elgg_echo('admin:unknown_section');
  489. }
  490. $body = elgg_view_layout('admin', array('content' => $content, 'title' => $title));
  491. echo elgg_view_page($title, $body, 'admin');
  492. return true;
  493. }
  494. /**
  495. * Serves up screenshots for plugins from
  496. * admin_plugin_screenshot/<plugin_id>/<size>/<ss_name>.<ext>
  497. *
  498. * @param array $pages The pages array
  499. * @return bool
  500. * @access private
  501. */
  502. function _elgg_admin_plugin_screenshot_page_handler($pages) {
  503. // only admins can use this for security
  504. elgg_admin_gatekeeper();
  505. $plugin_id = elgg_extract(0, $pages);
  506. // only thumbnail or full.
  507. $size = elgg_extract(1, $pages, 'thumbnail');
  508. // the rest of the string is the filename
  509. $filename_parts = array_slice($pages, 2);
  510. $filename = implode('/', $filename_parts);
  511. $filename = sanitise_filepath($filename, false);
  512. $plugin = elgg_get_plugin_from_id($plugin_id);
  513. if (!$plugin) {
  514. $file = elgg_get_root_path() . '_graphics/icons/default/medium.png';
  515. } else {
  516. $file = $plugin->getPath() . $filename;
  517. if (!file_exists($file)) {
  518. $file = elgg_get_root_path() . '_graphics/icons/default/medium.png';
  519. }
  520. }
  521. header("Content-type: image/jpeg");
  522. // resize to 100x100 for thumbnails
  523. switch ($size) {
  524. case 'thumbnail':
  525. echo get_resized_image_from_existing_file($file, 100, 100, true);
  526. break;
  527. case 'full':
  528. default:
  529. echo file_get_contents($file);
  530. break;
  531. }
  532. return true;
  533. }
  534. /**
  535. * Formats and serves out markdown files from plugins.
  536. *
  537. * URLs in format like admin_plugin_text_file/<plugin_id>/filename.ext
  538. *
  539. * The only valid files are:
  540. * * README.txt
  541. * * CHANGES.txt
  542. * * INSTALL.txt
  543. * * COPYRIGHT.txt
  544. * * LICENSE.txt
  545. *
  546. * @param array $pages
  547. * @return bool
  548. * @access private
  549. */
  550. function _elgg_admin_markdown_page_handler($pages) {
  551. elgg_admin_gatekeeper();
  552. _elgg_admin_add_plugin_settings_menu();
  553. elgg_set_context('admin');
  554. elgg_unregister_css('elgg');
  555. elgg_load_js('elgg.admin');
  556. elgg_load_js('jquery.jeditable');
  557. elgg_load_library('elgg:markdown');
  558. $plugin_id = elgg_extract(0, $pages);
  559. $plugin = elgg_get_plugin_from_id($plugin_id);
  560. $filename = elgg_extract(1, $pages);
  561. $error = false;
  562. if (!$plugin) {
  563. $error = elgg_echo('admin:plugins:markdown:unknown_plugin');
  564. $body = elgg_view_layout('admin', array('content' => $error, 'title' => $error));
  565. echo elgg_view_page($error, $body, 'admin');
  566. return true;
  567. }
  568. $text_files = $plugin->getAvailableTextFiles();
  569. if (!array_key_exists($filename, $text_files)) {
  570. $error = elgg_echo('admin:plugins:markdown:unknown_file');
  571. }
  572. $file = $text_files[$filename];
  573. $file_contents = file_get_contents($file);
  574. if (!$file_contents) {
  575. $error = elgg_echo('admin:plugins:markdown:unknown_file');
  576. }
  577. if ($error) {
  578. $title = $error;
  579. $body = elgg_view_layout('admin', array('content' => $error, 'title' => $title));
  580. echo elgg_view_page($title, $body, 'admin');
  581. return true;
  582. }
  583. $title = $plugin->getManifest()->getName() . ": $filename";
  584. $text = Markdown($file_contents);
  585. $body = elgg_view_layout('admin', array(
  586. // setting classes here because there's no way to pass classes
  587. // to the layout
  588. 'content' => '<div class="elgg-markdown">' . $text . '</div>',
  589. 'title' => $title
  590. ));
  591. echo elgg_view_page($title, $body, 'admin');
  592. return true;
  593. }
  594. /**
  595. * Handle request for robots.txt
  596. *
  597. * @access private
  598. */
  599. function _elgg_robots_page_handler() {
  600. $site = elgg_get_site_entity();
  601. header("Content-type: text/plain;charset=utf-8");
  602. $content = $site->getPrivateSetting('robots.txt');
  603. $plugin_content = elgg_trigger_plugin_hook('robots.txt', 'site', array('site' => $site), '');
  604. if ($plugin_content) {
  605. $content = $content . "\n\n" . $plugin_content;
  606. }
  607. echo $content;
  608. return true;
  609. }
  610. /**
  611. * When in maintenance mode, should the given URL be handled normally?
  612. *
  613. * @param string $current_url Current page URL
  614. * @return bool
  615. *
  616. * @access private
  617. */
  618. function _elgg_admin_maintenance_allow_url($current_url) {
  619. $site_path = preg_replace('~^https?~', '', elgg_get_site_url());
  620. $current_path = preg_replace('~^https?~', '', $current_url);
  621. if (0 === strpos($current_path, $site_path)) {
  622. $current_path = ($current_path === $site_path) ? '' : substr($current_path, strlen($site_path));
  623. } else {
  624. $current_path = false;
  625. }
  626. // allow plugins to control access for specific URLs/paths
  627. $params = array(
  628. 'current_path' => $current_path,
  629. 'current_url' => $current_url,
  630. );
  631. return (bool)elgg_trigger_plugin_hook('maintenance:allow', 'url', $params, false);
  632. }
  633. /**
  634. * Handle requests when in maintenance mode
  635. *
  636. * @access private
  637. */
  638. function _elgg_admin_maintenance_handler($hook, $type, $info) {
  639. if (elgg_is_admin_logged_in()) {
  640. return;
  641. }
  642. if ($info['identifier'] == 'action' && $info['segments'][0] == 'login') {
  643. return;
  644. }
  645. if (_elgg_admin_maintenance_allow_url(current_page_url())) {
  646. return;
  647. }
  648. elgg_unregister_plugin_hook_handler('register', 'menu:login', '_elgg_login_menu_setup');
  649. $site = elgg_get_site_entity();
  650. $message = $site->getPrivateSetting('elgg_maintenance_message');
  651. if (!$message) {
  652. $message = elgg_echo('admin:maintenance_mode:default_message');
  653. }
  654. elgg_load_css('maintenance');
  655. header("HTTP/1.1 503 Service Unavailable");
  656. $body = elgg_view_layout('maintenance', array(
  657. 'message' => $message,
  658. 'site' => $site,
  659. ));
  660. echo elgg_view_page($site->name, $body, 'maintenance');
  661. return false;
  662. }
  663. /**
  664. * Prevent non-admins from using actions
  665. *
  666. * @access private
  667. *
  668. * @param string $hook Hook name
  669. * @param string $type Action name
  670. * @return bool
  671. */
  672. function _elgg_admin_maintenance_action_check($hook, $type) {
  673. if (elgg_is_admin_logged_in()) {
  674. return true;
  675. }
  676. if ($type == 'login') {
  677. $username = get_input('username');
  678. $user = get_user_by_username($username);
  679. if (!$user) {
  680. $users = get_user_by_email($username);
  681. if ($users) {
  682. $user = $users[0];
  683. }
  684. }
  685. if ($user && $user->isAdmin()) {
  686. return true;
  687. }
  688. }
  689. if (_elgg_admin_maintenance_allow_url(current_page_url())) {
  690. return true;
  691. }
  692. register_error(elgg_echo('actionunauthorized'));
  693. return false;
  694. }
  695. /**
  696. * Adds default admin widgets to the admin dashboard.
  697. *
  698. * @param string $event
  699. * @param string $type
  700. * @param \ElggUser $user
  701. *
  702. * @return null|true
  703. * @access private
  704. */
  705. function _elgg_add_admin_widgets($event, $type, $user) {
  706. elgg_set_ignore_access(true);
  707. // check if the user already has widgets
  708. if (elgg_get_widgets($user->getGUID(), 'admin')) {
  709. return true;
  710. }
  711. // In the form column => array of handlers in order, top to bottom
  712. $adminWidgets = array(
  713. 1 => array('control_panel', 'admin_welcome'),
  714. 2 => array('online_users', 'new_users', 'content_stats'),
  715. );
  716. foreach ($adminWidgets as $column => $handlers) {
  717. foreach ($handlers as $position => $handler) {
  718. $guid = elgg_create_widget($user->getGUID(), $handler, 'admin');
  719. if ($guid) {
  720. $widget = get_entity($guid);
  721. /* @var \ElggWidget $widget */
  722. $widget->move($column, $position);
  723. }
  724. }
  725. }
  726. elgg_set_ignore_access(false);
  727. }
  728. return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
  729. $events->registerHandler('init', 'system', '_elgg_admin_init');
  730. };