misc.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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['sorting'] . " 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. $return = '<!-- General Meta Data -->';
  63. $return .= '<meta name="title" content="'.$row->title.'">';
  64. $return .= '<meta name="description" content="'.$row->description.' - via Lychee">';
  65. $return .= '<link rel="image_src" type="image/jpeg" href="'.$picture.'">';
  66. $return .= '<!-- Twitter Meta Data -->';
  67. $return .= '<meta name="twitter:card" content="photo">';
  68. $return .= '<meta name="twitter:title" content="'.$row->title.'">';
  69. $return .= '<meta name="twitter:image:src" content="'.$picture.'">';
  70. $return .= '<!-- Facebook Meta Data -->';
  71. $return .= '<meta property="og:title" content="'.$row->title.'">';
  72. $return .= '<meta property="og:image" content="'.$picture.'">';
  73. $return .= '<meta property="og:description" content="'.$row->description.' - via Lychee">';
  74. $return .= '<meta property="og:url" content="'.$url.'">';
  75. return $return;
  76. }
  77. function getExtension($filename) {
  78. $extension = strpos($filename, '.') !== false
  79. ? strrchr($filename, '.')
  80. : '';
  81. return $extension;
  82. }
  83. function getHashedString($password) {
  84. # Inspired by http://alias.io/2010/01/store-passwords-safely-with-php-and-mysql/
  85. # A higher $cost is more secure but consumes more processing power
  86. $cost = 10;
  87. # Create a random salt
  88. if (extension_loaded('openssl')) {
  89. $salt = strtr(substr(base64_encode(openssl_random_pseudo_bytes(17)),0,22), '+', '.');
  90. } elseif (extension_loaded('mcrypt')) {
  91. $salt = strtr(substr(base64_encode(mcrypt_create_iv(17, MCRYPT_DEV_URANDOM)),0,22), '+', '.');
  92. } else {
  93. $salt = "";
  94. for ($i = 0; $i < 22; $i++) {
  95. $salt .= substr("./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", mt_rand(0, 63), 1);
  96. }
  97. }
  98. # Prefix information about the hash so PHP knows how to verify it later.
  99. # "$2a$" Means we're using the Blowfish algorithm. The following two digits are the cost parameter.
  100. $salt = sprintf("$2a$%02d$", $cost) . $salt;
  101. # Hash the password with the salt
  102. return crypt($password, $salt);
  103. }
  104. function hasPermissions($path) {
  105. // Check if the given path is readable and writable
  106. // Both functions are also verifying that the path exists
  107. if (is_readable($path)===true&&
  108. is_writeable($path)===true) return true;
  109. return false;
  110. }
  111. function fastimagecopyresampled(&$dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h, $quality = 4) {
  112. ###
  113. # Plug-and-Play fastimagecopyresampled function replaces much slower imagecopyresampled.
  114. # Just include this function and change all "imagecopyresampled" references to "fastimagecopyresampled".
  115. # Typically from 30 to 60 times faster when reducing high resolution images down to thumbnail size using the default quality setting.
  116. # Author: Tim Eckel - Date: 09/07/07 - Version: 1.1 - Project: FreeRingers.net - Freely distributable - These comments must remain.
  117. #
  118. # Optional "quality" parameter (defaults is 3). Fractional values are allowed, for example 1.5. Must be greater than zero.
  119. # Between 0 and 1 = Fast, but mosaic results, closer to 0 increases the mosaic effect.
  120. # 1 = Up to 350 times faster. Poor results, looks very similar to imagecopyresized.
  121. # 2 = Up to 95 times faster. Images appear a little sharp, some prefer this over a quality of 3.
  122. # 3 = Up to 60 times faster. Will give high quality smooth results very close to imagecopyresampled, just faster.
  123. # 4 = Up to 25 times faster. Almost identical to imagecopyresampled for most images.
  124. # 5 = No speedup. Just uses imagecopyresampled, no advantage over imagecopyresampled.
  125. ###
  126. if (empty($src_image) || empty($dst_image) || $quality <= 0) { return false; }
  127. if ($quality < 5 && (($dst_w * $quality) < $src_w || ($dst_h * $quality) < $src_h)) {
  128. $temp = imagecreatetruecolor($dst_w * $quality + 1, $dst_h * $quality + 1);
  129. imagecopyresized($temp, $src_image, 0, 0, $src_x, $src_y, $dst_w * $quality + 1, $dst_h * $quality + 1, $src_w, $src_h);
  130. imagecopyresampled($dst_image, $temp, $dst_x, $dst_y, 0, 0, $dst_w, $dst_h, $dst_w * $quality, $dst_h * $quality);
  131. imagedestroy($temp);
  132. } else imagecopyresampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
  133. return true;
  134. }
  135. ?>