PluginProbe
The GDPR Framework By Data443 / trunk
The GDPR Framework By Data443 vtrunk
2.5.0 2.4.0 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.3 1.0.33 1.0.34 1.0.35 1.0.36 1.0.37 1.0.39 1.0.4 1.0.40 1.0.41 1.0.42 1.0.43 1.0.44 1.0.45 1.0.46 All 41 releases
gdpr-framework / src / DataSubject / DataExporter.php

DataExporter.php in The GDPR Framework By Data443 trunk, at src/DataSubject/DataExporter.php

152 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Codelight\GDPR\DataSubject;
4
5 /**
6 * Handle formatting and downloading data subject's data.
7 *
8 * Class DataManager
9 *
10 * @package Codelight\GDPR\DataSubject
11 */
12 class DataExporter
13 {
14 public function export(array $data, DataSubject $dataSubject, $format = 'html')
15 {
16 $data = $this->maybeUnserialize($data);
17
18 do_action('gdpr/export', $data, $dataSubject->getEmail(), $dataSubject, $format);
19
20 if ('html' === $format) {
21 $this->downloadHTML($data, $dataSubject);
22 } elseif ('json' === $format) {
23 $this->downloadJSON($data, $dataSubject);
24 }
25 }
26
27 /**
28 * Download a data subject's data in human-readable format,
29 * formatted as a table in an HTML document unless overridden.
30 *
31 * @param array $data
32 * @param DataSubject $dataSubject
33 */
34 protected function downloadHTML(array $data, DataSubject $dataSubject)
35 {
36 // Allow extensions to send a different response
37 do_action('gdpr/export/html', $data, $dataSubject->getEmail(), $dataSubject);
38
39 $filename = 'data_' . date("Y-m-d_H:i:s") . '.html';
40
41 // By default, send a downloadable HTML file
42 header("Pragma: public");
43 header("Expires: 0");
44 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
45 header("Cache-Control: private", false);
46 header('Content-Encoding: UTF-8');
47 header("Content-Type: text/html");
48 header("Content-Disposition: attachment; filename=\"{$filename}\";");
49 header("Content-Transfer-Encoding: binary");
50
51 echo $this->getHtmlData($data);
52 exit;
53 }
54
55 /**
56 * Download a data subject's data in machine-readable format,
57 * formatted as JSON unless overridden.
58 *
59 * @param array $data
60 * @param DataSubject $dataSubject
61 */
62 protected function downloadJSON(array $data, DataSubject $dataSubject)
63 {
64 // Allow extensions to send a different response
65 do_action('gdpr/export/json', $data, $dataSubject->getEmail(), $dataSubject);
66
67 $filename = 'data_' . date("Y-m-d_H:i:s") . '.json';
68
69 // By default, encode to JSON and send a JSON response
70 header("Pragma: public");
71 header("Expires: 0");
72 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
73 header("Cache-Control: private", false);
74 header('Content-Encoding: UTF-8');
75 header("Content-Type: application/json");
76 header("Content-Disposition: attachment; filename=\"{$filename}\";");
77 header("Content-Transfer-Encoding: binary");
78
79 wp_send_json($data);
80 }
81
82 protected function getHtmlData($data)
83 {
84 $table = $this->formatAsTable($this->maybeUnserialize($data));
85 return gdpr('view')->render('global/html-data', compact('table'));
86 }
87
88 protected function formatAsTable(array $data, $level = 0)
89 {
90 $output = "<table class='level-{$level}'>";
91 foreach ($data as $key => $value) {
92 $output .= "<tr>";
93
94 // Output key
95 $output .= "<td class='key'>";
96 $output .= esc_html($key);
97 $output .= "</td>";
98
99 // Output value
100 $output .= "<td class='value'>";
101
102 // Account for arrays with just one item, such as usermeta
103 if (is_array($value) && 1 === count($value)) {
104 $tmp = '';
105 foreach ($value as $key => $value2) {
106 $tmp = $value2;
107 }
108 $value = $tmp;
109 }
110
111 // In case of arrays, recurse
112 if ( is_array( $value ) ) {
113 $output .= $this->formatAsTable($value, ($level + 1));
114 } else {
115 // check for an object
116 if ( ! empty( $value->user_nicename ) ) {
117 $output .= esc_html( $value->user_nicename );
118 } elseif ( is_object( $value ) ) {
119 $output .= esc_html( print_r( $value, true ) );
120 } else {
121 $output .= wp_kses_post( $value );
122 }
123 }
124 $output .= "</td>";
125
126 $output .= "</tr>";
127 }
128
129 $output .= "</table>";
130 return $output;
131 }
132
133 /**
134 * Recursively maybe unserialize data
135 *
136 * @param array $data
137 * @return array
138 */
139 protected function maybeUnserialize(array $data)
140 {
141 foreach ($data as &$datum) {
142 if (is_array($datum)) {
143 $datum = $this->maybeUnserialize($datum);
144 } else {
145 $datum = maybe_unserialize($datum);
146 }
147 }
148
149 return $data;
150 }
151 }
152