Skip to content

Commit

Permalink
Fix creation of consultation media folder when installing via the wiz…
Browse files Browse the repository at this point in the history
…zard, fixes #42 (#62)
  • Loading branch information
mbohal authored Nov 2, 2019
1 parent b3d1f85 commit f632de9
Show file tree
Hide file tree
Showing 10 changed files with 201 additions and 381 deletions.
90 changes: 0 additions & 90 deletions application/configs/config.local.ini.bckp

This file was deleted.

6 changes: 3 additions & 3 deletions application/directories.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php
<?php

$rootPath = dirname(dirname(__FILE__));
$rootPath = dirname(__DIR__);

define('MEDIA_URL', '/www/media');
define('MEDIA_PATH', realpath($rootPath . '/' . MEDIA_URL));
define('VENDOR_PATH', realpath($rootPath . '/vendor'));
define('RUNTIME_PATH', realpath($rootPath . '/runtime'));
define('RUNTIME_PATH', realpath($rootPath . '/runtime'));
4 changes: 3 additions & 1 deletion application/modules/admin/forms/Media/Upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ public function init()
$this->addElement($folder);


$file = $this->createElement('file', 'file');
$file = $this
->createElement('file', 'file')
->setRequired(true);
$this->addElement($file);


Expand Down
242 changes: 117 additions & 125 deletions install/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,150 +14,142 @@
error_reporting(0);
set_time_limit(0);

$scriptPath = realpath(dirname(__FILE__) . '/views/scripts');
$helperPath = realpath(dirname(__FILE__) . '/../application/views/helpers');
$sqlPath = realpath(dirname(__FILE__) . '/../data');
$layoutPath = realpath(dirname(__FILE__) . '/views/layouts');
$langPath = realpath(dirname(__FILE__) . '/languages');
$zendLangPath = realpath(dirname(__FILE__) . '/../vendor/zendframework/zendframework1/resources/languages');

$configPath = realpath(dirname(__FILE__) . '/../application') . '/configs';
$logPath = realpath(dirname(__FILE__) . '/../runtime') . '/logs';
$sessionPath = realpath(dirname(__FILE__) . '/../runtime') . '/sessions';
$cachePath = realpath(dirname(__FILE__) . '/../runtime') . '/cache';
$imgCachePath = realpath(dirname(__FILE__) . '/../www') . '/image_cache';
$consultationsPath = realpath(dirname(__FILE__) . '/../www/media') . '/consultations';
$foldersPath = realpath(dirname(__FILE__) . '/../www/media') . '/folders';

$rootDir = realpath(dirname(__DIR__));
$locale = !empty($_GET['locale']) ? $_GET['locale'] : null;


