Platform  3.1
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
download.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
5  * @package FDL
6 */
7 /**
8  * Generated Header (not documented yet)
9  *
10  * @author Anakeen 2000
11  * @version $Id: download.php,v 1.5 2003/08/18 15:46:41 eric Exp $
12  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
13  * @package FDL
14  * @subpackage ACCESS
15  */
16 /**
17  */
18 // ---------------------------------------------------------------
19 // $Id: download.php,v 1.5 2003/08/18 15:46:41 eric Exp $
20 // $Source: /home/cvsroot/anakeen/freedom/core/Action/Access/download.php,v $
21 // ---------------------------------------------------------------
22 include_once ("Class.QueryDb.php");
23 include_once ("Class.Application.php");
24 include_once ("Class.User.php");
25 include_once ("Class.Acl.php");
26 include_once ("Class.Permission.php");
27 include_once ("Class.Domain.php");
28 include_once ("Lib.Http.php");
29 include_once ("FDL/freedom_util.php");
30 // -----------------------------------
31 function download(&$action)
32 {
33  // -----------------------------------
34  $dbaccess_freedom = $action->getParam('FREEDOM_DB');
35  $dbaccess_core = $action->getParam('CORE_DB');
36 
37  $cache = array(
38  'app' => array() ,
39  'acl' => array() ,
40  'user' => array()
41  );
42 
43  $q = new QueryDb($dbaccess_core, "permission");
44  $aclList = $q->query(0, 0, "TABLE", sprintf("SELECT id_user, id_application, id_acl FROM permission WHERE computed IS NULL OR computed = FALSE;"));
45 
46  $aclExport = array();
47  foreach ($aclList as $k => & $el) {
48  $app_name = getApplicationNameFromId($dbaccess_core, $el['id_application'], $cache);
49  if ($app_name === null) {
50  error_log(__CLASS__ . "::" . __FUNCTION__ . " " . sprintf("Unknown name for application with id '%s'", $el['id_application']));
51  continue;
52  }
53 
54  $acl_name = getAclNameFromId($dbaccess_core, $el['id_acl'], $cache);
55  if ($acl_name === null) {
56  error_log(__CLASS__ . "::" . __FUNCTION__ . " " . sprintf("Uknown name for acl with id '%s'", $el['id_acl']));
57  continue;
58  }
59  if ($el['id_acl'] < 0) {
60  $acl_name = sprintf("-%s", $acl_name);
61  }
62  // Try to fetch the logical name of id_user
63  $user_fid = getUserFIDFromWID($dbaccess_core, $el['id_user'], $cache);
64  if ($user_fid === null) {
65  error_log(__CLASS__ . "::" . __FUNCTION__ . " " . sprintf("Unknown fid for user with wid '%s'", $el['id_user']));
66  continue;
67  }
68  $user_name = getNameFromId($dbaccess_freedom, $user_fid);
69  // If there is no logical name, then keep the core id (id_user)
70  if ($user_name == "") {
71  $user_name = $el['id_user'];
72  }
73 
74  array_push($aclExport, array(
75  'fid' => $user_name,
76  'app_name' => $app_name,
77  'acl_name' => $acl_name
78  ));
79  }
80 
81  $action->lay->setBlockData("ACCESS", $aclExport);
82 
83  $tmpfile = tempnam(getTmpDir() , "access");
84  if ($tmpfile === false) {
85  $err = sprintf("Could not create temporary file!");
86  error_log(__CLASS__ . "::" . __FUNCTION__ . " " . $err);
87  return $err;
88  }
89 
90  @unlink($tmpfile);
91  $fp = @fopen($tmpfile, 'x');
92  if ($fp === false) {
93  $err = sprintf("Error opening temporary file '%s'", $tmpfile);
94  error_log(__CLASS__ . "::" . __FUNCTION__ . " " . $err);
95  return $err;
96  }
97 
98  $content = $action->lay->gen();
99 
100  $ret = @fwrite($fp, $content);
101  if ($ret === false) {
102  $err = sprintf("Error writing to temporary file '%s'", $tmpfile);
103  error_log(__CLASS__ . "::" . __FUNCTION__ . " " . $err);
104  @fclose($fp);
105  @unlink($tmpfile);
106  return $err;
107  }
108  @fclose($fp);
109 
110  return Http_DownloadFile($tmpfile, "access.csv", "text/csv",
111  /*inline*/
112  false, /*cache*/
113  false, /*deleteafter*/
114  true);
115 }
116 
117 function getApplicationNameFromId($dbaccess, $id, &$cache = null)
118 {
119  if (is_array($cache) && array_key_exists('app', $cache)) {
120  if (array_key_exists($id, $cache['app'])) {
121  return $cache['app'][$id];
122  }
123  }
124 
125  $query = new QueryDb($dbaccess, "application");
126  $query->addQuery(sprintf("id = %s", pg_escape_string($id)));
127  $res = $query->query(0, 0, "TABLE");
128  if (!is_array($res)) {
129  return null;
130  }
131 
132  $name = $res[0]['name'];
133  if (is_array($cache) && array_key_exists('app', $cache)) {
134  $cache['app'][$id] = $name;
135  }
136 
137  return $name;
138 }
139 
140 function getAclNameFromId($dbaccess, $id, &$cache = null)
141 {
142  if (is_array($cache) && array_key_exists('acl', $cache)) {
143  if (array_key_exists($id, $cache['acl'])) {
144  return $cache['acl'][$id];
145  }
146  }
147 
148  $query = new QueryDb($dbaccess, "acl");
149  $query->addQuery(sprintf("id = %s", pg_escape_string(abs($id))));
150  $res = $query->query(0, 0, "TABLE");
151  if (!is_array($res)) {
152  return null;
153  }
154 
155  $name = $res[0]['name'];
156  if (is_array($cache) && array_key_exists('acl', $cache)) {
157  $cache['acl'][$id] = $name;
158  }
159 
160  return $name;
161 }
162 
163 function getUserFIDFromWID($dbaccess, $wid, &$cache)
164 {
165  if (is_array($cache) && array_key_exists('user_fid', $cache)) {
166  if (array_key_exists($id, $cache['user_fid'])) {
167  return $cache['user_fid'][$id];
168  }
169  }
170 
171  $query = new QueryDb($dbaccess, "user");
172  $query->addQuery(sprintf("id = %s", pg_escape_string($wid)));
173  $res = $query->query(0, 0, "TABLE");
174  if (!is_array($res)) {
175  return null;
176  }
177 
178  $fid = $res[0]['fid'];
179  if (is_array($cache) && array_key_exists('user_fid', $cache)) {
180  $cache['user_fid'][$wid] = $fid;
181  }
182 
183  return $fid;
184 }
185 ?>
← centre documentaire © anakeen - published under CC License - Dynacase