2007-04-16 11:58:25 +00:00
|
|
|
<?php
|
2008-04-01 10:11:36 +00:00
|
|
|
$ADODB_CACHE_DIR=sys_get_temp_dir();
|
2007-04-16 11:58:25 +00:00
|
|
|
require_once "lib/adodb/adodb.inc.php";
|
|
|
|
|
2007-12-06 11:01:18 +00:00
|
|
|
/* Querylet {{{
|
|
|
|
* A fragment of a query, used to build large search queries
|
|
|
|
*/
|
|
|
|
class Querylet {
|
2007-04-16 11:58:25 +00:00
|
|
|
var $sql;
|
|
|
|
var $variables;
|
|
|
|
|
2008-02-06 17:24:08 +00:00
|
|
|
public function Querylet($sql, $variables=array()) {
|
2007-04-16 11:58:25 +00:00
|
|
|
$this->sql = $sql;
|
|
|
|
$this->variables = $variables;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function append($querylet) {
|
2008-02-06 17:24:08 +00:00
|
|
|
assert(!is_null($querylet));
|
2007-04-16 11:58:25 +00:00
|
|
|
$this->sql .= $querylet->sql;
|
|
|
|
$this->variables = array_merge($this->variables, $querylet->variables);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function append_sql($sql) {
|
|
|
|
$this->sql .= $sql;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function add_variable($var) {
|
|
|
|
$this->variables[] = $var;
|
|
|
|
}
|
|
|
|
} // }}}
|
|
|
|
|
2007-12-06 11:01:18 +00:00
|
|
|
/*
|
|
|
|
* A class for controlled database access, available through "global $database"
|
|
|
|
*/
|
2007-04-16 11:58:25 +00:00
|
|
|
class Database {
|
|
|
|
var $db;
|
|
|
|
var $extensions;
|
2007-07-05 21:41:13 +00:00
|
|
|
var $get_images = "SELECT images.*,UNIX_TIMESTAMP(posted) AS posted_timestamp FROM images ";
|
2007-04-16 11:58:25 +00:00
|
|
|
|
2007-12-11 18:37:11 +00:00
|
|
|
/*
|
|
|
|
* Create a new database object using connection info
|
|
|
|
* stored in config.php in the root shimmie folder
|
|
|
|
*/
|
2007-04-16 11:58:25 +00:00
|
|
|
public function Database() {
|
|
|
|
if(is_readable("config.php")) {
|
|
|
|
require_once "config.php";
|
2007-07-28 14:15:03 +00:00
|
|
|
$this->db = @NewADOConnection($database_dsn);
|
|
|
|
if($this->db) {
|
|
|
|
$this->db->SetFetchMode(ADODB_FETCH_ASSOC);
|
|
|
|
$this->db->Execute("SET NAMES utf8"); // FIXME: mysql specific :|
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$version = VERSION;
|
|
|
|
print "
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<title>Internal error - Shimmie-$version</title>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
Internal error: Could not connect to database
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
";
|
|
|
|
exit;
|
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
header("Location: install.php");
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// misc {{{
|
|
|
|
public function count_pages($tags=array()) {
|
|
|
|
global $config;
|
|
|
|
$images_per_page = $config->get_int('index_width') * $config->get_int('index_height');
|
|
|
|
if(count($tags) == 0) {
|
|
|
|
return ceil($this->db->GetOne("SELECT COUNT(*) FROM images") / $images_per_page);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$querylet = $this->build_search_querylet($tags);
|
2007-05-17 03:52:50 +00:00
|
|
|
$result = $this->execute($querylet->sql, $querylet->variables);
|
2007-04-16 11:58:25 +00:00
|
|
|
return ceil($result->RecordCount() / $images_per_page);
|
|
|
|
}
|
|
|
|
}
|
2007-05-17 03:48:34 +00:00
|
|
|
|
2007-05-23 23:26:11 +00:00
|
|
|
public function execute($query, $args=array()) {
|
2007-10-21 17:13:57 +00:00
|
|
|
$result = $this->db->Execute($query, $args);
|
|
|
|
if($result === False) {
|
|
|
|
print "SQL Error: " . $this->db->ErrorMsg();
|
|
|
|
print "<br>Query: $query";
|
|
|
|
print "<br>Args: "; print_r($args);
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
return $result;
|
2007-08-08 05:47:23 +00:00
|
|
|
}
|
|
|
|
|
2008-01-05 00:22:19 +00:00
|
|
|
public function get_all($query, $args=array()) {
|
|
|
|
$result = $this->db->GetAll($query, $args);
|
|
|
|
if($result === False) {
|
|
|
|
print "SQL Error: " . $this->db->ErrorMsg();
|
|
|
|
print "<br>Query: $query";
|
|
|
|
print "<br>Args: "; print_r($args);
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
2008-01-27 15:30:44 +00:00
|
|
|
|
|
|
|
public function upgrade_schema($filename) {
|
|
|
|
//print "<br>upgrading $filename";
|
2008-01-05 00:22:19 +00:00
|
|
|
|
2007-08-08 05:47:23 +00:00
|
|
|
global $config;
|
2008-01-27 15:30:44 +00:00
|
|
|
if($config->get_bool("in_upgrade")) return;
|
|
|
|
$config->set_bool("in_upgrade", true);
|
|
|
|
|
|
|
|
require_once "lib/adodb/adodb-xmlschema03.inc.php";
|
|
|
|
$schema = new adoSchema($this->db);
|
|
|
|
$sql = $schema->ParseSchema($filename);
|
|
|
|
//echo "<pre>"; var_dump($sql); echo "</pre>";
|
|
|
|
$result = $schema->ExecuteSchema();
|
2007-08-08 05:47:23 +00:00
|
|
|
|
2008-01-27 15:30:44 +00:00
|
|
|
if(!$result) {
|
|
|
|
die("Error creating tables from XML schema ($filename)");
|
2007-05-17 03:48:34 +00:00
|
|
|
}
|
2008-01-27 15:30:44 +00:00
|
|
|
|
|
|
|
$config->set_bool("in_upgrade", false);
|
2007-05-17 03:48:34 +00:00
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
// }}}
|
|
|
|
// tags {{{
|
|
|
|
public function resolve_alias($tag) {
|
|
|
|
$newtag = $this->db->GetOne("SELECT newtag FROM aliases WHERE oldtag=?", array($tag));
|
|
|
|
if(!empty($newtag)) {
|
|
|
|
return $newtag;
|
|
|
|
} else {
|
|
|
|
return $tag;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function sanitise($tag) {
|
2007-10-28 01:30:26 +00:00
|
|
|
$tag = preg_replace("/[\s?*]/", "", $tag);
|
|
|
|
$tag = preg_replace("/\.+/", ".", $tag);
|
2007-10-28 17:49:45 +00:00
|
|
|
$tag = preg_replace("/^(\.+[\/\\\\])+/", "", $tag);
|
2007-10-28 01:30:26 +00:00
|
|
|
return $tag;
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function build_search_querylet($terms) {
|
|
|
|
$tag_search = new Querylet("0");
|
|
|
|
$positive_tag_count = 0;
|
2007-04-28 18:58:23 +00:00
|
|
|
$negative_tag_count = 0;
|
2007-04-16 11:58:25 +00:00
|
|
|
$img_search = new Querylet("");
|
|
|
|
|
|
|
|
foreach($terms as $term) {
|
|
|
|
$negative = false;
|
|
|
|
if((strlen($term) > 0) && ($term[0] == '-')) {
|
|
|
|
$negative = true;
|
|
|
|
$term = substr($term, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
$term = $this->resolve_alias($term);
|
|
|
|
|
2008-02-06 17:24:08 +00:00
|
|
|
$stpe = new SearchTermParseEvent($term);
|
|
|
|
send_event($stpe);
|
|
|
|
if($stpe->is_querylet_set()) {
|
|
|
|
$img_search->append($stpe->get_querylet());
|
2007-06-25 16:40:47 +00:00
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
else {
|
|
|
|
$term = str_replace("*", "%", $term);
|
|
|
|
$term = str_replace("?", "_", $term);
|
2007-10-28 17:07:29 +00:00
|
|
|
if(!preg_match("/^[%_]+$/", $term)) {
|
|
|
|
$sign = $negative ? "-" : "+";
|
|
|
|
if($sign == "+") $positive_tag_count++;
|
|
|
|
else $negative_tag_count++;
|
|
|
|
$tag_search->append(new Querylet(" $sign (tag LIKE ?)", array($term)));
|
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-28 18:58:23 +00:00
|
|
|
if($positive_tag_count + $negative_tag_count == 0) {
|
2007-05-04 22:15:32 +00:00
|
|
|
$query = new Querylet($this->get_images);
|
2007-07-04 01:21:08 +00:00
|
|
|
|
|
|
|
if(strlen($img_search->sql) > 0) {
|
|
|
|
$query->append_sql("WHERE 1=1 ");
|
|
|
|
$query->append($img_search);
|
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
2007-07-04 00:56:02 +00:00
|
|
|
else if($positive_tag_count == 1 && $negative_tag_count == 0) {
|
|
|
|
$query = new Querylet(
|
2007-07-04 15:29:40 +00:00
|
|
|
// MySQL is braindead, and does a full table scan on images, running the subquery once for each row -_-
|
|
|
|
// "{$this->get_images} WHERE images.id IN (SELECT image_id FROM tags WHERE tag LIKE ?) ",
|
2007-07-05 21:30:37 +00:00
|
|
|
"
|
2007-07-05 21:41:13 +00:00
|
|
|
SELECT images.*, UNIX_TIMESTAMP(posted) AS posted_timestamp
|
2007-07-05 21:30:37 +00:00
|
|
|
FROM tags, image_tags, images
|
|
|
|
WHERE
|
|
|
|
tag LIKE ?
|
|
|
|
AND tags.id = image_tags.tag_id
|
|
|
|
AND image_tags.image_id = images.id
|
|
|
|
",
|
2007-07-04 00:56:02 +00:00
|
|
|
$tag_search->variables);
|
2007-07-04 01:21:08 +00:00
|
|
|
|
|
|
|
if(strlen($img_search->sql) > 0) {
|
|
|
|
$query->append($img_search);
|
|
|
|
}
|
2007-07-04 00:56:02 +00:00
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
else {
|
2007-04-28 19:02:54 +00:00
|
|
|
$s_tag_array = array_map("sql_escape", $tag_search->variables);
|
2007-04-28 18:40:08 +00:00
|
|
|
$s_tag_list = join(', ', $s_tag_array);
|
2007-07-06 07:28:42 +00:00
|
|
|
|
|
|
|
$tag_id_array = array();
|
2007-07-23 00:52:24 +00:00
|
|
|
$tags_ok = true;
|
2007-07-06 07:28:42 +00:00
|
|
|
foreach($tag_search->variables as $tag) {
|
2007-07-23 00:52:24 +00:00
|
|
|
$tag_ids = $this->db->GetCol("SELECT id FROM tags WHERE tag LIKE ?", array($tag));
|
|
|
|
$tag_id_array = array_merge($tag_id_array, $tag_ids);
|
|
|
|
$tags_ok = count($tag_ids) > 0;
|
|
|
|
if(!$tags_ok) break;
|
|
|
|
}
|
|
|
|
if($tags_ok) {
|
|
|
|
$tag_id_list = join(', ', $tag_id_array);
|
|
|
|
|
|
|
|
$subquery = new Querylet("
|
|
|
|
SELECT images.*, SUM({$tag_search->sql}) AS score
|
|
|
|
FROM images
|
|
|
|
LEFT JOIN image_tags ON image_tags.image_id = images.id
|
|
|
|
JOIN tags ON image_tags.tag_id = tags.id
|
|
|
|
WHERE tags.id IN ({$tag_id_list})
|
|
|
|
GROUP BY images.id
|
|
|
|
HAVING score = ?",
|
|
|
|
array_merge(
|
|
|
|
$tag_search->variables,
|
|
|
|
array($positive_tag_count)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$query = new Querylet("
|
|
|
|
SELECT *, UNIX_TIMESTAMP(posted) AS posted_timestamp
|
|
|
|
FROM ({$subquery->sql}) AS images ", $subquery->variables);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
# there are no results, "where 1=0" should shortcut things
|
|
|
|
$query = new Querylet("
|
|
|
|
SELECT images.*
|
|
|
|
FROM images
|
|
|
|
LEFT JOIN image_tags ON image_tags.image_id = images.id
|
|
|
|
JOIN tags ON image_tags.tag_id = tags.id
|
|
|
|
WHERE 1=0
|
|
|
|
");
|
2007-07-06 07:28:42 +00:00
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
|
2007-07-04 01:21:08 +00:00
|
|
|
if(strlen($img_search->sql) > 0) {
|
|
|
|
$query->append_sql("WHERE 1=1 ");
|
|
|
|
$query->append($img_search);
|
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function delete_tags_from_image($image_id) {
|
2007-07-07 02:52:33 +00:00
|
|
|
$this->execute("UPDATE tags SET count = count - 1 WHERE id IN (SELECT tag_id FROM image_tags WHERE image_id = ?)", array($image_id));
|
2007-07-05 21:30:37 +00:00
|
|
|
$this->execute("DELETE FROM image_tags WHERE image_id=?", array($image_id));
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function set_tags($image_id, $tags) {
|
|
|
|
$tags = tag_explode($tags);
|
|
|
|
|
|
|
|
$tags = array_map(array($this, 'resolve_alias'), $tags);
|
|
|
|
$tags = array_map(array($this, 'sanitise'), $tags);
|
|
|
|
$tags = array_unique($tags); // remove any duplicate tags
|
|
|
|
|
|
|
|
// delete old
|
|
|
|
$this->delete_tags_from_image($image_id);
|
|
|
|
|
|
|
|
// insert each new tag
|
|
|
|
foreach($tags as $tag) {
|
2007-07-07 02:52:33 +00:00
|
|
|
$this->execute("INSERT IGNORE INTO tags(tag) VALUES (?)", array($tag));
|
2007-07-05 21:30:37 +00:00
|
|
|
$this->execute("INSERT INTO image_tags(image_id, tag_id) VALUES(?, (SELECT id FROM tags WHERE tag = ?))", array($image_id, $tag));
|
2007-07-07 02:52:33 +00:00
|
|
|
$this->execute("UPDATE tags SET count = count + 1 WHERE tag = ?", array($tag));
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
2007-08-01 16:58:50 +00:00
|
|
|
|
|
|
|
public function set_source($image_id, $source) {
|
|
|
|
if(empty($source)) $source = null;
|
|
|
|
$this->execute("UPDATE images SET source=? WHERE id=?", array($source, $image_id));
|
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
// }}}
|
|
|
|
// images {{{
|
|
|
|
public function get_images($start, $limit, $tags=array()) {
|
|
|
|
$images = array();
|
|
|
|
|
|
|
|
assert($start >= 0);
|
|
|
|
assert($limit > 0);
|
|
|
|
if($start < 0) $start = 0;
|
|
|
|
if($limit < 1) $limit = 1;
|
|
|
|
|
|
|
|
if(count($tags) == 0) {
|
2007-08-06 23:59:27 +00:00
|
|
|
$result = $this->execute("{$this->get_images} ORDER BY id DESC LIMIT ? OFFSET ?", array($limit, $start));
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$querylet = $this->build_search_querylet($tags);
|
2007-08-06 23:59:27 +00:00
|
|
|
$querylet->append(new Querylet("ORDER BY images.id DESC LIMIT ? OFFSET ?", array($limit, $start)));
|
2007-05-17 03:52:50 +00:00
|
|
|
$result = $this->execute($querylet->sql, $querylet->variables);
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while(!$result->EOF) {
|
|
|
|
$images[] = new Image($result->fields);
|
|
|
|
$result->MoveNext();
|
|
|
|
}
|
|
|
|
return $images;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function get_next_image($id, $tags=array(), $next=true) {
|
|
|
|
if($next) {
|
|
|
|
$gtlt = "<";
|
|
|
|
$dir = "DESC";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$gtlt = ">";
|
|
|
|
$dir = "ASC";
|
|
|
|
}
|
|
|
|
|
|
|
|
if(count($tags) == 0) {
|
2007-07-05 21:41:13 +00:00
|
|
|
$row = $this->db->GetRow("{$this->get_images} WHERE images.id $gtlt ? ORDER BY images.id $dir LIMIT 1", array((int)$id));
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
else {
|
2007-07-15 17:29:05 +00:00
|
|
|
$tags[] = "id$gtlt$id";
|
2007-04-16 11:58:25 +00:00
|
|
|
$querylet = $this->build_search_querylet($tags);
|
2007-07-05 21:41:13 +00:00
|
|
|
$querylet->append_sql(" ORDER BY images.id $dir LIMIT 1");
|
2007-04-16 11:58:25 +00:00
|
|
|
$row = $this->db->GetRow($querylet->sql, $querylet->variables);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ($row ? new Image($row) : null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function get_prev_image($id, $tags=array()) {
|
|
|
|
return $this->get_next_image($id, $tags, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function get_image($id) {
|
|
|
|
$image = null;
|
2007-07-05 21:41:13 +00:00
|
|
|
$row = $this->db->GetRow("{$this->get_images} WHERE images.id=?", array($id));
|
2007-04-16 11:58:25 +00:00
|
|
|
return ($row ? new Image($row) : null);
|
|
|
|
}
|
|
|
|
|
2007-05-01 12:40:39 +00:00
|
|
|
public function get_image_by_hash($hash) {
|
|
|
|
$image = null;
|
2007-05-04 22:15:32 +00:00
|
|
|
$row = $this->db->GetRow("{$this->get_images} WHERE hash=?", array($hash));
|
2007-05-01 12:40:39 +00:00
|
|
|
return ($row ? new Image($row) : null);
|
|
|
|
}
|
|
|
|
|
2007-04-16 11:58:25 +00:00
|
|
|
public function remove_image($id) {
|
2007-05-17 03:52:50 +00:00
|
|
|
$this->execute("DELETE FROM images WHERE id=?", array($id));
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
// }}}
|
|
|
|
// users {{{
|
2007-05-16 23:50:51 +00:00
|
|
|
var $SELECT_USER = "SELECT *,(unix_timestamp(now()) - unix_timestamp(joindate))/(60*60*24) AS days_old FROM users ";
|
2007-04-16 11:58:25 +00:00
|
|
|
|
|
|
|
public function get_user_session($name, $session) {
|
|
|
|
$row = $this->db->GetRow("{$this->SELECT_USER} WHERE name LIKE ? AND md5(concat(pass, ?)) = ?",
|
|
|
|
array($name, $_SERVER['REMOTE_ADDR'], $session));
|
|
|
|
return $row ? new User($row) : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function get_user_by_id($id) {
|
|
|
|
$row = $this->db->GetRow("{$this->SELECT_USER} WHERE id=?", array($id));
|
|
|
|
return $row ? new User($row) : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function get_user_by_name($name) {
|
|
|
|
$row = $this->db->GetRow("{$this->SELECT_USER} WHERE name=?", array($name));
|
|
|
|
return $row ? new User($row) : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function get_user_by_name_and_hash($name, $hash) {
|
|
|
|
$row = $this->db->GetRow("{$this->SELECT_USER} WHERE name LIKE ? AND pass = ?", array($name, $hash));
|
|
|
|
return $row ? new User($row) : null;
|
|
|
|
}
|
|
|
|
// }}}
|
|
|
|
}
|
|
|
|
?>
|