-
Notifications
You must be signed in to change notification settings - Fork 0
/
kpicasa.module
executable file
·366 lines (313 loc) · 12.9 KB
/
kpicasa.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
<?php
function kpicasa_init(){
drupal_add_css(drupal_get_path('module', 'kpicasa') .'/kpicasa.css');
}
function kpicasa_menu() {
$items = array();
$items['admin/settings/kpicasa'] = array(
'title' => 'kPicasa settings',
'description' => 'Settings of the kPicasa module',
'page callback' => 'drupal_get_form',
'page arguments' => array('kpicasa_admin_settings'),
'access arguments' => array('administer site configuration'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
function kpicasa_admin_settings() {
$form = array();
$settings = _kpicasa_get_settings();
$form['kpicasa_general_settings'] = array(
'#type' => 'fieldset',
'#title' => t('General settings'),
'#tree' => TRUE,
);
$form['kpicasa_general_settings']['username'] = array(
'#type' => 'textfield',
'#title' => t('Picasa Web Albums Username'),
'#default_value' => $settings['kpicasa_general_settings']['username'],
);
$form['kpicasa_album_list'] = array(
'#type' => 'fieldset',
'#title' => t('Album list'),
'#tree' => TRUE,
);
$form['kpicasa_album_list']['extra'] = array(
'#type' => 'checkboxes',
'#title' => t('Show extra information'),
'#options' => array(
'summary' => t('Summary'),
'location' => t('Location'),
'published_date' => t('Published date'),
'number_of_pictures' => t('Number of pictures'),
),
'#default_value' => $settings['kpicasa_album_list']['extra'],
);
$form['picture_list'] = array(
'#type' => 'fieldset',
'#title' => t('Picture list'),
'#tree' => TRUE,
);
return system_settings_form($form);
}
function _kpicasa_get_settings() {
$settings['kpicasa_general_settings'] = variable_get('kpicasa_general_settings', array('username' => ''));
$settings['kpicasa_album_list'] = variable_get('kpicasa_album_list', array('extra' => array(
'summary' => 0,
'location' => 0,
'publised_date' => 0,
'number_of_pictures' => 0,
)));
return $settings;
}
function kpicasa_filter($op, $delta = 0, $format = -1, $text = '', $cache_id = 0) {
switch ($op) {
case 'list':
return array(0 => t('Picasa'));
case 'description':
return t('Allows users to integrate Picasa albums.');
case 'prepare':
// Note: we use the bytes 0xFE and 0xFF to replace < > during the
// filtering process. These bytes are not valid in UTF-8 data and thus
// least likely to cause problems.
$text = preg_replace('@<code>(.+?)</code>@se', "'\xFEcode\xFF'. codefilter_escape('\\1') .'\xFE/code\xFF'", $text);
$text = preg_replace('@<(\?(php)?|%)(.+?)(\?|%)>@se', "'\xFEphp\xFF'. codefilter_escape('\\3') .'\xFE/php\xFF'", $text);
return $text;
case 'process':
$picasas = _kpicasa_prepare($text); //returns an array of $tables[0] = table macro $table[1]= table html
if ($picasas) { // there are table macros in this node
return str_replace($picasas[0], $picasas[1], $text);
}
else {
return $text;
}
case 'prepare':
return $text;
case 'no cache':
return TRUE;
default:
return $text;
}
}
function _kpicasa_prepare($intext) {
$out = FALSE;
$matches = array();
preg_match_all('/\[picasa([^\[\]]+ )* \] /x', $intext, $matches);
$i = 0;
while (isset($matches[1][$i])) {
$out[0][$i] = $matches[0][$i];
//$map = array('#settings' => gmap_parse_macro($matches[1][$i], $ver));
//$out[1][$i] = theme('gmap', $map);
//drupal_set_message('settings: '.$matches[1][$i]);
$out[1][$i] = _kpicasa_show_albums($matches[1][$i]);
$i++;
} // endwhile process macro
return $out;
}
function _kpicasa_show_albums($options_string){
// Get the settings
$settings = _kpicasa_get_settings();
// Convert options string to array.
$options = array();
if ($options_string) {
if (substr($options_string,0,1) == '|') {
$options_string = substr($options_string, 1);
}
if ($options_string) {
$options = explode('|', $options_string);
}
}
$username = '';
if ($options[0]) {
$username = $options[0];
}
else {
$username = $settings['kpicasa_general_settings']['username'];
}
// Check if an album contants has to be schown.
if (isset($_GET['album'])) {
if (!is_array($_GET['album'])) {
if (strpos($_GET['album'], '/')) {
$album_parts = explode('/', $_GET['album']);
if (count($album_parts) == 2 && $album_parts[0] == $username) {
return _kpicasa_show_album($username, $album_parts[1]);
}
}
}
else {
foreach ($_GET['album'] as $album) {
$album_parts = explode('/', $_GET['album']);
if (count($album_parts) == 2 && $album_parts[0] == $username) {
return _kpicasa_show_album($username, $album_parts[1]);
}
}
}
}
// Get the XML.
$url = 'http://picasaweb.google.com/data/feed/api/user/' . urlencode($username) . '?kind=album';
$data = @file_get_contents( $url );
if (!$data) {
drupal_set_message(t('Unable to get Picasa album for this user %username', array('%username' => $username)), 'error');
return '';
}
$data = str_replace('gphoto:', 'gphoto_', $data);
$data = str_replace('media:', 'media_', $data);
$xml = @simplexml_load_string($data);
if ( $xml === false ) {
drupal_set_message(t('Invalid XML received from Picasa'), 'error');
return '';
}
// Prepare some variables.
/* TODO paging stuff
$page = isset($_GET['kpgp']) && intval($_GET['kpgp']) > 1 ? intval($_GET['kpgp']) : 1; // kpgp = kPicasa Gallery Page
$url = get_permalink();
if ( $page > 1 )
{
$url = add_query_arg('kpgp', $page, $url);
}
if ( $this->config['albumPerPage'] > 0 )
{
$start = ($page - 1) * $this->config['albumPerPage'];
$stop = $start + $this->config['albumPerPage'] - 1;
}
else
{
$start = 0;
$stop = count( $xml->entry ) - 1;
}
*/
// Set the class, depending on how many albums per row
//$class = $this->config['albumPerRow'] == 1 ? 'kpg-thumb-onePerRow' : 'kpg-thumb-multiplePerRow';
$class = 'kpg-thumb-multiplePerRow';
// Loop through the albums.
$output = '<div class="kpg-album-list">';
$i = 0; $j = 0;
foreach( $xml->entry as $album ) {
$output .= '<div class="kpg-album">';
$name = (string) $album->gphoto_name;
$title = htmlspecialchars( (string) $album->title );
$nbPhotos = (string) $album->gphoto_numphotos;
$albumURL = drupal_get_path_alias(request_uri());
$albumURL .= (strpos($albumURL, '?') ? '&' : '?') . 'album='.$username.'/'.$name;
$thumbURL = (string) $album->media_group->media_thumbnail['url'];
$thumbW = (string) $album->media_group->media_thumbnail['width'];
$thumbH = (string) $album->media_group->media_thumbnail['height'];
$output .= "<a href='$albumURL'><img src='$thumbURL' height='$thumbH' width='$thumbW' alt='".str_replace("'", "'", $title)."' class='kpg-thumb $class' /></a>";
$output .= '<div class="kpg-title"><a href="'.$albumURL.'">'.$title.($settings['kpicasa_album_list']['extra']['number_of_pictures'] ? ' ('.$nbPhotos.')' : '').'</a></div>';
if ($settings['kpicasa_album_list']['extra']['summary']) {
$summary = htmlspecialchars( (string) $album->summary );
$output .= "<div class='kpg-summary'>$summary</div>";
}
if ($settings['kpicasa_album_list']['extra']['location']) {
$location = htmlspecialchars( (string) $album->gphoto_location );
$output .= "<div class='kpg-location'>$location</div>";
}
if ($settings['kpicasa_album_list']['extra']['published_date']) {
$published = htmlspecialchars( date('Y-m-d', strtotime( $album->published ))); // that way it keeps the timezone
$output .= "<div class='kpg-published'>$published</div>";
}
/*
if ( $this->config['albumSummary'] == true && strlen($summary) )
{
print "<div class='kpg-summary'>$summary</div>";
}
if ( $this->config['albumLocation'] == true && strlen($location) )
{
print "<div class='kpg-location'>$location</div>";
}
if ( $this->config['albumPublished'] == true )
{
print "<div class='kpg-published'>$published</div>";
}
if ( $this->config['albumNbPhoto'] == 1 )
{
print '<div class="kpg-nbPhotos">'.sprintf(__ngettext('%d photo', '%d photos', $nbPhotos, 'kpicasa_gallery'), $nbPhotos).'</div>';
}
*/
$j++;
$i++;
$output .= '</div>'; // End div class="kpg-album".
}
$output .= '</div>'; // End div class="kpg-album-list".
return $output;
}
function _kpicasa_show_album($username, $album) {
//----------------------------------------
// Get the XML
//----------------------------------------
$url = 'http://picasaweb.google.com/data/feed/api/user/'.urlencode($username).'/album/'.urlencode($album).'?kind=photo';
/*
if ( strlen($authKey) > 0 ) {
$url .= '&authkey='.$authKey;
}
*/
$data = @file_get_contents( $url );
if (!$data) {
drupal_set_message(t('Unable to get Picasa album for this user %username', array('%username' => $username)), 'error');
return '';
}
$data = str_replace('gphoto:', 'gphoto_', $data);
$data = str_replace('media:', 'media_', $data);
$xml = @simplexml_load_string($data);
if ( $xml === false ) {
drupal_set_message(t('Unable to get Picasa album for this user %username', array('%username' => $username)), 'error');
return '';
}
//----------------------------------------
// TODO Display "back" link
//----------------------------------------
/*
if ( !$direct ) {
$backURL = remove_query_arg('album');
$backURL = remove_query_arg('kpap', $backURL);
print "<div id='kpg-backLink'><a href='$backURL'>« ".__('Back to album list', 'kpicasa_gallery').'</a></div>';
}
*/
//----------------------------------------
// Display album information
//----------------------------------------
$albumTitle = htmlspecialchars( (string) $xml->title );
$albumSummary = htmlspecialchars( (string) $xml->subtitle );
$albumLocation = htmlspecialchars( (string) $xml->gphoto_location );
//$albumPublished = wp_specialchars( date($this->config['dateFormat'], strtotime( $xml->published ))); // that way it keeps the timezone
$albumNbPhotos = (string) $xml->gphoto_numphotos;
$albumSlideshow = (string) $xml->link[2]['href'];
$output = '';
$output .= '<div id="kpg-album-description">';
$output .= "<div id='kpg-title'>$albumTitle</div>";
$output .= "<div id='kpg-summary'>$albumSummary</div>";
$output .= "<div id='kpg-location'>$albumLocation</div>";
//$output .= "<div id='kpg-published'>$albumPublished</div>";
$output .= '<div id="kpg-nbPhotos">'.$albumNbPhotos.'</div>';
//$output .= "<div id='kpg-slideshow'><a href='$albumSlideshow'>".__('Slideshow', 'kpicasa_gallery')."</a></div>";
$output .= '</div>';
$thumbIndex = 1;
//----------------------------------------
// Loop through the pictures
//----------------------------------------
$output .= '<div class="kpg-pictures">';
$i = 0; $j = 0;
foreach( $xml->entry as $photo ) {
$isVideo = (string) $photo->media_group->media_content[1]['medium'] == 'video' ? true : false;
$summary = htmlspecialchars((string) $photo->summary );
$thumbURL = (string) $photo->media_group->media_thumbnail[$thumbIndex]['url'];
$thumbW = (string) $photo->media_group->media_thumbnail[$thumbIndex]['width'];
$thumbH = (string) $photo->media_group->media_thumbnail[$thumbIndex]['height'];
if ( $isVideo == true ) {
$output .= '<div class="kpg-item kpg-video">';
$output .= 'video not yet supported';
$output .= '</div>';
}
else {
//$fullURL = (string) $photo->link[1]['href'];
$fullURL = (string) $photo->media_group->media_thumbnail[1]['url'];
$fullURL = str_replace('/s144/', '/s800/', $fullURL);
$output .= '<div class="kpg-item kpg-picture">';
$output .= "<a href='$fullURL' rel='lightbox'><img src='$thumbURL' height='$thumbH' width='$thumbW' alt='' class='kpg-thumb' /></a>";
$output .= '</div>';
}
}
$output .= '</div>';
$output .= '<div style="clear: both;" />';
return $output;
}