1<?php
2/**
3 * @author Bernhard Posselt <dev@bernhard-posselt.com>
4 * @author Lukas Reschke <lukas@statuscode.ch>
5 * @author Morris Jobke <hey@morrisjobke.de>
6 * @author Robin Appelman <icewind@owncloud.com>
7 * @author Robin McCorkell <robin@mccorkell.me.uk>
8 * @author Thomas Müller <thomas.mueller@tmit.eu>
9 *
10 * @copyright Copyright (c) 2018, ownCloud GmbH
11 * @license AGPL-3.0
12 *
13 * This code is free software: you can redistribute it and/or modify
14 * it under the terms of the GNU Affero General Public License, version 3,
15 * as published by the Free Software Foundation.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Affero General Public License for more details.
21 *
22 * You should have received a copy of the GNU Affero General Public License, version 3,
23 * along with this program.  If not, see <http://www.gnu.org/licenses/>
24 *
25 */
26
27namespace OC\AppFramework\Core;
28use OCP\AppFramework\IApi;
29
30/**
31 * This is used to wrap the owncloud static api calls into an object to make the
32 * code better abstractable for use in the dependency injection container
33 *
34 * Should you find yourself in need for more methods, simply inherit from this
35 * class and add your methods
36 * @deprecated
37 */
38class API implements IApi {
39	private $appName;
40
41	/**
42	 * constructor
43	 * @param string $appName the name of your application
44	 */
45	public function __construct($appName) {
46		$this->appName = $appName;
47	}
48
49	/**
50	 * Gets the userid of the current user
51	 * @return string the user id of the current user
52	 * @deprecated Use \OC::$server->getUserSession()->getUser()->getUID()
53	 */
54	public function getUserId() {
55		return \OCP\User::getUser();
56	}
57
58	/**
59	 * Adds a new javascript file
60	 * @deprecated include javascript and css in template files
61	 * @param string $scriptName the name of the javascript in js/ without the suffix
62	 * @param string $appName the name of the app, defaults to the current one
63	 */
64	public function addScript($scriptName, $appName=null) {
65		if ($appName === null) {
66			$appName = $this->appName;
67		}
68		\OCP\Util::addScript($appName, $scriptName);
69	}
70
71	/**
72	 * Adds a new css file
73	 * @deprecated include javascript and css in template files
74	 * @param string $styleName the name of the css file in css/without the suffix
75	 * @param string $appName the name of the app, defaults to the current one
76	 */
77	public function addStyle($styleName, $appName=null) {
78		if ($appName === null) {
79			$appName = $this->appName;
80		}
81		\OCP\Util::addStyle($appName, $styleName);
82	}
83
84	/**
85	 * @deprecated include javascript and css in template files
86	 * shorthand for addScript for files in the 3rdparty directory
87	 * @param string $name the name of the file without the suffix
88	 */
89	public function add3rdPartyScript($name) {
90		\OCP\Util::addScript($this->appName . '/3rdparty', $name);
91	}
92
93	/**
94	 * @deprecated include javascript and css in template files
95	 * shorthand for addStyle for files in the 3rdparty directory
96	 * @param string $name the name of the file without the suffix
97	 */
98	public function add3rdPartyStyle($name) {
99		\OCP\Util::addStyle($this->appName . '/3rdparty', $name);
100	}
101
102	/**
103	 * @deprecated communication between apps should happen over built in
104	 * callbacks or interfaces (check the contacts and calendar managers)
105	 * Checks if an app is enabled
106	 * also use \OC::$server->getAppManager()->isEnabledForUser($appName)
107	 * @param string $appName the name of an app
108	 * @return bool true if app is enabled
109	 */
110	public function isAppEnabled($appName) {
111		return \OCP\App::isEnabled($appName);
112	}
113
114	/**
115	 * used to return and open a new event source
116	 * @return \OCP\IEventSource a new open EventSource class
117	 * @deprecated Use \OC::$server->createEventSource();
118	 */
119	public function openEventSource() {
120		return \OC::$server->createEventSource();
121	}
122
123	/**
124	 * @deprecated register hooks directly for class that build in hook interfaces
125	 * connects a function to a hook
126	 * @param string $signalClass class name of emitter
127	 * @param string $signalName name of signal
128	 * @param string $slotClass class name of slot
129	 * @param string $slotName name of slot, in another word, this is the
130	 *               name of the method that will be called when registered
131	 *               signal is emitted.
132	 * @return bool always true
133	 */
134	public function connectHook($signalClass, $signalName, $slotClass, $slotName) {
135		return \OCP\Util::connectHook($signalClass, $signalName, $slotClass, $slotName);
136	}
137
138	/**
139	 * @deprecated implement the emitter interface instead
140	 * Emits a signal. To get data from the slot use references!
141	 * @param string $signalClass class name of emitter
142	 * @param string $signalName name of signal
143	 * @param array $params default: array() array with additional data
144	 * @return bool true if slots exists or false if not
145	 */
146	public function emitHook($signalClass, $signalName, $params = []) {
147		return  \OCP\Util::emitHook($signalClass, $signalName, $params);
148	}
149
150	/**
151	 * clear hooks
152	 * @deprecated clear hooks directly for class that build in hook interfaces
153	 * @param string $signalClass
154	 * @param string $signalName
155	 */
156	public function clearHook($signalClass=false, $signalName=false) {
157		if ($signalClass) {
158			\OC_Hook::clear($signalClass, $signalName);
159		}
160	}
161
162	/**
163	 * Register a backgroundjob task
164	 * @param string $className full namespace and class name of the class
165	 * @param string $methodName the name of the static method that should be
166	 * called
167	 * @deprecated Use \OC::$server->getJobList()->add();
168	 */
169	public function addRegularTask($className, $methodName) {
170		\OCP\BackgroundJob::addRegularTask($className, $methodName);
171	}
172
173	/**
174	 * Tells ownCloud to include a template in the admin overview
175	 * @param string $mainPath the path to the main php file without the php
176	 * suffix, relative to your apps directory! not the template directory
177	 * @param string $appName the name of the app, defaults to the current one
178	 */
179	public function registerAdmin($mainPath, $appName=null) {
180		if ($appName === null) {
181			$appName = $this->appName;
182		}
183
184		\OCP\App::registerAdmin($appName, $mainPath);
185	}
186}
187