material.mod.php 21.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
<?php
defined('IN_IA') or exit('Access Denied');
load()->func('file');


function material_sync($material, $exist_material, $type) {
	global $_W;
	$material = empty($material) ? array() : $material;
	foreach ($material as $news) {
		$attachid = '';
		$material_exist = pdo_get('wechat_attachment', array('uniacid' => $_W['uniacid'], 'media_id' => $news['media_id']));
		if (empty($material_exist)) {
			$material_data = array(
				'uniacid' => $_W['uniacid'],
				'acid' => $_W['acid'],
				'media_id' => $news['media_id'],
				'type' => $type,
				'model' => 'perm',
				'createtime' => $news['update_time']
			);
			if ($type == 'image') {
				$material_data['filename'] = $news['name'];
				$material_data['attachment'] = $news['url'];
			}
			if ($type == 'voice') {
				$material_data['filename'] = $news['name'];
			}
			if ($type == 'video') {
				$material_data['tag'] = iserializer(array('title' => $news['name']));
			}
			pdo_insert('wechat_attachment', $material_data);
			$attachid = pdo_insertid();
		} else {
			if ($type == 'image') {
				$material_data = array(
					'createtime' => $news['update_time'],
					'attachment' => $news['url'],
					'filename' => $news['name']
				);
				pdo_update('wechat_attachment', $material_data, array('uniacid' => $_W['uniacid'], 'media_id' => $news['media_id']));
			}
			if ($type == 'voice') {
				$material_data = array(
					'createtime' => $news['update_time'],
					'filename' => $news['name']
				);
				pdo_update('wechat_attachment', $material_data, array('uniacid' => $_W['uniacid'], 'media_id' => $news['media_id']));
			}
			if ($type == 'video') {
				$tag = empty($material_exist['tag']) ? array() : iunserializer($material_exist['tag']);
				$material_data = array(
					'createtime' => $news['update_time'],
					'tag' => iserializer(array('title' => $news['name'], 'url' => $tag['url']))
				);
				pdo_update('wechat_attachment', $material_data, array('uniacid' => $_W['uniacid'], 'media_id' => $news['media_id']));
			}
			$exist_material[] = $material_exist['id'];
		}
		if ($type == 'news') {
			$attachid = empty($attachid) ? $material_exist['id'] : $attachid;
			pdo_delete('wechat_news', array('uniacid' =>$_W['uniacid'], 'attach_id' => $attachid));
			foreach ($news['content']['news_item'] as $key => $new) {
				$new_data = array(
					'uniacid' => $_W['uniacid'],
					'attach_id' => $attachid,
					'thumb_media_id' => $new['thumb_media_id'],
					'thumb_url' => $new['thumb_url'],
					'title' => $new['title'],
					'author' => $new['author'],
					'digest' => $new['digest'],
					'content' => $new['content'],
					'content_source_url' => $new['content_source_url'],
					'show_cover_pic' => $new['show_cover_pic'],
					'url' => $new['url'],
					'displayorder' => $key,
				);
				pdo_insert('wechat_news', $new_data);
			}
			pdo_update('wechat_attachment', array('createtime' => $news['update_time']), array('media_id' => $news['media_id']));
		}
	}
	return $exist_material;
}


