functions.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. <?php
  2. /**
  3. * @name functions.php
  4. * @author Philipp Maurer
  5. * @author Tobias Reich
  6. * @copyright 2013 by Philipp Maurer, Tobias Reich
  7. */
  8. if(!defined('LYCHEE')) die('Direct access is not allowed!');
  9. // Database Functions
  10. function dbConnect() {
  11. global $db, $dbUser, $dbPassword, $dbHost;
  12. $database = new mysqli($dbHost, $dbUser, $dbPassword);
  13. if (mysqli_connect_errno() != 0) {
  14. echo mysqli_connect_errno().': '.mysqli_connect_error();
  15. return false;
  16. }
  17. if (!$database->select_db($db)) {
  18. createDatabase($db, $database);
  19. }
  20. $query = "SELECT * FROM lychee_photos, lychee_albums;";
  21. if(!$database->query($query)) createTables($database);
  22. return $database;
  23. }
  24. function dbClose() {
  25. global $database;
  26. if(!$database->close()) {
  27. echo "Closing the connection failed!";
  28. return false;
  29. }
  30. return true;
  31. }
  32. function createDatabase($db, $database) {
  33. $result = $database->query("CREATE DATABASE IF NOT EXISTS $db;");
  34. $database->select_db($db);
  35. if(!$result) return false;
  36. return true;
  37. }
  38. function createTables($database) {
  39. $query = "CREATE TABLE IF NOT EXISTS `lychee_albums` (
  40. `id` int(11) NOT NULL AUTO_INCREMENT,
  41. `title` varchar(50) NOT NULL,
  42. `sysdate` varchar(10) NOT NULL,
  43. `public` TINYINT(1) DEFAULT '0',
  44. `password` VARCHAR(100),
  45. PRIMARY KEY (`id`)
  46. ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;";
  47. $result = $database->query($query);
  48. if(!$result) return false;
  49. $query = "CREATE TABLE `lychee_photos` (
  50. `id` bigint(14) NOT NULL,
  51. `title` varchar(50) NOT NULL,
  52. `description` varchar(160) NOT NULL,
  53. `url` varchar(100) NOT NULL,
  54. `public` tinyint(1) NOT NULL,
  55. `shortlink` varchar(20) NOT NULL,
  56. `type` varchar(10) NOT NULL,
  57. `width` int(11) NOT NULL,
  58. `height` int(11) NOT NULL,
  59. `size` varchar(10) NOT NULL,
  60. `sysdate` varchar(10) NOT NULL,
  61. `systime` varchar(8) NOT NULL,
  62. `iso` varchar(15) NOT NULL,
  63. `aperture` varchar(10) NOT NULL,
  64. `make` varchar(20) NOT NULL,
  65. `model` varchar(50) NOT NULL,
  66. `shutter` varchar(10) NOT NULL,
  67. `focal` varchar(10) NOT NULL,
  68. `takedate` varchar(10) NOT NULL,
  69. `taketime` varchar(8) NOT NULL,
  70. `star` tinyint(1) NOT NULL,
  71. `thumbUrl` varchar(50) NOT NULL,
  72. `album` varchar(30) NOT NULL DEFAULT '0',
  73. `import_name` varchar(100) DEFAULT '',
  74. PRIMARY KEY (`id`)
  75. ) ENGINE=MyISAM DEFAULT CHARSET=latin1;";
  76. $result = $database->query($query);
  77. if(!$result) return false;
  78. return true;
  79. }
  80. // Upload Functions
  81. function upload($files, $albumID) {
  82. global $database;
  83. foreach ($files as $file) {
  84. switch($albumID) {
  85. // s for public (share)
  86. case 's':
  87. $public = 1;
  88. $star = 0;
  89. $albumID = 0;
  90. break;
  91. // f for starred (fav)
  92. case 'f':
  93. $star = 1;
  94. $public = 0;
  95. $albumID = 0;
  96. break;
  97. default:
  98. $star = 0;
  99. $public = 0;
  100. }
  101. $id = str_replace('.', '', microtime(true));
  102. while(strlen($id)<14) $id .= 0;
  103. $tmp_name = $file["tmp_name"];
  104. $type = getimagesize($tmp_name);
  105. if(($type[2]!=1)&&($type[2]!=2)&&($type[2]!=3)) return false;
  106. $data = $file["name"];
  107. $data = explode('.',$data);
  108. $data = array_reverse ($data);
  109. $data = $data[0];
  110. if(!is_uploaded_file($file)) {
  111. if (copy($tmp_name, "../uploads/big/$id.$data")) {
  112. unlink($tmp_name);
  113. $import_name = $tmp_name;
  114. }
  115. } else {
  116. move_uploaded_file($tmp_name, "../uploads/big/$id.$data");
  117. $import_name = "";
  118. }
  119. createThumb($id.".".$data);
  120. // Read infos
  121. $info = getCamera($id.".".$data);
  122. $title="";
  123. if(isset($info['type'])){$type=$info['type'];}else{$type="";}
  124. if(isset($info['width'])){$width=$info['width'];}else{$width="";}
  125. if(isset($info['height'])){$height=$info['height'] OR "";}else{$height="";}
  126. if(isset($info['size'])){$size=$info['size'] OR "";}else{$size="";}
  127. if(isset($info['date'])){$sysdate=$info['date'];}else{$sysdate="";}
  128. if(isset($info['time'])){$systime=$info['time'];}else{$systime="";}
  129. if(isset($info['iso'])){$iso=$info['iso'];}else{$iso="";}
  130. if(isset($info['aperture'])){$aperture=$info['aperture'];}else{$aperture="";}
  131. if(isset($info['make'])){$make=$info['make'];}else{$make="";}
  132. if(isset($info['model'])){$model=$info['model'] OR "";}else{$model="";}
  133. if(isset($info['shutter'])){$shutter=$info['shutter'];}else{$shutter="";}
  134. if(isset($info['focal'])){$focal=$info['focal'];}else{$focal="";}
  135. if(isset($info['takeDate'])){$takeDate=$info['takeDate'];}else{$takeDate="";}
  136. if(isset($info['takeTime'])){$takeTime=$info['takeTime'];}else{$takeTime="";}
  137. $query = "INSERT INTO lychee_photos (id, title, url, type, width, height, size, sysdate, systime, iso, aperture, make, model, shutter, focal, takedate, taketime, thumbUrl, album, public, star, import_name)
  138. VALUES ('$id', '$title', 'uploads/big/$id.$data', '$type', '$width', '$height', '$size', '$sysdate', '$systime', '$iso', '$aperture', '$make', '$model', '$shutter', '$focal', '$takeDate', '$takeTime', 'uploads/thumb/$id.$data', '$albumID', '$public', '$star', '$import_name');";
  139. $result = $database->query($query);
  140. }
  141. return true;
  142. }
  143. function getCamera($photoID) {
  144. global $database;
  145. $url = "../uploads/big/$photoID";
  146. $type = getimagesize($url);
  147. $type = $type['mime'];
  148. if(($type == "image/jpeg") && function_exists('exif_read_data') ){
  149. $exif = exif_read_data($url, "EXIF", 0);
  150. // General information
  151. $return['name'] = $exif['FileName'];
  152. $generalInfos = getimagesize($url);
  153. $return['type'] = $generalInfos['mime'];
  154. $return['width'] = $generalInfos[0];
  155. $return['height'] = $generalInfos[1];
  156. $size = (filesize($url) / 1024);
  157. if($size >= 1024){$size=round($size/1024,1)." MB";}else{$size=round($size,1)." KB";}
  158. $return['size'] = $size;
  159. $return['date'] = date("d.m.Y",filectime($url));
  160. $return['time'] = date("H:i:s",filectime($url));
  161. //echo $exif['FileDateTime']."<br/>".$exif['DateTimeOriginal'];
  162. // Camera Information
  163. if(isset($exif['ISOSpeedRatings'])){$return['iso']="ISO-".$exif['ISOSpeedRatings'];}
  164. if(isset($exif['COMPUTED']['ApertureFNumber'])){$return['aperture']=$exif['COMPUTED']['ApertureFNumber'];}
  165. if(isset($exif['Make'])){$return['make']=$exif['Make'];}
  166. if(isset($exif['Model'])){$return['model']=$exif['Model'];}
  167. if(isset($exif['ExposureTime'])){$return['shutter']=$exif['ExposureTime']." Sek.";}
  168. if(isset($exif['FocalLength'])){$return['focal']=($exif['FocalLength']/1)." mm";}
  169. if(isset($exif['Software'])){$return['software']=$exif['Software'];}
  170. if(isset($exif['DateTimeOriginal'])) {
  171. $exifDate = explode(" ",$exif['DateTimeOriginal']);
  172. $date = explode(":", $exifDate[0]); $return['takeDate'] = $date[2].".".$date[1].".".$date[0];
  173. $return['takeTime'] = $exifDate[1];
  174. }
  175. }else{
  176. $exif = getimagesize($url);
  177. $return['type'] = $exif['mime'];
  178. $return['width'] = $exif[0];
  179. $return['height'] = $exif[1];
  180. $size = (filesize($url) / 1024);
  181. if($size >= 1024){$size=round($size/1024,1)." MB";}else{$size=round($size,1)." KB";}
  182. $return['size'] = $size;
  183. $return['date'] = date("d.m.Y",filectime($url));
  184. $return['time'] = date("H:i:s",filectime($url));
  185. }
  186. return $return;
  187. }
  188. function createThumb($photoName, $width = 200, $width2x = 400, $height = 200, $height2x = 400) {
  189. global $database, $thumbQuality;
  190. $photoUrl = "../uploads/big/$photoName";
  191. $newUrl = "../uploads/thumb/$photoName";
  192. $thumbPhotoName = explode(".", $photoName);
  193. $newUrl2x = "../uploads/thumb/".$thumbPhotoName[0]."@2x.".$thumbPhotoName[1];
  194. $oldImg = getimagesize($photoUrl);
  195. $type = $oldImg['mime'];
  196. switch($type) {
  197. case "image/jpeg": $sourceImg = imagecreatefromjpeg($photoUrl); break;
  198. case "image/png": $sourceImg = imagecreatefrompng($photoUrl); break;
  199. case "image/gif": $sourceImg = imagecreatefromgif($photoUrl); break;
  200. default: return false;
  201. }
  202. $thumb = imagecreatetruecolor($width, $height);
  203. $thumb2x = imagecreatetruecolor($width2x, $height2x);
  204. if($oldImg[0]<$oldImg[1]) {
  205. $newSize = $oldImg[0];
  206. $startWidth = 0;
  207. $startHeight = $oldImg[1]/2 - $oldImg[0]/2;
  208. } else {
  209. $newSize = $oldImg[1];
  210. $startWidth = $oldImg[0]/2 - $oldImg[1]/2;
  211. $startHeight = 0;
  212. }
  213. imagecopyresampled($thumb,$sourceImg,0,0,$startWidth,$startHeight,$width,$height,$newSize,$newSize);
  214. imagecopyresampled($thumb2x,$sourceImg,0,0,$startWidth,$startHeight,$width2x,$height2x,$newSize,$newSize);
  215. switch($type) {
  216. case "image/jpeg": imagejpeg($thumb,$newUrl,$thumbQuality); imagejpeg($thumb2x,$newUrl2x,$thumbQuality); break;
  217. case "image/png": imagepng($thumb,$newUrl); imagepng($thumb2x,$newUrl2x); break;
  218. case "image/gif": imagegif($thumb,$newUrl); imagegif($thumb2x,$newUrl2x); break;
  219. default: return false;
  220. }
  221. return true;
  222. }
  223. // Session Functions
  224. function init($mode) {
  225. global $checkForUpdates, $bitlyUsername;
  226. $return["config"]["checkForUpdates"] = $checkForUpdates;
  227. $return["config"]["bitlyUsername"] = $bitlyUsername;
  228. if ($mode=="admin") $return["loggedIn"] = true;
  229. else $return["loggedIn"] = false;
  230. return $return;
  231. }
  232. function login($loginUser, $loginPassword) {
  233. global $database, $user, $password;
  234. if ($loginUser==$user&&$loginPassword==md5($password)) {
  235. // Admin Login
  236. $_SESSION['login'] = true;
  237. return true;
  238. } else {
  239. return false;
  240. }
  241. }
  242. function logout() {
  243. session_destroy();
  244. return true;
  245. }
  246. // Album Functions
  247. function addAlbum($title) {
  248. global $database;
  249. $title = mysqli_real_escape_string($database, $title);
  250. if(strlen($title)<1||strlen($title)>30) return false;
  251. $sysdate = date("d.m.Y");
  252. $query = "INSERT INTO lychee_albums (title, sysdate) VALUES ('$title', '$sysdate');";
  253. $result = $database->query($query);
  254. if(!$result) return false;
  255. return $database->insert_id;
  256. }
  257. function getAlbums($public) {
  258. global $database, $sorting;
  259. // Smart Albums
  260. if (!$public) $return = getSmartInfo();
  261. // Albums
  262. if ($public) $query = "SELECT * FROM lychee_albums WHERE public = 1 ORDER BY id $sorting;";
  263. else $query = "SELECT * FROM lychee_albums ORDER BY id $sorting;";
  264. $result = $database->query($query) OR die("Error: $result <br>".$database->error);
  265. $i=0;
  266. while($row = $result->fetch_object()) {
  267. $return["album"][$i]['id'] = $row->id;
  268. $return["album"][$i]['title'] = $row->title;
  269. $return["album"][$i]['public'] = $row->public;
  270. $return["album"][$i]['sysdate'] = $row->sysdate;
  271. if ($row->password=="") $return["album"][$i]['password'] = false;
  272. else $return["album"][$i]['password'] = true;
  273. // Thumbs
  274. if (($public&&$row->password=="")||(!$public)) {
  275. $albumID = $row->id;
  276. $query = "SELECT thumbUrl FROM lychee_photos WHERE album = '$albumID' ORDER BY id $sorting LIMIT 0, 3;";
  277. $result2 = $database->query($query);
  278. $k = 0;
  279. while($row2 = $result2->fetch_object()){
  280. $return["album"][$i]["thumb$k"] = $row2->thumbUrl;
  281. $k++;
  282. }
  283. if(!isset($return["album"][$i]["thumb0"]))$return["album"][$i]["thumb0"]="";
  284. if(!isset($return["album"][$i]["thumb1"]))$return["album"][$i]["thumb1"]="";
  285. if(!isset($return["album"][$i]["thumb2"]))$return["album"][$i]["thumb2"]="";
  286. }
  287. $i++;
  288. }
  289. if($i==0) $return["albums"] = false;
  290. else $return["albums"] = true;
  291. return $return;
  292. }
  293. function getSmartInfo() {
  294. global $database, $sorting;
  295. $query = "SELECT * FROM lychee_photos WHERE album = 0 ORDER BY id $sorting;";
  296. $result = $database->query($query);
  297. $i = 0;
  298. while($row = $result->fetch_object()) {
  299. if($i<3) $return["unsortThumb$i"] = $row->thumbUrl;
  300. $i++;
  301. }
  302. $return['unsortNum'] = $i;
  303. $query2 = "SELECT * FROM lychee_photos WHERE public = 1 ORDER BY id $sorting;";
  304. $result2 = $database->query($query2);
  305. $i = 0;
  306. while($row2 = $result2->fetch_object()) {
  307. if($i<3) $return["publicThumb$i"] = $row2->thumbUrl;
  308. $i++;
  309. }
  310. $return['publicNum'] = $i;
  311. $query3 = "SELECT * FROM lychee_photos WHERE star = 1 ORDER BY id $sorting;";
  312. $result3 = $database->query($query3);
  313. $i = 0;
  314. while($row3 = $result3->fetch_object()) {
  315. if($i<3) $return["starredThumb$i"] = $row3->thumbUrl;
  316. $i++;
  317. }
  318. $return['starredNum'] = $i;
  319. return $return;
  320. }
  321. function getAlbumInfo($albumID) {
  322. global $database;
  323. $query = "SELECT * FROM lychee_albums WHERE id = '$albumID';";
  324. $result = $database->query($query);
  325. $row = $result->fetch_object();
  326. $return['title'] = $row->title;
  327. $return['date'] = $row->sysdate;
  328. $return['public'] = $row->public;
  329. $query = "SELECT COUNT(*) AS num FROM lychee_photos WHERE album = '$albumID';";
  330. $result = $database->query($query);
  331. $row = $result->fetch_object();
  332. $return['num'] = $row->num;
  333. return $return;
  334. }
  335. function setAlbumTitle($albumID, $title) {
  336. global $database;
  337. $title = mysqli_real_escape_string($database, urldecode($title));
  338. if(strlen($title)<1||strlen($title)>30) return false;
  339. $query = "UPDATE lychee_albums SET title = '$title' WHERE id = '$albumID';";
  340. $result = $database->query($query);
  341. if(!$result) return false;
  342. return true;
  343. }
  344. function deleteAlbum($albumID, $delAll) {
  345. global $database;
  346. if($delAll=="true") {
  347. $query = "SELECT id FROM lychee_photos WHERE album = '$albumID';";
  348. $result = $database->query($query);
  349. $error = false;
  350. while($row = $result->fetch_object()) {
  351. if(!deletePhoto($row->id)) $error = true;
  352. }
  353. } else {
  354. $query = "UPDATE lychee_photos SET album = '0' WHERE album = '$albumID';";
  355. $result = $database->query($query);
  356. if(!$result) return false;
  357. }
  358. if($albumID!=0) {
  359. $query = "DELETE FROM lychee_albums WHERE id = '$albumID';";
  360. $result = $database->query($query);
  361. if(!$result) return false;
  362. }
  363. if($error) return false;
  364. return true;
  365. }
  366. function getAlbumArchive($albumID) {
  367. global $database;
  368. switch($albumID) {
  369. case 's':
  370. $query = "SELECT * FROM lychee_photos WHERE public = '1';";
  371. $zipTitle = "Public";
  372. break;
  373. case 'f':
  374. $query = "SELECT * FROM lychee_photos WHERE star = '1';";
  375. $zipTitle = "Starred";
  376. break;
  377. default:
  378. $query = "SELECT * FROM lychee_photos WHERE album = '$albumID';";
  379. $zipTitle = "Unsorted";
  380. }
  381. $result = $database->query($query);
  382. $files = array();
  383. $i=0;
  384. while($row = $result->fetch_object()) {
  385. $files[$i] = "../".$row->url;
  386. $i++;
  387. }
  388. $query = "SELECT * FROM lychee_albums WHERE id = '$albumID';";
  389. $result = $database->query($query);
  390. $row = $result->fetch_object();
  391. if($albumID!=0&&is_numeric($albumID))$zipTitle = $row->title;
  392. $filename = "../uploads/".$zipTitle.".zip";
  393. $zip = new ZipArchive();
  394. if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {
  395. return false;
  396. }
  397. foreach($files AS $zipFile) {
  398. $newFile = explode("/",$zipFile);
  399. $newFile = array_reverse($newFile);
  400. $zip->addFile($zipFile, $zipTitle."/".$newFile[0]);
  401. }
  402. $zip->close();
  403. header("Content-Type: application/zip");
  404. header("Content-Disposition: attachment; filename=\"$zipTitle.zip\"");
  405. header("Content-Length: ".filesize($filename));
  406. readfile($filename);
  407. unlink($filename);
  408. return true;
  409. }
  410. function setAlbumPublic($albumID) {
  411. global $database;
  412. $query = "SELECT public FROM lychee_albums WHERE id = '$albumID';";
  413. $result = $database->query($query);
  414. $row = $result->fetch_object();
  415. if($row->public == 0){
  416. $public = 1;
  417. }else{
  418. $public = 0;
  419. }
  420. $query = "UPDATE lychee_albums SET public = '$public', password = NULL WHERE id = '$albumID';";
  421. $result = $database->query($query);
  422. if(!$result) return false;
  423. return true;
  424. }
  425. function setAlbumPassword($albumID, $password) {
  426. global $database;
  427. $query = "UPDATE lychee_albums SET password = '$password' WHERE id = '$albumID';";
  428. $result = $database->query($query);
  429. if(!$result) return false;
  430. return true;
  431. }
  432. function isAlbumPublic($albumID, $password) {
  433. global $database;
  434. $query = "SELECT public, password FROM lychee_albums WHERE id = '$albumID';";
  435. $result = $database->query($query);
  436. $row = $result->fetch_object();
  437. if(($row->public == 1) && ($row->password == $password)){
  438. return true;
  439. }else{
  440. return false;
  441. }
  442. }
  443. // Photo Functions
  444. function getPhotos($albumID) {
  445. global $database, $sorting;
  446. switch($albumID) {
  447. case "f": $query = "SELECT id, title, sysdate, public, star, album, thumbUrl FROM lychee_photos WHERE star = 1 ORDER BY id $sorting;";
  448. break;
  449. case "s": $query = "SELECT id, title, sysdate, public, star, album, thumbUrl FROM lychee_photos WHERE public = 1 ORDER BY id $sorting;";
  450. break;
  451. default: $query = "SELECT id, title, sysdate, public, star, album, thumbUrl FROM lychee_photos WHERE album = '$albumID' ORDER BY id $sorting;";
  452. }
  453. $result = $database->query($query);
  454. $i = 0;
  455. while($row = $result->fetch_array()) {
  456. $return[$i] = $row;
  457. $i++;
  458. }
  459. if($i==0) return false;
  460. return $return;
  461. }
  462. function getPhotoInfo($photoID) {
  463. global $database;
  464. if(!is_numeric($photoID)) {
  465. $query = "SELECT COUNT(*) AS quantity FROM lychee_photos WHERE import_name = '../uploads/import/$photoID';";
  466. $result = $database->query($query);
  467. $row = $result->fetch_object();
  468. if($row->quantity == 0) {
  469. importPhoto($photoID, 's');
  470. }
  471. if(is_file("../uploads/import/$photoID")) {
  472. importPhoto($photoID, 's');
  473. }
  474. $query = "SELECT * FROM lychee_photos WHERE import_name = '../uploads/import/$photoID' ORDER BY ID DESC;";
  475. } else {
  476. $query = "SELECT * FROM lychee_photos WHERE id = '$photoID';";
  477. }
  478. $result = $database->query($query);
  479. $return = $result->fetch_array();
  480. return $return;
  481. }
  482. function downloadPhoto($photoID) {
  483. global $database;
  484. $query = "SELECT * FROM lychee_photos WHERE id = '$photoID';";
  485. $result = $database->query($query);
  486. $row = $result->fetch_object();
  487. $photo = "../".$row->url;
  488. $title = $row->title;
  489. $type = "appcication/zip";
  490. $filename = "./imageDownload.zip";
  491. $zip = new ZipArchive();
  492. if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) return false;
  493. $newFile = explode("/",$photo);
  494. $newFile = array_reverse($newFile);
  495. $zip->addFile($photo, $title.$newFile[0]);
  496. $zip->close();
  497. header("Content-Type: $type");
  498. header("Content-Disposition: attachment; filename=\"$title.zip\"");
  499. readfile($filename);
  500. unlink($filename);
  501. return true;
  502. }
  503. function setPhotoPublic($photoID, $url) {
  504. global $database;
  505. $query = "SELECT public, shortlink FROM lychee_photos WHERE id = '$photoID';";
  506. $result = $database->query($query);
  507. $row = $result->fetch_object();
  508. if($row->public == 0){
  509. $public = 1;
  510. }else{
  511. $public = 0;
  512. }
  513. if($public==0 || preg_match('/localhost/', $_SERVER['HTTP_REFERER']) || preg_match('\file:\/\/\/', $_SERVER['HTTP_REFERER'])) {
  514. $shortlink = "";
  515. }else{
  516. if($row->shortlink==""){
  517. $shortlink = urlShortner($url);
  518. }else{
  519. $shortlink = $row->shortlink;
  520. }
  521. }
  522. $query = "UPDATE lychee_photos SET public = '$public', shortlink = '$shortlink' WHERE id = '$photoID';";
  523. $result = $database->query($query);
  524. if(!$result) return false;
  525. return true;
  526. }
  527. function setPhotoStar($photoID) {
  528. global $database;
  529. $query = "SELECT star FROM lychee_photos WHERE id = '$photoID';";
  530. $result = $database->query($query);
  531. $row = $result->fetch_object();
  532. if($row->star == 0) {
  533. $star = 1;
  534. } else {
  535. $star = 0;
  536. }
  537. $query = "UPDATE lychee_photos SET star = '$star' WHERE id = '$photoID';";
  538. $result = $database->query($query);
  539. return true;
  540. }
  541. function nextPhoto($photoID, $albumID, $innerCall) {
  542. global $database, $sorting;
  543. if (!$innerCall&&$sorting=="ASC") return previousPhoto($photoID, $albumID, true);
  544. switch($albumID) {
  545. case 'f': $query = "SELECT * FROM lychee_photos WHERE id < '$photoID' AND star = '1' ORDER BY id DESC LIMIT 0, 1;";
  546. break;
  547. case 's': $query = "SELECT * FROM lychee_photos WHERE id < '$photoID' AND public = '1' ORDER BY id DESC LIMIT 0, 1;";
  548. break;
  549. default: $query = "SELECT * FROM lychee_photos WHERE id < '$photoID' AND album = '$albumID' ORDER BY id DESC LIMIT 0, 1;";
  550. }
  551. $result = $database->query($query);
  552. $return = $result->fetch_array();
  553. if(!$return || ($return==0)) {
  554. switch($albumID) {
  555. case 'f': $query = "SELECT * FROM lychee_photos WHERE star = '1' ORDER BY id DESC LIMIT 0, 1;";
  556. break;
  557. case 's': $query = "SELECT * FROM lychee_photos WHERE public = '1' ORDER BY id DESC LIMIT 0, 1;";
  558. break;
  559. default: $query = "SELECT * FROM lychee_photos WHERE album = '$albumID' ORDER BY id DESC LIMIT 0, 1;";
  560. }
  561. $result = $database->query($query);
  562. $return = $result->fetch_array();
  563. }
  564. return $return;
  565. }
  566. function previousPhoto($photoID, $albumID, $innerCall) {
  567. global $database, $sorting;
  568. if (!$innerCall&&$sorting=="ASC") return nextPhoto($photoID, $albumID, true);
  569. switch($albumID) {
  570. case 'f': $query = "SELECT * FROM lychee_photos WHERE id > '$photoID' AND star = '1' ORDER BY id LIMIT 0, 1;";
  571. break;
  572. case 's': $query = "SELECT * FROM lychee_photos WHERE id > '$photoID' AND public = '1' ORDER BY id LIMIT 0, 1;";
  573. break;
  574. default: $query = "SELECT * FROM lychee_photos WHERE id > '$photoID' AND album = '$albumID' ORDER BY id LIMIT 0, 1;";
  575. }
  576. $result = $database->query($query);
  577. $return = $result->fetch_array();
  578. if(!$return || ($return==0)) {
  579. switch($albumID) {
  580. case 'f': $query = "SELECT * FROM lychee_photos WHERE star = '1' ORDER BY id LIMIT 0, 1;";
  581. break;
  582. case 's': $query = "SELECT * FROM lychee_photos WHERE public = '1' ORDER BY id LIMIT 0, 1;";
  583. break;
  584. default: $query = "SELECT * FROM lychee_photos WHERE album = '$albumID' ORDER BY id LIMIT 0, 1;";
  585. }
  586. $result = $database->query($query);
  587. $return = $result->fetch_array();
  588. }
  589. return $return;
  590. }
  591. function setAlbum($photoID, $newAlbum) {
  592. global $database;
  593. $query = "UPDATE lychee_photos SET album = '$newAlbum' WHERE id = '$photoID';";
  594. $result = $database->query($query);
  595. if(!$result) return false;
  596. else return true;
  597. }
  598. function setPhotoTitle($photoID, $title) {
  599. global $database;
  600. $title = mysqli_real_escape_string($database, urldecode($title));
  601. if(strlen($title)>30) return false;
  602. $query = "UPDATE lychee_photos SET title = '$title' WHERE id = '$photoID';";
  603. $result = $database->query($query);
  604. if(!$result) return false;
  605. else return true;
  606. }
  607. function setPhotoDescription($photoID, $description) {
  608. global $database;
  609. $description = mysqli_real_escape_string($database, htmlentities($description));
  610. if(strlen($description)>160) return false;
  611. $query = "UPDATE lychee_photos SET description = '$description' WHERE id = '$photoID';";
  612. $result = $database->query($query);
  613. if(!$result) return false;
  614. return true;
  615. }
  616. function deletePhoto($photoID) {
  617. global $database;
  618. $query = "SELECT * FROM lychee_photos WHERE id = '$photoID';";
  619. $result = $database->query($query);
  620. if(!$result) return false;
  621. $row = $result->fetch_object();
  622. $retinaUrl = explode(".", $row->thumbUrl);
  623. $unlink1 = unlink("../".$row->url);
  624. $unlink2 = unlink("../".$row->thumbUrl);
  625. $unlink3 = unlink("../".$retinaUrl[0].'@2x.'.$retinaUrl[1]);
  626. $query = "DELETE FROM lychee_photos WHERE id = '$photoID';";
  627. $result = $database->query($query);
  628. if(!$unlink1 || !$unlink2 || !$unlink3) return false;
  629. if(!$result) return false;
  630. return true;
  631. }
  632. function importPhoto($name, $albumID) {
  633. $tmp_name = "../uploads/import/$name";
  634. $details = getimagesize($tmp_name);
  635. $size = filesize($tmp_name);
  636. $nameFile = array(array());
  637. $nameFile[0]['name'] = $name;
  638. $nameFile[0]['type'] = $details['mime'];
  639. $nameFile[0]['tmp_name'] = $tmp_name;
  640. $nameFile[0]['error'] = 0;
  641. $nameFile[0]['size'] = $size;
  642. if(!upload($nameFile, $albumID)) return false;
  643. else return true;
  644. }
  645. function importUrl($url, $albumID) {
  646. if (@getimagesize($url)) {
  647. $pathinfo = pathinfo($url);
  648. $filename = $pathinfo['filename'].".".$pathinfo['extension'];
  649. $tmp_name = "../uploads/import/$filename";
  650. copy($url, $tmp_name);
  651. return importPhoto($filename, $albumID);
  652. } else {
  653. return false;
  654. }
  655. }
  656. // Share Functions
  657. function urlShortner($url) {
  658. global $database, $bitlyUsername, $bitlyApi;
  659. if($bitlyUsername==""||$bitlyApi=="") return false;
  660. $url = urlencode($url);
  661. $bitlyAPI = "http://api.bit.ly/shorten?version=2.0.1&format=xml&longUrl=$url&login=$bitlyUsername&apiKey=$bitlyApi";
  662. $data = file_get_contents($bitlyAPI);
  663. $xml = simplexml_load_string($data);
  664. $shortlink = $xml->results->nodeKeyVal->shortUrl;
  665. return $shortlink;
  666. }
  667. function getShortlink($photoID) {
  668. global $database;
  669. $query = "SELECT * FROM lychee_photos WHERE id = '$photoID';";
  670. $result = $database->query($query);
  671. $row = $result->fetch_object();
  672. return $row->shortlink;
  673. }
  674. function facebookHeader($photoID) {
  675. $database = dbConnect();
  676. if(!is_numeric($photoID)) return false;
  677. $query = "SELECT * FROM lychee_photos WHERE id = '$photoID';";
  678. $result = $database->query($query);
  679. $row = $result->fetch_object();
  680. $parseUrl = parse_url("http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
  681. $thumb = $parseUrl['scheme']."://".$parseUrl['host'].$parseUrl['path']."/../".$row->thumbUrl;
  682. $return = '<meta name="title" content="'.$row->title.'" />';
  683. $return .= '<meta name="description" content="'.$row->description.' - via Lychee" />';
  684. $return .= '<link rel="image_src" type="image/jpeg" href="'. $thumb .'" />';
  685. return $return;
  686. }
  687. function isPhotoPublic($photoID, $password) {
  688. global $database;
  689. $photoID = mysqli_real_escape_string($database, $photoID);
  690. if(is_numeric($photoID)) {
  691. $query = "SELECT * FROM lychee_photos WHERE id = '$photoID';";
  692. } else {
  693. $query = "SELECT * FROM lychee_photos WHERE import_name = '../uploads/import/$photoID';";
  694. }
  695. $result = $database->query($query);
  696. $row = $result->fetch_object();
  697. if (!is_numeric($photoID)&&!$row) return true;
  698. if($row->public == 1) return true;
  699. else return isAlbumPublic($row->album, $password);
  700. }
  701. // Search Function
  702. function search($term) {
  703. global $database, $sorting;
  704. $term = mysqli_real_escape_string($database, $term);
  705. $query = "SELECT * FROM lychee_photos WHERE title like '%$term%' OR description like '%$term%';";
  706. $result = $database->query($query);
  707. while($row = $result->fetch_array()) {
  708. $return['photos'][] = $row;
  709. }
  710. $query = "SELECT * FROM lychee_albums WHERE title like '%$term%';";
  711. $result = $database->query($query);
  712. $i=0;
  713. while($row = $result->fetch_array()) {
  714. $return['albums'][$i] = $row;
  715. $query2 = "SELECT thumbUrl FROM lychee_photos WHERE album = '".$row['id']."' ORDER BY id $sorting LIMIT 0, 3;";
  716. $result2 = $database->query($query2);
  717. $k = 0;
  718. while($row2 = $result2->fetch_object()){
  719. $return['albums'][$i]["thumb$k"] = $row2->thumbUrl;
  720. $k++;
  721. }
  722. $i++;
  723. }
  724. return $return;
  725. }
  726. ?>