๐Ÿ“œ โฌ†๏ธ โฌ‡๏ธ

HybridAuth for UMI.CMS. Authorization through social networks on UMI.CMS

I wanted for a very long time for UMI to make a different script from loginza, since I think that it is not entirely successful, for several reasons (purely subjective). I can be mistaken if that is not throw stones.

It was inspired by one of the articles on habrahabr, unfortunately the article was deleted. In any case, I will leave the link to every fireman - this article . This article was told in general how to connect HybridAuth to your site, and matured to implement the method and write your article.

I will try to consider the implementation of authorization on UMI.CMS through the popular HybridAuth library.
')
Authorization for UMI.CMS will be implemented through providers:



The article on HybridAuth for UMI.CMS will be divided into several articles:
1) Customize user fields
2) Install and configure HybridAuth
3) Install HybridAuth + UMI.CMS script
4) Template correction

Custom user fields


First, go to the data templates (module) and find the โ€œUserโ€ data type there:
hybridauth-dlya-umi-cms-3

Add a new field "Avatar" and "User ID from social. networks ":

hybridauth-dlya-umi-cms-4

hybridauth-dlya-umi-cms-5

hybridauth-dlya-umi-cms-6

User setup is complete.

Install and configure HybridAuth


Download the library here (these are the original files), and here add. Provadiers. This link can be downloaded with vk.com already added (I suggest using this archive).

First you need to create applications for your site. I will not paint the process of creating an application, I am sure you will figure it out on your own. In order for you to quickly figure out the links to create an application:


Next, you need to configure HybridAuth. To do this, open _http: //your_site/hybridauth/install.php and select the desired services. I want to draw attention to the fact that the guys who developed the hybridauth immediately led links to creating applications.

After you have made all the settings and saved, you should see something like this:

hybridauth-dlya-umi-cms-2

Now you need to add a new provider.

If you used the original files from the repository, then you need to set the provider for vk.com (if you have taken the finished archive, then skip the vk.com setting). Follow this link and download the file. It must be placed in the \ hybridauth \ Hybrid \ Providers directory. In the file hybridauth \ config.php add a piece of code and enter the id and secret, which provide vkontakte.

"Vkontakte" => array ( "enabled" => true, "keys" => array ( "id" => "", "secret" => "" ) ), 


Installing the script HybridAuth + UMI.CMS


Next, you need to place in the root file auth_with_social.php with the following content:

 session_start(); require_once('standalone.php'); $config = CURRENT_WORKING_DIR . '/hybridauth/config.php'; require CURRENT_WORKING_DIR . '/hybridauth/Hybrid/Auth.php'; $domain = cmsController::getInstance()->getCurrentDomain()->getHost(); //   $users = cmsController::getInstance()->getModule("users"); // ""   users $data = cmsController::getInstance()->getModule("data"); // ""   data /* * /auth_with_social.php?login_with=_fb * /auth_with_social.php?login_with=_vk * /auth_with_social.php?login_with=_in * /auth_with_social.php?login_with=_gp * /auth_with_social.php?login_with=_tw * */ if( isset( $_GET["login_with"] ) ) { $login_with = $_GET["login_with"]; try { $hybridauth = new Hybrid_Auth($config); $adapter = false; switch ($login_with) { case "_fb": $adapter = $hybridauth->authenticate( "facebook" ); break; case "_vk": $adapter = $hybridauth->authenticate( "vkontakte" ); break; case "_in": $adapter = $hybridauth->authenticate( "linkedin" ); break; case "_gp": $adapter = $hybridauth->authenticate( "google" ); break; case "_tw": $adapter = $hybridauth->authenticate( "twitter" ); break; } //     ""   if(!$adapter) die( "<b>  !   .</b> "); $profile = $adapter->getUserProfile(); //     -  if( !isset( $profile ) ){ /*  $profile   auth_with_social.php   */ $domain = cmsController::getInstance()->getCurrentDomain()->getHost(); header ("Location: http://" . $domain); //   ,   :) } else { echo " "; echo "<pre>"; print_r($profile); echo "</pre>"; $objectTypes = umiObjectTypesCollection::getInstance(); $objectTypeId = $objectTypes->getBaseType("users", "user"); $objectType = $objectTypes->getType($objectTypeId); $provider = $login_with; //      $identifier = $profile->identifier; $login = $profile->email; $email = $profile->email; if(iconv_strlen($login) == 0) { $login = transliterate($profile->firstName); $login .= $profile->identifier; $email = $login . '@'. $domain; } $fname = $profile->firstName; $lname = $profile->lastName; $password = md5(generate_password(10)); $sel = new selector('objects'); $sel->types('object-type')->name('users', 'user'); $sel->where('e-mail')->equals($email); $user = $sel->first; if( $user instanceof iUmiObject ) { permissionsCollection::getInstance()->loginAsUser($user); session_commit(); header ("Location: http://" . $domain); } else { if(!preg_match("/.+@.+\..+/", $email)) { while(true) { $email = $login.rand(1,100)."@".getServer('HTTP_HOST'); if($this->checkIsUniqueEmail($email)) { break; } } } $object_id = umiObjectsCollection::getInstance()->addObject($login, $objectTypeId); $object = umiObjectsCollection::getInstance()->getObject($object_id); $object->setValue("login", $login); $object->setValue("password", md5($password)); $object->setValue("e-mail", $email); $object->setValue("fname", ($fname)); $object->setValue("lname", $lname); $object->setValue("loginza", $provider); $object->setValue("social_identifier", $identifier); $object->setValue("register_date", time()); $object->setValue("is_activated", '1'); $object->setValue("activate_code", md5(uniqid(rand(), true))); /*    */ $user_pic = "./files/users_upload/social_avatars/"; $user_pic .= $identifier . $provider; $png_pos = strpos($profile->photoURL , '.png'); $gif_pos = strpos($profile->photoURL , '.gif'); if ( $png_pos !== false ) $user_pic .= ".png"; elseif ( $gif_pos !== false ) $user_pic .= ".gif"; else $user_pic .= ".jpeg"; // How to save facebook profile picture using php graph Api // http://goo.gl/zgL3iR //     URL- // http://goo.gl/VHeXk1 $data = file_get_contents($profile->photoURL); $file = fopen($user_pic, 'wb'); fputs($file, $data); fclose($file); if( file_exists("$user_pic") ) { $oFile = new umiFile($user_pic); if (!$oFile->getIsBroken()) { $object->setValue("avatar", $oFile); } } /* //    */ $_SESSION['cms_login'] = $login; $_SESSION['cms_pass'] = md5($password); $_SESSION['user_id'] = $object_id; session_commit(); $group_id = regedit::getInstance()->getVal("//modules/users/def_group"); $object->setValue("groups", Array($group_id)); $data_module = cmsController::getInstance()->getModule('data'); $data_module->saveEditedObject($object_id, true); $object->commit(); header ("Location: http://" . $domain); } } } catch( Exception $e ) { /*$html = "  .    : "; $html .= "<b>" . $e->getMessage() . "</b>"; die( $html );*/ header ("Location: http://" . $domain . '/authorization/?error=' . $e->getMessage()); } } else { /*     auth_with_social.php   */ header ("Location: http://" . $domain); } /* *   * */ function generate_password($number) { $arr = array('a','b','c','d','e','f', 'g','h','i','j','k','l', 'm','n','o','p','r','s', 't','u','v','x','y','z', 'A','B','C','D','E','F', 'G','H','I','J','K','L', 'M','N','O','P','R','S', 'T','U','V','X','Y','Z', '1','2','3','4','5','6', '7','8','9','0','-'); //   $pass = ""; for($i = 0; $i < $number; $i++) { //     $index = rand(0, count($arr) - 1); $pass .= $arr[$index]; } return $pass; } /* *    * */ function transliterate($input) { $gost = array( "ะ„"=>"YE","ะ†"=>"I","ะƒ"=>"G","ั–"=>"i","โ„–"=>"-","ั”"=>"ye","ั“"=>"g", ""=>"A",""=>"B",""=>"V",""=>"G",""=>"D", ""=>"E",""=>"YO",""=>"ZH", ""=>"Z",""=>"I",""=>"J",""=>"K",""=>"L", ""=>"M",""=>"N",""=>"O",""=>"P",""=>"R", ""=>"S",""=>"T",""=>"U",""=>"F",""=>"Kh", ""=>"C",""=>"CH",""=>"SH",""=>"SHH",""=>"'", ""=>"Y",""=>"",""=>"E",""=>"YU",""=>"YA", ""=>"a",""=>"b",""=>"v",""=>"g",""=>"d", ""=>"e",""=>"yo",""=>"zh", ""=>"z",""=>"i",""=>"j",""=>"k",""=>"l", ""=>"m",""=>"n",""=>"o",""=>"p",""=>"r", ""=>"s",""=>"t",""=>"u",""=>"f",""=>"kh", ""=>"c",""=>"ch",""=>"sh",""=>"shh",""=>"", ""=>"y",""=>"",""=>"e",""=>"yu",""=>"ya", " "=>"-","โ€”"=>"-",","=>"-","!"=>"-","@"=>"-", "#"=>"-","$"=>"","%"=>"","^"=>"","&"=>"","*"=>"", "("=>"",")"=>"","+"=>"","="=>"",";"=>"",":"=>"", "'"=>"","\""=>"","~"=>"","`"=>"","?"=>"","/"=>"", "\\"=>"","["=>"","]"=>"","{"=>"","}"=>"","|"=>"", "."=>"-" ); $string = strtr($input, $gost); return strtolower($string); } 


Next, add the line to the .htaccess file:
 ###### SOCIAL ##### RewriteRule ^hybridauth/(.*)*$ hybridauth/index.php?%{QUERY_STRING} [L] ###### // SOCIAL ##### 


Template Adjustment


Next you need to add links to the template for authorization through social. network. You need to take this code:

 <ul class="b-social-links"> <li class="b-social-links__item"> <a href="/auth_with_social.php?login_with=_fb" class="fb" title="  Facebook">  Facebook</a> </li> <li class="b-social-links__item"> <a href="/auth_with_social.php?login_with=_vk" class="vk" title="  ">  </a> </li> <li class="b-social-links__item"> <a href="/auth_with_social.php?login_with=_in" class="in" title="  LinkedIn">  LinkedIn</a> </li> <li class="b-social-links__item"> <a href="/auth_with_social.php?login_with=_gp" class="gp" title="  Google+">  Google+</a> </li> <li class="b-social-links__item"> <a href="/auth_with_social.php?login_with=_tw" class="tw" title="  Twitter">  Twitter</a> </li> </ul> 


This is all set up.

Now I want to pay attention to if an error occurred during authorization. If this happens, the script redirects to the / authorization / page with the get parameter error.

In order to show the user an error, you need to create a page in the structure of the site and make it URL / authorization /. Next, use xslt to work out the error parameter. TPL-template engine is not considered.

References:


PS If you have questions and incomprehensible moments, write comments.
PSS If you find an error, let me know, I will correct it immediately. I tried to quickly share the experience, maybe someone will come in handy.

Thanks for attention.

Source: https://habr.com/ru/post/230365/


All Articles