diff options
Diffstat (limited to 'modules/gallery/models/item.php')
-rw-r--r-- | modules/gallery/models/item.php | 97 |
1 files changed, 72 insertions, 25 deletions
diff --git a/modules/gallery/models/item.php b/modules/gallery/models/item.php index 9016a04a..fc5c3ff9 100644 --- a/modules/gallery/models/item.php +++ b/modules/gallery/models/item.php @@ -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 { @@ -848,10 +848,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 +987,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", |