common.inc 9.04 KB
Newer Older
Dries's avatar
   
Dries committed
1
<?
Dries's avatar
 
Dries committed
2

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

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

Dries's avatar
   
Dries committed
12
13
14
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.";
15
  if (($errno == 1 || $errno == 2 || $errno == 4) && error_reporting()) {
Dries's avatar
   
Dries committed
16
17
18
19
20
    watchdog("error", $types[$errno] .": $message in $filename on line $line.");
    print $entry;
  }
}

Dries's avatar
   
Dries committed
21
function watchdog($type, $message) {
Dries's avatar
   
Dries committed
22
23
  global $user;
  db_query("INSERT INTO watchdog (user, type, message, location, hostname, timestamp) VALUES ('$user->id', '". check_input($type) ."', '". check_input($message) ."', '". check_input(getenv("REQUEST_URI")) ."', '". check_input(getenv("REMOTE_ADDR")) ."', '". time() ."')");
Dries's avatar
   
Dries committed
24
25
26
}

function throttle($type, $rate) {
Dries's avatar
   
Dries committed
27
  global $user;
Dries's avatar
   
Dries committed
28
  if (!user_access($user)) {
Dries's avatar
   
Dries committed
29
30
    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");
Dries's avatar
   
Dries committed
31
      die(message_throttle());
Dries's avatar
   
Dries committed
32
33
    }
    else {
Dries's avatar
   
Dries committed
34
      watchdog($type, "throttle");
Dries's avatar
   
Dries committed
35
36
37
38
    }
  }
}

Dries's avatar
   
Dries committed
39
40
41
42
43
44
45
46
47
48
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/";
}

Dries's avatar
   
Dries committed
49
function message_account() {
Dries's avatar
   
Dries committed
50
51
  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's avatar
   
Dries committed
52

Dries's avatar
   
Dries committed
53
54
55
56
function message_throttle() {
  return t("You exceeded the submission rate exceeded.  Please wait a few minutes and try again.");
}

Dries's avatar
CHANGES    
Dries committed
57
58
function check_form($text) {
  return htmlspecialchars(stripslashes($text));
Dries's avatar
   
Dries committed
59
60
}

Dries's avatar
CHANGES    
Dries committed
61
62
function check_export($text) {
  return htmlspecialchars(stripslashes($text));
Dries's avatar
   
Dries committed
63
64
}

Dries's avatar
CHANGES    
Dries committed
65
66
function check_code($text) {
  return $text;
Dries's avatar
   
Dries committed
67
68
}

Dries's avatar
CHANGES    
Dries committed
69
function check_preview($text) {
Dries's avatar
   
Dries committed
70
  return check_output(check_input($text));
Dries's avatar
CHANGES    
Dries committed
71
72
}

Dries's avatar
   
Dries committed
73
74
75
76
function check_query($text) {
  return addslashes(stripslashes($text));
}

Dries's avatar
CHANGES    
Dries committed
77
78
79
80
81
82
83
function check_input($text) {
  foreach (module_list() as $module) $text = module_invoke($module, "filter", $text);
  return addslashes(stripslashes(substr($text, 0, variable_get("max_input_size", 10000))));
}

function check_output($text, $nl2br = 0) {
  global $na;
Dries's avatar
   
Dries committed
84
  return ($text) ? ($nl2br ? nl2br(stripslashes($text)) : stripslashes($text)) : $na;
Dries's avatar
   
Dries committed
85
86
87
88
89
90
91
}

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

function format_interval($timestamp) {
Dries's avatar
   
Dries committed
92
  $units = array("year|years" => 31536000, "week|weeks" => 604800, "day|days" => 86400, "hour|hours" => 3600, "min|min" => 60, "sec|sec" => 1);
93
  foreach ($units as $key=>$value) {
Dries's avatar
   
Dries committed
94
    $key = explode("|", $key);
95
96
97
98
    if ($timestamp >= $value) {
      $output .= ($output ? " " : "") . format_plural(floor($timestamp / $value), $key[0], $key[1]);
      $timestamp %= $value;
    }
Dries's avatar
   
Dries committed
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
  }
  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":
Dries's avatar
   
Dries committed
119
120
121
122
123
124
125
126
127
128
      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;
        }
129
      }
Dries's avatar
   
Dries committed
130
131
132
133
134
135
136
137
138
      break;
    default:
      $date = t(date("l", $timestamp)) .", ". date("m/d/Y - H:i", $timestamp);
  }
  return $date;
}

function format_username($username) {
  global $user;
Dries's avatar
   
Dries committed
139
  if ($username) return (user_access($user, "account") ? "<A HREF=\"admin.php?mod=account&op=view&name=". urlencode($username) ."\">$username</A>" : "<A HREF=\"account.php?op=view&name=". urlencode($username) ."\">$username</A>");
Dries's avatar
   
Dries committed
140
141
142
143
144
  else return variable_get(anonymous, "Anonymous");
}

function format_email($address) {
  global $na;
Dries's avatar
   
Dries committed
145
  return ($address) ? "<a href=\"mailto:$address\">$address</A>" : $na;
Dries's avatar
   
Dries committed
146
147
148
149
150
}

function format_url($address, $description = "") {
  global $na;
  $description = ($description) ? $description : $address;
Dries's avatar
   
Dries committed
151
  return ($address) ? "<a href=\"$address\">". check_output($description) ."</a>" : $na;
Dries's avatar
   
Dries committed
152
153
154
155
156
157
}

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

Dries's avatar
   
Dries committed
158
function form($action, $form, $method = "post", $options = 0) {
Dries's avatar
   
Dries committed
159
  return "<form action=\"$action\" method=\"$method\"". ($options ? " $options" : "") .">\n$form</form>\n";
Dries's avatar
   
Dries committed
160
161
}

Dries's avatar
   
Dries committed
162
function form_item($title, $value, $description = 0) {
Dries's avatar
   
Dries committed
163
  return ($description) ? "<b>$title:</b><br />$value<br /><small><i>$description</i></small><p />\n" : "<b>$title:</b><br />$value<p />\n";
Dries's avatar
   
Dries committed
164
165
166
}

function form_textfield($title, $name, $value, $size, $maxlength, $description = 0) {
Dries's avatar
   
Dries committed
167
  return form_item($title, "<input maxlength=\"$maxlength\" name=\"edit[$name]\" size=\"$size\" value=\"". check_form($value) ."\" />", $description);
Dries's avatar
   
Dries committed
168
169
170
}

function form_textarea($title, $name, $value, $cols, $rows, $description = 0) {
Dries's avatar
   
Dries committed
171
  return form_item($title, "<textarea wrap=\"virtual\" cols=\"$cols\" rows=\"$rows\" name=\"edit[$name]\">". check_form($value) ."</textarea>", $description);
Dries's avatar
   
Dries committed
172
173
}

Kjartan's avatar
Changes    
Kjartan committed
174
function form_select($title, $name, $value, $options, $description = 0, $extra = 0) {
175
  if (count($options) > 0) {
Kjartan's avatar
Changes    
Kjartan committed
176
177
    foreach ($options as $key=>$choice) $select .= "<option value=\"$key\"". (is_array($value) ? (in_array($key, $value) ? " selected" : "") : ($key == $value ? " selected" : "")) .">". check_form($choice) ."</option>";
    return form_item($title, "<select name=\"edit[$name]\"". ($extra ? " $extra" : "") .">$select</select>", $description);
178
  }
Dries's avatar
   
Dries committed
179
180
}

181
function form_file($title, $name, $size, $description = 0) {
Dries's avatar
   
Dries committed
182
  return form_item($title, "<input type=\"file\" name=\"edit[$name]\" size=\"$size\" />\n", $description);
Dries's avatar
   
Dries committed
183
184
}

185
function form_hidden($name, $value) {
Dries's avatar
   
Dries committed
186
  return "<input type=\"hidden\" name=\"edit[$name]\" value=\"". check_form($value) ."\" />\n";
187
188
}

Dries's avatar
   
Dries committed
189
function form_submit($value) {
Dries's avatar
   
Dries committed
190
  return "<input type=\"submit\" name=\"op\" value=\"". check_form($value) ."\" />\n";
Dries's avatar
   
Dries committed
191
192
}

Dries's avatar
   
