backport patch 441
[platal.git] / modules / profile.php
CommitLineData
7d8b17cb 1<?php
2/***************************************************************************
3 * Copyright (C) 2003-2006 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
22class ProfileModule extends PLModule
23{
24 function handlers()
25 {
26 return array(
fb9a56cb 27 'photo' => $this->make_hook('photo', AUTH_PUBLIC),
28 'photo/change' => $this->make_hook('photo_change', AUTH_MDP),
29 'trombi' => $this->make_hook('trombi', AUTH_COOKIE),
7d8b17cb 30 );
31 }
32
33 function _trombi_getlist($offset, $limit)
34 {
35 global $globals;
36
37 $where = ( $this->promo > 0 ? "WHERE promo='{$this->promo}'" : "" );
38
39 $res = $globals->xdb->query(
40 "SELECT COUNT(*)
41 FROM auth_user_md5 AS u
42 RIGHT JOIN photo AS p ON u.user_id=p.uid
43 $where");
44 $pnb = $res->fetchOneCell();
45
46 $res = $globals->xdb->query(
47 "SELECT promo,user_id,a.alias AS forlife,IF(nom_usage='', nom, nom_usage) AS nom,prenom
48 FROM photo AS p
49 INNER JOIN auth_user_md5 AS u ON u.user_id=p.uid
50 INNER JOIN aliases AS a ON ( u.user_id=a.id AND a.type='a_vie' )
51 $where
52 ORDER BY promo,nom,prenom LIMIT {?}, {?}", $offset*$limit, $limit);
53
54 return array($pnb, $res->fetchAllAssoc());
55 }
56
adbdf493 57 function handler_photo(&$page, $x = null, $req = null)
58 {
59 if (is_null($x)) {
60 return PL_NOT_FOUND;
61 }
62
63 global $globals;
64
65 $res = $globals->xdb->query("SELECT id, pub FROM aliases
66 LEFT JOIN photo ON(id = uid)
67 WHERE alias = {?}", $x);
68 list($uid, $photo_pub) = $res->fetchOneRow();
69
70 if ($req && logged()) {
71 include 'validations.inc.php';
72 $myphoto = PhotoReq::get_request($uid);
73 Header('Content-type: image/'.$myphoto->mimetype);
74 echo $myphoto->data;
75 } else {
76 $res = $globals->xdb->query(
77 "SELECT attachmime, attach
78 FROM photo
79 WHERE uid={?}", $uid);
80
81 if ((list($type,$data) = $res->fetchOneRow()) && ($photo_pub == 'public' || logged())) {
82 Header("Content-type: image/$type");
83 echo $data;
84 } else {
85 Header('Content-type: image/png');
fb9a56cb 86 echo file_get_contents(dirname(__FILE__).'/../htdocs/images/none.png');
adbdf493 87 }
88 }
89 exit;
90 }
91
fb9a56cb 92 function handler_photo_change(&$page)
93 {
94 global $globals;
95
96 $page->changeTpl('trombino.tpl');
97
98 require_once('validations.inc.php');
99
100 $trombi_x = '/home/web/trombino/photos'.Session::get('promo')
101 .'/'.Session::get('forlife').'.jpg';
102
103 if (Env::has('upload')) {
104 $file = isset($_FILES['userfile']['tmp_name'])
105 ? $_FILES['userfile']['tmp_name']
106 : Env::get('photo');
107 if ($data = file_get_contents($file)) {
108 if ($myphoto = new PhotoReq(Session::getInt('uid'), $data)) {
109 $myphoto->submit();
110 }
111 } else {
112 $page->trig('Fichier inexistant ou vide');
113 }
114 } elseif (Env::has('trombi')) {
115 $myphoto = new PhotoReq(Session::getInt('uid'),
116 file_get_contents($trombi_x));
117 if ($myphoto) {
118 $myphoto->commit();
119 $myphoto->clean();
120 }
121 } elseif (Env::get('suppr')) {
122 $globals->xdb->execute('DELETE FROM photo WHERE uid = {?}',
123 Session::getInt('uid'));
124 $globals->xdb->execute('DELETE FROM requests
125 WHERE user_id = {?} AND type="photo"',
126 Session::getInt('uid'));
127 } elseif (Env::get('cancel')) {
128 $sql = $globals->xdb->query('DELETE FROM requests
129 WHERE user_id={?} AND type="photo"',
130 Session::getInt('uid'));
131 }
132
133 $sql = $globals->xdb->query('SELECT COUNT(*) FROM requests
134 WHERE user_id={?} AND type="photo"',
135 Session::getInt('uid'));
136 $page->assign('submited', $sql->fetchOneCell());
137 $page->assign('has_trombi_x', file_exists($trombi_x));
138
139 return PL_OK;
140 }
141
7d8b17cb 142 function handler_trombi(&$page, $promo = null)
143 {
144 require_once 'trombi.inc.php';
145
146 $page->changeTpl('trombipromo.tpl');
147 $page->assign('xorg_title','Polytechnique.org - Trombi Promo');
148
149 if (is_null($promo)) {
150 return PL_OK;
151 }
152
153 $this->promo = $promo = intval($promo);
154
155 if ($promo >= 1900 && $promo < intval(date('Y'))
156 || ($promo == -1 && has_perms()))
157 {
158 $trombi = new Trombi(array($this, '_trombi_getlist'));
159 $trombi->hidePromo();
160 $trombi->setAdmin();
161 $page->assign_by_ref('trombi', $trombi);
162 } else {
163 $page->trig('Promotion incorrecte (saisir au format YYYY). Recommence.');
164 }
165
166 return PL_OK;
167 }
168}
169
170?>