123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513 |
- <?php
- namespace Utils;
- use Typecho\Common;
- use Typecho\Db;
- use Typecho\I18n;
- use Typecho\Plugin;
- use Typecho\Widget;
- use Widget\Base\Options as BaseOptions;
- use Widget\Options;
- use Widget\Plugins\Edit;
- use Widget\Security;
- use Widget\Service;
- class Helper
- {
-
- public static function security(): Security
- {
- return Security::alloc();
- }
-
- public static function widgetById(string $table, int $pkId): ?Widget
- {
- $table = ucfirst($table);
- if (!in_array($table, ['Contents', 'Comments', 'Metas', 'Users'])) {
- return null;
- }
- $keys = [
- 'Contents' => 'cid',
- 'Comments' => 'coid',
- 'Metas' => 'mid',
- 'Users' => 'uid'
- ];
- $className = '\Widget\Base\\' . $table;
- $key = $keys[$table];
- $db = Db::get();
- $widget = Widget::widget($className . '@' . $pkId);
- $db->fetchRow(
- $widget->select()->where("{$key} = ?", $pkId)->limit(1),
- [$widget, 'push']
- );
- return $widget;
- }
-
- public static function requestService($method, $params)
- {
- Service::alloc()->requestService($method, $params);
- }
-
- public static function removePlugin(string $pluginName)
- {
- try {
-
- [$pluginFileName, $className] = Plugin::portal(
- $pluginName,
- __TYPECHO_ROOT_DIR__ . '/' . __TYPECHO_PLUGIN_DIR__
- );
-
- $plugins = Plugin::export();
- $activatedPlugins = $plugins['activated'];
-
- require_once $pluginFileName;
-
- if (
- !isset($activatedPlugins[$pluginName]) || !class_exists($className)
- || !method_exists($className, 'deactivate')
- ) {
- throw new Widget\Exception(_t('无法禁用插件'), 500);
- }
- call_user_func([$className, 'deactivate']);
- } catch (\Exception $e) {
-
- }
- $db = Db::get();
- try {
- Plugin::deactivate($pluginName);
- $db->query($db->update('table.options')
- ->rows(['value' => serialize(Plugin::export())])
- ->where('name = ?', 'plugins'));
- } catch (Plugin\Exception $e) {
-
- }
- $db->query($db->delete('table.options')->where('name = ?', 'plugin:' . $pluginName));
- }
-
- public static function lang(string $domain)
- {
- $currentLang = I18n::getLang();
- if ($currentLang) {
- $currentLang = basename($currentLang);
- $fileName = dirname(__FILE__) . '/' . $domain . '/lang/' . $currentLang;
- if (file_exists($fileName)) {
- I18n::addLang($fileName);
- }
- }
- }
-
- public static function addRoute(
- string $name,
- string $url,
- string $widget,
- ?string $action = null,
- ?string $after = null
- ): int {
- $routingTable = self::options()->routingTable;
- if (isset($routingTable[0])) {
- unset($routingTable[0]);
- }
- $pos = 0;
- foreach ($routingTable as $key => $val) {
- $pos++;
- if ($key == $after) {
- break;
- }
- }
- $pre = array_slice($routingTable, 0, $pos);
- $next = array_slice($routingTable, $pos);
- $routingTable = array_merge($pre, [
- $name => [
- 'url' => $url,
- 'widget' => $widget,
- 'action' => $action
- ]
- ], $next);
- self::options()->routingTable = $routingTable;
- return BaseOptions::alloc()->update(
- ['value' => serialize($routingTable)],
- Db::get()->sql()->where('name = ?', 'routingTable')
- );
- }
-
- public static function options(): Options
- {
- return Options::alloc();
- }
-
- public static function removeRoute(string $name): int
- {
- $routingTable = self::options()->routingTable;
- if (isset($routingTable[0])) {
- unset($routingTable[0]);
- }
- unset($routingTable[$name]);
- self::options()->routingTable = $routingTable;
- $db = Db::get();
- return BaseOptions::alloc()->update(
- ['value' => serialize($routingTable)],
- $db->sql()->where('name = ?', 'routingTable')
- );
- }
-
- public static function addAction(string $actionName, string $widgetName): int
- {
- $actionTable = unserialize(self::options()->actionTable);
- $actionTable = empty($actionTable) ? [] : $actionTable;
- $actionTable[$actionName] = $widgetName;
- return BaseOptions::alloc()->update(
- ['value' => (self::options()->actionTable = serialize($actionTable))],
- Db::get()->sql()->where('name = ?', 'actionTable')
- );
- }
-
- public static function removeAction(string $actionName): int
- {
- $actionTable = unserialize(self::options()->actionTable);
- $actionTable = empty($actionTable) ? [] : $actionTable;
- if (isset($actionTable[$actionName])) {
- unset($actionTable[$actionName]);
- reset($actionTable);
- }
- return BaseOptions::alloc()->update(
- ['value' => (self::options()->actionTable = serialize($actionTable))],
- Db::get()->sql()->where('name = ?', 'actionTable')
- );
- }
-
- public static function addMenu(string $menuName): int
- {
- $panelTable = unserialize(self::options()->panelTable);
- $panelTable['parent'] = empty($panelTable['parent']) ? [] : $panelTable['parent'];
- $panelTable['parent'][] = $menuName;
- BaseOptions::alloc()->update(
- ['value' => (self::options()->panelTable = serialize($panelTable))],
- Db::get()->sql()->where('name = ?', 'panelTable')
- );
- end($panelTable['parent']);
- return key($panelTable['parent']) + 10;
- }
-
- public static function removeMenu(string $menuName): int
- {
- $panelTable = unserialize(self::options()->panelTable);
- $panelTable['parent'] = empty($panelTable['parent']) ? [] : $panelTable['parent'];
- if (false !== ($index = array_search($menuName, $panelTable['parent']))) {
- unset($panelTable['parent'][$index]);
- }
- BaseOptions::alloc()->update(
- ['value' => (self::options()->panelTable = serialize($panelTable))],
- Db::get()->sql()->where('name = ?', 'panelTable')
- );
- return $index + 10;
- }
-
- public static function addPanel(
- int $index,
- string $fileName,
- string $title,
- string $subTitle,
- string $level,
- bool $hidden = false,
- string $addLink = ''
- ): int {
- $panelTable = unserialize(self::options()->panelTable);
- $panelTable['child'] = empty($panelTable['child']) ? [] : $panelTable['child'];
- $panelTable['child'][$index] = empty($panelTable['child'][$index]) ? [] : $panelTable['child'][$index];
- $fileName = urlencode(trim($fileName, '/'));
- $panelTable['child'][$index][]
- = [$title, $subTitle, 'extending.php?panel=' . $fileName, $level, $hidden, $addLink];
- $panelTable['file'] = empty($panelTable['file']) ? [] : $panelTable['file'];
- $panelTable['file'][] = $fileName;
- $panelTable['file'] = array_unique($panelTable['file']);
- BaseOptions::alloc()->update(
- ['value' => (self::options()->panelTable = serialize($panelTable))],
- Db::get()->sql()->where('name = ?', 'panelTable')
- );
- end($panelTable['child'][$index]);
- return key($panelTable['child'][$index]);
- }
-
- public static function removePanel(int $index, string $fileName): int
- {
- $panelTable = unserialize(self::options()->panelTable);
- $panelTable['child'] = empty($panelTable['child']) ? [] : $panelTable['child'];
- $panelTable['child'][$index] = empty($panelTable['child'][$index]) ? [] : $panelTable['child'][$index];
- $panelTable['file'] = empty($panelTable['file']) ? [] : $panelTable['file'];
- $fileName = urlencode(trim($fileName, '/'));
- if (false !== ($key = array_search($fileName, $panelTable['file']))) {
- unset($panelTable['file'][$key]);
- }
- $return = 0;
- foreach ($panelTable['child'][$index] as $key => $val) {
- if ($val[2] == 'extending.php?panel=' . $fileName) {
- unset($panelTable['child'][$index][$key]);
- $return = $key;
- }
- }
- BaseOptions::alloc()->update(
- ['value' => (self::options()->panelTable = serialize($panelTable))],
- Db::get()->sql()->where('name = ?', 'panelTable')
- );
- return $return;
- }
-
- public static function url(string $fileName): string
- {
- return Common::url('extending.php?panel=' . (trim($fileName, '/')), self::options()->adminUrl);
- }
-
- public static function configPlugin($pluginName, array $settings, bool $isPersonal = false)
- {
- if (empty($settings)) {
- return;
- }
- Edit::configPlugin($pluginName, $settings, $isPersonal);
- }
-
- public static function replyLink(
- string $theId,
- int $coid,
- string $word = 'Reply',
- string $formId = 'respond',
- int $style = 2
- ) {
- if (self::options()->commentsThreaded) {
- echo '<a href="#' . $formId . '" rel="nofollow" onclick="return typechoAddCommentReply(\'' .
- $theId . '\', ' . $coid . ', \'' . $formId . '\', ' . $style . ');">' . $word . '</a>';
- }
- }
-
- public static function cancelCommentReplyLink(string $word = 'Cancel', string $formId = 'respond')
- {
- if (self::options()->commentsThreaded) {
- echo '<a href="#' . $formId . '" rel="nofollow" onclick="return typechoCancelCommentReply(\'' .
- $formId . '\');">' . $word . '</a>';
- }
- }
-
- public static function threadedCommentsScript()
- {
- if (self::options()->commentsThreaded) {
- echo
- <<<EOF
- <script type="text/javascript">
- var typechoAddCommentReply = function (cid, coid, cfid, style) {
- var _ce = document.getElementById(cid), _cp = _ce.parentNode;
- var _cf = document.getElementById(cfid);
- var _pi = document.getElementById('comment-parent');
- if (null == _pi) {
- _pi = document.createElement('input');
- _pi.setAttribute('type', 'hidden');
- _pi.setAttribute('name', 'parent');
- _pi.setAttribute('id', 'comment-parent');
- var _form = 'form' == _cf.tagName ? _cf : _cf.getElementsByTagName('form')[0];
- _form.appendChild(_pi);
- }
- _pi.setAttribute('value', coid);
- if (null == document.getElementById('comment-form-place-holder')) {
- var _cfh = document.createElement('div');
- _cfh.setAttribute('id', 'comment-form-place-holder');
- _cf.parentNode.insertBefore(_cfh, _cf);
- }
- 1 == style ? (null == _ce.nextSibling ? _cp.appendChild(_cf)
- : _cp.insertBefore(_cf, _ce.nextSibling)) : _ce.appendChild(_cf);
- return false;
- };
- var typechoCancelCommentReply = function (cfid) {
- var _cf = document.getElementById(cfid),
- _cfh = document.getElementById('comment-form-place-holder');
- var _pi = document.getElementById('comment-parent');
- if (null != _pi) {
- _pi.parentNode.removeChild(_pi);
- }
- if (null == _cfh) {
- return true;
- }
- _cfh.parentNode.insertBefore(_cf, _cfh);
- return false;
- };
- </script>
- EOF;
- }
- }
- }
|