KXStudio Website https://kx.studio/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

154 lines
7.1KB

  1. <?php
  2. class Photo extends Feathers implements Feather {
  3. public function __init() {
  4. $this->setField(array("attr" => "photo",
  5. "type" => "file",
  6. "label" => __("Photo", "photo"),
  7. "note" => "<small>(Max. file size: ".ini_get('upload_max_filesize').")</small>"));
  8. if (isset($_GET['action']) and $_GET['action'] == "bookmarklet")
  9. $this->setField(array("attr" => "from_url",
  10. "type" => "text",
  11. "label" => __("From URL?", "photo"),
  12. "optional" => true,
  13. "no_value" => true));
  14. $this->setField(array("attr" => "caption",
  15. "type" => "text_block",
  16. "label" => __("Caption", "photo"),
  17. "optional" => true,
  18. "preview" => true,
  19. "bookmarklet" => "page_link"));
  20. $this->setFilter("caption", array("markup_text", "markup_post_text"));
  21. $this->respondTo("delete_post", "delete_file");
  22. $this->respondTo("filter_post", "filter_post");
  23. $this->respondTo("post_options", "add_option");
  24. $this->respondTo("admin_write_post", "swfupload");
  25. $this->respondTo("admin_edit_post", "swfupload");
  26. if (isset($_GET['url']) and
  27. preg_match("/http:\/\/(www\.)?flickr\.com\/photos\/([^\/]+)\/([0-9]+)/", $_GET['url'])) {
  28. $this->bookmarkletSelected();
  29. $page = get_remote($_GET['url']);
  30. preg_match("/class=\"photoImgDiv\">\n<img src=\"([^\?\"]+)/", $page, $image);
  31. $this->setField(array("attr" => "from_url",
  32. "type" => "text",
  33. "label" => __("From URL?", "photo"),
  34. "optional" => true,
  35. "value" => $image[1]));
  36. }
  37. if (isset($_GET['url']) and preg_match("/\.(jpg|jpeg|png|gif|bmp)$/", $_GET['url'])) {
  38. $this->bookmarkletSelected();
  39. $this->setField(array("attr" => "from_url",
  40. "type" => "text",
  41. "label" => __("From URL?", "photo"),
  42. "optional" => true,
  43. "value" => $_GET['url']));
  44. }
  45. }
  46. public function swfupload($admin, $post = null) {
  47. if (isset($post) and $post->feather != "photo" or
  48. isset($_GET['feather']) and $_GET['feather'] != "photo")
  49. return;
  50. Trigger::current()->call("prepare_swfupload", "photo", "*.jpg;*.jpeg;*.png;*.gif;*.bmp");
  51. }
  52. public function submit() {
  53. if (!isset($_POST['filename'])) {
  54. if (isset($_FILES['photo']) and $_FILES['photo']['error'] == 0)
  55. $filename = upload($_FILES['photo'], array("jpg", "jpeg", "png", "gif", "bmp"));
  56. elseif (!empty($_POST['from_url']))
  57. $filename = upload_from_url($_POST['from_url'], array("jpg", "jpeg", "png", "gif", "bmp"));
  58. else
  59. error(__("Error"), __("Couldn't upload photo."));
  60. } else
  61. $filename = $_POST['filename'];
  62. return Post::add(array("filename" => $filename,
  63. "caption" => $_POST['caption']),
  64. $_POST['slug'],
  65. Post::check_url($_POST['slug']));
  66. }
  67. public function update($post) {
  68. if (!isset($_POST['filename']))
  69. if (isset($_FILES['photo']) and $_FILES['photo']['error'] == 0) {
  70. $this->delete_file($post);
  71. $filename = upload($_FILES['photo'], array("jpg", "jpeg", "png", "gif", "tiff", "bmp"));
  72. } elseif (!empty($_POST['from_url'])) {
  73. $this->delete_file($post);
  74. $filename = upload_from_url($_POST['from_url'], array("jpg", "jpeg", "png", "gif", "tiff", "bmp"));
  75. } else
  76. $filename = $post->filename;
  77. else {
  78. $this->delete_file($post);
  79. $filename = $_POST['filename'];
  80. }
  81. $post->update(array("filename" => $filename,
  82. "caption" => $_POST['caption']));
  83. }
  84. public function title($post) {
  85. return oneof($post->title_from_excerpt(), $post->filename);
  86. }
  87. public function excerpt($post) {
  88. return $post->caption;
  89. }
  90. public function feed_content($post) {
  91. return self::image_tag($post, 500, 500)."<br /><br />".$post->caption;
  92. }
  93. public function delete_file($post) {
  94. if ($post->feather != "photo") return;
  95. unlink(MAIN_DIR.Config::current()->uploads_path.$post->filename);
  96. }
  97. public function filter_post($post) {
  98. if ($post->feather != "photo") return;
  99. $post->image = $this->image_tag($post);
  100. }
  101. public function image_tag($post, $max_width = 500, $max_height = null, $more_args = "quality=100") {
  102. $filename = $post->filename;
  103. $config = Config::current();
  104. $alt = !empty($post->alt_text) ? fix($post->alt_text, true) : $filename ;
  105. return '<img src="'.$config->chyrp_url.'/includes/thumb.php?file=..'.$config->uploads_path.urlencode($filename).'&amp;max_width='.$max_width.'&amp;max_height='.$max_height.'&amp;'.$more_args.'" alt="'.$alt.'" />';
  106. }
  107. public function image_link($post, $max_width = 500, $max_height = null, $more_args="quality=100") {
  108. $source = !empty($post->source) ? $post->source : uploaded($post->filename) ;
  109. return '<a href="'.$source.'">'.$this->image_tag($post, $max_width, $max_height, $more_args).'</a>';
  110. }
  111. public function add_option($options, $post = null) {
  112. if (isset($post) and $post->feather != "photo") return;
  113. if (!isset($_GET['feather']) and Config::current()->enabled_feathers[0] != "photo" or
  114. isset($_GET['feather']) and $_GET['feather'] != "photo") return;
  115. $options[] = array("attr" => "option[alt_text]",
  116. "label" => __("Alt-Text", "photo"),
  117. "type" => "text",
  118. "value" => oneof(@$post->alt_text, ""));
  119. $options[] = array("attr" => "option[source]",
  120. "label" => __("Source", "photo"),
  121. "type" => "text",
  122. "value" => oneof(@$post->source, ""));
  123. $options[] = array("attr" => "from_url",
  124. "label" => __("From URL?", "photo"),
  125. "type" => "text");
  126. return $options;
  127. }
  128. }