common.inc 9.27 KB
Newer Older
Dries Buytaert's avatar
   
Dries Buytaert committed
1
<?php
Dries Buytaert's avatar
 
Dries Buytaert committed
2

Dries Buytaert's avatar
   
Dries Buytaert committed
3
4
$na = "<I>na</I>";

Dries Buytaert's avatar
   
Dries Buytaert committed
5
function conf_init() {
6
  global $HTTP_HOST, $REQUEST_URI;
Dries Buytaert's avatar
   
Dries Buytaert committed
7
  $file = strtolower(strtr($HTTP_HOST ."". substr($REQUEST_URI, 0, strrpos($REQUEST_URI, "/")), "/:", ".."));
Dries Buytaert's avatar
   
Dries Buytaert committed
8
9
  while ($file && !file_exists("includes/$file.php")) $file = substr($file, 0, strrpos($file, "."));
  return $file ? $file : "setting";
10
11
}

Dries Buytaert's avatar
   
Dries Buytaert committed
12
13
14
15
16
17
18
19
20
function error_handler($errno, $message, $filename, $line, $variables) {
  $types = array(1 => "error", 2 => "warning", 4 => "parse error", 8 => "notice", 16 => "core error", 32 => "core warning", 64 => "compile error", 128 => "compile warning", 256 => "user error", 512 => "user warning", 1024 => "user notice");
  $entry = $types[$errno] .": $message in $filename on line $line.";
  if ($errno == 1 || $errno == 2 || $errno == 4) {
    watchdog("error", $types[$errno] .": $message in $filename on line $line.");
    print $entry;
  }
}

Dries Buytaert's avatar
   
Dries Buytaert committed
21
22
23
24
25
26
27
function watchdog($type, $message) {
  global $user, $watchdog, $PHP_SELF;
  $link = ($mod) ? $mod : substr(strrchr($PHP_SELF, "/"), 1, strrchr($PHP_SELF, "/") - 4);
  db_query("INSERT INTO watchdog (user, type, link, message, location, hostname, timestamp) VALUES ('$user->id', '". check_input($type) ."', '". check_input($link) ."', '". check_input($message) ."', '". check_input(getenv("REQUEST_URI")) ."', '". check_input(getenv("REMOTE_ADDR")) ."', '". time() ."')");
}

function throttle($type, $rate) {
Dries Buytaert's avatar
   
Dries Buytaert committed
28
  global $user;
Dries Buytaert's avatar
   
Dries Buytaert committed
29
30
31
32
  if (!(user_access($user, "watchdog") || user_access($user, "comment") || user_access($user, "node"))) {
    if ($throttle = db_fetch_object(db_query("SELECT * FROM watchdog WHERE type = '$type' AND hostname = '". getenv("REMOTE_ADDR") ."' AND ". time() ." - timestamp < $rate"))) {
      watchdog("warning", "throttle: '". getenv("REMOTE_ADDR") ."' exceeded submission rate - $throttle->type");
      header("Location: error.php?op=throttle");
Dries Buytaert's avatar
   
Dries Buytaert committed
33
      die("submission rate exceeded");
Dries Buytaert's avatar
   
Dries Buytaert committed
34
35
36
37
38
39
40
    }
    else {
      watchdog($type, "throttle control");
    }
  }
}

Dries Buytaert's avatar
   
Dries Buytaert committed
41
42
43
44
45
46
47
48
49
50
51
52
53
function path_uri() {
  global $HTTP_HOST, $REQUEST_URI;
  return "http://". $HTTP_HOST . substr($REQUEST_URI, 0, strrpos($REQUEST_URI, "/")) ."/";
}

function path_img() {
  // use "http://your-image-server.com/ if you want to host images on a seperate server.
  return "./images/";
}

function notice_account() {
  return t("This page requires a valid user account.  Please <A HREF=\"account.php\">create a user account</A> and <A HREF=\"account.php\">login</A> prior to accessing it.");
}
Dries Buytaert's avatar
   
Dries Buytaert committed
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

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

function check_select($message) {
  return check_textfield($message);
}

function check_export($message) {
  return check_textfield($message);
}

function check_textarea($message) {
  global $allowed_html;
  return htmlspecialchars(strip_tags(stripslashes($message), $allowed_html));
}

function check_input($message) {
  global $allowed_html;
  return strip_tags(addslashes(stripslashes(substr($message, 0, variable_get(max_input_size, 10000)))), $allowed_html);
}

function check_code($message) {
  return $message;
}

function check_mail($mail) {
  return eregi("^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3}$", $mail) ? 1 : 0;
}

