richdocuments/lib/session.php

157 lines
3.8 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
2013-09-07 19:49:48 +03:00
class Session extends Db{
const DB_TABLE = '`*PREFIX*documents_session`';
2013-08-07 18:02:20 +03:00
2013-08-17 19:42:50 +03:00
public static function add($genesis, $hash, $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,
2013-08-09 17:29:18 +03:00
'owner' => \OCP\User::getUser(),
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-09-07 22:13:50 +03:00
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();
}
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 .'
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
)
);
$info = $result->fetchRow();
if (!is_array($info)){
$info = array();
}
return $info;
}
public static function getSessionByFileId($fileId){
$sessions = self::getSessionsByFileIds(array($fileId));
2013-09-07 21:37:36 +03:00
if (count($sessions) > 1) {
2013-09-07 22:13:50 +03:00
Helper::errorLog('documents','more than one session found for file id ' . $fileId);
}
if (count($sessions)) {
return $sessions[0];
}
2013-09-07 22:13:50 +03:00
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();
}
$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 .')
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);
}
2013-08-08 18:31:47 +03:00
} while (self::getSession($id));
return $id;
2013-08-07 02:22:21 +03:00
}
2013-07-19 18:52:52 +03:00
}