-
Notifications
You must be signed in to change notification settings - Fork 5
/
anonymize.sh
executable file
·275 lines (184 loc) · 6.56 KB
/
anonymize.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/usr/bin/env bash
# *---------------------------------------------------------------------------
# SPDX-FileCopyrightText: Carlo Piana <[email protected]>
#
# SPDX-License-Identifier: AGPL-3.0-or-later
# *---------------------------------------------------------------------------
#
# ODT and DOCX anonymizer v.0.99
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# *---------------------------------------------------------------------------
red=$(tput setaf 1)
green=$(tput setaf 76)
normal=$(tput sgr0)
bold=$(tput bold)
underline=$(tput sgr 0 1)
# instout="install_output.log"
# insterr="install_error.log"
declare -a authors_array=()
declare -a authors_array_plus=()
author_string=""
orig_filename="$1"
i_ok() { printf "%s✔\n%s" "${green}" "${normal}"; }
i_ko() { printf "%s✖%s\n...exiting, check logs" "${red}" "${normal}"; }
# some variables that will be used and make sure the zip dir exists
curdir=$(pwd)
an_filename="_anonymized_$1"
zipdir="/tmp/libreoffice"
mkdir $zipdir &> /dev/null
rm -rf ${zipdir:?}/*
function check_i {
if [ $? -eq 0 ]; then
i_ok
else
i_ko; read -r
exit
fi
}
function substitute_docx() {
for f in $(find $zipdir -mindepth 2 -name '*.xml' ); do # get all xml only in subdirectory (not interested elsewhere)
sed -i -E s@"(author=\")$name_from(\")"@"\1$name_to\2"@g $f # this gets the author of the comments
sed -i -E s@"(By>)$name_from(<)"@"\1$name_to\2"@g "$f" ; done # caputure some metatags as well
}
function substitute_it {
if [[ $filetype == ODT ]] ; then
sed -i -E s@"(or>)$name_from(<)"@"\1$name_to\2"@g $zipdir/*.xml
elif [[ $filetype == DOCX ]]; then
substitute_docx
else
echo "WTF"
fi
}
function list_authors {
mapfile -t authors_array < <(grep -hoP "$author_string" $zipdir -R | sort | uniq | sed -E "s@$author_string@\1@g")
echo "+----------------------------------------------------------------"
if [[ ${#authors_array[*]} == 1 ]] ; then
printf "The author is: "
printf "%s " "${authors_array[@]}."
else
printf "Authors are: "
printf "\"%s\"" "${authors_array[@]}"
fi
printf "\n"
echo "+----------------------------------------------------------------"
}
function change_all {
printf "Please insert the name you want to be %sthe only one%s displayed in revisions \n" "${red}" "${normal}"
printf "instead of all the above\\n\\n%s: > %s" "${green}" "${normal}"
read -r name_to
printf "\\nThanks, we are going to replace everything with %s$name_to%s \\n" "${green}" "${normal}"
for i in "${authors_array[@]}" ; do
name_from="$i"
substitute_it
done
}
function choose_subs { # FIXME: make the choice only from the authors_array array
authors_array_plus=("${authors_array[@]}" "list" "quit")
quit_no=("${#authors_array_plus[*]}")
list_no=$(( quit_no - 1 ))
printf "%sYou have the following choices:%s\\n" "${underline}" "${normal}"
PS3=""$'\n'"Choose 1 to ${#authors_array[*]} to modify actual values;"$'\n'"${list_no} to list, ${quit_no} to exit;"$'\n'"Make your choice: ${green}:>${normal} "
select value in "${authors_array_plus[@]}"
do
if [[ ${value} == "quit" ]] ; then
printf "\\nThanks! and goodbye\\n\\n"
break
elif [[ ${value} == "list" ]] ; then
list_authors
choose_subs # restart from beginning to recreate menu. Cases change.
break # otherwise it returns to the function
else
name_from="${value}"
printf "insert the name you want to change %s into%s \n:> %s" "${bold}" "${value}" "${normal}"
read -r name_to
substitute_it
fi
done
}
clear
# Checking the required number of variables
[ -n "$orig_filename" ] && printf "\nFilename is present" || (printf "missing variable, sucker, a namefile is expected " && exit 1)
check_i
#checking if correct filetype
if
[[ $(file --mime-type -b "$orig_filename") =~ application/vnd.oasis.opendocument.text ]] ; then
printf "\\nGood file type ODT"
author_string="<dc:creator>(.*?)</dc:creator>"
filetype=ODT
elif
[[ $(file --mime-type -b "$orig_filename") =~ application/vnd.openxmlformats-officedocument.wordprocessingml.document ]]; then
printf "\\nGood filetype OOXML "
author_string="w:author=\"(.*?)\""
filetype=DOCX
elif
[[ $(file --mime-type -b "$orig_filename") =~ application/octet-stream ]] && [[ $orig_filename == *.docx ]]; then #sometimes mimetype is broken, use extension
printf "\\nGood filetype OOXML "
author_string="w:author=\"(.*?)\""
filetype=DOCX
else
(printf "\\nNot an ODT nor an OOXML document, can't do " && exit 1)
fi
check_i
printf "\nThis is the list of current authors,
now you will be asked to chose what you want to make of them: \n"
unzip -oq "$1" -d $zipdir
check_i
list_authors
# ok, we're ready, let's meddle with the content!
# Mock select menu
printf "Please enter your choice: \\n\\n"
options=("Change all" "Change only one")
select opt in "${options[@]}"
do
case $opt in
"Change all")
break
;;
"Change only one")
break
;;
*) echo "invalid option $REPLY";;
esac
done
printf "\\n"
# Now we ask for input
if [ "$REPLY" = "1" ]; then
change_all
printf "\\nnow the list of authors is: \\n"
list_authors
else
choose_subs
printf "\\nnow the list of authors is: \\n"
list_authors
fi
# this is a dirty hack, because I could not add to zipfile from outside the directory
# basing the directory with -b did not work hell knows why
# I am SO LAME
# cp $1 $filename # needed to have correct structure FIXME
cd "$zipdir" || exit 1 # in case cd fails
check_i
printf " move to zip directory; \\n\\n"
if [ -f "$curdir/$an_filename" ] ; then #remove anonymized if previously created, to make room for clean zipfile
rm "$curdir/$an_filename"
fi
find . -print | zip "$curdir/$an_filename" -@ 1>/dev/null
cd "$curdir" || exit 1 # in case it fails
check_i
printf " recreated file from zip directory; \\n\\n"
echo "
${green}Script complete${normal}
***${bold}WARNING${normal}*** Newfile is in $curdir/${bold}$an_filename${normal}
We are not going to replace the original file, we play it safe.
"