示例#1
0
 public static function initialize()
 {
     $run = new Run();
     if (\BWBlog\Utils::isAjax()) {
         $handler = new JsonResponseHandler();
     } else {
         $handler = new PrettyPageHandler();
     }
     $run->pushHandler($handler);
     $run->register();
 }
示例#2
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'];
     }
 }
示例#3
0
$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]);
                }
            }
        }
    }
} catch (ReflectionException $e) {
    try {
        Utils::invoke($rest_part, 'post', 'view', $rest_part);
    } catch (ReflectionException $e) {
        Utils::invoke($rest_part, 'error', 'notfound');
    }
}