Skip to content

Commit

Permalink
add format js, css, html
Browse files Browse the repository at this point in the history
  • Loading branch information
linh0804 committed Nov 19, 2024
1 parent 9fecd18 commit d316925
Show file tree
Hide file tree
Showing 8 changed files with 552 additions and 513 deletions.
685 changes: 349 additions & 336 deletions .init.php

Large diffs are not rendered by default.

Empty file removed _init.php
Empty file.
235 changes: 130 additions & 105 deletions edit_api.php
Original file line number Diff line number Diff line change
@@ -1,105 +1,130 @@
<?php

define('ACCESS', true);

require '.init.php';

$data = [
'status' => false,
'message' => 'error'
];

if (!isset($_POST['requestApi'])) {
goto end_request;
}

if ($dir == null || $name == null || !is_file(processDirectory($dir . '/' . $name))) {
$data['message'] = 'Đường dẫn không tồn tại';
goto end_request;
}


if (!isFormatText($name) && !isFormatUnknown($name)) {
$data['message'] = 'Tập tin này không phải dạng văn bản';
goto end_request;
}

if (isset($_POST['format_php'])) {
$configFile = __DIR__ . '/.php-cs-fixer.dist.php';
$tempFile = __DIR__ . '/tmp/fixer.txt';
//$cacheFile = __DIR__ . '/tmp/.php-cs-fixer.cache';

$content = isset($_POST['content']) ? $_POST['content'] : '';
$data = array(
'format' => '',
'error' => 'Không thành công! Yêu cầu chạy "composer install"!'
);

if (!empty($content)) {
file_put_contents($tempFile, $content);

@chmod('vendor/bin/php-cs-fixer', 0775);
$result = exec("vendor/bin/php-cs-fixer fix {$tempFile} --config {$configFile}");

if ($result) {
$data['format'] = file_get_contents($tempFile);
$data['error'] = '';

@unlink($tempFile);
//@unlink($cacheFile);
}
}

goto end_request;
}


// luu file
$dir = processDirectory($dir);
$path = $dir . '/' . $name;

if (!isset($_POST['content'])) {
$data['message'] = 'Chưa nhập nội dung';
goto end_request;
}

$content = $_POST['content'];
$currentOwner = fileowner($path);

if (file_put_contents($path, $content) !== false) {
// fix owner
@chown($path, $currentOwner);

$data['status'] = true;
$data['message'] = 'Lưu lại thành công';

$checkPHP = isset($_POST['check']) ? (bool) $_POST['check'] : false;

if ($checkPHP) {
$error_syntax = 'Lưu thành công! Không thể kiểm tra lỗi';
$isExecute = isFunctionExecEnable();

if ($isExecute) {
@exec(getPathPHP() . ' -c -f -l ' . $path, $output, $value);

if ($value == -1) {
} elseif ($value == 255 || count($output) == 3) {
$error_syntax = 'Lưu thành công! Có lỗi!';

$data['error'] = $output[1];
} else {
$error_syntax = 'Lưu thành công! Không có lỗi';
}
}

$data['message'] = $error_syntax;
}
} else {
$data['message'] = 'Lưu lại thất bại';
}

// response
end_request:
@ob_end_clean();
header('Content-Type: application/json; charset=utf-8');
echo json_encode($data);
<?php

define('ACCESS', true);

require '.init.php';

$data = [
'status' => false,
'message' => 'error'
];

if (!isset($_POST['requestApi'])) {
goto end_request;
}

if ($dir == null || $name == null || !is_file(processDirectory($dir . '/' . $name))) {
$data['message'] = 'Đường dẫn không tồn tại';
goto end_request;
}

if (!isFormatText($name) && !isFormatUnknown($name)) {
$data['message'] = 'Tập tin này không phải dạng văn bản';
goto end_request;
}

// thông tin file
$dir = processDirectory($dir);
$path = $dir . '/' . $name;

$content = isset($_POST['content']) ? $_POST['content'] : '';

if (isset($_POST['format'])) {
$formatType = trim($_POST['format']);

switch ($formatType) {
case 'php':
$configFile = __DIR__ . '/.php-cs-fixer.dist.php';
$tempFile = __DIR__ . '/tmp/fixer.txt';
$data = [
'format' => '',
'error' => 'Không thành công! Yêu cầu chạy "composer install"!'
];

if (!empty($content)) {
file_put_contents($tempFile, $content);

@chmod('vendor/bin/php-cs-fixer', 0775);
$result = exec("vendor/bin/php-cs-fixer fix {$tempFile} --config {$configFile}");

if ($result) {
$data['format'] = file_get_contents($tempFile);
$data['error'] = '';

@unlink($tempFile);
}
}
break;

case 'js':
case 'html':
case 'ts':
case 'css':
case 'scss':
case 'json':
case 'yaml':
$opt = [
'--print-width=1000000',
'--quote-props=preserve'
];
$res = runCommand('source ~/.bashrc; prettier ' . implode(' ', $opt) . ' ' . $path);
$data['format'] = $res['out'];
$data['error'] = '';

break;

default:
$data['format'] = $content;
$data['error'] = '';
}

goto end_request;
}


