]> git.mxchange.org Git - friendica-addons.git/blob - fromapp/fromapp.php
Changes:
[friendica-addons.git] / fromapp / fromapp.php
1 <?php
2 /**
3  * Name: FromApp
4  * Description: Change the displayed application you are posting from
5  * Version: 1.0
6  * Author: Commander Zot
7  *
8  */
9
10 use Friendica\App;
11 use Friendica\Core\Hook;
12 use Friendica\Core\Logger;
13 use Friendica\Core\Renderer;
14 use Friendica\DI;
15
16 function fromapp_install()
17 {
18         Hook::register('post_local', 'addon/fromapp/fromapp.php', 'fromapp_post_hook');
19         Hook::register('addon_settings', 'addon/fromapp/fromapp.php', 'fromapp_settings');
20         Hook::register('addon_settings_post', 'addon/fromapp/fromapp.php', 'fromapp_settings_post');
21         Logger::notice("installed fromapp");
22 }
23
24 function fromapp_settings_post($post)
25 {
26         if (!DI::userSession()->getLocalUserId() || empty($_POST['fromapp-submit'])) {
27                 return;
28         }
29
30         DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'fromapp', 'app', $_POST['fromapp-input']);
31         DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'fromapp', 'force', intval($_POST['fromapp-force']));
32 }
33
34 function fromapp_settings(array &$data)
35 {
36         if (!DI::userSession()->getLocalUserId()) {
37                 return;
38         }
39
40         $fromapp = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'fromapp', 'app', '');
41         $force   = intval(DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'fromapp', 'force'));
42
43         $t    = Renderer::getMarkupTemplate('settings.tpl', 'addon/fromapp/');
44         $html = Renderer::replaceMacros($t, [
45                 '$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],
46                 '$force'   => ['fromapp-force', DI::l10n()->t('Use this application name even if another application was used.'), $force],
47         ]);
48
49         $data = [
50                 'addon' => 'fromapp',
51                 'title' => DI::l10n()->t('FromApp Settings'),
52                 'html'  => $html,
53         ];
54 }
55
56 function fromapp_post_hook(array &$item)
57 {
58         if (!DI::userSession()->getLocalUserId()) {
59                 return;
60         }
61
62         if (DI::userSession()->getLocalUserId() != $item['uid']) {
63                 return;
64         }
65
66         $app = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'fromapp', 'app');
67         $force = intval(DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'fromapp', 'force'));
68
69         if (is_null($app) || (! strlen($app))) {
70                 return;
71         }
72
73         if (strlen(trim($item['app'])) && (! $force)) {
74                 return;
75         }
76
77         $apps = explode(',', $app);
78         $item['app'] = trim($apps[mt_rand(0, count($apps)-1)]);
79
80         return;
81 }