2015-08-22 23:47:31 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
require_once 'model.php';
|
|
|
|
require_once 'user.php';
|
|
|
|
|
|
|
|
class Poll extends model {
|
|
|
|
const ENCRYPTED_PROPERTIES = [
|
|
|
|
'anonymousUser',
|
|
|
|
'answerType',
|
|
|
|
'creationDate',
|
|
|
|
'description',
|
|
|
|
'expirationDate',
|
|
|
|
'forceAnswer',
|
|
|
|
'options',
|
|
|
|
'pollType',
|
|
|
|
'timezone',
|
|
|
|
'title'
|
|
|
|
];
|
|
|
|
|
|
|
|
const ID_LENGTH = 10;
|
|
|
|
const ID_CHARACTERS = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
|
|
|
|
|
|
const PLAIN_PROPERTIES = [
|
|
|
|
'version'
|
|
|
|
];
|
|
|
|
|
|
|
|
const SERVER_PROPERTIES = [
|
|
|
|
'serverExpirationDate'
|
|
|
|
];
|
|
|
|
|
2015-08-23 06:18:05 +02:00
|
|
|
private function delete() {
|
|
|
|
$this->deleteDirRecursively(
|
|
|
|
$this->getDir()
|
|
|
|
);
|
|
|
|
}
|
2016-05-18 22:18:36 +02:00
|
|
|
|
2015-08-23 06:18:05 +02:00
|
|
|
private function deleteDirRecursively($dir) {
|
|
|
|
if (substr($dir, -1) !== '/') {
|
|
|
|
throw new Exception('dir has to end on /');
|
|
|
|
}
|
2016-05-18 22:18:36 +02:00
|
|
|
|
2015-08-23 06:18:05 +02:00
|
|
|
$dirHandle = opendir($dir);
|
|
|
|
while (false !== ($filename = readdir($dirHandle))) {
|
|
|
|
if ($filename === '.' || $filename === '..') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_dir($dir . $filename)) {
|
|
|
|
$this->deleteDirRecursively($dir . $filename . '/');
|
|
|
|
}
|
|
|
|
elseif (is_file($dir . $filename)) {
|
|
|
|
unlink($dir . $filename);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw new Exception($filename . " in " . $dir . " is not a dir neither a file - what is it?");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir($dirHandle);
|
2016-05-18 22:18:36 +02:00
|
|
|
|
2015-08-23 06:18:05 +02:00
|
|
|
rmdir($dir);
|
|
|
|
}
|
|
|
|
|
2015-08-22 23:47:31 +02:00
|
|
|
protected function generateNewId() {
|
|
|
|
$characters = self::ID_CHARACTERS;
|
|
|
|
$randomString = '';
|
|
|
|
for ($i = 0; $i < self::ID_LENGTH; $i++) {
|
|
|
|
$randomString .= $characters[rand(0, strlen($characters) - 1)];
|
|
|
|
}
|
|
|
|
return $randomString;
|
|
|
|
}
|
2016-05-18 22:18:36 +02:00
|
|
|
|
2015-08-22 23:47:31 +02:00
|
|
|
protected function getDir() {
|
2015-08-23 18:56:41 +02:00
|
|
|
if (($this->get('id') === null)) {
|
|
|
|
throw new Exception('id must be set before calling getDir');
|
|
|
|
}
|
2015-08-22 23:47:31 +02:00
|
|
|
return DATA_FOLDER . $this->get('id') . '/';
|
|
|
|
}
|
2015-08-23 18:56:41 +02:00
|
|
|
|
|
|
|
protected function getPollDir() {
|
|
|
|
return $this->getDir();
|
|
|
|
}
|
2016-05-18 22:18:36 +02:00
|
|
|
|
2015-08-22 23:47:31 +02:00
|
|
|
protected function getPath() {
|
|
|
|
return $this->getDir() . 'poll_data';
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getUsers() {
|
|
|
|
$users = [];
|
|
|
|
|
2015-10-17 15:44:27 +02:00
|
|
|
$userDir = DATA_FOLDER . $this->get('id') . '/user/';
|
2015-08-22 23:47:31 +02:00
|
|
|
if (is_dir($userDir)) {
|
|
|
|
$dir = opendir($userDir);
|
|
|
|
while(false !== ($file = readdir($dir))) {
|
|
|
|
if($file === '.' || $file === '..') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$users[] = User::restore($this->get('id') . '_' . $file)->export();
|
|
|
|
}
|
2015-08-23 06:18:05 +02:00
|
|
|
closedir($dir);
|
2015-08-22 23:47:31 +02:00
|
|
|
}
|
2016-05-18 22:18:36 +02:00
|
|
|
|
2015-08-22 23:47:31 +02:00
|
|
|
return $users;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function includeRelationships(&$data) {
|
|
|
|
$data->users = $this->getUsers();
|
|
|
|
}
|
|
|
|
|
2015-08-23 06:18:05 +02:00
|
|
|
private function isExpired() {
|
|
|
|
return
|
|
|
|
( $expirationDate = DateTime::createFromFormat('Y-m-d\TH:i:s.uO', $this->get('serverExpirationDate')) ) &&
|
|
|
|
$expirationDate < new DateTime();
|
|
|
|
}
|
|
|
|
|
2015-08-22 23:47:31 +02:00
|
|
|
public static function isValidId($id) {
|
|
|
|
$idCharacters = str_split($id);
|
2015-08-23 18:56:41 +02:00
|
|
|
return strlen($id) === 10 &&
|
|
|
|
count(array_diff($idCharacters, str_split(self::ID_CHARACTERS))) === 0;
|
2015-08-22 23:47:31 +02:00
|
|
|
}
|
2015-08-23 06:18:05 +02:00
|
|
|
|
|
|
|
protected function restoreHook() {
|
|
|
|
if ($this->isExpired()) {
|
|
|
|
$this->delete();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2015-08-22 23:47:31 +02:00
|
|
|
}
|