2010-12-07 20:19:27 +01:00
|
|
|
<?php
|
|
|
|
define('ALIBERR_NOTGR', 20);
|
|
|
|
define('ALIBERR_BADSMEMB', 21);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Subj class
|
|
|
|
*
|
|
|
|
* users + groups
|
|
|
|
* with "linearized recursive membership" ;)
|
|
|
|
* (allow adding users to groups or groups to groups)
|
|
|
|
*
|
2011-01-05 18:19:58 +01:00
|
|
|
* @package Airtime
|
2010-12-07 20:19:27 +01:00
|
|
|
* @subpackage Alib
|
|
|
|
* @copyright 2010 Sourcefabric O.P.S.
|
|
|
|
* @license http://www.gnu.org/licenses/gpl.txt
|
|
|
|
*/
|
2011-09-13 20:16:16 +02:00
|
|
|
class Application_Model_Subjects {
|
2010-12-07 20:19:27 +01:00
|
|
|
|
|
|
|
/* ======================================================= public methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check login and password
|
|
|
|
*
|
|
|
|
* @param string $login
|
|
|
|
* @param string $pass
|
|
|
|
* optional
|
|
|
|
* @return boolean|int|PEAR_Error
|
|
|
|
*/
|
|
|
|
public static function Authenticate($login, $pass='')
|
|
|
|
{
|
2012-04-01 21:51:03 +02:00
|
|
|
global $CC_CONFIG;
|
|
|
|
$con = Propel::getConnection();
|
2010-12-07 20:19:27 +01:00
|
|
|
$cpass = md5($pass);
|
|
|
|
$sql = "SELECT id FROM ".$CC_CONFIG['subjTable']
|
2012-04-01 21:51:03 +02:00
|
|
|
." WHERE login='$login' AND pass='$cpass' AND type='U'"
|
|
|
|
." LIMIT 1";
|
|
|
|
$query = $con->query($sql)->fetchColumn(0);
|
|
|
|
return $query;
|
|
|
|
}
|
2010-12-07 20:19:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Change user password
|
|
|
|
*
|
|
|
|
* @param string $login
|
|
|
|
* @param string $oldpass
|
|
|
|
* old password (optional for 'superuser mode')
|
|
|
|
* @param string $pass
|
|
|
|
* optional
|
|
|
|
* @param boolean $passenc
|
|
|
|
* optional, password already encrypted if true
|
|
|
|
* @return boolean|PEAR_Error
|
|
|
|
*/
|
|
|
|
public static function Passwd($login, $oldpass=null, $pass='', $passenc=FALSE)
|
|
|
|
{
|
2012-04-01 21:51:03 +02:00
|
|
|
global $CC_CONFIG;
|
|
|
|
$con = Propel::getConnection();
|
2010-12-07 20:19:27 +01:00
|
|
|
if (!$passenc) {
|
|
|
|
$cpass = md5($pass);
|
|
|
|
} else {
|
|
|
|
$cpass = $pass;
|
|
|
|
}
|
|
|
|
if (!is_null($oldpass)) {
|
|
|
|
$oldcpass = md5($oldpass);
|
|
|
|
$oldpCond = "AND pass='$oldcpass'";
|
|
|
|
} else {
|
|
|
|
$oldpCond = '';
|
|
|
|
}
|
|
|
|
$sql = "UPDATE ".$CC_CONFIG['subjTable']." SET pass='$cpass'"
|
|
|
|
." WHERE login='$login' $oldpCond AND type='U'";
|
2012-04-01 21:51:03 +02:00
|
|
|
$con->exec($sql);
|
2010-12-07 20:19:27 +01:00
|
|
|
return TRUE;
|
2012-04-01 21:51:03 +02:00
|
|
|
}
|
2010-12-07 20:19:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------- groups */
|
|
|
|
|
|
|
|
/* --------------------------------------------------------- info methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get subject id from login
|
|
|
|
*
|
|
|
|
* @param string $login
|
2012-04-01 21:51:03 +02:00
|
|
|
* @return int|false
|
2010-12-07 20:19:27 +01:00
|
|
|
*/
|
|
|
|
public static function GetSubjId($login)
|
|
|
|
{
|
|
|
|
global $CC_CONFIG;
|
2012-04-01 21:51:03 +02:00
|
|
|
$con = Propel::getConnection();
|
2010-12-07 20:19:27 +01:00
|
|
|
$sql = "SELECT id FROM ".$CC_CONFIG['subjTable']
|
|
|
|
." WHERE login='$login'";
|
2012-04-01 21:51:03 +02:00
|
|
|
$query = $con->query($sql)->fetchColumn(0);
|
2012-04-19 22:54:38 +02:00
|
|
|
return ($query !== false) ? $query : NULL;
|
2012-04-01 21:51:03 +02:00
|
|
|
}
|
2010-12-07 20:19:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2012-04-01 21:51:03 +02:00
|
|
|
* Return true if uid is direct member of gid
|
2010-12-07 20:19:27 +01:00
|
|
|
*
|
|
|
|
* @param int $uid
|
|
|
|
* local user id
|
|
|
|
* @param int $gid
|
|
|
|
* local group id
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public static function IsMemberOf($uid, $gid)
|
|
|
|
{
|
2012-04-01 21:51:03 +02:00
|
|
|
global $CC_CONFIG;
|
|
|
|
$con = Propel::getConnection();
|
|
|
|
$sql = "SELECT count(*) as cnt"
|
2010-12-07 20:19:27 +01:00
|
|
|
." FROM ".$CC_CONFIG['smembTable']
|
|
|
|
." WHERE uid='$uid' AND gid='$gid'";
|
2012-04-01 21:51:03 +02:00
|
|
|
$res = $con->query($sql)->fetchColumn(0);
|
2010-12-07 20:19:27 +01:00
|
|
|
return (intval($res) > 0);
|
2012-04-01 21:51:03 +02:00
|
|
|
}
|
2010-12-07 20:19:27 +01:00
|
|
|
|
2012-04-01 21:51:03 +02:00
|
|
|
public static function increaseLoginAttempts($login)
|
|
|
|
{
|
|
|
|
global $CC_CONFIG;
|
|
|
|
$con = Propel::getConnection();
|
2011-09-13 20:16:16 +02:00
|
|
|
$sql = "UPDATE ".$CC_CONFIG['subjTable']." SET login_attempts = login_attempts+1"
|
|
|
|
." WHERE login='$login'";
|
2012-04-01 21:51:03 +02:00
|
|
|
$res = $con->exec($sql);
|
2011-09-13 20:16:16 +02:00
|
|
|
return (intval($res) > 0);
|
|
|
|
}
|
2012-04-01 21:51:03 +02:00
|
|
|
|
|
|
|
public static function resetLoginAttempts($login)
|
|
|
|
{
|
|
|
|
global $CC_CONFIG;
|
|
|
|
$con = Propel::getConnection();
|
2011-09-13 20:16:16 +02:00
|
|
|
$sql = "UPDATE ".$CC_CONFIG['subjTable']." SET login_attempts = '0'"
|
|
|
|
." WHERE login='$login'";
|
2012-04-01 21:51:03 +02:00
|
|
|
$res = $con->exec($sql);
|
|
|
|
return TRUE;
|
2011-09-13 20:16:16 +02:00
|
|
|
}
|
2012-04-01 21:51:03 +02:00
|
|
|
|
|
|
|
public static function getLoginAttempts($login)
|
|
|
|
{
|
|
|
|
global $CC_CONFIG;
|
|
|
|
$con = Propel::getConnection();
|
2011-09-13 20:16:16 +02:00
|
|
|
$sql = "SELECT login_attempts FROM ".$CC_CONFIG['subjTable']." WHERE login='$login'";
|
2012-04-01 21:51:03 +02:00
|
|
|
$res = $con->query($sql)->fetchColumn(0);
|
2012-04-19 22:54:38 +02:00
|
|
|
return ($res !== false) ? $res : 0;
|
2011-09-13 20:16:16 +02:00
|
|
|
}
|
2010-12-07 20:19:27 +01:00
|
|
|
|
|
|
|
} // class Subjects
|
2011-02-22 18:22:31 +01:00
|
|
|
|