/** * @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 ProxyUpload() { $ident = SPRequest::cmd('ident', null, 'post'); $data = SPRequest::file($ident, 'tmp_name'); $secret = md5(Sobi::Cfg('secret')); if ($data) { $properties = SPRequest::file($ident); $orgFileName = $properties['name']; $extension = SPFs::getExt($orgFileName); $orgFileName = str_replace('.' . $extension, '.' . strtolower($extension), $orgFileName); if ($properties['size'] > $this->maxSize) { $this->message(array('type' => 'error', 'text' => SPLang::e('FIELD_IMG_TOO_LARGE', $this->name, $properties['size'], $this->maxSize), 'id' => '')); } $dirNameHash = md5($orgFileName . time() . $secret); $dirName = SPLoader::dirPath("tmp.files.{$secret}.{$dirNameHash}", 'front', false); SPFs::mkdir($dirName); $path = $dirName . $orgFileName; /** @var $file SPImage */ $orgImage = SPFactory::Instance('base.fs.image'); if (!$orgImage->upload($data, $path)) { $this->message(array('type' => 'error', 'text' => SPLang::e('CANNOT_UPLOAD_FILE'), 'id' => '')); } if (Sobi::Cfg('image_field.fix_rotation', true)) { if ($orgImage->fixRotation()) { $orgImage->save(); } } if ($this->crop) { $croppedImage = clone $orgImage; list($originalWidth, $originalHeight) = getimagesize($path); $aspectRatio = $this->resizeWidth / $this->resizeHeight; $width = $aspectRatio * $originalHeight > $originalWidth ? $originalWidth : $aspectRatio * $originalHeight; $height = $originalWidth / $aspectRatio > $originalHeight ? $originalHeight : $originalWidth / $aspectRatio; try { $croppedImage->crop($width, $height); $croppedImage->saveAs($dirName . 'cropped_' . $orgFileName); $ico = SPFactory::Instance('base.fs.image', $dirName . 'cropped_' . $orgFileName); } catch (SPException $x) { $this->message(array('type' => 'error', 'text' => SPLang::e('FIELD_IMG_CANNOT_CROP', $x->getMessage()), 'id' => '')); } } else { $ico = clone $orgImage; } $image = clone $orgImage; try { $previewSize = explode(':', Sobi::Cfg('image.preview_size', '500:500')); $image->resample($previewSize[0], $previewSize[1], false); $image->saveAs($dirName . 'resized_' . $orgFileName); } catch (SPException $x) { $image->delete(); $this->message(array('type' => 'error', 'text' => SPLang::e('FIELD_IMG_CANNOT_RESAMPLE', $x->getMessage()), 'id' => '')); } try { $icoSize = explode(':', Sobi::Cfg('image.ico_size', '80:80')); $ico->resample($icoSize[0], $icoSize[1], false); $ico->saveAs($dirName . 'icon_' . $orgFileName); } catch (SPException $x) { $ico->delete(); $this->message(array('type' => 'error', 'text' => SPLang::e('FIELD_IMG_CANNOT_RESAMPLE', $x->getMessage()), 'id' => '')); } $path = $orgImage->getPathname(); $type = $this->check($path); $properties['tmp_name'] = $path; SPFs::write(SPLoader::dirPath("tmp.files.{$secret}", 'front', false) . '/' . $orgFileName . '.var', SPConfig::serialize($properties)); $response = array('type' => 'success', 'text' => $this->crop ? Sobi::Txt('IMAGE_UPLOADED_CROP', $properties['name'], $type) : Sobi::Txt('FILE_UPLOADED', $properties['name']), 'id' => 'directory://' . $dirNameHash, 'data' => array('name' => $properties['name'], 'type' => $properties['type'], 'size' => $properties['size'], 'original' => $dirNameHash . '/' . $properties['name'], 'icon' => $dirNameHash . '/' . 'icon_' . $orgFileName, 'crop' => $this->crop, 'height' => $this->resizeHeight, 'width' => $this->resizeWidth)); } else { $response = array('type' => 'error', 'text' => SPLang::e('CANNOT_UPLOAD_FILE_NO_DATA'), 'id' => ''); } $this->message($response); }
private function langFiles($tag, $def, $dir, &$FilesLog) { $target = $tag == 'administration' ? implode(DS, array(SOBI_ROOT, 'administrator', 'language', $this->id)) : implode(DS, array(SOBI_ROOT, 'language', $this->id)); if (!file_exists($target)) { $this->error = Sobi::Txt('LANG_INSTALL_NO_CORE', $this->id); $this->errorType = SPC::WARN_MSG; SPFs::mkdir($target); } $files = $def->getElementsByTagName($tag)->item(0)->getElementsByTagName('files')->item(0); $folder = $files->getAttribute('folder'); $folder = $dir . $folder . DS; foreach ($files->getElementsByTagName('filename') as $file) { if (file_exists($folder . $file->nodeValue)) { if (!SPFs::copy($folder . $file->nodeValue, $target . DS . $file->nodeValue)) { SPFactory::message()->error(Sobi::Txt('Cannot copy %s to %s', $folder . $file->nodeValue, $target . DS . $file->nodeValue), false); } else { $FilesLog[] = str_replace(array(DS . DS, SOBI_ROOT), array(DS, null), $target . DS . $file->nodeValue); } } else { SPFactory::message()->error(Sobi::Txt('File %s does not exist!', $folder . $file->nodeValue), false); } } }
private function install($file = null) { $arch = SPFactory::Instance('base.fs.archive'); $ajax = strlen(SPRequest::cmd('ident', null, 'post')); if (!$file && SPRequest::string('root')) { $file = str_replace('.xml', null, SPRequest::string('root')); $file = SPLoader::path('tmp.install.' . $file, 'front', true, 'xml'); } if (!$file) { $ident = SPRequest::cmd('ident', null, 'post'); $data = SPRequest::file($ident); $name = str_replace(array('.' . SPFs::getExt($data['name']), '.'), null, $data['name']); $path = SPLoader::dirPath('tmp.install.' . $name, 'front', false); $c = 0; while (SPFs::exists($path)) { $path = SPLoader::dirPath('tmp.install.' . $name . '_' . ++$c, 'front', false); } /* * temp directory - will be removed later but it needs to be writable for apache and Joomla! fs (FTP mode) */ try { if (Sobi::Cfg('ftp_mode')) { SPFs::mkdir($path, 0777); } else { SPFs::mkdir($path); } } catch (SPException $x) { return $this->ajaxResponse($ajax, $x->getMessage(), false, SPC::ERROR_MSG); } $file = $path . '/' . $data['name']; try { $arch->upload($data['tmp_name'], $file); } catch (SPException $x) { return $this->ajaxResponse($ajax, $x->getMessage(), false, SPC::ERROR_MSG); } } elseif (SPRequest::string('root') && $file) { $path = dirname($file); } else { $arch->setFile($file); $name = str_replace(array('.' . SPFs::getExt($file), '.'), null, basename($file)); $path = SPLoader::dirPath('tmp.install.' . $name, 'front', false); $c = 0; while (SPFs::exists($path)) { $path = SPLoader::dirPath('tmp.install.' . $name . '_' . ++$c, 'front', false); } /* * temp directory - will be removed later but it needs to writable for apache and Joomla! fs (FTP mode) */ try { if (Sobi::Cfg('ftp_mode')) { SPFs::mkdir($path, 0777); } else { SPFs::mkdir($path); } } catch (SPException $x) { return $this->ajaxResponse($ajax, $x->getMessage(), false, SPC::ERROR_MSG); } } if ($path) { if (!SPRequest::string('root')) { if (!$arch->extract($path)) { return $this->ajaxResponse($ajax, SPLang::e('CANNOT_EXTRACT_ARCHIVE', basename($file), $path), false, SPC::ERROR_MSG); } } $dir =& SPFactory::Instance('base.fs.directory', $path); $xml = array_keys($dir->searchFile('.xml', false, 2)); if (!count($xml)) { return $this->ajaxResponse($ajax, SPLang::e('NO_INSTALL_FILE_IN_PACKAGE'), false, SPC::ERROR_MSG); } $definition = $this->searchInstallFile($xml); if (!$definition) { if (SPFactory::CmsHelper()->installerFile($xml)) { try { $message = SPFactory::CmsHelper()->install($xml, $path); return $this->ajaxResponse($ajax, $message['msg'], $ajax, $message['msgtype']); } catch (SPException $x) { return $this->ajaxResponse($ajax, $x->getMessage(), $ajax, SPC::ERROR_MSG); } } else { return $this->ajaxResponse($ajax, SPLang::e('NO_INSTALL_FILE_IN_PACKAGE'), false, SPC::ERROR_MSG); } } /** @var $installer SPInstaller */ $installer =& SPFactory::Instance('services.installers.' . trim(strtolower($definition->documentElement->tagName)), $xml[0], trim($definition->documentElement->tagName)); try { $installer->validate(); $msg = $installer->install(); return $this->ajaxResponse($ajax, $msg, true, SPC::SUCCESS_MSG); } catch (SPException $x) { return $this->ajaxResponse($ajax, $x->getMessage(), false, SPC::ERROR_MSG); } } else { return $this->ajaxResponse($ajax, SPLang::e('NO_FILE_HAS_BEEN_UPLOADED'), false, SPC::ERROR_MSG); } }
/** * Small work-around * The imageTYPE function is not very suitable for OO code * @return void */ private function storeImage() { $st = preg_replace('/[^0-9]/', null, microtime(true) * 10000); $this->temp = SPLoader::path('tmp.img.' . $st, 'front', false, 'var', false); if (!SPLoader::dirPath('tmp.img', 'front', true)) { SPFs::mkdir(SPLoader::dirPath('tmp.img', 'front', false)); } switch ($this->type) { case IMAGETYPE_GIF: imagegif($this->image, $this->temp); break; case IMAGETYPE_JPEG: case IMAGETYPE_JPEG2000: imagejpeg($this->image, $this->temp, Sobi::Cfg('image.jpeg_quality', 75)); break; case IMAGETYPE_PNG: imagepng($this->image, $this->temp, Sobi::Cfg('image.png_compression', 0)); break; } $this->_content = file_get_contents($this->temp); if ($this->image) { imagedestroy($this->image); } }
/** * Moves file to new location * @param string $target - destination path * @return bool */ public function move($target) { $f = explode(DS, $target); $path = str_replace($f[count($f) - 1], null, $target); if (!SPFs::exists($path)) { SPFs::mkdir($path); } if (SPFs::move($this->_filename, $target)) { $this->_filename = $target; } return $this; }
/** * @param DOMNodeList $folders * @param $eid * @param $backup * @return void */ private function files($folders, $eid, $backup) { $log = array('created' => array(), 'modified' => array()); foreach ($folders as $folder) { $target = $folder->attributes->getNamedItem('path')->nodeValue; if (strstr($target, 'templates:default')) { $target = str_replace('templates:default', 'templates:' . SPC::DEFAULT_TEMPLATE, $target); } $basePath = explode(':', $target); $basePath = $basePath[0]; $target = str_replace($basePath . ':', null, $target); $target = $this->joinPath($basePath, $target, $eid); if (!SPFs::exists($target)) { if (SPFs::mkdir($target)) { $log['created'][] = $target; } } foreach ($folder->childNodes as $child) { // the path within the node value is the "from" path and has to be removed from the target path $remove = null; if (strstr($child->nodeValue, '/')) { $remove = explode('/', $child->nodeValue); array_pop($remove); if (!is_string($remove) && count($remove)) { $remove = implode('/', $remove); } else { $remove = null; } } switch ($child->nodeName) { case 'folder': /* * the directory iterator is a nice thing but it need lot of time and memory * - so let's simplify it */ // $dir = SPFactory::Instance( 'base.fs.directory', str_replace( DS . DS, DS, $this->root.DS.$child->nodeValue ) ); $files = array(); $this->travelDir($this->root . '/' . $child->nodeValue, $files); if (count($files)) { $this->_d(sprintf('List %s', print_r($files, true))); foreach ($files as $file) { $this->_d(sprintf('Parsing %s', $files)); $tfile = Sobi::FixPath(str_replace($this->root, null, $file)); $bPath = Sobi::FixPath($backup . $tfile); if ($remove && strstr($tfile, $remove) && strpos($tfile, $remove) < 2) { $tfile = Sobi::FixPath(str_replace($remove, null, $tfile)); } $t = Sobi::FixPath($target . $tfile); if (SPFs::exists($t)) { SPFs::copy($t, $bPath); } if (SPFs::copy($file, $t)) { // if this file existed already, do not add it to the log if (!SPFs::exists($bPath)) { $log['created'][] = $t; $this->_d(sprintf('File %s doesn\'t exist', $t)); } else { $this->_d(sprintf('File %s exist and will be backed up', $t)); $log['modified'][] = array('file' => $t, 'size' => filesize($t), 'checksum' => md5_file($t), 'backup' => $bPath); } } else { $this->_d(sprintf('Cannot copy %s to %s', $file, $t)); } } } break; case 'file': $bPath = null; $tfile = $child->nodeValue; // remove the install path if ($remove && strstr($child->nodeValue, $remove) && strpos($child->nodeValue, $remove) < 2) { $tfile = Sobi::FixPath(str_replace($remove, null, $child->nodeValue)); } $t = Sobi::FixPath($target . $tfile); // if modifying file - backup file if (SPFs::exists($t)) { $bPath = Sobi::FixPath("{$backup}/{$child->nodeValue}"); SPFs::copy($t, $bPath); } if (SPFs::copy("{$this->root}/{$child->nodeValue}", $t)) { // if this file existed already, do not add it to the log if (!SPFs::exists($bPath)) { $log['created'][] = $t; $this->_d(sprintf('File %s doesn\'t exist', $t)); } else { // if modifying file - store the current data so when we are going to restore it, we can be sure we are not overwriting some file $log['modified'][] = array('file' => $t, 'size' => filesize($t), 'checksum' => md5_file($t), 'backup' => $bPath); /** 1.1 changes - we don't want to restore the backed up files because it causing ,uch more problems as it is worth */ $log['created'][] = $t; $this->_d(sprintf('File %s exist and will be backed up', $t)); } } break; } } } return $log; }