better writing, file locking

This commit is contained in:
Savetheinternet 2011-05-20 17:19:27 +10:00
parent e9a6b3fa58
commit d8301964b1
2 changed files with 23 additions and 6 deletions

View File

@ -211,6 +211,23 @@
} else return false;
}
function file_write($path, $data) {
$fp = fopen($path, 'c');
// File locking
if(!flock($fp, LOCK_EX)) {
error('Unable to lock file!');
}
// Truncate file
ftruncate($fp, 0);
// Write data
fwrite($fp, $data);
flock($fp, LOCK_UN);
fclose($fp);
}
function listBoards() {
$query = query("SELECT * FROM `boards` ORDER BY `uri`") or error(db_error());
$boards = $query->fetchAll();
@ -972,7 +989,7 @@
$content['pages'][$page-1]['selected'] = true;
$content['btn'] = getPageButtons($content['pages']);
$content['hidden_inputs'] = createHiddenInputs();
@file_put_contents($filename, Element('index.html', $content)) or error("Couldn't write to file.");
file_write($filename, Element('index.html', $content));
if(isset($md5) && $md5 == md5_file($filename)) {
break;
@ -997,7 +1014,7 @@
'uri' => addslashes((!empty($uri) ? $config['uri_stylesheets'] : '') . $uri));
}
file_put_contents($config['file_script'], Element('main.js', Array(
file_write($config['file_script'], Element('main.js', Array(
'config' => $config,
'stylesheets' => $stylesheets
)));
@ -1269,7 +1286,7 @@
if($return)
return $body;
else
@file_put_contents($board['dir'] . $config['dir']['res'] . sprintf($config['file_page'], $id), $body) or error("Couldn't write to file.");
file_write($board['dir'] . $config['dir']['res'] . sprintf($config['file_page'], $id), $body);
}
function rrmdir($dir) {

View File

@ -66,7 +66,7 @@
}
// Update version number
file_put_contents($config['has_installed'], VERSION);
file_write($config['has_installed'], VERSION);
$page['title'] = 'Upgraded';
$page['body'] = '<p style="text-align:center">Successfully upgraded from ' . $version . ' to <strong>' . VERSION . '</strong>.</p>';
@ -401,7 +401,7 @@
if(!empty($sql_errors)) {
$page['body'] .= '<div class="ban"><h2>SQL errors</h2><p>SQL errors were encountered when trying to install the database. This may be the result of using a database which is already occupied with a Tinyboard installation; if so, you can probably ignore this.</p><p>The errors encountered were:</p><ul>' . $sql_errors . '</ul><p><a href="?step=5">Ignore errors and complete installation.</a></p></div>';
} else {
file_put_contents($config['has_installed'], VERSION);
file_write($config['has_installed'], VERSION);
if(!@unlink(__FILE__)) {
$page['body'] .= '<div class="ban"><h2>Delete install.php!</h2><p>I couldn\'t remove <strong>install.php</strong>. You will have to remove it manually.</p></div>';
}
@ -412,7 +412,7 @@
$page['title'] = 'Installation complete';
$page['body'] = '<p style="text-align:center">Thank you for using Tinyboard. Please remember to report any bugs you discover.</p>';
file_put_contents($config['has_installed'], VERSION);
file_write($config['has_installed'], VERSION);
if(!@unlink(__FILE__)) {
$page['body'] .= '<div class="ban"><h2>Delete install.php!</h2><p>I couldn\'t remove <strong>install.php</strong>. You will have to remove it manually.</p></div>';
}