* @brief Main API entry point
*
* @param App $a App
+ * @param App\Arguments $args The app arguments (optional, will retrieved by the DI-Container in case of missing)
* @return string|array API call result
* @throws Exception
*/
-function api_call(App $a)
+function api_call(App $a, App\Arguments $args = null)
{
global $API, $called_api;
+ if ($args == null) {
+ $args = DI::args();
+ }
+
$type = "json";
- if (strpos(DI::args()->getQueryString(), ".xml") > 0) {
+ if (strpos($args->getQueryString(), ".xml") > 0) {
$type = "xml";
}
- if (strpos(DI::args()->getQueryString(), ".json") > 0) {
+ if (strpos($args->getQueryString(), ".json") > 0) {
$type = "json";
}
- if (strpos(DI::args()->getQueryString(), ".rss") > 0) {
+ if (strpos($args->getQueryString(), ".rss") > 0) {
$type = "rss";
}
- if (strpos(DI::args()->getQueryString(), ".atom") > 0) {
+ if (strpos($args->getQueryString(), ".atom") > 0) {
$type = "atom";
}
try {
foreach ($API as $p => $info) {
- if (strpos(DI::args()->getQueryString(), $p) === 0) {
+ if (strpos($args->getQueryString(), $p) === 0) {
if (!api_check_method($info['method'])) {
throw new MethodNotAllowedException();
}
throw new NotImplementedException();
} catch (HTTPException $e) {
header("HTTP/1.1 {$e->getCode()} {$e->httpdesc}");
- return api_error($type, $e);
+ return api_error($type, $e, $args);
}
}
*
* @param string $type Return type (xml, json, rss, as)
* @param object $e HTTPException Error object
+ * @param App\Arguments $args The App arguments
* @return string|array error message formatted as $type
*/
-function api_error($type, $e)
+function api_error($type, $e, App\Arguments $args)
{
- $a = \get_app();
-
$error = ($e->getMessage() !== "" ? $e->getMessage() : $e->httpdesc);
/// @TODO: https://dev.twitter.com/overview/api/response-codes
$error = ["error" => $error,
"code" => $e->getCode() . " " . $e->httpdesc,
- "request" => DI::args()->getQueryString()];
+ "request" => $args->getQueryString()];
$return = api_format_data('status', $type, ['status' => $error]);
public $argv;
/** @deprecated 2019.09 - use App\Arguments->getArgc() */
public $argc;
- /** @deprecated 2019.09 - Use App\Module->getName() instead */
- public $module;
public $timezone;
public $interactive = true;
public $identities;
* @param App\Arguments $args The Friendica Arguments of the call
* @param Core\Process $process The process methods
*/
- public function __construct(Database $database, Configuration $config, App\Mode $mode, BaseURL $baseURL, LoggerInterface $logger, Profiler $profiler, L10n $l10n, Arguments $args, App\Module $module, App\Page $page, Core\Process $process)
+ public function __construct(Database $database, Configuration $config, App\Mode $mode, BaseURL $baseURL, LoggerInterface $logger, Profiler $profiler, L10n $l10n, Arguments $args, App\Page $page, Core\Process $process)
{
$this->database = $database;
$this->config = $config;
$this->argv = $args->getArgv();
$this->argc = $args->getArgc();
- $this->module = $module->getName();
$this->page = $page;
$this->load();
use Friendica\Core\System;
use Friendica\Database\Database;
use Friendica\Database\DBA;
+use Friendica\DI;
use Friendica\Model\User;
use Friendica\Network\HTTPException;
use Friendica\Util\DateTimeFormat;
if ($login_initial) {
Hook::callAll('logged_in', $a->user);
- if ($a->module !== 'home' && $this->session->exists('return_path')) {
+ if (DI::module()->getName() !== 'home' && $this->session->exists('return_path')) {
$this->baseUrl->redirect($this->session->get('return_path'));
}
}
if (Session::isAuthenticated()) {
$nav['logout'] = ['logout', L10n::t('Logout'), '', L10n::t('End this session')];
} else {
- $nav['login'] = ['login', L10n::t('Login'), ($a->module == 'login' ? 'selected' : ''), L10n::t('Sign in')];
+ $nav['login'] = ['login', L10n::t('Login'), (DI::module()->getName() == 'login' ? 'selected' : ''), L10n::t('Sign in')];
}
if (local_user()) {
$homelink = Session::get('visitor_home', '');
}
- if (($a->module != 'home') && (! (local_user()))) {
+ if ((DI::module()->getName() != 'home') && (! (local_user()))) {
$nav['home'] = [$homelink, L10n::t('Home'), '', L10n::t('Home Page')];
}
$arr = ['contact' => $contacts, 'entry' => $o];
// e.g. 'network_pre_contact_deny', 'profile_pre_contact_allow'
- Hook::callAll($a->module . '_pre_' . $selname, $arr);
+ Hook::callAll(DI::module()->getName() . '_pre_' . $selname, $arr);
if (DBA::isResult($contacts)) {
foreach ($contacts as $contact) {
$o .= '</select>' . PHP_EOL;
- Hook::callAll($a->module . '_post_' . $selname, $o);
+ Hook::callAll(DI::module()->getName() . '_post_' . $selname, $o);
return $o;
}
$arr = ['contact' => $contacts, 'entry' => $o];
// e.g. 'network_pre_contact_deny', 'profile_pre_contact_allow'
- Hook::callAll($a->module . '_pre_' . $selname, $arr);
+ Hook::callAll(DI::module()->getName() . '_pre_' . $selname, $arr);
$receiverlist = [];
$o .= implode(', ', $receiverlist);
}
- Hook::callAll($a->module . '_post_' . $selname, $o);
+ Hook::callAll(DI::module()->getName() . '_post_' . $selname, $o);
return $o;
}
// User ID that we know is not in the database
$this->wrongUserId = 666;
- $session = DI::session();
- $session->start();
+ DI::session()->start();
// Most API require login so we force the session
$_SESSION = [
}
];
$_SERVER['REQUEST_METHOD'] = 'method';
+ $_SERVER['QUERY_STRING'] = 'q=api_path';
$_GET['callback'] = 'callback_name';
- $this->app->query_string = 'api_path';
+ $args = DI::args()->determine($_SERVER, $_GET);
+
$this->assertEquals(
'callback_name(["some_data"])',
- api_call($this->app)
+ api_call($this->app, $args)
);
}
return ['data' => ['some_data']];
}
];
+
$_SERVER['REQUEST_METHOD'] = 'method';
+ $_SERVER['QUERY_STRING'] = 'q=api_path';
+
+ $args = DI::args()->determine($_SERVER, $_GET);
+
$this->config->set('system', 'profiler', true);
$this->config->set('rendertime', 'callstack', true);
$this->app->callstack = [
'network' => ['some_function' => 200]
];
- $this->app->query_string = 'api_path';
$this->assertEquals(
'["some_data"]',
- api_call($this->app)
+ api_call($this->app, $args)
);
}
}
];
$_SERVER['REQUEST_METHOD'] = 'method';
+ $_SERVER['QUERY_STRING'] = 'q=api_path';
+
+ $args = DI::args()->determine($_SERVER, $_GET);
- $this->app->query_string = 'api_path';
$this->assertEquals(
'{"status":{"error":"Internal Server Error","code":"500 Internal Server Error","request":"api_path"}}',
- api_call($this->app)
+ api_call($this->app, $args)
);
}
}
];
$_SERVER['REQUEST_METHOD'] = 'method';
+ $_SERVER['QUERY_STRING'] = 'q=api_path.json';
+
+ $args = DI::args()->determine($_SERVER, $_GET);
- $this->app->query_string = 'api_path.json';
$this->assertEquals(
'["some_data"]',
- api_call($this->app)
+ api_call($this->app, $args)
);
}
}
];
$_SERVER['REQUEST_METHOD'] = 'method';
+ $_SERVER['QUERY_STRING'] = 'q=api_path.xml';
+
+ $args = DI::args()->determine($_SERVER, $_GET);
- $this->app->query_string = 'api_path.xml';
$this->assertEquals(
'some_data',
- api_call($this->app)
+ api_call($this->app, $args)
);
}
}
];
$_SERVER['REQUEST_METHOD'] = 'method';
+ $_SERVER['QUERY_STRING'] = 'q=api_path.rss';
+
+ $args = DI::args()->determine($_SERVER, $_GET);
- $this->app->query_string = 'api_path.rss';
$this->assertEquals(
'<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
'some_data',
- api_call($this->app)
+ api_call($this->app, $args)
);
}
}
];
$_SERVER['REQUEST_METHOD'] = 'method';
+ $_SERVER['QUERY_STRING'] = 'q=api_path.atom';
+
+ $args = DI::args()->determine($_SERVER, $_GET);
- $this->app->query_string = 'api_path.atom';
$this->assertEquals(
'<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
'some_data',
- api_call($this->app)
+ api_call($this->app, $args)
);
}
global $API;
$API['api_path'] = ['method' => 'method'];
- $this->app->query_string = 'api_path';
+ $_SERVER['QUERY_STRING'] = 'q=api_path';
+
+ $args = DI::args()->determine($_SERVER, $_GET);
+
$this->assertEquals(
'{"status":{"error":"Method Not Allowed","code":"405 Method Not Allowed","request":"api_path"}}',
- api_call($this->app)
+ api_call($this->app, $args)
);
}
'method' => 'method',
'auth' => true
];
- $_SERVER['REQUEST_METHOD'] = 'method';
$_SESSION['authenticated'] = false;
+ $_SERVER['REQUEST_METHOD'] = 'method';
+ $_SERVER['QUERY_STRING'] = 'q=api_path';
+
+ $args = DI::args()->determine($_SERVER, $_GET);
- $this->app->query_string = 'api_path';
$this->assertEquals(
'{"status":{"error":"This API requires login","code":"401 Unauthorized","request":"api_path"}}',
- api_call($this->app)
+ api_call($this->app, $args)
);
}
{
$this->assertEquals(
'{"status":{"error":"error_message","code":"200 OK","request":""}}',
- api_error('json', new HTTPException\OKException('error_message'))
+ api_error('json', new HTTPException\OKException('error_message'), DI::args())
);
}
' <code>200 OK</code>' . "\n" .
' <request/>' . "\n" .
'</status>' . "\n",
- api_error('xml', new HTTPException\OKException('error_message'))
+ api_error('xml', new HTTPException\OKException('error_message'), DI::args())
);
}
' <code>200 OK</code>' . "\n" .
' <request/>' . "\n" .
'</status>' . "\n",
- api_error('rss', new HTTPException\OKException('error_message'))
+ api_error('rss', new HTTPException\OKException('error_message'), DI::args())
);
}
' <code>200 OK</code>' . "\n" .
' <request/>' . "\n" .
'</status>' . "\n",
- api_error('atom', new HTTPException\OKException('error_message'))
+ api_error('atom', new HTTPException\OKException('error_message'), DI::args())
);
}
use Dice\Dice;
use Friendica\App;
use Friendica\Core\Config\Configuration;
+use Friendica\Core\Config\JitConfiguration;
use Friendica\Core\Lock\SemaphoreLock;
use Friendica\DI;
$app->shouldReceive('getHostname')->andReturn('friendica.local');
$dice->shouldReceive('create')->with(App::class)->andReturn($app);
- $configMock = \Mockery::mock(Configuration::class);
+ $configMock = \Mockery::mock(JitConfiguration::class);
$configMock
->shouldReceive('get')
->with('system', 'temppath', NULL, false)
?>
</head>
- <body id="top" class="mod-<?php echo $a->module . " " . $is_singleuser_class . " " . $view_mode_class;?>">
+ <body id="top" class="mod-<?php echo DI::module()->getName() . " " . $is_singleuser_class . " " . $view_mode_class;?>">
<a href="#content" class="sr-only sr-only-focusable">Skip to main content</a>
<?php
if (!empty($page['nav']) && !$minimal) {