Beispiel #1
0
 public function index(HttpRequestEx $request)
 {
     $params = $request->getParameters();
     if (!empty($params['plugins'])) {
         $plugins = json_decode($params['plugins'], true);
         foreach ($plugins as $plugin => $tick) {
             if ($tick) {
                 $enabled[] = $plugin;
             }
         }
         if (!empty($enabled)) {
             echo "<pre>Starting composer..\n";
             //chdir($this->bootLoader->getBaseDir());
             //$output = $this->shell->run('composer -vv require %s', array_map(function ($f) { return "minutephp/$f:dev-master"; }, $enabled));
             $plugins = array_map(function ($f) {
                 return "minutephp/{$f}";
             }, $enabled);
             $success = $this->installer->install($plugins);
             if ($success) {
                 //--prefer-source
                 echo '<h3>All done!</h3>';
                 echo '<script>top.location.href = "/first-run/apache";</script>';
             } else {
                 echo "<h3>Something didn't go as expected.</h3>";
             }
         }
     }
 }
Beispiel #2
0
 public function getLoginRedirect(string $reason, bool $return = false, string $to = '')
 {
     $query = http_build_query(['redir' => $to ?: $this->request->getUri(), 'msg' => $reason]);
     $url = sprintf('%s?%s', $this->config->get('private/urls/login', '/login'), $query);
     if (!$return) {
         $this->setCookie('redir', $url);
         $redir = new Redirection($url);
         $redir->redirect();
     }
     return $url;
 }
Beispiel #3
0
 public function track()
 {
     if (empty($_COOKIE[self::HTTP_REFERRER_COOKIE]) && ($referrer = getenv('HTTP_REFERER'))) {
         $uri = parse_url($referrer);
         $domain = preg_quote($this->config->getPublicVars('domain'));
         $value = !preg_match("/{$domain}/", $uri['host']) ? sprintf('%s||%s', $this->request->getPath(), $referrer) : '/';
         $this->response->setCookie(self::HTTP_REFERRER_COOKIE, $value, '+1 year');
     }
     if (empty($_COOKIE[self::HTTP_CAMPAIGN_COOKIE]) && ($cmp = $this->request->getParameter('_cmp'))) {
         $this->response->setCookie(self::HTTP_CAMPAIGN_COOKIE, $cmp, '+1 year');
     }
 }
Beispiel #4
0
 /**
  * Session constructor.
  *
  * @param JwtEx $jwt
  * @param HttpRequestEx $request
  * @param HttpResponseEx $response
  * @param AccessManager $accessManager
  * @param UserInfo $userInfo
  * @param Config $config
  */
 public function __construct(JwtEx $jwt, HttpRequestEx $request, HttpResponseEx $response, AccessManager $accessManager, UserInfo $userInfo, Config $config)
 {
     $this->accessManager = $accessManager;
     $this->config = $config;
     $this->jwt = $jwt;
     $this->request = $request;
     $this->response = $response;
     $this->userInfo = $userInfo;
     $this->data = new stdClass();
     if ($cookie = $this->request->getCookie(self::COOKIE_NAME)) {
         if ($decoded = $this->jwt->decode($cookie)) {
             $this->data = $decoded;
         }
     }
 }
