]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/GNUsocialPhotos/GNUsocialPhotosPlugin.php
corrected height/width on ad-hoc'd thumnails.
[quix0rs-gnu-social.git] / plugins / GNUsocialPhotos / GNUsocialPhotosPlugin.php
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  * @copyright 2010 Free Software Foundation, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
27  */
28
29 /* Photo sharing plugin */
30
31 if (!defined('STATUSNET')) {
32     exit(1);
33 }
34
35 class GNUsocialPhotosPlugin extends Plugin
36 {
37
38     function onAutoload($cls)
39     {
40         $dir = dirname(__FILE__);
41
42         include_once $dir . '/lib/tempphoto.php';
43         switch ($cls)
44         {
45         case 'PhotosAction':
46             include_once $dir . '/lib/photolib.php';
47             include_once $dir . '/actions/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
48             break;
49         case 'PhotouploadAction':
50             include_once $dir . '/lib/photolib.php';
51             include_once $dir . '/actions/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
52             break;
53         default:
54             break;
55         }
56
57         include_once $dir . '/classes/gnusocialphoto.php';
58         include_once $dir . '/classes/gnusocialphotoalbum.php';
59         return true;
60     }
61
62     function onCheckSchema()
63     {
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)));
74                                           
75     }
76
77     function onRouterInitialized($m)
78     {
79         $m->connect(':nickname/photos', array('action' => 'photos'));
80         $m->connect('main/uploadphoto', array('action' => 'photoupload'));
81         common_log(LOG_INFO, "init'd!");
82         return true;
83     }
84
85     function onStartNoticeDistribute($notice)
86     {
87         common_log(LOG_INFO, "event: StartNoticeDistribute");
88         if (GNUsocialPhotoTemp::$tmp) {
89             GNUsocialPhotoTemp::$tmp->notice_id = $notice->id;
90             $photo_id = GNUsocialPhotoTemp::$tmp->insert();
91             if (!$photo_id) {
92                 common_log_db_error($photo, 'INSERT', __FILE__);
93                 throw new ServerException(_m('Problem saving photo.'));
94             }
95         }
96         return true;
97     }
98
99     function onEndNoticeAsActivity($notice, &$activity)
100     {
101         common_log(LOG_INFO, 'photo plugin: EndNoticeAsActivity');
102         $photo = GNUsocialPhoto::staticGet('notice_id', $notice->id);
103         if(!$photo) {
104             common_log(LOG_INFO, 'not a photo.');
105             return true;
106         }
107
108         $activity->objects[0]->type = ActivityObject::PHOTO;
109         $activity->objects[0]->thumbnail = $photo->thumb_uri;
110         $activity->objects[0]->largerImage = $photo->uri;
111         return false;
112     }
113
114
115     function onStartHandleFeedEntry($activity)
116     {
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.
125
126                 common_log(LOG_INFO, 'uri : ' .  $uri);
127                 common_log(LOG_INFO, 'thumb_uri : ' . $thumb_uri);
128
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);
135
136                 if(empty($thumb_uri)) {
137                     // We need a thumbnail, so if we aren't given one, use the actual picture for now.
138                     $thumb_uri = $uri;
139                 }
140
141                 if (!empty($uri) && !empty($thumb_uri)) {
142                     GNUsocialPhoto::saveNew($profile_id, $thumb_uri, $uri, $source, false);
143                 } else {
144                     common_log(LOG_INFO, 'bad URI for photo');
145                 }
146                 return false;
147             }
148         }
149         return true;
150     }
151
152     function onStartShowNoticeItem($action)
153     {
154         $photo = GNUsocialPhoto::staticGet('notice_id', $action->notice->id);
155         if($photo) { 
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();
165             return false;
166         }
167         return true;
168     } 
169 }