34 if ( empty($this->piwik_server_uri) || empty($this->piwik_admin_token))
return false;
37 'title' => _(
"Piwik statistics"),
41 array(
'txt' => _(
"Piwik Users"),
'url' =>
'piwik_userlist.php'),
42 array(
'txt' => _(
"Piwik Sites"),
'url' =>
'piwik_sitelist.php'),
54 $this->piwik_server_uri=
variable_get(
'piwik_server_uri',
'',
'Remote Piwik server uri');
55 $this->piwik_admin_token=
variable_get(
'piwik_admin_token',
'',
'Remote Piwik super-admin token');
56 $this->alternc_users=array();
57 $this->alternc_sites=array();
78 $db->query(
"SELECT COUNT(id) AS nb FROM piwik_sites WHERE uid= ? ;", array(
$cuid));
79 $q=Array(
"name"=>
"piwik",
"description"=>_(
"Statistics through Piwik accounts"),
"used"=>0);
80 if (
$db->next_record()) {
81 $q[
'used']=
$db->f(
'nb');
99 $msg->log(
"piwik",
"user_add");
101 if (empty($user_login) || is_null($user_login) || empty($user_mail) || is_null($user_mail)) {
102 $msg->raise(
"ERROR",
"piwik", _(
"All fields are mandatory"));
107 if (!filter_var($user_mail, FILTER_VALIDATE_EMAIL)) {
108 $msg->raise(
"ERROR",
"piwik", _(
"The email you entered is syntaxically incorrect"));
114 $user_alias = $user_login;
116 $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->result ===
'success') {
120 $user_creation_date =
$user->date_registered;
121 $ret_value =
$db->query(
"INSERT INTO piwik_users (uid, passwd, login, created_date) VALUES ( ?, ?, ?, ?);", array(
$cuid, md5($user_pass), $user_login, $user_creation_date));
124 $msg->raise(
"ERROR",
"piwik", $api_data->message);
141 return $this->
call_privileged_page(
'API',
'UsersManager.getSitesAccessFromUser', array(
'userLogin' => $user_login));
147 $msg->debug(
"piwik",
"get_users_access_from_site");
151 if (!is_numeric($site_id)) {
152 $msg->raise(
"ERROR",
'piwik',
'site_id must be numeric');
155 if (!in_array($site_id, $this->alternc_sites)) {
156 $msg->raise(
"ERROR",
'piwik',
"you don't own this piwik website");
160 $api_data = $this->
call_privileged_page(
'API',
'UsersManager.getUsersAccessFromSite', array(
'idSite' => $site_id));
161 if ($api_data !== FALSE) {
162 $api_data = $api_data[0];
163 foreach ($this->alternc_users AS
$key=>
$user) {
164 if (!array_key_exists(
$user, $api_data)) {
165 $api_data->$user =
'noaccess';
178 $api_data = $this->
call_privileged_page(
'API',
'UsersManager.getUser', array(
'userLogin' => $user_login));
190 if (!count($this->alternc_sites) || $force) {
191 $db->query(
"SELECT piwik_id AS site_id FROM piwik_sites WHERE uid= ? ;", array(
$cuid));
192 while (
$db->next_record())
193 $this->alternc_sites[]=
$db->f(
'site_id');
201 if (!count($this->alternc_users)) {
202 $db->query(
"SELECT login FROM piwik_users WHERE uid= ?;", array(
$cuid));
203 while (
$db->next_record())
204 $this->alternc_users[]=
$db->f(
'login');
212 $infos_user = array();
213 $api_calls = array();
216 $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");
217 while (
$db->next_record()) {
221 if (!isset($infos_user[
$id]))
222 $infos_user[
$id] = array();
224 if (!isset($api_calls[
$id]))
227 foreach ($api_calls[
$id] as $l => $cred) {
229 $infos_user[
$id][] = array(
'login' =>
$login,
'password' =>
$db->f(
'passwd'),
'cred' => $cred);
243 $msg->debug(
"piwik",
"user_has_sites");
245 $db->query(
"SELECT id FROM piwik_users WHERE uid='$cuid'");
246 if (
$db->num_rows() <= 1) {
247 $db->query(
"SELECT id FROM piwik_sites WHERE uid='$cuid'");
248 if (
$db->num_rows() > 0)
263 $msg->log(
"piwik",
"user_delete");
265 $db->query(
"SELECT created_date, COUNT(id) AS cnt FROM piwik_users WHERE uid= ? AND login= ? ", array(
$cuid, $piwik_user_login));
268 if (
$db->f(
'cnt') == 1) {
269 $api_data = $this->
call_privileged_page(
'API',
'UsersManager.deleteUser', array(
'userLogin' => $piwik_user_login));
270 if ($api_data->result ==
'success') {
271 return $db->query(
"DELETE FROM piwik_users WHERE uid= ? AND login= ? ;", array(
$cuid, $piwik_user_login));
277 $msg->raise(
"ERROR",
"piwik", _(
"You are not allowed to delete the statistics of this website"));
286 $msg->debug(
"piwik",
"users_list");
288 $db->query(
"SELECT login FROM piwik_users WHERE uid = ?;", array(
$cuid));
289 if (
$db->num_rows() == 0)
292 while (
$db->next_record())
293 $users .= ($users !==
'') ?
',' .
$db->f(
'login') :
$db->f(
'login');
294 return $this->
call_privileged_page(
'API',
'UsersManager.getUsers', array(
'userLogins' => $users));
326 $msg->debug(
"piwik",
"site_list");
330 array(
'filter_limit' => -1));
334 foreach ($api_data AS $site) {
336 if (!in_array($site->idsite, $this->alternc_sites))
339 $item =
new stdClass();
341 $item->id = $site->idsite;
342 $item->name = $site->name;
343 $item->main_url = $site->main_url;
345 $user_data = $this->
call_privileged_page(
'API',
'UsersManager.getUsersAccessFromSite', array(
'idSite' => $site->idsite));
350 $item->rights = $user_data[0];
362 return $this->
call_privileged_page(
'API',
'SitesManager.getJavascriptTag', array(
'idSite' => $site_id,
'piwikUrl' => $this->piwik_server_uri))->value;
369 array(
'filter_limit' => -1));
373 function site_add($siteName, $urls, $ecommerce = FALSE) {
376 $msg->log(
"piwik",
"site_add");
379 $urls = is_array($urls) ? implode(
',', $urls) : $urls;
380 $api_data = $this->
call_privileged_page(
'API',
'SitesManager.addSite', array(
'siteName' => $siteName,
'urls' => $urls));
382 if ($api_data->value) {
383 $id_site = $api_data->value;
387 $api_data = $this->
call_privileged_page(
'API',
'UsersManager.setUserAccess', array(
'userLogin' =>
$userslist[0]->login,
'idSites' => $id_site,
'access' =>
'view'));
389 if ($api_data->result ==
'success') {
391 $db->query(
"INSERT INTO piwik_sites set uid= ? , piwik_id= ? ", array(
$cuid, $id_site));
408 $msg->log(
"piwik",
"site_delete");
410 $db->query(
"SELECT COUNT(id) AS cnt FROM piwik_sites WHERE uid= ? AND piwik_id= ? ;", array(
$cuid, $site_id));
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));
421 $msg->raise(
"ERROR",
"piwik", _(
"You are not allowed to delete the statistics of this website"));
433 $msg->log(
"piwik",
"site_set_user_right");
435 if (!in_array($right, array(
'noaccess',
'view',
'admin')))
437 $api_data = $this->
call_privileged_page(
'API',
'UsersManager.setUserAccess', array(
'userLogin' =>
$login,
'access' => $right,
'idSites' => $site_id));
438 if ($api_data->result ==
'success') {
441 $msg->raise(
"ERROR",
'piwik', $api_data->messsage);
460 $escaped_name=preg_replace(
"/^'(.*)'/",
"\\1", $escaped_name);
461 return 'alternc_' . $admin->get_login_by_uid(
$cuid) .
'_' . $escaped_name;
475 function call_page($module, $method, $arguments=array(), $output =
'JSON') {
478 $msg->debug(
"piwik",
"call_page");
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);
484 $page_content = file_get_contents($url);
485 if ($page_content === FALSE) {
486 $msg->raise(
"ERROR",
"piwik", _(
"Unable to reach the API"));
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"));
499 $msg->raise(
"ERROR",
"piwik", _(
"Other format than JSON is not implemented yet"));
512 $msg->debug(
"piwik",
"call_privileged_page");
515 return $this->
call_page($module, $method, $arguments, $output);
variable_get($name, $default=null, $createit_comment=null)
Return a persistent variable.
This class manage piwik statistics management through AlternC, using piwik's "API".
user_edit()
Change a user @TODO: code this.
site_set_user_right($site_id, $login, $right)
call_privileged_page($module, $method, $arguments=array(), $output='JSON')
hook_admin_del_member()
hook called when an AlternC account is deleted
user_delete($piwik_user_login)
Delete a piwik user don't delete it locally unless it has been remotely deleted.
user_add($user_login, $user_mail)
user_has_sites()
does this user has piwik websites configured in AlternC ?
site_add($siteName, $urls, $ecommerce=FALSE)
get_alternc_sites($force=false)
hook_quota_get()
Returns the used quota for the $name service for the current user.
get_site_access($user_login)
clean_user_name($username)
return a clean username with a unique prefix per account
get_users_access_from_site($site_id)
user_checkremote($puser_id)
call_page($module, $method, $arguments=array(), $output='JSON')
create_pass($length=10, $classcount=3)
Create a password compatible with the password policy.
$userslist
Add a piwik account using piwik's API.
if(!isset($is_include)) if(! $key &&! $crt) $id
if(empty($_POST['key'])||empty($_POST['val'])) $key