<commit_before><?php
/*=========================================================================
 MIDAS Server
 Copyright (c) Kitware SAS. 26 rue <NAME>. 69100 Villeurbanne, FRANCE
 All rights reserved.
 More information http://www.kitware.com

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

         http://www.apache.org/licenses/LICENSE-2.0.txt

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
=========================================================================*/

/**
 *  AJAX request for the admin Controller
 */
class BrowseController extends AppController
{
  public $_models = array('User', 'Community', 'Folder', 'Item', 'ItemRevision');
  public $_daos = array('User', 'Community', 'Folder', 'Item');
  public $_components = array('Date', 'Utility', 'Sortdao');

  /** Init Controller */
  function init()
    {
    $this->view->activemenu = 'browse'; // set the active menu
    }  // end init()

  /** Index Action*/
  public function indexAction()
    {
    $items = array();
    $header = "";

    $this->view->Date = $this->Component->Date;

    $this->view->header = $this->t('Explore');

    $this->view->itemThumbnails = $this->Item->getRandomThumbnails($this->userSession->Dao, 0, 12, true);

    $this->view->items = $this->Item->getMostPopulars($this->userSession->Dao, 30);

    $this->view->nUsers = $this->User->getCountAll();
    $this->view->nCommunities = $this->Community->getCountAll();
    $this->view->nItems = $this->Item->getCountAll();

    $this->view->json['community']['titleCreateLogin'] = $this->t('Please log in');
    $this->view->json['community']['contentCreateLogin'] = $this->t('You need to be logged in to be able to create a community.');
    }

