diff --git a/inc/functions.php b/inc/functions.php index 97e2a802..b6b5f16e 100644 --- a/inc/functions.php +++ b/inc/functions.php @@ -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) { diff --git a/install.php b/install.php index c6a89da3..74482a08 100644 --- a/install.php +++ b/install.php @@ -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'] = '

Successfully upgraded from ' . $version . ' to ' . VERSION . '.

'; @@ -401,7 +401,7 @@ if(!empty($sql_errors)) { $page['body'] .= '

SQL errors

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.

The errors encountered were:

Ignore errors and complete installation.

'; } else { - file_put_contents($config['has_installed'], VERSION); + file_write($config['has_installed'], VERSION); if(!@unlink(__FILE__)) { $page['body'] .= '

Delete install.php!

I couldn\'t remove install.php. You will have to remove it manually.

'; } @@ -412,7 +412,7 @@ $page['title'] = 'Installation complete'; $page['body'] = '

Thank you for using Tinyboard. Please remember to report any bugs you discover.

'; - file_put_contents($config['has_installed'], VERSION); + file_write($config['has_installed'], VERSION); if(!@unlink(__FILE__)) { $page['body'] .= '

Delete install.php!

I couldn\'t remove install.php. You will have to remove it manually.

'; }