PageRenderTime 101ms CodeModel.GetById 81ms app.highlight 215ms RepoModel.GetById 117ms app.codeStats 1ms

/startZine/src/main/core/service/share.service.php

http://ownerpress.googlecode.com/
PHP | 2235 lines | 1668 code | 253 blank | 314 comment | 268 complexity | 7ec4764df97da91970104d514d95b57c MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1<?php
   2// +----------------------------------------------------------------------
   3// | ?????????? (Build on ThinkPHP)
   4// +----------------------------------------------------------------------
   5// | Copyright (c) 2011 http://fanwe.com All rights reserved.
   6// +----------------------------------------------------------------------
   7
   8/**
   9 * share.service.php
  10 *
  11 * ?????
  12 *
  13 * @package service
  14 * @author awfigq <awfigq@qq.com>
  15 */
  16class ShareService
  17{
  18	/*===========??????? Begin==============*/
  19	/**
  20	 * ?????????
  21	 * @param string $content ????
  22	 * @param string $type content,title,tag ????
  23	 * @return array(
  24	 *   'error_code' => '????'
  25	 *   'error_msg' => ????
  26	 * )
  27	 */
  28	public function checkWord(&$content,$type)
  29	{
  30		$result = array('error_code'=>0,'error_msg'=>'');
  31		$server = FS("ContentCheck");
  32		if($server->check($content) > 0)
  33		{
  34			$words_found = implode("?", $server->words_found);
  35			$tt_str = lang('share','word_'.$type);
  36			$result['error_code'] = $server->result;
  37			$result['error_msg'] = sprintf(lang('share','word_tips_'. $server->result),$tt_str,$words_found);
  38		}
  39		return $result;
  40	}
  41
  42	/**
  43	 * ??????????????
  44	 * @param mix $_POST ??????? $_POST['type'] default:??,bar:??,ershou:??,ask:??
  45	 * $_POST['share_data'] = photo ?? goods ??? goods_photo:????? default:???
  46	 * 	* ??
  47	 *  array(
  48	 *   'status' => xxx  ??  bool
  49	 *   'share_id' => share_id
  50	 *   'error_code' => '????'
  51	 *   'error_msg' => ????
  52	 * )
  53	 */
  54	public function submit($_POST,$is_check = true,$is_score = true,$is_album = FALSE)
  55	{
  56		//??????
  57		global $_FANWE;
  58		$share_content = htmlspecialchars(trim($_POST['content']));
  59		$share_data = array();
  60		$share_data['content'] = $share_content;
  61		
  62		$share_data['uid'] = intval($_FANWE['uid']);
  63		$share_data['parent_id'] = intval($_POST['parent_id']); //?????
  64		$share_data['rec_id'] = intval($_POST['rec_id']); //?????
  65		$share_data['base_id'] = intval($_POST['base_id']);
  66		$share_data['albumid'] = intval($_POST['albumid']);
  67		
  68		if($is_check)
  69		{
  70			$check_result = ShareService::checkWord($share_data['content'],'content');
  71			if($check_result['error_code'] == 1)
  72			{
  73				$check_result['status'] = false;
  74				return $check_result;
  75			}
  76		}
  77
  78		/*//????????????ID
  79		if($share_data['parent_id'] > 0 && $share_data['base_id'] == 0)
  80		{
  81			$base_id = intval(FDB::resultFirst('SELECT base_id
  82				FROM '.FDB::table("share").'
  83				WHERE share_id = '.$share_data['parent_id']));
  84
  85			$share_data['base_id'] = $base_id == 0 ? $share_data['parent_id'] : $base_id;
  86		}*/
  87
  88		if(isset($_POST['type']))
  89			$share_data['type'] = $_POST['type'];
  90
  91		$share_data['title'] = isset($_POST['title']) ? htmlspecialchars(trim($_POST['title'])) : '';
  92		if(!empty($share_data['title']) && $is_check)
  93		{
  94			$check_result = ShareService::checkWord($share_data['title'],'title');
  95			if($check_result['error_code'] == 1)
  96			{
  97				$check_result['status'] = false;
  98				return $check_result;
  99			}
 100		}
 101		
 102		$data['share'] = $share_data;
 103
 104		//????????
 105		$share_goods_data = array();
 106		if(isset($_POST['goods']) && is_array($_POST['goods']) && count($_POST['goods']) > 0)
 107		{
 108			$share_goods = $_POST['goods'];
 109			foreach($share_goods as $goods)
 110			{
 111				$goods = unserialize(authcode($goods,'DECODE'));
 112				$gkey = $goods['item']['key'];
 113				$c_data = array();
 114				$c_data['img'] = $goods['item']['img'];
 115				$c_data['goods_key'] = $gkey;
 116				$c_data['name'] = addslashes(htmlspecialchars($goods['item']['name']));
 117				$c_data['url'] = $goods['item']['url'];
 118				$c_data['taoke_url'] = $goods['item']['taoke_url'];
 119				$c_data['price'] = $goods['item']['price'];
 120				$c_data['sort'] = isset($_POST['goods_sort'][$gkey]) ? intval($_POST['goods_sort'][$gkey]) : 10;
 121				$c_data['shop_name'] = addslashes(htmlspecialchars($goods['shop']['name']));
 122				$c_data['shop_logo'] = $goods['shop']['logo'];
 123				$c_data['shop_url'] = $goods['shop']['url'];
 124				$c_data['shop_taoke_url'] = $goods['shop']['taoke_url'];
 125				array_push($share_goods_data,$c_data);
 126			}
 127		}
 128		$data['share_goods'] = $share_goods_data;
 129		//??????
 130		$share_photos_data = array();
 131		if(isset($_POST['pics']) && is_array($_POST['pics']) && count($_POST['pics']) > 0)
 132		{
 133			$share_photos = $_POST['pics'];
 134			foreach($share_photos as $pkey => $photo)
 135			{
 136				$photo = authcode($photo,'DECODE');
 137				$photo = unserialize($photo);
 138				$c_data = array();
 139				$c_data['img'] = $photo['path'];
 140				
 141				$type = $photo['type'];
 142				if(empty($type) || !in_array($type,array('default', 'dapei', 'look')))
 143					$type = 'default';
 144				$c_data['type'] = $type;
 145				$c_data['sort'] = isset($_POST['pics_sort'][$pkey]) ? intval($_POST['pics_sort'][$pkey]) : 10;
 146				array_push($share_photos_data,$c_data);
 147			}
 148		}
 149		$data['share_photo'] = $share_photos_data;
 150		
 151		if($share_data['albumid'] > 0 && count($share_photos_data) == 0 && count($share_goods_data) == 0)
 152			exit;
 153
 154		$data['share_tag'] = array();
 155
 156		if(isset($_POST['tags']) && trim($_POST['tags']) != '')
 157		{
 158			$tags = htmlspecialchars(trim($_POST['tags']));
 159			if($is_check)
 160			{
 161				$check_result = ShareService::checkWord($tags,'tag');
 162				if($check_result['error_code'] == 1)
 163				{
 164					$check_result['status'] = false;
 165					return $check_result;
 166				}
 167			}
 168			$tags = str_replace('?',' ',$tags);
 169			$data['share_tag'] = explode(' ',$tags);
 170		}
 171		
 172		$data['pub_out_check'] = intval($_POST['pub_out_check']);  //???????
 173		$result = ShareService::save($data,$is_score,$is_album);
 174		return $result;
 175	}
 176
 177	/**
 178	 * ??????
 179	 * ??????????????????????????
 180	 * $data = array( //???????
 181	 *  'share'=>array(
 182	 * 	  'uid'=> xxx, //?????ID
 183	 * 	  'parent_id'	=>	xxx //?????ID
 184	 * 	  'content'	=>	xxx //?????
 185	 * 	  'type'=> xxx  //?????????default
 186	 *    'title' => xxx //?????
 187	 *    'base_id' => xxx //??ID
 188	 * 	),
 189	 *
 190	 *  'share_photo'=>array( //??  #??#
 191	 *    array(  //???
 192	 *    'img' => xxx //??
 193	 *    )
 194	 *  ),
 195	 *  'share_goods'=>array( //????? #??#
 196	 *    array(
 197	 *    'img' => xxx  //???
 198	 *    'name' => xxx //??
 199	 *    'url'  => xxx //????
 200	 *    'price' => xxx  //??
 201	 *    'shop_name' => xxx //????
 202	 *    'shop_logo' => xxx //???logo
 203	 *    'shop_url' => xxx //????
 204	 *    ) //????
 205	 *  ),
 206	 *  'share_tag' => array(xxx,xxx,xxx),  //??????
 207	 * );
 208	 *
 209	 * ??
 210	 * array(
 211	 *   'status' => xxx  ??  bool
 212	 *   'share_id' => share_id
 213	 * )
 214	 */
 215	public function save($data,$is_score = true,$is_album = FALSE)
 216	{
 217		global $_FANWE;
 218		//??????
 219		$share_data = $data['share'];
 220		$share_album_id = (int)$share_data['albumid'];
 221		unset($share_data['albumid']);
 222		$share_data['create_time'] = TIME_UTC;
 223		$share_data['day_time'] = getTodayTime();
 224		$share_data['share_content_match'] = segmentToUnicode(strtolower(clearSymbol($share_data['content'])));
 225		
 226		
 227		$share_id = FDB::insert('share',$share_data,true);
 228		
 229		if(intval($share_id)>0)
 230		{
 231			$share_data_now_type = $share_data['type'];
 232			$share_data_rec_id = $share_data['rec_id'];
 233			if(empty($share_data_now_type))
 234				$share_data_now_type = 'default';
 235						
 236			/*//????? ?? ? ????????
 237			if(intval($share_data['parent_id']) > 0)
 238			{
 239				$base_share_id = FDB::resultFirst("select uid from ".FDB::table('share')." where share_id = ".$share_data['parent_id']);
 240				$result = FDB::query("INSERT INTO ".FDB::table('user_notice')."(uid, type, num, create_time) VALUES('$base_share_id',3,1,'".TIME_UTC."')", 'SILENT');
 241				if(!$result)
 242					FDB::query("UPDATE ".FDB::table('user_notice')." SET num = num + 1, create_time='".TIME_UTC."' WHERE uid='$base_share_id' AND type=3");
 243			}*/
 244			
 245			//????
 246			$is_event = false;
 247			$event_list = array();
 248			$pattern = "/#([^\f\n\r\t\v]{1,80}?)#/";
 249			preg_match_all($pattern,$share_data['content'],$event_list);
 250			if(!empty($event_list[1]))
 251			{
 252				array_unique($event_list[1]);
 253				foreach($event_list[1] as $v)
 254				{			
 255					$event_id = (int)FDB::resultFirst("select id from ".FDB::table("event")." where `title`='".$v."'");
 256					if($event_id == 0)
 257					{
 258						$event_data = array();
 259						$event_data['uid'] = $share_data['uid'];
 260						$event_data['title'] = $v;
 261						$event_data['share_id'] = $share_id;
 262						$event_data['create_time'] = TIME_UTC;
 263						$event_data['last_share'] = $share_id;
 264						$event_data['last_time'] = TIME_UTC;
 265						FDB::insert("event",$event_data);
 266					}
 267					else
 268					{
 269						$event_data = array();
 270						$event_data['event_id'] = $event_id;
 271						$event_data['uid'] = $share_data['uid'];
 272						$event_data['share_id'] = $share_id;
 273						FDB::insert("event_share",$event_data);
 274						FDB::query("update ".FDB::table("event")." set thread_count = thread_count+1,last_share=".$share_id.",last_time=".TIME_UTC." where id = $event_id");
 275					}
 276				}
 277			}
 278			
 279			$share_cates = array();
 280			$result['status'] = true;
 281			$result['share_id'] = $share_id;
 282			
 283			/*$content_match = FS('Words')->segment(clearExpress($share_data['content']),100);
 284			$title_tags = FS('Words')->segment($share_data['title'],100);
 285            if(!empty($title_tags))
 286				$content_match = array_merge($content_match, $title_tags);*/
 287
 288			$content_match = clearExpress($share_data['content']);
 289            $content_match .= $share_data['title'];
 290			
 291			$is_rel_imgage = false;
 292			$weibo_img = '';
 293			$weibo_img_sort = 100000;
 294			$photo_count = 0;
 295			
 296			$server_args = array();
 297			FS("Image")->getImageArgs(&$server_args);
 298			
 299			//??????
 300			if(isset($data['share_photo'])){
 301				$share_photo = $data['share_photo'];
 302				foreach($share_photo as $k=>$photo)
 303				{
 304					if($photo_count >= $_FANWE['setting']['share_pic_count'])
 305						break;
 306	
 307					$o_img = false;
 308					if(FS("Image")->getIsServer() && !empty($photo['server_code']))
 309					{
 310						$server = FS("Image")->getServer($photo['server_code']);
 311						if(!empty($server))
 312						{
 313							$server_args['share_id'] = $share_id;
 314							$server_args['img_path'] = $photo['img'];
 315							$server = FS("Image")->getImageUrlToken($server_args,$server,1);
 316							$body = FS("Image")->sendRequest($server,'saveshare',true);
 317							if(!empty($body))
 318							{
 319								$o_img = unserialize($body);
 320								FS("Image")->setServerUploadCount($o_img['server_code']);
 321								$share_server_code[] = $o_img['server_code'];
 322								$o_img['url'] = str_replace('./','./'.$o_img['server_code'].'/',$o_img['url']);
 323								$weibo_img_url = FS("Image")->getImageUrl($o_img['url'],1);
 324							}
 325						}
 326					}
 327					else
 328					{
 329						$o_img = copyImage($photo['img'],array(),'share',true,$share_id);
 330						$weibo_img_url = FS("Image")->getImageUrl($o_img['url'],1);
 331					}
 332	
 333					if(!empty($o_img))
 334					{
 335						if($photo['sort'] < $weibo_img_sort)
 336						{
 337							$weibo_img = $weibo_img_url;
 338							$weibo_img_sort = $photo['sort'];
 339						}
 340						$share_photo_data['uid'] = $_FANWE['uid'];
 341						$share_photo_data['share_id'] = $share_id;
 342						$share_photo_data['img'] =  $o_img['url'];
 343						$share_photo_data['type'] =  $photo['type'];
 344						$share_photo_data['sort'] =  $photo['sort'];
 345						$share_photo_data['img_width'] =  $o_img['width'];
 346						$share_photo_data['img_height'] =  $o_img['height'];
 347						$share_photo_data['server_code'] =  $photo['server_code'];
 348						FDB::insert('share_photo',$share_photo_data,true);
 349						$photo_count++;
 350					}
 351				}	
 352			}
 353			
 354			//??????
 355			if(isset($data['rel_photo']))
 356			{
 357				$share_photo = $data['rel_photo'];
 358				foreach($share_photo as $share_photo_data)
 359				{
 360					if($photo_count >= $_FANWE['setting']['share_pic_count'])
 361						break;
 362					
 363					$is_rel_imgage = true;
 364					if($share_photo_data['sort'] < $weibo_img_sort)
 365					{
 366						$weibo_img = $share_photo_data['img'];
 367						$weibo_img_sort = $share_photo_data['sort'];
 368					}
 369					
 370					$share_photo_data['uid'] = $_FANWE['uid'];
 371					$share_photo_data['share_id'] = $share_id;
 372					FDB::insert('share_photo',$share_photo_data,true);
 373					$photo_count++;
 374				}
 375			}
 376           //??????
 377		    $post_count=0;
 378			if(isset($data['share_posts'])){
 379				$share_posts=$data['share_posts'];
 380				foreach($share_posts as $post){
 381					    $share_post_data['uid'] = $_FANWE['uid'];
 382						$share_post_data['share_id'] = $share_id;
 383						$share_post_data['post_title'] =  $post['post_title'];
 384						$share_post_data['post_content'] = $post['post_content'];
 385						$share_post_data['post_excerpt'] =  $post['post_excerpt'];
 386						$share_post_data['post_type'] =  $post['type'];
 387						$share_post_data['sort'] =  $post['sort'];
 388						if(isset($post['feature_img']))
 389						$share_post_data['feature_img']=$post['feature_img'];
 390						FDB::insert('share_posts',$share_post_data,true);
 391						$post_count++;
 392				}
 393			}
 394			//??????
 395			
 396			$shop_ids = array();
 397			$goods_count = 0;
 398			//???????
 399			if(isset($data['share_goods']))
 400			{
 401				$share_goods = $data['share_goods'];
 402				foreach($share_goods as $goods)
 403				{
 404					if($goods_count >= $_FANWE['setting']['share_goods_count'])
 405						break;
 406	
 407					$shop_id = 0;
 408					if(!empty($goods['shop_url']))
 409					{
 410						$shop_id = FDB::resultFirst('SELECT shop_id
 411							FROM '.FDB::table('shop').'
 412							WHERE shop_url = \''.$goods['shop_url'].'\'');
 413	
 414						if(intval($shop_id) == 0)
 415						{
 416							$content_match .= $goods['shop_name'];
 417							$shop_logo = copyFile(FANWE_ROOT.$goods['shop_logo'],'shop',md5(microtime(true)).random('6').'.jpg',true);
 418							$shop_data['shop_name'] = $goods['shop_name'];
 419							$shop_data['shop_logo'] =  $shop_logo['url'];
 420							$shop_data['shop_url'] = $goods['shop_url'];
 421							$shop_data['taoke_url'] = $goods['shop_taoke_url'];
 422							$shop_id = FDB::insert('shop',$shop_data,true);
 423						}
 424						
 425						if($shop_id > 0)
 426							$shop_ids[] = $shop_id;
 427					}
 428	
 429					$goods_img = copyImage(FANWE_ROOT.$goods['img'],md5(microtime(true)).random('6').'.jpg',array(),'share',true,$share_id);
 430					if(!empty($goods_img))
 431					{
 432						if($goods['sort'] < $weibo_img_sort)
 433						{
 434							$weibo_img = $goods_img['url'];
 435							$weibo_img_sort = $goods['sort'];
 436						}
 437							
 438						$shop_id = intval($shop_id);
 439						//?????????
 440						$share_goods_data['uid'] = $_FANWE['uid'];
 441						$share_goods_data['share_id'] = $share_id;
 442						$share_goods_data['shop_id'] = $shop_id;
 443						$share_goods_data['img'] =  $goods_img['url'];
 444						$share_goods_data['name'] = $goods['name'];
 445						$share_goods_data['url'] = $goods['url'];
 446						$share_goods_data['price'] = $goods['price'];
 447						$share_goods_data['sort'] = $goods['sort'];
 448						$share_goods_data['taoke_url'] = $goods['taoke_url'];
 449						$share_goods_data['goods_key'] = $goods['goods_key'];
 450						FDB::insert('share_goods',$share_goods_data,true);
 451	
 452						$goods_tags = FS('Words')->segment($goods['name'],10);
 453						if(!empty($goods_tags))
 454						{
 455							$share_cates[] = ShareService::getCateByTags($goods_tags);
 456						}
 457						$content_match .= $goods['name'];
 458						$goods_count++;
 459					}
 460				}
 461			}
 462			
 463			//??????
 464			if(isset($data['rel_goods']))
 465			{
 466				$share_goods = $data['rel_goods'];
 467				foreach($share_goods as $share_goods_data)
 468				{
 469					if($goods_count >= $_FANWE['setting']['share_goods_count'])
 470						break;
 471					
 472					$is_rel_imgage = true;
 473					$shop_ids[] =  $share_goods_data['shop_id'];
 474					if($share_goods_data['sort'] < $weibo_img_sort)
 475					{
 476						$weibo_img = $share_goods_data['img'];
 477						$weibo_img_sort = $share_goods_data['sort'];
 478					}
 479					
 480					$share_goods_data['uid'] = $_FANWE['uid'];
 481					$share_goods_data['share_id'] = $share_id;
 482					FDB::insert('share_goods',$share_goods_data,true);
 483					$goods_tags = FS('Words')->segment($goods['name'],10);
 484					if(!empty($goods_tags))
 485					{
 486						$share_cates[] = ShareService::getCateByTags($goods_tags);
 487					}
 488					$content_match .= $goods['name'];
 489					$goods_count++;
 490				}
 491			}
 492
 493			if($goods_count > 0 && $photo_count > 0)
 494				$share_data_type = 'goods_photo';
 495			elseif($goods_count > 0)
 496				$share_data_type = 'goods';
 497			elseif($photo_count > 0)
 498				$share_data_type = 'photo';
 499			elseif($post_count>0)
 500			    $share_data_type='post';
 501			else
 502				$share_data_type = 'default';
 503			
 504			$update_share = array();
 505			$update_share['share_data'] = $share_data_type;
 506			
 507			if($share_album_id > 0 && in_array($share_data_type,array('goods','photo','goods_photo','post')))
 508			{
 509				$album = FDB::fetchFirst('SELECT cid,id,title FROM '.FDB::table('album').' WHERE id = '.$share_album_id);
 510				if($album)
 511				{
 512					$update_share['type'] = 'album_item';
 513					$share_data_now_type = 'album_item';
 514					$share_data_rec_id = $album['id'];
 515					$share_data_rec_cate = $album['cid'];
 516					$update_share['rec_id'] = $album['id'];
 517					$update_share['title'] = addslashes($album['title']);
 518				}
 519				else
 520				{
 521					$update_share['rec_id'] = 0;
 522					$share_data_rec_id = 0;
 523				}
 524			}
 525			else
 526				$share_data_rec_id = 0;
 527			
 528			FDB::update("share",$update_share,"share_id=".$share_id);
 529
 530			//??????---?????? $is_album = true
 531			if(!$is_album)
 532				FDB::query('UPDATE '.FDB::table('user_count').' 
 533					SET shares = shares + 1,goods = goods + '.$goods_count.',photos = photos + '.$photo_count.'
 534					WHERE uid = '.$share_data['uid']);
 535			
 536			FS('Medal')->runAuto($share_data['uid'],'shares');
 537			FS('User')->medalBehavior($share_data['uid'],'continue_share');
 538			
 539			switch($share_data_type)
 540			{
 541				case 'goods_photo':
 542					FS('Medal')->runAuto($share_data['uid'],'goods');
 543					FS('User')->medalBehavior($share_data['uid'],'continue_goods');
 544					FS('Medal')->runAuto($share_data['uid'],'photos');
 545					FS('User')->medalBehavior($share_data['uid'],'continue_photo');
 546				break;
 547				case 'goods':
 548					FS('Medal')->runAuto($share_data['uid'],'goods');
 549					FS('User')->medalBehavior($share_data['uid'],'continue_goods');
 550				break;
 551				case 'photo':
 552					FS('Medal')->runAuto($share_data['uid'],'photos');
 553					FS('User')->medalBehavior($share_data['uid'],'continue_photo');
 554				break;
 555			}
 556
 557			if(in_array($share_data_type,array('goods','photo','goods_photo','post')))
 558			{
 559				//?????????????
 560				//FS('User')->setShareIds($share_data['uid'],$share_id);
 561
 562				if(!empty($share_cates))
 563				{
 564					$cids = ShareService::getCidsByCates($share_cates);
 565				}
 566
 567				if(!empty($data['share_tag']))
 568				{
 569					if(empty($cids))
 570					{
 571						$share_cates = array();
 572						$share_cates[] = ShareService::getCateByTags($data['share_tag']);
 573						$cids = ShareService::getCidsByCates($share_cates);
 574					}
 575
 576					//$content_match = array_merge($content_match, $data['share_tag']);
 577				}
 578
 579				//????
 580				$share_tags = array();
 581				foreach($data['share_tag'] as $tag)
 582				{
 583					if(trim($tag) != '' && !in_array($tag,$share_tags))
 584					{
 585						array_push($share_tags,$tag);
 586
 587						//?????tags????
 588						FDB::query('UPDATE '.FDB::table('goods_tags').'
 589							SET count = count + 1
 590							WHERE tag_name = \''.$tag.'\'');
 591
 592						//????100??????
 593						FDB::query('UPDATE '.FDB::table('goods_tags').'
 594							SET is_hot = 1
 595							WHERE tag_name = \''.$tag.'\' AND count >= 100');
 596
 597						$content_match.=$tag;
 598						$tag_data = array();
 599						$tag_data['share_id'] = $share_id;
 600						$tag_data['tag_name'] = $tag;
 601						FDB::insert('share_tags',$tag_data);
 602					}
 603				}
 604				unset($share_tags);
 605
 606				if(!empty($cids))
 607				{
 608					foreach($cids as $cid)
 609					{
 610						$cate_data = array();
 611						$cate_data['share_id'] = $share_id;
 612						$cate_data['cate_id'] = $cid;
 613						FDB::insert('share_category',$cate_data);
 614					}
 615				}
 616
 617				//??????
 618				$share_match['share_id'] = $share_id;
 619				$share_match['content_match'] = segmentToUnicode(clearSymbol($content_match));
 620				FDB::insert("share_match",$share_match);
 621			}
 622			
 623			ShareService::updateShareCache($share_id);
 624			
 625			if($share_data_rec_id > 0)
 626			{
 627				$album_share = array();
 628				$album_share['album_id'] = $share_data_rec_id;
 629				$album_share['share_id'] = $share_id;
 630				$album_share['cid'] = $share_data_rec_cate;
 631				$album_share['create_day'] = getTodayTime();
 632				FDB::insert("album_share",$album_share);
 633				
 634				FS('Album')->updateAlbumByShare($share_data_rec_id,$share_id);
 635				FS('Album')->updateAlbum($share_data_rec_id);
 636			}
 637			
 638			if(count($shop_ids) > 0)
 639				FS("Shop")->updateShopStatistic($shop_ids);
 640			
 641			//??????
 642			$atme_share_type = FDB::resultFirst("select `type` from ".FDB::table("share")." where `share_id`='".$share_id."'");
 643			if($atme_share_type != "fav")
 644			{
 645				$atme_list = array();
 646				$pattern = "/@([^\f\n\r\t\v@ ]{2,20}?)(?:\:| )/";
 647				preg_match_all($pattern,$share_data['content'],$atme_list);
 648				if(!empty($atme_list[1]))
 649				{
 650					$atme_list[1] = array_unique($atme_list[1]);
 651					$users = array();
 652					foreach($atme_list[1] as $user)
 653					{
 654						if(!empty($user))
 655						{
 656							$users[] = $user;
 657						}
 658					}
 659					
 660					$res = FDB::query('SELECT uid 
 661						FROM '.FDB::table('user').'
 662						WHERE user_name '.FDB::createIN($users));
 663					while($data = FDB::fetch($res))
 664					{
 665						FS("User")->setUserTips($data['uid'],4,$share_id);
 666					}
 667				}
 668			}
 669			
 670			if($is_score && !in_array($share_data_now_type,array('fav','album_best','album_rec')))
 671			{
 672				if(!$is_rel_imgage && in_array($share_data_type,array('goods','photo','goods_photo','post')))
 673					FS("User")->updateUserScore($share_data['uid'],'share','image',$share_data['content'],$share_id);
 674				else
 675					FS("User")->updateUserScore($share_data['uid'],'share','default',$share_data['content'],$share_id);
 676			}
 677			
 678			if($data['pub_out_check'])
 679			{
 680				$weibo = array();
 681				$weibo['content'] = $share_data['content'];
 682				$weibo['img'] = $weibo_img;
 683				$weibo['ip'] = $_FANWE['client_ip'];
 684				$weibo['url'] = $_FANWE['site_url'].FU('note/index',array('sid'=>$share_id));
 685                $weibo['url'] = str_replace('//','/',$weibo['url']);
 686				$weibo['url'] = str_replace(':/','://',$weibo['url']);
 687				$weibo = base64_encode(serialize($weibo));
 688				if(empty($share_data['type']))
 689					$share_data['type'] = 'default';
 690				
 691				//???????
 692				$uid = $_FANWE['uid'];
 693				$user_binds = FS("User")->getUserBindList($uid);
 694				$is_open = false;
 695				foreach($user_binds as $class => $bind)
 696				{
 697					if($bind['sync'] && file_exists(FANWE_ROOT."login/".$class.".php"))
 698					{
 699						$check_field = "";
 700						if(in_array($share_data['type'],array('bar','ask')))
 701							$check_field = "topic";
 702						elseif($share_data['type'] == 'default')
 703							$check_field = "weibo";
 704						
 705						if($bind['sync'][$check_field] == 1)
 706						{
 707							$is_open = true;
 708							//????
 709							$schedule['uid'] = $uid;
 710							$schedule['type'] = $class;
 711							$schedule['data'] = $weibo;
 712							$schedule['pub_time'] = TIME_UTC;
 713							FDB::insert('pub_schedule',$schedule,true);
 714						}
 715					}
 716				}
 717				
 718				if($is_open)
 719				{
 720					$fp=fsockopen($_SERVER['HTTP_HOST'],80,&$errno,&$errstr,5);
 721					if($fp)
 722					{
 723						$request = "GET ".SITE_URL."login.php?loop=true&uid=".$uid." HTTP/1.0\r\n";
 724						$request .= "Host: ".$_SERVER['HTTP_HOST']."\r\n";
 725						$request .= "Connection: Close\r\n\r\n";
 726						fwrite($fp, $request);
 727						while(!feof($fp))
 728						{
 729							fgets($fp, 128);
 730							break;
 731						}
 732						fclose($fp);
 733					}
 734				}
 735			}
 736		}
 737		else
 738		{
 739			$result['status'] = false;
 740		}
 741		return $result;
 742	}
 743
 744	/*??????????*/
 745	public function getCateByTags($tags)
 746	{
 747		static $tag_cates = array();
 748
 749		$tags = array_unique($tags);
 750
 751		$cates = array();
 752		foreach($tags as $tag)
 753		{
 754			if(!empty($tag))
 755			{
 756				if(isset($tag_cates[$tag]))
 757				{
 758					foreach($tag_cates[$tag] as $data)
 759					{
 760						if(isset($cates[$data['cate_id']]))
 761							$cates[$data['cate_id']] += $data['weight'];
 762						else
 763							$cates[$data['cate_id']] = $data['weight'];
 764					}
 765					continue;
 766				}
 767
 768				$is_bln = true;
 769				$res = FDB::query('SELECT gct.cate_id,gct.weight
 770					FROM '.FDB::table('goods_tags').' AS gt
 771					LEFT JOIN '.FDB::table('goods_category_tags')." AS gct ON gct.tag_id = gt.tag_id
 772					WHERE gt.tag_name = '$tag'");
 773				while($data = FDB::fetch($res))
 774				{
 775					if($data['weight'] < 1)
 776						$data['weight'] = 1;
 777
 778					if(isset($cates[$data['cate_id']]))
 779						$cates[$data['cate_id']] += $data['weight'];
 780					else
 781						$cates[$data['cate_id']] = $data['weight'];
 782
 783					$tag_cates[$tag][] = $data;
 784					$is_bln = false;
 785				}
 786
 787				if($is_bln)
 788				{
 789					$like_tag = fMysqlLikeQuote($tag);
 790					$res = FDB::query('SELECT gct.cate_id,gct.weight
 791						FROM '.FDB::table('goods_tags').' AS gt
 792						LEFT JOIN '.FDB::table('goods_category_tags')." AS gct ON gct.tag_id = gt.tag_id
 793						WHERE gt.tag_name LIKE '%".$like_tag."%' OR INSTR('$tag',gt.tag_name) > 0");
 794					while($data = FDB::fetch($res))
 795					{
 796						if($data['weight'] < 1)
 797							$data['weight'] = 1;
 798
 799						if(isset($cates[$data['cate_id']]))
 800							$cates[$data['cate_id']] += $data['weight'];
 801						else
 802							$cates[$data['cate_id']] = $data['weight'];
 803
 804						$tag_cates[$tag] = $data;
 805					}
 806				}
 807			}
 808		}
 809
 810		if(empty($cates))
 811			return array();
 812
 813		$cids = array();
 814		foreach($cates as $cate_id => $weight)
 815		{
 816			$cids[$weight][] = $cate_id;
 817		}
 818
 819		krsort($cids);
 820		return each($cids);
 821	}
 822
 823	public function getCidsByCates($cates)
 824	{
 825		$list = array();
 826		$cids = array();
 827		foreach($cates as $cate)
 828		{
 829			foreach($cate['value'] as $cid)
 830			{
 831				if(isset($cids[$cid]))
 832					$cids[$cid] = $cate['key'];
 833				else
 834					$cids[$cid] += $cate['key'];
 835			}
 836		}
 837
 838		foreach($cates as $cate)
 839		{
 840			$id = 0;
 841			$weight = 0;
 842			foreach($cate['value'] as $cid)
 843			{
 844				if($cids[$cid] > $weight)
 845					$id = $cid;
 846			}
 847
 848			if($id > 0)
 849				$list[] = $id;
 850		}
 851
 852		return array_unique($list);
 853	}
 854
 855	public function deleteShare($share_id,$is_score = true)
 856	{
 857		$share = ShareService::getShareById($share_id);
 858		if(!empty($share))
 859		{
 860			$goods_count = FDB::resultFirst('SELECT COUNT(goods_id) FROM '.FDB::table('share_goods').' WHERE share_id = '.$share_id);
 861			$photo_count = FDB::resultFirst('SELECT COUNT(photo_id) FROM '.FDB::table('share_photo').' WHERE share_id = '.$share_id);
 862			$collect_count = FDB::resultFirst('SELECT COUNT(c_uid) FROM '.FDB::table('user_collect').' WHERE share_id = '.$share_id);
 863
 864			FDB::delete('share','share_id = '.$share_id);
 865			FDB::delete('share_goods','share_id = '.$share_id);
 866			//FDB::delete('share_photo','share_id = '.$share_id);
 867			FDB::delete('share_category','share_id = '.$share_id);
 868			FDB::delete('share_comment','share_id = '.$share_id);
 869			FDB::delete('share_match','share_id = '.$share_id);
 870			FDB::delete('share_tags','share_id = '.$share_id);
 871			FDB::delete('user_collect','share_id = '.$share_id);
 872			
 873			$pattern = "/#([^\f\n\r\t\v]{1,80}?)#/";
 874			if(preg_match($pattern,$share['content']))
 875			{
 876				FS("Event")->deleteEvent($share_id);
 877			}
 878			
 879			$pattern = "/@([^\f\n\r\t\v@ ]{2,20}?)(?:\:| )/";
 880			if(preg_match($pattern,$share['content']))
 881			{
 882				FDB::delete('atme','share_id = '.$share_id);
 883			}
 884			
 885			if(defined('MANAGE_HANDLER') && MANAGE_HANDLER && $is_score)
 886			{
 887				if(!in_array($share['type'],array('fav','album_best','album_rec')))
 888				{
 889					if($share['rec_uid'] == 0 && in_array($share['share_data'],array('goods','photo','goods_photo')))
 890						FS("User")->updateUserScore($share['uid'],'delete_share','image',$share['content'],$share_id);
 891					else
 892						FS("User")->updateUserScore($share['uid'],'delete_share','default',$share['content'],$share_id);
 893				}	
 894			}
 895			
 896			FDB::query('UPDATE '.FDB::table('user_count').' SET
 897				shares = shares - 1,
 898				photos = photos - '.$photo_count.',
 899				goods = goods - '.$goods_count.',
 900				collects = collects - '.$collect_count.' WHERE uid = '.$share['uid']);
 901
 902			$key = getDirsById($share_id);
 903			clearCacheDir('share/'.$key);
 904			
 905			$count = (int)FDB::resultFirst('SELECT COUNT(share_id) FROM '.FDB::table('share_rec').' WHERE share_id = '.$share_id);
 906			if($count == 0)
 907				clearDir(PUBLIC_ROOT.'./upload/share/'.$key,true);
 908		}
 909	}
 910
 911	public function deleteShareCache()
 912	{
 913	
 914	}
 915
 916	/**
 917	 * ????????
 918	 * @param int $share_id ????
 919	 * @return array
 920	 */
 921	public function getShareById($share_id)
 922	{
 923		$share_id = (int)$share_id;
 924		if(!$share_id)
 925			return false;
 926		
 927		static $list = array();
 928		if(!isset($list[$share_id]))
 929		{
 930			$share = FDB::fetchFirst('SELECT * FROM '.FDB::table('share').' WHERE share_id = '.$share_id);
 931			if($share)
 932				$share['url'] = FU('note/index',array('sid'=>$share_id));
 933			$list[$share_id] = $share;
 934		}
 935		return $list[$share_id];
 936	}
 937
 938    /**
 939	 * ??????
 940	 * @param int $share_id ????
 941	 * @return void
 942	 */
 943	public function updateShare($share_id,$title,$content)
 944	{
 945		if(empty($title) && empty($content))
 946			return;
 947		
 948		$data = array();
 949		if(!empty($title))
 950        	$data['title'] = $title;
 951			
 952		if(!empty($content))
 953        	$data['content'] = $content;
 954		
 955        FDB::update('share',$data,"share_id = '$share_id'");
 956        ShareService::updateShareMatch($share_id);
 957	}
 958
 959	/**
 960	 * ??????
 961	 * @param int $share_id ????
 962	 * @return array
 963	 */
 964	public function getShareDetail($share_id,$is_collect = false,$is_tag = false,$collect_count = 20)
 965	{
 966		$share = ShareService::getShareById($share_id);
 967		if($share)
 968		{
 969			$share['cache_data'] = unserialize($share['cache_data']);
 970			$share['authoritys'] = ShareService::getIsEditShare($share);
 971			$share['time'] = getBeforeTimelag($share['create_time']);
 972			ShareService::shareImageFormat($share);
 973		}
 974		return $share;
 975	}
 976
 977	/**
 978	 * ?????????
 979	 * @param int $share_id ????
 980	 * @return array
 981	 */
 982	public function getShareDynamic($share_id)
 983	{
 984		$dynamic = FDB::fetchFirst('SELECT collect_count,comment_count,relay_count,click_count
 985				FROM '.FDB::table('share').'
 986				WHERE share_id = '.$share_id);
 987		return $dynamic;
 988	}
 989
 990	/**
 991	 * ????????
 992	 * @param array $list ????
 993	 * @param bool $is_parent ????????
 994	 * @param bool $is_collect ?????????
 995	 * @param bool $is_parent ????????
 996	 * @return array
 997	 */
 998	public function getShareDetailList($list,$is_parent = false,$is_collect = false,$is_tag = false,$is_comment = false,$comment_count = 10,$collect_count = 20,$is_user = false)
 999	{
1000		global $_FANWE;
1001		$shares = array();
1002		$share_ids = array();
1003		$rec_shares_ids = array();
1004		$share_users = array();
1005		$share_collects = array();
1006		$share_comments = array();
1007		$share_follows = array();
1008		
1009		foreach($list as $item)
1010		{
1011			$share_id = $item['share_id'];
1012			$share_ids[] = $share_id;
1013			$item['cache_data'] = unserialize($item['cache_data']);
1014			$item['authoritys'] = ShareService::getIsEditShare($item);
1015			$item['time'] = getBeforeTimelag($item['create_time']);
1016			$item['url'] = FU('note/index',array('sid'=>$share_id));
1017			ShareService::shareImageFormat($item);
1018			$shares[$share_id] = $item;
1019			unset($shares[$share_id]['cache_data']);
1020			
1021			//????
1022			if($is_user)
1023			{
1024				$shares[$share_id]['user'] = &$share_users[$item['uid']];
1025				if($item['rec_uid'] > 0)
1026					$shares[$share_id]['rec_user'] = &$share_users[$item['rec_uid']];
1027			}
1028			
1029			//????
1030			if($is_comment)
1031			{
1032				$shares[$share_id]['comments'] = array();
1033				if(!empty($item['cache_data']['comments']))
1034				{
1035					$comment_ids = array_slice($item['cache_data']['comments'],0,$comment_count);
1036					foreach($comment_ids as $comment_id)
1037					{
1038						$shares[$share_id]['comments'][$comment_id] = &$share_comments[$comment_id];
1039					}
1040				}
1041			}
1042			
1043			//???????
1044			if($is_collect)
1045			{
1046				$shares[$share_id]['collects'] = array();
1047				if(!empty($item['cache_data']['collects']))
1048				{
1049					$collect_ids = array_slice($item['cache_data']['collects'],0,$collect_count);
1050					foreach($collect_ids as $collect_uid)
1051					{
1052						if($is_user)
1053							$shares[$share_id]['collects'][$collect_uid] = &$share_users[$collect_uid];
1054						else
1055							$shares[$share_id]['collects'][$collect_uid] = $collect_uid;
1056					}
1057				}
1058			}
1059
1060			if($is_tag)
1061			{
1062				$shares[$share_id]['is_eidt_tag'] = ShareService::getIsEditTag($item);
1063				$shares[$share_id]['tags'] = $item['cache_data']['tags'];
1064				ShareService::tagsFormat($shares[$share_id]['tags']['user']);
1065			}
1066
1067			$shares[$share_id]['is_relay'] = false;
1068			$shares[$share_id]['is_parent'] = false;
1069			
1070			if($is_parent)
1071			{
1072				if($item['base_id'] > 0)
1073				{
1074					$shares[$share_id]['is_relay'] = true;
1075					$rec_shares_ids[$item['base_id']] = false;
1076					$shares[$share_id]['relay_share'] = &$rec_shares_ids[$item['base_id']];
1077
1078					if($item['parent_id'] > 0 && $item['parent_id'] != $item['base_id'])
1079					{
1080						$shares[$share_id]['is_parent'] = true;
1081						$rec_shares_ids[$item['parent_id']] = false;
1082						$shares[$share_id]['parent_share'] = &$rec_shares_ids[$item['parent_id']];
1083					}
1084				}
1085			}
1086		}
1087		
1088		$rec_ids = array_keys($rec_shares_ids);
1089		if(count($rec_ids) > 0)
1090		{
1091			$intersects = array_intersect($share_ids,$rec_ids);
1092			$temp_ids = array();
1093			foreach($intersects as $share_id)
1094			{
1095				$rec_shares_ids[$share_id] = $shares[$share_id];
1096				$temp_ids[] = $share_id;
1097			}
1098			
1099			$diffs = array_diff($rec_ids,$temp_ids);
1100			if(count($diffs) > 0)
1101			{
1102				$res = FDB::query('SELECT * FROM '.FDB::table('share').' WHERE share_id IN ('.implode(',',$diffs).')');
1103				while($item = FDB::fetch($res))
1104				{
1105					$share_id = $item['share_id'];
1106					$share_ids[] = $share_id;
1107					$item['cache_data'] = unserialize($item['cache_data']);
1108					$item['authoritys'] = ShareService::getIsEditShare($item);
1109					$item['time'] = getBeforeTimelag($item['create_time']);
1110					$item['url'] = FU('note/index',array('sid'=>$share_id));
1111					ShareService::shareImageFormat($item);
1112					$rec_shares_ids[$share_id] = $item;
1113					unset($rec_shares_ids[$share_id]['cache_data']);
1114					
1115					//????
1116					if($is_user)
1117					{
1118						$rec_shares_ids[$share_id]['user'] = &$share_users[$item['uid']];
1119						if($item['rec_uid'] > 0)
1120							$rec_shares_ids[$share_id]['rec_user'] = &$share_users[$item['rec_uid']];
1121					}
1122					
1123					//????
1124					if($is_comment)
1125					{
1126						$rec_shares_ids[$share_id]['comments'] = array();
1127						if(!empty($item['cache_data']['comments']))
1128						{
1129							$comment_ids = array_slice($item['cache_data']['comments'],0,$comment_count);
1130							foreach($comment_ids as $comment_id)
1131							{
1132								$rec_shares_ids[$share_id]['comments'][$comment_id] = &$share_comments[$comment_id];
1133							}
1134						}
1135					}
1136					
1137					//???????
1138					if($is_collect)
1139					{
1140						$rec_shares_ids[$share_id]['collects'] = array();
1141						if(!empty($item['cache_data']['collects']))
1142						{
1143							$collect_ids = array_slice($item['cache_data']['collects'],0,$collect_count);
1144							foreach($collect_ids as $collect_uid)
1145							{
1146								if($is_user)
1147									$rec_shares_ids[$share_id]['collects'][$collect_uid] = &$share_users[$collect_uid];
1148								else
1149									$rec_shares_ids[$share_id]['collects'][$collect_uid] = $collect_uid;
1150							}
1151						}
1152					}
1153		
1154					if($is_tag)
1155					{
1156						$rec_shares_ids[$share_id]['is_eidt_tag'] = ShareService::getIsEditTag($item);
1157						$rec_shares_ids[$share_id]['tags'] = $item['cache_data']['tags'];
1158						ShareService::tagsFormat($rec_shares_ids[$share_id]['tags']['user']);
1159					}
1160				}
1161			}
1162		}
1163		
1164		$comment_ids = array_keys($share_comments);
1165		if(count($comment_ids) > 0)
1166		{
1167			$res = FDB::query("SELECT * FROM ".FDB::table('share_comment').' WHERE comment_id IN ('.implode(',',$comment_ids).')');
1168			while($item = FDB::fetch($res))
1169			{
1170				$item['time'] = getBeforeTimelag($item['create_time']);
1171				$share_comments[$item['comment_id']] = $item;
1172				if($is_user)
1173					$share_comments[$item['comment_id']]['user'] = &$share_users[$item['uid']];
1174			}
1175		}
1176		
1177		if($is_user)
1178			FS('User')->usersFormat($share_users);
1179		
1180		return $shares;
1181	}
1182	
1183	/**
1184	 * ???????
1185	 */
1186	public function getShareImage($share_id,$data_type)
1187	{
1188		$share_id = (int)$share_id;
1189		$list = array();
1190		switch($data_type)
1191		{
1192			case 'goods':
1193				$sql = 'SELECT share_id,goods_id AS id,img,\'g\' AS type,name,url,price,taoke_url
1194					FROM '.FDB::table('share_goods').'
1195					WHERE share_id = '.$share_id.' ORDER BY sort ASC';
1196				$res = FDB::query($sql);
1197				while($data = FDB::fetch($res))
1198				{
1199					$data['name'] = addslashes($data['name']);
1200					$data['url'] = addslashes($data['url']);
1201					$pkey = $data['type'].$data['id'];
1202					$list['all'][$pkey] = $data;
1203				}
1204			break;
1205			case 'photo':
1206				$sql = 'SELECT share_id,photo_id AS id,img,type AS ptype,\'m\' AS type,\'\' AS name,\'\' AS url,0 AS price
1207					FROM '.FDB::table('share_photo').'
1208					WHERE share_id = '.$share_id.' ORDER BY sort ASC';
1209				$res = FDB::query($sql);
1210				while($data = FDB::fetch($res))
1211				{
1212					$pkey = $data['type'].$data['id'];
1213					$ptype = $data['ptype'];
1214					unset($data['ptype']);
1215					$list['all'][$pkey] = $data;
1216					if($ptype != 'default')
1217					{
1218						$list[$ptype][] = $pkey;
1219					}
1220				}
1221			break;
1222			case 'goods_photo':
1223				$sql = '(SELECT share_id,goods_id AS id,img,\'default\' AS ptype,\'g\' AS type,name,url,price,taoke_url,sort
1224					FROM '.FDB::table('share_goods').'
1225					WHERE share_id = '.$share_id.')
1226					UNION
1227					(SELECT share_id,photo_id AS id,img,type AS ptype,\'m\' AS type,\'\' AS name,\'\' AS url,0 AS price,\'\' AS taoke_url,sort
1228					FROM '.FDB::table('share_photo').'
1229					WHERE share_id = '.$share_id.')
1230					ORDER BY sort ASC';
1231
1232				$res = FDB::query($sql);
1233				while($data = FDB::fetch($res))
1234				{
1235					$data['name'] = addslashes($data['name']);
1236					$data['url'] = addslashes($data['url']);
1237					$pkey = $data['type'].$data['id'];
1238					$ptype = $data['ptype'];
1239					unset($data['ptype']);
1240					unset($data['sort']);
1241					$list['all'][$pkey] = $data;
1242					if($ptype != 'default')
1243					{
1244						$list[$ptype][] = $pkey;
1245					}
1246				}
1247			break;
1248		}
1249		return $list;
1250	}
1251
1252	/**
1253	 * ?????????
1254	 */
1255	public function getShareImages(&$share_datas)
1256	{
1257		foreach($share_datas as $share_data => $share_ids)
1258		{
1259			if($share_data == 'default' || count($share_ids) == 0)
1260				continue;
1261			
1262			$share_ids = array_keys($share_ids);
1263			$list = array();
1264			switch($share_data)
1265			{
1266				case 'goods':
1267					$sql = 'SELECT share_id,goods_id AS id,img,\'g\' AS type,name,url,price,taoke_url
1268						FROM '.FDB::table('share_goods').'
1269						WHERE share_id IN ('.implode(',',$share_ids).') ORDER BY sort ASC';
1270					$res = FDB::query($sql);
1271					while($data = FDB::fetch($res))
1272					{
1273						$list[$data['share_id']]['all'][] = $data;
1274					}
1275				break;
1276				case 'photo':
1277					$sql = 'SELECT share_id,photo_id AS id,img,type AS ptype,\'m\' AS type,\'\' AS name,\'\' AS url,0 AS price
1278						FROM '.FDB::table('share_photo').'
1279						WHERE share_id IN ('.implode(',',$share_ids).') ORDER BY sort ASC';
1280					$res = FDB::query($sql);
1281					while($data = FDB::fetch($res))
1282					{
1283						$pkey = $data['type'].$data['id'];
1284						$ptype = $data['ptype'];
1285						unset($data['ptype']);
1286						$list[$data['share_id']]['all'][$pkey] = $data;
1287						if($ptype != 'default')
1288						{
1289							$list[$data['share_id']][$ptype][] = $pkey;
1290						}
1291					}
1292				break;
1293				case 'goods_photo':
1294					$sql = '(SELECT share_id,goods_id AS id,img,\'default\' AS ptype,\'g\' AS type,name,url,price,taoke_url,sort
1295						FROM '.FDB::table('share_goods').'
1296						WHERE share_id IN ('.implode(',',$share_ids).'))
1297						UNION
1298						(SELECT share_id,photo_id AS id,img,type AS ptype,\'m\' AS type,\'\' AS name,\'\' AS url,0 AS price,\'\' AS taoke_url,sort
1299						FROM '.FDB::table('share_photo').'
1300						WHERE share_id IN ('.implode(',',$share_ids).'))
1301						ORDER BY sort ASC';
1302
1303					$res = FDB::query($sql);
1304					while($data = FDB::fetch($res))
1305					{
1306						$pkey = $data['type'].$data['id'];
1307						$ptype = $data['ptype'];
1308						unset($data['ptype']);
1309						unset($data['sort']);
1310						$list[$data['share_id']]['all'][$pkey] = $data;
1311						if($ptype != 'default')
1312						{
1313							$list[$data['share_id']][$ptype][] = $pkey;
1314						}
1315					}
1316				break;
1317			}
1318			
1319			foreach($list as $share_id => $item)
1320			{
1321				foreach($item['all'] as $ik => $img)
1322				{
1323					if($img['type'] == 'g')
1324					{
1325						$img['goods_url'] = $img['url'];
1326						if(empty($img['taoke_url']))
1327							$img['to_url'] = FU('tgo',array('url'=>$img['url']));
1328						else
1329							$img['to_url'] = FU('tgo',array('url'=>$img['taoke_url']));
1330	
1331						$img['price_format'] = priceFormat($img['price']);
1332					}
1333					
1334					$img['url'] = FU('note/'.$img['type'],array('sid'=>$img['share_id'],'id'=>$img['id']));
1335					$item['all'][$ik] = $img;
1336				}
1337				$share_datas[$share_data][$share_id] = $item;
1338			}
1339		}
1340	}
1341	
1342	public function shareImageFormat(&$share,$pic_num = 0)
1343	{		
1344		$images = $share['cache_data']['imgs'];
1345		foreach($images['all'] as $ik => $img)
1346		{
1347			if($img['type'] == 'g')
1348			{
1349				$img['goods_url'] = $img['url'];
1350				if(empty($img['taoke_url']))
1351					$img['to_url'] = FU('tgo',array('url'=>$img['url']));
1352				else
1353					$img['to_url'] = FU('tgo',array('url'=>$img['taoke_url']));
1354
1355				$img['price_format'] = priceFormat($img['price']);
1356			}
1357			
1358			$img['url'] = FU('note/'.$img['type'],array('sid'=>$img['share_id'],'id'=>$img['id']));
1359			$images['all'][$ik] = $img;
1360			$share['imgs'][] = $img;
1361		}
1362		
1363		foreach($images['dapei'] as $ik)
1364		{
1365			$share['dapei_imgs'][] = $images['all'][$ik];
1366		}
1367		
1368		foreach($images['look'] as $ik)
1369		{
1370			$share['look_imgs'][] = $images['all'][$ik];
1371		}
1372		
1373		if($pic_num > 0 && count($share['imgs']) > $pic_num)
1374			$share['imgs'] = array_slice($share['imgs'],0,$pic_num);
1375		unset($images);
1376	}
1377
1378	/**
1379	 * ?????????????????
1380	 * @param array $_POST ?????
1381	 * @return array(
1382			'prev'=>?????,
1383			'next'=>?????,
1384		)
1385	 */
1386	public function getPrevNextShares($uid,$share_id)
1387	{
1388		$arr = array('prev'=>0,'next'=>0);
1389		$share_ids = FS('User')->getShareIds($uid);
1390		
1391		$key = array_search($share_id,$share_ids);
1392		if($key !== false)
1393		{
1394			$count = count($share_ids);
1395			if($count > 1)
1396			{
1397				if($key == 0)
1398				{
1399					$arr['prev'] = $share_ids[$count - 1];
1400					$arr['next'] = $share_ids[$key + 1];
1401				}	
1402				elseif($key == $count - 1)
1403				{
1404					$arr['prev'] = $share_ids[$key - 1];
1405					$arr['next'] = $share_ids[0];
1406				}	
1407				else
1408				{
1409					$arr['prev'] = $share_ids[$key - 1];
1410					$arr['next'] = $share_ids[$key + 1];
1411				}
1412			}
1413		}
1414		return $arr;
1415	}
1416	
1417	public function updateShareCache($share_id,$type = 'all')
1418	{
1419		$share_id = (int)$share_id;
1420		if(!$share_id)
1421			return;
1422		
1423		$share = FDB::fetchFirst('SELECT cache_data,share_data FROM '.FDB::table('share').' WHERE share_id = '.$share_id);
1424		if(!$share)
1425			return;
1426			
1427		$cache_data = unserialize($share['cache_data']);
1428		switch($type)
1429		{
1430			case 'tags':
1431				$cache_data['tags'] = ShareService::getShareTags($share_id,true);
1432			break;
1433			
1434			case 'collects':
1435				$cache_data['collects'] = ShareService::getShareCollectUser($share_id,50);
1436			break;
1437			
1438			case 'comments':
1439				$cache_data['comments'] = ShareService::getNewCommentIdsByShare($share_id,10);
1440			break;
1441			
1442			case 'imgs':
1443				$cache_data['imgs'] = ShareService::getShareImage($share_id,$share['share_data']);
1444			break;
1445			
1446			case 'all':
1447				$cache_data['tags'] = ShareService::getShareTags($share_id,true);
1448				$cache_data['collects'] = ShareService::getShareCollectUser($share_id,50);
1449				$cache_data['comments'] = ShareService::getNewCommentIdsByShare($share_id,10);
1450				$cache_data['imgs'] = ShareService::getShareImage($share_id,$share['share_data']);
1451			break;
1452		}
1453		unset($share['share_data']);
1454		$share['cache_data'] = serialize($cache_data);
1455		FDB::update("share",$share,'share_id = '.$share_id);
1456	}
1457
1458    public function updateShareMatch($share_id)
1459    {
1460        $share = ShareService::getShareById($share_id);
1461        if(!in_array($share['share_data'],array('goods','photo','goods_photo')))
1462            return;
1463		
1464		$share['cache_data'] = unserialize($share['cache_data']);
1465
1466		$content_match = clearExpress($share['content']);
1467        $content_match .= $share['title'];
1468
1469        if(isset($share['cache_data']['tags']['user']))
1470        {
1471            foreach($share['cache_data']['tags']['user'] as $tag)
1472            {
1473				$content_match.=$tag['tag_name'];
1474            }
1475        }
1476
1477        if(isset($share['cache_data']['tags']['admin']))
1478        {
1479            foreach($share['cache_data']['tags']['admin'] as $tag)
1480            {
1481				$content_match.=$tag['tag_name'];
1482            }
1483        }
1484
1485        if(isset($share['cache_data']['imgs']['all']))
1486        {
1487            foreach($share['cache_data']['imgs']['all'] as $img)
1488            {
1489                if(!empty($img['name']))
1490				{
1491					$content_match.=$img['name'];
1492				}
1493            }
1494        }
1495		
1496        //??????
1497        $share_match = array();
1498        $share_match['share_id'] = $share_id;
1499        $share_match['content_match'] = segmentToUnicode(clearSymbol($content_match));
1500        FDB::insert("share_match",$share_match,false,true);
1501    }
1502
1503	/**
1504	 * ?????????
1505	 * @param int $share ??
1506	 * @return array
1507	 */
1508	public function getIsEditShare(&$share)
1509	{
1510		static $edits = array();
1511		if(!isset($edits[$share['share_id']]))
1512		{
1513			global $_FANWE;
1514			$type = array('ask','bar');
1515			$is_edit = 0;
1516			$post = array('ask_post','bar_post');
1517			if(in_array($share['type'],$post))
1518			{
1519				if($share['uid'] == $_FANWE['uid'])
1520					$is_edit = 1;
1521
1522				if($share['type'] == 'ask_post')
1523					$thread = FS('ask')->getTopicById($share['rec_id']);
1524				else
1525					$thread = FS('Topic')->getTopicById($share['rec_id']);
1526
1527				if($thread['uid'] == $_FANWE['uid'])
1528					$is_edit = 2;
1529			}
1530			else
1531			{
1532				if(!in_array($share['type'],$type) && $share['uid'] == $_FANWE['uid'])
1533					$is_edit = 1;
1534			}
1535
1536			$edits[$share['share_id']] = $is_edit;
1537		}
1538
1539		return $edits[$share['share_id']];
1540	}
1541	/*===========??????? END  ==============*/
1542
1543	/*===========???? BEGIN  ==============*/
1544	/**
1545	 * ???????????
1546	 * @param int $share ??
1547	 * @return array
1548	 */
1549	public function getIsEditTag(&$share)
1550	{
1551		global $_FANWE;
1552		$_img_data = array('goods','photo','goods_photo');
1553		$is_edit_tag = false;
1554		if(in_array($share['share_data'],$_img_data) && $share['uid'] == $_FANWE['uid'])
1555			$is_edit_tag = true;
1556		return $is_edit_tag;
1557	}
1558
1559	/**
1560	 * ??????
1561	 * @param int $share_id ????
1562	 * @return array
1563	 */
1564	public function getShareTags($share_id,$is_update = false)
1565	{
1566		$share_id = (int)$share_id;
1567		if(!$share_id)
1568			return array();
1569		
1570		static $list = array();
1571		if(!isset($list[$share_id]) || $is_update)
1572		{
1573			$res = FDB::query('SELECT tag_name,is_admin
1574				FROM '.FDB::table('share_tags').'
1575				WHERE share_id = '.$share_id);
1576			while($data = FDB::fetch($res))
1577			{
1578				$data['tag_name'] = addslashes($data['tag_name']);
1579				if($data['is_admin'] == 0)
1580					$list[$share_id]['user'][] = $data;
1581				else
1582					$list[$share_id]['admin'][] = $data;
1583			}
1584		}
1585		
1586		return $list[$share_id];
1587	}
1588	
1589	public function tagsFormat(&$tags)
1590	{
1591		foreach($tags as $tk => $tag)
1592		{
1593			$tags[$tk]['url'] = FU('book/shopping',array('tag'=>urlencode($tag['tag_name'])));
1594		}
1595	}
1596
1597	/**
1598	 * ????????
1599	 * @param int $share_id ????
1600	 * @param array $tags = array(
1601	 		'user'=>??????,
1602			'admin'=>???????,(?????admin?????????????)
1603	 	);
1604	 * @return array
1605	 */
1606	public function updateShareTags($share_id,$tags)
1607	{
1608		global $_FANWE;
1609		//?????????
1610		FDB::delete('share_tags','share_id = '.$share_id.' AND is_admin = 0');
1611		if(isset($tags['user']))
1612		{
1613			$tags['user'] = str_replace('?',' ',$tags['user']);
1614			$tags['user'] = explode(' ',htmlspecialchars(trim($tags['user'])));
1615            $tags['user'] = array_unique($tags['user']);
1616            $tags['user'] = array_slice($tags['user'],0,$_FANWE['setting']['share_tag_count']);
1617
1618			$share_tags = array();
1619			foreach($tags['user'] as $tag)
1620			{
1621				if(trim($tag) != '' && !in_array($tag,$share_tags))
1622				{
1623					array_push($share_tags,$tag);
1624
1625					//?????tags????
1626					FDB::query('UPDATE '.FDB::table('goods_tags').'
1627						SET count = count + 1
1628						WHERE tag_name = \''.$tag.'\'');
1629
1630					//????100??????
1631					FDB::query('UPDATE '.FDB::table('goods_tags').'
1632						SET is_hot = 1
1633						WHERE tag_name = \''.$tag.'\' AND count >= 100');
1634
1635					$tag_data = array();
1636					$tag_data['share_id'] = $share_id;
1637					$tag_data['tag_name'] = $tag;
1638					FDB::insert('share_tags',$tag_data);
1639				}
1640			}
1641			ShareService::updateShareCache($share_id,'tags');
1642		}
1643
1644		//??????????
1645		if(isset($tags['admin']))
1646		{
1647			FDB::delete('share_tags','share_id = '.$share_id.' AND is_admin = 1');
1648
1649			$tags['admin'] = str_replace('?',' ',$tags['admin']);
1650			$tags['admin'] = explode(' ',htmlspecialchars(trim($tags['admin'])));
1651            $tags['admin'] = array_unique($tags['admin']);
1652
1653			$share_tags = array();
1654			foreach($tags['admin'] as $tag)
1655			{
1656				if(trim($tag) != '' && !in_array($tag,$share_tags))
1657				{
1658					array_push($share_tags,$tag);
1659
1660					//?????tags????
1661					FDB::query('UPDATE '.FDB::table('goods_tags').'
1662						SET count = count + 1
1663						WHERE tag_name = \''.$tag.'\'');
1664
1665					//????100??????
1666					FDB::query('UPDATE '.FDB::table('goods_tags').'
1667						SET is_hot = 1
1668						WHERE tag_name = \''.$tag.'\' AND count >= 100');
1669
1670					$tag_data = array();
1671					$tag_data['sh…

Large files files are truncated, but you can click here to view the full file