This commit is contained in:
143
GoLinks/Action.php
Normal file
143
GoLinks/Action.php
Normal file
@@ -0,0 +1,143 @@
|
||||
<?php
|
||||
/**
|
||||
* GoLinks Plugin
|
||||
*
|
||||
* @copyright Copyright (c) 2011 DEFE (http://defe.me)
|
||||
* @license GNU General Public License 2.0
|
||||
*
|
||||
*/
|
||||
|
||||
class GoLinks_Action extends Typecho_Widget implements Widget_Interface_Do
|
||||
{
|
||||
private $db;
|
||||
|
||||
public function __construct($request, $response, $params = NULL)
|
||||
{
|
||||
parent::__construct($request, $response, $params);
|
||||
|
||||
$this->db = Typecho_Db::get();
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加新的链接转换
|
||||
*
|
||||
*/
|
||||
public function add(){
|
||||
|
||||
$key = $this->request->key;
|
||||
$key = $key ? $key : Typecho_Common::randString(8);
|
||||
$target = $this->request->target;
|
||||
if($target === "" || $target === "http://"){
|
||||
$this->widget('Widget_Notice')->set(_t('请输入目标链接。'), NULL, 'error');
|
||||
}
|
||||
//判断key是否被占用
|
||||
elseif($this->getTarget($key)){ ;
|
||||
$this->widget('Widget_Notice')->set(_t('该key已被使用,请更换key值。'), NULL, 'error');
|
||||
} else {
|
||||
$links=array(
|
||||
'key' => $key,
|
||||
'target' => $this->request->target,
|
||||
'count' => 0
|
||||
);
|
||||
$insertId = $this->db->query($this->db->insert('table.golinks')->rows($links));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改链接
|
||||
*
|
||||
*/
|
||||
|
||||
public function edit(){
|
||||
$target = $this->request->url;
|
||||
$id = $this->request->id;
|
||||
if(trim($target) == "" || $target == "http://"){
|
||||
$this->response->throwJson('error');
|
||||
}else{
|
||||
if($id){
|
||||
$this->db->query($this->db->update('table.golinks')->rows(array('target' => $target))
|
||||
->where('id = ?', $id));
|
||||
$this->response->throwJson('success');
|
||||
}}
|
||||
}
|
||||
|
||||
/**
|
||||
*删除链接转换
|
||||
*
|
||||
* @param int $id
|
||||
*/
|
||||
public function del($id){
|
||||
$this->db->query($this->db->delete('table.golinks')
|
||||
->where('id = ?', $id));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 链接重定向
|
||||
*
|
||||
*/
|
||||
public function golink()
|
||||
{
|
||||
$key = $this->request->key;
|
||||
$target = $this->getTarget($key);
|
||||
|
||||
if( $target){
|
||||
//增加统计
|
||||
$count = $this->db->fetchObject($this->db->select('count')
|
||||
->from('table.golinks')
|
||||
->where('key = ?', $key))->count;
|
||||
|
||||
$count = $count+1;
|
||||
|
||||
$this->db->query($this->db->update('table.golinks')
|
||||
->rows(array('count' => $count))
|
||||
->where('key = ?', $key));
|
||||
|
||||
|
||||
//设置nofollow属性
|
||||
$this->response->setHeader('X-Robots-Tag','noindex, nofollow');
|
||||
//301重定向
|
||||
$this->response->redirect($target,301);
|
||||
|
||||
}else{
|
||||
throw new Typecho_Widget_Exception(_t('您访问的网页不存在'), 404);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取目标链接
|
||||
*
|
||||
* @param string $key
|
||||
* @return void
|
||||
*/
|
||||
public function getTarget($key){
|
||||
$target = $this->db->fetchRow($this->db->select('target')
|
||||
->from('table.golinks')
|
||||
->where(' key = ?' , $key));
|
||||
if($target['target']){
|
||||
return $target['target'];
|
||||
}else{
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 重设自定义链接
|
||||
*/
|
||||
public function resetLink(){
|
||||
$link = $this->request->link;
|
||||
Helper::removeRoute('go');
|
||||
Helper::addRoute('go', $link, 'GoLinks_Action', 'golink');
|
||||
$this->response->throwJson('success');
|
||||
}
|
||||
|
||||
public function action(){
|
||||
$this->widget('Widget_User')->pass('administrator');
|
||||
$this->on($this->request->is('add'))->add();
|
||||
$this->on($this->request->is('edit'))->edit();
|
||||
$this->on($this->request->is('del'))->del($this->request->del);
|
||||
$this->on($this->request->is('resetLink'))->resetLink();
|
||||
$this->response->goBack();
|
||||
}
|
||||
}
|
||||
?>
|
||||
83
GoLinks/Plugin.php
Normal file
83
GoLinks/Plugin.php
Normal file
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
/**
|
||||
* 把外部链接转换为 your_blog_path/go/key/<br>
|
||||
* 通过菜单“撰写->链接转换”设置
|
||||
*
|
||||
* @package 链接转换 GoLinks
|
||||
* @author DEFE
|
||||
* @version 0.3.0
|
||||
* @link http://defe.me
|
||||
*/
|
||||
class GoLinks_Plugin implements Typecho_Plugin_Interface
|
||||
{
|
||||
/**
|
||||
* 激活插件方法,如果激活失败,直接抛出异常
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
* @throws Typecho_Plugin_Exception
|
||||
*/
|
||||
public static function activate()
|
||||
{
|
||||
$db = Typecho_Db::get();
|
||||
$golinks = $db->getPrefix() . 'golinks';
|
||||
$adapter = $db->getAdapterName();
|
||||
if("Pdo_SQLite" === $adapter || "SQLite" === $adapter){
|
||||
$db->query(" CREATE TABLE IF NOT EXISTS ". $golinks ." (
|
||||
id INTEGER PRIMARY KEY,
|
||||
key TEXT,
|
||||
target TEXT,
|
||||
count NUMERIC)");
|
||||
}
|
||||
if("Pdo_Mysql" === $adapter || "Mysql" === $adapter){
|
||||
$db->query("CREATE TABLE IF NOT EXISTS ". $golinks ." (
|
||||
`id` int(8) NOT NULL AUTO_INCREMENT,
|
||||
`key` varchar(32) NOT NULL,
|
||||
`target` varchar(10000) NOT NULL,
|
||||
`count` int(8) DEFAULT '0',
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1");
|
||||
}
|
||||
|
||||
Helper::addAction('golinks', 'GoLinks_Action');
|
||||
Helper::addRoute('go', '/go/[key]/', 'GoLinks_Action', 'golink');
|
||||
Helper::addPanel(2, 'GoLinks/panel.php', '链接转换', '链接转换管理', 'administrator');
|
||||
return('数据表 '.$golinks.' 创建成功, 插件已经成功激活!');
|
||||
}
|
||||
|
||||
/**
|
||||
* 禁用插件方法,如果禁用失败,直接抛出异常
|
||||
*
|
||||
* @static
|
||||
* @access public
|
||||
* @return void
|
||||
* @throws Typecho_Plugin_Exception
|
||||
*/
|
||||
public static function deactivate()
|
||||
{
|
||||
Helper::removeRoute('go');
|
||||
Helper::removeAction('golinks');
|
||||
Helper::removePanel(2, 'GoLinks/panel.php');
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取插件配置面板
|
||||
*
|
||||
* @access public
|
||||
* @param Typecho_Widget_Helper_Form $form 配置面板
|
||||
* @return void
|
||||
*/
|
||||
public static function config(Typecho_Widget_Helper_Form $form)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* 个人用户的配置面板
|
||||
*
|
||||
* @access public
|
||||
* @param Typecho_Widget_Helper_Form $form
|
||||
* @return void
|
||||
*/
|
||||
public static function personalConfig(Typecho_Widget_Helper_Form $form){}
|
||||
|
||||
}
|
||||
7
GoLinks/README.md
Normal file
7
GoLinks/README.md
Normal file
@@ -0,0 +1,7 @@
|
||||
### 外链改内链跳转插件GoLinks v0.3.0
|
||||
|
||||
启用后可在菜单“撰写”-“链接转换”里设置跳转至外部链接的指定内链地址并对使用次数进行统计。
|
||||
|
||||
> 修正action响应及面板效果,兼容Typecho1.0+。
|
||||
|
||||
###### 更多详见作者博客:http://defe.me/prg/415.html
|
||||
142
GoLinks/panel.php
Normal file
142
GoLinks/panel.php
Normal file
@@ -0,0 +1,142 @@
|
||||
<?php
|
||||
include 'header.php';
|
||||
include 'menu.php';
|
||||
?>
|
||||
<div class="main">
|
||||
<div class="body container">
|
||||
<?php include 'page-title.php'; ?>
|
||||
<div class="container typecho-page-main">
|
||||
<div class="col-mb-12 typecho-list">
|
||||
<div class="typecho-option-tabs">
|
||||
<ul class="typecho-option-tabs clearfix">
|
||||
<li class="current">
|
||||
<form action="<?php $options->index('/action/golinks?add'); ?>" method="post" >
|
||||
KEY:<input name="key" id="key" type="text" value="" />
|
||||
目标:<input name="target" id="target" type="text" value="http://" />
|
||||
<input type="submit" class="btn-s primary" value="添加" />
|
||||
</form>
|
||||
</li>
|
||||
|
||||
<li class="right current">
|
||||
<?php $ro = Typecho_Router::get('go'); ?>
|
||||
自定义链接:<input id="links" name="links" value="<?php echo $ro['url'] ?>" type="text">
|
||||
<button id="qlinks" type="button">修改</button>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
<div class="typecho-table-wrap">
|
||||
<table class="typecho-list-table">
|
||||
<colgroup>
|
||||
<col width="8%"/>
|
||||
<col width="32%"/>
|
||||
<col width="47%"/>
|
||||
<col width="5%"/>
|
||||
<col width="8%"/>
|
||||
</colgroup>
|
||||
<thead>
|
||||
<tr>
|
||||
<th><?php _e('KEY'); ?></th>
|
||||
<th><?php _e('站内链接'); ?></th>
|
||||
<th><?php _e('目标链接'); ?> </th>
|
||||
<th><?php _e('统计'); ?> </th>
|
||||
<th><?php _e('操作'); ?></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php $page = isset($request->page) ? $request->page : 1 ; ?>
|
||||
<?php $links = $db->fetchAll($db->select()->from('table.golinks')->page($page, 15)->order('table.golinks.id', Typecho_Db::SORT_DESC)); ?>
|
||||
<?php foreach($links as $link): ?>
|
||||
<tr class="even" id="<?php _e($link['id']); ?>" >
|
||||
<td>
|
||||
<?php _e($link['key']); ?>
|
||||
</td>
|
||||
<td>
|
||||
<?php $rourl = str_replace('[key]', $link['key'], $ro['url']); ?>
|
||||
<?php $options->index($rourl);?>
|
||||
</td>
|
||||
<td id="e-<?php _e($link['id']); ?>"><?php _e($link['target']); ?></td>
|
||||
<td><?php _e($link['count']); ?></td>
|
||||
<td>
|
||||
<a href="#<?php _e($link['id']); ?>" class="operate-edit">修改</a>
|
||||
<a lang="<?php _e('你确认要删除该链接吗?'); ?>" href="<?php $options->index('/action/golinks?del=' . $link['id']); ?>" class="operate-delete"><?php _e('删除'); ?></a>
|
||||
</td>
|
||||
</tr>
|
||||
<?php endforeach;?>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<div class="typecho-pager">
|
||||
<div class="typecho-pager-content">
|
||||
<ul>
|
||||
<?php $total = $db->fetchObject($db->select(array('COUNT(id)' => 'num'))->from('table.golinks'))->num; ?>
|
||||
<?php for($i=1;$i<=ceil($total/15);$i++): ?>
|
||||
<li class='current'><a href="<?php $options->adminUrl('extending.php?panel=GoLinks%2Fpanel.php&page='.$i); ?>" style= 'cursor:pointer;' title='第 <?php _e($i); ?> 页'> <?php _e($i); ?> </a></li>
|
||||
<?php endfor; ?>
|
||||
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<?php
|
||||
include 'copyright.php';
|
||||
include 'common-js.php';
|
||||
include 'footer.php';
|
||||
?>
|
||||
<script type="text/javascript">
|
||||
$(document).ready(function () {
|
||||
$('.operate-delete').click(function () {
|
||||
var t = $(this), href = t.attr('href'), tr = t.parents('tr');
|
||||
if (confirm(t.attr('lang'))) {
|
||||
tr.fadeOut(function () {
|
||||
window.location.href = href;
|
||||
});
|
||||
}
|
||||
return false;
|
||||
});
|
||||
|
||||
$('.operate-edit').click(function () {
|
||||
var tr = $(this).parents('tr'), t = $(this), id = tr.attr('id');
|
||||
var value = $('#e-'+id).html();
|
||||
$('#e-'+id).html('<input type="text" id="t-'+id +'" size="55" value="'+ value + '" /> <button type="submit" id="u-'+ id +'" class="btn-s primary"><?php _e('确认'); ?></button> <button type="button" id="c-'+ id +'" class="btn-s cancel"><?php _e('取消'); ?></button>');
|
||||
$("[href='#"+id+"']").hide();
|
||||
|
||||
//确认
|
||||
$('#u-'+id).click(function(){
|
||||
$.ajax({
|
||||
url: '<?php $options->index('/action/golinks?edit'); ?>',
|
||||
data:'id='+id+'&url='+$('#t-'+id).val(),
|
||||
dataType:"json",
|
||||
success:function(data){
|
||||
if(data==='success'){
|
||||
$('#e-'+id).html($('#t-'+id).val());
|
||||
$("[href='#"+id+"']").show();
|
||||
}else{
|
||||
alert('请输入有效链接');
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
//取消
|
||||
$('#c-'+id).click(function(){
|
||||
$('#e-'+id).html(value);
|
||||
$("[href='#"+id+"']").show();
|
||||
});
|
||||
});
|
||||
|
||||
$('#qlinks').click(function(){
|
||||
$.ajax({
|
||||
url:'<?php $options->index('/action/golinks?resetLink'); ?>',
|
||||
data:'link='+$('#links').val(),
|
||||
dataType:'json',
|
||||
success:function(data){
|
||||
if('success' === data){
|
||||
location.reload();
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
</script>
|
||||
Reference in New Issue
Block a user