public function perform() { $hash = Request::getString($_REQUEST['x']); if (!strlen($hash) || base64_decode($hash, true) === false) { $this->req->setResult(['success' => false, 'error' => 'Wrong format of extTokenHash']); return; } $this->appInstance->externalAuthTokens->findByExtTokenHash($hash, function ($result) use($hash) { if ($result) { $this->req->setResult(['success' => false, 'error' => 'This token was already used.']); return; } $ip = $this->req->getIp(); $intToken = Crypt::hash(Daemon::uniqid() . "" . $ip . "" . Crypt::randomString()); $this->appInstance->externalAuthTokens->save(['extTokenHash' => $hash, 'intToken' => $intToken, 'ip' => $ip, 'useragent' => Request::getString($_SERVER['HTTP_USER_AGENT']), 'ctime' => microtime(true), 'status' => 'new'], function ($lastError) use($intToken) { if (!isset($lastError['n']) || $lastError['n'] === 0) { $this->req->setResult(['success' => false, 'errors' => ['code' => 'Sorry, internal error.']]); return; } $type = Request::getString($_REQUEST['type']); if ($type === 'email') { // send email.... } elseif ($type === 'redirect') { $this->req->redirectTo(HTTPClient::buildUrl(['/' . $this->req->locale . '/account/extauth', 'i' => $intToken]), false); } $this->req->setResult(['success' => true, 'intToken' => $intToken]); }); }); }
public function redirect() { if (!$this->checkReferer($this->appInstance->config->domain->value)) { $this->req->setResult(['error' => 'Wrong referer']); return; } $code = Request::getString($_GET['code']); if ($code === '') { Daemon::log('Authentication failed'); $this->req->status(401); $this->req->setResult(['error' => 'Authenticaion failed']); return; } $this->appInstance->httpclient->get($get = ['https://api.vk.com/oauth/access_token', 'client_id' => $this->cmp->config->vk_app_key->value, 'redirect_uri' => HTTPClient::buildUrl([$this->req->getBaseUrl() . '/component/Account/ExternalAuthRedirect/json', 'agent' => 'VK', 'backurl' => $this->getBackurl(true)]), 'client_secret' => $this->cmp->config->vk_app_secret->value, 'code' => $code], function ($conn, $success) use(&$get) { if (!$success) { $this->req->status(400); $this->req->setResult(['error' => 'request declined']); return; } Daemon::log(Debug::dump($get)); Daemon::log(Debug::dump($conn->body)); $response = json_decode(rtrim($conn->body), true); $user_id = isset($response['user_id']) ? (int) $response['user_id'] : 0; $access_token = Request::getString($response['access_token']); if ($user_id === 0 || $access_token === '') { $this->req->status(403); $this->req->setResult(['error' => 'no access token or user id']); return; } $this->appInstance->httpclient->get(['https://api.vk.com/method/users.get', 'uids' => $user_id, 'fields' => 'screen_name', 'access_token' => $access_token], function ($conn, $success) use($user_id) { $response = json_decode($conn->body, true); if (!$success || !is_array($response) || empty($user_id)) { $this->req->redirectTo($this->req->getBaseUrl(), false); $this->req->setResult(['error' => 'Unrecognized response']); return; } $data = []; if (isset($response['screen_name'])) { $data['username'] = Request::getString($response['screen_name']); } $this->req->components->account->acceptUserAuthentication('VK', $user_id, $data, [$this, 'finalRedirect']); }); }); }
/** * @param $url */ public function redirectTo($url, $finish = true, $perm = false) { $e = null; try { $url = HTTPClient::buildUrl($url); if (substr($url, 0, 1) === '/') { $url = $this->getBaseUrl() . $url; } if ($perm) { $this->status(301); $this->header('Location: ' . $url); } else { $this->status(302); $this->header('Cache-Control: no-cache, no-store, must-revalidate'); $this->header('Pragma: no-cache'); $this->header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); $this->header('Location: ' . $url); } } catch (RequestHeadersAlreadySent $e) { } if ($finish) { $this->finish(); } if ($e) { throw $e; } }
/** * Performs POST-request * @param string $url * @param array $data * @param array $params */ public function post($url, $data = [], $params = null) { if (!is_array($params)) { $params = ['resultcb' => $params]; } if (!isset($params['uri']) || !isset($params['host'])) { $prepared = Pool::parseUrl($url); if (!$prepared) { if (isset($params['resultcb'])) { call_user_func($params['resultcb'], false); } return; } list($params['host'], $params['uri']) = $prepared; } if ($params['uri'] === '') { $params['uri'] = '/'; } $this->lastURL = 'http://' . $params['host'] . $params['uri']; if (!isset($params['version'])) { $params['version'] = '1.1'; } if (!isset($params['contentType'])) { $params['contentType'] = 'application/x-www-form-urlencoded'; } $this->writeln('POST ' . $params['uri'] . ' HTTP/' . $params['version']); if (isset($params['proxy'])) { if (isset($params['proxy']['auth'])) { $this->writeln('Proxy-Authorization: basic ' . base64_encode($params['proxy']['auth']['username'] . ':' . $params['proxy']['auth']['password'])); } } if (!isset($params['keepalive']) || !$params['keepalive']) { $this->writeln('Connection: close'); } $this->writeln('Host: ' . $params['host']); if ($this->pool->config->expose->value && !isset($params['headers']['User-Agent'])) { $this->writeln('User-Agent: phpDaemon/' . Daemon::$version); } if (isset($params['cookie']) && sizeof($params['cookie'])) { $this->writeln('Cookie: ' . http_build_query($params['cookie'], '', '; ')); } foreach ($data as $val) { if (is_object($val) && $val instanceof UploadFile) { $params['contentType'] = 'multipart/form-data'; } } $this->writeln('Content-Type: ' . $params['contentType']); if ($params['contentType'] === 'application/x-www-form-urlencoded') { $body = http_build_query($data, '', '&', PHP_QUERY_RFC3986); } elseif ($params['contentType'] === 'application/x-json') { $body = json_encode($data); } else { $body = 'unsupported Content-Type'; } $this->writeln('Content-Length: ' . strlen($body)); if (isset($params['headers'])) { $this->customRequestHeaders($params['headers']); } if (isset($params['rawHeaders']) && $params['rawHeaders']) { $this->rawHeaders = []; } $this->writeln(''); $this->write($body); $this->writeln(''); $this->onResponse($params['resultcb']); }
/** * Constructor. * @return void */ public function init() { $this->httpclient = \PHPDaemon\Clients\HTTP\Pool::getInstance(); }
public function init() { Daemon::log(get_class($this) . ' up.'); ini_set('display_errors', 'On'); $this->redis = \PHPDaemon\Clients\Redis\Pool::getInstance($this->config->redisname->value); $this->db = \PHPDaemon\Clients\Mongo\Pool::getInstance($this->config->mongoname->value); $this->dbname = $this->config->dbname->value; $this->ipcId = new MongoId(); $this->JobManager = new JobManager($this); $this->Sendmail = new Sendmail($this); if (isset($this->config->BackendServer)) { $this->backendServer = BackendServer::getInstance($this->config->BackendServer, true, $this); } if (isset($this->config->BackendClient)) { $this->backendClient = BackendClient::getInstance($this->config->BackendClient, true, $this); } $this->discoverOrm($this->config->ormdir->value . '*.php'); /* $this->LockClient = \PHPDaemon\Clients\Lock\Pool::getInstance(); $this->LockClient->job(get_class($this) . '-' . $this->name, true, function ($jobname, $command, $client) { foreach (glob($this->config->themesdir->value . '*'.'/blocks/*') as $file) { Daemon::$process->fileWatcher->addWatch($file, array($this, 'onBlockFileChanged')); } });*/ $this->locales = array_map('basename', glob($this->config->localedir->value . '*', GLOB_ONLYDIR)); if (!in_array($this->config->defaultlocale->value, $this->locales, true)) { $this->locales[] = $this->config->defaultlocale->value; } if (!in_array('en', $this->locales, true)) { $this->locales[] = 'en'; } $this->components = new Components($this->fakeRequest()); foreach ($this->config as $k => $c) { if (isset($c->run->value) && $c->run->value) { if (substr($k, 0, 3) === 'Cmp') { $appInstance->components->{substr($k, 3)}; } } } $this->serializer = 'igbinary'; $this->httpclient = \PHPDaemon\Clients\HTTP\Pool::getInstance(); }
/** * Send request headers * @param $type * @param $url * @param &$params * @return void */ protected function sendRequestHeaders($type, $url, &$params) { if (!is_array($params)) { $params = ['resultcb' => $params]; } if (!isset($params['uri']) || !isset($params['host'])) { $prepared = Pool::parseUrl($url); if (!$prepared) { if (isset($params['resultcb'])) { $params['resultcb'](false); } return; } list($params['host'], $params['uri']) = $prepared; } if ($params['uri'] === '') { $params['uri'] = '/'; } $this->lastURL = 'http://' . $params['host'] . $params['uri']; if (!isset($params['version'])) { $params['version'] = '1.1'; } $this->writeln($type . ' ' . $params['uri'] . ' HTTP/' . $params['version']); if (isset($params['proxy'])) { if (isset($params['proxy']['auth'])) { $this->writeln('Proxy-Authorization: basic ' . base64_encode($params['proxy']['auth']['username'] . ':' . $params['proxy']['auth']['password'])); } } $this->writeln('Host: ' . $params['host']); if ($this->pool->config->expose->value && !isset($params['headers']['User-Agent'])) { $this->writeln('User-Agent: phpDaemon/' . Daemon::$version); } if (isset($params['cookie']) && sizeof($params['cookie'])) { $this->writeln('Cookie: ' . http_build_query($params['cookie'], '', '; ')); } if (isset($params['contentType'])) { if (!isset($params['headers'])) { $params['headers'] = []; } $params['headers']['Content-Type'] = $params['contentType']; } if (isset($params['headers'])) { $this->customRequestHeaders($params['headers']); } if (isset($params['rawHeaders']) && $params['rawHeaders']) { $this->rawHeaders = []; } if (isset($params['chunkcb']) && is_callable($params['chunkcb'])) { $this->chunkcb = $params['chunkcb']; } $this->writeln(''); $this->requests->push($type); $this->onResponse->push($params['resultcb']); $this->checkFree(); }