Alternc  3.2
Alternc logiel libre pour l'hébergement
 All Data Structures Namespaces Files Functions Variables Pages
m_piwik.php
Go to the documentation of this file.
1 <?php
2 /*
3  ----------------------------------------------------------------------
4  AlternC - Web Hosting System
5  Copyright (C) 2000-2012 by the AlternC Development Team.
6  https://alternc.org/
7  ----------------------------------------------------------------------
8  LICENSE
9 
10  This program is free software; you can redistribute it and/or
11  modify it under the terms of the GNU General Public License (GPL)
12  as published by the Free Software Foundation; either version 2
13  of the License, or (at your option) any later version.
14 
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU General Public License for more details.
19 
20  To read the license please visit http://www.gnu.org/copyleft/gpl.html
21  ----------------------------------------------------------------------
22  Purpose of file: Manage piwik Statistics set
23  ----------------------------------------------------------------------
24 */
25 
26 /**
27  * This class manage piwik statistics management through AlternC, using piwik's "API".
28  */
29 class m_piwik {
34 
35  function hook_menu() {
36  global $quota;
37  if ( empty($this->piwik_server_uri) || empty($this->piwik_admin_token)) return false;
38 
39  $obj = array(
40  'title' => _("Piwik statistics"),
41  'ico' => 'images/piwik.png',
42  'link' => 'toggle',
43  'pos' => 115,
44  'links' => array(
45  array( 'txt' => _("Piwik Users"), 'url' => 'piwik_userlist.php'),
46  array( 'txt' => _("Piwik Sites"), 'url' => 'piwik_sitelist.php'),
47  ),
48  ) ;
49 
50  return $obj;
51  }
52 
53  /*---------------------------------------------------------------------------*/
54  /** Constructor
55  */
56  function m_piwik() {
57  $this->piwik_server_uri=variable_get('piwik_server_uri',null,'Remote Piwik server uri');
58  $this->piwik_admin_token=variable_get('piwik_admin_token',null,'Remote Piwik super-admin token');
59  $this->alternc_users = $this->get_alternc_users();
60  $this->alternc_sites = $this->get_alternc_sites();
61  }
62 
63  /* ----------------------------------------------------------------- */
64  /** hook called when an AlternC account is deleted
65  */
66  function hook_admin_del_member() {
67  //FIXME : implement the hook_admin_del_member for piwik
68  return true;
69  }
70 
71 
72  /* ----------------------------------------------------------------- */
73  /** Returns the used quota for the $name service for the current user.
74  * @param $name string name of the quota
75  * @return integer the number of service used or false if an error occured
76  * @access private
77  */
78  function hook_quota_get() {
79  global $db, $cuid;
80  $db->query("SELECT COUNT(id) AS nb FROM piwik_users WHERE uid='$cuid'");
81  $q=Array("name"=>"piwik", "description"=>_("Statistics through Piwik accounts"), "used"=>0);
82  if ($db->next_record()) {
83  $q['used']=$db->f('nb');
84  }
85  return $q;
86  }
87 
88 
89  function url() {
91  }
92 
93 
94  /***********************/
95  /* User-related tasks */
96  /***********************/
97 
98 
99  function user_add($user_login, $user_mail = null) {
100 
101  global $db, $mem, $cuid, $err;
102 
103  $user_login = $this->clean_user_name($user_login);
104  $user_pass = create_pass();
105  $user_mail = $user_mail ? $user_mail : $mem->user['mail'];
106  $user_mail = create_pass(4) . '@gmail.com'; // FIXME $user_mail; Unicité sur les emails ... Soit on ajoute + random soit, on prompt
107  $user_alias = $user_login;
108 
109  $api_data = $this->call_privileged_page('API', 'UsersManager.addUser', array('userLogin' => $user_login, 'password' => $user_pass, 'email' => $user_mail, 'alias' => $user_alias), 'JSON');
110  if ($api_data) {
111  if ($api_data->result === 'success') {
112  $user = $this->get_user($user_login);
113  $user_creation_date = $user->date_registered;
114  return $db->query("INSERT INTO piwik_users (uid, login, created_date) VALUES ('$cuid', '$user_login', '$user_creation_date')");
115  }
116  } else { // api_data = false -> error is already filled
117  return FALSE;
118  }
119  }
120 
121 
122  // Edite un user
123  function user_edit() {
124  //FIXME
125  return true;
126  }
127 
128  function get_site_access($user_login) {
129  return $this->call_privileged_page('API', 'UsersManager.getSitesAccessFromUser', array('userLogin' => $user_login));
130  }
131 
132  function get_users_access_from_site($site_id) {
133  global $err, $cuid;
134 
135  if (!is_numeric($site_id)) {
136  $err->raise('piwik', 'site_id must be numeric');
137  return FALSE;
138  }
139  if (!in_array($site_id, $this->alternc_sites)) {
140  $err->raise('piwik', "you don't own this piwik website");
141  return FALSE;
142  }
143 
144  $api_data = $this->call_privileged_page('API', 'UsersManager.getUsersAccessFromSite', array('idSite' => $site_id));
145  if ($api_data !== FALSE) {
146  $api_data = $api_data[0]; // Data is in the first column
147  foreach ($this->alternc_users AS $key=>$user) {
148  if (!array_key_exists($user, $api_data)) {
149  $api_data->$user = 'noaccess';
150  }
151  }
152  return $api_data;
153  }
154  else return FALSE;
155  }
156 
157  function get_user($user_login) {
158  $api_data = $this->call_privileged_page('API', 'UsersManager.getUser', array('userLogin' => $user_login));
159 
160  if ($api_data)
161  return $api_data[0];
162  else
163  return FALSE;
164  }
165 
166  function get_alternc_users() {
167  global $db, $cuid;
168 
169  static $alternc_users = array();
170  $db->query("SELECT login FROM piwik_users WHERE uid='$cuid'");
171  while ($db->next_record())
172  array_push($alternc_users, $db->f('login'));
173 
174  return $alternc_users;
175  }
176  // Supprime l'utilisateur Piwik passé en parametre
177  // Ne le supprime pas localement tant que pas supprimé en remote
178  function user_delete($piwik_user_login) {
179  global $db, $cuid, $err;
180 
181  $db->query("SELECT created_date, COUNT(id) AS cnt FROM piwik_users WHERE uid='$cuid' AND login='$piwik_user_login'");
182  $db->next_record();
183 
184  if ($db->f('cnt') == 1) {
185  $api_data = $this->call_privileged_page('API', 'UsersManager.deleteUser', array('userLogin' => $piwik_user_login));
186  if ($api_data->result == 'success') {
187  return $db->query("DELETE FROM piwik_users WHERE uid='$cuid' AND login='$piwik_user_login'");
188  }
189  else {
190  return FALSE;
191  }
192  } else {
193  $err->raise("piwik", _("You are not allowed to delete the statistics of this website"));
194  return FALSE;
195  }
196  }
197 
198 
199  function users_list() {
200  global $db, $cuid;
201  $db->query("SELECT login FROM piwik_users WHERE uid = '$cuid'");
202  if ($db->num_rows() == 0)
203  return array();
204  $users = '';
205  while ($db->next_record())
206  $users .= ($users !== '') ? ',' . $db->f('login') : $db->f('login');
207  return $this->call_privileged_page('API', 'UsersManager.getUsers', array('userLogins' => $users));
208  }
209 
210 
211  // Verifie que l'utilisateur existe bien dans piwik
212  function user_checkremote($puser_id) {
213  //FIXME
214  return true;
215  }
216 
217 
218  // Récupére un token pour le SSO avec piwik pour l'user
219  function user_remoteauth() {
220  //FIXME
221  return true;
222  }
223 
224  // Montre la liste des site pour lesques un user à accés
225  function user_access() {
226  // FIXME
227  return true;
228  }
229 
230 
231 
232 
233 
234  /***********************/
235  /* Site-related tasks */
236  /***********************/
237 
238 
239  function site_list() {
240  $api_data = $this->call_privileged_page('API', 'SitesManager.getAllSites');
241  $data = array();
242 
243  if($api_data) {
244  foreach ($api_data AS $site) {
245 
246  if (!in_array($site->idsite, $this->alternc_sites))
247  continue;
248 
249  $item = new stdClass();
250 
251  $item->id = $site->idsite;
252  $item->name = $site->name;
253  $item->main_url = $site->main_url;
254 
255  $user_data = $this->call_privileged_page('API', 'UsersManager.getUsersAccessFromSite', array('idSite' => $site->idsite));
256 
257  //if (is_array($user_data)) {
258  // printvar($user_data);
259  //} else if(is_object($user_data)) {
260  $item->rights = $user_data[0];
261  //}
262 
263  $data[] = $item;
264  }
265  return $data;
266  } else
267  return FALSE;
268  }
269 
270  function site_js_tag($site_id) {
271  return $this->call_privileged_page('API', 'SitesManager.getJavascriptTag', array('idSite' => $site_id, 'piwikUrl' => $this->piwik_server_uri))->value;
272  }
273 
274  function get_alternc_sites() {
275  global $db, $cuid;
276 
277  static $alternc_sites = array();
278  $db->query("SELECT piwik_id AS site_id FROM piwik_sites WHERE uid='$cuid'");
279  while ($db->next_record())
280  array_push($alternc_sites, $db->f('site_id'));
281 
282  return $alternc_sites;
283  }
284 
285  function get_site_list()
286  {
287  return $this->call_privileged_page('API', 'SitesManager.getAllSites');
288  }
289  // Ajoute un site à Piwik
290  // can't figure out how to pass multiple url through the API
291  function site_add($siteName, $urls, $ecommerce = FALSE) {
292  $urls = is_array($urls) ? implode(',', $urls) : $urls;
293  $api_data = $this->call_privileged_page('API', 'SitesManager.addSite', array('siteName' => $siteName, 'urls' => $urls));
294  printvar($api_data);
295  return TRUE;
296  }
297 
298 
299  //SitesManager.deleteSite (idSite)
300  // Supprime un site de Piwik
301  function site_delete($site_id) {
302  global $db, $cuid, $err;
303 
304  $db->query("SELECT COUNT(id) AS cnt FROM piwik_sites WHERE uid='$cuid' AND piwik_id='$site_id'");
305  $db->next_record();
306 
307  if ($db->f('cnt') == 1) {
308  $api_data = $this->call_privileged_page('API', 'SitesManager.deleteSite', array('idSite' => $site_id));
309 
310  if ($api_data->result == 'success') {
311  return $db->query("DELETE FROM piwik_sites where uid='$cuid' AND piwik_id='$site_id' LIMIT 1");
312  } else {
313  return FALSE;
314  }
315  } else {
316  $err->raise("piwik", _("You are not allowed to delete the statistics of this website"));
317  return FALSE;
318  }
319 
320  return true;
321  }
322 
323 
324  function site_set_user_right($site_id, $login, $right)
325  {
326  global $err;
327  if (!in_array($right, array('noaccess', 'view', 'admin')))
328  return FALSE;
329  $api_data = $this->call_privileged_page('API', 'UsersManager.setUserAccess', array('userLogin' => $login, 'access' => $right, 'idSites' => $site_id));
330  if ($api_data->result == 'success') {
331  return TRUE;
332  } else {
333  $err->raise('piwik', $api_data->messsage);
334  return FALSE;
335  }
336  }
337  // Ajoute un alias sur un site existant
338  function site_alias_add() {
339  // FIXME
340  return true;
341  }
342 
343 
344 
345  /* Helper code FIXME: rename those function using "private" + "_" prefix */
346 
348  return mysql_real_escape_string(trim($username));
349  }
350 
351 
352  function dev() {
353  // $this->call_page('module', 'method', array('user' => 'fser', 'pass' => 'toto'));
354  // return $this->users_list();
355  }
356 
357 
358  function call_page($module, $method, $arguments=array(), $output = 'JSON') {
359  global $err;
360  $url = sprintf('%s/?module=%s&method=%s&format=%s', $this->piwik_server_uri, $module, $method, $output);
361  foreach ($arguments AS $k=>$v)
362  $url .= sprintf('&%s=%s', urlencode($k), $v); // urlencode($v));
363 
364  $page_content = file_get_contents($url);
365  if ($page_content === FALSE) {
366  $err->raise("piwik", _("Unable to reach the API"));
367  return FALSE;
368  }
369  if ($output == 'JSON') {
370  $api_data = json_decode($page_content);
371  if ($api_data === FALSE) {
372  $err->raise("piwik", _("Error while decoding response from the API"));
373  return FALSE;
374  }
375 
376  return $api_data;
377  } else {
378  $err->raise("piwik", _("Other format than JSON is not implemented yet"));
379  return FALSE;
380  }
381  }
382 
383 
384  function call_privileged_page($module, $method, $arguments=array(), $output = 'JSON') {
385  $arguments['token_auth'] = $this->piwik_admin_token;
386  return $this->call_page($module, $method, $arguments, $output);
387  }
388 
389 
390 } /* Class piwik */