1: <?php
2: /**
3: * Functions to edit files.
4: * Included in Area style,
5: * js, htmltpl in Frame right_bottom.
6: *
7: * Contains also common file and directory related functions
8: *
9: * TODO: merge with cFileHandler and cDirHandler
10: *
11: * @package Core
12: * @subpackage Backend
13: * @version SVN Revision $Rev:$
14: *
15: * @author Willi Man, Timo Trautmann
16: * @copyright four for business AG <www.4fb.de>
17: * @license http://www.contenido.org/license/LIZENZ.txt
18: * @link http://www.4fb.de
19: * @link http://www.contenido.org
20: */
21: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
22:
23: /**
24: * Function removes file meta information from database (used when a file is
25: * deleted)
26: *
27: * @param int $iIdClient - id of client which contains this file
28: * @param string $sFilename - name of corresponding file
29: * @param string $sType - type of file (css, js or templates)
30: * @param cDb $oDb - CONTENIDO database object
31: */
32: function removeFileInformation($iIdClient, $sFilename, $sType, $oDb) {
33: global $cfg;
34:
35: if (!isset($oDb) || !is_object($oDb)) {
36: $oDb = cRegistry::getDb();
37: }
38:
39: $iIdClient = cSecurity::toInteger($iIdClient);
40: $sFilename = cSecurity::filter((string) $sFilename, $oDb);
41: $sType = cSecurity::filter((string) $sType, $oDb);
42:
43: $sSql = "DELETE FROM `" . $cfg["tab"]["file_information"] . "` WHERE idclient=$iIdClient AND
44: filename='$sFilename' AND
45: type='$sType';";
46: $oDb->query($sSql);
47: $oDb->free();
48: }
49:
50: /**
51: * Function returns file meta information from database (used when files were
52: * versionned or description is displayed)
53: *
54: * @param int $iIdClient - id of client which contains this file
55: * @param string $sFilename - name of corresponding file
56: * @param string $sType - type of file (css, js or templates)
57: * @param cDb $oDb - CONTENIDO database object
58: * @return array Indexes:
59: * idsfi - Primary key of database record
60: * created - Datetime when file was created
61: * lastmodified - Datetime when file was last modified
62: * author - Author of file (CONTENIDO Backend User)
63: * modifiedby - Last modifier of file (CONTENIDO Backend User)
64: * description - Description which was inserted for this file
65: *
66: */
67: function getFileInformation($iIdClient, $sFilename, $sType, $oDb) {
68: global $cfg;
69:
70: if (!isset($oDb) || !is_object($oDb)) {
71: $oDb = cRegistry::getDb();
72: }
73:
74: $iIdClient = cSecurity::toInteger($iIdClient);
75: $sFilename = cSecurity::filter((string) $sFilename, $oDb);
76: $sType = cSecurity::filter((string) $sType, $oDb);
77:
78: $aFileInformation = array();
79: $sSql = "SELECT * FROM `" . $cfg["tab"]["file_information"] . "` WHERE idclient=$iIdClient AND
80: filename='$sFilename' AND
81: type='$sType';";
82: $oDb->query($sSql);
83: if ($oDb->numRows() > 0) {
84: $oDb->nextRecord();
85: $aFileInformation['idsfi'] = $oDb->f('idsfi');
86: $aFileInformation['created'] = $oDb->f('created');
87: $aFileInformation['lastmodified'] = $oDb->f('lastmodified');
88: $aFileInformation['author'] = cSecurity::unFilter($oDb->f('author'));
89: $aFileInformation['modifiedby'] = $oDb->f('modifiedby');
90: $aFileInformation['description'] = cSecurity::unFilter($oDb->f('description'));
91: }
92: $oDb->free();
93:
94: return $aFileInformation;
95: }
96:
97: /**
98: * Function updates file meta information (used when files were created or
99: * edited).
100: * It creates new database record for file meta informations if database record
101: * does
102: * not exist. Otherwise, existing record will be updated
103: *
104: * @param int $iIdClient - id of client which contains this file
105: * @param string $sFilename - name of corresponding file
106: * @param string $sType - type of file (css, js or templates)
107: * @param string $sAuthor - author of file
108: * @param string $sDescription - description of file
109: * @param cDb $oDb - CONTENIDO database object
110: * @param string $sFilenameNew - new filename if filename was changed (optional)
111: */
112: function updateFileInformation($iIdClient, $sFilename, $sType, $sAuthor, $sDescription, $oDb, $sFilenameNew = '') {
113: global $cfg;
114:
115: if (!isset($oDb) || !is_object($oDb)) {
116: $oDb = cRegistry::getDb();
117: }
118:
119: if ($sFilenameNew == '') {
120: $sFilenameNew = $sFilename;
121: }
122:
123: $iIdClient = cSecurity::toInteger($iIdClient);
124: $sFilename = cSecurity::filter((string) $sFilename, $oDb);
125: $sType = cSecurity::filter((string) $sType, $oDb);
126: $sDescription = cSecurity::filter((string) stripslashes($sDescription), $oDb);
127: $sAuthor = cSecurity::filter((string) $sAuthor, $oDb);
128:
129: $sSql = "SELECT * from `" . $cfg["tab"]["file_information"] . "` WHERE idclient=$iIdClient AND
130: filename='$sFilename' AND
131: type='$sType';";
132: $oDb->query($sSql);
133: if ($oDb->numRows() == 0) {
134: // $iNextId = $oDb->nextid('con_style_file_information');
135: $sSql = "INSERT INTO `" . $cfg["tab"]["file_information"] . "` (
136: `idclient` ,
137: `type` ,
138: `filename` ,
139: `created` ,
140: `lastmodified` ,
141: `author` ,
142: `modifiedby` ,
143: `description` )
144: VALUES (
145: $iIdClient,
146: '$sType',
147: '$sFilenameNew',
148: NOW(),
149: '0000-00-00 00:00:00',
150: '$sAuthor',
151: '',
152: '$sDescription'
153: );";
154: } else {
155: $sSql = "UPDATE `" . $cfg["tab"]["file_information"] . "` SET `lastmodified` = NOW(),
156: `modifiedby` = '$sAuthor',
157: `description` = '$sDescription',
158: `filename` = '$sFilenameNew'
159:
160: WHERE idclient=$iIdClient AND
161: filename='$sFilename' AND
162: type='$sType';";
163: }
164:
165: $oDb->free();
166: $oDb->query($sSql);
167: $oDb->free();
168: }
169:
170: /**
171: * Returns the filetype (extension).
172: *
173: * @param string $filename The file to get the type
174: * @return string Filetype
175: */
176: function getFileType($filename) {
177: return cFileHandler::getExtension($filename);
178: }
179:
180: /**
181: * Returns the size of a directory.
182: * AKA the combined filesizes of all files within it.
183: * Note that this function uses filesize(). There could be problems with files
184: * that are larger than 2GiB
185: *
186: * @param string The directory
187: * @param bool true if all the subdirectories should be included in the
188: * calculation
189: * @return bool int false in case of an error or the size
190: */
191: function getDirectorySize($sDirectory, $bRecursive = false) {
192: $ret = 0;
193: $files = scanDirectory($sDirectory, $bRecursive);
194: if ($files === false) {
195: return false;
196: }
197:
198: foreach ($files as $file) {
199: $temp = cFileHandler::info($file);
200: $ret += $temp['size'];
201: }
202:
203: return $ret;
204: }
205:
206: /**
207: * Scans passed directory and collects all found files
208: *
209: * @param string $sDirectory
210: * @param bool $bRecursive
211: * @return bool array of found files (full path and name) or false
212: */
213: function scanDirectory($sDirectory, $bRecursive = false) {
214: if (substr($sDirectory, strlen($sDirectory) - 1, 1) == '/') {
215: $sDirectory = substr($sDirectory, 0, strlen($sDirectory) - 1);
216: }
217:
218: if (!is_dir($sDirectory)) {
219: return false;
220: }
221:
222: $aFiles = array();
223: $openDirs = array();
224: $closedDirs = array();
225: array_push($openDirs, $sDirectory);
226:
227: while (count(($openDirs)) >= 1) {
228: $sDirectory = array_pop($openDirs);
229: if (is_dir($sDirectory)) {
230: if (($hDirHandle = opendir($sDirectory)) !== false) {
231: while (($sFile = readdir($hDirHandle)) !== false) {
232: if ($sFile != '.' && $sFile != '..') {
233: $sFullpathFile = $sDirectory . '/' . $sFile;
234: if (is_file($sFullpathFile) && cFileHandler::readable($sFullpathFile)) {
235: array_push($aFiles, $sFullpathFile);
236: } elseif (is_dir($sFullpathFile) && $bRecursive == true) {
237: if (!in_array($sFullpathFile, $closedDirs)) {
238: array_push($openDirs, $sFullpathFile);
239: }
240: }
241: }
242: }
243: closedir($hDirHandle);
244: }
245: }
246: array_push($closedDirs, $sDirectory);
247: }
248:
249: return $aFiles;
250: }
251:
252: /**
253: * Copies source directory to destination directory.
254: *
255: * @param string $sourcePath
256: * @param string $destinationPath
257: * @param int $mode Octal representation of file mode (0644, 0750, etc.)
258: * @param array $options Some additional options as follows
259: * <pre>
260: * $options['force_overwrite'] (bool) Flag to overwrite existing
261: * destination file, default value is false
262: * </pre>
263: */
264: function recursiveCopy($sourcePath, $destinationPath, $mode = 0777, array $options = array()) {
265: if (!is_dir($destinationPath)) {
266: mkdir($destinationPath, $mode);
267: }
268:
269: $forceOverwrite = (isset($options['force_overwrite']))? (bool) $options['force_overwrite'] : false;
270: $oldPath = getcwd();
271:
272: if (is_dir($sourcePath)) {
273: chdir($sourcePath);
274: $myhandle = opendir('.');
275:
276: while (($file = readdir($myhandle)) !== false) {
277: if ($file != '.' && $file != '..') {
278: if (is_dir($file)) {
279: // Copy directory
280: recursiveCopy($sourcePath . $file . '/', $destinationPath . $file . '/', $mode, $options);
281: chdir($sourcePath);
282: } elseif (cFileHandler::exists($sourcePath . $file)) {
283: // Copy file
284: if (cFileHandler::exists($destinationPath . $file)) {
285: if ($forceOverwrite) {
286: copy($sourcePath . $file, $destinationPath . $file);
287: }
288: } else {
289: copy($sourcePath . $file, $destinationPath . $file);
290: }
291: }
292: }
293: }
294: closedir($myhandle);
295: }
296:
297: chdir($oldPath);
298: }
299: