common.inc 9.01 KB
Newer Older
Dries's avatar
   
Dries committed
1
<?php
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
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's avatar
   
Dries 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's avatar
   
Dries committed
28
  global $user;
Dries's avatar
   
Dries 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's avatar
   
Dries committed
33
      die("submission rate exceeded");
Dries's avatar
   
Dries committed
34
35
36
37
38
39
40
    }
    else {
      watchdog($type, "throttle control");
    }
  }
}

Dries's avatar
   
Dries 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's avatar
   
Dries committed
54

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

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

Dries's avatar
CHANGES    
Dries committed
63
64
function check_code($text) {
  return $text;
Dries's avatar
   
Dries committed
65
66
67
68
69
70
71
72
73
74
}

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;
}

Dries's avatar
CHANGES    
Dries committed
75
76
77
78
function check_preview($text) {
  return check_output(check_input($text), 1);
}

Dries's avatar
   
Dries committed
79
80
81
82
function check_query($text) {
  return addslashes(stripslashes($text));
}

Dries's avatar
CHANGES    
Dries committed
83
84
85
86
87
88
89
90
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;
  return ($text) ? (($nl2br) ? nl2br(stripslashes($text)) : stripslashes($text)) : $na;
Dries's avatar
   
Dries committed
91
92
93
94
95
96
97
}

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

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

Dries's avatar
   
Dries committed
164
165
function form($action, $form, $method = "post", $options = 0) {
  return "<FORM ACTION=\"$action\" METHOD=\"$method\"". ($options ? " $options" : "") .">\n$form</FORM>\n";
Dries's avatar
   
Dries committed
166
167
}

Dries's avatar
   
Dries committed
168
function form_item($title, $value, $description = 0) {
Dries's avatar
   
Dries committed
169
  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
170
171
172
}

function form_textfield($title, $name, $value, $size, $maxlength, $description = 0) {
Dries's avatar
CHANGES    
Dries committed
173
  return form_item($title, "<INPUT MAXLENGTH=\"$maxlength\" NAME=\"edit[$name]\" SIZE=\"$size\" VALUE=\"". check_form($value) ."\">", $description);
Dries's avatar
   
Dries committed
174
175
176
}

function form_textarea($title, $name, $value, $cols, $rows, $description = 0) {
Dries's avatar
CHANGES    
Dries committed
177
  return form_item($title, "<TEXTAREA WRAP=\"virtual\" COLS=\"$cols\" ROWS=\"$rows\" NAME=\"edit[$name]\">". check_form($value) ."</TEXTAREA>", $description);
Dries's avatar
   
Dries committed
178
179
}

Dries's avatar
   
Dries committed
180
function form_select($title, $name, $value, $options, $description = 0) {
Dries's avatar
CHANGES    
Dries committed
181
  foreach ($options as $key=>$choice) $select .= "<OPTION VALUE=\"$key\"". ($key == $value ? " SELECTED" : "") .">". check_form($choice) ."</OPTION>";
Dries's avatar
   
Dries committed
182
183
184
  return form_item($title, "<SELECT NAME=\"edit[$name]\">$select</SELECT>", $description);
}

185
186
function form_file($title, $name, $size, $description = 0) {
  return form_item($title, "<INPUT TYPE=\"file\" NAME=\"edit[$name]\" SIZE=\"$size\"><P>\n", $description);
Dries's avatar
   
Dries committed
187
188
}

189
function form_hidden($name, $value) {
Dries's avatar
CHANGES    
Dries committed
190
  return "<INPUT TYPE=\"hidden\" NAME=\"edit[$name]\" VALUE=\"". check_form($value) ."\">\n";
191
192
}

Dries's avatar
   
Dries committed
193
function form_submit($value) {
Dries's avatar
CHANGES    
Dries committed
194
  return "<INPUT TYPE=\"submit\" NAME=\"op\" VALUE=\"". check_form($value) ."\">\n";
Dries's avatar
   
Dries committed
195
196
}

Dries's avatar
   
Dries committed
197
198
199
200
function field_get($string, $name) {
  foreach (explode(";", $string) as $data) {
    $entry = explode(":", $data);
    if ($entry[0] == $name) return $entry[1];
Dries's avatar
   
Dries committed
201
202
203
  }
}

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

  return $rval;
}

Dries's avatar
CHANGES    
Dries committed
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
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() {
  if (variable_get("dev_timer", 0)) timer_start();
}

function page_footer() {
  if (variable_get("dev_timer", 0)) timer_print();
}

Dries's avatar
   
Dries committed
248
$conf = conf_init();
249

Dries's avatar
   
Dries committed
250
include_once "includes/$conf.php";
Dries's avatar
   
Dries committed
251
include_once "includes/structure.inc";
Dries's avatar
 
Dries committed
252
include_once "includes/database.inc";
Dries's avatar
   
Dries committed
253
include_once "includes/variable.inc";
Dries's avatar
   
Dries committed
254
include_once "includes/comment.inc";
Dries's avatar
   
Dries committed
255
include_once "includes/module.inc";
Dries's avatar
   
Dries committed
256
257
include_once "includes/locale.inc";
include_once "includes/search.inc";
Dries's avatar
 
Dries committed
258
259
include_once "includes/theme.inc";
include_once "includes/user.inc";
Dries's avatar
   
Dries committed
260
include_once "includes/node.inc";
Dries's avatar
 
Dries committed
261

Dries's avatar
   
Dries committed
262
// initialize user session:
Dries's avatar
   
Dries committed
263
user_init();
Dries's avatar
   
Dries committed
264
265

// initialize installed modules:
Dries's avatar
   
Dries committed
266
module_init();
Dries's avatar
   
Dries committed
267
268

// initialize localization system:
Dries's avatar
   
Dries committed
269
$locale = locale_init();
Dries's avatar
   
Dries committed
270
271

// initialize configuration variables:
Dries's avatar
   
Dries committed
272
$conf = variable_init();
Dries's avatar
   
Dries committed
273
274

// initialize theme:
Dries's avatar
   
Dries committed
275
$theme = theme_init();
Dries's avatar
 
Dries committed
276

Dries's avatar
   
Dries committed
277
278
279
// set error handler:
set_error_handler("error_handler");

Dries's avatar
   
Dries committed
280
?>