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