/** * Save our results. Tearing down and unsetting the Wizard is handled by * in {@link runWizard()} and does not need to be implemented here. * * @param string $cacheName * @return boolean TRUE if save was successful and tear-down/cleanup of the * Wizard should ensue. * @access public * @since 1/28/08 */ public function saveWizard($cacheName) { $wizard = $this->getWizard($cacheName); if (!$wizard->validate()) { return false; } $values = $wizard->getAllValues(); // printpre($values); // return false; try { if (!defined('DATAPORT_TMP_DIR')) { throw new Exception("DATAPORT_TMP_DIR must be defined in the Segue configuration."); } $archivePath = $values['mode']['backup_file']['tmp_name']; $archiveName = basename($archivePath); $decompressDir = DATAPORT_TMP_DIR . '/' . $archiveName . '_source'; if (!$values['mode']['backup_file']['size']) { throw new Exception("File upload error - archive was not successfully uploaded and has no size."); } $this->decompressArchive($archivePath, $decompressDir); // Do the import $director = SiteDispatcher::getSiteDirector(); $doc = new Harmoni_DOMDocument(); $doc->load($decompressDir . "/site.xml"); // Validate the document contents $doc->schemaValidateWithException(MYDIR . "/doc/raw/dtds/segue2-site.xsd"); $mediaDir = $decompressDir; switch ($values['mode']['trust']) { case 'all': $class = 'DomImportSiteVisitor'; break; case 'time_only': $class = 'UntrustedAgentDomImportSiteVisitor'; break; default: $class = 'UntrustedAgentAndTimeDomImportSiteVisitor'; } $importer = new $class($doc, $mediaDir, $director); if ($values['mode']['roles'] == '1') { $importer->enableRoleImport(); } if ($values['mode']['comments'] == '0') { $importer->disableCommentImport(); } if (isset($values['owners'])) { $idMgr = Services::getService('Id'); foreach ($values['owners']['admins'] as $adminIdString) { $importer->addSiteAdministrator($idMgr->getId($adminIdString)); } } $importer->enableStatusOutput(); $site = $importer->importAtSlot($values['mode']['slotname']); // Delete the uploaded file unlink($archivePath); // Delete the decompressed Archive $this->deleteRecursive($decompressDir); } catch (Exception $importException) { // Delete the uploaded file try { if (file_exists($archivePath)) { unlink($archivePath); } } catch (Exception $deleteException) { print "\n<div>\n\t"; print $deleteException->getMessage(); print "\n</div>"; } // Delete the decompressed Archive try { if (file_exists($decompressDir)) { $this->deleteRecursive($decompressDir); } } catch (Exception $deleteException) { print "\n<div>\n\t"; print $deleteException->getMessage(); print "\n</div>"; } print "\n<div>\n\t"; print $importException->getMessage(); // print HarmoniErrorHandler::printDebugBacktrace($importException->getTrace()); print "\n</div>"; $wizard->backupFile->setValue(array('name' => null, 'size' => null, 'type' => null)); /********************************************************* * Log the failure *********************************************************/ if (Services::serviceRunning("Logging")) { $loggingManager = Services::getService("Logging"); $log = $loggingManager->getLogForWriting("Segue"); $formatType = new Type("logging", "edu.middlebury", "AgentsAndNodes", "A format in which the acting Agent[s] and the target nodes affected are specified."); $priorityType = new Type("logging", "edu.middlebury", "Error", "Recoverable errors."); $item = new AgentNodeEntryItem("Create Site", "Failure in importing site for placeholder, '" . $values['mode']['slotname'] . "'. " . $importException->getMessage()); $item->setBacktrace($importException->getTrace()); $item->addTextToBactrace("Archive Upload: " . printpre($values['mode']['backup_file'], true)); $log->appendLogWithTypes($item, $formatType, $priorityType); } return false; } /********************************************************* * Log the success *********************************************************/ if (Services::serviceRunning("Logging")) { $loggingManager = Services::getService("Logging"); $log = $loggingManager->getLogForWriting("Segue"); $formatType = new Type("logging", "edu.middlebury", "AgentsAndNodes", "A format in which the acting Agent[s] and the target nodes affected are specified."); $priorityType = new Type("logging", "edu.middlebury", "Event_Notice", "Normal events."); $item = new AgentNodeEntryItem("Create Site", "Site imported for placeholder, '" . $values['mode']['slotname'] . "'."); $item->addNodeId($site->getQualifierId()); $log->appendLogWithTypes($item, $formatType, $priorityType); } return true; }
/** * Log an error or exception with the Logging OSID implemenation * * @param string $type The type of error or exception that occurred * @param string $message A message. * @param array $backtrace * @param optional string $logName The name of the log to write to. * @param optional string $category A category for the error or exception. * @return void * @access public * @since 10/10/07 * @static */ public static function logMessage($type, $message, array $backtrace, $logName = 'Harmoni', $category = null) { /********************************************************* * Log the error in the default system log if the log_errors * directive is on. *********************************************************/ if (ini_get('log_errors') === true || ini_get('log_errors') === 'On' || ini_get('log_errors') === '1') { error_log("PHP " . $type . ": " . strip_tags($message)); } /********************************************************* * Log the error using the Logging OSID if available. *********************************************************/ // If we have an error in the error handler or the logging system, // don't infinitely loop trying to log the error of the error.... $testBacktrace = debug_backtrace(); for ($i = 1; $i < count($testBacktrace); $i++) { if (isset($testBacktrace[$i]['function']) && strtolower($testBacktrace[$i]['function']) == 'logMessage') { return; } } if (class_exists('Services') && Services::serviceRunning("Logging")) { $logName = preg_replace('/[^a-z0-9_\\s-.]/i', '', $logName); try { $loggingManager = Services::getService("Logging"); $log = $loggingManager->getLogForWriting($logName); $formatType = new Type("logging", "edu.middlebury", "AgentsAndNodes", "A format in which the acting Agent[s] and the target nodes affected are specified."); $priorityType = new Type("logging", "edu.middlebury", strip_tags($type), "Events involving critical system errors."); $item = new AgentNodeEntryItem(strip_tags($category), HtmlString::getSafeHtml($message)); $item->setBacktrace($backtrace); if (isset($_SERVER['REQUEST_URI'])) { $item->addTextToBactrace("\n<div><strong>REQUEST_URI: </strong>" . $_SERVER['REQUEST_URI'] . "</div>"); } if (isset($_SERVER['HTTP_REFERER'])) { $item->addTextToBactrace("\n<div><strong>HTTP_REFERER: </strong>" . htmlspecialchars($_SERVER['HTTP_REFERER']) . "</div>"); } if (isset($_SERVER['HTTP_USER_AGENT'])) { $item->addTextToBactrace("\n<div><strong>HTTP_USER_AGENT: </strong><pre>" . htmlspecialchars(print_r($_SERVER['HTTP_USER_AGENT'], true)) . "</pre></div>"); } // Log IP addresses of unauthenticated users for help in tracing // anonymous intrusion attempts. Currently not logging IP addresses // of logged-in users for privacy, even though visitor accounts // mean that logged-in users may be just as dangerous as anonymous ones. if (Services::serviceRunning("AuthN")) { $authN = Services::getService("AuthN"); if (!$authN->isUserAuthenticatedWithAnyType()) { if (isset($_SERVER['REMOTE_ADDR'])) { $item->addTextToBactrace("\n<div><strong>REMOTE_ADDR: </strong><pre>" . htmlspecialchars($_SERVER['REMOTE_ADDR']) . "</pre></div>"); } if (isset($_SERVER['REMOTE_HOST'])) { $item->addTextToBactrace("\n<div><strong>REMOTE_HOST: </strong><pre>" . htmlspecialchars($_SERVER['REMOTE_HOST']) . "</pre></div>"); } } } $item->addTextToBactrace("\n<div><strong>GET: </strong><pre>" . htmlspecialchars(print_r(self::stripPrivate($_GET), true)) . "</pre></div>"); $item->addTextToBactrace("\n<div><strong>POST: </strong><pre>" . htmlspecialchars(print_r(self::stripPrivate($_POST), true)) . "</pre></div>"); $log->appendLogWithTypes($item, $formatType, $priorityType); } catch (Exception $e) { // Just continue if we can't log the exception. } } }