GNUsocialPhotosPlugin.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * GNU Social
  4. * Copyright (C) 2010, Free Software Foundation, Inc.
  5. *
  6. * PHP version 5
  7. *
  8. * LICENCE:
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category Widget
  23. * @package GNU Social
  24. * @author Ian Denhardt <ian@zenhack.net>
  25. * @author Max Shinn <trombonechamp@gmail.com>
  26. * @copyright 2010 Free Software Foundation, Inc.
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  28. */
  29. /* Photo sharing plugin */
  30. if (!defined('STATUSNET')) {
  31. exit(1);
  32. }
  33. include_once $dir . '/lib/photolib.php';
  34. class GNUsocialPhotosPlugin extends Plugin
  35. {
  36. function onCheckSchema()
  37. {
  38. $schema = Schema::get();
  39. $schema->ensureTable('GNUsocialPhoto', GNUsocialPhoto::schemaDef());
  40. $schema->ensureTable('GNUsocialPhotoAlbum', GNUsocialPhotoAlbum::schemaDef());
  41. }
  42. function onRouterInitialized($m)
  43. {
  44. $m->connect(':nickname/photos', array('action' => 'photos'));
  45. $m->connect(':nickname/photos/:albumid', array('action' => 'photos'));
  46. $m->connect('main/uploadphoto', array('action' => 'photoupload'));
  47. $m->connect('photo/:photoid', array('action' => 'photo'));
  48. $m->connect('editphoto/:photoid', array('action' => 'editphoto'));
  49. return true;
  50. }
  51. function onStartNoticeDistribute($notice)
  52. {
  53. common_log(LOG_INFO, "event: StartNoticeDistribute");
  54. if (GNUsocialPhotoTemp::$tmp) {
  55. GNUsocialPhotoTemp::$tmp->notice_id = $notice->id;
  56. $photo_id = GNUsocialPhotoTemp::$tmp->insert();
  57. if (!$photo_id) {
  58. common_log_db_error($photo, 'INSERT', __FILE__);
  59. throw new ServerException(_m('Problem saving photo.'));
  60. }
  61. }
  62. return true;
  63. }
  64. function onEndNoticeAsActivity(Notice $stored, Activity $act, Profile $scoped=null)
  65. {
  66. common_log(LOG_INFO, 'photo plugin: EndNoticeAsActivity');
  67. $photo = GNUsocialPhoto::getKV('notice_id', $stored->id);
  68. if(!$photo) {
  69. common_log(LOG_INFO, 'not a photo.');
  70. return true;
  71. }
  72. $act->objects[0]->type = ActivityObject::PHOTO;
  73. $act->objects[0]->thumbnail = $photo->thumb_uri;
  74. $act->objects[0]->largerImage = $photo->uri;
  75. return false;
  76. }
  77. function onStartHandleFeedEntry($activity)
  78. {
  79. common_log(LOG_INFO, 'photo plugin: onEndAtomPubNewActivity');
  80. $oprofile = Ostatus_profile::ensureActorProfile($activity);
  81. foreach ($activity->objects as $object) {
  82. if($object->type == ActivityObject::PHOTO) {
  83. $uri = $object->largerImage;
  84. $thumb_uri = $object->thumbnail;
  85. $profile_id = $oprofile->profile_id;
  86. $source = 'unknown'; // TODO: put something better here.
  87. common_log(LOG_INFO, 'uri : ' . $uri);
  88. common_log(LOG_INFO, 'thumb_uri : ' . $thumb_uri);
  89. // It's possible this is validated elsewhere, but I'm not sure and
  90. // would rather be safe.
  91. $uri = filter_var($uri, FILTER_SANITIZE_URL);
  92. $thumb_uri = filter_var($thumb_uri, FILTER_SANITIZE_URL);
  93. $uri = filter_var($uri, FILTER_VALIDATE_URL);
  94. $thumb_uri = filter_var($thumb_uri, FILTER_VALIDATE_URL);
  95. if(empty($thumb_uri)) {
  96. // We need a thumbnail, so if we aren't given one, use the actual picture for now.
  97. $thumb_uri = $uri;
  98. }
  99. if (!empty($uri) && !empty($thumb_uri)) {
  100. GNUsocialPhoto::saveNew($profile_id, $thumb_uri, $uri, $source, false);
  101. } else {
  102. common_log(LOG_INFO, 'bad URI for photo');
  103. }
  104. return false;
  105. }
  106. }
  107. return true;
  108. }
  109. function onStartShowNoticeItem($action)
  110. {
  111. $photo = GNUsocialPhoto::getKV('notice_id', $action->notice->id);
  112. if($photo) {
  113. $action->out->elementStart('div', 'entry-title');
  114. $action->showAuthor();
  115. $action->out->elementStart('a', array('href' => $photo->getPageLink()));
  116. $action->out->element('img', array('src' => $photo->thumb_uri,
  117. 'width' => 256, 'height' => 192));
  118. $action->out->elementEnd('a');
  119. $action->out->elementEnd('div');
  120. $action->showNoticeInfo();
  121. $action->showNoticeOptions();
  122. return false;
  123. }
  124. return true;
  125. }
  126. /* function onEndShowNoticeFormData($action)
  127. {
  128. $link = "/main/uploadphoto";
  129. $action->out->element('label', array('for' => 'photofile'),_('Attach'));
  130. $action->out->element('input', array('id' => 'photofile',
  131. 'type' => 'file',
  132. 'name' => 'photofile',
  133. 'title' => _('Upload a photo')));
  134. }
  135. */
  136. function onEndPersonalGroupNav(Menu $nav, Profile $target, Profile $scoped=null)
  137. {
  138. $nav->out->menuItem(common_local_url('photos',
  139. array('nickname' => $nav->action->trimmed('nickname'))), _('Photos'),
  140. _('Photo gallery'), $nav->action->trimmed('action') == 'photos', 'nav_photos');
  141. }
  142. function onEndShowStyles($action)
  143. {
  144. $action->cssLink('/plugins/GNUsocialPhotos/res/style.css');
  145. }
  146. function onEndShowScripts($action)
  147. {
  148. $action->script('plugins/GNUsocialPhotos/res/gnusocialphotos.js');
  149. }
  150. }