caldav_SessionFile::saveFile()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
ccs 0
cts 6
cp 0
crap 2
1
<?php
2
//-------------------------------------------------------------------------
3
// OVIDENTIA http://www.ovidentia.org
4
// Ovidentia is free software; you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation; either version 2, or (at your option)
7
// any later version.
8
//
9
// This program is distributed in the hope that it will be useful, but
10
// WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
// See the GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with this program; if not, write to the Free Software
16
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17
// USA.
18
//-------------------------------------------------------------------------
19
/**
20
 * @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
21
 * @copyright Copyright (c) 2010 by CANTICO ({@link http://www.cantico.fr})
22
 */
23
require_once $GLOBALS['babInstallPath'].'utilit/path.class.php';
24
25
/**
26
 * Save file in a temporary session folder
27
 */
28
function caldav_SessionFile($filename, $contents)
29
{
30
	return new caldav_SessionFile($filename, $contents);
31
}
32
33
34
/**
35
 * Save file in a temporary session folder
36
 */
37
class caldav_SessionFile
38
{
39
	static $cleanup = null;
40
	
41
	public function __construct($filename, $contents)
42
	{
43
		$this->cleanup();
44
		$this->saveFile($filename, $contents);
45
	}
46
	
47
	
48
	/**
49
	 * Cleanup session dir
50
	 * @return unknown_type
51
	 */
52
	private function cleanup()
53
	{
54
		if (isset(self::$cleanup))
55
		{
56
			return;
57
		}
58
		
59
		$sessions = array();
60
		foreach(bab_getActiveSessions() as $arr)
61
		{
62
			$sessions[$arr['session_id']] = 1;
63
		}
64
		
65
		$addon = bab_getAddonInfosInstance('LibCaldav');
66
		$session_folders = new bab_Path($addon->getUploadPath(), 'tmp');
67
		
68
		foreach($session_folders as $path)
69
		{
70
			if (!isset($sessions[$path->getBasename()]))
71
			{
72
				$path->deleteDir();
73
			}
74
		}
75
		
76
		self::$cleanup = true;
77
	}
78
	
79
	/**
80
	 * 
81
	 * @param string $filename
82
	 * @param string $contents
83
	 * @return int
84
	 */
85
	private function saveFile($filename, $contents)
86
	{
87
		
88
		$addon = bab_getAddonInfosInstance('LibCaldav');
89
		$path = new bab_Path($addon->getUploadPath(), 'tmp', session_id());
90
		$path->createDir();
91
		$path->push($filename);
92
		
93
		return file_put_contents($path->toString(), $contents);
94
	}
95
}