/** * This method validates a plug name. It is much faster than calling clean_param. * * @param string $name a string that might be a plugin name. * @return bool if this string is a valid plugin name. */ function is_valid_plugin_name($name) { // This does not work for 'mod', bad luck, use any other type. return core_component::is_valid_plugin_name('tool', $name); }
// 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/>. /** * Test enrol plugin settings. * * @package core_enrol * @copyright 2013 Petr Skoda {@link http://skodak.org} * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ require __DIR__ . '../../config.php'; require_once "{$CFG->libdir}/adminlib.php"; $enrol = optional_param('enrol', '', PARAM_RAW); if (!core_component::is_valid_plugin_name('enrol', $enrol)) { $enrol = ''; } else { if (!file_exists("{$CFG->dirroot}/enrol/{$enrol}/lib.php")) { $enrol = ''; } } require_login(); require_capability('moodle/site:config', context_system::instance()); navigation_node::override_active_url(new moodle_url('/admin/settings.php', array('section' => 'manageenrols'))); admin_externalpage_setup('enroltestsettings'); $returnurl = new moodle_url('/admin/settings.php', array('section' => 'manageenrols')); echo $OUTPUT->header(); if (!$enrol) { $options = array(); $plugins = core_component::get_plugin_list('enrol');
/** * Returns an array of all the active instances of a particular module in given courses, sorted in the order they are defined * * Returns an array of all the active instances of a particular * module in given courses, sorted in the order they are defined * in the course. Returns an empty array on any errors. * * The returned objects includle the columns cw.section, cm.visible, * cm.groupmode, and cm.groupingid, and are indexed by cm.id. * * @global object * @global object * @param string $modulename The name of the module to get instances for * @param array $courses an array of course objects. * @param int $userid * @param int $includeinvisible * @return array of module instance objects, including some extra fields from the course_modules * and course_sections tables, or an empty array if an error occurred. */ function get_all_instances_in_courses($modulename, $courses, $userid = NULL, $includeinvisible = false) { global $CFG, $DB; if (!core_component::is_valid_plugin_name('mod', $modulename)) { throw new coding_exception('Invalid modulename parameter'); } $outputarray = array(); if (empty($courses) || !is_array($courses) || count($courses) == 0) { return $outputarray; } list($coursessql, $params) = $DB->get_in_or_equal(array_keys($courses), SQL_PARAMS_NAMED, 'c0'); $params['modulename'] = $modulename; if (!($rawmods = $DB->get_records_sql("SELECT cm.id AS coursemodule, m.*, cw.section, cm.visible AS visible,\n cm.groupmode, cm.groupingid\n FROM {course_modules} cm, {course_sections} cw, {modules} md,\n {" . $modulename . "} m\n WHERE cm.course {$coursessql} AND\n cm.instance = m.id AND\n cm.section = cw.id AND\n md.name = :modulename AND\n md.id = cm.module", $params))) { return $outputarray; } foreach ($courses as $course) { $modinfo = get_fast_modinfo($course, $userid); if (empty($modinfo->instances[$modulename])) { continue; } foreach ($modinfo->instances[$modulename] as $cm) { if (!$includeinvisible and !$cm->uservisible) { continue; } if (!isset($rawmods[$cm->id])) { continue; } $instance = $rawmods[$cm->id]; if (!empty($cm->extra)) { $instance->extra = $cm->extra; } $outputarray[] = $instance; } } return $outputarray; }
public function test_is_valid_plugin_name() { $this->assertTrue(core_component::is_valid_plugin_name('mod', 'example1')); $this->assertTrue(core_component::is_valid_plugin_name('mod', 'feedback360')); $this->assertFalse(core_component::is_valid_plugin_name('mod', 'feedback_360')); $this->assertFalse(core_component::is_valid_plugin_name('mod', '2feedback')); $this->assertFalse(core_component::is_valid_plugin_name('mod', '1example')); $this->assertFalse(core_component::is_valid_plugin_name('mod', 'example.xx')); $this->assertFalse(core_component::is_valid_plugin_name('mod', '.example')); $this->assertFalse(core_component::is_valid_plugin_name('mod', '_example')); $this->assertFalse(core_component::is_valid_plugin_name('mod', 'example_')); $this->assertFalse(core_component::is_valid_plugin_name('mod', 'example_x1')); $this->assertFalse(core_component::is_valid_plugin_name('mod', 'example-x1')); $this->assertFalse(core_component::is_valid_plugin_name('mod', 'role')); $this->assertTrue(core_component::is_valid_plugin_name('tool', 'example1')); $this->assertTrue(core_component::is_valid_plugin_name('tool', 'example_x1')); $this->assertTrue(core_component::is_valid_plugin_name('tool', 'example_x1_xxx')); $this->assertTrue(core_component::is_valid_plugin_name('tool', 'feedback360')); $this->assertTrue(core_component::is_valid_plugin_name('tool', 'feed_back360')); $this->assertTrue(core_component::is_valid_plugin_name('tool', 'role')); $this->assertFalse(core_component::is_valid_plugin_name('tool', '1example')); $this->assertFalse(core_component::is_valid_plugin_name('tool', 'example.xx')); $this->assertFalse(core_component::is_valid_plugin_name('tool', 'example-xx')); $this->assertFalse(core_component::is_valid_plugin_name('tool', '.example')); $this->assertFalse(core_component::is_valid_plugin_name('tool', '_example')); $this->assertFalse(core_component::is_valid_plugin_name('tool', 'example_')); $this->assertFalse(core_component::is_valid_plugin_name('tool', 'example__x1')); }
/** * Efficiently retrieves the $course (stdclass) and $cm (cm_info) objects, given * an instance id or record and module name. * * Usage: * list($course, $cm) = get_course_and_cm_from_instance($forum, 'forum'); * * Using this method has a performance advantage because it works by loading * modinfo for the course - which will then be cached and it is needed later * in most requests. It also guarantees that the $cm object is a cm_info and * not a stdclass. * * The $course object can be supplied if already known and will speed * up this function - although it is more efficient to use this function to * get the course if you are starting from an instance id. * * By default this obtains information (for example, whether user can access * the activity) for current user, but you can specify a userid if required. * * @param stdclass|int $instanceorid Id of module instance, or database object * @param string $modulename Modulename (required) * @param stdClass|int $courseorid Optional course object if already loaded * @param int $userid Optional userid (default = current) * @return array Array with 2 elements $course and $cm * @throws moodle_exception If the item doesn't exist or is of wrong module name */ function get_course_and_cm_from_instance($instanceorid, $modulename, $courseorid = 0, $userid = 0) { global $DB; // Get data from parameter. if (is_object($instanceorid)) { $instanceid = $instanceorid->id; if (isset($instanceorid->course)) { $courseid = (int) $instanceorid->course; } else { $courseid = 0; } } else { $instanceid = (int) $instanceorid; $courseid = 0; } // Get course from last parameter if supplied. $course = null; if (is_object($courseorid)) { $course = $courseorid; } else { if ($courseorid) { $courseid = (int) $courseorid; } } // Validate module name if supplied. if (!core_component::is_valid_plugin_name('mod', $modulename)) { throw new coding_exception('Invalid modulename parameter'); } if (!$course) { if ($courseid) { // If course ID is known, get it using normal function. $course = get_course($courseid); } else { // Get course record in a single query based on instance id. $pagetable = '{' . $modulename . '}'; $course = $DB->get_record_sql("\n SELECT c.*\n FROM {$pagetable} instance\n JOIN {course} c ON c.id = instance.course\n WHERE instance.id = ?", array($instanceid), MUST_EXIST); } } // Get cm from get_fast_modinfo. $modinfo = get_fast_modinfo($course, $userid); $instances = $modinfo->get_instances_of($modulename); if (!array_key_exists($instanceid, $instances)) { throw new moodle_exception('invalidmoduleid', 'error', $instanceid); } return array($course, $instances[$instanceid]); }
// 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/>. /** * Test auth settings. * * @package core_auth * @copyright 2013 Petr Skoda {@link http://skodak.org} * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ require __DIR__ . '../../config.php'; require_once "{$CFG->libdir}/adminlib.php"; $auth = optional_param('auth', '', PARAM_RAW); if (!core_component::is_valid_plugin_name('auth', $auth)) { $auth = ''; } else { if (!file_exists("{$CFG->dirroot}/auth/{$auth}/auth.php")) { $auth = ''; } } require_login(); require_capability('moodle/site:config', context_system::instance()); navigation_node::override_active_url(new moodle_url('/admin/settings.php', array('section' => 'manageauths'))); admin_externalpage_setup('authtestsettings'); $returnurl = new moodle_url('/admin/settings.php', array('section' => 'manageauths')); echo $OUTPUT->header(); if (!$auth) { $options = array(); $plugins = core_component::get_plugin_list('auth');
/** * Decode the request from the Moodle Plugins directory * * @param string $request submitted via 'installaddonrequest' HTTP parameter * @return stdClass|bool false on error, object otherwise */ protected function decode_remote_request($request) { $data = base64_decode($request, true); if ($data === false) { return false; } $data = json_decode($data); if (is_null($data)) { return false; } if (!isset($data->name) or !isset($data->component) or !isset($data->version)) { return false; } $data->name = s(strip_tags($data->name)); if ($data->component !== clean_param($data->component, PARAM_COMPONENT)) { return false; } list($plugintype, $pluginname) = core_component::normalize_component($data->component); if ($plugintype === 'core') { return false; } if ($data->component !== $plugintype . '_' . $pluginname) { return false; } if (!core_component::is_valid_plugin_name($plugintype, $pluginname)) { return false; } $plugintypes = core_component::get_plugin_types(); if (!isset($plugintypes[$plugintype])) { return false; } // Keep this regex in sync with the one used by the download.moodle.org/api/x.y/pluginfo.php if (!preg_match('/^[0-9]+$/', $data->version)) { return false; } return $data; }