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';
59 case 'EditphotoAction':
60 include_once $dir . '/lib/photolib.php';
61 include_once $dir . '/actions/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
66 include_once $dir . '/classes/gnusocialphoto.php';
67 include_once $dir . '/classes/gnusocialphotoalbum.php';
71 function onCheckSchema()
73 $schema = Schema::get();
74 $schema->ensureTable('GNUsocialPhoto',
75 array(new ColumnDef('id', 'int(11)', null, false, 'PRI', null, null, true),
76 new ColumnDef('notice_id', 'int(11)', null, false),
77 new ColumnDef('album_id', 'int(11)', null, false),
78 new ColumnDef('uri', 'varchar(512)', null, false),
79 new ColumnDef('thumb_uri', 'varchar(512)', null, false),
80 new ColumnDef('title', 'varchar(512)', null, false),
81 new ColumnDef('photo_description', 'text', null, false)));
82 $schema->ensureTable('GNUsocialPhotoAlbum',
83 array(new ColumnDef('album_id', 'int(11)', null, false, 'PRI', null, null, true),
84 new ColumnDef('profile_id', 'int(11)', null, false),
85 new ColumnDef('album_name', 'varchar(256)', null, false),
86 new ColumnDef('album_description', 'text', null, false)));
90 function onRouterInitialized($m)
92 $m->connect(':nickname/photos', array('action' => 'photos'));
93 $m->connect(':nickname/photos/:albumid', array('action' => 'photos'));
94 $m->connect('main/uploadphoto', array('action' => 'photoupload'));
95 $m->connect('photo/:photoid', array('action' => 'photo'));
96 $m->connect('editphoto/:photoid', array('action' => 'editphoto'));
100 function onStartNoticeDistribute($notice)
102 common_log(LOG_INFO, "event: StartNoticeDistribute");
103 if (GNUsocialPhotoTemp::$tmp) {
104 GNUsocialPhotoTemp::$tmp->notice_id = $notice->id;
105 $photo_id = GNUsocialPhotoTemp::$tmp->insert();
107 common_log_db_error($photo, 'INSERT', __FILE__);
108 throw new ServerException(_m('Problem saving photo.'));
114 function onEndNoticeAsActivity($notice, &$activity)
116 common_log(LOG_INFO, 'photo plugin: EndNoticeAsActivity');
117 $photo = GNUsocialPhoto::getKV('notice_id', $notice->id);
119 common_log(LOG_INFO, 'not a photo.');
123 $activity->objects[0]->type = ActivityObject::PHOTO;
124 $activity->objects[0]->thumbnail = $photo->thumb_uri;
125 $activity->objects[0]->largerImage = $photo->uri;
130 function onStartHandleFeedEntry($activity)
132 common_log(LOG_INFO, 'photo plugin: onEndAtomPubNewActivity');
133 $oprofile = Ostatus_profile::ensureActorProfile($activity);
134 foreach ($activity->objects as $object) {
135 if($object->type == ActivityObject::PHOTO) {
136 $uri = $object->largerImage;
137 $thumb_uri = $object->thumbnail;
138 $profile_id = $oprofile->profile_id;
139 $source = 'unknown'; // TODO: put something better here.
141 common_log(LOG_INFO, 'uri : ' . $uri);
142 common_log(LOG_INFO, 'thumb_uri : ' . $thumb_uri);
144 // It's possible this is validated elsewhere, but I'm not sure and
145 // would rather be safe.
146 $uri = filter_var($uri, FILTER_SANITIZE_URL);
147 $thumb_uri = filter_var($thumb_uri, FILTER_SANITIZE_URL);
148 $uri = filter_var($uri, FILTER_VALIDATE_URL);
149 $thumb_uri = filter_var($thumb_uri, FILTER_VALIDATE_URL);
151 if(empty($thumb_uri)) {
152 // We need a thumbnail, so if we aren't given one, use the actual picture for now.
156 if (!empty($uri) && !empty($thumb_uri)) {
157 GNUsocialPhoto::saveNew($profile_id, $thumb_uri, $uri, $source, false);
159 common_log(LOG_INFO, 'bad URI for photo');
167 function onStartShowNoticeItem($action)
169 $photo = GNUsocialPhoto::getKV('notice_id', $action->notice->id);
171 $action->out->elementStart('div', 'entry-title');
172 $action->showAuthor();
173 $action->out->elementStart('a', array('href' => $photo->getPageLink()));
174 $action->out->element('img', array('src' => $photo->thumb_uri,
175 'width' => 256, 'height' => 192));
176 $action->out->elementEnd('a');
177 $action->out->elementEnd('div');
178 $action->showNoticeInfo();
179 $action->showNoticeOptions();
185 /* function onEndShowNoticeFormData($action)
187 $link = "/main/uploadphoto";
188 $action->out->element('label', array('for' => 'photofile'),_('Attach'));
189 $action->out->element('input', array('id' => 'photofile',
191 'name' => 'photofile',
192 'title' => _('Upload a photo')));
195 function onEndPersonalGroupNav($nav)
198 $nav->out->menuItem(common_local_url('photos',
199 array('nickname' => $nav->action->trimmed('nickname'))), _('Photos'),
200 _('Photo gallery'), $nav->action->trimmed('action') == 'photos', 'nav_photos');
203 function onEndShowStyles($action)
205 $action->cssLink('/plugins/GNUsocialPhotos/res/style.css');
208 function onEndShowScripts($action)
210 $action->script('plugins/GNUsocialPhotos/res/gnusocialphotos.js');