/** * @param $url * @return Options */ private function _filter_url($url) { if (!$url instanceof Options) { $url = new Options(['loc' => $url]); } $scheme = Env::$request->isSecure() ? 'https://' : 'http://'; $domain = Env::$request->getHttpHost(); $loc = $url->get('loc'); $loc_pairs = explode('?', $loc); if (isset($loc_pairs[1])) { $loc_pairs[1] = rawurlencode($loc_pairs[1]); } $url['loc'] = $scheme . $domain . implode($loc_pairs, '?'); return $url; }
/** * @param $dir * @return array */ public function list_files($dir, $theme = null) { if (is_null($theme)) { $dir = $this->path . S . $dir; } if ($theme == '@current') { $theme_dir = $this->conf->get('theme_dir'); $theme_dir = str_replace('/', S, $theme_dir); $theme_dir = str_replace('[theme]', $this->load('Application.Settings')->conf->get('theme'), $theme_dir); $dir = ROOT . S . $theme_dir . S . $dir; } if (!is_dir($dir)) { return []; } $handler = opendir($dir); $list_files = []; while ($item = readdir($handler)) { if ($item == '.' || $item == '..') { continue; } if (!is_dir($dir . S . $item)) { $list_files[] = $item; } } return $list_files; }
/** * @return string */ public function code() { if ($this->configured()) { return "<script>(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)})(window,document,'script','//www.google-analytics.com/analytics.js','ga');ga('create', '{$this->conf->get('id')}', 'auto');ga('send', 'pageview');</script>"; } return null; }
/** * ULogin registration and authorization * * @param NCModule $module * @param \Service\Render\Theme $theme * @param \Service\Application\Translate $translate * @return array|\System\Engine\NCBlock[] */ static function globalize($module, $theme, $translate) { // Gravatar $theme->twig->addFilter(new \Twig_SimpleFilter('avatar', function ($user, $default = null) { return $user['avatar'] ? $user['avatar'] : User::get_gravatar_url($user['email'], 256, $default); })); // Refer host $theme->twig->addFilter(new \Twig_SimpleFilter('host', function ($url, $default = 1) { $data = parse_url($url); if (isset($data['host'])) { return $data['host']; } if ($default === 1) { return $url; } return $default; })); // URL Decode $theme->twig->addFilter(new \Twig_SimpleFilter('url_decode', function ($url) { return urldecode($url); })); // ULogin authentication if (!$module->user && Env::$request->isMethod('post') && isset($_POST['token'])) { $s = file_get_contents('http://ulogin.ru/token.php?token=' . $_POST['token'] . '&host=' . $_SERVER['HTTP_HOST']); $data = new Options(json_decode($s, true)); $hash = User::encrypt(User::encrypt($data->get('network') . $data->get('identity'))); if ($user = $module->auth->authenticate_by_hash($hash)) { $module->auth->login($user); } else { $user = User::create(['username' => $data['first_name'] . ' ' . $data['last_name'], 'password' => $hash, 'email' => $data->get('email', $data->get('identity')), 'avatar' => $data->get('photo', ''), 'group_id' => $module->settings->get('users_group')]); } if (isset($user) && $user instanceof User) { $module->auth->login($user); Env::$response->sendHeaders(); return static::redirect_response('/'); } } /* * Last 5 users */ $last_users = function () { return \User::as_array(User::find('all', ['conditions' => ['ban_time = 0 OR ban_time IS NULL'], 'order' => 'register_date DESC', 'limit' => 5])); }; return ['_user' => lazy_arr('users', ['$last' => $last_users]), 'ulogin' => ['small' => '<script src="//ulogin.ru/js/ulogin.js"></script><div id="uLogin" data-ulogin="******"></div>', 'panel' => '<script src="//ulogin.ru/js/ulogin.js"></script><div id="uLogin" data-ulogin="******"></div>']]; }
/** * @param $key * @param null $value */ public function set($key, $value = null) { $this->conf->set($key, $value); }
/** * @return bool */ public function configured() { $key = $this->conf->get('key') && $this->conf->get('secret'); $token = $this->conf->get('token') && $this->conf->get('token_secret'); return $key && $token; }
/** * @return array */ public function social_list() { return $this->conf->getArrayCopy(); }
/** * @param Request $request * @param Options $matches * @return string */ public function user(Request $request, Options $matches = null) { $who = $matches->get('id'); if ($who == 'my') { $user = $this->user; } else { /** @var \PostCategory $category */ $user = \User::find($matches->get('id')); } if ($user) { // Filter conditions $filter = ['conditions' => ['author_id = ?', $user->id]]; // Rows count $rows = \Post::count($filter); // Paginator /** @var Listing $pagination */ $pagination = NCService::load('Paginator.Listing', [$request->page, $rows]); // Limitation $filter = array_merge($filter, $pagination->limit()); // Ordering if ($request->order) { $filter['order'] = $request->order; } else { $filter['order'] = 'created_at DESC'; } // Get posts $posts = \Post::all($filter); // Rendering return $this->view->render('posts/list.twig', ['title' => $this->lang->translate('post.by_author', $user->username), 'posts' => \Post::as_array($posts), 'listing' => $pagination->pages(), 'page' => $request->page]); } return $this->error404($request); }
/** * @return bool */ public function save() { return $this->update_config('permissions', $this->map->getArrayCopy()); }
/** * @return array */ public function m_groups() { $response = $this->request('groups.get', ['user_id' => $this->conf->get('user_id'), 'filter' => 'admin', 'extended' => 1], 'response', [0]); array_shift($response); return $response; }
/** * Social media posting * * @param Request $request * @param Options $matches * @return mixed */ public function smp(Request $request, Options $matches = null) { /** @var SocialMedia $smp */ $smp = NCService::load('SocialMedia'); // Selected network $soc = $smp->network($request->get('soc') ? $request->get('soc') : $matches->get('drv')); if ($soc || $matches && $matches->get('drv')) { $message = ''; $status = ''; $manager = $smp->get_manager($soc['id']); $redirect_uri = $manager::$redirect_uri ? $manager::$redirect_uri : $this->map->reverse('smpp', $soc['id']); // If saving settings if ($request->isMethod('post')) { if ($request->isMethod('post') && $manager->setup($_POST)) { $soc = $smp->network($request->get('soc')); $status = 'success'; $message = $this->lang->translate('form.saved'); } else { $status = 'error'; $message = $this->lang->translate('form.failed'); } } // Getting token if ($matches && $matches->get('drv')) { if (Driver::process($soc['id'], [$redirect_uri])) { $status = 'success'; $message = $this->lang->translate('form.authorized'); } else { $status = 'error'; $message = $this->lang->translate('form.failed'); } } // VK Confirmation system if ($manager instanceof Vkontakte && $manager->active()) { $groups = $manager->request('groups.get', ['user_id' => $manager->conf->get('user_id'), 'filter' => 'admin', 'extended' => 1]); if (array_key_exists('error', $groups)) { $status = 'error'; if ($groups['error']['error_code'] == 17) { $message = $groups['error']['error_msg'] . ' - <a href="' . $groups['error']['redirect_uri'] . '" target="_blank">Fix this issue</a>'; } else { $message = $groups['error']['error_msg']; } } } return $this->view->render('com/smp/' . $soc['id'] . '.twig', ['title' => $this->lang->translate('admin.smp.setup', $soc['name']), 'network' => $soc, 'status' => $status, 'message' => $message, 'auth_url' => $manager->configured() ? $manager->authorize_url($redirect_uri) : false]); } return $this->view->render('dashboard/smp.twig', ['title' => $this->lang->translate('admin.smp.title'), 'socials' => $smp->social_list()]); }
/** * @param $url * @param array $data * @return mixed */ public function request($url, $data = []) { $data['key'] = $this->conf->get('key'); return static::GET($url, $data); }