Alternc  latest
Alternc logiel libre pour l'hébergement
piwik_sitelist.php
Go to the documentation of this file.
1 <?php
2 /*
3  ----------------------------------------------------------------------
4  LICENSE
5 
6  This program is free software; you can redistribute it and/or
7  modify it under the terms of the GNU General Public License (GPL)
8  as published by the Free Software Foundation; either version 2
9  of the License, or (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  To read the license please visit http://www.gnu.org/copyleft/gpl.html
17  ----------------------------------------------------------------------
18 */
19 
20 /**
21  * Listing of piwik site, and manage associated credentials
22  * @copyright AlternC-Team 2000-2017 https://alternc.com/
23  */
24 
25 require_once("../class/config.php");
26 include_once("head.php");
27 include_once("piwik_utils.php");
28 
29 $fields = array (
30  "site_id" => array ("request", "integer", -1), // alternc ID of the piwik site
31  "right" => array ("request", "array", FALSE), // array of rights associated foreach user of $site_id
32 );
34 
35 /* Get once alternc users and sites */
36 $piwik_alternc_users = $piwik->get_alternc_users();
37 $piwik_alternc_sites = $piwik->get_alternc_sites();
38 
39 /* Does current user still has quota ? */
40 if ($quota->cancreate("piwik")) {
41  $quotapiwik=$quota->getquota('piwik');
42 
43  /* If quota are still available, display form to let user add a new site */
44  if ($quotapiwik['t']>0) {
45 
46 ?>
47 <h3><?php __("Add a new website");?></h3>
48 <?php
49 echo $msg->msg_html_all(true, true);
50 ?>
51 <form method="post" action="piwik_addsites.php" id="main" name="addsites" >
52  <?php csrf_get(); ?>
53  <input type="text" class="int" name="site_urls" size="50" id="site_name" maxlength="255" value="" placeholder="<?php __("URL of the website")?>"/>
54  <input type="submit" name="submit" class="inb" value="<?php __("Create"); ?>" />
55 </form>
56 
57 <br/>
58 <hr/>
59 <?php
60  } // quotapiwik > 0
61 } else {
62  $msg->raise("INFO", "piwik", _("You cannot add any new Piwik sites, your quota is over."));
63 } // cancreate piwik
64 
65 
66 
67 /* In that part, we'll manage the rights associated to a selected piwik site. */
68 /* The output is the following: */
69 /* [ site [v]] */
70 /* - user1 no access () view () admin () */
71 /* - user2 no access () view () admin () */
72 /* [ submit ] */
73 
74 ?>
75 
76 <h3><?php __("Existing Piwik monitored websites"); ?></h3>
77 <?php
78 /* Get the list of piwik sites for current user */
79 $sitelist = $piwik->site_list();
80 $infos_urls = $piwik->get_users_url_infos();
81 
82 /* Form was submitted, need to deal with work to do. */
83 if ($right !== FALSE) {
84  // Should this stay here, or in the API?
85  if (!in_array($site_id, $piwik_alternc_sites))
86  $msg->raise("ERROR", "piwik", _("You don't own this piwik site!"));
87  else {
88  foreach ($sitelist as $site) {
89  if ($site->id == $site_id)
90  $domain = $site->name;
91  }
92 
93  /* Foreach row of right, extract user, and selected credential */
94  foreach ($right AS $user => $cred) {
95  /* Ensures that the user is legitimate for that user */
96  /* If not, we just break the loop, and set error message */
97  if (!in_array($user, $piwik_alternc_users)) {
98  $msg->raise("ERROR", "piwik", _('You dont own user'), $user);
99  break;
100  }
101 
102  foreach ($infos_urls[$site_id] as $v) {
103  if ($v['login'] == $user && $v['cred'] != $cred) {
104  /* Ok, current user has right to manage this piwik user. Update rights. */
105  if (!$piwik->site_set_user_right($site_id, $user, $cred)) {
106  break;
107  } else {
108  $msg->raise("INFO", "piwik", _("Account '%s' has been given '%s' rights on '%s'"), array($user, $cred, $domain));
109  }
110  }
111  }
112  }
113 
114  $infos_urls = $piwik->get_users_url_infos();
115  }
116 }
117 
118 echo $msg->msg_html_all();
119 
120 /* If user didn't add a website, just do nothing but display there's no site */
121 if (empty($sitelist)){
122  __("No existing Piwik websites");
123 } else {
124 /* Otherwize, display the html form, [ sitename, url, javascript code ] */
125 ?>
126 
127 <table class="tlist">
128  <tr><th/><th><?php __("Site name");?></th><th align=center><?php __("Site url"); ?></th><th>Javascript Code</th><th>Interface</th></tr>
129 <?php
130 
132 foreach ($sitelist as $site ){
133  $col=3-$col;
134 
135  $no_user=true;
136  $first_url="";
137  $list_users="<select name='list_users_".$site->id."' id='list_users_".$site->id."' onChange='change_url(".$site->id.", this);'>";
138  foreach ($infos_urls[$site->id] as $v) {
139  if ($v['cred'] != 'noaccess') {
140  $list_users .= "<option value='".$piwik->url()."?module=Login&action=logme&login=".$v['login']."&idSite=".$site->id."&password=".$v['password']."'>".$v['login']."</option>";
141  if ($no_user)
142  $first_url = $piwik->url()."?module=Login&action=logme&login=".$v['login']."&idSite=".$site->id."&password=".$v['password'];
143 
144  $no_user=false;
145  }
146  }
147  $list_users .= "</select>";
148 
149  ?>
150  <tr class="lst_clic<?php echo $col; ?>">
151  <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>
152  <td><?php echo $site->name ?></td>
153  <td><?php echo $site->main_url ?></td>
154  <td><textarea><?php echo $piwik->site_js_tag($site->id); ?></textarea></td>
155  <td>
156  <div class="ina">
157  <?php
158  if (! $no_user) {
159  ?>
160  <a id="connect_<?php echo $site->id; ?>" href="<?php echo $first_url; ?>" target="_blank"><?php __('Connect as'); ?></a>
161  <?php
162  echo $list_users;
163  } else {
164  ?>
165  <img src="images/warning.png" onmouseover='$("#alert_div_msg").show();' onmouseout='$("#alert_div_msg").hide();'>
166  <?php } ?>
167  </div>
168  </td>
169  </tr>
170  <?php
171 } // foreach sitelist
172 
173 
174 
175 /* We'll now manage credentials for piwik sites */
176 /* We first create a select item to choose the piwik site to administrate */
177 /* Then we display a list of users, and associated rights. */
178 /* To achieve this, we select all piwik users available for current alternc user */
179 /* If a piwik user has no rights on that site, its rights are set to "noaccess" */
180 ?>
181 </table>
182 
183 <h3><?php __("Credentials management"); ?></h3>
184 
185 <form method="get" action="piwik_sitelist.php">
186 <select name="site_id">
187 
188 <?php
189  foreach ($sitelist as $site)
190  printf ('<option value="%d"%s>%s</option>', $site->id, ($site->id == $site_id) ? ' selected ' : '', $site->name);
191 ?>
192 
193 </select>&nbsp;
194 <input type="submit" class="inb" value="ok" />
195 </form>
196 
197 <?php
198  // If a site was selected
199  if ($site_id != -1 && in_array($site_id, $piwik_alternc_sites)) {
200  echo '<form method="post">';
201  csrf_get();
202  echo '<dl>';
203  foreach ($piwik->get_users_access_from_site($site_id) AS $piwik_user => $cred) {
204  printf("<dt>%s:</dt>\n\t<dd>%s</dd>\n", $piwik_user, piwik_right_widget('right', $piwik_user, $cred));
205  }
206  echo '</dl>';
207  echo '<input type="submit" name="valid" class="inb" value="' , _("submit"), '" />';
208  echo '</form>';
209  }
210 } // empty userlist
211 ?>
212 <div class="ina" id="alert_div_msg" style="display:none;background-color:yellow;padding:5px;border:2px solid black;margin-top:3em;";>
213  <?php __("No user may access this site") ?>
214 </div>
215 
216 <script type="text/javascript">
217  function change_url(id, elt) {
218  document.getElementById("connect_" + id).href = elt.value;
219  }
220 </script>
221 
222 <?php include_once("foot.php"); ?>
$msg
Definition: bootstrap.php:75
$domain
Definition: dom_import.php:36
__($str)
Definition: functions.php:404
getFields($fields, $requestOnly=false)
Get the Fields of the posted form from $_REQUEST or POST or GET and check their type.
Definition: functions.php:688
csrf_get($return=false)
Give a new CSRF uniq token for a form the session must be up since the CSRF is linked to the session ...
Definition: functions.php:1159
$user
Definition: bootstrap.php:84
$quotapiwik
$sitelist
$infos_urls
$piwik_alternc_sites
$piwik_alternc_users
$fields
Listing of piwik site, and manage associated credentials.
piwik_right_widget($name, $subname, $cred)
Definition: piwik_utils.php:5