File: io.php

package info (click to toggle)
bbclone 0.4.6-8
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 4,304 kB
  • ctags: 528
  • sloc: php: 15,858; sh: 349; makefile: 41
file content (223 lines) | stat: -rw-r--r-- 6,715 bytes parent folder | download | duplicates (2)
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php
# This file is part of BBClone (The PHP web counter on steroids)

# $Header: /cvs/bbclone/lib/io.php,v 1.63 2005/03/08 01:05:29 olliver Exp $

# Copyright (C) 2001-2005, the BBClone Team (see file doc/authors.txt
# distributed with this library)

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# See doc/copying.txt for details

# remove unwanted stuff from user input
function bbc_clean($input, $sep = 0) {
  $sp = strpos($input, $sep);
  # only look for separator if really needed
  $input = (!empty($sep) && ($sp !== false)) ? substr($input, 0, $sp) : $input;
  $input = str_replace("\\", "/", strip_tags($input));
  $input = str_replace("$", "&#36;", htmlspecialchars($input, ENT_QUOTES));

  # Limit the maximum length to 512 chars
  return trim(substr($input, 0, 512));
}

# generates string output of an array
function bbc_array_to_str($tab) {
  static $indent = "";

  $oldindent = $indent;
  $indent   .= "  ";
  $sep       = "";
  $str = $indent."array(\n";
  $last_is_array = false;
  $k = 0;

  reset($tab);

  while (list($key, $val) = each($tab)) {
    # The separator treatment
    if (($last_is_array) || (is_array($val) && ($k !== 0))) {
      $str .= $sep."\n";
    }
    else $str .= $sep;

    # The key treatment
    if (preg_match("%^[0-9]+$%", $key)) {
      if ($key !== $k) {
        $str .= (((is_array($val)) || ($k === 0) || ($last_is_array)) ? "$indent  " : "")
               ."$key =>".((is_array($val)) ? "\n" : " ");
      }
      else $str .= ($k === 0) ? (is_array($val) ? "" : "$indent  ") : "";
    }
    else {
      $str .= (((is_array($val)) || ($k === 0) || ($last_is_array)) ? "$indent  " : "")
             ."\"$key\" =>".((is_array($val)) ? "\n" : " ");
    }

    # The value treatment
    $last_is_array = false;
    if (is_array($val)) {
      $str .= bbc_array_to_str($val);
      $last_is_array = true;
      $sep = ",";
    }
    else {
      $str .= (preg_match("%^[0-9]+$%", $val) ? $val : "\"$val\"");
      $sep = ", ";
    }
    $k++;
  }
  $str .= "\n$indent)";
  $indent = $oldindent;
  return $str;
}

# BBClone's ftok emulation for sem_get
function bbc_ftok($file) {
  $stat = stat($file);
  $dev = decbin($stat[0]);
  $inode = decbin($stat[1]);

  foreach (array("dev", "inode") as $what) {
    $lim = ($what == "inode") ? 16 : 8;

    if ($$what == $lim) continue;
    elseif ($$what < $lim) $$what = str_pad($$what, $lim, 0);
    else $$what = substr($$what, -$lim);
  }
  return bindec("1111000".$dev.$inode);
}

# returns the lock id
function bbc_semlock($file) {
  if (!$id = sem_get(bbc_ftok($file), 1) || !sem_acquire($id)) {
    if (is_resource($id)) {
      sem_release($id);
      @sem_remove($id);
    }
    return false;
  }
  return $id;
}

# write data, returns file pointer on success else false
function bbc_aquire_lock($file, $append = false) {
  $mode = !$append ? (defined("_BBC_DIO") ? O_RDWR : "rb+") : (defined("_BBC_DIO") ? O_WRONLY : "ab");
  $fp = defined("_BBC_DIO") ? dio_open($file, $mode | O_APPEND) : fopen($file, $mode);

  if (!is_resource($fp)) return false;
  if (defined("_BBC_SEM") && ($id = bbc_semlock($file))) return array($fp, $id);

  if ((defined("_BBC_DIO") && (dio_fcntl($fp, F_SETLK, 1) !== -1)) || (defined("_BBC_FLK") && flock($fp, LOCK_EX))) {
    return $fp;
  }

  defined("_BBC_DIO") ? dio_close($fp) : fclose($fp);
  return false;
}

