Alternc  latest
Alternc logiel libre pour l'hébergement
 All Classes Namespaces Files Functions Variables Pages
m_piwik Class Reference

This class manage piwik statistics management through AlternC, using piwik's "API". More...

Public Member Functions

 hook_menu ()
 
 m_piwik ()
 Constructor. More...
 
 hook_admin_del_member ()
 hook called when an AlternC account is deleted More...
 
 hook_quota_get ()
 Returns the used quota for the $name service for the current user. More...
 
 url ()
 
 user_add ($user_login, $user_mail)
 
 user_edit ()
 Change a user : code this. More...
 
 get_site_access ($user_login)
 
 get_users_access_from_site ($site_id)
 
 get_user ($user_login)
 
 get_alternc_users ()
 
 get_users_url_infos ()
 
 user_has_sites ()
 does this user has piwik websites configured in AlternC ? More...
 
 user_delete ($piwik_user_login)
 Delete a piwik user don't delete it locally unless it has been remotely deleted. More...
 
 users_list ()
 
 user_checkremote ($puser_id)
 
 user_remoteauth ()
 
 user_access ()
 
 site_list ()
 
 site_js_tag ($site_id)
 
 get_alternc_sites ()
 
 get_site_list ()
 
 site_add ($siteName, $urls, $ecommerce=FALSE)
 
 site_delete ($site_id)
 
 site_set_user_right ($site_id, $login, $right)
 
 site_alias_add ()
 
 clean_user_name ($username)
 return a clean username with a unique prefix per account More...
 
 dev ()
 
 call_page ($module, $method, $arguments=array(), $output= 'JSON')
 
 call_privileged_page ($module, $method, $arguments=array(), $output= 'JSON')
 

Public Attributes

 $piwik_server_uri
 
 $piwik_admin_token
 
 $alternc_users
 
 $alternc_sites
 

Detailed Description

This class manage piwik statistics management through AlternC, using piwik's "API".

Definition at line 26 of file m_piwik.php.

Member Function Documentation

m_piwik::call_page (   $module,
  $method,
  $arguments = array(),
  $output = 'JSON' 
)
Parameters
string$module
string$method

Definition at line 475 of file m_piwik.php.

References $msg.

Referenced by call_privileged_page().

475  {
476  global $msg;
477 
478  $msg->log("piwik","call_page");
479 
480  $url = sprintf('%s/?module=%s&method=%s&format=%s', $this->piwik_server_uri, $module, $method, $output);
481  foreach ($arguments AS $k=>$v)
482  $url .= sprintf('&%s=%s', urlencode($k), $v); // urlencode($v));
483 
484  $page_content = file_get_contents($url);
485  if ($page_content === FALSE) {
486  $msg->raise("ERROR", "piwik", _("Unable to reach the API"));
487  return FALSE;
488  }
489 
490  if ($output == 'JSON') {
491  $api_data = json_decode($page_content);
492  if ($api_data === FALSE) {
493  $msg->raise("ERROR", "piwik", _("Error while decoding response from the API"));
494  return FALSE;
495  }
496 
497  return $api_data;
498  } else {
499  $msg->raise("ERROR", "piwik", _("Other format than JSON is not implemented yet"));
500  return FALSE;
501  }
502  }
$msg
Definition: config.php:155
m_piwik::call_privileged_page (   $module,
  $method,
  $arguments = array(),
  $output = 'JSON' 
)
Parameters
string$module
string$method

Definition at line 509 of file m_piwik.php.

References $msg, $piwik_admin_token, and call_page().

Referenced by get_site_access(), get_site_list(), get_user(), get_users_access_from_site(), site_add(), site_delete(), site_js_tag(), site_list(), site_set_user_right(), user_add(), user_delete(), and users_list().

509  {
510  global $msg;
511 
512  $msg->log("piwik","call_privileged_page");
513 
514  $arguments['token_auth'] = $this->piwik_admin_token;
515  return $this->call_page($module, $method, $arguments, $output);
516  }
$msg
Definition: config.php:155
$piwik_admin_token
Definition: m_piwik.php:28
call_page($module, $method, $arguments=array(), $output= 'JSON')
Definition: m_piwik.php:475
m_piwik::clean_user_name (   $username)

return a clean username with a unique prefix per account

