/** * Build xml output and save it into sitemap folder * @param string $uniqueName * @return bool * @throws \Ffcms\Core\Exception\NativeException * @throws \Ffcms\Core\Exception\SyntaxException */ public function save($uniqueName) { // check if data exists if ($this->data === null || !Obj::isArray($this->data)) { return false; } // list data each every language, render xml output and write into file foreach ($this->data as $lang => $items) { $xml = App::$View->render('native/sitemap_urlset', ['items' => $items]); File::write(EntityIndexList::INDEX_PATH . '/' . $uniqueName . '.' . $lang . '.xml', $xml); } return true; }
/** * Write configurations data from array to cfg file * @param string $configFile * @param array $data * @return bool */ public function writeConfig($configFile, array $data) { $path = '/Private/Config/' . ucfirst(Str::lowerCase($configFile)) . '.php'; if (!File::exist($path) || !File::writable($path)) { return false; } $saveData = '<?php return ' . Arr::exportVar($data) . ';'; File::write($path, $saveData); return true; }
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; }
/** * Save model properties as configurations * @return bool */ public function makeSave() { $toSave = App::$Security->strip_php_tags($this->getAllProperties()); $stringSave = '<?php return ' . Arr::exportVar($toSave, null, true) . ';'; $cfgPath = '/Private/Config/Default.php'; if (File::exist($cfgPath) && File::writable($cfgPath)) { File::write($cfgPath, $stringSave); return true; } return false; }
/** * Prepare scan list on first run. Scan directory's and save as JSON */ private function prepareScanlist() { $files = (object) File::listFiles(root, $this->affectedExt); File::write('/Private/Antivirus/ScanFiles.json', json_encode($files)); }
/** * Try to download and parse remote avatar * @param string $url * @param int $userId */ protected function parseAvatar($url, $userId) { // check if user is defined if ((int) $userId < 1) { return; } // check remote image extension $imageExtension = Str::lastIn($url, '.', true); if (!Arr::in($imageExtension, ['png', 'gif', 'jpg', 'jpeg'])) { return; } // try to get image binary data $imageContent = Url::download($url); if ($imageContent === null || Str::likeEmpty($imageContent)) { return; } // write image to filesystem $imagePath = '/upload/user/avatar/original/' . $userId . '.' . $imageExtension; $write = File::write($imagePath, $imageContent); if ($write === false) { return; } // try to write and resize file try { $fileObject = new FileObject(root . $imagePath); $avatarUpload = new FormAvatarUpload(); $avatarUpload->resizeAndSave($fileObject, $userId, 'small'); $avatarUpload->resizeAndSave($fileObject, $userId, 'medium'); $avatarUpload->resizeAndSave($fileObject, $userId, 'big'); } catch (\Exception $e) { if (App::$Debug) { App::$Debug->addException($e); } } }
/** * Console installation * @return string * @throws NativeException */ public function actionInstall() { if (File::exist('/Private/Install/install.lock')) { throw new NativeException('Installation is locked! Please delete /Private/Install/install.lock'); } echo Console::$Output->writeHeader('License start'); echo File::read('/LICENSE') . PHP_EOL; echo Console::$Output->writeHeader('License end'); $config = Console::$Properties->get('database'); $newConfig = []; // creating default directory's foreach (self::$installDirs as $obj) { // looks like a directory if (!Str::contains('.', $obj)) { Directory::create($obj, 0777); } } echo Console::$Output->write('Upload and private directories are successful created!'); // set chmods echo $this->actionChmod(); // database config from input echo Console::$Output->writeHeader('Database connection configuration'); echo 'Driver(default:' . $config['driver'] . '):'; $dbDriver = Console::$Input->read(); if (Arr::in($dbDriver, ['mysql', 'pgsql', 'sqlite'])) { $newConfig['driver'] = $dbDriver; } // for sqlite its would be a path echo 'Host(default:' . $config['host'] . '):'; $dbHost = Console::$Input->read(); if (!Str::likeEmpty($dbHost)) { $newConfig['host'] = $dbHost; } echo 'Database name(default:' . $config['database'] . '):'; $dbName = Console::$Input->read(); if (!Str::likeEmpty($dbName)) { $newConfig['database'] = $dbName; } echo 'User(default:' . $config['username'] . '):'; $dbUser = Console::$Input->read(); if (!Str::likeEmpty($dbUser)) { $newConfig['username'] = $dbUser; } echo 'Password(default:' . $config['password'] . '):'; $dbPwd = Console::$Input->read(); if (!Str::likeEmpty($dbPwd)) { $newConfig['password'] = $dbPwd; } echo 'Table prefix(default:' . $config['prefix'] . '):'; $dbPrefix = Console::$Input->read(); if (!Str::likeEmpty($dbPrefix)) { $newConfig['prefix'] = $dbPrefix; } // merge configs and add new connection to db pull $dbConfigs = Arr::merge($config, $newConfig); Console::$Database->addConnection($dbConfigs, 'install'); try { Console::$Database->connection('install')->getDatabaseName(); } catch (\Exception $e) { return 'Testing database connection is failed! Run installer again and pass tested connection data! Log: ' . $e->getMessage(); } // autoload isn't work here include root . '/Apps/Controller/Console/Db.php'; // import db data $dbController = new DbController(); echo $dbController->actionImportAll('install'); // add system info about current install version $system = new System(); $system->setConnection('install'); $system->var = 'version'; $system->data = Version::VERSION; $system->save(); // set website send from email from input $emailConfig = Console::$Properties->get('adminEmail'); echo 'Website sendFrom email(default: ' . $emailConfig . '):'; $email = Console::$Input->read(); if (!Str::isEmail($email)) { $email = $emailConfig; } // set base domain echo 'Website base domain name(ex. ffcms.org):'; $baseDomain = Console::$Input->read(); if (Str::likeEmpty($baseDomain)) { $baseDomain = Console::$Properties->get('baseDomain'); } // generate other configuration data and security salt, key's and other echo Console::$Output->writeHeader('Writing configurations'); /** @var array $allCfg */ $allCfg = Console::$Properties->getAll('default'); $allCfg['database'] = $dbConfigs; $allCfg['adminEmail'] = $email; $allCfg['baseDomain'] = $baseDomain; echo Console::$Output->write('Generate password salt for BLOWFISH crypt'); $allCfg['passwordSalt'] = '$2a$07$' . Str::randomLatinNumeric(mt_rand(21, 30)) . '$'; echo Console::$Output->write('Generate security cookies for debug panel'); $allCfg['debug']['cookie']['key'] = 'fdebug_' . Str::randomLatinNumeric(mt_rand(8, 32)); $allCfg['debug']['cookie']['value'] = Str::randomLatinNumeric(mt_rand(32, 128)); // write config data $writeCfg = Console::$Properties->writeConfig('default', $allCfg); if ($writeCfg !== true) { return 'File /Private/Config/Default.php is unavailable to write data!'; } File::write('/Private/Install/install.lock', 'Install is locked'); return 'Configuration done! FFCMS 3 is successful installed! Visit your website. You can add administrator using command php console.php db/adduser'; }
/** * Save configurations build by installer interface */ public function make() { // prepare configurations to save /** @var array $cfg */ $cfg = App::$Properties->getAll('default'); $this->before(); $cfg['baseDomain'] = $this->baseDomain; $cfg['database'] = $this->db; $cfg['adminEmail'] = $this->email; $cfg['singleLanguage'] = $this->singleLanguage; $cfg['multiLanguage'] = (bool) $this->multiLanguage; $cfg['passwordSalt'] = '$2a$07$' . Str::randomLatinNumeric(mt_rand(21, 30)) . '$'; $cfg['debug']['cookie']['key'] = 'fdebug_' . Str::randomLatinNumeric(mt_rand(4, 16)); $cfg['debug']['cookie']['value'] = Str::randomLatinNumeric(mt_rand(32, 128)); // import database tables $connectName = 'install'; include root . '/Private/Database/install.php'; // insert admin user $user = new User(); $user->setConnection('install'); $user->login = $this->user['login']; $user->email = $this->user['email']; $user->role_id = 4; $user->password = App::$Security->password_hash($this->user['password'], $cfg['passwordSalt']); $user->save(); $profile = new Profile(); $profile->setConnection('install'); $profile->user_id = $user->id; $profile->save(); // set installation version $system = new System(); $system->setConnection('install'); $system->var = 'version'; $system->data = Version::VERSION; $system->save(); // write config data App::$Properties->writeConfig('default', $cfg); // make routing configs based on preset property $routing = []; switch ($this->mainpage) { case 'news': $routing = ['Alias' => ['Front' => ['/' => '/content/list/news', '/about' => '/content/read/page/about-page']]]; break; case 'about': $routing = ['Alias' => ['Front' => ['/' => '/content/read/page/about-page']]]; break; } // write routing configurations App::$Properties->writeConfig('routing', $routing); // write installer lock File::write('/Private/Install/install.lock', 'Installation is locked!'); }