function material_news_set($data, $attach_id) {
	global $_W;
	$attach_id = intval($attach_id);
	foreach ($data as $key => $news) {
		if (empty($news['title']) ||
			(!empty($news['thumb']) && !parse_path($news['thumb'])) ||
			(!empty($news['url']) && !parse_path($news['url'])) ||
			(!empty($news['content_source_url']) && !parse_path($news['content_source_url']))
		) {
			return error('-1', '参数有误');
		}
		if (!material_url_check($news['content_source_url']) || !material_url_check($news['url']) || !material_url_check($news['thumb'])) {
			return error('-3', '提交链接参数不合法');
		}
		$post_news[] = array(
			'id' => intval($news['id']),
			'uniacid' => $_W['uniacid'],
			'thumb_url' => $news['thumb'],
			'title' => addslashes($news['title']),
			'author' => addslashes($news['author']),
			'digest' => addslashes($news['digest']),
			'content' => safe_gpc_html(htmlspecialchars_decode($news['content'])),
			'url' => $news['url'],
			'show_cover_pic' => intval($news['show_cover_pic']),
			'displayorder' 		=> intval($key),
			'thumb_media_id' => addslashes($news['media_id']),
			'content_source_url' => $news['content_source_url'],
		);
	}
	if (!empty($attach_id)){
		$wechat_attachment = pdo_get('wechat_attachment', array(
			'id' => $attach_id,
			'uniacid' => $_W['uniacid']
		));
		if (empty($wechat_attachment)){
			return error('-2', '编辑素材不存在');
		}
		$wechat_attachment['model'] = 'local';
		pdo_update('wechat_attachment', $wechat_attachment, array(
			'id' => $attach_id
		));
		pdo_delete('wechat_news', array('attach_id' => $attach_id, 'uniacid' => $_W['uniacid']));
		foreach ($post_news as $id => $news) {
			$news['attach_id'] = $attach_id;
			unset($news['id']);
			pdo_insert('wechat_news', $news);
		}
		cache_delete(cache_system_key('material_reply:' . $attach_id));
	} else {
		$wechat_attachment = array(
			'uniacid' => $_W['uniacid'],
			'acid' => $_W['acid'],
			'media_id' => '',
			'type' => 'news',
			'model' => 'local',
			'createtime' => TIMESTAMP
		);
		pdo_insert('wechat_attachment', $wechat_attachment);
		$attach_id = pdo_insertid();
		foreach ($post_news as $key => $news) {
			$news['attach_id'] = $attach_id;
			pdo_insert('wechat_news', $news);
		}
	}
	return $attach_id;
}


function material_get($attach_id) {
	if (empty($attach_id)) {
		return error(1, "素材id参数不能为空");
	}
	if (is_numeric($attach_id)) {
		$material = pdo_get('wechat_attachment', array('id' => $attach_id));
	} else {
		$media_id = trim($attach_id);
		$material = pdo_get('wechat_attachment', array('media_id' => $media_id));
	}
	if (!empty($material)) {
		if ($material['type'] == 'news') {
			$material_table = table('material');
			$news = $material_table->materialNewsList($material['id']);
			if (!empty($news)) {
				foreach ($news as &$news_row) {
					$news_row['content_source_url'] = $news_row['content_source_url'];
					$news_row['thumb_url'] = tomedia($news_row['thumb_url']);
					preg_match_all('/src=[\'\"]?([^\'\"]*)[\'\"]?/i', $news_row['content'], $match);
					if (!empty($match[1])) {
						foreach ($match[1] as $val) {
							if ((strexists($val, 'http://') || strexists($val, 'https://')) && (strexists($val, 'mmbiz.qlogo.cn') || strexists($val, 'mmbiz.qpic.cn'))) {
								$news_row['content'] = str_replace($val, tomedia($val), $news_row['content']);
							}
						}
					}
					$news_row['content'] = str_replace('data-src', 'src', $news_row['content']);
				}
				unset($news_row);
			} else {
				return error('1', '素材不存在');
			}
			$material['news'] = $news;
		} elseif ($material['type'] == 'image') {
			$material['url'] = $material['attachment'];
			$material['attachment'] = tomedia($material['attachment']);

		}
		return $material;
	} else {
		return error('1', "素材不存在");
	}
}


function material_build_reply($attach_id) {
	if (empty($attach_id)) {
		return error(1, "素材id参数不能为空");
	}
	$cachekey = cache_system_key('material_reply:' . $attach_id);
	$reply = cache_load($cachekey);
	if (!empty($reply)) {
		return $reply;
	}
	$reply_material = material_get($attach_id);
	$reply = array();
	if ($reply_material['type'] == 'news') {
		if (!empty($reply_material['news'])) {
			foreach ($reply_material['news'] as $material) {
				$reply[] = array(
					'title' => $material['title'],
					'description' => $material['digest'],
					'picurl' => $material['thumb_url'],
					'url' => !empty($material['content_source_url']) ? $material['content_source_url'] : $material['url'],
				);
			}
		}
	}
	cache_write($cachekey, $reply, CACHE_EXPIRE_MIDDLE);
	return $reply;
}


