Merge branch 'develop' of https://github.com/shish/shimmie2 into develop

This commit is contained in:
Shish 2017-09-17 18:59:59 +01:00
commit 860e828c3e
26 changed files with 222 additions and 66 deletions

View file

@ -10,6 +10,7 @@ require_once "core/sys_config.inc.php";
require_once "core/util.inc.php";
require_once "lib/context.php";
require_once "vendor/autoload.php";
require_once "core/imageboard.pack.php";
// set up and purify the environment
_version_check();

View file

@ -44,6 +44,14 @@ class Block {
*/
public $id;
/**
* Should this block count as content for the sake of
* the 404 handler
*
* @var boolean
*/
public $is_content = true;
/**
* Construct a block.
*
@ -58,7 +66,11 @@ class Block {
$this->body = $body;
$this->section = $section;
$this->position = $position;
$this->id = preg_replace('/[^\w]/', '',str_replace(' ', '_', is_null($id) ? (is_null($header) ? md5($body) : $header) . $section : $id));
if(is_null($id)) {
$id = (empty($header) ? md5($body) : $header) . $section;
}
$this->id = preg_replace('/[^\w]/', '',str_replace(' ', '_', $id));
}
/**

View file

@ -314,13 +314,8 @@ class MemcacheCache implements CacheEngine {
*/
public function __construct($args) {
$hp = explode(":", $args);
if(class_exists("Memcache")) {
$this->memcache = new Memcache;
@$this->memcache->pconnect($hp[0], $hp[1]);
}
else {
print "no memcache"; exit;
}
$this->memcache = new Memcache;
@$this->memcache->pconnect($hp[0], $hp[1]);
}
/**
@ -378,6 +373,100 @@ class MemcacheCache implements CacheEngine {
*/
public function get_misses() {return $this->misses;}
}
class MemcachedCache implements CacheEngine {
/** @var \Memcached|null */
public $memcache=null;
/** @var int */
private $hits=0;
/** @var int */
private $misses=0;
/**
* @param string $args
*/
public function __construct($args) {
$hp = explode(":", $args);
$this->memcache = new Memcached;
#$this->memcache->setOption(Memcached::OPT_COMPRESSION, False);
#$this->memcache->setOption(Memcached::OPT_SERIALIZER, Memcached::SERIALIZER_PHP);
#$this->memcache->setOption(Memcached::OPT_PREFIX_KEY, phpversion());
$this->memcache->addServer($hp[0], $hp[1]);
}
/**
* @param string $key
* @return array|bool|string
*/
public function get($key) {
assert('!is_null($key)');
$key = urlencode($key);
$val = $this->memcache->get($key);
$res = $this->memcache->getResultCode();
if((DEBUG_CACHE === true) || (is_null(DEBUG_CACHE) && @$_GET['DEBUG_CACHE'])) {
$hit = $res == Memcached::RES_SUCCESS ? "hit" : "miss";
file_put_contents("data/cache.log", "Cache $hit: $key\n", FILE_APPEND);
}
if($res == Memcached::RES_SUCCESS) {
$this->hits++;
return $val;
}
else if($res == Memcached::RES_NOTFOUND) {
$this->misses++;
return false;
}
else {
error_log("Memcached error during get($key): $res");
}
}
/**
* @param string $key
* @param mixed $val
* @param int $time
*/
public function set($key, $val, $time=0) {
assert('!is_null($key)');
$key = urlencode($key);
$this->memcache->set($key, $val, $time);
$res = $this->memcache->getResultCode();
if((DEBUG_CACHE === true) || (is_null(DEBUG_CACHE) && @$_GET['DEBUG_CACHE'])) {
file_put_contents("data/cache.log", "Cache set: $key ($time)\n", FILE_APPEND);
}
if($res != Memcached::RES_SUCCESS) {
error_log("Memcached error during set($key): $res");
}
}
/**
* @param string $key
*/
public function delete($key) {
assert('!is_null($key)');
$key = urlencode($key);
$this->memcache->delete($key);
$res = $this->memcache->getResultCode();
if((DEBUG_CACHE === true) || (is_null(DEBUG_CACHE) && @$_GET['DEBUG_CACHE'])) {
file_put_contents("data/cache.log", "Cache delete: $key\n", FILE_APPEND);
}
if($res != Memcached::RES_SUCCESS && $res != Memcached::RES_NOTFOUND) {
error_log("Memcached error during delete($key): $res");
}
}
/**
* @return int
*/
public function get_hits() {return $this->hits;}
/**
* @return int
*/
public function get_misses() {return $this->misses;}
}
class APCCache implements CacheEngine {
public $hits=0, $misses=0;
@ -466,10 +555,13 @@ class Database {
private function connect_cache() {
$matches = array();
if(defined("CACHE_DSN") && CACHE_DSN && preg_match("#(memcache|apc)://(.*)#", CACHE_DSN, $matches)) {
if(defined("CACHE_DSN") && CACHE_DSN && preg_match("#(memcache|memcached|apc)://(.*)#", CACHE_DSN, $matches)) {
if($matches[1] == "memcache") {
$this->cache = new MemcacheCache($matches[2]);
}
else if($matches[1] == "memcached") {
$this->cache = new MemcachedCache($matches[2]);
}
else if($matches[1] == "apc") {
$this->cache = new APCCache($matches[2]);
}
@ -598,18 +690,15 @@ class Database {
* @param string $sql
*/
private function count_execs($db, $sql, $inputarray) {
if ((defined('DEBUG_SQL') && DEBUG_SQL === true) || (!defined('DEBUG_SQL') && @$_GET['DEBUG_SQL'])) {
$fp = @fopen("data/sql.log", "a");
if($fp) {
$sql = trim(preg_replace('/\s+/msi', ' ', $sql));
if(isset($inputarray) && is_array($inputarray) && !empty($inputarray)) {
fwrite($fp, $sql." -- ".join(", ", $inputarray)."\n");
}
else {
fwrite($fp, $sql."\n");
}
fclose($fp);
if((DEBUG_SQL === true) || (is_null(DEBUG_SQL) && @$_GET['DEBUG_SQL'])) {
$sql = trim(preg_replace('/\s+/msi', ' ', $sql));
if(isset($inputarray) && is_array($inputarray) && !empty($inputarray)) {
$text = $sql." -- ".join(", ", $inputarray)."\n";
}
else {
$text = $sql."\n";
}
file_put_contents("data/sql.log", $text, FILE_APPEND);
}
if(!is_array($inputarray)) $this->query_count++;
# handle 2-dimensional input arrays
@ -617,6 +706,14 @@ class Database {
else $this->query_count++;
}
private function count_time($method, $start) {
if((DEBUG_SQL === true) || (is_null(DEBUG_SQL) && @$_GET['DEBUG_SQL'])) {
$text = $method.":".(microtime(true) - $start)."\n";
file_put_contents("data/sql.log", $text, FILE_APPEND);
}
$this->dbtime += microtime(true) - $start;
}
/**
* Execute an SQL query and return an PDO result-set.
*
@ -661,7 +758,7 @@ class Database {
public function get_all($query, $args=array()) {
$_start = microtime(true);
$data = $this->execute($query, $args)->fetchAll();
$this->dbtime += microtime(true) - $_start;
$this->count_time("get_all", $_start);
return $data;
}
@ -675,7 +772,7 @@ class Database {
public function get_row($query, $args=array()) {
$_start = microtime(true);
$row = $this->execute($query, $args)->fetch();
$this->dbtime += microtime(true) - $_start;
$this->count_time("get_row", $_start);
return $row ? $row : null;
}
@ -693,7 +790,7 @@ class Database {
foreach($stmt as $row) {
$res[] = $row[0];
}
$this->dbtime += microtime(true) - $_start;
$this->count_time("get_col", $_start);
return $res;
}
@ -711,7 +808,7 @@ class Database {
foreach($stmt as $row) {
$res[$row[0]] = $row[1];
}
$this->dbtime += microtime(true) - $_start;
$this->count_time("get_pairs", $_start);
return $res;
}
@ -725,7 +822,7 @@ class Database {
public function get_one($query, $args=array()) {
$_start = microtime(true);
$row = $this->execute($query, $args)->fetch();
$this->dbtime += microtime(true) - $_start;
$this->count_time("get_one", $_start);
return $row[0];
}

View file

@ -189,7 +189,7 @@ class Image {
* @param string[] $tags
* @return boolean
*/
public function validate_accel($tags) {
public static function validate_accel($tags) {
$yays = 0;
$nays = 0;
foreach($tags as $tag) {
@ -209,7 +209,7 @@ class Image {
* @return null|PDOStatement
* @throws SCoreException
*/
public function get_accelerated_result($tags, $offset, $limit) {
public static function get_accelerated_result($tags, $offset, $limit) {
global $database;
if(!Image::validate_accel($tags)) {
@ -282,8 +282,7 @@ class Image {
}
else {
$querylet = Image::build_search_querylet($tags);
$result = $database->execute($querylet->sql, $querylet->variables);
return $result->rowCount();
return $database->get_one("SELECT COUNT(*) AS cnt FROM ($querylet->sql) AS tbl", $querylet->variables);
}
}
@ -967,7 +966,7 @@ class Image {
}
}
assert('$positive_tag_id_array || $negative_tag_id_array');
assert('$positive_tag_id_array || $negative_tag_id_array', @$_GET['q']);
$wheres = array();
if (!empty($positive_tag_id_array)) {
$positive_tag_id_list = join(', ', $positive_tag_id_array);
@ -1210,7 +1209,7 @@ function move_upload_to_archive(DataUploadEvent $event) {
* Add a directory full of images
*
* @param $base string
* @return array
* @return array|string[]
*/
function add_dir($base) {
$results = array();
@ -1250,7 +1249,7 @@ function add_image($tmpname, $filename, $tags) {
$metadata = array();
$metadata['filename'] = $pathinfo['basename'];
$metadata['extension'] = $pathinfo['extension'];
$metadata['tags'] = $tags;
$metadata['tags'] = Tag::explode($tags);
$metadata['source'] = null;
$event = new DataUploadEvent($tmpname, $metadata);
send_event($event);

View file

@ -966,6 +966,17 @@ function data_path($filename) {
return $filename;
}
if (!function_exists('mb_strlen')) {
// TODO: we should warn the admin that they are missing multibyte support
function mb_strlen($str, $encoding) {
return strlen($str);
}
function mb_internal_encoding($encoding) {}
function mb_strtolower($str) {
return strtolower($str);
}
}
/**
* @param string $url
* @param string $mfile

View file

@ -42,7 +42,9 @@ class Blocks extends Extension {
foreach($blocks as $block) {
$path = implode("/", $event->args);
if(strlen($path) < 4000 && fnmatch($block['pages'], $path)) {
$page->add_block(new Block($block['title'], $block['content'], $block['area'], $block['priority']));
$b = new Block($block['title'], $block['content'], $block['area'], $block['priority']);
$b->is_content = false;
$page->add_block($b);
}
}

View file

@ -73,7 +73,7 @@ class BulkAddCSV extends Extension {
$metadata = array();
$metadata['filename'] = $pathinfo['basename'];
$metadata['extension'] = $pathinfo['extension'];
$metadata['tags'] = $tags;
$metadata['tags'] = Tag::explode($tags);
$metadata['source'] = $source;
$event = new DataUploadEvent($tmpname, $metadata);
send_event($event);

View file

@ -30,6 +30,7 @@ class Chatbox extends Extension {
// loads the chatbox at the set location
$html = "<div id=\"yshout\"></div>";
$chatblock = new Block("Chatbox", $html, "main", 97);
$chatblock->is_content = false;
$page->add_block($chatblock);
}
}

View file

@ -304,9 +304,14 @@ class CronUploader extends Extension {
/**
* Generate the necessary DataUploadEvent for a given image and tags.
*
* @param string $tmpname
* @param string $filename
* @param string $tags
*/
private function add_image($tmpname, $filename, $tags) {
assert ( file_exists ( $tmpname ) );
assert('is_string($tags)');
$pathinfo = pathinfo ( $filename );
if (! array_key_exists ( 'extension', $pathinfo )) {
@ -315,7 +320,7 @@ class CronUploader extends Extension {
$metadata = array();
$metadata ['filename'] = $pathinfo ['basename'];
$metadata ['extension'] = $pathinfo ['extension'];
$metadata ['tags'] = ""; // = $tags; doesn't work when not logged in here
$metadata ['tags'] = array(); // = $tags; doesn't work when not logged in here
$metadata ['source'] = null;
$event = new DataUploadEvent ( $tmpname, $metadata );
send_event ( $event );
@ -329,7 +334,7 @@ class CronUploader extends Extension {
// Set tags
$img = Image::by_id($event->image_id);
$img->set_tags($tags);
$img->set_tags(Tag::explode($tags));
}
private function generate_image_queue($base = "", $subdir = "") {

View file

@ -43,10 +43,7 @@ class Handle404 extends Extension {
private function count_main($blocks) {
$n = 0;
foreach($blocks as $block) {
if($block->section == "main") $n++; // more hax.
}
if(ext_is_live("Chatbox")) {
$n--; // even more hax.
if($block->section == "main" && $block->is_content) $n++; // more hax.
}
return $n;
}

View file

@ -35,8 +35,10 @@ class ArchiveFileHandler extends Extension {
exec($cmd);
$results = add_dir($tmpdir);
if(count($results) > 0) {
// FIXME no theme?
$this->theme->add_status("Adding files", $results);
// Not all themes have the add_status() method, so need to check before calling.
if (method_exists($this->theme, "add_status")) {
$this->theme->add_status("Adding files", $results);
}
}
deltree($tmpdir);
$event->image_id = -2; // default -1 = upload wasn't handled

View file

@ -37,7 +37,7 @@ class FlashFileHandler extends DataHandlerExtension {
$image->hash = $metadata['hash'];
$image->filename = $metadata['filename'];
$image->ext = $metadata['extension'];
$image->tag_array = $metadata['tags'];
$image->tag_array = is_array($metadata['tags']) ? $metadata['tags'] : Tag::explode($metadata['tags']);
$image->source = $metadata['source'];
$info = getimagesize($filename);

View file

@ -67,7 +67,7 @@ class IcoFileHandler extends Extension {
$image->hash = $metadata['hash'];
$image->filename = $metadata['filename'];
$image->ext = $metadata['extension'];
$image->tag_array = $metadata['tags'];
$image->tag_array = is_array($metadata['tags']) ? $metadata['tags'] : Tag::explode($metadata['tags']);
$image->source = $metadata['source'];
return $image;

View file

@ -43,7 +43,7 @@ class MP3FileHandler extends DataHandlerExtension {
$image->filename = $metadata['filename'];
$image->ext = $metadata['extension'];
$image->tag_array = $metadata['tags'];
$image->tag_array = is_array($metadata['tags']) ? $metadata['tags'] : Tag::explode($metadata['tags']);
$image->source = $metadata['source'];
return $image;

View file

@ -35,7 +35,7 @@ class PixelFileHandler extends DataHandlerExtension {
$image->hash = $metadata['hash'];
$image->filename = (($pos = strpos($metadata['filename'],'?')) !== false) ? substr($metadata['filename'],0,$pos) : $metadata['filename'];
$image->ext = (($pos = strpos($metadata['extension'],'?')) !== false) ? substr($metadata['extension'],0,$pos) : $metadata['extension'];
$image->tag_array = $metadata['tags'];
$image->tag_array = is_array($metadata['tags']) ? $metadata['tags'] : Tag::explode($metadata['tags']);
$image->source = $metadata['source'];
return $image;

View file

@ -75,7 +75,7 @@ class SVGFileHandler extends Extension {
$image->hash = $metadata['hash'];
$image->filename = $metadata['filename'];
$image->ext = $metadata['extension'];
$image->tag_array = $metadata['tags'];
$image->tag_array = is_array($metadata['tags']) ? $metadata['tags'] : Tag::explode($metadata['tags']);
$image->source = $metadata['source'];
return $image;

View file

@ -170,7 +170,7 @@ class VideoFileHandler extends DataHandlerExtension {
$image->filesize = $metadata['size'];
$image->hash = $metadata['hash'];
$image->filename = $metadata['filename'];
$image->tag_array = $metadata['tags'];
$image->tag_array = is_array($metadata['tags']) ? $metadata['tags'] : Tag::explode($metadata['tags']);
$image->source = $metadata['source'];
return $image;

View file

@ -9,8 +9,9 @@ class VideoFileHandlerTheme extends Themelet {
$full_url = make_http($ilink);
$autoplay = $config->get_bool("video_playback_autoplay");
$loop = $config->get_bool("video_playback_loop");
$player = make_link('lib/vendor/swf/flashmediaelement.swf');
$html = "Video not playing? <a href='" . $image->parse_link_template(make_link('image/$id/$id%20-%20$tags.$ext')) . "'>Click here</a> to download the file.<br/>";
$html = "Video not playing? <a href='$ilink'>Click here</a> to download the file.<br/>";
//Browser media format support: https://developer.mozilla.org/en-US/docs/Web/HTML/Supported_media_formats
$supportedExts = ['mp4' => 'video/mp4', 'm4v' => 'video/mp4', 'ogv' => 'video/ogg', 'webm' => 'video/webm', 'flv' => 'video/flv'];
@ -22,8 +23,8 @@ class VideoFileHandlerTheme extends Themelet {
}
$html_fallback = "
<object width=\"100%\" height=\"480px\" type=\"application/x-shockwave-flash\" data=\"lib/vendor/swf/flashmediaelement.swf\">
<param name=\"movie\" value=\"lib/vendor/swf/flashmediaelement.swf\" />
<object width=\"100%\" height=\"480px\" type=\"application/x-shockwave-flash\" data=\"$player\">
<param name=\"movie\" value=\"$player\" />
<param name=\"allowFullScreen\" value=\"true\" />
<param name=\"wmode\" value=\"opaque\" />

View file

@ -265,7 +265,7 @@ class Index extends Extension {
$images = $database->cache->get("post-list:$page_number");
if(!$images) {
$images = Image::find_images(($page_number-1)*$page_size, $page_size, $search_terms);
$database->cache->set("post-list:$page_number", $images, 600);
$database->cache->set("post-list:$page_number", $images, 60);
}
}
else {

View file

@ -31,7 +31,7 @@ class Oekaki extends Extension {
$metadata = array();
$metadata['filename'] = 'oekaki.png';
$metadata['extension'] = $pathinfo['extension'];
$metadata['tags'] = 'oekaki tagme';
$metadata['tags'] = Tag::explode('oekaki tagme');
$metadata['source'] = null;
$duev = new DataUploadEvent($tmpname, $metadata);
send_event($duev);

View file

@ -500,7 +500,7 @@ class OuroborosAPI extends Extension
}
}
$meta = array();
$meta['tags'] = $post->tags;
$meta['tags'] = is_array($post->tags) ? $post->tags : Tag::explode($post->tags);
$meta['source'] = $post->source;
if (defined('ENABLED_EXTS')) {
if (strstr(ENABLED_EXTS, 'rating') !== false) {
@ -536,7 +536,8 @@ class OuroborosAPI extends Extension
if (!is_null($img)) {
$handler = $config->get_string("upload_collision_handler");
if($handler == "merge") {
$merged = array_merge(Tag::explode($post->tags), $img->get_tag_array());
$postTags = is_array($post->tags) ? $post->tags : Tag::explode($post->tags);
$merged = array_merge($postTags, $img->get_tag_array());
send_event(new TagSetEvent($img, $merged));
// This is really the only thing besides tags we should care

View file

@ -15,13 +15,24 @@
class RegenThumb extends Extension {
public function onPageRequest(PageRequestEvent $event) {
global $page, $user;
global $database, $page, $user;
if($event->page_matches("regen_thumb") && $user->can("delete_image") && isset($_POST['image_id'])) {
if($event->page_matches("regen_thumb/one") && $user->can("delete_image") && isset($_POST['image_id'])) {
$image = Image::by_id(int_escape($_POST['image_id']));
send_event(new ThumbnailGenerationEvent($image->hash, $image->ext, true));
$this->theme->display_results($page, $image);
}
if($event->page_matches("regen_thumb/mass") && $user->can("delete_image") && isset($_POST['tags'])) {
$tags = Tag::explode(strtolower($_POST['tags']), false);
$images = Image::find_images(0, 10000, $tags);
foreach($images as $image) {
send_event(new ThumbnailGenerationEvent($image->hash, $image->ext, true));
}
$page->set_mode("redirect");
$page->set_redirect(make_link("post/list"));
}
}
public function onImageAdminBlockBuilding(ImageAdminBlockBuildingEvent $event) {
@ -30,5 +41,12 @@ class RegenThumb extends Extension {
$event->add_part($this->theme->get_buttons_html($event->image->id));
}
}
public function onPostListBuilding(PostListBuildingEvent $event) {
global $user;
if($user->can("delete_image") && !empty($event->search_terms)) {
$event->add_control($this->theme->mtr_html(implode(" ", $event->search_terms)));
}
}
}

View file

@ -9,7 +9,7 @@ class RegenThumbTheme extends Themelet {
*/
public function get_buttons_html($image_id) {
return "
".make_form(make_link("regen_thumb"))."
".make_form(make_link("regen_thumb/one"))."
<input type='hidden' name='image_id' value='$image_id'>
<input type='submit' value='Regenerate Thumbnail'>
</form>
@ -29,5 +29,15 @@ class RegenThumbTheme extends Themelet {
$page->add_block(new NavBlock());
$page->add_block(new Block("Thumbnail", $this->build_thumb_html($image)));
}
public function mtr_html($terms) {
$h_terms = html_escape($terms);
$html = make_form(make_link("regen_thumb/mass"), "POST") . "
<input type='hidden' name='tags' value='$h_terms'>
<input type='submit' value='Regen all thumbs' onclick='return confirm(\"This can use a lot of CPU time.\\nAre you sure you want to do this?\")'>
</form>
";
return $html;
}
}

View file

@ -46,7 +46,7 @@ class TagList extends Extension {
$this->theme->display_page($page);
}
else if($event->page_matches("api/internal/tag_list/complete")) {
if(!isset($_GET["s"])) return;
if(!isset($_GET["s"]) || $_GET["s"] == "" || $_GET["s"] == "_") return;
//$limit = 0;
$cache_key = "autocomplete-" . strtolower($_GET["s"]);
@ -269,7 +269,7 @@ class TagList extends Extension {
$cache_key = data_path("cache/tag_alpha-" . md5("ta" . $tags_min . $starts_with) . ".html");
if(file_exists($cache_key)) {return file_get_contents($cache_key);}
$tag_data = $database->get_all($database->scoreql_to_sql("
$tag_data = $database->get_pairs($database->scoreql_to_sql("
SELECT tag, count
FROM tags
WHERE count >= :tags_min
@ -296,8 +296,10 @@ class TagList extends Extension {
mb_internal_encoding('UTF-8');
$lastLetter = "";
foreach($tag_data as $row) {
$tag = $row['tag'];
# postres utf8 string sort ignores punctuation, so we get "aza, a-zb, azc"
# which breaks down into "az, a-, az" :(
ksort($tag_data, SORT_STRING | SORT_FLAG_CASE);
foreach($tag_data as $tag => $count) {
if($lastLetter != mb_strtolower(substr($tag, 0, count($starts_with)+1))) {
$lastLetter = mb_strtolower(substr($tag, 0, count($starts_with)+1));
$h_lastLetter = html_escape($lastLetter);
@ -305,7 +307,6 @@ class TagList extends Extension {
}
$link = $this->tag_link($tag);
$h_tag = html_escape($tag);
$count = $row['count'];
$html .= "<a href='$link'>$h_tag&nbsp;($count)</a>\n";
}

View file

@ -30,11 +30,9 @@ class ViewImageTheme extends Themelet {
protected function build_pin(Image $image) {
if(isset($_GET['search'])) {
$search_terms = explode(' ', $_GET['search']);
$query = "search=".url_escape($_GET['search']);
}
else {
$search_terms = array();
$query = null;
}

View file

@ -132,7 +132,7 @@ abstract class ShimmiePHPUnitTestCase extends \PHPUnit_Framework_TestCase {
// post things
/**
* @param string $filename
* @param string|string[] $tags
* @param string $tags
* @return int
*/
protected function post_image($filename, $tags) {