<?php
!defined('M_P') && exit('Forbidden');
!$winduid && Showmsg('not_login');
require_once(D_P."data/bbscache/forum_cache.php");
require_once(R_P.'require/showimg.php');

InitGP(array('a','u'));
$page = (int)GetGP('page');

!$db_dopen && Showmsg('dairy_close');

$db_perpage = 10;
$page < 1 && $page = 1;

$u = (int)GetGP('u');
$u = $u ? $u : $winduid;

if ($u != $winduid) {
	$userdb = $db->get_one("SELECT username,icon FROM pw_members WHERE uid=".pwEscape($u));
	if (empty($userdb)) {
		$errorname = '';
		Showmsg('user_not_exists');
	}
	$thisbase = $basename .'q=diary&u='.$u.'&';
	list($faceimg) = showfacedesign($userdb['icon'],1);
	$username = $userdb['username'];
} else {
	$username = $windid;
	list($faceimg) = showfacedesign($winddb['icon'],1);
	$thisbase = $basename."q=diary&";
}

$o_uploadsize = !empty($o_uploadsize) ? unserialize($o_uploadsize) : array();

$uploadfiletype = $uploadfilesize = ' ';
foreach ($o_uploadsize as $key => $value) {
	$uploadfiletype .= $key.' ';
	$uploadfilesize .= $key.':'.$value.'KB; ';
}

$atc_name = getLangInfo('o','diary',false,1);

