Merged stuff from upstream/master
[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 include_once $dir . '/lib/photolib.php';
37
38 class GNUsocialPhotosPlugin extends Plugin
39 {
40     function onCheckSchema()
41     {
42         $schema = Schema::get();
43         $schema->ensureTable('GNUsocialPhoto', GNUsocialPhoto::schemaDef());
44         $schema->ensureTable('GNUsocialPhotoAlbum', GNUsocialPhotoAlbum::schemaDef());
45     }
46
47     function onRouterInitialized(URLMapper $m)
48     {
49         $m->connect(':nickname/photos', array('action' => 'photos'));
50         $m->connect(':nickname/photos/:albumid', array('action' => 'photos'));
51         $m->connect('main/uploadphoto', array('action' => 'photoupload'));
52         $m->connect('photo/:photoid', array('action' => 'photo'));
53         $m->connect('editphoto/:photoid', array('action' => 'editphoto'));
54         return true;
55     }
56
57     function onStartNoticeDistribute(Notice $notice)
58     {
59         common_log(LOG_INFO, "event: StartNoticeDistribute");
60         if (GNUsocialPhotoTemp::$tmp) {
61             GNUsocialPhotoTemp::$tmp->notice_id = $notice->id;
62             $photo_id = GNUsocialPhotoTemp::$tmp->insert();
63             if (!$photo_id) {
64                 common_log_db_error($photo, 'INSERT', __FILE__);
65                 throw new ServerException(_m('Problem saving photo.'));
66             }
67         }
68         return true;
69     }
70
71     function onEndNoticeAsActivity(Notice $stored, Activity $act, Profile $scoped=null)
72     {
73         common_log(LOG_INFO, 'photo plugin: EndNoticeAsActivity');
74         $photo = GNUsocialPhoto::getKV('notice_id', $stored->id);
75         if(!$photo) {
76             common_log(LOG_INFO, 'not a photo.');
77             return true;
78         }
79
80         $act->objects[0]->type = ActivityObject::PHOTO;
81         $act->objects[0]->thumbnail = $photo->thumb_uri;
82         $act->objects[0]->largerImage = $photo->uri;
83         return false;
84     }
85
86
87     function onStartHandleFeedEntry(Activity $activity)
88     {
89         common_log(LOG_INFO, 'photo plugin: onEndAtomPubNewActivity');
90         $oprofile = Ostatus_profile::ensureActorProfile($activity);
91         foreach ($activity->objects as $object) {
92             if($object->type == ActivityObject::PHOTO) {
93                 $uri = $object->largerImage;
94                 $thumb_uri = $object->thumbnail;
95                 $profile_id = $oprofile->profile_id;
96                 $source = 'unknown'; // TODO: put something better here.
97
98                 common_log(LOG_INFO, 'uri : ' .  $uri);
99                 common_log(LOG_INFO, 'thumb_uri : ' . $thumb_uri);
100
101                 // It's possible this is validated elsewhere, but I'm not sure and
102                 // would rather be safe.
103                 $uri = filter_var($uri, FILTER_SANITIZE_URL);
104                 $thumb_uri = filter_var($thumb_uri, FILTER_SANITIZE_URL);
105                 $uri = filter_var($uri, FILTER_VALIDATE_URL);
106                 $thumb_uri = filter_var($thumb_uri, FILTER_VALIDATE_URL);
107
108                 if(empty($thumb_uri)) {
109                     // We need a thumbnail, so if we aren't given one, use the actual picture for now.
110                     $thumb_uri = $uri;
111                 }
112
113                 if (!empty($uri) && !empty($thumb_uri)) {
114                     GNUsocialPhoto::saveNew($profile_id, $thumb_uri, $uri, $source, false);
115                 } else {
116                     common_log(LOG_INFO, 'bad URI for photo');
117                 }
118                 return false;
119             }
120         }
121         return true;
122     }
123
124     function onStartShowNoticeItem(Action $action)
125     {
126         $photo = GNUsocialPhoto::getKV('notice_id', $action->notice->id);
127         if($photo) { 
128             $action->out->elementStart('div', 'entry-title');
129             $action->showAuthor();
130             $action->out->elementStart('a', array('href' => $photo->getPageLink()));
131             $action->out->element('img', array('src' => $photo->thumb_uri,
132                                     'width' => 256, 'height' => 192));
133             $action->out->elementEnd('a');
134             $action->out->elementEnd('div');
135             $action->showNoticeInfo();
136             $action->showNoticeOptions();
137             return false;
138         }
139         return true;
140     } 
141
142     /*    function onEndShowNoticeFormData($action)
143     {
144         $link = "/main/uploadphoto";
145         $action->out->element('label', array('for' => 'photofile'),_('Attach'));
146         $action->out->element('input', array('id' => 'photofile',
147                                      'type' => 'file',
148                                      'name' => 'photofile',
149                                      'title' => _('Upload a photo')));
150     }
151     */
152     function onEndPersonalGroupNav(Menu $nav, Profile $target, Profile $scoped=null)
153     {
154       
155         $nav->out->menuItem(common_local_url('photos',
156                            array('nickname' => $nav->action->trimmed('nickname'))), _('Photos'), 
157                            _('Photo gallery'), $nav->action->trimmed('action') == 'photos', 'nav_photos');
158     }
159
160     function onEndShowStyles(Action $action)
161     {
162         $action->cssLink('/plugins/GNUsocialPhotos/res/style.css');
163     }
164
165     function onEndShowScripts(Action $action)
166     {
167         $action->script('plugins/GNUsocialPhotos/res/gnusocialphotos.js');
168     }
169 }
170