]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/GNUsocialPhotos/GNUsocialPhotosPlugin.php
added link to photo upload page.
[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         default:
55             break;
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         return true;
82     }
83
84     function onStartNoticeDistribute($notice)
85     {
86         common_log(LOG_INFO, "event: StartNoticeDistribute");
87         if (GNUsocialPhotoTemp::$tmp) {
88             GNUsocialPhotoTemp::$tmp->notice_id = $notice->id;
89             $photo_id = GNUsocialPhotoTemp::$tmp->insert();
90             if (!$photo_id) {
91                 common_log_db_error($photo, 'INSERT', __FILE__);
92                 throw new ServerException(_m('Problem saving photo.'));
93             }
94         }
95         return true;
96     }
97
98     function onEndNoticeAsActivity($notice, &$activity)
99     {
100         common_log(LOG_INFO, 'photo plugin: EndNoticeAsActivity');
101         $photo = GNUsocialPhoto::staticGet('notice_id', $notice->id);
102         if(!$photo) {
103             common_log(LOG_INFO, 'not a photo.');
104             return true;
105         }
106
107         $activity->objects[0]->type = ActivityObject::PHOTO;
108         $activity->objects[0]->thumbnail = $photo->thumb_uri;
109         $activity->objects[0]->largerImage = $photo->uri;
110         return false;
111     }
112
113
114     function onStartHandleFeedEntry($activity)
115     {
116         common_log(LOG_INFO, 'photo plugin: onEndAtomPubNewActivity');
117         $oprofile = Ostatus_profile::ensureActorProfile($activity);
118         foreach ($activity->objects as $object) {
119             if($object->type == ActivityObject::PHOTO) {
120                 $uri = $object->largerImage;
121                 $thumb_uri = $object->thumbnail;
122                 $profile_id = $oprofile->profile_id;
123                 $source = 'unknown'; // TODO: put something better here.
124
125                 common_log(LOG_INFO, 'uri : ' .  $uri);
126                 common_log(LOG_INFO, 'thumb_uri : ' . $thumb_uri);
127
128                 // It's possible this is validated elsewhere, but I'm not sure and
129                 // would rather be safe.
130                 $uri = filter_var($uri, FILTER_SANITIZE_URL);
131                 $thumb_uri = filter_var($thumb_uri, FILTER_SANITIZE_URL);
132                 $uri = filter_var($uri, FILTER_VALIDATE_URL);
133                 $thumb_uri = filter_var($thumb_uri, FILTER_VALIDATE_URL);
134
135                 if(empty($thumb_uri)) {
136                     // We need a thumbnail, so if we aren't given one, use the actual picture for now.
137                     $thumb_uri = $uri;
138                 }
139
140                 if (!empty($uri) && !empty($thumb_uri)) {
141                     GNUsocialPhoto::saveNew($profile_id, $thumb_uri, $uri, $source, false);
142                 } else {
143                     common_log(LOG_INFO, 'bad URI for photo');
144                 }
145                 return false;
146             }
147         }
148         return true;
149     }
150
151     function onStartShowNoticeItem($action)
152     {
153         $photo = GNUsocialPhoto::staticGet('notice_id', $action->notice->id);
154         if($photo) { 
155             $action->out->elementStart('div', 'entry-title');
156             $action->showAuthor();
157             $action->out->elementStart('a', array('href' => $photo->uri));
158             $action->out->element('img', array('src' => $photo->thumb_uri,
159                                     'width' => 256, 'height' => 192));
160             $action->out->elementEnd('a');
161             $action->out->elementEnd('div');
162             $action->showNoticeInfo();
163             $action->showNoticeOptions();
164             return false;
165         }
166         return true;
167     } 
168
169     function onEndPersonalGroupNav($nav)
170     {
171         if($nav->action instanceof ShowstreamAction) {
172             $nav->out->menuItem(common_local_url('photos',
173                 array('nickname' => $nav->action->trimmed('nickname'))), 'Photos');
174         }
175     }
176 }