summaryrefslogtreecommitdiff
path: root/modules/comment/models/comment.php
blob: d9d059956d9b926a93f565547039b49627161e49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php defined("SYSPATH") or die("No direct script access.");
/**
 * Gallery - a web based photo album viewer and editor
 * Copyright (C) 2000-2009 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
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
 */
class Comment_Model extends ORM {
  function item() {
    return ORM::factory("item", $this->item_id);
  }

  function author() {
    return identity::lookup_user($this->author_id);
  }

  function author_name() {
    $author = $this->author();
    if ($author->guest) {
      return $this->guest_name;
    } else {
      return $author->display_name();
    }
  }

  function author_email() {
    $author = $this->author();
    if ($author->guest) {
      return $this->guest_email;
    } else {
      return $author->email;
    }
  }

  function author_url() {
    $author = $this->author();
    if ($author->guest) {
      return $this->guest_url;
    } else {
      return $author->url;
    }
  }

  /**
   * Add some custom per-instance rules.
   */
  public function validate(Validation $array=null) {
    // validate() is recursive, only modify the rules on the outermost call.
    if (!$array) {
      $this->rules = array(
        "guest_name"  => array("callbacks" => array(array($this, "valid_author"))),
        "guest_email" => array("rules"     => array("email")),
        "guest_url"   => array("rules"     => array("url")),
        "item_id"     => array("callbacks" => array(array($this, "valid_item"))),
        "state"       => array("rules"     => array("Comment_Model::valid_state")),
        "text"        => array("rules"     => array("required")),
      );
    }

    parent::validate($array);
  }

  /**
   * @see ORM::save()
   */
  public function save() {
    $this->updated = time();
    if (!$this->loaded()) {
      // New comment
      $this->created = $this->updated;
      if (empty($this->state)) {
        $this->state = "published";
      }

      // These values are useful for spam fighting, so save them with the comment.  It's painful to
      // check each one to see if it already exists before setting it, so just use server_http_host
      // as a semaphore for now (we use that in g2_import.php)
      if (empty($this->server_http_host)) {
        $input = Input::instance();
        $this->server_http_accept = substr($input->server("HTTP_ACCEPT"), 0, 128);
        $this->server_http_accept_charset = substr($input->server("HTTP_ACCEPT_CHARSET"), 0, 64);
        $this->server_http_accept_encoding = substr($input->server("HTTP_ACCEPT_ENCODING"), 0, 64);
        $this->server_http_accept_language = substr($input->server("HTTP_ACCEPT_LANGUAGE"), 0, 64);
        $this->server_http_connection = substr($input->server("HTTP_CONNECTION"), 0, 64);
        $this->server_http_host = substr($input->server("HTTP_HOST"), 0, 64);
        $this->server_http_referer = substr($input->server("HTTP_REFERER"), 0, 255);
        $this->server_http_user_agent = substr($input->server("HTTP_USER_AGENT"), 0, 128);
        $this->server_query_string = substr($input->server("QUERY_STRING"), 0, 64);
        $this->server_remote_addr = substr($input->server("REMOTE_ADDR"), 0, 32);
        $this->server_remote_host = substr($input->server("REMOTE_HOST"), 0, 64);
        $this->server_remote_port = substr($input->server("REMOTE_PORT"), 0, 16);
      }

      $visible_change = $this->state == "published";
      parent::save();
      module::event("comment_created", $this);
    } else {
      // Updated comment
      $original = ORM::factory("comment", $this->id);
      $visible_change = $original->state == "published" || $this->state == "published";
      parent::save();
      module::event("comment_updated", $original, $this);
    }

    // We only notify on the related items if we're making a visible change.
    if ($visible_change) {
      $item = $this->item();
      module::event("item_related_update", $item);
    }

    return $this;
  }

  /**
   * Add a set of restrictions to any following queries to restrict access only to items
   * viewable by the active user.
   * @chainable
   */
  public function viewable() {
    $this->join("items", "items.id", "comments.item_id");
    return item::viewable($this);
  }

  /**
   * Make sure we have an appropriate author id set, or a guest name.
   */
  public function valid_author(Validation $v, $field) {
    if (empty($this->author_id)) {
      $v->add_error("author_id", "required");
    } else if ($this->author_id == identity::guest()->id && empty($this->guest_name)) {
      $v->add_error("guest_name", "required");
    }
  }

  /**
   * Make sure we have a valid associated item id.
   */
  public function valid_item(Validation $v, $field) {
    if (db::build()
        ->from("items")
        ->where("id", "=", $this->item_id)
        ->count_records() != 1) {
      $v->add_error("item_id", "invalid");
    }
  }

  /**
   * Make sure that the state is legal.
   */
  static function valid_state($value) {
    return in_array($value, array("published", "unpublished", "spam", "deleted"));
  }
}