PageRenderTime 49ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/bx/plugins/file.php

https://github.com/chregu/fluxcms
PHP | 349 lines | 322 code | 12 blank | 15 comment | 7 complexity | fc50bd71965de1bef799a059833392dc MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, Apache-2.0, LGPL-2.1
  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | Bx |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 2001-2007 Liip AG |
  6. // +----------------------------------------------------------------------+
  7. // | This program is free software; you can redistribute it and/or |
  8. // | modify it under the terms of the GNU General Public License (GPL) |
  9. // | as published by the Free Software Foundation; either version 2 |
  10. // | of the License, or (at your option) any later version. |
  11. // | The GPL can be found at http://www.gnu.org/licenses/gpl.html |
  12. // +----------------------------------------------------------------------+
  13. // | Author: Liip AG <devel@liip.ch> |
  14. // +----------------------------------------------------------------------+
  15. class bx_plugins_file extends bx_plugin implements bxIplugin {
  16. private static $instance = array();
  17. private $fileroots = array();
  18. private $absolutefileroots = array();
  19. /*** magic methods and functions ***/
  20. public static function getInstance($mode) {
  21. if (!isset(self::$instance[$mode])) {
  22. self::$instance[$mode] = new bx_plugins_file($mode);
  23. }
  24. return self::$instance[$mode];
  25. }
  26. public function __construct($mode) {
  27. $this->mode = $mode;
  28. }
  29. public function getIdByRequest($path, $name = NULL, $ext =NULL) {
  30. if (file_exists($this->getFileRoot($path).$name.".".$ext)) {
  31. return $this->getFileRoot($path).$name.".".$ext;
  32. }
  33. }
  34. public function getPipelineParametersById($path, $id) {
  35. // FIXME, we need another resource reader, it doesn't work if request != id
  36. return array('pipelineName'=>'resourceReader');
  37. }
  38. public function isRealResource($path , $id) {
  39. return true;
  40. }
  41. public function getFileRoot($uri) {
  42. if (!isset($this->fileroots[$uri])) {
  43. if ($root = $this->getParameter($uri,"virtualDir")) {
  44. if ($GLOBALS['POOL']->config->filesDoBaseDirCheck != 'false') {
  45. $rootCheck = realpath(BX_OPEN_BASEDIR.$root) ;
  46. if (BX_OS_WIN) {
  47. $rootCheck = str_replace('\\','/',$rootCheck);
  48. }
  49. if (strpos($rootCheck,BX_OPEN_BASEDIR) !== 0) {
  50. $this->fileroots[$uri] = '__notallowed';
  51. } else {
  52. $this->fileroots[$uri] = $root;
  53. }
  54. } else {
  55. $this->fileroots[$uri] = $root;
  56. }
  57. } else {
  58. $this->fileroots[$uri] = $uri;
  59. }
  60. }
  61. return $this->fileroots[$uri];
  62. }
  63. public function getAbsoluteFileRoot($uri) {
  64. if (!isset($this->absolutefileroots[$uri])) {
  65. $id = $this->getFileRoot($uri);
  66. if (BX_OS_WIN && preg_match("#^[a-zA-Z]:#",$id)) {
  67. $this->absolutefileroots[$uri] =$id;
  68. } else if (substr($id,0,1) == "/") {
  69. $this->absolutefileroots[$uri] =$id;
  70. } else {
  71. $this->absolutefileroots[$uri] = BX_OPEN_BASEDIR.$id;
  72. }
  73. }
  74. return $this->absolutefileroots[$uri];
  75. }
  76. public function getChildren($coll, $id) {
  77. $root = $this->getFileRoot($coll->uri).$id;
  78. $virtual = $this->getParameter($coll->uri,"virtualDir");
  79. if (file_exists(BX_OPEN_BASEDIR.$root)) {
  80. $dir = new DirectoryIterator(BX_OPEN_BASEDIR.$root);
  81. $ch = array();
  82. foreach ($dir as $file) {
  83. $name = $file->getFileName();
  84. if(!bx_helpers_string::isUtf8($name)) {
  85. $name = utf8_encode($name);
  86. }
  87. if (strpos($name,".") === 0) {
  88. continue;
  89. }
  90. if ($file->isDir()) {
  91. if ($virtual) {
  92. $c= new bx_resources_file($root.$name."/");
  93. $c->props['mimetype'] = "httpd/unix-directory";
  94. $c->uri = str_replace("$virtual",$coll->uri,$c->uri);
  95. $ch[] = $c;
  96. }
  97. } else {
  98. $ch[] = new bx_resources_file($root.$name);
  99. }
  100. }
  101. return $ch;
  102. }
  103. return array();
  104. }
  105. public function adminResourceExists ($path, $id, $ext=null, $sample = false) {
  106. return $this;
  107. }
  108. public function getResourceById($path, $id, $mock = false) {
  109. return new bx_resources_file($this->getFileRoot($path).$id);
  110. }
  111. public function handlePOST($path, $id, $data, $mode = null) {
  112. if ($mode == "FullXML") {
  113. $res = $this->getResourceById($path,$id);
  114. if ($res->mock) {
  115. $res->create();
  116. }
  117. $file = $this->getContentUriById($path,$id);
  118. //FIXME: resource should handle the save, not the plugin, actually..
  119. //remove dos linefeeds (fucks up svn diffs)
  120. $data['fullxml'] = preg_replace("#\r\n#","\n",$data['fullxml']);
  121. if (!file_put_contents($file,($data['fullxml']))) {
  122. print '<span style="color: red;">File '.$file.' could not be written</span>';
  123. return false;
  124. }
  125. return true;
  126. }
  127. }
  128. public function getResourceTypes() {
  129. return array('file','archive');
  130. }
  131. public static function getAddResourceParams($type, $path = null, $options = array()) {
  132. $i18n = $GLOBALS['POOL']->i18nadmin;
  133. $dom = new domDocument();
  134. $fields = $dom->createElement('fields');
  135. $nameNode = $dom->createElement('field');
  136. $nameNode->setAttribute('name', 'file');
  137. $nameNode->setAttribute('type', 'file');
  138. if ($type == 'file') {
  139. $helpNode = $dom->createElement("help", $i18n->translate("help_uploadfile"));
  140. } else if ($type == 'archive') {
  141. $helpNode = $dom->createElement("help", $i18n->translate("help_uploadzip"));
  142. }
  143. $nameNode->appendChild($helpNode);
  144. $fields->appendChild($nameNode);
  145. $nameNode = $dom->createElement('field');
  146. $nameNode->setAttribute('name', 'name');
  147. $nameNode->setAttribute('type', 'hidden');
  148. $fields->appendChild($nameNode);
  149. if ($type == 'image') {
  150. $sizeNode = $dom->createElement('field');
  151. $sizeNode->setAttribute('name', 'imagesize');
  152. $sizeNode->setAttribute('type', 'select');
  153. $sizes = array("leave", "600","800","1024");
  154. foreach ($sizes as $size) {
  155. $sizeOpt = $dom->createElement('option');
  156. if ($size != "leave") {
  157. $sizeOpt->setAttribute("name", "max. " . $size ."px width");
  158. } else {
  159. $sizeOpt->setAttribute("name", $size);
  160. }
  161. $sizeOpt->setAttribute("value", $size);
  162. $sizeNode->appendChild($sizeOpt);
  163. }
  164. $fields->appendChild($sizeNode);
  165. } else if ($type == 'archive') {
  166. // junk paths?
  167. $sizeNode = $dom->createElement('field');
  168. $sizeNode->setAttribute('name', 'junkpaths');
  169. $sizeNode->setAttribute('type', 'checkbox');
  170. if (isset($options['junkpaths']) && $options['junkpaths']) {
  171. $sizeNode->setAttribute('checked','checked');
  172. }
  173. $sizeNode->setAttribute('textBefore',$i18n->translate("Remove File Paths"));
  174. $helpNode = $dom->createElement("help", $i18n->translate("help_removefilepath"));
  175. $sizeNode->appendChild($helpNode);
  176. $fields->appendChild($sizeNode);
  177. // fix invalid paths?
  178. $sizeNode = $dom->createElement('field');
  179. $sizeNode->setAttribute('name', 'fixinvalid');
  180. $sizeNode->setAttribute('type', 'checkbox');
  181. if (isset($options['fixinvalid']) && $options['fixinvalid']) {
  182. $sizeNode->setAttribute('checked','checked');
  183. }
  184. $sizeNode->setAttribute('textBefore',$i18n->translate("Fix invalid filenames"));
  185. $helpNode = $dom->createElement("help", $i18n->translate("help_unlikedfiles"));
  186. $sizeNode->appendChild($helpNode);
  187. $fields->appendChild($sizeNode);
  188. }
  189. $dom->appendChild($fields);
  190. return $dom;
  191. }
  192. public function addResource($name, $parentUri, $options=array(), $resourceType = null, $returnAfterwards = FALSE) {
  193. $parts = bx_collections::getCollectionUriAndFileParts($parentUri);
  194. $rootPath = $this->getFileRoot($parts['colluri']);
  195. //some IEs send full path instead of just the filename
  196. // basename() cuts that off
  197. $filename = basename($_FILES['bx']['name']['plugins']['admin_addresource']['file']);
  198. $tmpname = $_FILES['bx']['tmp_name']['plugins']['admin_addresource']['file'];
  199. // prevent illegal filenames
  200. $filename = bx_helpers_string::makeUri($filename,true);
  201. $to = BX_OPEN_BASEDIR.$rootPath .$parts['rawname']. $filename;
  202. $id =( $parentUri. $filename);
  203. self::addFileResource($tmpname,$to, $parentUri,$resourceType,$options,$id);
  204. $r = $this->getResourceById(substr($parentUri,1), $filename);
  205. $r->onSave();
  206. if($returnAfterwards == TRUE)
  207. return $id;
  208. if (isset($_POST['bx']['plugins']['admin_addresource']['redirect'])) {
  209. if($filename == "none") {
  210. header("Location: ".$_POST['bx']['plugins']['admin_addresource']['redirect']);
  211. } else {
  212. header("Location: ".$_POST['bx']['plugins']['admin_addresource']['redirect']."?fileuri=$parentUri".$filename);
  213. }
  214. } else if ($resourceType == "archive") {
  215. header("Location: ".BX_WEBROOT."admin/addresource/".$parentUri."?type=archive&updateTree=$parentUri");
  216. } else {
  217. header("Location: ".BX_WEBROOT."admin/edit/".$id."?updateTree=$parentUri");
  218. }
  219. exit(0);
  220. }
  221. static public function addFileResource($tmpname,$to, $parentUri, $resourceType,$options,$id) {
  222. if ($GLOBALS['POOL']->config->allowPHPUpload != "true" && strtolower(substr($to,-3)) == "php") {
  223. $to = $to . ".txt";
  224. }
  225. if (!file_exists(dirname($to))) {
  226. mkdir(dirname($to),0755,true);
  227. }
  228. move_uploaded_file($tmpname,$to);
  229. chmod ($to,0644);
  230. if ($resourceType == "archive") {
  231. switch (popoon_helpers_mimetypes::getFromFileLocation($to)) {
  232. case "application/zip":
  233. // FIXME: make unzip adjustable...
  234. if (isset($options["junkpaths"]) && $options["junkpaths"] == "on") {
  235. $junk = " -j ";
  236. } else {
  237. $junk = "";
  238. }
  239. $exec = escapeshellcmd("unzip $junk -o -d ". escapeshellarg( dirname($to)). " ". escapeshellarg($to));
  240. exec($exec,$output);
  241. if (isset($options["fixinvalid"]) && $options["fixinvalid"] == "on") {
  242. array_shift($output);
  243. foreach ($output as $fileinfo) {
  244. $fileinfo = explode(" ",trim($fileinfo),2);
  245. $filename = array_pop($fileinfo);
  246. $dirname = dirname($filename);
  247. $filename = basename($filename);
  248. $filenameFixed = bx_helpers_string::makeUri($filename,true);
  249. if ($filenameFixed != $filename) {
  250. rename($dirname.'/'.$filename,$dirname.'/'.$filenameFixed);
  251. }
  252. }
  253. }
  254. break;
  255. case "application/x-gzip":
  256. $ar = new Archive_Tar($to,"gz");
  257. $ar->extract(dirname($to));
  258. break;
  259. case "application/x-bz2":
  260. $ar = new Archive_Tar($to,"bz2");
  261. $ar->extract($rootPath.$parts['rawname']);
  262. break;
  263. case "application/x-gtar":
  264. $ar = new Archive_Tar($to);
  265. $ar->extract($rootPath.$parts['rawname']);
  266. break;
  267. break;
  268. }
  269. unlink($to);
  270. return $to;
  271. } else {
  272. bx_metaindex::callIndexerFromFilename($to,$id);
  273. }
  274. }
  275. public function copyResourceById($path, $id, $to, $move = false) {
  276. if ($GLOBALS['POOL']->config->allowPHPUpload != "true" && strtolower(substr($to,-3)) == "php") {
  277. $to = $to . ".txt";
  278. }
  279. $r = $this->getResourceById($path, $id);
  280. $parts = bx_collections::getCollectionAndFileParts($to);
  281. $toCollUri = $this->getFileRoot(bx_collections::sanitizeUrl($parts['coll']->uri));
  282. if (($r instanceof bx_resource) && method_exists($r,"copy")) {
  283. return $r->copy($toCollUri.$parts['rawname']);
  284. }
  285. return array();
  286. }
  287. public function makeCollection($path,$new) {
  288. $filepath = $this->getAbsoluteFileRoot($path);
  289. bx_helpers_file::mkpath($filepath."/".$new);
  290. return true;
  291. }
  292. }