Changeset 4032


Ignore:
Timestamp:
Oct 13, 2009, 11:01:43 PM (11 years ago)
Author:
patdenice
Message:

[Plugin Autoupdate]
Add timeout to retrieve last version on piwigo server.

Location:
extensions/autoupdate
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • extensions/autoupdate/autoupdate.php

    r4007 r4032  
    11<?php
     2
     3/* Fetch remote function with timeout */
     4function autoupdate_fetchRemote($src, &$dest, $timeout=0, $user_agent='Piwigo', $step=0)
     5{
     6  // Try to retrieve data from local file?
     7  if (!url_is_remote($src))
     8  {
     9    $content = @file_get_contents($src);
     10    if ($content !== false)
     11    {
     12      is_resource($dest) ? @fwrite($dest, $content) : $dest = $content;
     13      return true;
     14    }
     15    else
     16    {
     17      return false;
     18    }
     19  }
     20
     21  // After 3 redirections, return false
     22  if ($step > 3) return false;
     23
     24  // Initialize $dest
     25  is_resource($dest) or $dest = '';
     26
     27  // Try curl to read remote file
     28  if (function_exists('curl_init'))
     29  {
     30    $ch = @curl_init();
     31    @curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
     32    @curl_setopt($ch, CURLOPT_URL, $src);
     33    @curl_setopt($ch, CURLOPT_HEADER, 1);
     34    @curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
     35    @curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     36    $content = @curl_exec($ch);
     37    $header_length = @curl_getinfo($ch, CURLINFO_HEADER_SIZE);
     38    $status = @curl_getinfo($ch, CURLINFO_HTTP_CODE);
     39    @curl_close($content);
     40    if ($content !== false and $status >= 200 and $status < 400)
     41    {
     42      if (preg_match('/Location:\s+?(.+)/', substr($content, 0, $header_length), $m))
     43      {
     44        return autoupdate_fetchRemote($m[1], $dest, $user_agent, $step+1);
     45      }
     46      $content = substr($content, $header_length);
     47      is_resource($dest) ? @fwrite($dest, $content) : $dest = $content;
     48      return true;
     49    }
     50  }
     51
     52  // Try file_get_contents to read remote file
     53  if (ini_get('allow_url_fopen'))
     54  {
     55    $opts['http'] = array('timeout' => $timeout);
     56    $ctx = stream_context_create($opts);
     57    $content = @file_get_contents($src, 0, $ctx);
     58    if ($content !== false)
     59    {
     60      is_resource($dest) ? @fwrite($dest, $content) : $dest = $content;
     61      return true;
     62    }
     63  }
     64
     65  return false;
     66}
    267
    368function autoupdate_deltree($path)
     
    3095  global $conf, $header_notes;
    3196
    32   if (preg_match('/(\d+\.\d+)\.(\d+)/', $version, $matches) and @fetchRemote(PHPWG_URL.'/download/latest_version', $result))
     97  if (preg_match('/(\d+\.\d+)\.(\d+)/', $version, $matches) and @autoupdate_fetchRemote(PHPWG_URL.'/download/latest_version', $result, 1))
    3398  {
    3499    $lines = @explode("\r\n", $result);
     
    49114      {
    50115        $zip = @fopen($filename, 'w+');
    51         @fetchRemote(PHPWG_URL.'/download/dlcounter.php?code='.str_replace(array('.', '_'), '', $code), $zip);
     116        @autoupdate_fetchRemote(PHPWG_URL.'/download/dlcounter.php?code='.str_replace(array('.', '_'), '', $code), $zip);
    52117        @fclose($zip);
    53118      }
Note: See TracChangeset for help on using the changeset viewer.