1
|
|
|
<?php |
|
|
|
|
2
|
|
|
/** |
3
|
|
|
* @author Thomas Tanghus |
4
|
|
|
* @copyright 2013-2014 Thomas Tanghus ([email protected]) |
5
|
|
|
* |
6
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
7
|
|
|
* later. |
8
|
|
|
* See the COPYING-README file. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace OCA\Contacts; |
12
|
|
|
|
13
|
|
|
use Sabre\VObject, |
14
|
|
|
OCP\AppFramework, |
15
|
|
|
OCA\Contacts\Controller\AddressBookController, |
16
|
|
|
OCA\Contacts\Controller\BackendController, |
17
|
|
|
OCA\Contacts\Controller\GroupController, |
18
|
|
|
OCA\Contacts\Controller\ContactController, |
19
|
|
|
OCA\Contacts\Controller\ContactPhotoController, |
20
|
|
|
OCA\Contacts\Controller\SettingsController, |
21
|
|
|
OCA\Contacts\Controller\ImportController; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* This class manages our app actions |
25
|
|
|
* |
26
|
|
|
* TODO: Merge in Dispatcher |
27
|
|
|
*/ |
28
|
1 |
|
App::$l10n = \OC::$server->getL10N('contacts'); |
29
|
|
|
|
30
|
|
|
class App { |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @brief Categories of the user |
34
|
|
|
* @var OC_VCategories |
35
|
|
|
*/ |
36
|
|
|
public static $categories = null; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @brief language object for calendar app |
40
|
|
|
* |
41
|
|
|
* @var \OCP\IL10N |
42
|
|
|
*/ |
43
|
|
|
public static $l10n; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* An array holding the current users address books. |
47
|
|
|
* @var array |
48
|
|
|
*/ |
49
|
|
|
protected static $addressBooks = array(); |
50
|
|
|
/** |
51
|
|
|
* If backends are added to this map, they will be automatically mapped |
52
|
|
|
* to their respective classes, if constructed with the 'getBackend' method. |
53
|
|
|
* |
54
|
|
|
* @var array |
55
|
|
|
*/ |
56
|
|
|
public static $backendClasses = array( |
57
|
|
|
'local' => 'OCA\Contacts\Backend\Database', |
58
|
|
|
'shared' => 'OCA\Contacts\Backend\Shared', |
59
|
|
|
// 'localusers' => 'OC\Contacts\Backend\LocalUsers', |
60
|
|
|
); |
61
|
|
|
|
62
|
2 |
|
public function __construct( |
63
|
|
|
$user = null, |
64
|
|
|
$addressBooksTableName = '*PREFIX*addressbook', |
65
|
|
|
$backendsTableName = '*PREFIX*addressbooks_backend', |
66
|
|
|
$dbBackend = null |
67
|
|
|
) { |
68
|
2 |
|
$this->user = $user ? $user : \OC::$server->getUserSession()->getUser()->getUId(); |
|
|
|
|
69
|
2 |
|
$this->addressBooksTableName = $addressBooksTableName; |
|
|
|
|
70
|
2 |
|
$this->backendsTableName = $backendsTableName; |
|
|
|
|
71
|
2 |
|
$this->dbBackend = $dbBackend |
|
|
|
|
72
|
2 |
|
? $dbBackend |
73
|
2 |
|
: new Backend\Database($user); |
74
|
2 |
|
if (\OCP\Config::getAppValue('contacts', 'backend_ldap', "false") === "true") { |
75
|
|
|
self::$backendClasses['ldap'] = 'OCA\Contacts\Backend\Ldap'; |
76
|
|
|
} |
77
|
2 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Gets backend by name. |
81
|
|
|
* |
82
|
|
|
* @param string $name |
83
|
|
|
* @return \Backend\AbstractBackend |
84
|
|
|
*/ |
85
|
2 |
|
public function getBackend($name) { |
86
|
2 |
|
$name = $name ? $name : 'local'; |
87
|
2 |
|
if (isset(self::$backendClasses[$name])) { |
88
|
2 |
|
return new self::$backendClasses[$name]($this->user); |
89
|
|
|
} else { |
90
|
|
|
throw new \Exception('No backend for: ' . $name, '404'); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Return all registered address books for current user. |
96
|
|
|
* For now this is hard-coded to using the Database and |
97
|
|
|
* Shared backends, but eventually admins will be able to |
98
|
|
|
* register additional backends, and users will be able to |
99
|
|
|
* subscribe to address books using those backends. |
100
|
|
|
* |
101
|
|
|
* @return AddressBook[] |
102
|
|
|
*/ |
103
|
|
|
public function getAddressBooksForUser() { |
104
|
|
|
if (!self::$addressBooks) { |
|
|
|
|
105
|
|
|
foreach (array_keys(self::$backendClasses) as $backendName) { |
106
|
|
|
$backend = self::getBackend($backendName, $this->user); |
|
|
|
|
107
|
|
|
$addressBooks = $backend->getAddressBooksForUser(); |
108
|
|
|
|
109
|
|
|
foreach ($addressBooks as $addressBook) { |
110
|
|
|
$addressBook['backend'] = $backendName; |
111
|
|
|
self::$addressBooks[] = new AddressBook($backend, $addressBook); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return self::$addressBooks; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Get an address book from a specific backend. |
123
|
|
|
* |
124
|
|
|
* @param string $backendName |
125
|
|
|
* @param string $addressbookid |
126
|
|
|
* @return AddressBook|null |
127
|
|
|
*/ |
128
|
2 |
|
public function getAddressBook($backendName, $addressbookid) { |
129
|
|
|
//\OCP\Util::writeLog('contacts', __METHOD__ . ': '. $backendName . ', ' . $addressbookid, \OCP\Util::DEBUG); |
130
|
2 |
|
foreach (self::$addressBooks as $addressBook) { |
131
|
2 |
|
if ($addressBook->getBackend()->name === $backendName |
132
|
2 |
|
&& $addressBook->getId() === $addressbookid |
133
|
2 |
|
) { |
134
|
1 |
|
return $addressBook; |
135
|
|
|
} |
136
|
2 |
|
} |
137
|
|
|
|
138
|
2 |
|
$backend = self::getBackend($backendName, $this->user); |
|
|
|
|
139
|
2 |
|
$info = $backend->getAddressBook($addressbookid); |
140
|
|
|
|
141
|
2 |
|
if (!$info) { |
142
|
|
|
throw new \Exception(self::$l10n->t('Address book not found'), 404); |
143
|
|
|
} |
144
|
|
|
|
145
|
2 |
|
$addressBook = new AddressBook($backend, $info); |
146
|
2 |
|
self::$addressBooks[] = $addressBook; |
147
|
2 |
|
return $addressBook; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Get a Contact from an address book from a specific backend. |
152
|
|
|
* |
153
|
|
|
* @param string $backendName |
154
|
|
|
* @param string $addressbookid |
155
|
|
|
* @param string $id - Contact id |
156
|
|
|
* @return Contact|null |
157
|
|
|
* |
158
|
|
|
*/ |
159
|
2 |
|
public function getContact($backendName, $addressbookid, $id) { |
160
|
2 |
|
$addressBook = $this->getAddressBook($backendName, $addressbookid); |
161
|
2 |
|
return $addressBook->getChild($id); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
} |
165
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.