| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2019-03-20 12:52:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  | namespace Grav\Plugin\Admin; | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-10 13:32:17 -06:00
										 |  |  | use DateTime; | 
					
						
							| 
									
										
										
										
											2015-08-11 18:30:27 -06:00
										 |  |  | use Grav\Common\Data; | 
					
						
							| 
									
										
										
										
											2019-10-25 10:51:44 -06:00
										 |  |  | use Grav\Common\Data\Data as GravData; | 
					
						
							| 
									
										
										
										
											2020-02-10 22:19:39 +02:00
										 |  |  | use Grav\Common\Debugger; | 
					
						
							| 
									
										
										
										
											2014-10-07 12:12:21 +03:00
										 |  |  | use Grav\Common\File\CompiledYamlFile; | 
					
						
							| 
									
										
										
										
											2020-01-21 09:46:42 +02:00
										 |  |  | use Grav\Common\Flex\Users\UserObject; | 
					
						
							| 
									
										
										
										
											2015-08-11 18:30:27 -06:00
										 |  |  | use Grav\Common\GPM\GPM; | 
					
						
							| 
									
										
										
										
											2016-10-13 10:13:18 -07:00
										 |  |  | use Grav\Common\GPM\Licenses; | 
					
						
							| 
									
										
										
										
											2016-08-15 10:45:58 -06:00
										 |  |  | use Grav\Common\GPM\Response; | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  | use Grav\Common\Grav; | 
					
						
							| 
									
										
										
										
											2019-04-20 17:43:26 -06:00
										 |  |  | use Grav\Common\Helpers\YamlLinter; | 
					
						
							| 
									
										
										
										
											2019-06-27 13:19:30 +03:00
										 |  |  | use Grav\Common\Language\Language; | 
					
						
							| 
									
										
										
										
											2015-08-17 18:22:15 -06:00
										 |  |  | use Grav\Common\Language\LanguageCodes; | 
					
						
							| 
									
										
										
										
											2018-05-10 10:14:18 +03:00
										 |  |  | use Grav\Common\Page\Collection; | 
					
						
							| 
									
										
										
										
											2019-03-05 15:46:55 +02:00
										 |  |  | use Grav\Common\Page\Interfaces\PageInterface; | 
					
						
							| 
									
										
										
										
											2015-08-11 18:30:27 -06:00
										 |  |  | use Grav\Common\Page\Page; | 
					
						
							|  |  |  | use Grav\Common\Page\Pages; | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  | use Grav\Common\Plugins; | 
					
						
							| 
									
										
										
										
											2018-10-07 14:24:23 -06:00
										 |  |  | use Grav\Common\Security; | 
					
						
							| 
									
										
										
										
											2019-03-20 12:52:16 +02:00
										 |  |  | use Grav\Common\Session; | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  | use Grav\Common\Themes; | 
					
						
							|  |  |  | use Grav\Common\Uri; | 
					
						
							| 
									
										
										
										
											2019-03-20 12:52:16 +02:00
										 |  |  | use Grav\Common\User\Interfaces\UserCollectionInterface; | 
					
						
							| 
									
										
										
										
											2020-01-16 15:48:47 +02:00
										 |  |  | use Grav\Common\User\Interfaces\UserInterface; | 
					
						
							| 
									
										
										
										
											2015-08-11 18:30:27 -06:00
										 |  |  | use Grav\Common\User\User; | 
					
						
							| 
									
										
										
										
											2015-11-21 16:15:23 +01:00
										 |  |  | use Grav\Common\Utils; | 
					
						
							| 
									
										
										
										
											2020-01-15 16:05:16 +02:00
										 |  |  | use Grav\Framework\Acl\Action; | 
					
						
							|  |  |  | use Grav\Framework\Acl\Permissions; | 
					
						
							| 
									
										
										
										
											2018-10-07 14:24:23 -06:00
										 |  |  | use Grav\Framework\Collection\ArrayCollection; | 
					
						
							| 
									
										
										
										
											2019-08-24 13:20:16 +03:00
										 |  |  | use Grav\Framework\Flex\Flex; | 
					
						
							| 
									
										
										
										
											2020-01-21 09:46:42 +02:00
										 |  |  | use Grav\Framework\Flex\Interfaces\FlexInterface; | 
					
						
							| 
									
										
										
										
											2019-11-13 11:49:18 +02:00
										 |  |  | use Grav\Framework\Flex\Interfaces\FlexObjectInterface; | 
					
						
							| 
									
										
										
										
											2019-06-27 13:19:30 +03:00
										 |  |  | use Grav\Framework\Route\Route; | 
					
						
							|  |  |  | use Grav\Framework\Route\RouteFactory; | 
					
						
							| 
									
										
										
										
											2018-05-10 20:04:14 +03:00
										 |  |  | use Grav\Plugin\Login\Login; | 
					
						
							|  |  |  | use Grav\Plugin\Login\TwoFactorAuth\TwoFactorAuth; | 
					
						
							| 
									
										
										
										
											2019-02-08 17:17:05 -07:00
										 |  |  | use PicoFeed\Parser\MalformedXmlException; | 
					
						
							| 
									
										
										
										
											2016-09-30 08:58:26 -06:00
										 |  |  | use RocketTheme\Toolbox\Event\Event; | 
					
						
							| 
									
										
										
										
											2014-10-01 22:28:16 +03:00
										 |  |  | use RocketTheme\Toolbox\File\File; | 
					
						
							| 
									
										
										
										
											2015-04-27 13:37:22 +02:00
										 |  |  | use RocketTheme\Toolbox\File\JsonFile; | 
					
						
							| 
									
										
										
										
											2016-01-22 21:52:25 +02:00
										 |  |  | use RocketTheme\Toolbox\ResourceLocator\UniformResourceIterator; | 
					
						
							| 
									
										
										
										
											2014-10-07 12:12:21 +03:00
										 |  |  | use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator; | 
					
						
							| 
									
										
										
										
											2014-10-01 22:28:16 +03:00
										 |  |  | use RocketTheme\Toolbox\Session\Message; | 
					
						
							| 
									
										
										
										
											2018-08-22 12:45:39 -06:00
										 |  |  | use Grav\Common\Yaml; | 
					
						
							| 
									
										
										
										
											2016-08-11 19:17:02 +02:00
										 |  |  | use Composer\Semver\Semver; | 
					
						
							|  |  |  | use PicoFeed\Reader\Reader; | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-28 11:11:43 -06:00
										 |  |  | define('LOGIN_REDIRECT_COOKIE', 'grav-login-redirect'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  | class Admin | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-10 22:19:39 +02:00
										 |  |  |     public const DEBUG = 1; | 
					
						
							|  |  |  |     public const MEDIA_PAGINATION_INTERVAL = 20; | 
					
						
							|  |  |  |     public const TMP_COOKIE_NAME = 'tmp-admin-message'; | 
					
						
							| 
									
										
										
										
											2017-05-26 14:30:37 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-20 12:52:16 +02:00
										 |  |  |     /** @var Grav */ | 
					
						
							| 
									
										
										
										
											2014-08-29 11:27:53 +03:00
										 |  |  |     public $grav; | 
					
						
							| 
									
										
										
										
											2019-03-20 12:52:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** @var string */ | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |     public $base; | 
					
						
							| 
									
										
										
										
											2019-03-20 12:52:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** @var string */ | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |     public $location; | 
					
						
							| 
									
										
										
										
											2019-03-20 12:52:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** @var string */ | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |     public $route; | 
					
						
							| 
									
										
										
										
											2019-03-20 12:52:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** @var User */ | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |     public $user; | 
					
						
							| 
									
										
										
										
											2019-03-20 12:52:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** @var array */ | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |     public $forgot; | 
					
						
							| 
									
										
										
										
											2019-03-20 12:52:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** @var string */ | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |     public $task; | 
					
						
							| 
									
										
										
										
											2019-03-20 12:52:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** @var array */ | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |     public $json_response; | 
					
						
							| 
									
										
										
										
											2019-03-20 12:52:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** @var Collection */ | 
					
						
							|  |  |  |     public $collection; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** @var bool */ | 
					
						
							|  |  |  |     public $multilang; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-27 13:19:30 +03:00
										 |  |  |     /** @var string */ | 
					
						
							|  |  |  |     public $language; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-20 12:52:16 +02:00
										 |  |  |     /** @var array */ | 
					
						
							| 
									
										
										
										
											2019-06-27 13:19:30 +03:00
										 |  |  |     public $languages_enabled = []; | 
					
						
							| 
									
										
										
										
											2019-03-20 12:52:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** @var Uri $uri */ | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |     protected $uri; | 
					
						
							| 
									
										
										
										
											2019-03-20 12:52:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** @var array */ | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |     protected $pages = []; | 
					
						
							| 
									
										
										
										
											2019-03-20 12:52:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** @var Session */ | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |     protected $session; | 
					
						
							| 
									
										
										
										
											2019-03-20 12:52:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** @var Data\Blueprints */ | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |     protected $blueprints; | 
					
						
							| 
									
										
										
										
											2019-03-20 12:52:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** @var GPM */ | 
					
						
							| 
									
										
										
										
											2015-04-04 14:49:40 +02:00
										 |  |  |     protected $gpm; | 
					
						
							| 
									
										
										
										
											2014-10-02 12:14:13 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-20 12:52:16 +02:00
										 |  |  |     /** @var int */ | 
					
						
							| 
									
										
										
										
											2016-01-28 14:03:53 +01:00
										 |  |  |     protected $pages_count; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-20 12:52:16 +02:00
										 |  |  |     /** @var bool */ | 
					
						
							| 
									
										
										
										
											2017-05-26 14:30:37 +02:00
										 |  |  |     protected $load_additional_files_in_background = false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-20 12:52:16 +02:00
										 |  |  |     /** @var bool */ | 
					
						
							| 
									
										
										
										
											2017-05-26 14:30:37 +02:00
										 |  |  |     protected $loading_additional_files_in_background = false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-20 12:52:16 +02:00
										 |  |  |     /** @var array */ | 
					
						
							| 
									
										
										
										
											2017-08-16 10:59:22 -06:00
										 |  |  |     protected $temp_messages = []; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Constructor. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2014-08-29 11:27:53 +03:00
										 |  |  |      * @param Grav   $grav | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |      * @param string $base | 
					
						
							|  |  |  |      * @param string $location | 
					
						
							|  |  |  |      * @param string $route | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2014-08-29 11:27:53 +03:00
										 |  |  |     public function __construct(Grav $grav, $base, $location, $route) | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-11-21 12:25:20 +02:00
										 |  |  |         // Register admin to grav because of calling $grav['user'] requires it.
 | 
					
						
							|  |  |  |         $grav['admin']     = $this; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |         $this->grav        = $grav; | 
					
						
							|  |  |  |         $this->base        = $base; | 
					
						
							|  |  |  |         $this->location    = $location; | 
					
						
							|  |  |  |         $this->route       = $route; | 
					
						
							| 
									
										
										
										
											2018-11-21 12:25:20 +02:00
										 |  |  |         $this->uri         = $grav['uri']; | 
					
						
							|  |  |  |         $this->session     = $grav['session']; | 
					
						
							| 
									
										
										
										
											2020-01-16 15:48:47 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-21 09:46:42 +02:00
										 |  |  |         /** @var FlexInterface|null $flex */ | 
					
						
							| 
									
										
										
										
											2020-01-16 15:48:47 +02:00
										 |  |  |         $flex = $grav['flex_objects'] ?? null; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** @var UserInterface $user */ | 
					
						
							|  |  |  |         $user = $grav['user']; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-31 11:56:03 +02:00
										 |  |  |         // Convert old user to Flex User if Flex Objects plugin has been enabled.
 | 
					
						
							| 
									
										
										
										
											2020-01-16 15:48:47 +02:00
										 |  |  |         if ($flex && !$user instanceof FlexObjectInterface) { | 
					
						
							| 
									
										
										
										
											2020-01-30 11:02:32 +02:00
										 |  |  |             $managed = !method_exists($flex, 'isManaged') || $flex->isManaged('user-accounts'); | 
					
						
							|  |  |  |             $directory = $managed ? $flex->getDirectory('user-accounts') : null; | 
					
						
							| 
									
										
										
										
											2020-01-30 13:07:57 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-21 09:46:42 +02:00
										 |  |  |             /** @var UserObject|null $test */ | 
					
						
							| 
									
										
										
										
											2020-02-03 12:15:51 +02:00
										 |  |  |             $test = $directory ? $directory->getObject($user->username) : null; | 
					
						
							| 
									
										
										
										
											2020-01-21 09:46:42 +02:00
										 |  |  |             if ($test) { | 
					
						
							| 
									
										
										
										
											2020-02-03 12:15:51 +02:00
										 |  |  |                 $test = clone $test; | 
					
						
							|  |  |  |                 $test->access = $user->access; | 
					
						
							|  |  |  |                 $test->groups = $user->groups; | 
					
						
							| 
									
										
										
										
											2020-01-21 09:46:42 +02:00
										 |  |  |                 $test->authenticated = $user->authenticated; | 
					
						
							|  |  |  |                 $test->authorized = $user->authorized; | 
					
						
							|  |  |  |                 $user = $test; | 
					
						
							| 
									
										
										
										
											2020-01-16 15:48:47 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $this->user = $user; | 
					
						
							| 
									
										
										
										
											2019-06-27 13:19:30 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |         /** @var Language $language */ | 
					
						
							|  |  |  |         $language = $grav['language']; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->multilang = $language->enabled(); | 
					
						
							| 
									
										
										
										
											2016-07-07 18:55:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-07 18:47:48 +02:00
										 |  |  |         // Load utility class
 | 
					
						
							| 
									
										
										
										
											2019-06-27 13:19:30 +03:00
										 |  |  |         if ($this->multilang) { | 
					
						
							| 
									
										
										
										
											2019-09-11 18:05:45 +03:00
										 |  |  |             $this->language = $language->getActive() ?? ''; | 
					
						
							| 
									
										
										
										
											2019-03-20 12:52:16 +02:00
										 |  |  |             $this->languages_enabled = (array)$this->grav['config']->get('system.languages.supported', []); | 
					
						
							| 
									
										
										
										
											2015-08-14 19:12:04 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             //Set the currently active language for the admin
 | 
					
						
							| 
									
										
										
										
											2019-06-27 13:19:30 +03:00
										 |  |  |             $languageCode = $this->grav['uri']->param('lang'); | 
					
						
							| 
									
										
										
										
											2019-09-11 18:05:45 +03:00
										 |  |  |             if (null === $languageCode && !$this->session->admin_lang) { | 
					
						
							|  |  |  |                 $this->session->admin_lang = $language->getActive() ?? ''; | 
					
						
							| 
									
										
										
										
											2015-08-14 19:12:04 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2019-09-11 18:05:45 +03:00
										 |  |  |             $this->language = ''; | 
					
						
							| 
									
										
										
										
											2015-08-14 19:12:04 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-10 22:19:39 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param string $message | 
					
						
							|  |  |  |      * @param array $data | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public static function addDebugMessage(string $message, $data = []) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         /** @var Debugger $debugger */ | 
					
						
							|  |  |  |         $debugger = Grav::instance()['debugger']; | 
					
						
							|  |  |  |         $debugger->addMessage($message, 'debug', $data); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |      * Return the languages available in the admin | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |      * @return array | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |     public static function adminLanguages() | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |         $languages = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $path = Grav::instance()['locator']->findResource('plugins://admin/languages'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** @var \DirectoryIterator $directory */ | 
					
						
							|  |  |  |         foreach (new \DirectoryIterator($path) as $file) { | 
					
						
							| 
									
										
										
										
											2018-10-04 06:43:36 -04:00
										 |  |  |             if ($file->isDir() || $file->isDot() || Utils::startsWith($file->getFilename(), '.')) { | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |                 continue; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-04 06:43:36 -04:00
										 |  |  |             $lang = $file->getBasename('.yaml'); | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |             $languages[$lang] = LanguageCodes::getNativeName($lang); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-04 12:27:02 -07:00
										 |  |  |         // sort languages
 | 
					
						
							|  |  |  |         asort($languages); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |         return $languages; | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-27 13:19:30 +03:00
										 |  |  |     public function getLanguage(): string | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-09-11 18:05:45 +03:00
										 |  |  |         return $this->language ?: $this->grav['language']->getLanguage() ?: 'en'; | 
					
						
							| 
									
										
										
										
											2019-06-27 13:19:30 +03:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |      * Return the found configuration blueprints | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |      * @return array | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |     public static function configurations() | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |         $configurations = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** @var UniformResourceIterator $iterator */ | 
					
						
							|  |  |  |         $iterator = Grav::instance()['locator']->getIterator('blueprints://config'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         foreach ($iterator as $file) { | 
					
						
							|  |  |  |             if ($file->isDir() || !preg_match('/^[^.].*.yaml$/', $file->getFilename())) { | 
					
						
							|  |  |  |                 continue; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2018-10-04 06:43:36 -04:00
										 |  |  |             $configurations[] = $file->getBasename('.yaml'); | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $configurations; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-22 21:34:21 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Return the tools found | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public static function tools() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $tools = []; | 
					
						
							| 
									
										
										
										
											2018-05-10 10:14:18 +03:00
										 |  |  |         Grav::instance()->fireEvent('onAdminTools', new Event(['tools' => &$tools])); | 
					
						
							| 
									
										
										
										
											2017-02-26 19:36:01 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-22 21:34:21 +01:00
										 |  |  |         return $tools; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-08 13:53:11 -07:00
										 |  |  |     public static function toolsPermissions() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $tools = static::tools(); | 
					
						
							|  |  |  |         $perms = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         foreach ($tools as $tool) { | 
					
						
							|  |  |  |             $perms = array_merge($perms, $tool[0]); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return array_unique($perms); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Return the languages available in the site | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public static function siteLanguages() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $languages = []; | 
					
						
							| 
									
										
										
										
											2018-05-10 10:14:18 +03:00
										 |  |  |         $lang_data = (array) Grav::instance()['config']->get('system.languages.supported', []); | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         foreach ($lang_data as $index => $lang) { | 
					
						
							|  |  |  |             $languages[$lang] = LanguageCodes::getNativeName($lang); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $languages; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Static helper method to return the admin form nonce | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return string | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public static function getNonce() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $action = 'admin-form'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return Utils::getNonce($action); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Static helper method to return the last used page name | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return string | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public static function getLastPageName() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return Grav::instance()['session']->lastPageName ?: 'default'; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Static helper method to return the last used page route | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return string | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public static function getLastPageRoute() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-02-04 11:22:27 +02:00
										 |  |  |         /** @var Session $session */ | 
					
						
							|  |  |  |         $session = Grav::instance()['session']; | 
					
						
							|  |  |  |         $route = $session->lastPageRoute; | 
					
						
							|  |  |  |         if ($route) { | 
					
						
							|  |  |  |             return $route; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** @var Admin $admin */ | 
					
						
							|  |  |  |         $admin = Grav::instance()['admin']; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $admin->getCurrentRoute(); | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-27 13:19:30 +03:00
										 |  |  |     public function getAdminRoute(string $path = '', $languageCode = null): Route | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         /** @var Language $language */ | 
					
						
							|  |  |  |         $language = $this->grav['language']; | 
					
						
							| 
									
										
										
										
											2019-09-06 14:27:52 +03:00
										 |  |  |         $languageCode = $languageCode ?? $language->getActive(); | 
					
						
							| 
									
										
										
										
											2019-08-29 16:28:19 +03:00
										 |  |  |         $languagePrefix = $languageCode ? '/' . $languageCode : ''; | 
					
						
							| 
									
										
										
										
											2019-06-27 13:19:30 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-28 18:56:37 +02:00
										 |  |  |         $root = $this->grav['uri']->rootUrl(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-27 13:19:30 +03:00
										 |  |  |         $parts = [ | 
					
						
							|  |  |  |             'path' => $path, | 
					
						
							|  |  |  |             'query' => '', | 
					
						
							|  |  |  |             'query_params' => [], | 
					
						
							|  |  |  |             'grav' => [ | 
					
						
							|  |  |  |                 // TODO: Make URL to be /admin/en, not /en/admin.
 | 
					
						
							| 
									
										
										
										
											2020-01-28 18:56:37 +02:00
										 |  |  |                 'root' => preg_replace('`//+`', '/', $root . $languagePrefix . $this->base), | 
					
						
							| 
									
										
										
										
											2019-06-27 13:19:30 +03:00
										 |  |  |                 'language' => '', //$languageCode,
 | 
					
						
							|  |  |  |                 'route' => ltrim($path, '/'), | 
					
						
							|  |  |  |                 'params' => '' | 
					
						
							|  |  |  |             ], | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return RouteFactory::createFromParts($parts); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function adminUrl(string $route = '', $languageCode = null) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-07-01 20:35:53 +03:00
										 |  |  |         return $this->getAdminRoute($route, $languageCode)->toString(true); | 
					
						
							| 
									
										
										
										
											2019-06-27 13:19:30 +03:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Static helper method to return current route. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return string | 
					
						
							| 
									
										
										
										
											2019-06-29 14:57:22 +03:00
										 |  |  |      * @deprecated 1.10 Use $admin->getCurrentRoute() instead | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public static function route() | 
					
						
							| 
									
										
										
										
											2019-06-27 13:19:30 +03:00
										 |  |  |     { | 
					
						
							|  |  |  |         user_error(__CLASS__ . '::' . __FUNCTION__ . '() is deprecated since Admin 1.9.7, use $admin->getCurrentRoute() instead', E_USER_DEPRECATED); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $admin = Grav::instance()['admin']; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $admin->getCurrentRoute(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function getCurrentRoute() | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-08-23 20:45:51 +03:00
										 |  |  |         $pages = static::enablePages(); | 
					
						
							| 
									
										
										
										
											2019-08-22 19:25:59 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-27 13:19:30 +03:00
										 |  |  |         $route = '/' . ltrim($this->route, '/'); | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-05 15:46:55 +02:00
										 |  |  |         /** @var PageInterface $page */ | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |         $page         = $pages->dispatch($route); | 
					
						
							|  |  |  |         $parent_route = null; | 
					
						
							|  |  |  |         if ($page) { | 
					
						
							| 
									
										
										
										
											2019-03-05 15:46:55 +02:00
										 |  |  |             /** @var PageInterface $parent */ | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |             $parent       = $page->parent(); | 
					
						
							|  |  |  |             $parent_route = $parent->rawRoute(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $parent_route; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-27 13:19:30 +03:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Redirect to the route stored in $this->redirect | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * Route may or may not be prefixed by /en or /admin or /en/admin. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param string $redirect | 
					
						
							|  |  |  |      * @param int$redirectCode | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function redirect($redirect, $redirectCode = 303) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // No redirect, do nothing.
 | 
					
						
							|  |  |  |         if (!$redirect) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-10 22:19:39 +02:00
										 |  |  |         Admin::DEBUG && Admin::addDebugMessage("Admin redirect: {$redirectCode} {$redirect}"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-01 20:18:42 +03:00
										 |  |  |         $redirect = '/' . ltrim(preg_replace('`//+`', '/', $redirect), '/'); | 
					
						
							| 
									
										
										
										
											2019-06-27 13:19:30 +03:00
										 |  |  |         $base = $this->base; | 
					
						
							| 
									
										
										
										
											2019-07-01 20:18:42 +03:00
										 |  |  |         $root = Grav::instance()['uri']->rootUrl(); | 
					
						
							|  |  |  |         if ($root === '/') { | 
					
						
							|  |  |  |             $root = ''; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-06-27 13:19:30 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-16 21:59:56 +03:00
										 |  |  |         $pattern = '`^((' . preg_quote($root, '`') . ')?(/[^/]+)?)' . preg_quote($base, '`') . '`ui'; | 
					
						
							| 
									
										
										
										
											2019-09-11 19:49:47 +03:00
										 |  |  |         // Check if we already have an admin path: /admin, /en/admin, /root/admin or /root/en/admin.
 | 
					
						
							|  |  |  |         if (preg_match($pattern, $redirect)) { | 
					
						
							|  |  |  |             $redirect = preg_replace('|^' . preg_quote($root, '|') . '|', '', $redirect); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-27 13:19:30 +03:00
										 |  |  |             $this->grav->redirect($redirect, $redirectCode); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($this->isMultilang()) { | 
					
						
							|  |  |  |             // Check if URL does not have language prefix.
 | 
					
						
							|  |  |  |             if (!Utils::pathPrefixedByLangCode($redirect)) { | 
					
						
							|  |  |  |                 /** @var Language $language */ | 
					
						
							|  |  |  |                 $language = $this->grav['language']; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 // Prefix path with language prefix: /en
 | 
					
						
							|  |  |  |                 // TODO: Use /admin/en instead of /en/admin in the future.
 | 
					
						
							|  |  |  |                 $redirect = $language->getLanguageURLPrefix($this->grav['session']->admin_lang) . $base . $redirect; | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 // TODO: Use /admin/en instead of /en/admin in the future.
 | 
					
						
							|  |  |  |                 //$redirect = preg_replace('`^(/[^/]+)/admin`', '\\1', $redirect);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 // Check if we already have language prefixed admin path: /en/admin
 | 
					
						
							|  |  |  |                 $this->grav->redirect($redirect, $redirectCode); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             // TODO: Use /admin/en instead of /en/admin in the future.
 | 
					
						
							|  |  |  |             // Prefix path with /admin
 | 
					
						
							|  |  |  |             $redirect = $base . $redirect; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->grav->redirect($redirect, $redirectCode); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Return true if multilang is active | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool True if multilang is active | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function isMultilang() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return count($this->grav['config']->get('system.languages.supported', [])) > 1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |     public static function getTempDir() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             $tmp_dir = Grav::instance()['locator']->findResource('tmp://', true, true); | 
					
						
							|  |  |  |         } catch (\Exception $e) { | 
					
						
							|  |  |  |             $tmp_dir = Grav::instance()['locator']->findResource('cache://', true, true) . '/tmp'; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-02-26 19:36:01 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |         return $tmp_dir; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-31 17:11:46 -06:00
										 |  |  |     public static function getPageMedia() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $files = []; | 
					
						
							|  |  |  |         $grav = Grav::instance(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-23 20:45:51 +03:00
										 |  |  |         $pages = static::enablePages(); | 
					
						
							| 
									
										
										
										
											2019-08-22 19:25:59 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-31 17:11:46 -06:00
										 |  |  |         $route = '/' . ltrim($grav['admin']->route, '/'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-05 15:46:55 +02:00
										 |  |  |         /** @var PageInterface $page */ | 
					
						
							| 
									
										
										
										
											2017-05-31 17:11:46 -06:00
										 |  |  |         $page         = $pages->dispatch($route); | 
					
						
							|  |  |  |         $parent_route = null; | 
					
						
							|  |  |  |         if ($page) { | 
					
						
							|  |  |  |             $media = $page->media()->all(); | 
					
						
							|  |  |  |             $files = array_keys($media); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return $files; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Get current session. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Session | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function session() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->session; | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Fetch and delete messages from the session queue. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param string $type | 
					
						
							| 
									
										
										
										
											2015-08-11 18:30:27 -06:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2014-09-09 06:10:31 +03:00
										 |  |  |      * @return array | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function messages($type = null) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2014-10-01 22:28:16 +03:00
										 |  |  |         /** @var Message $messages */ | 
					
						
							| 
									
										
										
										
											2014-09-09 06:10:31 +03:00
										 |  |  |         $messages = $this->grav['messages']; | 
					
						
							| 
									
										
										
										
											2015-08-11 18:30:27 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-09 06:10:31 +03:00
										 |  |  |         return $messages->fetch($type); | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Authenticate user. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2018-05-10 20:04:14 +03:00
										 |  |  |      * @param  array $credentials User credentials. | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-05-10 20:04:14 +03:00
										 |  |  |     public function authenticate($credentials, $post) | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-05-10 20:04:14 +03:00
										 |  |  |         /** @var Login $login */ | 
					
						
							| 
									
										
										
										
											2017-08-28 14:24:48 -06:00
										 |  |  |         $login = $this->grav['login']; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-10 20:04:14 +03:00
										 |  |  |         // Remove login nonce from the form.
 | 
					
						
							|  |  |  |         $credentials = array_diff_key($credentials, ['admin-nonce' => true]); | 
					
						
							|  |  |  |         $twofa = $this->grav['config']->get('plugins.admin.twofa_enabled', false); | 
					
						
							| 
									
										
										
										
											2017-08-28 14:24:48 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-10 20:04:14 +03:00
										 |  |  |         $rateLimiter = $login->getRateLimiter('login_attempts'); | 
					
						
							| 
									
										
										
										
											2019-02-13 11:08:33 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-05 08:20:38 +02:00
										 |  |  |         $userKey = (string)($credentials['username'] ?? ''); | 
					
						
							| 
									
										
										
										
											2018-05-10 20:04:14 +03:00
										 |  |  |         $ipKey = Uri::ip(); | 
					
						
							| 
									
										
										
										
											2018-12-05 08:20:38 +02:00
										 |  |  |         $redirect = $post['redirect'] ?? $this->base . $this->route; | 
					
						
							| 
									
										
										
										
											2017-08-28 14:24:48 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-15 01:55:56 +01:00
										 |  |  |         // Pseudonymization of the IP
 | 
					
						
							|  |  |  |         $ipKey = sha1($ipKey . $this->grav['config']->get('security.salt')); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-10 20:04:14 +03:00
										 |  |  |         // Check if the current IP has been used in failed login attempts.
 | 
					
						
							|  |  |  |         $attempts = count($rateLimiter->getAttempts($ipKey, 'ip')); | 
					
						
							| 
									
										
										
										
											2015-08-20 11:29:37 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-10 20:04:14 +03:00
										 |  |  |         $rateLimiter->registerRateLimitedAction($ipKey, 'ip')->registerRateLimitedAction($userKey); | 
					
						
							| 
									
										
										
										
											2015-08-20 11:29:37 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-10 20:04:14 +03:00
										 |  |  |         // Check rate limit for both IP and user, but allow each IP a single try even if user is already rate limited.
 | 
					
						
							|  |  |  |         if ($rateLimiter->isRateLimited($ipKey, 'ip') || ($attempts && $rateLimiter->isRateLimited($userKey))) { | 
					
						
							| 
									
										
										
										
											2020-02-10 22:19:39 +02:00
										 |  |  |             Admin::DEBUG && Admin::addDebugMessage('Admin login: rate limit, redirecting', $credentials); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-05 08:20:38 +02:00
										 |  |  |             $this->setMessage(static::translate(['PLUGIN_LOGIN.TOO_MANY_LOGIN_ATTEMPTS', $rateLimiter->getInterval()]), 'error'); | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-10 20:04:14 +03:00
										 |  |  |             $this->grav->redirect('/'); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-02-13 11:08:33 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-10 22:19:39 +02:00
										 |  |  |         Admin::DEBUG && Admin::addDebugMessage('Admin login', $credentials); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-10 20:04:14 +03:00
										 |  |  |         // Fire Login process.
 | 
					
						
							|  |  |  |         $event = $login->login( | 
					
						
							|  |  |  |             $credentials, | 
					
						
							|  |  |  |             ['admin' => true, 'twofa' => $twofa], | 
					
						
							|  |  |  |             ['authorize' => 'admin.login', 'return_event' => true] | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |         $user = $event->getUser(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-10 22:19:39 +02:00
										 |  |  |         Admin::DEBUG && Admin::addDebugMessage('Admin login: user', $user); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-10 20:04:14 +03:00
										 |  |  |         if ($user->authenticated) { | 
					
						
							|  |  |  |             $rateLimiter->resetRateLimit($ipKey, 'ip')->resetRateLimit($userKey); | 
					
						
							|  |  |  |             if ($user->authorized) { | 
					
						
							|  |  |  |                 $event->defMessage('PLUGIN_ADMIN.LOGIN_LOGGED_IN', 'info'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-05 08:20:38 +02:00
										 |  |  |                 $event->defRedirect($post['redirect'] ?? $redirect); | 
					
						
							| 
									
										
										
										
											2018-05-10 20:04:14 +03:00
										 |  |  |             } else { | 
					
						
							|  |  |  |                 $this->session->redirect = $redirect; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             if ($user->authorized) { | 
					
						
							|  |  |  |                 $event->defMessage('PLUGIN_LOGIN.ACCESS_DENIED', 'error'); | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 $event->defMessage('PLUGIN_LOGIN.LOGIN_FAILED', 'error'); | 
					
						
							| 
									
										
										
										
											2016-11-08 16:42:57 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-23 09:27:26 +03:00
										 |  |  |         $event->defRedirect($redirect); | 
					
						
							| 
									
										
										
										
											2015-07-28 11:11:43 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-10 20:04:14 +03:00
										 |  |  |         $message = $event->getMessage(); | 
					
						
							|  |  |  |         if ($message) { | 
					
						
							| 
									
										
										
										
											2018-12-05 08:20:38 +02:00
										 |  |  |             $this->setMessage(static::translate($message), $event->getMessageType()); | 
					
						
							| 
									
										
										
										
											2018-05-10 20:04:14 +03:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $redirect = $event->getRedirect(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->grav->redirect($redirect, $event->getRedirectCode()); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-08-25 16:20:57 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-10 20:04:14 +03:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Check Two-Factor Authentication. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function twoFa($data, $post) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         /** @var Login $login */ | 
					
						
							|  |  |  |         $login = $this->grav['login']; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** @var TwoFactorAuth $twoFa */ | 
					
						
							|  |  |  |         $twoFa = $login->twoFactorAuth(); | 
					
						
							|  |  |  |         $user = $this->grav['user']; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-05 08:20:38 +02:00
										 |  |  |         $code = $data['2fa_code'] ?? null; | 
					
						
							| 
									
										
										
										
											2018-05-10 20:04:14 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-05 08:20:38 +02:00
										 |  |  |         $secret = $user->twofa_secret ?? null; | 
					
						
							| 
									
										
										
										
											2018-05-10 20:04:14 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (!$code || !$secret || !$twoFa->verifyCode($secret, $code)) { | 
					
						
							|  |  |  |             $login->logout(['admin' => true]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $this->grav['session']->setFlashCookieObject(Admin::TMP_COOKIE_NAME, ['message' => $this->translate('PLUGIN_ADMIN.2FA_FAILED'), 'status' => 'error']); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $this->grav->redirect($this->uri->route(), 303); | 
					
						
							| 
									
										
										
										
											2017-08-26 18:25:34 -06:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-08-25 17:58:29 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-10 20:04:14 +03:00
										 |  |  |         $this->setMessage($this->translate('PLUGIN_ADMIN.LOGIN_LOGGED_IN'), 'info'); | 
					
						
							| 
									
										
										
										
											2017-08-25 17:58:29 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-10 20:04:14 +03:00
										 |  |  |         $user->authorized = true; | 
					
						
							| 
									
										
										
										
											2016-05-03 13:36:06 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-10 20:04:14 +03:00
										 |  |  |         $this->grav->redirect($post['redirect']); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-11-08 16:42:57 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-10 20:04:14 +03:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Logout from admin. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-12-05 08:20:38 +02:00
										 |  |  |     public function logout($data, $post) | 
					
						
							| 
									
										
										
										
											2018-05-10 20:04:14 +03:00
										 |  |  |     { | 
					
						
							|  |  |  |         /** @var Login $login */ | 
					
						
							|  |  |  |         $login = $this->grav['login']; | 
					
						
							| 
									
										
										
										
											2016-11-08 16:42:57 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-10 20:04:14 +03:00
										 |  |  |         $event = $login->logout(['admin' => true], ['return_event' => true]); | 
					
						
							| 
									
										
										
										
											2017-02-26 19:36:01 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-10 20:04:14 +03:00
										 |  |  |         $event->defMessage('PLUGIN_ADMIN.LOGGED_OUT', 'info'); | 
					
						
							|  |  |  |         $message = $event->getMessage(); | 
					
						
							|  |  |  |         if ($message) { | 
					
						
							|  |  |  |             $this->grav['session']->setFlashCookieObject(Admin::TMP_COOKIE_NAME, ['message' => $this->translate($message), 'status' => $event->getMessageType()]); | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-10 20:04:14 +03:00
										 |  |  |         $this->grav->redirect($this->base); | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-10 10:14:18 +03:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public static function doAnyUsersExist() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-05-02 14:13:52 +03:00
										 |  |  |         $accounts = Grav::instance()['accounts'] ?? null; | 
					
						
							|  |  |  |         if ($accounts instanceof \Countable) { | 
					
						
							|  |  |  |             return $accounts->count() > 0; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // TODO: remove old way to check for existence of a user account (Grav < v1.6.9)
 | 
					
						
							| 
									
										
										
										
											2018-05-10 10:14:18 +03:00
										 |  |  |         $account_dir = $file_path = Grav::instance()['locator']->findResource('account://'); | 
					
						
							|  |  |  |         $user_check = glob($account_dir . '/*.yaml'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-02 14:13:52 +03:00
										 |  |  |         return $user_check; | 
					
						
							| 
									
										
										
										
											2018-05-10 10:14:18 +03:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Add message into the session queue. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param string $msg | 
					
						
							|  |  |  |      * @param string $type | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function setMessage($msg, $type = 'info') | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         /** @var Message $messages */ | 
					
						
							|  |  |  |         $messages = $this->grav['messages']; | 
					
						
							|  |  |  |         $messages->add($msg, $type); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-16 10:59:22 -06:00
										 |  |  |     public function addTempMessage($msg, $type) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->temp_messages[] = ['message' => $msg, 'scope' => $type]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function getTempMessages() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->temp_messages; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Translate a string to the user-defined language | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param array|mixed $args | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param mixed       $languages | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return string | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2017-02-22 21:34:21 +01:00
										 |  |  |     public static function translate($args, $languages = null) | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2017-02-22 21:34:21 +01:00
										 |  |  |         $grav = Grav::instance(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |         if (is_array($args)) { | 
					
						
							|  |  |  |             $lookup = array_shift($args); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             $lookup = $args; | 
					
						
							|  |  |  |             $args   = []; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!$languages) { | 
					
						
							| 
									
										
										
										
											2018-03-01 16:52:06 -06:00
										 |  |  |             if ($grav['config']->get('system.languages.translations_fallback', true)) { | 
					
						
							|  |  |  |                 $languages = $grav['language']->getFallbackLanguages(); | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 $languages = (array)$grav['language']->getDefault(); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2019-09-11 18:05:45 +03:00
										 |  |  |             $languages = $grav['user']->authenticated ? [$grav['user']->language] : $languages; | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |         } else { | 
					
						
							|  |  |  |             $languages = (array)$languages; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         foreach ((array)$languages as $lang) { | 
					
						
							| 
									
										
										
										
											2019-01-25 12:25:22 -07:00
										 |  |  |             $translation = $grav['language']->getTranslation($lang, $lookup, true); | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if (!$translation) { | 
					
						
							| 
									
										
										
										
											2017-02-22 21:34:21 +01:00
										 |  |  |                 $language    = $grav['language']->getDefault() ?: 'en'; | 
					
						
							| 
									
										
										
										
											2019-01-25 12:25:22 -07:00
										 |  |  |                 $translation = $grav['language']->getTranslation($language, $lookup, true); | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (!$translation) { | 
					
						
							|  |  |  |                 $language    = 'en'; | 
					
						
							| 
									
										
										
										
											2019-01-25 12:25:22 -07:00
										 |  |  |                 $translation = $grav['language']->getTranslation($language, $lookup, true); | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if ($translation) { | 
					
						
							|  |  |  |                 if (count($args) >= 1) { | 
					
						
							|  |  |  |                     return vsprintf($translation, $args); | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2018-05-10 10:14:18 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 return $translation; | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $lookup; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Checks user authorisation to the action. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2018-05-10 10:14:18 +03:00
										 |  |  |      * @param  string|string[] $action | 
					
						
							| 
									
										
										
										
											2015-08-11 18:30:27 -06:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2015-09-11 15:00:03 +02:00
										 |  |  |     public function authorize($action = 'admin.login') | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-08-11 18:30:27 -06:00
										 |  |  |         $action = (array)$action; | 
					
						
							| 
									
										
										
										
											2015-05-11 16:35:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-13 11:49:18 +02:00
										 |  |  |         $user = $this->user; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-11 16:35:23 +02:00
										 |  |  |         foreach ($action as $a) { | 
					
						
							| 
									
										
										
										
											2019-11-13 11:49:18 +02:00
										 |  |  |             // Ignore 'admin.super' if it's not the only value to be checked.
 | 
					
						
							|  |  |  |             if ($a === 'admin.super' && count($action) > 1 && $user instanceof FlexObjectInterface) { | 
					
						
							|  |  |  |                 continue; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if ($user->authorize($a)) { | 
					
						
							| 
									
										
										
										
											2015-05-11 16:35:23 +02:00
										 |  |  |                 return true; | 
					
						
							| 
									
										
										
										
											2015-08-11 18:30:27 -06:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2015-05-11 16:35:23 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return false; | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Gets configuration data. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param string $type | 
					
						
							| 
									
										
										
										
											2015-08-11 18:30:27 -06:00
										 |  |  |      * @param array  $post | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |      * @return mixed | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |      * @throws \RuntimeException | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-03-16 11:09:48 +02:00
										 |  |  |     public function data($type, array $post = []) | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2014-10-10 13:26:39 +03:00
										 |  |  |         static $data = []; | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (isset($data[$type])) { | 
					
						
							|  |  |  |             return $data[$type]; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-10 13:26:39 +03:00
										 |  |  |         if (!$post) { | 
					
						
							| 
									
										
										
										
											2018-09-17 09:54:27 +03:00
										 |  |  |             $post = $this->grav['uri']->post(); | 
					
						
							| 
									
										
										
										
											2018-12-05 08:20:38 +02:00
										 |  |  |             $post = $post['data'] ?? []; | 
					
						
							| 
									
										
										
										
											2014-10-10 13:26:39 +03:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-04 16:51:41 +02:00
										 |  |  |         // Check to see if a data type is plugin-provided, before looking into core ones
 | 
					
						
							|  |  |  |         $event = $this->grav->fireEvent('onAdminData', new Event(['type' => &$type])); | 
					
						
							| 
									
										
										
										
											2018-09-07 16:23:56 -06:00
										 |  |  |         if ($event) { | 
					
						
							|  |  |  |             if (isset($event['data_type'])) { | 
					
						
							| 
									
										
										
										
											2018-09-07 16:13:07 -06:00
										 |  |  |                 return $event['data_type']; | 
					
						
							| 
									
										
										
										
											2018-12-05 08:20:38 +02:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (is_string($event['type'])) { | 
					
						
							| 
									
										
										
										
											2018-09-07 16:23:56 -06:00
										 |  |  |                 $type = $event['type']; | 
					
						
							| 
									
										
										
										
											2018-09-07 16:13:07 -06:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2016-10-04 16:51:41 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-26 14:49:45 -06:00
										 |  |  |         /** @var UniformResourceLocator $locator */ | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |         $locator  = $this->grav['locator']; | 
					
						
							| 
									
										
										
										
											2016-05-26 14:49:45 -06:00
										 |  |  |         $filename = $locator->findResource("config://{$type}.yaml", true, true); | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |         $file     = CompiledYamlFile::instance($filename); | 
					
						
							| 
									
										
										
										
											2016-05-26 14:49:45 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (preg_match('|plugins/|', $type)) { | 
					
						
							|  |  |  |             /** @var Plugins $plugins */ | 
					
						
							|  |  |  |             $plugins = $this->grav['plugins']; | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |             $obj     = $plugins->get(preg_replace('|plugins/|', '', $type)); | 
					
						
							| 
									
										
										
										
											2016-05-26 14:49:45 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-07 18:55:52 +02:00
										 |  |  |             if (!$obj) { | 
					
						
							|  |  |  |                 return []; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2016-05-26 14:49:45 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |             $obj->merge($post); | 
					
						
							|  |  |  |             $obj->file($file); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $data[$type] = $obj; | 
					
						
							|  |  |  |         } elseif (preg_match('|themes/|', $type)) { | 
					
						
							|  |  |  |             /** @var Themes $themes */ | 
					
						
							|  |  |  |             $themes = $this->grav['themes']; | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |             $obj    = $themes->get(preg_replace('|themes/|', '', $type)); | 
					
						
							| 
									
										
										
										
											2016-05-26 14:49:45 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-07 18:55:52 +02:00
										 |  |  |             if (!$obj) { | 
					
						
							|  |  |  |                 return []; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2016-05-26 14:49:45 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |             $obj->merge($post); | 
					
						
							|  |  |  |             $obj->file($file); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $data[$type] = $obj; | 
					
						
							| 
									
										
										
										
											2019-03-28 11:14:55 +02:00
										 |  |  |         } elseif (preg_match('|users?/|', $type)) { | 
					
						
							| 
									
										
										
										
											2019-03-20 12:52:16 +02:00
										 |  |  |             /** @var UserCollectionInterface $users */ | 
					
						
							| 
									
										
										
										
											2019-03-27 14:52:23 +02:00
										 |  |  |             $users = $this->grav['accounts']; | 
					
						
							| 
									
										
										
										
											2019-03-20 12:52:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-28 11:14:55 +02:00
										 |  |  |             $obj = $users->load(preg_replace('|users?/|', '', $type)); | 
					
						
							| 
									
										
										
										
											2019-02-07 09:58:45 +02:00
										 |  |  |             $obj->update($this->cleanUserPost($post)); | 
					
						
							| 
									
										
										
										
											2016-05-26 14:49:45 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |             $data[$type] = $obj; | 
					
						
							|  |  |  |         } elseif (preg_match('|config/|', $type)) { | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |             $type       = preg_replace('|config/|', '', $type); | 
					
						
							| 
									
										
										
										
											2016-05-26 14:49:45 -06:00
										 |  |  |             $blueprints = $this->blueprints("config/{$type}"); | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |             $config     = $this->grav['config']; | 
					
						
							|  |  |  |             $obj        = new Data\Data($config->get($type, []), $blueprints); | 
					
						
							| 
									
										
										
										
											2016-05-26 14:49:45 -06:00
										 |  |  |             $obj->merge($post); | 
					
						
							| 
									
										
										
										
											2016-06-02 20:21:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-26 14:49:45 -06:00
										 |  |  |             // FIXME: We shouldn't allow user to change configuration files in system folder!
 | 
					
						
							|  |  |  |             $filename = $this->grav['locator']->findResource("config://{$type}.yaml") | 
					
						
							|  |  |  |                 ?: $this->grav['locator']->findResource("config://{$type}.yaml", true, true); | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |             $file     = CompiledYamlFile::instance($filename); | 
					
						
							| 
									
										
										
										
											2016-05-26 14:49:45 -06:00
										 |  |  |             $obj->file($file); | 
					
						
							| 
									
										
										
										
											2017-05-26 14:30:37 +02:00
										 |  |  |             $data[$type] = $obj; | 
					
						
							|  |  |  |         } elseif (preg_match('|media-manager/|', $type)) { | 
					
						
							|  |  |  |             $filename = base64_decode(preg_replace('|media-manager/|', '', $type)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $file = File::instance($filename); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-23 20:45:51 +03:00
										 |  |  |             $pages = static::enablePages(); | 
					
						
							| 
									
										
										
										
											2019-08-22 19:25:59 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-20 12:52:16 +02:00
										 |  |  |             $obj = new \stdClass(); | 
					
						
							| 
									
										
										
										
											2017-05-26 14:30:37 +02:00
										 |  |  |             $obj->title = $file->basename(); | 
					
						
							|  |  |  |             $obj->path = $file->filename(); | 
					
						
							|  |  |  |             $obj->file = $file; | 
					
						
							| 
									
										
										
										
											2019-08-22 19:25:59 +03:00
										 |  |  |             $obj->page = $pages->get(dirname($obj->path)); | 
					
						
							| 
									
										
										
										
											2017-05-26 14:30:37 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-04 06:43:36 -04:00
										 |  |  |             $fileInfo = pathinfo($obj->title); | 
					
						
							|  |  |  |             $filename = str_replace(['@3x', '@2x'], '', $fileInfo['filename']); | 
					
						
							|  |  |  |             if (isset($fileInfo['extension'])) { | 
					
						
							|  |  |  |                 $filename .= '.' . $fileInfo['extension']; | 
					
						
							| 
									
										
										
										
											2017-05-26 14:30:37 +02:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if ($obj->page && isset($obj->page->media()[$filename])) { | 
					
						
							| 
									
										
										
										
											2017-06-06 17:44:30 +02:00
										 |  |  |                 $obj->metadata = new Data\Data($obj->page->media()[$filename]->metadata()); | 
					
						
							| 
									
										
										
										
											2017-05-26 14:30:37 +02:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-26 14:49:45 -06:00
										 |  |  |             $data[$type] = $obj; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             throw new \RuntimeException("Data type '{$type}' doesn't exist!"); | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $data[$type]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-13 15:46:26 -07:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Clean user form post and remove extra stuff that may be passed along | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2019-03-20 12:52:16 +02:00
										 |  |  |      * @param array $post | 
					
						
							| 
									
										
										
										
											2018-12-13 15:46:26 -07:00
										 |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2019-03-28 11:14:55 +02:00
										 |  |  |     public function cleanUserPost($post) | 
					
						
							| 
									
										
										
										
											2018-12-13 15:46:26 -07:00
										 |  |  |     { | 
					
						
							|  |  |  |         // Clean fields for all users
 | 
					
						
							|  |  |  |         unset($post['hashed_password']); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Clean field for users who shouldn't be able to modify these fields
 | 
					
						
							|  |  |  |         if (!$this->authorize(['admin.user', 'admin.super'])) { | 
					
						
							| 
									
										
										
										
											2019-03-28 11:14:55 +02:00
										 |  |  |             unset($post['access'], $post['state']); | 
					
						
							| 
									
										
										
										
											2018-12-13 15:46:26 -07:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $post; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-27 09:04:17 -06:00
										 |  |  |     protected function hasErrorMessage() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $msgs = $this->grav['messages']->all(); | 
					
						
							|  |  |  |         foreach ($msgs as $msg) { | 
					
						
							|  |  |  |             if (isset($msg['scope']) && $msg['scope'] === 'error') { | 
					
						
							|  |  |  |                 return true; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-30 11:22:24 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |      * Returns blueprints for the given type. | 
					
						
							| 
									
										
										
										
											2015-07-30 11:22:24 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |      * @param string $type | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Data\Blueprint | 
					
						
							| 
									
										
										
										
											2015-07-30 11:22:24 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |     public function blueprints($type) | 
					
						
							| 
									
										
										
										
											2015-04-04 14:49:40 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |         if ($this->blueprints === null) { | 
					
						
							|  |  |  |             $this->blueprints = new Data\Blueprints('blueprints://'); | 
					
						
							| 
									
										
										
										
											2015-04-04 14:49:40 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |         return $this->blueprints->get($type); | 
					
						
							| 
									
										
										
										
											2015-04-04 14:49:40 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Converts dot notation to array notation. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param  string $name | 
					
						
							| 
									
										
										
										
											2015-08-11 18:30:27 -06:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |      * @return string | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function field($name) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $path = explode('.', $name); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return array_shift($path) . ($path ? '[' . implode('][', $path) . ']' : ''); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-05 22:27:30 -06:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Get all routes. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-01-28 14:38:01 +01:00
										 |  |  |      * @param bool $unique | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2014-09-05 22:27:30 -06:00
										 |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2015-08-20 14:55:24 -06:00
										 |  |  |     public function routes($unique = false) | 
					
						
							| 
									
										
										
										
											2014-09-05 22:27:30 -06:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-08-23 20:45:51 +03:00
										 |  |  |         $pages = static::enablePages(); | 
					
						
							| 
									
										
										
										
											2015-08-11 18:30:27 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-20 14:55:24 -06:00
										 |  |  |         if ($unique) { | 
					
						
							|  |  |  |             $routes = array_unique($pages->routes()); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             $routes = $pages->routes(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-07-07 18:55:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-20 14:55:24 -06:00
										 |  |  |         return $routes; | 
					
						
							| 
									
										
										
										
											2014-09-05 22:27:30 -06:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-10-20 19:32:33 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-02 11:47:26 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Count the pages | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2019-03-20 12:52:16 +02:00
										 |  |  |      * @return int | 
					
						
							| 
									
										
										
										
											2015-10-02 11:47:26 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-01-28 14:03:53 +01:00
										 |  |  |     public function pagesCount() | 
					
						
							| 
									
										
										
										
											2015-10-02 11:47:26 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-01-28 14:03:53 +01:00
										 |  |  |         if (!$this->pages_count) { | 
					
						
							| 
									
										
										
										
											2019-08-27 10:57:33 +03:00
										 |  |  |             $pages = static::enablePages(); | 
					
						
							|  |  |  |             $this->pages_count = count($pages->all()); | 
					
						
							| 
									
										
										
										
											2016-01-28 14:03:53 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $this->pages_count; | 
					
						
							| 
									
										
										
										
											2015-10-02 11:47:26 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2014-09-05 22:27:30 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-08 11:13:52 -06:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2015-10-21 18:41:49 +02:00
										 |  |  |      * Get all template types | 
					
						
							| 
									
										
										
										
											2015-09-08 11:31:39 -06:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function types() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return Pages::types(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2015-10-21 18:41:49 +02:00
										 |  |  |      * Get all modular template types | 
					
						
							| 
									
										
										
										
											2015-09-08 11:13:52 -06:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function modularTypes() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return Pages::modularTypes(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-21 18:41:49 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Get all access levels | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function accessLevels() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-08-23 20:45:51 +03:00
										 |  |  |         $pages = static::enablePages(); | 
					
						
							| 
									
										
										
										
											2019-08-22 19:25:59 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (method_exists($pages, 'accessLevels')) { | 
					
						
							|  |  |  |             return $pages->accessLevels(); | 
					
						
							| 
									
										
										
										
											2015-11-30 19:00:12 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-05-10 10:14:18 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return []; | 
					
						
							| 
									
										
										
										
											2015-10-21 18:41:49 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 10:13:18 -07:00
										 |  |  |     public function license($package_slug) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return Licenses::get($package_slug); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-01 19:52:24 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-03-07 11:31:18 +01:00
										 |  |  |      * Generate an array of dependencies for a package, used to generate a list of | 
					
						
							|  |  |  |      * packages that can be removed when removing a package. | 
					
						
							| 
									
										
										
										
											2016-03-01 19:52:24 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-07-07 18:55:52 +02:00
										 |  |  |      * @param string $slug The package slug | 
					
						
							| 
									
										
										
										
											2016-03-01 19:52:24 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return array|bool | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-03-05 18:46:41 +01:00
										 |  |  |     public function dependenciesThatCanBeRemovedWhenRemoving($slug) | 
					
						
							| 
									
										
										
										
											2016-03-01 19:52:24 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         $gpm = $this->gpm(); | 
					
						
							|  |  |  |         if (!$gpm) { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-05 18:46:41 +01:00
										 |  |  |         $dependencies = []; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-04 19:20:57 +01:00
										 |  |  |         $package = $this->getPackageFromGPM($slug); | 
					
						
							| 
									
										
										
										
											2016-03-01 19:52:24 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if ($package) { | 
					
						
							|  |  |  |             if ($package->dependencies) { | 
					
						
							|  |  |  |                 foreach ($package->dependencies as $dependency) { | 
					
						
							| 
									
										
										
										
											2018-02-18 14:13:09 -07:00
										 |  |  | //                    if (count($gpm->getPackagesThatDependOnPackage($dependency)) > 1) {
 | 
					
						
							|  |  |  | //                        continue;
 | 
					
						
							|  |  |  | //                    }
 | 
					
						
							|  |  |  |                     if (isset($dependency['name'])) { | 
					
						
							|  |  |  |                         $dependency = $dependency['name']; | 
					
						
							| 
									
										
										
										
											2016-03-01 19:52:24 +01:00
										 |  |  |                     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-10 10:14:18 +03:00
										 |  |  |                     if (!in_array($dependency, $dependencies, true)) { | 
					
						
							| 
									
										
										
										
											2018-02-18 14:13:09 -07:00
										 |  |  |                         if (!in_array($dependency, ['admin', 'form', 'login', 'email', 'php'])) { | 
					
						
							| 
									
										
										
										
											2016-05-27 14:41:33 +02:00
										 |  |  |                             $dependencies[] = $dependency; | 
					
						
							|  |  |  |                         } | 
					
						
							| 
									
										
										
										
											2016-03-04 19:20:48 +01:00
										 |  |  |                     } | 
					
						
							| 
									
										
										
										
											2016-03-01 19:52:24 +01:00
										 |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $dependencies; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Get the GPM instance | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return GPM The GPM instance | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function gpm() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (!$this->gpm) { | 
					
						
							|  |  |  |             try { | 
					
						
							|  |  |  |                 $this->gpm = new GPM(); | 
					
						
							|  |  |  |             } catch (\Exception $e) { | 
					
						
							| 
									
										
										
										
											2019-05-16 11:54:18 +03:00
										 |  |  |                 $this->setMessage($e->getMessage(), 'error'); | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $this->gpm; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function getPackageFromGPM($package_slug) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $package = $this->plugins(true)[$package_slug]; | 
					
						
							|  |  |  |         if (!$package) { | 
					
						
							|  |  |  |             $package = $this->themes(true)[$package_slug]; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $package; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Get all plugins. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param bool $local | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return mixed | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function plugins($local = true) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $gpm = $this->gpm(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!$gpm) { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-22 15:50:37 +01:00
										 |  |  |         if ($local) { | 
					
						
							|  |  |  |             return $gpm->getInstalledPlugins(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-05-10 10:14:18 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $plugins = $gpm->getRepositoryPlugins(); | 
					
						
							|  |  |  |         if ($plugins) { | 
					
						
							|  |  |  |             return $plugins->filter(function ($package, $slug) use ($gpm) { | 
					
						
							|  |  |  |                 return !$gpm->isPluginInstalled($slug); | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return []; | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Get all themes. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param bool $local | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return mixed | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function themes($local = true) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $gpm = $this->gpm(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!$gpm) { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-22 15:50:37 +01:00
										 |  |  |         if ($local) { | 
					
						
							|  |  |  |             return $gpm->getInstalledThemes(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-05-10 10:14:18 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $themes = $gpm->getRepositoryThemes(); | 
					
						
							|  |  |  |         if ($themes) { | 
					
						
							|  |  |  |             return $themes->filter(function ($package, $slug) use ($gpm) { | 
					
						
							|  |  |  |                 return !$gpm->isThemeInstalled($slug); | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return []; | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-17 15:57:10 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Get list of packages that depend on the passed package slug | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-07-07 18:55:52 +02:00
										 |  |  |      * @param string $slug The package slug | 
					
						
							| 
									
										
										
										
											2016-03-17 15:57:10 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return array|bool | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getPackagesThatDependOnPackage($slug) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $gpm = $this->gpm(); | 
					
						
							|  |  |  |         if (!$gpm) { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $gpm->getPackagesThatDependOnPackage($slug); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-15 19:36:00 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Check the passed packages list can be updated | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2019-03-20 12:52:16 +02:00
										 |  |  |      * @param array $packages | 
					
						
							| 
									
										
										
										
											2016-03-15 19:36:00 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @throws \Exception | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function checkPackagesCanBeInstalled($packages) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $gpm = $this->gpm(); | 
					
						
							|  |  |  |         if (!$gpm) { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->gpm->checkPackagesCanBeInstalled($packages); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-07 11:31:18 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-03-09 19:40:23 +01:00
										 |  |  |      * Get an array of dependencies needed to be installed or updated for a list of packages | 
					
						
							| 
									
										
										
										
											2016-03-07 11:31:18 +01:00
										 |  |  |      * to be installed. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-03-09 19:40:23 +01:00
										 |  |  |      * @param array $packages The packages slugs | 
					
						
							| 
									
										
										
										
											2016-03-07 11:31:18 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return array|bool | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-03-09 19:40:23 +01:00
										 |  |  |     public function getDependenciesNeededToInstall($packages) | 
					
						
							| 
									
										
										
										
											2016-03-07 11:31:18 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         $gpm = $this->gpm(); | 
					
						
							|  |  |  |         if (!$gpm) { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-10 10:14:18 +03:00
										 |  |  |         return $this->gpm->getDependencies($packages); | 
					
						
							| 
									
										
										
										
											2016-03-07 11:31:18 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-05 22:27:30 -06:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Used by the Dashboard in the admin to display the X latest pages | 
					
						
							|  |  |  |      * that have been modified | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param  integer $count number of pages to pull back | 
					
						
							| 
									
										
										
										
											2015-08-11 18:30:27 -06:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-01-21 09:46:38 +02:00
										 |  |  |      * @return array|null | 
					
						
							| 
									
										
										
										
											2014-09-05 22:27:30 -06:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2014-10-02 12:14:13 -07:00
										 |  |  |     public function latestPages($count = 10) | 
					
						
							| 
									
										
										
										
											2014-09-05 22:27:30 -06:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-08-27 12:36:05 +03:00
										 |  |  |         /** @var Flex $flex */ | 
					
						
							| 
									
										
										
										
											2019-08-30 09:40:14 +03:00
										 |  |  |         $flex = $this->grav['flex_objects'] ?? null; | 
					
						
							| 
									
										
										
										
											2020-01-30 11:02:32 +02:00
										 |  |  |         $directory = $flex ? $flex->getDirectory('pages') : null; | 
					
						
							| 
									
										
										
										
											2019-08-27 12:36:05 +03:00
										 |  |  |         if ($directory) { | 
					
						
							|  |  |  |             return $directory->getIndex()->sort(['timestamp' => 'DESC'])->slice(0, $count); | 
					
						
							| 
									
										
										
										
											2019-08-24 13:20:16 +03:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-23 20:45:51 +03:00
										 |  |  |         $pages = static::enablePages(); | 
					
						
							| 
									
										
										
										
											2014-10-01 22:28:16 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-07 18:55:52 +02:00
										 |  |  |         $latest = []; | 
					
						
							| 
									
										
										
										
											2016-01-10 17:17:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-10 10:14:18 +03:00
										 |  |  |         if (null === $pages->routes()) { | 
					
						
							| 
									
										
										
										
											2016-01-21 09:46:38 +02:00
										 |  |  |             return null; | 
					
						
							| 
									
										
										
										
											2015-11-29 09:49:51 -05:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2014-09-05 22:27:30 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-01 22:28:16 +03:00
										 |  |  |         foreach ($pages->routes() as $url => $path) { | 
					
						
							| 
									
										
										
										
											2015-09-11 10:47:54 -06:00
										 |  |  |             $page = $pages->dispatch($url, true); | 
					
						
							| 
									
										
										
										
											2014-12-10 10:23:22 -07:00
										 |  |  |             if ($page && $page->routable()) { | 
					
						
							| 
									
										
										
										
											2015-08-11 18:30:27 -06:00
										 |  |  |                 $latest[$page->route()] = ['modified' => $page->modified(), 'page' => $page]; | 
					
						
							| 
									
										
										
										
											2014-12-10 10:23:22 -07:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2014-09-05 22:27:30 -06:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // sort based on modified
 | 
					
						
							| 
									
										
										
										
											2014-10-02 12:14:13 -07:00
										 |  |  |         uasort($latest, function ($a, $b) { | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |             if ($a['modified'] == $b['modified']) { | 
					
						
							|  |  |  |                 return 0; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2015-08-20 18:11:54 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |             return ($a['modified'] > $b['modified']) ? -1 : 1; | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // build new array with just pages in it
 | 
					
						
							|  |  |  |         $list = []; | 
					
						
							|  |  |  |         foreach ($latest as $item) { | 
					
						
							|  |  |  |             $list[] = $item['page']; | 
					
						
							| 
									
										
										
										
											2015-08-28 16:02:32 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-08-20 18:11:54 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |         return array_slice($list, 0, $count); | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-07-27 19:33:52 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-21 16:15:23 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |      * Get log file for fatal errors. | 
					
						
							| 
									
										
										
										
											2015-11-21 16:15:23 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return string | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |     public function logEntry() | 
					
						
							| 
									
										
										
										
											2015-11-21 16:15:23 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |         $file    = File::instance($this->grav['locator']->findResource("log://{$this->route}.html")); | 
					
						
							|  |  |  |         $content = $file->content(); | 
					
						
							| 
									
										
										
										
											2018-05-10 10:14:18 +03:00
										 |  |  |         $file->free(); | 
					
						
							| 
									
										
										
										
											2016-07-07 18:55:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |         return $content; | 
					
						
							| 
									
										
										
										
											2015-11-21 16:15:23 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-01 15:33:33 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |      * Search in the logs when was the latest backup made | 
					
						
							| 
									
										
										
										
											2015-10-01 15:33:33 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |      * @return array Array containing the latest backup information | 
					
						
							| 
									
										
										
										
											2015-10-01 15:33:33 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |     public function lastBackup() | 
					
						
							| 
									
										
										
										
											2015-10-01 15:33:33 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-12-05 08:20:38 +02:00
										 |  |  |         $file    = JsonFile::instance($this->grav['locator']->findResource('log://backup.log')); | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |         $content = $file->content(); | 
					
						
							|  |  |  |         if (empty($content)) { | 
					
						
							|  |  |  |             return [ | 
					
						
							|  |  |  |                 'days'        => '∞', | 
					
						
							|  |  |  |                 'chart_fill'  => 100, | 
					
						
							|  |  |  |                 'chart_empty' => 0 | 
					
						
							|  |  |  |             ]; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-10-01 15:33:33 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |         $backup = new \DateTime(); | 
					
						
							|  |  |  |         $backup->setTimestamp($content['time']); | 
					
						
							|  |  |  |         $diff = $backup->diff(new \DateTime()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $days       = $diff->days; | 
					
						
							|  |  |  |         $chart_fill = $days > 30 ? 100 : round($days / 30 * 100); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return [ | 
					
						
							|  |  |  |             'days'        => $days, | 
					
						
							|  |  |  |             'chart_fill'  => $chart_fill, | 
					
						
							|  |  |  |             'chart_empty' => 100 - $chart_fill | 
					
						
							|  |  |  |         ]; | 
					
						
							| 
									
										
										
										
											2015-10-01 15:33:33 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-30 11:16:05 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Determine if the plugin or theme info passed is from Team Grav | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param object $info Plugin or Theme info object | 
					
						
							| 
									
										
										
										
											2015-08-11 18:30:27 -06:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2015-07-30 11:16:05 +02:00
										 |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2015-07-27 19:33:52 -06:00
										 |  |  |     public function isTeamGrav($info) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-05-10 10:14:18 +03:00
										 |  |  |         return isset($info['author']['name']) && ($info['author']['name'] === 'Team Grav' || Utils::contains($info['author']['name'], 'Trilby Media')); | 
					
						
							| 
									
										
										
										
											2015-07-27 19:33:52 -06:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-08-11 18:30:27 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 10:13:18 -07:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Determine if the plugin or theme info passed is premium | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param object $info Plugin or Theme info object | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function isPremiumProduct($info) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-05-10 10:14:18 +03:00
										 |  |  |         return isset($info['premium']); | 
					
						
							| 
									
										
										
										
											2016-10-13 10:13:18 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-14 15:05:33 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Renders phpinfo | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return string The phpinfo() output | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-01-10 17:17:04 +01:00
										 |  |  |     function phpinfo() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-09-15 16:23:57 -06:00
										 |  |  |         if (function_exists('phpinfo')) { | 
					
						
							|  |  |  |             ob_start(); | 
					
						
							|  |  |  |             phpinfo(); | 
					
						
							| 
									
										
										
										
											2019-06-03 13:17:16 +03:00
										 |  |  |             $pinfo = ob_get_clean(); | 
					
						
							| 
									
										
										
										
											2015-09-15 16:23:57 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |             $pinfo = preg_replace('%^.*<body>(.*)</body>.*$%ms', '$1', $pinfo); | 
					
						
							| 
									
										
										
										
											2016-07-07 18:55:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-15 16:23:57 -06:00
										 |  |  |             return $pinfo; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-05-10 10:14:18 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return 'phpinfo() method is not available on this server.'; | 
					
						
							| 
									
										
										
										
											2015-08-11 18:30:27 -06:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-08-14 15:05:33 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-10 13:32:17 -06:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Guest date format based on euro/US | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2019-03-20 12:52:16 +02:00
										 |  |  |      * @param string $date | 
					
						
							| 
									
										
										
										
											2016-07-07 18:55:52 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-05-10 13:32:17 -06:00
										 |  |  |      * @return string | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function guessDateFormat($date) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         static $guess; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-26 17:28:55 -06:00
										 |  |  |         $date_formats = [ | 
					
						
							|  |  |  |             'm/d/y', | 
					
						
							|  |  |  |             'm/d/Y', | 
					
						
							|  |  |  |             'n/d/y', | 
					
						
							|  |  |  |             'n/d/Y', | 
					
						
							|  |  |  |             'd-m-Y', | 
					
						
							|  |  |  |             'd-m-y', | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $time_formats = [ | 
					
						
							|  |  |  |             'H:i', | 
					
						
							|  |  |  |             'G:i', | 
					
						
							|  |  |  |             'h:ia', | 
					
						
							|  |  |  |             'g:ia' | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-10 16:53:19 -06:00
										 |  |  |         if (!isset($guess[$date])) { | 
					
						
							| 
									
										
										
										
											2019-08-26 15:09:04 +03:00
										 |  |  |             $guess[$date] = 'd-m-Y H:i'; | 
					
						
							| 
									
										
										
										
											2016-08-26 17:28:55 -06:00
										 |  |  |             foreach ($date_formats as $date_format) { | 
					
						
							|  |  |  |                 foreach ($time_formats as $time_format) { | 
					
						
							| 
									
										
										
										
											2019-08-26 15:09:04 +03:00
										 |  |  |                     $full_format = "{$date_format} {$time_format}"; | 
					
						
							|  |  |  |                     if ($this->validateDate($date, $full_format)) { | 
					
						
							|  |  |  |                         $guess[$date] = $full_format; | 
					
						
							| 
									
										
										
										
											2016-08-26 17:28:55 -06:00
										 |  |  |                         break 2; | 
					
						
							| 
									
										
										
										
											2018-05-10 10:14:18 +03:00
										 |  |  |                     } | 
					
						
							| 
									
										
										
										
											2019-08-26 15:09:04 +03:00
										 |  |  |                     $full_format = "{$time_format} {$date_format}"; | 
					
						
							|  |  |  |                     if ($this->validateDate($date, $full_format)) { | 
					
						
							|  |  |  |                         $guess[$date] = $full_format; | 
					
						
							| 
									
										
										
										
											2016-08-26 17:28:55 -06:00
										 |  |  |                         break 2; | 
					
						
							|  |  |  |                     } | 
					
						
							| 
									
										
										
										
											2016-05-10 13:32:17 -06:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2016-08-26 17:28:55 -06:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2016-05-10 13:32:17 -06:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-10 16:53:19 -06:00
										 |  |  |         return $guess[$date]; | 
					
						
							| 
									
										
										
										
											2016-05-10 13:32:17 -06:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function validateDate($date, $format) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $d = DateTime::createFromFormat($format, $date); | 
					
						
							| 
									
										
										
										
											2016-07-07 18:55:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-10 13:32:17 -06:00
										 |  |  |         return $d && $d->format($format) == $date; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-21 09:46:38 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param string $php_format | 
					
						
							| 
									
										
										
										
											2016-07-07 18:55:52 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-01-21 09:46:38 +02:00
										 |  |  |      * @return string | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-05-10 13:32:17 -06:00
										 |  |  |     public function dateformatToMomentJS($php_format) | 
					
						
							| 
									
										
										
										
											2015-09-08 14:58:31 -06:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-07-07 18:55:52 +02:00
										 |  |  |         $SYMBOLS_MATCHING = [ | 
					
						
							| 
									
										
										
										
											2015-09-08 14:58:31 -06:00
										 |  |  |             // Day
 | 
					
						
							| 
									
										
										
										
											2016-02-05 23:49:39 -08:00
										 |  |  |             'd' => 'DD', | 
					
						
							| 
									
										
										
										
											2015-09-08 14:58:31 -06:00
										 |  |  |             'D' => 'ddd', | 
					
						
							| 
									
										
										
										
											2016-02-05 23:49:39 -08:00
										 |  |  |             'j' => 'D', | 
					
						
							| 
									
										
										
										
											2015-09-08 14:58:31 -06:00
										 |  |  |             'l' => 'dddd', | 
					
						
							| 
									
										
										
										
											2016-02-05 23:49:39 -08:00
										 |  |  |             'N' => 'E', | 
					
						
							|  |  |  |             'S' => 'Do', | 
					
						
							|  |  |  |             'w' => 'd', | 
					
						
							|  |  |  |             'z' => 'DDD', | 
					
						
							| 
									
										
										
										
											2015-09-08 14:58:31 -06:00
										 |  |  |             // Week
 | 
					
						
							| 
									
										
										
										
											2016-02-05 23:49:39 -08:00
										 |  |  |             'W' => 'W', | 
					
						
							| 
									
										
										
										
											2015-09-08 14:58:31 -06:00
										 |  |  |             // Month
 | 
					
						
							|  |  |  |             'F' => 'MMMM', | 
					
						
							|  |  |  |             'm' => 'MM', | 
					
						
							|  |  |  |             'M' => 'MMM', | 
					
						
							|  |  |  |             'n' => 'M', | 
					
						
							|  |  |  |             't' => '', | 
					
						
							|  |  |  |             // Year
 | 
					
						
							|  |  |  |             'L' => '', | 
					
						
							| 
									
										
										
										
											2016-02-05 23:49:39 -08:00
										 |  |  |             'o' => 'GGGG', | 
					
						
							|  |  |  |             'Y' => 'YYYY', | 
					
						
							| 
									
										
										
										
											2015-09-08 14:58:31 -06:00
										 |  |  |             'y' => 'yy', | 
					
						
							|  |  |  |             // Time
 | 
					
						
							| 
									
										
										
										
											2016-02-05 23:49:39 -08:00
										 |  |  |             'a' => 'a', | 
					
						
							|  |  |  |             'A' => 'A', | 
					
						
							|  |  |  |             'B' => 'SSS', | 
					
						
							| 
									
										
										
										
											2015-09-08 14:58:31 -06:00
										 |  |  |             'g' => 'h', | 
					
						
							|  |  |  |             'G' => 'H', | 
					
						
							|  |  |  |             'h' => 'hh', | 
					
						
							|  |  |  |             'H' => 'HH', | 
					
						
							|  |  |  |             'i' => 'mm', | 
					
						
							|  |  |  |             's' => 'ss', | 
					
						
							| 
									
										
										
										
											2016-02-05 23:49:39 -08:00
										 |  |  |             'u' => '', | 
					
						
							|  |  |  |             // Timezone
 | 
					
						
							|  |  |  |             'e' => '', | 
					
						
							|  |  |  |             'I' => '', | 
					
						
							|  |  |  |             'O' => 'ZZ', | 
					
						
							|  |  |  |             'P' => 'Z', | 
					
						
							|  |  |  |             'T' => 'z', | 
					
						
							|  |  |  |             'Z' => '', | 
					
						
							|  |  |  |             // Full Date/Time
 | 
					
						
							|  |  |  |             'c' => '', | 
					
						
							|  |  |  |             'r' => 'llll ZZ', | 
					
						
							|  |  |  |             'U' => 'X' | 
					
						
							| 
									
										
										
										
											2016-07-07 18:55:52 +02:00
										 |  |  |         ]; | 
					
						
							| 
									
										
										
										
											2018-05-10 10:14:18 +03:00
										 |  |  |         $js_format        = ''; | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |         $escaping         = false; | 
					
						
							| 
									
										
										
										
											2018-05-10 10:14:18 +03:00
										 |  |  |         $len = strlen($php_format); | 
					
						
							|  |  |  |         for ($i = 0; $i < $len; $i++) { | 
					
						
							| 
									
										
										
										
											2015-09-08 14:58:31 -06:00
										 |  |  |             $char = $php_format[$i]; | 
					
						
							| 
									
										
										
										
											2016-07-07 18:55:52 +02:00
										 |  |  |             if ($char === '\\') // PHP date format escaping character
 | 
					
						
							| 
									
										
										
										
											2015-09-08 14:58:31 -06:00
										 |  |  |             { | 
					
						
							|  |  |  |                 $i++; | 
					
						
							| 
									
										
										
										
											2016-07-07 18:55:52 +02:00
										 |  |  |                 if ($escaping) { | 
					
						
							|  |  |  |                     $js_format .= $php_format[$i]; | 
					
						
							|  |  |  |                 } else { | 
					
						
							|  |  |  |                     $js_format .= '\'' . $php_format[$i]; | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2015-09-08 14:58:31 -06:00
										 |  |  |                 $escaping = true; | 
					
						
							| 
									
										
										
										
											2016-07-07 18:55:52 +02:00
										 |  |  |             } else { | 
					
						
							|  |  |  |                 if ($escaping) { | 
					
						
							|  |  |  |                     $js_format .= "'"; | 
					
						
							|  |  |  |                     $escaping = false; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 if (isset($SYMBOLS_MATCHING[$char])) { | 
					
						
							| 
									
										
										
										
											2015-09-08 14:58:31 -06:00
										 |  |  |                     $js_format .= $SYMBOLS_MATCHING[$char]; | 
					
						
							| 
									
										
										
										
											2016-07-07 18:55:52 +02:00
										 |  |  |                 } else { | 
					
						
							| 
									
										
										
										
											2015-09-08 14:58:31 -06:00
										 |  |  |                     $js_format .= $char; | 
					
						
							| 
									
										
										
										
											2016-07-07 18:55:52 +02:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2015-09-08 14:58:31 -06:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-07-07 18:55:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-08 14:58:31 -06:00
										 |  |  |         return $js_format; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-05-04 14:26:27 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |      * Gets the entire permissions array | 
					
						
							| 
									
										
										
										
											2016-05-04 14:26:27 -06:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |      * @return array | 
					
						
							| 
									
										
										
										
											2020-01-15 16:05:16 +02:00
										 |  |  |      * @deprecated 1.10 Use $grav['permissions']->getInstances() instead. | 
					
						
							| 
									
										
										
										
											2016-05-04 14:26:27 -06:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |     public function getPermissions() | 
					
						
							| 
									
										
										
										
											2016-05-04 14:26:27 -06:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-01-15 16:05:16 +02:00
										 |  |  |         user_error(__METHOD__ . '() is deprecated since Admin 1.10, use $grav[\'permissions\']->getInstances() instead', E_USER_DEPRECATED); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $grav = $this->grav; | 
					
						
							|  |  |  |         /** @var Permissions $object */ | 
					
						
							|  |  |  |         $permissions = $grav['permissions']; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return array_fill_keys(array_keys($permissions->getInstances()), 'boolean'); | 
					
						
							| 
									
										
										
										
											2016-05-04 14:26:27 -06:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |      * Sets the entire permissions array | 
					
						
							| 
									
										
										
										
											2016-05-04 14:26:27 -06:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2019-03-20 12:52:16 +02:00
										 |  |  |      * @param array $permissions | 
					
						
							| 
									
										
										
										
											2020-01-21 09:46:42 +02:00
										 |  |  |      * @deprecated 1.10 Use PermissionsRegisterEvent::class event instead. | 
					
						
							| 
									
										
										
										
											2016-05-04 14:26:27 -06:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |     public function setPermissions($permissions) | 
					
						
							| 
									
										
										
										
											2016-05-04 14:26:27 -06:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-01-21 09:46:42 +02:00
										 |  |  |         user_error(__METHOD__ . '() is deprecated since Admin 1.10, use PermissionsRegisterEvent::class event instead', E_USER_DEPRECATED); | 
					
						
							| 
									
										
										
										
											2020-01-15 16:05:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $this->addPermissions($permissions); | 
					
						
							| 
									
										
										
										
											2016-05-04 14:26:27 -06:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Adds a permission to the permissions array | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2019-03-20 12:52:16 +02:00
										 |  |  |      * @param array $permissions | 
					
						
							| 
									
										
										
										
											2020-01-15 16:05:16 +02:00
										 |  |  |      * @deprecated 1.10 Use RegisterPermissionsEvent::class event instead. | 
					
						
							| 
									
										
										
										
											2016-05-04 14:26:27 -06:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function addPermissions($permissions) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-01-15 16:05:16 +02:00
										 |  |  |         user_error(__METHOD__ . '() is deprecated since Admin 1.10, use RegisterPermissionsEvent::class event instead', E_USER_DEPRECATED); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $grav = $this->grav; | 
					
						
							|  |  |  |         /** @var Permissions $object */ | 
					
						
							|  |  |  |         $object = $grav['permissions']; | 
					
						
							|  |  |  |         foreach ($permissions as $name => $type) { | 
					
						
							|  |  |  |             if (!$object->hasAction($name)) { | 
					
						
							|  |  |  |                 $action = new Action($name); | 
					
						
							|  |  |  |                 $object->addAction($action); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-05-04 14:26:27 -06:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-05-26 14:49:45 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-08 17:17:05 -07:00
										 |  |  |     public function getNotifications($force = false) | 
					
						
							| 
									
										
										
										
											2016-08-11 19:17:02 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-02-08 17:17:05 -07:00
										 |  |  |         $last_checked = null; | 
					
						
							|  |  |  |         $filename = $this->grav['locator']->findResource('user://data/notifications/' . md5($this->grav['user']->username) . YAML_EXT, true, true); | 
					
						
							| 
									
										
										
										
											2016-08-11 19:17:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-08 17:17:05 -07:00
										 |  |  |         $notifications_file = CompiledYamlFile::instance($filename); | 
					
						
							|  |  |  |         $notifications_content = (array)$notifications_file->content(); | 
					
						
							| 
									
										
										
										
											2016-08-11 19:17:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-08 17:17:05 -07:00
										 |  |  |         $last_checked = $notifications_content['last_checked'] ?? null; | 
					
						
							|  |  |  |         $notifications = $notifications_content['data'] ?? array(); | 
					
						
							|  |  |  |         $timeout = $this->grav['config']->get('system.session.timeout', 1800); | 
					
						
							| 
									
										
										
										
											2016-08-11 19:17:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-08 17:58:13 -07:00
										 |  |  |         if ($force || !$last_checked || empty($notifications) || (time() - $last_checked > $timeout)) { | 
					
						
							| 
									
										
										
										
											2019-02-12 14:41:58 -07:00
										 |  |  |             $body = Response::get('https://getgrav.org/notifications.json?' . time()); | 
					
						
							|  |  |  | //            $body = Response::get('http://localhost/notifications.json?' . time());
 | 
					
						
							| 
									
										
										
										
											2019-02-08 17:58:13 -07:00
										 |  |  |             $notifications = json_decode($body, true); | 
					
						
							| 
									
										
										
										
											2016-08-11 19:17:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-08 17:17:05 -07:00
										 |  |  |             // Sort by date
 | 
					
						
							|  |  |  |             usort($notifications, function ($a, $b) { | 
					
						
							|  |  |  |                 return strcmp($a['date'], $b['date']); | 
					
						
							|  |  |  |             }); | 
					
						
							| 
									
										
										
										
											2016-08-11 19:17:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-08 17:17:05 -07:00
										 |  |  |             // Reverse order and create a new array
 | 
					
						
							| 
									
										
										
										
											2019-02-08 17:58:13 -07:00
										 |  |  |             $notifications = array_reverse($notifications); | 
					
						
							|  |  |  |             $cleaned_notifications = []; | 
					
						
							| 
									
										
										
										
											2016-08-11 19:17:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-08 17:58:13 -07:00
										 |  |  |             foreach ($notifications as $key => $notification) { | 
					
						
							| 
									
										
										
										
											2019-02-08 17:17:05 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 if (isset($notification['permissions']) && !$this->authorize($notification['permissions'])) { | 
					
						
							|  |  |  |                     continue; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if (isset($notification['dependencies'])) { | 
					
						
							|  |  |  |                     foreach ($notification['dependencies'] as $dependency => $constraints) { | 
					
						
							|  |  |  |                         if ($dependency === 'grav') { | 
					
						
							|  |  |  |                             if (!Semver::satisfies(GRAV_VERSION, $constraints)) { | 
					
						
							|  |  |  |                                 continue; | 
					
						
							|  |  |  |                             } | 
					
						
							| 
									
										
										
										
											2016-08-11 19:17:02 +02:00
										 |  |  |                         } else { | 
					
						
							| 
									
										
										
										
											2019-02-08 17:17:05 -07:00
										 |  |  |                             $packages = array_merge($this->plugins()->toArray(), $this->themes()->toArray()); | 
					
						
							|  |  |  |                             if (!isset($packages[$dependency])) { | 
					
						
							|  |  |  |                                 continue; | 
					
						
							|  |  |  |                             } else { | 
					
						
							|  |  |  |                                 $version = $packages[$dependency]['version']; | 
					
						
							|  |  |  |                                 if (!Semver::satisfies($version, $constraints)) { | 
					
						
							|  |  |  |                                     continue; | 
					
						
							|  |  |  |                                 } | 
					
						
							| 
									
										
										
										
											2016-08-11 19:17:02 +02:00
										 |  |  |                             } | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-08 17:17:05 -07:00
										 |  |  |                 $cleaned_notifications[] = $notification; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-11 19:17:02 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2019-02-08 17:17:05 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-08 17:58:13 -07:00
										 |  |  |             // reset notifications
 | 
					
						
							|  |  |  |             $notifications = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             foreach($cleaned_notifications as $notification) { | 
					
						
							|  |  |  |                 foreach ($notification['location'] as $location) { | 
					
						
							|  |  |  |                     $notifications = array_merge_recursive($notifications, [$location => [$notification]]); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-08 17:17:05 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |             $notifications_file->content(['last_checked' => time(), 'data' => $notifications]); | 
					
						
							|  |  |  |             $notifications_file->save(); | 
					
						
							| 
									
										
										
										
											2016-08-11 19:17:02 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-26 19:36:01 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-08 17:17:05 -07:00
										 |  |  |         return $notifications; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Get https://getgrav.org news feed | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return mixed | 
					
						
							|  |  |  |      * @throws MalformedXmlException | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getFeed($force = false) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $last_checked = null; | 
					
						
							|  |  |  |         $filename = $this->grav['locator']->findResource('user://data/feed/' . md5($this->grav['user']->username) . YAML_EXT, true, true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $feed_file = CompiledYamlFile::instance($filename); | 
					
						
							|  |  |  |         $feed_content = (array)$feed_file->content(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $last_checked = $feed_content['last_checked'] ?? null; | 
					
						
							|  |  |  |         $feed = $feed_content['data'] ?? array(); | 
					
						
							|  |  |  |         $timeout = $this->grav['config']->get('system.session.timeout', 1800); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($force || !$last_checked || empty($feed) || ($last_checked && (time() - $last_checked > $timeout))) { | 
					
						
							|  |  |  |             $feed_url = 'https://getgrav.org/blog.atom'; | 
					
						
							|  |  |  |             $body = Response::get($feed_url); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $reader = new Reader(); | 
					
						
							|  |  |  |             $parser = $reader->getParser($feed_url, $body, 'utf-8'); | 
					
						
							|  |  |  |             $data = $parser->execute()->getItems(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // Get top 10
 | 
					
						
							|  |  |  |             $data = array_slice($data, 0, 10); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $feed = array_map(function ($entry) { | 
					
						
							|  |  |  |                 $simple_entry['title'] = $entry->getTitle(); | 
					
						
							|  |  |  |                 $simple_entry['url'] = $entry->getUrl(); | 
					
						
							|  |  |  |                 $simple_entry['date'] = $entry->getDate()->getTimestamp(); | 
					
						
							|  |  |  |                 $simple_entry['nicetime'] = $this->adminNiceTime($simple_entry['date']); | 
					
						
							|  |  |  |                 return $simple_entry; | 
					
						
							|  |  |  |             }, $data); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $feed_file->content(['last_checked' => time(), 'data' => $feed]); | 
					
						
							|  |  |  |             $feed_file->save(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $feed; | 
					
						
							| 
									
										
										
										
											2016-08-11 19:17:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-08 17:17:05 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function adminNiceTime($date, $long_strings = true) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (empty($date)) { | 
					
						
							| 
									
										
										
										
											2019-05-27 19:21:17 +03:00
										 |  |  |             return $this->translate('GRAV.NICETIME.NO_DATE_PROVIDED', null); | 
					
						
							| 
									
										
										
										
											2019-02-08 17:17:05 -07:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($long_strings) { | 
					
						
							|  |  |  |             $periods = [ | 
					
						
							|  |  |  |                 'NICETIME.SECOND', | 
					
						
							|  |  |  |                 'NICETIME.MINUTE', | 
					
						
							|  |  |  |                 'NICETIME.HOUR', | 
					
						
							|  |  |  |                 'NICETIME.DAY', | 
					
						
							|  |  |  |                 'NICETIME.WEEK', | 
					
						
							|  |  |  |                 'NICETIME.MONTH', | 
					
						
							|  |  |  |                 'NICETIME.YEAR', | 
					
						
							|  |  |  |                 'NICETIME.DECADE' | 
					
						
							|  |  |  |             ]; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             $periods = [ | 
					
						
							|  |  |  |                 'NICETIME.SEC', | 
					
						
							|  |  |  |                 'NICETIME.MIN', | 
					
						
							|  |  |  |                 'NICETIME.HR', | 
					
						
							|  |  |  |                 'NICETIME.DAY', | 
					
						
							|  |  |  |                 'NICETIME.WK', | 
					
						
							|  |  |  |                 'NICETIME.MO', | 
					
						
							|  |  |  |                 'NICETIME.YR', | 
					
						
							|  |  |  |                 'NICETIME.DEC' | 
					
						
							|  |  |  |             ]; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $lengths = ['60', '60', '24', '7', '4.35', '12', '10']; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $now = time(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // check if unix timestamp
 | 
					
						
							|  |  |  |         if ((string)(int)$date === (string)$date) { | 
					
						
							|  |  |  |             $unix_date = $date; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             $unix_date = strtotime($date); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // check validity of date
 | 
					
						
							|  |  |  |         if (empty($unix_date)) { | 
					
						
							| 
									
										
										
										
											2019-05-27 19:21:17 +03:00
										 |  |  |             return $this->translate('GRAV.NICETIME.BAD_DATE', null); | 
					
						
							| 
									
										
										
										
											2019-02-08 17:17:05 -07:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // is it future date or past date
 | 
					
						
							|  |  |  |         if ($now > $unix_date) { | 
					
						
							|  |  |  |             $difference = $now - $unix_date; | 
					
						
							| 
									
										
										
										
											2019-05-27 19:21:17 +03:00
										 |  |  |             $tense      = $this->translate('GRAV.NICETIME.AGO', null); | 
					
						
							| 
									
										
										
										
											2019-02-08 17:17:05 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             $difference = $unix_date - $now; | 
					
						
							| 
									
										
										
										
											2019-05-27 19:21:17 +03:00
										 |  |  |             $tense      = $this->translate('GRAV.NICETIME.FROM_NOW', null); | 
					
						
							| 
									
										
										
										
											2019-02-08 17:17:05 -07:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $len = count($lengths) - 1; | 
					
						
							|  |  |  |         for ($j = 0; $difference >= $lengths[$j] && $j < $len; $j++) { | 
					
						
							|  |  |  |             $difference /= $lengths[$j]; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $difference = round($difference); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($difference !== 1) { | 
					
						
							|  |  |  |             $periods[$j] .= '_PLURAL'; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($this->grav['language']->getTranslation($this->grav['user']->language, | 
					
						
							|  |  |  |             $periods[$j] . '_MORE_THAN_TWO') | 
					
						
							|  |  |  |         ) { | 
					
						
							|  |  |  |             if ($difference > 2) { | 
					
						
							|  |  |  |                 $periods[$j] .= '_MORE_THAN_TWO'; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 19:21:17 +03:00
										 |  |  |         $periods[$j] = $this->translate('GRAV.'.$periods[$j], null); | 
					
						
							| 
									
										
										
										
											2019-02-08 17:17:05 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return "{$difference} {$periods[$j]} {$tense}"; | 
					
						
							| 
									
										
										
										
											2016-08-11 19:17:02 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-26 14:49:45 -06:00
										 |  |  |     public function findFormFields($type, $fields, $found_fields = []) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         foreach ($fields as $key => $field) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (isset($field['type']) && $field['type'] == $type) { | 
					
						
							|  |  |  |                 $found_fields[$key] = $field; | 
					
						
							|  |  |  |             } elseif (isset($field['fields'])) { | 
					
						
							|  |  |  |                 $result = $this->findFormFields($type, $field['fields'], $found_fields); | 
					
						
							|  |  |  |                 if (!empty($result)) { | 
					
						
							|  |  |  |                     $found_fields = array_merge($found_fields, $result); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-07-07 18:55:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-26 14:49:45 -06:00
										 |  |  |         return $found_fields; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-08 11:28:41 +02:00
										 |  |  |     public function getPagePathFromToken($path, $page = null) | 
					
						
							| 
									
										
										
										
											2016-05-26 14:49:45 -06:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-01-08 11:28:41 +02:00
										 |  |  |         return Utils::getPagePathFromToken($path, $page ?: $this->page(true)); | 
					
						
							| 
									
										
										
										
											2016-05-26 14:49:45 -06:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Returns edited page. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param bool $route | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param null $path | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2019-03-05 15:46:55 +02:00
										 |  |  |      * @return PageInterface | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function page($route = false, $path = null) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (!$path) { | 
					
						
							|  |  |  |             $path = $this->route; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($route && !$path) { | 
					
						
							|  |  |  |             $path = '/'; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!isset($this->pages[$path])) { | 
					
						
							|  |  |  |             $this->pages[$path] = $this->getPage($path); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $this->pages[$path]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Returns the page creating it if it does not exist. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2019-03-20 12:52:16 +02:00
										 |  |  |      * @param string $path | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2019-03-20 12:52:16 +02:00
										 |  |  |      * @return PageInterface|null | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function getPage($path) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-08-23 20:45:51 +03:00
										 |  |  |         $pages = static::enablePages(); | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-10 10:14:18 +03:00
										 |  |  |         if ($path && $path[0] !== '/') { | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |             $path = "/{$path}"; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-08 12:01:27 -06:00
										 |  |  |         // Fix for entities in path causing looping...
 | 
					
						
							|  |  |  |         $path = urldecode($path); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |         $page = $path ? $pages->dispatch($path, true) : $pages->root(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!$page) { | 
					
						
							|  |  |  |             $slug = basename($path); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-10 10:14:18 +03:00
										 |  |  |             if ($slug === '') { | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |                 return null; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $ppath = str_replace('\\', '/', dirname($path)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // Find or create parent(s).
 | 
					
						
							| 
									
										
										
										
											2018-05-10 10:14:18 +03:00
										 |  |  |             $parent = $this->getPage($ppath !== '/' ? $ppath : ''); | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |             // Create page.
 | 
					
						
							| 
									
										
										
										
											2019-03-05 15:46:55 +02:00
										 |  |  |             $page = new Page(); | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |             $page->parent($parent); | 
					
						
							|  |  |  |             $page->filePath($parent->path() . '/' . $slug . '/' . $page->name()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // Add routing information.
 | 
					
						
							|  |  |  |             $pages->addPage($page, $path); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // Set if Modular
 | 
					
						
							| 
									
										
										
										
											2019-01-14 11:48:58 -07:00
										 |  |  |             $page->modularTwig($slug[0] === '_'); | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |             // Determine page type.
 | 
					
						
							|  |  |  |             if (isset($this->session->{$page->route()})) { | 
					
						
							|  |  |  |                 // Found the type and header from the session.
 | 
					
						
							|  |  |  |                 $data = $this->session->{$page->route()}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 // Set the key header value
 | 
					
						
							|  |  |  |                 $header = ['title' => $data['title']]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if (isset($data['visible'])) { | 
					
						
							| 
									
										
										
										
											2018-05-10 10:14:18 +03:00
										 |  |  |                     if ($data['visible'] === '' || $data['visible']) { | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |                         // if auto (ie '')
 | 
					
						
							| 
									
										
										
										
											2018-05-10 10:14:18 +03:00
										 |  |  |                         $pageParent = $page->parent(); | 
					
						
							|  |  |  |                         $children = $pageParent ? $pageParent->children() : []; | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |                         foreach ($children as $child) { | 
					
						
							|  |  |  |                             if ($child->order()) { | 
					
						
							|  |  |  |                                 // set page order
 | 
					
						
							| 
									
										
										
										
											2018-05-10 10:14:18 +03:00
										 |  |  |                                 $page->order(AdminController::getNextOrderInFolder($pageParent->path())); | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |                                 break; | 
					
						
							|  |  |  |                             } | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                     } | 
					
						
							| 
									
										
										
										
											2018-12-05 08:20:38 +02:00
										 |  |  |                     if ((int)$data['visible'] === 1 && !$page->order()) { | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |                         $header['visible'] = $data['visible']; | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-10 10:14:18 +03:00
										 |  |  |                 if ($data['name'] === 'modular') { | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |                     $header['body_classes'] = 'modular'; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-15 21:51:47 +02:00
										 |  |  |                 $name = $page->isModule() ? str_replace('modular/', '', $data['name']) : $data['name']; | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |                 $page->name($name . '.md'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 // Fire new event to allow plugins to manipulate page frontmatter
 | 
					
						
							| 
									
										
										
										
											2017-08-03 01:09:27 +02:00
										 |  |  |                 $this->grav->fireEvent('onAdminCreatePageFrontmatter', new Event(['header' => &$header, | 
					
						
							|  |  |  |                         'data' => $data])); | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 $page->header($header); | 
					
						
							| 
									
										
										
										
											2018-03-08 17:11:10 -07:00
										 |  |  |                 $page->frontmatter(Yaml::dump((array)$page->header(), 20)); | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |             } else { | 
					
						
							|  |  |  |                 // Find out the type by looking at the parent.
 | 
					
						
							| 
									
										
										
										
											2018-12-05 08:20:38 +02:00
										 |  |  |                 $type = $parent->childType() ?: $parent->blueprints()->get('child_type', 'default'); | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |                 $page->name($type . CONTENT_EXT); | 
					
						
							|  |  |  |                 $page->header(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $page; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-07 14:24:23 -06:00
										 |  |  |     public function generateReports() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $reports = new ArrayCollection(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-23 20:45:51 +03:00
										 |  |  |         $pages = static::enablePages(); | 
					
						
							| 
									
										
										
										
											2019-08-22 19:25:59 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-07 14:24:23 -06:00
										 |  |  |         // Default to XSS Security Report
 | 
					
						
							| 
									
										
										
										
											2019-08-22 19:25:59 +03:00
										 |  |  |         $result = Security::detectXssFromPages($pages, true); | 
					
						
							| 
									
										
										
										
											2018-10-07 14:24:23 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $reports['Grav Security Check'] = $this->grav['twig']->processTemplate('reports/security.html.twig', [ | 
					
						
							|  |  |  |             'result' => $result, | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-20 17:43:26 -06:00
										 |  |  |         // Linting Issues
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $result = YamlLinter::lint(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-21 14:02:55 -06:00
										 |  |  |         $reports['Grav Yaml Linter'] = $this->grav['twig']->processTemplate('reports/yamllinter.html.twig', [ | 
					
						
							| 
									
										
										
										
											2019-04-20 17:43:26 -06:00
										 |  |  |            'result' => $result, | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-07 14:24:23 -06:00
										 |  |  |         // Fire new event to allow plugins to manipulate page frontmatter
 | 
					
						
							|  |  |  |         $this->grav->fireEvent('onAdminGenerateReports', new Event(['reports' => $reports])); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $reports; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-26 17:52:26 -06:00
										 |  |  |     public function getRouteDetails() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return [$this->base, $this->location, $this->route]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-26 14:30:37 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Get the files list | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2018-12-05 08:20:38 +02:00
										 |  |  |      * @param bool $filtered | 
					
						
							|  |  |  |      * @param int $page_index | 
					
						
							|  |  |  |      * @return array|null | 
					
						
							| 
									
										
										
										
											2017-05-26 14:30:37 +02:00
										 |  |  |      * @todo allow pagination | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function files($filtered = true, $page_index = 0) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $param_type = $this->grav['uri']->param('type'); | 
					
						
							|  |  |  |         $param_date = $this->grav['uri']->param('date'); | 
					
						
							|  |  |  |         $param_page = $this->grav['uri']->param('page'); | 
					
						
							|  |  |  |         $param_page = str_replace('\\', '/', $param_page); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $files_cache_key = 'media-manager-files'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($param_type) { | 
					
						
							|  |  |  |             $files_cache_key .= "-{$param_type}"; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if ($param_date) { | 
					
						
							|  |  |  |             $files_cache_key .= "-{$param_date}"; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if ($param_page) { | 
					
						
							|  |  |  |             $files_cache_key .= "-{$param_page}"; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $page_files = null; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $cache_enabled = $this->grav['config']->get('plugins.admin.cache_enabled'); | 
					
						
							|  |  |  |         if (!$cache_enabled) { | 
					
						
							|  |  |  |             $this->grav['cache']->setEnabled(true); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $page_files = $this->grav['cache']->fetch(md5($files_cache_key)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!$cache_enabled) { | 
					
						
							|  |  |  |             $this->grav['cache']->setEnabled(false); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!$page_files) { | 
					
						
							|  |  |  |             $page_files = []; | 
					
						
							| 
									
										
										
										
											2019-08-23 20:45:51 +03:00
										 |  |  |             $pages = static::enablePages(); | 
					
						
							| 
									
										
										
										
											2017-05-26 14:30:37 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if ($param_page) { | 
					
						
							|  |  |  |                 $page = $pages->dispatch($param_page); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 $page_files = $this->getFiles('images', $page, $page_files, $filtered); | 
					
						
							|  |  |  |                 $page_files = $this->getFiles('videos', $page, $page_files, $filtered); | 
					
						
							|  |  |  |                 $page_files = $this->getFiles('audios', $page, $page_files, $filtered); | 
					
						
							|  |  |  |                 $page_files = $this->getFiles('files', $page, $page_files, $filtered); | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 $allPages = $pages->all(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if ($allPages) foreach ($allPages as $page) { | 
					
						
							|  |  |  |                     $page_files = $this->getFiles('images', $page, $page_files, $filtered); | 
					
						
							|  |  |  |                     $page_files = $this->getFiles('videos', $page, $page_files, $filtered); | 
					
						
							|  |  |  |                     $page_files = $this->getFiles('audios', $page, $page_files, $filtered); | 
					
						
							|  |  |  |                     $page_files = $this->getFiles('files', $page, $page_files, $filtered); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (count($page_files) >= self::MEDIA_PAGINATION_INTERVAL) { | 
					
						
							|  |  |  |                 $this->shouldLoadAdditionalFilesInBackground(true); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (!$cache_enabled) { | 
					
						
							|  |  |  |                 $this->grav['cache']->setEnabled(true); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             $this->grav['cache']->save(md5($files_cache_key), $page_files, 600); //cache for 10 minutes
 | 
					
						
							|  |  |  |             if (!$cache_enabled) { | 
					
						
							|  |  |  |                 $this->grav['cache']->setEnabled(false); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (count($page_files) >= self::MEDIA_PAGINATION_INTERVAL) { | 
					
						
							|  |  |  |             $page_files = array_slice($page_files, $page_index * self::MEDIA_PAGINATION_INTERVAL, self::MEDIA_PAGINATION_INTERVAL); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $page_files; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function shouldLoadAdditionalFilesInBackground($status = null) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if ($status) { | 
					
						
							|  |  |  |             $this->load_additional_files_in_background = true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $this->load_additional_files_in_background; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function loadAdditionalFilesInBackground($status = null) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (!$this->loading_additional_files_in_background) { | 
					
						
							|  |  |  |             $this->loading_additional_files_in_background = true; | 
					
						
							|  |  |  |             $this->files(false, false); | 
					
						
							|  |  |  |             $this->shouldLoadAdditionalFilesInBackground(false); | 
					
						
							|  |  |  |             $this->loading_additional_files_in_background = false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private function getFiles($type, $page, $page_files, $filtered) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $page_files = $this->getMediaOfType($type, $page, $page_files); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($filtered) { | 
					
						
							|  |  |  |             $page_files = $this->filterByType($page_files); | 
					
						
							|  |  |  |             $page_files = $this->filterByDate($page_files); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $page_files; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Get all the media of a type ('images' | 'audios' | 'videos' | 'files') | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param string $type | 
					
						
							| 
									
										
										
										
											2019-03-05 15:46:55 +02:00
										 |  |  |      * @param PageInterface|null $page | 
					
						
							| 
									
										
										
										
											2017-05-26 14:30:37 +02:00
										 |  |  |      * @param array $files | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2019-03-05 15:46:55 +02:00
										 |  |  |     private function getMediaOfType($type, ?PageInterface $page, array $files) | 
					
						
							| 
									
										
										
										
											2018-05-10 10:14:18 +03:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2017-05-26 14:30:37 +02:00
										 |  |  |         if ($page) { | 
					
						
							|  |  |  |             $media = $page->media(); | 
					
						
							|  |  |  |             $mediaOfType = $media->$type(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             foreach($mediaOfType as $title => $file) { | 
					
						
							| 
									
										
										
										
											2018-05-10 10:14:18 +03:00
										 |  |  |                 $files[] = [ | 
					
						
							| 
									
										
										
										
											2017-05-26 14:30:37 +02:00
										 |  |  |                     'title' => $title, | 
					
						
							|  |  |  |                     'type' => $type, | 
					
						
							| 
									
										
										
										
											2017-05-26 15:14:27 +02:00
										 |  |  |                     'page_route' => $page->route(), | 
					
						
							| 
									
										
										
										
											2017-05-26 14:30:37 +02:00
										 |  |  |                     'file' => $file->higherQualityAlternative() | 
					
						
							|  |  |  |                 ]; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-10 10:14:18 +03:00
										 |  |  |             return $files; | 
					
						
							| 
									
										
										
										
											2017-05-26 14:30:37 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-05-10 10:14:18 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return []; | 
					
						
							| 
									
										
										
										
											2017-05-26 14:30:37 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Filter media by type | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param array $filesFiltered | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private function filterByType($filesFiltered) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $filter_type = $this->grav['uri']->param('type'); | 
					
						
							|  |  |  |         if (!$filter_type) { | 
					
						
							|  |  |  |             return $filesFiltered; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $filesFiltered = array_filter($filesFiltered, function ($file) use ($filter_type) { | 
					
						
							|  |  |  |             return $file['type'] == $filter_type; | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $filesFiltered; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Filter media by date | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param array $filesFiltered | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private function filterByDate($filesFiltered) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $filter_date = $this->grav['uri']->param('date'); | 
					
						
							|  |  |  |         if (!$filter_date) { | 
					
						
							|  |  |  |             return $filesFiltered; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $year = substr($filter_date, 0, 4); | 
					
						
							|  |  |  |         $month = substr($filter_date, 5, 2); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $filesFilteredByDate = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         foreach($filesFiltered as $file) { | 
					
						
							|  |  |  |             $filedate = $this->fileDate($file['file']); | 
					
						
							|  |  |  |             $fileYear = $filedate->format('Y'); | 
					
						
							|  |  |  |             $fileMonth = $filedate->format('m'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if ($fileYear == $year && $fileMonth == $month) { | 
					
						
							|  |  |  |                 $filesFilteredByDate[] = $file; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $filesFilteredByDate; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Return the DateTime object representation of a file modified date | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param File $file | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return DateTime | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private function fileDate($file) { | 
					
						
							|  |  |  |         $datetime = new \DateTime(); | 
					
						
							|  |  |  |         $datetime->setTimestamp($file->toArray()['modified']); | 
					
						
							|  |  |  |         return $datetime; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Get the files dates list to be used in the Media Files filter | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function filesDates() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $files = $this->files(false); | 
					
						
							|  |  |  |         $dates = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         foreach ($files as $file) { | 
					
						
							|  |  |  |             $datetime = $this->fileDate($file['file']); | 
					
						
							|  |  |  |             $year = $datetime->format('Y'); | 
					
						
							|  |  |  |             $month = $datetime->format('m'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (!isset($dates[$year])) { | 
					
						
							|  |  |  |                 $dates[$year] = []; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (!isset($dates[$year][$month])) { | 
					
						
							|  |  |  |                 $dates[$year][$month] = 1; | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 $dates[$year][$month]++; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $dates; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Get the pages list to be used in the Media Files filter | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function pages() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-08-23 20:45:51 +03:00
										 |  |  |         $pages = static::enablePages(); | 
					
						
							| 
									
										
										
										
											2019-08-22 19:25:59 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $collection = $pages->all(); | 
					
						
							| 
									
										
										
										
											2017-05-26 14:30:37 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $pagesWithFiles = []; | 
					
						
							| 
									
										
										
										
											2019-08-22 19:25:59 +03:00
										 |  |  |         foreach ($collection as $page) { | 
					
						
							| 
									
										
										
										
											2017-05-26 14:30:37 +02:00
										 |  |  |             if (count($page->media()->all())) { | 
					
						
							|  |  |  |                 $pagesWithFiles[] = $page; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $pagesWithFiles; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-08-25 16:20:57 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-23 20:45:51 +03:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @return Pages | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public static function enablePages() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         static $pages; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($pages) { | 
					
						
							|  |  |  |             return $pages; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $grav = Grav::instance(); | 
					
						
							|  |  |  |         $admin = $grav['admin']; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** @var Pages $pages */ | 
					
						
							|  |  |  |         $pages = Grav::instance()['pages']; | 
					
						
							|  |  |  |         $pages->enablePages(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // If page is null, the default page does not exist, and we cannot route to it
 | 
					
						
							|  |  |  |         $page = $pages->dispatch('/', true); | 
					
						
							|  |  |  |         if ($page) { | 
					
						
							|  |  |  |             // Set original route for the home page.
 | 
					
						
							|  |  |  |             $home = '/' . trim($grav['config']->get('system.home.alias'), '/'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $page->route($home); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $admin->routes = $pages->routes(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Remove default route from routes.
 | 
					
						
							|  |  |  |         if (isset($admin->routes['/'])) { | 
					
						
							|  |  |  |             unset($admin->routes['/']); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $pages; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-20 11:05:11 -06:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Return HTTP_REFERRER if set | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return null | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-03-11 15:23:25 -06:00
										 |  |  |     public function getReferrer() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-12-05 08:20:38 +02:00
										 |  |  |         return $_SERVER['HTTP_REFERER'] ?? null; | 
					
						
							| 
									
										
										
										
											2018-03-11 15:23:25 -06:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-10-25 10:51:44 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-09 10:58:49 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Get Grav system log files | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2019-10-25 10:51:44 -06:00
										 |  |  |     public function getLogFiles() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $logs = new GravData(['grav.log' => 'Grav System Log', 'email.log' => 'Email Log']); | 
					
						
							|  |  |  |         Grav::instance()->fireEvent('onAdminLogFiles', new Event(['logs' => &$logs])); | 
					
						
							|  |  |  |         return $logs->toArray(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-12-09 10:58:49 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Get changelog for a given GPM package based on slug | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param null $slug | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getChangelog($slug = null) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $gpm = $this->gpm(); | 
					
						
							|  |  |  |         $changelog = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!empty($slug)) { | 
					
						
							|  |  |  |             $package = $gpm->findPackage($slug); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             $package = $gpm->grav; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($package) { | 
					
						
							|  |  |  |             $changelog = $package->getChangelog(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $changelog; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  | } |