Пример #1
0
function main()
{
    global $session;
    global $db_connect_info;
    $http_article_title = trim(strip_tags(empty($_POST['article-title']) ? '' : $_POST['article-title']));
    if (!$session->started()) {
        navigateTo(HREF_SIGNIN . '?redirect=' . HREF_CREATE . '?t=' . $http_article_title);
    }
    if (strlen(preg_replace('/\\s+/', '', $http_article_title)) < 2) {
        return array('result' => true, 'message' => '');
    }
    if (is_numeric($http_article_title)) {
        return array('result' => false, 'message' => '지식 제목으로 숫자를 사용할 수 없습니다');
    }
    $db = new YwDatabase($db_connect_info);
    if (!$db->connect()) {
        return array('result' => false, 'title' => $http_article_title, 'message' => '서버와의 연결에 실패했습니다');
    }
    if (!$db->query("SELECT 1 FROM " . ARTICLE_TABLE . " WHERE `title`='" . $http_article_title . "';")) {
        return array('result' => false, 'title' => $http_article_title, 'message' => '지식 정보를 조회하는데 실패했습니다');
    }
    if ($db->total_results() > 0) {
        return array('result' => false, 'title' => $http_article_title, 'message' => '이미 존재하는 지식입니다');
    }
    // 지식 등록
    if (!$db->query("INSERT INTO " . ARTICLE_TABLE . " (`title`) VALUES ('" . $db->purify($http_article_title) . "');")) {
        return array('result' => false, 'title' => $http_article_title, 'message' => '지식을 추가하는 중 서버 오류가 발생했습니다');
    }
    $db->log($session->name, LOG_CREATE, $http_article_title);
    $db->close();
    navigateTo(HREF_WRITE . '/' . $http_article_title);
    return array('result' => true, 'title' => $http_article_title, '');
}
Пример #2
0
function main()
{
    global $session;
    global $db_connect_info;
    global $http_user_email;
    if ($session->started()) {
        navigateTo(HREF_MAIN);
    }
    $http_user_email = trim($_POST['user-email']);
    // 입력 값의 유효성을 검증한다.
    if (empty($http_user_email)) {
        return array('result' => true, 'message' => '');
    }
    // 이메일 포멧의 유효성을 검증한다.
    if (!filter_var($http_user_email, FILTER_VALIDATE_EMAIL)) {
        return array('result' => false, 'message' => '이메일 주소가 올바르지 않습니다');
    }
    // reCAPTCHA를 검증한다.
    if (!getReCaptcha()) {
        return array('result' => false, 'message' => 'reCAPTCHA가 올바르게 입력되지 않았습니다');
    }
    $db = new YwDatabase($db_connect_info);
    // 데이터베이스 연결을 체크한다.
    if (!$db->connect()) {
        return array('result' => false, 'message' => '서버와의 연결에 실패했습니다');
    }
    // 아이디와 이메일 유효성을 검증한다.
    if (!$db->query("SELECT `name` FROM " . USER_TABLE . " WHERE `email`='" . $db->purify($http_user_email) . "';")) {
        return array('result' => false, 'message' => '이메일 주소를 조회하는데 실패했습니다');
    }
    if ($db->total_results() < 1) {
        return array('result' => false, 'message' => '존재하지 않는 이메일 주소입니다');
    }
    $result = $db->get_result();
    $user_name = $result['name'];
    // 새로운 비밀번호를 생성한다.
    $generated_password = bin2hex(openssl_random_pseudo_bytes(6));
    if (!$db->query("UPDATE " . USER_TABLE . " SET `password`='" . passwordHash($generated_password) . "' WHERE `email`='" . $db->purify($http_user_email) . "';")) {
        return array('result' => false, 'message' => '비밀번호를 업데이트하는데 실패했습니다');
    }
    $email_content = "<b>" . $user_name . "</b> 회원님의 새 비밀번호는 <b>" . $generated_password . "</b>입니다.";
    if (!getMailer($http_user_email, "연세위키 비밀번호를 알려드립니다", $email_content)) {
        return array('result' => false, 'message' => '이메일 발송에 실패했습니다');
    }
    $db->log($user_name, LOG_RESET, '1');
    $db->close();
    return array('result' => true, 'message' => '이메일로 아이디와 새로운 비밀번호를 전송했습니다');
}
Пример #3
0
function main()
{
    global $session;
    global $db_connect_info;
    $http_revision_id = trim($_GET['i']);
    $http_revision_target_id = trim($_GET['j']);
    $orginal_target = intval($http_revision_target_id) == 0;
    if (empty($http_revision_id) || empty($http_revision_target_id)) {
        return array('result' => false);
    }
    $db = new YwDatabase($db_connect_info);
    // 데이터베이스 연결을 체크합니다.
    if (!$db->connect()) {
        return array('result' => '서버와의 연결에 실패했습니다');
    }
    if (!$db->query("SELECT * FROM " . REVISION_TABLE . " WHERE `id`='" . $http_revision_id . "' LIMIT 1;")) {
        return array('result' => '글을 읽어오던 중 서버 에러가 발생했습니다');
    }
    if ($db->total_results() < 1) {
        return array('result' => '존재하지 않는 지식입니다');
    }
    $original = $db->get_result();
    // j값이 0일 경우 원본을 비교 대상으로 지정한다.
    if ($orginal_target) {
        $query = "SELECT * FROM " . ARTICLE_TABLE . " WHERE `id`='" . $revision['article_id'] . "' LIMIT 1;";
    } else {
        $query = "SELECT * FROM " . REVISION_TABLE . " WHERE `id`='" . $http_revision_target_id . "' LIMIT 1;";
    }
    if (!$db->query($query)) {
        return array('result' => '글을 읽어오던 중 서버 에러가 발생했습니다');
    }
    if ($db->total_results() < 1) {
        return array('result' => '존재하지 않는 지식입니다');
    }
    if ($orginal_target) {
        $result = $db->get_result();
        $revision['id'] = 0;
        $revision['article_title'] = $result['title'];
        $revision['snapshot_content'] = $result['content'];
        $revision['snapshot_tags'] = $result['tags'];
        $revision['timestamp'] = $result['timestamp'];
    } else {
        $revision = $db->get_result();
    }
    $db->close();
    return array('original' => $original, 'revision' => $revision);
}
Пример #4
0
function main()
{
    global $session;
    global $db_connect_info;
    $db = new YwDatabase($db_connect_info);
    // 데이터베이스 연결을 체크한다.
    if (!$db->connect()) {
        return array('result' => false, 'message' => '서버와의 연결에 실패했습니다');
    }
    $now_time = intval(date("H")) * 60 + intval(date("i"));
    if ($now_time > 19 * 60) {
        $now_date = date('Y-m-d', strtotime('+1 day'));
    } else {
        $now_date = date("Y-m-d");
    }
    return array('result' => true, 'recent' => getRecentUpdates($db), 'popular' => getPopularArticles($db), 'date' => $now_date);
}
Пример #5
0
function main()
{
    $http_query = trim(empty($_GET['q']) ? $_POST['q'] : $_GET['q']);
    $http_page = intval(empty($_GET['p']) ? '0' : $_GET['p']);
    if (empty($http_query)) {
        return array('result' => false, 'message' => '검색어가 없습니다');
    }
    // 검색 모드
    $tag_search_mode = strlen($http_query) > 1 && strcmp($http_query[0], "@") == 0;
    if ($tag_search_mode) {
        $http_query = substr($http_query, 1);
    }
    // 쿼리 유효성 검증
    $http_query = preg_replace('/\\s+/', ' ', $http_query);
    if (strlen($http_query) < 1) {
        return array('result' => false, 'message' => '검색어가 없습니다');
    }
    // 검색 쿼리 취득
    $keywords = explode(' ', $http_query);
    $query = $tag_search_mode ? getTagSearchQuery($keywords) : getContentSearchQuery($keywords);
    global $db_connect_info;
    $db = new YwDatabase($db_connect_info);
    if (!$db->connect()) {
        return array('result' => false, 'message' => '서버와의 연결에 실패했습니다');
    }
    // 정확히 제목이 일치하는 항목이 있으면 바로 이동
    if (count($keywords) == 1) {
        if (!$db->query("SELECT 1 FROM " . ARTICLE_TABLE . " WHERE `title`='" . $keywords[0] . "';")) {
            return array('result' => false, 'message' => '검색 결과를 가져오는데 실패했습니다' . ("SELECT 1 FROM " . ARTICLE_TABLE . " WHERE `title`='" . $keywords[0] . "';"));
        }
        if ($db->total_results() > 0) {
            navigateTo(HREF_READ . '/' . $keywords[0]);
        }
    }
    $start_time = microtime(true);
    // 전체 검색 결과를 얻기 위해 먼저 서치
    if (!$db->query($query)) {
        return array('result' => false, 'message' => '검색 결과를 가져오는데 실패했습니다2');
    }
    $elapsed_time = round(microtime(true) - $start_time, 5);
    $total_articles = $db->total_results();
    // 현재 페이지 결과 가져오기
    $query .= " LIMIT " . $http_page * MAX_ARTICLES . ", " . MAX_ARTICLES . ";";
    if (!$db->query($query)) {
        return array('result' => false, 'message' => '검색 결과를 가져오는데 실패했습니다3');
    }
    $search_result = array();
    while ($result = $db->get_result()) {
        array_push($search_result, $result);
    }
    return array('result' => true, 'search_result' => $search_result, 'keywords' => $keywords, 'total_results' => $total_articles, 'elapsed_time' => $elapsed_time);
}
Пример #6
0
function main()
{
    global $session;
    global $db_connect_info;
    $db = new YwDatabase($db_connect_info);
    // 데이터베이스 연결을 체크한다.
    if (!$db->connect()) {
        return array('result' => false, 'message' => '서버와의 연결에 실패했습니다');
    }
    if (!$db->query("SELECT * FROM " . REVISION_TABLE . " WHERE `id` IN (SELECT MAX(`id`) FROM " . REVISION_TABLE . " GROUP BY `article_id`) ORDER BY `id` DESC LIMIT " . MAX_RECENT_CHANGED . ";")) {
        return array('result' => false, 'message' => '서버와의 연결에 실패했습니다');
    }
    if ($db->total_results() < 1) {
        return array('result' => false, 'message' => '서버와의 연결에 실패했습니다');
    }
    $result_array = array();
    while ($result = $db->get_result()) {
        array_push($result_array, $result);
    }
    return array('result' => true, 'recent' => $result_array, 'message' => '');
}
Пример #7
0
function main()
{
    global $session;
    global $db_connect_info;
    $db = new YwDatabase($db_connect_info);
    if (!$db->connect()) {
        return array('result' => '서버와의 연결에 실패했습니다');
    }
    date_default_timezone_set('Asia/Seoul');
    $now_time = intval(date("H")) * 60 + intval(date("i"));
    // 만약 석식 시간이 지났으면 다음날 식단 보여주기
    if ($now_time > DINNER_END) {
        $now_date = date('Y-m-d', strtotime('+1 day'));
    } else {
        $now_date = date("Y-m-d");
    }
    if (!$db->query("SELECT `content` FROM " . ARTICLE_TABLE . " WHERE `title`='" . $now_date . " 국제캠퍼스 학식 정보' LIMIT 1;")) {
        return array('result' => false);
    }
    $result = $db->get_result();
    return array('result' => $result['content']);
}
Пример #8
0
function main()
{
    global $session;
    global $db_connect_info;
    $http_file = $_FILES['userfile'];
    if (!isset($http_file)) {
        return array('result' => '');
    }
    if (!$session->started()) {
        return array('result' => '로그인한 사용자만 업로드가 가능합니다');
    }
    $file_size = $http_file['size'];
    if ($file_size > FILE_MAXIMUM_SIZE) {
        return array('result' => '파일 최대 업로드 용량(' . FILE_MAXIMUM_SIZE / 1024 / 1024 . 'MB)을 초과하였습니다');
    }
    $file_extension = strtolower(pathinfo($http_file['name'], PATHINFO_EXTENSION));
    if (array_search($file_extension, FILE_AVALIABLE_EXTENSIONS, false) === false) {
        return array('result' => '업로드 가능한 포맷이 아닙니다');
    }
    $db = new YwDatabase($db_connect_info);
    if (!$db->connect()) {
        return array('result' => '서버와의 연결에 실패했습니다');
    }
    if (!$db->query("SHOW TABLE STATUS LIKE '" . 'yw_file' . "';")) {
        return array('result' => '서버와의 연결에 실패했습니다');
    }
    $result = $db->get_result();
    // 파일 이름 (36진수)
    $file_name = base_convert(intval($result['Auto_increment']), 10, 36);
    $file_path = FILE_DIRECTORY . '/' . $file_name . "." . $file_extension;
    if (!move_uploaded_file($http_file['tmp_name'], $file_path)) {
        return array('result' => '업로드에 실패했습니다');
    }
    if (!$db->query("INSERT INTO " . FILE_TABLE . " (`name`, `type`, `size`, `uploader`) VALUES ('" . $file_name . "." . $file_extension . "', '" . $file_extension . "', " . $file_size . ", '" . $session->name . "')")) {
        return array('result' => '파일 등록에 실패했습니다');
    }
    return array('result' => '/' . $file_path);
}
Пример #9
0
function main()
{
    global $session;
    global $db_connect_info;
    $http_article_title = trim($_GET['t']);
    $http_article_id = trim($_GET['i']);
    $http_no_redirect = isset($_GET['no-redirect']);
    $read_by_id = !empty($http_article_id);
    if (empty($http_article_title) && empty($http_article_id)) {
        return array('result' => false, 'message' => '');
    }
    $db = new YwDatabase($db_connect_info);
    // 데이터베이스 연결을 체크합니다.
    if (!$db->connect()) {
        return array('result' => false, 'message' => '서버와의 연결에 실패했습니다');
    }
    if ($read_by_id) {
        $query = "SELECT * FROM " . ARTICLE_TABLE . " WHERE `id`='{$http_article_id}' LIMIT 1;";
    } else {
        $query = "SELECT * FROM " . ARTICLE_TABLE . " WHERE `title`='{$http_article_title}' LIMIT 1;";
    }
    if (!$db->query($query)) {
        return array('result' => false, 'message' => '글을 읽어오던 중 서버 에러가 발생했습니다');
    }
    if ($db->total_results() < 1) {
        if (!$read_by_id) {
            navigateTo(HREF_SUGGEST . '?t=' . $http_article_title);
        }
        return array('result' => false, 'message' => '존재하지 않는 지식입니다');
    }
    $article = $db->get_result();
    $article_id = intval($result['id']);
    $article_title = $result['title'];
    $article_content = $result['content'];
    $article_tags = $result['tags'];
    $article_hits = $result['hits'];
    // 리다이렉트 문서인지 체크
    $stripped_content = trim(strip_tags($article['content']));
    if (!$http_no_redirect && startsWith($stripped_content, REDIRECT_KEYWORD)) {
        navigateTo(HREF_READ . '/' . trim(explode(' ', $stripped_content)[1]) . '?from=' . $article['title']);
    }
    // 조회수 증가
    if ($session->visit(intval($article['id']))) {
        $db->query("UPDATE " . ARTICLE_TABLE . " SET `hits`=`hits`+1, `today_hits`=`today_hits`+1 WHERE `id`='" . $article['id'] . "';");
    }
    $db->close();
    return array('result' => true, 'article' => $article, 'message' => '');
}
Пример #10
0
function main()
{
    global $session;
    global $db_connect_info;
    $http_user_name = trim(strip_tags($_POST['user-name']));
    $http_user_password = trim($_POST['user-password']);
    $http_redirect = empty($_POST['redirect']) ? HREF_MAIN : $_POST['redirect'];
    if ($session->started()) {
        navigateTo(HREF_MAIN);
    }
    if (empty($http_user_name) || empty($http_user_password)) {
        return array('result' => true, 'message' => '');
    }
    $db = new YwDatabase($db_connect_info);
    if (!$db->connect()) {
        return array('result' => false, 'message' => '서버와의 연결에 실패했습니다');
    }
    // 아이디가 유효한지 확인합니다.
    if (!$db->query("SELECT * FROM " . USER_TABLE . " WHERE `name`='" . $db->purify($http_user_name) . "';")) {
        return array('result' => false, 'message' => '유저 정보를 불러오는데 실패했습니다');
    }
    if ($db->total_results() < 1) {
        return array('result' => false, 'message' => '존재하지 않는 아이디입니다');
    }
    $result = $db->get_result();
    // 비밀번호가 일치하는지 확인합니다.
    if (strcmp(passwordHash($http_user_password), $result['password']) != 0) {
        $db->log($session->ip, LOG_SIGNIN, '0');
        return array('result' => false, 'message' => '비밀번호가 올바르지 않습니다');
    }
    // 세션 등록
    $session->start($result['name'], $result['id'], intval($result['permission']));
    $db->log($session->name, LOG_SIGNIN, '1');
    $db->close();
    navigateTo($http_redirect);
    return array('result' => true, 'message' => '');
}
Пример #11
0
function main()
{
    global $session;
    global $db_connect_info;
    $http_article_title = trim($_GET['t']);
    $http_article_id = trim($_GET['i']);
    $http_revisions_page = intval(isset($_GET['p']) ? $_GET['p'] : '0');
    $read_by_id = !empty($http_article_id);
    if (empty($http_article_title) && empty($http_article_id)) {
        return array('result' => false, '');
    }
    $db = new YwDatabase($db_connect_info);
    if (!$db->connect()) {
        return array('result' => false, '서버와의 연결에 실패했습니다');
    }
    if ($read_by_id) {
        $query = "SELECT * FROM " . ARTICLE_TABLE . " WHERE `id`='{$http_article_id}' LIMIT 1;";
    } else {
        $query = "SELECT * FROM " . ARTICLE_TABLE . " WHERE `title`='{$http_article_title}' LIMIT 1;";
    }
    if (!$db->query($query)) {
        return array('result' => false, '글을 읽어오던 중 서버 에러가 발생했습니다');
    }
    if ($db->total_results() < 1) {
        if (!$read_by_id) {
            navigateTo(HREF_SUGGEST . '?t=' . $http_article_title);
        }
        return array('result' => false, '존재하지 않는 지식입니다');
    }
    $article = $db->get_result();
    if (!$db->query("SELECT * FROM " . REVISION_TABLE . " WHERE `article_id`=" . $article['id'] . " ORDER BY `timestamp` DESC LIMIT " . $http_revisions_page * MAX_REVISIONS . "," . MAX_REVISIONS . ";")) {
        return array('result' => false, 'page' => $http_revisions_page, '지식의 역사를 불러오는데 실패했습니다');
    }
    $article['revisions'] = array();
    while ($result = $db->get_result()) {
        array_push($article['revisions'], $result);
    }
    return array('result' => true, 'page' => $http_revisions_page, 'article' => $article, '');
}
Пример #12
0
function main()
{
    global $session;
    global $db_connect_info;
    $http_keyword = trim($_GET['keyword']);
    if (empty($http_keyword)) {
        return array('result' => false);
    }
    $db = new YwDatabase($db_connect_info);
    if (!$db->connect()) {
        return array('result' => false);
    }
    if (!$db->query("SELECT `title` FROM " . ARTICLE_TABLE . " WHERE `title` LIKE '%" . $db->purify($http_keyword) . "%' ORDER BY `hits` DESC LIMIT 10;")) {
        return array('result' => false);
    }
    if ($db->total_results() < 1) {
        return array('result' => false);
    }
    $result_array = array();
    while ($result = $db->get_result()) {
        array_push($result_array, $result["title"]);
    }
    return array('result' => json_encode($result_array));
}
Пример #13
0
function main()
{
    global $session;
    global $db_connect_info;
    $http_revision_id = trim($_GET['i']);
    $http_revision_target_id = trim($_GET['j']);
    $http_pure = !empty($_GET['pure']);
    $http_rollback = !empty($_GET['rollback']);
    $orginal_target = intval($http_revision_target_id) == 0;
    if (empty($http_revision_id) || !isset($http_revision_target_id)) {
        return array('result' => false, 'message' => '');
    }
    $db = new YwDatabase($db_connect_info);
    // 데이터베이스 연결을 체크합니다.
    if (!$db->connect()) {
        return array('result' => false, 'message' => '서버와의 연결에 실패했습니다');
    }
    if (!$db->query("SELECT * FROM " . REVISION_TABLE . " WHERE `id`='" . $http_revision_id . "' LIMIT 1;")) {
        return array('result' => false, 'message' => '글을 읽어오던 중 서버 에러가 발생했습니다');
    }
    if ($db->total_results() < 1) {
        return array('result' => false, 'message' => '존재하지 않는 지식입니다');
    }
    $original = $db->get_result();
    if (!$db->query("SELECT * FROM " . ARTICLE_TABLE . " WHERE `id`='" . $original['article_id'] . "' LIMIT 1;")) {
        return array('result' => false, 'message' => '글을 읽어오던 중 서버 에러가 발생했습니다');
    }
    if ($http_rollback) {
        $article = $db->get_result();
        if (!$session->started()) {
            return array('result' => false, 'message' => '로그인한 사용자만 되돌릴 수 있습니다.');
        }
        if ($session->permission < intval($article['permission'])) {
            return array('result' => false, 'message' => '되돌리기 위한 권한이 부족합니다');
        }
        if (!$db->query("UPDATE " . ARTICLE_TABLE . " SET " . "`content`='" . $original['snapshot_content'] . "', " . "`title`='" . $original['article_title'] . "', " . "`tags`='" . $original['snapshot_tags'] . "' " . "WHERE `id`='" . $original['article_id'] . "';")) {
            return array('result' => false, 'message' => '되돌리기에 실패했습니다');
        }
        if (!$db->query("SELECT `revision` FROM " . REVISION_TABLE . " WHERE `article_id`='" . $original['article_id'] . "' ORDER BY `timestamp` DESC LIMIT 1;")) {
            return array('result' => false, 'message' => '되돌리기에 실패했습니다');
        }
        if ($db->total_results() < 1) {
            $article_recent_revision_number = 0;
        } else {
            $result = $db->get_result();
            $article_recent_revision_number = intval($result['revision']);
        }
        if (!$db->query("INSERT INTO " . REVISION_TABLE . " (`article_id`, `article_title`, `revision`, `user_name`, `snapshot_content`, `snapshot_tags`, `fluctuation`, `comment`) " . "VALUES (" . "'" . $article['id'] . "', " . "'" . $article['title'] . "', " . "'" . ($article_recent_revision_number + 1) . "', " . "'" . $session->name . "', " . "'" . $article['content'] . "', " . "'" . $article['tags'] . "', " . (strlen($original['snapshot_content']) - strlen($article['content'])) . ", " . "'" . $original['revision'] . "에서 복구함');")) {
            return array('result' => false, 'message' => '되돌리기에 실패했습니다');
        }
        // 되돌리기 성공
        navigateTo(HREF_READ . '/' . $article['id']);
        return array('result' => true);
    }
    // j값이 0일 경우 원본을 비교 대상으로 지정한다.
    if ($orginal_target) {
        $query = "SELECT * FROM " . ARTICLE_TABLE . " WHERE `id`='" . $original['article_id'] . "' LIMIT 1;";
    } else {
        $query = "SELECT * FROM " . REVISION_TABLE . " WHERE `id`='" . $http_revision_target_id . "' LIMIT 1;";
    }
    if (!$db->query($query)) {
        return array('result' => false, 'message' => '글을 읽어오던 중 서버 에러가 발생했습니다');
    }
    if ($db->total_results() < 1) {
        return array('result' => false, 'message' => '존재하지 않는 지식입니다');
    }
    if ($orginal_target) {
        $result = $db->get_result();
        $revision['id'] = 0;
        $revision['article_title'] = $result['title'];
        $revision['revision'] = '현재';
        $revision['snapshot_content'] = $result['content'];
        $revision['snapshot_tags'] = $result['tags'];
        $revision['timestamp'] = $result['timestamp'];
    } else {
        $revision = $db->get_result();
    }
    $db->close();
    return array('result' => true, 'original' => $original, 'revision' => $revision, 'original_json' => json_encode($original), 'revision_json' => json_encode($revision));
}
Пример #14
0
function main()
{
    global $session;
    global $db_connect_info;
    $http_user_name = trim(!empty($_GET['name']) ? $_GET['name'] : $_POST['user-name']);
    $http_user_info = strip_tags($_POST['user-info']);
    $http_user_commit_page = intval(!empty($_GET['p']) ? $_GET['p'] : '0');
    if (empty($http_user_name)) {
        return array('result' => false, 'message' => '존재하지 않는 유저입니다');
    }
    if (strlen($http_user_name) < 3) {
        return array('result' => false, 'message' => '존재하지 않는 유저입니다');
    }
    $db = new YwDatabase($db_connect_info);
    // 데이터베이스 연결을 체크합니다.
    if (!$db->connect()) {
        return array('result' => false, 'message' => '서버와의 연결에 실패했습니다');
    }
    // 아이디가 유효한지 확인합니다.
    if (!$db->query("SELECT * FROM " . USER_TABLE . " WHERE `name`='" . $db->purify($http_user_name) . "';")) {
        return array('result' => false, 'message' => '유저 정보를 불러오는데 실패했습니다');
    }
    if ($db->total_results() < 1) {
        return array('result' => false, 'message' => '존재하지 않는 아이디입니다');
    }
    $user = $db->get_result();
    if (!$db->query("SELECT * FROM " . REVISION_TABLE . " WHERE `user_name`='" . $db->purify($http_user_name) . "' ORDER BY `timestamp` DESC LIMIT " . $http_user_commit_page * MAX_REVISIONS . "," . MAX_REVISIONS . ";")) {
        return array('result' => false, 'user' => $user, 'message' => '유저 기여 정보를 불러오는데 실패했습니다');
    }
    $user['contributions'] = array();
    while ($result = $db->get_result()) {
        array_push($user['contributions'], $result);
    }
    // 만약 자기소개 업데이트를 요청했다면,
    if (!empty($http_user_info) && $session->id == $user['id'] && strcmp($user['info'], $http_user_info) != 0) {
        if (!$db->query("UPDATE " . USER_TABLE . " SET `info`='" . $db->purify($http_user_info) . "' WHERE `name`='" . $db->purify($http_user_name) . "';")) {
            return array('result' => false, 'user' => $user, 'page' => $http_user_commit_page, 'message' => '유저 정보를 업데이트하는데 실패했습니다');
        }
        $user['info'] = $http_user_info;
        $db->log($http_user_name, LOG_UPDATE_USER_INFO, $http_user_info);
    }
    return array('result' => true, 'user' => $user, 'page' => $http_user_commit_page, 'message' => '');
}
Пример #15
0
function main()
{
    global $session;
    global $db_connect_info;
    global $page_focus;
    $http_user_email = trim($_POST['user-email']);
    $http_user_password = $_POST['user-password'];
    $http_user_new_password = $_POST['user-new-password'];
    $http_user_new_password_re = $_POST['user-new-password-re'];
    $http_student_id = trim($_POST['student-id']);
    $http_student_password = $_POST['student-password'];
    $http_user_password_drop = $_POST['user-drop-password'];
    // 0: 계정 정보, 1: 재학생 인증, 2: 이메일 변경, 4: 비번 변경, 4: 계정 삭제
    $page_focus = 0;
    if (!$session->started()) {
        navigateTo(HREF_MAIN);
    }
    $db = new YwDatabase($db_connect_info);
    if (!$db->connect()) {
        return array('result' => false, 'message' => '서버와의 연결에 실패했습니다');
    }
    // 유저 정보 불러오기
    if (!$db->query("SELECT * FROM " . USER_TABLE . " WHERE `id`=" . $session->id . ";")) {
        return array('result' => false, 'message' => '유저 정보를 불러오는데 실패했습니다');
    }
    $user = $db->get_result();
    $user['login_history'] = array();
    // 최근 3일간 로그인 기록 가져오기
    if (!$db->query("SELECT * FROM " . LOG_TABLE . " WHERE `user_name`='" . $user['name'] . "' AND `behavior`='signin' AND `timestamp` >= (CURDATE() - INTERVAL 3 DAY) " . "ORDER BY `timestamp` DESC LIMIT 30;")) {
        return array('result' => false, 'user' => $user, 'message' => '최근 로그인 기록을 로드하는데 실패했습니다');
    }
    while ($result = $db->get_result()) {
        array_push($user['login_history'], $result);
    }
    if (!empty($http_student_id)) {
        $page_focus = 1;
        // 중복 학번 검사
        if (!$db->query("SELECT 1 FROM " . USER_TABLE . " WHERE `code`='" . $db->purify($http_student_id) . "';")) {
            return array('result' => false, 'user' => $user, 'message' => '학번을 조회하지 못했습니다');
        }
        if ($db->total_results() > 0) {
            return array('result' => false, 'user' => $user, 'message' => '이미 인증에 사용된 연세포탈 계정입니다');
        }
        // 포탈 로그인 인증
        if (!getYonseiAuth($http_student_id, $http_student_password)) {
            return array('result' => false, 'message' => '학번이나 비밀번호가 올바르지 않습니다');
        }
        if (!$db->query("UPDATE " . USER_TABLE . " SET `code`='" . $http_student_id . "'" . (intval($user['permission']) < 1 ? ", `permission`=1" : "") . " WHERE `id`=" . $user['id'] . ";")) {
            return array('result' => false, 'message' => '서버 오류로 인증을 완료하지 못했습니다');
        }
        if ($user_permission < 1) {
            $session->setPermission(1);
        }
        $db->log($session->name, LOG_STUDENT_AUTH, $http_student_id);
        navigateTo(HREF_DASHBOARD . '?auth=1');
        return array('result' => true, 'user' => $user, 'message' => '재학생 인증을 완료했습니다');
    }
    // 이메일 변경
    if (!empty($http_user_email)) {
        $page_focus = 2;
        if (!filter_var($http_user_email, FILTER_VALIDATE_EMAIL)) {
            return array('result' => false, 'user' => $user, 'message' => '이메일 주소가 올바르지 않습니다');
        }
        if (strcmp($user['email'], $http_user_email) == 0) {
            return array('result' => false, 'user' => $user, 'message' => '동일한 이메일 주소가 입력되었습니다');
        }
        if (!$db->query("SELECT 1 FROM " . USER_TABLE . " WHERE `email`='" . $db->purify($http_user_email) . "';")) {
            return array('result' => false, 'user' => $user, 'message' => '이메일 주소 조회에 실패했습니다');
        }
        if ($db->total_results() > 0) {
            return array('result' => false, 'user' => $user, 'message' => '이미 사용중인 이메일 주소입니다');
        }
        if (!$db->query("UPDATE " . USER_TABLE . " SET `email`='" . $db->purify($http_user_email) . "' WHERE `id`=" . $user['id'] . ";")) {
            return array('result' => false, 'user' => $user, 'message' => '이메일 주소 변경에 실패하였습니다');
        }
        $db->log($session->name, LOG_CHANGE_EMAIL, $user['email']);
        $user['email'] = $http_user_email;
        return array('result' => true, 'user' => $user, 'message' => '이메일 주소를 변경하였습니다');
    }
    // 비밀번호 변경
    if (!empty($http_user_new_password)) {
        $page_focus = 3;
        if (strcmp($http_user_new_password, $http_user_new_password_re) != 0) {
            return array('result' => false, 'user' => $user, 'message' => '비밀번호와 비밀번호 확인이 일치하지 않습니다');
        }
        if (strlen($http_user_new_password) < 4) {
            return array('result' => false, 'user' => $user, 'message' => '비밀번호는 4자 이상으로 입력해 주세요');
        }
        $http_user_password = passwordHash($http_user_password);
        $http_user_new_password = passwordHash($http_user_new_password);
        if (strcmp($user['password'], $http_user_password) != 0) {
            return array('result' => false, 'user' => $user, 'message' => '현재 비밀번호가 올바르지 않습니다');
        }
        if (!$db->query("UPDATE " . USER_TABLE . " SET `password`='" . $http_user_new_password . "' WHERE `id`=" . $user['id'] . ";")) {
            return array('result' => false, 'user' => $user, 'message' => '서버 오류로 비밀번호를 변경하지 못했습니다');
        }
        $db->log($session->name, LOG_CHANGE_PASSWORD, $user['password']);
        return array('result' => true, 'user' => $user, 'message' => '비밀번호를 변경하였습니다.');
    }
    // 계정 삭제
    if (!empty($http_user_password_drop)) {
        $page_focus = 4;
        if (strcmp($user['password'], passwordHash($http_user_password_drop)) != 0) {
            return array('result' => false, 'user' => $user, 'message' => '비밀번호가 올바르지 않습니다');
        }
        if (!$db->query("DELETE FROM " . USER_TABLE . " WHERE `id`=" . $user['id'] . ";")) {
            return array('result' => false, 'user' => $user, 'message' => '서버 오류로 계정을 삭제하지 못했습니다');
        }
        $db->log($session->name, LOG_DELETE_ACCOUNT, '');
        navigateTo(HREF_SIGNOUT);
        return array('result' => true, 'user' => $user, 'message' => '');
    }
    return array('result' => true, 'user' => $user);
}
Пример #16
0
function main()
{
    global $session;
    global $db_connect_info;
    global $http_user_name;
    global $http_user_email;
    if ($session->started()) {
        navigateTo(HREF_MAIN);
    }
    $http_user_name = trim($_POST['user-name']);
    $http_user_password = trim($_POST['user-password']);
    $http_user_password_re = trim($_POST['user-password-re']);
    $http_user_email = trim($_POST['user-email']);
    // 입력 값의 유효성을 검증한다.
    if (empty($http_user_name) || empty($http_user_password) || empty($http_user_email)) {
        return array('result' => true, 'message' => '');
    }
    if (strlen($http_user_name) < 2) {
        return array('result' => false, 'message' => '아이디는 3자 이상으로 입력해 주세요');
    }
    if (strlen($http_user_password) < 5) {
        return array('result' => false, 'message' => '비밀번호는 4자 이상으로 입력해 주세요');
    }
    if (strcmp($http_user_password, $http_user_password_re) != 0) {
        return array('result' => false, 'message' => '비밀번호와 비밀번호 확인이 일치하지 않습니다');
    }
    // 이메일 포멧의 유효성을 검증한다.
    if (!filter_var($http_user_email, FILTER_VALIDATE_EMAIL)) {
        return array('result' => false, 'message' => '올바르지 않은 이메일 주소입니다');
    }
    // reCAPTCHA를 검증한다.
    if (!getReCaptcha()) {
        return array('result' => false, 'message' => 'reCAPTCHA가 올바르게 입력되지 않았습니다');
    }
    $db = new YwDatabase($db_connect_info);
    // 데이터베이스 연결을 체크한다.
    if (!$db->connect()) {
        return array('result' => false, 'message' => '서버와의 연결에 실패했습니다');
    }
    // 아이디와 이메일 유효성을 검증한다.
    if (!$db->query("SELECT `name` FROM " . USER_TABLE . " WHERE `name`='" . $db->purify($http_user_name) . "' OR `email`='" . $db->purify($http_user_email) . "';")) {
        return array('result' => false, 'message' => '유저 정보를 불러오는데 실패하였습니다');
    }
    if ($db->total_results() > 0) {
        $result = $db->get_result();
        if (strcmp($http_user_name, $result['name']) == 0) {
            return array('result' => false, 'message' => '이미 사용중인 아이디입니다');
        } else {
            return array('result' => false, 'message' => '이미 사용중인 이메일 주소입니다');
        }
    }
    // 서버로 데이터를 전송한다.
    if (!$db->query("INSERT INTO " . USER_TABLE . " (`name`, `password`, `email`) VALUES ('" . $db->purify($http_user_name) . "', '" . passwordHash($http_user_password) . "', '" . $db->purify($http_user_email) . "');")) {
        return array('result' => false, 'message' => '계정을 생성하는데 실패했습니다');
    }
    $db->log($http_user_name, LOG_SIGNUP, '1');
    $db->close();
    navigateTo(HREF_SIGNIN . '?signup=1');
    return array('result' => true, 'message' => '');
}
Пример #17
0
function main()
{
    global $session;
    global $db_connect_info;
    $http_article_title = trim(!empty($_POST['article-title']) ? $_POST['article-title'] : $_GET['t']);
    $http_article_id = trim(!empty($_POST['article-id']) ? $_POST['article-id'] : $_GET['i']);
    $http_article_new_title = strip_tags(trim($_POST['article-new-title']));
    $http_article_content = $_POST['article-content'];
    $http_article_tags = preg_replace('!\\s+!', ' ', strip_tags($_POST['article-tags']));
    $http_article_delete = isset($_POST['article-delete']);
    $http_article_change_permission = isset($_POST['article-permission']);
    $http_article_permission = abs(intval($_POST['article-permission']));
    $http_article_comment = strip_tags($_POST['article-comment']);
    $read_by_id = !empty($http_article_id);
    // 파라미터가 충분하지 않음
    if (empty($http_article_title) && empty($http_article_id)) {
        navigateTo(HREF_MAIN);
    }
    // 로그인 되어있지 않을 경우 로그인 유도
    if (!$session->started()) {
        navigateTo(HREF_SIGNIN . '?redirect=' . HREF_WRITE . '/' . ($read_by_id ? $http_article_id : $http_article_title));
    }
    $db = new YwDatabase($db_connect_info);
    if (!$db->connect()) {
        return array('result' => false, 'message' => '서버와의 연결에 실패했습니다');
    }
    if ($read_by_id) {
        $query = "SELECT * FROM " . ARTICLE_TABLE . " WHERE `id`='{$http_article_id}' LIMIT 1;";
    } else {
        $query = "SELECT * FROM " . ARTICLE_TABLE . " WHERE `title`='{$http_article_title}' LIMIT 1;";
    }
    if (!$db->query($query)) {
        return array('result' => false, 'message' => '글을 읽어오던 중 서버 에러가 발생했습니다');
    }
    if ($db->total_results() < 1) {
        if (!$read_by_id) {
            navigateTo(HREF_SUGGEST . '?t=' . $http_article_title);
        }
        return array('result' => false, 'message' => '존재하지 않는 지식입니다');
    }
    $article = $db->get_result();
    $article_old = $article;
    // 편집 권한 검사
    if (intval($article['permission']) > $session->permission) {
        return array('result' => false, 'article' => $article, 'message' => '이 지식을 편집하기 위한 권한이 부족합니다');
    }
    // 별다른 편집 문자열이 들어오지 않았으면 편집 모드로 들어간다.
    if (empty($http_article_content)) {
        return array('result' => true, 'article' => $article, 'message' => '');
    }
    // 게시글 삭제 명령
    if ($http_article_delete) {
        if (!$db->query("DELETE FROM " . ARTICLE_TABLE . " WHERE `id`='" . $article['id'] . "';")) {
            return array('result' => false, 'message' => '게시글 삭제에 실패했습니다');
        }
        if (DELETE_REVISIONS) {
            $db->query("DELETE FROM " . REVISION_TABLE . " WHERE `article_id`='" . $article['id'] . "';");
        }
        navigateTo(HREF_READ . '/' . $article['title']);
    }
    // 글 내용 필터링
    if ($session->permission < PERMISSION_NO_FILTERING) {
        $http_article_content = getHtmlPurifier($http_article_content);
    }
    $article['content'] = $http_article_content;
    $article['tags'] = $http_article_tags;
    $query = "UPDATE " . ARTICLE_TABLE . " SET ";
    // 타이틀 유효성 검사
    if (!empty($http_article_new_title) && strcmp($http_article_new_title, $article['title']) != 0) {
        if (strlen(preg_replace('/\\s+/', '', $http_article_new_title)) < 2) {
            return array('result' => false, 'article' => $article, 'message' => '제목은 최소 두 글자 이상이어야 합니다');
        }
        if (is_numeric($http_article_new_title)) {
            return array('result' => false, 'article' => $article, 'message' => '지식 제목으로 숫자를 사용할 수 없습니다');
        }
        if (!$db->query("SELECT 1 FROM " . ARTICLE_TABLE . " WHERE `title`='" . $db->purify($http_article_new_title) . "';")) {
            return array('result' => false, 'article' => $article, 'message' => '지식 제목을 검증하는데 서버 오류가 발생했습니다');
        }
        if ($db->total_results() > 0) {
            return array('result' => false, 'article' => $article, 'message' => '이미 존재하는 지식 제목입니다');
        }
        $article['title'] = $http_article_new_title;
        $query .= "`title`='" . $db->purify($http_article_new_title) . "', ";
    }
    // 퍼미션 유효성 검사
    if ($http_article_change_permission) {
        if ($http_article_permission > $session->permission) {
            return array('result' => false, 'article' => $article, 'message' => '자신의 권한보다 지식 수정 권한을 크게 설정할 수 없습니다');
        }
        $article['permission'] = $http_article_permission;
        $query .= "`permission`='" . $http_article_permission . "', ";
    }
    // 태그. 중간 공백을 하나로 설정한다.
    $query .= "`tags`='" . $db->purify($http_article_tags) . "', ";
    $query .= "`content`='" . $db->purify($http_article_content) . "' ";
    $query .= "WHERE `id`='" . $article['id'] . "';";
    if (!$db->query($query)) {
        return array('result' => false, 'article' => $article, 'message' => '지식 업데이트 중 서버 오류가 발생했습니다');
    }
    // 가장 최근의 revision 레코드 넘버를 가져온다.
    if (!$db->query("SELECT `revision` FROM " . REVISION_TABLE . " WHERE `article_id`='" . $article['id'] . "' ORDER BY `timestamp` DESC LIMIT 1;")) {
        return array('result' => false, 'article' => $article, 'message' => '수정 기록을 불러오던 중 서버 오류가 발생했습니다');
    }
    $result = $db->get_result();
    if ($db->total_results() < 1) {
        $article_recent_revision_number = 0;
    } else {
        $article_recent_revision_number = intval($result['revision']);
    }
    if (!$db->query("INSERT INTO " . REVISION_TABLE . " (`article_id`, `article_title`, `revision`," . " `user_name`, `snapshot_content`, `snapshot_tags`, `fluctuation`, `comment`) VALUES ('" . $article_old['id'] . "', '" . $db->purify($article_old['title']) . "', " . ($article_recent_revision_number + 1) . ", '" . $session->name . "', '" . $db->purify($article_old['content']) . "', '" . $db->purify($article_old['tags']) . "', " . (strlen($http_article_content) - strlen($article_old['content'])) . ", '" . $db->purify($http_article_comment) . "');")) {
        return array('result' => false, 'message' => "수정 기록을 추가하던 중 서버 오류가 발생했습니다");
    }
    $db->log($session->name, LOG_WRITE, $article['id'] . '/' . ($article_recent_revision_number + 1));
    $db->close();
    navigateTo(HREF_READ . '/' . $article['title'] . '?update=1');
    return array('result' => true, 'article' => $article, 'message' => '');
}