c85068eaaf0802ce9712ff13ff23d6e3438ed527
[friendica-addons.git/.git] / mathjax / mathjax.php
1 <?php
2 /**
3  * Name: MathJax
4  * Description: Addon for Friendika to include MathJax (LaTeX math syntax)
5  * Version: 1.1
6  * Author: Tobias Diekershoff <https://social.diekershoff.de/profile/tobias>
7  * License: 3-clause BSD license
8  */
9 use Friendica\App;
10 use Friendica\Core\Addon;
11 use Friendica\Core\Config;
12 use Friendica\Core\L10n;
13 use Friendica\Core\PConfig;
14
15 function mathjax_install() {
16         Addon::registerHook('load_config', 'addon/mathjax/mathjax.php', 'mathjax_load_config');
17     Addon::registerHook('page_header', 'addon/mathjax/mathjax.php', 'mathjax_page_header');
18     Addon::registerHook('addon_settings', 'addon/mathjax/mathjax.php', 'mathjax_settings');
19     Addon::registerHook('addon_settings_post', 'addon/mathjax/mathjax.php', 'mathjax_settings_post');
20     Addon::registerHook('template_vars', 'addon/mathjax/mathjax.php', 'mathjax_template_vars');
21     logger('installed js_math addon');
22 }
23
24 function mathjax_uninstall() {
25         Addon::unregisterHook('load_config', 'addon/mathjax/mathjax.php', 'mathjax_load_config');
26     Addon::unregisterHook('page_header', 'addon/mathjax/mathjax.php', 'mathjax_page_header');
27     Addon::unregisterHook('addon_settings', 'addon/mathjax/mathjax.php', 'mathjax_settings');
28     Addon::unregisterHook('addon_settings_post', 'addon/mathjax/mathjax.php', 'mathjax_settings_post');
29     Addon::unregisterHook('template_vars', 'addon/mathjax/mathjax.php', 'mathjax_template_vars');
30 }
31
32 function mathjax_load_config(\Friendica\App $a)
33 {
34         $a->loadConfigFile(__DIR__. '/config/mathjax.ini.php');
35 }
36
37 function mathjax_template_vars($a, &$arr)
38 {
39     if (!array_key_exists('addon_hooks',$arr['vars']))
40     {
41         $arr['vars']['addon_hooks'] = array();
42     }
43     $arr['vars']['addon_hooks'][] = "mathjax";
44 }
45
46 function mathjax_settings_post ($a, $post) {
47     if (! local_user())
48         return;
49     if (!x($_POST,'mathjax-submit'))
50         return;
51     PConfig::set(local_user(),'mathjax','use',intval($_POST['mathjax_use']));
52 }
53 function mathjax_settings (&$a, &$s) {
54     if (! local_user())
55         return;
56     $use = PConfig::get(local_user(),'mathjax','use');
57     $usetext = (($use) ? ' checked="checked" ' : '');
58     $s .= '<span id="settings_mathjax_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_mathjax_expanded\'); openClose(\'settings_mathjax_inflated\');">';
59     $s .= '<h3>MathJax '.L10n::t('Settings').'</h3>';
60     $s .= '</span>';
61     $s .= '<div id="settings_mathjax_expanded" class="settings-block" style="display: none;">';
62     $s .= '<span class="fakelink" onclick="openClose(\'settings_mathjax_expanded\'); openClose(\'settings_mathjax_inflated\');">';
63     $s .= '<h3>MathJax '.L10n::t('Settings').'</h3>';
64     $s .= '</span>';
65     $s .= '<p>'.L10n::t('The MathJax addon renders mathematical formulae written using the LaTeX syntax surrounded by the usual $$ or an eqnarray block in the postings of your wall,network tab and private mail.').'</p>';
66     $s .= '<label id="mathjax_label" for="mathjax_use">'.L10n::t('Use the MathJax renderer').'</label>';
67     $s .= '<input id="mathjax_use" type="checkbox" name="mathjax_use" value="1"'. $usetext .' />';
68     $s .= '<div class="clear"></div>';
69
70     $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="mathjax-submit" name="mathjax-submit" class="settings-submit" value="' . L10n::t('Save Settings') . '" /></div>';
71     $s .= '</div>';
72 }
73 /*  we need to add one JavaScript include command to the html output
74  *  note that you have to check the jsmath/easy/load.js too.
75  */
76 function mathjax_page_header($a, &$b) {
77     //  if the visitor of the page is not a local_user, use MathJax
78     //  otherwise check the users settings.
79     $url = Config::get ('mathjax','baseurl');
80                 if(! $url) {
81                         $url = 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.4/MathJax.js?config=TeX-MML-AM_CHTML';
82                 }
83     if (! local_user()) {
84         $b .= '<script type="text/javascript" src="'.$url.'" async></script>';
85     } else {
86         $use = PConfig::get(local_user(),'mathjax','use');
87         if ($use) {
88             $b .= '<script type="text/javascript" src="'.$url.'" async></script>';
89         }
90     }
91 }
92 function mathjax_addon_admin_post (&$a) {
93     $baseurl = ((x($_POST, 'mjbaseurl')) ? trim($_POST['mjbaseurl']) : 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.4/MathJax.js?config=TeX-MML-AM_CHTML');
94     Config::set('mathjax','baseurl',$baseurl);
95     info(L10n::t('Settings updated.'). EOL);
96 }
97 function mathjax_addon_admin (App $a, &$o) {
98         $t = get_markup_template( "admin.tpl", "addon/mathjax/" );
99
100         if (Config::get('mathjax','baseurl','') == '') {
101                 Config::set('mathjax','baseurl','https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.4/MathJax.js?config=TeX-MML-AM_CHTML');
102         }
103
104         $o = replace_macros( $t, [
105                 '$submit' => L10n::t('Save Settings'),
106                 '$mjbaseurl' => ['mjbaseurl', L10n::t('MathJax Base URL'), Config::get('mathjax','baseurl' ), L10n::t('The URL for the javascript file that should be included to use MathJax. Can be either the MathJax CDN or another installation of MathJax.'), 'required']
107         ]);
108 }