/** * @param string $to - path where the archive should be extracted to * @return bool */ public function extract($to) { $r = false; $ext = SPFs::getExt($this->_filename); switch ($ext) { case 'zip': $zip = new ZipArchive(); if ($zip->open($this->_filename) === true) { SPException::catchErrors(SPC::WARNING); try { $zip->extractTo($to); $zip->close(); $r = true; } catch (SPException $x) { $t = Sobi::FixPath(Sobi::Cfg('fs.temp') . DS . md5(microtime())); SPFs::mkdir($t, 0777); $dir = SPFactory::Instance('base.fs.directory', $t); if ($zip->extractTo($t)) { $zip->close(); $dir->moveFiles($to); $r = true; } SPFs::delete($dir->getPathname()); } SPException::catchErrors(0); } break; } return $r; }
public function install() { $id = $this->xGetString('id'); $name = $this->xGetString('name'); if (SPLoader::dirPath('usr.templates.' . $id) && !SPRequest::bool('force')) { throw new SPException(SPLang::e('TEMPLATE_INST_DUPLICATE', $name) . ' ' . Sobi::Txt('FORCE_TPL_UPDATE', Sobi::Url(array('task' => 'extensions.install', 'force' => 1, 'root' => basename($this->root) . '/' . basename($this->xmlFile))))); } $requirements = $this->xGetChilds('requirements/*'); if ($requirements && $requirements instanceof DOMNodeList) { SPFactory::Instance('services.installers.requirements')->check($requirements); } $language = $this->xGetChilds('language/file'); $folder = @$this->xGetChilds('language/@folder')->item(0)->nodeValue; if ($language && $language instanceof DOMNodeList && $language->length) { $langFiles = array(); foreach ($language as $file) { $adm = false; if ($file->attributes->getNamedItem('admin')) { $adm = $file->attributes->getNamedItem('admin')->nodeValue == 'true' ? true : false; } $langFiles[$file->attributes->getNamedItem('lang')->nodeValue][] = array('path' => Sobi::FixPath("{$this->root}/{$folder}/" . trim($file->nodeValue)), 'name' => $file->nodeValue, 'adm' => $adm); } SPFactory::CmsHelper()->installLang($langFiles, false, true); } $path = SPLoader::dirPath('usr.templates.' . $id, 'front', false); if (SPRequest::bool('force')) { /** @var $from SPDirectory */ $from = SPFactory::Instance('base.fs.directory', $this->root); $from->moveFiles($path); } else { if (!SPFs::move($this->root, $path)) { throw new SPException(SPLang::e('CANNOT_MOVE_DIRECTORY', $this->root, $path)); } } if (!SPRequest::bool('force')) { $section = $this->xGetChilds('install'); if ($section instanceof DOMNodeList && $section->length) { $this->section($id); } } //05 Oct 2015 Kishore $exec = $this->xGetString('exec'); if ($exec && SPFs::exists($path . DS . $exec)) { include_once "{$path}/{$exec}"; } /** @var $dir SPDirectory */ $dir =& SPFactory::Instance('base.fs.directory', $path); $zip = array_keys($dir->searchFile('.zip', false)); if (count($zip)) { foreach ($zip as $file) { SPFs::delete($file); } } Sobi::Trigger('After', 'InstallTemplate', array($id)); $dir =& SPFactory::Instance('base.fs.directory', SPLoader::dirPath('tmp.install')); $dir->deleteFiles(); return Sobi::Txt('TP.TEMPLATE_HAS_BEEN_INSTALLED', array('template' => $name)); }
protected function check($file) { $allowed = SPLoader::loadIniFile('etc.files'); $mType = SPFactory::Instance('services.fileinfo', $file)->mimeType(); if (strlen($mType) && !in_array($mType, $allowed)) { SPFs::delete($file); $this->message(array('type' => 'error', 'text' => SPLang::e('FILE_WRONG_TYPE', $mType), 'id' => '')); } return $mType; }
public function install() { $id = $this->xGetString('id'); $name = $this->xGetString('name'); if (SPLoader::dirPath('usr.templates.' . $id) && !SPRequest::bool('force')) { throw new SPException(SPLang::e('TEMPLATE_INST_DUPLICATE', $name) . ' ' . Sobi::Txt('FORCE_TPL_UPDATE', Sobi::Url(array('task' => 'extensions.install', 'force' => 1, 'root' => basename($this->root) . '/' . basename($this->xmlFile))))); } $requirements = $this->xGetChilds('requirements/*'); if ($requirements && $requirements instanceof DOMNodeList) { SPFactory::Instance('services.installers.requirements')->check($requirements); } $path = SPLoader::dirPath('usr.templates.' . $id, 'front', false); if (SPRequest::bool('force')) { /** @var $from SPDirectory */ $from = SPFactory::Instance('base.fs.directory', $this->root); $from->moveFiles($path); } else { if (!SPFs::move($this->root, $path)) { throw new SPException(SPLang::e('CANNOT_MOVE_DIRECTORY', $this->root, $path)); } } if (!SPRequest::bool('force')) { $section = $this->xGetChilds('install'); if ($section instanceof DOMNodeList && $section->length) { $this->section($id); } } $exec = $this->xGetString('exec'); if ($exec && SPFs::exists($this->root . DS . $exec)) { include_once "{$this->root}/{$exec}"; } /** @var $dir SPDirectory */ $dir =& SPFactory::Instance('base.fs.directory', $path); $zip = array_keys($dir->searchFile('.zip', false)); if (count($zip)) { foreach ($zip as $file) { SPFs::delete($file); } } Sobi::Trigger('After', 'InstallTemplate', array($id)); $dir =& SPFactory::Instance('base.fs.directory', SPLoader::dirPath('tmp.install')); $dir->deleteFiles(); return Sobi::Txt('TP.TEMPLATE_HAS_BEEN_INSTALLED', array('template' => $name)); }
private function delete() { $dir = $this->dir(SPRequest::cmd('templateName')); if (SPRequest::cmd('templateName') == SPC::DEFAULT_TEMPLATE) { $this->response(Sobi::Url('template.info'), Sobi::Txt('TP.DO_NOT_REMOVE'), true, 'error'); } if ($dir && SPFs::delete($dir)) { $this->response(Sobi::Url(array('task' => 'config.general')), Sobi::Txt('TP.REMOVED'), false, 'success'); } else { $this->response(Sobi::Back(), Sobi::Txt('TP.CANNOT_REMOVE'), false, 'error'); } }
private function store($key, $value, $msg = null) { // let's try to create kinda mutex here $file = SPLoader::path('tmp.info', 'front', false, 'txt'); while (SPFs::exists($file)) { usleep(100000); } $c = date(DATE_RFC822); SPFs::write($file, $c); $store = Sobi::GetUserData('requirements', array()); $store[$key] = array('value' => $value, 'message' => $msg); Sobi::SetUserData('requirements', $store); SPFs::delete($file); // $msg = $msg ? $msg[ 'org' ][ 'label' ] : null; // $file = SPLoader::path( 'tmp.info', 'front', false, 'txt' ); // $cont = null; // if ( SPFs::exists( $file ) ) { // $cont = SPFs::read( $file ); // } // $txt = "{$cont}\n{$key}={$msg};{$value}"; // SPFs::write( $file, $txt ); }
private function purge() { try { SPFactory::db()->truncate('spdb_errors'); } catch (SPException $x) { $this->response(Sobi::Url('error'), Sobi::Txt('ERR.ERROR_LOG_NOT_DELETED', array('error' => $x->getMessage())), false, SPC::ERROR_MSG); } if (SPFs::exists(SOBI_PATH . '/var/log/error.log')) { SPFs::delete(SOBI_PATH . '/var/log/error.log'); } $this->response(Sobi::Url('error'), Sobi::Txt('ERR.ERROR_LOG_DELETED'), false, SPC::SUCCESS_MSG); }
/** * Remove all files in directory * @return bool */ public function deleteFiles() { $this->iterator(); $log = array(); foreach ($this->_dirIterator as $child) { if (!$child->isDot()) { SPFs::delete($child->getPathname()); } } }
/** * Save an entry * * @param bool $apply */ protected function save($apply) { $new = true; if (!$this->_model) { $this->setModel(SPLoader::loadModel($this->_type)); } if ($this->_model->get('oType') != 'entry') { Sobi::Error('Entry', sprintf('Serious security violation. Trying to save an object which claims to be an entry but it is a %s. Task was %s', $this->_model->get('oType'), SPRequest::task()), SPC::ERROR, 403, __LINE__, __FILE__); exit; } /* check if we have stored last edit in cache */ $tsId = SPRequest::string('editentry', null, false, 'cookie'); if (!$tsId) { $tsId = SPRequest::cmd('ssid'); } $request = $this->getCache($tsId); $this->_model->init(SPRequest::sid($request)); $tplPackage = Sobi::Cfg('section.template', SPC::DEFAULT_TEMPLATE); $this->tplCfg($tplPackage); $customClass = null; if (isset($this->_tCfg['general']['functions']) && $this->_tCfg['general']['functions']) { $customClass = SPLoader::loadClass('/' . str_replace('.php', null, $this->_tCfg['general']['functions']), false, 'templates'); if (method_exists($customClass, 'BeforeStoreEntry')) { $customClass::BeforeStoreEntry($this->_model, $this->store['post']); SPFactory::registry()->set('requestcache_stored', $this->store); SPFactory::registry()->set('requestcache', $this->store['post']); } } $preState = array('approved' => $this->_model->get('approved'), 'state' => $this->_model->get('state'), 'new' => !$this->_model->get('id')); SPFactory::registry()->set('object_previous_state', $preState); $this->_model->getRequest($this->_type, $request); Sobi::Trigger($this->name(), __FUNCTION__, array(&$this->_model)); if ($this->_model->get('id') && $this->_model->get('id') == SPRequest::sid()) { $new = false; if (Sobi::My('id') && Sobi::My('id') == $this->_model->get('owner')) { $this->authorise('edit', 'own'); } else { $this->authorise('edit', '*'); } } else { $this->authorise('add', 'own'); } $this->_model->save($request); /* if there is something pay */ $pCount = SPFactory::payment()->count($this->_model->get('id')); if ($pCount && !Sobi::Can('entry.payment.free')) { // $this->paymentView( $tsid ); if ($customClass && method_exists($customClass, 'BeforeStoreEntryPayment')) { $customClass::BeforeStoreEntryPayment($this->_model->get('id')); } SPFactory::payment()->store($this->_model->get('id')); } /* delete cache files on after */ $file = str_replace('.', '-', $tsId); if (SPLoader::dirPath('tmp.edit.' . $file)) { SPFs::delete(SPLoader::dirPath('tmp.edit.' . $file)); } else { SPFactory::cache()->deleteVar('request_cache_' . $tsId); } SPLoader::loadClass('env.cookie'); SPCookie::delete('editentry'); $sid = $this->_model->get('id'); $pid = SPRequest::int('pid') ? SPRequest::int('pid') : Sobi::Section(); if ($new) { if ($this->_model->get('state') || Sobi::Can('entry.see_unpublished.own')) { $msg = $this->_model->get('state') ? Sobi::Txt('EN.ENTRY_SAVED') : Sobi::Txt('EN.ENTRY_SAVED_NP'); $url = Sobi::Url(array('sid' => $sid, 'pid' => $pid)); } else { // determine if there is a custom redirect if (Sobi::Cfg('redirects.entry_save_enabled') && !($pCount && !Sobi::Can('entry.payment.free'))) { $redirect = Sobi::Cfg('redirects.entry_save_url', null); if (!preg_match('/http[s]?:\\/\\/.*/', $redirect) && $redirect != 'index.php') { $redirect = Sobi::Url($redirect); } $this->response($redirect, Sobi::Txt(Sobi::Cfg('redirects.entry_save_msg', 'EN.ENTRY_SAVED_NP')), true, Sobi::Cfg('redirects.entry_save_msgtype', SPC::SUCCESS_MSG)); } else { $msg = Sobi::Txt('EN.ENTRY_SAVED_NP'); $url = Sobi::Url(array('sid' => $pid)); } } } elseif ($this->_model->get('approved') || Sobi::Can('entry.see_unapproved.own')) { $url = Sobi::Url(array('sid' => $sid, 'pid' => $pid)); $msg = $this->_model->get('approved') ? Sobi::Txt('EN.ENTRY_SAVED') : Sobi::Txt('EN.ENTRY_SAVED_NA'); } else { if ($this->_model->get('approved')) { $msg = Sobi::Txt('EN.ENTRY_SAVED'); } else { $msg = Sobi::Txt('EN.ENTRY_SAVED_NA'); } $url = Sobi::Url(array('sid' => $sid, 'pid' => $pid)); } if ($pCount && !Sobi::Can('entry.payment.free')) { $ident = md5(microtime() . $tsId . $sid . time()); $data = array('data' => SPFactory::payment()->summary($sid), 'ident' => $ident); $url = Sobi::Url(array('sid' => $sid, 'task' => 'entry.payment'), false, false); if (Sobi::Cfg('cache.l3_enabled', true)) { SPFactory::cache()->addObj($data, 'payment', $sid, Sobi::Section(), true); } else { SPFs::write(SPLoader::path('tmp.edit.' . $ident . '.payment', 'front', false, 'var'), SPConfig::serialize($data)); $url = Sobi::Url(array('sid' => $sid, 'task' => 'entry.payment', 'tsid' => $ident), false, false); } SPLoader::loadClass('env.cookie'); SPCookie::set('payment_' . $sid, $ident, SPCookie::days(1)); } if ($customClass && method_exists($customClass, 'AfterStoreEntry')) { $customClass::AfterStoreEntry($this->_model); } $this->logChanges('save', SPRequest::string('history-note')); $this->response($url, $msg, true, SPC::SUCCESS_MSG); }
/** * Deletes a file * @return bool */ public function delete() { return SPFs::delete($this->_filename); }
/** * @param $file * @param $output * @param $u */ protected function compileLessFile($file, $output, $u, $compress = false) { include_once 'phar://' . SOBI_PATH . '/lib/services/third-party/less/less.phar.tar.gz/Autoloader.php'; try { Less_Autoloader::register(); if ($compress) { $options = array('compress' => true, 'strictMath' => true); } else { $options = array(); } $parser = new Less_Parser($options); $parser->parseFile($file); $css = $parser->getCss(); if (SPFs::exists($output)) { SPFs::delete($output); } SPFs::write($output, $css); } catch (Exception $x) { $this->response(Sobi::Url($u), SPLang::e('TP.LESS_FILE_NOT_COMPILED', $x->getMessage()), false, SPC::ERROR_MSG); } }
public function remove() { $pid = $this->xGetString('id'); $function = $this->xGetString('uninstall'); if ($function) { $obj = explode(':', $function); $function = $obj[1]; $obj = $obj[0]; return SPFactory::Instance($obj)->{$function}($this->definition); } $permissions = $this->xGetChilds('installLog/permissions/*'); if ($permissions && $permissions instanceof DOMNodeList) { $permsCtrl =& SPFactory::Instance('ctrl.adm.acl'); for ($i = 0; $i < $permissions->length; $i++) { $perm = explode('.', $permissions->item($i)->nodeValue); $permsCtrl->removePermission($perm[0], $perm[1], $perm[2]); } } /** it doesn't make much sense that way - a backup is ok but this action is called uninstall and not revert */ // $mods = $this->xGetChilds( 'installLog/modified/*' ); // if ( $mods && ( $mods instanceof DOMNodeList ) ) { // $this->revert( $mods ); // } $files = $this->xGetChilds('installLog/files/*'); if ($files && $files instanceof DOMNodeList) { for ($i = 0; $i < $files->length; $i++) { $file = $files->item($i)->nodeValue; if (!strstr($file, SOBI_ROOT)) { $file = Sobi::FixPath(SOBI_ROOT . "/{$file}"); } if (SPFs::exists($file)) { SPFs::delete($file); } } } $actions = $this->xGetChilds('installLog/actions/*'); if ($actions && $actions instanceof DOMNodeList) { for ($i = 0; $i < $actions->length; $i++) { try { SPFactory::db()->delete('spdb_plugin_task', array('pid' => $pid, 'onAction' => $actions->item($i)->nodeValue)); } catch (SPException $x) { Sobi::Error('installer', SPLang::e('Cannot remove plugin task "%s". Db query failed. Error: %s', $actions->item($i)->nodeValue, $x->getMessage()), SPC::WARNING, 0); } } if ($this->xGetString('type') == 'payment') { try { SPFactory::db()->delete('spdb_plugin_task', array('pid' => $pid, 'onAction' => 'PaymentMethodView')); } catch (SPException $x) { Sobi::Error('installer', SPLang::e('Cannot remove plugin task "PaymentMethodView". Db query failed. Error: %s', $x->getMessage()), SPC::WARNING, 0); } } } $field = $this->xdef->query("/{$this->type}/fieldType[@typeId]"); if ($field && $field->length) { try { SPFactory::db()->delete('spdb_field_types', array('tid' => $field->item(0)->getAttribute('typeId'))); } catch (SPException $x) { Sobi::Error('installer', SPLang::e('CANNOT_REMOVE_FIELD_DB_ERR', $field->item(0)->getAttribute('typeId'), $x->getMessage()), SPC::WARNING, 0); } } $tables = $this->xGetChilds('installLog/sql/tables/*'); if ($tables && $tables instanceof DOMNodeList) { for ($i = 0; $i < $tables->length; $i++) { try { SPFactory::db()->drop($tables->item($i)->nodeValue); } catch (SPException $x) { Sobi::Error('installer', SPLang::e('CANNOT_DROP_TABLE', $tables->item($i)->nodeValue, $x->getMessage()), SPC::WARNING, 0); } } } $inserts = $this->xGetChilds('installLog/sql/queries/*'); if ($inserts && $inserts instanceof DOMNodeList) { for ($i = 0; $i < $inserts->length; $i++) { $table = $inserts->item($i)->attributes->getNamedItem('table')->nodeValue; $where = array(); $cols = $inserts->item($i)->childNodes; if ($cols->length) { for ($j = 0; $j < $cols->length; $j++) { $where[$cols->item($j)->nodeName] = $cols->item($j)->nodeValue; } } try { SPFactory::db()->delete($table, $where, 1); } catch (SPException $x) { Sobi::Error('installer', SPLang::e('CANNOT_DELETE_DB_ENTRIES', $table, $x->getMessage()), SPC::WARNING, 0); } } } $type = strlen($this->xGetString('type')) ? $this->xGetString('type') : ($this->xGetString('fieldType') ? 'field' : null); switch ($type) { default: case 'SobiProApp': case 'plugin': $t = Sobi::Txt('EX.PLUGIN_TYPE'); break; case 'field': $t = Sobi::Txt('EX.FIELD_TYPE'); break; case 'payment': $t = Sobi::Txt('EX.PAYMENT_METHOD_TYPE'); break; case 'language': $t = Sobi::Txt('EX.LANGUAGE_TYPE'); break; case 'module': $t = Sobi::Txt('EX.MODULE_TYPE'); break; } try { SPFactory::db()->delete('spdb_plugins', array('pid' => $pid, 'type' => $type), 1); } catch (SPException $x) { Sobi::Error('installer', SPLang::e('CANNOT_DELETE_PLUGIN_DB_ERR', $pid, $x->getMessage()), SPC::ERROR, 0); } try { SPFactory::db()->delete('spdb_plugin_section', array('pid' => $pid)); } catch (SPException $x) { Sobi::Error('installer', SPLang::e('CANNOT_DELETE_PLUGIN_SECTION_DB_ERR', $pid, $x->getMessage()), SPC::WARNING, 0); } SPFs::delete($this->xmlFile); return ucfirst(Sobi::Txt('EX.EXTENSION_HAS_BEEN_REMOVED', array('type' => $t, 'name' => $this->xGetString('name')))); }
protected function cleanTemp($force = false) { $this->cleanJCache(); $this->cleanDir(SPLoader::dirPath('var.js'), 'js', $force); $this->cleanDir(SPLoader::dirPath('var.css'), 'css', $force); $this->cleanDir(SPLoader::dirPath('tmp.edit'), -1, $force); $this->cleanDir(SPLoader::dirPath('tmp.img'), -1, $force); $this->cleanDir(SPLoader::dirPath('tmp'), -1, $force); try { SPFactory::db()->delete('spdb_search', array('lastActive<' => 'FUNCTION:DATE_SUB( CURDATE() , INTERVAL 7 DAY )')); } catch (SPException $x) { Sobi::Error('cache', SPLang::e('DB_REPORTS_ERR', $x->getMessage()), SPC::WARNING, 0, __LINE__, __FILE__); } $updatesDef = SPLoader::path('etc.updates', 'front', false, 'xml'); if (SPFs::exists($updatesDef)) { SPFs::delete($updatesDef); } }