if (empty($a) || $a == 'privacy') {
	$did = (int)GetGP('did');
	require_once(R_P.'require/bbscode.php');

	$nums = $count = 0;
	if ($u != $winduid) {
		$sqladd = "WHERE uid=".pwEscape($u);
	} else {
		$sqladd = "WHERE uid=".pwEscape($winduid);
	}

	$diarydb = $diarytype = array();
	$query = $db->query("SELECT * FROM pw_diarytype $sqladd ORDER BY dtid");
	
	while ($rt = $db->fetch_array($query)) {
		$nums += $rt['num'];
		$diarytype[$rt['dtid']] = $rt;
	}
	$query = $db->query("SELECT dtid,privacy FROM pw_diary $sqladd");
	while ($rt = $db->fetch_array($query)) {
		if ($rt['privacy'] == '2' && $u != $winduid) {
			$dtids[$rt['dtid']] ++;
			$dnum ++;
		}
		$count ++;
	}
	if ($dtids) {
		foreach ($dtids as $key => $val) {
			if ($key) {
				$diarytype[$key]['num'] -= $val;
			} else {
				$nums ++;
			}
		}
	}
	if ($u != $winduid) {
		$count = $count - $dnum;
		$other = $count + $dnum - $nums;
	} else {
		$other = $count - $nums;
	}


	if (!$did) {
		$dtid = (int)GetGP('dtid');

		if ($u != $winduid) {
			$sqladd .= " AND privacy!=2";
		} else {
			$a == 'privacy' && $sqladd .= ' AND privacy=2';
		}
		if ($dtid == '-1') {
			$sqladd .= ' AND dtid=0';
		} elseif (is_numeric($dtid) && $dtid > 0) {
			$sqladd .= ' AND dtid='.pwEscape($dtid);
		}
		
		$sum = $db->get_value("SELECT COUNT(*) as count FROM pw_diary $sqladd");
		if ($sum) {
			$start = ($page-1)*$db_perpage;
			$numofpage = ceil($sum/$db_perpage);
			$query = $db->query("SELECT did,dtid,uid,aid,username,privacy,subject,ifconvert,ifwordsfb,content,r_num,c_num,postdate FROM pw_diary $sqladd ORDER BY postdate DESC ".pwLimit($start,$db_perpage));
			while ($rt = $db->fetch_array($query)) {
				$rt['postdate'] = get_date($rt['postdate'],'Y-m-d H:i');
				$rt['ifwordsfb'] != $db_wordsfb && $rt['content'] = wordsfb($rt['content'],$rt['ifwordsfb']);
				$rt['content'] = preg_replace("/\[s:(.+?)\]/eis",'',$rt['content']);
				if ($rt['ifconvert'] == 2) {
					$rt['content'] = convert($rt['content'], $db_windpost);
				} else {
					//strpos($rt['content'],'[s:') !== false && $rt['content'] = showface($rt['content']);
				}
				if ($o_shownum) {
					$rt['content'] = substrs($rt['content'],$o_shownum);
				}
				$rt['content'] = str_replace("\n","<br />",$rt['content']);

				$attachs = $aids = array();
				if ($rt['aid']) {
					$attachs = unserialize($rt['aid']);
					if (is_array($attachs)) {
						$aids = attachment($rt['content']);
					}
				}
				
				if ($attachs && is_array($attachs)) {
					if ($winduid == $rt['authorid'] || $groupid == 3) {
						$dfadmin = 1;
					} else {
						$dfadmin = 0;
					}
					foreach ($attachs as $at) {
						$atype = '';
						$rat = array();
						if ($at['type'] == 'img') {
							$a_url = geturl('diary/'.$at['attachurl'],'show');
							if (is_array($a_url)) {
								$atype = 'pic';
								$dfurl = '<br>'.cvpic($a_url[0], 1, $db_windpost['picwidth'], $db_windpost['picheight'], $at['ifthumb']);
								$rat = array('aid' => $at['aid'], 'img' => $dfurl, 'dfadmin' => $dfadmin, 'desc' => $at['desc']);
							}
						}
						if (!$atype) continue;
						if (in_array($at['aid'], $aids)) {
							$rt['content'] = attcontent($rt['content'], $atype, $rat);
						} else {
							
							$rt[$atype][$at['aid']] = $rat;
						}
					}
				}

				$rt['link'] = "$db_bbsurl/{$basename}q=diary&u=$rt[uid]&did=$rt[did]";
				$rt['title'] = "($rt[link])";
				
				$diarydb[] = $rt;
			}
			$pages = numofpage($sum,$page,$numofpage,$thisbase);
		}
	} elseif (is_numeric($did)) {
		if ($u != $winduid) {
			$sqladd .= " AND privacy!=2 AND did=".pwEscape($did);
		} else {
			$sqladd .= " AND did=".pwEscape($did);
		}

		$rt = $db->get_one("SELECT did,dtid,uid,aid,username,privacy,subject,content,ifconvert,ifwordsfb,r_num,c_num,postdate,ifcopy,copyurl FROM pw_diary $sqladd");
		!$rt && Showmsg('undefined_action');
		$rt['r_num'] += 1;
		
		$attachs = $aids = array();
		if ($rt['aid']) {
			$attachs = unserialize($rt['aid']);
			if (is_array($attachs)) {
				$aids = attachment($rt['content']);
			}
		}
		
		$rt['content'] = str_replace("\n","<br />",$rt['content']);
		if ($attachs && is_array($attachs)) {
			if ($winduid == $rt['authorid'] || $groupid == 3) {
				$dfadmin = 1;
			} else {
				$dfadmin = 0;
			}
			foreach ($attachs as $at) {
				$atype = '';
				$rat = array();
				if ($at['type'] == 'img') {
					$a_url = geturl('diary/'.$at['attachurl'],'show');
					
					if (is_array($a_url)) {
						$atype = 'pic';
						$dfurl = '<br>'.cvpic($a_url[0], 1, $db_windpost['picwidth'], $db_windpost['picheight'], $at['ifthumb']);
						$rat = array('aid' => $at['aid'], 'img' => $dfurl, 'dfadmin' => $dfadmin, 'desc' => $at['desc']);
					}
				}
				if (!$atype) continue;
				if (in_array($at['aid'], $aids)) {
					$rt['content'] = attcontent($rt['content'], $atype, $rat);
				} else {
					
					$rt[$atype][$at['aid']] = $rat;
				}
			}
		}

		$rt['link'] = "$db_bbsurl/{$basename}q=diary&u=$rt[uid]&did=$rt[did]";
		$rt['title'] = "($rt[link])";

		$rt['ifwordsfb'] != $db_wordsfb && $rt['content'] = wordsfb($rt['content'],$rt['ifwordsfb']);
		if ($rt['ifconvert'] == 2) {
			$rt['content'] = convert($rt['content'], $db_windpost);
		} else {
			strpos($rt['content'],'[s:') !== false && $rt['content'] = showface($rt['content']);
		}

		$rt['postdate'] = get_date($rt['postdate'],'Y-m-d H:i');
		
		$db->update("UPDATE pw_diary SET r_num=r_num+1 WHERE uid=".pwEscape($u)." AND did=".pwEscape($did));
	}

} elseif ($a == 'friend') {
	if ($friends = getFriends($winduid,0,0,false,1)) {
		$sqladd = 'WHERE privacy!=2';
		$uids = array_keys($friends);
		$sqladd .= ' AND uid IN('.pwImplode($uids).')';
	}
	if ($sqladd) {
		require_once(R_P.'require/bbscode.php');
		$sum = $db->get_value("SELECT COUNT(*) as count FROM pw_diary $sqladd");
		if ($sum) {
			$start = ($page-1)*$db_perpage;
			$numofpage = ceil($sum/$db_perpage);
			$query = $db->query("SELECT did,dtid,uid,aid,username,privacy,subject,ifconvert,ifwordsfb,content,r_num,c_num,postdate FROM pw_diary $sqladd ORDER BY postdate DESC ".pwLimit($start,$db_perpage));
			while ($rt = $db->fetch_array($query)) {

				$rt['postdate'] = get_date($rt['postdate'],'Y-m-d H:i');
				$rt['ifwordsfb'] != $db_wordsfb && $rt['content'] = wordsfb($rt['content'],$rt['ifwordsfb']);
				if ($rt['ifconvert'] == 2) {
					$rt['content'] = convert($rt['content'], $db_windpost);
				} else {
					strpos($rt['content'],'[s:') !== false && $rt['content'] = showface($rt['content']);
				}
				if ($o_shownum) {
					$rt['content'] = substrs($rt['content'],$o_shownum);
				}
				$rt['content'] = str_replace("\n","<br />",$rt['content']);

				$attachs = $aids = array();
				if ($rt['aid']) {
					$attachs = unserialize($rt['aid']);
					if (is_array($attachs)) {
						$aids = attachment($rt['content']);
					}
				}
				
				if ($attachs && is_array($attachs)) {
					if ($winduid == $rt['authorid'] || $groupid == 3) {
						$dfadmin = 1;
					} else {
						$dfadmin = 0;
					}
					foreach ($attachs as $at) {
						$atype = '';
						$rat = array();
						if ($at['type'] == 'img') {
							$a_url = geturl('diary/'.$at['attachurl'],'show');
							if (is_array($a_url)) {
								$atype = 'pic';
								$dfurl = '<br>'.cvpic($a_url[0], 1, $db_windpost['picwidth'], $db_windpost['picheight'], $at['ifthumb']);
								$rat = array('aid' => $at['aid'], 'img' => $dfurl, 'dfadmin' => $dfadmin, 'desc' => $at['desc']);
							}
						}
						if (!$atype) continue;
						if (in_array($at['aid'], $aids)) {
							$rt['content'] = attcontent($rt['content'], $atype, $rat);
						} else {
							
							$rt[$atype][$at['aid']] = $rat;
						}
					}
				}

				$rt['link'] = "$db_bbsurl/{$basename}q=diary&u=$rt[uid]&did=$rt[did]";
				$rt['title'] = "($rt[link])";

				if ($rt['uid'] != $winduid && $friends[$rt['uid']]) {
					$rt['face'] = $friends[$rt['uid']]['face'];
				}

				$diarydb[] = $rt;
			}
			$pages = numofpage($sum,$page,$numofpage,$thisbase."a=$a&");
		}
	}

} elseif ($a == 'write') {
	if (!$_POST['step']) {
		$editor = getstatus($winddb['userstatus'],11) ? 'wysiwyg' : 'textmode';
		$dtsel = '';
		$query = $db->query("SELECT * FROM pw_diarytype WHERE uid=".pwEscape($winduid)." ORDER BY dtid");
		while ($rt = $db->fetch_array($query)) {
			$dtsel .= "<option value=\"$rt[dtid]\">$rt[name]</option>";
		}
		$checked = 'checked';
		$disabled = '';
	} elseif ($_POST['step'] == 2) {
		InitGP(array('dtid','privacy','ifcopy'),'P');
		require_once(R_P.'require/bbscode.php');
		require_once(R_P.'require/postfunc.php');

		if(@include(D_P."data/bbscache/wordsfb.php")){
			if ($wordsfb) {
				foreach ($wordsfb as $key => $value) {
					$banword = (string) stripslashes($key);
					if (strpos($_POST['atc_title'],$banword)!==false) {
						Showmsg('title_wordsfb');

					} elseif (strpos($_POST['atc_content'],$banword)!==false) {
						Showmsg('content_wordsfb');
					}
				}
			}
			if ($replace) {
				foreach ($replace as $key => $value) {
					$banword = (string) stripslashes($key);
					if (strpos($_POST['atc_title'],$banword)!==false) {
						Showmsg('title_wordsfb');
					} elseif (strpos($_POST['atc_content'],$banword)!==false) {
						Showmsg('content_wordsfb');
					}
				}
			}
		}

		list($atc_title,$atc_content,$ifconvert,$ifwordsfb) = check_data('new');
		//$db_tcheck && $winddb['postcheck'] == tcheck($atc_content) && Showmsg('diary_content_same'); //内容验证
		
		$dtid = (int)$dtid;
		$privacy = (int)$privacy;
		$ifcopy = (int)$ifcopy;
		!$privacy && $ifcopy = 1;

		require_once(M_P.'require/upload.php');

		$pwSQL = pwSqlSingle(array(
			'uid'		=> $winduid,
			'dtid'		=> $dtid,
			'aid'		=> $attachs,
			'username'	=> $windid,
			'privacy'	=> $privacy,
			'subject'	=> $atc_title,
			'content'	=> $atc_content,
			'ifcopy'	=> $ifcopy,
			'ifconvert'	=> $ifconvert,
			'ifupload'	=> $ifupload,
			'ifwordsfb'	=> $ifwordsfb,
			'postdate'	=> $timestamp,
		));
		$db->update("INSERT INTO pw_diary SET $pwSQL");
		$db->update("UPDATE pw_diarytype SET num=num+1 WHERE uid=".pwEscape($winduid)." AND dtid=".pwEscape($dtid));//更新分类日志数
		$did = $db->insert_id();

		if ($aids) {
			$db->update("UPDATE pw_attachs SET did=".pwEscape($did)." WHERE aid IN($aids)");
		}

		if ($privacy != 2) {
			pwAddFeed($winduid,'diary',$did,array(
									'lang'=>'diary_data',
									'username'=>$windid,
									'uid'=>$winduid,
									'did'=>$did,
									'subject'=>$atc_title,)
				);
		}
		countPosts('+1');

		refreshto("$thisbase",'operate_success');
	}
} elseif ($a == 'edit') {
	if (!$_POST['step']) {
		$did = (int)GetGP('did');
		$editor = getstatus($winddb['userstatus'],11) ? 'wysiwyg' : 'textmode';
		$dtsel = '';
		$rt = $db->get_one("SELECT did,dtid,aid,privacy,subject,content,ifcopy FROM pw_diary WHERE uid=".pwEscape($winduid)." AND did=".pwEscape($did));
		
		!$rt && Showmsg('illegal_request');
		$attach = '';
		if ($rt['aid']) {
			$attachs = unserialize($rt['aid']);
			if (is_array($attachs)) {
				foreach ($attachs as $key => $value) {
					list($value['attachurl'],) = geturl('diary/'.$value['attachurl'],'lf');
					$attach .= "'$key' : ['$value[name]', '$value[size]', '$value[attachurl]', '$value[type]', '$value[special]', '$value[needrvrc]', '$value[ctype]', '$value[desc]'],";
				}
				$attach = rtrim($attach,',');
			}
		}

		$atc_content = $rt['content'];
		${'privacy_'.$rt['privacy']} = 'selected';
		$rt['ifcopy'] && $checked = 'checked';
		($rt['privacy'] == '2') && $disabled = 'disabled';

		$query = $db->query("SELECT * FROM pw_diarytype WHERE uid=".pwEscape($winduid)." ORDER BY dtid");
		while ($rs = $db->fetch_array($query)) {
			$selected = '';
			$rs['dtid'] == $rt['dtid'] && $selected .= 'selected';
			$dtsel .= "<option value=\"$rs[dtid]\" $selected>$rs[name]</option>";
		}
	} elseif ($_POST['step'] == 2) {
		InitGP(array('did','dtid','dtided','privacy','privacyed','ifcopy'),'P');

		require_once(R_P.'require/bbscode.php');
		require_once(R_P.'require/postfunc.php');

		if(@include(D_P."data/bbscache/wordsfb.php")){
			if ($wordsfb) {
				foreach ($wordsfb as $key => $value) {
					$banword = (string) stripslashes($key);
					if (strpos($_POST['atc_title'],$banword)!==false) {
						Showmsg('title_wordsfb');

					} elseif (strpos($_POST['atc_content'],$banword)!==false) {
						Showmsg('content_wordsfb');
					}
				}
			}
			if ($replace) {
				foreach ($replace as $key => $value) {
					$banword = (string) stripslashes($key);
					if (strpos($_POST['atc_title'],$banword)!==false) {
						Showmsg('title_wordsfb');
					} elseif (strpos($_POST['atc_content'],$banword)!==false) {
						Showmsg('content_wordsfb');
					}
				}
			}
		}

		list($atc_title,$atc_content,$ifconvert,$ifwordsfb) = check_data('modify');
		//$db_tcheck && $winddb['postcheck'] == tcheck($atc_content) && Showmsg('diary_content_same'); //内容验证
		
		$dtid = (int)$dtid;
		$dtided = (int)$dtided;
		$privacy = (int)$privacy;
		$ifcopy = (int)$ifcopy;

		/**
		* 附件修改
		*/
		$oldattach = $replacedb = $unsetattach = array();

		$aid = $db->get_value("SELECT aid FROM pw_diary WHERE uid=".pwEscape($winduid)." AND did=".pwEscape($did));
		
		if ($aid) {
			InitGP(array('keep','oldatt_special','oldatt_needrvrc'), 'P', 2);
			InitGP(array('oldatt_ctype','oldatt_desc'), 'P');
			$oldattach = unserialize(stripslashes($aid));
			foreach ($oldattach as $key => $value) {
				if (!@in_array($key,$keep)) {
					$unsetattach[$key] = $value;
					unset($oldattach[$key]);
				} else {
					$v = array(
						'special'	=> $oldatt_special[$key],		'ctype'		=> $oldatt_ctype[$key],
						'needrvrc'	=> $oldatt_needrvrc[$key],		'desc'		=> $oldatt_desc[$key]
					);
					
					$v['needrvrc'] = $v['special'] = 0;
					$v['ctype'] = '';
					
					$oldattach[$key] = array_merge($oldattach[$key], $v);

					if (array_key_exists('replace_'.$key, $_FILES)) {
						$db_attachnum++;
						$replacedb[$key] = $oldattach[$key];
					} elseif ($value['ctype'] <> $v['ctype'] || $value['desc'] <> $v['desc']) {
						$runsql[] = 'UPDATE pw_attachs SET ' . pwSqlSingle(array(
							'needrvrc'	=> $v['needrvrc'],
							'descrip'	=> $v['desc'],
							'special'	=> $v['special'],
							'ctype'		=> $v['ctype']
						)) . ' WHERE aid=' . pwEscape($key);
					}
				}
			}
		}
		require_once(M_P.'require/upload.php');

		if ($attachs) {
			$attachs = unserialize(stripslashes($attachs));
			foreach ($attachs as $key => $value) {
				$oldattach[$key] = $value;
			}
			$upmemdata .= ($upmemdata ? ',' : '')."uploadtime=".pwEscape($winddb['uploadtime']).",uploadnum=".pwEscape($winddb['uploadnum']);
		}
		if ($upmemdata) {
			$db->update("UPDATE pw_memberdata SET $upmemdata WHERE uid=".pwEscape($winduid));
		}
		if ($oldattach) {
			$oldattach = addslashes(serialize($oldattach));
		} else {
			$oldattach = '';
		}
		/**
		* 附件修改
		*/

		$pwSQL = pwSqlSingle(array(
			'dtid'		=> $dtid,
			'aid'		=> $oldattach,
			'privacy'	=> $privacy,
			'subject'	=> $atc_title,
			'content'	=> $atc_content,
			'ifcopy'	=> $ifcopy,
			'ifconvert'	=> $ifconvert,
			'ifupload'	=> $ifupload,
			'ifwordsfb'	=> $ifwordsfb,
		));


		$db->update("UPDATE pw_diary SET $pwSQL WHERE uid=".pwEscape($winduid)." AND did=".pwEscape($did));

		if ($aids) {
			$db->update("UPDATE pw_attachs SET did=".pwEscape($did)." WHERE aid IN($aids)");
		}
		
		if ($dtided != $dtid) {
			$db->update("UPDATE pw_diarytype SET num=num-1 WHERE uid=".pwEscape($winduid)." AND dtid=".pwEscape($dtided));
			$db->update("UPDATE pw_diarytype SET num=num+1 WHERE uid=".pwEscape($winduid)." AND dtid=".pwEscape($dtid));
		}

		if ($privacyed == 2 && $privacy !=2) {
			pwAddFeed($winduid,'diary',$did,array(
									'lang'=>'diary_data',
									'username'=>$windid,
									'uid'=>$winduid,
									'did'=>$did,
									'subject'=>$atc_title,)
				);
			countPosts('+1');
		} elseif ($privacyed != 2 && $privacy ==2) {
			if ($affected_rows = delAppAction('diary',$did)) {
				countPosts("-$affected_rows");
			}
		}

		refreshto("$thisbase",'operate_success');
	}
} elseif ($a == 'next') {

	define('AJAX',1);
	$did = (int)GetGP('did');
	$sqladd = "WHERE uid=".pwEscape($u);
	if ($u != $winduid) {
		$sqladd .= " AND privacy!=2 AND did>".pwEscape($did);
	} else {
		$sqladd .= " AND did>".pwEscape($did);
	}

	$did = $db->get_value("SELECT MIN(did) FROM pw_diary $sqladd");

	echo "success\t$did";
	ajax_footer();

} elseif ($a == 'pre') {

	define('AJAX',1);
	$did = (int)GetGP('did');
	$sqladd = "WHERE uid=".pwEscape($u);
	if ($u != $winduid) {
		$sqladd .= " AND privacy!=2 AND did<".pwEscape($did);
	} else {
		$sqladd .= " AND did<".pwEscape($did);
	}

	$did = $db->get_value("SELECT MAX(did) FROM pw_diary $sqladd");
	echo "success\t$did";
	ajax_footer();

}

require_once(R_P.'require/header.php');
require_once PrintEot('m_diary');footer();

?>