diff --git a/lib/componentlib.class.php b/lib/componentlib.class.php new file mode 100644 index 00000000000..79cb872f06e --- /dev/null +++ b/lib/componentlib.class.php @@ -0,0 +1,226 @@ +sourcebase = $sourcebase; + $this->zippath = $zippath; + $this->zipfilename = $zipfilename; + $this->md5filename = $md5filename; + $this->componentname= ''; + $this->destpath = $destpath; + $this->errorstring = ''; + $this->extramd5info = ''; + $this->requisitesok = false; + } + + /** + * This function will check if everything is properly set to begin + * one installation. It'll check for fopen wrappers enabled and + * admin privileges. Also, it will check for required settings + * and will fill everything as needed. + * + * @return boolean true/false (plus detailed error in errorstring) + */ + function check_requisites() { + + /// Check for admin (this will be out in the future) + if (!isadmin()) { + $this->errorstring='onlyadmicaninstallcomponents'; + return false; + } else { + /// Check for fopen remote enabled + if (!ini_get('allow_url_fopen')) { + $this->errorstring='remotedownloadnotallowed'; + return false; + } + } + /// Check that everything we need is present + if (empty($this->sourcebase) || empty($this->zippath) || empty($this->zipfilename)) { + $this->errorstring='missingrequiredfield'; + return false; + } + /// Check for correct sourcebase (this will be out in the future) + if ($this->sourcebase != 'http://download.moodle.org') { + $this->errorstring='wrongsourcebase'; + return false; + } + /// Check the zip file is a correct one (by extension) + if (stripos($this->zipfilename, '.zip') === false) { + $this->errorstring='wrongzipfilename'; + return false; + } + /// Calculate the componentnamea + $pos = stripos($this->zipfilename, '.zip'); + $this->componentname = substr($this->zipfilename, 0, $pos+1); + /// Calculate md5filename if it's empty + if (empty($this->md5filename)) { + $this->md5filename = $this->componentname.'md5'; + } + /// Set the requisites passed flag + $this->requisitesok = true; + return true; + } + + /** + * This function will download the specified md5 file, looking for the + * current componentname, returning its md5 field and storing extramd5info + * if present + * + * @return mixed md5 present in server (or false if error) + */ + function get_remote_md5() { + + /// Check requisites are passed + if (!$this->requisitesok) { + $this->errorstring='requisitesnotpassed'; + return false; + } + + /// Define and retrieve the full md5 file + $source = $this->sourcebase.'/'.$this->zippath.'/'.$this->md5filename; + $availablecomponents = array(); + if ($fp = fopen($source, 'r')) { + /// Read from URL, each line will be one component + while(!feof ($fp)) { + $availablecomponents[] = split(',', fgets($fp,1024)); + } + fclose($fp); + /// If no components have been found, return error + if (empty($availablecomponents)) { + $this->errorstring='cannotdownloadmd5file'; + return false; + } + /// Build an associative array of components, storing it in the + /// Look for our expected componentname + + print_object($availablecomponents); + + } else { + /// Return error + $this->errorstring='cannotdownloadcomponent'; + return false; + } + + } + + /** + * This function returns the errorstring + * + * @return string the error string + */ + function get_error_string() { + return $this->errorstring; + } + + +} /// End of component_installer class + +?> +