Example #1
0
 public function create($domaindata, $createDb = true)
 {
     $result = false;
     $hook = new \Innomatic\Process\Hook($this->rootda, 'innomatic', 'domain.create');
     if ($hook->callHooks('calltime', $this, array('domaindata' => $domaindata)) == \Innomatic\Process\Hook::RESULT_OK) {
         $domaindata['domainid'] = strtolower(str_replace(' ', '', trim($domaindata['domainid'])));
         // Checks if the domainid contains reserved words.
         if (in_array($domaindata['domainid'], $this->reservedNames)) {
             $log = $this->container->getLogger();
             $log->logEvent('innomatic.domain.create', 'Cannot create domain with id "' . $domaindata['domainid'] . '" since it is a reserved word', \Innomatic\Logging\Logger::WARNING);
             return false;
         }
         // When in enterprise edition, checks if there are no domains.
         $goon = true;
         if ($this->container->getEdition() == \Innomatic\Core\InnomaticContainer::EDITION_SINGLETENANT) {
             $check_query = $this->container->getDataAccess()->execute('SELECT count(*) AS domains FROM domains');
             if ($check_query->getFields('domains') > 0) {
                 $goon = false;
             }
         }
         if ($goon) {
             // Default settings and settings tuning
             //
             $nextseq = $this->rootda->getNextSequenceValue('domains_id_seq');
             // Set database name prefix
             $platformName = $this->container->getPlatformName();
             if (!strlen($platformName)) {
                 // Default prefix
                 $platformName = 'innomatic';
             } else {
                 $platformName = strtolower($platformName);
                 // Remove empty spaces
                 $platformName = str_replace(' ', '', $platformName);
                 // Remove accented characters
                 $platformName = iconv("utf-8", "ascii//TRANSLIT", $platformName);
             }
             // TODO check that the domainid doesn't contain unsupported characters.
             $domaindata['domainid'] = $this->defopt($domaindata['domainid'], $nextseq);
             $domaindata['domainname'] = $this->defopt(trim($domaindata['domainname']), $domaindata['domainid'] . ' domain');
             $domaindata['domainpassword'] = $this->defopt(trim($domaindata['domainpassword']), $domaindata['domainid']);
             $domaindata['domaindaname'] = $this->defopt(strtolower(str_replace(' ', '', trim($domaindata['domaindaname']))), $platformName . '_' . $domaindata['domainid'] . '_tenant');
             $domaindata['dataaccesshost'] = $this->defopt(trim($domaindata['dataaccesshost']), $this->container->getConfig()->value('RootDatabaseHost'));
             $domaindata['dataaccessport'] = $this->defopt(trim($domaindata['dataaccessport']), $this->container->getConfig()->value('RootDatabasePort'));
             $domaindata['dataaccessuser'] = $this->defopt(str_replace(' ', '', trim($domaindata['dataaccessuser'])), $this->container->getConfig()->value('RootDatabaseUser'));
             $domaindata['dataaccesspassword'] = $this->defopt(trim($domaindata['dataaccesspassword']), $this->container->getConfig()->value('RootDatabasePassword'));
             $domaindata['dataaccesstype'] = $this->defopt(trim($domaindata['dataaccesstype']), $this->container->getConfig()->value('RootDatabaseType'));
             $domaindata['domaincreationdate'] = isset($domaindata['domaincreationdate']) ? trim($domaindata['domaincreationdate']) : time();
             $domaindata['domainexpirydate'] = isset($domaindata['domainexpirytime']) ? trim($domaindata['domainexpirydate']) : time();
             $domaindata['domainactive'] = isset($domaindata['domainactive']) ? $domaindata['domainactive'] : $this->rootda->fmttrue;
             $domaindata['maxusers'] = isset($domaindata['maxusers']) ? $domaindata['maxusers'] : '0';
             if (!isset($domaindata['domainnotes'])) {
                 $domaindata['domainnotes'] = '';
             }
             $domaindata['webappskeleton'] = $this->defopt(trim($domaindata['webappskeleton']), 'default');
             if ($this->container->getEdition() == \Innomatic\Core\InnomaticContainer::EDITION_SINGLETENANT) {
                 $domaindata['domaindaname'] = $this->container->getConfig()->value('RootDatabaseName');
                 $domaindata['dataaccesshost'] = $this->container->getConfig()->value('RootDatabaseHost');
                 $domaindata['dataaccessport'] = $this->container->getConfig()->value('RootDatabasePort');
                 $domaindata['dataaccessuser'] = $this->container->getConfig()->value('RootDatabaseUser');
                 $domaindata['dataaccesspassword'] = $this->container->getConfig()->value('RootDatabasePassword');
                 $domaindata['dataaccesstype'] = $this->container->getConfig()->value('RootDatabaseType');
             }
             if ($this->rootda->execute('INSERT INTO domains VALUES ( ' . $nextseq . ',' . $this->rootda->formatText($domaindata['domainid']) . ',' . $this->rootda->formatText($domaindata['domainname']) . ',' . $this->rootda->formatText(md5($domaindata['domainpassword'])) . ',' . $this->rootda->formatText($domaindata['domaindaname']) . ',' . $this->rootda->formatText($domaindata['dataaccesshost']) . ',' . $this->rootda->formatInteger($domaindata['dataaccessport']) . ',' . $this->rootda->formatText($domaindata['dataaccessuser']) . ',' . $this->rootda->formatText($domaindata['dataaccesspassword']) . ',' . $this->rootda->formatText($domaindata['dataaccesstype']) . ',' . $this->rootda->formatDate($domaindata['domaincreationdate']) . ',' . $this->rootda->formatDate($domaindata['domainexpirydate']) . ',' . $this->rootda->formatText($domaindata['domainactive']) . ',' . $this->rootda->formatText($domaindata['domainnotes']) . ',' . $this->rootda->formatInteger($domaindata['maxusers']) . ',' . $this->rootda->formatText($domaindata['webappskeleton']) . ',' . $this->rootda->formatText($domaindata['webappurl']) . ')')) {
                 $this->domainid = $domaindata['domainid'];
                 $this->domainserial = $nextseq;
                 $this->domainlog = new \Innomatic\Logging\Logger($this->container->getHome() . 'core/domains/' . $domaindata['domainid'] . '/log/domain.log');
                 // Domain private directory tree creation inside Innomatic webapp.
                 $this->makedir($this->container->getHome() . 'core/domains/' . $domaindata['domainid']);
                 $this->makedir($this->container->getHome() . 'core/domains/' . $domaindata['domainid'] . '/log');
                 $this->makedir($this->container->getHome() . 'core/domains/' . $domaindata['domainid'] . '/conf');
                 // Domain webapp creation.
                 \Innomatic\Webapp\WebAppContainer::createWebApp($domaindata['domainid'], $domaindata['webappskeleton']);
                 // Creates the database, if asked.
                 if ($this->container->getEdition() == \Innomatic\Core\InnomaticContainer::EDITION_MULTITENANT) {
                     $args['dbtype'] = strlen($domaindata['dataaccesstype']) ? $domaindata['dataaccesstype'] : $this->container->getConfig()->value('RootDatabaseType');
                     $args['dbname'] = $domaindata['domaindaname'];
                     $args['dbhost'] = $domaindata['dataaccesshost'];
                     $args['dbport'] = $domaindata['dataaccessport'];
                     $args['dbuser'] = $domaindata['dataaccessuser'];
                     $args['dbpass'] = $domaindata['dataaccesspassword'];
                     $args['dblog'] = $this->container->getHome() . 'core/domains/' . $domaindata['domainid'] . '/log/dataaccess.log';
                     $args['name'] = $domaindata['domaindaname'];
                     $dasn_string = $args['dbtype'] . '://' . $args['dbuser'] . ':' . $args['dbpass'] . '@' . $args['dbhost'] . ':' . $args['dbport'] . '/' . $args['dbname'] . '?' . 'logfile=' . $args['dblog'];
                     $tmpdb = \Innomatic\Dataaccess\DataAccessFactory::getDataAccess(new \Innomatic\Dataaccess\DataAccessSourceName($dasn_string));
                     if ($createDb) {
                         if ($tmpdb->connect()) {
                             $tmpdb->dropDB($args);
                             $tmpdb->close();
                         }
                     }
                 } else {
                     $tmpdb = $this->rootda;
                 }
                 if (!$createDb or $this->container->getEdition() == \Innomatic\Core\InnomaticContainer::EDITION_SINGLETENANT or $created = $tmpdb->createDB($args)) {
                     if (isset($created) and $created == true) {
                         $this->domainlog->logEvent($domaindata['domainid'], 'Database ' . $args['dbname'] . ' created', \Innomatic\Logging\Logger::NOTICE);
                     }
                     if ($this->container->getEdition() == \Innomatic\Core\InnomaticContainer::EDITION_SINGLETENANT or $tmpdb->connect()) {
                         if ($this->container->getEdition() == \Innomatic\Core\InnomaticContainer::EDITION_MULTITENANT) {
                             $this->dataAccess = $tmpdb;
                         } else {
                             $this->dataAccess = $this->rootda;
                         }
                         //$xmldb = new DataAccessXmlTable( $tmpdb, DataAccessXmlTable::SQL_CREATE );
                         $this->container->setCurrentDomain($this);
                         // Prepares the domain admin user to be created later
                         $tmpuser = new \Innomatic\Domain\User\User($nextseq);
                         $this->container->setCurrentUser($tmpuser);
                         $tmpquery = $this->rootda->execute('SELECT id FROM applications WHERE appid=' . $this->rootda->formatText('innomatic'));
                         if ($this->enableApplication($tmpquery->getFields('id'))) {
                             // Create the administrator user
                             $tmpuser->createAdminUser($domaindata['domainid'], $domaindata['domainpassword']);
                             $log = $this->container->getLogger();
                             $log->logEvent($domaindata['domainid'], 'Created new domain ' . $domaindata['domainid'], \Innomatic\Logging\Logger::NOTICE);
                             $this->domainlog->logEvent($domaindata['domainid'], 'Created domain ' . $domaindata['domainid'], \Innomatic\Logging\Logger::NOTICE);
                             if ($hook->callHooks('domaincreated', $this, array('domaindata' => $domaindata)) != \Innomatic\Process\Hook::RESULT_ABORT) {
                                 $result = true;
                             }
                             if ($this->container->getConfig()->Value('SecurityAlertOnDomainOperation') == '1') {
                                 $innomatic_security = new \Innomatic\Security\SecurityManager();
                                 $innomatic_security->sendAlert('A domain has been created with id ' . $domaindata['domainid']);
                                 unset($innomatic_security);
                             }
                         } else {
                             $log = $this->container->getLogger();
                             $log->logEvent('innomatic.domains.domain.create', 'Unable to enable Innomatic to the domain', \Innomatic\Logging\Logger::ERROR);
                         }
                     } else {
                         $log = $this->container->getLogger();
                         $log->logEvent('innomatic.domains.domain.create', 'Unable to connect to domain database', \Innomatic\Logging\Logger::ERROR);
                     }
                 } else {
                     $log = $this->container->getLogger();
                     $log->logEvent('innomatic.domains.domain.create', 'Unable to create domain database', \Innomatic\Logging\Logger::ERROR);
                 }
             } else {
                 $log = $this->container->getLogger();
                 $log->logEvent('innomatic.domains.domain.create', 'Unable to insert domain row in domains table', \Innomatic\Logging\Logger::ERROR);
             }
         } else {
             $log = $this->container->getLogger();
             $log->logEvent('innomatic.domains.domain.create', 'Tried to create another domain in Enterprise edition', \Innomatic\Logging\Logger::WARNING);
         }
     }
     return $result;
 }