jugaad_model::update_file_data()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 6
nop 3
dl 0
loc 15
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
/*
4
 This file is too big. Can be broken in multiple models.
5
 One containing all the file operations,
6
 another with all the operations related to templating and data.
7
 */
8
9
class jugaad_model extends Model {
10
11
    public function __construct() {
12
        $this->load_library("db_lib");
13
    }
14
15
    public function new_file($parent, $slug, $type, $default_role, $template, $user) {
16
        $db_error = false;
17
        $this->DB->jugaad->autocommit(false);
0 ignored issues
show
Bug Best Practice introduced by
The property $DB is declared private in Model. Since you implement __get, consider adding a @property or @property-read.
Loading history...
18
19
        $stmt = $this->db_lib->prepared_execute(
0 ignored issues
show
Bug Best Practice introduced by
The property db_lib does not exist on jugaad_model. Since you implemented __get, consider adding a @property annotation.
Loading history...
20
            $this->DB->jugaad,
21
            "INSERT INTO `files` (`slug`, `parent`, `type`, `default_role`, `template`) VALUES (?, ?, ?, ?, ?)",
22
            "sisss",
23
            [$slug, $parent, $type, $default_role, $template]
24
        );
25
        if (!$stmt) {
26
            $db_error = true;
27
        }
28
29
        if (!$db_error) {
30
            $insert_id = $stmt->insert_id;
31
32
            $stmt = $this->db_lib->prepared_execute(
33
                $this->DB->jugaad,
34
                "INSERT INTO `file_versions` (`file_id`, `action`, `created_by`) VALUES (?, 'create', ?)",
35
                "is",
36
                [$insert_id, $user]
37
            );
38
            if (!$stmt) {
39
                $db_error = true;
40
            }
41
        }
42
43
        if ($db_error) {
44
            $this->DB->jugaad->rollback();
45
        } else {
46
            $this->DB->jugaad->commit();
47
        }
48
49
        $this->DB->jugaad->autocommit(true);
50
        return !$db_error;
51
    }
52
53
    public function update_file($file_id, $slug, $data, $template, $user) {
54
        if ($file_id === false) {
55
            return false;
56
        }
57
        $file_type = $this->get_file_type($file_id);
58
        if ($file_type === false) {
59
            return false;
60
        }
61
        $db_error = false;
62
        $this->DB->jugaad->autocommit(false);
0 ignored issues
show
Bug Best Practice introduced by
The property $DB is declared private in Model. Since you implement __get, consider adding a @property or @property-read.
Loading history...
63
64
        $stmt = $this->db_lib->prepared_execute(
0 ignored issues
show
Bug Best Practice introduced by
The property db_lib does not exist on jugaad_model. Since you implemented __get, consider adding a @property annotation.
Loading history...
65
            $this->DB->jugaad,
66
            "UPDATE `files` SET `slug`=?, `template`=? WHERE `id`=?",
67
            "ssi",
68
            [$slug, $template, $file_id]
69
        );
70
        if (!$stmt) {
71
            $db_error = true;
72
        }
73
74
        if ($file_type != 'directory') {
75
            $stmt = $this->db_lib->prepared_execute(
76
                $this->DB->jugaad,
77
                "INSERT INTO `file_versions` (`file_id`, `action`, `created_by`) VALUES (?, 'edit', ?)",
78
                "is",
79
                [$file_id, $user]
80
            );
81
            if (!$stmt) {
82
                $db_error = true;
83
            }
84
85
            if (!$db_error) {
86
                $version_id = $stmt->insert_id;
87
                $db_error = $this->update_file_data($file_id, $version_id, $data);
88
            }
89
        }
90
91
        if ($db_error) {
92
            $this->DB->jugaad->rollback();
93
        } else {
94
            $this->DB->jugaad->commit();
95
        }
96
97
        $this->DB->jugaad->autocommit(true);
98
        return !$db_error;
99
    }
100
101
    public function delete_file($file_id, $user) {
102
        if ($file_id === false || $file_id <= 0) {
103
            return false;
104
        }
105
106
        $db_error = false;
107
        $this->DB->jugaad->autocommit(false);
0 ignored issues
show
Bug Best Practice introduced by
The property $DB is declared private in Model. Since you implement __get, consider adding a @property or @property-read.
Loading history...
108
109
        $stmt = $this->db_lib->prepared_execute(
0 ignored issues
show
Bug Best Practice introduced by
The property db_lib does not exist on jugaad_model. Since you implemented __get, consider adding a @property annotation.
Loading history...
110
            $this->DB->jugaad,
111
            "INSERT INTO `trash_files` (`file_id`, `slug`, `parent`, `type`, `created_by`) SELECT `id`, `slug`, `parent`, `type`, ? FROM `files` WHERE `id`=?",
112
            "si",
113
            [$user, $file_id]
114
        );
115
        if (!$stmt) {
116
            $db_error = true;
117
        }
118
119
        $stmt = $this->db_lib->prepared_execute(
120
            $this->DB->jugaad,
121
            "DELETE FROM `files` WHERE `id`=?",
122
            "i",
123
            [$file_id]
124
        );
125
        if (!$stmt) {
126
            $db_error = true;
127
        }
128
129
        $stmt = $this->db_lib->prepared_execute(
130
            $this->DB->jugaad,
131
            "INSERT INTO `file_versions` (`file_id`, `action`, `created_by`) VALUES (?, 'delete', ?)",
132
            "is",
133
            [$file_id, $user]
134
        );
135
        if (!$stmt) {
136
            $db_error = true;
137
        }
138
139
        if ($db_error) {
140
            $this->DB->jugaad->rollback();
141
        } else {
142
            $this->DB->jugaad->commit();
143
        }
144
145
        $this->DB->jugaad->autocommit(true);
146
        return !$db_error;
147
    }
148
149
    public function recover_file($file_id, $user) {
150
        if ($file_id === false || $file_id <= 0) {
151
            return false;
152
        }
153
154
        $db_error = false;
155
        $this->DB->jugaad->autocommit(false);
0 ignored issues
show
Bug Best Practice introduced by
The property $DB is declared private in Model. Since you implement __get, consider adding a @property or @property-read.
Loading history...
156
157
        $stmt = $this->db_lib->prepared_execute(
0 ignored issues
show
Bug Best Practice introduced by
The property db_lib does not exist on jugaad_model. Since you implemented __get, consider adding a @property annotation.
Loading history...
158
            $this->DB->jugaad,
159
            "INSERT INTO `files` (`id`, `slug`, `parent`, `type`) SELECT `file_id`, `slug`, `parent`, `type` FROM `trash_files` WHERE `file_id`=?",
160
            "i",
161
            [$file_id]
162
        );
163
        if (!$stmt) {
164
            $db_error = true;
165
        }
166
167
        $stmt = $this->db_lib->prepared_execute(
168
            $this->DB->jugaad,
169
            "DELETE FROM `trash_files` WHERE `file_id`=?",
170
            "i",
171
            [$file_id]
172
        );
173
        if (!$stmt) {
174
            $db_error = true;
175
        }
176
177
        $stmt = $this->db_lib->prepared_execute(
178
            $this->DB->jugaad,
179
            "INSERT INTO `file_versions` (`file_id`, `action`, `created_by`) VALUES (?, 'recover', ?)",
180
            "is",
181
            [$file_id, $user]
182
        );
183
        if (!$stmt) {
184
            $db_error = true;
185
        }
186
187
        if ($db_error) {
188
            $this->DB->jugaad->rollback();
189
        } else {
190
            $this->DB->jugaad->commit();
191
        }
192
193
        $this->DB->jugaad->autocommit(true);
194
        return !$db_error;
195
    }
196
197
    public function get_slug_id($parent, $slug) {
198
        $stmt = $this->db_lib->prepared_execute(
0 ignored issues
show
Bug Best Practice introduced by
The property db_lib does not exist on jugaad_model. Since you implemented __get, consider adding a @property annotation.
Loading history...
199
            $this->DB->jugaad,
0 ignored issues
show
Bug Best Practice introduced by
The property $DB is declared private in Model. Since you implement __get, consider adding a @property or @property-read.
Loading history...
200
            "SELECT `id` FROM `files` WHERE `parent`=? AND `slug`=?",
201
            "is",
202
            [$parent, $slug]
203
        );
204
        if (!$stmt) {
205
            return false;
206
        }
207
208
        if ($row = $stmt->get_result()->fetch_row()) {
209
            return $row[0];
210
        }
211
        return false;
212
    }
213
214
    public function get_path_id($path) {
215
        $path = array_filter($path);
216
        $parent = 0;
217
        foreach ($path as $component) {
218
            $parent = $this->get_slug_id($parent, $component);
219
            if ($parent === false) {
220
                return false;
221
            }
222
        }
223
        return $parent;
224
    }
225
226
    public function get_file_path($file_id, $include_trash = false) {
227
        if ($file_id === false) {
228
            return false;
229
        }
230
        if ($file_id == 0) {
231
            return '/';
232
        }
233
        $path = '/';
234
        do {
235
            $file = $this->get_file($file_id, $include_trash);
236
            $file_id = $file['parent'];
237
            $path = '/' . $file['slug'] . $path;
238
        } while ($file_id !== 0);
239
        return $path;
240
    }
241
242
    public function get_file_type($file_id) {
243
        if ($file_id === false) {
244
            return false;
245
        }
246
        $stmt = $this->db_lib->prepared_execute(
0 ignored issues
show
Bug Best Practice introduced by
The property db_lib does not exist on jugaad_model. Since you implemented __get, consider adding a @property annotation.
Loading history...
247
            $this->DB->jugaad,
0 ignored issues
show
Bug Best Practice introduced by
The property $DB is declared private in Model. Since you implement __get, consider adding a @property or @property-read.
Loading history...
248
            "SELECT `type` FROM `files` WHERE `id`=?",
249
            "i",
250
            [$file_id]
251
        );
252
        if (!$stmt) {
253
            return false;
254
        }
255
        if ($row = $stmt->get_result()->fetch_row()) {
256
            return $row[0];
257
        }
258
        return false;
259
    }
260
261
    public function get_directory($file_id, $regex = false, $type = false, $template = false) {
262
        // Get list of files in directory
263
        if ($file_id === false) {
264
            return false;
265
        }
266
267
        $query = "SELECT `id`, `slug`, `parent`, `type`, `template` FROM `files` WHERE `parent`=?";
268
        $param_types = "i";
269
        $params = [$file_id];
270
271
        if ($regex !== false) {
272
            $query .= " AND `slug` RLIKE ?";
273
            $param_types .= "s";
274
            $params[] = $regex;
275
        }
276
277
        if ($type !== false) {
278
            $query .= " AND `type`=?";
279
            $param_types .= "s";
280
            $params[] = $type;
281
282
            if ($type == 'file' && $template !== false) {
283
                $query .= " AND `template` RLIKE ?";
284
                $param_types .= "s";
285
                $params[] = $template;
286
            }
287
        } elseif ($template !== false) {
288
            $query .= " AND ((`type`='file' AND `template` RLIKE ?) OR `type`!='file')";
289
            $param_types .= "s";
290
            $params[] = $template;
291
        }
292
293
        $stmt = $this->db_lib->prepared_execute(
0 ignored issues
show
Bug Best Practice introduced by
The property db_lib does not exist on jugaad_model. Since you implemented __get, consider adding a @property annotation.
Loading history...
294
            $this->DB->jugaad,
0 ignored issues
show
Bug Best Practice introduced by
The property $DB is declared private in Model. Since you implement __get, consider adding a @property or @property-read.
Loading history...
295
            $query,
296
            $param_types,
297
            $params
298
        );
299
        if (!$stmt) {
300
            return false;
301
        }
302
303
        $page_list = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
304
        return $page_list;
305
    }
306
307
    public function get_file($file_id, $include_trash = false) {
308
        // Get details of file
309
        if ($file_id === false) {
310
            return false;
311
        }
312
        $stmt = $this->db_lib->prepared_execute(
0 ignored issues
show
Bug Best Practice introduced by
The property db_lib does not exist on jugaad_model. Since you implemented __get, consider adding a @property annotation.
Loading history...
313
            $this->DB->jugaad,
0 ignored issues
show
Bug Best Practice introduced by
The property $DB is declared private in Model. Since you implement __get, consider adding a @property or @property-read.
Loading history...
314
            "SELECT `id`, `slug`, `parent`, `type`, `default_role`, `template` FROM `files` WHERE `id`=?",
315
            "i",
316
            [$file_id]
317
        );
318
        if (!$stmt) {
319
            return false;
320
        }
321
        if ($row = $stmt->get_result()->fetch_assoc()) {
322
            return $row;
323
        } elseif ($include_trash && $row = $this->get_file_trashed($file_id)) {
324
            $row['trashed'] = true;
325
            return $row;
326
        }
327
        return false;
328
    }
329
330
    public function get_file_trashed($file_id) {
331
        // Get details of a trashed file
332
        if ($file_id === false) {
333
            return false;
334
        }
335
        $stmt = $this->db_lib->prepared_execute(
0 ignored issues
show
Bug Best Practice introduced by
The property db_lib does not exist on jugaad_model. Since you implemented __get, consider adding a @property annotation.
Loading history...
336
            $this->DB->jugaad,
0 ignored issues
show
Bug Best Practice introduced by
The property $DB is declared private in Model. Since you implement __get, consider adding a @property or @property-read.
Loading history...
337
            "SELECT `file_id` as `id`, `slug`, `parent`, `type` FROM `trash_files` WHERE `file_id`=?",
338
            "i",
339
            [$file_id]
340
        );
341
        if (!$stmt) {
342
            return false;
343
        }
344
        if ($row = $stmt->get_result()->fetch_assoc()) {
345
            return $row;
346
        }
347
        return false;
348
    }
349
350
    /**
351
     * Recursively get files
352
     * @param  Array  $parent_dir Assosiative array containing atleast `id` and `path`
353
     * @param  string $type       Type (e.g. 'directory' or 'file')
354
     * @param  string $template   Regex for template name
355
     * @return array              Array of files
356
     */
357
    private function get_files_recursive($parent_dir, $type = false, $template = false) {
358
        $files = $this->get_directory(
359
            $parent_dir["id"],
360
            false,
361
            ($type == 'directory') ? 'directory' : false,
362
            $template
363
        );
364
365
        $return_files = [];
366
367
        if ($files) {
368
            foreach ($files as $file) {
369
                $file["path"] = $parent_dir["path"] . $file["slug"] . "/";
370
371
                if ($type === false || $type == $file["type"]) {
372
                    $return_files[] = $file;
373
                }
374
375
                if ($file["type"] == "directory") {
376
                    $return_files = array_merge(
377
                        $return_files,
378
                        $this->get_files_recursive($file, $type, $template)
0 ignored issues
show
Bug introduced by
It seems like $type can also be of type false; however, parameter $type of jugaad_model::get_files_recursive() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

378
                        $this->get_files_recursive($file, /** @scrutinizer ignore-type */ $type, $template)
Loading history...
Bug introduced by
It seems like $template can also be of type false; however, parameter $template of jugaad_model::get_files_recursive() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

378
                        $this->get_files_recursive($file, $type, /** @scrutinizer ignore-type */ $template)
Loading history...
379
                    );
380
                }
381
            }
382
        }
383
384
        return $return_files;
385
    }
386
387
    /**
388
     * Get all files satisfying regex path
389
     * @param  string $path     Path string
390
     * @param  string $template Template regex
391
     * @return array            Array of files
392
     */
393
    private function expand_regex_paths($path, $template = false) {
394
        $path = array_values(array_filter(explode("/", $path)));
395
        if (is_string($template)) {
396
            $template = "^" . $template . "$";
397
        }
398
399
        // Directories still satisfying the regex
400
        // Start with root
401
        $directories = [
402
            [
403
                "id" => 0,
404
                "path" => "/"
405
            ]
406
        ];
407
408
        $len = count($path);
409
        $last_segment = false;
410
411
        $files = [];
412
413
        for ($i = 0; $i < $len; $i++) {
414
            // Check if it is the last segment
415
            if ($i == $len - 1) {
416
                $last_segment = true;
417
            }
418
419
            $files = [];
420
421
            foreach ($directories as $parent) {
422
                $query_type = $last_segment ? "file" : "directory";
423
                $query_template = $last_segment ? $template : false;
424
425
                if ($path[$i] == "**") {
426
                    $new_files = $this->get_files_recursive(
427
                        $parent,
428
                        $query_type,
429
                        $query_template
0 ignored issues
show
Bug introduced by
It seems like $query_template can also be of type false; however, parameter $template of jugaad_model::get_files_recursive() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

429
                        /** @scrutinizer ignore-type */ $query_template
Loading history...
430
                    );
431
                } else {
432
                    $new_files = $this->get_directory(
433
                        $parent["id"],
434
                        "^" . $path[$i] . "$",
435
                        $query_type,
436
                        $query_template
437
                    );
438
                }
439
440
                if (!$new_files) {
441
                    continue;
442
                }
443
444
                foreach ($new_files as $file) {
445
                    if (!isset($file["path"])) {
446
                        $file["path"] = $parent["path"] . $file["slug"] . "/";
447
                    }
448
                    array_push($files, $file);
449
                }
450
            }
451
452
            if (!$last_segment) {
453
                $directories = $files;
454
            }
455
        }
456
457
        return $files;
458
    }
459
460
    private function eval_regex_preprocessor($preprocessor, $file_info) {
461
        $pre_data = [];
462
        $pre_data["path"] = array_filter(explode("/", $file_info["path"]));
463
        $pre_data["template"] = $file_info["template"];
464
465
        // Remove `{{` and `}}`
466
        $location = substr($preprocessor, 2, -2);
467
468
        // Get variable name and offsets
469
        if (preg_match('/^([a-z0-9_]+)(\[(-?\d+)?(?:([:])(-?\d+)?)?\])?$/i', $location, $matches)) {
470
            $identifier = (count($matches) >= 2) ? $matches[1] : false;
471
472
            $is_array_access = (count($matches) >= 3 && $matches[2]) ? true : false;
473
            $start_index = (count($matches) >= 4) ? intval($matches[3]) : 0;
474
            $is_array_range = (count($matches) >= 5 && $matches[4]) ? true : false;
475
            $end_index = (count($matches) >= 6) ? intval($matches[5]) : null;
476
477
            if (!array_key_exists($identifier, $pre_data)) {
478
                // Something wrong
479
                return "";
480
            }
481
482
            $ret_value = $pre_data[$identifier];
483
            $is_ret_array = is_array($ret_value);
484
485
            if (!$is_ret_array) {
486
                $ret_value = str_split($ret_value);
487
            }
488
489
            if ($is_array_access) {
490
                if ($is_array_range) {
491
                    if ($end_index !== null && $end_index < 0) {
492
                        $array_length = count($ret_value);
493
                        $end_index = $array_length + $end_index - 1;
494
                    }
495
                    $length = ($end_index === null) ? null : ($end_index - $start_index + 1);
496
                    if ($length < 0) {
497
                        $length = 0;
498
                    }
499
                } else {
500
                    $length = 1;
501
                }
502
503
                $ret_value = array_slice($ret_value, $start_index, $length);
504
            }
505
506
            if ($is_ret_array) {
507
                $ret_value = implode("/", $ret_value);
508
            } else {
509
                $ret_value = implode("", $ret_value);
510
            }
511
512
            return $ret_value;
513
        }
514
515
        return "";
516
    }
517
518
    private function preprocess_regex($regex, $file_info) {
519
        while (preg_match('/\{\{.*\}\}/i', $regex, $matches)) {
520
            $replacement = $this->eval_regex_preprocessor($matches[0], $file_info);
521
            $regex = str_replace($matches[0], $replacement, $regex);
522
        }
523
524
        return $regex;
525
    }
526
527
    private function get_external_data($file_id, $meta, $user, $version_id_only = false) {
528
        if (empty($meta["path"])) {
529
            return false;
530
        }
531
532
        $path = $meta["path"];
533
        $data = $meta["data"];
534
        $template = isset($meta["template"]) ? $meta["template"] : false;
535
536
        $current_file = $this->get_file($file_id);
537
        $current_file["path"] = $this->get_file_path($file_id);
538
        $path = $this->preprocess_regex($path, $current_file);
539
        $template = $this->preprocess_regex($template, $current_file);
540
541
        // Get external files
542
        $files = $this->expand_regex_paths($path, $template);
543
544
        $this->load_model("perms_model");
545
546
        // Get data for external files
547
        if ($version_id_only === true) {
548
            $ext_data = 0;
549
        } else {
550
            $ext_data = [];
551
        }
552
553
        foreach ($files as $file) {
554
            // Check file permission and discard if user does not have enough permissions
555
            $user_can = $this->perms_model->get_permissions($file["id"], $user);
0 ignored issues
show
Bug Best Practice introduced by
The property perms_model does not exist on jugaad_model. Since you implemented __get, consider adding a @property annotation.
Loading history...
556
            if (!$user_can['read_file']) {
557
                continue;
558
            }
559
560
            if ($version_id_only === true) {
561
                $ext_data = max($ext_data, $this->get_latest_version_id($file["id"]));
562
            } else {
563
                $ext_file = [];
564
                $ext_file["slug"] = $file["slug"];
565
                $ext_file["path"] = $file["path"];
566
                $ext_file["template"] = $file["template"];
567
                // TODO: $ext_file["url"] to acount for cannonical paths, e.g. index
568
                $ext_file["data"] = [];
569
570
                foreach ($data as $name => $ext_name) {
571
                    $ext_file["data"][$name] =
572
                        $this->get_field_value($file["id"], $ext_name, false, false);
573
                }
574
575
                $ext_data[] = $ext_file;
576
            }
577
        }
578
579
        return $ext_data;
580
    }
581
582
    private function get_field_value($file_id, $name, $meta = false, $user = false) {
583
        if (!empty($meta) && $meta["type"] == "external") {
584
            return $this->get_external_data($file_id, $meta, $user);
585
        }
586
587
        // Else, it is normal data, get it from database
588
        $stmt = $this->db_lib->prepared_execute(
0 ignored issues
show
Bug Best Practice introduced by
The property db_lib does not exist on jugaad_model. Since you implemented __get, consider adding a @property annotation.
Loading history...
589
            $this->DB->jugaad,
0 ignored issues
show
Bug Best Practice introduced by
The property $DB is declared private in Model. Since you implement __get, consider adding a @property or @property-read.
Loading history...
590
            "SELECT `value` FROM `file_data` WHERE `file_id`=? AND `name`=? ORDER BY `id` DESC LIMIT 1",
591
            "is",
592
            [$file_id, $name]
593
        );
594
        if (!$stmt) {
595
            return false;
596
        }
597
        if ($row = $stmt->get_result()->fetch_row()) {
598
            return $row[0] ? json_decode($row[0], true) : $row[0];
599
        }
600
        return false;
601
    }
602
603
    public function get_file_data($file_id, $template_meta, $user, $return_default = true) {
604
        // Get data for file based on template meta given
605
        if ($file_id === false || !is_array($template_meta)) {
606
            return false;
607
        }
608
        $data = [];
609
        foreach ($template_meta as $name => $meta) {
610
            $field = $this->get_field_value($file_id, $name, $meta, $user);
611
            if ($field === false) {
612
                if ($return_default) {
613
                    if (isset($meta['optional']) && $meta['optional']) {
614
                        $data[$name] = @$meta['default'] ?: '';
615
                    } else {
616
                        $data[$name] = @$meta['default'] ?: $meta['name'];
617
                    }
618
                } else {
619
                    $data[$name] = '';
620
                }
621
            } else {
622
                $data[$name] = $field;
623
            }
624
        }
625
        return $data;
626
    }
627
628
    private function update_file_data($file_id, $version_id, $data) {
629
        $db_error = false;
630
        $name = "";
631
        $value = "";
632
        $stmt = $this->DB->jugaad->prepare("INSERT INTO `file_data` (`file_id`, `version_id`, `name`, `value`) VALUES (?, ?, ?, ?)");
0 ignored issues
show
Bug Best Practice introduced by
The property $DB is declared private in Model. Since you implement __get, consider adding a @property or @property-read.
Loading history...
633
        if (!$stmt->bind_param("iiss", $file_id, $version_id, $name, $value)) {
634
            $db_error = true;
635
        }
636
        foreach ($data as $name => $val) {
637
            $value = json_encode($val);
0 ignored issues
show
Unused Code introduced by
The assignment to $value is dead and can be removed.
Loading history...
638
            if (!$stmt->execute()) {
639
                $db_error = true;
640
            }
641
        }
642
        return $db_error;
643
    }
644
645
    public function get_latest_version_id_with_external_data($file_id, $template_meta, $user) {
646
        if ($file_id === false || !is_array($template_meta)) {
647
            return false;
648
        }
649
650
        $version_id = $this->get_latest_version_id($file_id);
651
652
        $data = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $data is dead and can be removed.
Loading history...
653
        foreach ($template_meta as $name => $meta) {
654
            if ($meta["type"] == "external") {
655
                $version_id = max(
656
                    $version_id,
657
                    $this->get_external_data($file_id, $meta, $user, true)
658
                );
659
            }
660
        }
661
        return $version_id;
662
    }
663
664
    public function get_latest_version_id($file_id) {
665
        // Get latest version id
666
        if ($file_id === false) {
667
            return false;
668
        }
669
        $stmt = $this->db_lib->prepared_execute(
0 ignored issues
show
Bug Best Practice introduced by
The property db_lib does not exist on jugaad_model. Since you implemented __get, consider adding a @property annotation.
Loading history...
670
            $this->DB->jugaad,
0 ignored issues
show
Bug Best Practice introduced by
The property $DB is declared private in Model. Since you implement __get, consider adding a @property or @property-read.
Loading history...
671
            "SELECT `id` FROM `file_versions` WHERE `file_id`=? ORDER BY `id` DESC LIMIT 1",
672
            "i",
673
            [$file_id]
674
        );
675
        if (!$stmt) {
676
            return false;
677
        }
678
        if ($row = $stmt->get_result()->fetch_row()) {
679
            return $row[0];
680
        }
681
        return false;
682
    }
683
684
    public function get_history($file_id) {
685
        // Get list of edits for file
686
        if ($file_id === false) {
687
            return false;
688
        }
689
        $stmt = $this->db_lib->prepared_execute(
0 ignored issues
show
Bug Best Practice introduced by
The property db_lib does not exist on jugaad_model. Since you implemented __get, consider adding a @property annotation.
Loading history...
690
            $this->DB->jugaad,
0 ignored issues
show
Bug Best Practice introduced by
The property $DB is declared private in Model. Since you implement __get, consider adding a @property or @property-read.
Loading history...
691
            "SELECT `id`, `action`, `timestamp`, `created_by` FROM `file_versions` WHERE `file_id`=? ORDER BY `id` DESC",
692
            "i",
693
            [$file_id]
694
        );
695
        if (!$stmt) {
696
            return false;
697
        }
698
        $history_list = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
699
        return $history_list;
700
    }
701
702
    public function get_trash_list() {
703
        // Get list of files in trash
704
        $stmt = $this->DB->jugaad->prepare("SELECT `id`, `file_id`, `slug`, `parent`, `type`, `timestamp`, `created_by` FROM `trash_files` ORDER BY `timestamp` DESC");
0 ignored issues
show
Bug Best Practice introduced by
The property $DB is declared private in Model. Since you implement __get, consider adding a @property or @property-read.
Loading history...
705
        if (!$stmt->execute()) {
706
            return false;
707
        }
708
        $page_list = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
709
        foreach ($page_list as $key => $page) {
710
            $page_list[$key]['path'] = $this->get_file_path($page['file_id'], true);
711
712
            $parent = $this->get_file($page['parent'], true);
713
714
            if ($parent === false) {
715
                $page_list[$key]['recoverable'] = false;
716
                $page_list[$key]['reason'] = "Cannot find parent, the file is orphan. :/";
717
            } elseif (array_key_exists('trashed', $parent) && $parent['trashed']) {
718
                $page_list[$key]['recoverable'] = false;
719
                $page_list[$key]['reason'] = "Parent directory also in trash. Recover parent first.";
720
            } else {
721
                if ($this->get_slug_id($page['parent'], $page['slug']) !== false) {
722
                    $page_list[$key]['recoverable'] = false;
723
                    $page_list[$key]['reason'] = "A file currently exists at this path.";
724
                } else {
725
                    $page_list[$key]['recoverable'] = true;
726
                    $page_list[$key]['reason'] = "";
727
                }
728
            }
729
        }
730
        return $page_list;
731
    }
732
733
}
734