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';
46 include_once $dir . '/lib/photolib.php';
47 include_once $dir . '/actions/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
49 case 'PhotouploadAction':
50 include_once $dir . '/lib/photolib.php';
51 include_once $dir . '/actions/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
57 include_once $dir . '/classes/gnusocialphoto.php';
58 include_once $dir . '/classes/gnusocialphotoalbum.php';
62 function onCheckSchema()
64 $schema = Schema::get();
65 $schema->ensureTable('GNUsocialPhoto',
66 array(new ColumnDef('notice_id', 'int(11)', null, false),
67 new ColumnDef('album_id', 'int(11)', null, false),
68 new ColumnDef('uri', 'varchar(512)', null, false),
69 new ColumnDef('thumb_uri', 'varchar(512)', null, false)));
70 $schema->ensureTable('GNUsocialPhotoAlbum',
71 array(new ColumnDef('album_id', 'int(11)', null, false, 'PRI', null, null, true),
72 new ColumnDef('profile_id', 'int(11)', null, false),
73 new ColumnDef('album_name', 'varchar(256)', null, false)));
77 function onRouterInitialized($m)
79 $m->connect(':nickname/photos', array('action' => 'photos'));
80 $m->connect('main/uploadphoto', array('action' => 'photoupload'));
81 common_log(LOG_INFO, "init'd!");
85 function onStartNoticeDistribute($notice)
87 common_log(LOG_INFO, "event: StartNoticeDistribute");
88 if (GNUsocialPhotoTemp::$tmp) {
89 GNUsocialPhotoTemp::$tmp->notice_id = $notice->id;
90 $photo_id = GNUsocialPhotoTemp::$tmp->insert();
92 common_log_db_error($photo, 'INSERT', __FILE__);
93 throw new ServerException(_m('Problem saving photo.'));
99 function onEndNoticeAsActivity($notice, &$activity)
101 common_log(LOG_INFO, 'photo plugin: EndNoticeAsActivity');
102 $photo = GNUsocialPhoto::staticGet('notice_id', $notice->id);
104 common_log(LOG_INFO, 'not a photo.');
108 $activity->objects[0]->type = ActivityObject::PHOTO;
109 $activity->objects[0]->thumbnail = $photo->thumb_uri;
110 $activity->objects[0]->largerImage = $photo->uri;
115 function onStartHandleFeedEntry($activity)
117 common_log(LOG_INFO, 'photo plugin: onEndAtomPubNewActivity');
118 $oprofile = Ostatus_profile::ensureActorProfile($activity);
119 foreach ($activity->objects as $object) {
120 if($object->type == ActivityObject::PHOTO) {
121 $uri = $object->largerImage;
122 $thumb_uri = $object->thumbnail;
123 $profile_id = $oprofile->profile_id;
124 $source = 'unknown'; // TODO: put something better here.
126 common_log(LOG_INFO, 'uri : ' . $uri);
127 common_log(LOG_INFO, 'thumb_uri : ' . $thumb_uri);
129 // It's possible this is validated elsewhere, but I'm not sure and
130 // would rather be safe.
131 $uri = filter_var($uri, FILTER_SANITIZE_URL);
132 $thumb_uri = filter_var($thumb_uri, FILTER_SANITIZE_URL);
133 $uri = filter_var($uri, FILTER_VALIDATE_URL);
134 $thumb_uri = filter_var($thumb_uri, FILTER_VALIDATE_URL);
136 if(empty($thumb_uri)) {
137 // We need a thumbnail, so if we aren't given one, use the actual picture for now.
141 if (!empty($uri) && !empty($thumb_uri)) {
142 GNUsocialPhoto::saveNew($profile_id, $thumb_uri, $uri, $source, false);
144 common_log(LOG_INFO, 'bad URI for photo');
152 function onStartShowNoticeItem($action)
154 $photo = GNUsocialPhoto::staticGet('notice_id', $action->notice->id);
156 $action->out->elementStart('div', 'entry-title');
157 $action->showAuthor();
158 $action->out->elementStart('a', array('href' => $photo->uri));
159 $action->out->element('img', array('src' => $photo->thumb_uri,
160 'width' => 256, 'height' => 192));
161 $action->out->elementEnd('a');
162 $action->out->elementEnd('div');
163 $action->showNoticeInfo();
164 $action->showNoticeOptions();