Plugin Directory

Changeset 1008022


Ignore:
Timestamp:
10/15/2014 05:48:36 PM (11 years ago)
Author:
outself
Message:

refactoring

File:
1 edited

Legend:

Unmodified
Added
Removed
  • asgard/trunk/asgard.php

    r1007296 r1008022  
    141141        $zip = asgard_zip_files( $toscan, $basepath );
    142142        $scanres = asgard_scan_zip( $zip );
    143         if ( $scanres && $scanres->match ) {
    144             foreach ( $scanres->verdict as $path => $verdict ) {
     143        if ( $scanres && $scanres['match'] ) {
     144            foreach ( $scanres['verdict'] as $path => $verdict ) {
    145145                $result[$basepath . $path] = $verdict;
    146146            }
     
    244244    $extensions = array(
    245245        'php',
     246        'php5',
     247        'php4',
    246248        'phtml',
    247         'php5',
    248249        'html',
    249250        'htaccess',
     
    265266}
    266267
    267 
    268 function asgard_scan_zip( $path ) {
    269     $body = array(
    270         'file' => '@' . $path
    271     );
     268function asgard_api_post( $url, $body, $json=false ) {
    272269    $ch = curl_init();
    273     curl_setopt( $ch, CURLOPT_URL, ASGARD_API . '/scan_zip' );
     270    curl_setopt( $ch, CURLOPT_URL, $url );
    274271    curl_setopt( $ch, CURLOPT_POST, 1 );
    275272    curl_setopt( $ch, CURLOPT_POSTFIELDS, $body );
    276273    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
     274    curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 2 );
     275    curl_setopt( $ch, CURLOPT_TIMEOUT, 60 );
     276    if ( $json ) curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json; charset=utf-8' ) );
    277277    $result = curl_exec( $ch );
     278    $errno = curl_errno( $ch );
     279    if ( $errno != 0 ) {
     280        asgard_html_error( sprintf( 'POST %s: error=%s code=%d', $url, $errno, curl_error( $ch ) ) );
     281    }
     282
    278283    $http_status = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
    279     $errno = curl_errno( $ch );
    280284    if ( $http_status != 200 ) {
    281         asgard_html_error( 'Scan error: code=' . $http_status . '. Please, try again later.' );
    282     }
     285        asgard_html_error( sprintf( 'POST %s error: code=%d. Please, try again later.', $url, $http_status ) );
     286    }
     287
    283288    curl_close( $ch );
    284     return json_decode( $result );
     289    return json_decode( $result, true );
     290}
     291
     292function asgard_scan_zip( $path ) {
     293    return asgard_api_post( ASGARD_API . '/scan_zip', array( 'file' => '@' . $path ) );
    285294}
    286295
     
    292301    // send blog url and email for auth
    293302    $plugin_info = get_plugin_data( __FILE__ );
    294     $args = build_query( array(
     303    $q = build_query( array(
    295304            'checksum' => md5( $body ) ,
    296305            'site_url' => get_site_url() ,
     
    300309            'asgard_version' => $plugin_info['Version'],
    301310        ) );
    302     $response = wp_remote_post( ASGARD_API . '/check?' . $args, array(
    303             'body' => $body,
    304             'headers' => array(
    305                 'Content-Type' => 'application/json; charset=utf-8',
    306             ) ,
    307         ) );
    308     if ( is_wp_error( $response ) ) {
    309         $error_message = $response->get_error_message();
    310         die( "Something went wrong: $error_message" );
    311     }
    312     if ( $response['response']['code'] != 200 ) {
    313         asgard_html_error( 'Send Hashes error: code=' . $response['response']['code'] . '. Please, try again later.' );
    314     }
    315     $result = json_decode( $response['body'], true );
    316     return $result['result'];
    317 }
    318 
    319 
    320 function asgard_auth_key() {
    321     $plugin_info = get_plugin_data( __FILE__ );
    322     $body = array(
    323         'site_url' => get_site_url() ,
    324         'admin_email' => get_option( 'admin_email' ) ,
    325         'wp_version' => get_bloginfo( 'version' ) ,
    326         'asgard_checksum' => ASGARD_CHECKSUM,
    327         'asgard_version' => $plugin_info['Version'],
    328     );
    329 
    330     $response = wp_remote_post( ASGARD_API . '/auth', array( 'body' => $body ) );
    331     if ( is_wp_error( $response ) ) {
    332         $error_message = $response->get_error_message();
    333         die( "Something went wrong: $error_message" );
    334     }
    335 
    336     if ( $response['response']['code'] != 200 ) {
    337         asgard_html_error( 'Auth Error: code=' . $response['response']['code'] );
    338     }
    339 
    340     return json_decode( $response['body'], true );
    341 }
     311
     312    $result = asgard_api_post( ASGARD_API . '/check?' . $q, $body, 'json' );
     313    return is_array( $result['result'] ) ? $result['result'] : array();
     314}
Note: See TracChangeset for help on using the changeset viewer.