最简洁的OpenID实现?

时间:2011-01-15 10:37:13

标签: php openid

是否有低于5K或至少低于10K的OpenID实现?

请使用以下设置使用http://beta.phpformatter.com/格式化代码以获得真实尺寸:

  

压痕:

     

缩进风格:{K&R (One true brace style)}
  缩进:{Tabs}
  开始缩进:[1]
  缩进:[1]

     

共同:

     

[x]删除所有评论   [x]删除空行
  [x]很好地对齐作业陈述   [ ]在if,while,for,foreach,declare和catch语句之后对条件发表评论

     

改进:

     

[x]删除仅使用分号(;)的行   [x]从perl评论(#)发表正常评论(//)   [x]制作长开标签(      

支架:

     

[x]括号内的空格- ( )
  [x]空括号内的空格- ( )
  [x]区块括号内的空格- [ ]
  空[x]

内空格- [ ]

编辑:

Alix Axel's answer似乎很棒,但并不完美,但已经到了那里。顺便说一句:这是6.4K所以它不再是最小的了,但我正在通过它来清理更多。

<?php
    class openID {
        function __construct( $url = null, $realm = null, $return = null, $redirect = true, $verify = false ) {
            $data = array( );
            if ( ( $verify !== true ) && ( self::Value( $_REQUEST, 'openid_mode' ) !== false ) ) {
                if ( strcmp( 'id_res', self::Value( $_REQUEST, 'openid_mode' ) ) === 0 ) {
                    $data[ 'openid.sig' ]          = $_REQUEST[ 'openid_sig' ];
                    $data[ 'openid.mode' ]         = 'check_authentication';
                    $data[ 'openid.signed' ]       = $_REQUEST[ 'openid_signed' ];
                    $data[ 'openid.assoc_handle' ] = $_REQUEST[ 'openid_assoc_handle' ];
                    if ( array_key_exists( 'openid_op_endpoint', $_REQUEST ) === true ) {
                        $data[ 'openid.ns' ] = 'http://specs.openid.net/auth/2.0';
                    }
                    foreach ( explode( ',', self::Value( $_REQUEST, 'openid_signed' ) ) as $value ) {
                        $data[ 'openid.' . $value ] = $_REQUEST[ 'openid_' . str_replace( '.', '_', $value ) ];
                    }
                    if ( preg_match( '~is_valid\s*:\s*true~i', self::CURL( self::__construct( $_REQUEST[ 'openid_identity' ], false, false, false, true ), $data, 'POST' ) ) > 0 ) {
                        return self::Value( $_REQUEST, 'openid_claimed_id', self::Value( $_REQUEST, 'openid_identity' ) );
                    }
                }
            } else if ( ( $result = self::CURL( $url ) ) !== false ) {
                $xml    = self::XML( $result );
                $server = strval( self::XML( $xml, '//xrd/service/uri', 0 ) );
                if ( empty( $server ) === true ) {
                    $server = strval( self::XML( $xml, '//head/link[@rel="openid.server" or @rel="openid2.provider"]/@href', 0 ) );
                }
                if ( self::URL( $server ) === true ) {
                    if ( $redirect === true ) {
                        $realm    = ( isset( $realm ) === true ) ? $realm : sprintf( '%s://%s/', $_SERVER[ "HTTPS" ] ? "https" : "http", $_SERVER[ 'HTTP_HOST' ] );
                        $return   = ( isset( $return ) === true ) ? $return : sprintf( '%s://%s', $_SERVER[ "HTTPS" ] ? "https" : "http", $_SERVER[ 'HTTP_HOST' ] . $_SERVER[ 'REQUEST_URI' ] );
                        $delegate = ( preg_match( '~http://specs[.]openid[.]net/auth/2[.]0/server~', $result ) > 0 ) ? 'http://specs.openid.net/auth/2.0/identifier_select' : $url;
                        if ( preg_match( '~rel="openid[.]delegate"|<[^>]*Delegate[^>]*>~i', $result ) > 0 ) {
                            $delegate = parent::Value( ph()->Text->Regex( $result, '<([^>]*)Delegate[^>]*>([^>]+)</\1Delegate>', 1 ), 0 );
                            if ( empty( $delegate ) === true ) {
                                $delegate = strval( self::XML( $xml, '//head/link[@rel="openid.delegate"]/@href', 0, $delegate ) );
                            }
                        }
                        if ( preg_match( '~rel="openid2[.]provider"|http://specs[.]openid[.]net/auth/2[.]0~i', $result ) > 0 ) {
                            $data[ 'openid.ns' ] = 'http://specs.openid.net/auth/2.0';
                            if ( preg_match( '~rel="openid2[.]local_id"|<(Local|Canonical)ID[^>]*>~i', $result ) > 0 ) {
                                $delegate = self::Value( self::Regex( $result, '<(Local|Canonical)ID[^>]*>([^>]+)</\1ID>', 1 ), 0 );
                                if ( empty( $delegate ) === true ) {
                                    $delegate = strval( self::XML( $xml, '//head/link[@rel="openid2.local_id"]/@href', 0, $delegate ) );
                                }
                            }
                        }
                        $data[ 'openid.mode' ]                                                                                = 'checkid_setup';
                        $data[ 'openid.return_to' ]                                                                           = $return;
                        $data[ 'openid.claimed_id' ]                                                                          = $data[ 'openid.identity' ] = $delegate;
                        $data[ 'openid.' . ( ( array_key_exists( 'openid.ns', $data ) === true ) ? 'realm' : 'trust_root' ) ] = $realm;
                        self::Redirect( sprintf( '%s%s%s', $server, ( strpos( $server, '?' ) !== false ) ? '&' : '?', http_build_query( $data, '', '&' ) ) );
                    }
                    return $server;
                }
            }
            return false;
        }
        function CURL( $url, $data = null, $method = "GET", $options = array( ) ) {
            $result = false;
            if ( ( extension_loaded( "curl" ) === true ) && ( $this->URL( $url ) === true ) ) {
                $curl = curl_init( $url );
                if ( is_resource( $curl ) === true ) {
                    curl_setopt( $curl, CURLOPT_FAILONERROR, true );
                    curl_setopt( $curl, CURLOPT_AUTOREFERER, true );
                    curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, true );
                    curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
                    curl_setopt( $curl, CURLOPT_SSL_VERIFYHOST, false );
                    curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, false );
                    if ( preg_match( "~^GET$~i", $method ) > 0 ) {
                        curl_setopt( $curl, CURLOPT_HTTPGET, true );
                    } else if ( preg_match( "~^POST$~i", $method ) > 0 ) {
                        curl_setopt( $curl, CURLOPT_POST, true );
                        curl_setopt( $curl, CURLOPT_POSTFIELDS, $data );
                    }
                    $result = curl_exec( $curl );
                    if ( $result !== false ) {
                        curl_close( $curl );
                    }
                }
            }
            return $result;
        }
        function Redirect( $url, $permanent = false ) {
            if ( headers_sent() !== true ) {
                header( "Location: " . $url, true, ( $permanent === true ) ? 301 : 302 );
            }
            exit( );
        }
        function Regex( $string, $pattern, $key = null, $modifiers = null, $flag = PREG_PATTERN_ORDER, $default = false ) {
            $matches = array( );
            if ( preg_match_all( "~" . $pattern . "~" . $modifiers, $string, $matches, $flag ) > 0 ) {
                if ( isset( $key ) === true ) {
                    return ( $key === true ) ? $matches : Value( $matches, $key, $default );
                }
                return true;
            }
            return $default;
        }
        function URL( $value ) {
            return (bool) filter_var( $value, FILTER_VALIDATE_URL );
        }
        function Value( $data, $key = null, $default = false ) {
            if ( isset( $key ) === true ) {
                foreach ( (array) $key as $value ) {
                    if ( is_object( $data ) === true ) {
                        $data = get_object_vars( $data );
                    }
                    if ( array_key_exists( $value, (array) $data ) !== true ) {
                        return $default;
                    }
                    $data = $data[ $value ];
                }
            }
            return $data;
        }
        function XML( $xml, $xpath = null, $key = null, $default = false ) {
            if ( extension_loaded( "SimpleXML" ) === true ) {
                libxml_use_internal_errors( true );
                if ( ( is_string( $xml ) === true ) && ( class_exists( "DOMDocument" ) === true ) ) {
                    $dom = new DOMDocument();
                    if ( $dom->loadHTML( $xml ) === true ) {
                        return $this->XML( simplexml_import_dom( $dom ), $xpath, $key, $default );
                    }
                } else if ( is_object( $xml ) === true ) {
                    if ( isset( $xpath ) === true ) {
                        $xml = $xml->xpath( $xpath );
                        if ( isset( $key ) === true ) {
                            $xml = $this->Value( $xml, $key, $default );
                        }
                    }
                    return $xml;
                }
            }
            return false;
        }
    }
    new openID( "https://www.google.com/accounts/o8/id" );
