Esempio n. 1
0
 /**
  * Выполняет обработку запроса
  *
  * @param WS_Environment $env
  *
  * @return mixed
  */
 public function run(WS_Environment $env)
 {
     $this->request = $env->request;
     if ($this->request->server('AUTH_TYPE')) {
         if (!$this->auth()) {
             return Net_HTTP::Response(Net_HTTP::FORBIDDEN);
         }
     }
     return $this->application->run($env);
 }
Esempio n. 2
0
 /**
  * @param WS_Environment $env
  *
  * @return mixed
  */
 public function run(WS_Environment $env)
 {
     $env->auth = Core::object(array('user' => null, 'module' => $this->auth_module));
     $this->set_user($env);
     $uri = $env->request->url;
     try {
         $response = $this->application->run($env);
     } catch (WS_Auth_UnauthenticatedException $e) {
         $response = Net_HTTP::Response(Net_HTTP::UNAUTHORIZED);
     } catch (WS_Auth_ForbiddenException $e) {
         $response = Net_HTTP::Response(Net_HTTP::FORBIDDEN);
     }
     if ($response->status->code == Net_HTTP::UNAUTHORIZED) {
         return Net_HTTP::redirect_to(Core_Strings::replace($this->auth_url, '{url}', $uri));
     } else {
         return $response;
     }
 }
Esempio n. 3
0
 public function action($name, $data, $action, $item = false, $fields = array())
 {
     $args = func_get_args();
     $res = Net_HTTP::Response();
     $res->content_type('application/json');
     $query = WS::env()->request['query'];
     $ents = CMS::items_for_select(Core::invoke($data['search'], array($query)));
     $values = array();
     $empty = $this->get_empty($data);
     if ($empty && empty($query)) {
         $values[] = $empty;
     }
     foreach ($ents as $k => $e) {
         $values[] = array('id' => (int) $k, 'title' => (string) $e);
     }
     $res->body(json_encode(array('data' => $values)));
     return $res;
 }
Esempio n. 4
0
 /**
  * @param WS_Environment $env
  *
  * @return mixed
  */
 public function run(WS_Environment $env)
 {
     $name = $this->options['env_name'];
     if (!isset($env->{$name})) {
         $env->{$name} = Core::object(array('user' => null, 'module' => $this->auth_module));
     }
     if (isset($env->request->headers['Authorization'])) {
         list($login, $password) = $this->parse_credentials($env->request->headers['Authorization']);
         $env->{$name}->user = $this->auth_module->authenticate($login, $password);
     }
     try {
         $response = $this->application->run($env);
     } catch (WS_Auth_UnauthenticatedException $e) {
         $response = Net_HTTP::Response(Net_HTTP::UNAUTHORIZED)->header('WWW-Authenticate', 'Basic realm="' . $e->realm . '"');
     } catch (WS_Auth_ForbiddenException $e) {
         $response = Net_HTTP::Response(Net_HTTP::FORBIDDEN);
     }
     return $response;
 }
Esempio n. 5
0
 /**
  * @param WS_Environment $env
  *
  * @return Net_HTTP_Response
  */
 public function run(WS_Environment $env)
 {
     //if ($this->disabled) return $this->application->run($env);
     $error = null;
     try {
         $body = $this->application->run($env);
         $response = Net_HTTP::merge_response($body, $env->response);
     } catch (Exception $e) {
         $error = $e;
         if ($this->disabled) {
             throw $e;
         }
         $response = Net_HTTP::Response(Net_HTTP::INTERNAL_SERVER_ERROR);
     }
     if (!$response->body && ($template = $this->find_template_name_for($response->status))) {
         if (isset($env->not_found->static_file)) {
             $response->body(IO_FS::File($env->not_found->static_file));
         } else {
             $layout = isset($env->not_found->layout) ? $env->not_found->layout : 'work';
             $view = Templates::HTML($template);
             if ($layout) {
                 $view->within_layout($layout);
             }
             $view->root->with(array('env' => $env, 'response' => $response, 'error' => $error));
             if ($view->exists()) {
                 $response->body($view);
             } else {
                 if (IO_FS::exists($static_name = $template . '.html')) {
                     $response->body(IO_FS::File($static_name));
                 }
             }
         }
     }
     Events::call('ws.status', $response);
     return $response;
 }
Esempio n. 6
0
File: WS.php Progetto: techart/tao
 /**
  * Запускает приложение
  *
  * @param WS_ServiceInterface $app
  *
  * @return WS_Runner
  */
 public function run(WS_ServiceInterface $app)
 {
     $environment = WS::env();
     $environment->request = $this->adapter->make_request();
     $rc = Events::call('ws.run');
     if (!is_null($rc)) {
         return $rc;
     }
     $environment->response = Net_HTTP::Response();
     $body = $app->run($environment);
     $response = Net_HTTP::merge_response($body, $environment->response);
     return $this->adapter->process_response($response);
 }
Esempio n. 7
0
 protected function tree_json()
 {
     $res = Net_HTTP::Response()->content_type('application/json');
     $root = WS::env()->request['node'];
     // $this->page = (int) isset($this->request['page']) ? $this->request['page'] : 1;
     $data = $this->tree_data($root, json_decode(WS::env()->request['sort'], true));
     $result = array('total' => $this->get_tree_count($root), 'children' => $data, 'success' => true);
     return $res->body(json_encode($result));
 }
