Completed
Push — development ( c7b9bc...545c2a )
by Nils
07:45
created

export.queries.php ➔ outPutCsv()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @file          export.queries.php
4
 * @author        Nils Laumaillé
5
 * @version       2.1.27
6
 * @copyright     (c) 2009-2017 Nils Laumaillé
7
 * @licensing     GNU AFFERO GPL 3.0
8
 * @link          http://www.teampass.net
9
 *
10
 * This library is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
 */
14
15
require_once 'SecureHandler.php';
16
session_start();
17 View Code Duplication
if (!isset($_SESSION['CPM']) || $_SESSION['CPM'] != 1 || !isset($_SESSION['key']) || empty($_SESSION['key'])) {
18
    die('Hacking attempt...');
19
}
20
21
// Load config
22 View Code Duplication
if (file_exists('../includes/config/tp.config.php')) {
23
    require_once '../includes/config/tp.config.php';
24
} elseif (file_exists('./includes/config/tp.config.php')) {
25
    require_once './includes/config/tp.config.php';
26
} else {
27
    throw new Exception("Error file '/includes/config/tp.config.php' not exists", 1);
28
}
29
30
// No time limit
31
set_time_limit(0);
32
33
global $k, $settings;
34
include $SETTINGS['cpassman_dir'].'/includes/config/settings.php';
35
header("Content-type: text/html; charset=utf-8");
36
error_reporting(E_ERROR);
37
require_once $SETTINGS['cpassman_dir'].'/sources/main.functions.php';
38
require_once $SETTINGS['cpassman_dir'].'/sources/SplClassLoader.php';
39
40
// connect to DB
41
require_once $SETTINGS['cpassman_dir'].'/includes/libraries/Database/Meekrodb/db.class.php';
42
$pass = defuse_return_decrypted($pass);
43
DB::$host = $server;
44
DB::$user = $user;
45
DB::$password = $pass;
46
DB::$dbName = $database;
47
DB::$port = $port;
48
DB::$encoding = $encoding;
49
DB::$error_handler = true;
50
$link = mysqli_connect($server, $user, $pass, $database, $port);
51
$link->set_charset($encoding);
52
53
// Build tree
54
$tree = new SplClassLoader('Tree\NestedTree', $SETTINGS['cpassman_dir'].'/includes/libraries');
55
$tree->register();
56
$tree = new Tree\NestedTree\NestedTree($pre.'nested_tree', 'id', 'parent_id', 'title');
57
58
// User's language loading
59
require_once $SETTINGS['cpassman_dir'].'/includes/language/'.$_SESSION['user_language'].'.php';
60
61
// Prepare POST variables
62
$id = filter_input(INPUT_POST, 'id', FILTER_SANITIZE_NUMBER_INT);
63
$post_type = filter_input(INPUT_POST, 'type', FILTER_SANITIZE_STRING);
64
$post_idTree = filter_input(INPUT_POST, 'idTree', FILTER_SANITIZE_NUMBER_INT);
65
$post_idsList = filter_input(INPUT_POST, 'idsList', FILTER_SANITIZE_STRING);
66
$post_salt_key = filter_input(INPUT_POST, 'salt_key', FILTER_SANITIZE_STRING);
67
$post_file = filter_input(INPUT_POST, 'file', FILTER_SANITIZE_STRING);
68
$post_pdf_password = filter_input(INPUT_POST, 'pdf_password', FILTER_SANITIZE_STRING);
69
$post_number = filter_input(INPUT_POST, 'number', FILTER_SANITIZE_STRING);
70
$post_cpt = filter_input(INPUT_POST, 'cpt', FILTER_SANITIZE_STRING);
71
$post_file_link = filter_input(INPUT_POST, 'file_link', FILTER_SANITIZE_STRING);
72
$post_ids = filter_input(INPUT_POST, 'ids', FILTER_SANITIZE_STRING);
73
74
75
//Manage type of action asked
76
if (null !== $post_type) {
77
    switch ($post_type) {
78
        case "initialize_export_table":
79
            DB::query("TRUNCATE TABLE ".prefix_table("export"));
80
            break;
81
82
        //CASE export to PDF format
83
        case "export_to_pdf_format":
84
            if (!in_array($id, $_SESSION['forbiden_pfs']) && in_array($id, $_SESSION['groupes_visibles'])) {
85
                // get path
86
                $tree->rebuild();
87
                $folders = $tree->getPath($id, true);
88
                $path = "";
89
                foreach ($folders as $val) {
90
                    if ($path) {
91
                        $path .= " » ";
92
                    }
93
                    $path .= $val->title;
94
                }
95
96
                // send query
97
                $rows = DB::query(
98
                    "SELECT i.id as id, i.restricted_to as restricted_to, i.perso as perso, i.label as label, i.description as description, i.pw as pw, i.login as login, i.url as url, i.email as email,
99
                        l.date as date, i.pw_iv as pw_iv,
100
                        n.renewal_period as renewal_period
101
                        FROM ".prefix_table("items")." as i
102
                        INNER JOIN ".prefix_table("nested_tree")." as n ON (i.id_tree = n.id)
103
                        INNER JOIN ".prefix_table("log_items")." as l ON (i.id = l.id_item)
104
                        WHERE i.inactif = %i
105
                        AND i.id_tree= %i
106
                        AND (l.action = %s OR (l.action = %s AND l.raison LIKE %s))
107
                        ORDER BY i.label ASC, l.date DESC",
108
                    "0",
109
                    intval($id),
110
                    "at_creation",
111
                    "at_modification",
112
                    "at_pw :%"
113
                );
114
115
                $id_managed = '';
116
                $i = 0;
117
                $items_id_list = array();
118
                foreach ($rows as $record) {
119
                    $restricted_users_array = explode(';', $record['restricted_to']);
120
                    //exclude all results except the first one returned by query
121
                    if (empty($id_managed) || $id_managed != $record['id']) {
122
                        if ((in_array($id, $_SESSION['personal_visible_groups']) && !($record['perso'] == 1 && $_SESSION['user_id'] == $record['restricted_to']) && !empty($record['restricted_to']))
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
123
                            ||
124
                            (!empty($record['restricted_to']) && !in_array($_SESSION['user_id'], $restricted_users_array))
125
                        ) {
126
                            //exclude this case
127
                        } else {
128
                            //encrypt PW
129 View Code Duplication
                            if (empty($post_salt_key) === false && null !== $post_salt_key) {
130
                                $pw = cryption(
131
                                    $record['pw'],
132
                                    mysqli_escape_string($link, stripslashes($post_salt_key)),
133
                                    "decrypt"
134
                                );
135
                            } else {
136
                                $pw = cryption(
137
                                    $record['pw'],
138
                                    "",
139
                                    "decrypt"
140
                                );
141
                            }
142
143
                            // get KBs
144
                            $arr_kbs = "";
145
                            $rows_kb = DB::query(
146
                                "SELECT b.label, b.id
147
                                FROM ".prefix_table("kb_items")." AS a
148
                                INNER JOIN ".prefix_table("kb")." AS b ON (a.kb_id = b.id)
149
                                WHERE a.item_id = %i",
150
                                $record['id']
151
                            );
152
                            foreach ($rows_kb as $rec_kb) {
153
                                if (empty($arr_kbs)) {
154
                                    $arr_kbs = $rec_kb['label'];
155
                                } else {
156
                                    $arr_kbs .= " | ".$rec_kb['label'];
157
                                }
158
                            }
159
160
                            // get TAGS
161
                            $arr_tags = "";
162
                            $rows_tag = DB::query(
163
                                "SELECT tag
164
                                FROM ".prefix_table("tags")."
165
                                WHERE item_id = %i",
166
                                $record['id']
167
                            );
168
                            foreach ($rows_tag as $rec_tag) {
169
                                if (empty($arr_tags)) {
170
                                    $arr_tags = $rec_tag['tag'];
171
                                } else {
172
                                    $arr_tags .= " ".$rec_tag['tag'];
173
                                }
174
                            }
175
176
                            // store
177
                            DB::insert(
178
                                prefix_table("export"),
179
                                array(
180
                                    'id' => $record['id'],
181
                                    'description' => strip_tags(cleanString(html_entity_decode($record['description'], ENT_QUOTES | ENT_XHTML, UTF - 8), true)),
182
                                    'label' => cleanString(html_entity_decode($record['label'], ENT_QUOTES | ENT_XHTML, UTF - 8), true),
183
                                    'pw' => html_entity_decode($pw['string'], ENT_QUOTES | ENT_XHTML, UTF - 8),
184
                                    'login' => strip_tags(cleanString(html_entity_decode($record['login'], ENT_QUOTES | ENT_XHTML, UTF - 8), true)),
185
                                    'path' => $path,
186
                                    'url' => strip_tags(cleanString(html_entity_decode($record['url'], ENT_QUOTES | ENT_XHTML, UTF - 8), true)),
187
                                    'email' => strip_tags(cleanString(html_entity_decode($record['email'], ENT_QUOTES | ENT_XHTML, UTF - 8), true)),
188
                                    'kbs' => $arr_kbs,
189
                                    'tags' => $arr_tags
190
                                )
191
                            );
192
193
                            // log
194
                            logItems(
195
                                $record['id'],
196
                                $record['label'],
197
                                $_SESSION['user_id'],
198
                                'at_export',
199
                                $_SESSION['login'],
200
                                'pdf'
201
                            );
202
                        }
203
                    }
204
                    $id_managed = $record['id'];
205
                    $folder_title = $record['folder_title'];
206
                }
207
            }
208
            //}
209
            echo '[{}]';
210
            break;
211
212
        case "finalize_export_pdf":
213
            // query
214
            $rows = DB::query("SELECT * FROM ".prefix_table("export"));
215
            $counter = DB::count();
216
            if ($counter > 0) {
217
                // print
218
                //Some variables
219
                $table_full_width = 300;
220
                $table_col_width = array(40, 30, 30, 60, 27, 40, 25, 25);
221
                $table = array('label', 'login', 'pw', 'description', 'email', 'url', 'kbs', 'tags');
222
                $prev_path = "";
223
224
                //Prepare the PDF file
225
                include $SETTINGS['cpassman_dir'].'/includes/libraries/Pdf/Tfpdf/fpdf.php';
226
227
                $pdf = new FPDF_Protection("P", "mm", "A4", "ma page");
228
                $pdf->SetProtection(array('print'), $post_pdf_password);
229
230
                //Add font for regular text
231
                $pdf->AddFont('helvetica', '');
232
                //Add monospace font for passwords
233
                $pdf->AddFont('LiberationMono', '');
234
235
                $pdf->aliasNbPages();
236
                $pdf->addPage(L);
237
238
                $prev_path = "";
239
                foreach ($rows as $record) {
240
                    // decode
241
                    $record['label'] = utf8_decode($record['label']);
242
                    $record['login'] = utf8_decode($record['login']);
243
                    $record['pw'] = utf8_decode($record['pw']);
244
                    $record['description'] = utf8_decode($record['description']);
245
                    $record['email'] = utf8_decode($record['email']);
246
                    $record['url'] = utf8_decode($record['url']);
247
                    $record['kbs'] = utf8_decode($record['kbs']);
248
                    $record['tags'] = utf8_decode($record['tags']);
249
250
                    $printed_ids[] = $record['id'];
251
                    if ($prev_path != $record['path']) {
252
                        $pdf->SetFont('helvetica', '', 10);
253
                        $pdf->SetFillColor(192, 192, 192);
254
                        error_log('key: '.$key.' - paths: '.$record['path']);
255
                        $pdf->cell(0, 6, utf8_decode($record['path']), 1, 1, "L", true);
256
                        $pdf->SetFillColor(222, 222, 222);
257
                        $pdf->cell($table_col_width[0], 6, $LANG['label'], 1, 0, "C", true);
258
                        $pdf->cell($table_col_width[1], 6, $LANG['login'], 1, 0, "C", true);
259
                        $pdf->cell($table_col_width[2], 6, $LANG['pw'], 1, 0, "C", true);
260
                        $pdf->cell($table_col_width[3], 6, $LANG['description'], 1, 0, "C", true);
261
                        $pdf->cell($table_col_width[4], 6, $LANG['email'], 1, 0, "C", true);
262
                        $pdf->cell($table_col_width[5], 6, $LANG['url'], 1, 0, "C", true);
263
                        $pdf->cell($table_col_width[6], 6, $LANG['kbs'], 1, 0, "C", true);
264
                        $pdf->cell($table_col_width[7], 6, $LANG['tags'], 1, 1, "C", true);
265
                    }
266
                    $prev_path = $record['path'];
267
                    if (!isutf8($record['pw'])) {
268
                        $record['pw'] = "";
269
                    }
270
                    //row height calculation
271
                    $nb = 0;
272
                    $nb = max($nb, nbLines($table_col_width[0], $record['label']));
273
                    $nb = max($nb, nbLines($table_col_width[1], $record['login']));
274
                    $nb = max($nb, nbLines($table_col_width[3], $record['description']));
275
                    $nb = max($nb, nbLines($table_col_width[2], $record['pw']));
276
                    $nb = max($nb, nbLines($table_col_width[5], $record['url']));
277
                    $nb = max($nb, nbLines($table_col_width[6], $record['kbs']));
278
                    $nb = max($nb, nbLines($table_col_width[7], $record['tags']));
279
280
                    $h = 5 * $nb;
281
                    //Page break needed?
282
                    checkPageBreak($h);
283
                    //Draw cells
284
                    $pdf->SetFont('helvetica', '', 8);
285
                    for ($i = 0; $i < count($table); $i++) {
286
                        $w = $table_col_width[$i];
287
                        $a = 'L';
288
                        //actual position
289
                        $x = $pdf->GetX();
290
                        $y = $pdf->GetY();
291
                        //Draw
292
                        $pdf->Rect($x, $y, $w, $h);
293
                        //Write
294
                        $pdf->MultiCell($w, 5, ($record[$table[$i]]), 0, $a);
295
                        //go to right
296
                        $pdf->SetXY($x + $w, $y);
297
                    }
298
                    //return to line
299
                    $pdf->Ln($h);
300
                }
301
302
                $pdf_file = "print_out_pdf_".date("Y-m-d", mktime(0, 0, 0, date('m'), date('d'), date('y')))."_".generateKey().".pdf";
303
304
                //send the file
305
                $pdf->Output($SETTINGS['path_to_files_folder']."/".$pdf_file);
306
307
                //log
308
                logEvents('pdf_export', "", $_SESSION['user_id'], $_SESSION['login']);
309
310
                //clean table
311
                DB::query("TRUNCATE TABLE ".prefix_table("export"));
312
313
                echo '[{"text":"<a href=\''.$SETTINGS['url_to_files_folder'].'/'.$pdf_file.'\' download>'.$LANG['pdf_download'].'</a>"}]';
314
            }
315
            break;
316
317
        //CASE export in CSV format
318
        case "export_to_csv_format":
319
            //Init
320
            $full_listing = array();
321
            $full_listing[0] = array(
322
                'id'            => "id",
323
                'label'         => "label",
324
                'description'   => "description",
325
                'pw'            => "pw",
326
                'login'         => "login",
327
                'restricted_to' => "restricted_to",
328
                'perso'         => "perso",
329
                'url'           => "url",
330
                'email'         => "email",
331
                'kbs'           => "kb",
332
                'tags'          => "tag"
333
            );
334
335
            $id_managed = '';
336
            $i = 1;
337
            $items_id_list = array();
338
339
            foreach (explode(';', htmlentities($post_ids, ENT_QUOTES)) as $id) {
340
                if (!in_array($id, $_SESSION['forbiden_pfs']) && in_array($id, $_SESSION['groupes_visibles'])) {
341
                    $rows = DB::query(
342
                        "SELECT i.id as id, i.restricted_to as restricted_to, i.perso as perso,
343
                            i.label as label, i.description as description, i.pw as pw, i.login as login, i.url as url,
344
                            i.email as email,l.date as date, i.pw_iv as pw_iv,n.renewal_period as renewal_period
345
                        FROM ".prefix_table("items")." as i
346
                        INNER JOIN ".prefix_table("nested_tree")." as n ON (i.id_tree = n.id)
347
                        INNER JOIN ".prefix_table("log_items")." as l ON (i.id = l.id_item)
348
                        WHERE i.inactif = %i
349
                        AND i.id_tree= %i
350
                        AND (l.action = %s OR (l.action = %s AND l.raison LIKE %s))
351
                        ORDER BY i.label ASC, l.date DESC",
352
                        "0",
353
                        intval($id),
354
                        "at_creation",
355
                        "at_modification",
356
                        "at_pw :%"
357
                    );
358
                    foreach ($rows as $record) {
359
                        $restricted_users_array = explode(';', $record['restricted_to']);
360
                        //exclude all results except the first one returned by query
361
                        if (empty($id_managed) || $id_managed != $record['id']) {
362
                            if ((in_array($id, $_SESSION['personal_visible_groups']) && !($record['perso'] == 1 && $_SESSION['user_id'] == $record['restricted_to']) && !empty($record['restricted_to']))
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
363
                                ||
364
                                (!empty($record['restricted_to']) && !in_array($_SESSION['user_id'], $restricted_users_array))
365
                            ) {
366
                                //exclude this case
367
                            } else {
368
                                //encrypt PW
369 View Code Duplication
                                if (empty($post_salt_key) === false && null !== $post_salt_key) {
370
                                    $pw = cryption(
371
                                        $record['pw'],
372
                                        mysqli_escape_string($link, stripslashes($post_salt_key)),
373
                                        "decrypt"
374
                                    );
375
                                } else {
376
                                    $pw = cryption(
377
                                        $record['pw'],
378
                                        "",
379
                                        "decrypt"
380
                                    );
381
                                }
382
383
                                // get KBs
384
                                $arr_kbs = [];
385
                                $rows_kb = DB::query(
386
                                    "SELECT b.label, b.id
387
                                    FROM ".prefix_table("kb_items")." AS a
388
                                    INNER JOIN ".prefix_table("kb")." AS b ON (a.kb_id = b.id)
389
                                    WHERE a.item_id = %i",
390
                                    $record['id']
391
                                );
392
                                foreach ($rows_kb as $rec_kb) {
393
                                    array_push($arr_kbs, $rec_kb['label']);
394
                                }
395
396
                                // get TAGS
397
                                $arr_tags = [];
398
                                $rows_tag = DB::query(
399
                                    "SELECT tag
400
                                    FROM ".prefix_table("tags")."
401
                                    WHERE item_id = %i",
402
                                    $record['id']
403
                                );
404
                                foreach ($rows_tag as $rec_tag) {
405
                                    array_push($arr_tags, $rec_tag['tag']);
406
                                }
407
408
                                $full_listing[$i] = array(
409
                                    'id' => $record['id'],
410
                                    'label' => strip_tags(cleanString(html_entity_decode($record['label'], ENT_QUOTES | ENT_XHTML, UTF - 8), true)),
411
                                    'description' => htmlspecialchars_decode(addslashes(str_replace(array(";", "<br />"), array("|", "\n\r"), mysqli_escape_string($link, stripslashes(utf8_decode($record['description'])))))),
412
                                    'pw' => html_entity_decode($pw['string'], ENT_QUOTES | ENT_XHTML, UTF - 8),
413
                                    'login' => strip_tags(cleanString(html_entity_decode($record['login'], ENT_QUOTES | ENT_XHTML, UTF - 8), true)),
414
                                    'restricted_to' => isset($record['restricted_to']) ? $record['restricted_to'] : '',
415
                                    'perso' => $record['perso'] === "0" ? "False" : "True",
416
                                    'url' => $record['url'] !== "none" ? htmlspecialchars_decode($record['url']) : "",
417
                                    'email' => $record['email'] !== "none" ? htmlspecialchars_decode($record['email']) : "",
418
                                    'kbs' => implode(" | ", $arr_kbs),
419
                                    'tags' => implode(" ", $arr_tags)
420
                                );
421
                                $i++;
422
423
                                // log
424
                                logItems(
425
                                    $record['id'],
426
                                    $record['label'],
427
                                    $_SESSION['user_id'],
428
                                    'at_export',
429
                                    $_SESSION['login'],
430
                                    'csv'
431
                                );
432
                            }
433
                        }
434
                        $id_managed = $record['id'];
435
                    }
436
                }
437
            }
438
            // Save the file
439
            $csv_file = '/print_out_csv_'.time().'_'.generateKey().'.csv';
440
            $outstream = fopen($SETTINGS['path_to_files_folder'].$csv_file, "w");
441
442
            // Loop on Results, decode to UTF8 and write in CSV file
443
            foreach ($full_listing as $value) {
444
                $value = array_map("utf8_decode", $value);
445
                fputcsv($outstream, $value, ";");
446
            }
447
448
            // Close and display
449
            fclose($outstream);
450
451
            echo '[{"text":"<a href=\''.$SETTINGS['url_to_files_folder'].$csv_file.'\' target=\'_blank\'>'.$LANG['pdf_download'].'</a>"}]';
452
            break;
453
454
        //CASE export in HTML format
455
        case "export_to_html_format":
456
            // step 1:
457
            // - prepare export file
458
            // - get full list of objects id to export
459
            include $SETTINGS['cpassman_dir'].'/includes/config/include.php';
460
            require_once $SETTINGS['cpassman_dir'].'/includes/libraries/Encryption/GibberishAES/GibberishAES.php';
461
            $idsList = array();
462
            $objNumber = 0;
463
464
            foreach (explode(';', $post_ids) as $id) {
465
                if (!in_array($id, $_SESSION['forbiden_pfs']) && in_array($id, $_SESSION['groupes_visibles'])) {
466
                    // count elements to display
467
                    $result = DB::query(
468
                        "SELECT i.id AS id, i.label AS label, i.restricted_to AS restricted_to, i.perso AS perso
469
                        FROM ".prefix_table("items")." as i
470
                        INNER JOIN ".prefix_table("nested_tree")." as n ON (i.id_tree = n.id)
471
                        INNER JOIN ".prefix_table("log_items")." as l ON (i.id = l.id_item)
472
                        WHERE i.inactif = %i
473
                        AND i.id_tree= %i
474
                        AND (l.action = %s OR (l.action = %s AND l.raison LIKE %s))
475
                        ORDER BY i.label ASC, l.date DESC",
476
                        "0",
477
                        $id,
478
                        "at_creation",
479
                        "at_modification",
480
                        "at_pw :%"
481
                    );
482
                    foreach ($result as $record) {
483
                        $restricted_users_array = explode(';', $record['restricted_to']);
484
                        if ((
485
                                (in_array($id, $_SESSION['personal_visible_groups']) && !($record['perso'] == 1 && $_SESSION['user_id'] == $record['restricted_to']) && !empty($record['restricted_to']))
486
                                ||
487
                                (!empty($record['restricted_to']) && !in_array($_SESSION['user_id'], $restricted_users_array))
488
                                ||
489
                                (in_array($id, $_SESSION['groupes_visibles']))
490
                            ) && (
491
                                !in_array($record['id'], $idsList)
492
                            )
493
                        ) {
494
                            array_push($idsList, $record['id']);
495
                            $objNumber++;
496
497
                            // log
498
                            logItems(
499
                                $record['id'],
500
                                $record['label'],
501
                                $_SESSION['user_id'],
502
                                'at_export',
503
                                $_SESSION['login'],
504
                                'html'
505
                            );
506
                        }
507
                    }
508
                }
509
            }
510
511
                // prepare export file
512
                //save the file
513
                $html_file = '/teampass_export_'.time().'_'.generateKey().'.html';
514
                //print_r($full_listing);
515
                $outstream = fopen($SETTINGS['path_to_files_folder'].$html_file, "w");
516
                fwrite(
517
                    $outstream,
518
                    '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
519
    <head>
520
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
521
    <title>TeamPass Off-line mode</title>
522
    <style>
523
    body{font-family:sans-serif; font-size:11pt; background:#DCE0E8;}
524
    thead th{font-size:13px; font-weight:bold; background:#344151; padding:4px 10px 4px 10px; font-family:arial; color:#FFFFFF;}
525
    tr.line0 td {background-color:#FFFFFF; border-bottom:1px solid #CCCCCC; font-family:arial; font-size:11px;}
526
    tr.line1 td {background-color:#F0F0F0; border-bottom:1px solid #CCCCCC; font-family:arial; font-size:11px;}
527
    tr.path td {background-color:#C0C0C0; font-family:arial; font-size:11px; font-weight:bold;}
528
    #footer{width: 980px; height: 20px; line-height: 16px; margin: 10px auto 0 auto; padding: 10px; font-family: sans-serif; font-size: 10px; color:#000000;}
529
    #header{padding:10px; font-size:18px; background:#344151; color:#FFFFFF; border:2px solid #222E3D;}
530
    #itemsTable{width:100%;}
531
    #information{margin:10px 0 10px 0; background:#344151; color:#FFFFFF; border:2px solid #222E3D;}
532
    </style>
533
    </head>
534
    <body>
535
    <div id="header">
536
    '.$SETTINGS_EXT['tool_name'].' - Off Line mode
537
    </div>
538
    <div style="margin:10px; font-size:9px;">
539
    <i>This page was generated by <b>'.$_SESSION['name'].' '.$_SESSION['lastname'].'</b>, the '.date("Y/m/d H:i:s").'.</i>
540
    </div>
541
    <div id="information"></div>
542
    <div style="margin:10px;">
543
    Enter the decryption key : <input type="password" id="saltkey" />
544
    </div>
545
    <div>
546
    <table id="itemsTable">
547
        <thead><tr>
548
            <th style="width:15%;">'.$LANG['label'].'</th>
549
            <th style="width:10%;">'.$LANG['pw'].'</th>
550
            <th style="width:30%;">'.$LANG['description'].'</th>
551
            <th style="width:5%;">'.$LANG['user_login'].'</th>
552
            <th style="width:20%;">'.$LANG['url'].'</th>
553
        </tr></thead>'
554
                );
555
556
            fclose($outstream);
557
558
            // send back and continue
559
            echo '[{"loop":"true", "number":"'.$objNumber.'", "file":"'.$SETTINGS['path_to_files_folder'].$html_file.'" , "file_link":"'.$SETTINGS['url_to_files_folder'].$html_file.'"}]';
560
            break;
561
562
        //CASE export in HTML format - Iteration loop
563
        case "export_to_html_format_loop":
564
            // do checks ... if fails, return an error
565
            if (null !== $post_idTree || null !== $post_idsList) {
566
                echo '[{"error":"true"}]';
567
                break;
568
            }
569
570
            $full_listing = array();
571
            $items_id_list = array();
572
            include $SETTINGS['cpassman_dir'].'/includes/config/include.php';
573
            require_once $SETTINGS['cpassman_dir'].'/includes/libraries/Encryption/GibberishAES/GibberishAES.php';
574
575
            $rows = DB::query(
576
                "SELECT i.id as id, i.url as url, i.perso as perso, i.label as label, i.description as description, i.pw as pw, i.login as login, i.id_tree as id_tree,
577
                   l.date as date, i.pw_iv as pw_iv,
578
                   n.renewal_period as renewal_period
579
                FROM ".prefix_table("items")." as i
580
                INNER JOIN ".prefix_table("nested_tree")." as n ON (i.id_tree = n.id)
581
                INNER JOIN ".prefix_table("log_items")." as l ON (i.id = l.id_item)
582
                WHERE i.inactif = %i
583
                AND i.id_tree= %i
584
                AND (l.action = %s OR (l.action = %s AND l.raison LIKE %s))
585
                ORDER BY i.label ASC, l.date DESC",
586
                "0",
587
                $post_idTree,
588
                "at_creation",
589
                "at_modification",
590
                "at_pw :%"
591
            );
592
593
            foreach ($rows as $record) {
594
                //exclude all results except the first one returned by query
595
                if (empty($id_managed) || $id_managed != $record['id']) {
596
                    // decrypt PW
597
                    if (empty($post_salt_key) === false && null !== $post_salt_key) {
598
                        $pw = cryption(
599
                            $record['pw'],
600
                            mysqli_escape_string($link, stripslashes($post_salt_key)),
601
                            "decrypt"
602
                        );
603
                    } else {
604
                        $pw = cryption(
605
                            $record['pw'],
606
                            "",
607
                            "decrypt"
608
                        );
609
                    }
610
                    array_push($full_listing, array(
611
                        'id_tree' => $record['id_tree'],
612
                        'id' => $record['id'],
613
                        'label' => $record['label'],
614
                        'description' => addslashes(str_replace(array(";", "<br />"), array("|", "\n\r"), mysqli_escape_string($link, stripslashes(utf8_decode($record['description']))))),
615
                        'pw' => $pw['string'],
616
                        'login' => $record['login'],
617
                        'url' => $record['url'],
618
                        'perso' => $record['perso']
619
                    ));
620
                    $i++;
621
                    array_push($items_id_list, $record['id']);
622
                }
623
                $id_managed = $record['id'];
624
            }
625
626
            //save in export file
627
            $outstream = fopen($post_file, "a");
628
629
            $lineType = "line1";
630
            $idTree = "";
631
            foreach ($full_listing as $elem) {
632
                if ($lineType == "line0") {
633
                    $lineType = "line1";
634
                } else {
635
                    $lineType = "line0";
636
                }
637 View Code Duplication
                if (empty($elem['description'])) {
638
                    $desc = '&nbsp;';
639
                } else {
640
                    $desc = addslashes($elem['description']);
641
                }
642 View Code Duplication
                if (empty($elem['login'])) {
643
                    $login = '&nbsp;';
644
                } else {
645
                    $login = addslashes($elem['login']);
646
                }
647 View Code Duplication
                if (empty($elem['url'])) {
648
                    $url = '&nbsp;';
649
                } else {
650
                    $url = addslashes($elem['url']);
651
                }
652
653
                // Prepare tree
654
                if ($idTree != $elem['id_tree']) {
655
                    $arbo = $tree->getPath($elem['id_tree'], true);
656
                    foreach ($arbo as $folder) {
657
                        $arboHtml_tmp = htmlspecialchars(stripslashes($folder->title), ENT_QUOTES);
658
                        if (empty($arboHtml)) {
659
                            $arboHtml = $arboHtml_tmp;
660
                        } else {
661
                            $arboHtml .= ' » '.$arboHtml_tmp;
662
                        }
663
                    }
664
                    fputs(
665
                        $outstream,
666
                        '
667
            <tr class="path"><td colspan="5">'.$arboHtml.'</td></tr>'
668
                    );
669
                    $idTree = $elem['id_tree'];
670
                }
671
672
                $encPw = GibberishAES::enc($elem['pw'], $post_pdf_password);
673
                fputs(
674
                    $outstream,
675
                    '
676
            <tr class="'.$lineType.'">
677
                <td>'.addslashes($elem['label']).'</td>
678
                <td align="center"><span class="span_pw" id="span_'.$elem['id'].'"><a href="#" onclick="decryptme('.$elem['id'].', \''.$encPw.'\');return false;">Decrypt </a></span><input type="hidden" id="hide_'.$elem['id'].'" value="'.$encPw.'" /></td>
679
                <td>'.$desc.'</td>
680
                <td align="center">'.$login.'</td>
681
                <td align="center">'.$url.'</td>
682
                </tr>'
683
                );
684
            }
685
686
            fclose($outstream);
687
688
            // send back and continue
689
            echo '[{"loop":"true", "number":"'.$post_number.'", "cpt":"'.$post_cpt.'", "file":"'.$post_file.'", "idsList":"'.$post_idsList.'" , "file_link":"'.$post_file_link.'"}]';
690
            break;
691
692
            //CASE export in HTML format - Iteration loop
693
        case "export_to_html_format_finalize":
694
            // Load includes
695
            include $SETTINGS['cpassman_dir'].'/includes/config/include.php';
696
697
            // open file
698
            $outstream = fopen($post_file, "a");
699
700
            fputs(
701
                $outstream,
702
                '
703
        </table></div>
704
        <input type="button" value="Hide all" onclick="hideAll()" />
705
        <div id="footer" style="text-align:center;">
706
            <a href="http://teampass.net/about/" target="_blank" style="">'.$SETTINGS_EXT['tool_name'].'&nbsp;'.$SETTINGS_EXT['version'].'&nbsp;'.$SETTINGS_EXT['copyright'].'</a>
707
        </div>
708
        </body>
709
    </html>
710
    <script type="text/javascript">
711
        function decryptme(id, string)
712
        {
713
            if (document.getElementById("saltkey").value != "") {
714
                var decryptedPw;
715
716
                try {
717
                    decryptedPw = GibberishAES.dec(string, document.getElementById("saltkey").value)
718
                }
719
                catch(e) {
720
                    alert (e);
721
                    return decryptedPw;
722
                }
723
724
                document.getElementById("span_"+id).innerHTML = decryptedPw +
725
                    "&nbsp;<a href=\"#\" onclick=\"encryptme("+id+")\"><span style=\"font-size:7px;\">[Hide]</span></a>";
726
            } else {
727
                alert("Decryption Key is empty!");
728
            }
729
        }
730
        function encryptme(id)
731
        {
732
            document.getElementById("span_"+id).innerHTML = "<a href=\"#\" onclick=\"decryptme("+id+", \'"+document.getElementById("hide_"+id).value+"\')\">Decrypt</a>";
733
        }
734
        function hideAll()
735
        {
736
            var elements = document.getElementsByClassName("span_pw");
737
            for (var i=0, im=elements.length; im>i; i++) {
738
                var dataPw = elements[i].id.split("_");
739
                elements[i].innerHTML = "<a href=\"#\" onclick=\"decryptme("+dataPw[1]+", \'"+document.getElementById("hide_"+dataPw[1]).value+"\')\">Decrypt</a>";
740
            }
741
        }
742
        (function(e,r){"object"==typeof exports?module.exports=r():"function"==typeof define&&define.amd?define(r):e.GibberishAES=r()})(this,function(){"use strict";var e=14,r=8,n=!1,f=function(e){try{return unescape(encodeURIComponent(e))}catch(r){throw"Error on UTF-8 encode"}},c=function(e){try{return decodeURIComponent(escape(e))}catch(r){throw"Bad Key"}},t=function(e){var r,n,f=[];for(16>e.length&&(r=16-e.length,f=[r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r]),n=0;e.length>n;n++)f[n]=e[n];return f},a=function(e,r){var n,f,c="";if(r){if(n=e[15],n>16)throw"Decryption error: Maybe bad key";if(16===n)return"";for(f=0;16-n>f;f++)c+=String.fromCharCode(e[f])}else for(f=0;16>f;f++)c+=String.fromCharCode(e[f]);return c},o=function(e){var r,n="";for(r=0;e.length>r;r++)n+=(16>e[r]?"0":"")+e[r].toString(16);return n},d=function(e){var r=[];return e.replace(/(..)/g,function(e){r.push(parseInt(e,16))}),r},u=function(e,r){var n,c=[];for(r||(e=f(e)),n=0;e.length>n;n++)c[n]=e.charCodeAt(n);return c},i=function(n){switch(n){case 128:e=10,r=4;break;case 192:e=12,r=6;break;case 256:e=14,r=8;break;default:throw"Invalid Key Size Specified:"+n}},b=function(e){var r,n=[];for(r=0;e>r;r++)n=n.concat(Math.floor(256*Math.random()));return n},h=function(n,f){var c,t=e>=12?3:2,a=[],o=[],d=[],u=[],i=n.concat(f);for(d[0]=L(i),u=d[0],c=1;t>c;c++)d[c]=L(d[c-1].concat(i)),u=u.concat(d[c]);return a=u.slice(0,4*r),o=u.slice(4*r,4*r+16),{key:a,iv:o}},l=function(e,r,n){r=S(r);var f,c=Math.ceil(e.length/16),a=[],o=[];for(f=0;c>f;f++)a[f]=t(e.slice(16*f,16*f+16));for(0===e.length%16&&(a.push([16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16]),c++),f=0;a.length>f;f++)a[f]=0===f?x(a[f],n):x(a[f],o[f-1]),o[f]=s(a[f],r);return o},v=function(e,r,n,f){r=S(r);var t,o=e.length/16,d=[],u=[],i="";for(t=0;o>t;t++)d.push(e.slice(16*t,16*(t+1)));for(t=d.length-1;t>=0;t--)u[t]=p(d[t],r),u[t]=0===t?x(u[t],n):x(u[t],d[t-1]);for(t=0;o-1>t;t++)i+=a(u[t]);return i+=a(u[t],!0),f?i:c(i)},s=function(r,f){n=!1;var c,t=M(r,f,0);for(c=1;e+1>c;c++)t=g(t),t=y(t),e>c&&(t=k(t)),t=M(t,f,c);return t},p=function(r,f){n=!0;var c,t=M(r,f,e);for(c=e-1;c>-1;c--)t=y(t),t=g(t),t=M(t,f,c),c>0&&(t=k(t));return t},g=function(e){var r,f=n?D:B,c=[];for(r=0;16>r;r++)c[r]=f[e[r]];return c},y=function(e){var r,f=[],c=n?[0,13,10,7,4,1,14,11,8,5,2,15,12,9,6,3]:[0,5,10,15,4,9,14,3,8,13,2,7,12,1,6,11];for(r=0;16>r;r++)f[r]=e[c[r]];return f},k=function(e){var r,f=[];if(n)for(r=0;4>r;r++)f[4*r]=F[e[4*r]]^R[e[1+4*r]]^j[e[2+4*r]]^z[e[3+4*r]],f[1+4*r]=z[e[4*r]]^F[e[1+4*r]]^R[e[2+4*r]]^j[e[3+4*r]],f[2+4*r]=j[e[4*r]]^z[e[1+4*r]]^F[e[2+4*r]]^R[e[3+4*r]],f[3+4*r]=R[e[4*r]]^j[e[1+4*r]]^z[e[2+4*r]]^F[e[3+4*r]];else for(r=0;4>r;r++)f[4*r]=E[e[4*r]]^U[e[1+4*r]]^e[2+4*r]^e[3+4*r],f[1+4*r]=e[4*r]^E[e[1+4*r]]^U[e[2+4*r]]^e[3+4*r],f[2+4*r]=e[4*r]^e[1+4*r]^E[e[2+4*r]]^U[e[3+4*r]],f[3+4*r]=U[e[4*r]]^e[1+4*r]^e[2+4*r]^E[e[3+4*r]];return f},M=function(e,r,n){var f,c=[];for(f=0;16>f;f++)c[f]=e[f]^r[n][f];return c},x=function(e,r){var n,f=[];for(n=0;16>n;n++)f[n]=e[n]^r[n];return f},S=function(n){var f,c,t,a,o=[],d=[],u=[];for(f=0;r>f;f++)c=[n[4*f],n[4*f+1],n[4*f+2],n[4*f+3]],o[f]=c;for(f=r;4*(e+1)>f;f++){for(o[f]=[],t=0;4>t;t++)d[t]=o[f-1][t];for(0===f%r?(d=m(w(d)),d[0]^=K[f/r-1]):r>6&&4===f%r&&(d=m(d)),t=0;4>t;t++)o[f][t]=o[f-r][t]^d[t]}for(f=0;e+1>f;f++)for(u[f]=[],a=0;4>a;a++)u[f].push(o[4*f+a][0],o[4*f+a][1],o[4*f+a][2],o[4*f+a][3]);return u},m=function(e){for(var r=0;4>r;r++)e[r]=B[e[r]];return e},w=function(e){var r,n=e[0];for(r=0;4>r;r++)e[r]=e[r+1];return e[3]=n,e},A=function(e,r){var n,f=[];for(n=0;e.length>n;n+=r)f[n/r]=parseInt(e.substr(n,r),16);return f},C=function(e){var r,n=[];for(r=0;e.length>r;r++)n[e[r]]=r;return n},I=function(e,r){var n,f;for(f=0,n=0;8>n;n++)f=1===(1&r)?f^e:f,e=e>127?283^e<<1:e<<1,r>>>=1;return f},O=function(e){var r,n=[];for(r=0;256>r;r++)n[r]=I(e,r);return n},B=A("637c777bf26b6fc53001672bfed7ab76ca82c97dfa5947f0add4a2af9ca472c0b7fd9326363ff7cc34a5e5f171d8311504c723c31896059a071280e2eb27b27509832c1a1b6e5aa0523bd6b329e32f8453d100ed20fcb15b6acbbe394a4c58cfd0efaafb434d338545f9027f503c9fa851a3408f929d38f5bcb6da2110fff3d2cd0c13ec5f974417c4a77e3d645d197360814fdc222a908846eeb814de5e0bdbe0323a0a4906245cc2d3ac629195e479e7c8376d8dd54ea96c56f4ea657aae08ba78252e1ca6b4c6e8dd741f4bbd8b8a703eb5664803f60e613557b986c11d9ee1f8981169d98e949b1e87e9ce5528df8ca1890dbfe6426841992d0fb054bb16",2),D=C(B),K=A("01020408102040801b366cd8ab4d9a2f5ebc63c697356ad4b37dfaefc591",2),E=O(2),U=O(3),z=O(9),R=O(11),j=O(13),F=O(14),G=function(e,r,n){var f,c=b(8),t=h(u(r,n),c),a=t.key,o=t.iv,d=[[83,97,108,116,101,100,95,95].concat(c)];return e=u(e,n),f=l(e,a,o),f=d.concat(f),T.encode(f)},H=function(e,r,n){var f=T.decode(e),c=f.slice(8,16),t=h(u(r,n),c),a=t.key,o=t.iv;return f=f.slice(16,f.length),e=v(f,a,o,n)},L=function(e){function r(e,r){return e<<r|e>>>32-r}function n(e,r){var n,f,c,t,a;return c=2147483648&e,t=2147483648&r,n=1073741824&e,f=1073741824&r,a=(1073741823&e)+(1073741823&r),n&f?2147483648^a^c^t:n|f?1073741824&a?3221225472^a^c^t:1073741824^a^c^t:a^c^t}function f(e,r,n){return e&r|~e&n}function c(e,r,n){return e&n|r&~n}function t(e,r,n){return e^r^n}function a(e,r,n){return r^(e|~n)}function o(e,c,t,a,o,d,u){return e=n(e,n(n(f(c,t,a),o),u)),n(r(e,d),c)}function d(e,f,t,a,o,d,u){return e=n(e,n(n(c(f,t,a),o),u)),n(r(e,d),f)}function u(e,f,c,a,o,d,u){return e=n(e,n(n(t(f,c,a),o),u)),n(r(e,d),f)}function i(e,f,c,t,o,d,u){return e=n(e,n(n(a(f,c,t),o),u)),n(r(e,d),f)}function b(e){for(var r,n=e.length,f=n+8,c=(f-f%64)/64,t=16*(c+1),a=[],o=0,d=0;n>d;)r=(d-d%4)/4,o=8*(d%4),a[r]=a[r]|e[d]<<o,d++;return r=(d-d%4)/4,o=8*(d%4),a[r]=a[r]|128<<o,a[t-2]=n<<3,a[t-1]=n>>>29,a}function h(e){var r,n,f=[];for(n=0;3>=n;n++)r=255&e>>>8*n,f=f.concat(r);return f}var l,v,s,p,g,y,k,M,x,S=[],m=A("67452301efcdab8998badcfe10325476d76aa478e8c7b756242070dbc1bdceeef57c0faf4787c62aa8304613fd469501698098d88b44f7afffff5bb1895cd7be6b901122fd987193a679438e49b40821f61e2562c040b340265e5a51e9b6c7aad62f105d02441453d8a1e681e7d3fbc821e1cde6c33707d6f4d50d87455a14eda9e3e905fcefa3f8676f02d98d2a4c8afffa39428771f6816d9d6122fde5380ca4beea444bdecfa9f6bb4b60bebfbc70289b7ec6eaa127fad4ef308504881d05d9d4d039e6db99e51fa27cf8c4ac5665f4292244432aff97ab9423a7fc93a039655b59c38f0ccc92ffeff47d85845dd16fa87e4ffe2ce6e0a30143144e0811a1f7537e82bd3af2352ad7d2bbeb86d391",8);for(S=b(e),y=m[0],k=m[1],M=m[2],x=m[3],l=0;S.length>l;l+=16)v=y,s=k,p=M,g=x,y=o(y,k,M,x,S[l+0],7,m[4]),x=o(x,y,k,M,S[l+1],12,m[5]),M=o(M,x,y,k,S[l+2],17,m[6]),k=o(k,M,x,y,S[l+3],22,m[7]),y=o(y,k,M,x,S[l+4],7,m[8]),x=o(x,y,k,M,S[l+5],12,m[9]),M=o(M,x,y,k,S[l+6],17,m[10]),k=o(k,M,x,y,S[l+7],22,m[11]),y=o(y,k,M,x,S[l+8],7,m[12]),x=o(x,y,k,M,S[l+9],12,m[13]),M=o(M,x,y,k,S[l+10],17,m[14]),k=o(k,M,x,y,S[l+11],22,m[15]),y=o(y,k,M,x,S[l+12],7,m[16]),x=o(x,y,k,M,S[l+13],12,m[17]),M=o(M,x,y,k,S[l+14],17,m[18]),k=o(k,M,x,y,S[l+15],22,m[19]),y=d(y,k,M,x,S[l+1],5,m[20]),x=d(x,y,k,M,S[l+6],9,m[21]),M=d(M,x,y,k,S[l+11],14,m[22]),k=d(k,M,x,y,S[l+0],20,m[23]),y=d(y,k,M,x,S[l+5],5,m[24]),x=d(x,y,k,M,S[l+10],9,m[25]),M=d(M,x,y,k,S[l+15],14,m[26]),k=d(k,M,x,y,S[l+4],20,m[27]),y=d(y,k,M,x,S[l+9],5,m[28]),x=d(x,y,k,M,S[l+14],9,m[29]),M=d(M,x,y,k,S[l+3],14,m[30]),k=d(k,M,x,y,S[l+8],20,m[31]),y=d(y,k,M,x,S[l+13],5,m[32]),x=d(x,y,k,M,S[l+2],9,m[33]),M=d(M,x,y,k,S[l+7],14,m[34]),k=d(k,M,x,y,S[l+12],20,m[35]),y=u(y,k,M,x,S[l+5],4,m[36]),x=u(x,y,k,M,S[l+8],11,m[37]),M=u(M,x,y,k,S[l+11],16,m[38]),k=u(k,M,x,y,S[l+14],23,m[39]),y=u(y,k,M,x,S[l+1],4,m[40]),x=u(x,y,k,M,S[l+4],11,m[41]),M=u(M,x,y,k,S[l+7],16,m[42]),k=u(k,M,x,y,S[l+10],23,m[43]),y=u(y,k,M,x,S[l+13],4,m[44]),x=u(x,y,k,M,S[l+0],11,m[45]),M=u(M,x,y,k,S[l+3],16,m[46]),k=u(k,M,x,y,S[l+6],23,m[47]),y=u(y,k,M,x,S[l+9],4,m[48]),x=u(x,y,k,M,S[l+12],11,m[49]),M=u(M,x,y,k,S[l+15],16,m[50]),k=u(k,M,x,y,S[l+2],23,m[51]),y=i(y,k,M,x,S[l+0],6,m[52]),x=i(x,y,k,M,S[l+7],10,m[53]),M=i(M,x,y,k,S[l+14],15,m[54]),k=i(k,M,x,y,S[l+5],21,m[55]),y=i(y,k,M,x,S[l+12],6,m[56]),x=i(x,y,k,M,S[l+3],10,m[57]),M=i(M,x,y,k,S[l+10],15,m[58]),k=i(k,M,x,y,S[l+1],21,m[59]),y=i(y,k,M,x,S[l+8],6,m[60]),x=i(x,y,k,M,S[l+15],10,m[61]),M=i(M,x,y,k,S[l+6],15,m[62]),k=i(k,M,x,y,S[l+13],21,m[63]),y=i(y,k,M,x,S[l+4],6,m[64]),x=i(x,y,k,M,S[l+11],10,m[65]),M=i(M,x,y,k,S[l+2],15,m[66]),k=i(k,M,x,y,S[l+9],21,m[67]),y=n(y,v),k=n(k,s),M=n(M,p),x=n(x,g);return h(y).concat(h(k),h(M),h(x))},T=function(){var e="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",r=e.split(""),n=function(e){var n,f,c=[],t="";for(Math.floor(16*e.length/3),n=0;16*e.length>n;n++)c.push(e[Math.floor(n/16)][n%16]);for(n=0;c.length>n;n+=3)t+=r[c[n]>>2],t+=r[(3&c[n])<<4|c[n+1]>>4],t+=void 0!==c[n+1]?r[(15&c[n+1])<<2|c[n+2]>>6]:"=",t+=void 0!==c[n+2]?r[63&c[n+2]]:"=";for(f=t.slice(0,64)+"\n",n=1;Math.ceil(t.length/64)>n;n++)f+=t.slice(64*n,64*n+64)+(Math.ceil(t.length/64)===n+1?"":"\n");return f},f=function(r){r=r.replace(/\n/g,"");var n,f=[],c=[],t=[];for(n=0;r.length>n;n+=4)c[0]=e.indexOf(r.charAt(n)),c[1]=e.indexOf(r.charAt(n+1)),c[2]=e.indexOf(r.charAt(n+2)),c[3]=e.indexOf(r.charAt(n+3)),t[0]=c[0]<<2|c[1]>>4,t[1]=(15&c[1])<<4|c[2]>>2,t[2]=(3&c[2])<<6|c[3],f.push(t[0],t[1],t[2]);return f=f.slice(0,f.length-f.length%16)};return"function"==typeof Array.indexOf&&(e=r),{encode:n,decode:f}}();return{size:i,h2a:d,expandKey:S,encryptBlock:s,decryptBlock:p,Decrypt:n,s2a:u,rawEncrypt:l,rawDecrypt:v,dec:H,openSSLKey:h,a2h:o,enc:G,Hash:{MD5:L},Base64:T}});
743
    </script>'
744
            );
745
746
            fclose($outstream);
747
748
            echo '[{"text":"<a href=\''.
749
                cleanText($post_file_link, "css").
750
                '\' target=\'_blank\'>'.$LANG['pdf_download'].'</a>"}]';
751
            break;
752
    }
753
}
754
755
//SPECIFIC FUNCTIONS FOR FPDF
756
function checkPageBreak($height)
757
{
758
    global $pdf;
759
    //Continue on a new page if needed
760
    if ($pdf->GetY() + $height > $pdf->PageBreakTrigger) {
761
        $pdf->addPage($pdf->CurOrientation);
762
    }
763
}
764
765
function nbLines($width, $txt)
766
{
767
    global $pdf;
768
    //Calculate the number of lines needed by a Multicell with a width of w
769
    if ($width == 0) {
770
        $width = $pdf->w - $this->rMargin - $pdf->x;
0 ignored issues
show
Bug introduced by
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
771
    }
772
    $wmax = ($width - 2 * $pdf->cMargin) * 1000 / $pdf->FontSize;
773
    $s_text = str_replace("\r", '', $txt);
774
    $nb_char = strlen($s_text);
775
    if ($nb_char > 0 and $s_text[$nb_char - 1] == "\n") {
776
        $nb_char--;
777
    }
778
    $sep = -1;
779
    $var_i = 0;
780
    $var_j = 0;
781
    $var_l = 0;
782
    $var_nl = 1;
783
    while ($var_i < $nb_char) {
784
        $var_c = $s_text[$var_i];
785
        if ($var_c == "\n") {
786
            $var_i++;
787
            $sep = -1;
788
            $var_j = $var_i;
789
            $var_l = 0;
790
            $var_nl++;
791
            continue;
792
        }
793
        if ($var_c == ' ') {
794
            $sep = $var_i;
795
        }
796
        $var_l += 550;
797
        if ($var_l > $wmax) {
798
            if ($sep == -1) {
799
                if ($var_i == $var_j) {
800
                    $var_i++;
801
                }
802
            } else {
803
                $var_i = $sep + 1;
804
            }
805
            $sep = -1;
806
            $var_j = $var_i;
807
            $var_l = 0;
808
            $var_nl++;
809
        } else {
810
            $var_i++;
811
        }
812
    }
813
814
    return $var_nl;
815
}
816