misc.php 6.5 KB

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