?>

3 个答案:

答案 0 :(得分:3)

减少Lightopenid

首先我真的没有得到你的文件限制。如果你问我,我发现它有点像b * llshit ....

你可以使用lightopenid的调用图(使用某种死代码检测器)来查看你正在使用的函数(只使用了几个)。很多这些大函数都没有使用标准,例如我相信你可以删除protected function request_streams($url, $method='GET', $params=array())上的大函数line 198.我还删除了一些其他函数,这些函数在{{1例子。我的最终lightopenid课程看起来像这样(我打赌你甚至可以在使用调用图/死代码检测器时消除更多代码)。

但是现在我将文件大小降低到了9.8KB。如果你压缩它,你可以进一步降低它。我把它降到了你想要的10K标记,但现在代码再也无法被人类阅读了。

代码搜索:

您还可以查看这些资源/搜索以查看是否有更轻的(我非常怀疑):

答案 1 :(得分:2)

我所知道的最简单的openid实现是LightOpenId

它有30kb的评论,所以如果你通过该格式化程序运行它会变得更小

编辑:我发现一个较小的here(重新格式化后只有6.6 kb)

答案 2 :(得分:1)

灵感来自lightopenid和部分phunction (取决于其他5种方法)

public static function OpenID($id, $realm = null, $return = null, $verify = true)
{
    $data = array();

    if (($verify === true) && (array_key_exists('openid_mode', $_REQUEST) === true))
    {
        $result = parent::Value($_REQUEST, 'openid_claimed_id', parent::Value($_REQUEST, 'openid_identity'));

        if (strcmp('id_res', parent::Value($_REQUEST, 'openid_mode')) === 0)
        {
            $data['openid.mode'] = 'check_authentication';

            foreach (array('ns', 'sig', 'signed', 'assoc_handle') as $key)
            {
                $data['openid.' . $key] = parent::Value($_REQUEST, 'openid_' . $key);

                if (strcmp($key, 'signed') === 0)
                {
                    foreach (explode(',', parent::Value($_REQUEST, 'openid_signed')) as $value)
                    {
                        $data['openid.' . $value] = parent::Value($_REQUEST, 'openid_' . str_replace('.', '_', $value));
                    }
                }
            }

            return (preg_match('~is_valid\s*:\s*true~', self::CURL(self::OpenID($result, false, false, false), array_filter($data, 'is_string'), 'POST')) > 0) ? $result : false;
        }
    }

    else if (($result = self::XML(self::CURL($id))) !== false)
    {
        $server = null;
        $protocol = array
        (
            array('specs.openid.net/auth/2.0/server', 'specs.openid.net/auth/2.0/signon', array('openid2.provider', 'openid2.local_id')),
            array('openid.net/signon/1.1', 'openid.net/signon/1.0', array('openid.server', 'openid.delegate')),
        );

        foreach ($protocol as $key => $value)
        {
            while ($namespace = array_shift($value))
            {
                if (is_array($namespace) === true)
                {
                    $server = strval(self::XML($result, sprintf('//head/link[contains(@rel, "%s")]/@href', $namespace[0]), 0));
                    $delegate = strval(self::XML($result, sprintf('//head/link[contains(@rel, "%s")]/@href', $namespace[1]), 0, $id));
                }

                else if (is_object($xml = self::XML($result, sprintf('//xrd/service[contains(type, "http://%s")]', $namespace), 0)) === true)
                {
                    $server = parent::Value($xml, 'uri');

                    if ($key === 0)
                    {
                        $delegate = 'http://specs.openid.net/auth/2.0/identifier_select';

                        if (strcmp($namespace, 'specs.openid.net/auth/2.0/server') !== 0)
                        {
                            $delegate = parent::Value($xml, 'localid', parent::Value($xml, 'canonicalid', $id));
                        }
                    }

                    else if ($key === 1)
                    {
                        $delegate = parent::Value($xml, 'delegate', $id);
                    }
                }

                if (ph()->Is->URL($server) === true)
                {
                    if (($realm !== false) && ($return !== false))
                    {
                        $data['openid.mode'] = 'checkid_setup';
                        $data['openid.identity'] = $delegate;
                        $data['openid.return_to'] = parent::URL($return, null, null);

                        if ($key === 0)
                        {
                            $data['openid.ns'] = 'http://specs.openid.net/auth/2.0';
                            $data['openid.realm'] = parent::URL($realm, false, false);
                            $data['openid.claimed_id'] = $delegate;
                        }

                        else if ($key === 1)
                        {
                            $data['openid.trust_root'] = parent::URL($realm, false, false);
                        }

                        parent::Redirect(parent::URL($server, null, $data));
                    }

                    return $server;
                }
            }
        }
    }

    return false;
}

<强>用法:

OpenID('https://www.google.com/accounts/o8/id');

PS:我已经更新了之前发布的代码,因为它不是最优的,容易出现一些错误。