[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
1 <?php 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 * Variable Envelope Return Path management. 19 * 20 * @package core_message 21 * @copyright 2014 Andrew Nicols <andrew@nicols.co.uk> 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 namespace core\message\inbound; 26 27 defined('MOODLE_INTERNAL') || die(); 28 29 /** 30 * Variable Envelope Return Path manager class. 31 * 32 * @copyright 2014 Andrew Nicols <andrew@nicols.co.uk> 33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 34 */ 35 class manager { 36 37 /** 38 * Whether the Inbound Message interface is enabled. 39 * 40 * @return bool 41 */ 42 public static function is_enabled() { 43 global $CFG; 44 45 // Check whether Inbound Message is enabled at all. 46 if (!isset($CFG->messageinbound_enabled) || !$CFG->messageinbound_enabled) { 47 return false; 48 } 49 50 // Check whether the outgoing mailbox and domain are configured properly. 51 if (!isset($CFG->messageinbound_mailbox) || empty($CFG->messageinbound_mailbox)) { 52 return false; 53 } 54 55 if (!isset($CFG->messageinbound_domain) || empty($CFG->messageinbound_domain)) { 56 return false; 57 } 58 59 return true; 60 } 61 62 /** 63 * Update the database to create, update, and remove handlers. 64 * 65 * @param string $componentname - The frankenstyle component name. 66 */ 67 public static function update_handlers_for_component($componentname) { 68 global $DB; 69 70 $componentname = \core_component::normalize_componentname($componentname); 71 $existinghandlers = $DB->get_recordset('messageinbound_handlers', array('component' => $componentname)); 72 foreach ($existinghandlers as $handler) { 73 if (!class_exists($handler->classname)) { 74 self::remove_messageinbound_handler($handler); 75 } 76 } 77 78 self::create_missing_messageinbound_handlers_for_component($componentname); 79 } 80 81 /** 82 * Load handler instances for all of the handlers defined in db/messageinbound_handlers.php for the specified component. 83 * 84 * @param string $componentname - The name of the component to fetch the handlers for. 85 * @return \core\message\inbound\handler[] - List of handlers for this component. 86 */ 87 public static function load_default_handlers_for_component($componentname) { 88 $componentname = \core_component::normalize_componentname($componentname); 89 $dir = \core_component::get_component_directory($componentname); 90 91 if (!$dir) { 92 return array(); 93 } 94 95 $file = $dir . '/db/messageinbound_handlers.php'; 96 if (!file_exists($file)) { 97 return array(); 98 } 99 100 $handlers = null; 101 require_once($file); 102 103 if (!isset($handlers)) { 104 return array(); 105 } 106 107 $handlerinstances = array(); 108 109 foreach ($handlers as $handler) { 110 $record = (object) $handler; 111 $record->component = $componentname; 112 if ($handlerinstance = self::handler_from_record($record)) { 113 $handlerinstances[] = $handlerinstance; 114 } else { 115 throw new \coding_exception("Inbound Message Handler not found for '{$componentname}'."); 116 } 117 } 118 119 return $handlerinstances; 120 } 121 122 /** 123 * Update the database to contain a list of handlers for a component, 124 * adding any handlers which do not exist in the database. 125 * 126 * @param string $componentname - The frankenstyle component name. 127 */ 128 public static function create_missing_messageinbound_handlers_for_component($componentname) { 129 global $DB; 130 $componentname = \core_component::normalize_componentname($componentname); 131 132 $expectedhandlers = self::load_default_handlers_for_component($componentname); 133 foreach ($expectedhandlers as $handler) { 134 $recordexists = $DB->record_exists('messageinbound_handlers', array( 135 'component' => $componentname, 136 'classname' => $handler->classname, 137 )); 138 139 if (!$recordexists) { 140 $record = self::record_from_handler($handler); 141 $record->component = $componentname; 142 $DB->insert_record('messageinbound_handlers', $record); 143 } 144 } 145 } 146 147 /** 148 * Remove the specified handler. 149 * 150 * @param \core\message\inbound\handler $handler The handler to remove 151 */ 152 public static function remove_messageinbound_handler($handler) { 153 global $DB; 154 155 // Delete Inbound Message datakeys. 156 $DB->delete_records('messageinbound_datakeys', array('handler' => $handler->id)); 157 158 // Delete Inbound Message handlers. 159 $DB->delete_records('messageinbound_handlers', array('id' => $handler->id)); 160 } 161 162 /** 163 * Create a flat stdClass for the handler, appropriate for inserting 164 * into the database. 165 * 166 * @param \core\message\inbound\handler $handler The handler to retrieve the record for. 167 * @return \stdClass 168 */ 169 public static function record_from_handler($handler) { 170 $record = new \stdClass(); 171 $record->id = $handler->id; 172 $record->component = $handler->component; 173 $record->classname = get_class($handler); 174 if (strpos($record->classname, '\\') !== 0) { 175 $record->classname = '\\' . $record->classname; 176 } 177 $record->defaultexpiration = $handler->defaultexpiration; 178 $record->validateaddress = $handler->validateaddress; 179 $record->enabled = $handler->enabled; 180 181 return $record; 182 } 183 184 /** 185 * Load the Inbound Message handler details for a given record. 186 * 187 * @param \stdClass $record The record to retrieve the handler for. 188 * @return \core\message\inbound\handler or false 189 */ 190 protected static function handler_from_record($record) { 191 $classname = $record->classname; 192 if (strpos($classname, '\\') !== 0) { 193 $classname = '\\' . $classname; 194 } 195 if (!class_exists($classname)) { 196 return false; 197 } 198 199 $handler = new $classname; 200 if (isset($record->id)) { 201 $handler->set_id($record->id); 202 } 203 $handler->set_component($record->component); 204 205 // Overload fields which can be modified. 206 if (isset($record->defaultexpiration)) { 207 $handler->set_defaultexpiration($record->defaultexpiration); 208 } 209 210 if (isset($record->validateaddress)) { 211 $handler->set_validateaddress($record->validateaddress); 212 } 213 214 if (isset($record->enabled)) { 215 $handler->set_enabled($record->enabled); 216 } 217 218 return $handler; 219 } 220 221 /** 222 * Load the Inbound Message handler details for a given classname. 223 * 224 * @param string $classname The name of the class for the handler. 225 * @return \core\message\inbound\handler or false 226 */ 227 public static function get_handler($classname) { 228 global $DB; 229 230 if (strpos($classname, '\\') !== 0) { 231 $classname = '\\' . $classname; 232 } 233 234 $record = $DB->get_record('messageinbound_handlers', array('classname' => $classname), '*', IGNORE_MISSING); 235 if (!$record) { 236 return false; 237 } 238 return self::handler_from_record($record); 239 } 240 241 /** 242 * Load the Inbound Message handler with a given ID 243 * 244 * @param int $id 245 * @return \core\message\inbound\handler or false 246 */ 247 public static function get_handler_from_id($id) { 248 global $DB; 249 250 $record = $DB->get_record('messageinbound_handlers', array('id' => $id), '*', IGNORE_MISSING); 251 if (!$record) { 252 return false; 253 } 254 return self::handler_from_record($record); 255 } 256 257 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |