Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/shish/shimmie2 into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
shish committed Sep 17, 2017
2 parents a32bc64 + 408b45e commit 860e828
Show file tree
Hide file tree
Showing 26 changed files with 222 additions and 66 deletions.
1 change: 1 addition & 0 deletions core/_bootstrap.inc.php
Expand Up @@ -10,6 +10,7 @@
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();
Expand Down
14 changes: 13 additions & 1 deletion core/block.class.php
Expand Up @@ -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.
*
Expand All @@ -58,7 +66,11 @@ public function __construct($header, $body, /*string*/ $section="main", /*int*/
$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));
}

/**
Expand Down
145 changes: 121 additions & 24 deletions core/database.class.php
Expand Up @@ -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]);
}

/**
Expand Down Expand Up @@ -378,6 +373,100 @@ public function get_hits() {return $this->hits;}
*/
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;
Expand Down Expand Up @@ -466,10 +555,13 @@ public function __construct() {

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]);
}
Expand Down Expand Up @@ -598,25 +690,30 @@ public function get_driver_name() {
* @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
else if(is_array(reset($inputarray))) $this->query_count += sizeof($inputarray);
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.
*
Expand Down Expand Up @@ -661,7 +758,7 @@ public function execute($query, $args=array()) {
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;
}

Expand All @@ -675,7 +772,7 @@ public function get_all($query, $args=array()) {
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;
}

Expand All @@ -693,7 +790,7 @@ public function get_col($query, $args=array()) {
foreach($stmt as $row) {
$res[] = $row[0];
}
$this->dbtime += microtime(true) - $_start;
$this->count_time("get_col", $_start);
return $res;
}

Expand All @@ -711,7 +808,7 @@ public function get_pairs($query, $args=array()) {
foreach($stmt as $row) {
$res[$row[0]] = $row[1];
}
$this->dbtime += microtime(true) - $_start;
$this->count_time("get_pairs", $_start);
return $res;
}

Expand All @@ -725,7 +822,7 @@ public function get_pairs($query, $args=array()) {
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];
}

Expand Down
13 changes: 6 additions & 7 deletions core/imageboard.pack.php
Expand Up @@ -189,7 +189,7 @@ public static function find_images(/*int*/ $start, /*int*/ $limit, $tags=array()
* @param string[] $tags
* @return boolean
*/
public function validate_accel($tags) {
public static function validate_accel($tags) {
$yays = 0;
$nays = 0;
foreach($tags as $tag) {
Expand All @@ -209,7 +209,7 @@ public function validate_accel($tags) {
* @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)) {
Expand Down Expand Up @@ -282,8 +282,7 @@ public static function count_images($tags=array()) {
}
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);
}
}

Expand Down Expand Up @@ -967,7 +966,7 @@ private static function build_accurate_search_querylet($tag_querylets) {
}
}

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);
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
11 changes: 11 additions & 0 deletions core/util.inc.php
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion ext/blocks/main.php
Expand Up @@ -42,7 +42,9 @@ public function onPageRequest(PageRequestEvent $event) {
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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion ext/bulk_add_csv/main.php
Expand Up @@ -73,7 +73,7 @@ private function add_image($tmpname, $filename, $tags, $source, $rating, $thumbf
$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);
Expand Down
1 change: 1 addition & 0 deletions ext/chatbox/main.php
Expand Up @@ -30,6 +30,7 @@ public function onPageRequest(PageRequestEvent $event) {
// 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);
}
}
9 changes: 7 additions & 2 deletions ext/cron_uploader/main.php
Expand Up @@ -304,9 +304,14 @@ private function move_uploaded($path, $filename, $corrupt = false) {

/**
* 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 )) {
Expand All @@ -315,7 +320,7 @@ private function add_image($tmpname, $filename, $tags) {
$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 );
Expand All @@ -329,7 +334,7 @@ private function add_image($tmpname, $filename, $tags) {

// 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 = "") {
Expand Down

0 comments on commit 860e828

Please sign in to comment.