public function actions()
{
return [
'social' => [
'class' => 'yii\authclient\AuthAction',
'successCallback' => [$this, 'successCallback'],
],
];
}
/**
* @param $client
*/
public function successCallback($client)
{
$day = 86400;
$client_id = Yii::$app->request->get('authclient');
$attributes = $client->getUserAttributes();
$email = '';
$sex = 0;
// VK
if($client instanceof VKontakte)
{
$auth_params = $client->getAccessToken()->getParams();
$email = ArrayHelper::getValue($auth_params,'email','');
// Аватарка из соц сети
$vk_data_response = $client->api('users.get','POST',['uids'=> $attributes['id'] , 'fields'=> 'photo_max_orig']);
if($vk_data_response = ArrayHelper::getValue($vk_data_response,'response',false))
{
$vk_data = array_shift($vk_data_response);
Yii::$app->session->setFlash('social_avatar', $vk_data['photo_max_orig']);
}
}
if($client instanceof YandexOAuth)
{
$ya_data_response = $client->api('info','GET',['format'=>'json']);
$email = array_shift($ya_data_response['emails']);
Yii::$app->session->setFlash('social_avatar', "https://avatars.yandex.net/get-yapic/{$ya_data_response['default_avatar_id']}/islands");
if(!empty($ya_data_response['sex']))
{
$sex = $ya_data_response['sex'] == 'female' ? UserProfile::SEX_FEMALE : UserProfile::SEX_MALE;
}
}
if($client instanceof Odnoklassniki)
{
$od_data_response =$client->api('','GET',[
'method' => 'users.getCurrentUser',
'application_key' => $client->application_public_key
]);
$sex = $od_data_response['gender'] == 'female' ? UserProfile::SEX_FEMALE : UserProfile::SEX_MALE;
if(isset($od_data_response['pic_1']))
{
Yii::$app->session->setFlash('social_avatar', $od_data_response['pic_1']);
}
}
if($client instanceof Facebook) {
try
{
$curl = new Curl;
$fb_curl_result = $curl->get("https://graph.facebook.com/{$attributes['id']}/picture?width=300&redirect=false");
$fb_data_responce = Json::decode($fb_curl_result);
if (isset($fb_data_responce['data'])) {
Yii::$app->session->setFlash('social_avatar', $fb_data_responce['data']['url']);
}
}
catch (Exception $e) {};
}
$user_social = UserSocial::findOne([
'client' => $client_id ,
'internal_id' => $attributes['id']
]);
if(!$user_social)
{
$user = new User;
if(!empty($email) && !$user = User::find()->where(['email'=>$email])->one())
{
$user = new User;
$user->email = $email;
}
// тут я шаманил у меня соц пользователи не имеют логина и пароля через валидатор авторизации они не пройдут так как пустые.
$user->scenario = 'social';
if($user->isNewRecord)
{
$user->username = '';
$user->password_hash = '';
}
// регистрируем в User
if($user->register())
{
$user_social = new UserSocial;
$user_social->user_id = $user->id;
$user_social->client = $client_id;
$user_social->internal_id = (string)$attributes['id'];
$user_social->save();
// $this->referralRegistration($user->id);
}
//$this->setSocialAvatarToProfile($user->id);
Yii::$app->session->set('auth_social',[
'client' => $client_id,
'attributes' => $attributes,
'token' => ''
]);
}
$user = $user_social->user;
Yii::$app->user->login($user , $day * 366);
}
function while_fetch_assoc($res)
{
$rows = [];
while($row = mysql_fetch_assoc($res)){
$rows[] = $row;
}
return $rows;
}
$rows = while_fetch_assoc($query);
echo json_encode($rows);
exit(true);
$.ajax({
type: 'post', dataType: 'json',
url: '/get_table.php',
data: {}
}).done(function(data){
$.each(data,function(key,item){
var $row = $('<tr><tr>'); // создаем tr
$('.table tbody')append($row);
console.log(item);
// АF12 смотрим что в консоле прешло
});
});
<?php
$rows = array();
while($row = mysql_fetch_assoc($res)){
$rows[] =<<<TRHTML
<tr>
<td>{$row['id']}</td>
<td>{$row['name']}</td>
</tr>
TRHTML;
}
$.each(data,function(key,item){
$('.table')append(item);
});
<?php
namespace app\models\traits;
use app\models\User;
/**
* Class UserRelationUserId
*
* @package app\models\traits
* @property \app\models\User $user
*/
trait UserRelationUserId
{
/**
* Relationship get user
* @return \yii\db\ActiveQuery
*/
public function getUser()
{
/**@var \yii\db\ActiveRecord $this */
return $this->hasOne(User::className(), ['id' => 'user_id']);
}
}
source config_mysite
{
type = mysql
sql_host = localhost
sql_user = root
sql_pass = mysql
sql_db = new_db
sql_port = 3306
sql_query_pre = SET NAMES utf8
sql_query_pre = SET CHARACTER SET utf8
}
source mysite_selposts :config_mysite
{
sql_query = \
SELECT `id`, `text` FROM sel_posts
sql_field_string = text
}
namespace app\controllers\base;
use Yii;
class BaseController extends \yii\web\Controller
{
/**
* @param string $id
* @param \yii\base\Module $module
* @param array $config
*/
public function __construct($id, $module, $config = [])
{
// DI
Yii::$container->set('yii\widgets\LinkPager', ['maxButtonCount' => 5]);
return parent::__construct($id,$module,$config);
}
}
namespace app\controllers;
use Yii;
class SiteController extends base\BaseController
{
//...
}
public function init() {
Yii::$container->set('yii\widgets\LinkPager', ['maxButtonCount' => 5]);
}
ini_set('session.gc_maxlifetime', 120960);
ini_set('session.cookie_lifetime', 120960);
ini_set('session.save_path', __DIR__.'/../sessions/');
class DB {
public static $pdo = null;
public static function connect(){
if(self::$pdo === null ){
self::$pdo = connectDB($host,$user,$pass,$db,$charset);
}
return self::$pdo;
}
}
<?php
return [
'host => 'localhost', 'username' => 'root' , 'password' => 122
];
$config = include(PATH_CONFIG);