misc.php 6.1 KB

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