Definition at line 457 of file m_piwik.php.

References $cuid, $db, and $username.

Referenced by user_add().

457  {
458  global $admin, $cuid, $db;
459  $escaped_name=$db->quote(trim($username));
460  $escaped_name=preg_replace("/^'(.*)'/", "\\1", $escaped_name);
461  return 'alternc_' . $admin->get_login_by_uid($cuid) . '_' . $escaped_name;
462  }
global $db
Definition: bootstrap.php:22
$cuid
Definition: bootstrap.php:43
global $username
Definition: change.php:59
m_piwik::dev ( )

Definition at line 465 of file m_piwik.php.

465  {
466  // $this->call_page('module', 'method', array('user' => 'fser', 'pass' => 'toto'));
467  // return $this->users_list();
468  }
m_piwik::get_alternc_sites ( )

Definition at line 354 of file m_piwik.php.

References $alternc_sites, $cuid, $db, and $msg.

Referenced by m_piwik(), and site_add().

354  {
355  global $db, $cuid, $msg;
356 
357  $msg->log("piwik","get_alternc_sites");
358 
359  static $alternc_sites = array();
360  $db->query("SELECT piwik_id AS site_id FROM piwik_sites WHERE uid= ? ;", array($cuid));
361  while ($db->next_record())
362  array_push($alternc_sites, $db->f('site_id'));
363 
364  return $alternc_sites;
365  }
global $db
Definition: bootstrap.php:22
$alternc_sites
Definition: m_piwik.php:30
$msg
Definition: config.php:155
$cuid
Definition: bootstrap.php:43
m_piwik::get_alternc_users ( )

Definition at line 186 of file m_piwik.php.

References $alternc_users, $cuid, $db, and $msg.

Referenced by m_piwik().

186  {
187  global $db, $cuid, $msg;
188 
189  $msg->log("piwik","get_alternc_users");
190 
191  static $alternc_users = array();
192  $db->query("SELECT login FROM piwik_users WHERE uid= ?;", array($cuid));
193  while ($db->next_record())
194  array_push($alternc_users, $db->f('login'));
195 
196  return $alternc_users;
197  }
$alternc_users
Definition: m_piwik.php:29
global $db
Definition: bootstrap.php:22
$msg
Definition: config.php:155
$cuid
Definition: bootstrap.php:43
m_piwik::get_site_access (   $user_login)

Definition at line 141 of file m_piwik.php.

References call_privileged_page().

141  {
142  return $this->call_privileged_page('API', 'UsersManager.getSitesAccessFromUser', array('userLogin' => $user_login));
143  }
call_privileged_page($module, $method, $arguments=array(), $output= 'JSON')
Definition: m_piwik.php:509
m_piwik::get_site_list ( )

Definition at line 368 of file m_piwik.php.

References call_privileged_page().

369  {
370  return $this->call_privileged_page('API', 'SitesManager.getAllSites');
371  }
call_privileged_page($module, $method, $arguments=array(), $output= 'JSON')
Definition: m_piwik.php:509
m_piwik::get_user (   $user_login)
Parameters
string$user_login

Definition at line 176 of file m_piwik.php.

References call_privileged_page().

Referenced by user_add().

176  {
177  $api_data = $this->call_privileged_page('API', 'UsersManager.getUser', array('userLogin' => $user_login));
178 
179  if ($api_data)
180  return $api_data[0];
181  else
182  return FALSE;
183  }
call_privileged_page($module, $method, $arguments=array(), $output= 'JSON')
Definition: m_piwik.php:509
m_piwik::get_users_access_from_site (   $site_id)

Definition at line 145 of file m_piwik.php.

References $cuid, $key, $msg, $user, and call_privileged_page().

Referenced by get_users_url_infos().

