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=null)
 
 user_edit ()
 
 get_site_access ($user_login)
 
 get_users_access_from_site ($site_id)
 
 get_user ($user_login)
 
 get_alternc_users ()
 
 user_delete ($piwik_user_login)
 
 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)
 
 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 29 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 364 of file m_piwik.php.

References $err.

Referenced by call_privileged_page().

364  {
365  global $err;
366  $url = sprintf('%s/?module=%s&method=%s&format=%s', $this->piwik_server_uri, $module, $method, $output);
367  foreach ($arguments AS $k=>$v)
368  $url .= sprintf('&%s=%s', urlencode($k), $v); // urlencode($v));
369 
370  $page_content = file_get_contents($url);
371  if ($page_content === FALSE) {
372  $err->raise("piwik", _("Unable to reach the API"));
373  return FALSE;
374  }
375  if ($output == 'JSON') {
376  $api_data = json_decode($page_content);
377  if ($api_data === FALSE) {
378  $err->raise("piwik", _("Error while decoding response from the API"));
379  return FALSE;
380  }
381 
382  return $api_data;
383  } else {
384  $err->raise("piwik", _("Other format than JSON is not implemented yet"));
385  return FALSE;
386  }
387  }
$err
Definition: bootstrap.php:72
m_piwik::call_privileged_page (   $module,
  $method,
  $arguments = array(),
  $output = 'JSON' 
)
Parameters
string$module
string$method

Definition at line 394 of file m_piwik.php.

References $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().

394  {
395  $arguments['token_auth'] = $this->piwik_admin_token;
396  return $this->call_page($module, $method, $arguments, $output);
397  }
$piwik_admin_token
Definition: m_piwik.php:31
call_page($module, $method, $arguments=array(), $output= 'JSON')
Definition: m_piwik.php:364
m_piwik::clean_user_name (   $username)

Definition at line 348 of file m_piwik.php.

References $cuid, and $username.

Referenced by user_add().

348  {
349  global $admin, $cuid;
350  return 'alternc_' . $admin->get_login_by_uid($cuid) . '_' . mysql_real_escape_string(trim($username));
351  }
$cuid
Definition: bootstrap.php:43
global $username
Definition: change.php:59
m_piwik::dev ( )

Definition at line 354 of file m_piwik.php.

354  {
355  // $this->call_page('module', 'method', array('user' => 'fser', 'pass' => 'toto'));
356  // return $this->users_list();
357  }
m_piwik::get_alternc_sites ( )

Definition at line 276 of file m_piwik.php.

References $alternc_sites, $cuid, and $db.

Referenced by m_piwik().

276  {
277  global $db, $cuid;
278 
279  static $alternc_sites = array();
280  $db->query("SELECT piwik_id AS site_id FROM piwik_sites WHERE uid= ? ;", array($cuid));
281  while ($db->next_record())
282  array_push($alternc_sites, $db->f('site_id'));
283 
284  return $alternc_sites;
285  }
global $db
Definition: bootstrap.php:22
$alternc_sites
Definition: m_piwik.php:33
$cuid
Definition: bootstrap.php:43
m_piwik::get_alternc_users ( )

Definition at line 168 of file m_piwik.php.

References $alternc_users, $cuid, and $db.

Referenced by m_piwik().

168  {
169  global $db, $cuid;
170 
171  static $alternc_users = array();
172  $db->query("SELECT login FROM piwik_users WHERE uid= ?;", array($cuid));
173  while ($db->next_record())
174  array_push($alternc_users, $db->f('login'));
175 
176  return $alternc_users;
177  }
$alternc_users
Definition: m_piwik.php:32
global $db
Definition: bootstrap.php:22
$cuid
Definition: bootstrap.php:43
m_piwik::get_site_access (   $user_login)

Definition at line 127 of file m_piwik.php.

References call_privileged_page().

