summaryrefslogtreecommitdiff
path: root/includes/function.inc
blob: 0215955d076b16e2cd3dddede32acdc2094cc008 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?

function id2story($id) {
  ### Perform query:
  $result = db_query("SELECT s.*, u.userid FROM stories s LEFT JOIN users u ON s.author = u.id WHERE s.id = $id");
  return db_fetch_object($result);
}

function load_theme() {
  global $user, $themes;

  if ($user->theme && file_exists($themes[$user->theme][0])) {
    include $themes[$user->theme][0];     
  }
  else {
    include $themes[key($themes)][0];
  }
  return new Theme();
}

function check_field($message) {
  return str_replace("\"", "&quot;", stripslashes($message));
}

function check_input($message) {
  global $allowed_html;
  return strip_tags(addslashes($message), $allowed_html);
}

function check_output($message, $nl2br = 0) {
  global $allowed_html;
  if ($nl2br == 1) return nl2br(strip_tags(stripslashes($message), $allowed_html));
  else return strip_tags(stripslashes($message), $allowed_html);
}

function discussion_num_replies($id, $count = 0) {
  $result = db_query("SELECT COUNT(cid) FROM comments WHERE pid = $id");
  return ($result) ? db_result($result, 0) : 0;
}

function discussion_num_filtered($sid, $pid) {
  global $user;

  $threshold = ($user->id) ? $user->threshold  : "0"; 
  $pid = ($pid) ? $pid : 0;

  $result = db_query("SELECT COUNT(cid) FROM comments WHERE sid = $sid AND pid = $pid AND (votes != 0 AND score / votes < $threshold)");
  return ($result) ? db_result($result, 0) : 0;
}

function format_plural($count, $singular, $plural) {
  return ($count == 1) ? "$count $singular" : "$count $plural";
}

function format_date($timestamp, $type = "medium") {
  global $user;

  $timestamp += ($user->timezone) ? $user->timezone - date("Z") : 0;

  switch ($type) {
    case "small":
      $date = date("D, m/d/y - H:i", $timestamp);
      break;
    case "medium":
      $date = date("l, m/d/Y - H:i", $timestamp);
      break;
    case "large":
      $date = date("D, M d, Y - H:i", $timestamp);
      break;
    case "extra large":
      $date = date("l, F dS, Y - H:i", $timestamp);
      break;
    default:
      $date = date("D, M d, Y - H:i", $timestamp);
  }
  return $date;
}

function format_data($field, $replacement = "<I>na</I>") {
  return ($field) ? $field : $replacement;
}

function format_username($username, $admin = 0) {
  if ($username) return ($admin) ? "<A HREF=\"admin.php?section=accounts&op=view&name=$username\">$username</A>" : "<A HREF=\"account.php?op=view&name=$username\">$username</A>";
  else { global $anonymous; return $anonymous; }
}

function format_email($address) {
  return ($address) ? "<A HREF=\"mailto:$address\">$address</A>" : format_data($address);
}

function format_url($address, $description = "") {
   // POSSIBLE EXTENSIONS:
   //   1. add `http://' in case it's missing.
   //   2. add a trailing `/' in case it's missing.
   //   3. remove any parameters in the URI.
  $description = ($description) ? $description : $address;
  return ($address) ? "<A HREF=\"$address\">$description</A>" : format_data($address);
}

?>