openrat-cms

# OpenRat Content Management System
git clone http://git.code.weiherhei.de/openrat-cms.git
Log | Files | Refs

ObjectAction.class.php (17989B)


      1 <?php
      2 
      3 namespace cms\action;
      4 
      5 use ArrayUtils;
      6 use cms\model\Acl;
      7 use cms\model\Project;
      8 use cms\model\User;
      9 use cms\model\Group;
     10 use cms\model\Page;
     11 use cms\model\Folder;
     12 use cms\model\BaseObject;
     13 use cms\model\Language;
     14 use cms\model\File;
     15 use cms\model\Link;
     16 use Html;
     17 use Http;
     18 use Session;
     19 
     20 
     21 /**
     22  * Basis-Action-Klasse zum Bearbeiten des Basis-Objektes.
     23  * @author Jan Dankert
     24  */
     25 
     26 class ObjectAction extends BaseAction
     27 {
     28 
     29 	public $security = Action::SECURITY_USER;
     30 	
     31 	private $objectid;
     32 
     33     /**
     34      * @var BaseObject
     35      */
     36 	protected $baseObject;
     37 
     38 	public function __construct()
     39     {
     40         parent::__construct();
     41 
     42     }
     43 
     44 
     45     public function init()
     46     {
     47 		$baseObject = new BaseObject( $this->getRequestId() );
     48 		$baseObject->objectLoad();
     49 
     50 		$this->setBaseObject( $baseObject );
     51     }
     52 
     53 
     54 	protected function setBaseObject( $baseObject ) {
     55 
     56 		$this->baseObject = $baseObject;
     57 	}
     58 
     59 
     60 	public function copyView()
     61 	{
     62 		$sourceObject = new BaseObject( $this->getRequestId());
     63 		$sourceObject->load();
     64 		
     65 		$targetFolder = new BaseObject( $this->getRequestVar('targetFolderId',OR_FILTER_NUMBER));
     66 		$targetFolder->load();
     67 		
     68 		$this->setTemplateVar('source'  ,$sourceObject->getProperties() );
     69 		$this->setTemplateVar('sourceId',$sourceObject->objectid        );
     70 		$this->setTemplateVar('target'  ,$targetFolder->getProperties() );
     71 		$this->setTemplateVar('targetId',$targetFolder->objectid        );
     72 		$this->setTemplateVar('types'   ,array('move'=>'move','moveandlink'=>'moveandlink','copy'=>'copy','link'=>'link') );
     73 		
     74 		if   ( ! $targetFolder->hasRight(Acl::ACL_WRITE) )
     75 		{
     76 			$this->addNotice('folder', $targetFolder->name, 'NOT_WRITABLE',OR_NOTICE_ERROR);
     77 		}
     78 	}
     79 	
     80 	
     81 	/**
     82 	 * Ein Fileobjekt wird in einen neuen Ordner kopiert oder verschoben.
     83 	 */
     84 	public function copyPost()
     85 	{
     86 		$type           = $this->getRequestVar('type');
     87 		$targetObjectId = $this->getRequestVar('targetid',OR_FILTER_NUMBER);
     88 		$sourceObjectId = $this->getRequestVar('sourceid',OR_FILTER_NUMBER);
     89 
     90 		$sourceObject = new BaseObject( $sourceObjectId );
     91 		$sourceObject->load();
     92 		
     93 		$targetFolder = new BaseObject( $targetObjectId );
     94 		$targetFolder->load();
     95 		
     96 		// Prüfen, ob Schreibrechte im Zielordner bestehen.
     97 		if   ( ! $targetFolder->hasRight(Acl::ACL_WRITE) )
     98 		{
     99 			$this->addNotice('folder', $targetFolder->name, 'NOT_WRITABLE',OR_NOTICE_ERROR);
    100 			return;
    101 		}
    102 		
    103 		switch( $type )
    104 		{
    105 			case 'move':
    106 				
    107 				if	( $sourceObject->isFolder )
    108 				{
    109 					$f = new Folder( $sourceObjectId );
    110 					$allsubfolders = $f->getAllSubFolderIds();
    111 				
    112 					// Plausibilisierungsprüfung:
    113 					//
    114 					// Wenn
    115 					// - Das Zielverzeichnis sich nicht in einem Unterverzeichnis des zu verschiebenen Ordners liegt
    116 					// und
    117 					// - Das Zielverzeichnis nicht der zu verschiebene Ordner ist
    118 					// dann verschieben
    119 					if	( in_array($targetObjectId,$allsubfolders) || $sourceObjectId == $targetObjectId )
    120 					{
    121 						$this->addNotice('folder',$sourceObject->name,'ERROR',OR_NOTICE_ERROR);
    122 						return;
    123 					}
    124 				}
    125 				
    126 				// TODO:
    127 				// Beim Verschieben und Kopieren muss im Zielordner die Berechtigung
    128 				// zum Erstellen von Ordner, Dateien oder Seiten vorhanden sein.
    129 				$sourceObject->setParentId( $targetObjectId );
    130 				$this->addNotice($sourceObject->type, $sourceObject->name, 'moved');
    131 				break;
    132 				
    133 			case 'moveandlink':
    134 
    135 				$oldParentId = $sourceObject->parentid;
    136 				
    137 				$sourceObject->setParentId( $targetObjectId );
    138 				$this->addNotice($sourceObject->type, $sourceObject->name, 'moved');
    139 				
    140 				$link = new Link();
    141 				$link->parentid = $oldParentId;
    142 				$link->name     = lang('LINK_TO').' '.$sourceObject->name;
    143 				$link->filename = $sourceObject->filename;
    144 				$link->linkedObjectId = $sourceObjectId;
    145 				$link->add();
    146 				$this->addNotice('link', $link->name, 'added');
    147 				
    148 				break;
    149 				
    150 			case 'copy':
    151 				
    152 				switch( $sourceObject->getType() )
    153 				{
    154 					case 'folder':
    155 						// Ordner zur Zeit nicht kopieren
    156 						// Funktion waere zu verwirrend
    157 						$this->addNotice($sourceObject->getType(),$sourceObject->name,'CANNOT_COPY_FOLDER','error');
    158 						break;
    159 							
    160 					case 'file':
    161 						$f = new File( $sourceObjectId );
    162 						$f->load();
    163 						$f->filename = '';
    164 						$f->name     = lang('COPY_OF').' '.$f->name;
    165 						$f->parentid = $targetObjectId;
    166 						$f->add();
    167 						$f->copyValueFromFile( $sourceObjectId );
    168 				
    169 						$this->addNotice($sourceObject->getType(),$sourceObject->name,'COPIED','ok');
    170 						break;
    171 				
    172 					case 'page':
    173 						$p = new Page( $sourceObjectId );
    174 						$p->load();
    175 						$p->filename = '';
    176 						$p->name     = lang('COPY_OF').' '.$p->name;
    177 						$p->parentid = $targetObjectId;
    178 						$p->add();
    179 						$p->copyValuesFromPage( $sourceObjectId );
    180 						$this->addNotice($sourceObject->getType(),$sourceObject->name,'COPIED','ok');
    181 						break;
    182 							
    183 					case 'link':
    184 						$l = new Link( $sourceObjectId );
    185 						$l->load();
    186 						$l->filename = '';
    187 						$l->name     = lang('COPY_OF').' '.$l->name;
    188 						$l->parentid = $targetObjectId;
    189 						$l->add();
    190 						$this->addNotice($sourceObject->getType(),$sourceObject->name,'COPIED','ok');
    191 						break;
    192 							
    193 					default:
    194 						throw new \LogicException('fatal: unknown type while deleting');
    195 				}
    196 				break;				
    197 				
    198 			case 'link':
    199 
    200 				// Beim Verkn�pfen muss im Zielordner die Berechtigung zum Erstellen
    201 				// von Verkn�pfungen vorhanden sein.
    202 				if   ( ! $targetFolder->hasRight(Acl::ACL_CREATE_LINK) )
    203 				{
    204 					$this->addNotice('folder', $targetFolder->name, 'NOT_WRITABLE',OR_NOTICE_ERROR);
    205 					return;
    206 				}
    207 				
    208 				$link = new Link();
    209 				$link->parentid = $targetObjectId;
    210 				$link->name     = lang('LINK_TO').' '.$sourceObject->name;
    211 				$link->filename = $sourceObject->filename;
    212 				$link->linkedObjectId = $sourceObjectId;
    213 				$link->isLinkToObject = true;
    214 				$link->add();
    215 				$this->addNotice('link', $link->name, 'added');
    216 				// OK
    217 				break;
    218 				
    219 			default:
    220 				throw new \LogicException('Unknown type for copying');
    221 				break;
    222 		}
    223 		
    224 		$targetFolder->setTimestamp();
    225 		
    226 	}
    227 
    228 	/**
    229 	  * ACL zu einem Objekt setzen
    230 	  *
    231 	  * @access public
    232 	  */
    233 	function aclformPost()
    234 	{
    235 		$acl = new Acl();
    236 
    237 		$acl->objectid = $this->getRequestId();
    238 		
    239 		// Nachschauen, ob der Benutzer ueberhaupt berechtigt ist, an
    240 		// diesem Objekt die ACLs zu aendern.
    241 		$o = new BaseObject( $acl->objectid );
    242 
    243 		if	( !$o->hasRight( Acl::ACL_GRANT ) )
    244 			throw new \SecurityException('Not allowed to insert permissions.'); // Scheiss Hacker ;)
    245 		
    246 		// Handelt es sich um eine Benutzer- oder Gruppen ACL?
    247 		switch( $this->getRequestVar('type') )
    248 		{
    249 			case 'user':
    250 				$acl->userid  = $this->getRequestVar('userid' );
    251 				
    252 				if	( $acl->userid <= 0 )
    253 				{
    254 					$this->addValidationError('type'     );
    255 					$this->addValidationError('userid','');
    256 					$this->callSubAction('aclform');
    257 					return;
    258 				}
    259 				break;
    260 			case 'group':
    261 				$acl->groupid = $this->getRequestVar('groupid');
    262 				if	( $acl->groupid <= 0 )
    263 				{
    264 					$this->addValidationError('type'      );
    265 					$this->addValidationError('groupid','');
    266 					$this->callSubAction('aclform');
    267 					return;
    268 				}
    269 				break;
    270 			case 'all':
    271 				break;
    272 			default:
    273 				$this->addValidationError('type');
    274 				$this->callSubAction('aclform');
    275 				return;
    276 		}
    277 
    278 		$acl->languageid    = $this->getRequestVar(REQ_PARAM_LANGUAGE_ID);
    279 
    280 		$acl->write         = ( $this->hasRequestVar('write'        ) );
    281 		$acl->prop          = ( $this->hasRequestVar('prop'         ) );
    282 		$acl->delete        = ( $this->hasRequestVar('delete'       ) );
    283 		$acl->release       = ( $this->hasRequestVar('release'      ) );
    284 		$acl->publish       = ( $this->hasRequestVar('publish'      ) );
    285 		$acl->create_folder = ( $this->hasRequestVar('create_folder') );
    286 		$acl->create_file   = ( $this->hasRequestVar('create_file'  ) );
    287 		$acl->create_link   = ( $this->hasRequestVar('create_link'  ) );
    288 		$acl->create_page   = ( $this->hasRequestVar('create_page'  ) );
    289 		$acl->grant         = ( $this->hasRequestVar('grant'        ) );
    290 		$acl->transmit      = ( $this->hasRequestVar('transmit'     ) );
    291 
    292 		$acl->add();
    293 
    294 		// Falls die Berechtigung vererbbar ist, dann diese sofort an
    295 		// Unterobjekte vererben.
    296 		if	( $acl->transmit )
    297 		{
    298 			$folder = new Folder( $acl->objectid );
    299 			$oids = $folder->getObjectIds();
    300 			foreach( $folder->getAllSubfolderIds() as $sfid )
    301 			{
    302 				$subfolder = new Folder( $sfid );
    303 				$oids = array_merge($oids,$subfolder->getObjectIds());
    304 			}
    305 			
    306 			foreach( $oids as $oid )
    307 			{
    308 				$acl->objectid = $oid;
    309 				$acl->add();
    310 			}
    311 		}
    312 		
    313 		
    314 		
    315 		
    316 		$this->addNotice('','','ADDED',OR_NOTICE_OK);
    317 		
    318 		$o->setTimestamp();
    319 	}
    320 
    321 
    322 
    323 	/**
    324 	 * Alle Rechte anzeigen
    325 	 */
    326 	function rightsView()
    327 	{
    328 		$o = new BaseObject( $this->getRequestId() );
    329 		$o->objectLoadRaw();
    330 		$this->setTemplateVar( 'show',$o->getRelatedAclTypes() );
    331 		$this->setTemplateVar( 'type',$o->getType() );
    332 		
    333 		$acllist = array();
    334 
    335 
    336 		foreach( $o->getAllAclIds() as $aclid )
    337 		{
    338 			$acl = new Acl( $aclid );
    339 			$acl->load();
    340 			$key = 'bu'.$acl->username.'g'.$acl->groupname.'a'.$aclid;
    341 			$acllist[$key] = $acl->getProperties();
    342 			$acllist[$key]['aclid'] = $aclid;
    343 		}
    344 		ksort( $acllist );
    345 
    346 		$this->setTemplateVar('acls',$acllist );
    347 
    348 		$this->setTemplateVars( $o->getAssocRelatedAclTypes() );
    349     }
    350 
    351 	
    352 	
    353 	/**
    354 	 * Alle Rechte anzeigen
    355 	 */
    356 	function inheritView()
    357 	{
    358 		$o = new BaseObject( $this->getRequestId() );
    359 		$o->objectLoadRaw();
    360 		$this->setTemplateVar( 'type',$o->getType() );
    361 		
    362 		$acllist = array();
    363 		$this->setTemplateVar('acls',$acllist );
    364     }
    365 
    366 	
    367 	
    368 	/**
    369 	 * 
    370 	 * @return unknown_type
    371 	 */
    372 	function inheritPost()
    373 	{
    374 		Session::close();
    375 		
    376 		$folder = new Folder( $this->getRequestId() );
    377 		$folder->load();
    378 		
    379 		if	( ! $this->hasRequestVar('inherit') )
    380 		{
    381 			$this->addNotice('folder',$folder->name,'NOTHING_DONE',OR_NOTICE_WARN);
    382 			return;
    383 		}
    384 		
    385 		
    386 		$aclids = $folder->getAllAclIds();
    387 		
    388 		$newAclList = array();
    389 		foreach( $aclids as $aclid )
    390 		{
    391 			$acl = new Acl( $aclid );
    392 			$acl->load();
    393 			if	( $acl->transmit )
    394 				$newAclList[] = $acl;
    395 		}
    396 		Logger::debug('inheriting '.count($newAclList).' acls');
    397 		
    398 		$oids = $folder->getObjectIds();
    399 		
    400 		foreach( $folder->getAllSubfolderIds() as $sfid )
    401 		{
    402 			$subfolder = new Folder( $sfid );
    403 			
    404 			$oids = array_merge($oids,$subfolder->getObjectIds());
    405 		}
    406 		
    407 		foreach( $oids as $oid )
    408 		{
    409 			$object = new BaseObject( $oid );
    410 		
    411 			// Die alten ACLs des Objektes löschen.
    412 			foreach( $object->getAllAclIds() as $aclid )
    413 			{
    414 				$acl = new Acl( $aclid );
    415 				$acl->objectid = $oid;
    416 				$acl->delete();
    417 				Logger::debug('removing acl '.$aclid.' for object '.$oid);
    418 			}
    419 			
    420 			// Vererbbare ACLs des aktuellen Ordners anwenden.
    421 			foreach( $newAclList as $newAcl )
    422 			{
    423 				$newAcl->objectid = $oid;
    424 				$newAcl->add();
    425 				Logger::debug('adding new acl '.$newAcl->aclid.' for object '.$oid);
    426 			}
    427 		}
    428 		
    429 		$this->addNotice('folder',$folder->name,'SAVED',OR_NOTICE_OK);
    430 	}
    431 
    432 
    433 	/**
    434 	 * Formular anzeigen, um Rechte hinzufuegen
    435 	 */
    436 	function aclformView()
    437 	{
    438 		$o = new BaseObject( $this->getRequestId() );
    439 		$o->objectLoadRaw();
    440 
    441 		$this->setTemplateVars( $o->getAssocRelatedAclTypes() );
    442 		$this->setTemplateVar( 'show',$o->getRelatedAclTypes() );
    443 
    444 		$this->setTemplateVar('users'    ,User::listAll()   );
    445 		$this->setTemplateVar('groups'   ,Group::getAll()   );
    446 
    447 		$languages = array(0=>lang('ALL_LANGUAGES'));
    448 
    449 		$project = new Project( $this->baseObject->projectid );
    450 
    451 		$languages += $project->getLanguages();
    452 		$this->setTemplateVar('languages',$languages       );
    453 		$this->setTemplateVar('objectid' ,$o->objectid     );
    454 		$this->setTemplateVar('action'   ,$this->request->action);
    455 	}
    456 
    457 
    458 
    459 	/**
    460 	 * Entfernen einer ACL
    461 	 * 
    462 	 * @access protected
    463 	 */
    464 	function delaclPost()
    465 	{
    466 		$acl = new Acl($this->getRequestVar('aclid'));
    467 		$acl->load();
    468 
    469 		// Nachschauen, ob der Benutzer ueberhaupt berechtigt ist, an
    470 		// diesem Objekt die ACLs zu aendern.
    471 		$o = new BaseObject( $acl->objectid );
    472 
    473 		if	( !$o->hasRight( Acl::ACL_GRANT ) )
    474 			Http::notAuthorized('no grant rights'); // Da wollte uns wohl einer vereimern.
    475 
    476 		$acl->delete(); // Weg mit der ACL
    477 		
    478 		$this->addNotice('','','DELETED',OR_NOTICE_OK);
    479 	}
    480 
    481 
    482     /**
    483      * Abspeichern der Ordner-Eigenschaften.
    484      */
    485     public function propPost()
    486     {
    487         if   ( ! $this->hasRequestVar('filename' ) )
    488             throw new \ValidationException('filename');
    489 
    490         $this->baseObject->filename = BaseObject::urlify( $this->getRequestVar('filename') );
    491         $this->baseObject->save();
    492 
    493         $alias = $this->baseObject->getAliasForLanguage(null);
    494         $alias->filename = BaseObject::urlify( $this->getRequestVar( 'alias_filename') );
    495         $alias->parentid = $this->getRequestId('alias_folderid');
    496 
    497         // If no alias, remove the alias
    498         if   ( ! $alias->filename )
    499                 $alias->delete();
    500         else
    501                 $alias->save();
    502 
    503 
    504         // Should we do this?
    505         if	( $this->hasRequestVar('creationTimestamp') && $this->userIsAdmin() )
    506             $this->baseObject->createDate = $this->getRequestVar('creationTimestamp',OR_FILTER_NUMBER);
    507         $this->baseObject->setCreationTimestamp();
    508 
    509 
    510         $this->addNotice($this->baseObject->getType(),$this->baseObject->filename,'PROP_SAVED','ok');
    511     }
    512 
    513 
    514     /**
    515      * Abspeichern der Ordner-Eigenschaften.
    516      */
    517     public function namePost()
    518     {
    519         if   ( ! $this->hasRequestVar('name' ) )
    520             throw new \ValidationException('name');
    521 
    522         $name = $this->baseObject->getNameForLanguage( $this->getRequestId('languageid'));
    523 
    524         $language = new Language( $name->languageid );
    525         $language->load();
    526 
    527         $name->name = $this->getRequestVar( 'name' );
    528         $name->description = $this->getRequestVar( 'description' );
    529 
    530         $name->save();
    531 
    532         $this->addNotice($this->baseObject->getType(),$this->baseObject->filename,'SAVED','ok');
    533     }
    534 
    535 
    536 
    537 
    538 
    539 
    540     /**
    541      * Eigenschaften anzeigen.
    542      */
    543     public function propView()
    544     {
    545         $this->setTemplateVar( 'filename', $this->baseObject->filename   );
    546         $alias = $this->baseObject->getAliasForLanguage(null );
    547         $this->setTemplateVar( 'alias_filename', $alias->filename );
    548         $this->setTemplateVar( 'alias_folderid', $alias->parentid );
    549 
    550         $project = Project::create( $this->baseObject->projectid );
    551         $this->setTemplateVar( 'folders' , $project->getAllFlatFolders() );
    552     }
    553 
    554 
    555     /**
    556      * Eigenschaften anzeigen.
    557      */
    558     public function nameView()
    559     {
    560         $name = $this->baseObject->getNameForLanguage( $this->getRequestId('languageid') );
    561 
    562         $nameProps = get_object_vars( $name );
    563 
    564         $language = new Language( $name->languageid );
    565         $language->load();
    566         $nameProps[ 'languageName'     ] = $language->name;
    567         $this->setTemplateVars($nameProps);
    568 
    569 
    570     }
    571 
    572 
    573     public function settingsView()
    574     {
    575         $this->setTemplateVar('settings',$this->baseObject->settings);
    576 
    577         $this->setTemplateVar( 'valid_from_date' ,$this->baseObject->validFromDate==null?'':date('Y-m-d',$this->baseObject->validFromDate) );
    578         $this->setTemplateVar( 'valid_from_time' ,$this->baseObject->validFromDate==null?'':date('H:i'  ,$this->baseObject->validFromDate) );
    579         $this->setTemplateVar( 'valid_until_date',$this->baseObject->validToDate  ==null?'':date('Y-m-d',$this->baseObject->validToDate  ) );
    580         $this->setTemplateVar( 'valid_until_time',$this->baseObject->validToDate  ==null?'':date('H:i'  ,$this->baseObject->validToDate  ) );
    581     }
    582 
    583     public function settingsPost()
    584     {
    585         $this->baseObject->settings  = $this->getRequestVar( 'settings');
    586 
    587         // Validate YAML-Settings
    588         try {
    589             \YAML::parse( $this->baseObject->settings);
    590         }
    591         catch( \Exception $e )
    592         {
    593             throw new \ValidationException( 'settings' );
    594         }
    595 
    596         // Gültigkeitszeiträume speichern.
    597         if  ($this->hasRequestVar( 'valid_from_date' ))
    598             $this->baseObject->validFromDate = strtotime( $this->getRequestVar( 'valid_from_date' ).' '.$this->getRequestVar( 'valid_from_time' ) );
    599         else
    600             $this->baseObject->validFromDate = null;
    601 
    602         if  ($this->hasRequestVar( 'valid_until_date'))
    603             $this->baseObject->validToDate   = strtotime( $this->getRequestVar( 'valid_until_date').' '.$this->getRequestVar( 'valid_until_time') );
    604         else
    605             $this->baseObject->validToDate = null;
    606 
    607 
    608         $this->baseObject->save();
    609 
    610         $this->addNotice($this->baseObject->getType(),$this->baseObject->filename,'SAVED',OR_NOTICE_OK);
    611     }
    612 
    613 
    614     /**
    615      * Stellt fest, ob der angemeldete Benutzer Projekt-Admin ist.
    616      * Dies ist der Fall, wenn der Benutzer PROP-Rechte im Root-Folder hat.
    617      * @return bool|int
    618      */
    619     protected function userIsProjectAdmin() {
    620 
    621 	    $project = new Project( $this->baseObject->projectid );
    622 	    $rootFolder = new Folder( $project->getRootObjectId() );
    623 
    624 	    return $rootFolder->hasRight(Acl::ACL_PROP);
    625     }
    626 
    627 
    628 	/**
    629 	 * Show infos.
    630 	 */
    631 	public function infoView()
    632 	{
    633 		$this->setTemplateVars( $this->baseObject->getProperties() );
    634 
    635 		$this->setTemplateVar( 'is_valid'     ,$this->baseObject->isValid() );
    636 		$this->setTemplateVar( 'full_filename',$this->baseObject->full_filename() );
    637 		$this->setTemplateVar( 'extension'    , '' );
    638 		$this->setTemplateVar( 'mimetype'     , $this->baseObject->mimeType() );
    639 
    640 		// Read all objects linking to us.
    641 		$pages = $this->baseObject->getDependentObjectIds();
    642 
    643 		$list = array();
    644 		foreach( $pages as $id )
    645 		{
    646 			$o = new BaseObject( $id );
    647 			$o->load();
    648 			$list[$id] = array();
    649 			$list[$id]['name'] = $o->filename;
    650 			$list[$id]['type'] = $o->getType();
    651 		}
    652 
    653 		asort( $list );
    654 
    655 		$this->setTemplateVar('pages',$list);
    656 
    657 		$this->setTemplateVar('size',number_format($this->baseObject->getSize()/1000,0,',','.').' kB' );
    658 
    659 		$pad = str_repeat("\xC2\xA0",5); // Hard spaces
    660 		$this->setTemplateVar('settings', ArrayUtils::dryFlattenArray( $this->baseObject->getTotalSettings(),$pad ) );
    661 	}
    662 
    663 }