1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* ownCloud - News |
4
|
|
|
* |
5
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
6
|
|
|
* later. See the COPYING file. |
7
|
|
|
* |
8
|
|
|
* @author Alessandro Cosentino <[email protected]> |
9
|
|
|
* @author Bernhard Posselt <[email protected]> |
10
|
|
|
* @copyright Alessandro Cosentino 2012 |
11
|
|
|
* @copyright Bernhard Posselt 2012, 2014 |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace OCA\News\Controller; |
15
|
|
|
|
16
|
|
|
use \OCP\IRequest; |
17
|
|
|
use \OCP\IUserSession; |
18
|
|
|
use \OCP\IURLGenerator; |
19
|
|
|
use \OCP\Files\IRootFolder; |
20
|
|
|
use \OCP\AppFramework\ApiController; |
|
|
|
|
21
|
|
|
use \OCP\AppFramework\Http; |
22
|
|
|
|
23
|
|
|
class UserApiController extends ApiController { |
24
|
|
|
|
25
|
|
|
private $userSession; |
26
|
|
|
private $rootFolder; |
27
|
|
|
|
28
|
|
|
public function __construct($AppName, |
29
|
|
|
IRequest $request, |
30
|
|
|
IUserSession $userSession, |
31
|
|
|
IRootFolder $rootFolder){ |
32
|
|
|
parent::__construct($AppName, $request); |
33
|
|
|
$this->userSession = $userSession; |
34
|
|
|
$this->rootFolder = $rootFolder; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @NoAdminRequired |
39
|
|
|
* @NoCSRFRequired |
40
|
|
|
* @CORS |
41
|
|
|
*/ |
42
|
|
|
public function index() { |
43
|
|
|
$user = $this->userSession->getUser(); |
44
|
|
|
|
45
|
|
|
// find the avatar |
46
|
|
|
$jpgAvatar = '/' . $user->getUID() . '/avatar.jpg'; |
47
|
|
|
$pngAvatar = '/' . $user->getUID() . '/avatar.png'; |
48
|
|
|
$avatar = null; |
49
|
|
|
|
50
|
|
|
if ($this->rootFolder->nodeExists($jpgAvatar)) { |
51
|
|
|
$file = $this->rootFolder->get($jpgAvatar); |
52
|
|
|
$avatar = [ |
53
|
|
|
'data' => base64_encode($file->getContent()), |
54
|
|
|
'mime' => 'image/jpeg' |
55
|
|
|
]; |
56
|
|
|
} elseif ($this->rootFolder->nodeExists($pngAvatar)) { |
57
|
|
|
$file = $this->rootFolder->get($pngAvatar); |
58
|
|
|
$avatar = [ |
59
|
|
|
'data' => base64_encode($file->getContent()), |
60
|
|
|
'mime' => 'image/png' |
61
|
|
|
]; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return [ |
65
|
|
|
'userId' => $user->getUID(), |
66
|
|
|
'displayName' => $user->getDisplayName(), |
67
|
|
|
'lastLoginTimestamp' => $user->getLastLogin(), |
68
|
|
|
'avatar' => $avatar |
69
|
|
|
]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: