get_all_options();
$defaults = $this->option_defaults();
foreach( $defaults as $name=>$info ){
if( empty($info['widget']) && isset($options[$name])){
// Update non-widget settings only
$this->set_active_option($name,$options[$name]);
}
}
// Go ahead and reset info also
$this->set_private('results', array('photos'=>array(),'feed_found'=>false,'success'=>false,'userlink'=>'','hidden'=>'','message'=>'') );
}
//////////////////////////////////////////////////////////////////////////////////////
/////////////////////// Feed Fetch Functions //////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
/**
* Function for creating cache key
*
* @ Since 1.2.2
*/
function key_maker( $array ){
if( isset($array['name']) && is_array( $array['info'] ) ){
$return = $array['name'];
foreach( $array['info'] as $key=>$val ){
$return = $return."-".(!empty($val)?$val:$key);
}
$return = $this->filter_filename( $return );
return $return;
}
}
/**
* Filter string and remove specified characters
*
* @ Since 1.2.2
*/
function filter_filename( $name ){
$name = @ereg_replace('[[:cntrl:]]', '', $name ); // remove ASCII's control characters
$bad = array_merge(
array_map('chr', range(0,31)),
array("<",">",":",'"',"/","\\","|","?","*"," ",",","\'","."));
$return = str_replace($bad, "", $name); // Remove Windows filename prohibited characters
return $return;
}
//////////////////////////////////////////////////////////////////////////////////////
///////////////////////// Cache Functions /////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
/**
* Functions for retrieving results from cache
*
* @ Since 1.2.4
*
*/
function retrieve_from_cache( $key ){
if ( !$this->check_active_option('cache_disable') ) {
if( $this->cacheExists($key) ) {
$results = $this->getCache($key);
$results = @unserialize($results);
if( count($results) ){
$results['hidden'] .= '';
$this->set_private('results',$results);
if( $this->check_active_result('photos') ){
return true;
}
}
}
}
return false;
}
/**
* Functions for storing results in cache
*
* @ Since 1.2.4
*
*/
function store_in_cache( $key ){
if( $this->check_active_result('success') && !$this->check_active_option('disable_cache') ){
$cache_results = $this->get_private('results');
if(!is_serialized( $cache_results )) { $cache_results = @maybe_serialize( $cache_results ); }
$this->putCache($key, $cache_results);
$cachetime = $this->get_option( 'cache_time' );
if( !empty($cachetime) && is_numeric($cachetime) ){
$this->setExpiryInterval( $cachetime*60*60 );
}
}
}
/**
* Functions for caching results and clearing cache
*
* @since 1.1.0
*
*/
function setCacheDir($val) { $this->set_private('cacheDir',$val); }
function setExpiryInterval($val) { $this->set_private('expiryInterval',$val); }
function getExpiryInterval($val) { return (int)$this->get_private('expiryInterval'); }
function cacheExists($key) {
$filename_cache = $this->get_private('cacheDir') . '/' . $key . '.cache'; //Cache filename
$filename_info = $this->get_private('cacheDir') . '/' . $key . '.info'; //Cache info
if (file_exists($filename_cache) && file_exists($filename_info)) {
$cache_time = file_get_contents ($filename_info) + (int)$this->get_private('expiryInterval'); //Last update time of the cache file
$time = time(); //Current Time
$expiry_time = (int)$time; //Expiry time for the cache
if ((int)$cache_time >= (int)$expiry_time) {//Compare last updated and current time
return true;
}
}
return false;
}
function getCache($key) {
$filename_cache = $this->get_private('cacheDir') . '/' . $key . '.cache'; //Cache filename
$filename_info = $this->get_private('cacheDir') . '/' . $key . '.info'; //Cache info
if (file_exists($filename_cache) && file_exists($filename_info)) {
$cache_time = file_get_contents ($filename_info) + (int)$this->get_private('expiryInterval'); //Last update time of the cache file
$time = time(); //Current Time
$expiry_time = (int)$time; //Expiry time for the cache
if ((int)$cache_time >= (int)$expiry_time){ //Compare last updated and current time
return file_get_contents ($filename_cache); //Get contents from file
}
}
return null;
}
function putCache($key, $content) {
$time = time(); //Current Time
$dir = $this->get_private('cacheDir');
if ( !file_exists($dir) ){
@mkdir($dir);
$cleaning_info = $dir . '/cleaning.info'; //Cache info
@file_put_contents ($cleaning_info , $time); // save the time of last cache update
}
if ( file_exists($dir) && is_dir($dir) ){
$filename_cache = $dir . '/' . $key . '.cache'; //Cache filename
$filename_info = $dir . '/' . $key . '.info'; //Cache info
@file_put_contents($filename_cache , $content); // save the content
@file_put_contents($filename_info , $time); // save the time of last cache update
}
}
function clearAllCache() {
$dir = $this->get_private('cacheDir') . '/';
if(is_dir($dir)){
$opendir = @opendir($dir);
while(false !== ($file = readdir($opendir))) {
if($file != "." && $file != "..") {
if(file_exists($dir.$file)) {
$file_array = @explode('.',$file);
$file_type = @array_pop( $file_array );
// only remove cache or info files
if( 'cache' == $file_type || 'info' == $file_type){
@chmod($dir.$file, 0777);
@unlink($dir.$file);
}
}
/*elseif(is_dir($dir.$file)) {
@chmod($dir.$file, 0777);
@chdir('.');
@destroy($dir.$file.'/');
@rmdir($dir.$file);
}*/
}
}
@closedir($opendir);
}
}
function cleanCache() {
$cleaning_info = $this->get_private('cacheDir') . '/cleaning.info'; //Cache info
if (file_exists($cleaning_info)) {
$cache_time = file_get_contents ($cleaning_info) + (int)$this->cleaningInterval; //Last update time of the cache cleaning
$time = time(); //Current Time
$expiry_time = (int)$time; //Expiry time for the cache
if ((int)$cache_time < (int)$expiry_time){ //Compare last updated and current time
// Clean old files
$dir = $this->get_private('cacheDir') . '/';
if(is_dir($dir)){
$opendir = @opendir($dir);
while(false !== ($file = readdir($opendir))) {
if($file != "." && $file != "..") {
if(is_dir($dir.$file)) {
//@chmod($dir.$file, 0777);
//@chdir('.');
//@destroy($dir.$file.'/');
//@rmdir($dir.$file);
}
elseif(file_exists($dir.$file)) {
$file_array = @explode('.',$file);
$file_type = @array_pop( $file_array );
$file_key = @implode( $file_array );
if( $file_type && $file_key && 'info' == $file_type){
$filename_cache = $dir . $file_key . '.cache'; //Cache filename
$filename_info = $dir . $file_key . '.info'; //Cache info
if (file_exists($filename_cache) && file_exists($filename_info)) {
$cache_time = file_get_contents ($filename_info) + (int)$this->cleaningInterval; //Last update time of the cache file
$expiry_time = (int)$time; //Expiry time for the cache
if ((int)$cache_time < (int)$expiry_time) {//Compare last updated and current time
@chmod($filename_cache, 0777);
@unlink($filename_cache);
@chmod($filename_info, 0777);
@unlink($filename_info);
}
}
/*elseif (file_exists($filename_cache) && file_exists($filename_info)) {
$cache_time = file_get_contents ($filename_info) + (int)$this->cleaningInterval; //Last update time of the cache file
$expiry_time = (int)$time; //Expiry time for the cache
if ((int)$cache_time < (int)$expiry_time) {//Compare last updated and current time
@chmod($filename_cache, 0777);
@unlink($filename_cache);
@chmod($filename_info, 0777);
@unlink($filename_info);
}
}*/
}
}
}
}
@closedir($opendir);
}
@file_put_contents ($cleaning_info , $time); // save the time of last cache cleaning
}
}
}
/*
function putCacheImage($image_url){
$time = time(); //Current Time
if ( ! file_exists($this->cacheDir) ){
@mkdir($this->cacheDir);
$cleaning_info = $this->cacheDir . '/cleaning.info'; //Cache info
@file_put_contents ($cleaning_info , $time); // save the time of last cache update
}
if ( file_exists($this->cacheDir) && is_dir($this->cacheDir) ){
//replace with your cache directory
$dir = $this->cacheDir.'/';
//get the name of the file
$exploded_image_url = explode("/",$image_url);
$image_filename = end($exploded_image_url);
$exploded_image_filename = explode(".",$image_filename);
$name = current($exploded_image_filename);
$extension = end($exploded_image_filename);
//make sure its an image
if($extension=="gif"||$extension=="jpg"||$extension=="png"){
//get the remote image
$image_to_fetch = @file_get_contents($image_url);
//save it
$filename_image = $dir . $image_filename;
$filename_info = $dir . $name . '.info'; //Cache info
$local_image_file = @fopen($filename_image, 'w+');
@chmod($dir.$image_filename,0755);
@fwrite($local_image_file, $image_to_fetch);
@fclose($local_image_file);
@file_put_contents($filename_info , $time); // save the time of last cache update
}
}
}
function getImageCache($image_url) {
$dir = $this->cacheDir.'/';
$exploded_image_url = explode("/",$image_url);
$image_filename = end($exploded_image_url);
$exploded_image_filename = explode(".",$image_filename);
$name = current($exploded_image_filename);
$filename_image = $dir . $image_filename;
$filename_info = $dir . $name . '.info'; //Cache info
if (file_exists($filename_image) && file_exists($filename_info)) {
$cache_time = @file_get_contents ($filename_info) + (int)$this->expiryInterval; //Last update time of the cache file
$time = time(); //Current Time
$expiry_time = (int)$time; //Expiry time for the cache
if ((int)$cache_time >= (int)$expiry_time){ //Compare last updated and current time
return $this->cacheUrl.'/'.$image_filename; // Return image URL
}else{
$local_image_file = @fopen($filename_image, 'w+');
@chmod($dir.$image_filename,0755);
@fwrite($local_image_file, $image_to_fetch);
@fclose($local_image_file);
@file_put_contents($filename_info , $time); // save the time of last cache update
}
}elseif( $this->cacheAttempts < $this->cacheLimit ){
$this->putCacheImage($image_url);
$this->cacheAttempts++;
}
return null;
}
*/
}
/** ##############################################################################################################################################
* ##############################################################################################################################################
* ##############################################################################################################################################
* ##############################################################################################################################################
* ##############################################################################################################################################
* ##############################################################################################################################################
* ##############################################################################################################################################
* ##############################################################################################################################################
*
* AlpineBot Tertiary
*
* Display functions
* Contains ONLY UNIQUE functions
*
* ##########################################################################################
*/
class PhotoTileForInstagramBotTertiary extends PhotoTileForInstagramBotSecondary{
//////////////////////////////////////////////////////////////////////////////////////
////////////////// Unique Feed Fetch Functions /////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
/**
* Function for fetching instagram feed
*
* @ Since 1.2.1
* @ Updated 1.2.6
*/
function fetch_instagram_feed($request){
// No longer write out curl_init and user WP API instead
$response = wp_remote_get($request,
array(
'method' => 'GET',
'timeout' => 10,
'sslverify' => apply_filters('https_local_ssl_verify', false)
)
);
$this->append_active_result('hidden','');
if( is_wp_error( $response ) || !isset($response['body']) ) {
$this->append_active_result('hidden','');
if( is_wp_error( $response ) ){
$this->append_active_result('hidden','');
}elseif( !isset($response['body']) ){
$this->append_active_result('hidden','');
}
// Try again
if( method_exists( $this, 'manual_cURL' ) ){
$content = $this->manual_cURL($request);
}
if( !isset($content) ){
return false;
}
}else{
$content = $response['body'];
}
if( function_exists('json_decode') ){
$_instagram_json = @json_decode( $content, true );
}
if( empty($_instagram_json) && method_exists( $this, 'json_decoder' ) ){
$this->append_active_result('hidden','');
$_instagram_json = $this->json_decoder( $content );
}
if( empty($_instagram_json) || !isset($_instagram_json['meta']['code']) ){
$this->append_active_result('hidden','');
return false;
}elseif( 200 != $_instagram_json['meta']['code'] ){
$this->append_active_result('hidden','');
if( isset( $_instagram_json['meta']['error_message'] ) ){
$this->append_active_result('hidden','');
$this->append_active_result('message', '
- '.$_instagram_json['meta']['error_message'].'');
}
return false;
}else{
return $_instagram_json;
}
}
/**
* Alpine PhotoTile for Instagram: Photo Retrieval Function
* The PHP for retrieving content from Instagram.
*
* @ Since 1.0.0
* @ Updated 1.2.6
*/
function photo_retrieval(){
$options = $this->get_private('options');
$defaults = $this->option_defaults();
$instagram_uid = isset($options['instagram_user_id'])?$options['instagram_user_id']:'no_uid';
if( $instagram_uid == 'none' ){
$this->append_active_result('message','- You have not yet added an Instagram account to the plugin. Please return to the plugin\'s widget menu and follow the "Add an Instagram user" link.');
return;
}elseif( $instagram_uid == 'no_uid' ){
$this->append_active_result('message','- No Instagram user was specified.');
return;
}
$key_input = array(
'name' => 'instagram',
'info' => array(
'vers' => $this->get_private('vers'),
'src' => isset($options['instagram_source'])?$options['instagram_source']:'src',
'uid' => $instagram_uid,
'tag' => isset($options['instagram_tag'])?$options['instagram_tag']:'tag',$options['instagram_tag'],
'num' => isset($options['instagram_photo_number'])?$options['instagram_photo_number']:'num',
'link' => isset($options['instagram_display_link'])?$options['instagram_display_link']:'link',
'text' => isset($options['instagram_display_link_text'])?$options['instagram_display_link_text']:'text',
'size' => isset($options['instagram_photo_size'])?$options['instagram_photo_size']:'size',
)
);
$key = $this->key_maker( $key_input );
if( $this->retrieve_from_cache( $key ) ){ return; } // Check Cache
// Check if access_token is available for given user
$users = $this->get_instagram_users();
if( empty( $users[ $instagram_uid ] ) || empty( $users[ $instagram_uid ]['access_token'] )){
$this->append_active_result('hidden','');
$this->append_active_result('message','- Could not find an access token for '.$instagram_uid.'.');
if( !empty( $users[ $instagram_uid ] ) && is_array( $users[ $instagram_uid ] ) ){
foreach( $users[ $instagram_uid ] as $key=>$val ){
$this->hidden .= '';
}
}
return;
}
$token = $users[ $instagram_uid ]['access_token'];
$user_id = $users[ $instagram_uid ]['user_id'];
$num = $this->get_active_option('instagram_photo_number');
if( $this->check_active_option('photo_feed_offset') ){
$off = $this->get_active_option('photo_feed_offset');
$num = $num + $off;
}
if( $this->check_active_option('photo_feed_shuffle') && function_exists('shuffle') ){ // Shuffle the results
$num = min( 50, $num*4 );
}
$request = $this->get_instagram_request( $token, $user_id, $num );
if( $request ) {
$this->append_active_result('hidden','');
$this->try_json( $request, $num );
}
if( $this->check_active_result('success') ){
$src = $this->get_private('src');
if( $this->check_active_result('userlink') && $this->check_active_option($src.'_display_link') && $this->check_active_option($src.'_display_link_text') ){
$linkurl = $this->get_active_result('userlink');
$link = '