[SPIP][PLUGINS] v3.0-->v3.2
[lhc/web/www.git] / www / plugins / spip_piwik / piwik_pipelines.php
1 <?php
2 /**
3 * Plugin Piwik
4 *
5 * @package SPIP\Piwik\Pipelines
6 */
7
8 if (!defined('_ECRIRE_INC_VERSION')) {
9 return;
10 }
11
12 /**
13 * Insertion dans le pipeline insert_head (SPIP)
14 *
15 * Ajout du code de piwik dans le head si configuré comme tel
16 *
17 * @param string $flux
18 * Le contenu de la balise #INSERT_HEAD
19 * @return string $flux
20 * Le contenu de la balise #INSERT_HEAD modifié
21 *
22 */
23 function piwik_insert_head($flux) {
24 $options = array();
25
26 if (!function_exists('lire_config')) {
27 include_spip('inc/config');
28 }
29
30 if (lire_config('piwik/mode_insertion') == 'pipeline') {
31 $options['type'] = 'public';
32 $flux .= piwik_head_js($options);
33 }
34
35 return $flux;
36 }
37
38 /**
39 * Insertion dans le pipeline header_prive (SPIP)
40 *
41 * Insertion du code de Piwik dans l'espace privé si configuré comme tel
42 *
43 * @param string $flux
44 * Le contenu du head privé
45 * @return string $flux
46 * Le contenu du head privé modifié
47 */
48 function piwik_header_prive($flux) {
49 $options = array();
50
51 if (!function_exists('lire_config')) {
52 include_spip('inc/config');
53 }
54
55 if (lire_config('piwik/piwik_prive')) {
56 if (is_array(lire_config('piwik/restreindre_statut_prive'))) {
57 $options['statuts_restreints'] = lire_config('piwik/restreindre_statut_prive');
58 }
59 if (is_array(lire_config('piwik/restreindre_auteurs_prive'))) {
60 $options['auteurs_restreints'] = lire_config('piwik/restreindre_auteurs_prive');
61 }
62 $options['type'] = 'prive';
63 $flux .= piwik_head_js($options);
64 }
65 return $flux;
66 }
67
68 /**
69 * La fonction de génération du code du tracker javascript
70 *
71 * @param array $options [optional]
72 *
73 * @return
74 */
75 function piwik_head_js($options = array()) {
76 if (!function_exists('lire_config')) {
77 include_spip('inc/config');
78 }
79
80 $config = lire_config('piwik', array('id_piwik' => false, 'urlpiwik' => false));
81 $id_piwik = $config['idpiwik'];
82 $url_piwik = $config['urlpiwik'];
83 $conformite_cnil = '';
84 if ($config['conformite_cnil'] == 'on') {
85 $conformite_cnil = '
86 '.recuperer_fond('inc/js-cnil');
87 }
88 $afficher_js = true;
89
90 $ret = '';
91
92 if ($url_piwik && $id_piwik) {
93 if ((isset($options['statut_restreint']) and $options['statut_restreint'])
94 or (isset($options['auteurs_restreints']) and $options['auteurs_restreints'])) {
95 $statut = isset($GLOBALS['visiteur_session']['statut']) ? $GLOBALS['visiteur_session']['statut'] : '';
96 $id_auteur = isset($GLOBALS['visiteur_session']['id_auteur']) ?
97 $GLOBALS['visiteur_session']['id_auteur'] : '';
98 if (in_array($statut, $options['statuts_restreints'])) {
99 $afficher_js = false;
100 }
101 if ($afficher_js && in_array($id_auteur, $options['auteurs_restreints'])) {
102 $afficher_js = false;
103 }
104 }
105
106 if ($afficher_js) {
107 $ret .= "
108 <script type='text/javascript'>var _paq = _paq || [];$conformite_cnil
109 (function(){ var u=(('https:' == document.location.protocol) ? 'https://$url_piwik/' : 'http://$url_piwik/');
110 _paq.push(['setSiteId', $id_piwik]);
111 _paq.push(['setTrackerUrl', u+'piwik.php']);
112 _paq.push(['trackPageView']);
113 _paq.push(['enableLinkTracking']);
114 var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.type='text/javascript'; g.defer=true; g.async=true; g.src=u+'piwik.js';
115 s.parentNode.insertBefore(g,s); })();
116 </script>";
117 }
118 }
119
120 return $ret;
121 }
122
123 /**
124 * Insertion dans le pipeline formulaire_verifier (SPIP)
125 * On vérifie les valeurs des champs fournis
126 *
127 * @param array $flux
128 * Le contexte du pipeline
129 * @return array $flux
130 * Le contexte du pipeline modifié auquel on a ajouté nos erreurs potentielles
131 */
132 function piwik_formulaire_verifier($flux) {
133 if ($flux['args']['form'] == 'configurer_piwik') {
134 $obligatoires = array('token','user','urlpiwik');
135 foreach ($obligatoires as $obligatoire) {
136 if (!_request($obligatoire)) {
137 $flux['data'][$obligatoire] = _T('info_obligatoire');
138 }
139 }
140
141 $piwik_token = _request('token');
142 if (!$flux['data']['token'] && !preg_match('/^[a-f0-9]{32}$/i', $piwik_token)) {
143 $flux['data']['token'] = _T('piwik:cfg_erreur_token');
144 return $flux;
145 }
146 $piwik_url = 'http://'._request('urlpiwik').'/';
147
148 $piwik_recuperer_data = charger_fonction('piwik_recuperer_data', 'inc');
149
150 /**
151 * Vérifier la correspondance nom d'utilisateur/ token
152 * Qui nous permettra par la suite de définir d'autres choses
153 */
154 $method_verif_user = 'UsersManager.getUser';
155 $options_user = array('userLogin'=>_request('user'));
156 $datas_user = $piwik_recuperer_data($piwik_url, $piwik_token, '', $method_verif_user, 'PHP', $options_user);
157 if (is_array($datas_user = unserialize($datas_user))) {
158 if (!$flux['data']['user'] and $flux['data']['result'] == 'error') {
159 $flux['data']['user'] = _T('piwik:cfg_erreur_user_token');
160 }
161 /**
162 * Vérifier que ce token est un token admin
163 * Si non : mettre une meta comme quoi il n'est pas admin pour créer des sites
164 */
165 $method_verif_user_bis = 'UsersManager.getUsers';
166 $datas_user_bis = $piwik_recuperer_data($piwik_url, $piwik_token, '', $method_verif_user_bis, 'PHP');
167 $datas_user_bis = unserialize($datas_user_bis);
168 if (is_array($datas_user_bis) and ($datas_user_bis['result'] == 'error')) {
169 ecrire_meta('piwik_admin', 'non');
170 } else {
171 ecrire_meta('piwik_admin', 'oui');
172 unset($flux['data']['user']);
173 }
174 }
175
176 /**
177 * Récupération de la liste des sites où cet utilisateur
178 * a les droits d'admin
179 */
180 $method = 'SitesManager.getSitesWithAdminAccess';
181 $datas = $piwik_recuperer_data($piwik_url, $piwik_token, '', $method, 'PHP');
182 if (!$flux['data']['urlpiwik'] and !is_array(unserialize($datas))) {
183 $flux['data']['urlpiwik'] = _T('piwik:cfg_erreur_recuperation_data');
184 } else {
185 ecrire_meta('piwik_sites_dispo', $datas);
186 }
187 }
188 return $flux;
189 }