From 9f6e82764c5d2c497dd79f4c8cffe2b427ce8a04 Mon Sep 17 00:00:00 2001 From: Alan Garcia Date: Sat, 25 Aug 2012 08:45:04 +0000 Subject: [PATCH] Piwik by Fser --- .gitattributes | 7 + bureau/admin/menu_piwik.php | 38 ++++ bureau/admin/piwik_addaccount.php | 70 ++++++ bureau/admin/piwik_addsites.php | 50 +++++ bureau/admin/piwik_sitelist.php | 84 +++++++ bureau/admin/piwik_user_dodel.php | 72 ++++++ bureau/admin/piwik_userlist.php | 97 ++++++++ bureau/class/m_piwik.php | 361 ++++++++++++++++++++++++++++++ 8 files changed, 779 insertions(+) create mode 100644 bureau/admin/menu_piwik.php create mode 100644 bureau/admin/piwik_addaccount.php create mode 100644 bureau/admin/piwik_addsites.php create mode 100644 bureau/admin/piwik_sitelist.php create mode 100644 bureau/admin/piwik_user_dodel.php create mode 100644 bureau/admin/piwik_userlist.php create mode 100644 bureau/class/m_piwik.php diff --git a/.gitattributes b/.gitattributes index 9514b22c..86c668eb 100644 --- a/.gitattributes +++ b/.gitattributes @@ -281,12 +281,18 @@ bureau/admin/menu_lang.php -text bureau/admin/menu_logs.php -text bureau/admin/menu_mail.php -text bureau/admin/menu_mem.php -text +bureau/admin/menu_piwik.php -text bureau/admin/menu_quota.php -text bureau/admin/menu_sql.php -text bureau/admin/menu_sta2.php -text bureau/admin/menu_web.php -text bureau/admin/mxlist.php -text bureau/admin/phpinfo.php -text +bureau/admin/piwik_addaccount.php -text +bureau/admin/piwik_addsites.php -text +bureau/admin/piwik_sitelist.php -text +bureau/admin/piwik_user_dodel.php -text +bureau/admin/piwik_userlist.php -text bureau/admin/powered2.jpg -text bureau/admin/quota_show.php -text bureau/admin/quotas_oneuser.php -text @@ -351,6 +357,7 @@ bureau/class/m_mail_redirection.php -text bureau/class/m_mail_squirrelmail.php -text bureau/class/m_mem.php -text bureau/class/m_mysql.php -text +bureau/class/m_piwik.php -text bureau/class/m_quota.php -text bureau/class/m_sta2.php -text bureau/class/m_trash.php -text diff --git a/bureau/admin/menu_piwik.php b/bureau/admin/menu_piwik.php new file mode 100644 index 00000000..a900a472 --- /dev/null +++ b/bureau/admin/menu_piwik.php @@ -0,0 +1,38 @@ +getquota("piwik"); +if (!empty($piwik->piwik_server_uri) && $q["t"] > 0 && $r["u"] > 0) { +?> + + diff --git a/bureau/admin/piwik_addaccount.php b/bureau/admin/piwik_addaccount.php new file mode 100644 index 00000000..d92111a4 --- /dev/null +++ b/bureau/admin/piwik_addaccount.php @@ -0,0 +1,70 @@ +cancreate("piwik")) { + $error=_("You cannot add any new Piwik account, your quota is over."); + $fatal=1; +} + +$fields = array ( + "account_name" => array ("post", "string", ""), +); +getFields($fields); + +if (empty($account_name)) { + echo "

"._("Error : missing arguments.")."

"; + include_once("foot.php"); + exit; +} + +?> +

+
+
+user_add($account_name); +if (!$infos) +{ + $error = $err->errstr(); + + //if (isset($error) && $error) { + echo "

$error

"; + if (isset($fatal) && $fatal) { + include_once("foot.php"); + exit(); + } +} + +printVar($infos); + +include_once("foot.php"); +?> diff --git a/bureau/admin/piwik_addsites.php b/bureau/admin/piwik_addsites.php new file mode 100644 index 00000000..24dba52f --- /dev/null +++ b/bureau/admin/piwik_addsites.php @@ -0,0 +1,50 @@ + array ("post", "string", ""), + "site_urls" => array ("post", "string", ""), +); +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 during adding website.
".$err->errstr()); +} else { + $error=_("Successfully add website"); +} +include_once("piwik_sitelist.php"); + +?> diff --git a/bureau/admin/piwik_sitelist.php b/bureau/admin/piwik_sitelist.php new file mode 100644 index 00000000..480216ba --- /dev/null +++ b/bureau/admin/piwik_sitelist.php @@ -0,0 +1,84 @@ +$error

"; +} + +if ($quota->cancreate("piwik")) { + $quotapiwik=$quota->getquota('piwik'); + + if ($quotapiwik['u']>0) { +?> +

+
+ "/> + " /> +
+ +
+
+ 0 +} // cancreate piwik +?> + +

+site_list(); + +if (empty($sitelist)){ + __("No existing Piwik users"); +} else { +?> + + + + + + + + + + + +
name ?>main_url ?>
+ diff --git a/bureau/admin/piwik_user_dodel.php b/bureau/admin/piwik_user_dodel.php new file mode 100644 index 00000000..dd3ec5af --- /dev/null +++ b/bureau/admin/piwik_user_dodel.php @@ -0,0 +1,72 @@ + array ("post", "string", ""), + "login" => array ("request", "string", ""), +); +getFields($fields); + +if (empty($login)) { + $error=_("Missing login parameters"); + include('piwik_userlist.php'); + exit; + +} + +if(!empty($confirm_del) ) { + if (! $piwik->user_delete($login) ) { + $error=$err->errstr(); + include('piwik_userlist.php'); + exit; + } +} else { + include_once('head.php'); +?> +

+
+
+ +
+
+ +
+ + " /> + " onclick="document.location='piwik_userlist.php'" /> +
+ + diff --git a/bureau/admin/piwik_userlist.php b/bureau/admin/piwik_userlist.php new file mode 100644 index 00000000..5a5df304 --- /dev/null +++ b/bureau/admin/piwik_userlist.php @@ -0,0 +1,97 @@ +$error

"; +} + +//Mail creation. +if ($quota->cancreate("piwik")) { + $quotapiwik=$quota->getquota('piwik'); +?> +

+
+ "/> + " /> +
+ +
+
+0) { +?> +

+
+ "/> + " /> +
+ +
+
+ + +

+users_list(); + +// printVar($piwik->dev()); + +if (empty($userslist)){ + __("No existing Piwik users"); +} else { +?> + + + + + + + + + + + +
login ?>
+ diff --git a/bureau/class/m_piwik.php b/bureau/class/m_piwik.php new file mode 100644 index 00000000..8de3b4fc --- /dev/null +++ b/bureau/class/m_piwik.php @@ -0,0 +1,361 @@ +piwik_server_uri=variable_get('piwik_server_uri',null); + if (is_null($this->piwik_server_uri)) { // if not configuration var, setup one (with a default value) + variable_set('piwik_server_uri','','Remote Piwik server uri'); + $this->piwik_server_uri=''; + } + + $this->piwik_admin_token=variable_get('piwik_admin_token',null); + if (is_null($this->piwik_admin_token)) { // if not configuration var, setup one (with a default value) + variable_set('piwik_admin_token','','Remote Piwik super-admin token'); + $this->piwik_admin_token=''; + } + + } + + /** + * Quota name + */ + function alternc_quota_names() { + return "piwik"; + } + + + /* ----------------------------------------------------------------- */ + /** Fonction appellée par membres quand un membre est effacé. + * @param integer $uid Numéro de membre effacé. + * @access private + */ + function alternc_del_member() { + //FIXME + return true; + } + + /* ----------------------------------------------------------------- */ + /** + * Returns the used quota for the $name service for the current user. + * @param $name string name of the quota + * @return integer the number of service used or false if an error occured + * @access private + */ + function alternc_get_quota() { + global $db, $cuid; + $db->query("SELECT COUNT(id) AS nb FROM piwik_users WHERE uid='$cuid'"); + $db->next_record(); + + return $db->f('nb'); + } + + function url() + { + return $this->piwik_server_uri; + } + + + /***********************/ + /* Users related tasks */ + /***********************/ + + + // Créé un utilisateur + // Si échoue en remote, n'enregistre rien + function user_add($user_login, $user_mail = null) { + + global $db, $mem, $cuid, $err; + + $user_login = $this->clean_user_name($user_login); + $user_pass = create_pass(); + $user_mail = $user_mail ? $user_mail : $mem->user['mail']; + $user_mail = create_pass(4) . $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'); + if ($api_data) + { + if ($api_data->result === 'success') + { + $user = $this->get_user($user_login); + $user_creation_date = $user->date_registered; + $db->query("INSERT INTO piwik_users (uid, login, created_date) VALUES ('$cuid', '$user_login', '$user_creation_date')"); + } + } + else // api_data = false -> error is already filled + { + return FALSE; + } + + } + + + // Edite un user + function user_edit() { + //FIXME + return true; + } + + function get_user($user_login) + { + $api_data = $this->call_privileged_page('API', 'UsersManager.getUser', array('userLogin' => $user_login)); + + if ($api_data) + return $api_data[0]; + else + 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; + + $db->query("SELECT created_date, COUNT(id) AS cnt FROM piwik_users WHERE uid='$cuid' AND login='$piwik_user_login'"); + $db->next_record(); + + if ($db->f('cnt') == 1) + { + + $api_data = $this->call_privileged_page('API', 'UsersManager.getUser', array('userLogin' => $piwik_user_login)); + printvar($api_data); + if ($api_data[0]->date_registered == $db->f('created_date')) + echo "equals"; + else + echo "non equals"; + // $api_data = $this->call_privileged_page('API', 'UsersManager.deleteUser', array('idSite' => $site_id)); + + + } + else + { + $err->raise("piwik", _("You are not allowed to delete the statistics of this website")); + return FALSE; + } + + //SitesManager.deleteSite (idSite) + //FIXME + return true; + } + + + function users_list() { + global $db, $cuid; + + $db->query("SELECT login FROM piwik_users WHERE uid = '$cuid'"); + + if ($db->num_rows() == 0) + return array(); + + $users = ''; + while ($db->next_record()) + $users .= ($users !== '') ? ',' . $db->f('login') : $db->f('login'); + + return $this->call_privileged_page('API', 'UsersManager.getUsers', array('userLogins' => $users)); + } + + + + + // Verifie que l'utilisateur existe bien dans piwik + function user_checkremote($puser_id) { + //FIXME + return true; + } + + + // Récupére un token pour le SSO avec piwik pour l'user + function user_remoteauth() { + //FIXME + return true; + } + + // Montre la liste des site pour lesques un user à accés + function user_access() { + // FIXME + return true; + } + + + + + + + + /***********************/ + /* Sites related tasks */ + /***********************/ + + + function site_list() + { + $api_data = $this->call_privileged_page('API', 'SitesManager.getAllSites'); + $data = array(); + + if($api_data) + { + foreach ($api_data AS $site) + { + + $item = new stdClass(); + + $item->id = $site->idsite; + $item->name = $site->name; + $item->main_url = $site->main_url; + + $user_data = $this->call_privileged_page('API', 'UsersManager.getUsersAccessFromSite', array('idSite' => $site->idsite)); + + if (is_array($user_data)) + { + printvar($user_data); + } + else if(is_object($user_data)) + { + $item->rights = json_decode($user_data[0]); + } + + + + $data[] = $item; + } + + return $data; + } + else + return FALSE; + + } + // Ajoute un site à Piwik + // can't figure out how to pass multiple url through the API + function site_add($siteName, $urls, $ecommerce = FALSE) { + $urls = is_array($urls) ? implode(',', $urls) : $urls; + $api_data = $this->call_privileged_page('API', 'SitesManager.addSite', array('siteName' => $siteName, 'urls' => $urls)); + printvar($api_data); + + return TRUE; + } + + // Supprime un site de Piwik + function site_delete($site_id) { + global $db, $cuid, $err; + + $db->query("SELECT COUNT(id) AS cnt FROM piwik_sites WHERE uid='$cuid' AND piwik_id='$site_id'"); + $db->next_record(); + + if ($db->f('cnt') == 1) + { + $api_data = $this->call_privileged_page('API', 'SitesManager.deleteSite', array('idSite' => $site_id)); + printvar($api_data); + + } + else + { + $err->raise("piwik", _("You are not allowed to delete the statistics of this website")); + return FALSE; + } + + //SitesManager.deleteSite (idSite) + //FIXME + return true; + } + + // Ajoute un alias sur un site existant + function site_alias_add() { + // FIXME + return true; + } + + + + /* Helper code */ + + function clean_user_name($username) + { + return mysql_real_escape_string(trim($username)); + } + + function dev() + { + // $this->call_page('module', 'method', array('user' => 'fser', 'pass' => 'toto')); + // return $this->users_list(); + } + + function call_page($module, $method, $arguments=array(), $output = 'JSON') + { + global $err; + $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)); + + // We are supposed to chose what's enabled on our php instance :-) + // if (! ini_get('allow_url_fopen')==True) { + // $err->raise("piwik",2,"PHP var allow_url_fopen is not allowed"); + //} + echo $url; + $page_content = file_get_contents($url); + if ($page_content === FALSE) + { + $err->raise("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")); + return FALSE; + } + + return $api_data; + } + else + { + $err->raise("piwik", _("Other format than JSON is not implemented yet")); + return FALSE; + } + + } + + + function call_privileged_page($module, $method, $arguments=array(), $output = 'JSON') + { + $arguments['token_auth'] = $this->piwik_admin_token; + + return $this->call_page($module, $method, $arguments, $output); + } + +} /* Class piwik */ + +?>