Blame view

src/lib/socialAuth/OpenID/socialAuthOpenIDCustomFinish.class.php 10 KB
1ea3b987   Administrator   maby first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
  <?php
  
  /**
   * socialAuthOpenIDCustomFinish Class
   *
   * @author		Roman
   * @version         1.0.20111014
   */
  class socialAuthOpenIDCustomFinish extends socialAuthOpenID
  {
      ///////////////////////////////////////////////////////////////////////////
      
      public function init()
      {
          $store      = new Auth_OpenID_FileStore( sys_get_temp_dir() );        
          $consumer   = new Auth_OpenID_Consumer( $store );
  
          // Завершаем процесс авторизации, используя ответ сервера.
          $return_to = core::route( '@user_login_social?mechanism=openid_finish', true ) .'?'.$_SERVER['QUERY_STRING'];
  
          $openid_url = str_replace( array('http://','https://'), '', $this->openid_url );
          $openid_url = preg_replace( '/\/+$/', '', $openid_url );
  
          $response = $consumer->complete($return_to);
  
          switch( $response->status )
          {
              case Auth_OpenID_CANCEL:
                  throw new kException( core::i18n( 'w23_error_openid_check' ) );
                  break;
                  
              case Auth_OpenID_FAILURE:
                  throw new kException( core::i18n( 'w23_error_auth' ).$response->message );
                  break;
                  
              case Auth_OpenID_SUCCESS:            
                  $openid = $response->getDisplayIdentifier();
  
                  ///////////////////////////////////////////////////////////////
  
                  $sreg_resp = Auth_OpenID_SRegResponse::fromSuccessResponse($response, false);
                  $data1 = $sreg_resp->contents();
  
                  if( empty($data1) && strpos($openid, '.livejournal.com')===false && strpos($openid, 'openid.yandex.ru')===false )
                  {
                      $temp_args = isset($response->message->args) ? (array) $response->message->args : array();
                      
                      $data1 = array();
                      
                      if( !empty($temp_args) )
                      {                    
                          foreach( $temp_args['keys'] as $k => $v )
                          {
                              $data1[ str_replace( 'sreg.', '', $v['1'] ) ] = strval($temp_args['values'][$k]);
                          }
                      }                    
                  }
                  
                  $ax = new Auth_OpenID_AX_FetchResponse();
                  $obj = $ax->fromSuccessResponse($response);
  
                  $data2 = isset($obj->data) ? $obj->data : array();
                  
                  if( !empty($data2) )
                  {
                      $temp = array();
  
                      foreach( 
                          array( 
                              'http://axschema.org/namePerson/friendly'   => 'nickname',
                              'http://axschema.org/namePerson/first'      => 'firstname',
                              'http://axschema.org/namePerson/last'       => 'lastname',
                              'http://axschema.org/birthDate'             => 'dob',
                              'http://axschema.org/person/gender'         => 'gender',
                              'http://axschema.org/pref/language'         => 'language',
                              'http://axschema.org/pref/timezone'         => 'timezone',
                              'http://axschema.org/contact/email'         => 'email',
                              'http://axschema.org/contact/country/home'  => 'country',
                              
                          ) as $key => $val )
                      {
                          if( isset($data2[$key]['0']) )
                          {
                              $temp[$val] = $data2[$key]['0'];
                          } 
                      }
                      
                      if( !isset($temp['nickname']) )
                      {
                          $temp['nickname'] = $openid;
                      }
                      
                      $data2 = $temp;
                      
                      unset($temp);
                  }
                  
                  $data = array_merge( $data1, $data2 );
  
                  if( 
                      empty($data) || 
                      !isset($data['nickname']) || 
                      strlen(trim($data['nickname']))<=0 || 
                      strpos(trim($data['nickname']), '.livejournal.com')!==false ||
                      strpos(trim($data['nickname']), 'openid.yandex.ru')!==false
                    )
                  {
                      if( strpos($openid, '.livejournal.com')!==false )
                      {
                          if( preg_match( '#^https?\:\/\/(.+)\.livejournal\.com.*#i', $openid, $match ) )
                          {
                              if( isset($match['1']) && strlen($match['1'])>0 )
                              {
                                  $data['nickname'] = trim($match['1']);
                              }
                              
                              $file = file_get_contents( $openid.'data/foaf' );
                              
                              if( strlen($file)>10 )
                              {
                                  $xml = simplexml_load_string( str_replace( 'foaf:', 'foaf_', $file ) );    
  
                                  if( $xml )
                                  {
                                      $data['nickname'] = strval( $xml->{'foaf_Person'}->{'foaf_nick'} );
                                      $data['fullname'] = strval( $xml->{'foaf_Person'}->{'foaf_name'} );
                                      $data['dob'] = strval( $xml->{'foaf_Person'}->{'foaf_dateOfBirth'} );
                                  }
                              }
                          } 
                      }
                      else if( strpos($openid, 'openid.yandex.ru')!==false )
                      {
                          if( preg_match( '#^https?\:\/\/openid\.yandex\.ru/([^\/]+)#i', $openid, $match ) )
                          {
                              if( isset($match['1']) && strlen($match['1'])>0 )
                              {
                                  $data['nickname'] = trim( preg_replace( '/\/+$/', '', $match['1']) );
                              }                                
                          }
                      }
                  }
  
                  if( empty($data) || !isset($data['nickname']) || strlen($data['nickname'])<=0 )
                  {
                      throw new kException( core::i18n( 'w23_error_empty_answer' ) );
                  }
  
                  ///////////////////////////////////////////////////////////////
  
                  $user = array(
                      'id'            => $data['nickname'],
                      'login'         => $data['nickname'],
                      'email'         => ( isset($data['email']) ? $data['email'] : '' ),
                      'email_fake'    => $data['nickname'].'@'.$openid_url,
                      'name'          => ( ( isset($data['fullname']) && strlen($data['fullname'])>0 ) ? $data['fullname'] : '' ),
                      );
                      
                  if( strlen($user['name'])<=0 )
                  {
                      if( ( isset($data['firstname']) && strlen($data['firstname'])>0 ) || ( isset($data['lastname']) && strlen($data['lastname'])>0 ) )
                      {
                          $user['name'] = 
                              trim(
                                  ( ( isset($data['firstname']) && strlen($data['firstname'])>0 ) ? trim($data['firstname']).' '  : '' ).
                                  ( ( isset($data['lastname']) && strlen($data['lastname'])>0 )   ? trim($data['lastname'])       : '' )
                                  );
                      }
                      else
                      {
                          $user['name'] = $data['nickname'];
                      }
                  }
  
                  return 
                      socialAuth::userLoginOrRegisterIfNotExists( 
                          array(   
                              'login'         => 'openid__'.$openid_url.'__'.$user['login'],
                              'email'         => $user['email_fake'],
                              'name'          => $user['name'],
                              'bithday'       => ( isset($data['dob']) && strlen($data['dob'])>0          ? date( 'Y-m-d', strtotime($data['dob']) )  : null ),
                              'gender'        => ( isset($data['gender']) && strlen($data['gender'])>0    ? ( $data['gender']=='M' ? 1 : 0 )          : null ),
                              'region_id'     => null,
                              'about_me'      => null,
                              'interests'     => null,
                              'vote'          => null,
                              'contact_icq'   => null,
                              'options'       => etc::arr2hstore( 
                                                      array( 
                                                          'is_social'     => 1,
                                                          'id'            => $user['id'],
                                                          'login'         => $user['login'],
                                                          'email'         => $user['email'],
                                                          'name'          => $user['name'],
                                                          'location'      => ( isset($data['country']) ? $data['country'] : '' ),
                                                          'timezone'      => ( isset($data['timezone']) ? $data['timezone'] : '' ),
                                                          'lang'          => ( isset($data['language']) ? $data['language'] : '' ),
                                                          )
                                                      ),
                                                      
                              )                           
                          );
                  break;
                  
              default:
                  throw new kException( core::i18n( 'w23_error_unknown' ) );
                  break;
          }
          
          return false;
      }
      
      ///////////////////////////////////////////////////////////////////////////
  }