function check_name($name) {
  return ereg("[^a-zA-Z0-9_-]", $name) ? 0 : 1;
}

function check_output($message, $nl2br = 0) {
  global $allowed_html, $na;
  $var = strip_tags(stripslashes(format_text($message)), $allowed_html);
  return ($var) ? (($nl2br) ? nl2br($var) : $var) : $na;
}

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

function format_interval($timestamp) {
100
101
102
103
104
105
106
  $units = array("year|years"=>31536000, "week|weeks"=>604800, "day|days"=>86400, "hour|hours"=>3600, "min|min"=>60, "sec|sec"=>1);
  foreach ($units as $key=>$value) {
    $key=explode("|", $key);
    if ($timestamp >= $value) {
      $output .= ($output ? " " : "") . format_plural(floor($timestamp / $value), $key[0], $key[1]);
      $timestamp %= $value;
    }
Dries Buytaert's avatar
   
Dries Buytaert committed
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
  }
  return ($output) ? $output : "0 sec";
}

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

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

  switch ($type) {
    case "small":
      $date = date("m/d/y - H:i", $timestamp);
      break;
    case "medium":
      $date = t(date("l", $timestamp)) .", ". date("m/d/Y - H:i", $timestamp);
      break;
    case "large":
      $date = t(date("l", $timestamp)) .", ". t(date("F", $timestamp)) ." ". date("d, Y - H:i", $timestamp);
      break;
    case "custom":
127
128
129
130
131
132
133
      for ($i=strlen($format);$i>=0;$c=$format[--$i]) {
        if (strstr("DFlMSw",$c))
          $date=t(date($c,$timestamp)).$date;
        else if (strstr("AaBdgGhHiIjLmnrstTUYyZz",$c))
          $date=date($c,$timestamp).$date;
        else $date=$c.$date;
      }
Dries Buytaert's avatar
   
Dries Buytaert committed
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
      break;
    default:
      $date = t(date("l", $timestamp)) .", ". date("m/d/Y - H:i", $timestamp);
  }
  return $date;
}

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

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

function format_url($address, $description = "") {
  global $na;
  $description = ($description) ? $description : $address;
  return ($address) ? "<A HREF=\"$address\">". check_output($description) ."</A>" : $na;
}

function format_tag($link, $text) {
  return "'<a href=\"node.php?title='. urlencode('$link') .'\">'. ('$text' ? '$text' : '$link') .'</a>'";
}

function format_text($text) {

  $src = array( // "/(<\/?)(\w+)([^>]*>)/e",             // convert HTML to lower case
               "/\[\[(([^\|]*?)(\|([^\|]*?))?)\]\]/e");  // [link|description]
  $dst = array( // "'\\1'. strtolower('\\2') .'\\3'",    // convert HTML to lower case
               format_tag('\\2', '\\4'));                // [link|description]

  return preg_replace($src, $dst, $text);
}

172
173
function form($action, $form, $method = "post", $options="") {
  return "<FORM ACTION=\"$action\" METHOD=\"$method\"". (!empty($options) ? " $options" : "") .">\n$form</FORM>\n";
Dries Buytaert's avatar
   
Dries Buytaert committed
174
175
}

Dries Buytaert's avatar
   
Dries Buytaert committed
176
function form_item($title, $value, $description = 0) {
Dries Buytaert's avatar
   
Dries Buytaert committed
177
  return ($description) ? "<B>$title:</B><BR>$value<BR><SMALL><I>$description</I></SMALL><P>\n" : "<B>$title:</B><BR>$value<P>\n";
Dries Buytaert's avatar
   
Dries Buytaert committed
178
179
180
181
182
183
184
185
186
187
}

function form_textfield($title, $name, $value, $size, $maxlength, $description = 0) {
  return form_item($title, "<INPUT MAXLENGTH=\"$maxlength\" NAME=\"edit[$name]\" SIZE=\"$size\" VALUE=\"". check_textfield($value) ."\">", $description);
}

function form_textarea($title, $name, $value, $cols, $rows, $description = 0) {
  return form_item($title, "<TEXTAREA WRAP=\"virtual\" COLS=\"$cols\" ROWS=\"$rows\" NAME=\"edit[$name]\">". check_textarea($value) ."</TEXTAREA>", $description);
}

Dries Buytaert's avatar
   
Dries Buytaert committed
188
function form_select($title, $name, $value, $options, $description = 0) {
Dries Buytaert's avatar
   
Dries Buytaert committed
189
190
191
192
193
  foreach ($options as $key=>$choice) $select .= "<OPTION VALUE=\"$key\"". ($key == $value ? " SELECTED" : "") .">". check_select($choice) ."</OPTION>";
  return form_item($title, "<SELECT NAME=\"edit[$name]\">$select</SELECT>", $description);
}

