| 101 | | header('Content-Type: application/octet-stream'); |
| 102 | | header('Content-Disposition: inline; filename="anonymised_data.sql"'); |
| 103 | | header("Content-Transfer-Encoding: binary"); |
| 104 | | |
| 105 | | $html .= <<<EOT |
| 106 | | CREATE TABLE connections_anonymised |
| 107 | | ( |
| 108 | | conn_id text NOT NULL, |
| 109 | | timestamp_in timestamp, |
| 110 | | node_id text, |
| 111 | | timestamp_out timestamp, |
| 112 | | user_id text NOT NULL DEFAULT '', |
| 113 | | user_mac text, |
| 114 | | incoming int8, |
| 115 | | outgoing int8 |
| 116 | | ); |
| | 101 | /** Starting sql file with geolocation data */ |
| | 102 | $tmpdir = sys_get_temp_dir(); |
| | 103 | $nodefile = tempnam($tmpdir, 'wd'); |
| | 104 | $nfilehndl = fopen($nodefile, 'w'); |
| | 105 | $datafile = tempnam($tmpdir, 'wd'); |
| | 106 | $datahndl = fopen($datafile, 'w'); |
| | 107 | |
| | 108 | if (!$nfilehndl || !$datahndl) { |
| | 109 | $html .= "<p class='error'>"._("Could not create files for anonymised data")."</p>"; |
| | 110 | |
| | 111 | } else { |
| | 112 | /* header('Content-Type: application/octet-stream'); |
| | 113 | header('Content-Disposition: inline; filename="anonymised_nodes.sql"'); |
| | 114 | header("Content-Transfer-Encoding: binary"); */ |
| | 115 | |
| | 116 | $text = <<<EOT |
| | 117 | CREATE TABLE nodes_anonymised |
| | 118 | ( |
| | 119 | node_id text NOT NULL, |
| | 120 | latitude NUMERIC(16, 6), |
| | 121 | longitude NUMERIC(16, 6) |
| | 122 | ); |
| 118 | | $html .= "\n"; |
| 119 | | echo $html; |
| 120 | | $distinguish_users_by = $this->stats->getDistinguishUsersBy(); |
| 121 | | |
| 122 | | $candidate_connections_sql = $this->stats->getSqlCandidateConnectionsQuery("conn_id, users.user_id, nodes.node_id, connections.user_id, user_mac, timestamp_in, timestamp_out, incoming, outgoing ", true); |
| 123 | | |
| 124 | | $sql = "$candidate_connections_sql ORDER BY timestamp_in DESC"; |
| 125 | | $db->execSqlRaw($sql, $resultHandle, false); |
| 126 | | if($resultHandle) { |
| 127 | | while($row=pg_fetch_array($resultHandle,null,PGSQL_ASSOC)) |
| 128 | | { |
| 129 | | |
| 130 | | $keys = null; |
| 131 | | $values = null; |
| 132 | | $first = true; |
| 133 | | foreach ($row as $key=>$value) |
| | 124 | $text .= "\n"; |
| | 125 | |
| | 126 | fwrite($nfilehndl, $text); |
| | 127 | |
| | 128 | $node_constraint = $this->stats->getSqlNodeConstraint('nodes.node_id'); |
| | 129 | $network_constraint = $this->stats->getSqlNetworkConstraint('nodes.network_id'); |
| | 130 | $sql = "SELECT node_id, latitude, longitude \n"; |
| | 131 | $sql .= "FROM nodes \n"; |
| | 132 | $sql .= "WHERE 1=1 {$node_constraint} {$network_constraint}"; |
| | 133 | |
| | 134 | $db->execSql($sql, $nodes); |
| | 135 | |
| | 136 | if ($nodes) { |
| | 137 | foreach($nodes as $row) { |
| | 138 | $keys = null; |
| | 139 | $values = null; |
| | 140 | $first = true; |
| | 141 | foreach ($row as $key=>$value) |
| | 142 | { |
| | 143 | if($key == 'user_id' || $key == 'node_id' || $key == 'conn_id' || $key == 'user_mac' ) { |
| | 144 | $value = "'".$this->getNonRepeatableHash($value)."'"; |
| | 145 | } |
| | 146 | else if ($key == 'latitude' && empty ($value)) { |
| | 147 | $value = 'NULL'; |
| | 148 | } |
| | 149 | else if ($key == 'longitude' && empty ($value)) { |
| | 150 | $value = 'NULL'; |
| | 151 | } |
| | 152 | else { |
| | 153 | $value = "'$value'"; |
| | 154 | } |
| | 155 | if(!$first) { |
| | 156 | $keys .= ', '; |
| | 157 | $values .= ', '; |
| | 158 | } |
| | 159 | else { |
| | 160 | $first = false; |
| | 161 | } |
| | 162 | $keys .= $key; |
| | 163 | $values .= $value; |
| | 164 | } |
| | 165 | //fwrite($temp, "INSERT INTO connections_anonymised ($keys) VALUES ($values);\n"); |
| | 166 | fwrite($nfilehndl, "INSERT INTO nodes_anonymised ($keys) VALUES ($values);\n"); |
| | 167 | } |
| | 168 | } |
| | 169 | |
| | 170 | |
| | 171 | /** End sql file with node data */ |
| | 172 | |
| | 173 | /** Get the sql file with anonymised connection data */ |
| | 174 | /* header('Content-Type: application/octet-stream'); |
| | 175 | header('Content-Disposition: inline; filename="anonymised_data.sql"'); |
| | 176 | header("Content-Transfer-Encoding: binary");*/ |
| | 177 | |
| | 178 | $text = <<<EOT |
| | 179 | CREATE TABLE connections_anonymised |
| | 180 | ( |
| | 181 | conn_id text NOT NULL, |
| | 182 | timestamp_in timestamp, |
| | 183 | node_id text, |
| | 184 | timestamp_out timestamp, |
| | 185 | user_id text NOT NULL DEFAULT '', |
| | 186 | user_mac text, |
| | 187 | incoming int8, |
| | 188 | outgoing int8 |
| | 189 | ); |
| | 190 | EOT; |
| | 191 | $text .= "\n"; |
| | 192 | |
| | 193 | fwrite($datahndl, $text); |
| | 194 | $distinguish_users_by = $this->stats->getDistinguishUsersBy(); |
| | 195 | |
| | 196 | $candidate_connections_sql = $this->stats->getSqlCandidateConnectionsQuery("conn_id, users.user_id, nodes.node_id, connections.user_id, user_mac, timestamp_in, timestamp_out, incoming, outgoing ", true); |
| | 197 | |
| | 198 | $sql = "$candidate_connections_sql ORDER BY timestamp_in DESC"; |
| | 199 | $db->execSqlRaw($sql, $resultHandle, false); |
| | 200 | if($resultHandle) { |
| | 201 | while($row=pg_fetch_array($resultHandle,null,PGSQL_ASSOC)) |