1<?php
2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
3//
4// All Rights Reserved. See copyright.txt for details and a complete list of authors.
5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
6// $Id$
7
8class Services_File_VimeoController
9{
10	private $utilities;
11
12	function setUp()
13	{
14		Services_Exception_Disabled::check('vimeo_upload');
15		$this->utilities = new Services_File_Utilities;
16	}
17
18	function action_authorize()
19	{
20		$servicelib = TikiLib::lib('service');
21		TikiLib::lib('access')->redirect(
22			$servicelib->getUrl(
23				[
24					'controller' => 'oauth',
25					'action' => 'request',
26					'provider' => 'vimeo',
27				]
28			)
29		);
30	}
31
32	function action_upload($input)
33	{
34		global $prefs, $tiki_p_admin;
35
36		$galleryId = $input->galleryId->int() ?: $prefs['vimeo_default_gallery'];
37		$fieldId = $input->fromFieldId->int();
38		$itemId = $input->fromItemId->int();
39
40		$this->utilities->checkTargetGallery($galleryId);
41
42		$vimeolib = TikiLib::lib('vimeo');
43
44		if (! $vimeolib->isAuthorized()) {
45			throw new Services_Exception_NotAvailable(tr('Vimeo not authenticated.'));
46		}
47
48		$quota = $vimeolib->getQuota();
49		$ticket = $vimeolib->getTicket();
50
51		$errMsg = '';
52		$availableMB = 0;
53		$availableSD = 0;
54		$availableHD = 0;
55
56		if (! empty($ticket['error'])) {
57			$errMsg = tra($ticket['error']);
58			if ($tiki_p_admin === 'y' && isset($ticket['developer_message'])) {
59				$errMsg .= "\n" . tra($ticket['developer_message']);
60			}
61		} elseif (! empty($quota['error'])) {
62			$errMsg = tra($quota['error']['msg']);
63			if ($tiki_p_admin === 'y' && isset($quota['developer_message'])) {
64				$errMsg .= "\n" . tra($quota['developer_message']);
65			}
66		} else {
67			$availableMB = round($quota['space']['free'] / 1024 / 1024, 1);
68			$availableSD = $quota['quota']['sd'];
69			$availableHD = $quota['quota']['hd'];
70		}
71
72		if (empty($input->title->text())) {
73			$title = tr('Upload Video');
74		} else {
75			$title = $input->title->text();
76		}
77
78		return [
79			'title' => $title,
80			'availableMB' => $availableMB,
81			'availableSD' => $availableSD,
82			'availableHD' => $availableHD,
83			'ticket' => $ticket,
84			'galleryId' => $galleryId,
85			'fieldId' => $fieldId,
86			'itemId' => $itemId,
87			'errMsg' => $errMsg,
88		];
89	}
90
91	function action_complete($input)
92	{
93		global $tiki_p_admin;
94
95		$galleryId = $input->galleryId->int();
96		$gal_info = $this->utilities->checkTargetGallery($galleryId);
97
98		$completeUri = $input->completeUri->url();
99		$title = $input->title->text();
100		$filename = basename($input->file->text());
101
102		$vimeolib = TikiLib::lib('vimeo');
103
104		$completeInfo = $vimeolib->complete($completeUri);
105
106		$video = '';
107		$url = '';
108		$fileId = 0;
109
110		if (! empty($completeInfo['error'])) {
111			$errMsg = tra($completeInfo['error']);
112			if ($tiki_p_admin === 'y' && isset($completeInfo['developer_message'])) {
113				$errMsg .= "\n" . tra($completeInfo['developer_message']);
114			}
115		} elseif (! empty($completeInfo['Location'])) {
116			$errMsg = '';
117
118			$location = $completeInfo['Location'];
119
120
121			$urlparts = explode('/', $completeInfo['Location']);
122			foreach ($urlparts as $urlpart) {
123				if (ctype_digit($urlpart)) {
124					$video = $urlpart;
125				}
126			}
127
128			$vimeolib->setTitle($video, $title);
129			$url = 'http://vimeo.com' . $location;
130
131			$info = [
132			'expires' => TikiLib::lib('tiki')->now,
133			'etag' => null,
134			];
135
136			$size = $chunks['chunk']['size'];
137
138			// Add placeholder directly without verification, URL will show 404 until processed
139			$filegallib = TikiLib::lib('filegal');
140			$fileId = $this->utilities->uploadFile($gal_info, $title ?: $filename, $size, 'video/vimeo', 'REFERENCE');
141			$filegallib->attach_file_source($fileId, $url, $info, 1);
142		} else {
143			$errMsg = tra('Unknown error');
144		}
145
146		return [
147			'ticket' => $ticket,
148			'file' => $filename,
149			'name' => $title ?: $filename,
150			'video' => $video,
151			'url' => $url,
152			'fileId' => $fileId,
153			'err' => $errMsg,
154		];
155	}
156
157	/**
158	 * View controller function. Best-used when called from a bootstrap_modal smarty function.
159	 * @param $input
160	 * @return array
161	 * @throws Exception
162	 */
163	function action_view($input)
164	{
165		$fileId = $input->file_id->text();
166		$vimeoUrl = $input->vimeo_url->text();
167
168		if ($fileId) {
169			$filelib = TikiLib::lib("filegal");
170			$file = $filelib->get_file_info($fileId);
171		}
172
173		if ($input->title->text()) {
174			$title = $input->title->text();
175		} elseif (isset($file)) {
176			$title = $file["filename"];
177		}
178
179		return [
180			"title" => $title,
181			"file_id" => $fileId,
182			"vimeo_url" => $vimeoUrl,
183		];
184	}
185}
186