Esempio n. 8
0
File: HTTP.php Progetto: techart/tao
 /**
  */
 public function as_response()
 {
     return Net_HTTP::Response($this);
 }
Esempio n. 9
0
File: HTTP.php Progetto: techart/tao
 /**
  * @param Net_HTTP_Request $request
  *
  * @return Net_HTTP_Response
  */
 public function send($request)
 {
     if (is_string($request)) {
         $url = $request;
         $request = Net_HTTP::Request($request);
     } else {
         $url = $request->url;
     }
     if (preg_match('{[а-яА-Я]+}u', $request->host) && function_exists('idn_to_ascii')) {
         $request->host = idn_to_ascii($request->host);
         $url = $request->url;
     }
     if ($this->act_as_browser) {
         $this->additional_headers($request);
     }
     $id = $this->make_curl($url);
     $headers = $request->headers->as_array(true);
     $headers[] = 'Expect:';
     // by default curl expect 100-continue
     $options = $this->to_file ? array(CURLOPT_CUSTOMREQUEST => strtoupper($request->method_name)) : array(CURLOPT_RETURNTRANSFER => 1, CURLOPT_CUSTOMREQUEST => strtoupper($request->method_name), CURLOPT_HEADER => 1, CURLOPT_NOBODY => !$this->with_body);
     switch ($request->method_code) {
         case Net_HTTP::GET:
         case Net_HTTP::HEAD:
             break;
         case Net_HTTP::POST:
         case Net_HTTP::PUT:
         case Net_HTTP::DELETE:
             $body = isset($request->body) ? is_array($request->body) ? $request->body : (string) $request->body : $request->post_data;
             if (is_string($body)) {
                 $headers[] = 'Content-Length: ' . strlen($body);
             }
             $options[CURLOPT_POSTFIELDS] = $body;
             break;
     }
     if ($headers) {
         $options[CURLOPT_HTTPHEADER] = $headers;
     }
     if ($this->inspect) {
         $this->inspect($id, $options);
     }
     $result = $this->execute($id, $options);
     $header_size = curl_getinfo($id, CURLINFO_HEADER_SIZE);
     $this->info = curl_getinfo($id);
     $this->error = curl_error($id);
     $this->errno = curl_errno($id);
     $effective_url = trim(curl_getinfo($id, CURLINFO_EFFECTIVE_URL));
     // memory duplicate
     // $this->last_result = &$result;
     curl_close($id);
     if ($this->to_file) {
         $this->to_file->close();
         return Net_HTTP::Response()->status($this->info['http_code']);
     }
     if ($result !== false) {
         $header = substr($result, 0, $header_size);
         $body = substr($result, $header_size);
         unset($result);
         //FIXME:
         $header = preg_replace('{[^\\r\\n]{0,5}Connection established[^\\r\\n]{0,5}\\r\\n\\r\\n}i', '', $header, 1);
         $response = Net_HTTP_Response::from_string($body, $header);
         unset($body);
         unset($header);
         $response->url = $url;
         if ($this->auto_redirect && $response->status->is_redirect) {
             return $this->redirect($response, $request, $effective_url);
         }
     } else {
         if (!empty($this->error)) {
             $response = Net_HTTP::Response();
             $response->status(0, $this->error);
         } else {
             return null;
         }
     }
     return $response;
 }
Esempio n. 10
0
 protected function tree_update()
 {
     $data = json_decode($this->env->request->content, true);
     if (isset($data['id'])) {
         $data = array($data);
     }
     try {
         foreach ($data as $row) {
             if (!empty($row['id'])) {
                 $entity = $this->load($row['id']);
                 if ($entity->id()) {
                     foreach ($row as $k => $v) {
                         $entity[$k] = $v;
                     }
                     // Log::logger()->debug('item', $entity->attrs);
                     $entity->update();
                 }
             }
             $this->save_reorder($parent_id);
         }
         return Net_HTTP::Response()->content_type('application/json')->body(json_encode(array('success' => true, 'message' => 'OK')));
     } catch (Exception $e) {
         return Net_HTTP::Response()->content_type('application/json')->body(json_encode(array('success' => false, 'message' => $e->getMessage())));
     }
 }
Esempio n. 11
0
File: REST.php Progetto: techart/tao
 public function create_response($env)
 {
     if (!empty($this->callback_result)) {
         return $this->callback_result;
     }
     return Core::with($this->target_instance && $this->target_method ? ($result = $this->execute($this->target_instance, $this->target_method->name, $env, $this->match ? $this->match->parms : array(), $this->format, $this->target_method->defaults)) instanceof Net_HTTP_Response ? $result : Net_HTTP::Response($result)->content_type(!empty($this->format) ? $this->format : self::DEFAULT_CONTENT_TYPE) : Net_HTTP::Response(Net_HTTP::NOT_FOUND));
 }
Esempio n. 12
0
 /**
  */
 protected function not_implemented()
 {
     return Net_HTTP::Response(Net_HTTP::NOT_IMPLEMENTED);
 }