protected function call_plugin_impl($call_ctx)
 {
     static $plugin;
     if (is_null($plugin)) {
         try {
             hd_print('Instantiating plugin...');
             $plugin = $this->create_plugin();
             hd_print('Plugin instance created.');
         } catch (Exception $e) {
             hd_print('Error: can not instantiate plugin (' . $e->getMessage() . ')');
             return array(PluginOutputData::has_data => false, PluginOutputData::plugin_cookies => $call_ctx->plugin_cookies, PluginOutputData::is_error => true, PluginOutputData::error_action => ActionFactory::show_error(true, T::t('title_application_error'), array(T::t('msg_plugin_init_failed'))));
         }
     }
     $out_data = null;
     try {
         $out_data = $this->invoke_operation($plugin, $call_ctx);
     } catch (DuneException $e) {
         hd_print("Error: DuneException caught: " . $e->getMessage());
         return array(PluginOutputData::has_data => false, PluginOutputData::plugin_cookies => $call_ctx->plugin_cookies, PluginOutputData::is_error => true, PluginOutputData::error_action => $e->get_error_action());
     } catch (Exception $e) {
         hd_print("Error: Exception caught: " . $e->getMessage());
         return array(PluginOutputData::has_data => false, PluginOutputData::plugin_cookies => $call_ctx->plugin_cookies, PluginOutputData::is_error => true, PluginOutputData::error_action => ActionFactory::show_error(true, T::t('title_application_error'), array(T::t('msg_unhandled_plugin_error'))));
     }
     $plugin_output_data = array(PluginOutputData::has_data => !is_null($out_data), PluginOutputData::plugin_cookies => $call_ctx->plugin_cookies, PluginOutputData::is_error => false, PluginOutputData::error_action => null);
     if ($plugin_output_data[PluginOutputData::has_data]) {
         $plugin_output_data[PluginOutputData::data_type] = $this->get_out_type_code($call_ctx->op_type_code);
         $plugin_output_data[PluginOutputData::data] = $out_data;
     }
     return $plugin_output_data;
 }
 protected function call_plugin_impl($call_ctx)
 {
     static $plugin;
     if (is_null($plugin)) {
         try {
             hd_print('Instantiating plugin...');
             $plugin = $this->create_plugin();
             hd_print('Plugin instance created.');
         } catch (Exception $e) {
             hd_print('Error: can not instantiate plugin (' . $e->getMessage() . ')');
             return array(PluginOutputData::has_data => false, PluginOutputData::plugin_cookies => $call_ctx->plugin_cookies, PluginOutputData::is_error => true, PluginOutputData::error_action => ActionFactory::show_error(true, 'System error', array('Can not create PHP plugin instance.', 'Call the PHP plugin vendor.')));
         }
     }
     // assert($plugin);
     $out_data = null;
     try {
         $out_data = $this->invoke_operation($plugin, $call_ctx);
     } catch (DuneException $e) {
         hd_print("Error: DuneException caught: " . $e->getMessage());
         return array(PluginOutputData::has_data => false, PluginOutputData::plugin_cookies => $call_ctx->plugin_cookies, PluginOutputData::is_error => true, PluginOutputData::error_action => $e->get_error_action());
     } catch (Exception $e) {
         hd_print("Error: Exception caught: " . $e->getMessage());
         return array(PluginOutputData::has_data => false, PluginOutputData::plugin_cookies => $call_ctx->plugin_cookies, PluginOutputData::is_error => true, PluginOutputData::error_action => ActionFactory::show_error(true, 'System error', array('Unhandled PHP plugin error.', 'Call the PHP plugin vendor.')));
     }
     // Note: change_tv_favorites() may return NULL even if it's completed
     // successfully.
     $plugin_output_data = array(PluginOutputData::has_data => !is_null($out_data), PluginOutputData::plugin_cookies => $call_ctx->plugin_cookies, PluginOutputData::is_error => false, PluginOutputData::error_action => null);
     if ($plugin_output_data[PluginOutputData::has_data]) {
         $plugin_output_data[PluginOutputData::data_type] = $this->get_out_type_code($call_ctx->op_type_code);
         $plugin_output_data[PluginOutputData::data] = $out_data;
     }
     return $plugin_output_data;
 }
 public function handle_user_input(&$user_input, &$plugin_cookies)
 {
     hd_print('Vod search: handle_user_input:');
     foreach ($user_input as $key => $value) {
         hd_print("  {$key} => {$value}");
     }
     if ($user_input->action_type === 'apply') {
         $control_id = $user_input->control_id;
         if ($control_id === 'pattern') {
             $pattern = $user_input->pattern;
             $plugin_cookies->vod_search_pattern = $pattern;
             hd_print("Vod search: applying pattern '{$pattern}'");
             $defs = $this->do_get_control_defs($plugin_cookies);
             return ActionFactory::reset_controls($defs, ActionFactory::open_folder($this->vod->get_search_media_url_str($pattern), $pattern));
         }
     } else {
         if ($user_input->action_type === 'confirm') {
             $control_id = $user_input->control_id;
             $new_value = $user_input->{$control_id};
             if ($control_id === 'pattern') {
                 $pattern = $user_input->pattern;
                 if (preg_match('/^\\s*$/', $pattern)) {
                     return ActionFactory::show_error(false, 'Pattern should not be empty');
                 }
             }
         }
     }
     return null;
 }
 public static function deregister()
 {
     if (!self::$_logged_in) {
         return false;
     }
     $result = ZTVDaemonController::deregister();
     if ($result->output === false) {
         hd_print('Error: can not execute arescam.');
         throw new DuneException('%tr%caption_system_error', 0, ActionFactory::show_error(true, '%tr%caption_system_error', array('%tr%error_daemon_execution_failed', '%tr%error_contact_support')));
     }
     if ($result->rc === 0) {
         self::$_logged_in = false;
         self::$_login_expire = 0;
         return true;
     }
     return false;
 }
Beispiel #5
0
 public static function call($json_request)
 {
     $json_reply = null;
     for ($i = 0; $i < 3; ++$i) {
         try {
             $doc = HD::http_post_document(static::API_URL, json_encode($json_request));
             $json_reply = json_decode($doc);
         } catch (Exception $e) {
             hd_print('Error: failed to do HTTP-request.');
             if ($i == 2) {
                 throw new DuneException('API is unavailable', 0, ActionFactory::show_error(true, 'Системная ошибка', array('Сервер недоступен(' . $e->getMessage() . ').', 'Пожалуйста обратитесь в тех. поддержку.')));
             }
             usleep(100000 << $i);
             continue;
         }
         break;
     }
     if (is_null($json_reply)) {
         hd_print("Error: failed to decode API reply: '{$doc}'");
         throw new DuneException('API returned empty result', 0, ActionFactory::show_error(true, 'Системная ошибка', array('Сервер вернул пустой ответ.', 'Пожалуйста обратитесь в тех. поддержку.')));
     }
     if (isset($json_reply->error) && $json_reply->error) {
         hd_print("Error: API returned error status({$doc})");
         throw new DuneException('API returned error', $json_reply->code, ActionFactory::show_error(true, 'Системная ошибка', array('Сервер вернул ошибку(' . $json_reply->message . ').', 'Пожалуйста обратитесь в тех. поддержку.')));
     }
     // TODO: Think of a better check.
     if (!isset($json_request['method'])) {
         $request_count = count($json_request);
         $reply_count = count($json_reply);
         if ($request_count != $reply_count) {
             return false;
         }
         for ($i = 0, $n = 0; $i < $reply_count; $n = $n + ($json_reply[$i]->result ? 1 : 0), $i++) {
         }
         if ($i != $n) {
             return false;
         }
     }
     return $json_reply;
 }
 public function apply_subscription(&$plugin_cookies, &$user_input)
 {
     if (!isset($user_input->control_id) || $user_input->control_id != 'apply_subscription') {
         return false;
     }
     if ($user_input->user_name === '') {
         return array('need_close_dialog' => false, 'action' => ActionFactory::show_error(false, 'Error', array('Subscription should be non-empty.')));
     }
     $plugin_cookies->user_name = $user_input->user_name;
     $plugin_cookies->password = $user_input->password;
     $this->logout();
     $post_action = null;
     try {
         $this->try_login($plugin_cookies);
     } catch (DuneException $e) {
         $post_action = $e->get_error_action();
     }
     return array('need_close_dialog' => true, 'action' => $post_action);
 }