Dries committed
193
function field_get($string, $name) {
194
195
  foreach (explode(",", $string) as $data) {
    $entry = explode("=", $data);
Dries's avatar
   
Dries committed
196
    if ($entry[0] == $name) return $entry[1];
Dries's avatar
   
Dries committed
197
198
199
  }
}

Dries's avatar
   
Dries committed
200
function field_set($string, $name, $value) {
Dries's avatar
   
Dries committed
201
202
  if (!$value) {
    // remove entry:
203
204
205
    foreach (explode(",", $string) as $data) {
      $entry = explode("=", $data);
      if ($entry[0] != $name) $rval .= "$entry[0]=$entry[1],";
Dries's avatar
   
Dries committed
206
207
    }
  }
208
  else if (strstr($string, "$name=")) {
Dries's avatar
   
Dries committed
209
    // found: update exsisting entry:
210
211
    foreach (explode(",", $string) as $data) {
      $entry = explode("=", $data);
Dries's avatar
   
Dries committed
212
      if ($entry[0] == $name) $entry[1] = $value;
213
      $rval .= "$entry[0]=$entry[1],";
Dries's avatar
   
Dries committed
214
215
216
217
    }
  }
  else {
    // not found:
218
    $rval = "$string$name=$value,";
Dries's avatar
   
Dries committed
219
220
221
222
223
  }

  return $rval;
}

Dries's avatar
   
Dries committed
224
function field_merge($a, $b) {
225
226
  foreach (explode(",", $b) as $data) {
    $entry = explode("=", $data);
Dries's avatar
   
Dries committed
227
228
229
230
231
    $a = field_set($a, $entry[0], $entry[1]);
  }
  return $a;
}

Dries's avatar
CHANGES    
Dries committed
232
233
234
235
236
237
238
239
240
241
242
243
244
function timer_start() {
  global $timer;
  $timer = explode(" ", microtime());
}

function timer_print() {
  global $timer;
  $stop = explode(" ", microtime());
  $diff = $stop[0] - $timer[0];
  print "<PRE>execution time: $diff ms</PRE>";
}

function page_header() {
Dries's avatar
   
Dries committed
245
246
247
248
249
  global $user;

  if (variable_get("dev_timer", 0)) {
    timer_start();
  }
Dries's avatar
CHANGES    
Dries committed
250
251
252
}

function page_footer() {
Dries's avatar
   
Dries committed
253
254
255
  if (variable_get("dev_timer", 0)) {
    timer_print();
  }
Dries's avatar
CHANGES    
Dries committed
256
257
}

Dries's avatar
   
Dries committed
258
$conf = conf_init();
259

Dries's avatar
   
Dries committed
260
include_once "includes/$conf.php";
Dries's avatar
 
Dries committed
261
include_once "includes/database.inc";
Dries's avatar
   
Dries committed
262
include_once "includes/variable.inc";
Dries's avatar
   
Dries committed
263
include_once "includes/comment.inc";
Dries's avatar
   
Dries committed
264
include_once "includes/module.inc";
Dries's avatar
   
Dries committed
265
266
include_once "includes/locale.inc";
include_once "includes/search.inc";
Dries's avatar
 
Dries committed
267
268
include_once "includes/theme.inc";
include_once "includes/user.inc";
Dries's avatar
   
Dries committed
269
include_once "includes/node.inc";
Dries's avatar
 
Dries committed
270

Dries's avatar
   
Dries committed
271
// initialize user session:
Dries's avatar
   
Dries committed
272
user_init();
Dries's avatar
   
Dries committed
273
274

// initialize installed modules:
Dries's avatar
   
Dries committed
275
module_init();
Dries's avatar
   
Dries committed
276
277

// initialize localization system:
Dries's avatar
   
Dries committed
278
$locale = locale_init();
Dries's avatar
   
Dries committed
279
280

// initialize configuration variables:
Dries's avatar
   
Dries committed
281
$conf = variable_init();
Dries's avatar
   
Dries committed
282
283

// initialize theme:
Dries's avatar
   
Dries committed
284
$theme = theme_init();
Dries's avatar
 
Dries committed
285

Dries's avatar
   
Dries committed
286
287
288
// set error handler:
set_error_handler("error_handler");

Dries's avatar
   
Dries committed
289
?>