4 * Copyright (C) 2010, Free Software Foundation, Inc.
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.
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.
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/>.
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
30 /* Photo sharing plugin */
32 if (!defined('STATUSNET')) {
36 class GNUsocialPhotosPlugin extends Plugin
39 function onAutoload($cls)
41 $dir = dirname(__FILE__);
43 include_once $dir . '/lib/tempphoto.php';
44 include_once $dir . '/lib/photonav.php';
48 include_once $dir . '/lib/photolib.php';
49 include_once $dir . '/actions/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
51 case 'PhotouploadAction':
52 include_once $dir . '/lib/photolib.php';
53 include_once $dir . '/actions/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
56 include_once $dir . '/lib/photolib.php';
57 include_once $dir . '/actions/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
62 include_once $dir . '/classes/gnusocialphoto.php';
63 include_once $dir . '/classes/gnusocialphotoalbum.php';
67 function onCheckSchema()
69 $schema = Schema::get();
70 $schema->ensureTable('GNUsocialPhoto',
71 array(new ColumnDef('notice_id', 'int(11)', null, false, 'PRI'),
72 new ColumnDef('album_id', 'int(11)', null, false),
73 new ColumnDef('uri', 'varchar(512)', null, false),
74 new ColumnDef('thumb_uri', 'varchar(512)', null, false),
75 new ColumnDef('title', 'varchar(512)', null, false),
76 new ColumnDef('photo_description', 'text', null, false)));
77 $schema->ensureTable('GNUsocialPhotoAlbum',
78 array(new ColumnDef('album_id', 'int(11)', null, false, 'PRI', null, null, true),
79 new ColumnDef('profile_id', 'int(11)', null, false),
80 new ColumnDef('album_name', 'varchar(256)', null, false),
81 new ColumnDef('album_description', 'text', null, false)));
85 function onRouterInitialized($m)
87 $m->connect(':nickname/photos', array('action' => 'photos'));
88 $m->connect('main/uploadphoto', array('action' => 'photoupload'));
89 $m->connect('photo/:photoid', array('action' => 'photo'));
93 function onStartNoticeDistribute($notice)
95 common_log(LOG_INFO, "event: StartNoticeDistribute");
96 if (GNUsocialPhotoTemp::$tmp) {
97 GNUsocialPhotoTemp::$tmp->notice_id = $notice->id;
98 $photo_id = GNUsocialPhotoTemp::$tmp->insert();
100 common_log_db_error($photo, 'INSERT', __FILE__);
101 throw new ServerException(_m('Problem saving photo.'));
107 function onEndNoticeAsActivity($notice, &$activity)
109 common_log(LOG_INFO, 'photo plugin: EndNoticeAsActivity');
110 $photo = GNUsocialPhoto::staticGet('notice_id', $notice->id);
112 common_log(LOG_INFO, 'not a photo.');
116 $activity->objects[0]->type = ActivityObject::PHOTO;
117 $activity->objects[0]->thumbnail = $photo->thumb_uri;
118 $activity->objects[0]->largerImage = $photo->uri;
123 function onStartHandleFeedEntry($activity)
125 common_log(LOG_INFO, 'photo plugin: onEndAtomPubNewActivity');
126 $oprofile = Ostatus_profile::ensureActorProfile($activity);
127 foreach ($activity->objects as $object) {
128 if($object->type == ActivityObject::PHOTO) {
129 $uri = $object->largerImage;
130 $thumb_uri = $object->thumbnail;
131 $profile_id = $oprofile->profile_id;
132 $source = 'unknown'; // TODO: put something better here.
134 common_log(LOG_INFO, 'uri : ' . $uri);
135 common_log(LOG_INFO, 'thumb_uri : ' . $thumb_uri);
137 // It's possible this is validated elsewhere, but I'm not sure and
138 // would rather be safe.
139 $uri = filter_var($uri, FILTER_SANITIZE_URL);
140 $thumb_uri = filter_var($thumb_uri, FILTER_SANITIZE_URL);
141 $uri = filter_var($uri, FILTER_VALIDATE_URL);
142 $thumb_uri = filter_var($thumb_uri, FILTER_VALIDATE_URL);
144 if(empty($thumb_uri)) {
145 // We need a thumbnail, so if we aren't given one, use the actual picture for now.
149 if (!empty($uri) && !empty($thumb_uri)) {
150 GNUsocialPhoto::saveNew($profile_id, $thumb_uri, $uri, $source, false);
152 common_log(LOG_INFO, 'bad URI for photo');
160 function onStartShowNoticeItem($action)
162 $photo = GNUsocialPhoto::staticGet('notice_id', $action->notice->id);
164 $action->out->elementStart('div', 'entry-title');
165 $action->showAuthor();
166 $action->out->elementStart('a', array('href' => $photo->getPageLink()));
167 $action->out->element('img', array('src' => $photo->thumb_uri,
168 'width' => 256, 'height' => 192));
169 $action->out->elementEnd('a');
170 $action->out->elementEnd('div');
171 $action->showNoticeInfo();
172 $action->showNoticeOptions();
178 /* function onEndShowNoticeFormData($action)
180 $link = "/main/uploadphoto";
181 $action->out->element('label', array('for' => 'photofile'),_('Attach'));
182 $action->out->element('input', array('id' => 'photofile',
184 'name' => 'photofile',
185 'title' => _('Upload a photo')));
188 function onEndPersonalGroupNav($nav)
191 $nav->out->menuItem(common_local_url('photos',
192 array('nickname' => $nav->action->trimmed('nickname'))), _('Photos'),
193 _('Photo gallery'), $nav->action->trimmed('action') == 'photos', 'nav_photos');