/**
  * Install modules method
  *
  * @throws \Phire\Exception
  * @return void
  */
 public function installModules()
 {
     try {
         $path = BASE_PATH . APP_URI;
         if ($path == '') {
             $path = '/';
         }
         $modulePath1 = $_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/extensions/modules';
         $modulePath2 = __DIR__ . '/../../../../../module';
         $formats = Archive::formats();
         $phireCookie = null;
         foreach ($this->data['new'] as $name => $module) {
             $ext = substr($module, strrpos($module, '.') + 1);
             if (array_key_exists($ext, $formats)) {
                 $modPath = file_exists($modulePath1 . '/' . $module) ? $modulePath1 : $modulePath2;
                 if (!is_writable($modPath)) {
                     throw new \Phire\Exception($this->i18n->__('The modules folder is not writable.'));
                 }
                 $archive = new Archive($modPath . '/' . $module);
                 $archive->extract($modPath . '/');
                 if ((stripos($module, 'gz') || stripos($module, 'bz')) && file_exists($modPath . '/' . $name . '.tar')) {
                     unlink($modPath . '/' . $name . '.tar');
                 }
                 $dbType = Table\Extensions::getSql()->getDbType();
                 if ($dbType == \Pop\Db\Sql::SQLITE) {
                     $type = 'sqlite';
                 } else {
                     if ($dbType == \Pop\Db\Sql::PGSQL) {
                         $type = 'pgsql';
                     } else {
                         $type = 'mysql';
                     }
                 }
                 $sqlFile = $modPath . '/' . $name . '/data/' . strtolower($name) . '.' . $type . '.sql';
                 $cfg = null;
                 $tables = array();
                 $info = array();
                 // Check for a config and try to get info out of it
                 if (file_exists($modPath . '/' . $name . '/config') && file_exists($modPath . '/' . $name . '/config/module.php')) {
                     $cfg = file_get_contents($modPath . '/' . $name . '/config/module.php');
                     if (strpos($cfg, '*/') !== false) {
                         $cfgHeader = substr($cfg, 0, strpos($cfg, '*/'));
                         $cfgHeader = substr($cfgHeader, strpos($cfgHeader, '/*') + 2);
                         $cfgHeaderAry = explode("\n", $cfgHeader);
                         foreach ($cfgHeaderAry as $line) {
                             if (strpos($line, ':')) {
                                 $ary = explode(':', $line);
                                 if (isset($ary[0]) && isset($ary[1])) {
                                     $key = trim(str_replace('*', '', $ary[0]));
                                     $value = trim(str_replace('*', '', $ary[1]));
                                     $info[$key] = $value;
                                 }
                             }
                         }
                     }
                 }
                 if (file_exists($sqlFile)) {
                     // Get any tables required and created by this module
                     $sql = file_get_contents($sqlFile);
                     $tables = array();
                     $matches = array();
                     preg_match_all('/^CREATE TABLE(.*)$/mi', $sql, $matches);
                     if (isset($matches[0]) && isset($matches[0][0])) {
                         foreach ($matches[0] as $table) {
                             if (strpos($table, '`') !== false) {
                                 $table = substr($table, strpos($table, '`') + 1);
                                 $table = substr($table, 0, strpos($table, '`'));
                             } else {
                                 if (strpos($table, '"') !== false) {
                                     $table = substr($table, strpos($table, '"') + 1);
                                     $table = substr($table, 0, strpos($table, '"'));
                                 } else {
                                     if (strpos($table, "'") !== false) {
                                         $table = substr($table, strpos($table, "'") + 1);
                                         $table = substr($table, 0, strpos($table, "'"));
                                     } else {
                                         if (stripos($table, 'EXISTS') !== false) {
                                             $table = substr($table, stripos($table, 'EXISTS') + 6);
                                         } else {
                                             $table = substr($table, stripos($table, 'TABLE') + 5);
                                         }
                                         if (strpos($table, '(') !== false) {
                                             $table = substr($table, 0, strpos($table, '('));
                                         }
                                         $table = trim($table);
                                     }
                                 }
                             }
                             $tables[] = str_replace('[{prefix}]', DB_PREFIX, $table);
                         }
                     }
                     $ext = new Table\Extensions(array('name' => $name, 'file' => $module, 'type' => 1, 'active' => 1, 'assets' => serialize(array('tables' => $tables, 'info' => $info))));
                     $ext->save();
                     // If DB is SQLite
                     if (stripos($type, 'Sqlite') !== false) {
                         $dbName = realpath($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/.htphire.sqlite');
                         $dbUser = null;
                         $dbPassword = null;
                         $dbHost = null;
                         $installFile = $dbName;
                     } else {
                         $dbName = DB_NAME;
                         $dbUser = DB_USER;
                         $dbPassword = DB_PASS;
                         $dbHost = DB_HOST;
                         $installFile = null;
                     }
                     $db = array('database' => $dbName, 'username' => $dbUser, 'password' => $dbPassword, 'host' => $dbHost, 'prefix' => DB_PREFIX, 'type' => DB_INTERFACE == 'Pdo' ? 'Pdo_' . ucfirst(DB_TYPE) : DB_INTERFACE);
                     Dbs::install($dbName, $db, $sqlFile, $installFile, true, false);
                 } else {
                     $ext = new Table\Extensions(array('name' => $name, 'type' => 1, 'active' => 1, 'assets' => serialize(array('tables' => $tables, 'info' => $info))));
                     $ext->save();
                 }
                 if (null !== $cfg) {
                     $config = (include $modPath . '/' . $name . '/config/module.php');
                     if (null !== $config[$name]->install) {
                         $installFunc = $config[$name]->install;
                         $installFunc();
                     }
                 }
                 if (php_sapi_name() != 'cli') {
                     $cookie = Cookie::getInstance(array('path' => $path));
                     if (isset($cookie->phire)) {
                         if (null === $phireCookie) {
                             $phireCookie = $cookie->phire;
                         }
                         $i18n = file_exists($modPath . '/' . $name . '/data/assets/i18n');
                         $modules = (array) $phireCookie->modules;
                         $modules[] = array('name' => $name, 'i18n' => $i18n);
                         $phireCookie->modules = $modules;
                     }
                 }
             }
         }
         if (null !== $phireCookie) {
             $cookie = Cookie::getInstance(array('path' => $path));
             $cookie->set('phire', $phireCookie);
         }
     } catch (\Exception $e) {
         $this->data['error'] = $e->getMessage();
     }
 }