Base
This commit is contained in:
40
public/install/create_admin.php
Normal file
40
public/install/create_admin.php
Normal file
@ -0,0 +1,40 @@
|
||||
<?php
|
||||
include_once dirname(dirname(__DIR__)) . '/bootstrap/autoload.php';
|
||||
|
||||
sanitize();
|
||||
|
||||
if (post('name') != null) {
|
||||
$user = Model::factory(\Incoviba\common\User::class)->where('name', post('name'))->findOne();
|
||||
if ($user === false) {
|
||||
$user = Model::factory(\Incoviba\common\User::class)->create();
|
||||
$user->name = post('name');
|
||||
$user->password(post('password'));
|
||||
|
||||
$user->save();
|
||||
echo 'Created';
|
||||
}
|
||||
$role = Model::factory(\Incoviba\common\Role::class)->where('description', 'administrador')->findOne();
|
||||
if ($role == false) {
|
||||
$role = Model::factory(\Incoviba\common\Role::class)->create(['description' => 'administrador']);
|
||||
$role->save();
|
||||
}
|
||||
$usrRl = Model::factory(\Incoviba\common\UserRole::class)->where('user', $user->id)->where('role', $role->id)->findOne();
|
||||
if ($usrRl == false) {
|
||||
$usrRl = Model::factory(\Incoviba\common\UserRole::class)->create(['user' => $user->id, 'role' => $role->id]);
|
||||
$usrRl->save();
|
||||
}
|
||||
$perm = Model::factory(\Incoviba\common\Permission::class)->where('type', 2)->where('ext_id', $role->id)->where('all', 1)->where('access', 1)->findOne();
|
||||
if ($perm == false) {
|
||||
$perm = Model::factory(\Incoviba\common\Permission::class)->create([
|
||||
'type' => 2,
|
||||
'ext_id' => $role->id,
|
||||
'all' => 1,
|
||||
'access' => 1
|
||||
]);
|
||||
$perm->save();
|
||||
}
|
||||
header('Location: next_step.php?step=create_admin');
|
||||
} else {
|
||||
echo view('install.admin');
|
||||
}
|
||||
?>
|
36
public/install/create_guest.php
Normal file
36
public/install/create_guest.php
Normal file
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
include_once dirname(dirname(__DIR__)) . '/bootstrap/autoload.php';
|
||||
|
||||
sanitize();
|
||||
|
||||
$user = Model::factory(\Incoviba\common\User::class)->where('name', 'guest')->findOne();
|
||||
if ($user === false) {
|
||||
$user = Model::factory(\Incoviba\common\User::class)->create(['name' => 'guest']);
|
||||
$user->save();
|
||||
}
|
||||
$role = Model::factory(\Incoviba\common\Role::class)->where('description', 'guest')->findOne();
|
||||
if ($role === false) {
|
||||
$role = Model::factory(\Incoviba\common\Role::class)->create(['description' => 'guest']);
|
||||
$role->save();
|
||||
}
|
||||
$usrRl = Model::factory(\Incoviba\common\UserRole::class)->where('user', $user->id)->where('role', $role->id)->findOne();
|
||||
if ($usrRl === false) {
|
||||
$usrRl = Model::factory(\Incoviba\common\UserRole::class)->create(['user' => $user->id, 'role' => $role->id]);
|
||||
$usrRl->save();
|
||||
}
|
||||
$locations = Model::factory(\Incoviba\common\Location::class)->where('controller', 'auth')->findMany();
|
||||
foreach ($locations as $location) {
|
||||
$permission = Model::factory(\Incoviba\common\Permission::class)->where('type', 2)->where('ext_id', $role->id)->where('access', 1)->where('location', $location->id)->findOne();
|
||||
if ($permission === false) {
|
||||
$permission = Model::factory(\Incoviba\common\Permission::class)->create([
|
||||
'type' => 2,
|
||||
'ext_id' => $role->id,
|
||||
'access' => 1,
|
||||
'location' => $location->id
|
||||
]);
|
||||
$permission->save();
|
||||
}
|
||||
}
|
||||
|
||||
header('Location: next_step.php?step=create_guest');
|
||||
?>
|
128
public/install/create_user_base.php
Normal file
128
public/install/create_user_base.php
Normal file
@ -0,0 +1,128 @@
|
||||
<?php
|
||||
include_once dirname(dirname(__DIR__)) . '/bootstrap/autoload.php';
|
||||
|
||||
sanitize();
|
||||
|
||||
$models = [
|
||||
'User',
|
||||
'Location',
|
||||
'Role',
|
||||
'UserRole',
|
||||
'Permission',
|
||||
'Auth'
|
||||
];
|
||||
|
||||
$location = realpath(dirname(dirname(__DIR__)) . '/src/common');
|
||||
|
||||
function extractColumn($table, $info, &$keys, &$foreigns) {
|
||||
$column = [];
|
||||
switch ($info[0]) {
|
||||
case 'string';
|
||||
$column['type'] = 'VARCHAR';
|
||||
break;
|
||||
default:
|
||||
$column['type'] = strtoupper($info[0]);
|
||||
}
|
||||
$column['name'] = '`' . str_replace('$', '', $info[1]) . '`';
|
||||
if ($column['name'] == '`id`') {
|
||||
$column['attr']['unsigned'] = 'UNSIGNED';
|
||||
$column['attr']['autoincrement'] = 'AUTO_INCREMENT';
|
||||
$keys []= $column['name'];
|
||||
}
|
||||
array_shift($info);
|
||||
array_shift($info);
|
||||
foreach ($info as $data) {
|
||||
if (strpos($data, '=') !== false) {
|
||||
list($attr, $val) = explode('=', $data);
|
||||
switch ($attr) {
|
||||
case 'length':
|
||||
$column['type'] .= '(' . $val . ')';
|
||||
break;
|
||||
case 'foreign':
|
||||
list($ftable, $fk) = explode('.', $val);
|
||||
$foreigns []= 'fk_' . $table . '_' . $ftable . ' FOREIGN KEY (' . $column['name'] . ') REFERENCES ' . $ftable . '(' . $fk . ')';
|
||||
$column['attr']['unsigned'] = 'UNSIGNED';
|
||||
break;
|
||||
default:
|
||||
$column['attr'][$attr] = strtoupper($attr . ' ' . $val);
|
||||
}
|
||||
} else {
|
||||
$column['attr'][$data] = strtoupper($data);
|
||||
}
|
||||
}
|
||||
|
||||
if ($column['type'] == 'VARCHAR') {
|
||||
$column['type'] .= '(255)';
|
||||
}
|
||||
if (!isset($column['attr']['null'])) {
|
||||
$column['attr']['null'] = 'NOT NULL';
|
||||
}
|
||||
|
||||
$attrs = ['unsigned', 'zerofill', 'null', 'default', 'autoincrement'];
|
||||
$reordered = [];
|
||||
foreach ($attrs as $attr) {
|
||||
if (isset($column['attr'][$attr])) {
|
||||
$reordered[$attr] = $column['attr'][$attr];
|
||||
}
|
||||
}
|
||||
$column['attr'] = $reordered;
|
||||
|
||||
return $column;
|
||||
}
|
||||
function columnQuery($column) {
|
||||
$q = $column['name'] . ' ' . $column['type'];
|
||||
foreach ($column['attr'] as $attr) {
|
||||
$q .= ' ' . $attr;
|
||||
}
|
||||
return $q;
|
||||
}
|
||||
function tableQuery($table, $columns, $keys, $foreigns) {
|
||||
$q = 'CREATE TABLE `' . $table . '` (' . implode(', ', $columns);
|
||||
if (count($keys) > 0) {
|
||||
$q .= ', PRIMARY KEY (' . implode(', ', $keys) . ')';
|
||||
}
|
||||
if (count($foreigns) > 0) {
|
||||
$q .= ', CONSTRAINT ' . implode(', CONSTRAINT ', $foreigns);
|
||||
}
|
||||
$q .= ') ENGINE=InnoDB DEFAULT CHARSET=utf8;';
|
||||
|
||||
return $q;
|
||||
}
|
||||
|
||||
$cnt = 0;
|
||||
$queries = [];
|
||||
foreach ($models as $model) {
|
||||
$class = '\\Incoviba\\common\\' . $model;
|
||||
$ref = new ReflectionClass($class);
|
||||
|
||||
$table = $ref->getProperty('_table')->getValue();
|
||||
$comments = explode(PHP_EOL, $ref->getDocComment());
|
||||
|
||||
$columns = [];
|
||||
$keys = [];
|
||||
$foreigns = [];
|
||||
foreach ($comments as $comment) {
|
||||
if (strpos($comment, '@property') === false) {
|
||||
continue;
|
||||
}
|
||||
$info = explode(' ', substr($comment, strpos($comment, '@property') + strlen('@property ')));
|
||||
$column = extractColumn($table, $info, $keys, $foreigns);
|
||||
|
||||
$columns []= columnQuery($column);
|
||||
}
|
||||
|
||||
$q = tableQuery($table, $columns, $keys, $foreigns);
|
||||
$queries []= $q;
|
||||
}
|
||||
try {
|
||||
\ORM::getDb()->beginTransaction();
|
||||
foreach ($queries as $q) {
|
||||
\ORM::getDb()->query($q);
|
||||
}
|
||||
\ORM::getDb()->commit();
|
||||
header('Location: next_step.php?step=create_user_base');
|
||||
} catch (Exception $e) {
|
||||
\ORM::getDb()->rollBack();
|
||||
throw $e;
|
||||
}
|
||||
?>
|
7
public/install/end_install.php
Normal file
7
public/install/end_install.php
Normal file
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
include_once dirname(dirname(__DIR__)) . '/bootstrap/autoload.php';
|
||||
|
||||
sanitize();
|
||||
|
||||
echo view('install.end');
|
||||
?>
|
7
public/install/index.php
Normal file
7
public/install/index.php
Normal file
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
include_once dirname(dirname(__DIR__)) . '/bootstrap/autoload.php';
|
||||
|
||||
sanitize();
|
||||
|
||||
echo view('install.start');
|
||||
?>
|
39
public/install/log_locations.php
Normal file
39
public/install/log_locations.php
Normal file
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
include_once dirname(dirname(__DIR__)) . '/bootstrap/autoload.php';
|
||||
|
||||
$controllers = glob(config('locations.controllers') . '/*.php');
|
||||
$cnt = 0;
|
||||
$errors = 0;
|
||||
foreach ($controllers as $controller) {
|
||||
$info = pathinfo($controller);
|
||||
$name = $info['filename'];
|
||||
$controller = '' . Stringy\Stringy::create($name)->underscored();
|
||||
$class = '\\App\\Controller\\' . $name;
|
||||
$ref = new ReflectionClass($class);
|
||||
$methods = $ref->getMethods(ReflectionMethod::IS_STATIC);
|
||||
|
||||
try {
|
||||
foreach ($methods as $method) {
|
||||
$data = [
|
||||
'controller' => $controller,
|
||||
'action' => $method->name
|
||||
];
|
||||
$location = Model::factory(\Incoviba\common\Location::class)->where('controller', $controller)->where('action', $method->name)->findOne();
|
||||
if ($location !== false) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$location = Model::factory(\Incoviba\common\Location::class)->create($data);
|
||||
$location->save();
|
||||
$cnt ++;
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
d($e);
|
||||
$errors ++;
|
||||
}
|
||||
}
|
||||
|
||||
if ($errors == 0) {
|
||||
header('Location: next_step.php?step=log_locations');
|
||||
}
|
||||
?>
|
20
public/install/next_step.php
Normal file
20
public/install/next_step.php
Normal file
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
include_once dirname(dirname(__DIR__)) . '/bootstrap/autoload.php';
|
||||
|
||||
sanitize();
|
||||
|
||||
$steps = [
|
||||
'create_user_base',
|
||||
'log_locations',
|
||||
'create_guest',
|
||||
'create_admin',
|
||||
'end_install'
|
||||
];
|
||||
|
||||
if (get('step') == null) {
|
||||
$next = $steps[0];
|
||||
} else {
|
||||
$next = $steps[array_search(get('step'), $steps) + 1];
|
||||
}
|
||||
header('Location: ' . $next . '.php');
|
||||
?>
|
Reference in New Issue
Block a user