Merge pull request #1728 from owncloud/mountconfig

Use json for new mount configuration files
This commit is contained in:
Thomas Müller 2013-02-18 14:56:25 -08:00
commit d9035d4fdc
3 changed files with 223 additions and 44 deletions

View File

@ -279,13 +279,21 @@ class OC_Mount_Config {
* @return array
*/
private static function readData($isPersonal) {
$parser = new \OC\ArrayParser();
if ($isPersonal) {
$file = OC_User::getHome(OCP\User::getUser()).'/mount.php';
$phpFile = OC_User::getHome(OCP\User::getUser()).'/mount.php';
$jsonFile = OC_User::getHome(OCP\User::getUser()).'/mount.json';
} else {
$file = OC::$SERVERROOT.'/config/mount.php';
$phpFile = OC::$SERVERROOT.'/config/mount.php';
$jsonFile = OC::$SERVERROOT.'/config/mount.json';
}
if (is_file($file)) {
$mountPoints = include $file;
if (is_file($jsonFile)) {
$mountPoints = json_decode(file_get_contents($jsonFile), true);
if (is_array($mountPoints)) {
return $mountPoints;
}
} elseif (is_file($phpFile)) {
$mountPoints = $parser->parsePHP(file_get_contents($phpFile));
if (is_array($mountPoints)) {
return $mountPoints;
}
@ -300,39 +308,11 @@ class OC_Mount_Config {
*/
private static function writeData($isPersonal, $data) {
if ($isPersonal) {
$file = OC_User::getHome(OCP\User::getUser()).'/mount.php';
$file = OC_User::getHome(OCP\User::getUser()).'/mount.json';
} else {
$file = OC::$SERVERROOT.'/config/mount.php';
$file = OC::$SERVERROOT.'/config/mount.json';
}
$content = "<?php return array (\n";
if (isset($data[self::MOUNT_TYPE_GROUP])) {
$content .= "\t'group' => array (\n";
foreach ($data[self::MOUNT_TYPE_GROUP] as $group => $mounts) {
$content .= "\t\t'".$group."' => array (\n";
foreach ($mounts as $mountPoint => $mount) {
$content .= "\t\t\t'".addcslashes($mountPoint, "'")
."' => "
.str_replace("\n", '', var_export($mount, true)).", \n";
}
$content .= "\t\t),\n";
}
$content .= "\t),\n";
}
if (isset($data[self::MOUNT_TYPE_USER])) {
$content .= "\t'user' => array (\n";
foreach ($data[self::MOUNT_TYPE_USER] as $user => $mounts) {
$content .= "\t\t'".$user."' => array (\n";
foreach ($mounts as $mountPoint => $mount) {
$content .= "\t\t\t'".addcslashes($mountPoint, "'")
."' => "
.str_replace("\n", '', var_export($mount, true)).",\n";
}
$content .= "\t\t),\n";
}
$content .= "\t),\n";
}
$content .= ");\n?>";
$content = json_encode($data, JSON_PRETTY_PRINT);
@file_put_contents($file, $content);
}

189
lib/arrayparser.php Normal file
View File

@ -0,0 +1,189 @@
<?php
/**
* @author Robin Appelman
* @copyright 2013 Robin Appelman icewind@owncloud.com
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OC;
class SyntaxException extends \Exception {
}
class ArrayParser {
const TYPE_NUM = 1;
const TYPE_BOOL = 2;
const TYPE_STRING = 3;
const TYPE_ARRAY = 4;
function parsePHP($string) {
$string = $this->stripPHPTags($string);
$string = $this->stripAssignAndReturn($string);
return $this->parse($string);
}
function stripPHPTags($string) {
$string = trim($string);
if (substr($string, 0, 5) === '<?php') {
$string = substr($string, 5);
}
if (substr($string, -2) === '?>') {
$string = substr($string, 0, -2);
}
return $string;
}
function stripAssignAndReturn($string) {
$string = trim($string);
if (substr($string, 0, 6) === 'return') {
$string = substr($string, 6);
}
if (substr($string, 0, 1) === '$') {
list(, $string) = explode('=', $string, 2);
}
return $string;
}
function parse($string) {
$string = trim($string);
$string = trim($string, ';');
switch ($this->getType($string)) {
case self::TYPE_NUM:
return $this->parseNum($string);
case self::TYPE_BOOL:
return $this->parseBool($string);
case self::TYPE_STRING:
return $this->parseString($string);
case self::TYPE_ARRAY:
return $this->parseArray($string);
}
return null;
}
function getType($string) {
$string = strtolower($string);
$first = substr($string, 0, 1);
$last = substr($string, -1, 1);
$arrayFirst = substr($string, 0, 5);
if (($first === '"' or $first === "'") and ($last === '"' or $last === "'")) {
return self::TYPE_STRING;
} elseif ($string === 'false' or $string === 'true') {
return self::TYPE_BOOL;
} elseif ($arrayFirst === 'array' and $last === ')') {
return self::TYPE_ARRAY;
} else {
return self::TYPE_NUM;
}
}
function parseString($string) {
return substr($string, 1, -1);
}
function parseNum($string) {
return intval($string);
}
function parseBool($string) {
$string = strtolower($string);
return $string === 'true';
}
function parseArray($string) {
$body = substr($string, 5);
$body = trim($body);
$body = substr($body, 1, -1);
$items = $this->splitArray($body);
$result = array();
$lastKey = -1;
foreach ($items as $item) {
$item = trim($item);
if ($item) {
if (strpos($item, '=>')) {
list($key, $value) = explode('=>', $item, 2);
$key = $this->parse($key);
$value = $this->parse($value);
} else {
$key = ++$lastKey;
$value = $item;
}
if (is_numeric($key)) {
$lastKey = $key;
}
$result[$key] = $value;
}
}
return $result;
}
function splitArray($body) {
$inSingleQuote = false;//keep track if we are inside quotes
$inDoubleQuote = false;
$bracketDepth = 0;//keep track if we are inside brackets
$parts = array();
$start = 0;
$escaped = false;//keep track if we are after an escape character
$skips = array();//keep track of the escape characters we need to remove from the result
if (substr($body, -1, 1) !== ',') {
$body .= ',';
}
for ($i = 0; $i < strlen($body); $i++) {
$char = substr($body, $i, 1);
if ($char === '\\') {
if ($escaped) {
array_unshift($skips, $i - 1);
}
$escaped = !$escaped;
} else {
if ($char === '"' and !$inSingleQuote) {
if ($escaped) {
array_unshift($skips, $i - 1);
} else {
$inDoubleQuote = !$inDoubleQuote;
}
} elseif ($char === "'" and !$inDoubleQuote) {
if ($escaped) {
array_unshift($skips, $i - 1);
} else {
$inSingleQuote = !$inSingleQuote;
}
} elseif (!$inDoubleQuote and !$inSingleQuote) {
if ($char === '(') {
$bracketDepth++;
} elseif ($char === ')') {
if ($bracketDepth <= 0) {
throw new SyntaxException;
} else {
$bracketDepth--;
}
} elseif ($bracketDepth === 0 and $char === ',') {
$part = substr($body, $start, $i - $start);
foreach ($skips as $skip) {
$part = substr($part, 0, $skip - $start) . substr($part, $skip - $start + 1);
}
$parts[] = $part;
$start = $i + 1;
$skips = array();
}
}
$escaped = false;
}
}
return $parts;
}
}

View File

@ -215,9 +215,15 @@ class Filesystem {
if ($user == '') {
$user = \OC_User::getUser();
}
$parser = new \OC\ArrayParser();
// Load system mount points
if (is_file(\OC::$SERVERROOT . '/config/mount.php')) {
$mountConfig = include 'config/mount.php';
if (is_file(\OC::$SERVERROOT . '/config/mount.php') or is_file(\OC::$SERVERROOT . '/config/mount.json')) {
if(is_file(\OC::$SERVERROOT . '/config/mount.json')){
$mountConfig = json_decode(file_get_contents(\OC::$SERVERROOT . '/config/mount.json'), true);
}elseif(is_file(\OC::$SERVERROOT . '/config/mount.php')){
$mountConfig = $parser->parsePHP(file_get_contents(\OC::$SERVERROOT . '/config/mount.php'));
}
if (isset($mountConfig['global'])) {
foreach ($mountConfig['global'] as $mountPoint => $options) {
self::mount($options['class'], $options['options'], $mountPoint);
@ -253,8 +259,12 @@ class Filesystem {
// Load personal mount points
$root = \OC_User::getHome($user);
self::mount('\OC\Files\Storage\Local', array('datadir' => $root), $user);
if (is_file($root . '/mount.php')) {
$mountConfig = include $root . '/mount.php';
if (is_file($root . '/mount.php') or is_file($root . '/mount.json')) {
if (is_file($root . '/mount.json')){
$mountConfig = json_decode(file_get_contents($root . '/mount.json'), true);
} elseif (is_file($root . '/mount.php')){
$mountConfig = $parser->parsePHP(file_get_contents($root . '/mount.php'));
}
if (isset($mountConfig['user'][$user])) {
foreach ($mountConfig['user'][$user] as $mountPoint => $options) {
self::mount($options['class'], $options['options'], $mountPoint);
@ -613,11 +623,11 @@ class Filesystem {
}
/**
* Get the owner for a file or folder
*
* @param string $path
* @return string
*/
* Get the owner for a file or folder
*
* @param string $path
* @return string
*/
public static function getOwner($path) {
return self::$defaultInstance->getOwner($path);
}