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