classe piwik & fichiers section admin associés

This commit is contained in:
quenenni 2017-08-16 18:29:25 +02:00
parent b4588c870a
commit 6f30c8bac4
8 changed files with 311 additions and 155 deletions

View File

@ -28,44 +28,22 @@
---------------------------------------------------------------------- ----------------------------------------------------------------------
*/ */
require_once("../class/config.php"); require_once("../class/config.php");
include_once("head.php");
if (!$quota->cancreate("piwik")) { $userslist = $piwik->users_list();
$error=_("You cannot add any new Piwik account, your quota is over."); $quotapiwik = $quota->getquota('piwik');
$fatal=1;
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 ( $fields = array (
"account_name" => array ("post", "string", ""), "account_name" => array ("post", "string", ""),
"account_mail" => array ("post", "string", ""),
); );
getFields($fields); getFields($fields);
if (empty($account_name)) { if ($piwik->user_add($account_name, $account_mail) ) {
echo "<p class=\"alert alert-danger\">"._("Error : missing arguments.")."</p>"; $msg->raise('Ok', "piwik", _('Successfully added piwik account')); // à traduire (ou à corriger)
include_once("foot.php");
exit;
} }
include_once("piwik_userlist.php");
?>
<h3><?php printf(_("Creation of Piwik account \"%s\""),$account_name); ?></h3>
<hr id="topbar"/>
<br />
<?php
$infos = $piwik->user_add($account_name);
if (!$infos)
{
$error = $err->errstr();
//if (isset($error) && $error) {
echo "<p class=\"alert alert-danger\">$error</p>";
if (isset($fatal) && $fatal) {
include_once("foot.php");
exit();
}
}
else
{
printf("%s %s\n", _('Successfully added piwik user'), $account_name);
}
include_once("foot.php");
?> ?>

View File

@ -39,11 +39,9 @@ getFields($fields);
if(empty($site_name)) $site_name=$site_urls; if(empty($site_name)) $site_name=$site_urls;
if (empty($site_name)) { if (empty($site_name)) {
$error=("Error: missing arguments."); $msg->raise('Error', "piwik", _("All fields are mandatory"));
} elseif (! $piwik->site_add($site_name, $site_urls) ) { } elseif ( $piwik->site_add($site_name, $site_urls) ) {
$error=_("Error while adding website.<br/>".$err->errstr()); $msg->raise('Ok', "piwik", _("Website added Successfully"));
} else {
$error=_("Website added Successfully");
} }
include_once("piwik_sitelist.php"); include_once("piwik_sitelist.php");

View File

@ -31,23 +31,20 @@ require_once("../class/config.php");
$fields = array ( $fields = array (
"confirm_del" => array ("post", "string", ""), "confirm_del" => array ("post", "string", ""),
"siteid" => array ("post", "integer", -1), "siteid" => array ("request", "integer", -1),
); );
getFields($fields); getFields($fields);
if ($siteid === -1) { if ($siteid === -1) {
$error=_("Missing site parameters"); $msg->raise('Error', "piwik", _("Missing site parameters"));
include('piwik_sitelist.php'); include('piwik_sitelist.php');
exit; exit;
} }
if(!empty($confirm_del) ) { if(!empty($confirm_del) ) {
if (! $piwik->site_delete($siteid) ) { if ($piwik->site_delete($siteid) ) {
$error=$err->errstr(); $msg->raise('Ok', "piwik", _("Site successfully deleted"));
} else {
include_once('head.php');
__("Site successfully deleted");
} }
include('piwik_sitelist.php'); include('piwik_sitelist.php');

View File

@ -41,46 +41,18 @@ getFields($fields);
$piwik_alternc_users = $piwik->get_alternc_users(); $piwik_alternc_users = $piwik->get_alternc_users();
$piwik_alternc_sites = $piwik->get_alternc_sites(); $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<br />\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 "<p class=\"alert alert-danger\">$error</p>";
}
/* Does current user still has quota ? */ /* Does current user still has quota ? */
if ($quota->cancreate("piwik")) { if ($quota->cancreate("piwik")) {
$quotapiwik=$quota->getquota('piwik'); $quotapiwik=$quota->getquota('piwik');
/* If quota are still available, display form to let user add a new site */ /* If quota are still available, display form to let user add a new site */
if ($quotapiwik['u']>0) { if ($quotapiwik['t']>0) {
?> ?>
<h3><?php __("Add a new website");?></h3> <h3><?php __("Add a new website");?></h3>
<?php
echo $msg->msg_html_all("<li>", true, true);
?>
<form method="post" action="piwik_addsites.php" id="main" name="addsites" > <form method="post" action="piwik_addsites.php" id="main" name="addsites" >
<?php csrf_get(); ?> <?php csrf_get(); ?>
<input type="text" class="int" name="site_urls" size="50" id="site_name" maxlength="255" value="" placeholder="<?php __("URL of the website")?>"/> <input type="text" class="int" name="site_urls" size="50" id="site_name" maxlength="255" value="" placeholder="<?php __("URL of the website")?>"/>
@ -91,6 +63,8 @@ if ($quota->cancreate("piwik")) {
<hr/> <hr/>
<?php <?php
} // quotapiwik > 0 } // quotapiwik > 0
} else {
$msg->raise('Info', "piwik", _("You cannot add any new Piwik sites, your quota is over."));
} // cancreate piwik } // cancreate piwik
@ -106,9 +80,47 @@ if ($quota->cancreate("piwik")) {
<h3><?php __("Existing Piwik monitored websites"); ?></h3> <h3><?php __("Existing Piwik monitored websites"); ?></h3>
<?php <?php
/* Get the list of piwik sites for current user */ /* Get the list of piwik sites for current user */
$sitelist = $piwik->site_list(); $sitelist = $piwik->site_list();
$infos_urls = $piwik->get_users_url_infos();
/* 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))
$msg->raise('Error', "piwik", _("You don't own this piwik site!"));
else {
foreach ($sitelist as $site) {
if ($site->id == $site_id)
$domain = $site->name;
}
/* 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)) {
$msg->raise('Error', "piwik", _('You dont own user'), $user);
break;
}
foreach ($infos_urls[$site_id] as $v) {
if ($v['login'] == $user && $v['cred'] != $cred) {
/* Ok, current user has right to manage this piwik user. Update rights. */
if (!$piwik->site_set_user_right($site_id, $user, $cred)) {
break;
} else {
$msg->raise('Ok', "piwik", _("Account '%s' has been given '%s' rights on '%s'"), array($user, $cred, $domain));
}
}
}
}
$infos_urls = $piwik->get_users_url_infos();
}
}
echo $msg->msg_html_all();
/* If user didn't add a website, just do nothing but display there's no site */ /* If user didn't add a website, just do nothing but display there's no site */
if (empty($sitelist)){ if (empty($sitelist)){
@ -118,18 +130,47 @@ if (empty($sitelist)){
?> ?>
<table class="tlist"> <table class="tlist">
<tr><th/><th><?php __("Site name");?></th><th align=center><?php __("Site url"); ?></th><th>Javascript Code</th></tr> <tr><th/><th><?php __("Site name");?></th><th align=center><?php __("Site url"); ?></th><th>Javascript Code</th><th>Interface</th></tr>
<?php <?php
$col=1; $col=1;
foreach ($sitelist as $site ){ foreach ($sitelist as $site ){
$col=3-$col; $col=3-$col;
$no_user=true;
$first_url="";
$list_users="<select name='list_users_".$site->id."' id='list_users_".$site->id."' onChange='change_url(".$site->id.", this);'>";
foreach ($infos_urls[$site->id] as $v) {
if ($v['cred'] != 'noaccess') {
$list_users .= "<option value='".$piwik->url()."?module=Login&action=logme&login=".$v['login']."&idSite=".$site->id."&password=".$v['password']."'>".$v['login']."</option>";
if ($no_user)
$first_url = $piwik->url()."?module=Login&action=logme&login=".$v['login']."&idSite=".$site->id."&password=".$v['password'];
$no_user=false;
}
}
$list_users .= "</select>";
?> ?>
<tr class="lst_clic<?php echo $col; ?>"> <tr class="lst_clic<?php echo $col; ?>">
<td><div class="ina"><a href="/piwik_site_dodel.php?siteid=<?php echo $site->id; ?>"><img src="images/delete.png" alt="<?php __("Delete"); ?>" /><?php __("Delete"); ?></a></div></td> <td><div class="ina"><a href="/piwik_site_dodel.php?siteid=<?php echo $site->id; ?>"><img src="images/delete.png" alt="<?php __("Delete"); ?>" /><?php __("Delete"); ?></a></div></td>
<td align=right><?php echo $site->name ?></td> <td><?php echo $site->name ?></td>
<td><?php echo $site->main_url ?></td> <td><?php echo $site->main_url ?></td>
<td><textarea><?php echo $piwik->site_js_tag($site->id); ?></textarea></td> <td><textarea><?php echo $piwik->site_js_tag($site->id); ?></textarea></td>
<td>
<div class="ina">
<?php
if (! $no_user) {
?>
<a id="connect_<?php echo $site->id; ?>" href="<?php echo $first_url; ?>" target="_blank"><?php __('Connect as'); ?></a> <!-- à traduire -->
<?php
echo $list_users;
} else {
?>
<img src="images/warning.png" onmouseover='$("#alert_div_msg").show();' onmouseout='$("#alert_div_msg").hide();'>
<?php } ?>
</div>
</td>
</tr> </tr>
<?php <?php
} // foreach sitelist } // foreach sitelist
@ -144,10 +185,9 @@ foreach ($sitelist as $site ){
?> ?>
</table> </table>
<h3><?php __("Credentials management"); ?></h3> <h3><?php __("Credentials management"); ?></h3>
<form method="get"> <form method="get" action="piwik_sitelist.php">
<select name="site_id"> <select name="site_id">
<?php <?php
@ -174,5 +214,14 @@ foreach ($sitelist as $site ){
} }
} // empty userlist } // empty userlist
?> ?>
<div class="ina" id="alert_div_msg" style="display:none;background-color:yellow;padding:5px;border:2px solid black;margin-top:3em;";>
<?php __("No user may access this site") ?>
</div>
<script type="text/javascript">
function change_url(id, elt) {
document.getElementById("connect_" + id).href = elt.value;
}
</script>
<?php include_once("foot.php"); ?> <?php include_once("foot.php"); ?>

View File

@ -36,17 +36,17 @@ $fields = array (
getFields($fields); getFields($fields);
if (empty($login)) { if (empty($login)) {
$error=_("Missing login parameters"); $msg->raise('Error', "piwik", _("Missing login parameters"));
include('piwik_userlist.php'); include('piwik_userlist.php');
exit; exit;
} }
if(!empty($confirm_del)) { if(!empty($confirm_del)) {
if (! $piwik->user_delete($login) ) { // contrôle si ce compte piwik a encore des sites associés avant d'accepter sa suppression
$error=$err->errstr(); if ($piwik->user_has_sites()) {
} else { $msg->raise('Alert', "piwik", _("You must first remove all the piwik sites associated with this user before deleting him")); // à traduire
include_once('head.php'); } else if ($piwik->user_delete($login) ) {
printf("Utilisateur %s supprimé avec succès\n", $login); $msg->raise('Ok', "piwik", _("Account %s is successfully deleted"), $login);
} }
include('piwik_userlist.php'); include('piwik_userlist.php');

View File

@ -39,7 +39,7 @@ getFields($fields);
if ($user_name === FALSE) if ($user_name === FALSE)
{ {
$error = _('No piwik user specified'); $msg->raise('Error', "piwik", _('No piwik user specified'));
} }
else else
{ {
@ -49,7 +49,7 @@ else
$db->next_record(); $db->next_record();
if ($db->f('ok')!=1) 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 else
{ {
@ -57,7 +57,7 @@ else
$db->next_record(); $db->next_record();
if ($db->f('ok')!=1) 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 else
{ {
@ -66,13 +66,13 @@ else
{ {
$api_data = $piwik->site_set_user_right($site_id, $user_name, $right); $api_data = $piwik->site_set_user_right($site_id, $user_name, $right);
if ($api_data === FALSE) if ($api_data === FALSE)
echo $error; $msg->raise('Error', "piwik", $api_data->message)
else else
__('success'); $msg->raise('Ok', "piwik", _('success'));
} }
else 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); $user_piwik_users = array_flip($user_piwik_users);
} }
if (isset($error) && $error) {
echo "<p class=\"alert alert-danger\">$error</p>";
exit;
}
?> ?>
<h3><?php printf('%s "%s"', _("Rights for user"), $user_name); ?></h3> <h3><?php printf('%s "%s"', _("Rights for user"), $user_name); ?></h3>
<?php <?php
echo $msg->msg_html_all();
$raw_sites = $piwik->get_site_list(); $raw_sites = $piwik->get_site_list();
$piwik_sites = array(); $piwik_sites = array();
foreach ($raw_sites AS $site) { foreach ($raw_sites AS $site) {

View File

@ -30,50 +30,52 @@
require_once("../class/config.php"); require_once("../class/config.php");
include_once("head.php"); include_once("head.php");
if (isset($error) && $error) { $userslist = $piwik->users_list();
echo "<p class=\"alert alert-danger\">$error</p>"; $quotapiwik = $quota->getquota('piwik');
}
//Mail creation. // TODO - Put the limit of piwik users (here at 3) as a variable in alternC
if ($quota->cancreate("piwik")) { if ($quotapiwik['t'] > 0 && count($userslist) < 3) {
$quotapiwik=$quota->getquota('piwik');
?> ?>
<h3><?php __("Create a new piwik account");?></h3> <h3><?php __("Create a new piwik account");?></h3>
<?php
echo $msg->msg_html_all("<li>", true, true);
?>
<form method="post" action="piwik_addaccount.php" id="main" name="addaccount" > <form method="post" action="piwik_addaccount.php" id="main" name="addaccount" >
<?php csrf_get(); ?> <?php csrf_get(); ?>
<input type="text" class="int" name="account_name" size="20" id="account_name" maxlength="32" value="<?php if ($quotapiwik['u']==0) {echo $mem->user["login"];}?>"/> <table class="tedit">
<tr>
<th><label for="account_name"><?php __("Account Name"); ?></label></th> <!-- à traduire -->
<td><span class="int" id="account_namefx"><?php echo $mem->user["login"]; ?>_</span><input type="text" class="int" name="account_name" size="20" id="account_name" maxlength="32" value=""/>
</tr>
<tr>
<th><label for="account_mail"><?php __("Linked Account Email"); ?></label></th> <!-- à traduire -->
<td><input type="text" class="int" name="account_mail" size="20" id="account_mail" maxlength="32" value="<?php if (count($userslist) == 0) {echo $mem->user["mail"];}?>"/>
</tr>
<tr class="trbtn"><td colspan="2">
<input type="submit" name="submit" class="inb" value="<?php __("Create"); ?>" /> <input type="submit" name="submit" class="inb" value="<?php __("Create"); ?>" />
</tr>
</table>
<i>(<?php ehe("Max. 3 accounts"); ?>)</i>
</form> </form>
<script type="text/javascript">
document.forms['main'].account_name.focus();
</script>
<br/> <br/>
<hr/> <hr/>
<?php <?php
} else {
if ($quotapiwik['u']>0) { $msg->raise('Info', "piwik", _("You cannot add any new Piwik account, your quota is over."));
?>
<h3><?php __("Add a new website");?></h3>
<form method="post" action="piwik_addsites.php" id="main" name="addsites" >
<?php csrf_get(); ?>
<input type="text" class="int" name="site_urls" size="50" id="site_name" maxlength="255" value="" placeholder="<?php __("URL of the website")?>"/>
<input type="submit" name="submit" class="inb" value="<?php __("Create"); ?>" />
</form>
<br/>
<hr/>
<?php
} // quota > 0
} // cancreate piwik } // cancreate piwik
?> ?>
<h3><?php __("Existing Piwik accounts"); ?></h3> <h3><?php __("Existing Piwik accounts"); ?></h3>
<?php <?php
echo $msg->msg_html_all("<li>", true, true);
$userslist = $piwik->users_list();
// printVar($piwik->dev()); // printVar($piwik->dev());
if (empty($userslist)){ if (empty($userslist)){
__("No existing Piwik users"); $msg->raise('Info', "piwik", _("No existing Piwik accounts")); // à traduire (ou à corriger)
echo $msg->msg_html_all();
} else { } else {
?> ?>
@ -83,12 +85,39 @@ if (empty($userslist)){
$col=1; $col=1;
foreach ($userslist as $user ){ 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; $col=3-$col;
?> ?>
<tr class="lst_clic<?php echo $col; ?>"> <tr class="lst_clic<?php echo $col; ?>">
<td><div class="ina"><a href="piwik_user_dodel.php?login=<?php echo urlencode($user->login); ?>"><img src="images/delete.png" alt="<?php __("Delete"); ?>" /><?php __("Delete"); ?></a></div></td> <td>
<td align=right><?php echo $user->login ?></td> <div class="ina">
<td><div class="ina"><a href="<?php printf('%s?module=Login&action=logme&login=%s&password=%s', $piwik->url(), $user->login, $user->password); ?>" target="_blank"><?php __('Connect'); ?></a></td> <form method="post" action="piwik_user_dodel.php" name="<?php echo $form_id; ?>" id="<?php echo $form_id; ?>">
<?php csrf_get(); ?>
<input type="hidden" name="login" value="<?php ehe($user->login);?>" />
<input type="button" class="ina" name="delete" value="<?php __("Delete"); ?>" onclick="document.getElementById('<?php echo $form_id; ?>').submit();" style="background: url('/images/delete.png') no-repeat 3px 3px; padding-left: 16px;" />
</form>
</div>
</td>
<td><?php echo $user->login ?></td>
<!--<td><div class="ina"><a href="<?php printf('%s?module=Login&action=logme&login=%s&password=%s', $piwik->url(), $user->login, $user->password); ?>" target="_blank"><?php __('Connect'); ?></a></td>-->
<td>
<?php
if ($piwik_pwd) {
?>
<div class="ina"><a href="<?php printf('%s?module=Login&action=logme&login=%s&password=%s', $piwik->url(), $user->login, $piwik_pwd); ?>" target="_blank"><?php __('Connect'); ?></a>
<?php
} else {
?>
<div class="ina"><img src="images/warning.png" onmouseover='$("#alert_div_msg").show();' onmouseout='$("#alert_div_msg").hide();'></div>
<?php } ?>
</td>
</tr> </tr>
<?php <?php
} // foreach userlist } // foreach userlist
@ -96,4 +125,7 @@ foreach ($userslist as $user ){
?> ?>
</table> </table>
<div class="ina" id="alert_div_msg" style="display:none;background-color:yellow;padding:5px;border:2px solid black;margin-top:3em;";> <!-- à traduire -->
<?php __("An error occurred. It was not possible to retrieve the access information to the Piwik interface") ?>
</div>
<?php include_once("foot.php"); ?> <?php include_once("foot.php"); ?>

View File

@ -77,7 +77,7 @@ class m_piwik {
*/ */
function hook_quota_get() { function hook_quota_get() {
global $db, $cuid; 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); $q=Array("name"=>"piwik", "description"=>_("Statistics through Piwik accounts"), "used"=>0);
if ($db->next_record()) { if ($db->next_record()) {
$q['used']=$db->f('nb'); $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_login = $this->clean_user_name($user_login);
$user_pass = create_pass(); $user_pass = create_pass();
$user_mail = $mem->user['mail'];
$user_alias = $user_login; $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'); $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') { if ($api_data->result === 'success') {
$user = $this->get_user($user_login); $user = $this->get_user($user_login);
$user_creation_date = $user->date_registered; $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 } else { // api_data = false -> error is already filled
return FALSE; return FALSE;
@ -129,14 +144,16 @@ class m_piwik {
} }
function get_users_access_from_site($site_id) { 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)) { if (!is_numeric($site_id)) {
$err->raise('piwik', 'site_id must be numeric'); $msg->raise('Error', 'piwik', 'site_id must be numeric');
return FALSE; return FALSE;
} }
if (!in_array($site_id, $this->alternc_sites)) { 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; return FALSE;
} }
@ -166,7 +183,9 @@ class m_piwik {
} }
function get_alternc_users() { function get_alternc_users() {
global $db, $cuid; global $db, $cuid, $msg;
$msg->log("piwik","get_alternc_users");
static $alternc_users = array(); static $alternc_users = array();
$db->query("SELECT login FROM piwik_users WHERE uid= ?;", array($cuid)); $db->query("SELECT login FROM piwik_users WHERE uid= ?;", array($cuid));
@ -175,10 +194,52 @@ class m_piwik {
return $alternc_users; 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 // Supprime l'utilisateur Piwik passé en parametre
// Ne le supprime pas localement tant que pas supprimé en remote // Ne le supprime pas localement tant que pas supprimé en remote
function user_delete($piwik_user_login) { 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->query("SELECT created_date, COUNT(id) AS cnt FROM piwik_users WHERE uid= ? AND login= ? ", array($cuid, $piwik_user_login));
$db->next_record(); $db->next_record();
@ -192,14 +253,17 @@ class m_piwik {
return FALSE; return FALSE;
} }
} else { } 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; return FALSE;
} }
} }
function users_list() { 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)); $db->query("SELECT login FROM piwik_users WHERE uid = ?;", array($cuid));
if ($db->num_rows() == 0) if ($db->num_rows() == 0)
return array(); return array();
@ -239,6 +303,10 @@ class m_piwik {
function site_list() { function site_list() {
global $msg;
$msg->log("piwik","site_list");
$api_data = $this->call_privileged_page('API', 'SitesManager.getAllSites'); $api_data = $this->call_privileged_page('API', 'SitesManager.getAllSites');
$data = array(); $data = array();
@ -274,7 +342,9 @@ class m_piwik {
} }
function get_alternc_sites() { function get_alternc_sites() {
global $db, $cuid; global $db, $cuid, $msg;
$msg->log("piwik","get_alternc_sites");
static $alternc_sites = array(); static $alternc_sites = array();
$db->query("SELECT piwik_id AS site_id FROM piwik_sites WHERE uid= ? ;", array($cuid)); $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 // Ajoute un site à Piwik
// can't figure out how to pass multiple url through the API // can't figure out how to pass multiple url through the API
function site_add($siteName, $urls, $ecommerce = FALSE) { 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; $urls = is_array($urls) ? implode(',', $urls) : $urls;
$api_data = $this->call_privileged_page('API', 'SitesManager.addSite', array('siteName' => $siteName, '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) //SitesManager.deleteSite (idSite)
// Supprime un site de Piwik // Supprime un site de Piwik
function site_delete($site_id) { 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->query("SELECT COUNT(id) AS cnt FROM piwik_sites WHERE uid= ? AND piwik_id= ? ;", array($cuid, $site_id));
$db->next_record(); $db->next_record();
@ -315,7 +407,7 @@ class m_piwik {
return FALSE; return FALSE;
} }
} else { } 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; return FALSE;
} }
@ -325,14 +417,17 @@ class m_piwik {
function site_set_user_right($site_id, $login, $right) 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'))) if (!in_array($right, array('noaccess', 'view', 'admin')))
return FALSE; return FALSE;
$api_data = $this->call_privileged_page('API', 'UsersManager.setUserAccess', array('userLogin' => $login, 'access' => $right, 'idSites' => $site_id)); $api_data = $this->call_privileged_page('API', 'UsersManager.setUserAccess', array('userLogin' => $login, 'access' => $right, 'idSites' => $site_id));
if ($api_data->result == 'success') { if ($api_data->result == 'success') {
return TRUE; return TRUE;
} else { } else {
$err->raise('piwik', $api_data->messsage); $msg->raise('Error', 'piwik', $api_data->messsage);
return FALSE; return FALSE;
} }
} }
@ -346,8 +441,10 @@ class m_piwik {
/* return a clean username with a unique prefix per account */ /* return a clean username with a unique prefix per account */
function clean_user_name($username) { function clean_user_name($username) {
global $admin, $cuid; global $admin, $cuid, $db;
return 'alternc_' . $admin->get_login_by_uid($cuid) . '_' . mysql_real_escape_string(trim($username)); $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 * @param string $method
*/ */
function call_page($module, $method, $arguments=array(), $output = 'JSON') { 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); $url = sprintf('%s/?module=%s&method=%s&format=%s', $this->piwik_server_uri, $module, $method, $output);
foreach ($arguments AS $k=>$v) foreach ($arguments AS $k=>$v)
$url .= sprintf('&%s=%s', urlencode($k), $v); // urlencode($v)); $url .= sprintf('&%s=%s', urlencode($k), $v); // urlencode($v));
$page_content = file_get_contents($url); $page_content = file_get_contents($url);
if ($page_content === FALSE) { if ($page_content === FALSE) {
$err->raise("piwik", _("Unable to reach the API")); $msg->raise('Error', "piwik", _("Unable to reach the API"));
return FALSE; return FALSE;
} }
if ($output == 'JSON') { if ($output == 'JSON') {
$api_data = json_decode($page_content); $api_data = json_decode($page_content);
if ($api_data === FALSE) { 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 FALSE;
} }
return $api_data; return $api_data;
} else { } 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; return FALSE;
} }
} }
@ -392,6 +493,10 @@ class m_piwik {
* @param string $method * @param string $method
*/ */
function call_privileged_page($module, $method, $arguments=array(), $output = 'JSON') { 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; $arguments['token_auth'] = $this->piwik_admin_token;
return $this->call_page($module, $method, $arguments, $output); return $this->call_page($module, $method, $arguments, $output);
} }