summaryrefslogtreecommitdiff
path: root/modules/gallery/models
diff options
context:
space:
mode:
Diffstat (limited to 'modules/gallery/models')
-rw-r--r--modules/gallery/models/access_cache.php2
-rw-r--r--modules/gallery/models/access_intent.php2
-rw-r--r--modules/gallery/models/cache.php2
-rw-r--r--modules/gallery/models/failed_auth.php2
-rw-r--r--modules/gallery/models/graphics_rule.php2
-rw-r--r--modules/gallery/models/incoming_translation.php2
-rw-r--r--modules/gallery/models/item.php194
-rw-r--r--modules/gallery/models/log.php2
-rw-r--r--modules/gallery/models/message.php2
-rw-r--r--modules/gallery/models/module.php2
-rw-r--r--modules/gallery/models/outgoing_translation.php2
-rw-r--r--modules/gallery/models/permission.php2
-rw-r--r--modules/gallery/models/task.php2
-rw-r--r--modules/gallery/models/theme.php2
-rw-r--r--modules/gallery/models/var.php2
15 files changed, 106 insertions, 116 deletions
diff --git a/modules/gallery/models/access_cache.php b/modules/gallery/models/access_cache.php
index e730f25c..217dd147 100644
--- a/modules/gallery/models/access_cache.php
+++ b/modules/gallery/models/access_cache.php
@@ -1,7 +1,7 @@
<?php defined("SYSPATH") or die("No direct script access.");
/**
* Gallery - a web based photo album viewer and editor
- * Copyright (C) 2000-2010 Bharat Mediratta
+ * Copyright (C) 2000-2011 Bharat Mediratta
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
diff --git a/modules/gallery/models/access_intent.php b/modules/gallery/models/access_intent.php
index 31711014..43c46d0c 100644
--- a/modules/gallery/models/access_intent.php
+++ b/modules/gallery/models/access_intent.php
@@ -1,7 +1,7 @@
<?php defined("SYSPATH") or die("No direct script access.");
/**
* Gallery - a web based photo album viewer and editor
- * Copyright (C) 2000-2010 Bharat Mediratta
+ * Copyright (C) 2000-2011 Bharat Mediratta
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
diff --git a/modules/gallery/models/cache.php b/modules/gallery/models/cache.php
index 1c464d51..9d02a44c 100644
--- a/modules/gallery/models/cache.php
+++ b/modules/gallery/models/cache.php
@@ -1,7 +1,7 @@
<?php defined("SYSPATH") or die("No direct script access.");
/**
* Gallery - a web based photo album viewer and editor
- * Copyright (C) 2000-2010 Bharat Mediratta
+ * Copyright (C) 2000-2011 Bharat Mediratta
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
diff --git a/modules/gallery/models/failed_auth.php b/modules/gallery/models/failed_auth.php
index 649e4bc9..a418681d 100644
--- a/modules/gallery/models/failed_auth.php
+++ b/modules/gallery/models/failed_auth.php
@@ -1,7 +1,7 @@
<?php defined("SYSPATH") or die("No direct script access.");
/**
* Gallery - a web based photo album viewer and editor
- * Copyright (C) 2000-2010 Bharat Mediratta
+ * Copyright (C) 2000-2011 Bharat Mediratta
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
diff --git a/modules/gallery/models/graphics_rule.php b/modules/gallery/models/graphics_rule.php
index 09410af0..feaae3c6 100644
--- a/modules/gallery/models/graphics_rule.php
+++ b/modules/gallery/models/graphics_rule.php
@@ -1,7 +1,7 @@
<?php defined("SYSPATH") or die("No direct script access.");
/**
* Gallery - a web based photo album viewer and editor
- * Copyright (C) 2000-2010 Bharat Mediratta
+ * Copyright (C) 2000-2011 Bharat Mediratta
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
diff --git a/modules/gallery/models/incoming_translation.php b/modules/gallery/models/incoming_translation.php
index ff6a7160..07df9d2f 100644
--- a/modules/gallery/models/incoming_translation.php
+++ b/modules/gallery/models/incoming_translation.php
@@ -1,7 +1,7 @@
<?php defined("SYSPATH") or die("No direct script access.");
/**
* Gallery - a web based photo album viewer and editor
- * Copyright (C) 2000-2010 Bharat Mediratta
+ * Copyright (C) 2000-2011 Bharat Mediratta
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
diff --git a/modules/gallery/models/item.php b/modules/gallery/models/item.php
index 9016a04a..8f4bc5e4 100644
--- a/modules/gallery/models/item.php
+++ b/modules/gallery/models/item.php
@@ -1,7 +1,7 @@
<?php defined("SYSPATH") or die("No direct script access.");
/**
* Gallery - a web based photo album viewer and editor
- * Copyright (C) 2000-2010 Bharat Mediratta
+ * Copyright (C) 2000-2011 Bharat Mediratta
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -28,7 +28,7 @@ class Item_Model_Core extends ORM_MPTT {
if (!$this->loaded()) {
// Set reasonable defaults
$this->created = time();
- $this->rand_key = ((float)mt_rand()) / (float)mt_getrandmax();
+ $this->rand_key = random::percent();
$this->thumb_dirty = 1;
$this->resize_dirty = 1;
$this->sort_column = "created";
@@ -390,7 +390,7 @@ class Item_Model_Core extends ORM_MPTT {
if (file_exists($this->resize_path()) ||
file_exists($this->thumb_path())) {
$pi = pathinfo($this->name);
- $this->name = $pi["filename"] . "-" . rand() . "." . $pi["extension"];
+ $this->name = $pi["filename"] . "-" . random::int() . "." . $pi["extension"];
parent::save();
}
@@ -512,7 +512,7 @@ class Item_Model_Core extends ORM_MPTT {
->or_where("slug", "=", $this->slug)
->close()
->find()->id) {
- $rand = rand();
+ $rand = random::int();
if ($base_ext) {
$this->name = "$base_name-$rand.$base_ext";
} else {
@@ -546,83 +546,12 @@ class Item_Model_Core extends ORM_MPTT {
/**
* Find the position of the given child id in this album. The resulting value is 1-indexed, so
* the first child in the album is at position 1.
+ *
+ * This method stands as a backward compatibility for gallery 3.0, and will
+ * be deprecated in version 3.1.
*/
public function get_position($child, $where=array()) {
- if (!strcasecmp($this->sort_order, "DESC")) {
- $comp = ">";
- } else {
- $comp = "<";
- }
- $db = db::build();
-
- // If the comparison column has NULLs in it, we can't use comparators on it and will have to
- // deal with it the hard way.
- $count = $db->from("items")
- ->where("parent_id", "=", $this->id)
- ->where($this->sort_column, "IS", null)
- ->merge_where($where)
- ->count_records();
-
- if (empty($count)) {
- // There are no NULLs in the sort column, so we can just use it directly.
- $sort_column = $this->sort_column;
-
- $position = $db->from("items")
- ->where("parent_id", "=", $this->id)
- ->where($sort_column, $comp, $child->$sort_column)
- ->merge_where($where)
- ->count_records();
-
- // We stopped short of our target value in the sort (notice that we're using a < comparator
- // above) because it's possible that we have duplicate values in the sort column. An
- // equality check would just arbitrarily pick one of those multiple possible equivalent
- // columns, which would mean that if you choose a sort order that has duplicates, it'd pick
- // any one of them as the child's "position".
- //
- // Fix this by doing a 2nd query where we iterate over the equivalent columns and add them to
- // our base value.
- foreach ($db
- ->select("id")
- ->from("items")
- ->where("parent_id", "=", $this->id)
- ->where($sort_column, "=", $child->$sort_column)
- ->merge_where($where)
- ->order_by(array("id" => "ASC"))
- ->execute() as $row) {
- $position++;
- if ($row->id == $child->id) {
- break;
- }
- }
- } else {
- // There are NULLs in the sort column, so we can't use MySQL comparators. Fall back to
- // iterating over every child row to get to the current one. This can be wildly inefficient
- // for really large albums, but it should be a rare case that the user is sorting an album
- // with null values in the sort column.
- //
- // Reproduce the children() functionality here using Database directly to avoid loading the
- // whole ORM for each row.
- $order_by = array($this->sort_column => $this->sort_order);
- // Use id as a tie breaker
- if ($this->sort_column != "id") {
- $order_by["id"] = "ASC";
- }
-
- $position = 0;
- foreach ($db->select("id")
- ->from("items")
- ->where("parent_id", "=", $this->id)
- ->merge_where($where)
- ->order_by($order_by)
- ->execute() as $row) {
- $position++;
- if ($row->id == $child->id) {
- break;
- }
- }
- }
-
- return $position;
+ return item::get_position($child, $where);
}
/**
@@ -653,7 +582,7 @@ class Item_Model_Core extends ORM_MPTT {
/**
* Calculate the largest width/height that fits inside the given maximum, while preserving the
- * aspect ratio.
+ * aspect ratio. Don't upscale.
* @param int $max Maximum size of the largest dimension
* @return array
*/
@@ -661,6 +590,10 @@ class Item_Model_Core extends ORM_MPTT {
$width = $this->thumb_width;
$height = $this->thumb_height;
+ if ($width <= $max && $height <= $max) {
+ return array($height, $width);
+ }
+
if ($height) {
if (isset($max)) {
if ($width > $height) {
@@ -848,10 +781,17 @@ class Item_Model_Core extends ORM_MPTT {
}
} else {
// New items must have an extension
- if (!pathinfo($this->name, PATHINFO_EXTENSION)) {
+ $ext = pathinfo($this->name, PATHINFO_EXTENSION);
+ if (!$ext) {
$v->add_error("name", "illegal_data_file_extension");
return;
}
+
+ if ($this->is_movie() && !preg_match("/^(flv|mp4|m4v)$/i", $ext)) {
+ $v->add_error("name", "illegal_data_file_extension");
+ } else if ($this->is_photo() && !preg_match("/^(gif|jpg|jpeg|png)$/i", $ext)) {
+ $v->add_error("name", "illegal_data_file_extension");
+ }
}
}
@@ -980,48 +920,88 @@ class Item_Model_Core extends ORM_MPTT {
/**
* Same as ORM::as_array() but convert id fields into their RESTful form.
+ *
+ * @param array if specified, only return the named fields
*/
- public function as_restful_array() {
+ public function as_restful_array($fields=array()) {
+ if ($fields) {
+ $data = array();
+ foreach ($fields as $field) {
+ if (isset($this->object[$field])) {
+ $data[$field] = $this->__get($field);
+ }
+ }
+ $fields = array_flip($fields);
+ } else {
+ $data = $this->as_array();
+ }
+
// Convert item ids to rest URLs for consistency
- $data = $this->as_array();
- if ($tmp = $this->parent()) {
- $data["parent"] = rest::url("item", $tmp);
+ if (empty($fields) || isset($fields["parent"])) {
+ if ($tmp = $this->parent()) {
+ $data["parent"] = rest::url("item", $tmp);
+ }
+ unset($data["parent_id"]);
}
- unset($data["parent_id"]);
- if ($tmp = $this->album_cover()) {
- $data["album_cover"] = rest::url("item", $tmp);
+
+ if (empty($fields) || isset($fields["album_cover"])) {
+ if ($tmp = $this->album_cover()) {
+ $data["album_cover"] = rest::url("item", $tmp);
+ }
+ unset($data["album_cover_item_id"]);
}
- unset($data["album_cover_item_id"]);
- $data["web_url"] = $this->abs_url();
+ if (empty($fields) || isset($fields["web_url"])) {
+ $data["web_url"] = $this->abs_url();
+ }
if (!$this->is_album()) {
if (access::can("view_full", $this)) {
- $data["file_url"] = rest::url("data", $this, "full");
- $data["file_size"] = filesize($this->file_path());
- }
- if (access::user_can(identity::guest(), "view_full", $this)) {
- $data["file_url_public"] = $this->file_url(true);
+ if (empty($fields) || isset($fields["file_url"])) {
+ $data["file_url"] = rest::url("data", $this, "full");
+ }
+ if (empty($fields) || isset($fields["file_size"])) {
+ $data["file_size"] = filesize($this->file_path());
+ }
+ if (access::user_can(identity::guest(), "view_full", $this)) {
+ if (empty($fields) || isset($fields["file_url_public"])) {
+ $data["file_url_public"] = $this->file_url(true);
+ }
+ }
}
}
if ($this->is_photo()) {
- $data["resize_url"] = rest::url("data", $this, "resize");
- $data["resize_size"] = filesize($this->resize_path());
+ if (empty($fields) || isset($fields["resize_url"])) {
+ $data["resize_url"] = rest::url("data", $this, "resize");
+ }
+ if (empty($fields) || isset($fields["resize_size"])) {
+ $data["resize_size"] = filesize($this->resize_path());
+ }
if (access::user_can(identity::guest(), "view", $this)) {
- $data["resize_url_public"] = $this->resize_url(true);
+ if (empty($fields) || isset($fields["resize_url_public"])) {
+ $data["resize_url_public"] = $this->resize_url(true);
+ }
}
}
if ($this->has_thumb()) {
- $data["thumb_url"] = rest::url("data", $this, "thumb");
- $data["thumb_size"] = filesize($this->thumb_path());
+ if (empty($fields) || isset($fields["thumb_url"])) {
+ $data["thumb_url"] = rest::url("data", $this, "thumb");
+ }
+ if (empty($fields) || isset($fields["thumb_size"])) {
+ $data["thumb_size"] = filesize($this->thumb_path());
+ }
if (access::user_can(identity::guest(), "view", $this)) {
- $data["thumb_url_public"] = $this->thumb_url(true);
+ if (empty($fields) || isset($fields["thumb_url_public"])) {
+ $data["thumb_url_public"] = $this->thumb_url(true);
+ }
}
}
- $data["can_edit"] = access::can("edit", $this);
+ if (empty($fields) || isset($fields["can_edit"])) {
+ $data["can_edit"] = access::can("edit", $this);
+ }
// Elide some internal-only data that is going to cause confusion in the client.
foreach (array("relative_path_cache", "relative_url_cache", "left_ptr", "right_ptr",
@@ -1031,6 +1011,16 @@ class Item_Model_Core extends ORM_MPTT {
return $data;
}
+ /**
+ * Increments the view counter of this item
+ * We can't use math in ORM or the query builder, so do this by hand. It's important
+ * that we do this with math, otherwise concurrent accesses will damage accuracy.
+ */
+ public function increment_view_count() {
+ db::query("UPDATE {items} SET `view_count` = `view_count` + 1 WHERE `id` = $this->id")
+ ->execute();
+ }
+
private function _cache_buster($path) {
return "?m=" . (string)(file_exists($path) ? filemtime($path) : 0);
}
diff --git a/modules/gallery/models/log.php b/modules/gallery/models/log.php
index 5d065313..a57649ff 100644
--- a/modules/gallery/models/log.php
+++ b/modules/gallery/models/log.php
@@ -1,7 +1,7 @@
<?php defined("SYSPATH") or die("No direct script access.");
/**
* Gallery - a web based photo album viewer and editor
- * Copyright (C) 2000-2010 Bharat Mediratta
+ * Copyright (C) 2000-2011 Bharat Mediratta
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
diff --git a/modules/gallery/models/message.php b/modules/gallery/models/message.php
index 3f84efd0..9a0b99f7 100644
--- a/modules/gallery/models/message.php
+++ b/modules/gallery/models/message.php
@@ -1,7 +1,7 @@
<?php defined("SYSPATH") or die("No direct script access.");
/**
* Gallery - a web based photo album viewer and editor
- * Copyright (C) 2000-2010 Bharat Mediratta
+ * Copyright (C) 2000-2011 Bharat Mediratta
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
diff --git a/modules/gallery/models/module.php b/modules/gallery/models/module.php
index e54613d1..ca251ff5 100644
--- a/modules/gallery/models/module.php
+++ b/modules/gallery/models/module.php
@@ -1,7 +1,7 @@
<?php defined("SYSPATH") or die("No direct script access.");
/**
* Gallery - a web based photo album viewer and editor
- * Copyright (C) 2000-2010 Bharat Mediratta
+ * Copyright (C) 2000-2011 Bharat Mediratta
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
diff --git a/modules/gallery/models/outgoing_translation.php b/modules/gallery/models/outgoing_translation.php
index cd580bed..7f90a2b0 100644
--- a/modules/gallery/models/outgoing_translation.php
+++ b/modules/gallery/models/outgoing_translation.php
@@ -1,7 +1,7 @@
<?php defined("SYSPATH") or die("No direct script access.");
/**
* Gallery - a web based photo album viewer and editor
- * Copyright (C) 2000-2010 Bharat Mediratta
+ * Copyright (C) 2000-2011 Bharat Mediratta
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
diff --git a/modules/gallery/models/permission.php b/modules/gallery/models/permission.php
index 51bd95e7..52e3131e 100644
--- a/modules/gallery/models/permission.php
+++ b/modules/gallery/models/permission.php
@@ -1,7 +1,7 @@
<?php defined("SYSPATH") or die("No direct script access.");
/**
* Gallery - a web based photo album viewer and editor
- * Copyright (C) 2000-2010 Bharat Mediratta
+ * Copyright (C) 2000-2011 Bharat Mediratta
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
diff --git a/modules/gallery/models/task.php b/modules/gallery/models/task.php
index 4d435101..2ee9075a 100644
--- a/modules/gallery/models/task.php
+++ b/modules/gallery/models/task.php
@@ -1,7 +1,7 @@
<?php defined("SYSPATH") or die("No direct script access.");
/**
* Gallery - a web based photo album viewer and editor
- * Copyright (C) 2000-2010 Bharat Mediratta
+ * Copyright (C) 2000-2011 Bharat Mediratta
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
diff --git a/modules/gallery/models/theme.php b/modules/gallery/models/theme.php
index 50fd04fd..ab2ba57f 100644
--- a/modules/gallery/models/theme.php
+++ b/modules/gallery/models/theme.php
@@ -1,7 +1,7 @@
<?php defined("SYSPATH") or die("No direct script access.");
/**
* Gallery - a web based photo album viewer and editor
- * Copyright (C) 2000-2010 Bharat Mediratta
+ * Copyright (C) 2000-2011 Bharat Mediratta
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
diff --git a/modules/gallery/models/var.php b/modules/gallery/models/var.php
index f21486ee..6e406ac7 100644
--- a/modules/gallery/models/var.php
+++ b/modules/gallery/models/var.php
@@ -1,7 +1,7 @@
<?php defined("SYSPATH") or die("No direct script access.");
/**
* Gallery - a web based photo album viewer and editor
- * Copyright (C) 2000-2010 Bharat Mediratta
+ * Copyright (C) 2000-2011 Bharat Mediratta
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by