145  {
146  global $msg, $cuid;
147 
148  $msg->log("piwik","get_users_access_from_site");
149 
150  if (!is_numeric($site_id)) {
151  $msg->raise("ERROR", 'piwik', 'site_id must be numeric');
152  return FALSE;
153  }
154  if (!in_array($site_id, $this->alternc_sites)) {
155  $msg->raise("ERROR", 'piwik', "you don't own this piwik website");
156  return FALSE;
157  }
158 
159  $api_data = $this->call_privileged_page('API', 'UsersManager.getUsersAccessFromSite', array('idSite' => $site_id));
160  if ($api_data !== FALSE) {
161  $api_data = $api_data[0]; // Data is in the first column
162  foreach ($this->alternc_users AS $key=>$user) {
163  if (!array_key_exists($user, $api_data)) {
164  $api_data->$user = 'noaccess';
165  }
166  }
167  return $api_data;
168  }
169  else return FALSE;
170  }
$user
Definition: bootstrap.php:84
call_privileged_page($module, $method, $arguments=array(), $output= 'JSON')
Definition: m_piwik.php:509
$msg
Definition: config.php:155
$cuid
Definition: bootstrap.php:43
if(empty($_POST['key'])||empty($_POST['val'])) $key
Definition: tempovars.php:14
m_piwik::get_users_url_infos ( )

Definition at line 200 of file m_piwik.php.

References $cuid, $db, $id, $login, $msg, and get_users_access_from_site().

200  {
201  global $db,$cuid, $msg;
202  $infos_user = array();
203  $api_calls = array();
204 
205 
206  $db->query("SELECT login, passwd, s.piwik_id as id FROM piwik_users as u INNER JOIN piwik_sites as s on u.uid = s.uid WHERE u.uid = $cuid");
207  while ($db->next_record()) {
208  $id = $db->f('id');
209  $login = $db->f('login');
210 
211  if (!isset($infos_user[$id]))
212  $infos_user[$id] = array();
213 
214  if (!isset($api_calls[$id]))
215  $api_calls[$id] = $this->get_users_access_from_site($id);
216 
217  foreach ($api_calls[$id] as $l => $cred) {
218  if ($l == $login)
219  $infos_user[$id][] = array('login' => $login, 'password' => $db->f('passwd'), 'cred' => $cred);
220  }
221  }
222 
223  return $infos_user;
224  }
$login
global $db
Definition: bootstrap.php:22
get_users_access_from_site($site_id)
Definition: m_piwik.php:145
$msg
Definition: config.php:155
$cuid
Definition: bootstrap.php:43
if(!isset($is_include)) if(!$key &&!$crt) $id
m_piwik::hook_admin_del_member ( )

hook called when an AlternC account is deleted

Definition at line 65 of file m_piwik.php.

65  {
66  //FIXME : implement the hook_admin_del_member for piwik
67  return true;
68  }
m_piwik::hook_menu ( )

Definition at line 32 of file m_piwik.php.

32  {
33  global $quota;
34  if ( empty($this->piwik_server_uri) || empty($this->piwik_admin_token)) return false;
35 
36  $obj = array(
37  'title' => _("Piwik statistics"),
38  'ico' => 'images/piwik.png',
39  'link' => 'toggle',
40  'pos' => 115,
41  'links' => array(
42  array( 'txt' => _("Piwik Users"), 'url' => 'piwik_userlist.php'),
43  array( 'txt' => _("Piwik Sites"), 'url' => 'piwik_sitelist.php'),
44  ),
45  ) ;
46 
47  return $obj;
48  }
m_piwik::hook_quota_get ( )

Returns the used quota for the $name service for the current user.

Parameters
$namestring name of the quota
Returns
integer the number of service used or false if an error occured private

Definition at line 77 of file m_piwik.php.

References $cuid, $db, and $q.

77  {
78  global $db, $cuid;
79  $db->query("SELECT COUNT(id) AS nb FROM piwik_sites WHERE uid= ? ;", array($cuid));
80  $q=Array("name"=>"piwik", "description"=>_("Statistics through Piwik accounts"), "used"=>0);
81  if ($db->next_record()) {
82  $q['used']=$db->f('nb');
83  }
84  return $q;
85  }
global $db
Definition: bootstrap.php:22
$cuid
Definition: bootstrap.php:43
m_piwik::m_piwik ( )

Constructor.

Definition at line 54 of file m_piwik.php.

References get_alternc_sites(), get_alternc_users(), null, and variable_get().

