diff --git a/bureau/admin/piwik_addaccount.php b/bureau/admin/piwik_addaccount.php
index b1bd2fde..e8d37fdc 100644
--- a/bureau/admin/piwik_addaccount.php
+++ b/bureau/admin/piwik_addaccount.php
@@ -28,44 +28,22 @@
----------------------------------------------------------------------
*/
require_once("../class/config.php");
-include_once("head.php");
-if (!$quota->cancreate("piwik")) {
- $error=_("You cannot add any new Piwik account, your quota is over.");
- $fatal=1;
+$userslist = $piwik->users_list();
+$quotapiwik = $quota->getquota('piwik');
+
+if (!($quotapiwik['t'] > 0 && count($userslist) < 3)) {
+ $msg->raise('Error', "piwik", _("You cannot add any new Piwik account, your quota is over.")." ("._("Max. 3 accounts").")");
}
$fields = array (
"account_name" => array ("post", "string", ""),
+ "account_mail" => array ("post", "string", ""),
);
getFields($fields);
-if (empty($account_name)) {
- echo "
"._("Error : missing arguments.")."
";
- include_once("foot.php");
- exit;
+if ($piwik->user_add($account_name, $account_mail) ) {
+ $msg->raise('Ok', "piwik", _('Successfully added piwik account')); // à traduire (ou à corriger)
}
-
-?>
-
-
-
-user_add($account_name);
-if (!$infos)
-{
- $error = $err->errstr();
- //if (isset($error) && $error) {
- echo "$error
";
- if (isset($fatal) && $fatal) {
- include_once("foot.php");
- exit();
- }
-}
-else
-{
- printf("%s %s\n", _('Successfully added piwik user'), $account_name);
-}
-
-include_once("foot.php");
+include_once("piwik_userlist.php");
?>
diff --git a/bureau/admin/piwik_addsites.php b/bureau/admin/piwik_addsites.php
index 0d2ab486..e62ca431 100644
--- a/bureau/admin/piwik_addsites.php
+++ b/bureau/admin/piwik_addsites.php
@@ -39,11 +39,9 @@ getFields($fields);
if(empty($site_name)) $site_name=$site_urls;
if (empty($site_name)) {
- $error=("Error: missing arguments.");
-} elseif (! $piwik->site_add($site_name, $site_urls) ) {
- $error=_("Error while adding website. ".$err->errstr());
-} else {
- $error=_("Website added Successfully");
+ $msg->raise('Error', "piwik", _("All fields are mandatory"));
+} elseif ( $piwik->site_add($site_name, $site_urls) ) {
+ $msg->raise('Ok', "piwik", _("Website added Successfully"));
}
include_once("piwik_sitelist.php");
diff --git a/bureau/admin/piwik_site_dodel.php b/bureau/admin/piwik_site_dodel.php
index a229f408..2dda7e67 100755
--- a/bureau/admin/piwik_site_dodel.php
+++ b/bureau/admin/piwik_site_dodel.php
@@ -31,23 +31,20 @@ require_once("../class/config.php");
$fields = array (
"confirm_del" => array ("post", "string", ""),
- "siteid" => array ("post", "integer", -1),
+ "siteid" => array ("request", "integer", -1),
);
getFields($fields);
if ($siteid === -1) {
- $error=_("Missing site parameters");
+ $msg->raise('Error', "piwik", _("Missing site parameters"));
include('piwik_sitelist.php');
exit;
}
if(!empty($confirm_del) ) {
- if (! $piwik->site_delete($siteid) ) {
- $error=$err->errstr();
- } else {
- include_once('head.php');
- __("Site successfully deleted");
+ if ($piwik->site_delete($siteid) ) {
+ $msg->raise('Ok', "piwik", _("Site successfully deleted"));
}
include('piwik_sitelist.php');
diff --git a/bureau/admin/piwik_sitelist.php b/bureau/admin/piwik_sitelist.php
index 28f18a50..eddd8a4d 100755
--- a/bureau/admin/piwik_sitelist.php
+++ b/bureau/admin/piwik_sitelist.php
@@ -41,46 +41,18 @@ getFields($fields);
$piwik_alternc_users = $piwik->get_alternc_users();
$piwik_alternc_sites = $piwik->get_alternc_sites();
-/* Form was submitted, need to deal with work to do. */
-if ($right !== FALSE) {
- // Should this stay here, or in the API?
- if (!in_array($site_id, $piwik_alternc_sites))
- $error = _("You don't own this piwik site!");
- else {
- /* Foreach row of right, extract user, and selected credential */
- foreach ($right AS $user => $cred)
- {
- /* Ensures that the user is legitimate for that user */
- /* If not, we just break the loop, and set error message */
- if (!in_array($user, $piwik_alternc_users)) {
- $error = sprintf('%s "%s"', _('You dont own user'), $user);
- break;
- }
-
- /* Ok, current user has right to manage this piwik user. Update rights. */
- printf ("%s -> %s \n", $user, $cred);
- if (!$piwik->site_set_user_right($site_id, $user, $cred)) {
- $error = $err->errstr();
- break;
- }
- }
- }
-}
-
-/* If something went wrong, display error message, but continue with the page rendering */
-if (isset($error) && $error) {
- echo "$error
";
-}
-
/* Does current user still has quota ? */
if ($quota->cancreate("piwik")) {
$quotapiwik=$quota->getquota('piwik');
/* If quota are still available, display form to let user add a new site */
- if ($quotapiwik['u']>0) {
+ if ($quotapiwik['t']>0) {
?>
+msg_html_all("", true, true);
+?>
+
+
+
+
+
+
diff --git a/bureau/admin/piwik_user_dodel.php b/bureau/admin/piwik_user_dodel.php
index fe06d65a..e87e0022 100755
--- a/bureau/admin/piwik_user_dodel.php
+++ b/bureau/admin/piwik_user_dodel.php
@@ -36,17 +36,17 @@ $fields = array (
getFields($fields);
if (empty($login)) {
- $error=_("Missing login parameters");
+ $msg->raise('Error', "piwik", _("Missing login parameters"));
include('piwik_userlist.php');
exit;
}
if(!empty($confirm_del)) {
- if (! $piwik->user_delete($login) ) {
- $error=$err->errstr();
- } else {
- include_once('head.php');
- printf("Utilisateur %s supprimé avec succès\n", $login);
+ // contrôle si ce compte piwik a encore des sites associés avant d'accepter sa suppression
+ if ($piwik->user_has_sites()) {
+ $msg->raise('Alert', "piwik", _("You must first remove all the piwik sites associated with this user before deleting him")); // à traduire
+ } else if ($piwik->user_delete($login) ) {
+ $msg->raise('Ok', "piwik", _("Account %s is successfully deleted"), $login);
}
include('piwik_userlist.php');
diff --git a/bureau/admin/piwik_useradmin.php b/bureau/admin/piwik_useradmin.php
index b3feb4ef..b28a4264 100755
--- a/bureau/admin/piwik_useradmin.php
+++ b/bureau/admin/piwik_useradmin.php
@@ -39,7 +39,7 @@ getFields($fields);
if ($user_name === FALSE)
{
- $error = _('No piwik user specified');
+ $msg->raise('Error', "piwik", _('No piwik user specified'));
}
else
{
@@ -49,7 +49,7 @@ else
$db->next_record();
if ($db->f('ok')!=1)
{
- $error = _("You don't own this piwik website");
+ $msg->raise('Error', "piwik", _("You don't own this piwik website"));
}
else
{
@@ -57,7 +57,7 @@ else
$db->next_record();
if ($db->f('ok')!=1)
{
- $error = _("You don't own this piwik user");
+ $msg->raise('Error', "piwik", _("You don't own this piwik user"));
}
else
{
@@ -66,13 +66,13 @@ else
{
$api_data = $piwik->site_set_user_right($site_id, $user_name, $right);
if ($api_data === FALSE)
- echo $error;
+ $msg->raise('Error', "piwik", $api_data->message)
else
- __('success');
+ $msg->raise('Ok', "piwik", _('success'));
}
else
{
- $error = _("This right does not exist");
+ $msg->raise('Error', "piwik", _("This right does not exist"));
}
}
}
@@ -96,14 +96,11 @@ else
$user_piwik_users = array_flip($user_piwik_users);
}
-
-if (isset($error) && $error) {
- echo "$error
";
- exit;
-}
?>
msg_html_all();
+
$raw_sites = $piwik->get_site_list();
$piwik_sites = array();
foreach ($raw_sites AS $site) {
diff --git a/bureau/admin/piwik_userlist.php b/bureau/admin/piwik_userlist.php
index 620cb07f..4d450ea8 100755
--- a/bureau/admin/piwik_userlist.php
+++ b/bureau/admin/piwik_userlist.php
@@ -30,50 +30,52 @@
require_once("../class/config.php");
include_once("head.php");
-if (isset($error) && $error) {
- echo "$error
";
-}
+$userslist = $piwik->users_list();
+$quotapiwik = $quota->getquota('piwik');
-//Mail creation.
-if ($quota->cancreate("piwik")) {
- $quotapiwik=$quota->getquota('piwik');
+// TODO - Put the limit of piwik users (here at 3) as a variable in alternC
+if ($quotapiwik['t'] > 0 && count($userslist) < 3) {
?>
+msg_html_all("", true, true);
+?>
- "/>
+
+ ()
-
+
0) {
-?>
-
-
-
- "/>
- " />
-
-
-
-
- 0
+} else {
+ $msg->raise('Info', "piwik", _("You cannot add any new Piwik account, your quota is over."));
} // cancreate piwik
?>
users_list();
-
+echo $msg->msg_html_all(" ", true, true);
// printVar($piwik->dev());
if (empty($userslist)){
- __("No existing Piwik users");
+ $msg->raise('Info', "piwik", _("No existing Piwik accounts")); // à traduire (ou à corriger)
+ echo $msg->msg_html_all();
} else {
?>
@@ -83,12 +85,39 @@ if (empty($userslist)){
$col=1;
foreach ($userslist as $user ){
+ unset($piwik_pwd);
+ $form_id="main_".$user->login;
+
+ $db->query("SELECT passwd FROM piwik_users WHERE login = '$user->login'");
+ if ($db->next_record()) {
+ $piwik_pwd = $db->f('passwd');
+ }
+
$col=3-$col;
?>
-
- login ?>
-
+
+
+
+
+
+ " onclick="document.getElementById('').submit();" style="background: url('/images/delete.png') no-repeat 3px 3px; padding-left: 16px;" />
+
+
+
+ login ?>
+
+
+
+
+
+
+
diff --git a/bureau/class/m_piwik.php b/bureau/class/m_piwik.php
index 2ae6edc1..27bd9975 100644
--- a/bureau/class/m_piwik.php
+++ b/bureau/class/m_piwik.php
@@ -77,7 +77,7 @@ class m_piwik {
*/
function hook_quota_get() {
global $db, $cuid;
- $db->query("SELECT COUNT(id) AS nb FROM piwik_users WHERE uid= ? ;", array($cuid));
+ $db->query("SELECT COUNT(id) AS nb FROM piwik_sites WHERE uid= ? ;", array($cuid));
$q=Array("name"=>"piwik", "description"=>_("Statistics through Piwik accounts"), "used"=>0);
if ($db->next_record()) {
$q['used']=$db->f('nb');
@@ -96,13 +96,24 @@ class m_piwik {
/***********************/
- function user_add($user_login, $user_mail = null) {
+ function user_add($user_login, $user_mail) {
+ global $db, $mem, $cuid, $msg;
- global $db, $mem, $cuid, $err;
+ $msg->log("piwik","user_add");
+
+ if (empty($user_login) || is_null($user_login) || empty($user_mail) || is_null($user_mail)) {
+ $msg->raise('Error', "piwik", _("All fields are mandatory"));
+ return false;
+ }
+
+ // Validate the email syntax:
+ if (!filter_var($user_mail, FILTER_VALIDATE_EMAIL)) {
+ $msg->raise('Error', "piwik", _("The email you entered is syntaxically incorrect"));
+ return false;
+ }
$user_login = $this->clean_user_name($user_login);
$user_pass = create_pass();
- $user_mail = $mem->user['mail'];
$user_alias = $user_login;
$api_data = $this->call_privileged_page('API', 'UsersManager.addUser', array('userLogin' => $user_login, 'password' => $user_pass, 'email' => $user_mail, 'alias' => $user_alias), 'JSON');
@@ -110,7 +121,11 @@ class m_piwik {
if ($api_data->result === 'success') {
$user = $this->get_user($user_login);
$user_creation_date = $user->date_registered;
- return $db->query("INSERT INTO piwik_users (uid, login, created_date) VALUES ( ?, ?, ?,);", array($cuid, $user_login, $user_creation_date));
+ $ret_value = $db->query("INSERT INTO piwik_users (uid, passwd, login, created_date) VALUES ( ?, ?, ?);", array($cuid, md5('$user_pass'), $user_login, $user_creation_date));
+ return $ret_value;
+ } else {
+ $msg->raise('Error', "piwik", $api_data->message);
+ return FALSE;
}
} else { // api_data = false -> error is already filled
return FALSE;
@@ -129,14 +144,16 @@ class m_piwik {
}
function get_users_access_from_site($site_id) {
- global $err, $cuid;
+ global $msg, $cuid;
+
+ $msg->log("piwik","get_users_access_from_site");
if (!is_numeric($site_id)) {
- $err->raise('piwik', 'site_id must be numeric');
+ $msg->raise('Error', 'piwik', 'site_id must be numeric');
return FALSE;
}
if (!in_array($site_id, $this->alternc_sites)) {
- $err->raise('piwik', "you don't own this piwik website");
+ $msg->raise('Error', 'piwik', "you don't own this piwik website");
return FALSE;
}
@@ -166,7 +183,9 @@ class m_piwik {
}
function get_alternc_users() {
- global $db, $cuid;
+ global $db, $cuid, $msg;
+
+ $msg->log("piwik","get_alternc_users");
static $alternc_users = array();
$db->query("SELECT login FROM piwik_users WHERE uid= ?;", array($cuid));
@@ -175,10 +194,52 @@ class m_piwik {
return $alternc_users;
}
+
+ function get_users_url_infos() {
+ global $db,$cuid, $msg;
+ $infos_user = array();
+ $api_calls = array();
+
+
+ $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");
+ while ($db->next_record()) {
+ $id = $db->f('id');
+ $login = $db->f('login');
+
+ if (!isset($infos_user[$id]))
+ $infos_user[$id] = array();
+
+ if (!isset($api_calls[$id]))
+ $api_calls[$id] = $this->get_users_access_from_site($id);
+
+ foreach ($api_calls[$id] as $l => $cred) {
+ if ($l == $login)
+ $infos_user[$id][] = array('login' => $login, 'password' => $db->f('passwd'), 'cred' => $cred);
+ }
+ }
+
+ return $infos_user;
+ }
+
+ // Regarde si l'utilisateur a des sites piwik configurés dans AlternC
+ function user_has_sites() {
+ global $db, $cuid, $msg;
+
+ $msg->log("piwik","user_has_sites");
+
+ $db->query("SELECT id FROM piwik_sites WHERE uid='$cuid'");
+ if ($db->num_rows() > 0)
+ return true;
+
+ return false;
+ }
+
// Supprime l'utilisateur Piwik passé en parametre
// Ne le supprime pas localement tant que pas supprimé en remote
function user_delete($piwik_user_login) {
- global $db, $cuid, $err;
+ global $db, $cuid, $msg;
+
+ $msg->log("piwik","user_delete");
$db->query("SELECT created_date, COUNT(id) AS cnt FROM piwik_users WHERE uid= ? AND login= ? ", array($cuid, $piwik_user_login));
$db->next_record();
@@ -192,14 +253,17 @@ class m_piwik {
return FALSE;
}
} else {
- $err->raise("piwik", _("You are not allowed to delete the statistics of this website"));
+ $msg->raise('Error', "piwik", _("You are not allowed to delete the statistics of this website"));
return FALSE;
}
}
function users_list() {
- global $db, $cuid;
+ global $db, $cuid, $msg;
+
+ $msg->log("piwik","users_list");
+
$db->query("SELECT login FROM piwik_users WHERE uid = ?;", array($cuid));
if ($db->num_rows() == 0)
return array();
@@ -239,6 +303,10 @@ class m_piwik {
function site_list() {
+ global $msg;
+
+ $msg->log("piwik","site_list");
+
$api_data = $this->call_privileged_page('API', 'SitesManager.getAllSites');
$data = array();
@@ -274,7 +342,9 @@ class m_piwik {
}
function get_alternc_sites() {
- global $db, $cuid;
+ global $db, $cuid, $msg;
+
+ $msg->log("piwik","get_alternc_sites");
static $alternc_sites = array();
$db->query("SELECT piwik_id AS site_id FROM piwik_sites WHERE uid= ? ;", array($cuid));
@@ -291,18 +361,40 @@ class m_piwik {
// Ajoute un site à Piwik
// can't figure out how to pass multiple url through the API
function site_add($siteName, $urls, $ecommerce = FALSE) {
- global $db, $cuid;
+ global $db, $cuid, $piwik, $msg;
+
+ $msg->log("piwik","site_add");
+
$urls = is_array($urls) ? implode(',', $urls) : $urls;
$api_data = $this->call_privileged_page('API', 'SitesManager.addSite', array('siteName' => $siteName, 'urls' => $urls));
- $db->query("INSERT INTO piwik_sites set uid= ? , piwik_id= ? ", array($cuid, $api_data->value));
- return TRUE;
+
+ if ($api_data->value) {
+ $id_site = $api_data->value;
+
+ // Ajout de donner auto les droits de lecture à ce nouvel utilisateur pour le site qu'il a ajouté
+ $userslist = $piwik->users_list();
+ $api_data = $this->call_privileged_page('API', 'UsersManager.setUserAccess', array('userLogin' => $userslist[0]->login, 'idSites' => $id_site, 'access' => 'view'));
+
+ if ($api_data->result == 'success') {
+ // On enregistre le site dans alternC
+ $db->query("INSERT INTO piwik_sites set uid= ? , piwik_id= ? ", array($cuid, $id_site));
+
+ // Permet de prendre en compte le site qu'on vient de créer dans la page quis'affiche
+ $this->alternc_sites = $this->get_alternc_sites();
+ return TRUE;
+ }
+ return TRUE;
+ } else
+ return FALSE;
}
//SitesManager.deleteSite (idSite)
// Supprime un site de Piwik
function site_delete($site_id) {
- global $db, $cuid, $err;
+ global $db, $cuid, $msg;
+
+ $msg->log("piwik","site_delete");
$db->query("SELECT COUNT(id) AS cnt FROM piwik_sites WHERE uid= ? AND piwik_id= ? ;", array($cuid, $site_id));
$db->next_record();
@@ -315,7 +407,7 @@ class m_piwik {
return FALSE;
}
} else {
- $err->raise("piwik", _("You are not allowed to delete the statistics of this website"));
+ $msg->raise('Error', "piwik", _("You are not allowed to delete the statistics of this website"));
return FALSE;
}
@@ -325,14 +417,17 @@ class m_piwik {
function site_set_user_right($site_id, $login, $right)
{
- global $err;
+ global $msg;
+
+ $msg->log("piwik","site_set_user_right");
+
if (!in_array($right, array('noaccess', 'view', 'admin')))
return FALSE;
$api_data = $this->call_privileged_page('API', 'UsersManager.setUserAccess', array('userLogin' => $login, 'access' => $right, 'idSites' => $site_id));
if ($api_data->result == 'success') {
return TRUE;
} else {
- $err->raise('piwik', $api_data->messsage);
+ $msg->raise('Error', 'piwik', $api_data->messsage);
return FALSE;
}
}
@@ -346,8 +441,10 @@ class m_piwik {
/* return a clean username with a unique prefix per account */
function clean_user_name($username) {
- global $admin, $cuid;
- return 'alternc_' . $admin->get_login_by_uid($cuid) . '_' . mysql_real_escape_string(trim($username));
+ global $admin, $cuid, $db;
+ $escaped_name=$db->quote(trim($username));
+ $escaped_name=preg_replace("/^'(.*)'/", "\\1", $escaped_name);
+ return 'alternc_' . $admin->get_login_by_uid($cuid) . '_' . $escaped_name;
}
@@ -362,26 +459,30 @@ class m_piwik {
* @param string $method
*/
function call_page($module, $method, $arguments=array(), $output = 'JSON') {
- global $err;
+ global $msg;
+
+ $msg->log("piwik","call_page");
+
$url = sprintf('%s/?module=%s&method=%s&format=%s', $this->piwik_server_uri, $module, $method, $output);
foreach ($arguments AS $k=>$v)
$url .= sprintf('&%s=%s', urlencode($k), $v); // urlencode($v));
$page_content = file_get_contents($url);
if ($page_content === FALSE) {
- $err->raise("piwik", _("Unable to reach the API"));
+ $msg->raise('Error', "piwik", _("Unable to reach the API"));
return FALSE;
}
+
if ($output == 'JSON') {
$api_data = json_decode($page_content);
if ($api_data === FALSE) {
- $err->raise("piwik", _("Error while decoding response from the API"));
+ $msg->raise('Error', "piwik", _("Error while decoding response from the API"));
return FALSE;
}
return $api_data;
} else {
- $err->raise("piwik", _("Other format than JSON is not implemented yet"));
+ $msg->raise('Error', "piwik", _("Other format than JSON is not implemented yet"));
return FALSE;
}
}
@@ -392,6 +493,10 @@ class m_piwik {
* @param string $method
*/
function call_privileged_page($module, $method, $arguments=array(), $output = 'JSON') {
+ global $msg;
+
+ $msg->log("piwik","call_privileged_page");
+
$arguments['token_auth'] = $this->piwik_admin_token;
return $this->call_page($module, $method, $arguments, $output);
}