| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Flickrstickr module
|
| 7 |
* Requirements: image module
|
| 8 |
* Suggested: tinymce module
|
| 9 |
*
|
| 10 |
* Module written by: Elek Marton
|
| 11 |
* Module maintainer: Aron Novak < aaron at szentimre dot hu >
|
| 12 |
*/
|
| 13 |
|
| 14 |
/**
|
| 15 |
* Search for unique flickr tag for replacement
|
| 16 |
*/
|
| 17 |
define('FLICKRSTICKR_TAGPREG_FLICKRTAG', '/\[flickr:([^\/]+)\/([^\/]+)\/([\w]+)\|(left|center|right)(?:\|(\d*))?(?:\|(\d*))?\]/');
|
| 18 |
/**
|
| 19 |
* Search for unique img tag with appendix for replacement
|
| 20 |
*/
|
| 21 |
define('FLICKRSTICKR_TAGPREG_APPENDIX', '/<img [^<>]*src="http:\/\/static.flickr.com\/([0-9]+)\/([0-9]+)_(.{10}).*\#(left|center|right)_([0-9]*)_([0-9]*)"[^<>]*>/');
|
| 22 |
|
| 23 |
/**
|
| 24 |
* If run PHP4, need to define file_put_contents. In PHP5 this is native function
|
| 25 |
*
|
| 26 |
* @param string $filename
|
| 27 |
* @param mixed $data
|
| 28 |
* @return integer Written bytes
|
| 29 |
*/
|
| 30 |
function flickrstickr_file_put_contents($filename, $data) {
|
| 31 |
$mode = 'w';
|
| 32 |
$file = @fopen($filename, $mode);
|
| 33 |
if ($file === FALSE) {
|
| 34 |
return 0;
|
| 35 |
}
|
| 36 |
else {
|
| 37 |
$bytes_written = fwrite($file, $data);
|
| 38 |
fclose($file);
|
| 39 |
return $bytes_written;
|
| 40 |
}
|
| 41 |
}
|
| 42 |
|
| 43 |
/**
|
| 44 |
* Determine wheter this site is drupal 4.7 or not
|
| 45 |
*
|
| 46 |
* @return bool True if version is 4.7
|
| 47 |
*/
|
| 48 |
function flickrstickr_isdrupal47() {
|
| 49 |
static $new_API;
|
| 50 |
if (!isset ($new_API)) {
|
| 51 |
$new_API = function_exists('drupal_get_form');
|
| 52 |
}
|
| 53 |
return $new_API;
|
| 54 |
}
|
| 55 |
|
| 56 |
/**
|
| 57 |
* implements hook_menu
|
| 58 |
*/
|
| 59 |
function flickrstickr_menu($may_cache) {
|
| 60 |
$items = array();
|
| 61 |
if (!$may_cache) {
|
| 62 |
$items[] = array(
|
| 63 |
'path' => 'flickrstickr/xml/user',
|
| 64 |
'title' => t('xml proxy'),
|
| 65 |
'callback' => 'flickrstickr_proxy_user',
|
| 66 |
'access' => TRUE,
|
| 67 |
'type' => MENU_CALLBACK,
|
| 68 |
'weight' => 5,
|
| 69 |
);
|
| 70 |
$items[] = array(
|
| 71 |
'path' => 'flickrstickr/xml/images',
|
| 72 |
'title' => t('xml proxy'),
|
| 73 |
'callback' => 'flickrstickr_proxy_images',
|
| 74 |
'access' => TRUE,
|
| 75 |
'type' => MENU_CALLBACK,
|
| 76 |
'weight' => 5,
|
| 77 |
);
|
| 78 |
$items[] = array(
|
| 79 |
'path' => 'admin/settings/flickrstickr',
|
| 80 |
'title' => 'FlickrStickr',
|
| 81 |
'callback' => 'drupal_get_form',
|
| 82 |
'callback arguments' => array('flickrstickr_settings'),
|
| 83 |
'access' => user_access('administer site configuration'),
|
| 84 |
);
|
| 85 |
}
|
| 86 |
return $items;
|
| 87 |
}
|
| 88 |
|
| 89 |
/**
|
| 90 |
* Public ajax proxy for user search
|
| 91 |
* Bcause the javascript restriction
|
| 92 |
*/
|
| 93 |
function flickrstickr_proxy_user() {
|
| 94 |
header("Content-type: text/xml; charset=UTF-8");
|
| 95 |
$apikey = flickrstickr_getapikey();
|
| 96 |
$content = flickrstrick_get_url("http://www.flickr.com/services/rest/?method=flickr.people.findByUsername&api_key=". $apikey ."&username=". urlencode(arg(3)), "r");
|
| 97 |
echo $content;
|
| 98 |
}
|
| 99 |
|
| 100 |
/**
|
| 101 |
* public ajax proxy for tag search
|
| 102 |
* Because the javascript restriction
|
| 103 |
*/
|
| 104 |
function flickrstickr_proxy_images() {
|
| 105 |
global $user;
|
| 106 |
$flickrstickr = flickrstickr_getuserparam();
|
| 107 |
header("Content-type: text/xml; charset=UTF-8");
|
| 108 |
$apikey = flickrstickr_getapikey();
|
| 109 |
$userid = urlencode(arg(3));
|
| 110 |
$page = urlencode(arg(5));
|
| 111 |
if ($userid == "!recent!") {
|
| 112 |
$query = "http://www.flickr.com/services/rest/?method=flickr.photos.getRecent&api_key=". $apikey ."&per_page=". $flickrstickr['imgno'] ."&page=". $page;
|
| 113 |
}
|
| 114 |
else {
|
| 115 |
$query = "http://www.flickr.com/services/rest/?method=flickr.photos.search&api_key=". $apikey ."&per_page=". $flickrstickr['imgno'] ."&page=". $page;
|
| 116 |
if (arg(3) != "all") {
|
| 117 |
$query .= "&user_id=". $userid;
|
| 118 |
}
|
| 119 |
if (arg(4) != null) {
|
| 120 |
$query .= "&tags=". urlencode(arg(4));
|
| 121 |
}
|
| 122 |
}
|
| 123 |
echo flickrstrick_get_url($query);
|
| 124 |
}
|
| 125 |
|
| 126 |
/**
|
| 127 |
* implements hook_user
|
| 128 |
*/
|
| 129 |
function flickrstickr_user($type, & $edit, & $user, $category= NULL) {
|
| 130 |
if (!user_access('flickrstickr access', $user)) {
|
| 131 |
return array();
|
| 132 |
}
|
| 133 |
$flickrstickr = flickrstickr_getuserparam();
|
| 134 |
if ($type == 'form' && $category == 'account') {
|
| 135 |
$options_link = array(
|
| 136 |
'none' => 'None',
|
| 137 |
'flickr_img' => 'Flickr image page',
|
| 138 |
);
|
| 139 |
$options_sizes = array(
|
| 140 |
'100' => 'Thumbnail',
|
| 141 |
'240' => 'Small',
|
| 142 |
'500' => 'Medium',
|
| 143 |
'1000' => 'Large',
|
| 144 |
);
|
| 145 |
$options_format = array(
|
| 146 |
'filter' => 'Filter tag',
|
| 147 |
'html' => 'plain html',
|
| 148 |
);
|
| 149 |
$form = array();
|
| 150 |
$form['flickrstickr'] = array(
|
| 151 |
'#type' => 'fieldset',
|
| 152 |
'#title' => t('Flickr image sticker settings'),
|
| 153 |
);
|
| 154 |
$form['flickrstickr']['flickrstickr_imgno'] = array(
|
| 155 |
'#type' => 'textfield',
|
| 156 |
'#title' => t('Flickr image sticker settings'),
|
| 157 |
'#default_value' => $flickrstickr['imgno'],
|
| 158 |
'#maxlength' => 5,
|
| 159 |
'#size' => 5,
|
| 160 |
'#description' => t('Number of recent photos to retrieve.'),
|
| 161 |
'#required' => TRUE,
|
| 162 |
);
|
| 163 |
$form['flickrstickr']['flickrstickr_username'] = array(
|
| 164 |
'#type' => 'textfield',
|
| 165 |
'#title' => t('Default flickr username'),
|
| 166 |
'#default_value' => $flickrstickr['username'],
|
| 167 |
'#maxlength' => 15,
|
| 168 |
'#size' => 15,
|
| 169 |
);
|
| 170 |
$form['flickrstickr']['flickrstickr_link'] = array(
|
| 171 |
'#type' => 'select',
|
| 172 |
'#title' => t('Photo Link Destination'),
|
| 173 |
'#default_value' => $flickrstickr['link'],
|
| 174 |
'#options' => $options_link,
|
| 175 |
'#description' => t('Where the photos inserted into your post should link to.'),
|
| 176 |
'#required' => TRUE,
|
| 177 |
);
|
| 178 |
return $form;
|
| 179 |
}
|
| 180 |
}
|
| 181 |
|
| 182 |
/**
|
| 183 |
* imlements hook_form_alter
|
| 184 |
*/
|
| 185 |
function flickrstickr_form_alter($form_id, & $form) {
|
| 186 |
if (preg_match('/_node_form$/', $form_id) && user_access('flickrstickr access')) {
|
| 187 |
$form['flickrstickr'] = array(
|
| 188 |
'#type' => 'fieldset',
|
| 189 |
'#title' => t('Flickr image insert'),
|
| 190 |
'#collapsible' => TRUE,
|
| 191 |
'#collapsed' => TRUE,
|
| 192 |
'#weight' => 20,
|
| 193 |
);
|
| 194 |
$form['flickrstickr']['images'] = array(
|
| 195 |
'#type' => 'markup',
|
| 196 |
'#value' => _flickrstickr_htmlpart(),
|
| 197 |
'#weight' => -1,
|
| 198 |
);
|
| 199 |
}
|
| 200 |
}
|
| 201 |
|
| 202 |
/**
|
| 203 |
* Create the flickr images selector
|
| 204 |
*/
|
| 205 |
function _flickrstickr_htmlpart() {
|
| 206 |
global $user;
|
| 207 |
global $base_url;
|
| 208 |
$flickrstickr = flickrstickr_getuserparam();
|
| 209 |
$base_path = base_path();
|
| 210 |
drupal_set_html_head('<link rel="stylesheet" type="text/css" href="'. $base_path. drupal_get_path('module', 'flickrstickr').'/flickrstickr.css" media="screen" />');
|
| 211 |
drupal_set_html_head('<script src="'. $base_path. drupal_get_path('module', 'flickrstickr'). '/flickrstickr.js"></script>');
|
| 212 |
// This line is needed because of floating image selector
|
| 213 |
drupal_set_html_head('<!-- compliance patch for microsoft browsers --><!--[if lt IE 7]><script src="'. $base_path. drupal_get_path('module', 'flickrstickr'). '/ie7/ie7-standard-p.js" type="text/javascript"></script><![endif]-->');
|
| 214 |
$ret = '<input type="button" value="' . t('Hide the image selector') . '" onclick="toggleSelector()" id="flickr_togglebutton" />';
|
| 215 |
// Nasty hack to prevent IE displaying dock button that simply don't wok
|
| 216 |
$agent = $_SERVER['HTTP_USER_AGENT'];
|
| 217 |
if (strpos($agent, 'MSIE') == FALSE || strpos($agent, 'Opera') != FALSE) {
|
| 218 |
// Maybe IE7 will support dockbutton functionality. Should check later!
|
| 219 |
$ret .= '<input type="button" value="' . t('Dock the image selector') . '" onclick="dockSelector()" id="flickr_dockbutton" />';
|
| 220 |
}
|
| 221 |
$ret .= '<div id="flickrstickr_float_selector"><div id="flickrstickr">';
|
| 222 |
$ret .= '<table id="flickrstickr_table>';
|
| 223 |
$ret .= '<tr id="flickrstickr_tr">';
|
| 224 |
$ret .= '<td><input type="button" value="<" onclick="prevPage()" id="navprev" />';
|
| 225 |
for ($i = 0; $i < $flickrstickr['imgno']; $i++) {
|
| 226 |
$ret .= '<td><img id="flickrstickrphoto'. $i .'" onClick="insertText('. $i .')"></td>';
|
| 227 |
}
|
| 228 |
$ret .= '<td><input type="button" value=">" onclick="nextPage()" id="navnext" />';
|
| 229 |
$ret .= '</tr>';
|
| 230 |
$ret .= '</table>';
|
| 231 |
$ret .= '</div>';
|
| 232 |
$ret .= '<p id="flickrsettings">';
|
| 233 |
$ret .= t('User') .': <input id="flickrstickr_user" type="text" name="user" value="'. $flickrstickr['username'] .'" onkeypress="return handleEnter(event)" />';
|
| 234 |
$ret .= t('Tag') .': <input id="flickrstickr_tag" type="text" name="tag" onkeypress="return handleEnter(event)" />';
|
| 235 |
// If you want to add a new size, just add a new element to this array. (but this should be put into settings?)
|
| 236 |
$options_size = array('|200' => t('Default'),
|
| 237 |
'|100' => t('Thumbnail'),
|
| 238 |
'|240' => T('Small'),
|
| 239 |
'|500' => t('Medium'),
|
| 240 |
'|1000' => t('Large'));
|
| 241 |
$ret .= t('Insert size') . ': <select id="flickrstickr_size" name="size" onchange="updateImages()" />';
|
| 242 |
foreach ($options_size as $key => $value) {
|
| 243 |
$ret .= '<option value="'. $key .'">'. $value .'</option>';
|
| 244 |
}
|
| 245 |
$ret .= '</select>';
|
| 246 |
$ret .= t('Popup size') . ': <select id="flickrstickr_size_popup" name="size" onchange="updateImages()" />';
|
| 247 |
foreach ($options_size as $key => $value) {
|
| 248 |
$ret .= '<option value="'. $key .'">'. $value .'</option>';
|
| 249 |
}
|
| 250 |
$ret .= '</select>';
|
| 251 |
$ret .= t('Align') . ': <select id="flickrstickr_align" name="align" onchange="updateImages()" />';
|
| 252 |
$ret .= '<option value="left">' . t('left') . '</option>';
|
| 253 |
$ret .= '<option value="center">' . t('center') . '</option>';
|
| 254 |
$ret .= '<option value="right">' . t('right') . '</option>';
|
| 255 |
$ret .= '</select>';
|
| 256 |
$ret .= '<input type="button" value="'. t('Search') .'" onclick="loadImages()">';
|
| 257 |
$ret .= '<span id="flickrstickr_loading"></span></p></div><br />';
|
| 258 |
$ret .= "<script>"."var photosRequest = getHTTPObject();\n"."var userRequest = getHTTPObject();\n"."var userids = new Array();\n"."var images = new Array();\n";
|
| 259 |
$ret .= "function getMaxImageNo(){\n". "return ". $flickrstickr['imgno'] .";}\n";
|
| 260 |
$ret .= "function getBaseUrl(){\n". "return \"". $base_url ."\";}\n";
|
| 261 |
$ret .= "for (i=0;i<". $flickrstickr['imgno'] .";i++) {"."images[i]= new Object();}";
|
| 262 |
$ret .= 'function insertText(id){';
|
| 263 |
$nodetype = arg(2);
|
| 264 |
$cck = strpos($nodetype, 'content-');
|
| 265 |
if ($cck === FALSE) {
|
| 266 |
$ret .= 'textarea = document.getElementById("edit-body");';
|
| 267 |
}
|
| 268 |
else {
|
| 269 |
$ret .= 'textarea = document.getElementById("edit-field_body-0-value");';
|
| 270 |
}
|
| 271 |
$ret .= 'var size=document.getElementById("flickrstickr_size").value;';
|
| 272 |
$inserted = "";
|
| 273 |
if ($flickrstickr['format'] == 'filter') {
|
| 274 |
$inserted = '[flickr:"+images[id].server+"/"+images[id].id+"/"+images[id].secret+"|"+document.getElementById(\'flickrstickr_align\').value+document.getElementById(\'flickrstickr_size\').value+document.getElementById(\'flickrstickr_size_popup\').value+"]';
|
| 275 |
}
|
| 276 |
else {
|
| 277 |
$inserted = '<img class=\"flickrstickr_image\" src=\"http://static.flickr.com/"+images[id].server+"/"+images[id].id+"_"+images[id].secret+".jpg\"/>';
|
| 278 |
}
|
| 279 |
if ($flickrstickr['link'] != 'none') {
|
| 280 |
$inserted = '<a href=\"http://www.flickr.com/photos/"+images[id].owner+"/"+images[id].id+"\">'. $inserted .'</a>';
|
| 281 |
}
|
| 282 |
$ret .= 'insertAtCursor(textarea, "<div style=\"text-align: "+document.getElementById(\'flickrstickr_align\').value+"\">'. $inserted .'</div>");';
|
| 283 |
$ret .= '}';
|
| 284 |
$ret .= '</script>';
|
| 285 |
return $ret;
|
| 286 |
}
|
| 287 |
|
| 288 |
/**
|
| 289 |
* get user paramters or defaults if not exists
|
| 290 |
*/
|
| 291 |
function flickrstickr_getuserparam() {
|
| 292 |
global $user;
|
| 293 |
$flickrstickr['link'] = isset($user->flickrstickr_link) ? $user->flickrstickr_link : "none";
|
| 294 |
$flickrstickr['imgno'] = isset($user->flickrstickr_imgno) ? $user->flickrstickr_imgno : 10;
|
| 295 |
$flickrstickr['size'] = isset($user->flickrstickr_size) ? $user->flickrstickr_size : "100";
|
| 296 |
$flickrstickr['format'] = isset($user->flickrstickr_format) ? $user->flickrstickr_format : "filter";
|
| 297 |
$flickrstickr['username'] = isset($user->flickrstickr_username) ? $user->flickrstickr_username : "";
|
| 298 |
return $flickrstickr;
|
| 299 |
}
|
| 300 |
|
| 301 |
/**
|
| 302 |
* get current flickr ap key
|
| 303 |
* see: http://www.flickr.com/services/api/key.gne
|
| 304 |
*/
|
| 305 |
function flickrstickr_getapikey() {
|
| 306 |
return variable_get('flickrstickr_flickrkey', '');
|
| 307 |
}
|
| 308 |
|
| 309 |
/**
|
| 310 |
* implements hook_help
|
| 311 |
*/
|
| 312 |
function flickrstickr_help($section) {
|
| 313 |
switch ($section) {
|
| 314 |
case 'admin/modules#description':
|
| 315 |
$output = t("Enable flickr image inserter in your node forms.");
|
| 316 |
break;
|
| 317 |
}
|
| 318 |
return $output;
|
| 319 |
}
|
| 320 |
|
| 321 |
/*
|
| 322 |
global $user;
|
| 323 |
$node = array('uid' => $user->uid, 'name' => $user->name, 'type' => 'image');
|
| 324 |
*/
|
| 325 |
|
| 326 |
/**
|
| 327 |
* implements hook_filter
|
| 328 |
*/
|
| 329 |
function flickrstickr_filter($op, $delta = 0, $format = -1, $text = '') {
|
| 330 |
switch ($op) {
|
| 331 |
case 'list':
|
| 332 |
return array(0 => t('Flickrstickr filter'));
|
| 333 |
case 'no cache':
|
| 334 |
return true;
|
| 335 |
case 'description':
|
| 336 |
return t('Allows users to insert flickr images');
|
| 337 |
case 'prepare':
|
| 338 |
return $text;
|
| 339 |
case 'process':
|
| 340 |
// The order is important! The APPENDIX filter must be before the FLICKRTAG filter!
|
| 341 |
$text = preg_replace_callback(FLICKRSTICKR_TAGPREG_APPENDIX, 'flickrstickr_processappendix', str_replace("<img", "\n<img", $text));
|
| 342 |
$text = preg_replace_callback(FLICKRSTICKR_TAGPREG_FLICKRTAG, 'flickrstickr_processflickrtag', $text);
|
| 343 |
return $text;
|
| 344 |
default:
|
| 345 |
return $text;
|
| 346 |
}
|
| 347 |
}
|
| 348 |
|
| 349 |
/**
|
| 350 |
* filter callback function for flicktrag solution
|
| 351 |
*
|
| 352 |
*/
|
| 353 |
function flickrstickr_processflickrtag($matches) {
|
| 354 |
$params = flickrstickr_getuserparam();
|
| 355 |
if (!isset($matches[5])) {
|
| 356 |
$size = $params['size'];
|
| 357 |
$popup_size = $params['size'];
|
| 358 |
}
|
| 359 |
else {
|
| 360 |
$size = $matches[5];
|
| 361 |
$popup_size = isset($matches[6]) ? $matches[6] : $size;
|
| 362 |
}
|
| 363 |
|
| 364 |
$filepath = "images/". $matches[1] ."_". $matches[2] ."_". $matches[3];
|
| 365 |
$result = db_query("SELECT nid, filepath FROM {files} WHERE filepath LIKE '%s'", '%' .$filepath. '%');
|
| 366 |
|
| 367 |
|
| 368 |
// In any case link the original image
|
| 369 |
$big_path = $filepath;
|
| 370 |
$image = db_fetch_object($result);
|
| 371 |
$filepath = $image->filepath;
|
| 372 |
$orient = 0;
|
| 373 |
if ($image) {
|
| 374 |
$nid = $image->nid;
|
| 375 |
}
|
| 376 |
$url = flickrstickr_getlocalrurl($nid, $size, $orient);
|
| 377 |
$href = url("node/". $nid);
|
| 378 |
// Know the nid then gather the possible sizes - choose the most closest to requested.
|
| 379 |
$this_file = "images/". $matches[1] ."_". $matches[2] ."_". $matches[3];
|
| 380 |
$result = db_query("SELECT filepath, filename FROM {files} WHERE nid='%d' AND filepath LIKE '%s%'", $nid, $this_file);
|
| 381 |
$i = 0;
|
| 382 |
while ($img = db_fetch_array($result)) {
|
| 383 |
$paths[$i] = 'files/' . $img['filepath'];
|
| 384 |
$img_details = image_get_info($paths[$i]);
|
| 385 |
$avail_sizes[$i] = $img_details['width'];
|
| 386 |
$diffs[$i] = abs($size - $avail_sizes[$i]);
|
| 387 |
$diffs_pop[$i] = abs($popup_size - $avail_sizes[$i]);
|
| 388 |
++$i;
|
| 389 |
}
|
| 390 |
if (!is_array($diffs) || !is_array($diffs_pop)) {
|
| 391 |
drupal_set_message(t('Some inserted images not found. Possibly manually deleted or other internal error occured.'), 'error');
|
| 392 |
return '';
|
| 393 |
}
|
| 394 |
// Search the absolute value minimum of $diffs
|
| 395 |
$num_imgs = sizeof($diffs);
|
| 396 |
$val = reset($diffs);
|
| 397 |
$index = 0;
|
| 398 |
$val_pop = reset($diffs_pop);
|
| 399 |
$index_pop = 0;
|
| 400 |
for ($i = 0; $i < $num_imgs; $i++) {
|
| 401 |
if ($diffs[$i] < $val) {
|
| 402 |
$val = $diffs[$i];
|
| 403 |
$index = $i;
|
| 404 |
}
|
| 405 |
if ($diffs_pop[$i] < $val_pop) {
|
| 406 |
$val_pop = $diffs_pop[$i];
|
| 407 |
$index_pop = $i;
|
| 408 |
}
|
| 409 |
}
|
| 410 |
$filepath = $paths[$index];
|
| 411 |
$filepath_pop = $paths[$index_pop];
|
| 412 |
|
| 413 |
// todo: improve that one to find proper image sizes
|
| 414 |
// change alex
|
| 415 |
$local_url = base_path() . $filepath; // User defined inline size
|
| 416 |
$pop_local_url = base_path() . $filepath_pop; // User defined popup size
|
| 417 |
$big_local_url = base_path() . $big_path; // The largest what we have
|
| 418 |
|
| 419 |
if ($orient) {
|
| 420 |
$size_param = 'height='. $size;
|
| 421 |
}
|
| 422 |
else {
|
| 423 |
$size_param = 'width='. $size;
|
| 424 |
}
|
| 425 |
// change alex
|
| 426 |
// $img = '<img '.$size_param.' class="flickrsitckr_image" align="'.$matches[4].'" src="'.$url.'"/>';
|
| 427 |
$img = '<img '. $size_param .' class="flickrstickr_image" align="'. $matches[4] .'" src="'. $local_url .'" />';
|
| 428 |
$possible_sizes = array('_s', '_t', '_m');
|
| 429 |
if ($href != '') {
|
| 430 |
$img = '<a rel="lightbox" href="'. $pop_local_url .'">'. $img .'</a>';
|
| 431 |
}
|
| 432 |
$img = '<div style="text-align: '. $matches[4] .';"><a rel="lightbox" href="' . $pop_local_url .'">'. $img .'</a></div>';
|
| 433 |
return $img;
|
| 434 |
}
|
| 435 |
|
| 436 |
/**
|
| 437 |
* Filter callback function for url appendix solution
|
| 438 |
*
|
| 439 |
*/
|
| 440 |
function flickrstickr_processappendix($matches) {
|
| 441 |
// If user has specifiy a size trough tinymce we should handle it
|
| 442 |
preg_match('/width="([0-9]*)"/', $matches[0], $size);
|
| 443 |
if (isset($size[1])) {
|
| 444 |
$matches[5] = $size[1];
|
| 445 |
}
|
| 446 |
$flickrtag = '[flickr:' . $matches[1] . '/' . $matches[2] . '/' . $matches[3]
|
| 447 |
. '|' . $matches[4] . '|' . $matches[5] . '|' . $matches[6] . ']';
|
| 448 |
return $flickrtag;
|
| 449 |
}
|
| 450 |
|
| 451 |
/**
|
| 452 |
* Build flickr static picture URL
|
| 453 |
*
|
| 454 |
* @param integer $server
|
| 455 |
* @param integer $id
|
| 456 |
* @param string $secret
|
| 457 |
* @param integer $size
|
| 458 |
* @param integer $orient
|
| 459 |
* @return string The flickrstickr URL
|
| 460 |
*/
|
| 461 |
function flickrstickr_getflickrurl($server, $id, $secret, $size, &$orient) {
|
| 462 |
if ($size != 'o') {
|
| 463 |
$flickrsizes = array('t' => 100, 'm' => '240', '-' => '500', 'b' => '1024');
|
| 464 |
if (!is_numeric($size)) {
|
| 465 |
$size = $flickrsizes[$size];
|
| 466 |
}
|
| 467 |
$sizes = flickrstickr_getflickrsizes($id);
|
| 468 |
$best_size = 100000;
|
| 469 |
$best_file = null;
|
| 470 |
foreach ($sizes as $key=>$sizes) {
|
| 471 |
$longest = ($sizes['width'] > $sizes['height'] ? $sizes['width'] : $sizes['height']);
|
| 472 |
if ($longest >= $size && $longest < $best_size) {
|
| 473 |
$best_file = $sizes['source'];
|
| 474 |
$best_size = $longest;
|
| 475 |
}
|
| 476 |
}
|
| 477 |
if ($sizes['width'] > $sizes['height']) {
|
| 478 |
$orient = 0;
|
| 479 |
}
|
| 480 |
else {
|
| 481 |
$orient = 1;
|
| 482 |
}
|
| 483 |
return $best_file;
|
| 484 |
}
|
| 485 |
else {
|
| 486 |
return 'http://static.flickr.com/'.$server."/".$id."_".$secret.'_o.jpg';
|
| 487 |
}
|
| 488 |
}
|
| 489 |
|
| 490 |
/**
|
| 491 |
* Get the largest image's path
|
| 492 |
*
|
| 493 |
* @param integer $nid
|
| 494 |
* @param integer $size
|
| 495 |
* @param integer $orient
|
| 496 |
* @return strint The path of the image
|
| 497 |
*/
|
| 498 |
function flickrstickr_getlocalrurl($nid, $size, &$orient) {
|
| 499 |
global $base_url;
|
| 500 |
$image = node_load($nid);
|
| 501 |
if (!$image) {
|
| 502 |
return '';
|
| 503 |
}
|
| 504 |
$best_file = null;
|
| 505 |
$best_size = 100000;
|
| 506 |
foreach ($image->images as $key=>$filename) {
|
| 507 |
$sizes = image_get_info(file_create_path($filename));
|
| 508 |
$longest = ($sizes['width'] > $sizes['height'] ? $sizes['width'] : $sizes['height']);
|
| 509 |
if ($longest >= $size && $longest < $best_size) {
|
| 510 |
$best_file = $filename;
|
| 511 |
$best_size = $longest;
|
| 512 |
}
|
| 513 |
}
|
| 514 |
|
| 515 |
if ($best_file == null) {
|
| 516 |
$best_file = $image->images['_original'];
|
| 517 |
}
|
| 518 |
$sizes = image_get_info(file_create_path($best_file));
|
| 519 |
if ($sizes['width'] > $sizes['height']) {
|
| 520 |
$orient = 0;
|
| 521 |
}
|
| 522 |
else {
|
| 523 |
$orient = 1;
|
| 524 |
}
|
| 525 |
return file_create_path($best_file);
|
| 526 |
}
|
| 527 |
|
| 528 |
/**
|
| 529 |
* Create the proper image node for the actual flicktrag
|
| 530 |
*
|
| 531 |
* @param array $matches The actual flickrtag's information
|
| 532 |
*/
|
| 533 |
function flickrstickr_saveimages($matches) {
|
| 534 |
// Check if image module is available
|
| 535 |
if (!function_exists('_image_build_derivatives')) {
|
| 536 |
drupal_set_message(t('Flickrstickr requires image module. Please install or enable it!'), 'error');
|
| 537 |
return;
|
| 538 |
}
|
| 539 |
|
| 540 |
global $user;
|
| 541 |
$image_info = flickrstickr_getflickrimageinfo($matches[2], $matches[3]);
|
| 542 |
|
| 543 |
$orient = 0;
|
| 544 |
// File extension detection later!
|
| 545 |
$path = "files/images/". $matches[1] ."_". $matches[2] ."_". $matches[3];
|
| 546 |
// think of leaving out the file_exists check
|
| 547 |
if (file_exists($path. ".jpg") || file_exists($path. ".png") || file_exists($path. ".gif")) {
|
| 548 |
//check that the node still exist
|
| 549 |
$filepath = "images/". $matches[1] ."_". $matches[2] ."_". $matches[3];
|
| 550 |
$result = db_query('SELECT nid FROM {files} WHERE filepath LIKE "%%%s%"', $filepath);
|
| 551 |
$image = db_fetch_object($result);
|
| 552 |
if (!is_null($image)) {
|
| 553 |
return;
|
| 554 |
}
|
| 555 |
}
|
| 556 |
// change alex - this shows me wether a flickr image i added was already in the directory
|
| 557 |
drupal_set_message(t("Creating local copy of flickr picture") ."<br />". $path);
|
| 558 |
|
| 559 |
$sizes = flickrstickr_getflickrsizes($matches[2]);
|
| 560 |
$max = 0;
|
| 561 |
for ($i = 0; $i < sizeof($sizes); $i++) {
|
| 562 |
$longest = ($sizes[$i]['width'] > $sizes[$i]['height'] ? $sizes[$i]['width'] : $sizes[$i]['height']);
|
| 563 |
$longest_max = ($sizes[$max]['width'] > $sizes[$max]['height'] ? $sizes[$max]['width'] : $sizes[$max]['height']);
|
| 564 |
if ($longest>$longest_max) {
|
| 565 |
$max = $i;
|
| 566 |
}
|
| 567 |
}
|
| 568 |
|
| 569 |
$url = $sizes[$max]['source'];
|
| 570 |
// Nasty thing: the last 3 character of the url, but the flickr API don't send the originaltype field reliably
|
| 571 |
$extension = substr($url, strlen($url) - 3, 3);
|
| 572 |
$path .= '.'. $extension;
|
| 573 |
$content = flickrstrick_get_url($url);
|
| 574 |
flickrstickr_file_put_contents($path, $content);
|
| 575 |
|
| 576 |
$default_nid = variable_get('flickrstickr_nodetemplate', '');
|
| 577 |
if ($default_nid) {
|
| 578 |
$node = node_load($default_nid);
|
| 579 |
unset($node->nid);
|
| 580 |
}
|
| 581 |
else {
|
| 582 |
$node =& new stdClass();
|
| 583 |
$node->promote = 0;
|
| 584 |
$node->status = 1;
|
| 585 |
$node->sticky = 0;
|
| 586 |
$node->revision = 0;
|
| 587 |
$node->moderate = 0;
|
| 588 |
$node->comment = 0;
|
| 589 |
|
| 590 |
}
|
| 591 |
global $user;
|
| 592 |
$node->type = 'image';
|
| 593 |
$node->uid = $user->uid;
|
| 594 |
$node->title = $image_info['title'];
|
| 595 |
$node->body = $image_info['description'];
|
| 596 |
$node->teaser = $node->body;
|
| 597 |
$node->created = time();
|
| 598 |
$node->changed = time();
|
| 599 |
$node->images = array();
|
| 600 |
$node->images['_original'] = $path;
|
| 601 |
|
| 602 |
$node = node_submit($node);
|
| 603 |
|
| 604 |
if ($node->validated) {
|
| 605 |
node_save($node);
|
| 606 |
_image_build_derivatives($node);
|
| 607 |
|
| 608 |
// Create exact flickr size
|
| 609 |
|
| 610 |
$source = file_create_path($node->images['_original']);
|
| 611 |
$destination = _image_filename(basename($source), 'exact_flickr_size', $temp);
|
| 612 |
if (!image_scale($source, file_create_path($destination), $matches[5], $matches[5])) {
|
| 613 |
drupal_set_message(t('Unable to create %label image', array('%label' => $size['label'])), 'error');
|
| 614 |
}
|
| 615 |
else {
|
| 616 |
$node->images[$size['label']] = $destination;
|
| 617 |
if (!$temp) {
|
| 618 |
_image_insert($node, 'exact_flickr_size', file_create_path($destination));
|
| 619 |
}
|
| 620 |
}
|
| 621 |
}
|
| 622 |
else {
|
| 623 |
drupal_set_message(t('Unable to create image node.'), 'error');
|
| 624 |
}
|
| 625 |
return '';
|
| 626 |
}
|
| 627 |
|
| 628 |
/**
|
| 629 |
* implements hook_nodeapi
|
| 630 |
* save all the images in the node
|
| 631 |
*
|
| 632 |
*/
|
| 633 |
function flickrstickr_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
|
| 634 |
static $ispreview = FALSE;
|
| 635 |
global $ispreview;
|
| 636 |
if ($op == 'validate') {
|
| 637 |
$ispreview = TRUE;
|
| 638 |
}
|
| 639 |
if (($op == "insert" || $op == 'update') && $node->type != 'image') {
|
| 640 |
$texts = array($node->field_body[0]['value'],
|
| 641 |
$node->body,
|
| 642 |
$node->field_teaser[0]['value'],
|
| 643 |
$node->field_short_teaser[0]['value']);
|
| 644 |
|
| 645 |
// Transform appendix style mark to flickrtag
|
| 646 |
for ($i = 0; $i < sizeof($texts); $i++) {
|
| 647 |
$texts[$i] = preg_replace_callback(FLICKRSTICKR_TAGPREG_APPENDIX,
|
| 648 |
'flickrstickr_processappendix',
|
| 649 |
str_replace("<img", "\n<img", $texts[$i]));
|
| 650 |
preg_replace_callback(FLICKRSTICKR_TAGPREG_FLICKRTAG,
|
| 651 |
'flickrstickr_saveimages',
|
| 652 |
$texts[$i]);
|
| 653 |
}
|
| 654 |
}
|
| 655 |
}
|
| 656 |
|
| 657 |
/**
|
| 658 |
* Get image properties from flickr site
|
| 659 |
*
|
| 660 |
* @param integer $photo_id
|
| 661 |
* @param string $secret
|
| 662 |
* @return array The image properties in an assoc. array
|
| 663 |
*/
|
| 664 |
function flickrstickr_getflickrimageinfo($photo_id, $secret) {
|
| 665 |
global $user;
|
| 666 |
$flickrstickr = flickrstickr_getuserparam();
|
| 667 |
$apikey = flickrstickr_getapikey();
|
| 668 |
$userid = urlencode(arg(3));
|
| 669 |
$query = "http://www.flickr.com/services/rest/?method=flickr.photos.getInfo&api_key=".$apikey."&photo_id=".$photo_id."&secret=".$secret;
|
| 670 |
|
| 671 |
$xml = flickrstrick_get_url($query);
|
| 672 |
|
| 673 |
|
| 674 |
$parser = xml_parser_create();
|
| 675 |
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
|
| 676 |
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
|
| 677 |
xml_parse_into_struct($parser, $xml, $values, $tags);
|
| 678 |
xml_parser_free($parser);
|
| 679 |
|
| 680 |
foreach ($tags['title'] as $key) {
|
| 681 |
if ($values[$key]['type'] != 'complete') {
|
| 682 |
continue;
|
| 683 |
}
|
| 684 |
$info['title'] = $values[$key]['value'];
|
| 685 |
break;
|
| 686 |
}
|
| 687 |
|
| 688 |
foreach ($tags['description'] as $key) {
|
| 689 |
if ($values[$key]['type'] != 'complete') {
|
| 690 |
continue;
|
| 691 |
}
|
| 692 |
$info['description'] = $values[$key]['value'];
|
| 693 |
break;
|
| 694 |
}
|
| 695 |
if ($info['description'] == NULL) {
|
| 696 |
$info['description'] = '';;
|
| 697 |
}
|
| 698 |
return $info;
|
| 699 |
}
|
| 700 |
|
| 701 |
/**
|
| 702 |
* Get available image sizes
|
| 703 |
*
|
| 704 |
* @param integer $photo_id
|
| 705 |
* @return array The sizes in pixel
|
| 706 |
*/
|
| 707 |
function flickrstickr_getflickrsizes($photo_id) {
|
| 708 |
$apikey = flickrstickr_getapikey();
|
| 709 |
$userid = urlencode(arg(3));
|
| 710 |
$query = "http://www.flickr.com/services/rest/?method=flickr.photos.getSizes&api_key=".$apikey."&photo_id=".$photo_id;
|
| 711 |
$xml = flickrstrick_get_url($query);
|
| 712 |
$sizes = array();
|
| 713 |
$parser = xml_parser_create();
|
| 714 |
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
|
| 715 |
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
|
| 716 |
xml_parse_into_struct($parser, $xml, $values, $tags);
|
| 717 |
xml_parser_free($parser);
|
| 718 |
foreach ($tags['size'] as $key) {
|
| 719 |
if ($values[$key]['type'] != 'complete') {
|
| 720 |
continue;
|
| 721 |
}
|
| 722 |
$sizes[] = $values[$key]['attributes'];
|
| 723 |
}
|
| 724 |
return $sizes;
|
| 725 |
}
|
| 726 |
|
| 727 |
/**
|
| 728 |
* implements hook_perm
|
| 729 |
*/
|
| 730 |
function flickrstickr_perm() {
|
| 731 |
return array('flickrstickr access', 'flickrstickr admin');
|
| 732 |
}
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
/**
|
| 737 |
* implements hook_settings
|
| 738 |
*/
|
| 739 |
function flickrstickr_settings() {
|
| 740 |
$form = array();
|
| 741 |
$form['settings'] = array(
|
| 742 |
'#type' => 'fieldset',
|
| 743 |
'#title' => t('FlickrStickr settings'),
|
| 744 |
);
|
| 745 |
$form['settings']['flickrstickr_flickrkey'] = array(
|
| 746 |
'#type' => 'textfield',
|
| 747 |
'#title' => t('Flickrapi key'),
|
| 748 |
'#default_value' => flickrstickr_getapikey(),
|
| 749 |
'#maxlength' => 36,
|
| 750 |
'#size' => 36,
|
| 751 |
);
|
| 752 |
return system_settings_form($form);
|
| 753 |
}
|
| 754 |
|
| 755 |
/**
|
| 756 |
* Test if the <var>$host</var> is alive
|
| 757 |
*
|
| 758 |
* @param string $host A valid network hostname
|
| 759 |
* @return bool TRUE if alive and FALSE if not.
|
| 760 |
*/
|
| 761 |
function flickrstickr_ping($host) {
|
| 762 |
$fp = fopen($host, 80);
|
| 763 |
stream_set_timeout($fp, 2);
|
| 764 |
$res = fread($fp, 2000);
|
| 765 |
$info = stream_get_meta_data($fp);
|
| 766 |
if (!$fp) {
|
| 767 |
return FALSE;
|
| 768 |
}
|
| 769 |
else {
|
| 770 |
fclose($fp);
|
| 771 |
return TRUE;
|
| 772 |
}
|
| 773 |
}
|
| 774 |
|
| 775 |
/**
|
| 776 |
* Retrieve an URL with cURL
|
| 777 |
*
|
| 778 |
* @param string $url
|
| 779 |
*/
|
| 780 |
function flickrstrick_get_url($url) {
|
| 781 |
$ch = curl_init();
|
| 782 |
curl_setopt($ch, CURLOPT_URL, $url);
|
| 783 |
curl_setopt($ch, CURLOPT_HEADER, 0);
|
| 784 |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
| 785 |
$output = curl_exec($ch);
|
| 786 |
$error = curl_error($ch);
|
| 787 |
if($error) watchdog("flickrstickr", $error);
|
| 788 |
curl_close($ch);
|
| 789 |
return $output;
|
| 790 |
}
|