127  {
128  return $this->call_privileged_page('API', 'UsersManager.getSitesAccessFromUser', array('userLogin' => $user_login));
129  }
call_privileged_page($module, $method, $arguments=array(), $output= 'JSON')
Definition: m_piwik.php:394
m_piwik::get_site_list ( )

Definition at line 287 of file m_piwik.php.

References call_privileged_page().

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

Definition at line 159 of file m_piwik.php.

References call_privileged_page().

Referenced by user_add().

159  {
160  $api_data = $this->call_privileged_page('API', 'UsersManager.getUser', array('userLogin' => $user_login));
161 
162  if ($api_data)
163  return $api_data[0];
164  else
165  return FALSE;
166  }
call_privileged_page($module, $method, $arguments=array(), $output= 'JSON')
Definition: m_piwik.php:394
m_piwik::get_users_access_from_site (   $site_id)

Definition at line 131 of file m_piwik.php.

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

131  {
132  global $err, $cuid;
133 
134  if (!is_numeric($site_id)) {
135  $err->raise('piwik', 'site_id must be numeric');
136  return FALSE;
137  }
138  if (!in_array($site_id, $this->alternc_sites)) {
139  $err->raise('piwik', "you don't own this piwik website");
140  return FALSE;
141  }
142 
143  $api_data = $this->call_privileged_page('API', 'UsersManager.getUsersAccessFromSite', array('idSite' => $site_id));
144  if ($api_data !== FALSE) {
145  $api_data = $api_data[0]; // Data is in the first column
146  foreach ($this->alternc_users AS $key=>$user) {
147  if (!array_key_exists($user, $api_data)) {
148  $api_data->$user = 'noaccess';
149  }
150  }
151  return $api_data;
152  }
153  else return FALSE;
154  }
$user
Definition: bootstrap.php:84
call_privileged_page($module, $method, $arguments=array(), $output= 'JSON')
Definition: m_piwik.php:394
$err
Definition: bootstrap.php:72
$cuid
Definition: bootstrap.php:43
if(empty($_POST['key'])||empty($_POST['val'])) $key
Definition: tempovars.php:14
m_piwik::hook_admin_del_member ( )

hook called when an AlternC account is deleted

Definition at line 66 of file m_piwik.php.

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

Definition at line 35 of file m_piwik.php.

35  {
36  global $quota;
37  if ( empty($this->piwik_server_uri) || empty($this->piwik_admin_token)) return false;
38 
39  $obj = array(
40  'title' => _("Piwik statistics"),
41  'ico' => 'images/piwik.png',
42  'link' => 'toggle',
43  'pos' => 115,
44  'links' => array(
45  array( 'txt' => _("Piwik Users"), 'url' => 'piwik_userlist.php'),
46  array( 'txt' => _("Piwik Sites"), 'url' => 'piwik_sitelist.php'),
47  ),
48  ) ;
49 
50  return $obj;
51  }
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 78 of file m_piwik.php.

References $cuid, $db, and $q.

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

Constructor.

Definition at line 56 of file m_piwik.php.

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

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

Definition at line 293 of file m_piwik.php.

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

293  {
294  global $db, $cuid;
295  $urls = is_array($urls) ? implode(',', $urls) : $urls;
296  $api_data = $this->call_privileged_page('API', 'SitesManager.addSite', array('siteName' => $siteName, 'urls' => $urls));
297  $db->query("INSERT INTO piwik_sites set uid= ? , piwik_id= ? ", array($cuid, $api_data->value));
298  return TRUE;
299  }
call_privileged_page($module, $method, $arguments=array(), $output= 'JSON')
Definition: m_piwik.php:394
global $db
Definition: bootstrap.php:22
$cuid
Definition: bootstrap.php:43
m_piwik::site_alias_add ( )

Definition at line 340 of file m_piwik.php.

340  {
341  // FIXME
342  return true;
343  }
m_piwik::site_delete (   $site_id)

