| 1 | <?php |
| 2 | /*************************************************************************** |
| 3 | * Copyright (C) 2003-2011 Polytechnique.org * |
| 4 | * http://opensource.polytechnique.org/ * |
| 5 | * * |
| 6 | * This program is free software; you can redistribute it and/or modify * |
| 7 | * it under the terms of the GNU General Public License as published by * |
| 8 | * the Free Software Foundation; either version 2 of the License, or * |
| 9 | * (at your option) any later version. * |
| 10 | * * |
| 11 | * This program is distributed in the hope that it will be useful, * |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 | * GNU General Public License for more details. * |
| 15 | * * |
| 16 | * You should have received a copy of the GNU General Public License * |
| 17 | * along with this program; if not, write to the Free Software * |
| 18 | * Foundation, Inc., * |
| 19 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
| 20 | ***************************************************************************/ |
| 21 | |
| 22 | |
| 23 | class SharingAPIModule extends PlModule |
| 24 | { |
| 25 | function handlers() |
| 26 | { |
| 27 | return array( |
| 28 | 'api/1/sharing/search' => $this->make_api_hook('search', AUTH_COOKIE, 'api_user_readonly'), |
| 29 | 'api/1/sharing/bulkmail' => $this->make_api_hook('bulkmail', AUTH_COOKIE, 'api_user_readonly'), |
| 30 | 'api/1/sharing/picture' => $this->make_hook('picture_token', AUTH_PUBLIC), |
| 31 | ); |
| 32 | } |
| 33 | |
| 34 | function handler_search(PlPage $page, PlUser $authUser, $payload) |
| 35 | { |
| 36 | require_once 'partnersharing.inc.php'; |
| 37 | $partner = PartnerSharing::fetchByAPIUser($authUser); |
| 38 | if ($partner == null || !$partner->has_directory) { |
| 39 | return PL_FORBIDDEN; |
| 40 | } |
| 41 | |
| 42 | $this->load('request.inc.php'); |
| 43 | |
| 44 | $payload = new PlDict($payload); |
| 45 | |
| 46 | $errors = WSDirectoryRequest::validatePayload($payload); |
| 47 | |
| 48 | if (count($errors)) { |
| 49 | foreach ($errors as $error_code) { |
| 50 | $page->trigError(WSDirectoryRequest::$ERROR_MESSAGES[$error_code]); |
| 51 | } |
| 52 | return PL_BAD_REQUEST; |
| 53 | } |
| 54 | |
| 55 | // Processing |
| 56 | $request = new WSDirectoryRequest($partner, $payload); |
| 57 | $request->assignToPage($page); |
| 58 | return PL_JSON; |
| 59 | } |
| 60 | |
| 61 | function handler_bulkmail(PlPage $page, PlUser $authUser, $payload) |
| 62 | { |
| 63 | require_once 'partnersharing.inc.php'; |
| 64 | $partner = PartnerSharing::fetchByAPIUser($authUser); |
| 65 | if ($partner == null || !$partner->has_bulkmail) { |
| 66 | return PL_FORBIDDEN; |
| 67 | } |
| 68 | |
| 69 | if (!isset($payload['uids'])) { |
| 70 | $page->trigError('Malformed query.'); |
| 71 | return PL_BAD_REQUEST; |
| 72 | } |
| 73 | |
| 74 | $uids = $payload['uids']; |
| 75 | |
| 76 | $pf = new UserFilter( |
| 77 | new PFC_And( |
| 78 | new UFC_PartnerSharingID($partner->id, $uids), |
| 79 | new UFC_HasValidEmail(), |
| 80 | new UFC_PartnerSharingEmail($partner->id) |
| 81 | )); |
| 82 | |
| 83 | $contexts = array(); |
| 84 | foreach ($pf->iterUsers() as $user) { |
| 85 | $contexts[] = array( |
| 86 | 'name' => $user->fullName(), |
| 87 | 'email' => $user->bestEmail(), |
| 88 | 'gender' => $user->isFemale() ? 'woman' : 'man', |
| 89 | ); |
| 90 | } |
| 91 | $page->jsonAssign('contexts', $contexts); |
| 92 | return PL_JSON; |
| 93 | } |
| 94 | |
| 95 | function handler_picture_token(PlPage $page, $size, $token) |
| 96 | { |
| 97 | XDB::rawExecute('DELETE FROM profile_photo_tokens |
| 98 | WHERE expires <= NOW()'); |
| 99 | $pid = XDB::fetchOneCell('SELECT pid |
| 100 | FROM profile_photo_tokens |
| 101 | WHERE token = {?}', $token); |
| 102 | if ($pid != null) { |
| 103 | $res = XDB::fetchOneAssoc('SELECT attach, attachmime, x, y, last_update |
| 104 | FROM profile_photos |
| 105 | WHERE pid = {?}', $pid); |
| 106 | $photo = PlImage::fromData($res['attach'], 'image/' . $res['attachmime'], $res['x'], $res['y'], $res['last_update']); |
| 107 | $photo->send(); |
| 108 | } else { |
| 109 | return PL_NOT_FOUND; |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
| 115 | ?> |