// luu file
if (!isset($_POST['content'])) {
$data['message'] = 'Chưa nhập nội dung';
goto end_request;
}

$content = $_POST['content'];
$currentOwner = fileowner($path);

if (file_put_contents($path, $content) !== false) {
// fix owner
@chown($path, $currentOwner);

$data['status'] = true;
$data['message'] = 'Lưu lại thành công';

$checkPHP = isset($_POST['check']) ? (bool) $_POST['check'] : false;

if ($checkPHP) {
$error_syntax = 'Lưu thành công! Không thể kiểm tra lỗi';
$isExecute = isFunctionExecEnable();

if ($isExecute) {
@exec(getPathPHP() . ' -c -f -l ' . $path, $output, $value);

if ($value == -1) {
} elseif ($value == 255 || count($output) == 3) {
$error_syntax = 'Lưu thành công! Có lỗi!';

$data['error'] = $output[1];
} else {
$error_syntax = 'Lưu thành công! Không có lỗi';
}
}

$data['message'] = $error_syntax;
}
} else {
$data['message'] = 'Lưu lại thất bại';
}

// response
end_request:
@ob_end_clean();
header('Content-Type: application/json; charset=utf-8');
echo json_encode($data);
16 changes: 9 additions & 7 deletions edit_code.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,14 @@

// <input type="checkbox" checked="checked" id="code_readonly" /> ReadOnly
echo '</select>
<span style="float: right">
<button class="button" id="code_format">Format</button>
<input type="checkbox" id="code_wrap" /> Wrap
</span>
</div>
<hr/>
<span style="float: right">';
if (ableFormatCode($fileExt)) {
echo '<button class="button" id="code_format">Format</button> ';
}
echo '<input type="checkbox" id="code_wrap" /> Wrap
</span>
</div>
<hr/>
</div>
<form id="code_form" action="javascript:void(0)">
Expand Down Expand Up @@ -201,7 +203,7 @@ function save() {
var data = new FormData();
data.append("requestApi", 1);
data.append("format_php", 1);
data.append("format", "' . $fileExt . '");
data.append("content", editor.state.doc.toString());
fetch("'. $actionEdit .'", {
Expand Down
8 changes: 4 additions & 4 deletions edit_text.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@
<form action="javascript:void(0)" id="code_form" method="post">
<span class="bull">&bull; </span>Nội dung:
<div style="display: inline-block; float: right">
<input type="button" id="code_highlight" value="Format" />
<input type="checkbox" id="code_wrap" /> Wrap
<div style="display: inline-block; float: right">'
. (ableFormatCode($file->getExtension()) ? '<input type="button" id="code_highlight" value="Format"> ' : '')
. '<input type="checkbox" id="code_wrap" /> Wrap
</div>
<div class="parent_box_edit">
Expand Down Expand Up @@ -191,7 +191,7 @@ function save() {

var data = new FormData();
data.append("requestApi", 1);
data.append("format_php", 1);
data.append("format", "<?= $file->getExtension() ?>");
data.append("content", editorElement.value);

fetch("<?= $actionEdit ?>", {
Expand Down
1 change: 1 addition & 0 deletions header.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<link rel="icon" type="image/x-icon" href="icon/icon.ico" />
<link rel="shortcut icon" type="image/x-icon" href="icon/icon.ico" />

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js" integrity="sha512-v2CJ7UaYy4JwqLDIrZUI/4hqeoQieOmAZNXBeQyjo21dadnwR+8ZaIJVT8EE2iyI61OV8e6M8PP2/4hpQINQ/g==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="<?= asset('js/script.js') ?>" defer></script>
</head>

Expand Down
15 changes: 7 additions & 8 deletions js/checkbox.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
function onCheckItem()
{
for (let i = 0; i < document.form.elements.length; ++i) {
if (document.form.elements[i].type === 'checkbox') {
document.form.elements[i].checked = document.form.all.checked === true;
}
}
}
function onCheckItem() {
for (let i = 0; i < document.form.elements.length; ++i) {
if (document.form.elements[i].type === "checkbox") {
document.form.elements[i].checked = document.form.all.checked === true;
}
}
}
Loading

0 comments on commit d316925

Please sign in to comment.