Definition at line 304 of file m_piwik.php.

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

304  {
305  global $db, $cuid, $err;
306 
307  $db->query("SELECT COUNT(id) AS cnt FROM piwik_sites WHERE uid= ? AND piwik_id= ? ;", array($cuid, $site_id));
308  $db->next_record();
309 
310  if ($db->f('cnt') == 1) {
311  $api_data = $this->call_privileged_page('API', 'SitesManager.deleteSite', array('idSite' => $site_id));
312  if ($api_data->result == 'success') {
313  return $db->query("DELETE FROM piwik_sites where uid= ? AND piwik_id= ? LIMIT 1", array($cuid, $site_id));
314  } else {
315  return FALSE;
316  }
317  } else {
318  $err->raise("piwik", _("You are not allowed to delete the statistics of this website"));
319  return FALSE;
320  }
321 
322  return true;
323  }
call_privileged_page($module, $method, $arguments=array(), $output= 'JSON')
Definition: m_piwik.php:394
global $db
Definition: bootstrap.php:22
$err
Definition: bootstrap.php:72
$cuid
Definition: bootstrap.php:43
m_piwik::site_js_tag (   $site_id)

Definition at line 272 of file m_piwik.php.

References call_privileged_page().

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

Definition at line 241 of file m_piwik.php.

References call_privileged_page().

241  {
242  $api_data = $this->call_privileged_page('API', 'SitesManager.getAllSites');
243  $data = array();
244 
245  if($api_data) {
246  foreach ($api_data AS $site) {
247 
248  if (!in_array($site->idsite, $this->alternc_sites))
249  continue;
250 
251  $item = new stdClass();
252 
253  $item->id = $site->idsite;
254  $item->name = $site->name;
255  $item->main_url = $site->main_url;
256 
257  $user_data = $this->call_privileged_page('API', 'UsersManager.getUsersAccessFromSite', array('idSite' => $site->idsite));
258 
259  //if (is_array($user_data)) {
260  // printvar($user_data);
261  //} else if(is_object($user_data)) {
262  $item->rights = $user_data[0];
263  //}
264 
265  $data[] = $item;
266  }
267  return $data;
268  } else
269  return FALSE;
270  }
call_privileged_page($module, $method, $arguments=array(), $output= 'JSON')
Definition: m_piwik.php:394
m_piwik::site_set_user_right (   $site_id,
  $login,
  $right 
)

Definition at line 326 of file m_piwik.php.

References $err, and call_privileged_page().

327  {
328  global $err;
329  if (!in_array($right, array('noaccess', 'view', 'admin')))
330  return FALSE;
331  $api_data = $this->call_privileged_page('API', 'UsersManager.setUserAccess', array('userLogin' => $login, 'access' => $right, 'idSites' => $site_id));
332  if ($api_data->result == 'success') {
333  return TRUE;
334  } else {
335  $err->raise('piwik', $api_data->messsage);
336  return FALSE;
337  }
338  }
call_privileged_page($module, $method, $arguments=array(), $output= 'JSON')
Definition: m_piwik.php:394
$err
Definition: bootstrap.php:72
m_piwik::url ( )

Definition at line 89 of file m_piwik.php.

References $piwik_server_uri.

89  {
91  }
$piwik_server_uri
Definition: m_piwik.php:30
m_piwik::user_access ( )

Definition at line 227 of file m_piwik.php.

227  {
228  // FIXME
229  return true;
230  }
m_piwik::user_add (   $user_login,
  $user_mail = null 
)

Definition at line 99 of file m_piwik.php.

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

