diff options
Diffstat (limited to 'inc/functions.php')
| -rw-r--r-- | inc/functions.php | 130 |
1 files changed, 59 insertions, 71 deletions
diff --git a/inc/functions.php b/inc/functions.php index 6854d8b7..6ce76472 100644 --- a/inc/functions.php +++ b/inc/functions.php @@ -96,9 +96,9 @@ function loadConfig() { $configstr = file_get_contents('inc/instance-config.php'); - if (isset($board['dir']) && file_exists($board['dir'] . '/config.php')) { - $configstr .= file_get_contents($board['dir'] . '/config.php'); - } + if (isset($board['dir']) && file_exists($board['dir'] . '/config.php')) { + $configstr .= file_get_contents($board['dir'] . '/config.php'); + } $matches = array(); preg_match_all('/[^\/*#]\$config\s*\[\s*[\'"]locale[\'"]\s*\]\s*=\s*([\'"])(.*?)\1/', $configstr, $matches); if ($matches && isset ($matches[2]) && $matches[2]) { @@ -240,6 +240,9 @@ function loadConfig() { event_handler('post', 'postHandler'); } + if (is_array($config['anonymous'])) + $config['anonymous'] = $config['anonymous'][array_rand($config['anonymous'])]; + event('load-config'); if ($config['debug']) { @@ -713,22 +716,15 @@ function displayBan($ban) { } $ban['ip'] = $_SERVER['REMOTE_ADDR']; + if ($ban['post'] && isset($ban['post']['board'], $ban['post']['id'])) { if (openBoard($ban['post']['board'])) { - - $query = query(sprintf("SELECT `thumb`, `file` FROM ``posts_%s`` WHERE `id` = " . + $query = query(sprintf("SELECT `files` FROM ``posts_%s`` WHERE `id` = " . (int)$ban['post']['id'], $board['uri'])); if ($_post = $query->fetch(PDO::FETCH_ASSOC)) { $ban['post'] = array_merge($ban['post'], $_post); - } else { - $ban['post']['file'] = 'deleted'; - $ban['post']['thumb'] = false; } - } else { - $ban['post']['file'] = 'deleted'; - $ban['post']['thumb'] = false; } - if ($ban['post']['thread']) { $post = new Post($ban['post']); } else { @@ -883,7 +879,7 @@ function insertFloodPost(array $post) { function post(array $post) { global $pdo, $board; - $query = prepare(sprintf("INSERT INTO ``posts_%s`` VALUES ( NULL, :thread, :subject, :email, :name, :trip, :capcode, :body, :body_nomarkup, :time, :time, :thumb, :thumbwidth, :thumbheight, :file, :width, :height, :filesize, :filename, :filehash, :password, :ip, :sticky, :locked, 0, :embed)", $board['uri'])); + $query = prepare(sprintf("INSERT INTO ``posts_%s`` VALUES ( NULL, :thread, :subject, :email, :name, :trip, :capcode, :body, :body_nomarkup, :time, :time, :files, :num_files, :filehash, :password, :ip, :sticky, :locked, 0, :embed)", $board['uri'])); // Basic stuff if (!empty($post['subject'])) { @@ -943,31 +939,12 @@ function post(array $post) { } if ($post['has_file']) { - $query->bindValue(':thumb', $post['thumb']); - $query->bindValue(':thumbwidth', $post['thumbwidth'], PDO::PARAM_INT); - $query->bindValue(':thumbheight', $post['thumbheight'], PDO::PARAM_INT); - $query->bindValue(':file', $post['file']); - - if (isset($post['width'], $post['height'])) { - $query->bindValue(':width', $post['width'], PDO::PARAM_INT); - $query->bindValue(':height', $post['height'], PDO::PARAM_INT); - } else { - $query->bindValue(':width', null, PDO::PARAM_NULL); - $query->bindValue(':height', null, PDO::PARAM_NULL); - } - - $query->bindValue(':filesize', $post['filesize'], PDO::PARAM_INT); - $query->bindValue(':filename', $post['filename']); + $query->bindValue(':files', json_encode($post['files'])); + $query->bindValue(':num_files', $post['num_files']); $query->bindValue(':filehash', $post['filehash']); } else { - $query->bindValue(':thumb', null, PDO::PARAM_NULL); - $query->bindValue(':thumbwidth', null, PDO::PARAM_NULL); - $query->bindValue(':thumbheight', null, PDO::PARAM_NULL); - $query->bindValue(':file', null, PDO::PARAM_NULL); - $query->bindValue(':width', null, PDO::PARAM_NULL); - $query->bindValue(':height', null, PDO::PARAM_NULL); - $query->bindValue(':filesize', null, PDO::PARAM_NULL); - $query->bindValue(':filename', null, PDO::PARAM_NULL); + $query->bindValue(':files', null, PDO::PARAM_NULL); + $query->bindValue(':num_files', 0); $query->bindValue(':filehash', null, PDO::PARAM_NULL); } @@ -995,33 +972,40 @@ function bumpThread($id) { } // Remove file from post -function deleteFile($id, $remove_entirely_if_already=true) { +function deleteFile($id, $remove_entirely_if_already=true, $file=null) { global $board, $config; - $query = prepare(sprintf("SELECT `thread`,`thumb`,`file` FROM ``posts_%s`` WHERE `id` = :id LIMIT 1", $board['uri'])); + $query = prepare(sprintf("SELECT `thread`, `files`, `num_files` FROM ``posts_%s`` WHERE `id` = :id LIMIT 1", $board['uri'])); $query->bindValue(':id', $id, PDO::PARAM_INT); $query->execute() or error(db_error($query)); if (!$post = $query->fetch(PDO::FETCH_ASSOC)) error($config['error']['invalidpost']); + $files = json_decode($post['files']); + $file_to_delete = $file !== false ? $files[(int)$file] : (object)array('file' => false); - if ($post['file'] == 'deleted' && !$post['thread']) + if ($files[0]->file == 'deleted' && $post['num_files'] == 1 && !$post['thread']) return; // Can't delete OP's image completely. - $query = prepare(sprintf("UPDATE ``posts_%s`` SET `thumb` = NULL, `thumbwidth` = NULL, `thumbheight` = NULL, `filewidth` = NULL, `fileheight` = NULL, `filesize` = NULL, `filename` = NULL, `filehash` = NULL, `file` = :file WHERE `id` = :id", $board['uri'])); - if ($post['file'] == 'deleted' && $remove_entirely_if_already) { + $query = prepare(sprintf("UPDATE ``posts_%s`` SET `files` = :file WHERE `id` = :id", $board['uri'])); + if (($file && $file_to_delete->file == 'deleted') && $remove_entirely_if_already) { // Already deleted; remove file fully - $query->bindValue(':file', null, PDO::PARAM_NULL); + $files[$file] = null; } else { - // Delete thumbnail - file_unlink($board['dir'] . $config['dir']['thumb'] . $post['thumb']); - - // Delete file - file_unlink($board['dir'] . $config['dir']['img'] . $post['file']); + foreach ($files as $i => $f) { + if (($file !== false && $i == $file) || $file === null) { + // Delete thumbnail + file_unlink($board['dir'] . $config['dir']['thumb'] . $f->thumb); + unset($files[$i]->thumb); - // Set file to 'deleted' - $query->bindValue(':file', 'deleted', PDO::PARAM_INT); + // Delete file + file_unlink($board['dir'] . $config['dir']['img'] . $f->file); + $files[$i]->file = 'deleted'; + } + } } + $query->bindValue(':file', json_encode($files), PDO::PARAM_STR); + $query->bindValue(':id', $id, PDO::PARAM_INT); $query->execute() or error(db_error($query)); @@ -1059,7 +1043,7 @@ function deletePost($id, $error_if_doesnt_exist=true, $rebuild_after=true) { global $board, $config; // Select post and replies (if thread) in one query - $query = prepare(sprintf("SELECT `id`,`thread`,`thumb`,`file` FROM ``posts_%s`` WHERE `id` = :id OR `thread` = :id", $board['uri'])); + $query = prepare(sprintf("SELECT `id`,`thread`,`files` FROM ``posts_%s`` WHERE `id` = :id OR `thread` = :id", $board['uri'])); $query->bindValue(':id', $id, PDO::PARAM_INT); $query->execute() or error(db_error($query)); @@ -1089,13 +1073,14 @@ function deletePost($id, $error_if_doesnt_exist=true, $rebuild_after=true) { // Rebuild thread $rebuild = &$post['thread']; } - if ($post['thumb']) { - // Delete thumbnail - file_unlink($board['dir'] . $config['dir']['thumb'] . $post['thumb']); - } - if ($post['file']) { + if ($post['files']) { // Delete file - file_unlink($board['dir'] . $config['dir']['img'] . $post['file']); + foreach (json_decode($post['files']) as $i => $f) { + if ($f->file !== 'deleted') { + file_unlink($board['dir'] . $config['dir']['img'] . $f->file); + file_unlink($board['dir'] . $config['dir']['thumb'] . $f->thumb); + } + } } $ids[] = (int)$post['id']; @@ -1212,8 +1197,8 @@ function index($page, $mod=false) { $num_images = 0; foreach ($replies as $po) { - if ($po['file']) - $num_images++; + if ($po['num_files']) + $num_images+=$po['num_files']; $thread->add(new Post($po, $mod ? '?/' : $config['root'], $mod)); } @@ -1236,7 +1221,7 @@ function index($page, $mod=false) { 'post_url' => $config['post_url'], 'config' => $config, 'boardlist' => createBoardlist($mod), - 'threads' => $threads + 'threads' => $threads, ); } @@ -1371,7 +1356,7 @@ function checkRobot($body) { // Returns an associative array with 'replies' and 'images' keys function numPosts($id) { global $board; - $query = prepare(sprintf("SELECT COUNT(*) AS `replies`, COUNT(NULLIF(`file`, 0)) AS `images` FROM ``posts_%s`` WHERE `thread` = :thread", $board['uri'], $board['uri'])); + $query = prepare(sprintf("SELECT COUNT(*) AS `replies`, SUM(`num_files`) AS `images` FROM ``posts_%s`` WHERE `thread` = :thread", $board['uri'], $board['uri'])); $query->bindValue(':thread', $id, PDO::PARAM_INT); $query->execute() or error(db_error($query)); @@ -1929,7 +1914,7 @@ function markup(&$body, $track_cites = false) { } // replace tabs with 8 spaces - $body = str_replace("\t", ' ', $body); + $body = str_replace("\t", ' ', $body); return $tracked_cites; } @@ -2076,8 +2061,8 @@ function buildThread50($id, $return = false, $mod = false, $thread = null, $anti if (!isset($thread)) { $thread = new Thread($post, $mod ? '?/' : $config['root'], $mod); } else { - if ($post['file']) - $num_images++; + if ($post['files']) + $num_images += $post['num_files']; $thread->add(new Post($post, $mod ? '?/' : $config['root'], $mod)); } @@ -2089,7 +2074,8 @@ function buildThread50($id, $return = false, $mod = false, $thread = null, $anti if ($query->rowCount() == $config['noko50_count']+1) { - $count = prepare(sprintf("SELECT COUNT(`id`) as `num` FROM ``posts_%s`` WHERE `thread` = :thread UNION ALL SELECT COUNT(`id`) FROM ``posts_%s`` WHERE `file` IS NOT NULL AND `thread` = :thread", $board['uri'], $board['uri'])); + $count = prepare(sprintf("SELECT COUNT(`id`) as `num` FROM ``posts_%s`` WHERE `thread` = :thread UNION ALL + SELECT SUM(`num_files`) FROM ``posts_%s`` WHERE `files` IS NOT NULL AND `thread` = :thread", $board['uri'], $board['uri'])); $count->bindValue(':thread', $id, PDO::PARAM_INT); $count->execute() or error(db_error($count)); @@ -2109,8 +2095,8 @@ function buildThread50($id, $return = false, $mod = false, $thread = null, $anti foreach ($allPosts as $index => $post) { if ($index == count($allPosts)-count($thread->posts)) break; - if ($post->file) - $thread->omitted_images++; + if ($post->files) + $thread->omitted_images += $post->num_files; } } @@ -2256,13 +2242,15 @@ function getPostByHashInThread($hash, $thread) { } function undoImage(array $post) { - if (!$post['has_file']) + if (!$post['has_file'] || !isset($post['files'])) return; - if (isset($post['file_path'])) - file_unlink($post['file_path']); - if (isset($post['thumb_path'])) - file_unlink($post['thumb_path']); + foreach ($post['files'] as $key => $file) { + if (isset($file['file_path'])) + file_unlink($file['file_path']); + if (isset($file['thumb_path'])) + file_unlink($file['thumb_path']); + } } function rDNS($ip_addr) { |
