00001 <?php
00010 include_once("core/modules/moduleinformation/ModuleInfo.class.php");
00011 include_once("core/utils/FileOps.class.php");
00012 include_once("core/db/dbjames.class.php");
00013 include_once("core/system/JSystem.class.php");
00014 include_once("core/i18n/I18N.class.php");
00015 include_once("core/comms/ThrowEvent.inc.php");
00016
00017 define("DEFAULT_INFO_FILENAME", "config.xml");
00018
00019 define("MODULEINSTALL_ERROR_PATH", 1);
00020 define("MODULEINSTALL_ERROR_FILE_NOT_FOUND", 2);
00021 define("MODULEINSTALL_ERROR_NO_MODULE_DEFINED", 4);
00022 define("MODULEINSTALL_ERROR_DEST_PATH_NO_WRITABLE", 5);
00023 define("MODULEINSTALL_ERROR_ALREADY_INSTALLED", 6);
00024 define("MODULEINSTALL_ERROR_INSERT_MODULES", 7);
00025 define("MODULEINSTALL_ERROR_INSERT_PERMISSIONS", 8);
00026 define("MODULEINSTALL_ERROR_INSERT_CONFIG", 9);
00027 define("MODULEINSTALL_ERROR_INSERT_TABLES", 10);
00028 define("MODULEINSTALL_ERROR_INSERT_CONFIG_OPTIONS", 11);
00029 define("MODULEINSTALL_ERROR_INSERT_CONFIG_DEFAULT", 12);
00030
00039 class ModuleInstall extends JBaseObject
00040 {
00041
00042 var $modulesourcepath;
00043 var $infofile;
00044 var $destpath;
00045
00046
00047 var $modinfo;
00048
00049
00050 var $moduleid;
00051
00052
00053 var $dbObj;
00054 var $validModule;
00055
00057 function ModuleInstall($path)
00058 {
00059 $this->validModule = false;
00060 $this->dbObj = new DBJames();
00061 $this->setSourcePath($path);
00062 }
00063
00069 function SetSourcePath($path)
00070 {
00071 if (! is_dir($path))
00072 {
00073 return $this->RaiseError(MODULEINSTALL_ERROR_PATH);
00074 }
00075 else
00076 {
00077 $this->modulesourcepath = $path;
00078 $this->setInfoFile();
00079 $ret = true;
00080 }
00081
00082 return $ret;
00083 }
00084
00091 function SetInfoFile($name=DEFAULT_INFO_FILENAME)
00092 {
00093 $this->infofile = $this->modulesourcepath."/".$name;
00094 if (! file_exists($this->infofile))
00095 return $this->RaiseError(MODULEINSTALL_ERROR_FILE_NOT_FOUND);
00096 else
00097 {
00098 $ret = true;
00099 $this->modinfo = ModuleInfo::getInformation("file", $this->infofile);
00100 $this->destpath = JSystem::GetConfig("JamesDir")."/modules/";
00101 if (! file_exists($this->destpath))
00102 @mkdir($this->destpath,0700);
00103 $this->validModule = true;
00104 }
00105
00106 return $ret;
00107 }
00108
00113 function &GetModuleInfo()
00114 {
00115 return $this->modinfo;
00116 }
00117
00122 function Install()
00123 {
00124 if (! $this->validModule)
00125 {
00126 return $this->RaiseError(MODULEINSTALL_ERROR_NO_MODULE_DEFINED);
00127 }
00128 else
00129 {
00130 $ret = true;
00131
00132 $ret = $this->FilesInstall();
00133
00134 if (! JSystem::IsError($ret))
00135 $ret = $this->ModulesInfoInstall();
00136 if (! JSystem::IsError($ret))
00137 $ret = $this->TablesInstall();
00138 if (! JSystem::IsError($ret))
00139 $ret = $this->PermissionsInstall();
00140 if (! JSystem::IsError($ret))
00141 $ret = $this->ConfigInstall();
00142
00143
00144
00145 if (! JSystem::IsError($ret))
00146 {
00147 ThrowEvent("InstallModule", $this->moduleid);
00148 $ret = $this->modinfo->GetName();
00149 }
00150 }
00151
00152 return $ret;
00153 }
00154
00159 function FilesInstall()
00160 {
00161 if (! is_writable($this->destpath))
00162 {
00163 return $this->RaiseError(MODULEINSTALL_ERROR_DEST_PATH_NO_WRITABLE);
00164 }
00165
00166 if (file_exists($this->destpath . $this->modinfo->getName()))
00167 {
00168 return $this->RaiseError(MODULEINSTALL_ERROR_ALREADY_INSTALLED);
00169 }
00170
00171
00172 FileOps::cp($this->modulesourcepath, $this->destpath);
00173 }
00174
00179 function ModulesInfoInstall()
00180 {
00181 $query = " INSERT INTO Modules ";
00182 $query .= " (name) VALUES ";
00183 $query .= " (";
00184 $query .= " \"".$this->modinfo->getName()."\")";
00185
00186 $this->dbObj->Execute($query);
00187 if ($this->dbObj->rs)
00188 {
00189 $this->dbObj->setInsertID();
00190 $this->moduleid = $this->dbObj->newID;
00191
00192 $result = true;
00193 }
00194 else
00195 {
00196 return $this->RaiseError(MODULEINSTALL_ERROR_INSERT_MODULES);
00197 }
00198
00199 return $result;
00200 }
00201
00206 function TablesInstall()
00207 {
00208 $result = true;
00209
00210 $sqlQuery = $this->modinfo->getSql();
00211
00212 if (count($sqlQuery) > 0)
00213 {
00214 foreach ($sqlQuery as $query)
00215 {
00216 if ($result)
00217 {
00218 $this->dbObj->Execute($query);
00219
00220 if (! $this->dbObj->rs)
00221 return $this->RaiseError(MODULEINSTALL_ERROR_INSERT_TABLES,
00222 $query);
00223 }
00224 }
00225 }
00226
00227 return $result;
00228 }
00229
00234 function PermissionsInstall()
00235 {
00236 $perms = $this->modinfo->GetPermissions();
00237 $ret = $this->_PermissionsInsert("hidden", gettext("Hidden"));
00238
00239 if (count($perms) > 0)
00240 {
00241 foreach ($perms as $perm)
00242 {
00243 if (! JSystem::IsError($ret))
00244 {
00245 $ret = $this->_PermissionsInsert($perm->GetName());
00246 }
00247 }
00248 }
00249 return $ret;
00250 }
00251
00260 function _PermissionsInsert($name)
00261 {
00262 $query = " INSERT INTO Permissions ";
00263 $query .= " (name, idM) VALUES ";
00264 $query .= " ('".$name."', ";
00265 $query .= " '".$this->moduleid."') ";
00266
00267 $result = true;
00268 $this->dbObj->Execute($query);
00269
00270 if (! $this->dbObj->rs)
00271 {
00272 return $this->RaiseError(MODULEINSTALL_ERROR_INSERT_PERMISSIONS,
00273 $name);
00274 }
00275
00276 return $result;
00277 }
00278
00283 function ConfigInstall()
00284 {
00285 $confs = $this->modinfo->GetConfig();
00286 $ret = true;
00287
00288 if (count($confs) > 0)
00289 {
00290 foreach ($confs as $configKey)
00291 {
00292 if (! JSystem::IsError($ret))
00293 {
00294 $ret = $this->_ConfigInsert($configKey);
00295
00296 $this->dbObj->setInsertID();
00297 $idK = $this->dbObj->newID;
00298
00299
00300 if (! JSystem::IsError($ret))
00301 $ret = $this->_ConfigSetDefault($configKey);
00302 }
00303 }
00304 }
00305 return $ret;
00306 }
00307
00315 function _ConfigInsert($configKey)
00316 {
00317 $query = " INSERT INTO ConfigKeys ";
00318 $query .= " (idM, keyName) VALUES ";
00319 $query .= " ('".$this->moduleid."', ";
00320 $query .= " '".$configKey->GetName()."') ";
00321
00322 $result = true;
00323 $this->dbObj->Execute($query);
00324
00325 if (! $this->dbObj->rs)
00326 {
00327 return $this->RaiseError(MODULEINSTALL_ERROR_INSERT_CONFIG,
00328 $configKey->GetName());
00329 }
00330
00331 return $result;
00332 }
00333
00340 function _ConfigSetDefault($configKey)
00341 {
00342
00343 if ($configKey->HasDefaultValue())
00344 {
00345
00346 $keyName = $configKey->GetName();
00347 $defaultValue = $configKey->GetDefaultValue();
00348
00349
00350 $petition = new ConfigPetition();
00351 $petition->SetModule($this->moduleid);
00352
00353
00354 $cache =& ConfigCache::GetInstance();
00355
00356 $cache->SetPetition($petition, array($keyName => $defValue));
00357 }
00358
00359 return true;
00360 }
00361
00369 function RaiseError($error, $extra="")
00370 {
00371 switch($error)
00372 {
00373 case MODULEINSTALL_ERROR_PATH:
00374 $r = gettext("Source path not valid");
00375 break;
00376 case MODULEINSTALL_ERROR_FILE_NOT_FOUND:
00377 $r = gettext("Config file of the module not found");
00378 break;
00379 case MODULEINSTALL_ERROR_NO_MODULE_DEFINED:
00380 $r = gettext("No module defined to be installed");
00381 break;
00382 case MODULEINSTALL_ERROR_DEST_PATH_NO_WRITABLE:
00383 $r = gettext("Destination path no writable");
00384 break;
00385 case MODULEINSTALL_ERROR_ALREADY_INSTALLED:
00386 $r = gettext("Module already installed");
00387 break;
00388 case MODULEINSTALL_ERROR_INSERT_MODULES:
00389 $r = gettext("Error inserting the module in the DB");
00390 break;
00391 case MODULEINSTALL_ERROR_INSERT_PERMISSIONS:
00392 $r = gettext("Error inserting permmisions in the DB");
00393 break;
00394 case MODULEINSTALL_ERROR_INSERT_CONFIG:
00395 $r = gettext("Error inserting the config keys in the DB");
00396 break;
00397 case MODULEINSTALL_ERROR_INSERT_TABLES:
00398 $r = gettext("Error inserting tables in the DB");
00399 break;
00400 case MODULEINSTALL_ERROR_INSERT_CONFIG_DEFAULT:
00401 $r = gettext("Error inserting default config value in the DB");
00402 break;
00403 default:
00404 $r = gettext("Unknown error");
00405 }
00406
00407 return parent::RaiseError(new JError($error, $r, JERROR_DEFAULT, $extra));
00408 }
00409 }
00410 ?>