Beispiel #7
0
 public static function http_get_document($url, $opts = null)
 {
     $url = str_replace('//', '/', $url);
     $url = str_replace('http:/', 'http://', $url);
     $url = str_replace('https:/', 'https://', $url);
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_TIMEOUT, 20);
     curl_setopt($ch, CURLOPT_USERAGENT, 'DuneHD/1.0');
     curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
     curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
     $emplexerClientHeaders = array('X-Plex-Client-Capabilities' => 'protocols=shoutcast,webkit,http-video;videoDecoders=h264{profile:high&resolution:1080&level:51};audioDecoders=mp3,aac', 'X-Plex-Client-Identifier' => 'b6d41f8a-e5ad-4987-a74b-ca12b143b0e0', 'X-Plex-Client-Platform' => 'Dune', 'X-Plex-Language' => 'en', 'X-Plex-Version' => 'abc');
     if (isset($opts)) {
         foreach ($opts as $k => $v) {
             curl_setopt($ch, $k, $v);
         }
     }
     hd_print("HTTP fetching \nurl:'{$url}'...");
     $content = curl_exec($ch);
     $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
     $acceptedHeaders = array(200, 403);
     hd_print('http_code = ' . $http_code);
     if (!in_array($http_code, $acceptedHeaders)) {
         $err_msg = "HTTP request failed ({$http_code})";
         hd_print($err_msg);
         HD::print_backtrace();
         switch ($http_code) {
             case '0':
                 //timeout
                 $msg = "The area you are trying to access is taking too long to respond, try again. ({$http_code})";
                 break;
             case '404':
                 $msg = "the area you are trying to access does not return any xml. ({$http_code})";
                 break;
             default:
                 $msg = "houve um problema ao acessar essa area, try again. ({$http_code})";
                 break;
         }
         throw new DuneException('Error: "' . $err_msg . '"', 0, ActionFactory::show_error(false, $msg));
     }
     hd_print("HTTP OK ({$http_code})");
     curl_close($ch);
     if ($http_code == 200) {
         return $content;
     } elseif ($http_code == 403) {
         $session_expired = array('result' => 'error', 'error' => 'Session expired', 'error_code' => '403');
         return json_encode($session_expired);
     }
 }
 public function handle_user_input(&$user_input, &$plugin_cookies)
 {
     if (!isset($user_input->control_id)) {
         if ($plugin_cookies->require_restart) {
             ZTVDaemonController::restart($plugin_cookies->use_proxy == 'yes' ? array('-y' => $plugin_cookies->proxy_ip . ':' . $plugin_cookies->proxy_port) : array());
             $plugin_cookies->require_restart = false;
         }
         if (!ZTVTerminal::login()) {
             return ActionFactory::show_dialog('%tr%dialog_registration_form', $this->do_registration_form_defs($plugin_cookies), true);
         }
         if ($this->load_profiles()) {
             return $this->profile->require_password ? ActionFactory::show_dialog('%ext%<key_local>dialog_profile_password<p>' . $this->profile->title . '</p></key_local>', $this->do_profile_form_defs($plugin_cookies), true) : ActionFactory::open_folder();
         }
         return;
     } else {
         if ($user_input->action_type == 'confirm' || $user_input->action_type == 'apply') {
             switch ($user_input->control_id) {
                 case 'epg_provider':
                     $plugin_cookies->epg_provider = $user_input->epg_provider;
                     break;
                 case 'use_proxy':
                     $plugin_cookies->use_proxy = $user_input->use_proxy;
                     $plugin_cookies->proxy_ip = isset($plugin_cookies->proxy_ip) ? $plugin_cookies->proxy_ip : '192.168.0.1';
                     $plugin_cookies->proxy_port = isset($plugin_cookies->proxy_port) ? $plugin_cookies->proxy_port : '8080';
                     $plugin_cookies->require_restart = true;
                     break;
                 case 'proxy_ip':
                     $plugin_cookies->proxy_ip = $user_input->proxy_ip;
                     $plugin_cookies->require_restart = true;
                     break;
                 case 'proxy_port':
                     $plugin_cookies->proxy_port = $user_input->proxy_port;
                     $plugin_cookies->require_restart = true;
                     break;
                 case 'daemon_start':
                     ZTVDaemonController::start($plugin_cookies->use_proxy == 'yes' ? array('-y' => $plugin_cookies->proxy_ip . ':' . $plugin_cookies->proxy_port) : array());
                     break;
                 case 'daemon_restart':
                     ZTVDaemonController::restart($plugin_cookies->use_proxy == 'yes' ? array('-y' => $plugin_cookies->proxy_ip . ':' . $plugin_cookies->proxy_port) : array());
                     break;
                 case 'daemon_stop':
                     ZTVDaemonController::stop();
                     break;
                 case 'register_button':
                     return ActionFactory::show_dialog('%tr%dialog_registration_form', $this->do_registration_form_defs($plugin_cookies), true);
                     break;
                 case 'register_terminal':
                     if (is_null($user_input->username) || trim($user_input->username) == '') {
                         return ActionFactory::show_error(false, '%tr%caption_error', array('%tr%error_username_empty'));
                     } else {
                         if (is_null($user_input->password) || trim($user_input->password) == '') {
                             return ActionFactory::show_error(false, '%tr%caption_error', array('%tr%error_pin_empty'));
                         }
                     }
                     if (!ZTVTerminal::register($user_input->username, $user_input->password)) {
                         return ActionFactory::show_error(false, '%tr%caption_error', array('%tr%error_invalid_password_or_username'));
                     }
                     $plugin_cookies->username = $user_input->username;
                     $plugin_cookies->password = $user_input->password;
                     return $user_input->selected_media_url == 'setup' ? ActionFactory::close_dialog_and_run(ActionFactory::show_title_dialog('%tr%dialog_successful_login')) : ActionFactory::close_dialog_and_run(ActionFactory::open_folder());
                     break;
                 case 'deregister_terminal':
                     if (!ZTVTerminal::deregister()) {
                         return ActionFactory::show_error(false, '%tr%caption_error', array('%tr%error_cant_logout'));
                     }
                     return $user_input->selected_media_url == 'setup' ? ActionFactory::close_dialog_and_run(ActionFactory::show_title_dialog('%tr%dialog_successful_login')) : ActionFactory::close_dialog_and_run(ActionFactory::open_folder());
                     break;
                 case 'register_cancel':
                     if (ZTVTerminal::login() && $user_input->selected_media_url != 'setup') {
                         return ActionFactory::close_dialog_and_run(ActionFactory::open_folder());
                     }
                     return ActionFactory::reset_controls($this->do_registration_form_defs($plugin_cookies));
                     break;
                 case 'profile':
                     $this->profile = $this->profiles[$user_input->profile];
                     if ($this->profile->require_password) {
                         return ActionFactory::show_dialog('%ext%<key_local>dialog_profile_password<p>' . $this->profile->title . '</p></key_local>', $this->do_profile_form_defs($plugin_cookies, !$this->profile->password ? 'set_profile_password' : 'change_profile_password'), true);
                     }
                     $json_reply = ZTVApi::call(ZTVApi::set_profile($this->profile));
                     if ($json_reply === false) {
                         return ActionFactory::show_error(false, '%tr%caption_error', array('%tr%error_cant_select_profile'));
                     }
                     break;
                 case 'check_profile_password':
                     if ($user_input->profile_password != $this->profile->password) {
                         return ActionFactory::show_error(false, '%tr%caption_error', array('%tr%error_invalid_password'));
                     }
                     if ($user_input->selected_media_url != 'setup') {
                         return ActionFactory::close_dialog_and_run(ActionFactory::open_folder());
                     }
                     $json_reply = ZTVApi::call(ZTVApi::set_profile($this->profile));
                     if ($json_reply === false) {
                         return ActionFactory::close_dialog_and_run(ActionFactory::show_error(false, '%tr%caption_error', array('%tr%error_cant_validate_password')));
                     }
                     break;
                 case 'change_profile_password':
                     if ($user_input->profile_password != $this->profile->password) {
                         return ActionFactory::show_error(false, '%tr%caption_error', array('%tr%error_invalid_password'));
                     }
                     if (!$user_input->profile_password_new) {
                         return ActionFactory::show_error(false, '%tr%caption_error', array('%tr%error_new_password_empty'));
                     }
                     if ($user_input->profile_password_new != $user_input->profile_password_repeat) {
                         return ActionFactory::show_error(false, '%tr%caption_error', array('%tr%error_passwords_mismatch'));
                     }
                     $json_reply = ZTVApi::call(ZTVApi::set_profile($this->profile, $user_input->profile_password));
                     return $json_reply === false ? ActionFactory::close_dialog_and_run(ActionFactory::show_error(false, '%tr%caption_error', array('%tr%error_cant_change_password'))) : ActionFactory::close_dialog_and_run(ActionFactory::show_title_dialog('%tr%dialog_successful_password_change'));
                     break;
                 case 'set_profile_password':
                     if (!$user_input->profile_password) {
                         return ActionFactory::show_error(false, '%tr%caption_error', array('%tr%error_new_password_empty'));
                     }
                     if ($user_input->profile_password != $user_input->profile_password_repeat) {
                         return ActionFactory::show_error(false, '%tr%caption_error', array('%tr%error_passwords_mismatch.'));
                     }
                     $json_reply = ZTVApi::call(ZTVApi::set_profile($this->profile, $user_input->profile_password));
                     return $json_reply === false ? ActionFactory::close_dialog_and_run(ActionFactory::show_error(false, '%tr%caption_error', array('%tr%error_cant_set_password'))) : ActionFactory::close_dialog_and_run(ActionFactory::show_title_dialog('%tr%dialog_successful_password_set'));
                     break;
                 case 'profile_cancel':
                     // TODO: Reset to the last selected profile instead of default.
                     $this->profile = $this->profiles[1];
                     $json_reply = ZTVApi::call(ZTVApi::set_profile($this->profile));
                     if ($json_reply === false) {
                         return;
                     }
                     return $user_input->selected_media_url != 'setup' ? ActionFactory::close_dialog_and_run(ActionFactory::open_folder()) : ActionFactory::close_dialog_and_run(ActionFactory::show_title_dialog('%ext%<key_local>dialog_default_profile_set<p>' . $this->profile->title . '</p></key_local>'));
                     break;
             }
         }
     }
     return ActionFactory::reset_controls($this->do_get_control_defs($plugin_cookies));
 }
 public function handle_user_input(&$user_input, &$plugin_cookies)
 {
     if (!isset($user_input->control_id)) {
         if ($plugin_cookies->require_restart) {
             ZTVDaemonController::restart($plugin_cookies->use_proxy == 'yes' ? array('-y' => $plugin_cookies->proxy_ip . ':' . $plugin_cookies->proxy_port) : array());
             $plugin_cookies->require_restart = false;
         }
         if (!ZTVTerminal::login()) {
             return ActionFactory::show_dialog('Введите регистрационные данные', $this->do_registration_form_defs($plugin_cookies), true);
         }
         $result = $this->load_profiles();
         if ($result) {
             if ($this->profile->require_password) {
                 return ActionFactory::show_dialog('Введите пароль профиля ' . $this->profile->title, $this->do_profile_form_defs($plugin_cookies, false), true);
             }
             return ActionFactory::open_folder();
         }
         return;
     } else {
         if ($user_input->action_type == 'confirm' || $user_input->action_type == 'apply') {
             switch ($user_input->control_id) {
                 case 'use_proxy':
                     $plugin_cookies->use_proxy = $user_input->use_proxy;
                     $plugin_cookies->proxy_ip = isset($plugin_cookies->proxy_ip) ? $plugin_cookies->proxy_ip : '192.168.0.1';
                     $plugin_cookies->proxy_port = isset($plugin_cookies->proxy_port) ? $plugin_cookies->proxy_port : '8080';
                     $plugin_cookies->require_restart = true;
                     break;
                 case 'proxy_ip':
                     $plugin_cookies->proxy_ip = $user_input->proxy_ip;
                     $plugin_cookies->require_restart = true;
                     break;
                 case 'proxy_port':
                     $plugin_cookies->proxy_port = $user_input->proxy_port;
                     $plugin_cookies->require_restart = true;
                     break;
                 case 'daemon_start_button':
                     ZTVDaemonController::start($plugin_cookies->use_proxy == 'yes' ? array('-y' => $plugin_cookies->proxy_ip . ':' . $plugin_cookies->proxy_port) : array());
                     break;
                 case 'daemon_restart_button':
                     ZTVDaemonController::restart($plugin_cookies->use_proxy == 'yes' ? array('-y' => $plugin_cookies->proxy_ip . ':' . $plugin_cookies->proxy_port) : array());
                     break;
                 case 'daemon_stop_button':
                     ZTVDaemonController::stop();
                     break;
                 case 'register_button':
                     return ActionFactory::show_dialog('Введите регистрационные данные', $this->do_registration_form_defs($plugin_cookies), true);
                     break;
                 case 'register_terminal':
                     if (is_null($user_input->username) || trim($user_input->username) === '') {
                         return ActionFactory::show_error(false, 'Ошибка', array('Идентификатор не может быть пустым.'));
                     } else {
                         if (is_null($user_input->password) || trim($user_input->password) === '') {
                             return ActionFactory::show_error(false, 'Ошибка', array('PIN-код не может быть пустым.'));
                         }
                     }
                     if (!ZTVTerminal::register($user_input->username, $user_input->password)) {
                         return ActionFactory::show_error(false, 'Ошибка', array('Неверный идентификатор или PIN-код.'));
                     }
                     $plugin_cookies->username = $user_input->username;
                     $plugin_cookies->password = $user_input->password;
                     if ($user_input->selected_media_url == 'setup') {
                         return ActionFactory::close_dialog_and_run(ActionFactory::show_title_dialog('Вход выполнен.'));
                     }
                     return ActionFactory::close_dialog_and_run(ActionFactory::open_folder());
                     break;
                 case 'deregister_terminal':
                     if (ZTVTerminal::deregister() && $user_input->selected_media_url == 'setup') {
                         return ActionFactory::close_dialog_and_run(ActionFactory::show_title_dialog('Выход выполнен.'));
                     }
                     return ActionFactory::show_error(false, 'Ошибка', array('Не удалось выполненить выход.'));
                     break;
                 case 'register_cancel':
                     if (ZTVTerminal::login() && $user_input->selected_media_url != 'setup') {
                         return ActionFactory::close_dialog_and_run(ActionFactory::open_folder());
                     }
                     return ActionFactory::reset_controls($this->do_registration_form_defs($plugin_cookies));
                     break;
                 case 'profile':
                     $this->profile = $this->profiles[$user_input->profile];
                     if ($this->profile->require_password) {
                         return ActionFactory::show_dialog('Введите пароль профиля ' . $this->profile->title, $this->do_profile_form_defs($plugin_cookies, $this->profile->require_password && !$this->profile->password), true);
                     }
                     if ($this->tv->set_profile($this->profile->id)) {
                         return ActionFactory::show_title_dialog('Профиль успешно установлен');
                     }
                     break;
                 case 'set_profile_password':
                     if (!$user_input->profile_password) {
                         return ActionFactory::show_error(false, 'Ошибка', array('Пароль не задан.'));
                     }
                     if ($user_input->profile_password != $user_input->profile_password_repeat) {
                         return ActionFactory::show_error(false, 'Ошибка', array('Введенные пароли не совпадают.'));
                     }
                     $result = $this->tv->api_call(array(HD::make_json_rpc_request('set_terminal_params', array('macaddr' => HD::get_mac_addr_dashed(), 'profile_id' => $this->profile->id)), HD::make_json_rpc_request('update_profile', array('macaddr' => HD::get_mac_addr_dashed(), 'profile_id' => $this->profile->id, 'profile_password' => $user_input->profile_password))));
                     if (count($result) == 2 && $result[0]->result && $result[1]->result) {
                         return ActionFactory::close_dialog_and_run(ActionFactory::show_title_dialog('Профиль успешно установлен'));
                     }
                     return;
                     break;
                 case 'verify_profile_password':
                     if ($user_input->profile_password != $this->profile->password) {
                         return ActionFactory::show_error(false, 'Ошибка', array('Неверный пароль.'));
                     }
                     if ($user_input->selected_media_url != 'setup') {
                         return ActionFactory::close_dialog_and_run(ActionFactory::open_folder());
                     }
                     $result = $this->tv->set_profile($this->profile->id);
                     if ($result) {
                         return ActionFactory::close_dialog_and_run(ActionFactory::show_title_dialog('Профиль успешно установлен'));
                     }
                     return;
                     break;
                 case 'profile_cancel':
                     $result = $this->tv->set_profile(1);
                     if (!$result) {
                         return;
                     }
                     if ($user_input->selected_media_url != 'setup') {
                         return ActionFactory::close_dialog_and_run(ActionFactory::open_folder());
                     }
                     $this->profile = $this->profiles[1];
                     return ActionFactory::close_dialog_and_run(ActionFactory::show_title_dialog('Установлен профиль по умолчанию(' . $this->profile->title . ')'));
                     break;
             }
         }
     }
     return ActionFactory::reset_controls($this->do_get_control_defs($plugin_cookies));
 }
 public function handle_user_input(&$user_input, &$plugin_cookies)
 {
     hd_print('Setup: handle_user_input:');
     foreach ($user_input as $key => $value) {
         hd_print("  {$key} => {$value}");
     }
     $need_close_dialog = false;
     $need_reset_controls = false;
     $post_action = null;
     if ($user_input->action_type === 'apply') {
         $control_id = $user_input->control_id;
         if ($control_id === 'edit_subscription') {
             return $this->session->do_get_edit_subscription_action($plugin_cookies, $this);
         } else {
             if ($control_id === 'apply_subscription') {
                 if ($user_input->user_name === '') {
                     return ActionFactory::show_error(false, 'Error', array('Subscription should be non-empty.'));
                 }
                 $plugin_cookies->user_name = $user_input->user_name;
                 $plugin_cookies->password = $user_input->password;
                 $this->session->logout();
                 try {
                     $this->session->try_login($plugin_cookies);
                 } catch (DuneException $e) {
                     $post_action = $e->get_error_action();
                 }
                 $need_close_dialog = true;
                 $need_reset_controls = true;
             } else {
                 if ($control_id === 'edit_pcode') {
                     return $this->do_get_edit_pcode_action();
                 } else {
                     if ($control_id === 'apply_pcode') {
                         try {
                             $this->session->api_change_pcode($user_input->current_pcode, $user_input->new_pcode, $user_input->new_pcode_copy);
                         } catch (DuneException $e) {
                             return $e->get_error_action();
                         }
                         $need_close_dialog = true;
                         $need_reset_controls = true;
                     }
                 }
             }
         }
     } else {
         if ($user_input->action_type === 'confirm') {
             $control_id = $user_input->control_id;
             $new_value = $user_input->{$control_id};
             hd_print("Setup: changing {$control_id} value to {$new_value}");
             if ($control_id === 'show_in_main_screen') {
                 $plugin_cookies->show_in_main_screen = $new_value;
                 if ($new_value === 'auto') {
                     $plugin_cookies->show_tv = 'lang(russian)';
                 } else {
                     $plugin_cookies->show_tv = $new_value;
                 }
             } else {
                 if ($control_id === 'bitrate' || $control_id == 'stream_server' || $control_id == 'http_caching' || $control_id == 'timeshift') {
                     try {
                         $this->session->api_set_setting($control_id, $new_value);
                     } catch (DuneException $e) {
                         return $e->get_error_action();
                     }
                 } else {
                     return null;
                 }
             }
             $need_reset_controls = true;
         }
     }
     if ($need_reset_controls) {
         $defs = $this->do_get_control_defs($plugin_cookies);
         $reset_controls_action = ActionFactory::reset_controls($defs, $post_action);
         if ($need_close_dialog) {
             return ActionFactory::close_dialog_and_run($reset_controls_action);
         }
         return $reset_controls_action;
     }
     return null;
 }