$view = new View($layoutPath, $scriptPath, $helperPath, $zendLangPath, $langPath, $locale);
$fileSystem = new FileSystem(
$configPath,
$logPath,
$sessionPath,
$cachePath,
$imgCachePath,
$consultationsPath,
$foldersPath
$view = new View(
realpath(__DIR__ . '/views/layouts'),
realpath(__DIR__ . '/views/scripts'),
$rootDir . '/application/views/helpers',
$rootDir . '/vendor/shardj/zf1-future/resources/languages',
realpath(__DIR__ . '/languages'),
$locale
);

const RUNTIME_FOLDERS = [
'runtime/logs',
'runtime/sessions',
'runtime/cache',
'www/image_cache',
'www/media/consultations',
'www/media/folders',
];
const SOURCE_FOLDERS = [
'application/configs'
];
$allFolders = array_merge(RUNTIME_FOLDERS, SOURCE_FOLDERS);


$sqlPath = $rootDir . '/data';
$fileSystem = new FileSystem($rootDir);
$configPath = $rootDir . '/application/configs';

if (!isset($locale)) {
$view->render('step-1.phtml');
} elseif (file_exists($configPath . '/config.local.ini')) {
$view->render('already-installed.phtml');
} elseif (!$fileSystem->validateWritable()) {
if (!$fileSystem->createFolders()) {
} elseif (!$fileSystem->validateWritable($allFolders)) {
$fileSystem->createFolders(RUNTIME_FOLDERS);
$nonWritableFolders = $fileSystem->getNonWritableFolders($allFolders);
if ($nonWritableFolders) {
$view->assign([
'configPathOK' => is_writable($configPath),
'sessionPathOK' => is_writable($sessionPath),
'logPathOK' => is_writable($logPath),
'cachePathOK' => is_writable($cachePath),
'imgCachePathOK' => is_writable($imgCachePath),
'consultationsPathOK' => is_writable($consultationsPath),
'foldersPathOK' => is_writable($foldersPath),
'configPath' => $configPath,
'sessionPath' => $sessionPath,
'logPath' => $logPath,
'cachePath' => $cachePath,
'imgCachePath' => $imgCachePath,
'consultationsPath' => $consultationsPath,
'foldersPath' => $foldersPath,
'allFolders' => $allFolders,
'locale' => $locale,
'nonWritableFolders' => $nonWritableFolders,
]);
$view->render('file-permissions.phtml');
}
}


$form = new InstallForm();
if (!empty($_POST)) {
if ($form->isValid($_POST)) {
try {
$db = new Db($_POST['dbName'], $_POST['dbHost'], $_POST['dbUsername'], $_POST['dbPass']);

$phinxConfigTemplate = file_get_contents($configPath . '/phinx.local-example.yml');
if (!$phinxConfigTemplate) {
throw new Exception('Cannot load phinx config template.');
}
if (!file_put_contents($configPath . '/phinx.local.yml', str_replace([
'host: db',
'name: dbjr',
'user: root',
'pass: pass',
], [
sprintf('host: %s', $_POST['dbHost']),
sprintf('name: %s', $_POST['dbName']),
sprintf('user: %s', $_POST['dbUsername']),
sprintf('pass: %s', $_POST['dbPass']),
], $phinxConfigTemplate))
) {
throw new Exception('Cannot write phinx config.');
}
if (!empty($_POST) && $form->isValid($_POST)) {
try {
$db = new Db($_POST['dbName'], $_POST['dbHost'], $_POST['dbUsername'], $_POST['dbPass']);

$phinxMigrate = new Service_PhinxMigrate('production');

$db->initDb(
$sqlPath,
$_POST['adminName'],
$_POST['adminEmail'],
$_POST['adminPassword'],
$_POST['locale'],
function () use ($phinxMigrate) {
$phinxMigrate->run();
}
);

$config = new Config(new Zend_Config_Writer_Ini(), $configPath);
$baseUrl = preg_replace(
sprintf('#^%s#', $_SERVER['DOCUMENT_ROOT']),
'',
realpath(dirname(__FILE__) . '/../')
);
$config->writeConfigLocalIni(
$baseUrl === '' ? '/' : $baseUrl,
[
'host' => $_POST['dbHost'],
'name' => $_POST['dbName'],
'userName' => $_POST['dbUsername'],
'password' => $_POST['dbPass']
],
[
'fromAddress' => $_POST['emailFromAddress'],
'fromName' => $_POST['emailFromName'],
'replyToAddress' => $_POST['emailReplyToAddress'],
'replyToName' => $_POST['emailReplyToName'],
'smtp' => [
'auth' => $_POST['emailSmtpAuth'],
'port' => $_POST['emailSmtpPort'],
'ssl' => $_POST['emailSmtpSsl'],
'host' => $_POST['emailSmtpHost'],
'password' => $_POST['emailSmtpPass'],
'username' => $_POST['emailSmtpUserName'],
]
],
'xx',
$_POST['cronKey'],
mb_substr(hash('sha256', random_bytes(20)), 32),
$_POST['googleId'],
$_POST['googleSecret'],
$_POST['facebookId'],
$_POST['facebookSecret'],
$_POST['vimeoAccessToken']
);

$frontController = Zend_Controller_Front::getInstance();
$frontController->setBaseUrl($baseUrl);
$frontController->getRouter()->addDefaultRoutes();
$view->render('step-3.phtml');
} catch (PDOException $e) {
$form->getElement('dbHost')->addError('The specified db credentials are invalid.');
$form->getElement('dbName')->addError('The specified db credentials are invalid.');
$form->getElement('dbUsername')->addError('The specified db credentials are invalid.');
$form->getElement('dbPass')->addError('The specified db credentials are invalid.');
} catch (Exception $e) {
$form->getElement('dbUsername')->addError('Cannot complete db initialization. Please check access rights.');
$phinxConfigTemplate = file_get_contents($configPath . '/phinx.local-example.yml');
if (!$phinxConfigTemplate) {
throw new Exception('Cannot load phinx config template.');
}
if (!file_put_contents($configPath . '/phinx.local.yml', str_replace([
'host: db',
'name: dbjr',
'user: root',
'pass: pass',
], [
sprintf('host: %s', $_POST['dbHost']),
sprintf('name: %s', $_POST['dbName']),
sprintf('user: %s', $_POST['dbUsername']),
sprintf('pass: %s', $_POST['dbPass']),
], $phinxConfigTemplate))
) {
throw new Exception('Cannot write phinx config.');
}

$phinxMigrate = new Service_PhinxMigrate('production');

$consultationId = $db->initDb(
$sqlPath,
$_POST['adminName'],
$_POST['adminEmail'],
$_POST['adminPassword'],
$_POST['locale'],
static function () use ($phinxMigrate) {
$phinxMigrate->run();
}
);
$fileSystem->createFolders(['/www/media/consultations/' . $consultationId]);

$config = new Config(new Zend_Config_Writer_Ini(), $configPath);
$baseUrl = preg_replace(
sprintf('#^%s#', $_SERVER['DOCUMENT_ROOT']),
'',
realpath($rootDir)
);
$config->writeConfigLocalIni(
$baseUrl === '' ? '/' : $baseUrl,
[
'host' => $_POST['dbHost'],
'name' => $_POST['dbName'],
'userName' => $_POST['dbUsername'],
'password' => $_POST['dbPass']
],
[
'fromAddress' => $_POST['emailFromAddress'],
'fromName' => $_POST['emailFromName'],
'replyToAddress' => $_POST['emailReplyToAddress'],
'replyToName' => $_POST['emailReplyToName'],
'smtp' => [
'auth' => $_POST['emailSmtpAuth'],
'port' => $_POST['emailSmtpPort'],
'ssl' => $_POST['emailSmtpSsl'],
'host' => $_POST['emailSmtpHost'],
'password' => $_POST['emailSmtpPass'],
'username' => $_POST['emailSmtpUserName'],
]
],
'xx',
$_POST['cronKey'],
mb_substr(hash('sha256', random_bytes(20)), 32),
$_POST['googleId'],
$_POST['googleSecret'],
$_POST['facebookId'],
$_POST['facebookSecret'],
$_POST['vimeoAccessToken']
);

$frontController = Zend_Controller_Front::getInstance();
$frontController->setBaseUrl($baseUrl);
$frontController->getRouter()->addDefaultRoutes();
$view->render('step-3.phtml');
} catch (PDOException $e) {
$form->getElement('dbHost')->addError('The specified db credentials are invalid.');
$form->getElement('dbName')->addError('The specified db credentials are invalid.');
$form->getElement('dbUsername')->addError('The specified db credentials are invalid.');
$form->getElement('dbPass')->addError('The specified db credentials are invalid.');
} catch (Exception $e) {
$form->getElement('dbUsername')->addError('Something went wrong, we could not create the installation.');
}
}

Expand Down
2 changes: 1 addition & 1 deletion install/src/Form/InstallForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ public function init()



$hash = $this->createElement('hash', 'csrf_token_instalation', ['salt' => 'unique']);
$hash = $this->createElement('hash', 'csrf_token_installation', ['salt' => 'unique']);
$hash
->setSalt(md5(mt_rand(1, 100000) . time()))
->setTimeout(360);
Expand Down
Loading

0 comments on commit f632de9

Please sign in to comment.