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
|
<?php
/**
* Perform installation and updates on simpleSAMLphp modules
* based on information found in a module definition.
*
* @Author Andreas Åkre Solberg, <andreas.solberg@uninett.no>
*/
class sspmod_core_ModuleInstaller {
public $module;
public function __construct(sspmod_core_ModuleDefinition $module) {
$this->module = $module;
}
public function remove($branch = NULL) {
$access = $this->module->getAccess($branch);
switch($access['type']) {
// case 'svn' :
// $this->requireInstalled();
// $this->remove($access);
// break;
default:
$this->requireInstalled();
$this->removeModuleDir($access);
break;
}
}
public function install($branch = NULL) {
$access = $this->module->getAccess($branch);
switch($access['type']) {
case 'svn' :
$this->requireNotInstalled();
$this->svnCheckout($access);
$this->enable();
$this->prepareConfig();
break;
case 'zip' :
$this->requireNotInstalled();
$this->zipLoad($access);
$this->enable();
$this->prepareConfig();
break;
default:
throw new Exception('Unknown access method type. Not one of [zip,tgz,svn]');
}
}
public static function exec($cmd) {
echo ' $ ' . $cmd . "\n";
$output = shell_exec(escapeshellcmd($cmd));
if (empty($output)) return;
$oa = explode("\n", $output);
foreach($oa AS $ol) {
echo ' > ' . $ol . "\n";
}
}
public function upgrade($branch = NULL) {
$access = $this->module->getAccess($branch);
switch($access['type']) {
case 'svn' :
$this->requireInstalled();
$this->svnUpdate($access);
$this->enable();
$this->prepareConfig();
break;
case 'zip' :
$this->requireInstalled();
$this->zipLoad($access);
$this->enable();
$this->prepareConfig();
break;
default:
throw new Exception('Unknown access method type. Not one of [zip,tgz,svn]');
}
}
public function dirExists() {
$config = SimpleSAML_Configuration::getConfig('config.php');
$basedir = $config->getBaseDir();
$dir = $basedir . 'modules/' . $this->module->def['id'];
return (file_exists($dir) && is_dir($dir));
}
public function requireValidURL($url) {
if (!preg_match('|http(s)?://[a-zA-Z0-9_-/.]|', $url))
throw new Exception('Invalid URL [' . $url . ']');
}
public function requireNotInstalled() {
if ($this->dirExists())
throw new Exception('The module [' . $this->module->def['id'] . '] is already installed.');
}
public function requireInstalled() {
if (!$this->dirExists())
throw new Exception('The module [' . $this->module->def['id'] . '] is not installed.');
}
public function svnCheckout($access) {
$config = SimpleSAML_Configuration::getConfig('config.php');
$basedir = $config->getBaseDir();
$cmd = "svn co " . escapeshellarg($access['url']) . " " . $basedir . "modules/" . $this->module->def['id'];
self::exec($cmd);
}
public function svnUpdate($access) {
$config = SimpleSAML_Configuration::getConfig('config.php');
$basedir = $config->getBaseDir();
$cmd = "svn up " . $basedir . "modules/" . $this->module->def['id'];
self::exec($cmd);
}
public function removeModuleDir($access) {
$config = SimpleSAML_Configuration::getConfig('config.php');
$basedir = $config->getBaseDir();
$cmd = "rm -rf " . $basedir . "modules/" . $this->module->def['id'];
self::exec($cmd);
}
public function enable() {
$config = SimpleSAML_Configuration::getConfig('config.php');
$basedir = $config->getBaseDir();
$this->requireInstalled();
$cmd = "touch " . $basedir . "modules/" . $this->module->def['id'] . '/enable';
self::exec($cmd);
}
public function prepareConfig() {
$config = SimpleSAML_Configuration::getConfig('config.php');
$basedir = $config->getBaseDir();
$this->requireInstalled();
$dir = $basedir . "modules/" . $this->module->def['id'] . '/config-templates';
if (!file_exists($dir)) return;
$files = scandir($dir);
foreach($files AS $file) {
if(!preg_match('|^.*\.php|', $file)) continue;
if (file_exists($basedir . 'config/' . $file)) {
echo "Configuration file [" . $file . "] already exists. Will not overwrite existing file.\n";
continue;
}
$cmd = 'cp ' . $dir . '/' . $file . ' ' . $basedir . 'config/';
self::exec($cmd);
}
}
public function zipLoad($access) {
$config = SimpleSAML_Configuration::getConfig('config.php');
$basedir = $config->getBaseDir();
$zipfile = $access['url'];
$localfile = tempnam(sys_get_temp_dir(), 'ssp-module-');
$filecontents = file_get_contents($zipfile);
file_put_contents($localfile, $filecontents);
$cmd = "unzip -qo " . escapeshellarg($localfile) . " -d " . $basedir . "modules/";
self::exec($cmd);
}
}
|