99  {
100 
101  global $db, $mem, $cuid, $err;
102 
103  $user_login = $this->clean_user_name($user_login);
104  $user_pass = create_pass();
105  $user_mail = $mem->user['mail'];
106  $user_alias = $user_login;
107 
108  $api_data = $this->call_privileged_page('API', 'UsersManager.addUser', array('userLogin' => $user_login, 'password' => $user_pass, 'email' => $user_mail, 'alias' => $user_alias), 'JSON');
109  if ($api_data) {
110  if ($api_data->result === 'success') {
111  $user = $this->get_user($user_login);
112  $user_creation_date = $user->date_registered;
113  return $db->query("INSERT INTO piwik_users (uid, login, created_date) VALUES ( ?, ?, ?,);", array($cuid, $user_login, $user_creation_date));
114  }
115  } else { // api_data = false -> error is already filled
116  return FALSE;
117  }
118  }
$user
Definition: bootstrap.php:84
call_privileged_page($module, $method, $arguments=array(), $output= 'JSON')
Definition: m_piwik.php:394
global $db
Definition: bootstrap.php:22
get_user($user_login)
Definition: m_piwik.php:159
$err
Definition: bootstrap.php:72
$cuid
Definition: bootstrap.php:43
create_pass($length=8)
Definition: functions.php:853
clean_user_name($username)
Definition: m_piwik.php:348
$mem
Definition: bootstrap.php:71
m_piwik::user_checkremote (   $puser_id)

Definition at line 214 of file m_piwik.php.

214  {
215  //FIXME
216  return true;
217  }
m_piwik::user_delete (   $piwik_user_login)

Definition at line 180 of file m_piwik.php.

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

180  {
181  global $db, $cuid, $err;
182 
183  $db->query("SELECT created_date, COUNT(id) AS cnt FROM piwik_users WHERE uid= ? AND login= ? ", array($cuid, $piwik_user_login));
184  $db->next_record();
185 
186  if ($db->f('cnt') == 1) {
187  $api_data = $this->call_privileged_page('API', 'UsersManager.deleteUser', array('userLogin' => $piwik_user_login));
188  if ($api_data->result == 'success') {
189  return $db->query("DELETE FROM piwik_users WHERE uid= ? AND login= ? ;", array($cuid, $piwik_user_login));
190  }
191  else {
192  return FALSE;
193  }
194  } else {
195  $err->raise("piwik", _("You are not allowed to delete the statistics of this website"));
196  return FALSE;
197  }
198  }
call_privileged_page($module, $method, $arguments=array(), $output= 'JSON')
Definition: m_piwik.php:394
global $db
Definition: bootstrap.php:22
$err
Definition: bootstrap.php:72
$cuid
Definition: bootstrap.php:43
m_piwik::user_edit ( )

Definition at line 122 of file m_piwik.php.

122  {
123  //FIXME
124  return true;
125  }
m_piwik::user_remoteauth ( )

Definition at line 221 of file m_piwik.php.

221  {
222  //FIXME
223  return true;
224  }
m_piwik::users_list ( )

Definition at line 201 of file m_piwik.php.

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

201  {
202  global $db, $cuid;
203  $db->query("SELECT login FROM piwik_users WHERE uid = ?;", array($cuid));
204  if ($db->num_rows() == 0)
205  return array();
206  $users = '';
207  while ($db->next_record())
208  $users .= ($users !== '') ? ',' . $db->f('login') : $db->f('login');
209  return $this->call_privileged_page('API', 'UsersManager.getUsers', array('userLogins' => $users));
210  }
call_privileged_page($module, $method, $arguments=array(), $output= 'JSON')
Definition: m_piwik.php:394
global $db
Definition: bootstrap.php:22
$cuid
Definition: bootstrap.php:43

Member Data Documentation

m_piwik::$alternc_sites

Definition at line 33 of file m_piwik.php.

Referenced by get_alternc_sites().

m_piwik::$alternc_users

Definition at line 32 of file m_piwik.php.

Referenced by get_alternc_users().

m_piwik::$piwik_admin_token

Definition at line 31 of file m_piwik.php.

Referenced by call_privileged_page().

m_piwik::$piwik_server_uri

Definition at line 30 of file m_piwik.php.

Referenced by url().


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