Plugin Directory


Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • pixabay-images/trunk/pixabay-images.php

    r926633 r1067992  
    1 <?
     1<?php
    22
    33/*
     
    55Plugin URI: http://pixabay.com/blog/posts/p-36/
    66Description: Find quality public domain images from Pixabay and upload them with just one click.
    7 Version: 2.0
     7Version: 2.4
    88Author: Simon Steinberger
    99Author URI: http://pixabay.com/users/Simon/
     
    1313
    1414// i18n
     15function pixabay_images_load_textdomain() { load_plugin_textdomain('pixabay_images', false, dirname(plugin_basename(__FILE__ )).'/langs/'); }
    1516add_action('plugins_loaded', 'pixabay_images_load_textdomain');
    16 function pixabay_images_load_textdomain() {
    17     load_plugin_textdomain('pixabay_images', false, dirname(plugin_basename(__FILE__ )).'/langs/');
    18 }
    1917
    2018
     
    2422
    2523// add tab to media upload window
    26 add_filter('media_upload_tabs', function($tabs){ $tabs['pixabaytab'] = __('Pixabay Images', 'pixabay_images'); return $tabs; });
     24function media_upload_tabs_handler($tabs) { $tabs['pixabaytab'] = __('Pixabay Images', 'pixabay_images'); return $tabs; }
     25add_filter('media_upload_tabs', 'media_upload_tabs_handler');
    2726
    2827
    2928// add button next to "Add Media"
    3029$pixabay_images_settings = get_option('pixabay_images_options');
    31 if (!$pixabay_images_settings['button'] | $pixabay_images_settings['button']=='true')
    32     add_filter('media_buttons_context', function($editor_id=''){ return '<a href="'.add_query_arg('tab', 'pixabaytab', esc_url(get_upload_iframe_src())).'" id="'.esc_attr($editor_id).'-add_media" class="thickbox button" title="'.esc_attr__('Pixabay Images', 'pixabay_images').'"><img style="position:relative;top:-1px" src="'.plugin_dir_url(__FILE__).'favicon.ico'.'"> Pixabay</a>'; });
     30if (!$pixabay_images_settings['button'] | $pixabay_images_settings['button']=='true') {
     31    function media_buttons_context_handler($editor_id='') { return '<a href="'.add_query_arg('tab', 'pixabaytab', esc_url(get_upload_iframe_src())).'" id="'.esc_attr($editor_id).'-add_media" class="thickbox button" title="'.esc_attr__('Pixabay Images', 'pixabay_images').'"><img style="position:relative;top:-2px" src="'.plugin_dir_url(__FILE__).'favicon.png'.'"> Pixabay</a>'; }
     32    add_filter('media_buttons_context', 'media_buttons_context_handler');
     33}
    3334
    3435
     
    6061                <p><input id="q" type="text" value="" style="width:100%;max-width:500px;padding:7px 9px"></p>
    6162                <p>
    62                     <label style="margin-right:15px"><input type="checkbox" id="filter_photos"<?= $pixabay_images_settings['image_type']=='clipart'?'':' checked="checked"'; ?>><?_e('Photos', 'pixabay_images');?></label>
    63                     <label style="margin-right:20px"><input type="checkbox" id="filter_cliparts"<?= $pixabay_images_settings['image_type']=='photo'?'':' checked="checked"'; ?>><?_e('Cliparts', 'pixabay_images');?></label>
     63                    <label style="margin-right:15px"><input type="checkbox" id="filter_photos"<?= $pixabay_images_settings['image_type']=='clipart'?'':' checked="checked"'; ?>><?= _e('Photos', 'pixabay_images'); ?></label>
     64                    <label style="margin-right:20px"><input type="checkbox" id="filter_cliparts"<?= $pixabay_images_settings['image_type']=='photo'?'':' checked="checked"'; ?>><?= _e('Cliparts', 'pixabay_images'); ?></label>
    6465                    <span style="margin-right:20px">|</span>
    65                     <label style="margin-right:15px"><input type="checkbox" id="filter_horizontal"<?= $pixabay_images_settings['orientation']=='vertical'?'':' checked="checked"'; ?>><?_e('Horizontal', 'pixabay_images');?></label>
    66                     <label style="margin-right:25px"><input type="checkbox" id="filter_vertical"<?= $pixabay_images_settings['orientation']=='horizontal'?'':' checked="checked"'; ?>><?_e('Vertical', 'pixabay_images');?></label>
    67                     <a href="options-general.php?page=pixabay_images_settings" target="_blank"><?_e('Settings', 'pixabay_images');?></a>
     66                    <label style="margin-right:15px"><input type="checkbox" id="filter_horizontal"<?= $pixabay_images_settings['orientation']=='vertical'?'':' checked="checked"'; ?>><?= _e('Horizontal', 'pixabay_images'); ?></label>
     67                    <label style="margin-right:25px"><input type="checkbox" id="filter_vertical"<?= $pixabay_images_settings['orientation']=='horizontal'?'':' checked="checked"'; ?>><?= _e('Vertical', 'pixabay_images'); ?></label>
     68                    <a href="options-general.php?page=pixabay_images_settings" target="_blank"><?= _e('Settings', 'pixabay_images'); ?></a>
    6869                </p>
    69                 <input id="submit_search" class="button" type="submit" value="<?_e('Search', 'pixabay_images');?>">
     70                <input id="submit_search" class="button" type="submit" value="<?= _e('Search', 'pixabay_images'); ?>">
    7071            </form>
    7172            <div id="pixabay_results" style="margin-top:25px;padding-top:25px;border-top:1px solid #ddd"></div>
     
    7374        <script>
    7475            function crossDomainAjax(c,a){if("XDomainRequest" in window&&window.XDomainRequest!==null){var b=new XDomainRequest();b.open("get",c);b.onload=function(){var e=new ActiveXObject("Microsoft.XMLDOM"),d=$.parseJSON(b.responseText);e.async=false;if(d==null||typeof(d)=="undefined"){d=$.parseJSON(data.firstChild.textContent)}a(d)};b.onerror=function(){_result=false};b.send()}else{if(navigator.userAgent.indexOf('MSIE')!=-1&&parseInt(navigator.userAgent.match(/MSIE ([\d.]+)/)[1])<8){return false}else{$.ajax({url:c,cache:false,dataType:"json",type:"GET",async:false,success:function(d,e){a(d)}})}}};
    75             function escapeJS(s){return s.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;').replace(/"/g,'&quot;').replace(/'/g,"\\'");}
     76            function escapejs(s){return s.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;').replace(/"/g,'&quot;').replace(/'/g,"\\'");}
    7677            // hoverIntent r7
    7778            (function(e){e.fn.hoverIntent=function(t,n,r){var i={interval:100,sensitivity:7,timeout:0};if(typeof t==="object"){i=e.extend(i,t)}else if(e.isFunction(n)){i=e.extend(i,{over:t,out:n,selector:r})}else{i=e.extend(i,{over:t,out:t,selector:n})}var s,o,u,a;var f=function(e){s=e.pageX;o=e.pageY};var l=function(t,n){n.hoverIntent_t=clearTimeout(n.hoverIntent_t);if(Math.abs(u-s)+Math.abs(a-o)<i.sensitivity){e(n).off("mousemove.hoverIntent",f);n.hoverIntent_s=1;return i.over.apply(n,[t])}else{u=s;a=o;n.hoverIntent_t=setTimeout(function(){l(t,n)},i.interval)}};var c=function(e,t){t.hoverIntent_t=clearTimeout(t.hoverIntent_t);t.hoverIntent_s=0;return i.out.apply(t,[e])};var h=function(t){var n=jQuery.extend({},t);var r=this;if(r.hoverIntent_t){r.hoverIntent_t=clearTimeout(r.hoverIntent_t)}if(t.type=="mouseenter"){u=n.pageX;a=n.pageY;e(r).on("mousemove.hoverIntent",f);if(r.hoverIntent_s!=1){r.hoverIntent_t=setTimeout(function(){l(n,r)},i.interval)}}else{e(r).off("mousemove.hoverIntent",f);if(r.hoverIntent_s==1){r.hoverIntent_t=setTimeout(function(){c(n,r)},i.timeout)}}};return this.on({"mouseenter.hoverIntent":h,"mouseleave.hoverIntent":h},i.selector)}})(jQuery)
    7879
    79             var $=jQuery, post_id=<?=absint($_REQUEST['post_id'])?>,
     80            var $=jQuery, post_id=<?=absint($_REQUEST['post_id']) ?>,
    8081                lang='<?= $pixabay_images_settings['language']?$pixabay_images_settings['language']:substr(get_locale(), 0, 2) ?>',
    81                 per_page=<?=$pixabay_images_settings['per_page']?$pixabay_images_settings['per_page']:30?>,
     82                per_page=<?=$pixabay_images_settings['per_page']?$pixabay_images_settings['per_page']:30 ?>,
    8283                form = $('#pixabay_images_form'), hits, cache, resizeTimer, q, image_type, orientation;
    8384
     
    124125                    s += '<span class="button disabled">Prev</span>';
    125126                else
    126                     s += '<a href="#" onclick="return call_api(\''+escapeJS(q)+'\', '+(p-1)+');" class="button">Prev</a>';
     127                    s += '<a href="#" onclick="return call_api(\''+escapejs(q)+'\', '+(p-1)+');" class="button">Prev</a>';
    127128                for (i=1; i < pages+1; i++) {
    128                     s += '<a href="#" onclick="return call_api(\''+escapeJS(q)+'\', '+i+');" class="button'+(p==i?' disabled':'')+'">'+i+'</a>';
     129                    s += '<a href="#" onclick="return call_api(\''+escapejs(q)+'\', '+i+');" class="button'+(p==i?' disabled':'')+'">'+i+'</a>';
    129130                }
    130131                if (p==pages)
    131                     s += '<span class="button disabled">Prev</span>';
     132                    s += '<span class="button disabled">Next</span>';
    132133                else
    133                     s += '<a href="#" onclick="return call_api(\''+escapeJS(q)+'\', '+(p+1)+');" class="button">Next</a>';
     134                    s += '<a href="#" onclick="return call_api(\''+escapejs(q)+'\', '+(p+1)+');" class="button">Next</a>';
    134135                s += '</div>';
    135136                $('#pixabay_results').html(s);
     
    166167
    167168                        preview = $('<div data-idx="'+idx+'" class="preview">\
    168                             <div title="<?=_e('Insert image')?>" style="padding:4px 5px 6px;margin:0 0 5px;text-align:left;border-bottom:1px solid #ddd">\
     169                            <div title="<?= _e('Insert image') ?>" style="padding:4px 5px 6px;margin:0 0 5px;text-align:left;border-bottom:1px solid #ddd">\
    169170                                <a href="#" class="upload 150px" style="margin-right:15px">'+img.width()+' x '+img.height()+'</a>\
    170171                                <a href="#" class="upload 640px" style="margin-right:15px">'+width640+' x '+height640+'</a>\
    171172                                <a href="#" class="upload 1280px">'+width1280+' x '+height1280+'</a>\
    172173                            </div>\
    173                             <a title="<?=_e('Insert image')?>" href="#" class="upload 640px" style="display:block;margin:auto;position:relative;width:'+parseInt(width640/2)+'px;height:'+parseInt(height640/2)+'px;">\
     174                            <a title="<?= _e('Insert image') ?>" href="#" class="upload 640px" style="display:block;margin:auto;position:relative;width:'+parseInt(width640/2)+'px;height:'+parseInt(height640/2)+'px;">\
    174175                                <img src="'+img.attr('src')+'" style="width:'+parseInt(width640/2)+'px !important;height:'+parseInt(height640/2)+'px !important;">\
    175176                                <img src="'+hits[idx].webformatURL+'" style="width:'+parseInt(width640/2)+'px !important;height:'+parseInt(height640/2)+'px !important;">\
    176177                            </a>\
    177                             <div style="padding:6px 5px 4px;margin:5px 0 0;text-align:left;border-top:1px solid #ddd"><?_e('CC0 Image by', 'pixabay_images');?> <a href="http://pixabay.com/users/'+hits[idx].user+'/" target="_blank">'+hits[idx].user+'</a> / <a href="http://pixabay.com/'+lang+'/photos/?image_type='+image_type+'&orientation='+orientation+'&q='+escapeJS(q)+'" target="_blank">Pixabay</a></div>\
     178                            <div style="padding:6px 5px 4px;margin:5px 0 0;text-align:left;border-top:1px solid #ddd"><?= _e('CC0 Image by', 'pixabay_images'); ?> <a href="http://pixabay.com/users/'+hits[idx].user+'/" target="_blank">'+hits[idx].user+'</a> / <a href="http://pixabay.com/'+lang+'/photos/?image_type='+image_type+'&orientation='+orientation+'&q='+escapejs(q)+'" target="_blank">Pixabay</a></div>\
    178179                        </div>');
    179180                        thumb.append(preview);
     
    208209            });
    209210        </script>
    210     <?
     211    <?php
    211212}
    212 add_action('media_upload_pixabaytab', function(){ wp_iframe('media_pixabay_images_tab'); });
     213function media_upload_pixabaytab_handler() { wp_iframe('media_pixabay_images_tab'); }
     214add_action('media_upload_pixabaytab', 'media_upload_pixabaytab_handler');
    213215
    214216
     
    217219    $pixabay_images_settings = get_option('pixabay_images_options');
    218220
    219     # "pluggable.php" is required for current_user_can() and other upload relevant functions
     221    # "pluggable.php" is required for current_user_can() and other upload relevant functions
    220222    require_once(ABSPATH.'wp-includes/pluggable.php');
    221     if (!is_admin() or !current_user_can('edit_post', $post_id) ) die("You don't have permission to edit this post.");
     223    if (!is_user_logged_in() or !current_user_can('edit_post', $post_id)) die("You don't have permission to edit this post.");
     224
     225    // parse image_url
     226    $url = parse_url($_POST['image_url']);
     227    if(strcmp($url['host'], "pixabay.com")){
     228    die("Error: wrong host in url (must be pixabay.com)");
     229    }
    222230
    223231    // get image file
     
    227235    $q_tags = explode(' ' , $_POST['q']);
    228236    array_splice($q_tags, 2);
    229     foreach ($q_tags as $k=>$v) $q_tags[$k] = trim($v);
     237    foreach ($q_tags as $k=>$v) {
     238        // Remove ../../../..
     239        $v = str_replace("..","",$v);
     240        $q_tags[$k] = trim($v);
     241    }
    230242    $path_info = pathinfo($_POST['image_url']);
    231243    $file_name = implode('_', $q_tags).'_'.time().'.'.$path_info['extension'];
     
    242254    $result = @file_put_contents($target_file_name, $response['body']);
    243255    unset($response['body']);
    244     if ($result === false) die('Error: Failed to write file - '.$target_file_name);
     256    if ($result === false) die('Error: Failed to write file: '.$target_file_name);
     257
     258    // are we dealing with an image
     259    require_once(ABSPATH.'wp-admin/includes/image.php');
     260    if (!wp_read_image_metadata($target_file_name)) {
     261        unlink($target_file_name);
     262        die('Error: File is not an image.');
     263    }
     264
    245265    $image_title = ucwords(implode(', ', $q_tags));
    246266    $attachment_caption = '';
    247267    if (!$pixabay_images_settings['attribution'] | $pixabay_images_settings['attribution']=='true')
    248         $attachment_caption = '<a href="http://pixabay.com/users/'.$_POST['image_user'].'/">'.$_POST['image_user'].'</a> / Pixabay';
     268        $attachment_caption = '<a href="http://pixabay.com/users/'.htmlentities($_POST['image_user']).'/">'.htmlentities($_POST['image_user']).'</a> / Pixabay';
    249269
    250270    // insert attachment
     
    259279    if ($attach_id == 0) die('Error: File attachment error');
    260280
    261     require_once(ABSPATH.'wp-admin/includes/image.php');
    262281    $attach_data = wp_generate_attachment_metadata($attach_id, $target_file_name);
    263282    $result = wp_update_attachment_metadata($attach_id, $attach_data);
Note: See TracChangeset for help on using the changeset viewer.