greenText()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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