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 input data to database */ public function make() { // save data to db $this->_record->title = $this->title; $this->_record->text = $this->text; $this->_record->path = $this->path; $this->_record->category_id = (int) $this->categoryId; $this->_record->display = 0; // set to premoderation $this->_record->author_id = (int) $this->authorId; if ($this->_new === true) { $this->_record->comment_hash = $this->generateCommentHash(); } $this->_record->save(); // work with poster data if ($this->poster !== null) { // lets move poster from tmp to gallery $originDir = '/upload/gallery/' . $this->_record->id . '/orig/'; $thumbDir = '/upload/gallery/' . $this->_record->id . '/thumb/'; if (!Directory::exist($originDir)) { Directory::create($originDir); } if (!Directory::exist($thumbDir)) { Directory::create($thumbDir); } $fileName = App::$Security->simpleHash($this->poster->getClientOriginalName() . $this->poster->getSize()); $newFullName = $fileName . '.' . $this->poster->guessExtension(); // move poster to upload gallery directory $this->poster->move(Normalize::diskFullPath($originDir), $newFullName); // initialize image resizer $thumb = new Image(); $thumb->setCacheDir(root . '/Private/Cache/images'); // open original file, resize it and save $thumbSaveName = Normalize::diskFullPath($thumbDir) . '/' . $fileName . '.jpg'; $thumb->open(Normalize::diskFullPath($originDir) . DIRECTORY_SEPARATOR . $newFullName)->cropResize($this->_configs['galleryResize'])->save($thumbSaveName, 'jpg', 90); $thumb = null; // update poster in database $this->_record->poster = $newFullName; $this->_record->save(); } }
/** * Download file from $url and save it into $path * @param string $url * @param string $path * @return bool */ public static function saveFromUrl($url, $path) { if (!filter_var($url, FILTER_VALIDATE_URL) || !function_exists('curl_init')) { return false; } $path = Normalize::diskFullPath($path); // check if upload directory is exists $dir = dirname($path); if (!Directory::exist($dir)) { Directory::create($dir); } // initialize stream resource $stream = @fopen($path, 'w'); // initialize curl & set required options, target url, destination save stream $curl = \curl_init(); \curl_setopt($curl, CURLOPT_URL, $url); if (Str::startsWith('https', $url)) { \curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); \curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); } \curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); \curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5); \curl_setopt($curl, CURLOPT_HEADER, 0); \curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322'); \curl_setopt($curl, CURLOPT_FAILONERROR, true); \curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); \curl_setopt($curl, CURLOPT_AUTOREFERER, true); \curl_setopt($curl, CURLOPT_TIMEOUT, 10); // set destination file path \curl_setopt($curl, CURLOPT_FILE, $stream); \curl_exec($curl); \curl_close($curl); fclose($stream); return true; }
/** * 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'; }
/** * Upload new files to content item gallery * @param int $id * @return string * @throws ForbiddenException * @throws NativeException * @throws \Exception */ public function actionGalleryupload($id) { // check if id is passed if (Str::likeEmpty($id)) { throw new NativeException('Wrong input data'); } // check if user have permission to access there if (!App::$User->isAuth() || !App::$User->identity()->getRole()->can('global/file')) { throw new NativeException(__('Permissions to upload is denied')); } // check if directory exist if (!Directory::exist('/upload/gallery/' . $id)) { Directory::create('/upload/gallery/' . $id); } // get file object /** @var $file \Symfony\Component\HttpFoundation\File\UploadedFile */ $file = $this->request->files->get('file'); if ($file === null || $file->getError() !== 0) { throw new NativeException(__('Unexpected error in upload process')); } // check file size if ($file->getSize() < 1 || $file->getSize() > $this->maxSize) { throw new ForbiddenException(__('File size is too big. Max size: %size%kb', ['size' => (int) ($this->maxSize / 1024)])); } // check file extension if (!Arr::in($file->guessExtension(), $this->allowedExt)) { throw new ForbiddenException(__('File extension is not allowed to upload. Allowed: %s%', ['s' => implode(', ', $this->allowedExt)])); } // create origin directory $originPath = '/upload/gallery/' . $id . '/orig/'; if (!Directory::exist($originPath)) { Directory::create($originPath); } // lets make a new file name $fileName = App::$Security->simpleHash($file->getClientOriginalName() . $file->getSize()); $fileNewName = $fileName . '.' . $file->guessExtension(); // check if image is already loaded if (File::exist($originPath . $fileNewName)) { throw new ForbiddenException(__('File is always exists!')); } // save file from tmp to gallery origin directory $file->move(Normalize::diskFullPath($originPath), $fileNewName); // lets resize preview image for it $thumbPath = '/upload/gallery/' . $id . '/thumb/'; if (!Directory::exist($thumbPath)) { Directory::create($thumbPath); } $thumb = new Image(); $thumb->setCacheDir(root . '/Private/Cache/images'); // open original file, resize it and save $thumbSaveName = Normalize::diskFullPath($thumbPath) . '/' . $fileName . '.jpg'; $thumb->open(Normalize::diskFullPath($originPath) . DIRECTORY_SEPARATOR . $fileNewName)->cropResize($this->maxResize)->save($thumbSaveName, 'jpg', 90); $thumb = null; $this->setJsonHeader(); return json_encode(['status' => 1, 'file' => ['thumbnailUrl' => '/upload/gallery/' . $id . '/thumb/' . $fileName . '.jpg', 'url' => '/upload/gallery/' . $id . '/orig/' . $fileNewName, 'name' => $fileNewName]]); }