Exemple #1
0
 public function beforeSave($insert)
 {
     if (!parent::beforeSave($insert)) {
         return false;
     }
     if ($insert) {
         $this->create_time = time();
         $this->ip = XUtils::getClientIP();
         $this->user_agent = Yii::$app->request->userAgent;
     }
     $this->ip = htmlspecialchars($this->ip);
     $this->user_agent = htmlspecialchars($this->user_agent);
     $this->type = htmlspecialchars($this->type);
     $this->action = htmlspecialchars($this->action);
     $this->result = htmlspecialchars($this->result);
     $this->detail = htmlspecialchars($this->detail);
     return true;
 }
 public function actionIndex()
 {
     $server['serverSoft'] = $_SERVER['SERVER_SOFTWARE'];
     $server['serverOs'] = PHP_OS;
     $server['phpVersion'] = PHP_VERSION;
     $server['fileupload'] = ini_get('file_uploads') ? ini_get('upload_max_filesize') : '禁止上传';
     $server['serverUri'] = $_SERVER['SERVER_NAME'];
     $server['maxExcuteTime'] = ini_get('max_execution_time') . ' 秒';
     $server['maxExcuteMemory'] = ini_get('memory_limit');
     $server['excuteUseMemory'] = function_exists('memory_get_usage') ? XUtils::dataFormat(memory_get_usage()) : '未知';
     $dbsize = 0;
     $connection = Yii::$app->db;
     $sql = "SHOW TABLE STATUS LIKE '{$connection->tablePrefix}%'";
     $command = $connection->createCommand($sql)->queryAll();
     foreach ($command as $table) {
         $dbsize += $table['Data_length'] + $table['Index_length'];
     }
     $mysqlVersion = $connection->createCommand("SELECT version() AS version")->queryAll();
     $server['mysqlVersion'] = $mysqlVersion[0]['version'];
     $server['dbsize'] = $dbsize ? XUtils::dataFormat($dbsize) : '未知';
     return $this->render('index', ['server' => $server]);
 }
                <em class="glyphicon glyphicon-new-window"></em><span>阅读全文</span>
            </a></div>
    </div>
    <footer class="entry-footer row">
		<span><i class="glyphicon glyphicon-user"></i>
		<a href="<?php 
echo Url::toRoute(['user/alias', 'name' => $post->author_name]);
?>
"><?php 
echo $post->author_name;
?>
</a>
		</span>
		<span><i class="glyphicon glyphicon-time"></i>
            <?php 
echo XUtils::XDateFormatter($post->post_time);
?>
		</span>
		<span>
			<i class="glyphicon glyphicon-eye-open"></i>
            <?php 
echo $post->view_count;
?>
 浏览
		</span>
		<span>
			<a href="<?php 
