/** * Build variables and display output html */ public function buildOutput() { $this->after(); // if layout is not required and this is just standalone app if ($this->layout === null) { $content = $this->output; } else { $layoutPath = App::$Alias->currentViewPath . '/layout/' . $this->layout . '.php'; if (!File::exist($layoutPath)) { throw new NativeException('Layout not founded: ' . $layoutPath); } $body = $this->output; // pass global data to config viewer if (App::$Debug !== null) { App::$Debug->bar->getCollector('config')->setData(['Global Vars' => Variables::instance()->getGlobalsArray()]); } // cleanup buffer from random shits after exception throw'd ob_clean(); // start buffering to render layout ob_start(); include $layoutPath; $content = ob_get_clean(); // read buffer content & stop buffering // set custom css library's not included on static call $cssIncludeCode = App::$View->showCodeLink('css'); if (!Str::likeEmpty($cssIncludeCode)) { $content = Str::replace('</head>', $cssIncludeCode . '</head>', $content); } // add debug bar if (App::$Debug !== null) { $content = Str::replace(['</body>', '</head>'], [App::$Debug->renderOut() . '</body>', App::$Debug->renderHead() . '</head>'], $content); } } return $content; }
/** * Normalize local disk-based path. Ex: ../../dir/./dir/file.txt * @param string $path * @return string */ public static function diskPath($path) { // its full-based path? Lets return real path if (Str::startsWith(root, $path)) { // fix path collisions if is not exist if (file_exists($path)) { return realpath($path); } else { return $path; } } // else - sounds like relative path $path = Str::replace('\\', '/', $path); $splitPath = explode('/', $path); $outputPath = []; foreach ($splitPath as $index => $part) { if ($part === '.' || Str::length(trim($part)) < 1) { continue; } if ($part === '..') { // level-up (: array_pop($outputPath); continue; } $outputPath[] = trim($part); } return implode(DIRECTORY_SEPARATOR, $outputPath); }
/** * Show scan results * @return string */ public function actionAntivirusresults() { $response = null; if (!File::exist('/Private/Antivirus/Infected.json')) { $response = ['status' => 0]; } else { $data = json_decode(File::read('/Private/Antivirus/Infected.json')); $compile = []; foreach ($data as $file => $sign) { $file = Str::replace('\\', '/', Str::sub($file, strlen(root))); $compile[$file][] = $sign; } $response = ['status' => 1, 'data' => $compile]; } $this->setJsonHeader(); return json_encode($response); }
/** * Browse files from ckeditor * @param string $type * @throws NativeException * @throws \Ffcms\Core\Exception\SyntaxException */ public function actionBrowse($type) { $files = null; $relative = null; // check if request type is defined if ($this->allowedExt[$type] === null || !Obj::isArray($this->allowedExt[$type])) { throw new NativeException('Hack attempt'); } // list files in directory $files = File::listFiles('/upload/' . $type, $this->allowedExt[$type]); // absolute path to relative URI foreach ($files as $file) { $newName = Str::sub($file, Str::length(root) + 1); $relative[] = trim(Str::replace(DIRECTORY_SEPARATOR, '/', $newName), '/'); } // generate response return App::$View->render('editor/browse', ['callbackName' => App::$Security->strip_tags(App::$Request->query->get('CKEditor')), 'callbackId' => (int) App::$Request->query->get('CKEditorFuncNum'), 'files' => $relative, 'type' => $type], __DIR__); }
/** * Display native exception * @param string $message * @return string|null */ public function display($message = null) { // if passed message is null get exception msg if ($message === null) { $message = $this->message; } // hide root path from exception $message = Str::replace(root, '$DOCUMENT_ROOT', $message); $message = strip_tags($message); // generate response based on environment type switch (env_type) { case 'html': return $this->sendHTML($message); case 'json': return $this->sendJSON($message); } return $message; }
/** * Get internalization of current text from i18n * @param string $index * @param string $text * @param array|null $params * @return string */ public function get($index, $text, array $params = null) { if (App::$Request->getLanguage() !== App::$Properties->get('baseLanguage')) { if ($index !== null && !Arr::in($index, $this->indexes)) { $this->cached = Arr::merge($this->cached, $this->load($index)); $this->indexes[] = $index; } if ($this->cached !== null && Obj::isString($text) && isset($this->cached[$text])) { $text = $this->cached[$text]; } } if (Obj::isArray($params) && count($params) > 0) { foreach ($params as $var => $value) { $text = Str::replace('%' . $var . '%', $value, $text); } } return $text; }
/** * Get label value by variable name * @param string $param * @return mixed */ public final function getLabel($param) { $labels = $this->labels(); $response = null; // maybe array-dotted declaration? if (Str::contains('.', $param)) { // not defined for all array-dotted nesting? if (Str::likeEmpty($labels[$param])) { // lets set default array label (before first dot-separation) $response = $labels[Str::firstIn($param, '.')]; } else { $response = $labels[$param]; } } else { $response = $labels[$param]; } return Str::likeEmpty($response) ? Str::replace('.', ' ', Str::splitCamelCase($param)) : $response; }
/** * Display exception template * @throws JsonException * @throws \Exception * @return string */ public function display() { // hide path root and stip tags from exception message $msg = Str::replace(root, '$DOCUMENT_ROOT', $this->getMessage()); $msg = App::$Security->strip_tags($msg); // get message translation $this->text = App::$Translate->get('Default', $this->text, ['e' => $msg]); // if exception is throwed not from html-based environment if (env_type !== 'html') { if (env_type === 'json') { return (new JsonException($msg))->display(); } else { return $this->text; } } // add notification in debug bar if exist if (App::$Debug !== null) { App::$Debug->addException($this); } // return rendered result return $this->buildFakePage(); }
, <?php echo Date::convertToDatetime($answer->created_at, Date::FORMAT_TO_HOUR); ?> <span class="pull-right"> <?php echo Url::link(['feedback/update', 'answer', $answer->id], __('Edit'), ['class' => 'label label-primary']); ?> <?php echo Url::link(['feedback/delete', 'answer', $answer->id], __('Delete'), ['class' => 'label label-danger']); ?> </span> </div> <div class="panel-body"> <?php echo Str::replace("\n", "<br />", $answer->message); ?> </div> </div> <?php } } ?> <?php if ($model !== null) { ?> <h3><?php echo __('Add answer'); ?> </h3>
/** * Try to find exist viewer full path * @param string $path * @param string|null $source * @return null|string * @throws NativeException */ private function findViewer($path, $source = null) { $tmpPath = null; // sounds like a relative path for current view theme if (Str::contains('/', $path)) { // lets try to get full path for current theme $tmpPath = $path; if (!Str::startsWith($this->themePath, $path)) { $tmpPath = Normalize::diskPath($this->themePath . '/' . $path . '.php'); } } else { // sounds like a object-depended view call from controller or etc // get stack trace of callbacks $calledLog = debug_backtrace(); $calledController = null; // lets try to find controller in backtrace foreach ($calledLog as $caller) { if (isset($caller['class']) && Str::startsWith('Apps\\Controller\\', $caller['class'])) { $calledController = (string) $caller['class']; } } // depended controller is not founded? Let finish if ($calledController === null) { throw new NativeException('View render is failed: callback controller not founded! Call with relative path: ' . $path); } // get controller name $controllerName = Str::sub($calledController, Str::length('Apps\\Controller\\' . env_name . '\\')); $controllerName = Str::lowerCase($controllerName); // get full path $tmpPath = $this->themePath . DIRECTORY_SEPARATOR . $controllerName . DIRECTORY_SEPARATOR . $path . '.php'; } // check if builded view full path is exist if (File::exist($tmpPath)) { return $tmpPath; } // hmm, not founded. Lets try to find in caller directory (for widgets, apps packages and other) if ($source !== null) { $tmpPath = Normalize::diskPath($source . DIRECTORY_SEPARATOR . $path . '.php'); if (File::exist($tmpPath)) { // add notify for native views if (App::$Debug !== null) { App::$Debug->addMessage('Render native viewer: ' . Str::replace(root, null, $tmpPath), 'info'); } return $tmpPath; } } if (App::$Debug !== null) { App::$Debug->addMessage('Viewer not founded on rendering: ' . $path, 'warning'); } return null; }
public function createObject($name, $type) { $singleName = false; if (!Str::contains('/', $name)) { if ($type === 'ActiveRecord') { $singleName = true; } else { $this->message = 'Command dosn\'t contains valid name. Example: Front/SomeForm, Admin/SomePkg/SomeInput'; return false; } } $objectDirPath = null; $objectNamespace = null; $objectName = null; $objectTemplate = null; if ($singleName) { $objectDirPath = root . '/Apps/' . $type . '/'; $objectNamespace = 'Apps\\' . $type; $objectName = ucfirst($name); } else { $split = explode('/', $name); $workground = ucfirst(strtolower(array_shift($split))); $objectName = ucfirst(array_pop($split)); $subName = false; if (count($split) > 0) { // some sub-namespace / folder path foreach ($split as $part) { if (Str::length($part) > 0) { $subName[] = ucfirst(strtolower($part)); } } } if ($type === 'Widget') { $objectDirPath = root . '/Widgets/' . $workground; $objectNamespace = 'Widgets\\' . $workground; } else { $objectDirPath = root . '/Apps/' . $type . '/' . $workground; $objectNamespace = 'Apps\\' . $type . '\\' . $workground; } if (false !== $subName) { $objectDirPath .= '/' . implode('/', $subName); $objectNamespace .= '\\' . implode('\\', $subName); } // try to find workground-based controller if (File::exist('/Private/Carcase/' . $workground . '/' . $type . '.tphp')) { $objectTemplate = File::read('/Private/Carcase/' . $workground . '/' . $type . '.tphp'); } } if (!Directory::exist($objectDirPath) && !Directory::create($objectDirPath)) { $this->message = 'Directory could not be created: ' . $objectDirPath; return false; } if ($objectTemplate === null) { $objectTemplate = File::read('/Private/Carcase/' . $type . '.tphp'); if (false === $objectTemplate) { $this->message = 'Php template file is not founded: /Private/Carcase/' . $type . '.tphp'; return false; } } $objectContent = Str::replace(['%namespace%', '%name%'], [$objectNamespace, $objectName], $objectTemplate); $objectFullPath = $objectDirPath . '/' . $objectName . '.php'; if (File::exist($objectFullPath)) { $this->message = $type . ' is always exist: ' . $objectFullPath; return false; } File::write($objectFullPath, $objectContent); $this->message = $type . ' template was created: [' . $objectName . '] in path: ' . Str::replace(root, '', $objectDirPath); return true; }
/** * Display form field. Allowed type: text, password, textarea, checkbox, select, checkboxes, file, captcha, email, hidden * @param $object * @param $type * @param null|array $property * @param null|string $helper * @param null|string $layerFile * @return null|string * @throws NativeException * @throws SyntaxException */ public function field($object, $type, $property = null, $helper = null, $layerFile = null) { if ($this->model === null) { if (App::$Debug !== null) { App::$Debug->addMessage('Form model is not defined for field name: [' . strip_tags($object) . ']'); } return null; } // can be dots separated object $propertyName = $object; if (Str::contains('.', $propertyName)) { $propertyName = strstr($propertyName, '.', true); } // check if model contains current tag name as property if (!property_exists($this->model, $propertyName)) { if (App::$Debug !== null) { App::$Debug->addMessage('Form field ["' . $object . '"] is not defined in model: [' . get_class($this->model) . ']', 'error'); } return null; } // prepare layer template file path if ($layerFile === null) { switch ($type) { case 'checkbox': $layerFile = static::$structLayer['checkbox']; break; case 'radio': $layerFile = static::$structLayer['radio']; break; default: $layerFile = static::$structLayer['base']; break; } } // prepare labels text and label "for" attr $labelFor = $this->name . '-' . $propertyName; $labelText = $this->model->getLabel($object); $itemValue = $this->model->{$propertyName}; // sounds like a dot-separated $object if ($propertyName !== $object) { $nesting = trim(strstr($object, '.'), '.'); $labelFor .= '-' . Str::replace('.', '-', $nesting); $itemValue = Arr::getByPath($nesting, $itemValue); } // initialize form fields constructor and build output dom html value $constructor = new Constructor($this->model, $this->name, $type); $elementDOM = $constructor->makeTag($object, $itemValue, $property); // if item is hidden - return tag without assign of global template if ($type === 'hidden') { return $elementDOM; } // render output viewer return App::$View->render($layerFile, ['name' => $labelFor, 'label' => $labelText, 'item' => $elementDOM, 'help' => self::nohtml($helper)]); }