Code Duplication    Length = 37-38 lines in 2 locations

main/inc/lib/blog.lib.php 2 locations

@@ 367-403 (lines=37) @@
364
                Database::query($sql);
365
            }
366
367
            if ($has_attachment) {
368
                $courseDir = $_course['path'].'/upload/blog';
369
                $sys_course_path = api_get_path(SYS_COURSE_PATH);
370
                $updir = $sys_course_path.$courseDir;
371
372
                // Try to add an extension to the file if it hasn't one
373
                $new_file_name = add_ext_on_mime(
374
                    stripslashes($_FILES['user_upload']['name']),
375
                    $_FILES['user_upload']['type']
376
                );
377
378
                // user's file name
379
                $file_name = $_FILES['user_upload']['name'];
380
381
                if (!filter_extension($new_file_name)) {
382
                    echo Display::return_message(get_lang('UplUnableToSaveFileFilteredExtension'), 'error');
383
                } else {
384
                    $new_file_name = uniqid('');
385
                    $new_path = $updir.'/'.$new_file_name;
386
                    $result = @move_uploaded_file($_FILES['user_upload']['tmp_name'], $new_path);
387
                    $comment = Database::escape_string($file_comment);
388
                    $file_name = Database::escape_string($file_name);
389
                    $size = intval($_FILES['user_upload']['size']);
390
391
                    // Storing the attachments if any
392
                    if ($result) {
393
                        $sql = "INSERT INTO $blog_table_attachment (c_id, filename,comment, path, post_id,size, blog_id,comment_id)
394
                            VALUES ($course_id, '$file_name', '$comment', '$new_file_name', $last_post_id, $size, $blog_id, 0)";
395
                        Database::query($sql);
396
                        $id = Database::insert_id();
397
                        if ($id) {
398
                            $sql = "UPDATE $blog_table_attachment SET id = iid WHERE iid = $id";
399
                            Database::query($sql);
400
                        }
401
                    }
402
                }
403
            }
404
405
            return $last_post_id;
406
        } else {
@@ 528-565 (lines=38) @@
525
                $sql = "UPDATE $tbl_blogs_comments SET comment_id = iid WHERE iid = $last_id";
526
                Database::query($sql);
527
528
                if ($has_attachment) {
529
                    $courseDir = $_course['path'] . '/upload/blog';
530
                    $sys_course_path = api_get_path(SYS_COURSE_PATH);
531
                    $updir = $sys_course_path . $courseDir;
532
533
                    // Try to add an extension to the file if it hasn't one
534
                    $new_file_name = add_ext_on_mime(
535
                        stripslashes($_FILES['user_upload']['name']),
536
                        $_FILES['user_upload']['type']
537
                    );
538
539
                    // user's file name
540
                    $file_name = Database::escape_string($_FILES['user_upload']['name']);
541
542
                    if (!filter_extension($new_file_name)) {
543
                        echo Display::return_message(get_lang('UplUnableToSaveFileFilteredExtension'), 'error');
544
                    } else {
545
                        $new_file_name = uniqid('');
546
                        $new_path = $updir . '/' . $new_file_name;
547
                        $result = @move_uploaded_file($_FILES['user_upload']['tmp_name'], $new_path);
548
                        $comment = Database::escape_string($file_comment);
549
                        $size = intval($_FILES['user_upload']['size']);
550
551
                        // Storing the attachments if any
552
                        if ($result) {
553
                            $sql = "INSERT INTO $blog_table_attachment (c_id, filename,comment, path, post_id,size,blog_id,comment_id)
554
                                VALUES ($course_id, '$file_name', '$comment', '$new_file_name', $post_id, $size, $blog_id, $last_id)";
555
                            Database::query($sql);
556
557
                            $id = Database::insert_id();
558
559
                            if ($id) {
560
                                $sql = "UPDATE $blog_table_attachment SET id = iid WHERE iid = $id";
561
                                Database::query($sql);
562
                            }
563
                        }
564
                    }
565
                }
566
            }
567
        }
568
    }