common.inc 9.01 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
  if (!user_access("access administration pages")) {
Dries's avatar
   
Dries committed
28
29
    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
30
      die(message_throttle());
Dries's avatar
   
Dries committed
31
32
    }
    else {
Dries's avatar
   
Dries committed
33
      watchdog($type, "throttle");
Dries's avatar
   
Dries committed
34
35
36
37
    }
  }
}

Dries's avatar
   
Dries committed
38
39
40
41
42
43
44
45
46
47
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
48
49
function message_access() {
  return t("You are not authorized to access to this page.");
Dries's avatar
   
Dries committed
50
}
Dries's avatar
   
Dries committed
51

Dries's avatar
   
Dries committed
52
53
54
55
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
56
57
function check_form($text) {
  return htmlspecialchars(stripslashes($text));
Dries's avatar
   
Dries committed
58
59
}

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

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

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

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

Dries's avatar
CHANGES    
Dries committed
76
77
78
79
80
81
82
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
83
  return ($text) ? ($nl2br ? nl2br(stripslashes($text)) : stripslashes($text)) : $na;
Dries's avatar
   
Dries committed
84
85
86
87
88
89
90
}

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

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

function format_username($username) {
Dries's avatar
   
Dries committed
137
  if ($username) return (user_access("administer accounts") ? "<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
138
139
140
141
142
  else return variable_get(anonymous, "Anonymous");
}

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

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

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

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

Dries's avatar
   
Dries committed
160
function form_item($title, $value, $description = 0) {
Dries's avatar
   
Dries committed
161
  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
162
163
164
}

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

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

Kjartan's avatar
Changes    
Kjartan committed
172
function form_select($title, $name, $value, $options, $description = 0, $extra = 0) {
173
  if (count($options) > 0) {
Kjartan's avatar
Changes    
Kjartan committed
174
175
    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);
176
  }
Dries's avatar
   
Dries committed
177
178
}

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

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

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

Dries's avatar
   
Dries committed
191
function field_get($string, $name) {
192
193
  ereg(",$name=([^,]+)", ",$string", $regs);
  return $regs[1];
Dries's avatar
   
Dries committed
194
195
}

Dries's avatar
   
Dries committed
196
function field_set($string, $name, $value) {
197
198
199
  $rval = ereg_replace(",$name=[^,]+", "", ",$string");
  if ($value) $rval .= ($rval == "," ? "" : ",") ."$name=$value";
  return substr($rval, 1);
Dries's avatar
   
Dries committed
200
201
}

Dries's avatar
   
Dries committed
202
function field_merge($a, $b) {
203
204
  foreach (explode(",", $b) as $data) {
    $entry = explode("=", $data);
Dries's avatar
   
Dries committed
205
206
207
208
209
    $a = field_set($a, $entry[0], $entry[1]);
  }
  return $a;
}

Dries's avatar
   
Dries committed
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
function link_page() {

  $links[] = "<a href=\"index.php\">". t("home") ."</a>";
  $links[] = "<a href=\"search.php\">". t("search") ."</a>";
  $links[] = "<a href=\"submit.php\">". t("submit") ."</a>";
  $links[] = "<a href=\"account.php\">". t("account") ."</a>";

  foreach (module_list() as $name) {
    if (module_hook($name, "link")) {
      $links = array_merge($links, module_invoke($name, "link", "page"));
    }
  }


  return $links;
}

function link_node($node) {
  return module_invoke("node", "link", array("node", $node));
}

Dries's avatar
CHANGES    
Dries committed
231
232
233
234
235
236
237
238
239
240
241
242
243
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
244
245
246
247
248
  global $user;

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

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

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

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

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

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

// initialize installed modules:
Dries's avatar
   
Dries committed
277
module_init();
Dries's avatar
   
Dries committed
278
279

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

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

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

Dries's avatar
   
Dries committed
288
?>