MDL-55252 mod_chat: prevent direct script access to lib files.
[moodle.git] / mod / chat / locallib.php
CommitLineData
24ba58ee 1<?php
24ba58ee
PL
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Library of functions for chat outside of the core api
19 */
20
530d5535
SB
21defined('MOODLE_INTERNAL') || die();
22
e80451e8 23require_once($CFG->dirroot . '/mod/chat/lib.php');
24ba58ee
PL
24require_once($CFG->libdir . '/portfolio/caller.php');
25
26/**
a6be3193 27 * @package mod_chat
24ba58ee
PL
28 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
29 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30 */
31class chat_portfolio_caller extends portfolio_module_caller_base {
32 /** @var object */
33 private $chat;
34 /** @var int Timestamp */
35 protected $start;
36 /** @var int Timestamp */
37 protected $end;
38 /**
39 * @return array
40 */
41 public static function expected_callbackargs() {
42 return array(
43 'id' => true,
44 'start' => false,
45 'end' => false,
46 );
47 }
48 /**
49 * @global object
50 */
51 public function load_data() {
52 global $DB;
53
54 if (!$this->cm = get_coursemodule_from_id('chat', $this->id)) {
55 throw new portfolio_caller_exception('invalidid', 'chat');
56 }
57 $this->chat = $DB->get_record('chat', array('id' => $this->cm->instance));
58 $select = 'chatid = ?';
59 $params = array($this->chat->id);
60 if ($this->start && $this->end) {
61 $select .= ' AND timestamp >= ? AND timestamp <= ?';
62 $params[] = $this->start;
63 $params[] = $this->end;
64 }
65 $this->messages = $DB->get_records_select(
66 'chat_messages',
67 $select,
68 $params,
69 'timestamp ASC'
70 );
71 $select .= ' AND userid = ?';
72 $params[] = $this->user->id;
73 $this->participated = $DB->record_exists_select(
74 'chat_messages',
75 $select,
76 $params
77 );
78 }
79 /**
80 * @return array
81 */
82 public static function base_supported_formats() {
83 return array(PORTFOLIO_FORMAT_PLAINHTML);
84 }
85 /**
86 *
87 */
88 public function expected_time() {
89 return portfolio_expected_time_db(count($this->messages));
90 }
91 /**
92 * @return string
93 */
94 public function get_sha1() {
95 $str = '';
96 ksort($this->messages);
97 foreach ($this->messages as $m) {
98 $str .= implode('', (array)$m);
99 }
100 return sha1($str);
101 }
102
103 /**
104 * @return bool
105 */
106 public function check_permissions() {
6536217c 107 $context = context_module::instance($this->cm->id);
24ba58ee
PL
108 return has_capability('mod/chat:exportsession', $context)
109 || ($this->participated
110 && has_capability('mod/chat:exportparticipatedsession', $context));
111 }
112
113 /**
114 * @todo Document this function
115 */
116 public function prepare_package() {
117 $content = '';
118 $lasttime = 0;
119 $sessiongap = 5 * 60; // 5 minutes silence means a new session
120 foreach ($this->messages as $message) { // We are walking FORWARDS through messages
121 $m = clone $message; // grrrrrr - this causes the sha1 to change as chat_format_message changes what it's passed.
e80451e8 122 $formatmessage = chat_format_message($m, $this->cm->course, $this->user);
24ba58ee
PL
123 if (!isset($formatmessage->html)) {
124 continue;
125 }
126 if (empty($lasttime) || (($message->timestamp - $lasttime) > $sessiongap)) {
127 $content .= '<hr />';
128 $content .= userdate($message->timestamp);
129 }
130 $content .= $formatmessage->html;
131 $lasttime = $message->timestamp;
132 }
133 $content = preg_replace('/\<img[^>]*\>/', '', $content);
134
135 $this->exporter->write_new_file($content, clean_filename($this->cm->name . '-session.html'), false);
136 }
137
138 /**
139 * @return string
140 */
141 public static function display_name() {
142 return get_string('modulename', 'chat');
143 }
144
145 /**
146 * @global object
147 * @return string
148 */
149 public function get_return_url() {
150 global $CFG;
151
152 return $CFG->wwwroot . '/mod/chat/report.php?id='
c52b4db5 153 . $this->cm->id . ((isset($this->start)) ? '&start=' . $this->start . '&end=' . $this->end : '');
24ba58ee
PL
154 }
155}
a2592fec
AD
156
157/**
158 * A chat event such a user entering or leaving a chat activity
159 *
160 * @package mod_chat
161 * @copyright 2012 Andrew Davis
162 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
163 */
164class event_message implements renderable {
165
166 /** @var string The URL of the profile of the user who caused the event */
167 public $senderprofile;
168
169 /** @var string The ready to display name of the user who caused the event */
170 public $sendername;
171
172 /** @var string Ready to display event time */
173 public $time;
174
175 /** @var string Event description */
176 public $event;
177
178 /** @var string The chat theme name */
179 public $theme;
180
181 /**
182 * event_message constructor
183 *
184 * @param string $senderprofile The URL of the profile of the user who caused the event
185 * @param string $sendername The ready to display name of the user who caused the event
186 * @param string $time Ready to display event time
187 * @param string $theme The chat theme name
188 */
c52b4db5 189 public function __construct($senderprofile, $sendername, $time, $event, $theme) {
a2592fec
AD
190
191 $this->senderprofile = $senderprofile;
192 $this->sendername = $sendername;
193 $this->time = $time;
194 $this->event = $event;
195 $this->theme = $theme;
196 }
197}
198
199/**
200 * A chat message from a user
201 *
202 * @package mod_chat
203 * @copyright 2012 Andrew Davis
204 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
205 */
206class user_message implements renderable {
207
208 /** @var string The URL of the profile of the user sending the message */
209 public $senderprofile;
210
211 /** @var string The ready to display name of the user sending the message */
212 public $sendername;
213
214 /** @var string HTML for the avatar of the user sending the message */
215 public $avatar;
216
217 /** @var string Empty or a html class definition to append to the html */
218 public $mymessageclass;
219
220 /** @var string Ready to display message time */
221 public $time;
222
223 /** @var string The message */
224 public $message;
225
226 /** @var string The name of the chat theme to use */
227 public $theme;
228
229 /**
230 * user_message constructor
231 *
232 * @param string $senderprofile The URL of the profile of the user sending the message
233 * @param string $sendername The ready to display name of the user sending the message
234 * @param string $avatar HTML for the avatar of the user sending the message
235 * @param string $mymessageclass Empty or a html class definition to append to the html
236 * @param string $time Ready to display message time
237 * @param string $message The message
238 * @param string $theme The name of the chat theme to use
239 */
c52b4db5 240 public function __construct($senderprofile, $sendername, $avatar, $mymessageclass, $time, $message, $theme) {
a2592fec 241
bf5a27ce 242 $this->senderprofile = $senderprofile;
a2592fec
AD
243 $this->sendername = $sendername;
244 $this->avatar = $avatar;
245 $this->mymessageclass = $mymessageclass;
246 $this->time = $time;
247 $this->message = $message;
248 $this->theme = $theme;
249 }
250}