  /** move or copy selected element*/
  public function movecopyAction()
    {
    $shareSubmit = $this->_getParam('shareElement');
    $duplicateSubmit = $this->_getParam('duplicateElement');
    $moveSubmit = $this->_getParam('moveElement');

    $select = $this->_getParam('selectElement');
    $share = $this->_getParam('share');
    $duplicate = $this->_getParam('duplicate');
    $move = $this->_getParam('move');

    // used for movecopyAction
    if(isset($moveSubmit) || isset($shareSubmit) || isset($duplicateSubmit))
      {
      $elements = explode(';', $this->_getParam('elements'));
      $destination = $this->_getParam('destination');
      $ajax = $this->_getParam('ajax');
      $folderIds = explode('-', $elements[0]);
      $itemIds = explode('-', $elements[1]);
      $folders = $this->Folder->load($folderIds);
      $items = $this->Item->load($itemIds);
      $destinationFolder = $this->Folder->load($destination);
      if(empty($folders) && empty ($items))
        {
        throw new Zend_Exception("No element selected");
        }
      if($destinationFolder == false)
        {
        throw new Zend_Exception("Unable to load destination");
        }

      // Move folder(s)
      foreach($folders as $folder)
        {
        if(isset($moveSubmit))
          {
          $this->Folder->move($folder, $destinationFolder);
          }
        }

      $sourceFolderIds = array();
      foreach($items as $item)
        {
        if(isset($shareSubmit))
          {
          foreach($item->getFolders() as $parentFolder)
            {
            $folderId = $parentFolder->getKey();
            array_push($sourceFolderIds, $folderId);
            }
          if(in_array($destinationFolder->getKey(), $sourceFolderIds))
            {
            $this->_redirect('/item/'.$item->getKey());
            }
          else
            {
            // Do not update item name in item share action
            $this->Folder->addItem($destinationFolder, $item, false);
            $this->Item->addReadonlyPolicy($item, $destinationFolder);
            }
          }
        elseif(isset($duplicateSubmit))
          {
          $this->Item->duplicateItem($item, $this->userSession->Dao, $destinationFolder);
          }
        else //moveSubmit, Move item(s)
          {
          $from = $this->_getParam('from');
          $fromFolder = $this->Folder->load($from);
          if($destinationFolder == false)
            {
            throw new Zend_Exception("Unable to load destination");
            }
          if($fromFolder == false)
            {
            throw new Zend_Exception("Unable to load move from folder");
            }
          if($destinationFolder->getKey() != $fromFolder->getKey())<commit_msg>BUG: refs #759. Add server-side policy checks for movecopy actions<commit_after><?php
/*=========================================================================
 MIDAS Server
 Copyright (c) Kitware SAS. 26 rue <NAME>. 69100 Villeurbanne, FRANCE
 All rights reserved.
 More information http://www.kitware.com

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

         http://www.apache.org/licenses/LICENSE-2.0.txt

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
=========================================================================*/

/**
 *  AJAX request for the admin Controller
 */
class BrowseController extends AppController
{
  public $_models = array('User', 'Community', 'Folder', 'Item', 'ItemRevision');
  public $_daos = array('User', 'Community', 'Folder', 'Item');
  public $_components = array('Date', 'Utility', 'Sortdao');

  /** Init Controller */
  function init()
    {
    $this->view->activemenu = 'browse'; // set the active menu
    }  // end init()

  /** Index Action*/
  public function indexAction()
    {
    $items = array();
    $header = "";

    $this->view->Date = $this->Component->Date;

    $this->view->header = $this->t('Explore');

    $this->view->itemThumbnails = $this->Item->getRandomThumbnails($this->userSession->Dao, 0, 12, true);

    $this->view->items = $this->Item->getMostPopulars($this->userSession->Dao, 30);

    $this->view->nUsers = $this->User->getCountAll();
    $this->view->nCommunities = $this->Community->getCountAll();
    $this->view->nItems = $this->Item->getCountAll();

    $this->view->json['community']['titleCreateLogin'] = $this->t('Please log in');
    $this->view->json['community']['contentCreateLogin'] = $this->t('You need to be logged in to be able to create a community.');
    }

  /** move or copy selected element*/
  public function movecopyAction()
    {
    $shareSubmit = $this->_getParam('shareElement');
    $duplicateSubmit = $this->_getParam('duplicateElement');
    $moveSubmit = $this->_getParam('moveElement');

    $select = $this->_getParam('selectElement');
    $share = $this->_getParam('share');
    $duplicate = $this->_getParam('duplicate');
    $move = $this->_getParam('move');

    // used for movecopyAction
    if(isset($moveSubmit) || isset($shareSubmit) || isset($duplicateSubmit))
      {
      $elements = explode(';', $this->_getParam('elements'));
      $destination = $this->_getParam('destination');
      $ajax = $this->_getParam('ajax');
      $folderIds = explode('-', $elements[0]);
      $itemIds = explode('-', $elements[1]);
      $folders = $this->Folder->load($folderIds);
      $items = $this->Item->load($itemIds);
      $destinationFolder = $this->Folder->load($destination);
      if(empty($folders) && empty ($items))
        {
        throw new Zend_Exception("No element selected");
        }
      if($destinationFolder == false)
        {
        throw new Zend_Exception("Unable to load destination");
        }
      if(!$this->Folder->policyCheck($destinationFolder, $this->userSession->Dao, MIDAS_POLICY_WRITE))
        {
        throw new Zend_Exception('Write permission required into the destination folder');
        }

      // Folders can only be moved, not shared or duplicated
      if(isset($moveSubmit))
        {
        foreach($folders as $folder)
          {
          if(!$this->Folder->policyCheck($folder, $this->userSession->Dao, MIDAS_POLICY_ADMIN))
            {
            throw new Zend_Exception('You must own a folder in order to move it');
            }
          $this->Folder->move($folder, $destinationFolder);
          }
        }

      $sourceFolderIds = array();
      foreach($items as $item)
        {
        if(isset($shareSubmit))
          {
          if(!$this->Item->policyCheck($item, $this->userSession->Dao, MIDAS_POLICY_READ))
            {
            throw new Zend_Exception('You must have read permission on an item to share it');
            }
          foreach($item->getFolders() as $parentFolder)
            {
            $folderId = $parentFolder->getKey();
            array_push($sourceFolderIds, $folderId);
            }
          if(in_array($destinationFolder->getKey(), $sourceFolderIds))
            {
            $this->_redirect('/item/'.$item->getKey());
            }
          else
            {
            // Do not update item name in item share action
            $this->Folder->addItem($destinationFolder, $item, false);
            $this->Item->addReadonlyPolicy($item, $destinationFolder);
            }
          }
        elseif(isset($duplicateSubmit))
          {
          if(!$this->Item->policyCheck($item, $this->userSession->Dao, MIDAS_POLICY_READ))
            {
            throw new Zend_Exception('You must have read permission on an item to duplicate it');
            }
          $this->Item->duplicateItem($item, $this->userSession->Dao, $destinationFolder);
          }
        else //moveSubmit, Move item(s)
          {
          if(!$this->Item->policyCheck($item, $this->userSession->Dao, MIDAS_POLICY_ADMIN))
            {
            throw new Zend_Exception('You must own an item in order to move it');
            }
          $from = $this->_getParam('from');
          $fromFolder = $this->Folder->load($from);
          if($destinationFolder == false)
            {
            throw new Zend_Exception("Unable to load destination");
            }
          if($fromFolder == false)
            {
            throw new Zend_Exception("Unable to load move from folder");
            }
          if($destinationFolder->getKey() != $fromFolder->getKey())
{'fraction_non_alphanumeric': 0.17413809039652464, 'fraction_numerical': 0.003604769387189204, 'mean_word_length': 2.766098155238427, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 175, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '233241', 'n_tokens_mistral': 2983, 'n_tokens_neox': 2899, 'n_words': 719}