Created
December 3, 2015 15:11
-
-
Save maximzasorin/71c0bfc46247f7c7114c to your computer and use it in GitHub Desktop.
Авторизация для комментариев с помощью uLogin (HostCMS)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Передаем токен на сервер для запуска скрипта авторизации | |
$.ajax({ | |
method: 'POST', | |
url: '/ulogin/', | |
data: $.param({ | |
ajax: 1, | |
token: token | |
}), | |
success: function() { | |
// Обновляем страницу | |
location.reload(); | |
} | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
define('NETWORK_PROPERTY_ID', 342); | |
define('IDENTITY_PROPERTY_ID', 343); | |
define('AVATAR_PROPERTY_ID', 344); | |
// Авторизация пользователя | |
if (Core_Array::getPost('token')) | |
{ | |
// Данные с сервера | |
$json = file_get_contents('http://ulogin.ru/token.php?token=' . Core_Array::getPost('token') . '&host=' . $_SERVER['HTTP_HOST']); | |
$data = json_decode($json, true); | |
if (array_key_exists('error', $data)) | |
{ | |
header(' ', true, 400); | |
print $data['error']; | |
exit(); | |
} | |
$network = $data['network']; | |
$identity = $data['identity']; | |
// Ищем пользователя по identity | |
$oSiteuser = Core_Entity::factory('siteuser'); | |
$oSiteuser->queryBuilder() | |
->join('property_value_strings', 'property_value_strings.entity_id', '=', 'siteusers.id', array( | |
array('AND' => array('property_value_strings.property_id', '=', IDENTITY_PROPERTY_ID)) | |
)) | |
->where('property_value_strings.value', '=', $identity) | |
->where('siteusers.deleted', '=', '0'); | |
$aoSiteusers = $oSiteuser->findAll(); | |
if (count($aoSiteusers) > 0) | |
{ | |
$oSiteuser = $aoSiteusers[0]; | |
} | |
else | |
{ | |
// Создаем нового пользователя | |
$oSiteuser = Core_Entity::factory('Siteuser'); | |
$oSiteuser->login = Core_Str::stripTags($data['email']); | |
$oSiteuser->password = Core_Hash::instance()->hash(Core_Password::get(12)); | |
$oSiteuser->email = $data['email']; | |
$oSiteuser->name = Core_Str::stripTags($data['first_name']); | |
$oSiteuser->surname = Core_Str::stripTags($data['last_name']); | |
$oSiteuser->active = true; | |
$oSiteuser->save(); | |
// Устанавливаем $network и $identity | |
Kad_Property::setValue($oSiteuser, NETWORK_PROPERTY_ID, $data['network']); | |
Kad_Property::setValue($oSiteuser, IDENTITY_PROPERTY_ID, $data['identity']); | |
} | |
// Логгиним | |
$oSiteuser->setCurrent(); | |
// Обновляем аватар | |
if (array_key_exists('photo_big', $data) && $data['photo_big'] != '') | |
{ | |
$oProperty = Core_Entity::factory('property', AVATAR_PROPERTY_ID); | |
$sDestinationFolder = $oSiteuser->getDirPath(); // Папка назначения | |
$oSiteuser->createDir(); // Создаем папку назначения | |
$sSourceFile = $data['photo_big']; // Файл-источник | |
// Создаем временный файл | |
$sTempFileName = tempnam(CMS_FOLDER . TMP_DIR, "CMS"); | |
// Копируем содержимое WEB-файла в локальный временный файл | |
file_put_contents($sTempFileName, file_get_contents($sSourceFile)); | |
// Файл-источник равен временному файлу | |
$sSourceFile = $sTempFileName; | |
switch(Core_Image::exifImagetype($sSourceFile)) | |
{ | |
case 1: | |
$sExt = 'gif'; | |
break; | |
case 2: | |
$sExt = 'jpeg'; | |
break; | |
case 3: | |
$sExt = 'png'; | |
break; | |
default: | |
$sExt = 'jpeg'; | |
break; | |
} | |
$sTargetFileName = "property_{$oProperty->id}.{$sExt}"; | |
// Создаем массив параметров для загрузки картинок элементу | |
$aPicturesParam = array(); | |
$aPicturesParam['large_image_isset'] = TRUE; | |
$aPicturesParam['large_image_source'] = $sSourceFile; | |
$aPicturesParam['large_image_name'] = "avatar.{$sExt}"; | |
$aPicturesParam['large_image_target'] = $sDestinationFolder . $sTargetFileName; | |
$aPicturesParam['large_image_preserve_aspect_ratio'] = TRUE; | |
$aPicturesParam['large_image_max_width'] = $oProperty->image_large_max_width; | |
$aPicturesParam['large_image_max_height'] = $oProperty->image_large_max_height; | |
$aPicturesParam['large_image_watermark'] = FALSE; | |
$aPicturesParam['create_small_image_from_large'] = FALSE; | |
$aPropertyValues = $oProperty->getValues($oSiteuser->id, FALSE); | |
$oProperty_Value = count($aPropertyValues) | |
? $aPropertyValues[0] | |
: $oProperty->createNewValue($oSiteuser->id); | |
// Удаляем старое большое изображение | |
if ($oProperty_Value->file != '') | |
{ | |
try | |
{ | |
Core_File::delete($sDestinationFolder . $oProperty_Value->file); | |
} catch (Exception $e) {} | |
} | |
try | |
{ | |
$aResult = Core_File::adminUpload($aPicturesParam); | |
} | |
catch (Exception $exc) | |
{ | |
Core_Message::show($exc->getMessage(), 'error'); | |
$aResult = array('large_image' => FALSE); | |
} | |
if ($aResult['large_image']) | |
{ | |
$oProperty_Value->file = $sTargetFileName; | |
$oProperty_Value->file_name = ''; | |
} | |
$oProperty_Value->save(); | |
// Файл временный, подлежит удалению | |
try | |
{ | |
Core_File::delete($sSourceFile); | |
} catch (Exception $e) {} | |
} | |
} | |
// Выход пользователя | |
if (Core_Array::getGet('action') == 'exit' && $oSiteuser) | |
{ | |
$oSiteuser->unsetCurrent(); | |
} | |
if (!Core_Array::getGet('ajax')) | |
{ | |
header('Location: ' . ( | |
$_SERVER['HTTP_REFERER'] | |
? $_SERVER['HTTP_REFERER'] | |
: '/' | |
) | |
); | |
} | |
exit(); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- Вывод имени пользователя и ссылки --> | |
<xsl:choose> | |
<xsl:when test="siteuser_id != 0 and siteuser/name != ''"> | |
<a href="{siteuser/property_value[tag_name = 'ulogin_identity']/value}"> | |
<xsl:value-of select="siteuser/name" /> | |
<xsl:text> </xsl:text> | |
<xsl:value-of select="siteuser/surname" /> | |
</a> | |
</xsl:when> | |
<xsl:otherwise> | |
<xsl:value-of select="author" /> | |
</xsl:otherwise> | |
</xsl:choose> | |
<!-- Вывод кнопки авторизации --> | |
<xsl:choose> | |
<xsl:when test="/informationsystem/siteuser_id = 0"> | |
<div class="social-auth"> | |
<div class="social-auth__info">Чтобы оставить комментарий необходимо авторизоваться с помощью одной из соц. сетей.</div> | |
<div class="social-auth__button"> | |
<a href="#" id="uLogin" data-ulogin="display=window;fields=first_name,last_name,email;optional=photo,photo_big;callback=uloginCallback"><img src="http://ulogin.ru/img/button.png" width="187" height="30" alt="МультиВход" /></a> | |
</div> | |
</div> | |
</xsl:when> | |
<xsl:otherwise> | |
<!-- | |
Вывод формы комментариев | |
--> | |
</xsl:otherwise> | |
</xsl:choose> | |
<!-- Вывод имени и ссылки выхода --> | |
<div class="col-md-2"> | |
<span class="auth-name"> | |
<a href="{/informationsystem/siteuser/property_value[tag_name = 'ulogin_identity']/value}"> | |
<xsl:value-of select="/informationsystem/siteuser/name" /> | |
<xsl:text> </xsl:text> | |
<xsl:value-of select="/informationsystem/siteuser/surname" /> | |
</a> | |
</span> | |
<span class="logout"> | |
(<a href="/ulogin/?action=exit">выйти</a>) | |
</span> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment