/**
     * 保存分享数据
     * 注:所有图片地址经处理过并转存过的临时图片或远程图片
     * $data = array( //分享的基本数据
     *  'share'=>array(
     * 	  'uid'=> xxx, //分享的会员ID
     * 	  'parent_id'	=>	xxx //转发的分享ID
     * 	  'content'	=>	xxx //分享的内容
     * 	  'type'=> xxx  //分享的来源,默认为default
     *    'title' => xxx //分享的标题
     *    'base_id' => xxx //原创ID
     * 	),
     *
     *  'share_photo'=>array( //图库  #可选#
     *    array(  //多张图
     *    'img' => xxx //原图
     *    )
     *  ),
     *  'share_goods'=>array( //分享的商品 #可选#
     *    array(
     *    'img' => xxx  //商品图
     *    'name' => xxx //品名
     *    'url'  => xxx //商品地址
     *    'price' => xxx  //价格
     *    'shop_name' => xxx //商户名称
     *    'shop_logo' => xxx //商户的logo
     *    'shop_url' => xxx //商户地址
     *    ) //多个商品
     *  ),
     *  'share_tag' => array(xxx,xxx,xxx),  //该分享的标签
     * );
     *
     * 返回
     * array(
     *   'status' => xxx  状态  bool
     *   'share_id' => share_id
     * )
     */
    public function save($data, $is_score = true)
    {
        global $_FANWE;
        //保存分享数据
        $share_data = $data['share'];
        $share_album_id = (int) $share_data['albumid'];
        unset($share_data['albumid']);
        $share_data['create_time'] = TIME_UTC;
        $share_data['day_time'] = getTodayTime();
        $share_id = FDB::insert('share', $share_data, true);
        if (intval($share_id) > 0) {
            $share_data_now_type = $share_data['type'];
            $share_data_rec_id = $share_data['rec_id'];
            $share_server_code = array();
            if (empty($share_data_now_type)) {
                $share_data_now_type = 'default';
            }
            /*//是否是回复 是的 话 添加评论消息提示
            		if(intval($share_data['parent_id']) > 0)
            		{
            			$base_share_id = FDB::resultFirst("select uid from ".FDB::table('share')." where share_id = ".$share_data['parent_id']);
            			$result = FDB::query("INSERT INTO ".FDB::table('user_notice')."(uid, type, num, create_time) VALUES('$base_share_id',3,1,'".TIME_UTC."')", 'SILENT');
            			if(!$result)
            				FDB::query("UPDATE ".FDB::table('user_notice')." SET num = num + 1, create_time='".TIME_UTC."' WHERE uid='$base_share_id' AND type=3");
            		}*/
            //保存话题
            $is_event = false;
            $event_list = array();
            $pattern = "/#([^\f\n\r\t\v]{1,80}?)#/";
            preg_match_all($pattern, $share_data['content'], $event_list);
            if (!empty($event_list[1])) {
                array_unique($event_list[1]);
                foreach ($event_list[1] as $v) {
                    $event_id = (int) FDB::resultFirst("select id from " . FDB::table("event") . " where `title`='" . $v . "'");
                    if ($event_id == 0) {
                        $event_data = array();
                        $event_data['uid'] = $share_data['uid'];
                        $event_data['title'] = $v;
                        $event_data['share_id'] = $share_id;
                        $event_data['create_time'] = TIME_UTC;
                        $event_data['last_share'] = $share_id;
                        $event_data['last_time'] = TIME_UTC;
                        FDB::insert("event", $event_data);
                    } else {
                        $event_data = array();
                        $event_data['event_id'] = $event_id;
                        $event_data['uid'] = $share_data['uid'];
                        $event_data['share_id'] = $share_id;
                        FDB::insert("event_share", $event_data);
                        FDB::query("update " . FDB::table("event") . " set thread_count = thread_count+1,last_share=" . $share_id . ",last_time=" . TIME_UTC . " where id = {$event_id}");
                    }
                }
            }
            $share_cates = array();
            $result['status'] = true;
            $result['share_id'] = $share_id;
            /*$content_match = FS('Words')->segment(clearExpress($share_data['content']),100);
            		$title_tags = FS('Words')->segment($share_data['title'],100);
                       if(!empty($title_tags))
            			$content_match = array_merge($content_match, $title_tags);*/
            $content_match = clearExpress($share_data['content']);
            $content_match .= $share_data['title'];
            $is_rel_imgage = false;
            $weibo_img = '';
            $weibo_img_sort = 100000;
            $photo_count = 0;
            $server_args = array();
            FS("Image")->getImageArgs(&$server_args);
            //保存分享图片
            $share_photo = $data['share_photo'];
            foreach ($share_photo as $k => $photo) {
                if ($photo_count >= $_FANWE['setting']['share_pic_count']) {
                    break;
                }
                $o_img = false;
                if (FS("Image")->getIsServer() && !empty($photo['server_code'])) {
                    $server = FS("Image")->getServer($photo['server_code']);
                    if (!empty($server)) {
                        $server_args['share_id'] = $share_id;
                        $server_args['img_path'] = $photo['img'];
                        $server = FS("Image")->getImageUrlToken($server_args, $server, 1);
                        $body = FS("Image")->sendRequest($server, 'saveshare', true);
                        if (!empty($body)) {
                            $o_img = unserialize($body);
                            FS("Image")->setServerUploadCount($o_img['server_code']);
                            $share_server_code[] = $o_img['server_code'];
                            $o_img['url'] = str_replace('./', './' . $o_img['server_code'] . '/', $o_img['url']);
                            $weibo_img_url = FS("Image")->getImageUrl($o_img['url'], 1);
                        }
                    }
                } else {
                    $o_img = copyImage($photo['img'], array(), 'share', true, $share_id);
                    $weibo_img_url = FS("Image")->getImageUrl($o_img['url'], 1);
                }
                if (!empty($o_img)) {
                    if ($photo['sort'] < $weibo_img_sort) {
                        $weibo_img = $weibo_img_url;
                        $weibo_img_sort = $photo['sort'];
                    }
                    $share_photo_data['uid'] = $_FANWE['uid'];
                    $share_photo_data['share_id'] = $share_id;
                    $share_photo_data['img'] = $o_img['url'];
                    $share_photo_data['type'] = $photo['type'];
                    $share_photo_data['sort'] = $photo['sort'];
                    $share_photo_data['img_width'] = $o_img['width'];
                    $share_photo_data['img_height'] = $o_img['height'];
                    $share_photo_data['server_code'] = $photo['server_code'];
                    FDB::insert('share_photo', $share_photo_data, true);
                    $photo_count++;
                }
            }
            //保存引用图片
            if (isset($data['rel_photo'])) {
                $share_photo = $data['rel_photo'];
                foreach ($share_photo as $share_photo_data) {
                    if ($photo_count >= $_FANWE['setting']['share_pic_count']) {
                        break;
                    }
                    $is_rel_imgage = true;
                    if ($share_photo_data['sort'] < $weibo_img_sort) {
                        $weibo_img = FS("Image")->getImageUrl($share_photo_data['img'], 1);
                        $weibo_img_sort = $share_photo_data['sort'];
                    }
                    $share_photo_data['uid'] = $_FANWE['uid'];
                    $share_photo_data['share_id'] = $share_id;
                    FDB::insert('share_photo', $share_photo_data, true);
                    $photo_count++;
                }
            }
            $shop_ids = array();
            $goods_count = 0;
            //保存分享的商品
            if (isset($data['share_goods'])) {
                $share_goods = $data['share_goods'];
                foreach ($share_goods as $goods) {
                    if ($goods_count >= $_FANWE['setting']['share_goods_count']) {
                        break;
                    }
                    $shop_id = 0;
                    if (!empty($goods['shop_url'])) {
                        $shop_id = FDB::resultFirst('SELECT shop_id
							FROM ' . FDB::table('shop') . '
							WHERE shop_url = \'' . $goods['shop_url'] . '\'');
                        if (intval($shop_id) == 0) {
                            $content_match .= $goods['shop_name'];
                            $shop_logo['url'] = '';
                            if (!empty($goods['shop_logo'])) {
                                if (FS("Image")->getIsServer() && !empty($goods['shop_server_code'])) {
                                    $server = FS("Image")->getServer($goods['shop_server_code']);
                                    if (!empty($server)) {
                                        $args = array();
                                        $args['pic_url'] = $goods['shop_logo'];
                                        $server = FS("Image")->getImageUrlToken($args, $server, 1);
                                        $body = FS("Image")->sendRequest($server, 'saveshop', true);
                                        if (!empty($body)) {
                                            $shop_logo = unserialize($body);
                                            FS("Image")->setServerUploadCount($shop_logo['server_code']);
                                            $shop_logo['url'] = str_replace('./', './' . $shop_logo['server_code'] . '/', $shop_logo['url']);
                                        }
                                    }
                                } else {
                                    $shop_logo = copyFile($goods['shop_logo'], 'shop', true);
                                }
                            }
                            $shop_data['shop_name'] = $goods['shop_name'];
                            $shop_data['shop_logo'] = $shop_logo['url'];
                            $shop_data['server_code'] = $goods['shop_server_code'];
                            $shop_data['shop_url'] = $goods['shop_url'];
                            $shop_data['taoke_url'] = $goods['shop_taoke_url'];
                            $shop_id = FDB::insert('shop', $shop_data, true);
                        }
                        if ($shop_id > 0) {
                            $shop_ids[] = $shop_id;
                        }
                    }
                    if (FS("Image")->getIsServer() && !empty($goods['server_code'])) {
                        $server = FS("Image")->getServer($goods['server_code']);
                        if (!empty($server)) {
                            $server_args['share_id'] = $share_id;
                            $server_args['img_path'] = $goods['img'];
                            $server = FS("Image")->getImageUrlToken($server_args, $server, 1);
                            $body = FS("Image")->sendRequest($server, 'saveshare', true);
                            if (!empty($body)) {
                                $goods_img = unserialize($body);
                                FS("Image")->setServerUploadCount($goods_img['server_code']);
                                $share_server_code[] = $goods_img['server_code'];
                                $goods_img['url'] = str_replace('./', './' . $goods_img['server_code'] . '/', $goods_img['url']);
                                $weibo_img_url = FS("Image")->getImageUrl($goods_img['url'], 1);
                            }
                        }
                    } else {
                        $goods_img = copyImage($goods['img'], array(), 'share', true, $share_id);
                        $weibo_img_url = FS("Image")->getImageUrl($goods_img['url'], 1);
                    }
                    if (!empty($goods_img)) {
                        if ($goods['sort'] < $weibo_img_sort) {
                            $weibo_img = $weibo_img_url;
                            $weibo_img_sort = $goods['sort'];
                        }
                        $shop_id = intval($shop_id);
                        //开始保存分享的商品
                        $share_goods_data['uid'] = $_FANWE['uid'];
                        $share_goods_data['share_id'] = $share_id;
                        $share_goods_data['shop_id'] = $shop_id;
                        $share_goods_data['img'] = $goods_img['url'];
                        $share_goods_data['name'] = $goods['name'];
                        $share_goods_data['url'] = $goods['url'];
                        $share_goods_data['price'] = $goods['price'];
                        $share_goods_data['sort'] = $goods['sort'];
                        $share_goods_data['taoke_url'] = $goods['taoke_url'];
                        $share_goods_data['goods_key'] = $goods['goods_key'];
                        $share_goods_data['img_width'] = $goods_img['width'];
                        $share_goods_data['img_height'] = $goods_img['height'];
                        $share_goods_data['server_code'] = $goods['server_code'];
                        FDB::insert('share_goods', $share_goods_data, true);
                        $goods_tags = FS('Words')->segment($goods['name'], 10);
                        if (!empty($goods_tags)) {
                            $share_cates[] = ShareService::getCateByTags($goods_tags);
                        }
                        $content_match .= $goods['name'];
                        $goods_count++;
                    }
                }
            }
            //保存引用商品
            if (isset($data['rel_goods'])) {
                $share_goods = $data['rel_goods'];
                foreach ($share_goods as $share_goods_data) {
                    if ($goods_count >= $_FANWE['setting']['share_goods_count']) {
                        break;
                    }
                    $is_rel_imgage = true;
                    $shop_ids[] = $share_goods_data['shop_id'];
                    if ($share_goods_data['sort'] < $weibo_img_sort) {
                        $weibo_img = $weibo_img = FS("Image")->getImageUrl($share_goods_data['img'], 1);
                        $weibo_img_sort = $share_goods_data['sort'];
                    }
                    $share_goods_data['uid'] = $_FANWE['uid'];
                    $share_goods_data['share_id'] = $share_id;
                    FDB::insert('share_goods', $share_goods_data, true);
                    $goods_tags = FS('Words')->segment($goods['name'], 10);
                    if (!empty($goods_tags)) {
                        $share_cates[] = ShareService::getCateByTags($goods_tags);
                    }
                    $content_match .= $goods['name'];
                    $goods_count++;
                }
            }
            if ($goods_count > 0 && $photo_count > 0) {
                $share_data_type = 'goods_photo';
            } elseif ($goods_count > 0) {
                $share_data_type = 'goods';
            } elseif ($photo_count > 0) {
                $share_data_type = 'photo';
            } else {
                $share_data_type = 'default';
            }
            $update_share = array();
            $update_share['share_data'] = $share_data_type;
            if (count($share_server_code) > 0) {
                $share_server_code = array_unique($share_server_code);
                $update_share['server_code'] = implode(',', $share_server_code);
            }
            if ($share_album_id > 0 && in_array($share_data_type, array('goods', 'photo', 'goods_photo'))) {
                $album = FDB::fetchFirst('SELECT cid,id,title FROM ' . FDB::table('album') . ' WHERE id = ' . $share_album_id);
                if ($album) {
                    $update_share['type'] = 'album_item';
                    $share_data_now_type = 'album_item';
                    $share_data_rec_id = $album['id'];
                    $share_data_rec_cate = $album['cid'];
                    $update_share['rec_id'] = $album['id'];
                    $update_share['title'] = addslashes($album['title']);
                } else {
                    $update_share['rec_id'] = 0;
                    $share_data_rec_id = 0;
                }
            } else {
                $share_data_rec_id = 0;
            }
            FDB::update("share", $update_share, "share_id=" . $share_id);
            //更新会员统计
            FDB::query('UPDATE ' . FDB::table('user_count') . '
				SET shares = shares + 1,goods = goods + ' . $goods_count . ',photos = photos + ' . $photo_count . '
				WHERE uid = ' . $share_data['uid']);
            FS('Medal')->runAuto($share_data['uid'], 'shares');
            FS('User')->medalBehavior($share_data['uid'], 'continue_share');
            switch ($share_data_type) {
                case 'goods_photo':
                    FS('Medal')->runAuto($share_data['uid'], 'goods');
                    FS('User')->medalBehavior($share_data['uid'], 'continue_goods');
                    FS('Medal')->runAuto($share_data['uid'], 'photos');
                    FS('User')->medalBehavior($share_data['uid'], 'continue_photo');
                    break;
                case 'goods':
                    FS('Medal')->runAuto($share_data['uid'], 'goods');
                    FS('User')->medalBehavior($share_data['uid'], 'continue_goods');
                    break;
                case 'photo':
                    FS('Medal')->runAuto($share_data['uid'], 'photos');
                    FS('User')->medalBehavior($share_data['uid'], 'continue_photo');
                    break;
            }
            if (in_array($share_data_type, array('goods', 'photo', 'goods_photo'))) {
                //更新会员发布的有图分享编号
                //FS('User')->setShareIds($share_data['uid'],$share_id);
                if (!empty($share_cates)) {
                    $cids = ShareService::getCidsByCates($share_cates);
                }
                if (!empty($data['share_tag'])) {
                    if (empty($cids)) {
                        $share_cates = array();
                        $share_cates[] = ShareService::getCateByTags($data['share_tag']);
                        $cids = ShareService::getCidsByCates($share_cates);
                    }
                    //$content_match = array_merge($content_match, $data['share_tag']);
                }
                //保存标签
                $share_tags = array();
                foreach ($data['share_tag'] as $tag) {
                    if (trim($tag) != '' && !in_array($tag, $share_tags)) {
                        array_push($share_tags, $tag);
                        //为已存在的tags更新统计
                        FDB::query('UPDATE ' . FDB::table('goods_tags') . '
							SET count = count + 1
							WHERE tag_name = \'' . $tag . '\'');
                        //数量大于100时为热门标签
                        FDB::query('UPDATE ' . FDB::table('goods_tags') . '
							SET is_hot = 1
							WHERE tag_name = \'' . $tag . '\' AND count >= 100');
                        $content_match .= $tag;
                        $tag_data = array();
                        $tag_data['share_id'] = $share_id;
                        $tag_data['tag_name'] = $tag;
                        FDB::insert('share_tags', $tag_data);
                    }
                }
                unset($share_tags);
                if (!empty($cids)) {
                    foreach ($cids as $cid) {
                        $cate_data = array();
                        $cate_data['share_id'] = $share_id;
                        $cate_data['cate_id'] = $cid;
                        FDB::insert('share_category', $cate_data);
                    }
                }
                //保存匹配查询
                $share_match['share_id'] = $share_id;
                $share_match['content_match'] = segmentToUnicode(clearSymbol($content_match));
                FDB::insert("share_match", $share_match);
            }
            ShareService::updateShareCache($share_id);
            if ($share_data_rec_id > 0) {
                $album_share = array();
                $album_share['album_id'] = $share_data_rec_id;
                $album_share['share_id'] = $share_id;
                $album_share['cid'] = $share_data_rec_cate;
                $album_share['create_day'] = getTodayTime();
                FDB::insert("album_share", $album_share);
                FS('Album')->updateAlbumByShare($share_data_rec_id, $share_id);
                FS('Album')->updateAlbum($share_data_rec_id);
            }
            if (count($shop_ids) > 0) {
                FS("Shop")->updateShopStatistic($shop_ids);
            }
            //保存提到我的
            $atme_share_type = FDB::resultFirst("select `type` from " . FDB::table("share") . " where `share_id`='" . $share_id . "'");
            if ($atme_share_type != "fav") {
                $atme_list = array();
                $pattern = "/@([^\f\n\r\t\v@ ]{2,20}?)(?:\\:| )/";
                preg_match_all($pattern, $share_data['content'], $atme_list);
                if (!empty($atme_list[1])) {
                    $atme_list[1] = array_unique($atme_list[1]);
                    $users = array();
                    foreach ($atme_list[1] as $user) {
                        if (!empty($user)) {
                            $users[] = $user;
                        }
                    }
                    $res = FDB::query('SELECT uid 
						FROM ' . FDB::table('user') . '
						WHERE user_name ' . FDB::createIN($users));
                    while ($data = FDB::fetch($res)) {
                        FS("User")->setUserTips($data['uid'], 4, $share_id);
                    }
                }
            }
            if ($is_score && !in_array($share_data_now_type, array('fav', 'album_best', 'album_rec'))) {
                if (!$is_rel_imgage && in_array($share_data_type, array('goods', 'photo', 'goods_photo'))) {
                    FS("User")->updateUserScore($share_data['uid'], 'share', 'image', $share_data['content'], $share_id);
                } else {
                    FS("User")->updateUserScore($share_data['uid'], 'share', 'default', $share_data['content'], $share_id);
                }
            }
            if ($data['pub_out_check']) {
                $weibo = array();
                $weibo['content'] = $share_data['content'];
                $weibo['img'] = $weibo_img;
                $weibo['ip'] = $_FANWE['client_ip'];
                $weibo['url'] = $_FANWE['site_url'] . FU('note/index', array('sid' => $share_id));
                $weibo['url'] = str_replace('//', '/', $weibo['url']);
                $weibo['url'] = str_replace(':/', '://', $weibo['url']);
                $weibo = base64_encode(serialize($weibo));
                if (empty($share_data['type'])) {
                    $share_data['type'] = 'default';
                }
                //转发到外部微博
                $uid = $_FANWE['uid'];
                $user_binds = FS("User")->getUserBindList($uid);
                $is_open = false;
                foreach ($user_binds as $class => $bind) {
                    if ($bind['sync'] && file_exists(FANWE_ROOT . "login/" . $class . ".php")) {
                        $check_field = "";
                        if (in_array($share_data['type'], array('bar', 'ask'))) {
                            $check_field = "topic";
                        } elseif ($share_data['type'] == 'default') {
                            $check_field = "weibo";
                        }
                        if ($bind['sync'][$check_field] == 1) {
                            $is_open = true;
                            //开始推送
                            $schedule['uid'] = $uid;
                            $schedule['type'] = $class;
                            $schedule['data'] = $weibo;
                            $schedule['pub_time'] = TIME_UTC;
                            FDB::insert('pub_schedule', $schedule, true);
                        }
                    }
                }
                if ($is_open) {
                    $fp = fsockopen($_SERVER['HTTP_HOST'], 80, &$errno, &$errstr, 5);
                    if ($fp) {
                        $request = "GET " . SITE_URL . "login.php?loop=true&uid=" . $uid . " HTTP/1.0\r\n";
                        $request .= "Host: " . $_SERVER['HTTP_HOST'] . "\r\n";
                        $request .= "Connection: Close\r\n\r\n";
                        fwrite($fp, $request);
                        while (!feof($fp)) {
                            fgets($fp, 128);
                            break;
                        }
                        fclose($fp);
                    }
                }
            }
        } else {
            $result['status'] = false;
        }
        return $result;
    }