54  {
55  $this->piwik_server_uri=variable_get('piwik_server_uri',null,'Remote Piwik server uri');
56  $this->piwik_admin_token=variable_get('piwik_admin_token',null,'Remote Piwik super-admin token');
57  $this->alternc_users = $this->get_alternc_users();
58  $this->alternc_sites = $this->get_alternc_sites();
59  }
get_alternc_sites()
Definition: m_piwik.php:354
get_alternc_users()
Definition: m_piwik.php:186
PR null
Definition: lang-css.js:1
variable_get($name, $default=null, $createit_comment=null)
Return a persistent variable.
Definition: variables.php:85
m_piwik::site_add (   $siteName,
  $urls,
  $ecommerce = FALSE 
)

Definition at line 374 of file m_piwik.php.

References $cuid, $db, $msg, $userslist, call_privileged_page(), and get_alternc_sites().

374  {
375  global $db, $cuid, $piwik, $msg;
376 
377  $msg->log("piwik","site_add");
378 
379  $urls = is_array($urls) ? implode(',', $urls) : $urls;
380  $api_data = $this->call_privileged_page('API', 'SitesManager.addSite', array('siteName' => $siteName, 'urls' => $urls));
381 
382  if ($api_data->value) {
383  $id_site = $api_data->value;
384 
385  // Ajout de donner auto les droits de lecture à ce nouvel utilisateur pour le site qu'il a ajouté
386  $userslist = $piwik->users_list();
387  $api_data = $this->call_privileged_page('API', 'UsersManager.setUserAccess', array('userLogin' => $userslist[0]->login, 'idSites' => $id_site, 'access' => 'view'));
388 
389  if ($api_data->result == 'success') {
390  // On enregistre le site dans alternC
391  $db->query("INSERT INTO piwik_sites set uid= ? , piwik_id= ? ", array($cuid, $id_site));
392 
393  // Permet de prendre en compte le site qu'on vient de créer dans la page quis'affiche
394  $this->alternc_sites = $this->get_alternc_sites();
395  return TRUE;
396  }
397  return TRUE;
398  } else
399  return FALSE;
400  }
$userslist
Add a piwik account using piwik's API.
call_privileged_page($module, $method, $arguments=array(), $output= 'JSON')
Definition: m_piwik.php:509
get_alternc_sites()
Definition: m_piwik.php:354
global $db
Definition: bootstrap.php:22
$msg
Definition: config.php:155
$cuid
Definition: bootstrap.php:43
m_piwik::site_alias_add ( )

Definition at line 448 of file m_piwik.php.

448  {
449  // FIXME
450  return true;
451  }
m_piwik::site_delete (   $site_id)

Definition at line 405 of file m_piwik.php.

References $cuid, $db, $msg, and call_privileged_page().

405  {
406  global $db, $cuid, $msg;
407 
408  $msg->log("piwik","site_delete");
409 
410  $db->query("SELECT COUNT(id) AS cnt FROM piwik_sites WHERE uid= ? AND piwik_id= ? ;", array($cuid, $site_id));
411  $db->next_record();
412 
413  if ($db->f('cnt') == 1) {
414  $api_data = $this->call_privileged_page('API', 'SitesManager.deleteSite', array('idSite' => $site_id));
415  if ($api_data->result == 'success') {
416  return $db->query("DELETE FROM piwik_sites where uid= ? AND piwik_id= ? LIMIT 1", array($cuid, $site_id));
417  } else {
418  return FALSE;
419  }
420  } else {
421  $msg->raise("ERROR", "piwik", _("You are not allowed to delete the statistics of this website"));
422  return FALSE;
423  }
424 
425  return true;
426  }
call_privileged_page($module, $method, $arguments=array(), $output= 'JSON')
Definition: m_piwik.php:509
global $db
Definition: bootstrap.php:22
$msg
Definition: config.php:155
$cuid
Definition: bootstrap.php:43
m_piwik::site_js_tag (   $site_id)

Definition at line 349 of file m_piwik.php.

References call_privileged_page().

349  {
350  return $this->call_privileged_page('API', 'SitesManager.getJavascriptTag', array('idSite' => $site_id, 'piwikUrl' => $this->piwik_server_uri))->value;
351  }
call_privileged_page($module, $method, $arguments=array(), $output= 'JSON')
Definition: m_piwik.php:509
m_piwik::site_list ( )

Definition at line 313 of file m_piwik.php.

References $msg, and call_privileged_page().

