Plugin Directory

Changeset 451077


Ignore:
Timestamp:
10/14/2011 01:38:14 PM (14 years ago)
Author:
freerobby
Message:

moving functions to optimize performance

Location:
sexybookmarks/trunk/includes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • sexybookmarks/trunk/includes/helper-functions.php

    r451068 r451077  
    4646}
    4747
    48 /* Adds FB Namespace */
    49 function shrsb_addFBNameSpace($attr) {
    50     $attr .= "\n xmlns:og=\"http://opengraphprotocol.org/schema/\"";
    51     $attr .= "\n xmlns:fb=\"http://www.facebook.com/2008/fbml\"";
    52     return $attr;
    53 }
    54 
    55 function shrsb_preFlight_Checks() {
    56     global $shrsb_plugopts;
    57 
    58     //Check for the directory exists or not
    59     if(!wp_mkdir_p(SHRSB_UPLOADDIR.'spritegen/')) {
    60         @error_log("Failed to create path ".dirname($path));
    61     }
    62     if (!is_writable(SHRSB_UPLOADDIR.'spritegen')) {
    63         // the spritegen folder isn't writable. Try changing it to writable
    64       @chmod(SHRSB_UPLOADDIR.'spritegen/', 0775);
    65       // may or may not work
    66   }
    67  
    68     if( ((function_exists('curl_init') && function_exists('curl_exec')) || function_exists('file_get_contents'))
    69             && (is_dir(SHRSB_UPLOADDIR) && is_writable(SHRSB_UPLOADDIR))
    70             && ((isset($_POST['bookmark']) && is_array($_POST['bookmark']) && sizeof($_POST['bookmark']) > 0 ) || (isset($shrsb_plugopts['bookmark']) && is_array($shrsb_plugopts['bookmark']) && sizeof($shrsb_plugopts['bookmark']) > 0 ))
    71             && (!isset($shrsb_plugopts['custom-mods']) ||  isset($shrsb_plugopts['custom-mods']) && $shrsb_plugopts['custom-mods'] !== 'yes') ) {
    72 
    73         return true;
    74     }
    75     else {
    76         return false;
    77     }
    78 }
    79 
    80 function get_sprite_file($opts, $type) {
    81   global $shrsb_plugopts;
    82   $shrbase = $shrsb_plugopts['shrbase']?$shrsb_plugopts['shrbase']:'http://www.shareaholic.com';
    83   $spritegen = $shrbase.'/api/sprite/?v=1&apikey=8afa39428933be41f8afdb8ea21a495c&imageset=60'.$opts.'&apitype='.$type;
    84   $filename = SHRSB_UPLOADDIR.'spritegen/shr-custom-sprite.'.$type;
    85   $content = FALSE;
    86 
    87   if (!is_writable(SHRSB_UPLOADDIR.'spritegen')) {
    88         // the spritegen folder isn't writable. Try changing it to writable
    89       @chmod(SHRSB_UPLOADDIR.'spritegen', 0775);
    90       // may or may not work
    91   }
    92   if ( $type == 'png' ) {
    93     $fp_opt = 'rb';
    94   }
    95   else {
    96     $fp_opt = 'r';
    97   }
    98 
    99   if(function_exists('wp_remote_retrieve_body') && function_exists('wp_remote_get') && function_exists('wp_remote_retrieve_response_code')) {
    100     $request = wp_remote_get(
    101       $spritegen,
    102       array(
    103         'user-agent' => "shr-wpspritebot-fopen/v" . SHRSB_vNum,
    104         'headers' => array(
    105           'Referer' => get_bloginfo('url')
    106         )
    107       )
    108     );
    109     $response = wp_remote_retrieve_response_code($request);
    110     if($response == 200 || $response == '200') {
    111       $content = wp_remote_retrieve_body($request);
    112     }
    113     else {
    114       $content = FALSE;
    115     }
    116   }
    117 
    118   if ( $content === FALSE && function_exists('curl_init') && function_exists('curl_exec') ) {
    119       $ch = curl_init();
    120     curl_setopt($ch, CURLOPT_URL, $spritegen);
    121     curl_setopt($ch, CURLOPT_FAILONERROR, TRUE);
    122     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    123     curl_setopt($ch, CURLOPT_TIMEOUT, 6);
    124     curl_setopt($ch, CURLOPT_USERAGENT, "shr-wpspritebot-cURL/v" . SHRSB_vNum);
    125     curl_setopt($ch, CURLOPT_REFERER, get_bloginfo('url'));
    126     curl_setopt($ch, CURLOPT_HEADER, FALSE);
    127     curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    128     curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE);
    129 
    130     $content = curl_exec($ch);
    131 
    132     if ( curl_errno($ch) != 0 ) {
    133       $content = FALSE;
    134     }
    135     curl_close($ch);
    136   }
    137 
    138   if ( $content !== FALSE ) {
    139     if ( $type == 'png' ) {
    140       $fp_opt = 'w+b';
    141     }
    142     else {
    143       $fp_opt = 'w+';
    144     }
    145 
    146    
    147     $fp = @fopen($filename, $fp_opt);
    148 
    149     if ( $fp !== FALSE ) {
    150       $ret = @fwrite($fp, $content);
    151       @fclose($fp);
    152     }
    153     else {
    154       $ret = @file_put_contents($filename, $content);
    155     }
    156 
    157     if ( $ret !== FALSE ) {
    158       @chmod($filename, 0666);
    159       return 0;
    160     }
    161     else {
    162       return 1;
    163     }
    164   }
    165   else {
    166     return 2;
    167   }
    168 }
    169 
    170 /**
    171  * Gets the contents of a url on www.shareaholic.com.  We use shrbase as the
    172  * URL base path.  The caller is responsible for keeping track of whether the
    173  * cache is up-to-date or not.  If the cache is stale (because some argument
    174  * has changed), then the caller should pass true as the second argument.
    175  *
    176  * @url        - the partial url without base.  ex. /publishers
    177  * @path       - path to cache result to, under spritegen.
    178  *               ex. /publishers.html
    179  *               pass null to use the path part of url
    180  * @clearcache - force call and overwrite cache.
    181  */
    182 function _shrsb_fetch_content($url, $path, $clearcache=false) {
    183   global $shrsb_plugopts;
    184 
    185   $shrbase = $shrsb_plugopts['shrbase']?$shrsb_plugopts['shrbase']:'http://www.shareaholic.com';
    186 
    187   if (!preg_match('|^/|', $url)) {
    188     @error_log("url must start with '/' in _shrsb_fetch_content");
    189     return FALSE;
    190   }
    191 
    192   // default path
    193   if (null === $path) {
    194     $url_parts = explode('?', $url);
    195     $path = rtrim($url_parts[0], '/');
    196   }
    197 
    198   $base_path = path_join(SHRSB_UPLOADDIR, 'spritegen');
    199   $abs_path = $base_path.$path;
    200 
    201   if ($clearcache || !($retval = _shrsb_read_file($abs_path))) {
    202     $response = wp_remote_get($shrbase.$url);
    203     if (is_wp_error($response)) {
    204       @error_log("Failed to fetch ".$shrbase.$url);
    205       $retval = FALSE;
    206     } else {
    207       $retval = $response['body'];
    208     }
    209 
    210    $write_succeed = _shrsb_write_file($abs_path, $retval);
    211    if(!$write_succeed) {
    212        $retval = FALSE;
    213    }
    214   }
    215 
    216   return $retval;
    217 }
    218 
    219 function _shrsb_write_file($path, $content) {
    220   $dir = dirname($path);
    221   $return = false;
    222   if(!wp_mkdir_p(dirname($path))) {
    223     @error_log("Failed to create path ".dirname($path));
    224   }
    225   $fh = fopen($path, 'w+');
    226   if (!$fh) {
    227     @error_log("Failed to open ".$path);
    228   }
    229   else {
    230     if (!fwrite($fh, $content)) {
    231       @error_log("Failed to write to ".$path);
    232     } else {
    233         $return = true;
    234     }
    235     @fclose($fh);
    236   }
    237   return $return;
    238 }
    239 
    240 function _shrsb_read_file($path) {
    241   $content = FALSE;
    242 
    243   $fh = @fopen($path, 'r');
    244   if (!$fh) {
    245     @error_log("Failed to open ".$path);
    246   }
    247   else {
    248     if (!$content = fread($fh, filesize($path))) {
    249       @error_log("Failed to read from ".$path);
    250     }
    251     @fclose($fh);
    252   }
    253 
    254   return $content;
    255 }
    256 
    257 //Copy the file in to the requested folder
    258 function _shrsb_copy_file($des , $src){
    259     if(!$des || !$src )
    260         return false;
    261     return _shrsb_write_file($des ,_shrsb_read_file($src));
    262 }
    263 
    264 
    265 
    26648/**
    26749 * Return Google Analytics for Admin Pages
     
    28567EOD;
    28668    return $google_analytics;
    287 }
    288 
    289 /**
    290  * Return SnapEngage Help Tab
    291  *
    292  * @return string
    293  * @author Jay Meattle
    294  **/
    295  
    296 function get_snapengage() {
    297     $snapengage = <<<EOD
    298 <!-- SnapEngage -->
    299 <script type="text/javascript">
    300 document.write(unescape("%3Cscript src='" + ((document.location.protocol=="https:")?"https://snapabug.appspot.com":"http://www.snapengage.com") + "/snapabug.js' type='text/javascript'%3E%3C/script%3E"));</script><script type="text/javascript">
    301 SnapABug.setDomain('shareaholic.com');
    302 SnapABug.addButton("62fa2e8b-38a9-4304-ba5c-86503444d30c","1","85%");
    303 </script>
    304 <!-- SnapEngage End -->
    305 EOD;
    306     return $snapengage;
    307 }
    308 
    309 //Change the directory path to webpath
    310 function shr_dir_to_path($dir){
    311     if(!$dir){
    312         return false;
    313     }
    314     $path = get_option("siteurl");
    315     if(substr($path, -1) != '/'){
    316         $path .= '/';
    317     }
    318     $path .= substr($dir , strlen(ABSPATH));
    319     return $path;
    32069}
    32170
  • sexybookmarks/trunk/includes/shrsb_settings_page.php

    r451072 r451077  
    235235
    236236
     237/**
     238 * Return SnapEngage Help Tab
     239 *
     240 * @return string
     241 * @author Jay Meattle
     242 **/
     243 
     244function get_snapengage() {
     245    $snapengage = <<<EOD
     246<!-- SnapEngage -->
     247<script type="text/javascript">
     248document.write(unescape("%3Cscript src='" + ((document.location.protocol=="https:")?"https://snapabug.appspot.com":"http://www.snapengage.com") + "/snapabug.js' type='text/javascript'%3E%3C/script%3E"));</script><script type="text/javascript">
     249SnapABug.setDomain('shareaholic.com');
     250SnapABug.addButton("62fa2e8b-38a9-4304-ba5c-86503444d30c","1","85%");
     251</script>
     252<!-- SnapEngage End -->
     253EOD;
     254    return $snapengage;
     255}
     256
     257//Change the directory path to webpath
     258function shr_dir_to_path($dir){
     259    if(!$dir){
     260        return false;
     261    }
     262    $path = get_option("siteurl");
     263    if(substr($path, -1) != '/'){
     264        $path .= '/';
     265    }
     266    $path .= substr($dir , strlen(ABSPATH));
     267    return $path;
     268}
     269
     270
     271
     272/**
     273 * Gets the contents of a url on www.shareaholic.com.  We use shrbase as the
     274 * URL base path.  The caller is responsible for keeping track of whether the
     275 * cache is up-to-date or not.  If the cache is stale (because some argument
     276 * has changed), then the caller should pass true as the second argument.
     277 *
     278 * @url        - the partial url without base.  ex. /publishers
     279 * @path       - path to cache result to, under spritegen.
     280 *               ex. /publishers.html
     281 *               pass null to use the path part of url
     282 * @clearcache - force call and overwrite cache.
     283 */
     284function _shrsb_fetch_content($url, $path, $clearcache=false) {
     285  global $shrsb_plugopts;
     286
     287  $shrbase = $shrsb_plugopts['shrbase']?$shrsb_plugopts['shrbase']:'http://www.shareaholic.com';
     288
     289  if (!preg_match('|^/|', $url)) {
     290    @error_log("url must start with '/' in _shrsb_fetch_content");
     291    return FALSE;
     292  }
     293
     294  // default path
     295  if (null === $path) {
     296    $url_parts = explode('?', $url);
     297    $path = rtrim($url_parts[0], '/');
     298  }
     299
     300  $base_path = path_join(SHRSB_UPLOADDIR, 'spritegen');
     301  $abs_path = $base_path.$path;
     302
     303  if ($clearcache || !($retval = _shrsb_read_file($abs_path))) {
     304    $response = wp_remote_get($shrbase.$url);
     305    if (is_wp_error($response)) {
     306      @error_log("Failed to fetch ".$shrbase.$url);
     307      $retval = FALSE;
     308    } else {
     309      $retval = $response['body'];
     310    }
     311
     312   $write_succeed = _shrsb_write_file($abs_path, $retval);
     313   if(!$write_succeed) {
     314       $retval = FALSE;
     315   }
     316  }
     317
     318  return $retval;
     319}
     320
     321
     322//Copy the file in to the requested folder
     323function _shrsb_copy_file($des , $src){
     324    if(!$des || !$src )
     325        return false;
     326    return _shrsb_write_file($des ,_shrsb_read_file($src));
     327}
     328
     329function _shrsb_write_file($path, $content) {
     330  $dir = dirname($path);
     331  $return = false;
     332  if(!wp_mkdir_p(dirname($path))) {
     333    @error_log("Failed to create path ".dirname($path));
     334  }
     335  $fh = fopen($path, 'w+');
     336  if (!$fh) {
     337    @error_log("Failed to open ".$path);
     338  }
     339  else {
     340    if (!fwrite($fh, $content)) {
     341      @error_log("Failed to write to ".$path);
     342    } else {
     343        $return = true;
     344    }
     345    @fclose($fh);
     346  }
     347  return $return;
     348}
     349
     350function _shrsb_read_file($path) {
     351  $content = FALSE;
     352
     353  $fh = @fopen($path, 'r');
     354  if (!$fh) {
     355    @error_log("Failed to open ".$path);
     356  }
     357  else {
     358    if (!$content = fread($fh, filesize($path))) {
     359      @error_log("Failed to read from ".$path);
     360    }
     361    @fclose($fh);
     362  }
     363
     364  return $content;
     365}
     366
     367
     368function get_sprite_file($opts, $type) {
     369  global $shrsb_plugopts;
     370  $shrbase = $shrsb_plugopts['shrbase']?$shrsb_plugopts['shrbase']:'http://www.shareaholic.com';
     371  $spritegen = $shrbase.'/api/sprite/?v=1&apikey=8afa39428933be41f8afdb8ea21a495c&imageset=60'.$opts.'&apitype='.$type;
     372  $filename = SHRSB_UPLOADDIR.'spritegen/shr-custom-sprite.'.$type;
     373  $content = FALSE;
     374
     375  if (!is_writable(SHRSB_UPLOADDIR.'spritegen')) {
     376        // the spritegen folder isn't writable. Try changing it to writable
     377      @chmod(SHRSB_UPLOADDIR.'spritegen', 0775);
     378      // may or may not work
     379  }
     380  if ( $type == 'png' ) {
     381    $fp_opt = 'rb';
     382  }
     383  else {
     384    $fp_opt = 'r';
     385  }
     386
     387  if(function_exists('wp_remote_retrieve_body') && function_exists('wp_remote_get') && function_exists('wp_remote_retrieve_response_code')) {
     388    $request = wp_remote_get(
     389      $spritegen,
     390      array(
     391        'user-agent' => "shr-wpspritebot-fopen/v" . SHRSB_vNum,
     392        'headers' => array(
     393          'Referer' => get_bloginfo('url')
     394        )
     395      )
     396    );
     397    $response = wp_remote_retrieve_response_code($request);
     398    if($response == 200 || $response == '200') {
     399      $content = wp_remote_retrieve_body($request);
     400    }
     401    else {
     402      $content = FALSE;
     403    }
     404  }
     405
     406  if ( $content === FALSE && function_exists('curl_init') && function_exists('curl_exec') ) {
     407      $ch = curl_init();
     408    curl_setopt($ch, CURLOPT_URL, $spritegen);
     409    curl_setopt($ch, CURLOPT_FAILONERROR, TRUE);
     410    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
     411    curl_setopt($ch, CURLOPT_TIMEOUT, 6);
     412    curl_setopt($ch, CURLOPT_USERAGENT, "shr-wpspritebot-cURL/v" . SHRSB_vNum);
     413    curl_setopt($ch, CURLOPT_REFERER, get_bloginfo('url'));
     414    curl_setopt($ch, CURLOPT_HEADER, FALSE);
     415    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
     416    curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE);
     417
     418    $content = curl_exec($ch);
     419
     420    if ( curl_errno($ch) != 0 ) {
     421      $content = FALSE;
     422    }
     423    curl_close($ch);
     424  }
     425
     426  if ( $content !== FALSE ) {
     427    if ( $type == 'png' ) {
     428      $fp_opt = 'w+b';
     429    }
     430    else {
     431      $fp_opt = 'w+';
     432    }
     433
     434   
     435    $fp = @fopen($filename, $fp_opt);
     436
     437    if ( $fp !== FALSE ) {
     438      $ret = @fwrite($fp, $content);
     439      @fclose($fp);
     440    }
     441    else {
     442      $ret = @file_put_contents($filename, $content);
     443    }
     444
     445    if ( $ret !== FALSE ) {
     446      @chmod($filename, 0666);
     447      return 0;
     448    }
     449    else {
     450      return 1;
     451    }
     452  }
     453  else {
     454    return 2;
     455  }
     456}
     457
     458
     459function shrsb_preFlight_Checks() {
     460    global $shrsb_plugopts;
     461
     462    //Check for the directory exists or not
     463    if(!wp_mkdir_p(SHRSB_UPLOADDIR.'spritegen/')) {
     464        @error_log("Failed to create path ".dirname($path));
     465    }
     466    if (!is_writable(SHRSB_UPLOADDIR.'spritegen')) {
     467        // the spritegen folder isn't writable. Try changing it to writable
     468      @chmod(SHRSB_UPLOADDIR.'spritegen/', 0775);
     469      // may or may not work
     470  }
     471 
     472    if( ((function_exists('curl_init') && function_exists('curl_exec')) || function_exists('file_get_contents'))
     473            && (is_dir(SHRSB_UPLOADDIR) && is_writable(SHRSB_UPLOADDIR))
     474            && ((isset($_POST['bookmark']) && is_array($_POST['bookmark']) && sizeof($_POST['bookmark']) > 0 ) || (isset($shrsb_plugopts['bookmark']) && is_array($shrsb_plugopts['bookmark']) && sizeof($shrsb_plugopts['bookmark']) > 0 ))
     475            && (!isset($shrsb_plugopts['custom-mods']) ||  isset($shrsb_plugopts['custom-mods']) && $shrsb_plugopts['custom-mods'] !== 'yes') ) {
     476
     477        return true;
     478    }
     479    else {
     480        return false;
     481    }
     482}
     483
     484/* Adds FB Namespace */
     485function shrsb_addFBNameSpace($attr) {
     486    $attr .= "\n xmlns:og=\"http://opengraphprotocol.org/schema/\"";
     487    $attr .= "\n xmlns:fb=\"http://www.facebook.com/2008/fbml\"";
     488    return $attr;
     489}
     490
    237491?>
Note: See TracChangeset for help on using the changeset viewer.