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 * @copyright 2010 Free Software Foundation, Inc.
26 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
29 /* Photo sharing plugin */
31 if (!defined('STATUSNET')) {
35 class GNUsocialPhotosPlugin extends Plugin
38 function onAutoload($cls)
40 $dir = dirname(__FILE__);
42 include_once $dir . '/lib/tempphoto.php';
43 include_once $dir . '/lib/photonav.php';
47 include_once $dir . '/lib/photolib.php';
48 include_once $dir . '/actions/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
50 case 'PhotouploadAction':
51 include_once $dir . '/lib/photolib.php';
52 include_once $dir . '/actions/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
55 include_once $dir . '/lib/photolib.php';
56 include_once $dir . '/actions/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
61 include_once $dir . '/classes/gnusocialphoto.php';
62 include_once $dir . '/classes/gnusocialphotoalbum.php';
66 function onCheckSchema()
68 $schema = Schema::get();
69 $schema->ensureTable('GNUsocialPhoto',
70 array(new ColumnDef('notice_id', 'int(11)', null, false, 'PRI'),
71 new ColumnDef('album_id', 'int(11)', null, false),
72 new ColumnDef('uri', 'varchar(512)', null, false),
73 new ColumnDef('thumb_uri', 'varchar(512)', null, false),
74 new ColumnDef('title', 'varchar(512)', null, false),
75 new ColumnDef('photo_description', 'text', null, false)));
76 $schema->ensureTable('GNUsocialPhotoAlbum',
77 array(new ColumnDef('album_id', 'int(11)', null, false, 'PRI', null, null, true),
78 new ColumnDef('profile_id', 'int(11)', null, false),
79 new ColumnDef('album_name', 'varchar(256)', null, false),
80 new ColumnDef('album_description', 'text', null, false)));
84 function onRouterInitialized($m)
86 $m->connect(':nickname/photos', array('action' => 'photos'));
87 $m->connect('main/uploadphoto', array('action' => 'photoupload'));
88 $m->connect(':nickname/photo/:photoid', array('action' => 'photo'));
92 function onStartNoticeDistribute($notice)
94 common_log(LOG_INFO, "event: StartNoticeDistribute");
95 if (GNUsocialPhotoTemp::$tmp) {
96 GNUsocialPhotoTemp::$tmp->notice_id = $notice->id;
97 $photo_id = GNUsocialPhotoTemp::$tmp->insert();
99 common_log_db_error($photo, 'INSERT', __FILE__);
100 throw new ServerException(_m('Problem saving photo.'));
106 function onEndNoticeAsActivity($notice, &$activity)
108 common_log(LOG_INFO, 'photo plugin: EndNoticeAsActivity');
109 $photo = GNUsocialPhoto::staticGet('notice_id', $notice->id);
111 common_log(LOG_INFO, 'not a photo.');
115 $activity->objects[0]->type = ActivityObject::PHOTO;
116 $activity->objects[0]->thumbnail = $photo->thumb_uri;
117 $activity->objects[0]->largerImage = $photo->uri;
122 function onStartHandleFeedEntry($activity)
124 common_log(LOG_INFO, 'photo plugin: onEndAtomPubNewActivity');
125 $oprofile = Ostatus_profile::ensureActorProfile($activity);
126 foreach ($activity->objects as $object) {
127 if($object->type == ActivityObject::PHOTO) {
128 $uri = $object->largerImage;
129 $thumb_uri = $object->thumbnail;
130 $profile_id = $oprofile->profile_id;
131 $source = 'unknown'; // TODO: put something better here.
133 common_log(LOG_INFO, 'uri : ' . $uri);
134 common_log(LOG_INFO, 'thumb_uri : ' . $thumb_uri);
136 // It's possible this is validated elsewhere, but I'm not sure and
137 // would rather be safe.
138 $uri = filter_var($uri, FILTER_SANITIZE_URL);
139 $thumb_uri = filter_var($thumb_uri, FILTER_SANITIZE_URL);
140 $uri = filter_var($uri, FILTER_VALIDATE_URL);
141 $thumb_uri = filter_var($thumb_uri, FILTER_VALIDATE_URL);
143 if(empty($thumb_uri)) {
144 // We need a thumbnail, so if we aren't given one, use the actual picture for now.
148 if (!empty($uri) && !empty($thumb_uri)) {
149 GNUsocialPhoto::saveNew($profile_id, $thumb_uri, $uri, $source, false);
151 common_log(LOG_INFO, 'bad URI for photo');
159 function onStartShowNoticeItem($action)
161 $photo = GNUsocialPhoto::staticGet('notice_id', $action->notice->id);
163 $action->out->elementStart('div', 'entry-title');
164 $action->showAuthor();
165 $action->out->elementStart('a', array('href' => $photo->uri));
166 $action->out->element('img', array('src' => $photo->thumb_uri,
167 'width' => 256, 'height' => 192));
168 $action->out->elementEnd('a');
169 $action->out->elementEnd('div');
170 $action->showNoticeInfo();
171 $action->showNoticeOptions();
177 function onEndPersonalGroupNav($nav)
179 $nav->out->menuItem(common_local_url('photos',
180 array('nickname' => $nav->action->trimmed('nickname'))), 'Photos');