Ejemplo n.º 1
0
 /**
  * clean up $filename and avoid doublettes within folder $dir
  *
  * @param string $filename
  * @param FilesystemFolder $dir
  * @param integer $starting_index used in renamed file
  * @return string
  */
 public static function sanitizeFilename($filename, FilesystemFolder $dir, $ndx = 2)
 {
     $filename = str_replace(array(' ', 'ä', 'ö', 'ü', 'Ä', 'Ö', 'Ü', 'ß'), array('_', 'ae', 'oe', 'ue', 'Ae', 'Oe', 'Ue', 'ss'), $filename);
     $filename = preg_replace('/[^0-9a-z_#,;\\-\\.\\(\\)]/i', '_', $filename);
     if (!file_exists($dir->getPath() . $filename)) {
         return $filename;
     }
     $pathinfo = pathinfo($filename);
     $pathinfo['extension'] = !empty($pathinfo['extension']) ? '.' . $pathinfo['extension'] : '';
     while (file_exists($dir->getPath() . sprintf('%s(%d)%s', $pathinfo['filename'], $ndx, $pathinfo['extension']))) {
         ++$ndx;
     }
     return sprintf('%s(%d)%s', $pathinfo['filename'], $ndx, $pathinfo['extension']);
 }
Ejemplo n.º 2
0
 /**
  * creates metafolder from supplied filesystem folder
  * nested set is updated accordingly
  *
  * @param FilesystemFolder $f
  * @param array $metaData optional data for folder
  * @throws MetaFolderException
  */
 public static function create(FilesystemFolder $f, array $metaData = [])
 {
     if ($parentFolder = $f->getParentFolder()) {
         $parentFolder->createMetaFolder();
     }
     try {
         self::getInstance($f->getPath());
     } catch (MetaFolderException $e) {
         $db = Application::getInstance()->getDb();
         if (strpos($f->getPath(), Application::getInstance()->getAbsoluteAssetsPath()) === 0) {
             $metaData['Path'] = trim(substr($f->getPath(), strlen(Application::getInstance()->getAbsoluteAssetsPath())), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
         } else {
             $metaData['Path'] = rtrim($f->getPath(), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
         }
         $metaData['Alias'] = strtolower(preg_replace('~[\\\\/]~', '_', rtrim($metaData['Path'], DIRECTORY_SEPARATOR)));
         if (!isset($metaData['Access']) || !preg_match('~^rw?$~i', $metaData['Access'])) {
             $metaData['Access'] = 'RW';
         } else {
             $metaData['Access'] = strtoupper($metaData['Access']);
         }
         $tree = explode(DIRECTORY_SEPARATOR, trim($metaData['Path'], DIRECTORY_SEPARATOR));
         if (count($tree) == 1) {
             //no parent
             $rows = $db->doPreparedQuery('SELECT MAX(r) + 1 AS l FROM folders');
             $metaData['l'] = !isset($rows[0]['l']) ? 0 : $rows[0]['l'];
             $metaData['r'] = $rows[0]['l'] + 1;
             $metaData['level'] = 0;
         } else {
             array_pop($tree);
             try {
                 $parent = self::getInstance(implode(DIRECTORY_SEPARATOR, $tree) . DIRECTORY_SEPARATOR);
                 // has parent directory
                 $rows = $db->doPreparedQuery("SELECT r, l, level FROM folders WHERE foldersID = ?", [$parent->getId()]);
                 $db->execute('UPDATE folders SET r = r + 2 WHERE r >= ?', [(int) $rows[0]['r']]);
                 $db->execute('UPDATE folders SET l = l + 2 WHERE l > ?', [(int) $rows[0]['r']]);
                 $metaData['l'] = $rows[0]['r'];
                 $metaData['r'] = $rows[0]['r'] + 1;
                 $metaData['level'] = $rows[0]['level'] + 1;
             } catch (MetaFolderException $e) {
                 // no parent directory
                 $rows = $db->doPreparedQuery('SELECT MAX(r) + 1 AS l FROM folders');
                 $metaData['l'] = !isset($rows[0]['l']) ? 0 : $rows[0]['l'];
                 $metaData['r'] = $rows[0]['l'] + 1;
                 $metaData['level'] = 0;
             }
         }
         $id = $db->insertRecord('folders', $metaData);
         // refresh nesting for all active metafolder instances
         foreach (array_keys(self::$instancesById) as $id) {
             self::getInstance(NULL, $id)->refreshNesting();
         }
         return self::getInstance($f->getPath());
     }
     throw new MetaFolderException('Metafolder for ' . $f->getPath() . ' already exists.', MetaFolderException::METAFOLDER_ALREADY_EXISTS);
 }