@@ -206,7 +206,7 @@ protected function auth($token){
206206 /**
207207 * name:添加链接
208208 */
209- public function add_link ($ token ,$ fid ,$ title ,$ url ,$ description = '' ,$ weight = 0 ,$ property = 0 ,$ url_standby = '' ,$ font_icon= '' ){
209+ public function add_link ($ token ,$ fid ,$ title ,$ url ,$ description = '' ,$ weight = 0 ,$ property = 0 ,$ url_standby = '' ,$ font_icon = '' ){
210210 $ this ->auth ($ token );
211211 $ fid = intval ($ fid );
212212 //检测链接是否合法
@@ -227,9 +227,13 @@ public function add_link($token,$fid,$title,$url,$description = '',$weight = 0,$
227227 'description ' => htmlspecialchars ($ description ,ENT_QUOTES ),
228228 'add_time ' => time (),
229229 'weight ' => $ weight ,
230- 'property ' => $ property ,
231- 'font_icon ' => $ font_icon
230+ 'property ' => $ property
232231 ];
232+
233+ //如果$font_icon不为空,才一起追加写入数据库
234+ if ( !empty ($ font_icon ) ) {
235+ $ data ['font_icon ' ] = $ font_icon ;
236+ }
233237 //插入数据库
234238 $ re = $ this ->db ->insert ('on_links ' ,$ data );
235239 //返回影响行数
@@ -539,11 +543,28 @@ public function upload($token,$type){
539543 * 图标上传
540544 * type:上传类型
541545 */
542- public function uploadImages ($ token, $ type ){
546+ public function uploadImages ($ token ){
543547 $ this ->auth ($ token );
548+ //获取icon名称
549+ $ icon_name = $ _POST ['icon_name ' ];
550+ //获取老文件名称,然后删除
551+ $ old_pic = $ _POST ['old_pic ' ];
552+ //如果老文件名称合法,则删除
553+ $ pattern = "/^data\/upload\/[0-9]+\/[0-9a-zA-Z]+\.(jpg|jpeg|png|bmp|gif|svg)$/ " ;
554+ //如果名称不合法,则终止执行
555+ if ( preg_match ($ pattern ,$ old_pic ) ){
556+ @unlink ($ old_pic );
557+ }
558+
559+ //如果名称是空的
560+ if ( empty ($ icon_name ) ) {
561+ $ this ->return_json (-2000 ,'' ,'获取图标名称失败! ' );
562+ }
563+
544564 if ($ _FILES ["file " ]["error " ] > 0 )
545565 {
546- $ this ->err_msg (-1015 ,'File upload failed! ' );
566+ //$this->err_msg(-1015,'File upload failed!');
567+ $ this ->return_json (-2000 ,'' ,'File upload failed! ' );
547568 }
548569 else
549570 {
@@ -555,20 +576,33 @@ public function uploadImages($token,$type){
555576
556577 //临时文件位置
557578 $ temp = $ _FILES ["file " ]["tmp_name " ];
558- if ( $ suffix != 'ico ' && $ suffix != 'jpg ' && $ suffix != 'png ' && $ suffix != 'bmp ' ) {
579+ if ( $ suffix != 'ico ' && $ suffix != 'jpg ' && $ suffix != 'jpeg ' && $ suffix != ' png ' && $ suffix != 'bmp ' && $ suffix != ' gif ' && $ suffix != ' svg ' ) {
559580 //删除临时文件
560- unlink ($ filename );
561- $ this ->err_msg (-1014 ,'Unsupported file suffix name! ' );
581+ @unlink ($ filename );
582+ @unlink ($ temp );
583+ $ this ->return_json (-2000 ,'' ,'Unsupported file suffix name! ' );
562584 }
563585
564- $ newfilename ='upload/ ' .time ().'. ' .$ suffix ;
565-
566- if ( copy ($ temp ,$ newfilename ) ) {
586+ //上传路径,格式为data/upload/202212/1669689755.png
587+ $ upload_path = "data/upload/ " .date ( "Ym " , time () ).'/ ' .$ icon_name .'. ' .$ suffix ;
588+
589+ //如果目录不存在,则创建
590+ $ upload_dir = dirname ($ upload_path );
591+ if ( !is_dir ( $ upload_dir ) ) {
592+ //递归创建目录
593+ mkdir ($ upload_dir ,0755 ,true );
594+ }
595+
596+ //$newfilename = 'upload/'.time().'.'.$suffix;
597+ //移动临时文件到指定上传路径
598+ if ( move_uploaded_file ($ temp ,$ upload_path ) ) {
567599 $ data = [
568- 'code ' => 0 ,
569- 'file_name ' => $ newfilename
600+ 'file_name ' => $ upload_path
570601 ];
571- exit (json_encode ($ data ));
602+ $ this ->return_json (200 ,$ data ,'success ' );
603+ }
604+ else {
605+ $ this ->return_json (-2000 ,'' ,'上传失败,请检查目录权限! ' );
572606 }
573607 }
574608 }
@@ -613,6 +647,12 @@ public function export_link(){
613647 public function edit_link ($ token ,$ id ,$ fid ,$ title ,$ url ,$ description = '' ,$ weight = 0 ,$ property = 0 ,$ url_standby = '' ,$ font_icon = '' ){
614648 $ this ->auth ($ token );
615649 $ fid = intval ($ fid );
650+ /**
651+ * name:获取更新类型
652+ * description:主要是因为兼容部分之前老的接口,老的接口不用变动,只能从OneNav后台添加图标,因此增加type判断是否是OneNav后台
653+ * console:指从OneNav后台进行更新
654+ */
655+ $ type = trim ($ _GET ['type ' ]);
616656 //检测链接是否合法
617657 //$this->check_link($fid,$title,$url);
618658 $ this ->check_link ([
@@ -636,9 +676,16 @@ public function edit_link($token,$id,$fid,$title,$url,$description = '',$weight
636676 'description ' => htmlspecialchars ($ description ,ENT_QUOTES ),
637677 'up_time ' => time (),
638678 'weight ' => $ weight ,
639- 'property ' => $ property ,
640- 'font_icon ' => $ font_icon
679+ 'property ' => $ property
641680 ];
681+
682+ if ( !empty ($ font_icon ) ) {
683+ $ data ['font_icon ' ] = $ font_icon ;
684+ }
685+ //如果是从OneNav后台更新,则无论如何都要加上font_icon
686+ if ( $ type === 'console ' ) {
687+ $ data ['font_icon ' ] = $ font_icon ;
688+ }
642689 //插入数据库
643690 $ re = $ this ->db ->update ('on_links ' ,$ data ,[ 'id ' => $ id ]);
644691 //返回影响行数
@@ -2226,6 +2273,37 @@ public function site_info() {
22262273
22272274 $ this ->return_json (200 ,$ site ,'success ' );
22282275 }
2276+
2277+ /**
2278+ * name:删除链接图标
2279+ */
2280+ public function del_link_icon (){
2281+ //验证授权
2282+ $ this ->auth ($ token );
2283+
2284+ //获取图标路径
2285+ $ icon_path = trim ($ _POST ['icon_path ' ]);
2286+ //正则判断路径是否合法
2287+ $ pattern = "/^data\/upload\/[0-9]+\/[0-9a-zA-Z]+\.(jpg|jpeg|png|bmp|gif|svg)$/ " ;
2288+ //如果名称不合法,则终止执行
2289+ if ( !preg_match ($ pattern ,$ icon_path ) ){
2290+ $ this ->return_json (-2000 ,'' ,'非法路径! ' );
2291+ }
2292+
2293+ //继续执行
2294+ //检查图标是否存在
2295+ if ( !is_file ($ icon_path ) ) {
2296+ $ this ->return_json (-2000 ,'' ,'图标文件不存在,无需删除! ' );
2297+ }
2298+
2299+ //执行删除操作
2300+ if ( unlink ($ icon_path ) ) {
2301+ $ this ->return_json (200 ,'' ,'success ' );
2302+ }
2303+ else {
2304+ $ this ->return_json (-2000 ,'' ,'图标删除失败,请检查目录权限! ' );
2305+ }
2306+ }
22292307
22302308}
22312309
0 commit comments