|
|
|
@ -59,18 +59,35 @@ class Api {
@@ -59,18 +59,35 @@ class Api {
|
|
|
|
|
if( empty($id) ){ |
|
|
|
|
$this->err_msg(-1003,'The category ID cannot be empty!'); |
|
|
|
|
} |
|
|
|
|
//根据fid查询这个分类是否存在 |
|
|
|
|
$count = $this->db->count("on_categorys", [ |
|
|
|
|
"id" => $fid |
|
|
|
|
]); |
|
|
|
|
|
|
|
|
|
//如果fid不是0,且查询结果小于1,则认为这个父级ID是不存在的,则不允许修改 |
|
|
|
|
if( !empty($fid) && ($count < 1) ) { |
|
|
|
|
$this->err_msg(-2000,'父级ID不存在!'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//查询fid是否是二级分类的ID,如果是,则不允许修改 |
|
|
|
|
$category = $this->db->get("on_categorys","*",[ |
|
|
|
|
"id" => $fid |
|
|
|
|
]); |
|
|
|
|
//如果查询到他的父ID不是0,则是一个二级分类 |
|
|
|
|
if( intval($category['fid']) !== 0 ) { |
|
|
|
|
$this->err_msg(-2000,'父分类不能是二级分类!'); |
|
|
|
|
} |
|
|
|
|
//如果分类名为空 |
|
|
|
|
elseif( empty($name ) ){ |
|
|
|
|
$this->err_msg(-1004,'The category name cannot be empty!'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//更新数据库 |
|
|
|
|
else{ |
|
|
|
|
//根据分类ID查询改分类下面是否已经存在子分类,如果存在子分类了则不允许设置为子分类,实用情况:一级分类下存在二级分类,无法再将改一级分类修改为二级分类 |
|
|
|
|
$count = $this->db->count("on_categorys", [ |
|
|
|
|
"fid" => $id |
|
|
|
|
]); |
|
|
|
|
//改分类下的子分类数量大于0,并且将父级ID修改为其它分类 |
|
|
|
|
//该分类下的子分类数量大于0,并且父级ID修改为其它分类 |
|
|
|
|
if( ( $count > 0 ) && ( $fid !== 0 ) ) { |
|
|
|
|
$this->err_msg(-2000,'修改失败,该分类下已存在子分类!'); |
|
|
|
|
} |
|
|
|
@ -245,6 +262,26 @@ class Api {
@@ -245,6 +262,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 +360,129 @@ class Api {
@@ -323,6 +360,129 @@ 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("/<DT><H3.+>(.*)<\/H3>/i",$HTMLh,$category) ){ |
|
|
|
|
//匹配到文件夹名时加入数组 |
|
|
|
|
array_push($categoryt,$category[1]); |
|
|
|
|
array_push($categorys,$category[1]); |
|
|
|
|
}elseif( preg_match('/<\/DL><p>/i',$HTMLh) ){ |
|
|
|
|
//匹配到文件夹结束标记时删除一个 |
|
|
|
|
array_pop($categorys); |
|
|
|
|
}elseif( preg_match('/<DT><A HREF="(.+)" ADD_DAT.+>(.+)<\/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); |
|
|
|
|
//追加一个默认分类,用来存储部分链接找不到分类的情况 |
|
|
|
|
array_push($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']); |
|
|
|
|
//如果链接的分类是空的,则设置为默认分类 |
|
|
|
|
$category_name = empty( $category_name ) ? "默认分类" : $category_name; |
|
|
|
|
|
|
|
|
|
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 |
|
|
|
|
]; |
|
|
|
|
|
|
|
|
|
//插入数据库 |
|
|
|
|
$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) { |
|
|
|
|
$value = empty($value) ? "默认分类" : $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:上传类型,默认为上传书签,后续类型保留使用 |
|
|
|
@ -357,6 +517,40 @@ class Api {
@@ -357,6 +517,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:修改链接 |
|
|
|
|
*/ |
|
|
|
@ -690,7 +884,7 @@ class Api {
@@ -690,7 +884,7 @@ class Api {
|
|
|
|
|
* 验证是否登录 |
|
|
|
|
*/ |
|
|
|
|
protected function is_login(){ |
|
|
|
|
$key = md5(USER.PASSWORD.'onenav'); |
|
|
|
|
$key = md5(USER.PASSWORD.'onenav'.$_SERVER['HTTP_USER_AGENT']); |
|
|
|
|
//获取session |
|
|
|
|
$session = $_COOKIE['key']; |
|
|
|
|
//如果已经成功登录 |
|
|
|
@ -1118,6 +1312,13 @@ class Api {
@@ -1118,6 +1312,13 @@ class Api {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
/** |
|
|
|
|
* 用户状态 |
|
|
|
|
*/ |
|
|
|
|
public function check_login(){ |
|
|
|
|
$status = $this->is_login() ? "true" : "false"; |
|
|
|
|
$this->return_json(200,$status,""); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|