Ejemplo n.º 1
0
 public static function edit($title, $md, $url, $time = null, $category = null, $tags = [], $state = self::STATE_OPEN, $id = null)
 {
     if ($id !== null && mb_strlen((string) $id) !== 24) {
         throw new \Exception('Invalid argument: id');
     }
     if ($time == null) {
         $time = time();
     }
     $html = \Breezewish\Marked\Marked::render($md, ['langPrefix' => 'prettyprint lang-', 'breaks' => true]);
     if (P_HTML_FILTER) {
         $html = \BWBlog\Escaper::purify($html);
     }
     // page doesn't need short introductions
     if ($state != self::STATE_PAGE) {
         // process introduction
         foreach (['<!-- more -->', '<!--more -->', '<!-- more-->', '<!--more-->'] as $meta) {
             $pos = mb_stripos($html, $meta, 0, 'UTF-8');
             if ($pos !== false) {
                 break;
             }
         }
         if ($pos !== false) {
             $intro = \BWBlog\Utils::closeTags(mb_substr($html, 0, $pos));
             $more = true;
         } else {
             $intro = $html;
             $more = false;
         }
     } else {
         $more = false;
         $intro = '';
     }
     // page uses direct url
     if ($state != self::STATE_PAGE) {
         $rest_url = \BWBlog\Utils::trimRestURI(str_replace('\\', '/', date('Y/m/d/', $time) . $url));
     } else {
         $rest_url = \BWBlog\Utils::trimRestURI(str_replace('\\', '/', 'page/' . $url));
     }
     $doc = ['title' => $title, 'content' => ['markdown' => $md, 'html' => $html, 'introhtml' => $intro, 'more' => $more], 'time' => ['main' => $time], 'url' => $url, 'rest_url' => $rest_url, 'category' => $category, 'lcategory' => strtolower($category), 'tags' => $tags, 'ltags' => array_map('strtolower', array_map('trim', $tags)), 'state' => (int) $state];
     global $db;
     if ($id === null) {
         // create
         $doc['time']['create'] = time();
         $db->selectCollection(MONGO_PREFIX . 'posts')->insert($doc);
         return $doc['_id'];
     } else {
         // update
         $doc['time']['edit'] = time();
         $result = $db->selectCollection(MONGO_PREFIX . 'posts')->findAndModify(['_id' => new \MongoId((string) $id)], ['$set' => $doc], ['_id' => 1], ['upsert' => true, 'new' => true]);
         return $result['_id'];
     }
 }
Ejemplo n.º 2
0
<?php

include __DIR__ . '/includes/init.php';
use BWBlog\Utils;
$url = parse_url($_SERVER['REQUEST_URI']);
$url = $url['path'];
// REST
$rest_part = mb_substr($url, mb_strlen(dirname($_SERVER['SCRIPT_NAME'])));
$rest_part = \BWBlog\Utils::trimRestURI($rest_part);
$rest_arr = explode('/', $rest_part);
$rest_arr = array_map(function ($value) {
    if (mb_strlen($value) == 0) {
        return 'index';
    } else {
        return urldecode($value);
    }
}, $rest_arr);
$pc = count($rest_arr);
try {
    if ($pc === 0) {
        Utils::invoke($rest_part, 'index', 'index');
    } else {
        if ($pc === 1) {
            Utils::invoke($rest_part, $rest_arr[0], 'index');
        } else {
            if ($pc === 2) {
                Utils::invoke($rest_part, $rest_arr[0], $rest_arr[1]);
            } else {
                if ($pc >= 3) {
                    Utils::invoke($rest_part, $rest_arr[0], $rest_arr[1], $rest_arr[2]);
                }