moodle /lib/classes/message/inbound/handler.php

Language PHP Lines 307
MD5 Hash ee2ad4ea97f7b6b3e62a537bceb70a05 Estimated Cost $2,661 (why?)
Repository https://bitbucket.org/moodle/moodle.git View Raw File View Project SPDX
  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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle 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 3 of the License, or
// (at your option) any later version.
//
// Moodle 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 Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * Abstract class describing Inbound Message Handlers.
 *
 * @package    core_message
 * @copyright  2014 Andrew Nicols
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
namespace core\message\inbound;

/**
 * Abstract class describing Inbound Message Handlers.
 *
 * @copyright  2014 Andrew NIcols
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 *
 * @property-read int $id The ID of the handler in the database
 * @property-read string $component The component of this handler
 * @property-read int $defaultexpiration Default expiration of new addresses for this handler
 * @property-read string $description The description of this handler
 * @property-read string $name The name of this handler
 * @property-read bool $validateaddress Whether the address validation is a requiredment
 * @property-read bool $enabled Whether this handler is currently enabled
 * @property-read string $classname The name of handler class
 */
abstract class handler {

    /**
     * @var int $id The id of the handler in the database.
     */
    private $id = null;

    /**
     * @var string $component The component to which this handler belongs.
     */
    private $component = '';

    /**
     * @var int $defaultexpiration The default expiration time to use when created a new key.
     */
    private $defaultexpiration = WEEKSECS;

    /**
     * @var bool $validateaddress Whether to validate the sender address when processing this handler.
     */
    private $validateaddress = true;

    /**
     * @var bool $enabled Whether this handler is currently enabled.
     */
    private $enabled = false;

    /**
     * @var $accessibleproperties A list of the properties which can be read.
     */
    private $accessibleproperties = array(
        'id' => true,
        'component' => true,
        'defaultexpiration' => true,
        'validateaddress' => true,
        'enabled' => true,
    );

    /**
     * Magic getter to fetch the specified key.
     *
     * @param string $key The name of the key to retrieve
     */
    public function __get($key) {
        // Some properties have logic behind them.
        $getter = 'get_' . $key;
        if (method_exists($this, $getter)) {
            return $this->$getter();
        }

        // Check for a commonly accessibly property.
        if (isset($this->accessibleproperties[$key])) {
            return $this->$key;
        }

        // Unknown property - bail.
        throw new \coding_exception('unknown_property ' . $key);
    }

    /**
     * Set the id name.
     *
     * @param int $id The id to set
     * @return int The newly set id
     */
    public function set_id($id) {
        return $this->id = $id;
    }

    /**
     * Set the component name.
     *
     * @param string $component The component to set
     * @return string The newly set component
     */
    public function set_component($component) {
        return $this->component = $component;
    }

    /**
     * Whether the current handler allows changes to the address validation
     * setting.
     *
     * By default this will return true, but for some handlers it may be
     * necessary to disallow such changes.
     *
     * @return boolean
     */
    public function can_change_validateaddress() {
        return true;
    }

    /**
     * Set whether validation of the address is required.
     *
     * @param bool $validateaddress The new state of validateaddress
     * @return bool
     */
    public function set_validateaddress($validateaddress) {
        return $this->validateaddress = $validateaddress;
    }

    /**
     * Whether the current handler allows changes to expiry of the generated email address.
     *
     * By default this will return true, but for some handlers it may be
     * necessary to disallow such changes.
     *
     * @return boolean
     */
    public function can_change_defaultexpiration() {
        return true;
    }

    /**
     * Whether this handler can be disabled (or enabled).
     *
     * By default this will return true, but for some handlers it may be
     * necessary to disallow such changes. For example, a core handler to
     * handle rejected mail validation should not be disabled.
     *
     * @return boolean
     */
    public function can_change_enabled() {
        return true;
    }

    /**
     * Set the enabled name.
     *
     * @param bool $enabled The new state of enabled
     * @return bool
     */
    public function set_enabled($enabled) {
        return $this->enabled = $enabled;
    }

    /**
     * Set the default validity for new keys.
     *
     * @param int $period The time in seconds before a key expires
     * @return int
     */
    public function set_defaultexpiration($period) {
        return $this->defaultexpiration = $period;
    }

    /**
     * Get the non-namespaced name of the current class.
     *
     * @return string The classname
     */
    private function get_classname() {
        $classname = get_class($this);
        if (strpos($classname, '\\') !== 0) {
            $classname = '\\' . $classname;
        }

        return $classname;
    }

    /**
     * Return a description for the current handler.
     *
     * @return string
     */
    protected abstract function get_description();

    /**
     * Return a name for the current handler.
     * This appears in the admin pages as a human-readable name.
     *
     * @return string
     */
    protected abstract function get_name();

    /**
     * Process the message against the current handler.
     *
     * @param \stdClass $record The Inbound Message Handler record
     * @param \stdClass $messagedata The message data
     */
    public abstract function process_message(\stdClass $record, \stdClass $messagedata);

    /**
     * Return the content of any success notification to be sent.
     * Both an HTML and Plain Text variant must be provided.
     *
     * If this handler does not need to send a success notification, then
     * it should return a falsey value.
     *
     * @param \stdClass $messagedata The message data.
     * @param \stdClass $handlerresult The record for the newly created post.
     * @return \stdClass with keys `html` and `plain`.
     */
    public function get_success_message(\stdClass $messagedata, $handlerresult) {
        return false;
    }

    /**
     * Remove quoted message string from the text (NOT HTML) message.
     *
     * @param \stdClass $messagedata The Inbound Message record
     *
     * @return array message and message format to use.
     */
    protected static function remove_quoted_text($messagedata) {
        if (!empty($messagedata->plain)) {
            $text = $messagedata->plain;
        } else {
            $text = html_to_text($messagedata->html);
        }
        $messageformat = FORMAT_PLAIN;

        $splitted = preg_split("/\n|\r/", $text);
        if (empty($splitted)) {
            return array($text, $messageformat);
        }

        $i = 0;
        $flag = false;
        foreach ($splitted as $i => $element) {
            if (stripos($element, ">") === 0) {
                // Quoted text found.
                $flag = true;
                // Remove 2 non empty line before this.
                for ($j = $i - 1; ($j >= 0); $j--) {
                    $element = $splitted[$j];
                    if (!empty($element)) {
                        unset($splitted[$j]);
                        break;
                    }
                }
                break;
            }
        }
        if ($flag) {
            // Quoted text was found.
            // Retrieve everything from the start until the line before the quoted text.
            $splitted = array_slice($splitted, 0, $i-1);

            // Strip out empty lines towards the end, since a lot of clients add a huge chunk of empty lines.
            $reverse = array_reverse($splitted);
            foreach ($reverse as $i => $line) {
                if (empty($line)) {
                    unset($reverse[$i]);
                } else {
                    // Non empty line found.
                    break;
                }
            }

            $replaced = implode(PHP_EOL, array_reverse($reverse));
            $message = trim($replaced);
        } else {
            // No quoted text, fallback to original text.
            if (!empty($messagedata->html)) {
                $message = $messagedata->html;
                $messageformat = FORMAT_HTML;
            } else {
                $message = $messagedata->plain;
            }
        }
        return array($message, $messageformat);
    }
}
Back to Top