/
class.tx_wecmap_domainmgr.php
271 lines (216 loc) · 8.12 KB
/
class.tx_wecmap_domainmgr.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<?php
/***************************************************************
* Copyright notice
*
* (c) 2005-2009 Christian Technology Ministries International Inc.
* All rights reserved
*
* This file is part of the Web-Empowered Church (WEC)
* (http://WebEmpoweredChurch.org) ministry of Christian Technology Ministries
* International (http://CTMIinc.org). The WEC is developing TYPO3-based
* (http://typo3.org) free software for churches around the world. Our desire
* is to use the Internet to help offer new life through Jesus Christ. Please
* see http://WebEmpoweredChurch.org/Jesus.
*
* You can redistribute this file and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation;
* either version 2 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 file is distributed in the hope that it will be useful for ministry,
* 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 file!
***************************************************************/
/**
* Domain <=> API Key manager class for the WEC Map extension. This class
* provides user functions for handling domains and API keys
*/
class tx_wecmap_domainmgr {
public $extKey = 'wec_map';
/**
* @var \TYPO3\CMS\Extbase\Object\ObjectManager
*/
protected $objectManager;
function getKey($domain = null) {
// check to see if this is an update from the old config schema and convert to the new
$isOld = $this->checkForOldConfig();
// get key from configuration
$keyConfig = tx_wecmap_backend::getExtConf('apiKey.google');
// if we are using the old config, return the old key this time. It will be changed for next time.
if($isOld) return $keyConfig;
// get current domain
if($domain == null) $domain = \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('HTTP_HOST');
// loop through all the domain->key pairs we have to find the right one
$found = false;
foreach( $keyConfig as $key => $value ) {
if($domain == $key) {
$found = true;
return $value;
}
}
// if we didn't get an exact match, check for partials and guess
if(!$found) {
foreach( $keyConfig as $key => $value ) {
if(strpos($domain, $key) !== false) {
$found = true;
return $value;
}
}
} else {
return null;
}
}
function checkForOldConfig() {
global $TYPO3_CONF_VARS;
$keyConfig = tx_wecmap_backend::getExtConf('apiKey.google');
if(is_array($keyConfig)) return false;
$key = $keyConfig;
$domain = \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('HTTP_HOST');
$this->saveApiKey(array($domain => $key));
return true;
}
function processPost($post) {
$allDomains = $this->getAllDomains();
// prepare the two arrays we need in the loop
$extconfArray = array();
$returnArray = array();
// get total number of domain->key pairs
$number = count($post)/2;
// loop through all the pairs
for ( $i=0; $i < $number; $i++ ) {
// get the domain and key
$curKey = $post[$i+$number];
$curDomain = $post[$i];
// if there is no key, we don't want to save it in extconf
if(!empty($curKey) && !empty($curDomain)) $extconfArray[$curDomain] = $curKey;
// get all but manually added domains
$domains1 = $this->getRequestDomain();
$domains2 = $this->getDomainRecords();
$domains = array_keys(array_merge($domains1, $domains2));
// if there is no domain, or we want to delete a domain, we won't return it.
// we also make sure not to recommend domains that were just deleted but manually added before
if(!empty($curDomain) && !(!empty($allDomains[$curDomain]) && empty($curKey) && !in_array($curDomain, $domains))) $returnArray[$curDomain] = $curKey;
}
// save the domain->key pairs, even if empty
$this->saveApiKey($extconfArray);
// sort the array and reverse it so we show filled out records first, empty ones last
asort($returnArray);
return array_reverse($returnArray);
}
/*
* Looks up the API key in extConf within localconf.php
* @return array The Google Maps API keys.
*/
function getApiKeys() {
return tx_wecmap_backend::getExtConf('apiKey.google');
}
/*
* Saves the API key to extConf in localconf.php.
* @param string The new Google Maps API Key.
* @return none
*/
function saveApiKey($dataArray) {
global $TYPO3_CONF_VARS;
$extConf = unserialize($TYPO3_CONF_VARS['EXT']['extConf'][$this->extKey]);
$extConf['apiKey.']['google'] = $dataArray;
$this->objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
$instObj = $this->objectManager->get(\TYPO3\CMS\Core\Configuration\ConfigurationManager::class);
// Get lines from localconf file
$lines = $instObj->getLocalConfigurationFileLocation();
$instObj->removeLocalConfigurationKeysByPath( array( 'EXT/extConf/'.$this->extKey ) ) ;
$instObj->setLocalConfigurationValueByPath( 'EXT/extConf/'.$this->extKey , serialize($extConf) );
}
/**
* Returns an assoc array with domains as key and api key as value
*
* @return array
**/
function getAllDomains() {
$domainRecords = $this->getDomainRecords();
// get domains entries from extconf
$extconfDomains = $this->getApiKeys();
// get domain from the current http request
$requestDomain = $this->getRequestDomain();
// Now combine all the records we got into one array with the domain as key and the api key as value
return $this->combineAndSort($domainRecords, $extconfDomains, $requestDomain);
}
/**
* Returns an assoc array with domain record domains as keys and api key as value
*
* @return array
**/
function getDomainRecords() {
// get domain records
$domainRecords = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('domainName', 'sys_domain', 'hidden=0');
$newArray = array();
foreach( $domainRecords as $key => $value ) {
$newArray[$value['domainName']] = '';
}
return $newArray;
}
/**
* Returns the domain of the current http request
*
* @return array
**/
function getRequestDomain() {
// get domain from the current http request
$requestDomain = \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('HTTP_HOST');
return array($requestDomain => '');
}
/**
* combine all the arrays, making each key unique and preferring the one that has a value,
* then sort so that all empty values are last
*
* @return array
**/
function combineAndSort($a1, $a2, $a3) {
if(!is_array($a1)) $a1 = array();
if(!is_array($a2)) $a2 = array();
if(!is_array($a3)) $a3 = array();
// combine the first and the second
$temp1 = array();
foreach( $a1 as $key => $value ) {
// if there is the same key in array2, check the values
if(array_key_exists($key, $a2)) {
// if a2 doesn't have a value, use a1's value
if(empty($a2[$key])) {
$temp1[$key] = $value;
} else {
$temp1[$key] = $a2[$key];
}
} else {
$temp1[$key] = $value;
}
}
$temp2 = array_merge($a2, $temp1);
// combine the temp and the third
$temp3 = array();
foreach( $temp2 as $key => $value ) {
// if there is the same key in array2, check the values
if(array_key_exists($key, $a3)) {
// if a3 doesn't have a value, use a1's value
if(empty($a3[$key])) {
$temp3[$key] = $value;
} else {
$temp3[$key] = $a3[$key];
}
} else {
$temp3[$key] = $value;
}
}
// merge the third into the second
$temp4 = array_merge($a3, $temp3);
// sort by value, reverse, and return
asort($temp4);
return array_reverse($temp4);
}
}
if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/wec_map/class.tx_wecmap_domainmgr.php']) {
include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/wec_map/class.tx_wecmap_domainmgr.php']);
}