313  {
314  global $msg;
315 
316  $msg->log("piwik","site_list");
317 
318  $api_data = $this->call_privileged_page('API', 'SitesManager.getAllSites');
319  $data = array();
320 
321  if($api_data) {
322  foreach ($api_data AS $site) {
323 
324  if (!in_array($site->idsite, $this->alternc_sites))
325  continue;
326 
327  $item = new stdClass();
328 
329  $item->id = $site->idsite;
330  $item->name = $site->name;
331  $item->main_url = $site->main_url;
332 
333  $user_data = $this->call_privileged_page('API', 'UsersManager.getUsersAccessFromSite', array('idSite' => $site->idsite));
334 
335  //if (is_array($user_data)) {
336  // printvar($user_data);
337  //} else if(is_object($user_data)) {
338  $item->rights = $user_data[0];
339  //}
340 
341  $data[] = $item;
342  }
343  return $data;
344  } else
345  return FALSE;
346  }
call_privileged_page($module, $method, $arguments=array(), $output= 'JSON')
Definition: m_piwik.php:509
$msg
Definition: config.php:155
m_piwik::site_set_user_right (   $site_id,
  $login,
  $right 
)

Definition at line 429 of file m_piwik.php.

References $login, $msg, and call_privileged_page().

430  {
431  global $msg;
432 
433  $msg->log("piwik","site_set_user_right");
434 
435  if (!in_array($right, array('noaccess', 'view', 'admin')))
436  return FALSE;
437  $api_data = $this->call_privileged_page('API', 'UsersManager.setUserAccess', array('userLogin' => $login, 'access' => $right, 'idSites' => $site_id));
438  if ($api_data->result == 'success') {
439  return TRUE;
440  } else {
441  $msg->raise("ERROR", 'piwik', $api_data->messsage);
442  return FALSE;
443  }
444  }
call_privileged_page($module, $method, $arguments=array(), $output= 'JSON')
Definition: m_piwik.php:509
$login
$msg
Definition: config.php:155
m_piwik::url ( )

Definition at line 88 of file m_piwik.php.

References $piwik_server_uri.

88  {
90  }
$piwik_server_uri
Definition: m_piwik.php:27
m_piwik::user_access ( )

Definition at line 302 of file m_piwik.php.

302  {
303  // FIXME
304  return true;
305  }
m_piwik::user_add (   $user_login,
  $user_mail 
)

Definition at line 97 of file m_piwik.php.

References $cuid, $db, $mem, $msg, $user, call_privileged_page(), clean_user_name(), create_pass(), and get_user().

97  {
98  global $db, $mem, $cuid, $msg;
99 
100  $msg->log("piwik","user_add");
101 
102  if (empty($user_login) || is_null($user_login) || empty($user_mail) || is_null($user_mail)) {
103  $msg->raise("ERROR", "piwik", _("All fields are mandatory"));
104  return false;
105  }
106 
107  // Validate the email syntax:
108  if (!filter_var($user_mail, FILTER_VALIDATE_EMAIL)) {
109  $msg->raise("ERROR", "piwik", _("The email you entered is syntaxically incorrect"));
110  return false;
111  }
112 
113  $user_login = $this->clean_user_name($user_login);
114  $user_pass = create_pass();
115  $user_alias = $user_login;
116 
117  $api_data = $this->call_privileged_page('API', 'UsersManager.addUser', array('userLogin' => $user_login, 'password' => $user_pass, 'email' => $user_mail, 'alias' => $user_alias), 'JSON');
118  if ($api_data) {
119  if ($api_data->result === 'success') {
120  $user = $this->get_user($user_login);
121  $user_creation_date = $user->date_registered;
122  $ret_value = $db->query("INSERT INTO piwik_users (uid, passwd, login, created_date) VALUES ( ?, ?, ?, ?);", array($cuid, md5('$user_pass'), $user_login, $user_creation_date));
123  return $ret_value;
124  } else {
125  $msg->raise("ERROR", "piwik", $api_data->message);
126  return FALSE;
127  }
128  } else { // api_data = false -> error is already filled
129  return FALSE;
130  }
131  }
$user
Definition: bootstrap.php:84
call_privileged_page($module, $method, $arguments=array(), $output= 'JSON')
Definition: m_piwik.php:509
create_pass($length=10, $classcount=3)
Create a password compatible with the password policy.
Definition: functions.php:877
global $db
Definition: bootstrap.php:22
get_user($user_login)
Definition: m_piwik.php:176
$msg
Definition: config.php:155
$cuid
Definition: bootstrap.php:43
clean_user_name($username)
return a clean username with a unique prefix per account
Definition: m_piwik.php:457
$mem
Definition: bootstrap.php:71
m_piwik::user_checkremote (   $puser_id)

