/**
  * Module settings
  *  
  * @access public
  */
 function settings()
 {
     $message = '';
     $message_type = '';
     $this->EE->load->library('hyperlink_lib');
     //save
     if (isset($_POST['button_submit'])) {
         $validation_errors = array();
         //fetch settings
         $settings = array();
         $settings["screenshot_service"] = $this->EE->input->post('screenshot_service');
         $settings["screenshot_services"] = $this->EE->input->post('screenshot_services');
         $settings["screenshot_dir"] = $this->EE->input->post('screenshot_dir');
         $settings["publish_entry_with_invalid_links"] = $this->EE->input->post('publish_entry_with_invalid_links');
         $settings["schedule_validation_of_links"]['schedule'] = isset($_POST['schedule_validation_of_links']['schedule']);
         $settings["schedule_validation_of_links"]['change_status'] = $_POST['schedule_validation_of_links']['change_status'];
         $settings = $this->_settings_merge($this->EE->hyperlink_lib->default_settings, $settings);
         //validate
         $this->EE->lang->load('form_validation');
         if ($settings['screenshot_service'] == 'GrabzIt') {
             if (!$settings["screenshot_services"]["GrabzIt"]["api_key"]) {
                 $validation_errors["screenshot_services"]["GrabzIt"]["api_key"] = sprintf(lang("required"), 'API key');
             }
             if (!$settings["screenshot_services"]["GrabzIt"]["api_secret"]) {
                 $validation_errors["screenshot_services"]["GrabzIt"]["api_secret"] = sprintf(lang("required"), 'API secret');
             }
             $max_height = $this->EE->hyperlink_lib->grabzit_packages[$settings["screenshot_services"]["GrabzIt"]["service_package"]]['max_height'];
             $max_width = $this->EE->hyperlink_lib->grabzit_packages[$settings["screenshot_services"]["GrabzIt"]["service_package"]]['max_width'];
             if ($max_width < (int) $settings["screenshot_services"]["GrabzIt"]["image_width"]) {
                 $validation_errors["screenshot_services"]["GrabzIt"]["image_width"] = sprintf(lang("msg_error_image_width_too_big"), $max_width);
             }
             if ($max_height < (int) $settings["screenshot_services"]["GrabzIt"]["image_height"]) {
                 $validation_errors["screenshot_services"]["GrabzIt"]["image_height"] = sprintf(lang("msg_error_image_height_too_big"), $max_height);
             }
             if ((int) $settings["screenshot_services"]["GrabzIt"]["image_width"] < 20) {
                 $validation_errors["screenshot_services"]["GrabzIt"]["image_width"] = sprintf(lang("msg_error_image_width_too_small"), 20);
             }
             if ((int) $settings["screenshot_services"]["GrabzIt"]["image_height"] < 20) {
                 $validation_errors["screenshot_services"]["GrabzIt"]["image_height"] = sprintf(lang("msg_error_image_height_too_small"), 20);
             }
             if (!$settings["screenshot_dir"]) {
                 $validation_errors["screenshot_dir"] = sprintf(lang("required"), lang("label_download_directory"));
             }
             if ($settings["screenshot_services"]["GrabzIt"]["api_key"] && $settings["screenshot_services"]["GrabzIt"]["api_secret"]) {
                 include_once "libraries/grabzit/GrabzItClient.class.php";
                 try {
                     $grabzIt = new GrabzItClient($settings["screenshot_services"]["GrabzIt"]["api_key"], $settings["screenshot_services"]["GrabzIt"]["api_secret"]);
                     $id = $grabzIt->TakePicture("http://www.google.com", null, null, 1024, 768, $max_width, $max_height);
                 } catch (Exception $e) {
                     if (strpos($e->getMessage(), 'Image height too large') !== FALSE) {
                         $validation_errors["screenshot_services"]["GrabzIt"]["service_package"] = lang("msg_error_invalid_service_package");
                     } elseif (strpos($e->getMessage(), 'Invalid signature detected') !== FALSE) {
                         $validation_errors["screenshot_services"]["GrabzIt"]["api_secret"] = $e->getMessage();
                     } else {
                         $validation_errors["screenshot_services"]["GrabzIt"]["api_key"] = $e->getMessage();
                     }
                 }
             }
         }
         if (count($validation_errors)) {
             $message = lang('msg_error_settings_validaton_failed');
             $message_type = 'error';
         } else {
             $data = array('settings' => base64_encode(serialize($settings)));
             $this->EE->db->update('exp_hyperlink_settings', $data, 'site_id = ' . $this->EE->config->item('site_id'));
             $message = lang('msg_settings_saved');
             $message_type = 'success';
         }
     }
     $sites = $this->EE->db->select("*")->from('exp_sites')->order_by('site_id', 'ASC')->get()->result_array();
     //** -----------------------------
     //**	directory options
     //** -----------------------------
     $directory_options = array('' => lang('label_choose_directory'));
     foreach ($sites as $site) {
         $site_dirs = array();
         foreach ($this->EE->hyperlink_lib->upload_preferences() as $preference) {
             if ($preference["site_id"] == $site["site_id"]) {
                 $site_dirs[$preference["id"]] = $preference["name"];
             }
         }
         $directory_options[$site["site_label"]] = $site_dirs;
     }
     //** -----------------------------
     //**	grabzit_packages
     //** -----------------------------
     $grabzit_packages = array();
     foreach ($this->EE->hyperlink_lib->grabzit_packages as $package_id => $package) {
         $grabzit_packages[$package_id] = $package["name"];
     }
     //** -----------------------------
     //**	statuses
     //** -----------------------------
     $statuses = array('' => lang('label_status_do_not_change'));
     foreach ($this->EE->db->select("status")->from("exp_statuses")->get()->result() as $status) {
         $statuses[$status->status] = $status->status;
     }
     //** -----------------------------
     //**	get ACT
     //** -----------------------------
     $act_id = (int) @$this->EE->db->select('action_id')->from('exp_actions')->where('class', 'Hyperlink')->where('method', 'hyperlink_schedule_validation_of_links')->get()->row()->action_id;
     $hyperlink_schedule_validation_url = $this->EE->functions->create_url('/') . '?ACT=' . $act_id;
     $vars = array("settings" => isset($_POST['button_submit']) ? $settings : $this->EE->hyperlink_lib->settings, "directory_options" => $directory_options, "grabzit_packages" => $grabzit_packages, "statuses" => $statuses, "hyperlink_schedule_validation_url" => $hyperlink_schedule_validation_url, "message" => $message, "message_type" => $message_type, "validation_errors" => isset($validation_errors) ? $validation_errors : null);
     return $this->_view('settings', $vars, TRUE, lang('title_settings'));
 }
 /**
  * Take screenshot
  *
  * @return	
  */
 function take_screenshot($hyperlink_id, $screenshot_id = FALSE)
 {
     //**
     //**	Get info about link
     //**
     $query = $this->EE->db->select('*')->from('exp_hyperlink')->where('hyperlink_id', $hyperlink_id)->where('hyperlink_http_status >=', 200)->where('hyperlink_http_status <', 400)->get();
     if ($query->num_rows == 0) {
         return FALSE;
     }
     $hyperlink = $query->row_array();
     //**
     //**	Load settings
     //**
     $this->_load_settings($hyperlink["site_id"]);
     //**
     //**	No Dir, No Screenshot
     //**
     if (!$this->settings['screenshot_dir']) {
         return FALSE;
     }
     //**
     //**	No Service, No Screenshot
     //**
     if (!$this->settings['screenshot_service']) {
         return FALSE;
     }
     //**	---------------------------------------------
     //**	GrabzIt
     //**	---------------------------------------------
     if ($this->settings['screenshot_service'] == 'GrabzIt') {
         include_once "grabzit/GrabzItClient.class.php";
         //if EXPIRED, get new request
         if ($hyperlink['screenshot_status'] == 'expired') {
             $screenshot_id = false;
         }
         try {
             //one connect is ok
             global $__GRABZIT_CLIENT__;
             if (!isset($__GRABZIT_CLIENT__)) {
                 $__GRABZIT_CLIENT__ = new GrabzItClient($this->settings['screenshot_services']['GrabzIt']['api_key'], $this->settings['screenshot_services']['GrabzIt']['api_secret']);
             }
             if (!$screenshot_id) {
                 $screenshot_id = (string) $__GRABZIT_CLIENT__->TakePicture($hyperlink['hyperlink_url'], null, null, $this->settings['screenshot_services']['GrabzIt']['browser_width'], $this->settings['screenshot_services']['GrabzIt']['browser_height'], $this->settings['screenshot_services']['GrabzIt']['image_width'], $this->settings['screenshot_services']['GrabzIt']['image_height']);
                 if ($screenshot_id) {
                     $data = array('screenshot_id' => $screenshot_id, 'screenshot_status' => 'send');
                 } else {
                     $data = array('screenshot_error' => 'GrabzIt request failed', 'screenshot_status' => 'error');
                 }
                 $this->EE->db->update('exp_hyperlink', $data, 'hyperlink_id = ' . (int) $hyperlink_id);
             }
             $screenshotTaked = 0;
             //get status
             if ($screenshot_id) {
                 $status = $__GRABZIT_CLIENT__->GetStatus($screenshot_id);
                 if ($status->Processing) {
                     $data = array('screenshot_id' => $screenshot_id, 'screenshot_status' => 'processing');
                     $this->EE->db->update('exp_hyperlink', $data, 'hyperlink_id = ' . (int) $hyperlink_id);
                 }
                 if ($status->Cached) {
                     $data = array('screenshot_id' => $screenshot_id, 'screenshot_status' => 'cached');
                     $this->EE->db->update('exp_hyperlink', $data, 'hyperlink_id = ' . (int) $hyperlink_id);
                     $screenshotTaked = 1;
                 }
                 if ($status->Expired) {
                     $data = array('screenshot_status' => 'expired', 'screenshot_error' => $status->Message);
                     $this->EE->db->update('exp_hyperlink', $data, 'hyperlink_id = ' . (int) $hyperlink_id);
                     //log errors
                     $this->errors[] = $status->Message;
                 }
             }
             if ($screenshotTaked) {
                 $screenshot = $__GRABZIT_CLIENT__->GetPicture($screenshot_id);
                 $screenshot_name = 'screenshot-grabzit-' . uniqid() . '.jpg';
                 $screenshot_dir = $this->settings['screenshot_dir'];
                 if ($this->save_image($screenshot_name, $screenshot, $screenshot_dir)) {
                     $data = array('screenshot_name' => $screenshot_name, 'screenshot_dir' => $screenshot_dir, 'screenshot_status' => 'ok', 'screenshot_error' => null);
                     $this->EE->db->update('exp_hyperlink', $data, 'hyperlink_id = ' . (int) $hyperlink_id);
                 }
             }
         } catch (Exception $e) {
             $this->errors[] = 'Caught exception: ' . $e->getMessage();
             $data = array('screenshot_status' => 'error', 'screenshot_error' => $e->getMessage());
             $this->EE->db->update('exp_hyperlink', $data, 'hyperlink_id = ' . (int) $hyperlink_id);
             return FALSE;
         }
     }
 }