0

我必须在 CodeIgniter 中构建的应用程序的现有表单中添加一个 Facebook 类似按钮。

除非用户使用官方 FB 之类的按钮代码单击(可怕,我知道。),否则该表单无效。我不习惯编写点火器代码,我的 PHP 知识有限。非常感谢扩展我的代码的任何帮助。

目前我有以下内容:

    public function likeGate( ) {

    if( $this->hasUserPerms() != true )
    {
        $this->tplVars['js_footer'] = 'app.fb.geifPerms();';
    }
    return false;
}

用于验证:

    public function getPostDetails()
{
    header('Content-Type: application/json');

    $errors = array();
    $fields = array(
        'liked' => function( $value ){

            if( $this->tplVars['perms'] == false || $this->tplVars['likes'] == false )
            {
            return false;
            }
            else
            {
            return true;
            }

        },

        'full_name' => function( $value ){

            if( strlen( $value ) > 5 && preg_match('/^[a-zA-ZàáâäãåąćęèéêëìíîïłńòóôöõøùúûüÿýżźñçčšžÀÁÂÄÃÅĄĆĘÈÉÊËÌÍÎÏŁŃÒÓÔÖÕØÙÚÛÜŸÝŻŹÑßÇŒÆČŠŽ∂ð ,.\'-]+$/iu', $value ) == 1 )
            {
                return true;
            }
            else
            {
                return false;
            }
        },

        'email' => function( $value )
        {
            if( preg_match( '/^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/', $value ) )
            {
                return true;
            }
            return false;
        },

        'terms' => function( $value )
        {
            if( $value !== 'agreed' )
            {
                return false;   
            }

            return true;
        },

        'card_number' => function( $value )
        {
            if( !empty( $value ) && ( strlen( $value ) < 11 || strlen( $value ) > 12 )  ) // Maybe add further card validation
            {
                return false;
            }
            return true;
        }
    );

    foreach( $fields as $key => $field )
    {
        if( ( !isset( $_POST[$key] ) && $key != 'card_number' ) ) {
            $errors[$key] = 'This is a required field'; 

        } else if( isset( $_POST[$key] ) && $field($_POST[$key]) == false ) {
            $errors[$key] = 'Invalid value, please correct it and try again.';  
        }
    }

    if( !empty( $errors ) )
    {
        $return = array( 'success' => false, 'errors' => $errors );

    }
    else if( $this->userHasSubmission() )
    {
        $return = array('success' => false, 'errors' => array( 'full_name' => 'User already has completed a submission'));

    }
    else
    {
        $data   = array_merge( $_POST, array('fbid' => $this->facebook->getUser()));
        $return = array('success' => true, 'errors' => array());

        $this->load->model('Henhouse_model');
        $this->Henhouse_model->insertRow( $data );
    }


    echo json_encode( $return );
    exit;
}

用户权限:

    private function hasUserPerms()
{
    if( isset( $_SESSION['fb_token'] ) )
    {
        $this->facebook->setAccessToken( $_SESSION['fb_token'] );
    }
    else if( isset( $_GET['code'] ) )
    {
        $this->facebook->setAccessToken($_GET['code']);     
    }

    $this->facebook->getAccessToken();
    $fbid = $this->facebook->getUser();

    // check if we have valid user
    if( $fbid )
    {
        try
        {
            // Proceed knowing you have a logged in user who's authenticated.
            $fb_user_profile = $this->facebook->api('/me');
            $_SESSION['fb_token'] = $this->facebook->getAccessToken();
        }
        catch ( FacebookApiException $e ) 
        {
            return false;
        }
    }
    else
    {
        return false;
    }

    return true;
}

private function userLikesPage()
{
    if( !$this->hasUserPerms() )
    {
        return false;
    }

    $like_status = $this->facebook->api('/me/likes/140681946901');

    if( isset( $like_status ) && $like_status['data'] == true )
    {
        return true;
    }

    return false;
}
4

0 回答 0