misc.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. ###
  3. # @name Misc Module
  4. # @copyright 2015 by Tobias Reich
  5. ###
  6. if (!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
  7. function search($database, $settings, $term) {
  8. if (!isset($database, $settings, $term)) return false;
  9. $return['albums'] = '';
  10. # Initialize return var
  11. $return = array(
  12. 'photos' => null,
  13. 'albums' => null,
  14. 'hash' => ''
  15. );
  16. ###
  17. # Photos
  18. ###
  19. $query = Database::prepare($database, "SELECT id, title, tags, public, star, album, thumbUrl, takestamp, url FROM ? WHERE title LIKE '%?%' OR description LIKE '%?%' OR tags LIKE '%?%'", array(LYCHEE_TABLE_PHOTOS, $term, $term, $term));
  20. $result = $database->query($query);
  21. while($photo = $result->fetch_assoc()) {
  22. $photo = Photo::prepareData($photo);
  23. $return['photos'][$photo['id']] = $photo;
  24. }
  25. ###
  26. # Albums
  27. ###
  28. $query = Database::prepare($database, "SELECT id, title, public, sysstamp, password FROM ? WHERE title LIKE '%?%' OR description LIKE '%?%'", array(LYCHEE_TABLE_ALBUMS, $term, $term));
  29. $result = $database->query($query);
  30. while($album = $result->fetch_assoc()) {
  31. # Turn data from the database into a front-end friendly format
  32. $album = Album::prepareData($album);
  33. # Thumbs
  34. $query = Database::prepare($database, "SELECT thumbUrl FROM ? WHERE album = '?' " . $settings['sortingPhotos'] . " LIMIT 0, 3", array(LYCHEE_TABLE_PHOTOS, $album['id']));
  35. $thumbs = $database->query($query);
  36. # For each thumb
  37. $k = 0;
  38. while ($thumb = $thumbs->fetch_object()) {
  39. $album['thumbs'][$k] = LYCHEE_URL_UPLOADS_THUMB . $thumb->thumbUrl;
  40. $k++;
  41. }
  42. # Add to return
  43. $return['albums'][$album['id']] = $album;
  44. }
  45. # Hash
  46. $return['hash'] = md5(json_encode($return));
  47. return $return;
  48. }
  49. function getGraphHeader($database, $photoID) {
  50. if (!isset($database, $photoID)) return false;
  51. $photo = new Photo($database, null, null, $photoID);
  52. if ($photo->getPublic('')===false) return false;
  53. $query = Database::prepare($database, "SELECT title, description, url, medium FROM ? WHERE id = '?'", array(LYCHEE_TABLE_PHOTOS, $photoID));
  54. $result = $database->query($query);
  55. $row = $result->fetch_object();
  56. if (!$result||!$row) return false;
  57. if ($row->medium==='1') $dir = 'medium';
  58. else $dir = 'big';
  59. $parseUrl = parse_url('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
  60. $url = $parseUrl['scheme'] . '://' . $parseUrl['host'] . $parseUrl['path'] . '?' . $parseUrl['query'];
  61. $picture = $parseUrl['scheme'] . '://' . $parseUrl['host'] . $parseUrl['path'] . '/../uploads/' . $dir . '/' . $row->url;
  62. $url = htmlentities($url);
  63. $picture = htmlentities($picture);
  64. $row->title = htmlentities($row->title);
  65. $row->description = htmlentities($row->description);
  66. $return = '<!-- General Meta Data -->';
  67. $return .= '<meta name="title" content="' . $row->title . '">';
  68. $return .= '<meta name="description" content="' . $row->description . ' - via Lychee">';
  69. $return .= '<link rel="image_src" type="image/jpeg" href="' . $picture . '">';
  70. $return .= '<!-- Twitter Meta Data -->';
  71. $return .= '<meta name="twitter:card" content="photo">';
  72. $return .= '<meta name="twitter:title" content="' . $row->title . '">';
  73. $return .= '<meta name="twitter:image:src" content="' . $picture . '">';
  74. $return .= '<!-- Facebook Meta Data -->';
  75. $return .= '<meta property="og:title" content="' . $row->title . '">';
  76. $return .= '<meta property="og:description" content="' . $row->description . ' - via Lychee">';
  77. $return .= '<meta property="og:image" content="' . $picture . '">';
  78. $return .= '<meta property="og:url" content="' . $url . '">';
  79. return $return;
  80. }
  81. function getExtension($filename) {
  82. $extension = strpos($filename, '.') !== false
  83. ? strrchr($filename, '.')
  84. : '';
  85. return $extension;
  86. }
  87. function getHashedString($password) {
  88. # Inspired by http://alias.io/2010/01/store-passwords-safely-with-php-and-mysql/
  89. # A higher $cost is more secure but consumes more processing power
  90. $cost = 10;
  91. # Create a random salt
  92. if (extension_loaded('openssl')) {
  93. $salt = strtr(substr(base64_encode(openssl_random_pseudo_bytes(17)),0,22), '+', '.');
  94. } elseif (extension_loaded('mcrypt')) {
  95. $salt = strtr(substr(base64_encode(mcrypt_create_iv(17, MCRYPT_DEV_URANDOM)),0,22), '+', '.');
  96. } else {
  97. $salt = "";
  98. for ($i = 0; $i < 22; $i++) {
  99. $salt .= substr("./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", mt_rand(0, 63), 1);
  100. }
  101. }
  102. # Prefix information about the hash so PHP knows how to verify it later.
  103. # "$2a$" Means we're using the Blowfish algorithm. The following two digits are the cost parameter.
  104. $salt = sprintf("$2a$%02d$", $cost) . $salt;
  105. # Hash the password with the salt
  106. return crypt($password, $salt);
  107. }
  108. function hasPermissions($path) {
  109. // Check if the given path is readable and writable
  110. // Both functions are also verifying that the path exists
  111. if (is_readable($path)===true&&
  112. is_writeable($path)===true) return true;
  113. return false;
  114. }
  115. function fastimagecopyresampled(&$dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h, $quality = 4) {
  116. ###
  117. # Plug-and-Play fastimagecopyresampled function replaces much slower imagecopyresampled.
  118. # Just include this function and change all "imagecopyresampled" references to "fastimagecopyresampled".
  119. # Typically from 30 to 60 times faster when reducing high resolution images down to thumbnail size using the default quality setting.
  120. # Author: Tim Eckel - Date: 09/07/07 - Version: 1.1 - Project: FreeRingers.net - Freely distributable - These comments must remain.
  121. #
  122. # Optional "quality" parameter (defaults is 3). Fractional values are allowed, for example 1.5. Must be greater than zero.
  123. # Between 0 and 1 = Fast, but mosaic results, closer to 0 increases the mosaic effect.
  124. # 1 = Up to 350 times faster. Poor results, looks very similar to imagecopyresized.
  125. # 2 = Up to 95 times faster. Images appear a little sharp, some prefer this over a quality of 3.
  126. # 3 = Up to 60 times faster. Will give high quality smooth results very close to imagecopyresampled, just faster.
  127. # 4 = Up to 25 times faster. Almost identical to imagecopyresampled for most images.
  128. # 5 = No speedup. Just uses imagecopyresampled, no advantage over imagecopyresampled.
  129. ###
  130. if (empty($src_image) || empty($dst_image) || $quality <= 0) { return false; }
  131. if ($quality < 5 && (($dst_w * $quality) < $src_w || ($dst_h * $quality) < $src_h)) {
  132. $temp = imagecreatetruecolor($dst_w * $quality + 1, $dst_h * $quality + 1);
  133. imagecopyresized($temp, $src_image, 0, 0, $src_x, $src_y, $dst_w * $quality + 1, $dst_h * $quality + 1, $src_w, $src_h);
  134. imagecopyresampled($dst_image, $temp, $dst_x, $dst_y, 0, 0, $dst_w, $dst_h, $dst_w * $quality, $dst_h * $quality);
  135. imagedestroy($temp);
  136. } else imagecopyresampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
  137. return true;
  138. }
  139. ?>