PageRenderTime 66ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/include/class.datei.php

https://bitbucket.org/icarito/pmc
PHP | 700 lines | 424 code | 55 blank | 221 comment | 35 complexity | 6bd2767650b42f4da2437d2eeea87bbd MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * class datei (file) provides methods to handle files and folders
  4. *
  5. * @author Open Dynamics / Philipp Kiszka <info@o-dyn.de>
  6. * @name datei
  7. * @version 0.5.5
  8. * @package Collabtive
  9. * @link http://www.o-dyn.de
  10. * @license http://opensource.org/licenses/gpl-license.php GNU General Public License v3 or later
  11. */
  12. class datei
  13. {
  14. /**
  15. * Constructor
  16. * Initialize the event log
  17. */
  18. function __construct()
  19. {
  20. $this->mylog = new mylog;
  21. }
  22. // FOLDER METHODS
  23. /**
  24. * Create a new folder
  25. *
  26. * @param int $project Project ID the folder belongs to
  27. * @param string $folder Name of the new folder
  28. * @param string $desc Description of the folder
  29. * @return bool
  30. */
  31. function addFolder($parent, $project, $folder, $desc, $visible = "")
  32. {
  33. $project = (int) $project;
  34. $folder = mysql_real_escape_string($folder);
  35. $desc = mysql_real_escape_string($desc);
  36. $folder = str_replace("ä", "ae" , $folder);
  37. $folder = str_replace("ö", "oe" , $folder);
  38. $folder = str_replace("ü", "ue" , $folder);
  39. $folder = str_replace("ß", "ss" , $folder);
  40. // remove whitespace
  41. $folder = preg_replace("/\W/", "", $folder);
  42. $folder = preg_replace("/[^-_0-9a-zA-Z]/", "_", $folder);
  43. // insert the folder into the db
  44. $ins = mysql_query("INSERT INTO projectfolders (parent,project,name,description,visible) VALUES ($parent,$project,'$folder','$desc','$visstr')");
  45. if ($ins)
  46. {
  47. // create the folder
  48. $makefolder = CL_ROOT . "/files/" . CL_CONFIG . "/$project/$folder/";
  49. if (!file_exists($makefolder))
  50. {
  51. if (mkdir($makefolder, 0777))
  52. {
  53. // folder created return true
  54. return true;
  55. }
  56. }
  57. else
  58. {
  59. // folder already existed, return false
  60. return false;
  61. }
  62. }
  63. else
  64. {
  65. // folder wasnt created , return false
  66. return false;
  67. }
  68. }
  69. /**
  70. * Delete a folder
  71. * Deletes the given folder with all files in it and all of its subfolders.
  72. *
  73. * @param int $id folder id
  74. * @param int $id project id
  75. * @return bool
  76. */
  77. function deleteFolder($id, $project)
  78. {
  79. $id = (int) $id;
  80. $folder = $this->getFolder($id);
  81. $files = $this->getProjectFiles($project, 10000, $id);
  82. // delete all the files in the folder from the database (and filesystem as well)
  83. foreach($files as $file)
  84. {
  85. $this->loeschen($file["ID"]);
  86. }
  87. if (!empty($folder["subfolders"]))
  88. {
  89. foreach($folder["subfolders"] as $sub)
  90. {
  91. $this->deleteFolder($sub["ID"], $sub["project"]);
  92. }
  93. }
  94. $del = mysql_query("DELETE FROM projectfolders WHERE ID = $id");
  95. // remove directory
  96. $foldstr = CL_ROOT . "/files/" . CL_CONFIG . "/$project/" . $folder["name"] . "/";
  97. delete_directory($foldstr);
  98. return true;
  99. }
  100. /**
  101. * Get directory
  102. *
  103. * @param string $id Directory
  104. * @return array $files Found files and directories
  105. */
  106. function getFolder($id)
  107. {
  108. $id = (int) $id;
  109. $sel = mysql_query("SELECT * FROM projectfolders WHERE ID = $id LIMIT 1");
  110. $folder = mysql_fetch_array($sel);
  111. $folder["subfolders"] = $this->getSubFolders($folder["ID"]);
  112. return $folder;
  113. }
  114. /**
  115. * Recursively get subdirectories of a given directory
  116. *
  117. * @param int $id Directory
  118. * @return array $files Found files and directories
  119. */
  120. function getSubFolders($parent)
  121. {
  122. $parent = (int) $parent;
  123. $sel = mysql_query("SELECT * FROM projectfolders WHERE parent = $parent");
  124. $folders = array();
  125. while ($folder = mysql_fetch_array($sel))
  126. {
  127. $folder["subfolders"] = $this->getSubFolders($folder["ID"]);
  128. array_push($folders, $folder);
  129. }
  130. if (!empty($folders))
  131. {
  132. return $folders;
  133. }
  134. else
  135. {
  136. return false;
  137. }
  138. }
  139. /**
  140. * Get all the directories in a project
  141. *
  142. * @param string $id project Project ID
  143. * @return array $files Found files and directories
  144. */
  145. function getProjectFolders($project, $parent = 0)
  146. {
  147. $project = (int) $project;
  148. $sel = mysql_query("SELECT * FROM projectfolders WHERE project = $project AND parent = $parent");
  149. $folders = array();
  150. while ($folder = mysql_fetch_array($sel))
  151. {
  152. $folder["subfolders"] = $this->getSubFolders($folder["ID"]);
  153. array_push($folders, $folder);
  154. }
  155. if (!empty($folders))
  156. {
  157. return $folders;
  158. }
  159. else
  160. {
  161. return false;
  162. }
  163. }
  164. /**
  165. * Get all the directories in a project
  166. *
  167. * @param string $id project Project ID
  168. * @return array $files Found files and directories
  169. */
  170. function getAllProjectFolders($project)
  171. {
  172. $project = (int) $project;
  173. $sel = mysql_query("SELECT * FROM projectfolders WHERE project = $project");
  174. $folders = array();
  175. while ($folder = mysql_fetch_array($sel))
  176. {
  177. $folder["subfolders"] = $this->getSubFolders($folder["ID"]);
  178. array_push($folders, $folder);
  179. }
  180. if (!empty($folders))
  181. {
  182. return $folders;
  183. }
  184. else
  185. {
  186. return false;
  187. }
  188. }
  189. // FILE METHODS
  190. /**
  191. * Upload a file
  192. * Does filename sanitizing as well as MIME-type determination
  193. * Also adds the file to the database using add_file()
  194. *
  195. * @param string $fname Name of the HTML form field POSTed from
  196. * @param string $ziel Destination directory
  197. * @param int $project Project ID of the associated project
  198. * @return bool
  199. */
  200. function upload($fname, $ziel, $project, $folder = 0)
  201. {
  202. $name = $_FILES[$fname]['name'];
  203. $typ = $_FILES[$fname]['type'];
  204. $size = $_FILES[$fname]['size'];
  205. $tmp_name = $_FILES[$fname]['tmp_name'];
  206. $tstr = $fname . "-title";
  207. $tastr = $fname . "-tags";
  208. $visible = $_POST["visible"];
  209. if (!empty($visible[0]))
  210. {
  211. $visstr = serialize($visible);
  212. }
  213. else
  214. {
  215. $visstr = "";
  216. }
  217. $title = $_POST[$tstr];
  218. $tags = $_POST[$tastr];
  219. $error = $_FILES[$fname]['error'];
  220. $root = CL_ROOT;
  221. if (empty($name))
  222. {
  223. return false;
  224. }
  225. $desc = $_POST['desc'];
  226. $tagobj = new tags();
  227. $tags = $tagobj->formatInputTags($tags);
  228. // find the extension
  229. $teilnamen = explode(".", $name);
  230. $teile = count($teilnamen);
  231. $workteile = $teile - 1;
  232. $erweiterung = $teilnamen[$workteile];
  233. $subname = "";
  234. // if its a php file, treat it as plaintext so its not executed when opened in the browser.
  235. if (stristr($typ, "php"))
  236. {
  237. $erweiterung = "txt";
  238. $typ = "text/plain";
  239. }
  240. for ($i = 0; $i < $workteile; $i++)
  241. {
  242. $subname .= $teilnamen[$i];
  243. }
  244. $randval = mt_rand(1, 9999999);
  245. // only allow a-z , 0-9 in filenames, substitute other chars with _
  246. $subname = str_replace("ä", "ae" , $subname);
  247. $subname = str_replace("ö", "oe" , $subname);
  248. $subname = str_replace("ü", "ue" , $subname);
  249. $subname = str_replace("ß", "ss" , $subname);
  250. $subname = preg_replace("/[^-_0-9a-zA-Z]/", "_", $subname);
  251. // remove whitespace
  252. $subname = preg_replace("/\W/", "", $subname);
  253. // if filename is longer than 200 chars, cut it.
  254. if (strlen($subname) > 200)
  255. {
  256. $subname = substr($subname, 0, 200);
  257. }
  258. $name = $subname . "_" . $randval . "." . $erweiterung;
  259. $datei_final = $root . "/" . $ziel . "/" . $name;
  260. $datei_final2 = $ziel . "/" . $name;
  261. if (!file_exists($datei_final))
  262. {
  263. if (move_uploaded_file($tmp_name, $datei_final))
  264. {
  265. $filesize = filesize($datei_final);
  266. if ($project > 0)
  267. {
  268. /**
  269. * file did not already exist, was uploaded, and a project is set
  270. * add the file to the database, add the upload event to the log and return the file ID.
  271. */
  272. chmod($datei_final, 0755);
  273. $fid = $this->add_file($name, $desc, $project, 0, "$tags", $datei_final2, "$typ", $title, $folder, $visstr);
  274. $this->mylog->add($title, 'datei', 1, $project);
  275. return $fid;
  276. }
  277. else
  278. {
  279. // no project means the file is not added to the database wilfully. return file name.
  280. return $name;
  281. }
  282. }
  283. else
  284. {
  285. // file was not uploaded / error occured. return false
  286. return false;
  287. }
  288. }
  289. else
  290. {
  291. // file already exists. return false
  292. return false;
  293. }
  294. }
  295. /**
  296. * Edit a file
  297. *
  298. * @param int $id File ID
  299. * @param string $title Title of the file
  300. * @param string $desc Description text
  301. * @param string $tags Associated tags (not yet implemented)
  302. * @return bool
  303. */
  304. function edit($id, $title, $desc, $tags)
  305. {
  306. $id = (int) $id;
  307. $title = mysql_real_escape_string($title);
  308. $desc = mysql_real_escape_string($desc);
  309. $tags = mysql_real_escape_string($tags);
  310. // get project for logging
  311. $sel = mysql_query("SELECT project FROM files WHERE ID = $id");
  312. $proj = mysql_fetch_row($sel);
  313. $project = $proj[0];
  314. $sql = mysql_query("UPDATE files SET `title` = '$title', `desc` = '$desc', `tags` = '$tags' WHERE id = $id");
  315. if ($sql)
  316. {
  317. $this->mylog->add($title, 'datei' , 2, $project);
  318. return true;
  319. }
  320. else
  321. {
  322. return false;
  323. }
  324. }
  325. /**
  326. * Delete a file
  327. *
  328. * @param int $datei File ID
  329. * @return bool
  330. */
  331. function loeschen($datei)
  332. {
  333. $datei = (int) $datei;
  334. $sel1 = mysql_query("SELECT datei,name,project,title FROM files WHERE ID = $datei");
  335. $thisfile = mysql_fetch_row($sel1);
  336. if (!empty($thisfile))
  337. {
  338. $fname = $thisfile[1];
  339. $project = $thisfile[2];
  340. $ftitle = $thisfile[3];
  341. $thisfile = $thisfile[0];
  342. $delfile = "./" . $thisfile;
  343. if (!file_exists($delfile))
  344. {
  345. return false;
  346. }
  347. $del = mysql_query("DELETE FROM files WHERE ID = $datei");
  348. $del2 = mysql_query("DELETE FROM files_attached WHERE file = $datei");
  349. if ($del)
  350. {
  351. if (unlink($delfile))
  352. {
  353. $this->mylog->add($ftitle, 'datei' , 3, $project);
  354. return true;
  355. }
  356. else
  357. {
  358. return false;
  359. }
  360. }
  361. }
  362. else
  363. {
  364. return false;
  365. }
  366. }
  367. /**
  368. * Return a file
  369. *
  370. * @param string $id File ID
  371. * @return array $file File details
  372. */
  373. function getFile($id)
  374. {
  375. $id = (int) $id;
  376. // get the file from MySQL
  377. $sel = mysql_query("SELECT * FROM files WHERE ID=$id");
  378. $file = mysql_fetch_array($sel);
  379. if (!empty($file))
  380. {
  381. // determine if there is an mimetype icon corresponding to the files mimetype. If not set 'none'
  382. $file['type'] = str_replace("/", "-", $file["type"]);
  383. $set = new settings();
  384. $settings = $set->getSettings();
  385. $myfile = "./templates/" . $settings["template"] . "/images/files/" . $file['type'] . ".png";
  386. if (!file_exists($myfile))
  387. {
  388. $file['type'] = "none";
  389. }
  390. // determine if its an image or textfile or some other file. this is needed for lightboxes
  391. if (stristr($file['type'], "image"))
  392. {
  393. $file['imgfile'] = 1;
  394. } elseif (stristr($file['type'], "text"))
  395. {
  396. $file['imgfile'] = 2;
  397. }
  398. else
  399. {
  400. $file['imgfile'] = 0;
  401. }
  402. // split the tags string into an array, and also count how many tags the file has
  403. $tagobj = new tags();
  404. $thetags = $tagobj->splitTagStr($file["tags"]);;
  405. $file["tagsarr"] = $thetags;
  406. $file["tagnum"] = count($file["tagsarr"]);
  407. // strip slashes from titles , desc and tags
  408. $file["title"] = stripslashes($file["title"]);
  409. $file["desc"] = stripslashes($file["desc"]);
  410. $file["tags"] = stripslashes($file["tags"]);
  411. $file["size"] = filesize($file["datei"]) / 1024;
  412. $file["size"] = round($file["size"]);
  413. $file["addedstr"] = date("d.m.y",$file["added"]);
  414. $userobj = new user();
  415. $file["userdata"] = $userobj->getProfile($file["user"]);
  416. return $file;
  417. }
  418. else
  419. {
  420. return false;
  421. }
  422. }
  423. /**
  424. * Move a file to another folder
  425. *
  426. * @param int $file File ID
  427. * @param int $folder Folder ID
  428. * @return bool
  429. */
  430. function moveFile($file, $target)
  431. {
  432. $file = (int) $file;
  433. $target = (int)$target;
  434. // Get the file
  435. $thefile = $this->getFile($file);
  436. // Get the target folder
  437. $thefolder = $this->getFolder($target);
  438. // Build filesystem paths
  439. $targetstr = "files/" . CL_CONFIG . "/" . $thefile["project"] . "/" . $thefolder["name"] . "/" . $thefile["name"];
  440. $rootstr = CL_ROOT . "/" . $thefile["datei"];
  441. // update database
  442. $upd = mysql_query("UPDATE files SET datei = '$targetstr', folder = '$thefolder[ID]' WHERE ID = $thefile[ID]");
  443. // move the file physically
  444. return rename($rootstr, $targetstr);
  445. }
  446. /**
  447. * List all files associated to a given project
  448. *
  449. * @param string $id Project ID
  450. * @param int $lim Limit
  451. * @param int $folder Folder
  452. * @return array $files Found files
  453. */
  454. function getProjectFiles($id, $lim = 25, $folder = "")
  455. {
  456. $id = (int) $id;
  457. $lim = (int) $lim;
  458. $folder = (int) $folder;
  459. if ($folder > 0)
  460. {
  461. $fold = "files/" . CL_CONFIG . "/$id/$folder/";
  462. $sel = mysql_query("SELECT COUNT(*) FROM files WHERE project = $id AND folder = $folder ORDER BY ID DESC");
  463. }
  464. else
  465. {
  466. $sel = mysql_query("SELECT COUNT(*) FROM files WHERE project = $id AND folder = 0 ORDER BY ID DESC");
  467. }
  468. $num = mysql_fetch_row($sel);
  469. $num = $num[0];
  470. SmartyPaginate::connect();
  471. // set items per page
  472. SmartyPaginate::setLimit($lim);
  473. SmartyPaginate::setTotal($num);
  474. $start = SmartyPaginate::getCurrentIndex();
  475. $lim = SmartyPaginate::getLimit();
  476. $files = array();
  477. if ($folder > 0)
  478. {
  479. $sql = "SELECT * FROM files WHERE project = $id AND folder = $folder ORDER BY ID DESC LIMIT $start,$lim";
  480. $sel2 = mysql_query($sql);
  481. }
  482. else
  483. {
  484. $sel2 = mysql_query("SELECT * FROM files WHERE project = $id AND folder = 0 ORDER BY ID DESC LIMIT $start,$lim");
  485. }
  486. $tagobj = new tags();
  487. while ($file = mysql_fetch_array($sel2))
  488. {
  489. if (!empty($file))
  490. {
  491. /*
  492. $file['type'] = str_replace("/", "-", $file['type']);
  493. $set = new settings();
  494. $settings = $set->getSettings();
  495. $myfile = "./templates/" . $settings['template'] . "/images/files/" . $file['type'] . ".png";
  496. if (stristr($file['type'], "image"))
  497. {
  498. $file['imgfile'] = 1;
  499. } elseif (stristr($file['type'], "text"))
  500. {
  501. $file['imgfile'] = 2;
  502. }
  503. else
  504. {
  505. $file['imgfile'] = 0;
  506. }
  507. if (!file_exists($myfile))
  508. {
  509. $file['type'] = "none";
  510. }
  511. $thetags = $tagobj->splitTagStr($file["tags"]);;
  512. $file["tagsarr"] = $thetags;
  513. $file["tagnum"] = count($file["tagsarr"]);
  514. $file["title"] = stripslashes($file["title"]);
  515. $file["desc"] = stripslashes($file["desc"]);
  516. $file["tags"] = stripslashes($file["tags"]);
  517. array_push($files, $file);
  518. */
  519. array_push($files, $this->GetFile($file["ID"]));
  520. }
  521. }
  522. if (!empty($files))
  523. {
  524. return $files;
  525. }
  526. else
  527. {
  528. return false;
  529. }
  530. }
  531. /**
  532. * List all files associated to a given project
  533. *
  534. * @param string $id Project ID
  535. * @param int $lim Limit
  536. * @param int $folder Folder
  537. * @return array $files Found files
  538. */
  539. function getAllProjectFiles($id)
  540. {
  541. $id = (int) $id;
  542. $lim = (int) $lim;
  543. $folder = (int) $folder;
  544. $files = array();
  545. $sel2 = mysql_query("SELECT * FROM files WHERE project = $id ORDER BY ID DESC");
  546. $tagobj = new tags();
  547. while ($file = mysql_fetch_array($sel2))
  548. {
  549. if (!empty($file))
  550. {
  551. /*
  552. $file['type'] = str_replace("/", "-", $file['type']);
  553. $set = new settings();
  554. $settings = $set->getSettings();
  555. $myfile = "./templates/" . $settings['template'] . "/images/files/" . $file['type'] . ".png";
  556. if (stristr($file['type'], "image"))
  557. {
  558. $file['imgfile'] = 1;
  559. } elseif (stristr($file['type'], "text"))
  560. {
  561. $file['imgfile'] = 2;
  562. }
  563. else
  564. {
  565. $file['imgfile'] = 0;
  566. }
  567. if (!file_exists($myfile))
  568. {
  569. $file['type'] = "none";
  570. }
  571. $thetags = $tagobj->splitTagStr($file["tags"]);;
  572. $file["tagsarr"] = $thetags;
  573. $file["tagnum"] = count($file["tagsarr"]);
  574. $file["title"] = stripslashes($file["title"]);
  575. $file["desc"] = stripslashes($file["desc"]);
  576. $file["tags"] = stripslashes($file["tags"]);
  577. array_push($files, $file);
  578. */
  579. array_push($files, $this->getFile($file["ID"]));
  580. }
  581. }
  582. if (!empty($files))
  583. {
  584. return $files;
  585. }
  586. else
  587. {
  588. return false;
  589. }
  590. }
  591. /**
  592. * Seed the random number generator
  593. *
  594. * @return float $value Initial value
  595. */
  596. private function make_seed()
  597. {
  598. list($usec, $sec) = explode(' ', microtime());
  599. $value = (float) $sec + ((float) $usec * 100000);
  600. return $value;
  601. }
  602. /**
  603. * Add a file to the database
  604. *
  605. * @param string $name Filename
  606. * @param string $desc Description
  607. * @param int $project ID of the associated project
  608. * @param int $milestone ID of the associated milestone
  609. * @param string $tags Tags for the file (not yet implemented)
  610. * @param string $datei File path
  611. * @param string $type MIME Type
  612. * @param string $title Title of the file
  613. * @param int $ folder Optional parameter. It holds the ID of the subfolder the file is uploaded to (0 = root directory)
  614. * @return bool $insid
  615. */
  616. private function add_file($name, $desc, $project, $milestone, $tags, $datei, $type, $title, $folder = 0, $visstr = "")
  617. {
  618. $name = mysql_real_escape_string($name);
  619. $desc = mysql_real_escape_string($desc);
  620. $tags = mysql_real_escape_string($tags);
  621. $datei = mysql_real_escape_string($datei);
  622. $project = (int) $project;
  623. $milestone = (int) $milestone;
  624. $folder = (int) $folder;
  625. $userid = $_SESSION["userid"];
  626. $type = mysql_real_escape_string($type);
  627. $title = mysql_real_escape_string($title);
  628. $now = time();
  629. $ins = mysql_query("INSERT INTO files (`name`,`desc`,`project`,`milestone`,`user`,`tags`,`added`,`datei`,`type`,`title`,`folder`,`visible`) VALUES ('$name','$desc',$project,$milestone,$userid,'$tags','$now','$datei','$type','$title','$folder','$visstr')");
  630. if ($ins)
  631. {
  632. $insid = mysql_insert_id();
  633. return $insid;
  634. }
  635. else
  636. {
  637. return false;
  638. }
  639. }
  640. }
  641. ?>