From 89e5172dbd5a650f6e66646d04f2cca9caaa8a1e Mon Sep 17 00:00:00 2001 From: xiaoz Date: Mon, 9 May 2022 12:38:42 +0800 Subject: [PATCH 01/12] 20220509 --- class/Api.php | 137 +++++++++++++++++++++++++++++++++++++++++++++ controller/api.php | 21 +++++++ 2 files changed, 158 insertions(+) diff --git a/class/Api.php b/class/Api.php index bb41ec6..0c620e9 100755 --- a/class/Api.php +++ b/class/Api.php @@ -245,6 +245,26 @@ class Api { } } } + /** + * 批量修改链接属性为公有或私有 + */ + public function set_link_attribute($data) { + $this->auth($token); + //获取链接ID,是一个数组 + $ids = implode(',',$data['ids']); + $property = intval($data['property']); + //拼接SQL文件 + $sql = "UPDATE on_links SET property = $property WHERE id IN ($ids)"; + $re = $this->db->query($sql); + //返回影响行数 + $row = $re->rowCount(); + if ( $row > 0 ){ + $this->return_json(200,"success"); + } + else{ + $this->return_json(-2000,"failed"); + } + } /** * 批量导入链接 @@ -323,6 +343,123 @@ class Api { ]; exit(json_encode($data)); } + /** + * 批量导入链接并自动创建分类,这是新的导入接口 + */ + public function import_link($filename,$property = 0) { + //过滤$filename + $filename = str_replace('../','',$filename); + $filename = str_replace('./','',$filename); + $this->auth($token); + //检查文件是否存在 + if ( !file_exists($filename) ) { + $this->err_msg(-1016,'File does not exist!'); + } + //解析HTML数据 + $content = file_get_contents($filename); + $HTMLs = explode("\n",$content);//分割文本 + $data = []; //链接组 + $categorys = []; //分类信息组 + $categoryt = []; //分类信息表 + + // 遍历HTML + foreach( $HTMLs as $HTMLh ){ + //匹配分类名称 + if( preg_match("/
(.*)<\/H3>/i",$HTMLh,$category) ){ + //匹配到文件夹名时加入数组 + array_push($categoryt,$category[1]); + array_push($categorys,$category[1]); + }elseif( preg_match('/<\/DL>

/i',$HTMLh) ){ + //匹配到文件夹结束标记时删除一个 + array_pop($categorys); + }elseif( preg_match('/

(.+)<\/A>/i',$HTMLh,$urls) ){ + $datat['category'] = $categorys[count($categorys) -1]; + $datat['title'] = $urls[2]; + $datat['url'] = $urls[1]; + array_push($data,$datat); + } + } + $categoryt = array_unique($categoryt); + + //批量创建分类 + $this->batch_create_category($categoryt); + //查询所有分类 + $categorys = $this->db->select("on_categorys",[ + "name", + "id", + "fid" + ]); + // var_dump($categorys); + // exit; + //链接计数 + $i = 0; + //统计链接总数 + $count = count($data); + //批量导入链接 + foreach ($data as $key => $value) { + $category_name = trim($value['category']); + + foreach ($categorys as $category) { + if( trim( $category['name'] ) == $category_name ) { + $fid = intval($category['id']); + break; + } + } + //合并数据 + $link_data = [ + 'fid' => $fid, + 'title' => htmlspecialchars($value['title']), + 'url' => htmlspecialchars($value['url'],ENT_QUOTES), + 'add_time' => time(), + 'weight' => 0, + 'property' => $property + ]; + // var_dump($link_data); + // exit; + //插入数据库 + $re = $this->db->insert('on_links',$link_data); + //返回影响行数 + $row = $re->rowCount(); + if ($row) { + $i++; + } + } + //删除书签文件 + unlink($filename); + $this->return_json(200,"success",[ + "count" => $count, + "success" => $i, + "failed" => $count - $i + ]); + + } + /** + * 批量创建分类 + * 接收一个一维数组 + */ + protected function batch_create_category($category_name) { + $i = 0; + foreach ($category_name as $key => $value) { + $data = [ + 'name' => trim($value), + 'add_time' => time(), + 'weight' => 0, + 'property' => 1, + 'description' => "书签导入时自动创建", + 'fid' => 0 + ]; + try { + //插入分类目录 + $this->db->insert("on_categorys",$data); + $i++; + } catch (\Throwable $th) { + continue; + } + + } + return $i; + } + /** * 书签上传 * type:上传类型,默认为上传书签,后续类型保留使用 diff --git a/controller/api.php b/controller/api.php index 14c3459..ebbaacb 100755 --- a/controller/api.php +++ b/controller/api.php @@ -221,6 +221,16 @@ function imp_link($api) { $property = intval(@$_POST['property']); $api->imp_link($token,$filename,$fid,$property); } +//新版书签批量导入并自动创建分类 +function import_link($api) { + //获取token + $token = $_POST['token']; + //获取书签路径 + $filename = trim($_POST['filename']); + $fid = intval($_POST['fid']); + $property = intval(@$_POST['property']); + $api->import_link($filename,$property); +} //检查弱密码 function check_weak_password($api) { //获取token @@ -354,4 +364,15 @@ function save_theme_config($api) { //获取主题配置信息 function get_theme_config($api) { $api->get_theme_config(); +} + +//批量设置链接私有属性 +function set_link_attribute($api) { + $ids = $_POST['ids']; + $property = intval( $_POST['property'] ); + $data = [ + "ids" => $ids, + "property" => $property + ]; + $api->set_link_attribute($data); } \ No newline at end of file From 33fdd8f3b5ebd645e173129d7cf9b23fe42ab883 Mon Sep 17 00:00:00 2001 From: xiaoz Date: Mon, 9 May 2022 12:51:55 +0800 Subject: [PATCH 02/12] 20220509 --- templates/admin/imp_link.php | 6 ++--- templates/admin/link_list.php | 2 ++ templates/admin/static/embed.js | 45 ++++++++++++++++++++++++++++++--- 3 files changed, 47 insertions(+), 6 deletions(-) diff --git a/templates/admin/imp_link.php b/templates/admin/imp_link.php index 8d4b32b..fa1265b 100755 --- a/templates/admin/imp_link.php +++ b/templates/admin/imp_link.php @@ -5,7 +5,7 @@
- +
仅支持 .html 格式导入,导入时会自动创建不存在的分类,使用前请参考帮助文档
@@ -23,7 +23,7 @@
-
+
diff --git a/templates/admin/link_list.php b/templates/admin/link_list.php index 4e4f4b4..9feb99e 100755 --- a/templates/admin/link_list.php +++ b/templates/admin/link_list.php @@ -34,6 +34,8 @@
+ +
diff --git a/templates/admin/static/embed.js b/templates/admin/static/embed.js index a7a1b92..c90440e 100755 --- a/templates/admin/static/embed.js +++ b/templates/admin/static/embed.js @@ -193,6 +193,28 @@ layui.use(['element','table','layer','form','upload'], function(){ } //console.log(data); break; + case "set_private": + //用户点击设为私有按钮 + var data = checkStatus.data; + ids = []; + //获取链接所有ID,并拼接为数组 + for(let i = 0;i < data.length;i++) { + ids.push(data[i].id); + } + //调用函数设为私有 + set_link_attribute(ids,1); + break; + case "set_public": + //用户点击设为私有按钮 + var data = checkStatus.data; + ids = []; + //获取链接所有ID,并拼接为数组 + for(let i = 0;i < data.length;i++) { + ids.push(data[i].id); + } + //调用函数设为公有 + set_link_attribute(ids,0); + break; case 'isAll': layer.msg(checkStatus.isAll ? '全选': '未全选'); break; @@ -546,12 +568,12 @@ layui.use(['element','table','layer','form','upload'], function(){ //识别链接信息 form.on('submit(imp_link)', function(data){ //用ajax异步加载 - $.post('/index.php?c=api&method=imp_link',data.field,function(data,status){ + $.post('/index.php?c=api&method=import_link',data.field,function(data,status){ //如果添加成功 - if(data.code == 0) { + if(data.code == 200) { layer.open({ title: '导入完成' - ,content: data.msg + ,content: "总数:" + data.msg.count + " 成功:" + data.msg.success + " 失败:" + data.msg.failed }); //layer.msg('已添加!', {icon: 1}); } @@ -745,3 +767,20 @@ function get_latest_version(){ }); } + +//设置链接属性,公有或私有,接收一个链接id数组和一个链接属性 +function set_link_attribute(ids,property) { + if( ids.length === 0 ) { + layer.msg("请先选择链接!",{icon:5}); + } + else{ + $.post("/index.php?c=api&method=set_link_attribute",{ids:ids,property:property},function(data,status){ + if( data.code == 200 ){ + layer.msg("设置已更新!",{icon:1}); + } + else{ + layer.msg("设置失败!",{icon:5}); + } + }); + } +} \ No newline at end of file From 23a00c8816c7186042fc1c149ca6b6a43136d914 Mon Sep 17 00:00:00 2001 From: xiaoz Date: Mon, 9 May 2022 12:52:44 +0800 Subject: [PATCH 03/12] 20220509 --- templates/default/static/style.css | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/default/static/style.css b/templates/default/static/style.css index 9cc96af..23a7297 100755 --- a/templates/default/static/style.css +++ b/templates/default/static/style.css @@ -17,7 +17,7 @@ body{ overflow: hidden; } .link-line .angle{ - width:70px; + width:50px; height:70px; position: absolute; background: #FF5722; From 6dfd1505d498220fba34da30ffec33b8668cfbb4 Mon Sep 17 00:00:00 2001 From: xiaoz Date: Mon, 9 May 2022 17:42:23 +0800 Subject: [PATCH 04/12] 20220509 --- class/Api.php | 39 +++++++++++++++++++++++++++++++++ controller/api.php | 28 +++++++++++++++++++++++ templates/admin/header.php | 2 +- templates/admin/link_list.php | 5 ++++- templates/admin/static/embed.js | 25 +++++++++++++++++++++ 5 files changed, 97 insertions(+), 2 deletions(-) diff --git a/class/Api.php b/class/Api.php index 0c620e9..8d58b60 100755 --- a/class/Api.php +++ b/class/Api.php @@ -380,6 +380,8 @@ class Api { } } $categoryt = array_unique($categoryt); + //追加一个默认分类,用来存储部分链接找不到分类的情况 + array_push($categoryt,"默认分类"); //批量创建分类 $this->batch_create_category($categoryt); @@ -398,6 +400,8 @@ class Api { //批量导入链接 foreach ($data as $key => $value) { $category_name = trim($value['category']); + //如果链接的分类是空的,则设置为默认分类 + $value['category'] = empty( $value['category'] ) ? "默认分类" : $value['category']; foreach ($categorys as $category) { if( trim( $category['name'] ) == $category_name ) { @@ -405,6 +409,7 @@ class Api { break; } } + //合并数据 $link_data = [ 'fid' => $fid, @@ -494,6 +499,40 @@ class Api { } } } + /** + * 导出HTML链接进行备份 + */ + public function export_link(){ + //鉴权 + $this->auth($token); + //查询所有分类 + $categorys = $this->db->select("on_categorys","*"); + + //定义一个空数组用来存储查询后的数据 + $data = []; + + //遍历分类 + foreach ($categorys as $key => $category) { + //查询该分类下的所有链接 + $links = $this->db->select("on_links","*",[ + "fid" => $category['id'] + ]); + // echo $category['name']; + // var_dump($links); + // exit; + //组合为一个一维数组 + + $arr[$category['name']] = $links; + // var_dump(); + // exit; + $data[$category['name']] = $arr[$category['name']]; + + //清除临时数据 + unset($arr); + } + //返回数据 + return $data; + } /** * name:修改链接 */ diff --git a/controller/api.php b/controller/api.php index ebbaacb..c9a3ef4 100755 --- a/controller/api.php +++ b/controller/api.php @@ -375,4 +375,32 @@ function set_link_attribute($api) { "property" => $property ]; $api->set_link_attribute($data); +} + +//导出链接数据 +function export_link($api) { + header('Content-Type: text/html;charset=utf8'); + $data = $api->export_link(); + //当前时间 + $current = time(); + echo <<< EOF + +从OneNav导出的书签 +

Bookmarks

+EOF; + //遍历结果 + foreach ($data as $key => $value) { + + echo "
\n"; + echo "

$key

\n"; + echo "

\n"; + foreach ($value as $link) { + $title = $link['title']; + $add_time = $link['add_time']; + echo "
$title
\n"; + } + echo "

\n"; + echo "
\n"; + + } } \ No newline at end of file diff --git a/templates/admin/header.php b/templates/admin/header.php index b70e2e7..75d70b6 100755 --- a/templates/admin/header.php +++ b/templates/admin/header.php @@ -10,7 +10,7 @@
- +
  • 前台首页
  • diff --git a/templates/admin/link_list.php b/templates/admin/link_list.php index 9feb99e..a351e57 100755 --- a/templates/admin/link_list.php +++ b/templates/admin/link_list.php @@ -21,11 +21,11 @@
    -
+
@@ -46,6 +46,9 @@ 编辑 删除 + + +
diff --git a/templates/admin/static/embed.js b/templates/admin/static/embed.js index c90440e..ad077fd 100755 --- a/templates/admin/static/embed.js +++ b/templates/admin/static/embed.js @@ -783,4 +783,29 @@ function set_link_attribute(ids,property) { } }); } +} + +//导出所有链接 +function export_link(url, fileName) { + layer.confirm('导出的链接可以导入到浏览器也可以再次导入到OneNav!', {icon: 3, title:'确定导出所有链接?'}, function(index){ + var date = new Date(); + var current_time = date.toLocaleDateString(); + current_time = current_time.replaceAll("/","-"); + var url = "index.php?c=api&method=export_link"; + var fileName = "OneNav_Export_" + current_time + ".html"; + var x = new XMLHttpRequest(); + x.open("GET", url, true); + x.responseType = 'blob'; + x.onload=function(e) { + var url = window.URL.createObjectURL(x.response) + var a = document.createElement('a'); + a.href = url + a.download = fileName; + a.click() + } + x.send(); + + layer.close(index); + }); + } \ No newline at end of file From e286a2e32cc446f70552f5f350b863adcce58e38 Mon Sep 17 00:00:00 2001 From: xiaoz Date: Mon, 9 May 2022 17:42:42 +0800 Subject: [PATCH 05/12] update --- templates/default/index.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/templates/default/index.php b/templates/default/index.php index da186fb..5af60d7 100755 --- a/templates/default/index.php +++ b/templates/default/index.php @@ -209,6 +209,9 @@ ?>