PageRenderTime 69ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/controller/utilscontroller.php

https://gitlab.com/eneiluj/gpxedit-oc
PHP | 371 lines | 327 code | 17 blank | 27 comment | 13 complexity | 23ebdfc5e6b6c1650b7121f6d9840c4c MD5 | raw file
  1. <?php
  2. /**
  3. * ownCloud - gpxedit
  4. *
  5. * This file is licensed under the Affero General Public License version 3 or
  6. * later. See the COPYING file.
  7. *
  8. * @author Julien Veyssier <eneiluj@gmx.fr>
  9. * @copyright Julien Veyssier 2015
  10. */
  11. namespace OCA\GpxEdit\Controller;
  12. use OCP\App\IAppManager;
  13. use OCP\IURLGenerator;
  14. use OCP\IConfig;
  15. use OCP\AppFramework\Http;
  16. use OCP\AppFramework\Http\RedirectResponse;
  17. use OCP\AppFramework\Http\ContentSecurityPolicy;
  18. use OCP\IRequest;
  19. use OCP\AppFramework\Http\DataResponse;
  20. use OCP\AppFramework\Http\DataDisplayResponse;
  21. use OCP\AppFramework\Http\Response;
  22. use OCP\AppFramework\Controller;
  23. /**
  24. * Recursive find files from name pattern
  25. */
  26. function globRecursive($path, $find, $recursive=True) {
  27. $result = Array();
  28. $dh = opendir($path);
  29. while (($file = readdir($dh)) !== false) {
  30. if (substr($file, 0, 1) === '.') continue;
  31. $rfile = "{$path}/{$file}";
  32. if (is_dir($rfile) and $recursive) {
  33. foreach (globRecursive($rfile, $find) as $ret) {
  34. array_push($result, $ret);
  35. }
  36. } else {
  37. if (fnmatch($find, $file)){
  38. array_push($result, $rfile);
  39. }
  40. }
  41. }
  42. closedir($dh);
  43. return $result;
  44. }
  45. /*
  46. * search into all directories in PATH environment variable
  47. * to find a program and return it if found
  48. */
  49. function getProgramPath($progname){
  50. $path_ar = explode(':',getenv('path'));
  51. $path_ar = array_merge($path_ar, explode(':',getenv('PATH')));
  52. foreach ($path_ar as $path){
  53. $supposed_gpath = $path.'/'.$progname;
  54. if (file_exists($supposed_gpath) and
  55. is_executable($supposed_gpath)){
  56. return $supposed_gpath;
  57. }
  58. }
  59. return null;
  60. }
  61. function endswith($string, $test) {
  62. $strlen = strlen($string);
  63. $testlen = strlen($test);
  64. if ($testlen > $strlen) return false;
  65. return substr_compare($string, $test, $strlen - $testlen, $testlen) === 0;
  66. }
  67. class UtilsController extends Controller {
  68. private $userId;
  69. private $userfolder;
  70. private $config;
  71. private $userAbsoluteDataPath;
  72. private $dbconnection;
  73. private $dbtype;
  74. public function __construct($AppName, IRequest $request, $UserId,
  75. $userfolder, $config, IAppManager $appManager){
  76. parent::__construct($AppName, $request);
  77. $this->userId = $UserId;
  78. $this->dbtype = $config->getSystemValue('dbtype');
  79. // IConfig object
  80. $this->config = $config;
  81. if ($this->dbtype === 'pgsql'){
  82. $this->dbdblquotes = '"';
  83. }
  84. else{
  85. $this->dbdblquotes = '';
  86. }
  87. if ($UserId !== '' and $userfolder !== null){
  88. // path of user files folder relative to DATA folder
  89. $this->userfolder = $userfolder;
  90. // absolute path to user files folder
  91. $this->userAbsoluteDataPath =
  92. $this->config->getSystemValue('datadirectory').
  93. rtrim($this->userfolder->getFullPath(''), '/');
  94. // make cache if it does not exist
  95. $cachedirpath = $this->userAbsoluteDataPath.'/../cache';
  96. if (! is_dir($cachedirpath)){
  97. mkdir($cachedirpath);
  98. }
  99. $this->dbconnection = \OC::$server->getDatabaseConnection();
  100. }
  101. }
  102. /**
  103. */
  104. public function deleteExtraSymbol($name) {
  105. $filename = str_replace(array('../', '..\\', '/'), '', $name);
  106. $filepath = $this->config->getSystemValue('datadirectory').'/gpxedit/symbols/'.$filename;
  107. if (file_exists($filepath)){
  108. unlink($filepath);
  109. }
  110. return new DataResponse(
  111. [
  112. 'data' =>
  113. [
  114. 'name' => $filename,
  115. 'message' => 'Deleted'
  116. ],
  117. 'status' => 'success'
  118. ]
  119. );
  120. }
  121. /**
  122. */
  123. public function uploadExtraSymbol($addExtraSymbolName) {
  124. $newSymbol = $this->request->getUploadedFile('uploadsymbol');
  125. $filename = str_replace(array('../', '..\\', '/'), '', $addExtraSymbolName);
  126. if (!endswith($newSymbol['name'], '.png')){
  127. return new DataResponse(
  128. [
  129. 'data' =>
  130. [
  131. 'message' => 'File has to be a png'
  132. ],
  133. 'status' => 'fail'
  134. ],
  135. Http::STATUS_UNPROCESSABLE_ENTITY
  136. );
  137. }
  138. if (empty($newSymbol)) {
  139. return new DataResponse(
  140. [
  141. 'data' => [
  142. 'message' => 'No file uploaded'
  143. ]
  144. ],
  145. Http::STATUS_UNPROCESSABLE_ENTITY
  146. );
  147. }
  148. if(!empty($newSymbol)) {
  149. $filepath = $this->config->getSystemValue('datadirectory').'/gpxedit/symbols/'.$filename.'.png';
  150. $content = file_get_contents($newSymbol['tmp_name']);
  151. file_put_contents($filepath, $content);
  152. }
  153. return new DataResponse(
  154. [
  155. 'data' =>
  156. [
  157. 'name' => $filename.'.png',
  158. 'message' => 'Saved'
  159. ],
  160. 'status' => 'success'
  161. ]
  162. );
  163. }
  164. /**
  165. * @NoAdminRequired
  166. * @NoCSRFRequired
  167. * @PublicPage
  168. */
  169. public function getExtraSymbol() {
  170. $filename = str_replace(array('../', '..\\', '/'), '', $_GET['name']);
  171. $filepath = $this->config->getSystemValue('datadirectory').'/gpxedit/symbols/'.$filename;
  172. $filecontent = file_get_contents($filepath);
  173. $response = new DataDisplayResponse(
  174. $filecontent, \OCP\AppFramework\Http::STATUS_OK, Array('Content-type'=>'image/png')
  175. );
  176. $csp = new ContentSecurityPolicy();
  177. $csp->addAllowedImageDomain('*')
  178. ->addAllowedMediaDomain('*')
  179. ->addAllowedConnectDomain('*');
  180. $response->setContentSecurityPolicy($csp);
  181. return $response;
  182. }
  183. /**
  184. * Add one tile server to the DB for current user
  185. * @NoAdminRequired
  186. */
  187. public function addTileServer($servername, $serverurl, $type,
  188. $layers, $version, $tformat, $opacity, $transparent,
  189. $minzoom, $maxzoom, $attribution) {
  190. // first we check it does not already exist
  191. $sqlts = 'SELECT servername FROM *PREFIX*gpxedit_tile_servers ';
  192. $sqlts .= 'WHERE '.$this->dbdblquotes.'user'.$this->dbdblquotes.'=\''.$this->userId.'\' ';
  193. $sqlts .= 'AND servername='.$this->db_quote_escape_string($servername).' ';
  194. $sqlts .= 'AND type='.$this->db_quote_escape_string($type).' ';
  195. $req = $this->dbconnection->prepare($sqlts);
  196. $req->execute();
  197. $ts = null;
  198. while ($row = $req->fetch()){
  199. $ts = $row['servername'];
  200. break;
  201. }
  202. $req->closeCursor();
  203. // then if not, we insert it
  204. if ($ts === null){
  205. $sql = 'INSERT INTO *PREFIX*gpxedit_tile_servers';
  206. $sql .= ' ('.$this->dbdblquotes.'user'.$this->dbdblquotes.', type, servername, url, layers, version, format, opacity, transparent, minzoom, maxzoom, attribution) ';
  207. $sql .= 'VALUES (\''.$this->userId.'\',';
  208. $sql .= $this->db_quote_escape_string($type).',';
  209. $sql .= $this->db_quote_escape_string($servername).',';
  210. $sql .= $this->db_quote_escape_string($serverurl).',';
  211. $sql .= $this->db_quote_escape_string($layers).',';
  212. $sql .= $this->db_quote_escape_string($version).',';
  213. $sql .= $this->db_quote_escape_string($tformat).',';
  214. $sql .= $this->db_quote_escape_string($opacity).',';
  215. $sql .= $this->db_quote_escape_string($transparent).',';
  216. $sql .= $this->db_quote_escape_string($minzoom).',';
  217. $sql .= $this->db_quote_escape_string($maxzoom).',';
  218. $sql .= $this->db_quote_escape_string($attribution).');';
  219. $req = $this->dbconnection->prepare($sql);
  220. $req->execute();
  221. $req->closeCursor();
  222. $ok = 1;
  223. }
  224. else{
  225. $ok = 0;
  226. }
  227. $response = new DataResponse(
  228. [
  229. 'done'=>$ok
  230. ]
  231. );
  232. $csp = new ContentSecurityPolicy();
  233. $csp->addAllowedImageDomain('*')
  234. ->addAllowedMediaDomain('*')
  235. ->addAllowedConnectDomain('*');
  236. $response->setContentSecurityPolicy($csp);
  237. return $response;
  238. }
  239. /**
  240. * Delete one tile server entry from DB for current user
  241. * @NoAdminRequired
  242. */
  243. public function deleteTileServer($servername, $type) {
  244. $sqldel = 'DELETE FROM *PREFIX*gpxedit_tile_servers ';
  245. $sqldel .= 'WHERE '.$this->dbdblquotes.'user'.$this->dbdblquotes.'='.$this->db_quote_escape_string($this->userId).' AND servername=';
  246. $sqldel .= $this->db_quote_escape_string($servername).' AND type='.$this->db_quote_escape_string($type).';';
  247. $req = $this->dbconnection->prepare($sqldel);
  248. $req->execute();
  249. $req->closeCursor();
  250. $response = new DataResponse(
  251. [
  252. 'done'=>1
  253. ]
  254. );
  255. $csp = new ContentSecurityPolicy();
  256. $csp->addAllowedImageDomain('*')
  257. ->addAllowedMediaDomain('*')
  258. ->addAllowedConnectDomain('*');
  259. $response->setContentSecurityPolicy($csp);
  260. return $response;
  261. }
  262. /**
  263. * Save options values to the DB for current user
  264. * @NoAdminRequired
  265. */
  266. public function saveOptionsValues($optionsValues) {
  267. // first we check if user already has options values in DB
  268. $sqlts = 'SELECT jsonvalues FROM *PREFIX*gpxedit_options ';
  269. $sqlts .= 'WHERE '.$this->dbdblquotes.'user'.$this->dbdblquotes.'=\''.$this->userId.'\' ';
  270. $req = $this->dbconnection->prepare($sqlts);
  271. $req->execute();
  272. $check = null;
  273. while ($row = $req->fetch()){
  274. $check = $row['jsonvalues'];
  275. break;
  276. }
  277. $req->closeCursor();
  278. // if nothing is there, we insert
  279. if ($check === null){
  280. $sql = 'INSERT INTO *PREFIX*gpxedit_options';
  281. $sql .= ' ('.$this->dbdblquotes.'user'.$this->dbdblquotes.', jsonvalues) ';
  282. $sql .= 'VALUES (\''.$this->userId.'\',';
  283. $sql .= '\''.$optionsValues.'\');';
  284. $req = $this->dbconnection->prepare($sql);
  285. $req->execute();
  286. $req->closeCursor();
  287. }
  288. // else we update the values
  289. else{
  290. $sqlupd = 'UPDATE *PREFIX*gpxedit_options ';
  291. $sqlupd .= 'SET jsonvalues=\''.$optionsValues.'\' ';
  292. $sqlupd .= 'WHERE '.$this->dbdblquotes.'user'.$this->dbdblquotes.'=\''.$this->userId.'\' ; ';
  293. $req = $this->dbconnection->prepare($sqlupd);
  294. $req->execute();
  295. $req->closeCursor();
  296. }
  297. $response = new DataResponse(
  298. [
  299. 'done'=>true
  300. ]
  301. );
  302. $csp = new ContentSecurityPolicy();
  303. $csp->addAllowedImageDomain('*')
  304. ->addAllowedMediaDomain('*')
  305. ->addAllowedConnectDomain('*');
  306. $response->setContentSecurityPolicy($csp);
  307. return $response;
  308. }
  309. /**
  310. * get options values to the DB for current user
  311. * @NoAdminRequired
  312. */
  313. public function getOptionsValues($optionsValues) {
  314. $sqlov = 'SELECT jsonvalues FROM *PREFIX*gpxedit_options ';
  315. $sqlov .= 'WHERE '.$this->dbdblquotes.'user'.$this->dbdblquotes.'='.$this->db_quote_escape_string($this->userId).' ;';
  316. $req = $this->dbconnection->prepare($sqlov);
  317. $req->execute();
  318. $ov = '{}';
  319. while ($row = $req->fetch()){
  320. $ov = $row["jsonvalues"];
  321. }
  322. $req->closeCursor();
  323. $response = new DataResponse(
  324. [
  325. 'values'=>$ov
  326. ]
  327. );
  328. $csp = new ContentSecurityPolicy();
  329. $csp->addAllowedImageDomain('*')
  330. ->addAllowedMediaDomain('*')
  331. ->addAllowedConnectDomain('*');
  332. $response->setContentSecurityPolicy($csp);
  333. return $response;
  334. }
  335. /*
  336. * quote and choose string escape function depending on database used
  337. */
  338. private function db_quote_escape_string($str){
  339. return $this->dbconnection->quote($str);
  340. }
  341. }