Beispiel #5
0
 public function setup(HttpRequestEx $request)
 {
     $params = $request->getParameters();
     try {
         if (!empty($params['db']['database']) && !empty($params['db']['username']) && !empty($params['db']['password'])) {
             try {
                 $conn = $this->database->connect($params['db']);
                 if ($pdo = $conn->getPdo()) {
                     $conf = sprintf('%s/app/Config/db-config', $this->bootLoader->getBaseDir());
                     if (file_put_contents($conf, sprintf('mysql://%s:%s@%s/%s', $params['db']['username'], $params['db']['password'], $params['db']['host'], $params['db']['database']))) {
                         if ($this->installer->install(['minutephp/site'], 'require', true)) {
                             $sth = $pdo->prepare('REPLACE INTO users SET email = :email, password = :password, ip_addr = :ip, created_at = NOW(), updated_at = NOW(), first_name = "Admin", verified = "true"');
                             $sth->execute(['email' => sprintf('admin@%s', $params['site']['domain'] ?? 'localhost'), 'password' => password_hash(Str::random(), PASSWORD_DEFAULT), 'ip' => $this->sniffer->getUserIP()]);
                             if ($admin_id = $pdo->lastInsertId()) {
                                 $sth = $pdo->prepare('REPLACE INTO m_user_groups set user_id = :user_id, group_name = "admin", created_at = NOW(), updated_at = NOW(), 
                                                                        expires_at = "20200101", credits = 999, comments = "First run"');
                                 $sth->execute(['user_id' => $admin_id]);
                                 $types = ['public' => $params['site'] ?? [], 'private' => []];
                                 foreach ($types as $type => $data) {
                                     $sth = $pdo->prepare('REPLACE INTO m_configs set type = :type, data_json = :data');
                                     $sth->execute(['type' => $type, 'data' => json_encode($data)]);
                                 }
                                 $this->session->startSession($admin_id);
                                 return 'pass';
                             }
                         } else {
                             throw new FirstRunError($this->lang->getText("Unable to run composer"));
                         }
                     }
                 }
             } catch (\Throwable $e) {
                 throw new FirstRunError($this->lang->getText("Unable to connect to database.\n") . $e->getMessage());
             }
         }
         throw new FirstRunError($this->lang->getText('All connection parameters are required. Please check connection details'));
     } catch (\Throwable $e) {
         if (!empty($conf) && file_exists($conf)) {
             @unlink($conf);
         }
         throw new FirstRunError("Error: " . $e->getMessage());
     }
 }
Beispiel #6
0
 /**
  * Get all <event name="$eventName"></event> tags and fire $eventName events,
  * then replace the <event> tag with the response generated by event handlers.
  *
  */
 public function render()
 {
     $html = !empty($this->layout) ? $this->compiler->replaceContentTagInLayout($this->content ?? '', $this->layout) : $this->content ?? '';
     $event = new ViewParserEvent($this, $html);
     $this->dispatcher->fire(ViewParserEvent::VIEWPARSER_RENDER, $event);
     $output = preg_replace_callback('~(<minute-event.*?</minute-event>)~mi', function ($matches) {
         $tag = $matches[1];
         $attrs = (array) new SimpleXMLElement($tag);
         $attrs = current($attrs);
         $event = new ViewEvent($this, $tag, $attrs);
         $eventName = strtolower(strtr($attrs['name'], ['_' => '.']));
         if (preg_match('/^(user|import)\\./i', $eventName)) {
             if (!empty($attrs['as'])) {
                 $importEvent = new ImportEvent($event, $tag, $attrs);
                 $this->dispatcher->fire($eventName, $importEvent);
                 foreach ($attrs as $k => $v) {
                     if ($k !== 'name' && $k !== 'as') {
                         $values[] = sprintf('%s="%s"', $k, htmlentities($v));
                     }
                 }
                 $data = $importEvent->getContent();
                 $import = sprintf('<minute-importer into="%s" data="%s" %s></minute-importer>', $attrs['as'], htmlentities(json_encode($data ?? [])), join(' ', $values ?? []));
                 $event->setContent($import);
             } else {
                 $this->dispatcher->fire($eventName, $event);
             }
         } else {
             throw new ViewError("Server side event '{$eventName}' failed (server side events may start with USER_ or IMPORT_ only)");
         }
         return $event->getContent();
     }, $event->getHtml());
     /** @var Helper $helper */
     foreach ($this->helpers ?? [] as $helper) {
         if ($content = $helper->getTemplate()) {
             $tagName = $helper->getPosition() === Helper::POSITION_BODY ? '</body>' : '</head>';
             $output = $this->tagUtils->insertBeforeTag($tagName, $content, $output);
         }
     }
     $output = preg_replace_callback('~<title></title>~', function () {
         $url = $this->request->getPath();
         $event = new SeoEvent($url);
         $this->dispatcher->fire(SeoEvent::SEO_GET_TITLE, $event);
         $result = sprintf('<title>%s</title>%s', $event->getTitle() ?: ucwords(trim(join(' ', preg_split('/\\W+/', $url)))), PHP_EOL);
         foreach ($event->getMeta() as $meta) {
             $result .= sprintf('<meta name="%s" content="%s" />%s', $meta['name'], $meta['content'], PHP_EOL);
         }
         return $result;
     }, $output);
     return $output;
 }
Beispiel #7
0
 public function importSession(ViewEvent $event)
 {
     if (!$this->database->isConnected()) {
         return;
     }
     /** @var RouteEx $route */
     $view = $event->getView();
     $vars = $view->getVars();
     $route = $view->get('_route');
     $data = $this->getCachedSessionData(false);
     $data['request'] = array_merge(['url' => getenv('REQUEST_URI')], $this->request->getParameters());
     $data['params'] = array_diff_key($route->getDefaults(), array_flip(['controller', 'auth', 'models', '_route']));
     foreach ($vars as $key => $value) {
         if ($key[0] !== '_' && (is_scalar($value) || is_array($value))) {
             $data['vars'][$key] = $value;
         }
     }
     if (!empty($data['site'])) {
         $data['site']['version'] = $this->database->hasRdsAccess() ? 'production' : 'debug';
     }
     $printer = sprintf('<script' . '>Minute.setSessionData(%s)</script>', json_encode($data));
     $event->setContent($printer);
 }