]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/GNUsocialPhotos/GNUsocialPhotosPlugin.php
Merge branch 'master' of git://gitorious.org/~trombonechamp/statusnet/trombonechamps...
[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         include_once $dir . '/lib/photonav.php';
44         switch ($cls)
45         {
46         case 'PhotosAction':
47             include_once $dir . '/lib/photolib.php';
48             include_once $dir . '/actions/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
49             break;
50         case 'PhotouploadAction':
51             include_once $dir . '/lib/photolib.php';
52             include_once $dir . '/actions/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
53             break;
54                 case 'PhotoAction':
55                 include_once $dir . '/lib/photolib.php';
56                         include_once $dir . '/actions/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
57                         break;
58         default:
59             break;
60         }
61         include_once $dir . '/classes/gnusocialphoto.php';
62         include_once $dir . '/classes/gnusocialphotoalbum.php';
63         return true;
64     }
65
66     function onCheckSchema()
67     {
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)));
81                                           
82     }
83
84     function onRouterInitialized($m)
85     {
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'));
89         return true;
90     }
91
92     function onStartNoticeDistribute($notice)
93     {
94         common_log(LOG_INFO, "event: StartNoticeDistribute");
95         if (GNUsocialPhotoTemp::$tmp) {
96             GNUsocialPhotoTemp::$tmp->notice_id = $notice->id;
97             $photo_id = GNUsocialPhotoTemp::$tmp->insert();
98             if (!$photo_id) {
99                 common_log_db_error($photo, 'INSERT', __FILE__);
100                 throw new ServerException(_m('Problem saving photo.'));
101             }
102         }
103         return true;
104     }
105
106     function onEndNoticeAsActivity($notice, &$activity)
107     {
108         common_log(LOG_INFO, 'photo plugin: EndNoticeAsActivity');
109         $photo = GNUsocialPhoto::staticGet('notice_id', $notice->id);
110         if(!$photo) {
111             common_log(LOG_INFO, 'not a photo.');
112             return true;
113         }
114
115         $activity->objects[0]->type = ActivityObject::PHOTO;
116         $activity->objects[0]->thumbnail = $photo->thumb_uri;
117         $activity->objects[0]->largerImage = $photo->uri;
118         return false;
119     }
120
121
122     function onStartHandleFeedEntry($activity)
123     {
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.
132
133                 common_log(LOG_INFO, 'uri : ' .  $uri);
134                 common_log(LOG_INFO, 'thumb_uri : ' . $thumb_uri);
135
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);
142
143                 if(empty($thumb_uri)) {
144                     // We need a thumbnail, so if we aren't given one, use the actual picture for now.
145                     $thumb_uri = $uri;
146                 }
147
148                 if (!empty($uri) && !empty($thumb_uri)) {
149                     GNUsocialPhoto::saveNew($profile_id, $thumb_uri, $uri, $source, false);
150                 } else {
151                     common_log(LOG_INFO, 'bad URI for photo');
152                 }
153                 return false;
154             }
155         }
156         return true;
157     }
158
159     function onStartShowNoticeItem($action)
160     {
161         $photo = GNUsocialPhoto::staticGet('notice_id', $action->notice->id);
162         if($photo) { 
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();
172             return false;
173         }
174         return true;
175     } 
176
177     function onEndPersonalGroupNav($nav)
178     {
179         if($nav->action instanceof ShowstreamAction) {
180             $nav->out->menuItem(common_local_url('photos',
181                 array('nickname' => $nav->action->trimmed('nickname'))), 'Photos');
182         }
183     }
184 }