Subversion Repositories social

[/] [stable/] [extension/] [social/] [classes/] [axsocialfacebookmobileapi.php] - Blame information for rev 1

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1 myxwr
<?php
2
 
3
class axSocialFacebookMobileAPI implements axSocialInterface
4
{
5
    public function __construct( $blockName )
6
    {
7
        $this->baseURI = 'http://m.facebook.com';
8
        $this->curlOptions['cookieFile'] = 'var/log/fb'.eZUser::currentUserID().'.cookie.txt';
9
        $this->curlOptions['UserAgent'] = $_SERVER['HTTP_USER_AGENT'];
10
        $this->ini = eZINI::instance( 'axsocial.ini' );
11
        if( $this->ini->hasGroup( axSocialInterface::INIBLOCKPREFIX . $blockName ) )
12
        {
13
            $this->accessOptions = $this->ini->group( axSocialInterface::INIBLOCKPREFIX . $blockName );
14
        }
15
        else
16
        {
17
            eZDebug::writeError( "Undefined group: '$blockName' in " . $this->ini->FileName, __METHOD__ );
18
            return null;
19
        }
20
    }
21
 
22
 
23
    public function connect()
24
    {
25
        $login = $this->accessOptions['Login'];
26
        $password = $this->accessOptions['Password'];
27
 
28
        $this->sendCurlRequest( $this->baseURI );
29
 
30
        $fbLoginURI = $this->getFormActionURL( $this->lastRequest['data'], 'login_form' );
31
        $fbLoginParams = $this->getInputValuesFromFormContent( $this->lastRequest['data'], 'login_form' );
32
        $fbLoginParams .= '&email=' . $login . '&pass=' . $password;
33
 
34
        $this->sendCurlRequest( $fbLoginURI, $fbLoginParams );
35
    }
36
 
37
 
38
    public function disconnect()
39
    {
40
        return true;
41
    }
42
 
43
    /*!
44
     * post( $message ):
45
     * @TODO: ProfileID or WallID to post contents to can be choosed dynamicly or per ini
46
     */
47
    public function post( $message )
48
    {
49
        $this->sendCurlRequest( $this->baseURI );
50
 
51
        $fbPostMessageURI = $this->baseURI . $this->getFormActionURL( $this->lastRequest['data'], 'composer_form' );
52
        $fbPostParams = $this->getInputValuesFromFormContent( $this->lastRequest['data'], 'composer_form' );
53
        $fbPostParams .= '&status=' . $message;
54
 
55
        $this->sendCurlRequest( $fbPostMessageURI, $fbPostParams );
56
    }
57
 
58
    /*!
59
     *
60
     */
61
    public function sendCurlRequest( $url, $postData = false )
62
    {
63
        $curl = curl_init();
64
 
65
        if( $postData !== false && $postData != '' )
66
        {
67
            curl_setopt( $curl, CURLOPT_POST, 1 );
68
            curl_setopt( $curl, CURLOPT_POSTFIELDS, $postData );
69
        }
70
 
71
        curl_setopt( $curl, CURLOPT_URL,              $url );
72
        curl_setopt( $curl, CURLOPT_FOLLOWLOCATION,   1 );
73
        curl_setopt( $curl, CURLOPT_RETURNTRANSFER,   1 );
74
        curl_setopt( $curl, CURLOPT_HEADER,           0 );     // output headers above page content
75
        curl_setopt( $curl, CURLINFO_HEADER_OUT,      true );      // also get headers via curlinfo (not necessary)
76
        curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER,   false );
77
        curl_setopt( $curl, CURLOPT_ENCODING,         "" );
78
        curl_setopt( $curl, CURLOPT_COOKIEFILE,       $this->curlOptions['cookieFile'] );
79
        curl_setopt( $curl, CURLOPT_COOKIEJAR,        $this->curlOptions['cookieFile'] );
80
        curl_setopt( $curl, CURLOPT_USERAGENT,        $this->curlOptions['UserAgent'] );
81
 
82
        $this->lastRequest['data'] = curl_exec( $curl );
83
        $this->lastRequest['header'] = curl_getinfo( $curl );
84
 
85
        curl_close( $curl );
86
        return $this->lastRequest;
87
    }
88
 
89
    public function getInputValuesFromFormContent( $contents, $form_id )
90
    {
91
        $inputParams = array();
92
        $doc = new DOMDocument();
93
        if( $doc->loadhtml( $contents ) )
94
        {
95
            $xpath = new DOMXpath( $doc );
96
            foreach( $xpath->query( '//form[@id="'.$form_id.'"]//input' ) as $eInput )
97
            {
98
                $inputParams[$eInput->getAttribute( 'name' )] = $eInput->getAttribute( 'value' );
99
            }
100
        }
101
        $tmp = array();
102
        foreach( $inputParams as $k => $v )
103
        {
104
            $tmp[] = $k . '=' . $v;
105
        }
106
        $inputParams = join( '&', $tmp );
107
        return $inputParams;
108
    }
109
 
110
    public function getFormActionURL( $contents, $form_id )
111
    {
112
        $inputParams = array();
113
        $doc = new DOMDocument();
114
        if( $doc->loadhtml( $contents ) )
115
        {
116
            $xpath = new DOMXpath( $doc );
117
            foreach( $xpath->query( '//form[@id="'.$form_id.'"]' ) as $eForm )
118
            {
119
                $inputParams[] = $eForm->getAttribute( 'action' );
120
            }
121
        }
122
        return $inputParams[0];
123
    }
124
 
125
    private $ini;
126
    private $connection;
127
    private $accessOptions;
128
    private $baseURI;
129
    private $curlOptions;
130
    private $lastRequest;
131
}
132
 
133
 
134
?>