forked from froemken/dropbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.tx_faldropbox_tca.php
128 lines (111 loc) · 4.2 KB
/
class.tx_faldropbox_tca.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
/***************************************************************
* Copyright notice
*
* (c) 2012 Stefan froemken <[email protected]>
*
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
*
*
* @package sfstefan
* @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3 or later
*
*/
class tx_faldropbox_tca {
/**
* @var \TYPO3\CMS\Core\Registry
*/
protected $registry;
/**
* @var Dropbox_OAuth_PEAR
*/
protected $oauth;
/**
* initializes this object
*
* @param array $config
*/
public function initialization(array $config) {
$this->registry = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Registry');
$this->oauth = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('Dropbox_OAuth_PEAR', $config['appKey'], $config['appSecret']);
}
/**
* show informations about dropbox authentication
*
* @param array $PA
* @param array $fObj
* @return string
*/
public function dropboxLink($PA, $fObj) {
$config = $this->getConfiguration($PA);
if(empty($config['appKey'])) return '<div>You have to set App key first and save the record.</div>';
if(empty($config['appSecret'])) return '<div>You have to set App key first and save the record.</div>';
if(empty($config['accessType'])) return '<div>You have to save this record first.</div>';
$this->initialization($config);
$settings = $this->registry->get('fal_dropbox', 'config');
if(empty($settings)) {
// create a new request which the user have to apply
$settings['requestToken'] = $this->oauth->getRequestToken();
$settings['appKey'] = $config['appKey'];
$settings['appSecret'] = $config['appSecret'];
$settings['accessType'] = $config['accessType'];
$this->registry->set('fal_dropbox', 'config', $settings);
return '<div>Link: <a href="' . $this->oauth->getAuthorizeUrl() . '" target="_blank">Connect App with your Dropbox account</a></div>';
} elseif(empty($settings['requestToken'])) {
// create a new request which the user have to apply
$settings['requestToken'] = $this->oauth->getRequestToken();
$this->registry->set('fal_dropbox', 'config', $settings);
return '<div>Link: <a href="' . $this->oauth->getAuthorizeUrl() . '" target="_blank">Connect App with your Dropbox account</a></div>';
} elseif(empty($settings['accessToken'])) {
// ok now we can try to access the real access token
$this->oauth->setToken($settings['requestToken']);
try {
$settings['accessToken'] = $this->oauth->getAccessToken();
$this->registry->set('fal_dropbox', 'config', $settings);
} catch(Exception $e) {
// create a new request which the user have to apply
$settings['requestToken'] = $this->oauth->getRequestToken();
$this->registry->set('fal_dropbox', 'config', $settings);
return '<div>Link: <a href="' . $this->oauth->getAuthorizeUrl() . '" target="_blank">Connect App with your Dropbox account</a></div>';
}
} else {
$this->oauth->setToken($settings['accessToken']);
}
return '<div style="color: green;">You\'re now authenticated to your dropbox account</div>';
}
/**
* get configuration
*
* @param array $PA
* @return array
*/
public function getConfiguration($PA) {
$config = array();
if ($PA['row']['configuration']) {
$xmlArray = \TYPO3\CMS\Core\Utility\GeneralUtility::xml2array($PA['row']['configuration']);
foreach ($xmlArray['data']['sDEF']['lDEF'] as $key => $value) {
$config[$key] = $value['vDEF'];
}
}
return $config;
}
}
?>