1<?php 2/* 3 * vim:set softtabstop=4 shiftwidth=4 expandtab: 4 * 5 * LICENSE: GNU Affero General Public License, version 3 (AGPL-3.0-or-later) 6 * Copyright 2001 - 2020 Ampache.org 7 * 8 * This program is free software: you can redistribute it and/or modify 9 * it under the terms of the GNU Affero General Public License as published by 10 * the Free Software Foundation, either version 3 of the License, or 11 * (at your option) any later version. 12 * 13 * This program is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU Affero General Public License for more details. 17 * 18 * You should have received a copy of the GNU Affero General Public License 19 * along with this program. If not, see <https://www.gnu.org/licenses/>. 20 * 21 */ 22declare(strict_types=0); 23 24namespace Ampache\Plugin; 25 26use Ampache\Config\AmpConfig; 27use Ampache\Repository\Model\Preference; 28use Ampache\Repository\Model\User; 29use Ampache\Module\System\Core; 30 31class AmpacheMatomo 32{ 33 public $name = 'Matomo'; 34 public $categories = 'stats'; 35 public $description = 'Matomo statistics'; 36 public $url = ''; 37 public $version = '000001'; 38 public $min_ampache = '370034'; 39 public $max_ampache = '999999'; 40 41 // These are internal settings used by this class, run this->load to fill them out 42 private $site_id; 43 private $matomo_url; 44 45 /** 46 * Constructor 47 * This function does nothing... 48 */ 49 public function __construct() 50 { 51 $this->description = T_('Matomo statistics'); 52 53 return true; 54 } 55 56 /** 57 * install 58 * This is a required plugin function. It inserts our preferences 59 * into Ampache 60 */ 61 public function install() 62 { 63 // Check and see if it's already installed 64 if (Preference::exists('matomo_site_id')) { 65 return false; 66 } 67 68 Preference::insert('matomo_site_id', T_('Matomo Site ID'), '1', 100, 'string', 'plugins', 'matomo'); 69 Preference::insert('matomo_url', T_('Matomo URL'), AmpConfig::get('web_path') . '/matomo/', 100, 'string', 70 'plugins', $this->name); 71 72 return true; 73 } 74 75 /** 76 * uninstall 77 * This is a required plugin function. It removes our preferences from 78 * the database returning it to its original form 79 */ 80 public function uninstall() 81 { 82 Preference::delete('matomo_site_id'); 83 Preference::delete('matomo_url'); 84 85 return true; 86 } 87 88 /** 89 * upgrade 90 * This is a recommended plugin function 91 */ 92 public function upgrade() 93 { 94 return true; 95 } 96 97 /** 98 * display_user_field 99 * This display the module in user page 100 */ 101 public function display_on_footer() 102 { 103 $currentUrl = scrub_out("http" . (filter_has_var(INPUT_SERVER, 104 'HTTPS') ? 's' : '') . '://' . Core::get_server('HTTP_HOST') . Core::get_server('REQUEST_URI')); 105 106 echo "<!-- Matomo -->\n"; 107 echo "<script>\n"; 108 echo "var _paq = _paq || [];\n"; 109 echo "_paq.push(['trackLink', '" . $currentUrl . "', 'link']);\n"; 110 echo "_paq.push(['enableLinkTracking']);\n"; 111 echo "(function() {\n"; 112 echo "var u='" . scrub_out($this->matomo_url) . "';\n"; 113 echo "_paq.push(['setTrackerUrl', u+'matomo.php']);\n"; 114 echo "_paq.push(['setSiteId', " . scrub_out($this->site_id) . "]);\n"; 115 if (Core::get_global('user')->id > 0) { 116 echo "_paq.push(['setUserId', '" . Core::get_global('user')->username . "']);\n"; 117 } 118 echo "var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];\n"; 119 echo "g.async=true; g.defer=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);\n"; 120 echo "})();\n"; 121 echo "</script>\n"; 122 echo "<noscript><p><img src='" . scrub_out($this->matomo_url) . "matomo.php?idsite=" . scrub_out($this->site_id) . "' style='border:0;' alt= '' /></p></noscript>\n"; 123 echo "<!-- End Matomo Code -->\n"; 124 } 125 126 /** 127 * load 128 * This loads up the data we need into this object, this stuff comes 129 * from the preferences. 130 * @param User $user 131 * @return boolean 132 */ 133 public function load($user) 134 { 135 $user->set_preferences(); 136 $data = $user->prefs; 137 // load system when nothing is given 138 if (!strlen(trim($data['matomo_site_id'])) || !strlen(trim($data['matomo_url']))) { 139 $data = array(); 140 $data['matomo_site_id'] = Preference::get_by_user(-1, 'matomo_site_id'); 141 $data['matomo_url'] = Preference::get_by_user(-1, 'matomo_url'); 142 } 143 144 $this->site_id = trim($data['matomo_site_id']); 145 if (!strlen($this->site_id)) { 146 debug_event('matomo.plugin', 'No Matomo Site ID, user field plugin skipped', 3); 147 148 return false; 149 } 150 151 $this->matomo_url = trim($data['matomo_url']); 152 if (!strlen($this->matomo_url)) { 153 debug_event('matomo.plugin', 'No Matomo URL, user field plugin skipped', 3); 154 155 return false; 156 } 157 158 return true; 159 } 160} 161