Completed
Push — master ( c4929a...3bd4fb )
by Julius
19:34 queued 18:15
created

Helper::getGuestName()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 10
c 0
b 0
f 0
cc 4
nc 2
nop 0
crap 20
1
<?php
2
/**
3
 * ownCloud - Richdocuments App
4
 *
5
 * @author Victor Dubiniuk
6
 * @copyright 2013 Victor Dubiniuk [email protected]
7
 *
8
 * This file is licensed under the Affero General Public License version 3 or
9
 * later.
10
 */
11
12
namespace OCA\Richdocuments;
13
14
use \DateTime;
15
use \DateTimeZone;
16
use OCP\Files\Folder;
17
18
class Helper {
19
20
	/** @var string|null */
21
	private $userId;
22
23
	public function __construct($userId) {
24
		$this->userId = $userId;
25
	}
26
27
	/**
28
	 * @param string $fileId
29
	 * @return array
30
	 * @throws \Exception
31
	 */
32
	public static function parseFileId($fileId) {
33
		$arr = explode('_', $fileId);
34
		$templateId = null;
35
		if (count($arr) === 1) {
36
			$fileId = $arr[0];
37
			$instanceId = '';
38
			$version = '0';
39
		} else if (count($arr) === 2) {
40
			list($fileId, $instanceId) = $arr;
41
			$version = '0';
42
		} else if (count($arr) === 3) {
43
			list($fileId, $instanceId, $version) = $arr;
44
		} else {
45
			throw new \Exception('$fileId has not the expected format');
46
		}
47
48
		if (strpos($fileId, '-') !== false) {
49
			list($fileId, $templateId) = explode('/', $fileId);
50
		}
51
52
		return [
53
			$fileId,
54
			$instanceId,
55
			$version,
56
			$templateId
57
		];
58
	}
59
60
	/**
61
	 * WOPI helper function to convert to ISO 8601 round-trip format.
62
	 * @param integer $time Must be seconds since unix epoch
63
	 */
64
	public static function toISO8601($time)
65
	{
66
		// TODO: Be more precise and don't ignore milli, micro seconds ?
67
		$datetime = DateTime::createFromFormat('U', $time, new DateTimeZone('UTC'));
68
		if ($datetime)
69
			return $datetime->format('Y-m-d\TH:i:s.u\Z');
70
71
		return false;
72
	}
73
74
	public static function getNewFileName(Folder $folder, $filename) {
75
		$fileNum = 1;
76
77
		while ($folder->nodeExists($filename)) {
78
			$fileNum++;
79
			$filename = preg_replace('/(\.| \(\d+\)\.)([^.]*)$/', ' (' . $fileNum . ').$2', $filename);
80
		}
81
82
		return $filename;
83
	}
84
85
	public function getGuestName() {
86
		if ($this->userId !== null || !isset($_COOKIE['guestUser']) || $_COOKIE['guestUser'] === '') {
87
			return null;
88
		}
89
		return $_COOKIE['guestUser'];
90
	}
91
92
}
93