admin.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  1. <?php
  2. namespace Grav\Plugin;
  3. use Grav\Common\Data;
  4. use Grav\Common\File\CompiledYamlFile;
  5. use Grav\Common\GPM\GPM;
  6. use Grav\Common\Grav;
  7. use Grav\Common\Language\LanguageCodes;
  8. use Grav\Common\Page\Page;
  9. use Grav\Common\Page\Pages;
  10. use Grav\Common\Plugins;
  11. use Grav\Common\Themes;
  12. use Grav\Common\Uri;
  13. use Grav\Common\User\User;
  14. use RocketTheme\Toolbox\File\File;
  15. use RocketTheme\Toolbox\File\JsonFile;
  16. use RocketTheme\Toolbox\File\LogFile;
  17. use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator;
  18. use RocketTheme\Toolbox\Session\Message;
  19. use RocketTheme\Toolbox\Session\Session;
  20. use Symfony\Component\Yaml\Yaml;
  21. define('LOGIN_REDIRECT_COOKIE', 'grav-login-redirect');
  22. class Admin
  23. {
  24. /**
  25. * @var Grav
  26. */
  27. public $grav;
  28. /**
  29. * @var Uri $uri
  30. */
  31. protected $uri;
  32. /**
  33. * @var array
  34. */
  35. protected $pages = array();
  36. /**
  37. * @var Session
  38. */
  39. protected $session;
  40. /**
  41. * @var Data\Blueprints
  42. */
  43. protected $blueprints;
  44. /**
  45. * @var string
  46. */
  47. public $base;
  48. /**
  49. * @var string
  50. */
  51. public $location;
  52. /**
  53. * @var string
  54. */
  55. public $route;
  56. /**
  57. * @var User
  58. */
  59. public $user;
  60. /**
  61. * @var Lang
  62. */
  63. protected $lang;
  64. /**
  65. * @var Grav\Common\GPM\GPM
  66. */
  67. protected $gpm;
  68. /**
  69. * Constructor.
  70. *
  71. * @param Grav $grav
  72. * @param string $base
  73. * @param string $location
  74. * @param string $route
  75. */
  76. public function __construct(Grav $grav, $base, $location, $route)
  77. {
  78. $this->grav = $grav;
  79. $this->base = $base;
  80. $this->location = $location;
  81. $this->route = $route;
  82. $this->uri = $this->grav['uri'];
  83. $this->session = $this->grav['session'];
  84. $this->user = $this->grav['user'];
  85. $language = $this->grav['language'];
  86. if ($language->enabled()) {
  87. $this->multilang = true;
  88. $this->languages_enabled = $this->grav['config']->get('system.languages.supported', []);
  89. //Set the currently active language for the admin
  90. $language = $this->grav['uri']->param('lang');
  91. if (!$language) {
  92. if (!$this->session->admin_lang) $this->session->admin_lang = 'en';
  93. $language = $this->session->admin_lang;
  94. }
  95. $this->grav['language']->setActive($language ?: 'en');
  96. } else {
  97. $this->grav['language']->setActive('en');
  98. $this->multilang = false;
  99. }
  100. }
  101. /**
  102. * Get current session.
  103. *
  104. * @return Session
  105. */
  106. public function session()
  107. {
  108. return $this->session;
  109. }
  110. /**
  111. * Add message into the session queue.
  112. *
  113. * @param string $msg
  114. * @param string $type
  115. */
  116. public function setMessage($msg, $type = 'info')
  117. {
  118. /** @var Message $messages */
  119. $messages = $this->grav['messages'];
  120. $messages->add($msg, $type);
  121. }
  122. /**
  123. * Fetch and delete messages from the session queue.
  124. *
  125. * @param string $type
  126. *
  127. * @return array
  128. */
  129. public function messages($type = null)
  130. {
  131. /** @var Message $messages */
  132. $messages = $this->grav['messages'];
  133. return $messages->fetch($type);
  134. }
  135. /**
  136. * Authenticate user.
  137. *
  138. * @param array $form Form fields.
  139. *
  140. * @return bool
  141. */
  142. public function authenticate($form)
  143. {
  144. if (!$this->user->authenticated && isset($form['username']) && isset($form['password'])) {
  145. $user = User::load($form['username']);
  146. //default to english if language not set
  147. if (empty($user->language)) {
  148. $user->set('language', 'en');
  149. }
  150. if ($user->exists()) {
  151. $user->authenticated = true;
  152. // Authenticate user.
  153. $result = $user->authenticate($form['password']);
  154. if ($result) {
  155. $this->user = $this->session->user = $user;
  156. /** @var Grav $grav */
  157. $grav = $this->grav;
  158. $this->setMessage($this->translate('PLUGIN_ADMIN.LOGIN_LOGGED_IN', [$this->user->language]), 'info');
  159. $redirect_route = $this->uri->route();
  160. $grav->redirect($redirect_route);
  161. }
  162. }
  163. }
  164. return $this->authorize();
  165. }
  166. /**
  167. * Checks user authorisation to the action.
  168. *
  169. * @param string $action
  170. *
  171. * @return bool
  172. */
  173. public function authorize($action = 'admin.login')
  174. {
  175. $action = (array)$action;
  176. foreach ($action as $a) {
  177. if ($this->user->authorize($a)) {
  178. return true;
  179. }
  180. }
  181. return false;
  182. }
  183. /**
  184. * Returns edited page.
  185. *
  186. * @param bool $route
  187. *
  188. * @return Page
  189. */
  190. public function page($route = false)
  191. {
  192. $path = $this->route;
  193. if ($route && !$path) {
  194. $path = '/';
  195. }
  196. if (!isset($this->pages[$path])) {
  197. $this->pages[$path] = $this->getPage($path);
  198. }
  199. return $this->pages[$path];
  200. }
  201. /**
  202. * Returns blueprints for the given type.
  203. *
  204. * @param string $type
  205. *
  206. * @return Data\Blueprint
  207. */
  208. public function blueprints($type)
  209. {
  210. if ($this->blueprints === null) {
  211. $this->blueprints = new Data\Blueprints('blueprints://');
  212. }
  213. return $this->blueprints->get($type);
  214. }
  215. /**
  216. * Gets configuration data.
  217. *
  218. * @param string $type
  219. * @param array $post
  220. *
  221. * @return Data\Data|null
  222. * @throws \RuntimeException
  223. */
  224. public function data($type, $post = array())
  225. {
  226. static $data = [];
  227. if (isset($data[$type])) {
  228. return $data[$type];
  229. }
  230. if (!$post) {
  231. $post = isset($_POST) ? $_POST : [];
  232. }
  233. switch ($type) {
  234. case 'configuration':
  235. case 'system':
  236. $type = 'system';
  237. $blueprints = $this->blueprints("config/{$type}");
  238. $config = $this->grav['config'];
  239. $obj = new Data\Data($config->get('system'), $blueprints);
  240. $obj->merge($post);
  241. $file = CompiledYamlFile::instance($this->grav['locator']->findResource("config://{$type}.yaml"));
  242. $obj->file($file);
  243. $data[$type] = $obj;
  244. break;
  245. case 'settings':
  246. case 'site':
  247. $type = 'site';
  248. $blueprints = $this->blueprints("config/{$type}");
  249. $config = $this->grav['config'];
  250. $obj = new Data\Data($config->get('site'), $blueprints);
  251. $obj->merge($post);
  252. $file = CompiledYamlFile::instance($this->grav['locator']->findResource("config://{$type}.yaml"));
  253. $obj->file($file);
  254. $data[$type] = $obj;
  255. break;
  256. case 'login':
  257. $data[$type] = null;
  258. break;
  259. default:
  260. /** @var UniformResourceLocator $locator */
  261. $locator = $this->grav['locator'];
  262. $filename = $locator->findResource("config://{$type}.yaml", true, true);
  263. $file = CompiledYamlFile::instance($filename);
  264. if (preg_match('|plugins/|', $type)) {
  265. /** @var Plugins $plugins */
  266. $plugins = $this->grav['plugins'];
  267. $obj = $plugins->get(preg_replace('|plugins/|', '', $type));
  268. $obj->merge($post);
  269. $obj->file($file);
  270. $data[$type] = $obj;
  271. } elseif (preg_match('|themes/|', $type)) {
  272. /** @var Themes $themes */
  273. $themes = $this->grav['themes'];
  274. $obj = $themes->get(preg_replace('|themes/|', '', $type));
  275. $obj->merge($post);
  276. $obj->file($file);
  277. $data[$type] = $obj;
  278. } elseif (preg_match('|users/|', $type)) {
  279. $obj = User::load(preg_replace('|users/|', '', $type));
  280. $obj->merge($post);
  281. $data[$type] = $obj;
  282. } else {
  283. throw new \RuntimeException("Data type '{$type}' doesn't exist!");
  284. }
  285. }
  286. return $data[$type];
  287. }
  288. /**
  289. * Get the GPM instance
  290. *
  291. * @return GPM The GPM instance
  292. */
  293. public function gpm()
  294. {
  295. if (!$this->gpm) {
  296. try {
  297. $this->gpm = new GPM();
  298. } catch (\Exception $e) {}
  299. }
  300. return $this->gpm;
  301. }
  302. /**
  303. * Converts dot notation to array notation.
  304. *
  305. * @param string $name
  306. *
  307. * @return string
  308. */
  309. public function field($name)
  310. {
  311. $path = explode('.', $name);
  312. return array_shift($path) . ($path ? '[' . implode('][', $path) . ']' : '');
  313. }
  314. /**
  315. * Get all routes.
  316. *
  317. * @return array
  318. */
  319. public function routes($unique = false)
  320. {
  321. /** @var Pages $pages */
  322. $pages = $this->grav['pages'];
  323. if ($unique) {
  324. $routes = array_unique($pages->routes());
  325. } else {
  326. $routes = $pages->routes();
  327. }
  328. return $routes;
  329. }
  330. /**
  331. * Count the pages
  332. *
  333. * @return array
  334. */
  335. public function countPages()
  336. {
  337. $routable = $this->grav['pages']->all()->routable();
  338. $modular = $this->grav['pages']->all()->modular();
  339. return count($routable) + count($modular);
  340. }
  341. /**
  342. * Get All template types
  343. *
  344. * @return array
  345. */
  346. public function types()
  347. {
  348. return Pages::types();
  349. }
  350. /**
  351. * Get All modular template types
  352. *
  353. * @return array
  354. */
  355. public function modularTypes()
  356. {
  357. return Pages::modularTypes();
  358. }
  359. /**
  360. * Get all plugins.
  361. *
  362. * @return array
  363. */
  364. public function plugins($local = true)
  365. {
  366. $gpm = $this->gpm();
  367. if (!$gpm) {
  368. return;
  369. }
  370. return $local ? $gpm->getInstalledPlugins() : $gpm->getRepositoryPlugins()->filter(function (
  371. $package,
  372. $slug
  373. ) use ($gpm) {
  374. return !$gpm->isPluginInstalled($slug);
  375. });
  376. }
  377. /**
  378. * Get all themes.
  379. *
  380. * @return array
  381. */
  382. public function themes($local = true)
  383. {
  384. $gpm = $this->gpm();
  385. if (!$gpm) {
  386. return;
  387. }
  388. return $local ? $gpm->getInstalledThemes() : $gpm->getRepositoryThemes()->filter(function ($package, $slug) use
  389. (
  390. $gpm
  391. ) {
  392. return !$gpm->isThemeInstalled($slug);
  393. });
  394. }
  395. /**
  396. * Get log file for fatal errors.
  397. *
  398. * @return string
  399. */
  400. public function logs()
  401. {
  402. if (!isset($this->logs)) {
  403. $file = LogFile::instance($this->grav['locator']->findResource('log://exception.log'));
  404. $content = $file->content();
  405. $this->logs = array_reverse($content);
  406. }
  407. return $this->logs;
  408. }
  409. /**
  410. * Used by the Dashboard in the admin to display the X latest pages
  411. * that have been modified
  412. *
  413. * @param integer $count number of pages to pull back
  414. *
  415. * @return array
  416. */
  417. public function latestPages($count = 10)
  418. {
  419. /** @var Pages $pages */
  420. $pages = $this->grav['pages'];
  421. $latest = array();
  422. foreach ($pages->routes() as $url => $path) {
  423. $page = $pages->dispatch($url, true);
  424. if ($page && $page->routable()) {
  425. $latest[$page->route()] = ['modified' => $page->modified(), 'page' => $page];
  426. }
  427. }
  428. // sort based on modified
  429. uasort($latest, function ($a, $b) {
  430. if ($a['modified'] == $b['modified']) {
  431. return 0;
  432. }
  433. return ($a['modified'] > $b['modified']) ? -1 : 1;
  434. });
  435. // build new array with just pages in it
  436. $list = array();
  437. foreach ($latest as $item) {
  438. $list[] = $item['page'];
  439. }
  440. return array_slice($list, 0, $count);
  441. }
  442. /**
  443. * Get log file for fatal errors.
  444. *
  445. * @return string
  446. */
  447. public function logEntry()
  448. {
  449. $file = File::instance($this->grav['locator']->findResource("log://{$this->route}.html"));
  450. $content = $file->content();
  451. return $content;
  452. }
  453. /**
  454. * Search in the logs when was the latest backup made
  455. *
  456. * @return array Array containing the latest backup information
  457. */
  458. public function lastBackup()
  459. {
  460. $file = JsonFile::instance($this->grav['locator']->findResource("log://backup.log"));
  461. $content = $file->content();
  462. if (empty($content)) {
  463. return [
  464. 'days' => '&infin;',
  465. 'chart_fill' => 100,
  466. 'chart_empty' => 0
  467. ];
  468. }
  469. $backup = new \DateTime();
  470. $backup->setTimestamp($content['time']);
  471. $diff = $backup->diff(new \DateTime());
  472. $days = $diff->days;
  473. $chart_fill = $days > 30 ? 100 : round($days / 30 * 100);
  474. return [
  475. 'days' => $days,
  476. 'chart_fill' => $chart_fill,
  477. 'chart_empty' => 100 - $chart_fill
  478. ];
  479. }
  480. /**
  481. * Returns the page creating it if it does not exist.
  482. *
  483. * @param $path
  484. *
  485. * @return Page
  486. */
  487. public function getPage($path)
  488. {
  489. /** @var Pages $pages */
  490. $pages = $this->grav['pages'];
  491. if ($path && $path[0] != '/') {
  492. $path = "/{$path}";
  493. }
  494. $page = $path ? $pages->dispatch($path, true) : $pages->root();
  495. if (!$page) {
  496. $slug = basename($path);
  497. if ($slug == '') {
  498. return null;
  499. }
  500. $ppath = str_replace('\\', '/' , dirname($path));
  501. // Find or create parent(s).
  502. $parent = $this->getPage($ppath != '/' ? $ppath : '');
  503. // Create page.
  504. $page = new Page;
  505. $page->parent($parent);
  506. $page->filePath($parent->path() . '/' . $slug . '/' . $page->name());
  507. // Add routing information.
  508. $pages->addPage($page, $path);
  509. // Set if Modular
  510. $page->modularTwig($slug[0] == '_');
  511. // Determine page type.
  512. if (isset($this->session->{$page->route()})) {
  513. // Found the type and header from the session.
  514. $data = $this->session->{$page->route()};
  515. $header = ['title' => $data['title']];
  516. if (isset($data['visible'])) {
  517. if ($data['visible'] == '' || $data['visible']) {
  518. // if auto (ie '')
  519. $children = $page->parent()->children();
  520. foreach ($children as $child) {
  521. if ($child->order()) {
  522. // set page order
  523. $page->order(1000);
  524. break;
  525. }
  526. }
  527. }
  528. }
  529. if ($data['name'] == 'modular') {
  530. $header['body_classes'] = 'modular';
  531. }
  532. $name = $page->modular() ? str_replace('modular/', '', $data['name']) : $data['name'];
  533. $page->name($name . '.md');
  534. $page->header($header);
  535. $page->frontmatter(Yaml::dump((array)$page->header(), 10, 2, false));
  536. } else {
  537. // Find out the type by looking at the parent.
  538. $type = $parent->childType() ? $parent->childType() : $parent->blueprints()->get('child_type',
  539. 'default');
  540. $page->name($type . CONTENT_EXT);
  541. $page->header();
  542. }
  543. $page->modularTwig($slug[0] == '_');
  544. }
  545. return $page;
  546. }
  547. /**
  548. * Return the languages available in the admin
  549. *
  550. * @return array
  551. */
  552. public static function adminLanguages()
  553. {
  554. $languages = [];
  555. $lang_data = Yaml::parse(file_get_contents(__DIR__ . '/../languages.yaml'));
  556. foreach ($lang_data as $lang => $values) {
  557. $languages[$lang] = LanguageCodes::getNativeName($lang);
  558. }
  559. return $languages;
  560. }
  561. /**
  562. * Return the languages available in the site
  563. *
  564. * @return array
  565. */
  566. public static function siteLanguages()
  567. {
  568. $languages = [];
  569. $lang_data = Grav::instance()['config']->get('system.languages.supported', []);
  570. foreach ($lang_data as $index => $lang) {
  571. $languages[$lang] = LanguageCodes::getNativeName($lang);
  572. }
  573. return $languages;
  574. }
  575. /**
  576. * Static helper method to return current route.
  577. *
  578. * @return string
  579. */
  580. public static function route()
  581. {
  582. $pages = Grav::instance()['pages'];
  583. $route = '/' . ltrim(Grav::instance()['admin']->route, '/');
  584. $page = $pages->dispatch($route);
  585. $parent_route = null;
  586. if ($page) {
  587. $parent = $page->parent();
  588. $parent_route = $parent->rawRoute();
  589. }
  590. return $parent_route;
  591. }
  592. /**
  593. * Static helper method to return the last used page name
  594. *
  595. * @return string
  596. */
  597. public static function getLastPageName()
  598. {
  599. return Grav::instance()['session']->lastPageName ?: 'default';
  600. }
  601. /**
  602. * Static helper method to return the last used page route
  603. *
  604. * @return string
  605. */
  606. public static function getLastPageRoute()
  607. {
  608. return Grav::instance()['session']->lastPageRoute ?: self::route();
  609. }
  610. /**
  611. * Determine if the plugin or theme info passed is from Team Grav
  612. *
  613. * @param object $info Plugin or Theme info object
  614. *
  615. * @return bool
  616. */
  617. public function isTeamGrav($info)
  618. {
  619. if (isset($info['author']['name']) && $info['author']['name'] == 'Team Grav') {
  620. return true;
  621. } else {
  622. return false;
  623. }
  624. }
  625. /**
  626. * Renders phpinfo
  627. *
  628. * @return string The phpinfo() output
  629. */
  630. function phpinfo() {
  631. if (function_exists('phpinfo')) {
  632. ob_start();
  633. phpinfo();
  634. $pinfo = ob_get_contents();
  635. ob_end_clean();
  636. $pinfo = preg_replace('%^.*<body>(.*)</body>.*$%ms', '$1', $pinfo);
  637. return $pinfo;
  638. } else {
  639. return 'phpinfo() method is not available on this server.';
  640. }
  641. }
  642. /**
  643. * Translate a string to the user-defined language
  644. *
  645. * @param $string the string to translate
  646. */
  647. public function translate($string) {
  648. return $this->_translate($string, [$this->grav['user']->authenticated ? $this->grav['user']->language : 'en']);
  649. }
  650. public function _translate($args, Array $languages = null, $array_support = false, $html_out = false)
  651. {
  652. if (is_array($args)) {
  653. $lookup = array_shift($args);
  654. } else {
  655. $lookup = $args;
  656. $args = [];
  657. }
  658. if ($lookup) {
  659. if (empty($languages) || reset($languages) == null) {
  660. if ($this->grav['config']->get('system.languages.translations_fallback', true)) {
  661. $languages = $this->grav['language']->getFallbackLanguages();
  662. } else {
  663. $languages = (array)$this->grav['language']->getDefault();
  664. }
  665. }
  666. } else {
  667. $languages = ['en'];
  668. }
  669. foreach ((array)$languages as $lang) {
  670. $translation = $this->grav['language']->getTranslation($lang, $lookup, $array_support);
  671. if (!$translation) {
  672. $language = $this->grav['language']->getDefault() ?: 'en';
  673. $translation = $this->grav['language']->getTranslation($language, $lookup, $array_support);
  674. }
  675. if ($translation) {
  676. if (count($args) >= 1) {
  677. return vsprintf($translation, $args);
  678. } else {
  679. return $translation;
  680. }
  681. }
  682. }
  683. return $lookup;
  684. }
  685. function dateformat2Kendo($php_format)
  686. {
  687. $SYMBOLS_MATCHING = array(
  688. // Day
  689. 'd' => 'dd',
  690. 'D' => 'ddd',
  691. 'j' => 'd',
  692. 'l' => 'dddd',
  693. 'N' => '',
  694. 'S' => '',
  695. 'w' => '',
  696. 'z' => '',
  697. // Week
  698. 'W' => '',
  699. // Month
  700. 'F' => 'MMMM',
  701. 'm' => 'MM',
  702. 'M' => 'MMM',
  703. 'n' => 'M',
  704. 't' => '',
  705. // Year
  706. 'L' => '',
  707. 'o' => '',
  708. 'Y' => 'yyyy',
  709. 'y' => 'yy',
  710. // Time
  711. 'a' => 'tt',
  712. 'A' => 'tt',
  713. 'B' => '',
  714. 'g' => 'h',
  715. 'G' => 'H',
  716. 'h' => 'hh',
  717. 'H' => 'HH',
  718. 'i' => 'mm',
  719. 's' => 'ss',
  720. 'u' => ''
  721. );
  722. $js_format = "";
  723. $escaping = false;
  724. for($i = 0; $i < strlen($php_format); $i++)
  725. {
  726. $char = $php_format[$i];
  727. if($char === '\\') // PHP date format escaping character
  728. {
  729. $i++;
  730. if($escaping) $js_format .= $php_format[$i];
  731. else $js_format .= '\'' . $php_format[$i];
  732. $escaping = true;
  733. }
  734. else
  735. {
  736. if($escaping) { $js_format .= "'"; $escaping = false; }
  737. if(isset($SYMBOLS_MATCHING[$char]))
  738. $js_format .= $SYMBOLS_MATCHING[$char];
  739. else
  740. $js_format .= $char;
  741. }
  742. }
  743. return $js_format;
  744. }
  745. }