# writes data or truncates file and returns file pointer on success
function bbc_write_data($fp, $data = false, $append = false) {
  if (defined("_BBC_SEM") && is_array($fp)) $fp = $fp[0];
  if (!is_resource($fp)) return false;

  if (!defined("_BBC_DIO")) set_file_buffer($fp, 0);
  if (!$append) defined("_BBC_DIO") ? dio_truncate($fp, 0) : ftruncate($fp, 0);
  if ($data) defined("_BBC_DIO") ? dio_write($fp, $data) : fwrite($fp, $data);

  return true;
}

# finish writing to a file
function bbc_release_lock($fp) {
  # The semaphore needs to be released first
  if (defined("_BBC_SEM")) {
    list($fp, $id) = $fp;
    if (is_resource($id)) {
      sem_release($id);
      @sem_remove($id);
    }
  }

  if (!is_resource($fp)) return;
  if (defined("_BBC_FLK")) flock($fp, LOCK_UN);
  if (defined("_BBC_DIO")) dio_fcntl($fp, F_SETLK, 0);

  return (defined("_BBC_DIO") ? dio_close($fp) : fclose($fp));
}

#initialise the bbc_marker class
function bbc_exec_marker() {
  $bbc_marker =& new bbc_marker;

  if ($bbc_marker->ignored === true) return bbc_msg(false, "i");
  else $msg =& $bbc_marker->write_entry();

  switch ($msg[1]) {
    case "o":
      if (!defined("_OK")) define("_OK", 1);
      return bbc_msg($msg[0], "o");
    case "l":
      return bbc_msg($msg[0], "l");
    case "w":
      return bbc_msg($msg[0], "w");
    default:
      return bbc_msg($msg[0]);
  }
}

# kill stats if desired
function bbc_kill_stats() {
  global $BBC_ACCESS_FILE, $BBC_CACHE_PATH, $BBC_COUNTER_FILES, $BBC_COUNTER_PREFIX, $BBC_COUNTER_SUFFIX,
         $BBC_LAST_FILE;

  $paths = array($BBC_ACCESS_FILE, $BBC_LAST_FILE);

  for ($i = 0; $i < $BBC_COUNTER_FILES; $i++) $paths[] = $BBC_CACHE_PATH.$BBC_COUNTER_PREFIX.$i.$BBC_COUNTER_SUFFIX;

  foreach ($paths as $file) fclose(fopen($file, "wb"));
  return true;
}

# Parse all counter files of var/ and return an array of N rows,
# with N as amount of new connections, sorted in increasing time of connection.
# The counters files are emptied afterwards.
function bbc_counter_to_array() {
  global $BBC_CACHE_PATH, $BBC_COUNTER_PREFIX, $BBC_COUNTER_SUFFIX, $BBC_SEP, $BBC_COUNTER_COLUMNS,
         $BBC_COUNTER_COLUMN_NAMES, $BBC_COUNTER_FILES, $BBC_DEBUG;

  for ($i = 0, $new_cnt = 0; $i < $BBC_COUNTER_FILES; $i++) {
    $file = $BBC_CACHE_PATH.$BBC_COUNTER_PREFIX.$i.$BBC_COUNTER_SUFFIX;

    if (!is_readable($file)) {
      if (!empty($BBC_DEBUG)) print(bbc_msg($file));
      $no_acc = 1;
      continue;
    }

    if (!is_writable($file) && empty($no_acc)) {
      if (!empty($BBC_DEBUG)) print(bbc_msg($file, "w"));
      continue;
    }

    for ($j = 0, $data = file($file), $max = count($data); $j < $max; $j++) {
      $line = explode($BBC_SEP, trim($data[$j]));

      if (empty($line[0]) || !preg_match("%[0-9]+%", $line[0])) continue;

      for ($k = 0; ($k < $BBC_COUNTER_COLUMNS); $k++) {
        $new_visits[$new_cnt][($BBC_COUNTER_COLUMN_NAMES[$k])] = trim($line[$k]);
      }
      $new_cnt++;
    }
    # reset the counter files
    fclose(fopen($file, "wb"));
  }
  if (!empty($new_visits)) usort($new_visits, "bbc_sort_time_sc");
  return (empty($new_visits) ? array() : $new_visits);
}
?>