Completed
Push — master ( 66eec9...6b9acc )
by Cody
10s
created

greenText()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
  date_default_timezone_set('UTC');
3
  if (isset($_POST['submit'])) {
4
    if (isset($_POST['message']) && !empty($_POST['message'])) {
5
      /* -----------------------*
6
      *        FILE UPLOAD      *
7
      * ----------------------- */
8
      if (isset($_FILES['file']['name']) && $_FILES['file']['name'] != "") {
9
        $target_dir = "images/";
10
        $target_file_for_base_name = explode(".", $_FILES["file"]["name"]); //sorry, this is horrid, I know.
11
        $target_file_for_base_name = $last_msg + 1 . "." . $target_file_for_base_name[1];
12
        $target_file = $target_dir . basename($target_file_for_base_name);
13
        $uploadOk = 1;
14
        $imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
15
        $check = getimagesize($_FILES["file"]["tmp_name"]);
16
        if ($check !== false) {
17
          $uploadOk = 1;
18
        } else {
19
          echo "what are you doing anon? that file was not an image or a webm...";
20
          $uploadOk = 0;
21
        }
22
23
        //limit file size
24
        if ($_FILES["file"]["size"] > $max_file_size) {
25
            echo "Your file is too large.";
26
            $uploadOk = 0;
27
        }
28
29
        // Allow certain file formats
30
        if ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" && $imageFileType != "webm") {
31
            echo "Only JPG, JPEG, PNG & GIF files are allowed.";
32
            $uploadOk = 0;
33
        }
34
35
        // Upload the file
36
        if ($uploadOk == 0) {
37
            echo "Sorry, your file was not uploaded.";
38
        } else {
39
            move_uploaded_file($_FILES["file"]["tmp_name"], $target_file);
40
        }
41
      }
42
43
      /* ---------------------------*
44
      *        END FILE UPLOAD      *
45
      * --------------------------- */
46
      $file_name_for_db = '';
47
      if (isset($target_file_for_base_name)) {
48
        $file_name_for_db = $target_file_for_base_name;
49
      }
50
51
      $new_message = array(
52
        'id' => $last_msg + 1,
53
        'message' => nl2br(greenText(str_replace("<", "&lt", $_POST['message'])), false),
54
        'file' => $file_name_for_db,
55
        'date' => date("Y/m/d") . " at " . date("h:i:s A") . " UTC"
56
      );
57
58
      array_push($messages["messages"], $new_message);
59
      $jsondata = json_encode($messages, JSON_PRETTY_PRINT);
60
      file_put_contents($file, $jsondata);
61
    } else {
62
      echo "The message field is <b>required</b>.";
63
    }
64
  }
65
66
  function greenText($msg){
67
    preg_match_all('/>.*/', $msg, $matches, PREG_SET_ORDER, 0);
68
69
    foreach ($matches as $m) {
70
      $msg = str_replace($m[0], '<span style="color:#789922; display:inline;">' . $m[0] . '</span>', $msg);
71
    }
72
73
    return str_replace("\n", "", $msg);
74
  }
75