functions.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. <?php
  2. /**
  3. * @name functions.php
  4. * @author Philipp Maurer
  5. * @author Tobias Reich
  6. * @copyright 2012 by Philipp Maurer, Tobias Reich
  7. */
  8. include("config.php");
  9. // Database Functions
  10. function dbConnect() {
  11. global $db, $dbUser, $dbPassword;
  12. $connect = mysql_connect("localhost", $dbUser, $dbPassword);
  13. if(!$connect) {
  14. echo "No connection: ".mysql_error();
  15. return false;
  16. }
  17. $dbSelect = mysql_select_db($db);
  18. if(!$dbSelect) {
  19. echo "Can not find Database!";
  20. return false;
  21. }
  22. $query = "SELECT * FROM photos, albums;";
  23. if(!mysql_query($query)) createTables();
  24. return true;
  25. }
  26. function dbClose() {
  27. $close = mysql_close();
  28. if(!$close) {
  29. echo "Closing the connection failed!";
  30. return false;
  31. }
  32. return true;
  33. }
  34. function createTables() {
  35. $query = "CREATE TABLE IF NOT EXISTS `albums` (
  36. `id` int(11) NOT NULL AUTO_INCREMENT,
  37. `title` varchar(50) NOT NULL,
  38. `sysdate` varchar(10) NOT NULL,
  39. PRIMARY KEY (`id`)
  40. ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;";
  41. $result = mysql_query($query);
  42. if(!$result) return false;
  43. $query = "CREATE TABLE IF NOT EXISTS `photos` (
  44. `id` bigint(14) NOT NULL,
  45. `title` varchar(50) NOT NULL,
  46. `description` varchar(160) NOT NULL,
  47. `url` varchar(100) NOT NULL,
  48. `public` tinyint(1) NOT NULL,
  49. `shortlink` varchar(20) NOT NULL,
  50. `type` varchar(10) NOT NULL,
  51. `width` int(11) NOT NULL,
  52. `height` int(11) NOT NULL,
  53. `size` varchar(10) NOT NULL,
  54. `sysdate` varchar(10) NOT NULL,
  55. `systime` varchar(8) NOT NULL,
  56. `iso` varchar(15) NOT NULL,
  57. `aperture` varchar(10) NOT NULL,
  58. `make` varchar(20) NOT NULL,
  59. `model` varchar(50) NOT NULL,
  60. `shutter` varchar(10) NOT NULL,
  61. `focal` varchar(10) NOT NULL,
  62. `takedate` varchar(10) NOT NULL,
  63. `taketime` varchar(8) NOT NULL,
  64. `star` tinyint(1) NOT NULL,
  65. `album` varchar(30) NOT NULL DEFAULT '0',
  66. `thumbUrl` varchar(50) NOT NULL,
  67. PRIMARY KEY (`id`)
  68. ) ENGINE=MyISAM DEFAULT CHARSET=latin1;";
  69. $result = mysql_query($query);
  70. if(!$result) return false;
  71. return true;
  72. }
  73. // Upload Functions
  74. function upload($file, $albumID) {
  75. switch($albumID) {
  76. case 's':
  77. $public = 1;
  78. $star = 0;
  79. $albumID = 0;
  80. break;
  81. case 'f':
  82. $star = 1;
  83. $public = 0;
  84. $albumID = 0;
  85. break;
  86. default:
  87. $star = 0;
  88. $public = 0;
  89. }
  90. $id = str_replace('.', '', microtime(true));
  91. while(strlen($id)<14) $id .= 0;
  92. $tmp_name = $file['File']["tmp_name"];
  93. $type = getimagesize($tmp_name);
  94. if(($type[2]!=1)&&($type[2]!=2)&&($type[2]!=3)) return false;
  95. $data = $file['File']["name"];
  96. $data = explode('.',$data);
  97. $data = array_reverse ($data);
  98. $data = $data[0];
  99. move_uploaded_file($tmp_name, "../uploads/big/$id.$data");
  100. createThumb($id.".".$data);
  101. // Read infos
  102. $info = getCamera($id.".".$data);
  103. $title="";
  104. if(isset($info['type'])){$type=$info['type'];}else{$type="";}
  105. if(isset($info['width'])){$width=$info['width'];}else{$width="";}
  106. if(isset($info['height'])){$height=$info['height'] OR "";}else{$height="";}
  107. if(isset($info['size'])){$size=$info['size'] OR "";}else{$size="";}
  108. if(isset($info['date'])){$sysdate=$info['date'];}else{$sysdate="";}
  109. if(isset($info['time'])){$systime=$info['time'];}else{$systime="";}
  110. if(isset($info['iso'])){$iso=$info['iso'];}else{$iso="";}
  111. if(isset($info['aperture'])){$aperture=$info['aperture'];}else{$aperture="";}
  112. if(isset($info['make'])){$make=$info['make'];}else{$make="";}
  113. if(isset($info['model'])){$model=$info['model'] OR "";}else{$model="";}
  114. if(isset($info['shutter'])){$shutter=$info['shutter'];}else{$shutter="";}
  115. if(isset($info['focal'])){$focal=$info['focal'];}else{$focal="";}
  116. if(isset($info['takeDate'])){$takeDate=$info['takeDate'];}else{$takeDate="";}
  117. if(isset($info['takeTime'])){$takeTime=$info['takeTime'];}else{$takeTime="";}
  118. $query = "INSERT INTO photos (id, title, url, type, width, height, size, sysdate, systime, iso, aperture, make, model, shutter, focal, takedate, taketime, thumbUrl, album, public, star)
  119. 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');";
  120. $result = mysql_query($query);
  121. }
  122. function getCamera($photoID) {
  123. $return = array();
  124. $url = "../uploads/big/$photoID";
  125. $type = getimagesize($url);
  126. $type = $type['mime'];
  127. if(($type == "image/jpeg") && function_exists('exif_read_data') ){
  128. $exif = exif_read_data($url, "EXIF", 0);
  129. // General information
  130. $return['name'] = $exif['FileName'];
  131. $generalInfos = getimagesize($url);
  132. $return['type'] = $generalInfos['mime'];
  133. $return['width'] = $generalInfos[0];
  134. $return['height'] = $generalInfos[1];
  135. $size = (filesize($url) / 1024);
  136. if($size >= 1024){$size=round($size/1024,1)." MB";}else{$size=round($size,1)." KB";}
  137. $return['size'] = $size;
  138. $return['date'] = date("d.m.Y",filectime($url));
  139. $return['time'] = date("H:i:s",filectime($url));
  140. echo $exif['FileDateTime']."<br/>".$exif['DateTimeOriginal'];
  141. // Camera Information
  142. if(isset($exif['ISOSpeedRatings'])){$return['iso']="ISO-".$exif['ISOSpeedRatings'];}
  143. if(isset($exif['COMPUTED']['ApertureFNumber'])){$return['aperture']=$exif['COMPUTED']['ApertureFNumber'];}
  144. if(isset($exif['Make'])){$return['make']=$exif['Make'];}
  145. if(isset($exif['Model'])){$return['model']=$exif['Model'];}
  146. if(isset($exif['ExposureTime'])){$return['shutter']=$exif['ExposureTime']." Sek.";}
  147. if(isset($exif['FocalLength'])){$return['focal']=($exif['FocalLength']/1)." mm";}
  148. if(isset($exif['Software'])){$return['software']=$exif['Software'];}
  149. if(isset($exif['DateTimeOriginal'])) {
  150. $exifDate = explode(" ",$exif['DateTimeOriginal']);
  151. $date = explode(":", $exifDate[0]); $return['takeDate'] = $date[2].".".$date[1].".".$date[0];
  152. $return['takeTime'] = $exifDate[1];
  153. }
  154. }else{
  155. $exif = getimagesize($url);
  156. $return['type'] = $exif['mime'];
  157. $return['width'] = $exif[0];
  158. $return['height'] = $exif[1];
  159. $size = (filesize($url) / 1024);
  160. if($size >= 1024){$size=round($size/1024,1)." MB";}else{$size=round($size,1)." KB";}
  161. $return['size'] = $size;
  162. $return['date'] = date("d.m.Y",filectime($url));
  163. $return['time'] = date("H:i:s",filectime($url));
  164. }
  165. return $return;
  166. }
  167. function createThumb($photoName, $width = 200, $height = 200) {
  168. global $thumbQuality;
  169. $photoUrl = "../uploads/big/$photoName";
  170. $newUrl = "../uploads/thumb/$photoName";
  171. $oldImg = getimagesize($photoUrl);
  172. $type = $oldImg['mime'];
  173. switch($type) {
  174. case "image/jpeg": $sourceImg = imagecreatefromjpeg($photoUrl); break;
  175. case "image/png": $sourceImg = imagecreatefrompng($photoUrl); break;
  176. case "image/gif": $sourceImg = imagecreatefromgif($photoUrl); break;
  177. default: return false;
  178. }
  179. $thumb = imagecreatetruecolor($width, $height);
  180. if($oldImg[0]<$oldImg[1]) {
  181. $newSize = $oldImg[0];
  182. $startWidth = 0;
  183. $startHeight = $oldImg[1]/2 - $oldImg[0]/2;
  184. } else {
  185. $newSize = $oldImg[1];
  186. $startWidth = $oldImg[0]/2 - $oldImg[1]/2;
  187. $startHeight = 0;
  188. }
  189. imagecopyresampled($thumb,$sourceImg,0,0,$startWidth,$startHeight,$width,$height,$newSize,$newSize);
  190. switch($type) {
  191. case "image/jpeg": imagejpeg($thumb,$newUrl,$thumbQuality); break;
  192. case "image/png": imagepng($thumb,$newUrl); break;
  193. case "image/gif": imagegif($thumb,$newUrl); break;
  194. default: return false;
  195. }
  196. return true;
  197. }
  198. // Session Functions
  199. function login($loginUser, $loginPassword) {
  200. global $user, $password;
  201. if(($loginUser == $user) && ($loginPassword == $password)){
  202. $_SESSION['login'] = true;
  203. return true;
  204. } else {
  205. return false;
  206. }
  207. }
  208. function logout() {
  209. session_destroy();
  210. return true;
  211. }
  212. // Album Functions
  213. function addAlbum($title) {
  214. $title = mysql_escape_string($title);
  215. $sysdate = date("d.m.Y");
  216. $query = "INSERT INTO albums (title, sysdate) VALUES ('$title', '$sysdate');";
  217. $result = mysql_query($query);
  218. if(!$result) return false;
  219. return mysql_insert_id();
  220. }
  221. function getAlbums() {
  222. $return = array(array());
  223. $query = "SELECT id, title, sysdate FROM albums ORDER BY id DESC;";
  224. $result = mysql_query($query) OR die("Error: $result <br>".mysql_error());
  225. $i=0;
  226. while($row = mysql_fetch_object($result)) {
  227. $return[$i]['id'] = $row->id;
  228. $return[$i]['title'] = $row->title;
  229. $return[$i]['sysdate'] = $row->sysdate;
  230. $albumID = $row->id;
  231. $query = "SELECT thumbUrl FROM photos WHERE album = '$albumID' ORDER BY id DESC LIMIT 0, 3;";
  232. $result2 = mysql_query($query);
  233. $k = 0;
  234. while($row2 = mysql_fetch_object($result2)){
  235. $return[$i]["thumb$k"] = $row2->thumbUrl;
  236. $k++;
  237. }
  238. if(!isset($return[$i]["thumb0"]))$return[$i]["thumb0"]="";
  239. if(!isset($return[$i]["thumb1"]))$return[$i]["thumb1"]="";
  240. if(!isset($return[$i]["thumb2"]))$return[$i]["thumb2"]="";
  241. $i++;
  242. }
  243. if($i==0) return false;
  244. return $return;
  245. }
  246. function getSmartInfo() {
  247. $return = array();
  248. $query = "SELECT * FROM photos WHERE album = 0 ORDER BY id DESC;";
  249. $result = mysql_query($query);
  250. $i = 0;
  251. while($row = mysql_fetch_object($result)) {
  252. if($i<3) $return["unsortThumb$i"] = $row->thumbUrl;
  253. $i++;
  254. }
  255. $return['unsortNum'] = $i;
  256. $query2 = "SELECT * FROM photos WHERE public = 1 ORDER BY id DESC;";
  257. $result2 = mysql_query($query2);
  258. $i = 0;
  259. while($row2 = mysql_fetch_object($result2)) {
  260. if($i<3) $return["publicThumb$i"] = $row2->thumbUrl;
  261. $i++;
  262. }
  263. $return['publicNum'] = $i;
  264. $query3 = "SELECT * FROM photos WHERE star = 1 ORDER BY id DESC;";
  265. $result3 = mysql_query($query3);
  266. $i = 0;
  267. while($row3 = mysql_fetch_object($result3)) {
  268. if($i<3) $return["starredThumb$i"] = $row3->thumbUrl;
  269. $i++;
  270. }
  271. $return['starredNum'] = $i;
  272. return $return;
  273. }
  274. function getAlbumInfo($albumID) {
  275. $return = array();
  276. $query = "SELECT * FROM albums WHERE id = '$albumID';";
  277. $result = mysql_query($query);
  278. $row = mysql_fetch_object($result);
  279. $return['title'] = $row->title;
  280. $return['date'] = $row->sysdate;
  281. $return['star'] = $row->star;
  282. $return['public'] = $row->public;
  283. $query = "SELECT COUNT(*) AS num FROM photos WHERE album = '$albumID';";
  284. $result = mysql_query($query);
  285. $row = mysql_fetch_object($result);
  286. $return['num'] = $row->num;
  287. return $return;
  288. }
  289. function setAlbumTitle($albumID, $title) {
  290. $title = mysql_real_escape_string(urldecode($title));
  291. if(strlen($title)<3||strlen($title)>30) return false;
  292. $query = "UPDATE albums SET title = '$title' WHERE id = '$albumID';";
  293. $result = mysql_query($query);
  294. if(!$result) return false;
  295. return true;
  296. }
  297. function deleteAlbum($albumID, $delAll) {
  298. if($delAll=="true") {
  299. $query = "SELECT id FROM photos WHERE album = '$albumID';";
  300. $result = mysql_query($query);
  301. $error = false;
  302. while($row = mysql_fetch_object($result)) {
  303. if(!deletePhoto($row->id)) $error = true;
  304. }
  305. } else {
  306. $query = "UPDATE photos SET album = '0' WHERE album = '$albumID';";
  307. $result = mysql_query($query);
  308. if(!$result) return false;
  309. }
  310. if($albumID!=0) {
  311. $query = "DELETE FROM albums WHERE id = '$albumID';";
  312. $result = mysql_query($query);
  313. if(!$result) return false;
  314. }
  315. if($error) return false;
  316. return true;
  317. }
  318. function getAlbumArchive($albumID) {
  319. switch($albumID) {
  320. case 's':
  321. $query = "SELECT * FROM photos WHERE public = '1';";
  322. $zipTitle = "Public";
  323. break;
  324. case 'f':
  325. $query = "SELECT * FROM photos WHERE star = '1';";
  326. $zipTitle = "Starred";
  327. break;
  328. default:
  329. $query = "SELECT * FROM photos WHERE album = '$albumID';";
  330. $zipTitle = "Unsorted";
  331. }
  332. $result = mysql_query($query);
  333. $files = array();
  334. $i=0;
  335. while($row = mysql_fetch_object($result)) {
  336. $files[$i] = "../".$row->url;
  337. $i++;
  338. }
  339. $query = "SELECT * FROM albums WHERE id = '$albumID';";
  340. $result = mysql_query($query);
  341. $row = mysql_fetch_object($result);
  342. if($albumID!=0&&is_numeric($albumID))$zipTitle = $row->title;
  343. $filename = "./".$zipTitle.".zip";
  344. $zip = new ZipArchive();
  345. if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {
  346. return false;
  347. }
  348. foreach($files AS $zipFile) {
  349. $newFile = explode("/",$zipFile);
  350. $newFile = array_reverse($newFile);
  351. $zip->addFile($zipFile, $zipTitle."/".$newFile[0]);
  352. }
  353. $zip->close();
  354. header("Content-Type: application/zip");
  355. header("Content-Disposition: attachment; filename=\"$zipTitle.zip\"");
  356. readfile($filename);
  357. unlink($filename);
  358. return true;
  359. }
  360. // Photo Functions
  361. function getPhotos($albumID) {
  362. switch($albumID) {
  363. case "f": $query = "SELECT * FROM photos WHERE star = 1 ORDER BY id DESC;";
  364. break;
  365. case "s": $query = "SELECT * FROM photos WHERE public = 1 ORDER BY id DESC;";
  366. break;
  367. default: $query = "SELECT * FROM photos WHERE album = '$albumID' ORDER BY id DESC;";
  368. }
  369. $result = mysql_query($query);
  370. $return = array(array());
  371. $i = 0;
  372. while($row = mysql_fetch_array($result)) {
  373. $return[$i] = $row;
  374. $i++;
  375. }
  376. if($i==0) return false;
  377. return $return;
  378. }
  379. function getPhotoInfo($photoID) {
  380. $query = "SELECT * FROM photos WHERE id = '$photoID';";
  381. $result = mysql_query($query);
  382. $return = mysql_fetch_array($result);
  383. return $return;
  384. }
  385. function downloadPhoto($photoID) {
  386. $query = "SELECT * FROM photos WHERE id = '$photoID';";
  387. $result = mysql_query($query);
  388. $row = mysql_fetch_object($result);
  389. $photo = "../".$row->url;
  390. $title = $row->title;
  391. $type = "appcication/zip";
  392. $filename = "./imageDownload.zip";
  393. $zip = new ZipArchive();
  394. if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) return false;
  395. $newFile = explode("/",$photo);
  396. $newFile = array_reverse($newFile);
  397. $zip->addFile($photo, $title.$newFile[0]);
  398. $zip->close();
  399. header("Content-Type: $type");
  400. header("Content-Disposition: attachment; filename=\"$title.zip\"");
  401. readfile($filename);
  402. unlink($filename);
  403. return true;
  404. }
  405. function countPhotos() {
  406. $query = "SELECT COUNT(*) AS num FROM photos;";
  407. $result = mysql_query($query);
  408. $row = mysql_fetch_object($result);
  409. return $row->num;
  410. }
  411. function setPhotoPublic($photoID, $url) {
  412. $query = "SELECT public, shortlink FROM photos WHERE id = '$photoID';";
  413. $row = mysql_fetch_object(mysql_query($query));
  414. if($row->public == 0){
  415. $public = 1;
  416. }else{
  417. $public = 0;
  418. }
  419. if(preg_match('/localhost/', $_SERVER['HTTP_REFERER'])) {
  420. $shortlink = "";
  421. }else{
  422. if($row->shortlink==""){
  423. $shortlink = urlShortner($url);
  424. }else{
  425. $shortlink = $row->shortlink;
  426. }
  427. }
  428. $query = "UPDATE photos SET public = '$public', shortlink = '$shortlink' WHERE id = '$photoID';";
  429. $result = mysql_query($query);
  430. if(!$result) return false;
  431. return true;
  432. }
  433. function setPhotoStar($photoID) {
  434. $query = "SELECT star FROM photos WHERE id = '$photoID';";
  435. $row = mysql_fetch_object(mysql_query($query));
  436. if($row->star == 0) {
  437. $star = 1;
  438. } else {
  439. $star = 0;
  440. }
  441. $query = "UPDATE photos SET star = '$star' WHERE id = '$photoID';";
  442. $result = mysql_query($query);
  443. return true;
  444. }
  445. function nextPhoto($photoID, $albumID) {
  446. switch($albumID) {
  447. case 'f': $query = "SELECT * FROM photos WHERE id < '$photoID' AND star = '1' ORDER BY id DESC LIMIT 0, 1;";
  448. break;
  449. case 's': $query = "SELECT * FROM photos WHERE id < '$photoID' AND public = '1' ORDER BY id DESC LIMIT 0, 1;";
  450. break;
  451. default: $query = "SELECT * FROM photos WHERE id < '$photoID' AND album = '$albumID' ORDER BY id DESC LIMIT 0, 1;";
  452. }
  453. $result = mysql_query($query);
  454. $return = mysql_fetch_array($result);
  455. if(!$return || ($return==0)) {
  456. switch($albumID) {
  457. case 'f': $query = "SELECT * FROM photos WHERE star = '1' ORDER BY id DESC LIMIT 0, 1;";
  458. break;
  459. case 's': $query = "SELECT * FROM photos WHERE public = '1' ORDER BY id DESC LIMIT 0, 1;";
  460. break;
  461. default: $query = "SELECT * FROM photos WHERE album = '$albumID' ORDER BY id DESC LIMIT 0, 1;";
  462. }
  463. $result = mysql_query($query);
  464. $return = mysql_fetch_array($result);
  465. }
  466. return $return;
  467. }
  468. function previousPhoto($photoID, $albumID) {
  469. switch($albumID) {
  470. case 'f': $query = "SELECT * FROM photos WHERE id > '$photoID' AND star = '1' LIMIT 0, 1;";
  471. break;
  472. case 's': $query = "SELECT * FROM photos WHERE id > '$photoID' AND public = '1' LIMIT 0, 1;";
  473. break;
  474. default: $query = "SELECT * FROM photos WHERE id > '$photoID' AND album = '$albumID' LIMIT 0, 1;";
  475. }
  476. $result = mysql_query($query);
  477. $return = mysql_fetch_array($result);
  478. if(!$return || ($return==0)) {
  479. switch($albumID) {
  480. case 'f': $query = "SELECT * FROM photos WHERE star = '1' ORDER BY id LIMIT 0, 1;";
  481. break;
  482. case 's': $query = "SELECT * FROM photos WHERE public = '1' ORDER BY id LIMIT 0, 1;";
  483. break;
  484. default: $query = "SELECT * FROM photos WHERE album = '$albumID' ORDER BY id LIMIT 0, 1;";
  485. }
  486. $result = mysql_query($query);
  487. $return = mysql_fetch_array($result);
  488. }
  489. return $return;
  490. }
  491. function movePhoto($photoID, $newAlbum) {
  492. $query = "UPDATE photos SET album = '$newAlbum' WHERE id = '$photoID';";
  493. $result = mysql_query($query);
  494. if(!$result) return false;
  495. else return true;
  496. }
  497. function setPhotoTitle($photoID, $title) {
  498. $title = mysql_real_escape_string(urldecode($title));
  499. if(strlen($title)<3||strlen($title)>30) return false;
  500. $query = "UPDATE photos SET title = '$title' WHERE id = '$photoID';";
  501. $result = mysql_query($query);
  502. if(!$result) return false;
  503. else return true;
  504. }
  505. function setPhotoDescription($photoID, $description) {
  506. $description = mysql_real_escape_string(htmlentities($description));
  507. if(strlen($description)>160) return false;
  508. $query = "UPDATE photos SET description = '$description' WHERE id = '$photoID';";
  509. $result = mysql_query($query);
  510. if(!$result) return false;
  511. return true;
  512. }
  513. function deletePhoto($photoID) {
  514. $query = "SELECT * FROM photos WHERE id = '$photoID';";
  515. $result = mysql_query($query);
  516. if(!$result) return false;
  517. $row = mysql_fetch_object($result);
  518. $unlink1 = unlink("../".$row->url);
  519. $unlink2 = unlink("../".$row->thumbUrl);
  520. if(!$unlink1 || !$unlink2) return false;
  521. $query = "DELETE FROM photos WHERE id = '$photoID';";
  522. $result = mysql_query($query);
  523. if(!$result) return false;
  524. return true;
  525. }
  526. // Share Functions
  527. function urlShortner($url) {
  528. global $bitlyUsername, $bitlyApi;
  529. if($bitlyUsername==""||$bitlyApi=="") return false;
  530. $url = urlencode($url);
  531. $bitlyAPI = "http://api.bit.ly/shorten?version=2.0.1&format=xml&longUrl=$url&login=$bitlyUsername&apiKey=$bitlyApi";
  532. $data = file_get_contents($bitlyAPI);
  533. $xml = simplexml_load_string($data);
  534. $shortlink = $xml->results->nodeKeyVal->shortUrl;
  535. return $shortlink;
  536. }
  537. function sharePhoto($photoID, $url) {
  538. $query = "SELECT * FROM photos WHERE id = '$photoID'";
  539. $result = mysql_query($query);
  540. $row = mysql_fetch_object($result);
  541. $thumb = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']."/../../".$row->thumbUrl;
  542. $title = $row->title;
  543. $description = $row->description;
  544. $shortlink = $row->shortlink;
  545. $twitterUrl = "https://twitter.com/share?url=".urlencode("$url");
  546. $facebookUrl = "http://www.facebook.com/sharer.php?u=".urlencode("$url")."&t=".urlencode($title);
  547. $tumblrUrl = "http://www.tumblr.com/share/link?url=".urlencode("$url")."&name=". urlencode($title)."&description=".urlencode($description);
  548. $pinterestUrl = "http://pinterest.com/pin/create/button/?url=".urlencode("$url")."&media=".urlencode($thumb);
  549. $mailUrl = "mailto:?subject=".rawurlencode($title)."&body=".rawurlencode("Hey guy! Check this out: $url");
  550. $share = array();
  551. $share['twitter'] = $twitterUrl;
  552. $share['facebook'] = $facebookUrl;
  553. $share['tumblr'] = $tumblrUrl;
  554. $share['pinterest'] = $pinterestUrl;
  555. $share['mail'] = $mailUrl;
  556. $share['shortlink'] = $shortlink;
  557. return $share;
  558. }
  559. function facebookHeader($photoID) {
  560. if(!is_numeric($photoID)) return false;
  561. dbConnect();
  562. $query = "SELECT * FROM photos WHERE id = '$photoID';";
  563. $result = mysql_query($query);
  564. $row = mysql_fetch_object($result);
  565. $parseUrl = parse_url("http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
  566. $thumb = $parseUrl['scheme']."://".$parseUrl['host'].$parseUrl['path']."/../".$row->thumbUrl;
  567. $return = '<meta name="title" content="'.$row->title.'" />';
  568. $return .= '<meta name="description" content="'.$row->description.' - via Lychee" />';
  569. $return .= '<link rel="image_src" type="image/jpeg" href="'. $thumb .'" />';
  570. return $return;
  571. }
  572. function isPhotoPublic($photoID) {
  573. $query = "SELECT * FROM photos WHERE id = '$photoID';";
  574. $result = mysql_query($query);
  575. $row = mysql_fetch_object($result);
  576. if($row->public == 1) return true;
  577. return false;
  578. }
  579. // Search Function
  580. function search($term) {
  581. $term = mysql_real_escape_string($term);
  582. $query = "SELECT * FROM photos WHERE title like '%$term%' OR description like '%$term%';";
  583. $result = mysql_query($query);
  584. while($row = mysql_fetch_array($result)) {
  585. $return['photos'][] = $row;
  586. }
  587. $query = "SELECT * FROM albums WHERE title like '%$term%';";
  588. $result = mysql_query($query);
  589. $i=0;
  590. while($row = mysql_fetch_array($result)) {
  591. $return['albums'][$i] = $row;
  592. $query = "SELECT thumbUrl FROM photos WHERE album = '".$row['id']."' ORDER BY id DESC LIMIT 0, 3;";
  593. $result2 = mysql_query($query);
  594. $k = 0;
  595. while($row2 = mysql_fetch_object($result2)){
  596. $return['albums'][$i]["thumb$k"] = $row2->thumbUrl;
  597. $k++;
  598. }
  599. }
  600. return $return;
  601. }
  602. ?>