| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  | <?php | 
					
						
							|  |  |  | namespace Grav\Plugin; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-11 19:57:57 -07:00
										 |  |  | use Grav\Common\Cache; | 
					
						
							| 
									
										
										
										
											2014-10-01 22:28:16 +03:00
										 |  |  | use Grav\Common\Config\Config; | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  | use Grav\Common\Filesystem\Folder; | 
					
						
							| 
									
										
										
										
											2014-08-29 11:27:53 +03:00
										 |  |  | use Grav\Common\Grav; | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  | use Grav\Common\Themes; | 
					
						
							|  |  |  | use Grav\Common\Uri; | 
					
						
							|  |  |  | use Grav\Common\Data; | 
					
						
							|  |  |  | use Grav\Common\Page; | 
					
						
							| 
									
										
										
										
											2015-04-20 20:41:34 +02:00
										 |  |  | use Grav\Common\Page\Collection; | 
					
						
							| 
									
										
										
										
											2015-04-11 16:01:27 +02:00
										 |  |  | use Grav\Common\User\User; | 
					
						
							| 
									
										
										
										
											2015-04-27 13:37:22 +02:00
										 |  |  | use Grav\Common\Utils; | 
					
						
							|  |  |  | use Grav\Common\Backup\ZipBackup; | 
					
						
							|  |  |  | use RocketTheme\Toolbox\File\JsonFile; | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | class AdminController | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-08-29 11:27:53 +03:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @var Grav | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public $grav; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @var string | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public $view; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @var string | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public $task; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @var string | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public $route; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @var array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public $post; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @var Admin | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected $admin; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @var string | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected $redirect; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @var int | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected $redirectCode; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2014-08-29 11:27:53 +03:00
										 |  |  |      * @param Grav   $grav | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |      * @param string $view | 
					
						
							|  |  |  |      * @param string $task | 
					
						
							|  |  |  |      * @param string $route | 
					
						
							|  |  |  |      * @param array  $post | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2014-08-29 11:27:53 +03:00
										 |  |  |     public function __construct(Grav $grav, $view, $task, $route, $post) | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2014-08-29 11:27:53 +03:00
										 |  |  |         $this->grav = $grav; | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |         $this->view = $view; | 
					
						
							|  |  |  |         $this->task = $task ? $task : 'display'; | 
					
						
							|  |  |  |         $this->post = $this->getPost($post); | 
					
						
							|  |  |  |         $this->route = $route; | 
					
						
							| 
									
										
										
										
											2014-08-29 11:27:53 +03:00
										 |  |  |         $this->admin = $this->grav['admin']; | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Performs a task. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function execute() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $success = false; | 
					
						
							|  |  |  |         $method = 'task' . ucfirst($this->task); | 
					
						
							|  |  |  |         if (method_exists($this, $method)) { | 
					
						
							|  |  |  |             try { | 
					
						
							|  |  |  |                 $success = call_user_func(array($this, $method)); | 
					
						
							|  |  |  |             } catch (\RuntimeException $e) { | 
					
						
							|  |  |  |                 $success = true; | 
					
						
							| 
									
										
										
										
											2015-07-22 14:01:54 -06:00
										 |  |  |                 $this->admin->setMessage($e->getMessage(), 'error'); | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2014-10-10 13:41:27 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |             // Grab redirect parameter.
 | 
					
						
							|  |  |  |             $redirect = isset($this->post['_redirect']) ? $this->post['_redirect'] : null; | 
					
						
							|  |  |  |             unset($this->post['_redirect']); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |             // Redirect if requested.
 | 
					
						
							|  |  |  |             if ($redirect) { | 
					
						
							|  |  |  |                 $this->setRedirect($redirect); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return $success; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function redirect() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (!$this->redirect) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $base = $this->admin->base; | 
					
						
							| 
									
										
										
										
											2014-12-10 10:23:22 -07:00
										 |  |  |         $path = trim(substr($this->redirect, 0, strlen($base)) == $base ? substr($this->redirect, strlen($base)) : $this->redirect, '/'); | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-29 11:27:53 +03:00
										 |  |  |         $this->grav->redirect($base . '/' . preg_replace('|/+|', '/', $path), $this->redirectCode); | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Handle login. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool True if the action was performed. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function taskLogin() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-07-21 22:02:13 -06:00
										 |  |  |         $l = $this->grav['language']; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-29 15:03:06 +03:00
										 |  |  |         if ($this->admin->authenticate($this->post)) { | 
					
						
							| 
									
										
										
										
											2015-07-22 14:01:54 -06:00
										 |  |  |             $this->admin->setMessage($l->translate('LOGIN_LOGGED_IN'), 'info'); | 
					
						
							| 
									
										
										
										
											2014-08-29 15:03:06 +03:00
										 |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2015-07-22 14:01:54 -06:00
										 |  |  |             $this->admin->setMessage($l->translate('LOGIN_FAILED'), 'error'); | 
					
						
							| 
									
										
										
										
											2014-08-29 15:03:06 +03:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Handle logout. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool True if the action was performed. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function taskLogout() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-07-21 22:02:13 -06:00
										 |  |  |         $l = $this->grav['language']; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |         $this->admin->session()->invalidate()->start(); | 
					
						
							| 
									
										
										
										
											2015-07-22 14:01:54 -06:00
										 |  |  |         $this->admin->setMessage($l->translate('LOGGED_OUT'), 'info'); | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |         $this->setRedirect('/'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-11 19:57:57 -07:00
										 |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-20 16:01:20 +02:00
										 |  |  |     protected function taskForgot() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-07-21 22:02:13 -06:00
										 |  |  |         $l = $this->grav['language']; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-20 16:01:20 +02:00
										 |  |  |         $data = $this->post; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $username = isset($data['username']) ? $data['username'] : ''; | 
					
						
							|  |  |  |         $user = !empty($username) ? User::load($username) : null; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!isset($this->grav['Email'])) { | 
					
						
							| 
									
										
										
										
											2015-07-22 14:01:54 -06:00
										 |  |  |             $this->admin->setMessage($l->translate('FORGOT_EMAIL_NOT_CONFIGURED'), 'error'); | 
					
						
							| 
									
										
										
										
											2015-04-20 16:01:20 +02:00
										 |  |  |             $this->setRedirect('/'); | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!$user || !$user->exists()) { | 
					
						
							| 
									
										
										
										
											2015-07-22 14:01:54 -06:00
										 |  |  |             $this->admin->setMessage($l->translate(['FORGOT_USERNAME_DOES_NOT_EXIST', $username]), 'error'); | 
					
						
							| 
									
										
										
										
											2015-04-20 16:01:20 +02:00
										 |  |  |             $this->setRedirect('/forgot'); | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (empty($user->email)) { | 
					
						
							| 
									
										
										
										
											2015-07-22 14:01:54 -06:00
										 |  |  |             $this->admin->setMessage($l->translate(['FORGOT_CANNOT_RESET_EMAIL_NO_EMAIL', $username]), 'error'); | 
					
						
							| 
									
										
										
										
											2015-04-20 16:01:20 +02:00
										 |  |  |             $this->setRedirect('/forgot'); | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $token = md5(uniqid(mt_rand(), true)); | 
					
						
							|  |  |  |         $expire = time() + 604800; // next week
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $user->reset = $token . '::' . $expire; | 
					
						
							|  |  |  |         $user->save(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $author = $this->grav['config']->get('site.author.name', ''); | 
					
						
							|  |  |  |         $fullname = $user->fullname ?: $username; | 
					
						
							|  |  |  |         $reset_link = rtrim($this->grav['uri']->rootUrl(true), '/') . '/' . trim($this->admin->base, '/') . '/reset/task:reset/user:' . $username . '/token:' . $token; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-21 22:02:13 -06:00
										 |  |  |         $sitename = $this->grav['config']->get('site.title', 'Website'); | 
					
						
							|  |  |  |         $from = $this->grav['config']->get('plugins.email.from', 'noreply@getgrav.org'); | 
					
						
							| 
									
										
										
										
											2015-04-20 16:01:20 +02:00
										 |  |  |         $to = $user->email; | 
					
						
							| 
									
										
										
										
											2015-07-21 22:02:13 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $subject = $l->translate(['FORGOT_EMAIL_SUBJECT', $sitename]); | 
					
						
							|  |  |  |         $content = $l->translate(['FORGOT_EMAIL_BODY', $fullname, $reset_link, $author, $sitename]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $body = $this->grav['twig']->processTemplate('email/base.html.twig', ['content' => $content]); | 
					
						
							| 
									
										
										
										
											2015-04-20 16:01:20 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $message = $this->grav['Email']->message($subject, $body, 'text/html') | 
					
						
							|  |  |  |             ->setFrom($from) | 
					
						
							|  |  |  |             ->setTo($to); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $sent = $this->grav['Email']->send($message); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($sent < 1) { | 
					
						
							| 
									
										
										
										
											2015-07-22 14:01:54 -06:00
										 |  |  |             $this->admin->setMessage($l->translate('FORGOT_FAILED_TO_EMAIL'), 'error'); | 
					
						
							| 
									
										
										
										
											2015-04-20 16:01:20 +02:00
										 |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2015-07-22 14:01:54 -06:00
										 |  |  |             $this->admin->setMessage($l->translate(['FORGOT_INSTRUCTIONS_SENT_VIA_EMAIL', $to]), 'info'); | 
					
						
							| 
									
										
										
										
											2015-04-20 16:01:20 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->setRedirect('/'); | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function taskReset() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-07-21 22:02:13 -06:00
										 |  |  |         $l = $this->grav['language']; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-20 16:01:20 +02:00
										 |  |  |         $data = $this->post; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (isset($data['password'])) { | 
					
						
							|  |  |  |             $username = isset($data['username']) ? $data['username'] : null; | 
					
						
							|  |  |  |             $user = !empty($username) ? User::load($username) : null; | 
					
						
							|  |  |  |             $password = isset($data['password']) ? $data['password'] : null; | 
					
						
							|  |  |  |             $token = isset($data['token']) ? $data['token'] : null; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (!empty($user) && $user->exists() && !empty($user->reset)) { | 
					
						
							|  |  |  |                 list($good_token, $expire) = explode('::', $user->reset); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if ($good_token === $token) { | 
					
						
							|  |  |  |                     if (time() > $expire) { | 
					
						
							| 
									
										
										
										
											2015-07-22 14:01:54 -06:00
										 |  |  |                         $this->admin->setMessage($l->translate('RESET_LINK_EXPIRED'), 'error'); | 
					
						
							| 
									
										
										
										
											2015-04-20 16:01:20 +02:00
										 |  |  |                         $this->setRedirect('/forgot'); | 
					
						
							|  |  |  |                         return true; | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     unset($user->hashed_password); | 
					
						
							|  |  |  |                     unset($user->reset); | 
					
						
							|  |  |  |                     $user->password = $password; | 
					
						
							|  |  |  |                     $user->save(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-22 14:01:54 -06:00
										 |  |  |                     $this->admin->setMessage($l->translate('RESET_PASSWORD_RESET'), 'info'); | 
					
						
							| 
									
										
										
										
											2015-04-20 16:01:20 +02:00
										 |  |  |                     $this->setRedirect('/'); | 
					
						
							|  |  |  |                     return true; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-22 14:01:54 -06:00
										 |  |  |             $this->admin->setMessage($l->translate('RESET_INVALID_LINK'), 'error'); | 
					
						
							| 
									
										
										
										
											2015-04-20 16:01:20 +02:00
										 |  |  |             $this->setRedirect('/forgot'); | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             $user = $this->grav['uri']->param('user'); | 
					
						
							|  |  |  |             $token = $this->grav['uri']->param('token'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (empty($user) || empty($token)) { | 
					
						
							| 
									
										
										
										
											2015-07-22 14:01:54 -06:00
										 |  |  |                 $this->admin->setMessage($l->translate('RESET_INVALID_LINK'), 'error'); | 
					
						
							| 
									
										
										
										
											2015-04-20 16:01:20 +02:00
										 |  |  |                 $this->setRedirect('/forgot'); | 
					
						
							|  |  |  |                 return true; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $this->admin->forgot = [ 'username' => $user, 'token' => $token ]; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-11 19:57:57 -07:00
										 |  |  |     protected function taskClearCache() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-05-11 16:37:43 +02:00
										 |  |  |         if (!$this->authoriseTask('clear cache', ['admin.cache', 'admin.super'])) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-11 19:57:57 -07:00
										 |  |  |         $results = Cache::clearCache('standard'); | 
					
						
							|  |  |  |         if (count($results) > 0) { | 
					
						
							| 
									
										
										
										
											2015-05-11 16:37:43 +02:00
										 |  |  |             $this->admin->json_response = ['status' => 'success', 'message' => 'Cache cleared']; | 
					
						
							| 
									
										
										
										
											2014-12-11 19:57:57 -07:00
										 |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2015-05-11 16:37:43 +02:00
										 |  |  |             $this->admin->json_response = ['status' => 'error', 'message' => 'Error clearing cache']; | 
					
						
							| 
									
										
										
										
											2014-12-11 19:57:57 -07:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-27 13:37:22 +02:00
										 |  |  |     protected function taskBackup() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-05-11 16:37:43 +02:00
										 |  |  |         if (!$this->authoriseTask('backup', ['admin.maintenance', 'admin.super'])) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-27 13:37:22 +02:00
										 |  |  |         $download = $this->grav['uri']->param('download'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($download) { | 
					
						
							|  |  |  |             Utils::download(base64_decode(urldecode($download)), true); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $log = JsonFile::instance($this->grav['locator']->findResource("log://backup.log", true, true)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-04 11:53:29 +02:00
										 |  |  |         try { | 
					
						
							|  |  |  |             $backup = ZipBackup::backup(); | 
					
						
							|  |  |  |         } catch (\Exception $e) { | 
					
						
							|  |  |  |             $this->admin->json_response = [ | 
					
						
							|  |  |  |                 'status' => 'error', | 
					
						
							|  |  |  |                 'message' => 'An error occured. '.  $e->getMessage() | 
					
						
							|  |  |  |             ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-27 13:37:22 +02:00
										 |  |  |         $download = urlencode(base64_encode($backup)); | 
					
						
							|  |  |  |         $url = rtrim($this->grav['uri']->rootUrl(true), '/') . '/' . trim($this->admin->base, '/') . '/task:backup/download:' . $download; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $log->content([ | 
					
						
							|  |  |  |             'time' => time(), | 
					
						
							|  |  |  |             'location' => $backup | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  |         $log->save(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->admin->json_response = [ | 
					
						
							|  |  |  |             'status' => 'success', | 
					
						
							|  |  |  |             'message' => 'Your backup is ready for download. <a href="'.$url.'" class="button">Download backup</a>', | 
					
						
							|  |  |  |             'toastr' => [ | 
					
						
							|  |  |  |                 'timeOut' => 0, | 
					
						
							|  |  |  |                 'closeButton' => true | 
					
						
							|  |  |  |             ] | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-20 20:41:34 +02:00
										 |  |  |     protected function taskFilterPages() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-05-11 16:37:43 +02:00
										 |  |  |         if (!$this->authoriseTask('filter pages', ['admin.pages', 'admin.super'])) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-20 20:41:34 +02:00
										 |  |  |         $data = $this->post; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $flags = !empty($data['flags']) ? array_map('strtolower', explode(',', $data['flags'])) : []; | 
					
						
							|  |  |  |         $queries = !empty($data['query']) ? explode(',', $data['query']) : []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $collection = $this->grav['pages']->all(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (count($flags)) { | 
					
						
							|  |  |  |             if (in_array('modular', $flags)) | 
					
						
							|  |  |  |                 $collection = $collection->modular(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (in_array('visible', $flags)) | 
					
						
							|  |  |  |                 $collection = $collection->visible(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (in_array('routable', $flags)) | 
					
						
							|  |  |  |                 $collection = $collection->routable(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!empty($queries)) { | 
					
						
							|  |  |  |             foreach ($collection as $page) { | 
					
						
							|  |  |  |                 foreach ($queries as $query) { | 
					
						
							|  |  |  |                     $query = trim($query); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     // $page->content();
 | 
					
						
							|  |  |  |                     if (stripos($page->getRawContent(), $query) === false && stripos($page->title(), $query) === false) { | 
					
						
							|  |  |  |                         $collection->remove($page); | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-11 16:37:43 +02:00
										 |  |  |         $results = []; | 
					
						
							|  |  |  |         foreach ($collection as $path => $page) { | 
					
						
							|  |  |  |             $results[] = $page->route(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->admin->json_response = [ | 
					
						
							|  |  |  |             'status' => 'success', | 
					
						
							|  |  |  |             'message' => 'Pages filtered', | 
					
						
							|  |  |  |             'results' => $results | 
					
						
							|  |  |  |         ]; | 
					
						
							| 
									
										
										
										
											2015-04-20 20:41:34 +02:00
										 |  |  |         $this->admin->collection = $collection; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-22 15:49:53 -06:00
										 |  |  |     protected function taskListmedia() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-05-11 16:37:43 +02:00
										 |  |  |         if (!$this->authoriseTask('list media', ['admin.pages', 'admin.super'])) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-22 15:49:53 -06:00
										 |  |  |         $page = $this->admin->page(true); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-22 17:13:19 -06:00
										 |  |  |         if (!$page) { | 
					
						
							| 
									
										
										
										
											2015-05-11 16:37:43 +02:00
										 |  |  |             $this->admin->json_response = ['status' => 'error', 'message' => 'No Page found']; | 
					
						
							| 
									
										
										
										
											2014-10-01 22:28:16 +03:00
										 |  |  |             return false; | 
					
						
							| 
									
										
										
										
											2014-09-22 17:13:19 -06:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $media_list = array(); | 
					
						
							| 
									
										
										
										
											2014-12-10 10:23:22 -07:00
										 |  |  |         foreach ($page->media()->all() as $name => $media) { | 
					
						
							|  |  |  |             $media_list[$name] = ['url' => $media->cropZoom(150, 100)->url(),'size' => $media->get('size')]; | 
					
						
							| 
									
										
										
										
											2014-09-22 15:49:53 -06:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-05-11 16:37:43 +02:00
										 |  |  |         $this->admin->json_response = ['status' => 'ok', 'results' => $media_list]; | 
					
						
							| 
									
										
										
										
											2014-09-22 15:49:53 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-22 16:35:11 -06:00
										 |  |  |     protected function taskAddmedia() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-05-11 16:37:43 +02:00
										 |  |  |         if (!$this->authoriseTask('add media', ['admin.pages', 'admin.super'])) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-22 16:35:11 -06:00
										 |  |  |         $page = $this->admin->page(true); | 
					
						
							| 
									
										
										
										
											2014-10-01 22:28:16 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |         /** @var Config $config */ | 
					
						
							| 
									
										
										
										
											2014-09-22 16:35:11 -06:00
										 |  |  |         $config = $this->grav['config']; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-07 15:32:07 -06:00
										 |  |  |         if (!isset($_FILES['file']['error']) || is_array($_FILES['file']['error'])) { | 
					
						
							| 
									
										
										
										
											2015-05-11 16:37:43 +02:00
										 |  |  |             $this->admin->json_response = ['status' => 'error', 'message' => 'Invalid Parameters']; | 
					
						
							| 
									
										
										
										
											2014-10-07 15:32:07 -06:00
										 |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2014-09-22 16:35:11 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-07 15:32:07 -06:00
										 |  |  |         // Check $_FILES['file']['error'] value.
 | 
					
						
							|  |  |  |         switch ($_FILES['file']['error']) { | 
					
						
							|  |  |  |             case UPLOAD_ERR_OK: | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case UPLOAD_ERR_NO_FILE: | 
					
						
							| 
									
										
										
										
											2015-05-11 16:37:43 +02:00
										 |  |  |                 $this->admin->json_response = ['status' => 'error', 'message' => 'No files sent']; | 
					
						
							| 
									
										
										
										
											2014-09-22 16:35:11 -06:00
										 |  |  |                 return; | 
					
						
							| 
									
										
										
										
											2014-10-07 15:32:07 -06:00
										 |  |  |             case UPLOAD_ERR_INI_SIZE: | 
					
						
							|  |  |  |             case UPLOAD_ERR_FORM_SIZE: | 
					
						
							| 
									
										
										
										
											2015-05-11 16:37:43 +02:00
										 |  |  |                 $this->admin->json_response = ['status' => 'error', 'message' => 'Exceeded filesize limit.']; | 
					
						
							| 
									
										
										
										
											2014-10-07 15:32:07 -06:00
										 |  |  |                 return; | 
					
						
							|  |  |  |             default: | 
					
						
							| 
									
										
										
										
											2015-05-11 16:37:43 +02:00
										 |  |  |                 $this->admin->json_response = ['status' => 'error', 'message' => 'Unkown errors']; | 
					
						
							| 
									
										
										
										
											2014-10-07 15:32:07 -06:00
										 |  |  |                 return; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2014-09-22 16:35:11 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-27 19:33:48 +02:00
										 |  |  |         $grav_limit = $config->get('system.media.upload_limit', 0); | 
					
						
							| 
									
										
										
										
											2014-10-07 15:32:07 -06:00
										 |  |  |         // You should also check filesize here.
 | 
					
						
							| 
									
										
										
										
											2015-04-27 19:33:48 +02:00
										 |  |  |         if ($grav_limit > 0 && $_FILES['file']['size'] > grav_limit) { | 
					
						
							| 
									
										
										
										
											2015-05-11 16:37:43 +02:00
										 |  |  |             $this->admin->json_response = ['status' => 'error', 'message' => 'Exceeded Grav filesize limit.']; | 
					
						
							| 
									
										
										
										
											2014-10-07 15:32:07 -06:00
										 |  |  |             return; | 
					
						
							| 
									
										
										
										
											2014-09-22 16:35:11 -06:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2014-10-07 15:32:07 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Check extension
 | 
					
						
							|  |  |  |         $fileParts = pathinfo($_FILES['file']['name']); | 
					
						
							|  |  |  |         $fileExt = strtolower($fileParts['extension']); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // If not a supported type, return
 | 
					
						
							|  |  |  |         if (!$config->get("media.{$fileExt}")) { | 
					
						
							| 
									
										
										
										
											2015-05-11 16:37:43 +02:00
										 |  |  |             $this->admin->json_response = ['status' => 'error', 'message' => 'Unsupported file type: '.$fileExt]; | 
					
						
							| 
									
										
										
										
											2014-10-07 15:32:07 -06:00
										 |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Upload it
 | 
					
						
							| 
									
										
										
										
											2014-12-10 10:23:22 -07:00
										 |  |  |         if (!move_uploaded_file($_FILES['file']['tmp_name'], sprintf('%s/%s', $page->path(), $_FILES['file']['name']))) { | 
					
						
							| 
									
										
										
										
											2015-05-11 16:37:43 +02:00
										 |  |  |             $this->admin->json_response = ['status' => 'error', 'message' => 'Failed to move uploaded file.']; | 
					
						
							| 
									
										
										
										
											2014-10-07 15:32:07 -06:00
										 |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-11 16:37:43 +02:00
										 |  |  |         $this->admin->json_response = ['status' => 'success', 'message' => 'File uploaded successfully']; | 
					
						
							| 
									
										
										
										
											2014-10-07 15:32:07 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-22 17:13:19 -06:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     protected function taskDelmedia() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-05-11 16:37:43 +02:00
										 |  |  |         if (!$this->authoriseTask('delete media', ['admin.pages', 'admin.super'])) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-22 17:27:48 -06:00
										 |  |  |         $page = $this->admin->page(true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!$page) { | 
					
						
							| 
									
										
										
										
											2015-05-11 16:37:43 +02:00
										 |  |  |             $this->admin->json_response = ['status' => 'error', 'message' => 'No Page found']; | 
					
						
							| 
									
										
										
										
											2014-10-01 22:28:16 +03:00
										 |  |  |             return false; | 
					
						
							| 
									
										
										
										
											2014-09-22 17:27:48 -06:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2014-09-22 17:13:19 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-22 17:27:48 -06:00
										 |  |  |         $filename = !empty($this->post['filename']) ? $this->post['filename'] : null; | 
					
						
							|  |  |  |         if ($filename) { | 
					
						
							|  |  |  |             $targetPath = $page->path().'/'.$filename; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-22 22:43:59 -06:00
										 |  |  |             if (file_exists($targetPath)) { | 
					
						
							| 
									
										
										
										
											2014-12-10 10:23:22 -07:00
										 |  |  |                 if (unlink($targetPath)) { | 
					
						
							| 
									
										
										
										
											2015-05-11 16:37:43 +02:00
										 |  |  |                     $this->admin->json_response = ['status' => 'success', 'message' => 'File deleted: '.$filename]; | 
					
						
							| 
									
										
										
										
											2014-09-22 22:43:59 -06:00
										 |  |  |                 } else { | 
					
						
							| 
									
										
										
										
											2015-05-11 16:37:43 +02:00
										 |  |  |                     $this->admin->json_response = ['status' => 'error', 'message' => 'File could not be deleted: '.$filename]; | 
					
						
							| 
									
										
										
										
											2014-09-22 22:43:59 -06:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2014-09-22 17:27:48 -06:00
										 |  |  |             } else { | 
					
						
							| 
									
										
										
										
											2015-05-11 16:37:43 +02:00
										 |  |  |                 $this->admin->json_response = ['status' => 'error', 'message' => 'File not found: '.$filename]; | 
					
						
							| 
									
										
										
										
											2014-09-22 17:27:48 -06:00
										 |  |  |             } | 
					
						
							|  |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2015-05-11 16:37:43 +02:00
										 |  |  |             $this->admin->json_response = ['status' => 'error', 'message' => 'No file found']; | 
					
						
							| 
									
										
										
										
											2014-09-22 17:27:48 -06:00
										 |  |  |         } | 
					
						
							|  |  |  |         return true; | 
					
						
							| 
									
										
										
										
											2014-09-22 16:35:11 -06:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Enable plugin. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool True if the action was performed. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function taskEnable() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-05-11 16:37:43 +02:00
										 |  |  |         if (!$this->authoriseTask('enable plugin', ['admin.plugins', 'admin.super'])) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |         if ($this->view != 'plugins') { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Filter value and save it.
 | 
					
						
							| 
									
										
										
										
											2014-10-10 13:41:27 +03:00
										 |  |  |         $this->post = array('enabled' => 1, '_redirect' => 'plugins'); | 
					
						
							|  |  |  |         $obj = $this->prepareData(); | 
					
						
							|  |  |  |         $obj->save(); | 
					
						
							| 
									
										
										
										
											2015-07-22 14:01:54 -06:00
										 |  |  |         $this->admin->setMessage('Successfully enabled plugin', 'info'); | 
					
						
							| 
									
										
										
										
											2014-10-10 13:41:27 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Enable plugin. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool True if the action was performed. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function taskDisable() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-05-11 16:37:43 +02:00
										 |  |  |         if (!$this->authoriseTask('disable plugin', ['admin.plugins', 'admin.super'])) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-10 13:41:27 +03:00
										 |  |  |         if ($this->view != 'plugins') { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Filter value and save it.
 | 
					
						
							|  |  |  |         $this->post = array('enabled' => 0, '_redirect' => 'plugins'); | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |         $obj = $this->prepareData(); | 
					
						
							|  |  |  |         $obj->save(); | 
					
						
							| 
									
										
										
										
											2015-07-22 14:01:54 -06:00
										 |  |  |         $this->admin->setMessage('Successfully disabled plugin', 'info'); | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Set default theme. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool True if the action was performed. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2014-10-10 13:46:54 +03:00
										 |  |  |     public function taskActivate() | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-05-11 16:37:43 +02:00
										 |  |  |         if (!$this->authoriseTask('activate theme', ['admin.themes', 'admin.super'])) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |         if ($this->view != 'themes') { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-10 13:49:13 +03:00
										 |  |  |         $this->post = array('_redirect' => 'themes'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |         // Make sure theme exists (throws exception)
 | 
					
						
							| 
									
										
										
										
											2014-10-10 13:46:54 +03:00
										 |  |  |         $name = $this->route; | 
					
						
							| 
									
										
										
										
											2014-08-29 15:03:06 +03:00
										 |  |  |         $this->grav['themes']->get($name); | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // Store system configuration.
 | 
					
						
							|  |  |  |         $system = $this->admin->data('system'); | 
					
						
							|  |  |  |         $system->set('pages.theme', $name); | 
					
						
							|  |  |  |         $system->save(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Force configuration reload and save.
 | 
					
						
							|  |  |  |         /** @var Config $config */ | 
					
						
							| 
									
										
										
										
											2014-08-29 11:27:53 +03:00
										 |  |  |         $config = $this->grav['config']; | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |         $config->reload()->save(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // TODO: find out why reload and save doesn't always update the object itself (and remove this workaround).
 | 
					
						
							|  |  |  |         $config->set('system.pages.theme', $name); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-22 14:01:54 -06:00
										 |  |  |         $this->admin->setMessage('Successfully changed default theme.', 'info'); | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-04 14:49:40 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Handles installing plugins and themes | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool True is the action was performed | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function taskInstall() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-05-11 16:37:43 +02:00
										 |  |  |         $type = $this->view === 'plugins' ? 'plugins' : 'themes'; | 
					
						
							|  |  |  |         if (!$this->authoriseTask('install ' . $type, ['admin.' . $type, 'admin.super'])) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-13 21:37:12 +02:00
										 |  |  |         require_once __DIR__ . '/gpm.php'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $package = $this->route; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $result = \Grav\Plugin\Admin\Gpm::install($package, []); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($result) { | 
					
						
							| 
									
										
										
										
											2015-07-22 14:01:54 -06:00
										 |  |  |             $this->admin->setMessage("Installation successful.", 'info'); | 
					
						
							| 
									
										
										
										
											2015-04-13 21:37:12 +02:00
										 |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2015-07-22 14:01:54 -06:00
										 |  |  |             $this->admin->setMessage("Installation failed.", 'error'); | 
					
						
							| 
									
										
										
										
											2015-04-13 21:37:12 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->post = array('_redirect' => $this->view . '/' . $this->route); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Handles updating plugins and themes | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool True is the action was performed | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function taskUpdate() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         require_once __DIR__ . '/gpm.php'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $package = $this->route; | 
					
						
							| 
									
										
										
										
											2015-05-11 16:37:43 +02:00
										 |  |  |         $permissions = []; | 
					
						
							| 
									
										
										
										
											2015-04-13 21:37:12 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // Update multi mode
 | 
					
						
							|  |  |  |         if (!$package) { | 
					
						
							|  |  |  |             $package = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if ($this->view === 'plugins' || $this->view === 'update') { | 
					
						
							|  |  |  |                 $package = $this->admin->gpm()->getUpdatablePlugins(); | 
					
						
							| 
									
										
										
										
											2015-05-11 16:37:43 +02:00
										 |  |  |                 $permissions['plugins'] = ['admin.super', 'admin.plugins']; | 
					
						
							| 
									
										
										
										
											2015-04-13 21:37:12 +02:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if ($this->view === 'themes' || $this->view === 'update') { | 
					
						
							|  |  |  |                 $package = array_merge($package, $this->admin->gpm()->getUpdatableThemes()); | 
					
						
							| 
									
										
										
										
											2015-05-11 16:37:43 +02:00
										 |  |  |                 $permissions['themes'] = ['admin.super', 'admin.themes']; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         foreach ($permissions as $type => $p) { | 
					
						
							|  |  |  |             if (!$this->authoriseTask('update ' . $type , $p)) { | 
					
						
							|  |  |  |                 return; | 
					
						
							| 
									
										
										
										
											2015-04-13 21:37:12 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $result = \Grav\Plugin\Admin\Gpm::update($package, []); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($this->view === 'update') { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if ($result) { | 
					
						
							| 
									
										
										
										
											2015-05-11 16:37:43 +02:00
										 |  |  |                 $this->admin->json_response = ['status' => 'success', 'message' => 'Everything updated']; | 
					
						
							| 
									
										
										
										
											2015-04-13 21:37:12 +02:00
										 |  |  |             } else { | 
					
						
							| 
									
										
										
										
											2015-05-11 16:37:43 +02:00
										 |  |  |                 $this->admin->json_response = ['status' => 'error', 'message' => 'Updates failed']; | 
					
						
							| 
									
										
										
										
											2015-04-13 21:37:12 +02:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             if ($result) { | 
					
						
							| 
									
										
										
										
											2015-07-22 14:01:54 -06:00
										 |  |  |                 $this->admin->setMessage("Installation successful.", 'info'); | 
					
						
							| 
									
										
										
										
											2015-04-13 21:37:12 +02:00
										 |  |  |             } else { | 
					
						
							| 
									
										
										
										
											2015-07-22 14:01:54 -06:00
										 |  |  |                 $this->admin->setMessage("Installation failed.", 'error'); | 
					
						
							| 
									
										
										
										
											2015-04-13 21:37:12 +02:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $this->post = array('_redirect' => $this->view . '/' . $this->route); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Handles uninstalling plugins and themes | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool True is the action was performed | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function taskUninstall() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-05-11 16:37:43 +02:00
										 |  |  |         $type = $this->view === 'plugins' ? 'plugins' : 'themes'; | 
					
						
							|  |  |  |         if (!$this->authoriseTask('uninstall ' . $type, ['admin.' . $type, 'admin.super'])) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-13 21:37:12 +02:00
										 |  |  |         require_once __DIR__ . '/gpm.php'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-04 14:49:40 +02:00
										 |  |  |         $package = $this->route; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-13 21:37:12 +02:00
										 |  |  |         $result = \Grav\Plugin\Admin\Gpm::uninstall($package, []); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($result) { | 
					
						
							| 
									
										
										
										
											2015-07-22 14:01:54 -06:00
										 |  |  |             $this->admin->setMessage("Uninstall successful.", 'info'); | 
					
						
							| 
									
										
										
										
											2015-04-13 21:37:12 +02:00
										 |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2015-07-22 14:01:54 -06:00
										 |  |  |             $this->admin->setMessage("Uninstall failed.", 'error'); | 
					
						
							| 
									
										
										
										
											2015-04-13 21:37:12 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->post = array('_redirect' => $this->view); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-04 14:49:40 +02:00
										 |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Handles form and saves the input data if its valid. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool True if the action was performed. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function taskSave() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-05-11 16:37:43 +02:00
										 |  |  |         if (!$this->authoriseTask('save', $this->dataPermissions())) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |         $data = $this->post; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Special handler for pages data.
 | 
					
						
							|  |  |  |         if ($this->view == 'pages') { | 
					
						
							|  |  |  |             /** @var Page\Pages $pages */ | 
					
						
							| 
									
										
										
										
											2014-08-29 11:27:53 +03:00
										 |  |  |             $pages = $this->grav['pages']; | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |             // Find new parent page in order to build the path.
 | 
					
						
							|  |  |  |             $route = !isset($data['route']) ? dirname($this->admin->route) : $data['route']; | 
					
						
							| 
									
										
										
										
											2014-09-26 14:10:32 +03:00
										 |  |  |             $parent = $route && $route != '/' ? $pages->dispatch($route, true) : $pages->root(); | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |             $obj = $this->admin->page(true); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-13 14:29:17 +02:00
										 |  |  |             $original_slug = $obj->slug(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |             // Change parent if needed and initialize move (might be needed also on ordering/folder change).
 | 
					
						
							|  |  |  |             $obj = $obj->move($parent); | 
					
						
							|  |  |  |             $this->preparePage($obj); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // Reset slug and route. For now we do not support slug twig variable on save.
 | 
					
						
							| 
									
										
										
										
											2015-05-13 14:29:17 +02:00
										 |  |  |             $obj->slug($original_slug); | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             // Handle standard data types.
 | 
					
						
							|  |  |  |             $obj = $this->prepareData(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if ($obj) { | 
					
						
							|  |  |  |             $obj->validate(); | 
					
						
							|  |  |  |             $obj->filter(); | 
					
						
							|  |  |  |             $obj->save(); | 
					
						
							| 
									
										
										
										
											2015-07-22 14:01:54 -06:00
										 |  |  |             $this->admin->setMessage('Successfully saved', 'info'); | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-07 12:12:21 +03:00
										 |  |  |         if ($this->view != 'pages') { | 
					
						
							|  |  |  |             // Force configuration reload.
 | 
					
						
							|  |  |  |             /** @var Config $config */ | 
					
						
							|  |  |  |             $config = $this->grav['config']; | 
					
						
							|  |  |  |             $config->reload(); | 
					
						
							| 
									
										
										
										
											2015-04-11 16:01:27 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if ($this->view === 'users') { | 
					
						
							|  |  |  |                 $this->grav['user']->merge(User::load($this->admin->route)->toArray()); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2014-10-07 12:12:21 +03:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-13 16:25:02 +02:00
										 |  |  |         // Always redirect if a page was change, to refresh it
 | 
					
						
							|  |  |  |         if ($obj instanceof Page\Page) { | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |             $this->setRedirect($this->view . '/' . $obj->route()); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Continue to the new page. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool True if the action was performed. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function taskContinue() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2014-10-10 15:25:07 +03:00
										 |  |  |         if ($this->view == 'users') { | 
					
						
							|  |  |  |             $this->setRedirect("{$this->view}/{$this->post['username']}"); | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |         if ($this->view != 'pages') { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $data = $this->post; | 
					
						
							| 
									
										
										
										
											2014-09-17 11:54:57 +03:00
										 |  |  |         $route = $data['route'] != '/' ? $data['route'] : ''; | 
					
						
							| 
									
										
										
										
											2014-10-10 11:57:57 +03:00
										 |  |  |         $folder = ltrim($data['folder'], '_'); | 
					
						
							|  |  |  |         if (!empty($data['modular'])) { | 
					
						
							|  |  |  |             $folder = '_' . $folder; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |         $path = $route . '/' . $folder; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-17 11:54:57 +03:00
										 |  |  |         $this->admin->session()->{$path} = $data; | 
					
						
							|  |  |  |         $this->setRedirect("{$this->view}/{$path}"); | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Save page as a new copy. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool True if the action was performed. | 
					
						
							|  |  |  |      * @throws \RuntimeException | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function taskCopy() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-05-11 16:37:43 +02:00
										 |  |  |         if (!$this->authoriseTask('copy page', ['admin.pages', 'admin.super'])) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |         // Only applies to pages.
 | 
					
						
							|  |  |  |         if ($this->view != 'pages') { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             /** @var Page\Pages $pages */ | 
					
						
							| 
									
										
										
										
											2014-08-29 11:27:53 +03:00
										 |  |  |             $pages = $this->grav['pages']; | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |             $data = $this->post; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // Find new parent page in order to build the path.
 | 
					
						
							| 
									
										
										
										
											2015-04-27 21:58:10 -06:00
										 |  |  |             $route = trim($data['route'], '/'); | 
					
						
							|  |  |  |             $parent = empty($route) ? $pages->root() : $pages->dispatch($data['route'], true); | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |             // And then get the current page.
 | 
					
						
							|  |  |  |             $page = $this->admin->page(true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // Make a copy of the current page and fill the updated information into it.
 | 
					
						
							|  |  |  |             $page = $page->copy($parent); | 
					
						
							|  |  |  |             $this->preparePage($page); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-27 17:43:58 +02:00
										 |  |  |             // Make sure the header is loaded in case content was set through raw() (expert mode)
 | 
					
						
							|  |  |  |             $page->header(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |             // Deal with folder naming conflicts, but limit number of searches to 99.
 | 
					
						
							|  |  |  |             $break = 99; | 
					
						
							|  |  |  |             while ($break > 0 && file_exists($page->filePath())) { | 
					
						
							|  |  |  |                 $break--; | 
					
						
							|  |  |  |                 $match = preg_split('/-(\d+)$/', $page->path(), 2, PREG_SPLIT_DELIM_CAPTURE); | 
					
						
							|  |  |  |                 $page->path($match[0] . '-' . (isset($match[1]) ? (int) $match[1] + 1 : 2)); | 
					
						
							|  |  |  |                 // Reset slug and route. For now we do not support slug twig variable on save.
 | 
					
						
							|  |  |  |                 $page->slug(''); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // Validation, type filtering and saving the changes.
 | 
					
						
							|  |  |  |             $page->validate(); | 
					
						
							|  |  |  |             $page->filter(); | 
					
						
							|  |  |  |             $page->save(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // Enqueue message and redirect to new location.
 | 
					
						
							| 
									
										
										
										
											2015-07-22 14:01:54 -06:00
										 |  |  |             $this->admin->setMessage('Successfully copied', 'info'); | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |             $this->setRedirect($this->view . '/' . $page->route()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         } catch (\Exception $e) { | 
					
						
							|  |  |  |             throw new \RuntimeException('Copying page failed on error: ' . $e->getMessage()); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Reorder pages. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool True if the action was performed. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function taskReorder() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-05-11 16:37:43 +02:00
										 |  |  |         if (!$this->authoriseTask('reorder pages', ['admin.pages', 'admin.super'])) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |         // Only applies to pages.
 | 
					
						
							|  |  |  |         if ($this->view != 'pages') { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-22 14:01:54 -06:00
										 |  |  |         $this->admin->setMessage('Reordering was successful', 'info'); | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Delete page. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool True if the action was performed. | 
					
						
							|  |  |  |      * @throws \RuntimeException | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function taskDelete() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-05-11 16:37:43 +02:00
										 |  |  |         if (!$this->authoriseTask('delete page', ['admin.pages', 'admin.super'])) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |         // Only applies to pages.
 | 
					
						
							|  |  |  |         if ($this->view != 'pages') { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** @var Uri $uri */ | 
					
						
							| 
									
										
										
										
											2014-08-29 11:27:53 +03:00
										 |  |  |         $uri = $this->grav['uri']; | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             $page = $this->admin->page(); | 
					
						
							|  |  |  |             Folder::delete($page->path()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-30 17:41:45 -06:00
										 |  |  |             // Set redirect to either referrer or pages list.
 | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |             $redirect = $uri->referrer(); | 
					
						
							|  |  |  |             if ($redirect == $uri->route()) { | 
					
						
							| 
									
										
										
										
											2014-09-30 17:41:45 -06:00
										 |  |  |                 $redirect = 'pages'; | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-22 14:01:54 -06:00
										 |  |  |             $this->admin->setMessage('Successfully deleted', 'info'); | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |             $this->setRedirect($redirect); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         } catch (\Exception $e) { | 
					
						
							|  |  |  |             throw new \RuntimeException('Deleting page failed on error: ' . $e->getMessage()); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Prepare and return POST data. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param array $post | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function &getPost($post) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         unset($post['task']); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Decode JSON encoded fields and merge them to data.
 | 
					
						
							|  |  |  |         if (isset($post['_json'])) { | 
					
						
							|  |  |  |             $post = array_merge_recursive($post, $this->jsonDecode($post['_json'])); | 
					
						
							|  |  |  |             unset($post['_json']); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return $post; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Recursively JSON decode data. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param  array $data | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function jsonDecode(array $data) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         foreach ($data as &$value) { | 
					
						
							|  |  |  |             if (is_array($value)) { | 
					
						
							|  |  |  |                 $value = $this->jsonDecode($value); | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 $value = json_decode($value, true); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return $data; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-10 10:23:22 -07:00
										 |  |  |     protected function setRedirect($path, $code = 303) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |         $this->redirect = $path; | 
					
						
							|  |  |  |         $this->code = $code; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     protected function prepareData() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $type = trim("{$this->view}/{$this->admin->route}", '/'); | 
					
						
							|  |  |  |         $data = $this->admin->data($type, $this->post); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $data; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-11 16:37:43 +02:00
										 |  |  |     protected function dataPermissions() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $type = $this->view; | 
					
						
							|  |  |  |         $permissions = ['admin.super']; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         switch ($type) { | 
					
						
							|  |  |  |             case 'configuration': | 
					
						
							|  |  |  |             case 'system': | 
					
						
							| 
									
										
										
										
											2015-05-13 18:47:52 +02:00
										 |  |  |                 $permissions[] = ['admin.configuration']; | 
					
						
							| 
									
										
										
										
											2015-05-11 16:37:43 +02:00
										 |  |  |                 break; | 
					
						
							|  |  |  |             case 'settings': | 
					
						
							|  |  |  |             case 'site': | 
					
						
							| 
									
										
										
										
											2015-05-13 18:47:52 +02:00
										 |  |  |                 $permissions[] = ['admin.settings']; | 
					
						
							| 
									
										
										
										
											2015-05-11 16:37:43 +02:00
										 |  |  |                 break; | 
					
						
							|  |  |  |             case 'plugins': | 
					
						
							| 
									
										
										
										
											2015-05-13 18:47:52 +02:00
										 |  |  |                 $permissions[] = ['admin.plugins']; | 
					
						
							| 
									
										
										
										
											2015-05-11 16:37:43 +02:00
										 |  |  |                 break; | 
					
						
							|  |  |  |             case 'themes': | 
					
						
							| 
									
										
										
										
											2015-05-13 18:47:52 +02:00
										 |  |  |                 $permissions[] = ['admin.themes']; | 
					
						
							| 
									
										
										
										
											2015-05-11 16:37:43 +02:00
										 |  |  |                 break; | 
					
						
							|  |  |  |             case 'users': | 
					
						
							| 
									
										
										
										
											2015-05-13 18:47:52 +02:00
										 |  |  |                 $permissions[] = ['admin.users']; | 
					
						
							| 
									
										
										
										
											2015-05-11 16:37:43 +02:00
										 |  |  |                 break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $permissions; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |     protected function preparePage(\Grav\Common\Page\Page $page) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $input = $this->post; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $order = max(0, (int) isset($input['order']) ? $input['order'] : $page->value('order')); | 
					
						
							|  |  |  |         $ordering = $order ? sprintf('%02d.', $order) : ''; | 
					
						
							|  |  |  |         $slug = empty($input['folder']) ? $page->value('folder') : (string) $input['folder']; | 
					
						
							|  |  |  |         $page->folder($ordering . $slug); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (isset($input['type'])) { | 
					
						
							| 
									
										
										
										
											2014-10-08 19:55:50 +03:00
										 |  |  |             $type = (string) $input['type']; | 
					
						
							|  |  |  |             $name = preg_replace('|.*/|', '', $type) . '.md'; | 
					
						
							|  |  |  |             $page->name($name); | 
					
						
							|  |  |  |             $page->template($type); | 
					
						
							| 
									
										
										
										
											2014-10-08 15:14:46 +03:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-20 15:34:35 -06:00
										 |  |  |         // special case for Expert mode build the raw, unset content
 | 
					
						
							|  |  |  |         if (isset($input['frontmatter']) && isset($input['content'])) { | 
					
						
							|  |  |  |             $page->raw("---\n" . (string) $input['frontmatter'] . "\n---\n" . (string) $input['content']); | 
					
						
							|  |  |  |             unset($input['content']); | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (isset($input['header'])) { | 
					
						
							|  |  |  |             $page->header((object) $input['header']); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         // Fill content last because of it also renders the output.
 | 
					
						
							|  |  |  |         if (isset($input['content'])) { | 
					
						
							| 
									
										
										
										
											2015-05-13 18:53:15 +02:00
										 |  |  |             $page->rawMarkdown((string) $input['content']); | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-05-11 16:37:43 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     protected function authoriseTask($task = '', $permissions = []) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (!$this->admin->authorise($permissions)) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if ($this->grav['uri']->extension() === 'json') | 
					
						
							|  |  |  |                 $this->admin->json_response = ['status' => 'unauthorized', 'message' => 'You have insufficient permissions for task ' . $task . '.']; | 
					
						
							|  |  |  |             else | 
					
						
							| 
									
										
										
										
											2015-07-22 14:01:54 -06:00
										 |  |  |                 $this->admin->setMessage('You have insufficient permissions for task ' . $task . '.', 'error'); | 
					
						
							| 
									
										
										
										
											2015-05-11 16:37:43 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  | } |