1 <?php 
declare(strict_types
=1); 
   4  * This file is part of the Rapsys TreeBundle package. 
   6  * (c) Raphaël Gertz <symfony@rapsys.eu> 
   8  * For the full copyright and license information, please view the LICENSE 
   9  * file that was distributed with this source code. 
  12 namespace Rapsys\TreeBundle\Repository
; 
  14 use Doctrine\ORM\Query\ResultSetMapping
; 
  16 use Rapsys\TreeBundle\Repository
; 
  21 class ElementRepository 
extends Repository 
{ 
  23          * Find element as array 
  25          * @param ?integer $uid The user id 
  26          * @param integer $id The element id 
  27          * @param string $path The element path 
  28          * @return ?array The element array 
  30         public function findOneByUidIdPathAsArray(?int $uid, int $id, string $path): ?array { 
  36         GREATEST(a.created, e.created) AS created, 
  37         GREATEST(a.updated, e.updated) AS updated, 
  38         GREATEST(a.created, e.created, a.updated, e.updated) AS modified, 
  42 FROM Rapsys\TreeBundle\Entity\Element AS e 
  43 JOIN Rapsys\TreeBundle\Entity\Album AS a ON (a.id = e.album_id) 
  44 WHERE e.id = :id AND e.path = SUBSTR(:path, 1, LENGTH(e.path)) AND e.user_id IN (NULL, :uid) 
  47                 //Replace bundle entity name by table name 
  48                 $req = $this->replace($req); 
  50                 //Get result set mapping instance 
  51                 $rsm = new ResultSetMapping(); 
  54                 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php 
  55                 //addScalarResult($sqlColName, $resColName, $type = 'string'); 
  56                 $rsm->addScalarResult('id', 'id', 'integer') 
  57                         ->addScalarResult('path', 'path', 'string') 
  58                         ->addScalarResult('created', 'created', 'datetime') 
  59                         ->addScalarResult('updated', 'updated', 'datetime') 
  60                         ->addScalarResult('modified', 'modified', 'datetime') 
  61                         ->addScalarResult('a_id', 'a_id', 'integer') 
  62                         ->addScalarResult('a_path', 'a_path', 'string') 
  63                         ->addScalarResult('a_slug', 'a_slug', 'string') 
  64                         ->addIndexByScalar('id'); 
  68                         ->createNativeQuery($req, $rsm) 
  69                         ->setParameter('uid', $uid) 
  70                         ->setParameter('id', $id) 
  71                         ->setParameter('path', $path) 
  72                         ->getOneOrNullResult(); 
  79                         !($realpath = realpath($result['a_path'].($path?DIRECTORY_SEPARATOR
.$path:''))) || 
  80                         //Realpath not matching element path 
  81                         $result['a_path'].($result['path']?DIRECTORY_SEPARATOR
.$result['path']:'') !== substr($realpath, 0, strlen($result['a_path'].($result['path']?DIRECTORY_SEPARATOR
.$result['path']:''))) 
  91                                 'name' => ucfirst($result['a_slug']), 
  92                                 'link' => $alink = $this->router
->generate('rapsystree_album', [ 'id' => $result['a_id'], 'path' => '', 'slug' => $result['a_slug'] ]) 
  96                                 'name' => $name = trim(str_replace('/', ' / ', '/'.$result['path'])), 
  97                                 'link' => $link = $this->router
->generate('rapsystree_element', [ 'id' => $result['id'], 'path' => $result['path'] ]) 
 102                 $base = $result['path']; 
 104                 //Iterate on intermediate breadcrumbs 
 105                 foreach(array_slice(explode('/', substr($realpath, strlen($result['a_path'].($result['path']?DIRECTORY_SEPARATOR
.$result['path']:'')))), 1) as $value) { 
 108                                 'name' => ($base == '' ? '' : '/ ').$value, 
 109                                 'link' => $this->router
->generate('rapsystree_element', [ 'id' => $result['id'], 'path' => ($base .= ($base == '' ? '' : '/').$value) ]) 
 123                 if (is_dir($realpath)) { 
 124                         //Iterate on directory 
 125                         foreach(array_diff(scandir($realpath), ['.', '..']) as $item) { 
 126                                 //TODO: exclude .svn, .git, .passwd, .*.un~, etc... (if not already protected by haproxy/apache) 
 130                                         //Without item realpath 
 131                                         !($itempath = realpath($realpath.DIRECTORY_SEPARATOR
.$item)) || 
 132                                         //Item realpath not matching element path 
 133                                         $result['a_path'].($result['path']?DIRECTORY_SEPARATOR
.$result['path']:'') !== substr($itempath, 0, strlen($result['a_path'].($result['path']?DIRECTORY_SEPARATOR
.$result['path']:''))) 
 140                                 if (is_dir($itempath)) { 
 142                                         $directories[$item.'/'] = $this->router
->generate('rapsystree_element', [ 'id' => $result['id'], 'path' => $path.'/'.$item ]); 
 144                                 } elseif (is_file($itempath)) { 
 146                                         $fileinfos = $this->file
->infos($itempath); 
 149                                         $files[$fileinfos['name']] = [ 
 151                                                 'link' => $this->router
->generate('rapsystree_element', [ 'id' => $result['id'], 'path' => $path.'/'.$item ]) 
 156                                         throw new \
Exception('Unknown element item type'); 
 160                 } elseif (is_file($realpath)) { 
 162                         $fileinfos = $this->file
->infos($realpath); 
 167                                 'link' => $this->router
->generate('rapsystree_element', [ 'id' => $result['id'], 'path' => $path ]) 
 172                         throw new \
Exception('Unknown element type'); 
 177                         'id' => $result['id'], 
 179                         'path' => $result['path'], 
 180                         'realpath' => $realpath, 
 182                         'created' => $result['created'], 
 183                         'updated' => $result['updated'], 
 184                         'modified' => $result['modified'], 
 186                                 'id' => $result['a_id'], 
 187                                 'path' => $result['a_path'], 
 188                                 'slug' => $result['a_slug'], 
 191                         'breadcrumbs' => $breadcrumbs, 
 192                         'directories' => $directories,