diff --git a/application/config/routes.php b/application/config/routes.php index 1339115..539fa19 100644 --- a/application/config/routes.php +++ b/application/config/routes.php @@ -55,4 +55,7 @@ $route['translate_uri_dashes'] = FALSE; //API路由 $route['api/upload'] = 'upload/localhost'; $route['api/upload/parse'] = 'upload/parse'; -$route['api/upload/(:any)'] = 'upload/localhost/$1'; \ No newline at end of file +$route['api/upload/(:any)'] = 'upload/localhost/$1'; +$route['api/parse'] = '/upload/parse'; +//删除链接 +$route['delete/(:any)'] = '/del/token/$1'; \ No newline at end of file diff --git a/application/controllers/Del.php b/application/controllers/Del.php index 7536a67..8614f7d 100644 --- a/application/controllers/Del.php +++ b/application/controllers/Del.php @@ -1,5 +1,11 @@ load->model('query','',TRUE); + //加载数据库模型 + $this->load->model('delete','',TRUE); //加载类 $this->load->library('basic'); - //检测是否登录 - $this->basic->is_login(TRUE); - } - //根据img_images ID删除图片 + //根据img_images ID删除图片,需要检查用户是否登录 public function id($id){ + //检测是否登录 + $this->basic->is_login(TRUE); + @$id = (int)$id; $img = $this->query->img_id($id); @@ -38,5 +46,32 @@ $re = json_encode($re); echo $re; } + //根据token删除单张图片,不需要登录,只需要知道token即可 + public function token($value){ + //对value进行过滤 + $value = trim($value); + $value = strip_tags($value); + $len = strlen($value); + if($len !== 16){ + exit('不是有效的token!'); + } + //获取图片信息 + $img = $this->query->get_token($value); + //如果返回空,说明token不存在 + if($img === NULL){ + exit('token不存在,可能是图片已经被删除!'); + } + //删除图片 + //从数据库中删除 + $this->delete->del_img($img->imgid); + //从磁盘中删除 + $path = FCPATH.$img->path; + $thumbnail_path = FCPATH.$img->thumb_path; + //缩略图地址 + unlink($path); + unlink($thumbnail_path); + + echo '图片已删除!'; + } } ?> \ No newline at end of file diff --git a/application/controllers/Maintain.php b/application/controllers/Maintain.php index 62293af..2f420d3 100644 --- a/application/controllers/Maintain.php +++ b/application/controllers/Maintain.php @@ -58,6 +58,15 @@ // $query = $this->db->query($sql)->row(); // var_dump($query); } + //版本升级 + public function upgrade(){ + $data['admin_title'] = 'ImgURL升级'; + //加载视图 + $this->load->view('admin/header',$data); + $this->load->view('admin/left'); + $this->load->view('admin/upgrade'); + $this->load->view('admin/footer'); + } } ?> \ No newline at end of file diff --git a/application/controllers/Upgrade.php b/application/controllers/Upgrade.php new file mode 100644 index 0000000..f676118 --- /dev/null +++ b/application/controllers/Upgrade.php @@ -0,0 +1,32 @@ +load->library('basic'); + $this->basic->is_login(TRUE); + //加载模型 + $this->load->model('query','',TRUE); + } + public function v22_to_v23(){ + //升级数据库操作 + $result = $this->query->to23(); + if($result){ + echo '升级完毕,请关闭此页面!'; + } + else{ + echo '升级失败,未知错误!'; + } + } +} \ No newline at end of file diff --git a/application/controllers/Upload.php b/application/controllers/Upload.php index a5cf28d..975ecc0 100644 --- a/application/controllers/Upload.php +++ b/application/controllers/Upload.php @@ -21,6 +21,8 @@ public $temp; //用户是否已经登录的属性 protected $user; + //获取站点主域名 + protected $main_domain; //构造函数 public function __construct() { @@ -44,6 +46,8 @@ $this->load->library('basic'); //加载查询模型 $this->load->model('query','',TRUE); + $this->main_domain = $this->basic->domain(); + //用户已经登录 if($this->basic->is_login(FALSE)){ $this->user = 'admin'; @@ -155,7 +159,17 @@ } //图片没有上传过 else{ - //需要插入到images表的数据 + $arr = array( + "ip" => get_ip(), + "ua" => get_ua(), + "date" => $this->date + ); + + //生成token + $token = $this->token($arr); + //生成删除链接 + $delete = $this->main_domain.'/delete/'.$token; + //需要插入到img_images表的数据 $datas = array( "imgid" => $imgid, "path" => $relative_path, @@ -165,7 +179,8 @@ "ua" => get_ua(), "date" => $this->date, "user" => $this->user, - "level" => 'unknown' + "level" => 'unknown', + "token" => $token ); //需要插入到imginfo表的数据 $imginfo = array( @@ -189,7 +204,8 @@ "url" => $url, "thumbnail_url" => $thumbnail_url, "width" => $data['image_width'], - "height" => $data['image_height'] + "height" => $data['image_height'], + "delete" => $delete ); //根据不同的类型返回不同的数据 $this->re_data($type,$info); @@ -442,5 +458,22 @@ $this->succeed_msg($info); //echo $re; } + /* + 1. 该方法生成图片的唯一删除token + 2. 参数为一个数组,内容为IP/UA/DATE + 3. ip + ua + date + 4位随机数,进行md5加密得到token + */ + protected function token($arr){ + $ip = $arr['ip']; + $ua = $arr['ua']; + $date = $arr['date']; + //生成4位随机数 + $str = GetRandStr(4); + $token = $ip.$ua.$date.$str; + $token = md5($token); + //token只需要16位 + $token = substr($token, 8, 16); + return $token; + } } ?> \ No newline at end of file diff --git a/application/helpers/basic_helper.php b/application/helpers/basic_helper.php index 970dcd1..cb1a489 100644 --- a/application/helpers/basic_helper.php +++ b/application/helpers/basic_helper.php @@ -141,4 +141,24 @@ return $img['path']; } } + //生成4位随机数,方法来自:https://blog.csdn.net/happy_jijiawei/article/details/50581094 + function GetRandStr($len) + { + $chars = array( + "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", + "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", + "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", + "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", + "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", + "3", "4", "5", "6", "7", "8", "9" + ); + $charsLen = count($chars) - 1; + shuffle($chars); + $output = ""; + for ($i=0; $i<$len; $i++) + { + $output .= $chars[mt_rand(0, $charsLen)]; + } + return $output; + } ?> \ No newline at end of file diff --git a/application/libraries/Basic.php b/application/libraries/Basic.php index 5ca61af..0fd1599 100644 --- a/application/libraries/Basic.php +++ b/application/libraries/Basic.php @@ -134,5 +134,10 @@ break; } } + //获取站点主域名 + public function domain(){ + $domain = $this->CI->query->get_domain(); + return $domain; + } } ?> \ No newline at end of file diff --git a/application/models/Query.php b/application/models/Query.php index dd190ca..86d98ae 100644 --- a/application/models/Query.php +++ b/application/models/Query.php @@ -323,5 +323,46 @@ $datas = $this->db->query($sql)->result_array(); return $datas; } + //v2.2升级v2.3 + public function to23(){ + // $sql = 'alter table "img_images" ADD "token" TEXT(16) DEFAULT NULL; + // CREATE UNIQUE INDEX "token" ON "img_images" ("token" ASC); + // '; + $sqls = array( + 'alter table "img_images" ADD "token" TEXT(16) DEFAULT NULL;', + 'CREATE UNIQUE INDEX "token" ON "img_images" ("token" ASC)', + 'CREATE UNIQUE INDEX "imginfo_imgid" ON "img_imginfo" ("imgid" ASC)' + ); + //遍历SQL语句 + foreach ($sqls as $value) { + $datas = $this->db->query($value); + } + //var_dump($datas); + if($datas){ + return TRUE; + } + else{ + return FALSE; + } + } + //查询站点主域名 + public function get_domain() { + $sql = 'SELECT "values" FROM "img_options" WHERE `name` = "site_url"'; + $data = $this->db->query($sql)->row(); + + if($data){ + return $data->values; + } + else{ + return FALSE; + } + } + //根据token查询图片信息 + public function get_token($value){ + //先获取img id + $sql = "SELECT a.*,b.mime,b.width,b.height,b.views,b.ext,b.client_name FROM img_images AS a INNER JOIN img_imginfo AS b ON a.token = '{$value}' AND a.imgid = b.imgid"; + $imginfo = $this->db->query($sql)->row(); + return $imginfo; + } } ?> \ No newline at end of file diff --git a/application/views/admin/header.php b/application/views/admin/header.php index bf3fcc7..2a9b323 100644 --- a/application/views/admin/header.php +++ b/application/views/admin/header.php @@ -13,7 +13,7 @@
复制 | +||
Delete Link | ++ | 复制 | +
ImgURL v2.3发布
+