1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/movim_ynh.git synced 2024-09-03 19:46:19 +02:00
movim_ynh/sources/system/User.php

213 lines
4.7 KiB
PHP
Raw Normal View History

<?php
/**
* @file User.php
* This file is part of Movim.
*
* @brief Handles the user's login and user.
*
* @author Jaussoin Timothée
*
* @date 2014
*
* Copyright (C)2014 Movim
*
* See COPYING for licensing information.
*/
class User {
public $username = '';
private $password = '';
private $config = array();
public $caps;
2015-08-19 11:28:28 +02:00
public $userdir;
public $useruri;
2015-08-19 11:28:28 +02:00
public $sizelimit;
/**
* Class constructor. Reloads the user's session or attempts to authenticate
* the user.
*/
2015-12-15 11:54:49 +01:00
function __construct($username = false)
{
2015-12-15 11:54:49 +01:00
if($username) {
$this->username = $username;
}
$session = \Sessionx::start();
2015-12-15 11:54:49 +01:00
if($session->active && $this->username == null) {
$this->username = $session->user.'@'.$session->host;
2015-12-15 11:54:49 +01:00
}
2015-12-15 11:54:49 +01:00
if($this->username != null) {
$this->userdir = DOCUMENT_ROOT.'/users/'.$this->username.'/';
$this->useruri = BASE_URI.'users/'.$this->username.'/';
}
}
/**
* @brief Reload the user configuration
*/
function reload()
{
$session = \Sessionx::start();
if($session->config) {
$this->config = $session->config;
$lang = $this->getConfig('language');
if(isset($lang)) {
2015-09-10 15:41:45 +02:00
$l = Movim\i18n\Locale::start();
$l->load($lang);
}
$cd = new modl\CapsDAO;
$caps = $cd->get($session->host);
$this->caps = unserialize($caps->features);
}
}
2015-08-19 11:28:28 +02:00
/**
* Get the current size in bytes of the user directory
*/
function dirSize()
{
$sum = 0;
foreach($this->getDir() as $s)
$sum = $sum + filesize($this->userdir.$s);
2015-08-19 11:28:28 +02:00
return $sum;
}
2015-08-19 11:28:28 +02:00
/**
* Get a list of the files in the user dir with uri, dir and thumbs
*/
function getDir()
{
$dir = array();
if(is_dir($this->userdir))
foreach(scandir($this->userdir) as $s) {
if(
2015-08-19 11:28:28 +02:00
$s != '.' &&
$s != '..' &&
$s != 'index.html') {
array_push($dir, $s);
}
}
2015-08-19 11:28:28 +02:00
return $dir;
}
/**
* Checks if the user has an open session.
*/
function isLogged()
{
// User is not logged in if both the session vars and the members are unset.
$session = \Sessionx::start();
if($session->active)
return $session->active;
else
return false;
}
function desauth()
{
$pd = new modl\PresenceDAO();
$pd->clearPresence($this->username);
$s = \Sessionx::start();
$s->destroy();
2015-08-19 11:28:28 +02:00
$sess = Session::start();
Session::dispose();
}
function createDir()
{
if(!is_dir($this->userdir)
&& $this->userdir != '') {
mkdir($this->userdir);
touch($this->userdir.'index.html');
}
}
function getLogin()
{
return $this->username;
}
2015-08-19 11:28:28 +02:00
function getServer()
{
$exp = explodeJid($this->username);
return $exp['server'];
}
function getUser()
{
$exp = explodeJid($this->username);
return $exp['username'];
}
function getPass()
{
return $this->password;
}
function setConfig(array $config)
{
$session = \Sessionx::start();
$session->config = $config;
2015-12-15 11:54:49 +01:00
file_put_contents($this->userdir.'config.dump', serialize($config));
$this->reload();
}
function getConfig($key = false)
{
if($key == false)
return $this->config;
if(isset($this->config[$key]))
return $this->config[$key];
}
2015-12-15 11:54:49 +01:00
function getDumpedConfig($key = false)
{
$config = unserialize(file_get_contents($this->userdir.'config.dump'));
if($key == false)
return $config;
if(isset($config[$key]))
return $config[$key];
}
function isSupported($key)
{
$this->reload();
if($this->caps != null) {
switch($key) {
case 'pubsub':
return in_array('http://jabber.org/protocol/pubsub#publish', $this->caps);
break;
2015-08-19 11:28:28 +02:00
case 'upload':
$id = new \Modl\ItemDAO;
return ($id->getUpload($this->getServer()) != null);
break;
default:
return false;
break;
}
} elseif($key == 'anonymous') {
$session = \Sessionx::start();
return ($session->mechanism == 'ANONYMOUS');
} else {
return false;
}
}
}