예제 #1
0
파일: server.php 프로젝트: secondsano/unico
function response($request)
{
    global $smtp_config;
    parse_str($request, $request);
    $response = new stdClass();
    $action = getSafe($request, 'action', 'page');
    $news_file = 'server_data/news.json';
    $news = file_get_contents($news_file);
    $news = json_decode($news);
    $images = file_get_contents('server_data/images.json');
    $images = json_decode($images);
    $tariffs = file_get_contents('server_data/tariffs.json');
    $tariffs = json_decode($tariffs);
    if (!$news || !$images) {
        $response->type = 'error';
        $response->message = 'Server files errors!';
        return_json($response);
    }
    $NPP = 6;
    // news per page
    $NN = count($news->news);
    // news number
    switch ($action) {
        case 'tariffs':
            $ttype = getSafe($request, 'ttype', false);
            $response->type = 'success';
            $response->data = new stdClass();
            if (!$ttype) {
                $response->data->tariffs = $tariffs->tariffs;
            } else {
                $response->data->tariffs = $tariffs->tariffs->{$ttype};
            }
            break;
        case 'captcha':
            $request = $_POST;
            $reset = getSafe($request, 'reset', false);
            $captcha = getSafe($request, 'captcha');
            session_start();
            if ($reset) {
                session_destroy();
                $response->type = 'success';
            } else {
                if ($_SESSION['code'] == $captcha) {
                    $response->type = 'success';
                } else {
                    $response->type = 'error';
                }
                $response->code = $_SESSION['code'];
            }
            break;
        case 'mail':
            $request = $_POST;
            $subject = 'Сообщение с сайта УНИКО';
            $message = getSafe($request, 'message');
            $headers = 'From: unico@vgg.ru' . "\r\n" . "Content-Type: text; charset=utf-8";
            $mail = new PHPMailer();
            $mail->IsSMTP();
            $mail->Host = $smtp_config['host'];
            $mail->SMTPAuth = true;
            $mail->Port = $smtp_config['port'];
            $mail->Username = $smtp_config['username'];
            $mail->Password = $smtp_config['password'];
            $mail->CharSet = 'UTF-8';
            $mail->SetFrom(FROM_MAIL, 'Унико');
            $mail->Subject = $subject;
            $mail->isHtml(false);
            $mail->Body = $message;
            $addresses = explode(' ', UNICO_MAIL);
            foreach ($addresses as $address) {
                $mail->AddAddress($address);
            }
            if ($mail->Send()) {
                $response->type = 'success';
            } else {
                $response->type = 'error';
            }
            break;
        case 'igroups':
            $response->type = 'success';
            $response->data = new stdClass();
            $response->data->groups = $images->groups;
            break;
        case 'images':
            $group = getSafe($request, 'g');
            if ($group) {
                foreach ($images->groups as $i) {
                    if ($i->id == $group) {
                        $group = $i;
                        break;
                    }
                }
            }
            $response->type = 'success';
            $response->data = new stdClass();
            $images_output = array();
            foreach ($images->images as $i) {
                if (is_object($group)) {
                    if (!in_array($i->id, $group->images)) {
                        continue;
                    }
                }
                $i->image = GALLERY_PATH . '/' . $i->image;
                $images_output[] = $i;
            }
            $response->data->images = $images_output;
            break;
        case 'groups':
            $response->type = 'success';
            $response->data = new stdClass();
            $response->data->groups = $news->groups;
            break;
        case 'hot':
            $response->type = 'success';
            $response->data = new stdClass();
            $response->data->hots = $news->hots;
            break;
        case 'item':
            $current_item = $request['n'];
            $response->type = 'success';
            $response->data = new stdClass();
            foreach ($news->news as $item) {
                if ($item->id == $current_item) {
                    $current_item = $item;
                    break;
                }
            }
            if (empty($current_item)) {
                $current_item = $news->news[0];
            }
            $current_item->text = implode(' ', $current_item->text);
            $current_item->url = 'action=page';
            $response->data->news = array($current_item);
            break;
        case 'index':
            $news_output = array();
            foreach ($news->index as $id) {
                foreach ($news->news as $n) {
                    if ($n->id == $id) {
                        $n->text = implode(' ', $n->text);
                        if (strlen($n->text) > 300) {
                            $n->text = mb_substr($n->text, 0, 300, 'utf8') . "...";
                        }
                        $n->url = "news.php?action=item&n={$id}";
                        $news_output[] = $n;
                        break;
                    }
                }
            }
            $response->type = 'success';
            $response->data = new stdClass();
            $response->data->news = $news_output;
            break;
        case 'news_all':
            $response->data = $news;
            $response->file = $news_file;
            break;
        case 'page':
            $current_page = getSafe($request, 'n', 1);
            $search = getSafe($request, 's');
            $group = getSafe($request, 'g');
            if ($group) {
                foreach ($news->groups as $n) {
                    if ($n->id == $group) {
                        $group = $n;
                        break;
                    }
                }
            }
            $response->type = 'success';
            $response->data = new stdClass();
            $news_output = array();
            foreach ($news->news as $n) {
                $n->text = implode(' ', $n->text);
                if ($search) {
                    if (strpos($n->title, $search) === false && strpos($n->text, $search) === false) {
                        continue;
                    }
                } else {
                    if (is_object($group)) {
                        if (!in_array($n->id, $group->news)) {
                            continue;
                        }
                    }
                }
                if (strlen($n->text) > 300) {
                    $n->text = mb_substr($n->text, 0, 300, 'utf8') . "...";
                }
                $n->url = 'action=item&n=' . $n->id;
                $news_output[] = $n;
            }
            $response->data->current_page = $current_page;
            $response->data->pages = ceil(count($news_output) / $NPP);
            usort($news_output, "cmp");
            $news_output = array_slice($news_output, ($current_page - 1) * $NPP, $NPP);
            $response->data->news = $news_output;
            break;
        default:
            $response->type = 'error';
            $response->message = 'There is no such action';
            break;
    }
    return_json($response);
    return $response;
}
예제 #2
0
파일: index.php 프로젝트: Gitsyw/kldns
        if ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            $sid = getSid();
            $sql = "UPDATE `kldns_users` SET `sid`=:sid,lasttime=logintime,lastip=loginip,logintime=NOW(),loginip=:ip WHERE (`uid`=:uid) limit 1";
            $stmt = $db->prepare($sql);
            $stmt->execute(array(':uid' => $row['uid'], ':sid' => $sid, ':ip' => getIp()));
            setCookie('kldns_sid', $sid, time() + 3600 * 24 * 7, '/');
            exit("<script language='javascript'>alert('登录成功!进入控制面板!');window.location.href='/control.php';</script>");
        } else {
            $loginMsg = '登录失败,用户名或密码不正确';
        }
    }
} elseif ($action == 'reg') {
    $user = getSafe(getRequest('user', 'post'));
    $pwd = getSafe(getRequest('pwd', 'post'));
    $email = getSafe(getRequest('email', 'post'));
    $code = getSafe(getRequest('code', 'post'));
    $sql = 'SELECT uid FROM `kldns_users` WHERE `user`=:user limit 1';
    $checkUser = $db->prepare($sql);
    $checkUser->execute(array(':user' => $user));
    $sql = 'SELECT uid FROM `kldns_users` WHERE `email`=:email limit 1';
    $checkEmail = $db->prepare($sql);
    $checkEmail->execute(array(':email' => $email));
    if (strlen($user) < 3 || strlen($pwd) < 6) {
        $regMsg = '用户名或密码格式不正确';
    } elseif (!preg_match('/^[a-zA-Z0-9\\-\\_]+@[a-zA-Z0-9\\-]+\\.[a-zA-Z]+$/', $email)) {
        $regMsg = '邮箱格式不正确';
    } elseif (strlen($code) != 4 || !isset($_COOKIE['verification']) || md5(strtolower($code)) !== $_COOKIE['verification']) {
        $regMsg = '验证码错误';
    } elseif ($checkUser->fetch()) {
        $regMsg = '此用户名已存在';
    } elseif ($checkEmail->fetch()) {