| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							| 
									
										
										
										
											2014-10-07 12:12:21 +03:00
										 |  |  | use Grav\Common\File\CompiledYamlFile; | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							| 
									
										
										
										
											2015-08-17 18:22:15 -06:00
										 |  |  | use Grav\Common\Language\LanguageCodes; | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  | use Grav\Common\Themes; | 
					
						
							|  |  |  | use Grav\Common\Uri; | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							| 
									
										
										
										
											2016-07-07 18:47:48 +02:00
										 |  |  | use Grav\Plugin\Admin\Utils as AdminUtils; | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  | use RocketTheme\Toolbox\Session\Session; | 
					
						
							| 
									
										
										
										
											2014-09-26 14:10:32 +03:00
										 |  |  | use Symfony\Component\Yaml\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 | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-05-31 19:37:44 +02:00
										 |  |  |     const MEDIA_PAGINATION_INTERVAL = 20; | 
					
						
							| 
									
										
										
										
											2017-05-26 14:30:37 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-29 11:27:53 +03:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @var Grav | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public $grav; | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |      * @var string | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |     public $base; | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |      * @var string | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |     public $location; | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |      * @var string | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |     public $route; | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |      * @var User | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |     public $user; | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |      * @var array | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |     public $forgot; | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @var string | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |     public $task; | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |      * @var array | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |     public $json_response; | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |      * @var Uri $uri | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |     protected $uri; | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @var array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected $pages = []; | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @var Session | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected $session; | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @var Data\Blueprints | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected $blueprints; | 
					
						
							| 
									
										
										
										
											2015-08-14 15:05:33 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-01-21 09:46:38 +02:00
										 |  |  |      * @var GPM | 
					
						
							| 
									
										
										
										
											2014-10-02 12:14:13 -07:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2015-04-04 14:49:40 +02:00
										 |  |  |     protected $gpm; | 
					
						
							| 
									
										
										
										
											2014-10-02 12:14:13 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-28 14:03:53 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @var int | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected $pages_count; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-04 14:26:27 -06:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |      * @var array | 
					
						
							| 
									
										
										
										
											2016-05-04 14:26:27 -06:00
										 |  |  |      */ | 
					
						
							|  |  |  |     protected $permissions; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-26 14:30:37 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @var bool | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected $load_additional_files_in_background = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @var bool | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected $loading_additional_files_in_background = false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |         $this->grav        = $grav; | 
					
						
							|  |  |  |         $this->base        = $base; | 
					
						
							|  |  |  |         $this->location    = $location; | 
					
						
							|  |  |  |         $this->route       = $route; | 
					
						
							|  |  |  |         $this->uri         = $this->grav['uri']; | 
					
						
							|  |  |  |         $this->session     = $this->grav['session']; | 
					
						
							|  |  |  |         $this->user        = $this->grav['user']; | 
					
						
							| 
									
										
										
										
											2016-05-04 14:26:27 -06:00
										 |  |  |         $this->permissions = []; | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |         $language          = $this->grav['language']; | 
					
						
							| 
									
										
										
										
											2016-07-07 18:55:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-07 18:47:48 +02:00
										 |  |  |         // Load utility class
 | 
					
						
							| 
									
										
										
										
											2015-08-14 19:12:04 +02:00
										 |  |  |         if ($language->enabled()) { | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |             $this->multilang         = true; | 
					
						
							| 
									
										
										
										
											2015-08-14 19:12:04 +02:00
										 |  |  |             $this->languages_enabled = $this->grav['config']->get('system.languages.supported', []); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             //Set the currently active language for the admin
 | 
					
						
							|  |  |  |             $language = $this->grav['uri']->param('lang'); | 
					
						
							|  |  |  |             if (!$language) { | 
					
						
							| 
									
										
										
										
											2016-04-25 10:18:24 +02:00
										 |  |  |                 if (!$this->session->admin_lang) { | 
					
						
							| 
									
										
										
										
											2016-05-03 13:36:06 -06:00
										 |  |  |                     $this->session->admin_lang = $this->grav['language']->getLanguage(); | 
					
						
							| 
									
										
										
										
											2016-04-25 10:18:24 +02:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2015-08-14 19:12:04 +02:00
										 |  |  |                 $language = $this->session->admin_lang; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             $this->grav['language']->setActive($language ?: 'en'); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             $this->grav['language']->setActive('en'); | 
					
						
							|  |  |  |             $this->multilang = false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							|  |  |  |             if ($file->isDir() || $file->isDot() || Utils::startsWith($file->getBasename(), '.')) { | 
					
						
							|  |  |  |                 continue; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $lang = basename($file->getBasename(), '.yaml'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $languages[$lang] = LanguageCodes::getNativeName($lang); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $languages; | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             $configurations[] = basename($file->getBasename(), '.yaml'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $configurations; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-22 21:34:21 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Return the tools found | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public static function tools() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $tools = []; | 
					
						
							|  |  |  |         $event = 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; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Return the languages available in the site | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public static function siteLanguages() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $languages = []; | 
					
						
							|  |  |  |         $lang_data = Grav::instance()['config']->get('system.languages.supported', []); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         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() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return Grav::instance()['session']->lastPageRoute ?: self::route(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Static helper method to return current route. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return string | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public static function route() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $pages = Grav::instance()['pages']; | 
					
						
							|  |  |  |         $route = '/' . ltrim(Grav::instance()['admin']->route, '/'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** @var Page $page */ | 
					
						
							|  |  |  |         $page         = $pages->dispatch($route); | 
					
						
							|  |  |  |         $parent_route = null; | 
					
						
							|  |  |  |         if ($page) { | 
					
						
							|  |  |  |             $parent       = $page->parent(); | 
					
						
							|  |  |  |             $parent_route = $parent->rawRoute(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $parent_route; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     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(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $pages = $grav['pages']; | 
					
						
							|  |  |  |         $route = '/' . ltrim($grav['admin']->route, '/'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** @var Page $page */ | 
					
						
							|  |  |  |         $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. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-04-07 13:09:22 +02:00
										 |  |  |      * @param  array $data Form data. | 
					
						
							|  |  |  |      * @param  array $post Additional form fields. | 
					
						
							| 
									
										
										
										
											2015-08-11 18:30:27 -06:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-04-07 13:09:22 +02:00
										 |  |  |     public function authenticate($data, $post) | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-04-07 13:09:22 +02:00
										 |  |  |         if (!$this->user->authenticated && isset($data['username']) && isset($data['password'])) { | 
					
						
							| 
									
										
										
										
											2016-07-07 18:47:48 +02:00
										 |  |  |             // Perform RegEX check on submitted username to check for emails
 | 
					
						
							|  |  |  |             if (filter_var($data['username'], FILTER_VALIDATE_EMAIL)) { | 
					
						
							| 
									
										
										
										
											2016-07-07 18:53:37 +02:00
										 |  |  |                 $user = AdminUtils::findUserByEmail($data['username']); | 
					
						
							| 
									
										
										
										
											2016-07-07 18:47:48 +02:00
										 |  |  |             } else { | 
					
						
							|  |  |  |                 $user = User::load($data['username']); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2015-08-20 11:29:37 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |             //default to english if language not set
 | 
					
						
							|  |  |  |             if (empty($user->language)) { | 
					
						
							|  |  |  |                 $user->set('language', 'en'); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-11 17:47:37 +02:00
										 |  |  |             if ($user->exists()) { | 
					
						
							| 
									
										
										
										
											2014-09-09 06:10:31 +03:00
										 |  |  |                 $user->authenticated = true; | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 // Authenticate user.
 | 
					
						
							| 
									
										
										
										
											2016-04-07 13:09:22 +02:00
										 |  |  |                 $result = $user->authenticate($data['password']); | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-08 16:42:57 +01:00
										 |  |  |                 if (!$result) { | 
					
						
							|  |  |  |                     return false; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-08 16:42:57 +01:00
										 |  |  |         $action = []; | 
					
						
							| 
									
										
										
										
											2015-07-28 11:11:43 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-08 16:42:57 +01:00
										 |  |  |         if ($user->authorize('admin.login')) { | 
					
						
							|  |  |  |             $this->user = $this->session->user = $user; | 
					
						
							| 
									
										
										
										
											2016-05-03 13:36:06 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-08 16:42:57 +01:00
										 |  |  |             /** @var Grav $grav */ | 
					
						
							|  |  |  |             $grav = $this->grav; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             unset($this->grav['user']); | 
					
						
							|  |  |  |             $this->grav['user'] = $user; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $this->setMessage($this->translate('PLUGIN_ADMIN.LOGIN_LOGGED_IN'), 'info'); | 
					
						
							|  |  |  |             $grav->redirect($post['redirect']); | 
					
						
							| 
									
										
										
										
											2017-02-26 19:36:01 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-08 16:42:57 +01:00
										 |  |  |             return true; //never reached
 | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-08 16:42:57 +01:00
										 |  |  |         return false; | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07: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); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * 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) { | 
					
						
							| 
									
										
										
										
											2017-02-22 21:34:21 +01:00
										 |  |  |             $languages = [$grav['user']->authenticated ? $grav['user']->language : 'en']; | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |         } else { | 
					
						
							|  |  |  |             $languages = (array)$languages; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($lookup) { | 
					
						
							|  |  |  |             if (empty($languages) || reset($languages) == null) { | 
					
						
							| 
									
										
										
										
											2017-02-22 21:34:21 +01:00
										 |  |  |                 if ($grav['config']->get('system.languages.translations_fallback', true)) { | 
					
						
							|  |  |  |                     $languages = $grav['language']->getFallbackLanguages(); | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |                 } else { | 
					
						
							| 
									
										
										
										
											2017-02-22 21:34:21 +01:00
										 |  |  |                     $languages = (array)$grav['language']->getDefault(); | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         foreach ((array)$languages as $lang) { | 
					
						
							| 
									
										
										
										
											2017-02-22 21:34:21 +01:00
										 |  |  |             $translation = $grav['language']->getTranslation($lang, $lookup); | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if (!$translation) { | 
					
						
							| 
									
										
										
										
											2017-02-22 21:34:21 +01:00
										 |  |  |                 $language    = $grav['language']->getDefault() ?: 'en'; | 
					
						
							|  |  |  |                 $translation = $grav['language']->getTranslation($language, $lookup); | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (!$translation) { | 
					
						
							|  |  |  |                 $language    = 'en'; | 
					
						
							| 
									
										
										
										
											2017-02-22 21:34:21 +01:00
										 |  |  |                 $translation = $grav['language']->getTranslation($language, $lookup); | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if ($translation) { | 
					
						
							|  |  |  |                 if (count($args) >= 1) { | 
					
						
							|  |  |  |                     return vsprintf($translation, $args); | 
					
						
							|  |  |  |                 } else { | 
					
						
							|  |  |  |                     return $translation; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $lookup; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Checks user authorisation to the action. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2015-08-11 18:30:27 -06:00
										 |  |  |      * @param  string $action | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							|  |  |  |         foreach ($action as $a) { | 
					
						
							| 
									
										
										
										
											2015-09-11 15:00:03 +02:00
										 |  |  |             if ($this->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) { | 
					
						
							| 
									
										
										
										
											2016-03-16 11:09:48 +02:00
										 |  |  |             $post = isset($_POST['data']) ? $_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])); | 
					
						
							|  |  |  |         if ($event && isset($event['data_type'])) { | 
					
						
							|  |  |  |             return $event['data_type']; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  |         } elseif (preg_match('|users/|', $type)) { | 
					
						
							|  |  |  |             $obj = User::load(preg_replace('|users/|', '', $type)); | 
					
						
							|  |  |  |             $obj->merge($post); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $data[$type] = $obj; | 
					
						
							|  |  |  |         } elseif (preg_match('|user/|', $type)) { | 
					
						
							|  |  |  |             $obj = User::load(preg_replace('|user/|', '', $type)); | 
					
						
							|  |  |  |             $obj->merge($post); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $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); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $obj = new \StdClass(); | 
					
						
							|  |  |  |             $obj->title = $file->basename(); | 
					
						
							|  |  |  |             $obj->path = $file->filename(); | 
					
						
							|  |  |  |             $obj->file = $file; | 
					
						
							|  |  |  |             $obj->page = $this->grav['pages']->get(dirname($obj->path)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $filename = pathinfo($obj->title)['filename']; | 
					
						
							| 
									
										
										
										
											2017-05-26 17:51:36 -06:00
										 |  |  |             $filename = str_replace(['@3x', '@2x'], '', $filename); | 
					
						
							| 
									
										
										
										
											2017-05-26 14:30:37 +02:00
										 |  |  |             if (isset(pathinfo($obj->title)['extension'])) { | 
					
						
							|  |  |  |                 $filename .= '.' . pathinfo($obj->title)['extension']; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if ($obj->page && isset($obj->page->media()[$filename])) { | 
					
						
							|  |  |  |                 $obj->metadata = new Data($obj->page->media()[$filename]->metadata()); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2014-10-01 22:28:16 +03:00
										 |  |  |         /** @var Pages $pages */ | 
					
						
							|  |  |  |         $pages = $this->grav['pages']; | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							|  |  |  |             $this->pages_count = count($this->grav['pages']->all()); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         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() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-11-30 19:00:12 +01:00
										 |  |  |         if (method_exists($this->grav['pages'], 'accessLevels')) { | 
					
						
							|  |  |  |             return $this->grav['pages']->accessLevels(); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             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) { | 
					
						
							| 
									
										
										
										
											2016-03-04 19:20:48 +01:00
										 |  |  |                     if (count($gpm->getPackagesThatDependOnPackage($dependency)) > 1) { | 
					
						
							|  |  |  |                         continue; | 
					
						
							| 
									
										
										
										
											2016-03-01 19:52:24 +01:00
										 |  |  |                     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-04 19:20:48 +01:00
										 |  |  |                     if (!in_array($dependency, $dependencies)) { | 
					
						
							| 
									
										
										
										
											2016-05-27 14:41:33 +02:00
										 |  |  |                         if (!in_array($dependency, ['admin', 'form', 'login', 'email'])) { | 
					
						
							|  |  |  |                             $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) { | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         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(); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             $plugins = $gpm->getRepositoryPlugins(); | 
					
						
							|  |  |  |             if ($plugins) { | 
					
						
							|  |  |  |                 return $plugins->filter(function ( | 
					
						
							|  |  |  |                     $package, | 
					
						
							|  |  |  |                     $slug | 
					
						
							|  |  |  |                 ) use ($gpm) { | 
					
						
							|  |  |  |                     return !$gpm->isPluginInstalled($slug); | 
					
						
							|  |  |  |                 }); | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 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(); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             $themes = $gpm->getRepositoryThemes(); | 
					
						
							|  |  |  |             if ($themes) { | 
					
						
							|  |  |  |                 return $themes->filter(function ( | 
					
						
							|  |  |  |                     $package, | 
					
						
							|  |  |  |                     $slug | 
					
						
							|  |  |  |                 ) use ($gpm) { | 
					
						
							|  |  |  |                     return !$gpm->isThemeInstalled($slug); | 
					
						
							|  |  |  |                 }); | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 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 | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param $packages | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @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; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-09 19:40:23 +01:00
										 |  |  |         $dependencies = $this->gpm->getDependencies($packages); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-07 11:31:18 +01:00
										 |  |  |         return $dependencies; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2014-10-01 22:28:16 +03:00
										 |  |  |         /** @var Pages $pages */ | 
					
						
							|  |  |  |         $pages = $this->grav['pages']; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-07 18:55:52 +02:00
										 |  |  |         $latest = []; | 
					
						
							| 
									
										
										
										
											2016-01-10 17:17:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-07 18:55:52 +02:00
										 |  |  |         if (is_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(); | 
					
						
							| 
									
										
										
										
											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
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |         $file    = JsonFile::instance($this->grav['locator']->findResource("log://backup.log")); | 
					
						
							|  |  |  |         $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) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2017-03-10 16:53:01 -07:00
										 |  |  |         if (isset($info['author']['name']) && ($info['author']['name'] == 'Team Grav' || Utils::contains($info['author']['name'], 'Trilby Media'))) { | 
					
						
							| 
									
										
										
										
											2015-07-27 19:33:52 -06:00
										 |  |  |             return true; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											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) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (isset($info['premium'])) { | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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(); | 
					
						
							|  |  |  |             $pinfo = ob_get_contents(); | 
					
						
							|  |  |  |             ob_end_clean(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $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; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             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 | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param $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])) { | 
					
						
							| 
									
										
										
										
											2016-08-26 17:28:55 -06:00
										 |  |  |             foreach ($date_formats as $date_format) { | 
					
						
							|  |  |  |                 foreach ($time_formats as $time_format) { | 
					
						
							|  |  |  |                     if ($this->validateDate($date, "$date_format $time_format")) { | 
					
						
							|  |  |  |                         $guess[$date] = "$date_format $time_format"; | 
					
						
							|  |  |  |                         break 2; | 
					
						
							|  |  |  |                     } elseif ($this->validateDate($date, "$time_format $date_format")) { | 
					
						
							|  |  |  |                         $guess[$date] = "$time_format $date_format"; | 
					
						
							|  |  |  |                         break 2; | 
					
						
							|  |  |  |                     } | 
					
						
							| 
									
										
										
										
											2016-05-10 13:32:17 -06:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2016-08-26 17:28:55 -06:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (!isset($guess[$date])) { | 
					
						
							| 
									
										
										
										
											2016-05-10 16:53:19 -06:00
										 |  |  |                 $guess[$date] = 'd-m-Y H:i'; | 
					
						
							| 
									
										
										
										
											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
										 |  |  |         ]; | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |         $js_format        = ""; | 
					
						
							|  |  |  |         $escaping         = false; | 
					
						
							| 
									
										
										
										
											2016-07-07 18:55:52 +02:00
										 |  |  |         for ($i = 0; $i < strlen($php_format); $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 | 
					
						
							| 
									
										
										
										
											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
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |         return $this->permissions; | 
					
						
							| 
									
										
										
										
											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
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |      * @param $permissions | 
					
						
							| 
									
										
										
										
											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
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |         $this->permissions = $permissions; | 
					
						
							| 
									
										
										
										
											2016-05-04 14:26:27 -06:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Adds a permission to the permissions array | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param $permissions | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function addPermissions($permissions) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->permissions = array_merge($this->permissions, $permissions); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-05-26 14:49:45 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-11 19:17:02 +02:00
										 |  |  |     public function processNotifications($notifications) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // Sort by date
 | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |         usort($notifications, function ($a, $b) { | 
					
						
							| 
									
										
										
										
											2016-08-11 19:17:02 +02:00
										 |  |  |             return strcmp($a->date, $b->date); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $notifications = array_reverse($notifications); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Make adminNicetimeFilter available
 | 
					
						
							|  |  |  |         require_once(__DIR__ . '/../twig/AdminTwigExtension.php'); | 
					
						
							|  |  |  |         $adminTwigExtension = new AdminTwigExtension(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-26 19:36:01 +01:00
										 |  |  |         $filename           = $this->grav['locator']->findResource('user://data/notifications/' . $this->grav['user']->username . YAML_EXT, | 
					
						
							|  |  |  |             true, true); | 
					
						
							| 
									
										
										
										
											2016-08-11 19:17:02 +02:00
										 |  |  |         $read_notifications = CompiledYamlFile::instance($filename)->content(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $notifications_processed = []; | 
					
						
							|  |  |  |         foreach ($notifications as $key => $notification) { | 
					
						
							|  |  |  |             $is_valid = true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (in_array($notification->id, $read_notifications)) { | 
					
						
							|  |  |  |                 $notification->read = true; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if ($is_valid && isset($notification->permissions) && !$this->authorize($notification->permissions)) { | 
					
						
							|  |  |  |                 $is_valid = false; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if ($is_valid && isset($notification->dependencies)) { | 
					
						
							|  |  |  |                 foreach ($notification->dependencies as $dependency => $constraints) { | 
					
						
							|  |  |  |                     if ($dependency == 'grav') { | 
					
						
							|  |  |  |                         if (!Semver::satisfies(GRAV_VERSION, $constraints)) { | 
					
						
							|  |  |  |                             $is_valid = false; | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                     } else { | 
					
						
							|  |  |  |                         $packages = array_merge($this->plugins()->toArray(), $this->themes()->toArray()); | 
					
						
							|  |  |  |                         if (!isset($packages[$dependency])) { | 
					
						
							|  |  |  |                             $is_valid = false; | 
					
						
							|  |  |  |                         } else { | 
					
						
							|  |  |  |                             $version = $packages[$dependency]['version']; | 
					
						
							|  |  |  |                             if (!Semver::satisfies($version, $constraints)) { | 
					
						
							|  |  |  |                                 $is_valid = false; | 
					
						
							|  |  |  |                             } | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     if (!$is_valid) { | 
					
						
							|  |  |  |                         break; | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if ($is_valid) { | 
					
						
							|  |  |  |                 $notifications_processed[] = $notification; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Process notifications
 | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |         $notifications_processed = array_map(function ($notification) use ($adminTwigExtension) { | 
					
						
							| 
									
										
										
										
											2016-08-11 19:17:02 +02:00
										 |  |  |             $notification->date = $adminTwigExtension->adminNicetimeFilter($notification->date); | 
					
						
							| 
									
										
										
										
											2017-02-26 19:36:01 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-11 19:17:02 +02:00
										 |  |  |             return $notification; | 
					
						
							|  |  |  |         }, $notifications_processed); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $notifications_processed; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function getPagePathFromToken($path) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2017-05-20 22:21:34 -06:00
										 |  |  |         return Utils::getPagePathFromToken($path, $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 | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Page | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     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. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param $path | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Page | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getPage($path) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         /** @var Pages $pages */ | 
					
						
							|  |  |  |         $pages = $this->grav['pages']; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($path && $path[0] != '/') { | 
					
						
							|  |  |  |             $path = "/{$path}"; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $page = $path ? $pages->dispatch($path, true) : $pages->root(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!$page) { | 
					
						
							|  |  |  |             $slug = basename($path); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if ($slug == '') { | 
					
						
							|  |  |  |                 return null; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $ppath = str_replace('\\', '/', dirname($path)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // Find or create parent(s).
 | 
					
						
							|  |  |  |             $parent = $this->getPage($ppath != '/' ? $ppath : ''); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // Create page.
 | 
					
						
							|  |  |  |             $page = new Page; | 
					
						
							|  |  |  |             $page->parent($parent); | 
					
						
							|  |  |  |             $page->filePath($parent->path() . '/' . $slug . '/' . $page->name()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // Add routing information.
 | 
					
						
							|  |  |  |             $pages->addPage($page, $path); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // Set if Modular
 | 
					
						
							|  |  |  |             $page->modularTwig($slug[0] == '_'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // 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'])) { | 
					
						
							|  |  |  |                     if ($data['visible'] == '' || $data['visible']) { | 
					
						
							|  |  |  |                         // if auto (ie '')
 | 
					
						
							|  |  |  |                         $children = $page->parent()->children(); | 
					
						
							|  |  |  |                         foreach ($children as $child) { | 
					
						
							|  |  |  |                             if ($child->order()) { | 
					
						
							|  |  |  |                                 // set page order
 | 
					
						
							| 
									
										
										
										
											2017-03-26 19:46:02 -07:00
										 |  |  |                                 $page->order(AdminController::getNextOrderInFolder($page->parent()->path())); | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |                                 break; | 
					
						
							|  |  |  |                             } | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                     if ($data['visible'] == 1 && !$page->order()) { | 
					
						
							|  |  |  |                         $header['visible'] = $data['visible']; | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if ($data['name'] == 'modular') { | 
					
						
							|  |  |  |                     $header['body_classes'] = 'modular'; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 $name = $page->modular() ? str_replace('modular/', '', $data['name']) : $data['name']; | 
					
						
							|  |  |  |                 $page->name($name . '.md'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 // Fire new event to allow plugins to manipulate page frontmatter
 | 
					
						
							|  |  |  |                 $this->grav->fireEvent('onAdminCreatePageFrontmatter', new Event(['header' => &$header])); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 $page->header($header); | 
					
						
							|  |  |  |                 $page->frontmatter(Yaml::dump((array)$page->header(), 10, 2, false)); | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 // Find out the type by looking at the parent.
 | 
					
						
							|  |  |  |                 $type = $parent->childType() | 
					
						
							|  |  |  |                     ? $parent->childType() | 
					
						
							|  |  |  |                     : $parent->blueprints()->get('child_type', | 
					
						
							| 
									
										
										
										
											2017-02-26 19:36:01 +01:00
										 |  |  |                         'default'); | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  |                 $page->name($type . CONTENT_EXT); | 
					
						
							|  |  |  |                 $page->header(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             $page->modularTwig($slug[0] == '_'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $page; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-11 19:17:02 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Get https://getgrav.org news feed | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return mixed | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getFeed() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-08-15 10:45:58 -06:00
										 |  |  |         $feed_url = 'https://getgrav.org/blog.atom'; | 
					
						
							| 
									
										
										
										
											2016-08-11 19:17:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-15 10:45:58 -06:00
										 |  |  |         $body = Response::get($feed_url); | 
					
						
							| 
									
										
										
										
											2016-08-11 19:17:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-15 10:45:58 -06:00
										 |  |  |         $reader = new Reader(); | 
					
						
							|  |  |  |         $parser = $reader->getParser($feed_url, $body, 'utf-8'); | 
					
						
							| 
									
										
										
										
											2016-08-11 19:17:02 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $feed = $parser->execute(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $feed; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @todo allow pagination | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     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 = []; | 
					
						
							|  |  |  |             $pages = $this->grav['pages']; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             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 | 
					
						
							|  |  |  |      * @param Page\Page $page | 
					
						
							|  |  |  |      * @param array $files | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private function getMediaOfType($type, $page, $page_files) { | 
					
						
							|  |  |  |         if ($page) { | 
					
						
							| 
									
										
										
										
											2017-05-26 16:15:49 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | //            $path = $page->path();
 | 
					
						
							| 
									
										
										
										
											2017-05-26 14:30:37 +02:00
										 |  |  |             $media = $page->media(); | 
					
						
							|  |  |  |             $mediaOfType = $media->$type(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             foreach($mediaOfType as $title => $file) { | 
					
						
							|  |  |  |                 $page_files[] = [ | 
					
						
							|  |  |  |                     '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() | 
					
						
							|  |  |  |                 ]; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return $page_files; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             return []; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * 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() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $pages = $this->grav['pages']->all(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $pagesWithFiles = []; | 
					
						
							|  |  |  |         if ($pages) foreach ($pages as $page) { | 
					
						
							|  |  |  |             if (count($page->media()->all())) { | 
					
						
							|  |  |  |                 $pagesWithFiles[] = $page; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $pagesWithFiles; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  | } |