echo $post->getUrl();
?>
#comments">
                <span class="badge"><?php 
 /**
  * 获取最近的 {$limit} 条评论
  * @param int $limit
  * @param bool|false $refresh
  * @param int $size
  * @return array|mixed|null
  */
 public static function getRecentComments($limit = 10, $refresh = false, $size = 40)
 {
     $cache_key = '__recentComments';
     $limit = intval($limit) ? intval($limit) : 10;
     if ($refresh) {
         $comments = null;
     } else {
         $comments = Yii::$app->cache->get($cache_key);
     }
     if (empty($comments)) {
         /* @var self[] $post_comments */
         $post_comments = self::find()->with('post')->limit($limit)->all();
         $ids = $avatars = $urls = array();
         foreach ($post_comments as $comment) {
             $ids[$comment->pid] = $comment->pid;
             if (!isset($avatars[$comment->id])) {
                 $avatars[$comment->id] = XUtils::getAvatar($comment->email, $size);
             }
         }
         foreach ($post_comments as $comment) {
             $comments[] = ['id' => $comment->id, 'author' => $comment->author, 'author_url' => $comment->url, 'pid' => $comment->pid, 'post_url' => $comment->post->getUrl(true), 'content' => $comment->content, 'create_time' => $comment->create_time, 'email' => $comment->email, 'avatar' => $avatars[$comment->id], 'title' => $comment->post ? $comment->post->title : ''];
         }
         $dp = new DbDependency();
         $dp->sql = (new Query())->select('MAX(update_time)')->from(self::tableName())->createCommand()->rawSql;
         Yii::$app->cache->set($cache_key, $comments, 3600, $dp);
     }
     return $comments;
 }
 /**
  * 保存前校验补充数据
  * @param bool $insert
  * @return bool
  * @throws \yii\base\Exception
  * @throws \yii\base\InvalidConfigException
  */
 public function beforeSave($insert)
 {
     if (!parent::beforeSave($insert)) {
         return false;
     }
     if ($this->isNewRecord) {
         $this->register_time = time();
         $this->register_ip = XUtils::getClientIP();
         $this->auth_key = Yii::$app->security->generateRandomKey();
     }
     //注册黑名单
     if (in_array($this->username, $this->nameBlackList) || in_array($this->nickname, $this->nameBlackList)) {
         $this->{$this}->addError('username', '该用户名不能被注册!');
     }
     if (in_array($this->scenario, array(self::SCENARIO_REGISTER, self::SCENARIO_MODIFY_PWD))) {
         $this->password = $this->hashPassword($this->password);
     }
     $this->info = HtmlPurifier::process($this->info, ['HTML.ForbiddenElements' => ['a']]);
     //管理员更改用户数据不修改活动时间
     if ($this->scenario !== self::SCENARIO_MANAGE) {
         $this->active_time = time();
     }
     $this->update_time = time();
     return true;
 }
 /**
  * 自动填写create_time、post_time、update_time
  * 新 Post 自动填写作者ID和作者的昵称
  * 自动填写最终修改人(name和id)
  * @see CActiveRecord::beforeSave()
  * @param bool $insert
  * @return bool
  */
 public function beforeSave($insert)
 {
     if (!parent::beforeSave($insert)) {
         return false;
     }
     $this->comment_count = intval($this->comment_count);
     //创建新Post
     if ($insert) {
         $this->create_time = $this->update_time = time();
         $this->ext_info = null;
     }
     //编辑状态记录信息
     if (in_array($this->scenario, [self::SCENARIO_EDIT, self::SCENARIO_MANAGE])) {
         $this->ext_info = is_array($this->ext_info) ? serialize($this->ext_info) : null;
         $this->update_time = time();
     }
     //修改保存状态,处理发布时间
     if ($this->status != self::STATUS_PUBLISHED && $this->status != self::STATUS_HIDDEN) {
         $this->post_time = null;
     } else {
         if (empty($this->post_time)) {
             $this->post_time = time();
         } elseif (!is_numeric($this->post_time)) {
             $this->post_time = strtotime($this->post_time);
         }
     }
     if ($this->status != self::STATUS_HIDDEN) {
         $this->password = null;
     }
     //title安全修正,并生成文章URL别名
     $this->title = htmlspecialchars($this->title);
     if (!$this->alias) {
         $this->alias = $this->title;
     }
     $this->alias = str_replace([' ', '%'], ['-'], trim($this->alias));
     $this->alias = strip_tags($this->alias);
     $this->alias = htmlspecialchars($this->alias);
     //alias唯一性校验
     if (self::find()->where(['alias' => $this->alias])->exists()) {
         $this->addError('alias', '访问别名不能重复!');
         return false;
     }
     //生成并处理excerpt
     if (!$this->excerpt) {
         if ($this->status == self::STATUS_PUBLISHED) {
             $this->excerpt = strip_tags($this->excerpt, '<p><ul><li><strong>');
             $this->excerpt = XUtils::strimwidthWithTag($this->content, 0, 350, '...');
         }
     }
     $this->excerpt = str_replace(['<p></p>', '<p><br /></p>'], '', $this->excerpt);
     //处理封面图片
     if ($this->cover) {
         //TODO 验证cover
     } else {
         $this->cover = $this->getCoverImage();
     }
     //处理并生成tags
     $tags_array = array_unique(explode(',', str_replace([' ', ','], ',', $this->tags)));
     foreach ($tags_array as $key => $tag) {
         if (preg_match('/^[0-9a-zA-Z_\\x{4e00}-\\x{9fa5}]+$/u', $tag)) {
             $tags_array[$key] = $tag;
         } else {
             unset($tags_array[$key]);
         }
     }
     $this->tags = implode(',', $tags_array);
     return true;
 }
use app\components\XUtils;
use app\models\User;
use app\modules\admin\widgets\Menu;
/**
 * @var View $this
 * @var User $current_user
 */
$current_url = Url::current();
$current_user = Yii::$app->user->identity;
?>
<aside class="main-sidebar">
    <section class="sidebar">
        <div class="user-panel">
            <div class="pull-left image">
                <img src="<?php 
echo XUtils::getAvatar($current_user->email);
?>
" class="img-circle" alt="<?php 
echo $current_user->nickname;
?>
">
            </div>
            <div class="pull-left info">
                <p><?php 
echo $current_user->nickname;
?>
</p>
                <a href="#"><i class="fa fa-circle text-success"></i> Online</a>
            </div>
        </div>
        <?php