Definition at line 289 of file m_piwik.php.

289  {
290  //FIXME
291  return true;
292  }
m_piwik::user_delete (   $piwik_user_login)

Delete a piwik user don't delete it locally unless it has been remotely deleted.

Definition at line 250 of file m_piwik.php.

References $cuid, $db, $msg, and call_privileged_page().

250  {
251  global $db, $cuid, $msg;
252 
253  $msg->log("piwik","user_delete");
254 
255  $db->query("SELECT created_date, COUNT(id) AS cnt FROM piwik_users WHERE uid= ? AND login= ? ", array($cuid, $piwik_user_login));
256  $db->next_record();
257 
258  if ($db->f('cnt') == 1) {
259  $api_data = $this->call_privileged_page('API', 'UsersManager.deleteUser', array('userLogin' => $piwik_user_login));
260  if ($api_data->result == 'success') {
261  return $db->query("DELETE FROM piwik_users WHERE uid= ? AND login= ? ;", array($cuid, $piwik_user_login));
262  }
263  else {
264  return FALSE;
265  }
266  } else {
267  $msg->raise("ERROR", "piwik", _("You are not allowed to delete the statistics of this website"));
268  return FALSE;
269  }
270  }
call_privileged_page($module, $method, $arguments=array(), $output= 'JSON')
Definition: m_piwik.php:509
global $db
Definition: bootstrap.php:22
$msg
Definition: config.php:155
$cuid
Definition: bootstrap.php:43
m_piwik::user_edit ( )

Change a user : code this.

Definition at line 137 of file m_piwik.php.

137  {
138  return true;
139  }
m_piwik::user_has_sites ( )

does this user has piwik websites configured in AlternC ?

Definition at line 230 of file m_piwik.php.

References $cuid, $db, and $msg.

230  {
231  global $db, $cuid, $msg;
232 
233  $msg->log("piwik","user_has_sites");
234 
235  $db->query("SELECT id FROM piwik_users WHERE uid='$cuid'");
236  if ($db->num_rows() <= 1) {
237  $db->query("SELECT id FROM piwik_sites WHERE uid='$cuid'");
238  if ($db->num_rows() > 0)
239  return true;
240  }
241 
242  return false;
243  }
global $db
Definition: bootstrap.php:22
$msg
Definition: config.php:155
$cuid
Definition: bootstrap.php:43
m_piwik::user_remoteauth ( )

Definition at line 296 of file m_piwik.php.

296  {
297  //FIXME
298  return true;
299  }
m_piwik::users_list ( )

Definition at line 273 of file m_piwik.php.

References $cuid, $db, $msg, and call_privileged_page().

273  {
274  global $db, $cuid, $msg;
275 
276  $msg->log("piwik","users_list");
277 
278  $db->query("SELECT login FROM piwik_users WHERE uid = ?;", array($cuid));
279  if ($db->num_rows() == 0)
280  return array();
281  $users = '';
282  while ($db->next_record())
283  $users .= ($users !== '') ? ',' . $db->f('login') : $db->f('login');
284  return $this->call_privileged_page('API', 'UsersManager.getUsers', array('userLogins' => $users));
285  }
call_privileged_page($module, $method, $arguments=array(), $output= 'JSON')
Definition: m_piwik.php:509
global $db
Definition: bootstrap.php:22
$msg
Definition: config.php:155
$cuid
Definition: bootstrap.php:43

Member Data Documentation

m_piwik::$alternc_sites

Definition at line 30 of file m_piwik.php.

Referenced by get_alternc_sites().

m_piwik::$alternc_users

Definition at line 29 of file m_piwik.php.

Referenced by get_alternc_users().

m_piwik::$piwik_admin_token

Definition at line 28 of file m_piwik.php.

Referenced by call_privileged_page().

m_piwik::$piwik_server_uri

Definition at line 27 of file m_piwik.php.

Referenced by url().


The documentation for this class was generated from the following file: