Overview

Packages

  • CONTENIDO
  • Core
    • Authentication
    • Backend
    • Cache
    • CEC
    • Chain
    • ContentType
    • Database
    • Debug
    • Exception
    • Frontend
      • Search
      • URI
      • Util
    • GenericDB
      • Model
    • GUI
      • HTML
    • I18N
    • LayoutHandler
    • Log
    • Security
    • Session
    • Util
    • Validation
    • Versioning
    • XML
  • Module
    • ContentSitemapHtml
    • ContentSitemapXml
    • ContentUserForum
    • NavigationTop
    • ScriptCookieDirective
  • mpAutoloaderClassMap
  • None
  • PHP
  • Plugin
    • ContentAllocation
    • CronjobOverview
    • FormAssistant
    • FrontendLogic
    • FrontendUsers
    • Linkchecker
    • ModRewrite
    • Newsletter
    • Repository
      • FrontendNavigation
      • KeywordDensity
    • SmartyWrapper
    • UrlShortener
    • UserForum
    • Workflow
  • PluginManager
  • Setup
    • Form
    • GUI
    • Helper
      • Environment
      • Filesystem
      • MySQL
      • PHP
    • UpgradeJob

Classes

  • cAuth
  • cAuthHandlerAbstract
  • cAuthHandlerBackend
  • cAuthHandlerFrontend
  • Overview
  • Package
  • Class
  • Tree
  • Deprecated
  • Todo
  1: <?php
  2: 
  3: /**
  4:  * This file contains the backend authentication handler class.
  5:  *
  6:  * @package    Core
  7:  * @subpackage Authentication
  8:  * @author     Dominik Ziegler
  9:  * @copyright  four for business AG <www.4fb.de>
 10:  * @license    http://www.contenido.org/license/LIZENZ.txt
 11:  * @link       http://www.4fb.de
 12:  * @link       http://www.contenido.org
 13:  */
 14: 
 15: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
 16: 
 17: /**
 18:  * This class is the backend authentication handler for CONTENIDO.
 19:  *
 20:  * @package    Core
 21:  * @subpackage Authentication
 22:  */
 23: class cAuthHandlerBackend extends cAuthHandlerAbstract {
 24: 
 25:     /**
 26:      * Constructor to create an instance of this class.
 27:      *
 28:      * Automatically sets the lifetime of the authentication to the
 29:      * configured value.
 30:      */
 31:     public function __construct() {
 32:         $cfg = cRegistry::getConfig();
 33:         $this->_lifetime = (int) $cfg['backend']['timeout'];
 34:         if ($this->_lifetime == 0) {
 35:             $this->_lifetime = 15;
 36:         }
 37:     }
 38: 
 39:     /**
 40:      * Handle the pre authentication.
 41:      *
 42:      * There is no pre authentication in backend so false is returned.
 43:      *
 44:      * @see cAuthHandlerAbstract::preAuthorize()
 45:      * @return false
 46:      */
 47:     public function preAuthorize() {
 48:         return false;
 49:     }
 50: 
 51:     /**
 52:      * Display the login form.
 53:      * Includes a file which displays the login form.
 54:      *
 55:      * @see cAuthHandlerAbstract::displayLoginForm()
 56:      * 
 57:      * @throws cDbException
 58:      * @throws cException
 59:      * @throws cInvalidArgumentException
 60:      */
 61:     public function displayLoginForm() {
 62:         // @TODO  We need a better solution for this.
 63:         //        One idea could be to set the request/response type in
 64:         //        global $cfg array instead of checking $_REQUEST['ajax']
 65:         //        everywhere...
 66:         if (isset($_REQUEST['ajax']) && $_REQUEST['ajax'] != '') {
 67:             $oAjax = new cAjaxRequest();
 68:             $sReturn = $oAjax->handle('authentication_fail');
 69:             echo $sReturn;
 70:         } else {
 71:             include(cRegistry::getBackendPath() . 'main.loginform.php');
 72:         }
 73:     }
 74: 
 75:     /**
 76:      * Validate the credentials.
 77:      *
 78:      * Validate the users input against source and return a valid user
 79:      * ID or false.
 80:      *
 81:      * @see cAuthHandlerAbstract::validateCredentials()
 82:      *
 83:      * @return string|false
 84:      *
 85:      * @throws cDbException
 86:      * @throws cException
 87:      */
 88:     public function validateCredentials() {
 89:         $username = !empty($_POST['username']) ? $_POST['username'] : '';
 90:         $password = !empty($_POST['password']) ? $_POST['password'] : '';
 91:         $formtimestamp = !empty($_POST['formtimestamp']) ? $_POST['formtimestamp'] : '';
 92: 
 93:         // add slashes if they are not automatically added
 94:         if (cRegistry::getConfigValue('simulate_magic_quotes') !== true) {
 95:             // backward compatiblity of passwords
 96:             $password = addslashes($password);
 97:             // avoid sql injection in query by username on cApiUserCollection select string
 98:             $username = addslashes($username);
 99:         }
100: 
101:         $groupPerm = array();
102: 
103:         if ($password == '') {
104:             return false;
105:         }
106: 
107:         if (($formtimestamp + (60 * 15)) < time()) {
108:             return false;
109:         }
110: 
111:         if (isset($username)) {
112:             $this->auth['uname'] = $username;
113:         } else if ($this->_defaultNobody == true) {
114:             $uid = $this->auth['uname'] = $this->auth['uid'] = self::AUTH_UID_NOBODY;
115: 
116:             return $uid;
117:         }
118: 
119:         $uid = false;
120:         $perm = false;
121:         $pass = false;
122:         $salt = false;
123: 
124:         $userColl = new cApiUserCollection();
125:         $where = "username = '" . $username . "'";
126:         $where .= " AND (valid_from <= NOW() OR valid_from = '0000-00-00 00:00:00' OR valid_from is NULL)";
127:         $where .= " AND (valid_to >= NOW() OR valid_to = '0000-00-00 00:00:00' OR valid_to is NULL)";
128: 
129:         $maintenanceMode = getSystemProperty('maintenance', 'mode');
130:         if ($maintenanceMode == 'enabled') {
131:             $where .= " AND perms = 'sysadmin'";
132:         }
133: 
134:         $userColl->select($where);
135: 
136:         while (($item = $userColl->next()) !== false) {
137:             $uid = $item->get('user_id');
138:             $perm = $item->get('perms');
139:             // password is stored as a sha256 hash
140:             $pass = $item->get('password');
141:             $salt = $item->get("salt");
142:         }
143: 
144:         if ($uid == false || hash("sha256", md5($password) . $salt) != $pass) {
145:             // No user found, sleep and exit
146:             sleep(5);
147: 
148:             return false;
149:         }
150: 
151:         if ($perm != '') {
152:             $groupPerm[] = $perm;
153:         }
154: 
155:         $groupColl = new cApiGroupCollection();
156:         $groups = $groupColl->fetchByUserID($uid);
157:         foreach ($groups as $group) {
158:             $groupPerm[] = $group->get('perms');
159:         }
160: 
161:         $perm = implode(',', $groupPerm);
162: 
163:         $this->auth['perm'] = $perm;
164: 
165:         return $uid;
166:     }
167: 
168:     /**
169:      * Log the successful authentication.
170:      *
171:      * @see cAuthHandlerAbstract::logSuccessfulAuth()
172:      * 
173:      * @throws cDbException
174:      * @throws cException
175:      */
176:     public function logSuccessfulAuth() {
177:         global $client, $lang, $saveLoginTime;
178: 
179:         $perm = new cPermission();
180: 
181:         // Find the first accessible client and language for the user
182:         $clientLangColl = new cApiClientLanguageCollection();
183:         $clientLangColl->select();
184: 
185:         $bFound = false;
186:         while ($bFound == false) {
187:             if (($item = $clientLangColl->next()) === false) {
188:                 break;
189:             }
190: 
191:             $iTmpClient = $item->get('idclient');
192:             $iTmpLang = $item->get('idlang');
193: 
194:             if ($perm->have_perm_client_lang($iTmpClient, $iTmpLang)) {
195:                 $client = $iTmpClient;
196:                 $lang = $iTmpLang;
197:                 $bFound = true;
198:             }
199:         }
200: 
201:         if (!is_numeric($client) || !is_numeric($lang)) {
202:             return;
203:         }
204: 
205:         $idaction = $perm->getIDForAction('login');
206: 
207:         $authInfo = $this->getAuthInfo();
208:         $uid = $authInfo['uid'];
209: 
210:         // create a actionlog entry
211:         $actionLogCol = new cApiActionlogCollection();
212:         $actionLogCol->create($uid, $client, $lang, $idaction, 0);
213: 
214:         $sess = cRegistry::getSession();
215:         $sess->register('saveLoginTime');
216:         $saveLoginTime = true;
217:     }
218: 
219:     /**
220:      * Returns true if a user is logged in.
221:      *
222:      * @see cAuthHandlerAbstract::isLoggedIn()
223:      * @return bool
224:      */
225:     public function isLoggedIn() {
226:         $authInfo = $this->getAuthInfo();
227: 
228:         if(isset($authInfo['uid'])) {
229:             $user = new cApiUser($authInfo['uid']);
230: 
231:             return $user->get('user_id') != '';
232:         } else {
233:             return false;
234:         }
235:     }
236: 
237: }
238: 
CMS CONTENIDO 4.10.0 API documentation generated by ApiGen 2.8.0