From 1ba380272703f5e4fe04aa0be277ec61684022d6 Mon Sep 17 00:00:00 2001 From: Jean-Christian Denis Date: Thu, 9 Sep 2021 22:30:34 +0200 Subject: [PATCH] rewrite admin page --- index.php | 469 +++++++++++++++++++++++------------------------------- 1 file changed, 196 insertions(+), 273 deletions(-) diff --git a/index.php b/index.php index 896661c..4ee29b8 100644 --- a/index.php +++ b/index.php @@ -11,329 +11,252 @@ * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ -if (!defined('DC_CONTEXT_ADMIN')){return;} +if (!defined('DC_CONTEXT_ADMIN')) { + return null; +} dcPage::check('admin'); -$s = $core->blog->settings->noodles; -$msg = isset($_POST['done']) ? __('Configuration successfully updated') : ''; -$tab = isset($_REQUEST['tab']) ? $_REQUEST['tab'] : 'blocs'; -$img_green = '%s'; -$img_red = '%s'; +include dirname(__FILE__) . '/inc/_default_noodles.php'; -include dirname(__FILE__).'/inc/_default_noodles.php'; +$s = $core->blog->settings->noodles; $__noodles = noodles::decode($s->noodles_object); - -if ($__noodles->isEmpty()) -{ +if ($__noodles->isEmpty()) { $__noodles = $__default_noodles; -} -else -{ +} else { $default_noodles_array = $__default_noodles->noodles(); - foreach($default_noodles_array AS $id => $noodle) - { - if ($__noodles->exists($id)) continue; - $__noodles->{$id} = $noodle; + foreach($default_noodles_array AS $id => $noodle) { + if ($__noodles->exists($id)) { + continue; + } + $__noodles->set($id, $noodle); } } - -if (!$s->noodles_active) -{ - $tab = 'settings'; +$public_path = path::real($core->blog->public_path); +if (!is_dir($public_path) || !is_writable($public_path)) { + $public_path = false; } +$default_images = files::scandir(dirname(__FILE__) . '/default-templates/img/'); +if (!is_array($default_images)) { + $default_images = []; +} +$default_image = $s->noodles_image; -$default_avatars_images = files::scandir(dirname(__FILE__).'/default-templates/img/'); -$avatar_paths = noodlesLibImagePath::getArray($core,'noodles'); - -$combo_active = array( - __('no') => 0, - __('yes') => 1 -); -$combo_place = array( +$combo_place = [ __('Begin') => 'prepend', __('End') => 'append', __('Before') => 'before', __('After') => 'after' -); -$combo_rating = array( - 'G'=>'g', - 'PG'=>'pg', - 'R'=>'r', - 'X'=>'x' -); -$combo_size = array( - '16px'=>16, - '24px'=>24, - '32px'=>32, - '48px'=>48, - '56px'=>56, - '64px'=>64, - '92px'=>92, - '128px'=>128, - '256px'=>256 -); +]; +$combo_rating = [ + 'G' => 'g', + 'PG' => 'pg', + 'R' => 'r', + 'X' => 'x' +]; +$combo_size = [ + '16px' => 16, + '24px' => 24, + '32px' => 32, + '48px' => 48, + '56px' => 56, + '64px' => 64, + '92px' => 92, + '128px' => 128, + '256px' => 256 +]; -if (!empty($_POST['save']) && $tab == 'settings') -{ - try - { - $s->put('noodles_active',$_POST['noodles_active'],'boolean'); +if (!empty($_POST['save'])) { + try { + $public_file = $public_path . '/noodles-default-image.png'; + $s->put('noodles_active', !empty($_POST['noodles_active']), 'boolean'); - # Destination image according to noodlesLibImagePath() - $dest_file = DC_ROOT.'/'.$s->public_path.'/noodles-default-image.png'; - - # user upload image - if ($_POST['noodles_image'] == 'user') - { - if (2 == $_FILES['noodlesuserfile']['error']) - { + // user upload image + if ($_POST['noodles_image'] == 'user' && !empty($public_path)) { + if (2 == $_FILES['noodlesuserfile']['error']) { throw new Exception(__('Maximum file size exceeded')); } - if ($_FILES['noodlesuserfile']['type'] != 'image/x-png') - { - throw new Exception(__('Image must be in png format')); - } - if (0 != $_FILES['noodlesuserfile']['error']) - { + if (0 != $_FILES['noodlesuserfile']['error']) { throw new Exception(__('Something went wrong while download file')); } - if ($_FILES['noodlesuserfile']['type'] != 'image/x-png') - { + if (!in_array($_FILES['noodlesuserfile']['type'], ['image/png', 'image/x-png'])) { throw new Exception(__('Image must be in png format')); } - if (move_uploaded_file($_FILES['noodlesuserfile']['tmp_name'],$dest_file)) - { - $s->put('noodles_image',1,'boolean'); + if (move_uploaded_file($_FILES['noodlesuserfile']['tmp_name'], $public_file)) { + $s->put('noodles_image', 1, 'boolean'); + } else { + throw new Exception(__('Failed to save image.')); } - } - # Default gravatar.com avatar - elseif ($_POST['noodles_image'] == 'gravatar.com') - { - $s->put('noodles_image',0,'boolean'); - } - # existsing noodles image on blog - elseif ($_POST['noodles_image'] == 'existsing') - { - $s->put('noodles_image',1,'boolean'); - } - # noodles image - elseif (preg_match('/^gravatar-[0-9]+.png$/',$_POST['noodles_image'])) - { - $source = dirname(__FILE__).'/default-templates/img/'.$_POST['noodles_image']; - if (!file_exists($source)) - { + // Default gravatar.com avatar + } elseif ($_POST['noodles_image'] == 'gravatar.com') { + $s->put('noodles_image', 0, 'boolean'); + + // existsing noodles image on blog + } elseif ($_POST['noodles_image'] == 'existing') { + $s->put('noodles_image', 1, 'boolean'); + + // noodles image + } elseif (preg_match('/^gravatar-[0-9]+.png$/', $_POST['noodles_image']) && !empty($public_path)) { + $source = dirname(__FILE__) . '/default-templates/img/' . $_POST['noodles_image']; + if (!file_exists($source)) { throw new Exception(__('Something went wrong while search file')); } - if (file_put_contents($dest_file,file_get_contents($source))) - { - $s->put('noodles_image',1,'boolean'); + if (files::putContent($public_file, file_get_contents($source))) { + $s->put('noodles_image', 1, 'boolean'); } + + // Default gravatar.com avatar + } else { //if ($_POST['noodles_image'] == 'gravatar.com') { + $s->put('noodles_image', 0, 'boolean'); } - $core->blog->triggerBlog(); - http::redirect('plugin.php?p=noodles&tab=settings&done=1'); - } - catch (Exception $e) - { - $core->error->add($e->getMessage()); - } -} - -if (!empty($_POST['save']) && $tab == 'blocs' && !empty($_POST['noodle'])) -{ - try - { - foreach($_POST['noodle'] as $id => $bloc) - { - foreach($bloc as $k => $v) - { - $__noodles->{$id}->set($k,$v); - } + // behaviors + foreach($_POST['noodle'] as $id => $bloc) { + $__noodles->get($id) + ->set('active', !empty($bloc['active'])) + ->set('rating', $bloc['rating'] ?? 'g') + ->set('size', $bloc['size'] ?? '16') + ->set('css', $bloc['css'] ?? '') + ->set('target', $bloc['target'] ?? '') + ->set('place', $bloc['place'] ?? 'prepend') + ; } - $s->put('noodles_object',$__noodles->encode(),'string'); + $s->put('noodles_object', $__noodles->encode(), 'string'); $core->blog->triggerBlog(); - http::redirect('plugin.php?p=noodles&tab=blocs&done=1'); - } - catch (Exception $e) - { + dcPage::addSuccessNotice(__('Configuration succesfully updated')); + $core->adminurl->redirect('admin.plugin.noodles'); + } catch (Exception $e) { $core->error->add($e->getMessage()); } } -if (!empty($_POST['reset']) && $tab == 'blocs') -{ - try - { - $s->put('noodles_object','','string'); - $core->blog->triggerBlog(); - http::redirect('plugin.php?p=noodles&tab=blocs&done=1'); - } - catch (Exception $e) - { - $core->error->add($e->getMessage()); +echo '' . __('Noodles') . '' . +dcPage::breadcrumb([ + html::escapeHTML($core->blog->name) => '', + __('Noodles') => '', + __('Plugin configuration') => '' +]) . +dcPage::notices() . ' + +
+

' . sprintf(__('Configure "%s"'), __('Noodles')) . '

+

' . __('Activation') . '

+

' . form::checkbox('noodles_active', 1, $s->noodles_active) . ' +

+
+

' . __('Avatar') . '

+

' . __('Select default avatar to display on unknown users.') . '

'; + +if (!empty($public_path)) { + echo '
'; + sort($default_images); + $i = 0; + foreach($default_images AS $f) { + if (!preg_match('/gravatar-[0-9]+.png/', $f)) { + continue; + } + $i++; + $sz = getimagesize(dirname(__FILE__) . '/default-templates/img/' . $f); + $sz[2] = files::size(filesize(dirname(__FILE__) . '/default-templates/img/' . $f)); + + echo ' +
+

' . form::radio(['noodles_image', 'noodles_image_' . $i], $f) . ' +

+
+

+
+

' . $sz[0] . 'x' . $sz[1] . '
' . $sz[2] . '

+
+
'; } + echo '
'; } -echo -''. -''. -''.__('Noodles').''. -dcPage::jsLoad('js/_posts_list.js'). -dcPage::jsPageTabs($tab). -''. -''. -'

'.html::escapeHTML($core->blog->name).' › '.__('Noodles').'

'. - (!empty($msg) ? '

'.$msg.'

' : ''); +echo '
'; -# Blocs -if ($s->noodles_active) -{ - echo - '
'. - ''. - ''. - ''. - ''. - ''. - ''. - ''. - ''. - ''. - ''. - ''. - ''; +if (null !== ($default_image_path = noodlesLibImagePath::getPath($core, 'noodles'))) { + $sz = getimagesize($default_image_path); + $sz[2] = files::size(filesize($default_image_path)); - foreach($__noodles->noodles() as $noodle) - { - echo - ''. - ''. - ''. - ''. - ''. - ''. - ''. - ''. - ''. - ''. - ''; - } - echo - '
'.__('Name').''.__('Enable').''.__('Size').''.__('Rating').''.__('PHP').''.__('JS').''.__('JS target').''.__('JS place').''.__('Adjust avatar CSS').'
'.$noodle->name().''.form::combo(array('noodle['.$noodle->id().'][active]'),$combo_active,$noodle->active).''.form::combo(array('noodle['.$noodle->id().'][size]'),$combo_size,$noodle->size).''.form::combo(array('noodle['.$noodle->id().'][rating]'),$combo_rating,$noodle->rating).''.($noodle->hasPhpCallback() ? $img_green : $img_red).''.$img_green.''.form::field(array('noodle['.$noodle->id().'][target]'),20,255,$noodle->target).''.form::combo(array('noodle['.$noodle->id().'][place]'),$combo_place,$noodle->place).''. - form::textArea(array('noodle['.$noodle->id().'][css]'),50,2,$noodle->css). - ' .noodles-'.$noodle->id().'{}
'. - '

'. - form::hidden(array('p'),'noodles'). - form::hidden(array('tab'),'blocs'). - $core->formNonce(). - ' '. - '

'. - ''. - '
'; + echo ' +
+

' . form::radio(['noodles_image', 'public_image'], 'existing', !empty($default_image)) . ' +

+
+

+
+

' . $sz[0] . 'x' . $sz[1] . '
' . $sz[2] . '

+
+
'; } -# Settings -echo -'
'. -'
'. -'
'.__('Options').''. -'

'. +if (!empty($public_path)) { + echo ' +
+

' . form::radio(['noodles_image', 'upload_image'], 'user') . ' +

+

' . form::hidden(['MAX_FILE_SIZE'], 30000) . ' + +

' . __('Image must be in "png" format and have a maximum file size of 30Ko') . '

+
'; +} +echo ' +
+

'. form::radio(['noodles_image', 'com_image'], 'gravatar.com', empty($default_image)) . ' +

+
'; -'
'. -'
'.__('Default avatar').''. -''. -''; - -// By default use gravatar.com image -$default = '' == $s->noodles_image; - -echo -''. -''. -''; - -if (!$default) -{ - $exists = false; - - // then use theme image - if (file_exists($avatar_paths['theme']['dir'])) - { - $exists = $avatar_paths['theme']; - } - // then public image - elseif (file_exists($avatar_paths['public']['dir'])) - { - $exists = $avatar_paths['public']; - } - // then module - elseif (file_exists($avatar_paths['module']['dir'])) - { - $exists = $avatar_paths['module']; - } - if ($exists) - { - $sz = getimagesize($exists['dir']); - $sz[2] = files::size(filesize($exists['dir'])); - - echo - ''. - ''. - ''. - ''. - ''; - } +if (empty($public_path)) { + echo '

' . __('Public directory is not writable, you can not use custom gravatar.') . '

'; } -// noodles avatars -sort($default_avatars_images); -foreach($default_avatars_images AS $f) -{ - if (!preg_match('/gravatar-[0-9]+.png/',$f)) continue; - $sz = getimagesize(dirname(__FILE__).'/default-templates/img/'.$f); - $sz[2] = files::size(filesize(dirname(__FILE__).'/default-templates/img/'.$f)); +echo ' + +

' . __('Behaviors') . '

+
+
 '.__('Avatar').''.__('size').'
'.$sz[0].'x'.$sz[1].'
'.$sz[2].'
+ + + + + + + + +'; - echo - ''. - ''. - ''. - ''. - ''; +foreach($__noodles->noodles() as $noodle) { + echo ' + + + + + + + + + + + + '; } +echo ' +
'.__('Behavior').''.__('Size').''.__('Rating').''.__('PHP').''.__('JS').''.__('Target').''.__('Place').''.__('Adjust avatar CSS').'
'.$sz[0].'x'.$sz[1].'
'.$sz[2].'
' . form::checkbox(['noodle[' . $noodle->id() . '][active]', 'ck_' . $noodle->id()], 1, $noodle->active) . '' . form::combo(['noodle[' . $noodle->id() . '][size]'], $combo_size, $noodle->size) . '' . form::combo(['noodle[' . $noodle->id() . '][rating]'], $combo_rating, $noodle->rating) . '' . ($noodle->hasPhpCallback() ? + 'ok' : + 'nok' + ) . 'ok' . form::field(['noodle[' . $noodle->id() . '][target]'], 20, 255, $noodle->target) . '' . form::combo(['noodle[' . $noodle->id() . '][place]'], $combo_place, $noodle->place).'' . form::field(['noodle[' . $noodle->id() . '][css]'], 50, 255, $noodle->css).' .noodles-' . $noodle->id() . '{}
+

' . __('Target and Place are for javascript.') . '

+
-// user upload avatar -echo -''. -''.form::radio(array('noodles_image'),'user').''. -''.form::hidden(array('MAX_FILE_SIZE'),30000). - ' *'. -''. -''. -'

* '.__('Image must be in "png" format and have a maximum file size of 30Ko').'

'. -''. -'

'. -form::hidden(array('p'),'noodles'). -form::hidden(array('tab'),'settings'). -$core->formNonce(). -'

'. -''. -'
'; +

+ ' . +$core->formNonce() . '

+ -# Footer -echo -'
-

-noodles - '.$core->plugins->moduleInfo('noodles','version').'  -'.__('Noodles').' -

'; \ No newline at end of file