]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/GNUsocialPhotos/GNUsocialPhotosPlugin.php
Photo Galleries now actually use the database.
[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         switch ($cls)
43         {
44         case 'PhotosAction':
45             include_once $dir . '/lib/photolib.php';
46             include_once $dir . '/actions/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
47             break;
48         case 'PhotouploadAction':
49             include_once $dir . '/lib/photolib.php';
50             include_once $dir . '/actions/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
51             break;
52         default:
53             break;
54         }
55
56         include_once $dir . '/classes/gnusocialphoto.php';
57         return true;
58     }
59
60     function onCheckSchema()
61     {
62         $schema = Schema::get();
63         $schema->ensureTable('GNUsocialPhoto',
64                                 array(new ColumnDef('notice_id', 'int(11)', null, false),
65                                       new ColumnDef('album_id', 'int(11)', null, false),
66                                       //new ColumnDef('album_name', 'varchar(30)', null, false),
67                                       new ColumnDef('uri', 'varchar(512)', null, false),
68                                       new ColumnDef('thumb_uri', 'varchar(512)', null, false)));
69     }
70
71     function onRouterInitialized($m)
72     {
73         $m->connect(':nickname/photos', array('action' => 'photos'));
74         $m->connect('main/uploadphoto', array('action' => 'photoupload'));
75         common_log(LOG_INFO, "init'd!");
76         return true;
77     }
78
79     function onStartActivityDefaultObjectType(&$notice, &$xs, &$type)
80     {
81         $photo = GNUsocialPhoto::staticGet('notice_id', $notice->id);
82         if($photo) {
83             $type = ActivityObject::PHOTO;
84         }
85     }
86
87     function onStartActivityObjects(&$notice, &$xs, &$objects)
88     {
89         $photo = GNUsocialPhoto::staticGet('notice_id', $notice->id);
90         if($photo) {
91             $object = new ActivityObject();
92             $object->thumbnail = $photo->thumb_uri;
93             $object->largerImage = $photo->uri;
94             $object->type = ActivityObject::PHOTO;
95             
96             $object->id = $notice->id;
97             $objects[0] = $object;
98         }
99     }
100
101     function onStartHandleFeedEntry($activity)
102     {
103         if ($activity->verb == ActivityVerb::POST) {
104             $oprofile = Ostatus_profile::ensureActorProfile($activity);
105             foreach ($activity->objects as $object) {
106                 if ($object->type == ActivityObject::PHOTO) {
107                     $uri = $object->largerImage;
108                     $thumb_uri = $object->thumbnail;
109                     $profile_id = $oprofile->profile_id;
110                     $source = 'unknown'; // TODO: put something better here.
111
112                     $uri = filter_var($uri, FILTER_SANITIZE_URL);
113                     $thumb_uri = filter_var($thumb_uri, FILTER_SANITIZE_URL);
114                     $uri = filter_var($uri, FILTER_VALIDATE_URL);
115                     $thumb_uri = filter_var($thumb_uri, FILTER_VALIDATE_URL);
116                     if (!empty($uri) && !empty($thumb_uri)) {
117                         GNUsocialPhoto::saveNew($profile_id, $thumb_uri, $uri, $source);
118                     }
119                     return false;
120                 }
121             }
122         }
123         return true;
124     }
125
126
127     function onStartShowNoticeItem($action)
128     {
129         $photo = GNUsocialPhoto::staticGet('notice_id', $action->notice->id);
130         if($photo) { 
131             $action->out->elementStart('div', 'entry-title');
132             $action->showAuthor();
133             $action->out->elementStart('a', array('href' => $photo->uri));
134             $action->out->element('img', array('src' => $photo->thumb_uri));
135             $action->out->elementEnd('a');
136             $action->out->elementEnd('div');
137             $action->showNoticeInfo();
138             $action->showNoticeOptions();
139             return false;
140         }
141         return true;
142     } 
143 }