Skip to content

Commit

Permalink
Converted to PSR-2
Browse files Browse the repository at this point in the history
  • Loading branch information
Normann Lou committed Jan 7, 2016
2 parents 1e3e767 + 480a4b6 commit a681cfb
Show file tree
Hide file tree
Showing 16 changed files with 585 additions and 432 deletions.
39 changes: 24 additions & 15 deletions code/admin/NonSecuredAssetAdmin.php
Expand Up @@ -7,7 +7,8 @@
* @package silverstripe-advancedassets
* @todo Modify addFolder() and initValidate() to show messages within the CMS.
*/
class NonSecuredAssetAdmin extends AssetAdmin implements PermissionProvider {
class NonSecuredAssetAdmin extends AssetAdmin implements PermissionProvider
{

/**
*
Expand Down Expand Up @@ -41,7 +42,8 @@ class NonSecuredAssetAdmin extends AssetAdmin implements PermissionProvider {
*
* @return void
*/
public function init() {
public function init()
{
parent::init();
$this->initValidate();
}
Expand All @@ -53,12 +55,13 @@ public function init() {
* @return SS_HTTPResponse
* @todo Refactor into single static. There are v.close dupes of this in the other controllers.
*/
public function initValidate() {
public function initValidate()
{
$id = SecuredFilesystem::get_numeric_identifier($this, 'ID');
if($id) {
if ($id) {
$folder = DataObject::get_by_id("Folder", $id);
if($folder && $folder->exists()) {
if($folder->Secured) {
if ($folder && $folder->exists()) {
if ($folder->Secured) {
$message = _t('SecuredFilesystem.messages.ERROR_ACCESS_ONLY_IN_SECURED_FILES');
return SecuredFilesystem::show_access_message($this, $message);
}
Expand All @@ -72,7 +75,8 @@ public function initValidate() {
/**
* @return array
*/
public function providePermissions() {
public function providePermissions()
{
$title = _t('NonSecuredAssetAdmin.labels.CMS_PERMISSIONS_LABEL_FILES_ADVANCED', 'Files (Advanced)');
return array(
"CMS_ACCESS_NonSecuredAssetAdmin" => array(
Expand All @@ -86,7 +90,8 @@ public function providePermissions() {
*
* @return SS_List
*/
public function getList() {
public function getList()
{
$list = parent::getList();
$list = $list->exclude("Secured", "1");
return $list;
Expand All @@ -96,17 +101,19 @@ public function getList() {
*
* @return SS_List
*/
public function SiteTreeAsUL() {
public function SiteTreeAsUL()
{
return $this->getSiteTreeFor($this->stat('tree_class'), null, 'ChildFoldersExcludeSecured');
}

/**
* @param boolean $unlinked
* @return array
*/
public function Breadcrumbs($unlinked = false) {
public function Breadcrumbs($unlinked = false)
{
$items = parent::Breadcrumbs($unlinked);
if(isset($items[0]->Title)){
if (isset($items[0]->Title)) {
$items[0]->Link = Controller::join_links(singleton('NonSecuredAssetAdmin')->Link('show'), 0);
}
return $items;
Expand All @@ -118,7 +125,8 @@ public function Breadcrumbs($unlinked = false) {
*
* @return null
*/
public function doSync() {
public function doSync()
{
$message = SecuredFilesystem::sync_secured();
$this->response->addHeader('X-Status', rawurlencode($message));

Expand All @@ -132,11 +140,12 @@ public function doSync() {
* @param SS_HTTPRequest $request
* @return HTMLText
*/
public function addfolder($request) {
public function addfolder($request)
{
$parentId = SecuredFilesystem::get_numeric_identifier($this, 'ParentID');
$folder = DataObject::get_by_id("Folder", $parentId);
if($folder && $folder->exists()) {
if($folder->Secured) {
if ($folder && $folder->exists()) {
if ($folder->Secured) {
$message = _t('SecuredFilesystem.messages.ERROR_ACCESS_ONLY_IN_SECURED_FILES');
return SecuredFilesystem::show_access_message($this, $message);
}
Expand Down
99 changes: 56 additions & 43 deletions code/admin/SecuredAssetAdmin.php
Expand Up @@ -11,7 +11,8 @@
* @see {@link FileSecured} and {@link FolderSecured}.
* @todo Modify addFolder() and initValidate() to show messages within the CMS.
*/
class SecuredAssetAdmin extends AssetAdmin implements PermissionProvider {
class SecuredAssetAdmin extends AssetAdmin implements PermissionProvider
{

/**
*
Expand Down Expand Up @@ -62,7 +63,8 @@ class SecuredAssetAdmin extends AssetAdmin implements PermissionProvider {
*
* @return void
*/
public function init() {
public function init()
{
self::instantiate();
parent::init();
$this->initValidate();
Expand All @@ -75,12 +77,13 @@ public function init() {
* @return SS_HTTPResponse
* @todo Refactor into single static. There are v.close dupes of this in the other controllers.
*/
public function initValidate() {
public function initValidate()
{
$folderId = SecuredFilesystem::get_numeric_identifier($this, 'ID');
if($folderId) {
if ($folderId) {
$folder = DataObject::get_by_id("Folder", $folderId);
if($folder && $folder->exists()) {
if(!$folder->Secured) {
if ($folder && $folder->exists()) {
if (!$folder->Secured) {
$message = _t('SecuredFilesystem.messages.ERROR_ACCESS_ONLY_IN_FILES');
return SecuredFilesystem::show_access_message($this, $message);
}
Expand All @@ -95,20 +98,21 @@ public function initValidate() {
*
* @return void
*/
public static function instantiate() {
public static function instantiate()
{
$secured_root_folder = BASE_PATH . DIRECTORY_SEPARATOR . ASSETS_DIR . DIRECTORY_SEPARATOR . SECURED_FILES_ASSET_SUBDIR;
if(!is_dir($secured_root_folder)) {
if (!is_dir($secured_root_folder)) {
FileSecured::find_or_make_secured(SECURED_FILES_ASSET_SUBDIR . DIRECTORY_SEPARATOR . "Uploads");
}

$resource_folder = BASE_PATH . DIRECTORY_SEPARATOR . SECURED_FILES_MODULE_DIR . DIRECTORY_SEPARATOR . 'resource';
$default_lock_images_folder = BASE_PATH . DIRECTORY_SEPARATOR . ASSETS_DIR . DIRECTORY_SEPARATOR . '_defaultlockimages';
if(!is_dir($default_lock_images_folder)) {
if (!is_dir($default_lock_images_folder)) {
mkdir($default_lock_images_folder, Config::inst()->get('Filesystem', 'folder_create_mask'));
$resource_images_folder = $resource_folder . DIRECTORY_SEPARATOR . 'images';
$dir = dir($resource_images_folder);
while(false !== $entry = $dir->read()) {
if($entry == '.' || $entry == '..') {
while (false !== $entry = $dir->read()) {
if ($entry == '.' || $entry == '..') {
continue;
}
copy($resource_images_folder . DIRECTORY_SEPARATOR . $entry, $default_lock_images_folder . DIRECTORY_SEPARATOR . $entry);
Expand All @@ -122,7 +126,8 @@ public static function instantiate() {
*
* @return SS_List
*/
public function getList() {
public function getList()
{
$list = parent::getList();
$list = $list->filter("Secured", 1);
$securedRoot = FileSecured::getSecuredRoot();
Expand All @@ -137,16 +142,17 @@ public function getList() {
*
* @return number
*/
public function currentPageID() {
if(is_numeric($this->request->requestVar('ID'))) {
public function currentPageID()
{
if (is_numeric($this->request->requestVar('ID'))) {
return $this->request->requestVar('ID');
} elseif(is_numeric($this->urlParams['ID'])) {
} elseif (is_numeric($this->urlParams['ID'])) {
return $this->urlParams['ID'];
} elseif(Session::get("{$this->class}.currentPage")) {
} elseif (Session::get("{$this->class}.currentPage")) {
return Session::get("{$this->class}.currentPage");
} else {
$securedRoot = FileSecured::getSecuredRoot();
if($securedRoot && $securedRoot->exists()) {
if ($securedRoot && $securedRoot->exists()) {
return $securedRoot->ID;
} else {
SecuredAssetAdmin::instantiate();
Expand All @@ -162,9 +168,11 @@ public function currentPageID() {
* @param FieldList $fields
* @return Form
*/
public function getEditForm($id = null, $fields = null) {
if(!$id)
public function getEditForm($id = null, $fields = null)
{
if (!$id) {
$id = $this->currentPageID();
}
$form = parent::getEditForm($id, $fields);
$folder = ($id && is_numeric($id) && $id > 0) ? DataObject::get_by_id('Folder', $id, false) : $this->currentPage();
$gridField = $form->Fields()->dataFieldByName('File');
Expand All @@ -187,41 +195,41 @@ public function getEditForm($id = null, $fields = null) {
'WhoCanEditHTML' => 'HTMLText->RAW',
));

if($id == FileSecured::getSecuredRoot()->ID) {
if ($id == FileSecured::getSecuredRoot()->ID) {
$form->Fields()->removeByName("DetailsView");
$config->removeComponentsByType("GridFieldLevelup");
} else {
$config->getComponentByType("GridFieldLevelup")->setLinkSpec('admin/' . self::$url_segment . '/show/%d');
}

$gridField->setTitle(_t("SECUREDASSETADMIN.SecuriedFiles", "Advanced Assets"));
if($id == FileSecured::getSecuredRoot()->ID) {
if ($id == FileSecured::getSecuredRoot()->ID) {
$form->Fields()->removeByName("DetailsView");
}

// Need to use CMSSecuredFileAddController, so update the "Upload" button.
if($folder->canCreate()) {
if ($folder->canCreate()) {
$uploadBtn = new LiteralField(
'UploadButton', sprintf(
'<a class="ss-ui-button ss-ui-action-constructive cms-panel-link"'
. ' data-pjax-target="Content" data-icon="drive-upload"'
. ' href="%s">%s</a>',
Controller::join_links(singleton('CMSSecuredFileAddController')->Link(), '?ID=' . $folder->ID),
. ' href="%s">%s</a>',
Controller::join_links(singleton('CMSSecuredFileAddController')->Link(), '?ID=' . $folder->ID),
_t('Folder.UploadFilesButton', 'Upload')
)
);
} else {
$uploadBtn = null;
}

foreach(array("ListView", "TreeView") as $viewName) {
foreach (array("ListView", "TreeView") as $viewName) {
$view = $form->Fields()->fieldByName("Root." . $viewName);
foreach($view->Fields() as $f) {
if($f instanceof CompositeField) {
foreach($f->FieldList() as $cf) {
if($cf instanceof CompositeField) {
foreach ($view->Fields() as $f) {
if ($f instanceof CompositeField) {
foreach ($f->FieldList() as $cf) {
if ($cf instanceof CompositeField) {
$cf->removeByName("UploadButton");
if($uploadBtn) {
if ($uploadBtn) {
$cf->insertBefore($uploadBtn, "AddFolderButton");
}
}
Expand All @@ -237,7 +245,8 @@ public function getEditForm($id = null, $fields = null) {
*
* @return SS_List
*/
public function SiteTreeAsUL() {
public function SiteTreeAsUL()
{
$root = FileSecured::getSecuredRoot();
return $this->getSiteTreeFor($this->stat('tree_class'), $root->ID, 'ChildFoldersOnlySecured');
}
Expand All @@ -247,18 +256,19 @@ public function SiteTreeAsUL() {
* @param boolean $unlinked
* @return ArrayList
*/
public function Breadcrumbs($unlinked = false) {
public function Breadcrumbs($unlinked = false)
{
$itemsDefault = parent::Breadcrumbs($unlinked);

$items = new ArrayList();
$i = 0;
foreach($itemsDefault as $item) {
if($i !== 0) {
foreach ($itemsDefault as $item) {
if ($i !== 0) {
$items->push($item);
}
$i++;
}
if(isset($items[0]->Title)) {
if (isset($items[0]->Title)) {
$items[0]->Title = _t("SECUREDASSETADMIN.SecuriedFiles", SECURED_FILES_MODULE_NAME);
}

Expand All @@ -269,7 +279,8 @@ public function Breadcrumbs($unlinked = false) {
*
* @return array
*/
public function providePermissions() {
public function providePermissions()
{
$title = _t("SECUREDASSETADMIN.MENUTITLE", LeftAndMain::menu_title_for_class($this->class));
return array(
"CMS_ACCESS_SecuredAssetAdmin" => array(
Expand All @@ -286,7 +297,8 @@ public function providePermissions() {
*
* @return null
*/
public function doSync() {
public function doSync()
{
$securedRoot = FileSecured::getSecuredRoot();
$message = SecuredFilesystem::sync_secured($securedRoot->ID);
$this->response->addHeader('X-Status', rawurlencode($message));
Expand All @@ -300,11 +312,12 @@ public function doSync() {
* @param SS_HTTPRequest $request
* @return HTMLText
*/
public function addfolder($request) {
public function addfolder($request)
{
$parentId = SecuredFilesystem::get_numeric_identifier($this, 'ParentID');
$folder = DataObject::get_by_id("Folder", $parentId);
if($folder && $folder->exists()) {
if(!$folder->Secured) {
if ($folder && $folder->exists()) {
if (!$folder->Secured) {
$message = _t('SecuredFilesystem.messages.ERROR_ACCESS_ONLY_IN_FILES');
return SecuredFilesystem::show_access_message($this, $message);
}
Expand All @@ -324,9 +337,10 @@ public function addfolder($request) {
* @param string $folderRes
* @return void
*/
public static function write_config_files($folderSec, $folderRes) {
public static function write_config_files($folderSec, $folderRes)
{
// Take the dummy config files from the module's root dir, then populate and move
if(!file_exists($folderSec . DIRECTORY_SEPARATOR . '.htaccess')) {
if (!file_exists($folderSec . DIRECTORY_SEPARATOR . '.htaccess')) {
$data = new ArrayData(array(
'base' => BASE_URL ? BASE_URL : '/',
'frameworkDir' => FRAMEWORK_DIR,
Expand All @@ -339,5 +353,4 @@ public static function write_config_files($folderSec, $folderRes) {
file_put_contents($folderSec . DIRECTORY_SEPARATOR . 'web.config', $webDotConfig->getValue());
}
}

}

0 comments on commit a681cfb

Please sign in to comment.