api.gallery.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. <?php
  2. ////////////////////////////////////////////////////////////////////////////////
  3. // Copyright (C) ReloadCMS Development Team //
  4. // http://reloadcms.sf.net //
  5. // //
  6. // This program is distributed in the hope that it will be useful, //
  7. // but WITHOUT ANY WARRANTY, without even the implied warranty of //
  8. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. //
  9. // //
  10. // This product released under GNU General Public License v2 //
  11. ////////////////////////////////////////////////////////////////////////////////
  12. $gd_formats = '';
  13. if(function_exists('imagegif')) $gd_formats .= 'gif ';
  14. if(function_exists('imagejpeg')) $gd_formats .= 'jpg jpeg jpe ';
  15. if(function_exists('imagepng')) $gd_formats .= 'png ';
  16. define('GD_SUPPORTED_FORMATS', $gd_formats);
  17. define('GD_SUPPORTED_FORMATS_PREG', '#.*\.(' . implode('|', explode(' ', $gd_formats)) . ')$#i');
  18. define('GALLERY_UPLOAD_DIR', GALLERY_PATH . 'new/');
  19. define('GALLERY_INDEXES_DIR', GALLERY_PATH . 'indexes/');
  20. define('GALLERY_IMAGES_DIR', GALLERY_PATH . 'images/');
  21. define('GALLERY_THUMBS_DIR', GALLERY_PATH . 'thumbnails/');
  22. define('GALLERY_COMMENTS_DIR', GALLERY_PATH . 'comments/');
  23. class gallery{
  24. var $img_preg = '/.*\.(jpg|jpe|jpeg|gif|png|bmp)$/i';
  25. var $gd_preg = GD_SUPPORTED_FORMATS_PREG;
  26. var $path = GALLERY_PATH;
  27. var $indexes = array();
  28. public function __construct(){
  29. // Load Index files
  30. $this->loadIndexFiles();
  31. }
  32. function rebuildIndex(){
  33. @set_time_limit(0);
  34. $this->scanForRemovedImages();
  35. $this->cleanUpDirectories();
  36. $this->scanForNewImages();
  37. $this->cleanUpIndexes();
  38. $this->saveIndexFiles();
  39. }
  40. function scanForRemovedImages(){
  41. foreach ($this->indexes['filename'] as $filename){
  42. if(!is_file(GALLERY_IMAGES_DIR . $filename)){
  43. $this->removeImage($filename);
  44. }
  45. }
  46. }
  47. function cleanUpDirectories(){
  48. $images = rcms_scandir(GALLERY_IMAGES_DIR);
  49. foreach ($images as $image){
  50. if(!in_array($image, $this->indexes['filename'])){
  51. rcms_delete_files(GALLERY_IMAGES_DIR . $image);
  52. }
  53. }
  54. $images = rcms_scandir(GALLERY_COMMENTS_DIR);
  55. foreach ($images as $image){
  56. if(!in_array(substr($image, 0, -4), $this->indexes['filename'])){
  57. rcms_delete_files(GALLERY_COMMENTS_DIR . $image);
  58. }
  59. }
  60. $images = rcms_scandir(GALLERY_THUMBS_DIR);
  61. foreach ($images as $image){
  62. if(!in_array(substr($image, 0, -4), $this->indexes['filename'])){
  63. rcms_delete_files(GALLERY_THUMBS_DIR . $image);
  64. }
  65. }
  66. }
  67. function scanForNewImages(){
  68. $return = array();
  69. $new_images = $this->getImages(GALLERY_UPLOAD_DIR);
  70. foreach ($new_images as $image){
  71. $image_newname = $image;
  72. $temp_i = 0;
  73. $ext = array_reverse(explode('.', $image));
  74. $ext = $ext[0];
  75. while(in_array($image_newname, $this->indexes['filename']) || is_file(GALLERY_IMAGES_DIR . $image_newname)){
  76. $temp_i++;
  77. $image_newname = substr($image, 0, -(strlen($ext))-1) . '_' . $temp_i . '.' . $ext;
  78. }
  79. if(substr($ext, 0, 2) == 'jp') $type = 'jpeg'; else $type = $ext;
  80. list($width, $height, $x, $x) = getimagesize(GALLERY_UPLOAD_DIR . $image);
  81. $size = $width . 'x' . $height;
  82. rcms_rename_file(GALLERY_UPLOAD_DIR . $image, GALLERY_IMAGES_DIR . $image_newname);
  83. $this->registerInIndex($image_newname, $image_newname, $size, $type);
  84. $return[$image] = $image_newname;
  85. }
  86. return $return;
  87. }
  88. function cleanUpIndexes(){
  89. foreach ($this->indexes['type'] as $type => $images){
  90. if(empty($images)){
  91. rcms_remove_index($type, $this->indexes['type'][$type], true);
  92. }
  93. }
  94. foreach ($this->indexes['size'] as $size => $images){
  95. if(empty($images)){
  96. rcms_remove_index($size, $this->indexes['size'][$size], true);
  97. }
  98. }
  99. foreach ($this->indexes['keywords'] as $keyword => $images){
  100. if(empty($images)){
  101. rcms_remove_index($size, $this->indexes['keywords'][$keyword], true);
  102. }
  103. }
  104. }
  105. function loadIndexFiles(){
  106. if(!is_readable(GALLERY_INDEXES_DIR . 'main.dat') || !($this->indexes['main'] = @unserialize(file_get_contents(GALLERY_INDEXES_DIR . 'main.dat')))){
  107. $this->indexes['main'] = array();
  108. $this->indexes['filename'] = array();
  109. $this->indexes['title'] = array();
  110. $this->indexes['size'] = array();
  111. $this->indexes['type'] = array();
  112. $this->indexes['keywords'] = array();
  113. return true;
  114. }
  115. $this->indexes['filename'] = @unserialize(@file_get_contents(GALLERY_INDEXES_DIR . 'filename.dat'));
  116. $this->indexes['title'] = @unserialize(@file_get_contents(GALLERY_INDEXES_DIR . 'title.dat'));
  117. $this->indexes['size'] = @unserialize(@file_get_contents(GALLERY_INDEXES_DIR . 'size.dat'));
  118. $this->indexes['type'] = @unserialize(@file_get_contents(GALLERY_INDEXES_DIR . 'type.dat'));
  119. $this->indexes['keywords'] = @unserialize(@file_get_contents(GALLERY_INDEXES_DIR . 'keywords.dat'));
  120. return true;
  121. }
  122. function registerInIndex($filename, $title, $size, $type){
  123. $this->indexes['main'][$filename] = array('title' => $title, 'size' => $size, 'type' => $type);
  124. $this->indexes['filename'][] = $filename;
  125. $this->indexes['title'][$filename] = $title;
  126. $this->indexes['size'][$size][] = $filename;
  127. $this->indexes['type'][strtolower($type)][] = $filename;
  128. }
  129. function unregisterInIndex($filename){
  130. if(empty($this->indexes['main'][$filename])) return false;
  131. $k_f = array_search($filename, $this->indexes['filename']);
  132. $size = $this->indexes['main'][$filename]['size'];
  133. $type = strtolower($this->indexes['main'][$filename]['type']);
  134. $k_s = @array_search($filename, $this->indexes['size'][$size]);
  135. $k_t = @array_search($filename, $this->indexes['type'][$type]);
  136. $this->indexes['filename'][$k_f] = '';
  137. $this->indexes['size'][$size][$k_s] = '';
  138. $this->indexes['type'][$type][$k_t] = '';
  139. $this->unsetKeywords($filename);
  140. rcms_remove_index($k_f, $this->indexes['filename'], true);
  141. rcms_remove_index($filename, $this->indexes['title'], true);
  142. rcms_remove_index($k_s, $this->indexes['size'][$size], true);
  143. rcms_remove_index($k_t, $this->indexes['type'][$type], true);
  144. $this->indexes['main'][$filename] = array();
  145. rcms_remove_index($filename, $this->indexes['main'], true);
  146. return true;
  147. }
  148. function setKeywords($filename, $keywords){
  149. if(empty($this->indexes['main'][$filename]) || !is_file(GALLERY_IMAGES_DIR . $filename)) return false;
  150. $this->indexes['main'][$filename]['keywords'] = $keywords;
  151. if(!empty($keywords)){
  152. $keywords_array = explode(';', $keywords);
  153. foreach ($keywords_array as $keyword){
  154. $keyword = trim($keyword);
  155. $this->indexes['keywords'][$keyword][] = $filename;
  156. }
  157. }
  158. return true;
  159. }
  160. function unsetKeywords($filename){
  161. if(empty($this->indexes['main'][$filename]) || !is_file(GALLERY_IMAGES_DIR . $filename)) return false;
  162. $keywords = explode(';', @$this->indexes['main'][$filename]['keywords']);
  163. if(!empty($keywords)){
  164. foreach ($keywords as $keyword){
  165. $keyword = trim($keyword);
  166. $k = @array_search($filename, $this->indexes['keywords'][$keyword]);
  167. @rcms_remove_index($k, $this->indexes['keywords'][$keyword], true);
  168. }
  169. }
  170. return true;
  171. }
  172. function changeKeywords($filename, $keywords){
  173. if(empty($this->indexes['main'][$filename]) || !is_file(GALLERY_IMAGES_DIR . $filename)) return false;
  174. if(!empty($this->indexes['main'][$filename]['keywords'])){
  175. $this->unsetKeywords($filename);
  176. }
  177. $this->setKeywords($filename, $keywords);
  178. return true;
  179. }
  180. function removeImage($filename){
  181. $this->unregisterInIndex($filename);
  182. if(is_file(GALLERY_IMAGES_DIR . $filename)) rcms_delete_files(GALLERY_IMAGES_DIR . $filename);
  183. if(is_file(GALLERY_COMMENTS_DIR . $filename . '.dat')) rcms_delete_files(GALLERY_COMMENTS_DIR . $filename . '.dat');
  184. if(is_file(GALLERY_THUMBS_DIR . $filename . '.jpg')) rcms_delete_files(GALLERY_THUMBS_DIR . $filename . '.jpg');
  185. return true;
  186. }
  187. function saveIndexFiles(){
  188. file_write_contents(GALLERY_INDEXES_DIR . 'main.dat', serialize($this->indexes['main']));
  189. file_write_contents(GALLERY_INDEXES_DIR . 'filename.dat', serialize($this->indexes['filename']));
  190. file_write_contents(GALLERY_INDEXES_DIR . 'title.dat', serialize($this->indexes['title']));
  191. file_write_contents(GALLERY_INDEXES_DIR . 'size.dat', serialize($this->indexes['size']));
  192. file_write_contents(GALLERY_INDEXES_DIR . 'type.dat', serialize($this->indexes['type']));
  193. file_write_contents(GALLERY_INDEXES_DIR . 'keywords.dat', serialize($this->indexes['keywords']));
  194. return true;
  195. }
  196. function getImages($directory){
  197. $directory = rcms_scandir($directory);
  198. $images = array();
  199. foreach ($directory as $file){
  200. if (preg_match($this->img_preg, $file)) {
  201. $images[] = $file;
  202. }
  203. }
  204. return $images;
  205. }
  206. function getFullImagesList(){
  207. $temp = $this->indexes['filename'];
  208. $this->indexes['filename'] = array();
  209. foreach ($temp as $key => $data){
  210. if(!empty($data)) $this->indexes['filename'][$key] = $data;
  211. }
  212. $this->saveIndexFiles();
  213. natsort($this->indexes['filename']);
  214. return $this->indexes['filename'];
  215. }
  216. function getAvaiableValues($field){
  217. if(empty($this->indexes[$field])) return false;
  218. $result = array();
  219. foreach (array_keys($this->indexes[$field]) as $key){
  220. if(!empty($this->indexes[$field][$key])){
  221. $result[] = $key;
  222. }
  223. }
  224. natsort($result);
  225. return $result;
  226. }
  227. function getLimitedImagesList($field, $value){
  228. if(empty($this->indexes[$field][$value])) return false;
  229. $result = array();
  230. foreach ($this->indexes[$field][$value] as $image){
  231. if(in_array($image, $this->indexes['filename'])) $result[] = $image;
  232. }
  233. natsort($result);
  234. return $result;
  235. }
  236. function getImage($filename){
  237. if(empty($this->indexes['main'][$filename]) || !is_file(GALLERY_IMAGES_DIR . $filename)) return false;
  238. return '<div style="overflow: hidden; width: 100%;"><a href="' . GALLERY_IMAGES_DIR . $filename . '" target="_blank"><img src="' . GALLERY_IMAGES_DIR . $filename . '" alt="' . $this->indexes['main'][$filename]['title'] . '" style="max-width: 95%;" /></a></div>';
  239. }
  240. function getData($filename){
  241. if(empty($this->indexes['main'][$filename]) || !is_file(GALLERY_IMAGES_DIR . $filename)) return false;
  242. return $this->indexes['main'][$filename];
  243. }
  244. function getComments($filename){
  245. if(empty($this->indexes['main'][$filename]) || !is_file(GALLERY_IMAGES_DIR . $filename)) return false;
  246. if(true){
  247. $comments = guestbook_get_last_msgs(
  248. null,
  249. true,
  250. false,
  251. GALLERY_COMMENTS_DIR . $filename . '.dat'
  252. );
  253. foreach ($comments as $mid => $message) {
  254. $comments[$mid]['id'] = $mid;
  255. }
  256. return $comments;
  257. }
  258. return false;
  259. }
  260. function countComments($filename){
  261. if(empty($this->indexes['main'][$filename]) || !is_file(GALLERY_IMAGES_DIR . $filename)) return false;
  262. if(true){
  263. return count(guestbook_get_last_msgs(null, false, false, GALLERY_COMMENTS_DIR . $filename . '.dat'));
  264. }
  265. return false;
  266. }
  267. function postComment($filename, $text){
  268. global $system;
  269. if(empty($this->indexes['main'][$filename]) || !is_file(GALLERY_IMAGES_DIR . $filename)) return false;
  270. if(!empty($text)){
  271. return guestbook_post_msg($system->user['username'],
  272. $system->user['nickname'],
  273. $text,
  274. GALLERY_COMMENTS_DIR . $filename . '.dat'
  275. );
  276. }
  277. }
  278. function removeComment($filename, $cid){
  279. if(empty($this->indexes['main'][$filename]) || !is_file(GALLERY_IMAGES_DIR . $filename)) return false;
  280. return guestbook_post_remove($cid, GALLERY_COMMENTS_DIR . $filename . '.dat');
  281. }
  282. function getThumbnail($filename, $mw = 150, $mh = 150){
  283. if(empty($this->indexes['main'][$filename]) || !is_file(GALLERY_IMAGES_DIR . $filename)) return false;
  284. if($return = $this->generateThumbnail($filename, $mw, $mh)){
  285. return $return;
  286. } else {
  287. $path = GALLERY_IMAGES_DIR . $filename;
  288. $stat = getimagesize($path);
  289. $iw = $stat[0];
  290. $ih = $stat[1];
  291. if(($iw > $mh) || ($iw < $mw)) {
  292. $sizefactor = (($ih > $iw) ? ($mh / $ih) : ($mw / $iw));
  293. } else {
  294. $sizefactor =1;
  295. }
  296. $nw = (int) ($iw * $sizefactor);
  297. $nh = (int) ($ih * $sizefactor);
  298. unset($sizefactor);
  299. return '<img src="' . $path . '" width="' . $nw . '" height="' . $nh . '" border="0" alt="" />';
  300. }
  301. }
  302. function generateThumbnail($filename, $mw, $mh){
  303. $path = GALLERY_IMAGES_DIR . $filename;
  304. if(empty($this->indexes['main'][$filename]) || !is_file(GALLERY_IMAGES_DIR . $filename) || !preg_match($this->gd_preg, $filename)) return false;
  305. if(!is_file(GALLERY_THUMBS_DIR . $filename . '.jpg')){
  306. if(substr(strtolower($filename), -4) == '.jpg') {
  307. $img = imagecreatefromjpeg($path);
  308. } elseif(substr(strtolower($filename), -4) == '.gif') {
  309. $img = imagecreatefromgif($path);
  310. } elseif(substr(strtolower($filename), -4) == '.png') {
  311. $img = imagecreatefrompng($path);
  312. } else return false;
  313. if(!empty($img)){
  314. $stat = getimagesize($path);
  315. $iw = $stat[0];
  316. $ih = $stat[1];
  317. if(($iw > $mh) || ($iw < $mw)) {
  318. $sizefactor = (($ih > $iw) ? ($mh / $ih) : ($mw / $iw));
  319. } else {
  320. $sizefactor =1;
  321. }
  322. $nw = (int) ($iw * $sizefactor);
  323. $nh = (int) ($ih * $sizefactor);
  324. unset($sizefactor);
  325. $thumb = imagecreatetruecolor($nw, $nh);
  326. imagecopyresampled($thumb, $img, 0, 0, 0, 0, $nw, $nh, $iw, $ih);
  327. imagejpeg($thumb, GALLERY_THUMBS_DIR . $filename . '.jpg');
  328. imagedestroy($thumb);
  329. imagedestroy($img);
  330. }
  331. }
  332. return '<img src="' . GALLERY_THUMBS_DIR . $filename . '.jpg" alt="' . $filename . '" />';
  333. }
  334. function setDataValue($filename, $dataname, $value){
  335. if(empty($this->indexes['main'][$filename]) || !is_file(GALLERY_IMAGES_DIR . $filename)) return false;
  336. $this->indexes['main'][$filename][$dataname] = $value;
  337. $this->indexes[$dataname][$filename] = $value;
  338. return true;
  339. }
  340. }
  341. ?>