friendica-addons/fromapp/fromapp.php

83 lines
2.3 KiB
PHP
Raw Normal View History

2012-09-24 08:20:36 +00:00
<?php
/**
* Name: FromApp
* Description: Change the displayed application you are posting from
* Version: 1.0
* Author: Commander Zot
*
*/
use Friendica\App;
use Friendica\Core\Hook;
use Friendica\Core\Logger;
use Friendica\Core\Renderer;
2022-10-18 20:42:46 +00:00
use Friendica\Core\Session;
use Friendica\DI;
2012-09-24 08:20:36 +00:00
2018-01-22 19:03:11 +00:00
function fromapp_install()
{
Hook::register('post_local', 'addon/fromapp/fromapp.php', 'fromapp_post_hook');
Hook::register('addon_settings', 'addon/fromapp/fromapp.php', 'fromapp_settings');
Hook::register('addon_settings_post', 'addon/fromapp/fromapp.php', 'fromapp_settings_post');
Logger::notice("installed fromapp");
2012-09-24 08:20:36 +00:00
}
function fromapp_settings_post(App $a, $post)
2018-01-22 19:03:11 +00:00
{
2022-10-18 20:42:46 +00:00
if (!Session::getLocalUser() || empty($_POST['fromapp-submit'])) {
2012-09-24 08:20:36 +00:00
return;
2018-01-22 19:03:11 +00:00
}
2012-09-24 08:20:36 +00:00
2022-10-18 20:42:46 +00:00
DI::pConfig()->set(Session::getLocalUser(), 'fromapp', 'app', $_POST['fromapp-input']);
DI::pConfig()->set(Session::getLocalUser(), 'fromapp', 'force', intval($_POST['fromapp-force']));
2012-09-24 08:20:36 +00:00
}
function fromapp_settings(App &$a, array &$data)
2018-01-22 19:03:11 +00:00
{
2022-10-18 20:42:46 +00:00
if (!Session::getLocalUser()) {
2012-09-24 08:20:36 +00:00
return;
2018-01-22 19:03:11 +00:00
}
2012-09-24 08:20:36 +00:00
2022-10-18 20:42:46 +00:00
$fromapp = DI::pConfig()->get(Session::getLocalUser(), 'fromapp', 'app', '');
$force = intval(DI::pConfig()->get(Session::getLocalUser(), 'fromapp', 'force'));
$t = Renderer::getMarkupTemplate('settings.tpl', 'addon/fromapp/');
$html = Renderer::replaceMacros($t, [
'$fromapp' => ['fromapp-input', DI::l10n()->t('The application name you would like to show your posts originating from. Separate different app names with a comma. A random one will then be selected for every posting.'), $fromapp],
'$force' => ['fromapp-force', DI::l10n()->t('Use this application name even if another application was used.'), $force],
]);
$data = [
'addon' => 'fromapp',
'title' => DI::l10n()->t('FromApp Settings'),
'html' => $html,
];
2012-09-24 08:20:36 +00:00
}
function fromapp_post_hook(App $a, &$item)
{
2022-10-18 20:42:46 +00:00
if (!Session::getLocalUser()) {
return;
}
2012-09-24 08:20:36 +00:00
2022-10-18 20:42:46 +00:00
if (Session::getLocalUser() != $item['uid']) {
return;
}
2012-09-24 08:20:36 +00:00
2022-10-18 20:42:46 +00:00
$app = DI::pConfig()->get(Session::getLocalUser(), 'fromapp', 'app');
$force = intval(DI::pConfig()->get(Session::getLocalUser(), 'fromapp', 'force'));
2012-09-24 08:20:36 +00:00
if (is_null($app) || (! strlen($app))) {
return;
}
2012-09-24 08:20:36 +00:00
if (strlen(trim($item['app'])) && (! $force)) {
2012-09-25 00:21:30 +00:00
return;
}
2012-09-25 00:21:30 +00:00
$apps = explode(',', $app);
$item['app'] = trim($apps[mt_rand(0, count($apps)-1)]);
2012-09-24 08:20:36 +00:00
return;
2013-11-19 12:57:51 +00:00
}