function form_hidden($name, $value) {
Dries Buytaert's avatar
   
Dries Buytaert committed
194
  return "<INPUT TYPE=\"hidden\" NAME=\"edit[$name]\" VALUE=\"". check_textfield($value) ."\">\n";
Dries Buytaert's avatar
   
Dries Buytaert committed
195
196
}

197
198
199
200
function form_file($name) {
  return "<INPUT TYPE=\"file\" NAME=\"edit[$name]\">\n";
}

Dries Buytaert's avatar
   
Dries Buytaert committed
201
function form_submit($value) {
Dries Buytaert's avatar
   
Dries Buytaert committed
202
  return "<INPUT TYPE=\"submit\" NAME=\"op\" VALUE=\"". check_textfield($value) ."\">\n";
Dries Buytaert's avatar
   
Dries Buytaert committed
203
204
}

Dries Buytaert's avatar
   
Dries Buytaert committed
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
function field_get($object, $variable, $field) {
  $data = explode(";", $object->$variable);
  for (reset($data); current($data); next($data)) {
    $entry = explode(":", current($data));
    if (reset($entry) == $field) $rval = end($entry);
  }
  return $rval;
}

function field_set($object, $variable, $name, $value) {
  $field = $object->$variable;

  if (!$value) {
    // remove entry:
    $data = explode(";", $field);
    for (reset($data); current($data); next($data)) {
      $entry = explode(":", current($data));
      if ($entry[0] != $name) $rval .= "$entry[0]:$entry[1];";
    }
  }
  else if (strstr($field, "$name:")) {
    // found: update exsisting entry:
    $data = explode(";", $field);
    for (reset($data); current($data); next($data)) {
      $entry = explode(":", current($data));
      if ($entry[0] == $name) $entry[1] = $value;
      $rval .= "$entry[0]:$entry[1];";
    }
  }
  else {
    // not found:
    $rval = "$field$name:$value;";
  }

  return $rval;
}

Dries Buytaert's avatar
   
Dries Buytaert committed
242
$conf = conf_init();
243

Dries Buytaert's avatar
   
Dries Buytaert committed
244
include_once "includes/$conf.php";
Dries Buytaert's avatar
   
Dries Buytaert committed
245
include_once "includes/structure.inc";
Dries Buytaert's avatar
 
Dries Buytaert committed
246
include_once "includes/database.inc";
Dries Buytaert's avatar
   
Dries Buytaert committed
247
include_once "includes/variable.inc";
Dries Buytaert's avatar
   
Dries Buytaert committed
248
include_once "includes/comment.inc";
Dries Buytaert's avatar
   
Dries Buytaert committed
249
include_once "includes/module.inc";
Dries Buytaert's avatar
   
Dries Buytaert committed
250
251
include_once "includes/locale.inc";
include_once "includes/search.inc";
Dries Buytaert's avatar
   
Dries Buytaert committed
252
include_once "includes/timer.inc";
Dries Buytaert's avatar
 
Dries Buytaert committed
253
254
include_once "includes/theme.inc";
include_once "includes/user.inc";
Dries Buytaert's avatar
   
Dries Buytaert committed
255
include_once "includes/node.inc";
Dries Buytaert's avatar
 
Dries Buytaert committed
256

Dries Buytaert's avatar
   
Dries Buytaert committed
257
// initialize user session:
Dries Buytaert's avatar
   
Dries Buytaert committed
258
user_init();
Dries Buytaert's avatar
   
Dries Buytaert committed
259
260

// initialize installed modules:
Dries Buytaert's avatar
   
Dries Buytaert committed
261
module_init();
Dries Buytaert's avatar
   
Dries Buytaert committed
262
263

// initialize localization system:
Dries Buytaert's avatar
   
Dries Buytaert committed
264
$locale = locale_init();
Dries Buytaert's avatar
   
Dries Buytaert committed
265
266

// initialize configuration variables:
Dries Buytaert's avatar
   
Dries Buytaert committed
267
$conf = variable_init();
Dries Buytaert's avatar
   
Dries Buytaert committed
268
269

// initialize theme:
Dries Buytaert's avatar
   
Dries Buytaert committed
270
$theme = theme_init();
Dries Buytaert's avatar
 
Dries Buytaert committed
271

Dries Buytaert's avatar
   
Dries Buytaert committed
272
273
274
// set error handler:
set_error_handler("error_handler");

Dries Buytaert's avatar
   
Dries Buytaert committed
275
?>