0

I am building a module and I set the cache in the form function, which will be retrieved in the insert_new_email function.

I can retrieve the cached data in the form function, but I can not in the insert_new_email function.

Below is the class I use for my mod.email_blast.php file

class email_blast {

    public $return_data;

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->EE =& get_instance();
        $this->EE->load->library('session');
        if (!class_exists('MCAPI'))
        {
            require PATH_THIRD.'email_blast/libraries/MCAPI.php';
        }
    }

    // ----------------------------------------------------------------

    public function form() {
        $service = $this->EE->TMPL->fetch_param('service', false);
        $api_key = $this->EE->TMPL->fetch_param('api_key', false);
        $list_id = $this->EE->TMPL->fetch_param('list_id', false);

        if($api_key == FALSE || $list_id == FALSE) {
            return "You are missing parameter(s)";
        }

        $this->EE->session->set_cache('email_blast', 'api_key', $api_key);
        $this->EE->session->set_cache('email_blast', 'list_id', $list_id);
        $this->EE->session->set_cache('email_blast', 'service', $service);

        // Build an array to hold the form's hidden fields
        $hidden_fields = array(
            'ACT'       => ee()->functions->fetch_action_id('Email_blast', 'insert_new_email'),
            'RET'       => ee()->functions->fetch_current_uri(),
            'API_KEY'   => $this->EE->session->cache('email_blast', 'api_key')
        );

        // Build an array with the form data
        $form_data = array(
            "id" => $this->EE->TMPL->form_id,
            "class" => $this->EE->TMPL->form_class,
            "hidden_fields" => $hidden_fields,
        );

        // Fetch contents of the tag pair, ie, the form contents
        $tagdata = $this->EE->TMPL->tagdata;

        $form = $this->EE->functions->form_declaration($form_data) . 
            $tagdata . "</form>";

        return $form;
    } 

    function insert_new_email()
    {
        $email = ee()->input->get_post('email');
        $email = trim(strip_tags($email));
        $api_key = $this->EE->session->cache('email_blast', 'api_key');
        $api = new MCAPI($api_key);
        $merge_vars = array ("OPTINIP" => $_SERVER['REMOTE_ADDR']);
        $list_id = $this->EE->session->cache('email_blast', 'list_id');
        $result = $api->listSubscribe($list_id, $email, $merge_vars, 'html', 'false', 'true', 'false', 'true');

        $data = array(
            'title'     => 'Mailing list',
            'heading'   => 'Thank you',
            'content'   => $list_id,
            'link'      => array($_POST['RET'], 'Site name here'),
            'email'     => $email,
        );

        ee()->output->show_message($data);
    }
}

1 Answer 1

4

The session cache only lasts for a single page request. So by the time your action gets called, that cache is gone. You'd be better off adding those to the form as hidden fields, and then grabbing them from POST in your action.

3
  • Thanks, I'll probably put them in the database, more secure. Commented Aug 23, 2013 at 20:07
  • 2
    Sure. Another technique is to encrypt the values before adding them as hidden fields. Safecracker does this. Commented Aug 23, 2013 at 20:08
  • I decided to add them into the main config file, easier for deploying into new sites, as we'll use ee-garage.com/nsm-config-bootstrap, thank you for the advice, it helped! Commented Aug 26, 2013 at 12:58

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.