3 * Copyright (c) STMicroelectronics, 2007. All Rights Reserved.
5 * Originally written by Mohamed CHAARI, 2007
7 * This file is a part of codendi.
9 * codendi is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * codendi is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 * A class to handle mails attachments storage
30 class ForumML_FileStorage {
32 // Root directory to host mails attachments
36 * ForumML_FileStorage Constructor
38 * @param root: The ForumML attachments directory
40 function __construct($root) {
45 * Store - stores attached files in the ForumML root dir
47 * @param filename: name of attached file
48 * @param content: content of attached file
49 * @param list: mailing-list name
50 * @param date: date of attachment in YYYY_MM_DD format
51 * @param encod: encoding of attachment
53 * @return int size of attached file
55 function store($filename, $content, $list, $date, $encod="") {
56 $path = $this->_getPath($filename, $list, $date, "store");
57 $ret = file_put_contents($path, $content);
67 * +---------------------------------------------------------------------------------+
68 * | +-----------------------------------------+ |
70 * _|__ _______ _|__ | |
71 * name list_id date v v
72 * Attach.doc 7 2007_10_19 => foruuml_dir/<listname>/2007_10_19/Attach_doc
74 * +---------------------------------------------+|
77 * Upload (to temporary location):
78 * +-----------------------------------------------------------------------+
83 * Attach.doc => foruuml_dir/upload/Attach_doc
88 * _getPath - Get the absolute path where to Upload/Store attached file
90 * @param name: basename of attached file
91 * @param list: mailing-list name
92 * @param date: attachment date (YYYY_MM_DD)
93 * @param string type: upload/store
97 function _getPath($name, $list, $date, $type) {
100 if (trim($name) == '') {
101 $name = 'attachment';
103 $name = mb_convert_encoding($name, 'ascii', 'utf-8');
104 // restrict file name to 64 characters (maximum)
105 if (strlen($name) > 64) {
106 $name = substr($name, 0, 64);
109 $name = preg_replace('`[^a-z0-9_-]`i', '_', $name);
110 $name = preg_replace('`_{2,}`', '_', $name);
113 if ($type == "upload") {
114 $path_elements = array($this->root, $type);
115 } elseif ($type == "store") {
116 $path_elements = array($this->root, $list, $date);
120 foreach($path_elements as $elem) {
122 if (!is_dir($path)) {
127 // Ensure that same file doesn't exists yet
130 while($this->fileExists($path.$name.$ext)) {
135 return $path.$name.$ext;
138 function fileExists($path) {
139 return is_file($path);