]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/GNUsocialPhotos/GNUsocialPhotosPlugin.php
Individual ids for photos
[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  * @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
28  */
29
30 /* Photo sharing plugin */
31
32 if (!defined('STATUSNET')) {
33     exit(1);
34 }
35
36 class GNUsocialPhotosPlugin extends Plugin
37 {
38
39     function onAutoload($cls)
40     {
41         $dir = dirname(__FILE__);
42
43         include_once $dir . '/lib/tempphoto.php';
44         include_once $dir . '/lib/photonav.php';
45         switch ($cls)
46         {
47         case 'PhotosAction':
48             include_once $dir . '/lib/photolib.php';
49             include_once $dir . '/actions/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
50             break;
51         case 'PhotouploadAction':
52             include_once $dir . '/lib/photolib.php';
53             include_once $dir . '/actions/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
54             break;
55         case 'PhotoAction':
56             include_once $dir . '/lib/photolib.php';
57             include_once $dir . '/actions/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
58             break;
59         default:
60             break;
61         }
62         include_once $dir . '/classes/gnusocialphoto.php';
63         include_once $dir . '/classes/gnusocialphotoalbum.php';
64         return true;
65     }
66
67     function onCheckSchema()
68     {
69         $schema = Schema::get();
70         $schema->ensureTable('GNUsocialPhoto',
71                                 array(new ColumnDef('id', 'int(11)', null, false, 'PRI', null, null, true),
72                                       new ColumnDef('notice_id', 'int(11)', null, false),
73                                       new ColumnDef('album_id', 'int(11)', null, false),
74                                       new ColumnDef('uri', 'varchar(512)', null, false),
75                                       new ColumnDef('thumb_uri', 'varchar(512)', null, false),
76                                       new ColumnDef('title', 'varchar(512)', null, false),
77                                       new ColumnDef('photo_description', 'text', null, false)));
78         $schema->ensureTable('GNUsocialPhotoAlbum',
79                                 array(new ColumnDef('album_id', 'int(11)', null, false, 'PRI', null, null, true),
80                                       new ColumnDef('profile_id', 'int(11)', null, false),
81                                       new ColumnDef('album_name', 'varchar(256)', null, false),
82                                       new ColumnDef('album_description', 'text', null, false)));
83                                           
84     }
85
86     function onRouterInitialized($m)
87     {
88         $m->connect(':nickname/photos', array('action' => 'photos'));
89         $m->connect('main/uploadphoto', array('action' => 'photoupload'));
90         $m->connect('photo/:photoid', array('action' => 'photo'));
91         return true;
92     }
93
94     function onStartNoticeDistribute($notice)
95     {
96         common_log(LOG_INFO, "event: StartNoticeDistribute");
97         if (GNUsocialPhotoTemp::$tmp) {
98             GNUsocialPhotoTemp::$tmp->notice_id = $notice->id;
99             $photo_id = GNUsocialPhotoTemp::$tmp->insert();
100             if (!$photo_id) {
101                 common_log_db_error($photo, 'INSERT', __FILE__);
102                 throw new ServerException(_m('Problem saving photo.'));
103             }
104         }
105         return true;
106     }
107
108     function onEndNoticeAsActivity($notice, &$activity)
109     {
110         common_log(LOG_INFO, 'photo plugin: EndNoticeAsActivity');
111         $photo = GNUsocialPhoto::staticGet('notice_id', $notice->id);
112         if(!$photo) {
113             common_log(LOG_INFO, 'not a photo.');
114             return true;
115         }
116
117         $activity->objects[0]->type = ActivityObject::PHOTO;
118         $activity->objects[0]->thumbnail = $photo->thumb_uri;
119         $activity->objects[0]->largerImage = $photo->uri;
120         return false;
121     }
122
123
124     function onStartHandleFeedEntry($activity)
125     {
126         common_log(LOG_INFO, 'photo plugin: onEndAtomPubNewActivity');
127         $oprofile = Ostatus_profile::ensureActorProfile($activity);
128         foreach ($activity->objects as $object) {
129             if($object->type == ActivityObject::PHOTO) {
130                 $uri = $object->largerImage;
131                 $thumb_uri = $object->thumbnail;
132                 $profile_id = $oprofile->profile_id;
133                 $source = 'unknown'; // TODO: put something better here.
134
135                 common_log(LOG_INFO, 'uri : ' .  $uri);
136                 common_log(LOG_INFO, 'thumb_uri : ' . $thumb_uri);
137
138                 // It's possible this is validated elsewhere, but I'm not sure and
139                 // would rather be safe.
140                 $uri = filter_var($uri, FILTER_SANITIZE_URL);
141                 $thumb_uri = filter_var($thumb_uri, FILTER_SANITIZE_URL);
142                 $uri = filter_var($uri, FILTER_VALIDATE_URL);
143                 $thumb_uri = filter_var($thumb_uri, FILTER_VALIDATE_URL);
144
145                 if(empty($thumb_uri)) {
146                     // We need a thumbnail, so if we aren't given one, use the actual picture for now.
147                     $thumb_uri = $uri;
148                 }
149
150                 if (!empty($uri) && !empty($thumb_uri)) {
151                     GNUsocialPhoto::saveNew($profile_id, $thumb_uri, $uri, $source, false);
152                 } else {
153                     common_log(LOG_INFO, 'bad URI for photo');
154                 }
155                 return false;
156             }
157         }
158         return true;
159     }
160
161     function onStartShowNoticeItem($action)
162     {
163         $photo = GNUsocialPhoto::staticGet('notice_id', $action->notice->id);
164         if($photo) { 
165             $action->out->elementStart('div', 'entry-title');
166             $action->showAuthor();
167             $action->out->elementStart('a', array('href' => $photo->getPageLink()));
168             $action->out->element('img', array('src' => $photo->thumb_uri,
169                                     'width' => 256, 'height' => 192));
170             $action->out->elementEnd('a');
171             $action->out->elementEnd('div');
172             $action->showNoticeInfo();
173             $action->showNoticeOptions();
174             return false;
175         }
176         return true;
177     } 
178
179     /*    function onEndShowNoticeFormData($action)
180     {
181         $link = "/main/uploadphoto";
182         $action->out->element('label', array('for' => 'photofile'),_('Attach'));
183         $action->out->element('input', array('id' => 'photofile',
184                                      'type' => 'file',
185                                      'name' => 'photofile',
186                                      'title' => _('Upload a photo')));
187     }
188     */
189     function onEndPersonalGroupNav($nav)
190     {
191       
192         $nav->out->menuItem(common_local_url('photos',
193                            array('nickname' => $nav->action->trimmed('nickname'))), _('Photos'), 
194                            _('Photo gallery'), $nav->action->trimmed('action') == 'photos', 'nav_photos');
195     }
196 }
197