Find this useful? Enter your email to receive occasional updates for securing PHP code.
Signing you up...
Thank you for signing up!
PHP Decode
<?php namespace Widget\Metas\Category; use Typecho\Common; use Typecho\Db\Exception; use..
Decoded Output download
<?php
namespace Widget\Metas\Category;
use Typecho\Common;
use Typecho\Db\Exception;
use Typecho\Validate;
use Typecho\Widget\Helper\Form;
use Widget\Base\Metas;
use Widget\ActionInterface;
use Widget\Metas\EditTrait;
use Widget\Notice;
if (!defined('__TYPECHO_ROOT_DIR__')) {
exit;
}
/**
*
*
* @category typecho
* @package Widget
* @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
* @license GNU General Public License 2.0
*/
class Edit extends Metas implements ActionInterface
{
use EditTrait;
/**
*
* @throws \Exception
*/
public function execute()
{
/** */
$this->user->pass('editor');
}
/**
*
*
* @param integer $mid
* @return boolean
* @throws Exception
*/
public function categoryExists(int $mid): bool
{
$category = $this->db->fetchRow($this->db->select()
->from('table.metas')
->where('type = ?', 'category')
->where('mid = ?', $mid)->limit(1));
return isset($category);
}
/**
*
*
* @param string $name
* @return boolean
* @throws Exception
*/
public function nameExists(string $name): bool
{
$select = $this->db->select()
->from('table.metas')
->where('type = ?', 'category')
->where('name = ?', $name)
->limit(1);
if ($this->request->is('mid')) {
$select->where('mid <> ?', $this->request->get('mid'));
}
$category = $this->db->fetchRow($select);
return !$category;
}
/**
*
*
* @param string $name
* @return boolean
* @throws Exception
*/
public function nameToSlug(string $name): bool
{
if (empty($this->request->slug)) {
$slug = Common::slugName($name);
if (empty($slug) || !$this->slugExists($name)) {
return false;
}
}
return true;
}
/**
*
*
* @param string $slug
* @return boolean
* @throws Exception
*/
public function slugExists(string $slug): bool
{
$select = $this->db->select()
->from('table.metas')
->where('type = ?', 'category')
->where('slug = ?', Common::slugName($slug))
->limit(1);
if ($this->request->is('mid')) {
$select->where('mid <> ?', $this->request->get('mid'));
}
$category = $this->db->fetchRow($select);
return !$category;
}
/**
*
*
* @throws Exception
*/
public function insertCategory()
{
if ($this->form('insert')->validate()) {
$this->response->goBack();
}
/** */
$category = $this->request->from('name', 'slug', 'description', 'parent');
$category['slug'] = Common::slugName(Common::strBy($category['slug'] ?? null, $category['name']));
$category['type'] = 'category';
$category['order'] = $this->getMaxOrder('category', $category['parent']) + 1;
/** */
$category['mid'] = $this->insert($category);
$this->push($category);
/** */
Notice::alloc()->highlight($this->theId);
/** */
Notice::alloc()->set(
_t(' <a href="%s">%s</a> ', $this->permalink, $this->name),
'success'
);
/** */
$this->response->redirect(Common::url('manage-categories.php'
. ($category['parent'] ? '?parent=' . $category['parent'] : ''), $this->options->adminUrl));
}
/**
*
*
* @param string|null $action
* @return Form
* @throws Exception
*/
public function form(?string $action = null): Form
{
/** */
$form = new Form($this->security->getIndex('/action/metas-category-edit'), Form::POST_METHOD);
/** */
$name = new Form\Element\Text('name', null, null, _t('') . ' *');
$form->addInput($name);
/** */
$slug = new Form\Element\Text(
'slug',
null,
null,
_t(''),
_t(', , , .')
);
$form->addInput($slug);
/** */
$options = [0 => _t('')];
$parents = Rows::allocWithAlias(
'options',
($this->request->is('mid') ? 'ignore=' . $this->request->get('mid') : '')
);
while ($parents->next()) {
$options[$parents->mid] = str_repeat(' ', $parents->levels) . $parents->name;
}
$parent = new Form\Element\Select(
'parent',
$options,
$this->request->get('parent'),
_t(''),
_t('.')
);
$form->addInput($parent);
/** */
$description = new Form\Element\Textarea(
'description',
null,
null,
_t(''),
_t(', .')
);
$form->addInput($description);
/** */
$do = new Form\Element\Hidden('do');
$form->addInput($do);
/** */
$mid = new Form\Element\Hidden('mid');
$form->addInput($mid);
/** */
$submit = new Form\Element\Submit();
$submit->input->setAttribute('class', 'btn primary');
$form->addItem($submit);
if (isset($this->request->mid) && 'insert' != $action) {
/** */
$meta = $this->db->fetchRow($this->select()
->where('mid = ?', $this->request->mid)
->where('type = ?', 'category')->limit(1));
if (!$meta) {
$this->response->redirect(Common::url('manage-categories.php', $this->options->adminUrl));
}
$name->value($meta['name']);
$slug->value($meta['slug']);
$parent->value($meta['parent']);
$description->value($meta['description']);
$do->value('update');
$mid->value($meta['mid']);
$submit->value(_t(''));
$_action = 'update';
} else {
$do->value('insert');
$submit->value(_t(''));
$_action = 'insert';
}
if (empty($action)) {
$action = $_action;
}
/** */
if ('insert' == $action || 'update' == $action) {
$name->addRule('required', _t(''));
$name->addRule([$this, 'nameExists'], _t(''));
$name->addRule([$this, 'nameToSlug'], _t(''));
$name->addRule('xssCheck', _t(''));
$slug->addRule([$this, 'slugExists'], _t(''));
$slug->addRule('xssCheck', _t(''));
}
if ('update' == $action) {
$mid->addRule('required', _t(''));
$mid->addRule([$this, 'categoryExists'], _t(''));
}
return $form;
}
/**
*
*
* @throws Exception
*/
public function updateCategory()
{
if ($this->form('update')->validate()) {
$this->response->goBack();
}
/** */
$category = $this->request->from('name', 'slug', 'description', 'parent');
$category['mid'] = $this->request->get('mid');
$category['slug'] = Common::slugName(Common::strBy($category['slug'] ?? null, $category['name']));
$category['type'] = 'category';
$current = $this->db->fetchRow($this->select()->where('mid = ?', $category['mid']));
if ($current['parent'] != $category['parent']) {
$parent = $this->db->fetchRow($this->select()->where('mid = ?', $category['parent']));
if ($parent['mid'] == $category['mid']) {
$category['order'] = $parent['order'];
$this->update([
'parent' => $current['parent'],
'order' => $current['order']
], $this->db->sql()->where('mid = ?', $parent['mid']));
} else {
$category['order'] = $this->getMaxOrder('category', $category['parent']) + 1;
}
}
/** */
$this->update($category, $this->db->sql()->where('mid = ?', $this->request->filter('int')->get('mid')));
$this->push($category);
/** */
Notice::alloc()->highlight($this->theId);
/** */
Notice::alloc()
->set(_t(' <a href="%s">%s</a> ', $this->permalink, $this->name), 'success');
/** */
$this->response->redirect(Common::url('manage-categories.php'
. ($category['parent'] ? '?parent=' . $category['parent'] : ''), $this->options->adminUrl));
}
/**
*
*
* @access public
* @return void
* @throws Exception
*/
public function deleteCategory()
{
$categories = $this->request->filter('int')->getArray('mid');
$deleteCount = 0;
foreach ($categories as $category) {
$parent = $this->db->fetchObject($this->select()->where('mid = ?', $category))->parent;
if ($this->delete($this->db->sql()->where('mid = ?', $category))) {
$this->db->query($this->db->delete('table.relationships')->where('mid = ?', $category));
$this->update(['parent' => $parent], $this->db->sql()->where('parent = ?', $category));
$deleteCount++;
}
}
/** */
Notice::alloc()
->set($deleteCount > 0 ? _t('') : _t(''), $deleteCount > 0 ? 'success' : 'notice');
/** */
$this->response->goBack();
}
/**
*
* @throws Exception
*/
public function mergeCategory()
{
/** */
$validator = new Validate();
$validator->addRule('merge', 'required', _t(''));
$validator->addRule('merge', [$this, 'categoryExists'], _t(''));
if ($error = $validator->run($this->request->from('merge'))) {
Notice::alloc()->set($error, 'error');
$this->response->goBack();
}
$merge = $this->request->get('merge');
$categories = $this->request->filter('int')->getArray('mid');
if ($categories) {
$this->merge($merge, 'category', $categories);
/** */
Notice::alloc()->set(_t(''), 'success');
} else {
Notice::alloc()->set(_t(''));
}
/** */
$this->response->goBack();
}
/**
*
* @throws Exception
*/
public function sortCategory()
{
$categories = $this->request->filter('int')->getArray('mid');
if ($categories) {
$this->sort($categories, 'category');
}
if (!$this->request->isAjax()) {
/** */
$this->response->redirect(Common::url('manage-categories.php', $this->options->adminUrl));
} else {
$this->response->throwJson(['success' => 1, 'message' => _t('')]);
}
}
/**
*
*
* @throws Exception
*/
public function refreshCategory()
{
$categories = $this->request->filter('int')->getArray('mid');
if ($categories) {
foreach ($categories as $category) {
$this->refreshCountByTypeAndStatus($category, 'post');
}
Notice::alloc()->set(_t(''), 'success');
} else {
Notice::alloc()->set(_t(''));
}
/** */
$this->response->goBack();
}
/**
*
*
* @throws Exception
*/
public function defaultCategory()
{
/** */
$validator = new Validate();
$validator->addRule('mid', 'required', _t(''));
$validator->addRule('mid', [$this, 'categoryExists'], _t(''));
if ($error = $validator->run($this->request->from('mid'))) {
Notice::alloc()->set($error, 'error');
} else {
$this->db->query($this->db->update('table.options')
->rows(['value' => $this->request->get('mid')])
->where('name = ?', 'defaultCategory'));
$this->db->fetchRow($this->select()->where('mid = ?', $this->request->get('mid'))
->where('type = ?', 'category')->limit(1), [$this, 'push']);
/** */
Notice::alloc()->highlight($this->theId);
/** */
Notice::alloc()->set(
_t('<a href="%s">%s</a> ', $this->permalink, $this->name),
'success'
);
}
/** */
$this->response->redirect(Common::url('manage-categories.php', $this->options->adminUrl));
}
/**
*
*
* @return string|null
* @throws \Typecho\Widget\Exception|Exception
*/
public function getMenuTitle(): ?string
{
if ($this->request->is('mid')) {
$category = $this->db->fetchRow($this->select()
->where('type = ? AND mid = ?', 'category', $this->request->filter('int')->get('mid')));
if (!empty($category)) {
return _t(' %s', $category['name']);
}
}
if ($this->request->is('parent')) {
$category = $this->db->fetchRow($this->select()
->where('type = ? AND mid = ?', 'category', $this->request->filter('int')->get('parent')));
if (!empty($category)) {
return _t(' %s ', $category['name']);
}
} else {
return null;
}
throw new \Typecho\Widget\Exception(_t(''), 404);
}
/**
*
*
* @access public
* @return void
* @throws Exception
*/
public function action()
{
$this->security->protect();
$this->on($this->request->is('do=insert'))->insertCategory();
$this->on($this->request->is('do=update'))->updateCategory();
$this->on($this->request->is('do=delete'))->deleteCategory();
$this->on($this->request->is('do=merge'))->mergeCategory();
$this->on($this->request->is('do=sort'))->sortCategory();
$this->on($this->request->is('do=refresh'))->refreshCategory();
$this->on($this->request->is('do=default'))->defaultCategory();
$this->response->redirect($this->options->adminUrl);
}
}
?>
Did this file decode correctly?
Original Code
<?php
namespace Widget\Metas\Category;
use Typecho\Common;
use Typecho\Db\Exception;
use Typecho\Validate;
use Typecho\Widget\Helper\Form;
use Widget\Base\Metas;
use Widget\ActionInterface;
use Widget\Metas\EditTrait;
use Widget\Notice;
if (!defined('__TYPECHO_ROOT_DIR__')) {
exit;
}
/**
*
*
* @category typecho
* @package Widget
* @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
* @license GNU General Public License 2.0
*/
class Edit extends Metas implements ActionInterface
{
use EditTrait;
/**
*
* @throws \Exception
*/
public function execute()
{
/** */
$this->user->pass('editor');
}
/**
*
*
* @param integer $mid
* @return boolean
* @throws Exception
*/
public function categoryExists(int $mid): bool
{
$category = $this->db->fetchRow($this->db->select()
->from('table.metas')
->where('type = ?', 'category')
->where('mid = ?', $mid)->limit(1));
return isset($category);
}
/**
*
*
* @param string $name
* @return boolean
* @throws Exception
*/
public function nameExists(string $name): bool
{
$select = $this->db->select()
->from('table.metas')
->where('type = ?', 'category')
->where('name = ?', $name)
->limit(1);
if ($this->request->is('mid')) {
$select->where('mid <> ?', $this->request->get('mid'));
}
$category = $this->db->fetchRow($select);
return !$category;
}
/**
*
*
* @param string $name
* @return boolean
* @throws Exception
*/
public function nameToSlug(string $name): bool
{
if (empty($this->request->slug)) {
$slug = Common::slugName($name);
if (empty($slug) || !$this->slugExists($name)) {
return false;
}
}
return true;
}
/**
*
*
* @param string $slug
* @return boolean
* @throws Exception
*/
public function slugExists(string $slug): bool
{
$select = $this->db->select()
->from('table.metas')
->where('type = ?', 'category')
->where('slug = ?', Common::slugName($slug))
->limit(1);
if ($this->request->is('mid')) {
$select->where('mid <> ?', $this->request->get('mid'));
}
$category = $this->db->fetchRow($select);
return !$category;
}
/**
*
*
* @throws Exception
*/
public function insertCategory()
{
if ($this->form('insert')->validate()) {
$this->response->goBack();
}
/** */
$category = $this->request->from('name', 'slug', 'description', 'parent');
$category['slug'] = Common::slugName(Common::strBy($category['slug'] ?? null, $category['name']));
$category['type'] = 'category';
$category['order'] = $this->getMaxOrder('category', $category['parent']) + 1;
/** */
$category['mid'] = $this->insert($category);
$this->push($category);
/** */
Notice::alloc()->highlight($this->theId);
/** */
Notice::alloc()->set(
_t(' <a href="%s">%s</a> ', $this->permalink, $this->name),
'success'
);
/** */
$this->response->redirect(Common::url('manage-categories.php'
. ($category['parent'] ? '?parent=' . $category['parent'] : ''), $this->options->adminUrl));
}
/**
*
*
* @param string|null $action
* @return Form
* @throws Exception
*/
public function form(?string $action = null): Form
{
/** */
$form = new Form($this->security->getIndex('/action/metas-category-edit'), Form::POST_METHOD);
/** */
$name = new Form\Element\Text('name', null, null, _t('') . ' *');
$form->addInput($name);
/** */
$slug = new Form\Element\Text(
'slug',
null,
null,
_t(''),
_t(', , , .')
);
$form->addInput($slug);
/** */
$options = [0 => _t('')];
$parents = Rows::allocWithAlias(
'options',
($this->request->is('mid') ? 'ignore=' . $this->request->get('mid') : '')
);
while ($parents->next()) {
$options[$parents->mid] = str_repeat(' ', $parents->levels) . $parents->name;
}
$parent = new Form\Element\Select(
'parent',
$options,
$this->request->get('parent'),
_t(''),
_t('.')
);
$form->addInput($parent);
/** */
$description = new Form\Element\Textarea(
'description',
null,
null,
_t(''),
_t(', .')
);
$form->addInput($description);
/** */
$do = new Form\Element\Hidden('do');
$form->addInput($do);
/** */
$mid = new Form\Element\Hidden('mid');
$form->addInput($mid);
/** */
$submit = new Form\Element\Submit();
$submit->input->setAttribute('class', 'btn primary');
$form->addItem($submit);
if (isset($this->request->mid) && 'insert' != $action) {
/** */
$meta = $this->db->fetchRow($this->select()
->where('mid = ?', $this->request->mid)
->where('type = ?', 'category')->limit(1));
if (!$meta) {
$this->response->redirect(Common::url('manage-categories.php', $this->options->adminUrl));
}
$name->value($meta['name']);
$slug->value($meta['slug']);
$parent->value($meta['parent']);
$description->value($meta['description']);
$do->value('update');
$mid->value($meta['mid']);
$submit->value(_t(''));
$_action = 'update';
} else {
$do->value('insert');
$submit->value(_t(''));
$_action = 'insert';
}
if (empty($action)) {
$action = $_action;
}
/** */
if ('insert' == $action || 'update' == $action) {
$name->addRule('required', _t(''));
$name->addRule([$this, 'nameExists'], _t(''));
$name->addRule([$this, 'nameToSlug'], _t(''));
$name->addRule('xssCheck', _t(''));
$slug->addRule([$this, 'slugExists'], _t(''));
$slug->addRule('xssCheck', _t(''));
}
if ('update' == $action) {
$mid->addRule('required', _t(''));
$mid->addRule([$this, 'categoryExists'], _t(''));
}
return $form;
}
/**
*
*
* @throws Exception
*/
public function updateCategory()
{
if ($this->form('update')->validate()) {
$this->response->goBack();
}
/** */
$category = $this->request->from('name', 'slug', 'description', 'parent');
$category['mid'] = $this->request->get('mid');
$category['slug'] = Common::slugName(Common::strBy($category['slug'] ?? null, $category['name']));
$category['type'] = 'category';
$current = $this->db->fetchRow($this->select()->where('mid = ?', $category['mid']));
if ($current['parent'] != $category['parent']) {
$parent = $this->db->fetchRow($this->select()->where('mid = ?', $category['parent']));
if ($parent['mid'] == $category['mid']) {
$category['order'] = $parent['order'];
$this->update([
'parent' => $current['parent'],
'order' => $current['order']
], $this->db->sql()->where('mid = ?', $parent['mid']));
} else {
$category['order'] = $this->getMaxOrder('category', $category['parent']) + 1;
}
}
/** */
$this->update($category, $this->db->sql()->where('mid = ?', $this->request->filter('int')->get('mid')));
$this->push($category);
/** */
Notice::alloc()->highlight($this->theId);
/** */
Notice::alloc()
->set(_t(' <a href="%s">%s</a> ', $this->permalink, $this->name), 'success');
/** */
$this->response->redirect(Common::url('manage-categories.php'
. ($category['parent'] ? '?parent=' . $category['parent'] : ''), $this->options->adminUrl));
}
/**
*
*
* @access public
* @return void
* @throws Exception
*/
public function deleteCategory()
{
$categories = $this->request->filter('int')->getArray('mid');
$deleteCount = 0;
foreach ($categories as $category) {
$parent = $this->db->fetchObject($this->select()->where('mid = ?', $category))->parent;
if ($this->delete($this->db->sql()->where('mid = ?', $category))) {
$this->db->query($this->db->delete('table.relationships')->where('mid = ?', $category));
$this->update(['parent' => $parent], $this->db->sql()->where('parent = ?', $category));
$deleteCount++;
}
}
/** */
Notice::alloc()
->set($deleteCount > 0 ? _t('') : _t(''), $deleteCount > 0 ? 'success' : 'notice');
/** */
$this->response->goBack();
}
/**
*
* @throws Exception
*/
public function mergeCategory()
{
/** */
$validator = new Validate();
$validator->addRule('merge', 'required', _t(''));
$validator->addRule('merge', [$this, 'categoryExists'], _t(''));
if ($error = $validator->run($this->request->from('merge'))) {
Notice::alloc()->set($error, 'error');
$this->response->goBack();
}
$merge = $this->request->get('merge');
$categories = $this->request->filter('int')->getArray('mid');
if ($categories) {
$this->merge($merge, 'category', $categories);
/** */
Notice::alloc()->set(_t(''), 'success');
} else {
Notice::alloc()->set(_t(''));
}
/** */
$this->response->goBack();
}
/**
*
* @throws Exception
*/
public function sortCategory()
{
$categories = $this->request->filter('int')->getArray('mid');
if ($categories) {
$this->sort($categories, 'category');
}
if (!$this->request->isAjax()) {
/** */
$this->response->redirect(Common::url('manage-categories.php', $this->options->adminUrl));
} else {
$this->response->throwJson(['success' => 1, 'message' => _t('')]);
}
}
/**
*
*
* @throws Exception
*/
public function refreshCategory()
{
$categories = $this->request->filter('int')->getArray('mid');
if ($categories) {
foreach ($categories as $category) {
$this->refreshCountByTypeAndStatus($category, 'post');
}
Notice::alloc()->set(_t(''), 'success');
} else {
Notice::alloc()->set(_t(''));
}
/** */
$this->response->goBack();
}
/**
*
*
* @throws Exception
*/
public function defaultCategory()
{
/** */
$validator = new Validate();
$validator->addRule('mid', 'required', _t(''));
$validator->addRule('mid', [$this, 'categoryExists'], _t(''));
if ($error = $validator->run($this->request->from('mid'))) {
Notice::alloc()->set($error, 'error');
} else {
$this->db->query($this->db->update('table.options')
->rows(['value' => $this->request->get('mid')])
->where('name = ?', 'defaultCategory'));
$this->db->fetchRow($this->select()->where('mid = ?', $this->request->get('mid'))
->where('type = ?', 'category')->limit(1), [$this, 'push']);
/** */
Notice::alloc()->highlight($this->theId);
/** */
Notice::alloc()->set(
_t('<a href="%s">%s</a> ', $this->permalink, $this->name),
'success'
);
}
/** */
$this->response->redirect(Common::url('manage-categories.php', $this->options->adminUrl));
}
/**
*
*
* @return string|null
* @throws \Typecho\Widget\Exception|Exception
*/
public function getMenuTitle(): ?string
{
if ($this->request->is('mid')) {
$category = $this->db->fetchRow($this->select()
->where('type = ? AND mid = ?', 'category', $this->request->filter('int')->get('mid')));
if (!empty($category)) {
return _t(' %s', $category['name']);
}
}
if ($this->request->is('parent')) {
$category = $this->db->fetchRow($this->select()
->where('type = ? AND mid = ?', 'category', $this->request->filter('int')->get('parent')));
if (!empty($category)) {
return _t(' %s ', $category['name']);
}
} else {
return null;
}
throw new \Typecho\Widget\Exception(_t(''), 404);
}
/**
*
*
* @access public
* @return void
* @throws Exception
*/
public function action()
{
$this->security->protect();
$this->on($this->request->is('do=insert'))->insertCategory();
$this->on($this->request->is('do=update'))->updateCategory();
$this->on($this->request->is('do=delete'))->deleteCategory();
$this->on($this->request->is('do=merge'))->mergeCategory();
$this->on($this->request->is('do=sort'))->sortCategory();
$this->on($this->request->is('do=refresh'))->refreshCategory();
$this->on($this->request->is('do=default'))->defaultCategory();
$this->response->redirect($this->options->adminUrl);
}
}
Function Calls
None |
Stats
MD5 | 3246d89133c6fc470835e724aeddaf98 |
Eval Count | 0 |
Decode Time | 117 ms |