richdocuments/lib/session.php

184 lines
4.5 KiB
PHP
Raw Normal View History

2013-07-19 18:52:52 +03:00
<?php
2013-08-08 00:22:21 +03:00
/**
2013-08-28 12:02:27 +02:00
* ownCloud - Documents App
2013-08-08 00:22:21 +03:00
*
* @author Victor Dubiniuk
* @copyright 2013 Victor Dubiniuk victor.dubiniuk@gmail.com
*
* This file is licensed under the Affero General Public License version 3 or
* later.
*/
2013-07-19 18:52:52 +03:00
2013-08-28 12:02:27 +02:00
namespace OCA\Documents;
2013-07-19 18:52:52 +03:00
class Session extends Db {
const DB_TABLE = '`*PREFIX*documents_session`';
public static function start($uid, File $file){
list($ownerView, $path) = $file->getOwnerViewAndPath();
$session = Session::getSessionByFileId( $file->getFileId() );
//If there is no existing session we need to start a new one
if (!$session || empty($session)){
$genesisPath = $ownerView->storeDocument($ownerView, $path);
if (!$genesisPath){
throw new \Exception('Unable to copy document. Check permissions and make sure you have enought free space.');
}
$hash = $ownerView->getHashByGenesis($genesisPath);
$session = Session::add(
$genesisPath, $hash, $file->getOwner(), $file->getFileId()
);
}
$session['permissions'] = $ownerView->getFilePermissions($path);
$session['member_id'] = (string) Member::add($session['es_id'], $uid, Helper::getRandomColor());
return $session;
}
public static function add($genesis, $hash, $owner, $fileId){
2013-08-28 15:24:41 +02:00
$query = \OCP\DB::prepare('
INSERT INTO ' . self::DB_TABLE . ' (`es_id`, `genesis_url`, `genesis_hash`, `owner`, `file_id`)
2013-08-28 15:24:41 +02:00
VALUES (?, ?, ?, ?, ?)
');
2013-08-07 02:22:21 +03:00
$data = array(
2013-08-08 18:31:47 +03:00
'es_id' => self::getUniqueSessionId(),
2013-08-08 14:44:13 +03:00
'genesis_url' => $genesis,
'genesis_hash' => $hash,
'owner' => $owner,
2013-08-17 19:42:50 +03:00
'file_id' => $fileId
2013-08-07 02:22:21 +03:00
);
2013-08-08 14:44:13 +03:00
$result = $query->execute(array_values($data));
2013-08-07 02:22:21 +03:00
if ($result){
return $data;
}
2013-08-07 02:22:21 +03:00
return false;
}
2013-08-09 18:29:36 +03:00
public static function getAll(){
$query = \OCP\DB::prepare('SELECT * FROM ' . self::DB_TABLE);
2013-08-09 18:29:36 +03:00
$result = $query->execute();
return $result->fetchAll();
}
2013-08-09 18:29:36 +03:00
public static function getSession($id){
$query = \OCP\DB::prepare('SELECT * FROM ' . self::DB_TABLE . ' WHERE `es_id`= ?');
2013-08-09 18:29:36 +03:00
$result = $query->execute(array($id));
return $result->fetchRow();
}
2013-08-18 17:11:22 +03:00
public static function getInfo($esId){
2013-08-28 15:24:41 +02:00
$query = \OCP\DB::prepare('
SELECT `s`.*, COUNT(`m`.`member_id`) AS `users`
FROM ' . self::DB_TABLE . ' AS `s`
2013-08-28 15:24:41 +02:00
LEFT JOIN `*PREFIX*documents_member` AS `m` ON `s`.`es_id`=`m`.`es_id`
AND `m`.`status`=' . Member::MEMBER_STATUS_ACTIVE . '
2013-08-28 15:24:41 +02:00
AND `m`.`uid` != ?
WHERE `s`.`es_id` = ?
GROUP BY `m`.`es_id`
');
2013-08-18 17:11:22 +03:00
$result = $query->execute(
array(
\OCP\User::getUser(),
$esId
2013-08-18 17:11:22 +03:00
)
);
2013-08-18 17:11:22 +03:00
$info = $result->fetchRow();
if (!is_array($info)){
$info = array();
}
return $info;
}
public static function getSessionByFileId($fileId){
$sessions = self::getSessionsByFileIds(array($fileId));
if (count($sessions) > 1){
Helper::errorLog('documents', 'more than one session found for file id ' . $fileId);
2013-09-07 22:13:50 +03:00
}
if (count($sessions)){
return $sessions[0];
}
return null;
}
public static function getSessionsByFileIds($fileIds){
if (!is_array($fileIds)){
$fileIds = array($fileIds);
}
2013-09-07 19:49:48 +03:00
$stmt = self::buildPlaceholders($fileIds);
$query = \OCP\DB::prepare('SELECT * FROM ' . self::DB_TABLE . ' WHERE `file_id` IN (' . $stmt . ')');
$result = $query->execute($fileIds);
$sessions = $result->fetchAll();
if (!is_array($sessions)){
$sessions = array();
}
return $sessions;
2013-08-09 18:29:36 +03:00
}
2013-08-18 17:11:22 +03:00
public static function getInfoByFileid($fileIds){
2013-09-07 19:49:48 +03:00
if (!is_array($fileIds)){
return array();
}
2013-09-07 19:49:48 +03:00
$stmt = self::buildPlaceholders($fileIds);
if (!$stmt){
2013-08-18 17:11:22 +03:00
return array();
}
2013-08-28 15:24:41 +02:00
$query = \OCP\DB::prepare('
SELECT `s`.*, COUNT(`m`.`member_id`) AS `users`
FROM ' . self::DB_TABLE . ' AS `s`
2013-08-28 15:24:41 +02:00
LEFT JOIN `*PREFIX*documents_member` AS `m` ON `s`.`es_id`=`m`.`es_id`
AND `m`.`status`=' . Member::MEMBER_STATUS_ACTIVE . '
WHERE `s`.`file_id` IN (' . $stmt . ')
2013-08-28 15:24:41 +02:00
GROUP BY `m`.`es_id`
');
2013-09-07 19:49:48 +03:00
$result = $query->execute($fileIds);
2013-08-18 17:11:22 +03:00
$info = $result->fetchAll();
if (!is_array($info)){
$info = array();
}
return $info;
}
2013-09-11 16:58:01 +03:00
public static function cleanUp($esId){
self::delete($esId);
Member::deleteBySessionId($esId);
Op::deleteBySessionId($esId);
}
public static function delete($esId){
$query = \OCP\DB::prepare('DELETE FROM ' . self::DB_TABLE . ' WHERE `es_id` = ?');
$query->execute(array($esId));
}
2013-08-18 17:11:22 +03:00
2013-08-08 18:31:47 +03:00
protected static function getUniqueSessionId(){
do{
// this prevents branching for stable5 for now:
// OC_Util::generate_random_bytes was camelCased
if (method_exists('\OC_Util', 'generate_random_bytes')){
$id = \OC_Util::generate_random_bytes(30);
} else {
$id = \OC_Util::generateRandomBytes(30);
}
}while (self::getSession($id));
2013-08-08 18:31:47 +03:00
return $id;
2013-08-07 02:22:21 +03:00
}
2013-07-19 18:52:52 +03:00
}