function material_strip_wechat_image_proxy($content) {
	global $_W;
	$match_wechat = array();
	$content = htmlspecialchars_decode($content);
	preg_match_all ('/<img.*src=[\'"](.*)[\'"].*\/?>/iU', $content, $match_wechat);
	if (!empty($match_wechat[1])) {
		foreach ($match_wechat[1] as $val) {
			$wechat_thumb_url = urldecode(str_replace($_W['siteroot'] . 'web/index.php?c=utility&a=wxcode&do=image&attach=', '', $val));
			$content = str_replace($val, $wechat_thumb_url, $content);
		}
	}
	return $content;
}


function material_get_image_url($content) {
	global $_W;
	$content = htmlspecialchars_decode ($content);
	$match = array ();
	$images = array ();
	preg_match_all ('/<img.*src=[\'"](.*\.(?:png|jpg|jpeg|jpe|gif))[\'"].*\/?>/iU', $content, $match);
	if (!empty($match[1])) {
		foreach ($match[1] as $val) {
			if ((strexists ($val, 'http://') || strexists ($val, 'https://')) && !strexists ($val, 'mmbiz.qlogo.cn') && !strexists ($val, 'mmbiz.qpic.cn')) {
				$images[] = $val;
			} else {
				if (strexists ($val, './attachment/images/')) {
					$images[] = tomedia ($val);
				}
			}
		}
	}
	return $images;
}


function material_parse_content($content) {
	global $_W;
	$content = material_strip_wechat_image_proxy($content);
	$images = material_get_image_url($content);
	if (!empty($images)) {
		foreach ($images as $image) {
			$thumb = file_remote_attach_fetch(tomedia($image), 1024, 'material/images');
			if(is_error($thumb)) {
				return $thumb;
			}
			$thumb = ATTACHMENT_ROOT . $thumb;
			$account_api = WeAccount::create($_W['acid']);
			$result = $account_api->uploadNewsThumb($thumb);
			if (is_error($result)) {
				return $result;
			} else {
				$content = str_replace($image, $result, $content);
			}
		}
	}
	return $content;
}

function material_local_news_upload($attach_id) {
	global $_W;
	$account_api = WeAccount::create($_W['acid']);
	$material = material_get($attach_id);
	if (is_error($material)){
		return error('-1', '获取素材文件失败');
	}
	foreach ($material['news'] as $news) {
		if (empty($news['content'])){
			return error('-6', '素材内容不能为空');
		}
		$news['content'] = material_parse_content($news['content']);
		if (!empty($news['content_source_url'])) {
			$news['content_source_url'] = safe_gpc_url($news['content_source_url'], false, $_W['siteroot'] . 'app/' . $news['content_source_url']);
		}
		if (is_error($news['content'])) {
			return error('-2', $news['content']['message']);
		}
		if (empty($news['thumb_media_id'])) {
			if (empty($news['thumb_url'])){
				return error('-7', '图文封面不能为空');
			}else{
				$result = material_local_upload_by_url($news['thumb_url']);
				if (is_error($result)){
					return error('-3', $result['message']);
				}
				$news['thumb_media_id'] = $result['media_id'];
				$news['thumb_url'] = $result['url'];
			}
		}
		pdo_update('wechat_news', $news, array('id' => $news['id']));
		if (empty($material['media_id'])){
			$articles['articles'][] = $news;
		} else {
			$edit_attachment['media_id'] = $material['media_id'];
			$edit_attachment['index'] = $news['displayorder'];
			$edit_attachment['articles'] = $news;
			$result = $account_api->editMaterialNews($edit_attachment);
			if (is_error($result)){
				return error('-4', $result['message']);
			}
		}
	}
	if (empty($material['media_id'])){
		$media_id = $account_api->addMatrialNews($articles);
		if (is_error($media_id)) {
			return error('-5', $media_id, '');
		}
		$material_info = $account_api->getMaterial($media_id, false);
		if (!empty($material_info['news_item'])) {
			foreach ($material_info['news_item'] as $key => $info) {
				pdo_update('wechat_news', array('url' => $info['url']), array('uniacid' => $_W['uniacid'], 'attach_id' => $material['id'], 'displayorder' => $key));
			}
		}
		pdo_update('wechat_attachment', array(
			'media_id' => $media_id,
			'model' => 'perm'
		), array(
			'uniacid' => $_W['uniacid'],
			'id' => $attach_id
		));
	} else {
		pdo_update('wechat_attachment', array('model' => 'perm'), array('uniacid' => $_W['uniacid'], 'id' => $attach_id));
	}
	return $material;
}

