/** * Read feedback message and answers and work with add answer model * @param int $id * @param string $hash * @return string * @throws \Ffcms\Core\Exception\NativeException * @throws ForbiddenException * @throws \Ffcms\Core\Exception\SyntaxException */ public function actionRead($id, $hash) { if (!Obj::isLikeInt($id) || Str::length($hash) < 16 || Str::length($hash) > 64) { throw new ForbiddenException(__('The feedback request is not founded')); } // get feedback post record from database $recordPost = FeedbackPost::where('id', '=', $id)->where('hash', '=', $hash)->first(); if ($recordPost === null) { throw new ForbiddenException(__('The feedback request is not founded')); } $userId = App::$User->isAuth() ? App::$User->identity()->getId() : 0; $model = null; // check if feedback post is not closed for answers if ((int) $recordPost->closed === 0) { // init new answer add model $model = new FormAnswerAdd($recordPost, $userId); // if answer is sender lets try to make it model if ($model->send() && $model->validate()) { $model->make(); App::$Session->getFlashBag()->add('success', __('Your answer was added')); $model->clearProperties(); } } // render output view return $this->view->render('read', ['model' => $model, 'post' => $recordPost, 'answers' => $recordPost->getAnswers()->get()]); }
/** * EntityContentSearch constructor. Pass search terms (query string) to model and used items to skip it by id. * @param $terms * @param int|array $skipIds */ public function __construct($terms, $skipIds = 0) { $this->_terms = App::$Security->strip_tags(trim($terms, ' ')); if (Obj::isLikeInt($skipIds)) { $this->_skip = [$skipIds]; } elseif (Obj::isArray($skipIds)) { $this->_skip = $skipIds; } parent::__construct(); }
/** * Try to convert string to date time format * @param string|int $rawDate * @param string $format * @return string|bool */ public static function convertToDatetime($rawDate, $format = 'd.m.Y') { if (Obj::isLikeInt($rawDate)) { // convert timestamp to date format $rawDate = date($format, $rawDate); } try { $object = new \DateTime($rawDate); return $object->format($format); } catch (\Exception $e) { return false; } }
/** * Set default configurations if not defined * {@inheritDoc} * @see \Ffcms\Core\Arch\Widget::init() */ public function init() { $cfg = $this->getConfigs(); // check cache is defined if ($this->cache === null || !Obj::isLikeInt($this->cache)) { $this->cache = (int) $cfg['cache']; } // check tag count is defined if ($this->count === null || !Obj::isLikeInt($this->count)) { $this->count = (int) $cfg['count']; } $this->_lang = App::$Request->getLanguage(); $this->_cacheName = 'widget.contenttag.' . $this->createStringClassSnapshotHash(); }
/** * Get current application configs as array * @return array */ public function getConfigs() { if ($this->configs !== null) { return $this->configs; } $configs = (array) $this->application->configs; foreach ($configs as $cfg => $value) { if (Obj::isLikeInt($value)) { $configs[$cfg] = (int) $value; // convert string 1 "1" to int 1 1 } } $this->configs = $configs; return $this->configs; }
/** * Get user nickname by user id with predefined value on empty or not exist profile * @param $userId * @param string $onEmpty * @return string */ public static function parseUserNick($userId = null, $onEmpty = 'guest') { // try to get user id as integer if (Obj::isLikeInt($userId)) { $userId = (int) $userId; } else { // user id is empty, lets return default value return \App::$Security->strip_tags($onEmpty); } // try to find user active record as object $identity = App::$User->identity($userId); if ($identity === null || $identity === false) { return \App::$Security->strip_tags($onEmpty); } // return user nickname from profile return $identity->getProfile()->getNickname(); }
/** * Prepare widget. Set default configs if not defined on initialization * {@inheritDoc} * @see \Ffcms\Core\Arch\Widget::init() */ public function init() { $cfg = $this->getConfigs(); // check if categories is empty if ($this->categories === null) { $this->categories = $cfg['categories']; } // check cache is defined if ($this->cache === null || !Obj::isLikeInt($this->cache)) { $this->cache = (int) $cfg['cache']; } // check item count is defined if ($this->count === null || !Obj::isLikeInt($this->count)) { $this->count = (int) $cfg['count']; } $this->_cacheName = 'widget.newcontent.' . $this->createStringClassSnapshotHash(); }
/** * Get user profile via user_id like object (!!! profile.id !== user.id !!!) * @param int|null $user_id * @return self|null */ public static function identity($user_id = null) { if ($user_id === null) { $user_id = MainApp::$Session->get('ff_user_id'); } if ($user_id === null || !Obj::isLikeInt($user_id) || $user_id < 1) { return null; } // check in cache if (MainApp::$Memory->get('profile.object.cache.' . $user_id) !== null) { return MainApp::$Memory->get('profile.object.cache.' . $user_id); } // find row $profile = self::where('user_id', '=', $user_id); // empty? lets return null if (false === $profile || null === $profile || $profile->count() !== 1) { return null; } $object = $profile->first(); MainApp::$Memory->set('profile.object.cache.' . $user_id, $object); return $object; }
}); if (type == 'now') { $('#messages-now').html(msgBody); $(".message-scroll-body").animate({ scrollTop: $(document).height() }, "slow"); } else if(type == 'before') { $('#messages-before').prepend(msgBody); } else if (type == 'after') { $('#messages-now').append(msgBody); $(".message-scroll-body").animate({ scrollTop: $(document).height() }, "slow"); } }); }; <?php // check if defined ?newdialog=userid $dialogId = \App::$Request->query->get('newdialog', false); if (false !== $dialogId && Obj::isLikeInt($dialogId) && $dialogId > 0) { ?> new_dialog = <?php echo $dialogId; ?> ; active_dialog_id = new_dialog; <?php } ?> // load dialogs when page ready $.fn.loadDialogUsers(); // set scheduled loader window.setInterval($.fn.loadDialogUsers, 15 * 1000); // callback for user onclick -> show dialogs $(document).on('click', '.media-person', function() {
/** * Validate path filter * @return bool */ public function validatePath() { // try to find this item $find = Content::where('path', '=', $this->path); // exclude self id if ($this->_content->id !== null && Obj::isLikeInt($this->_content->id)) { $find->where('id', '!=', $this->_content->id); } // limit only current category id $find->where('category_id', '=', $this->categoryId); return $find->count() < 1; }
/** * Delete category action * @param int $id * @return string * @throws ForbiddenException * @throws \Ffcms\Core\Exception\SyntaxException * @throws \Ffcms\Core\Exception\NativeException */ public function actionCategorydelete($id) { // check id if (!Obj::isLikeInt($id) || $id < 2) { throw new ForbiddenException(); } // get object relation $record = ContentCategory::find($id); if ($record === null || $record === false) { throw new ForbiddenException(); } // init model with object relation $model = new FormCategoryDelete($record); // check if delete is submited if ($model->send() && $model->validate()) { $model->make(); App::$Session->getFlashBag()->add('success', __('Category is successful removed')); $this->response->redirect('content/categories'); } // draw view return $this->view->render('category_delete', ['model' => $model]); }
<!-- Main menu --> <?php echo Navbar::display(['nav' => ['class' => 'navbar-default'], 'property' => ['id' => 'headmenu', 'class' => 'navbar-nav'], 'brand' => ['link' => '/', 'text' => __('Home')], 'collapseId' => 'collapse-mainmenu', 'activeOrder' => 'action', 'items' => [['link' => ['content/list', 'news'], 'text' => __('News'), 'position' => 'left'], ['link' => ['content/read', 'page', 'about-page'], 'text' => __('About'), 'position' => 'left'], ['link' => ['feedback/create'], 'text' => __('Feedback'), 'position' => 'left'], ['link' => ['profile/index/all'], 'text' => __('Users'), 'position' => 'right']]]); ?> <div class="row"> <div class="col-md-9 content-container"> <?php if ($this->breadcrumbs !== null && Obj::isArray($this->breadcrumbs)) { ?> <ol class="breadcrumb"> <?php foreach ($this->breadcrumbs as $bUrl => $bText) { ?> <?php if (Obj::isLikeInt($bUrl)) { // only text ?> <li class="active"><?php echo \App::$Security->strip_tags($bText); ?> </li> <?php } else { ?> <li> <a href="<?php echo \App::$Security->strip_tags($bUrl); ?> "><?php echo \App::$Security->strip_tags($bText);
/** * Filter ['object', 'int'] * @param $object * @return bool */ public static function int($object) { return Obj::isLikeInt($object); }
/** * List answers by comment id as json object * @param int $commentId * @return string * @throws ForbiddenException * @throws NotFoundException */ public function actionShowanswers($commentId) { $this->setJsonHeader(); // check input data if (!Obj::isLikeInt($commentId) || (int) $commentId < 1) { throw new ForbiddenException('Input data is incorrect'); } // get configs $configs = AppRecord::getConfigs('widget', 'Comments'); // get data from db by comment id $records = CommentAnswer::where('comment_id', '=', $commentId)->where('moderate', '=', 0); if ((int) $configs['onlyLocale'] === 1) { $records = $records->where('lang', '=', $this->request->getLanguage()); } // check objects count if ($records->count() < 1) { throw new NotFoundException(__('No answers for comment is founded')); } // prepare output $response = []; foreach ($records->get() as $row) { $commentAnswer = new EntityCommentData($row); $response[] = $commentAnswer->make(); } return json_encode(['status' => 1, 'data' => $response]); }
/** * Recovery form and recovery submit action * @param int|null $id * @param string|null $token * @return string * @throws \Ffcms\Core\Exception\NativeException * @throws ForbiddenException * @throws NotFoundException * @throws \Ffcms\Core\Exception\SyntaxException */ public function actionRecovery($id = null, $token = null) { if (App::$User->isAuth()) { // always auth? prevent any actions throw new ForbiddenException(); } // is recovery submit? if (Obj::isLikeInt($id) && Str::length($token) >= 64) { $rObject = UserRecovery::where('id', '=', $id)->where('token', '=', $token)->where('archive', '=', false); // check if recovery row exist if ($rObject->count() !== 1) { throw new NotFoundException('This recovery data is not found'); } $rData = $rObject->first(); // check if user with this "user_id" in recovery row exist $rUser = App::$User->identity($rData->user_id); if ($rUser === null) { throw new NotFoundException('User is not found'); } // all is ok, lets set new pwd $rUser->password = $rData->password; $rUser->save(); $rData->archive = true; $rData->save(); // add notification App::$Session->getFlashBag()->add('success', __('Your account are successful recovered. We recommend you change password')); // lets open user session with recovered data $loginModel = new FormLogin(); $loginModel->openSession($rUser); $this->response->redirect('/'); // session is opened, refresh page } // lets work with recovery form data $model = new FormRecovery(true); if ($model->send()) { if ($model->validate()) { $model->make(); App::$Session->getFlashBag()->add('success', __('We send to you email with instruction to recovery your account')); } else { App::$Session->getFlashBag()->add('error', __('Form validation is failed')); } } // render visual form content return $this->view->render('recovery', ['model' => $model]); }
/** * Change user rating action * @throws ForbiddenException * @throws NativeException * @throws NotFoundException * @return string */ public function actionChangerating() { if (!App::$User->isAuth()) { throw new ForbiddenException('Auth required'); } $this->setJsonHeader(); // get operation type and target user id $target_id = (int) $this->request->get('target'); $type = $this->request->get('type'); // check type of query if ($type !== '+' && $type !== '-') { throw new NativeException('Wrong data'); } // check if passed user id is exist if (!Obj::isLikeInt($target_id) || $target_id < 1 || !App::$User->isExist($target_id)) { throw new NotFoundException('Wrong user info'); } $cfg = \Apps\ActiveRecord\App::getConfigs('app', 'Profile'); // check if rating is enabled for website if ((int) $cfg['rating'] !== 1) { throw new NativeException('Rating is disabled'); } // get target and sender objects $target = App::$User->identity($target_id); $sender = App::$User->identity(); // disable self-based changes ;) if ($target->getId() === $sender->getId()) { throw new ForbiddenException('Self change prevented'); } // check delay $diff = Date::convertToTimestamp(time() - $cfg['ratingDelay'], Date::FORMAT_SQL_TIMESTAMP); $query = ProfileRating::where('target_id', '=', $target->getId())->where('sender_id', '=', $sender->getId())->where('created_at', '>=', $diff)->orderBy('id', 'DESC'); if ($query !== null && $query->count() > 0) { throw new ForbiddenException('Delay required'); } // delay is ok, lets insert a row $record = new ProfileRating(); $record->target_id = $target->getId(); $record->sender_id = $sender->getId(); $record->type = $type; $record->save(); // update target profile $profile = $target->getProfile(); if ($type === '+') { $profile->rating += 1; } else { $profile->rating -= 1; } $profile->save(); return json_encode(['status' => 1, 'data' => 'ok']); }
/** * Check if new login is always exist * @param string $login * @param int|null $userId * @return bool */ public static function isUniqueLogin($login, $userId = null) { $find = User::where('login', '=', $login); if ($userId !== null && Obj::isLikeInt($userId)) { $find->where('id', '!=', $userId); } return $find->count() === 0; }
/** * Delete custom field action * @param int $id * @return string * @throws \Ffcms\Core\Exception\SyntaxException * @throws \Ffcms\Core\Exception\NativeException * @throws ForbiddenException */ public function actionFielddelete($id) { if (!Obj::isLikeInt($id) || $id < 1) { throw new ForbiddenException(); } // check if record with $id is exist $record = ProfileField::find($id); if ($record === null || $record === false) { throw new ForbiddenException(); } $model = new FormFieldUpdate($record); // if delete is submited - lets remove this record if ($model->send()) { $model->delete(); $this->response->redirect('profile/fieldlist'); } return $this->view->render('field_delete', ['model' => $model]); }
/** * Check if user with $id exist * @param int $id * @return bool */ public static function isExist($id) { if (!Obj::isLikeInt($id) || $id < 1) { return false; } // convert id to real integer $id = (int) $id; $find = MainApp::$Memory->get('user.counter.cache.' . $id); if ($find === null) { $find = self::where('id', '=', $id)->count(); MainApp::$Memory->set('user.counter.cache.' . $id, $find); } return $find === 1; }
/** * Remove items from gallery (preview+full) * @param int $id * @param string $file * @throws ForbiddenException * @throws NativeException * @return string */ public function actionGallerydelete($id, $file = null) { if ($file === null || Str::likeEmpty($file)) { $file = (string) $this->request->query->get('file', null); } // check passed data if (Str::likeEmpty($file) || !Obj::isLikeInt($id)) { throw new NativeException('Wrong input data'); } // check passed file extension $fileExt = Str::lastIn($file, '.', true); $fileName = Str::firstIn($file, '.'); if (!Arr::in($fileExt, $this->allowedExt)) { throw new ForbiddenException('Wrong file extension'); } // generate path $thumb = '/upload/gallery/' . $id . '/thumb/' . $fileName . '.jpg'; $full = '/upload/gallery/' . $id . '/orig/' . $file; // check if file exists and remove if (File::exist($thumb) || File::exist($full)) { File::remove($thumb); File::remove($full); } else { throw new NativeException('Image is not founded'); } return json_encode(['status' => 1, 'msg' => 'Image is removed']); }
/** * Unblock always blocked user * @param string $target_id * @return string * @throws \Ffcms\Core\Exception\SyntaxException * @throws \Ffcms\Core\Exception\NativeException * @throws ForbiddenException * @throws NotFoundException */ public function actionUnblock($target_id) { // check if user is auth if (!App::$User->isAuth()) { throw new ForbiddenException(); } // check if target is defined if (!Obj::isLikeInt($target_id) || $target_id < 1 || !App::$User->isExist($target_id)) { throw new NotFoundException(); } $user = App::$User->identity(); // check if target user in blacklist of current user if (!Blacklist::have($user->getId(), $target_id)) { throw new NotFoundException(); } $model = new FormIgnoreDelete($user, $target_id); if ($model->send() && $model->validate()) { $model->make(); $this->response->redirect(Url::to('profile/ignore')); } return $this->view->render('unblock', ['model' => $model]); }