/** * Create a test institution * @param array $record * @throws ErrorException if creating failed * @return int new institution id */ public function create_institution($record) { // Data validation if (empty($record['name']) || !preg_match('/^[a-zA-Z]{1,255}$/', $record['name'])) { throw new SystemException("Invalid institution name '" . $record['name'] . "'. The institution name is entered for system database identification only and must be a single text word without numbers or symbols."); } if (!empty($record['name']) && record_exists('institution', 'name', $record['name'])) { throw new SystemException("Invalid institution name '" . $record['name'] . "'. " . get_string('institutionnamealreadytaken', 'admin')); } if (get_config('licensemetadata') && !empty($record['licensemandatory']) && (isset($record['licensedefault']) && $record['licensedefault'] == '')) { throw new SystemException("Invalid institution license setting. " . get_string('licensedefaultmandatory', 'admin')); } if (!empty($record['lang']) && $record['lang'] != 'sitedefault' && !array_key_exists($record['lang'], get_languages())) { throw new SystemException("Invalid institution language setting: '" . $record['lang'] . "'. This language is not installed for the site."); } // Create a new institution db_begin(); // Update the basic institution record... $newinstitution = new Institution(); $newinstitution->initialise($record['name'], $record['displayname']); $institution = $newinstitution->name; $newinstitution->showonlineusers = !isset($record['showonlineusers']) ? 2 : $record['showonlineusers']; if (get_config('usersuniquebyusername')) { // Registering absolutely not allowed when this setting is on, it's a // security risk. See the documentation for the usersuniquebyusername // setting for more information $newinstitution->registerallowed = 0; } else { $newinstitution->registerallowed = !empty($record['registerallowed']) ? 1 : 0; $newinstitution->registerconfirm = !empty($record['registerconfirm']) ? 1 : 0; } if (!empty($record['lang'])) { if ($record['lang'] == 'sitedefault') { $newinstitution->lang = null; } else { $newinstitution->lang = $record['lang']; } } $newinstitution->theme = empty($record['theme']) || $record['theme'] == 'sitedefault' ? null : $record['theme']; $newinstitution->dropdownmenu = !empty($record['dropdownmenu']) ? 1 : 0; $newinstitution->skins = !empty($record['skins']) ? 1 : 0; $newinstitution->style = null; if (get_config('licensemetadata')) { $newinstitution->licensemandatory = !empty($record['licensemandatory']) ? 1 : 0; $newinstitution->licensedefault = isset($record['licensedefault']) ? $record['licensedefault'] : ''; } $newinstitution->defaultquota = empty($record['defaultquota']) ? get_config_plugin('artefact', 'file', 'defaultquota') : $record['defaultquota']; $newinstitution->defaultmembershipperiod = !empty($record['defaultmembershipperiod']) ? intval($record['defaultmembershipperiod']) : null; $newinstitution->maxuseraccounts = !empty($record['maxuseraccounts']) ? intval($record['maxuseraccounts']) : null; $newinstitution->expiry = !empty($record['expiry']) ? db_format_timestamp($record['expiry']) : null; $newinstitution->allowinstitutionpublicviews = isset($record['allowinstitutionpublicviews']) && $record['allowinstitutionpublicviews'] ? 1 : 0; // Save the changes to the DB $newinstitution->commit(); // Automatically create an internal authentication authinstance $authinstance = (object) array('instancename' => 'internal', 'priority' => 0, 'institution' => $newinstitution->name, 'authname' => 'internal'); insert_record('auth_instance', $authinstance); // We need to add the default lines to the site_content table for this institution // We also need to set the institution to be using default static pages to begin with // so that using custom institution pages is an opt-in situation $pages = site_content_pages(); $now = db_format_timestamp(time()); foreach ($pages as $name) { $page = new stdClass(); $page->name = $name; $page->ctime = $now; $page->mtime = $now; $page->content = get_string($page->name . 'defaultcontent', 'install', get_string('staticpageconfiginstitution', 'install')); $page->institution = $newinstitution->name; insert_record('site_content', $page); $institutionconfig = new stdClass(); $institutionconfig->institution = $newinstitution->name; $institutionconfig->field = 'sitepages_' . $name; $institutionconfig->value = 'mahara'; insert_record('institution_config', $institutionconfig); } if (isset($record['commentthreaded'])) { set_config_institution($newinstitution->name, 'commentthreaded', (bool) $record['commentthreaded']); } db_commit(); }
/** * Set or update an institution config value. * * @param string $institutionname The institution name * @param string $key The config name * @param string $value The config's new value * @return boolean Whether or not the config was updated successfully */ function set_config_institution($institutionname, $key, $value) { global $CFG; if (isset($CFG->fetchedinst->{$institutionname})) { $inst = $CFG->fetchedinst->{$institutionname}; } else { // No cache hit, so instatiate a new Institution object try { $inst = new Institution($institutionname); } catch (ParamOutOfRangeException $e) { return null; } } if (isset($inst)) { $inst->{$key} = $value; $inst->commit(); return true; } return false; }
public function bootstrap($wwwroot, $pubkey, $appname = 'moodle', $institution = null) { $wwwroot = dropslash($wwwroot); if (!$this->findByWwwroot($wwwroot)) { $hostname = get_hostname_from_uri($wwwroot); // Get the IP address for that host - if this fails, it will // return the hostname string $ipaddress = gethostbyname($hostname); // Couldn't find the IP address? if ($ipaddress === $hostname && !preg_match('/^\\d+\\.\\d+\\.\\d+.\\d+$/', $hostname)) { throw new ParamOutOfRangeException('Could not find IP address for host: ' . addslashes($hostname)); return false; } // Default the name to the wwwroot $this->name = $wwwroot; // Get a page from the remote host, and check its title. $homepage = file_get_contents($wwwroot); if (!empty($homepage) && ($count = preg_match("@<title>(.*)</title>@siU", $homepage, $matches))) { $this->name = $matches[1]; } $exists = get_record('application', 'name', $appname); if (empty($exists)) { throw new ParamOutOfRangeException('Application ' . addslashes($appname) . ' does not exist.'); } $this->appname = $appname; $this->application = Application::findByName($this->appname); $this->wwwroot = $wwwroot; $this->ipaddress = $ipaddress; require_once get_config('libroot') . 'institution.php'; if (null == $institution) { $institution = new Institution(); $institution->name = preg_replace('/[^a-zA-Z]/', '', $this->name); // Check that the institution name has not already been taken. // If it has, we change it until we find a name that works $existinginstitutionnames = get_column('institution', 'name'); if (in_array($institution->name, $existinginstitutionnames)) { $success = false; foreach (range('a', 'z') as $character) { $testname = $institution->name . $character; if (!in_array($testname, $existinginstitutionnames)) { $success = true; $institution->name = $testname; break; } } if (!$success) { // We couldn't find a unique name. Noes! throw new RemoteServerException('Could not create a unique institution name'); } } $institution->displayname = $this->name; $institution->commit(); $this->institution = $institution->name; } else { $this->institution = $institution; } if (empty($pubkey)) { try { $somekey = get_public_key($this->wwwroot, $this->appname); $this->publickey = new PublicKey($somekey, $this->wwwroot); } catch (XmlrpcClientException $e) { $errcode = $e->getCode(); if ($errcode == 404) { throw new RemoteServerException('404: Incorrect WWWRoot or Application: file not found.'); } elseif ($errcode == 704) { throw new RemoteServerException('Networking is disabled on the host at ' . $this->wwwroot . '.'); } else { throw new RemoteServerException('Error retrieving public key, failed with error code ' . $errcode . ': ' . $e->getMessage()); } } catch (Exception $e) { throw new RemoteServerException('Error retrieving public key: ' . $e->getMessage()); } } else { $this->publickey = new PublicKey($pubkey, $this->wwwroot); } $this->lastconnecttime = 0; $this->initialized = self::INITIALIZED; $this->changed = true; if (false == $this->publickey->expires) { $this->publickey == null; return false; } } return true; }