function material_local_upload_by_url($url, $type='images') {
	global $_W;
	$account_api = WeAccount::create($_W['acid']);
	if (! empty($_W['setting']['remote']['type'])) {
		$remote_file_url = tomedia($url);
		$filepath = file_remote_attach_fetch($remote_file_url,0,'');
		if(is_error($filepath)) {
			return $filepath;
		}
		$filepath = ATTACHMENT_ROOT . $filepath;
	} else {
		if (strexists(parse_url($url, PHP_URL_PATH), '/attachment/')) {
			$url = substr(parse_url($url, PHP_URL_PATH), strpos(parse_url($url, PHP_URL_PATH), '/attachment/') + strlen('/attachment/'));
		}
		$filepath = ATTACHMENT_ROOT . $url;
	}
	$filesize = filesize($filepath);
	if ($filesize > 1024 * 1024 && $type == 'videos') {
		return error(-1, '要转换的微信素材视频不能超过10M');
	}
	return $account_api->uploadMediaFixed($filepath, $type);
}


function material_local_upload($material_id){
	global $_W;
	$type_arr = array('1' => 'images', '2' => 'voices', '3' => 'videos');
	$material = pdo_get('core_attachment', array('uniacid' => $_W['uniacid'], 'id' => $material_id));
	if (empty($material)) {
		return error('-1', '同步素材不存在或已删除');
	}
	return material_local_upload_by_url($material['attachment'], $type_arr[$material['type']]);
}


function material_upload_limit() {
	global $_W;
	$default = 5 * 1024 * 1024;
	$upload_limit = array(
		'num' => '30',
		'image' => $default,
		'voice' => $default,
		'video' => $default
	);
	$upload = $_W['setting']['upload'];
	if (isset($upload['image']['limit']) && (bytecount($upload['image']['limit'].'kb')>0)){
		$upload_limit['image'] = bytecount($upload['image']['limit'].'kb');
	}
	if (isset($upload['image']['limit']) && (bytecount($upload['audio']['limit'].'kb')>0)){
		$upload_limit['voice'] = $upload_limit['video'] = bytecount($upload['audio']['limit'].'kb');
	}
	return $upload_limit;
}


function material_news_delete($material_id){
	global $_W;
	$permission = permission_account_user_menu($_W['uid'], $_W['uniacid'], 'system');
	if (is_error($permission)) {
		return error(-1, $permission['message']);
	}
	if (empty($_W['isfounder']) && !empty($permission) && !in_array('platform_material', $permission) && !in_array('all', $permission)) {
		return error('-1', '您没有权限删除该文件');
	}
	$material_id = intval($material_id);
	$material = pdo_get('wechat_attachment', array('uniacid' => $_W['uniacid'], 'id' => $material_id));
	if (empty($material)){
		return error('-2', '素材文件不存在或已删除');
	}
	if (!empty($material['media_id'])){
		$account_api = WeAccount::create($_W['acid']);
		$result = $account_api->delMaterial($material['media_id']);
	}
	if (is_error($result)){
		return $result;
	}
	pdo_delete('wechat_news', array('uniacid' => $_W['uniacid'], 'attach_id' => $material_id));
	pdo_delete('wechat_attachment', array('uniacid' => $_W['uniacid'], 'id' => $material_id));
	return $result;
}


