3 /* Copyright (C) 2009 Winch Gate Property Limited
5 * This file is part of ryzom_api.
6 * ryzom_api is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * ryzom_api is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with ryzom_api. If not, see <http://www.gnu.org/licenses/>.
20 function getDirLinks($url_params, $path, $getvar, $home) {
22 $dirs = explode('/', $path);
24 $ret .= _l($home, $url_params, array($getvar => ''));
25 foreach($dirs as $dirname) {
27 $ret .= ' » '._l($dirname, $url_params, array($getvar => '/'.$dirpath.$dirname));
28 $dirpath .= $dirname.'/';
34 function isEmptyDir($dir)
36 if (($files = scandir($dir)) && count($files) <= 2) {
42 class ryDataFileManager
{
50 function __construct($id, $app_name=APP_NAME
) {
51 $this->app_name
= $app_name;
52 $id = (strlen($id) == 0?
'0':'').$id;
53 $id = (strlen($id) == 1?
'0':'').$id;
55 $this->log_dir
= RYAPP_PATH
.$app_name.'/data/logs/';
56 $this->data_dir
= RYAPP_PATH
.$app_name.'/data/app/';
57 $this->user_dir
= RYAPP_PATH
.$app_name.'/data/chars/'.$id[0].'/'.$id[1].'/'.$id.'/';
59 if (!is_dir($this->user_dir
))
60 @mkdir
($this->user_dir
, 0777, true);
62 if (!is_dir($this->log_dir
)) {
63 @mkdir
($this->log_dir
, 0777, true);
64 @mkdir
($this->data_dir
, 0777, true);
68 /*** Generic datafiles access methods ***/
70 function getData($name, $default=null) {
71 if (file_exists($name))
72 return unserialize(file_get_contents($name));
73 if ($default !== NULL) {
74 @file_put_contents
($name, serialize($default));
80 function saveData($name, $datas, $create_folders=true) {
81 if ($create_folders) {
82 if (!is_dir(dirname($name)))
83 @mkdir
(dirname($name), 0777, true);
86 @file_put_contents
($name, serialize($datas));
91 function listDataFiles($dir) {
93 if ($handle = @opendir
($dir)) {
94 while (false !== ($file = readdir($handle))) {
95 if ($file != '.' && $file != '..' && $file[0] != '.')
104 function loadAppData($name, $default=null) {
105 return $this->getData($this->data_dir
.$name, $default);
108 function saveAppData($name, $datas, $create_folders=true) {
109 return $this->saveData($this->data_dir
.$name, $datas, $create_folders);
112 function listAppDataFiles($basedir='') {
113 return $this->listDataFiles($this->data_dir
.$basedir);
119 function loadUserData($name, $default=null) {
120 return $this->getData($this->user_dir
.$name, $default);
123 function saveUserData($name, $datas, $create_folders=true) {
124 return $this->saveData($this->user_dir
.$name, $datas, $create_folders);
127 function listUserDataFiles($basedir='') {
128 return $this->listDataFiles($this->user_dir
.$basedir);
131 function loadUserDataFromApp($name, $app, $default=null) {
133 $file = RYAPP_PATH
.$app.'/data/chars/'.$id[0].'/'.$id[1].'/'.$id.'/'.$name;
134 if (file_exists($file))
135 return unserialize(file_get_contents($file));
136 if ($default !== null)
141 function saveUserDataFromApp($name, $app, $datas) {
143 $dir = RYAPP_PATH
.$app.'/data/chars/'.$id[0].'/'.$id[1].'/'.$id.'/';
145 @mkdir
($dir, 0777, true);
146 file_put_contents($dir.$name, serialize($datas));