public function actionEditconfig($cid)
 {
     $configModel = Config::model();
     $configInfo = $configModel->findByPk($cid);
     $configInfotmp = $configInfo;
     Yii::import('application.vendors.*');
     require_once 'Qiniu/rs.php';
     require_once 'Qiniu/io.php';
     $bucket = Yii::app()->params['bucket'];
     $accessKey = Yii::app()->params['accessKey'];
     $secretKey = Yii::app()->params['secretKey'];
     if (isset($_POST['Config'])) {
         $post = $_POST['Config'];
         $configInfo->attributes = $post;
         if ($configInfo->validate()) {
             $images = "";
             if (!empty($_FILES['icon']['tmp_name'])) {
                 $file = $_FILES['icon'];
                 $type = ".jpg";
                 if (ImageTypeCheck($file['name'], $file['size'])) {
                     $newname = time() . rand(10000, 99999) . $type;
                     Qiniu_SetKeys($accessKey, $secretKey);
                     $putPolicy = new Qiniu_RS_PutPolicy($bucket);
                     $upToken = $putPolicy->Token(null);
                     list($ret, $err) = Qiniu_Put($upToken, $newname, file_get_contents($file['tmp_name']), null);
                     if ($err === null) {
                         //ok
                         $images = Yii::app()->params['resurl'] . $newname;
                     } else {
                         //no
                     }
                 }
                 //Get original Pic and Del
                 $icon = GetImageFileName($configInfotmp->icon);
                 if (!empty($icon)) {
                     Qiniu_SetKeys($accessKey, $secretKey);
                     $client = new Qiniu_MacHttpClient(null);
                     $err = Qiniu_RS_Delete($client, $bucket, $icon[0]);
                 }
             } else {
                 $images = $configInfo->icon;
             }
             $configInfo->icon = $images;
             if (!$configInfo->save()) {
                 Yii::app()->user->setFlash('state', "Sorry,系统错误,修改配置失败 :(");
             } else {
                 $this->redirect(array('webconfig'));
             }
         }
     }
     $data = array('editModel' => $configInfo);
     $this->render('editconfig', $data);
 }
 public function actionG()
 {
     $uid = $_POST['who'];
     $tag = $_POST['taid'];
     $key = $_POST['keys'];
     $offset = $_POST['offset'];
     $pagesize = Yii::app()->params['blogCount'];
     $cutSize = 150;
     $where = array();
     $where[] = "hide=0";
     if (!empty($tag)) {
         $where[] = "b.labelid = {$tag}";
     }
     if (!empty($key)) {
         $where[] = "b.title like '%" . $key . "%'";
     }
     $where = implode(" and ", $where);
     $sql = "select b.*,b.id as bid,l.label,u.id from {{user}} u, {{blog}} b,{{label}} l " . "where b.userid = {$uid} and b.userid = u.id and b.labelid = l.id and {$where} order by b.time desc limit " . ($offset - 1) * $pagesize . "," . $pagesize;
     $blogs = Yii::app()->db->createCommand($sql)->queryAll();
     $sql = "select count(*) as allrecords from {{blog}} b where b.userid = {$uid} and hide=0 ";
     $allRecord = Yii::app()->db->createCommand($sql)->queryAll();
     $allPages = ceil($allRecord[0]['allrecords'] / $pagesize);
     foreach ($blogs as $key => $b) {
         $blogs[$key]['image'] = !empty($b['image']) ? GetImageFileName($b['image']) : "";
         if (date("Y-m-d") == date("Y-m-d", $b['time'])) {
             $blogs[$key]['time'] = "今天 " . date("H:i:s", $b['time']);
         } else {
             if (date("Y-m-d", strtotime("-1 day")) == date("Y-m-d", $b['time'])) {
                 $blogs[$key]['time'] = "昨天 " . date("H:i:s", $b['time']);
             } else {
                 if (date("Y-m-d", strtotime("-2 day")) == date("Y-m-d", $b['time'])) {
                     $blogs[$key]['time'] = "前天 " . date("H:i:s", $b['time']);
                 } else {
                     $blogs[$key]['time'] = date("Y-m-d H:i:s", $b['time']);
                 }
             }
         }
         //$blogs[$key]['content'] = Helper::WordTruncate(strip_tags($b['content']),$cutSize,false);
         $blogs[$key]['content'] = "";
         $blogs[$key]['blogurl'] = $this->createUrl("index/c", array('who' => $b['userid'], 'id' => $b['bid']));
     }
     $data['blogs'] = $blogs;
     $data['url'] = Yii::app()->params['resurl'];
     $data['allpages'] = $allPages;
     $data['adminurl'] = $this->createUrl("/admin/admin/index");
     echo json_encode($data);
 }
 public function actionT($uid, $id)
 {
     $this->Myaccess($uid);
     //加载布局uid
     $this->uid = $uid;
     $data = array('uid' => $uid, 'url' => "http://" . Yii::app()->params['bucket'] . "." . Yii::app()->params['domain'] . "/");
     //关于我
     $userModel = new User();
     $data['userInfo'] = $userModel->findByPk($uid);
     //相册
     $photoModel = new Photo();
     $sql = "select * from {{photo}} where userid = {$uid} order by rand() limit 0,6";
     $data['photoInfo'] = $photoModel->findAllBySql($sql);
     //标签
     $data['labelInfo'] = $this->GetTags($uid);
     //文章列表
     $cutSize = 200;
     //截取字符数
     $sql = "select b.*,b.id as bid,l.label,u.* from {{user}} u, {{blog}} b,{{label}} l where b.userid = {$uid} and b.userid = u.id and b.labelid = l.id ";
     $order = " order by b.time desc  ";
     if ($id != "all") {
         $sql = $sql . "and b.labelid = {$id} " . $order;
     } else {
         $sql = $sql . $order;
     }
     $criteria = new CDbCriteria();
     $result = Yii::app()->db->createCommand($sql)->query();
     $pages = new CPagination($result->rowCount);
     $pages->pageSize = Yii::app()->params['blogCount'];
     $pages->applyLimit($criteria);
     $result = Yii::app()->db->createCommand($sql . " LIMIT :offset,:limit");
     $result->bindValue(':offset', $pages->currentPage * $pages->pageSize);
     $result->bindValue(':limit', $pages->pageSize);
     $blogs = $result->queryAll();
     /**
      * 结果集处理
      * 1.图片缩放
      * 2.截取文字
      */
     foreach ($blogs as $key => $b) {
         $blogs[$key]['image'] = GetImageFileName($b['image']);
         $blogs[$key]['content'] = Helper::WordTruncate(strip_tags($b['content']), $cutSize, false);
     }
     //分两种方式显示
     $data['pages'] = $pages;
     $data['blogs'] = $blogs;
     $this->render('index', $data);
 }
 public function actionEditblog($id)
 {
     $label = new Article();
     $labels = $label->findAll('userid=:uid', array(':uid' => Yii::app()->session['uid']));
     //$labesArray是对应于id,label键值的一维数组
     $labesArray[0] = '--请选择--';
     foreach ($labels as $l) {
         $labesArray[$l->id] = $l->label;
     }
     $blog = Blog::model();
     $blogInfo = $blog->findByPk($id);
     $blogInfotmp = $blogInfo;
     //存储到session[blogid] 防止imageUP后台处理时有添加一条记录到blog表中
     Yii::app()->session['blogid'] = $id;
     //七牛上传类
     Yii::import('application.vendors.*');
     require_once 'Qiniu/io.php';
     require_once 'Qiniu/rs.php';
     $bucket = Yii::app()->params['bucket'];
     $accessKey = Yii::app()->params['accessKey'];
     $secretKey = Yii::app()->params['secretKey'];
     if (isset($_POST['Blog'])) {
         $post = $_POST['Blog'];
         //$post['view'] = 0;
         $post['time'] = time();
         $post['userid'] = Yii::app()->session['uid'];
         $blogInfo->attributes = $post;
         if ($blogInfo->validate()) {
             /**
              * 处理图片
              * require_once 'UPimage/UPimage.php';		//图片缩略类
              * $resizeimage = new resizeimage("1.jpg", "320", "240", "1","2.jpg");
              * 1.缩略图
              * 2.文章图片 删除就行  添加记录由后台异步处理	//删除文章时处理
              */
             //echo CheckUploadFiles($_FILES['thumb']);die;
             //1
             //a 查出缩略图片并 b删除七牛上的图片	传递一个特殊的字符串
             $images = "";
             //拼接图片以@方式
             if (CheckUploadFiles($_FILES['thumb'])) {
                 //删除图片
                 $file = $_FILES['thumb'];
                 $type = ".jpg";
                 if (count($file['name']) != 0) {
                     foreach ($file['name'] as $key => $f) {
                         if (ImageTypeCheck($file['name'][$key], $file['size'][$key])) {
                             $newname = time() . rand(10000, 99999) . $type;
                             Qiniu_SetKeys($accessKey, $secretKey);
                             $putPolicy = new Qiniu_RS_PutPolicy($bucket);
                             $upToken = $putPolicy->Token(null);
                             list($ret, $err) = Qiniu_Put($upToken, $newname, file_get_contents($file['tmp_name'][$key]), null);
                             if ($err === null) {
                                 //成功
                                 $images .= "http://" . Yii::app()->params['bucket'] . "." . Yii::app()->params['domain'] . "/" . $newname . "@";
                             } else {
                                 //失败
                             }
                         }
                     }
                 }
                 //删除图片
                 $imgs = GetImageFileName($blogInfotmp->image);
                 Qiniu_SetKeys($accessKey, $secretKey);
                 $client = new Qiniu_MacHttpClient(null);
                 foreach ($imgs as $i) {
                     if (!isset($client)) {
                         $client = new Qiniu_MacHttpClient(null);
                     }
                     $err = Qiniu_RS_Delete($client, $bucket, $i);
                 }
             } else {
                 //没有上传图片 不做任何操作 保持原来的图片
                 $images = $blogInfo->image;
             }
             //$post['image'] = $images;
             $blogInfo->attributes = $post;
             //2
             //删除文章文字配图 和业务数据库中的记录
             //					Qiniu_SetKeys($accessKey, $secretKey);
             //					$client = new Qiniu_MacHttpClient(null);
             //					$imgageModel = Image::model();
             //					$textImage = $imgageModel->findAll('blogid=:bid',array('bid'=>$id));
             //					foreach ($textImage as $ti){
             //						if(!isset($client))
             //								$client = new Qiniu_MacHttpClient(null);
             //
             //							$err = Qiniu_RS_Delete($client, $bucket, $ti->image);
             //					}
             //					$imgageModel->deleteAll('blogid=:bid',array('bid'=>$id));
             $blogInfo->image = $images;
             $blogInfo->content = $_POST['content'];
             if ($blogInfo->save()) {
                 /**
                  * 注销blogid	必须的
                  */
                 Yii::app()->session['blogid'] = null;
                 Yii::app()->user->setFlash('sendblogsuccess', '修改文章成功 :)');
                 $this->redirect(array('articles'));
             }
         }
     }
     $data = array('labels' => $labesArray, 'blogModel' => $blogInfo);
     $this->render('editblog', $data);
 }