function material_delete($material_id, $location){
	global $_W;
	if (empty($_W['isfounder']) && !permission_check_account_user('platform_material_delete')) {
		return error('-1', '您没有权限删除该文件');
	}
	$material_id = intval($material_id);
	$table = $location == 'wechat' ? 'wechat_attachment' : 'core_attachment';
	$material = pdo_get($table, array('id' => $material_id));
	if (empty($material)){
		return error('-2', '素材文件不存在或已删除');
	}
	if ($location == 'wechat' && !empty($material['media_id'])){
		$account_api = WeAccount::create($_W['acid']);
		$result = $account_api->delMaterial($material['media_id']);
	} else {
		if (! empty($_W['setting']['remote']['type'])) {
			$result = file_remote_delete($material['attachment']);
		} else {
			$result = file_delete($material['attachment']);
		}
	}
	if (is_error($result)) {
		return error('-3', '删除文件操作发生错误');
	}
	pdo_delete($table, array('id' => $material_id));
	return $result;
}


function material_url_check($url) {
	if (empty($url)){
		return true;
	} else {
		$pattern ="/^((https|http|tel):\/\/|\.\/index.php)[^\s]+/i";
		return preg_match($pattern, $url);
	}
}

function material_news_list($server = '', $search ='', $page = array('page_index' => 1, 'page_size' => 24)) {
	global $_W;
	$conditions[':uniacid'] = $_W['uniacid'];
	$news_model_sql = '';
	if (!empty($server)) {
		$news_model_sql = " AND a.model = :news_model";
		$conditions[':news_model'] = $server;
	}

	$search_sql = '';
	if (!empty($search)) {
		$search_sql = " AND (b.title LIKE :search_title OR b.author = :search_author OR b.digest LIKE :search_digest)";
		$conditions[':search_title'] = "%{$search}%";
		$conditions[':search_author'] = "%{$search}%";
		$conditions[':search_digest'] = "%{$search}%";
	}

	$select_sql = "SELECT  %s FROM " . tablename('wechat_attachment') . " AS a RIGHT JOIN " . tablename('wechat_news') . " AS b ON a.id = b.attach_id WHERE  a.uniacid = :uniacid AND a.type = 'news' AND a.id <> '' " . $news_model_sql . $search_sql . "%s";
	$list_sql = sprintf($select_sql, "a.id as id, a.filename, a.attachment, a.media_id, a.type, a.model, a.tag, a.createtime, b.displayorder, b.title, b.digest, b.thumb_url, b.thumb_media_id, b.attach_id, b.url", " ORDER BY a.createtime DESC, b.displayorder ASC LIMIT " . ($page['page_index'] - 1) * $page['page_size'] . ", " . $page['page_size']);
	$total_sql = sprintf($select_sql, "count(*)", '');
	$total = pdo_fetchcolumn($total_sql, $conditions);
	$news_list = pdo_fetchall($list_sql, $conditions);
	$material_list = array();
	if (! empty($news_list)) {
		foreach ($news_list as $news){
			if (isset($material_list[$news['attach_id']])){
				$material_list[$news['attach_id']]['items'][$news['displayorder']] = $news;
			}else{
				$material_list[$news['attach_id']] = array(
					'id' => $news['id'],
					'filename' => $news['filename'],
					'attachment' => $news['attachment'],
					'media_id' => $news['media_id'],
					'type' => $news['type'],
					'model' => $news['model'],
					'tag' => $news['tag'],
					'createtime' => $news['createtime'],
					'items' => array($news['displayorder'] => $news),
				);
			}
		}
	}
		foreach ($material_list as $key => &$news) {
		if (isset($news['items']) && is_array($news['items'])) {
			foreach ($news['items'] as &$item) {
				$item['thumb_url'] = tomedia($item['thumb_url']);
			}
		}
	}
	unset($news_list);
	$pager = pagination($total, $page['page_index'], $page['page_size'],'',$context = array('before' => 5, 'after' => 4, 'isajax' => $_W['isajax']));
	$material_news = array('material_list' => $material_list, 'page' => $pager);
	return $material_news;
}

function material_list($type = '', $server = '', $page = array('page_index' => 1, 'page_size' => 24)) {
	global $_W;
	$tables = array(MATERIAL_LOCAL => 'core_attachment', MATERIAL_WEXIN => 'wechat_attachment');
	$conditions['uniacid'] = $_W['uniacid'];
		$table = $tables[$server];
		switch ($type) {
			case 'voice' :
				$conditions['type'] = $server == MATERIAL_LOCAL ? ATTACH_TYPE_VOICE : 'voice';
				break;
			case 'video' :
				$conditions['type'] = $server == MATERIAL_LOCAL ? ATTACH_TYPE_VEDIO : 'video';
				break;
			default :
				$conditions['type'] = $server == MATERIAL_LOCAL ? ATTACH_TYPE_IMAGE : 'image';
				break;
		}
		if ($server == 'local') {
			$material_list = pdo_getslice($table, $conditions, array($page['page_index'], $page['page_size']), $total, array(), '', 'createtime DESC');
		} else {
			$conditions['model'] = MATERIAL_WEXIN;
			$material_list = pdo_getslice($table, $conditions, array($page['page_index'], $page['page_size']), $total, array(), '', 'createtime DESC');
			if ($type == 'video'){
				foreach ($material_list as &$row) {
					$row['tag'] = $row['tag'] == '' ? array() : iunserializer($row['tag']);
				}
				unset($row);
			}
		}
		$pager = pagination($total, $page['page_index'], $page['page_size'],'',$context = array('before' => 5, 'after' => 4, 'isajax' => $_W['isajax']));
		$material_news = array('material_list' => $material_list, 'page' => $pager);
		return $material_news;
}



function material_news_to_local($attach_id) {
		$material = material_get($attach_id);
	if(is_error($material)) {
		return $material;
	}
	$attach_id = material_news_set($material['news'],$attach_id);
	if(is_error($attach_id)) {
		return $attach_id;
	}
	$material['items'] = $material['news'];	return $material;
}


function material_to_local($resourceid, $uniacid, $uid, $type = 'image') {
	$material = material_get($resourceid);
	if(is_error($material)) {
		return $material;
	}
	return material_network_image_to_local($material['url'], $uniacid, $uid);
}


function material_network_image_to_local($url, $uniacid, $uid) {
	return material_network_to_local($url, $uniacid, $uid, 'image');
}



function material_network_to_local($url, $uniacid, $uid, $type = 'image') {
	$path = file_remote_attach_fetch($url); 	if(is_error($path)) {
		return $path;
	}
	$filename = pathinfo($path,PATHINFO_FILENAME);
	$data = array('uniacid' => $uniacid, 'uid' => $uid,
		'filename' => $filename,
		'attachment' => $path,
		'type' => $type == 'image' ? ATTACH_TYPE_IMAGE : ($type == 'audio'||$type == 'voice' ? ATTACH_TYPE_VOICE : ATTACH_TYPE_VEDIO),
		'createtime'=>TIMESTAMP
	);
	pdo_insert('core_attachment', $data);
	$id = pdo_insertid();
	$data['id'] = $id;
	$data['url'] = tomedia($path);
	return $data;
}



function material_to_wechat($attach_id, $uniacid, $uid, $acid, $type = 'image') {
	$result = material_local_upload($attach_id); 	if (is_error($result)) {
		return $result;
	}
	$tag = $result['url'];
	if($type == 'video') {
		$tag = serialize(array('title'=>'网络视频','description'=>'网络视频'));
	}
	$data = array('uniacid' => $uniacid, 'uid' => $uid, 'acid' => $acid,
		'media_id' => $result['media_id'],
		'attachment' => $result['url'],
		'type' => $type,
		'tag' => $tag,
		'model' => 'perm',
		'createtime'=>TIMESTAMP
	);
	pdo_insert('wechat_attachment', $data);
	$id = pdo_insertid();
	$data['url'] = tomedia($result['url']);
	$data['id'] = $id;
	return $data;
}



function material_network_image_to_wechat($url, $uniacid, $uid, $acid) {
	return material_network_to_wechat($url, $uniacid, $uid, $acid, 'image');
}


function material_network_to_wechat($url, $uniacid, $uid, $acid, $type = 'image') {
	$local = material_network_to_local($url, $uniacid, $uid, $type); 	if (is_error($local)) {
		return $local;
	}
	return material_to_wechat($local['id'], $uniacid, $uid, $acid, $type);
}