Plugin Directory

Changeset 2796544


Ignore:
Timestamp:
10/10/2022 10:24:46 AM (3 years ago)
Author:
andreadegiovine
Message:

2.0.3

Location:
cloud-customer-area
Files:
26 added
6 deleted
6 edited

Legend:

Unmodified
Added
Removed
  • cloud-customer-area/trunk/cloud-customer-area.php

    r2793340 r2796544  
    11<?php
     2
     3namespace CloudCustomerArea;
     4
    25/*
    36Plugin Name: Cloud Customer Area
     
    811Text Domain: cloud-customer-area
    912Domain Path: /languages/
    10 Version: 1.1.9
     13Version: 2.0.3
    1114*/
    1215
     
    1922define('CCA_STRING', 'cloud-customer-area');
    2023define('CCA_SLUG', 'cloud_customer_area');
     24define('CCA_VER', get_file_data(__FILE__, array('Version' => 'Version'), false)['Version'] );
     25define('CCA_PRO_MIN_VER', '2.0.0');
    2126
    2227foreach (glob(CCA_PATH . "inc/*.php") as $file) {
     
    2429}
    2530
    26 if (!class_exists('cloud_customer_area')) {
    27     class cloud_customer_area
    28     {
    29         public $default_settings = array(
    30             'general' => array(
    31                 'customer_roles' => array(),
    32                 'customer_dir_name' => 'user_login',
    33                 'customer_can_upload' => 0,
    34                 'file_types' => array(),
    35                 'file_size_limit' => '',
    36             ),
    37             'customize' => array(
    38                 'label_name' => 'Name',
    39                 'label_date' => 'Date',
    40                 'label_type' => 'Type',
    41                 'label_download' => 'Download',
    42                 'label_select' => 'Choose file to upload',
    43                 'label_upload' => 'Upload file',
    44                 'label_supported' => '<strong>Supported files:</strong> %s',
    45                 'label_size' => '<strong>File size limit:</strong> %s',
    46                 'label_err_supported' => 'An error has occurred, file type not supported.',
    47                 'label_err_size' => 'An error has occurred, file size exceeds the limit.',
    48                 'label_err_generic' => 'An error has occurred, please try again or contact us.',
    49                 'label_uploaded' => 'The file was successfully uploaded.',
    50                 'label_nofiles' => 'No files available.',
    51                 'label_guest' => 'Before accessing this content, <a href="%s">please log in</a>.',
    52                 'label_logout' => 'Don\'t forget to <a href="%s">log out</a>.',
    53                 'table_bg' => '#fff',
    54                 'table_color' => '#555',
    55                 'table_border_color' => '#ccc',
    56                 'table_thead_bg' => '#eee',
    57                 'table_thead_color' => '#000',
    58             ),
    59             'oauth' => array(
    60                 'id_client' => '',
    61                 'client_secret' => '',
    62                 'access_token' => array('access_token' => ''),
    63             ),
    64         );
    65 
    66         public $files_mime = array(
    67             'image' => array('.jpg', '.jpeg', '.png', '.gif'),
    68             'video' => array('.mp4', '.3gp', '.mov', '.avi'),
    69             'document' => array('.pdf', '.doc', '.docs'),
    70             'archive' => array('.zip', '.rar', '.tar'),
    71         );
    72 
    73         public function __construct()
    74         {
    75             //
    76         }
    77 
    78         public function init_plugin()
    79         {
    80             register_activation_hook(__FILE__, array($this, 'plugin_install'));
    81             add_action('after_setup_theme', array($this, 'disable_admin_bar_customer_role'));
    82 
    83             add_action('init', array($this, 'init_set_token'));
    84             add_action('init', array($this, 'init_load_textdomain'));
    85             add_action('init', array($this, 'init_download_file'));
    86             add_action('admin_menu', array($this, 'init_settings_page'));
    87             add_action('admin_enqueue_scripts', array($this, 'init_admin_enqueue'));
    88             add_shortcode('cloud-customer-area', array($this, 'init_shortcode'));
    89             add_action('wp_head', array($this, 'init_dynamic_style'), 9999);
    90             add_action('wp_enqueue_scripts', array($this, 'init_public_enqueue'), 9998);
    91             add_action('wp_ajax_init_ajax_listen', array($this, 'init_ajax_listen'));
    92             add_action('wp_ajax_nopriv_init_ajax_listen', array($this, 'init_ajax_listen'));
    93             add_filter('plugin_action_links', array($this, 'init_actions_links'), 10, 2);
    94         }
    95 
    96         public function is_pro_version_active()
    97         {
    98             $return = false;
    99             $pro_version = in_array('cloud-customer-area-pro/cloud-customer-area-pro.php', apply_filters('active_plugins', get_option('active_plugins')));
    100             if ($pro_version) {
    101                 $return = true;
    102             }
    103             return $return;
    104         }
    105 
    106         public function init_actions_links($links, $file)
    107         {
    108             if ($file == 'cloud-customer-area/cloud-customer-area.php') {
    109                 $links[] = sprintf('<a href="%s"> %s </a>', menu_page_url(CCA_STRING, false), __('Settings', 'cloud-customer-area'));
    110                 if (!$this->is_pro_version_active()) {
    111                     $links[] = sprintf('<a href="%s" style="font-weight: bold;"> %s </a>', 'https://www.andreadegiovine.it/risorse/plugin/cloud-customer-area?utm_source=tools_plugin_page&utm_medium=plugin_page&utm_campaign=cloud_customer_area', __('Get PRO', 'cloud-customer-area'));
    112                 }
    113             }
    114             return $links;
    115         }
    116 
    117         public function init_dynamic_style()
    118         {
    119             $table_bg = CCA_getSetting('table_bg', 'customize');
    120             $table_border_color = CCA_getSetting('table_border_color', 'customize');
    121             $table_color = CCA_getSetting('table_color', 'customize');
    122             $table_thead_bg = CCA_getSetting('table_thead_bg', 'customize');
    123             $table_thead_color = CCA_getSetting('table_thead_color', 'customize');
    124 ?>
    125             <style>
    126                 .cloud-customer-area-table {
    127                     background-color: <?php echo $table_bg; ?>;
    128                     border-color: <?php echo $table_border_color; ?>;
    129                     color: <?php echo $table_color; ?>;
    130                 }
    131 
    132                 .cloud-customer-area-table thead tr {
    133                     background-color: <?php echo $table_thead_bg; ?>;
    134                     color: <?php echo $table_thead_color; ?>;
    135                 }
    136 
    137                 .cloud-customer-area-table tbody tr,
    138                 .cloud-customer-area-table tbody td {
    139                     border-color: <?php echo $table_border_color; ?>;
    140                 }
    141 
    142                 .cloud-customer-area-table .load td,
    143                 .cloud-customer-area-upload button.uploading:before {
    144                     background-image: url(<?php echo CCA_URL; ?>assets/loading.svg);
    145                 }
    146 
    147                 .cloud-customer-area-upload label {
    148                     border-color: <?php echo $table_border_color; ?>;
    149                 }
    150 
    151                 .cloud-customer-area-upload label .select-file:before {
    152                     background-image: url(<?php echo CCA_URL; ?>assets/folder.svg);
    153                 }
    154             </style>
    155 <?php
    156         }
    157 
    158         public function disable_admin_bar_customer_role()
    159         {
    160             if (current_user_can('customer_area_user') && !is_admin()) {
    161                 show_admin_bar(false);
    162             }
    163         }
    164 
    165         public function init_download_file()
    166         {
    167             $customer_roles = CCA_getSetting('customer_roles', 'general');
    168 
    169             if (isset($_GET['action']) && $_GET['action'] == 'download' && isset($_GET['download_name']) && isset($_GET['download_id']) && isset($_GET['token']) && (!in_array('guest', $customer_roles) ? wp_verify_nonce($_GET['token'], CCA_SLUG . '_download') : true)) {
    170                 CCA_googleApi_downloadFile($_GET['download_name'], $_GET['download_id'], (!empty($_GET['export']) ? $_GET['export'] : false));
    171                 exit;
    172             } else {
    173                 return;
    174             }
    175         }
    176 
    177         public function init_public_enqueue($hook)
    178         {
    179             wp_register_style('frontend-' . CCA_STRING, CCA_URL . 'assets/frontend.css', false, '1.0.0');
    180             wp_enqueue_style('frontend-' . CCA_STRING);
    181 
    182             wp_enqueue_script('frontend-' . CCA_STRING, CCA_URL . 'assets/frontend.js', array('jquery'));
    183             wp_localize_script(
    184                 'frontend-' . CCA_STRING,
    185                 'frontend_' . CCA_SLUG,
    186                 array(
    187                     'ajaxurl' => admin_url('admin-ajax.php'),
    188                     'token' => wp_create_nonce(CCA_SLUG . '_list_files'),
    189                     'user' => get_current_user_id(),
    190                 )
    191             );
    192         }
    193 
    194         public function init_ajax_listen()
    195         {
    196             $customer_roles = CCA_getSetting('customer_roles', 'general');
    197 
    198             if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest' && CCA_currentUserCan() && isset($_REQUEST['token']) && (!in_array('guest', $customer_roles) ? wp_verify_nonce($_REQUEST['token'], CCA_SLUG . '_list_files') : true) && check_ajax_referer(CCA_SLUG . '_list_files', 'token', false)) {
    199                 $user = isset($_REQUEST['user']) || $_REQUEST['user'] == 0 ? sanitize_text_field($_REQUEST['user']) : false;
    200 
    201                 $user_folder = CCA_googleApi_getUserFolder();
    202                 if (!empty($user_folder)) {
    203                     $user_files = CCA_googleApi_listFiles("'" . $user_folder . "' in parents and trashed=false");
    204                     if (!$user_files) {
    205                         $return_output[] = array(
    206                             'name' => __(CCA_getSetting('label_err_generic', 'customize'), 'cloud-customer-area'),
    207                             'date' => ' - ',
    208                             'ext' => ' - ',
    209                             'download_link' => ' - ',
    210                         );
    211                     }
    212                 } else {
    213                     $user_files = false;
    214                 }
    215 
    216                 $return_output = array();
    217 
    218                 if ($user_files) {
    219                     foreach ($user_files as $file) {
    220 
    221                         $date = explode('T', $file->modifiedTime)[0];
    222                         $date = date(get_option('date_format'), strtotime($date));
    223 
    224                         $time = explode('T', $file->modifiedTime)[1];
    225                         $time = explode(':', $time)[0] . ':' . explode(':', $time)[1];
    226                         $download_args = [
    227                             'action' => 'download',
    228                             'download_name' => urlencode($file->name),
    229                             'download_id' => $file->id,
    230                             'token' => wp_create_nonce(CCA_SLUG . '_download')
    231                         ];
    232                         if(strpos($file->mimeType, 'application/vnd.google-apps') !== false){
    233                             $download_args['export'] = str_replace('application/vnd.google-apps.', '', $file->mimeType);
    234                         }
    235                         $download_link = add_query_arg($download_args, site_url());
    236                         $return_output[] = array(
    237                             'name' => $file->name,
    238                             'date' => $date,
    239                             'ext' => !empty($file->fileExtension) ? $file->fileExtension : '',
    240                             'icon' => !empty($file->iconLink) ? $file->iconLink : '',
    241                             'download_link' => '<a href="' . $download_link . '" target="_blank">' . __(CCA_getSetting('label_download', 'customize'), 'cloud-customer-area') . '</a>',
    242                         );
    243                     }
    244                 }
    245 
    246                 if (empty($return_output)) {
    247                     $return_output[] = array(
    248                         'name' => __(CCA_getSetting('label_nofiles', 'customize'), 'cloud-customer-area'),
    249                         'date' => ' - ',
    250                         'ext' => ' - ',
    251                         'download_link' => ' - ',
    252                     );
    253                 }
    254 
    255                 echo json_encode($return_output);
    256             }
    257 
    258             do_action('cca_ajax_upload');
    259 
    260             die();
    261         }
    262 
    263         public function init_shortcode()
    264         {
    265             $output = '';
    266 
    267             if (CCA_currentUserCan()) {
    268 
    269                 $output = apply_filters('cca_form_upload', $output);
    270 
    271                 $output .= '<table class="' . CCA_STRING . '-table">';
    272                 $output .= '<thead><tr><th>' . __(CCA_getSetting('label_name', 'customize'), 'cloud-customer-area') . '</th><th>' . __(CCA_getSetting('label_date', 'customize'), 'cloud-customer-area') . '</th><th>' . __(CCA_getSetting('label_type', 'customize'), 'cloud-customer-area') . '</th><th>' . __(CCA_getSetting('label_download', 'customize'), 'cloud-customer-area') . '</th></tr></thead><tbody>';
    273                 $output .= '<tr class="load"><td colspan="4"></td></tr>';
    274                 $output .= '</tbody></table>';
    275                 if (is_user_logged_in()) {
    276                     $output .= '<div class="' . CCA_STRING . '-logout">' . sprintf(CCA_getSetting('label_logout', 'customize'), wp_logout_url($_SERVER['REQUEST_URI'])) . '</div>';
    277                 }
    278             } else {
    279 
    280                 $output .= '<div class="' . CCA_STRING . '-guest">' . sprintf(CCA_getSetting('label_guest', 'customize'), wp_login_url($_SERVER['REQUEST_URI'])) . '</div>';
    281             }
    282 
    283             return '<div class="' . CCA_STRING . '-container">' . $output . '</div>';
    284         }
    285 
    286         public function plugin_install()
    287         {
    288             add_role('customer_area_user', __('Cloud Area User', 'cloud-customer-area'), array());
    289         }
    290 
    291         public function init_set_token()
    292         {
    293             if (isset($_GET['code'])) {
    294                 $access_token = CCA_googleApi_getToken($_GET['code']);
    295                 CCA_updateAccessToken($access_token);
    296                 wp_redirect(CCA_getSetting('redirect_url', 'oauth'));
    297                 exit;
    298             }
    299         }
    300 
    301         public function plugin_reset()
    302         {
    303             delete_option(CCA_SLUG . '_general');
    304             delete_option(CCA_SLUG . '_oauth');
    305         }
    306 
    307         public function init_load_textdomain()
    308         {
    309             load_plugin_textdomain(CCA_STRING, false, CCA_PATH . 'languages');
    310         }
    311 
    312         public function init_settings_page()
    313         {
    314             add_action('admin_init', array($this, 'init_settings_options'));
    315             add_menu_page(__('Cloud Customer Area settings', 'cloud-customer-area'), __('Customer Area', 'cloud-customer-area'), 'administrator', CCA_STRING, array($this, 'get_settings_page'), 'dashicons-cloud');
    316         }
    317 
    318         public function init_settings_options()
    319         {
    320             register_setting(CCA_STRING . '-general', CCA_STRING . '-pro_license_key');
    321             register_setting(CCA_STRING . '-general', CCA_STRING . '-pro_last_license_check');
    322             register_setting(CCA_STRING . '-general', CCA_SLUG . '_general');
    323             register_setting(CCA_STRING . '-customize', CCA_SLUG . '_customize');
    324             register_setting(CCA_STRING . '-oauth', CCA_SLUG . '_oauth');
    325         }
    326 
    327         public function get_settings_page()
    328         {
    329             require_once(CCA_PATH . 'part/settings_page.php');
    330         }
    331 
    332         public function init_admin_enqueue()
    333         {
    334             wp_register_style('admin-' . CCA_STRING, CCA_URL . 'assets/admin-ui.css', false, '1.0.0');
    335             wp_enqueue_style('admin-' . CCA_STRING);
    336         }
    337     }
    338     $wp_cloud_customer_area = new cloud_customer_area();
    339     $wp_cloud_customer_area->init_plugin();
    340 }
     31$main = new \CloudCustomerArea\Inc\Main();
     32$main->init_plugin();
  • cloud-customer-area/trunk/inc/google-api.php

    r2793340 r2796544  
    11<?php
     2
     3namespace CloudCustomerArea\Inc;
    24
    35if (!defined('ABSPATH')) {
     
    57}
    68
    7 if (!class_exists('CCA_googleDrive')) {
    8     class CCA_googleDrive
    9     {
    10         public $client_id = '';
    11         public $client_secret = '';
    12         public $redirect_url = '';
    13 
    14         public function __construct($client_id, $client_secret, $redirect_url)
    15         {
    16             $this->client_id = $client_id;
    17             $this->client_secret = $client_secret;
    18             $this->redirect_url = $redirect_url;
    19         }
    20 
    21         public function sendAPIRequest($url, $content, $method = 'POST', $headers = false, $count = 0)
    22         {
    23             $count = $count + 1;
    24             $args = array(
    25                 'method' => $method,
    26                 'httpversion' => '1.0',
    27                 'sslverify' => false,
    28                 'headers' => array('Content-Type: application/json'),
    29                 'body' => $content
    30             );
    31             if ($headers) {
    32                 $args['headers'] = $headers;
     9class GoogleDrive
     10{
     11    /**
     12     * @var Main
     13     */
     14    private $main;
     15
     16    /**
     17     * @var string
     18     */
     19    private $client_id;
     20
     21    /**
     22     * @var string
     23     */
     24    private $client_secret;
     25
     26    /**
     27     * @var string
     28     */
     29    private $redirect_url;
     30
     31    /**
     32     * @var string
     33     */
     34    private $oauth_token;
     35
     36    /**
     37     * @var string
     38     */
     39    private $last_request;
     40
     41    /**
     42     * @var int
     43     */
     44    private $request_attemps = 0;
     45
     46    /**
     47     * @var int
     48     */
     49    private $max_request_attempts = 3;
     50
     51    public function __construct()
     52    {
     53        $this->main = new Main();
     54
     55        $this->client_id = $this->main->get_settings('id_client', 'oauth');
     56        $this->client_secret = $this->main->get_settings('client_secret', 'oauth');
     57        $this->redirect_url = $this->main->get_settings('redirect_url', 'oauth');
     58        $this->oauth_token = $this->main->get_settings('access_token', 'oauth');
     59    }
     60
     61    // Utils
     62
     63    /**
     64     * @param $url
     65     * @param $content
     66     * @param $method
     67     * @param $headers
     68     * @param $returnHeaders
     69     * @return false|string
     70     */
     71    public function sendAPIRequest($url, $content, $method = 'POST', $headers = false, $returnHeader = false)
     72    {
     73        $args = [
     74            'method' => $method,
     75            'timeout' => 300,
     76            'httpversion' => '1.0',
     77            'sslverify' => false,
     78            'headers' => ['Content-Type: application/json'],
     79            'body' => $content
     80        ];
     81        if ($headers) {
     82            $args['headers'] = $headers;
     83        }
     84
     85        $this->checkRequestAttemps([$url, $args]);
     86
     87        $response = wp_remote_request($url, $args);
     88
     89        array_unshift($args, ['url' => $url]);
     90        unset($args['timeout'], $args['sslverify'], $args['httpversion']);
     91
     92        $error = false;
     93        if (is_wp_error($response)) {
     94            $error = [
     95                'code' => implode(' - ', $response->get_error_codes()),
     96                'message' => implode(' - ', $response->get_error_messages())
     97            ];
     98        } elseif (!in_array(wp_remote_retrieve_response_code($response), [200, 201, 206, 308])) {
     99            $error = [
     100                'code' => wp_remote_retrieve_response_code($response),
     101                'message' => wp_remote_retrieve_body($response)
     102            ];
     103        }
     104        if ($error) {
     105            $this->main->write_log("Try API request \n\nRequest:\n" . print_r($args, true) . "\n\nResponse Error:\n\n" . print_r($error, true));
     106            return false;
     107        }
     108        $return = $returnHeader ? wp_remote_retrieve_header($response, $returnHeader) : wp_remote_retrieve_body($response);
     109        $this->main->write_log("Try API request \n\nRequest:\n" . print_r($args, true) . "\n\nResponse:\n" . print_r($return, true));
     110        return $return;
     111    }
     112
     113    /**
     114     * @param $request
     115     * @return void
     116     * @throws \Exception
     117     */
     118    private function checkRequestAttemps($request = [])
     119    {
     120        $serialized_request = serialize($request);
     121        if ($serialized_request == $this->last_request) {
     122            $this->request_attemps = $this->request_attemps + 1;
     123            if ($this->request_attemps > $this->max_request_attempts) {
     124                $this->main->write_log(__('Max request attemps', 'cloud-customer-area'));
     125                throw new \Exception($this->main->get_settings('label_err_generic', 'customize'), 500);
    33126            }
    34             $response = wp_remote_request($url, $args);
    35             if (!is_wp_error($response) && ($response['response']['code'] == 200 || $response['response']['code'] == 201)) {
    36                 return $response['body'];
    37             } else {
    38                 $request_log = array(
    39                     'endpoint' => $url,
    40                     'method' => $method,
    41                     'headers' => $headers,
    42                     'body' => $content,
    43                 );
    44 
    45                 if(function_exists('CCA_write_log')){
    46                     CCA_write_log("Try API request " .$count . "/3\n\nRequest:\n" . print_r($request_log, true) . "\n\nResponse body:\n" . print_r($response['body'], true));
    47                 }
    48 
    49                 // if ($count < 3) {
    50                 //     $this->sendAPIRequest($url, $content, $method, $headers, $count);
    51                 // } else {
    52                     return $response['body'];
    53                 // }
     127        } else {
     128            $this->last_request = $serialized_request;
     129        }
     130    }
     131
     132    /**
     133     * @return false|string
     134     */
     135    public function getConnectUrl()
     136    {
     137        if (empty($this->client_id) || empty($this->client_secret)) {
     138            return false;
     139        }
     140        $base_url = 'https://accounts.google.com/o/oauth2/auth';
     141        $url_query = [
     142            'response_type' => 'code',
     143            'access_type' => 'offline',
     144            'approval_prompt' => 'force',
     145            'client_id' => $this->client_id,
     146            'redirect_uri' => rawurlencode($this->redirect_url),
     147            'state' => '',
     148            'scope' => rawurlencode('https://www.googleapis.com/auth/drive'),
     149        ];
     150        $url = add_query_arg($url_query, $base_url);
     151        return $url;
     152    }
     153
     154    /**
     155     * @return string[]
     156     */
     157    public function getOauthHeaders()
     158    {
     159        $token = $this->getValidToken();
     160        return [
     161            'Authorization' => 'Bearer "' . $token->access_token . '"',
     162            'Accept' => 'application/json',
     163        ];
     164    }
     165
     166    // Oauth token
     167
     168    /**
     169     * @param $code
     170     * @return false|string
     171     */
     172    public function getToken($code = '')
     173    {
     174        if (empty($code)) {
     175            return false;
     176        }
     177        $args = [
     178            'code' => $code,
     179            'client_id' => $this->client_id,
     180            'client_secret' => $this->client_secret,
     181            'redirect_uri' => $this->redirect_url,
     182            'grant_type' => 'authorization_code',
     183        ];
     184        $response = $this->sendAPIRequest('https://accounts.google.com/o/oauth2/token', $args);
     185        return $response;
     186    }
     187
     188    /**
     189     * @param $token
     190     * @return mixed
     191     */
     192    public function updateToken($token)
     193    {
     194        $token->expires_in = time() - 10 + $token->expires_in;
     195        $this->main->update_setting('access_token', 'oauth', $token);
     196        return $token;
     197    }
     198
     199    /**
     200     * @return bool
     201     */
     202    private function checkToken()
     203    {
     204        if (!isset($this->oauth_token->access_token)) {
     205            return false;
     206        }
     207        if (!empty($this->oauth_token->expires_in) && $this->oauth_token->expires_in > time()) {
     208            return true;
     209        }
     210        $args = [
     211            'access_token' => $this->oauth_token->access_token,
     212        ];
     213        $response = $this->sendAPIRequest('https://accounts.google.com/o/oauth2/tokeninfo', $args);
     214        if ($response) {
     215            return true;
     216        }
     217        return false;
     218    }
     219
     220    /**
     221     * @return false|string
     222     */
     223    private function refreshToken()
     224    {
     225        if (!isset($this->oauth_token->refresh_token)) {
     226            return false;
     227        }
     228        $args = [
     229            'refresh_token' => $this->oauth_token->refresh_token,
     230            'client_id' => $this->client_id,
     231            'client_secret' => $this->client_secret,
     232            'grant_type' => 'refresh_token',
     233        ];
     234        $response = $this->sendAPIRequest('https://accounts.google.com/o/oauth2/token', $args);
     235        return $response;
     236    }
     237
     238    /**
     239     * @return false|mixed|string
     240     */
     241    private function getValidToken()
     242    {
     243        $check_request = $this->checkToken();
     244        if (!$check_request) {
     245            $store_refresh_token = $this->oauth_token->refresh_token;
     246            $refreshed_request = $this->refreshToken();
     247            if (!$refreshed_request || empty(json_decode($refreshed_request)->access_token)) {
     248                return $this->getValidToken();
    54249            }
    55         }
    56 
    57         public function connectUrl()
    58         {
    59             if (empty($this->client_id) || empty($this->client_secret)) {
    60                 return false;
    61             }
    62             $base_url = 'https://accounts.google.com/o/oauth2/auth';
    63             $url_query = array(
    64                 'response_type' => 'code',
    65                 'access_type' => 'offline',
    66                 'approval_prompt' => 'force',
    67                 'client_id' => $this->client_id,
    68                 'redirect_uri' => rawurlencode($this->redirect_url),
    69                 'state' => '',
    70                 'scope' => rawurlencode('https://www.googleapis.com/auth/drive'),
    71             );
    72             $url = add_query_arg($url_query, $base_url);
    73             return $url;
    74         }
    75 
    76         public function getToken($code = '')
    77         {
    78             if (empty($code)) {
    79                 return false;
    80             }
    81             $args = array(
    82                 'code' => $code,
    83                 'client_id' => $this->client_id,
    84                 'client_secret' => $this->client_secret,
    85                 'redirect_uri' => $this->redirect_url,
    86                 'grant_type' => 'authorization_code',
    87             );
    88             $response = $this->sendAPIRequest('https://accounts.google.com/o/oauth2/token', $args);
    89             return $response;
    90         }
    91 
    92         public function checkToken($token = '')
    93         {
    94             if (!isset($token->access_token)) {
    95                 return false;
    96             }
    97             $args = array(
    98                 'access_token' => $token->access_token,
    99             );
    100             $response = $this->sendAPIRequest('https://accounts.google.com/o/oauth2/tokeninfo', $args);
    101             return $response;
    102         }
    103 
    104         public function refreshToken($token = '')
    105         {
    106             if (!isset($token->access_token)) {
    107                 return false;
    108             }
    109             $args = array(
    110                 'refresh_token' => $token->refresh_token,
    111                 'client_id' => $this->client_id,
    112                 'client_secret' => $this->client_secret,
    113                 'grant_type' => 'refresh_token',
    114             );
    115             $response = $this->sendAPIRequest('https://accounts.google.com/o/oauth2/token', $args);
    116             return $response;
    117         }
    118 
    119         public function listFiles($token = '', $q = '')
    120         {
    121             if (!isset($token->access_token)) {
    122                 return false;
    123             }
    124             $args = array(
    125                 'key' => $this->client_id,
    126                 'fields' => 'nextPageToken, files(id, name, thumbnailLink, fileExtension, modifiedTime, mimeType, iconLink, properties)',
    127             );
    128             if (!empty($q)) {
    129                 $args['q'] = $q;
    130             }
    131             $headers = array(
    132                 'Authorization' => 'Bearer "' . $token->access_token . '"',
    133                 'Accept' => 'application/json',
    134             );
    135             $response = $this->sendAPIRequest('https://www.googleapis.com/drive/v3/files', $args, 'GET', $headers);
    136             return $response;
    137         }
    138 
    139         public function createFolder($token = '', $name = false)
    140         {
    141             if (!isset($token->access_token)) {
    142                 return false;
    143             }
    144             $args = array(
    145                 'name' => $name,
    146                 'mimeType' => 'application/vnd.google-apps.folder',
    147             );
    148             $headers = array(
    149                 'Authorization' => 'Bearer "' . $token->access_token . '"',
    150                 'Content-Type' => 'application/json',
    151             );
    152             $response = $this->sendAPIRequest('https://www.googleapis.com/drive/v3/files', json_encode($args), 'POST', $headers);
    153             return $response;
    154         }
    155 
    156         public function getFile($token = '', $fileID = false, $export = false)
    157         {
    158             if (!isset($token->access_token)) {
    159                 return false;
    160             }
    161             $fileExportTypeMap = [
    162                 'audio' => 'audio/mpeg',
    163                 'document' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
    164                 'drawing' => 'application/pdf',
    165                 'file' => 'application/octet-stream',
    166                 'presentation' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
    167                 'spreadsheet' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
    168             ];
    169             $headers = array(
    170                 'Authorization' => 'Bearer "' . $token->access_token . '"',
    171             );
    172             if($export){
    173                 if(empty($fileExportTypeMap[$export])){
    174                     return 'Error';
    175                 }
    176                 $response = $this->sendAPIRequest('https://www.googleapis.com/drive/v3/files/' . $fileID . '/export?mimeType=' . $fileExportTypeMap[$export], '', 'GET', $headers);
    177             } else {
    178                 $response = $this->sendAPIRequest('https://www.googleapis.com/drive/v3/files/' . $fileID . '/?alt=media', '', 'GET', $headers);
    179             }
    180             return $response;
    181         }
     250            $refreshed_request = json_decode($refreshed_request);
     251            $refreshed_request->refresh_token = $store_refresh_token;
     252            $this->oauth_token = $this->updateToken($refreshed_request);
     253        }
     254        return $this->oauth_token;
     255    }
     256
     257    // Actions
     258
     259    /**
     260     * @param $name
     261     * @return false|string
     262     */
     263    public function createFolder($name = false)
     264    {
     265        $args = [
     266            'name' => $name,
     267            'mimeType' => 'application/vnd.google-apps.folder',
     268        ];
     269        $headers = $this->getOauthHeaders();
     270        $headers['Content-Type'] = 'application/json';
     271        $response = $this->sendAPIRequest('https://www.googleapis.com/drive/v3/files', json_encode($args), 'POST', $headers);
     272        return $response;
     273    }
     274
     275    /**
     276     * @param $fileID
     277     * @return false|string
     278     */
     279    public function getFileInfo($fileID = false)
     280    {
     281        $args = [
     282            'key' => $this->client_id,
     283            'fields' => 'name, mimeType, size',
     284        ];
     285        $headers = $this->getOauthHeaders();
     286        $response = $this->sendAPIRequest('https://www.googleapis.com/drive/v3/files/' . $fileID, $args, 'GET', $headers);
     287        return $response;
     288    }
     289
     290    /**
     291     * @param $q
     292     * @return false|string
     293     */
     294    public function listFiles($q = '')
     295    {
     296        $args = [
     297            'key' => $this->client_id,
     298            'fields' => 'nextPageToken, files(id, name, modifiedTime, iconLink, mimeType, size)',
     299        ];
     300        if (!empty($q)) {
     301            $args['q'] = $q;
     302        }
     303        $headers = $this->getOauthHeaders();
     304        $response = $this->sendAPIRequest('https://www.googleapis.com/drive/v3/files', $args, 'GET', $headers);
     305        return $response;
     306    }
     307
     308    /**
     309     * @param $fileId
     310     * @param $part
     311     * @return false|string
     312     */
     313    public function downloadFile($fileId = false, $part = false)
     314    {
     315        $chunkSizeBytes = apply_filters('cca_download_max_chunk_size', 3) * 1024 * 1024;
     316        $chunkStart = ($chunkSizeBytes * ($part - 1)) + ($part - 1);
     317        $chunkEnd = $chunkStart + $chunkSizeBytes;
     318        $headers = $this->getOauthHeaders();
     319        unset($headers['Accept']);
     320        $headers['Range'] = sprintf('bytes=%s-%s', $chunkStart, $chunkEnd);
     321        $content = $this->sendAPIRequest('https://www.googleapis.com/drive/v3/files/' . $fileId . '/?alt=media', '', 'GET', $headers);
     322        return $content;
     323    }
     324
     325    /**
     326     * @param $fileId
     327     * @param $mime
     328     * @return false|string
     329     */
     330    public function exportFile($fileId = false, $mime = false)
     331    {
     332        $headers = $this->getOauthHeaders();
     333        unset($headers['Accept']);
     334        $content = $this->sendAPIRequest('https://www.googleapis.com/drive/v3/files/' . $fileId . '/export?mimeType=' . $mime, '', 'GET', $headers);
     335        return $content;
    182336    }
    183337}
  • cloud-customer-area/trunk/part/settings_page_customize.php

    r2310114 r2796544  
    55}
    66
     7$main = new \CloudCustomerArea\Inc\Main();
    78?>
    89<form method="post" action="options.php">
     
    1011    <?php do_settings_sections( CCA_STRING.'-customize' ); ?>
    1112    <?php
    12     $label_name = CCA_getSetting('label_name', 'customize');
    13     $label_date = CCA_getSetting('label_date', 'customize');
    14     $label_type = CCA_getSetting('label_type', 'customize');
    15     $label_download = CCA_getSetting('label_download', 'customize');   
    16     $label_err_generic = CCA_getSetting('label_err_generic', 'customize');   
    17     $label_nofiles = CCA_getSetting('label_nofiles', 'customize');
    18     $label_guest = CCA_getSetting('label_guest', 'customize');
    19     $label_logout = CCA_getSetting('label_logout', 'customize');
    20     $table_bg = CCA_getSetting('table_bg', 'customize');
    21     $table_color = CCA_getSetting('table_color', 'customize');
    22     $table_border_color = CCA_getSetting('table_border_color', 'customize');
    23     $table_thead_bg = CCA_getSetting('table_thead_bg', 'customize');
    24     $table_thead_color = CCA_getSetting('table_thead_color', 'customize');
     13    $label_name = $main->get_settings('label_name', 'customize');
     14    $label_date = $main->get_settings('label_date', 'customize');
     15    $label_type = $main->get_settings('label_type', 'customize');
     16    $label_file_size = $main->get_settings('label_file_size', 'customize');
     17    $label_download = $main->get_settings('label_download', 'customize');   
     18    $label_err_generic = $main->get_settings('label_err_generic', 'customize');   
     19    $label_nofiles = $main->get_settings('label_nofiles', 'customize');
     20    $label_guest = $main->get_settings('label_guest', 'customize');
     21    $label_logout = $main->get_settings('label_logout', 'customize');
     22    $table_bg = $main->get_settings('table_bg', 'customize');
     23    $table_color = $main->get_settings('table_color', 'customize');
     24    $table_border_color = $main->get_settings('table_border_color', 'customize');
     25    $table_thead_bg = $main->get_settings('table_thead_bg', 'customize');
     26    $table_thead_color = $main->get_settings('table_thead_color', 'customize');
    2527    ?>
    2628    <p><?php printf(__('To <span class="cca-pro-required">unlock all the features</span>, please <a href="%s" target="_blank" title="Get PRO version">get the PRO version</a>.', 'cloud-customer-area'), 'https://www.andreadegiovine.it/risorse/plugin/cloud-customer-area/?utm_source=tools_plugin_page&utm_medium=plugin_page&utm_campaign=cloud_customer_area' ); ?></p>
     
    4345            <td>
    4446                <input type="text" name="<?php echo CCA_SLUG.'_customize'; ?>[label_type]" value="<?php echo $label_type;?>" />
     47            </td>
     48        </tr>
     49        <tr valign="top">
     50            <th scope="row"><?php _e('Label for Size', 'cloud-customer-area'); ?></th>
     51            <td>
     52                <input type="text" name="<?php echo CCA_SLUG.'_customize'; ?>[label_file_size]" value="<?php echo $label_file_size;?>" />
    4553            </td>
    4654        </tr>
  • cloud-customer-area/trunk/part/settings_page_main.php

    r2539404 r2796544  
    55}
    66
     7$main = new \CloudCustomerArea\Inc\Main();
    78?>
    89<form method="post" action="options.php">
     
    1011    <?php do_settings_sections(CCA_STRING . '-general'); ?>
    1112    <?php
    12     $customer_roles = CCA_getSetting('customer_roles', 'general');
    13     $customer_dir_name = CCA_getSetting('customer_dir_name', 'general');
    14 
    15     $all_roles = CCA_getRoles();
     13    $customer_dir_name = $main->get_settings('customer_dir_name', 'general');
    1614    ?>
    1715    <p><?php printf(__('To <span class="cca-pro-required">unlock all the features</span>, please <a href="%s" target="_blank" title="Get PRO version">get the PRO version</a>.', 'cloud-customer-area'), 'https://www.andreadegiovine.it/risorse/plugin/cloud-customer-area/?utm_source=tools_plugin_page&utm_medium=plugin_page&utm_campaign=cloud_customer_area'); ?></p>
     
    3432                    </label> <?php _e('Guest User', 'cloud-customer-area'); ?>
    3533                </span>
    36                 <?php foreach ($all_roles as $role) { ?>
     34                <?php foreach ($main->get_roles() as $role) { ?>
    3735                    <br><br>
    3836                    <span class="cca-pro-required">
  • cloud-customer-area/trunk/part/settings_page_oauth.php

    r2310114 r2796544  
    55}
    66
     7$main = new \CloudCustomerArea\Inc\Main();
     8$google_api = new \CloudCustomerArea\Inc\GoogleDrive();
    79?>
    810<form method="post" action="options.php">
     
    1012    <?php do_settings_sections( CCA_STRING.'-oauth' ); ?>
    1113    <?php
    12     $id_client = CCA_getSetting('id_client', 'oauth');
    13     $client_secret = CCA_getSetting('client_secret', 'oauth');
    14     $access_token = CCA_getSetting('access_token', 'oauth');
    15     $redirect_url = CCA_getSetting('redirect_url', 'oauth');
     14    $id_client = $main->get_settings('id_client', 'oauth');
     15    $client_secret = $main->get_settings('client_secret', 'oauth');
     16    $access_token = $main->get_settings('access_token', 'oauth');
     17    $redirect_url = $main->get_settings('redirect_url', 'oauth');
    1618    ?>
    1719    <p><?php printf(__('All information about the plugin configuration is available on the <a href="%s" target="_blank" title="WordPress plugin developer">Developer website</a>.', 'cloud-customer-area'), 'https://www.andreadegiovine.it/risorse/plugin/cloud-customer-area?utm_source=tools_plugin_page&utm_medium=plugin_page&utm_campaign=cloud_customer_area' ); ?></p>
     
    4749    <div class="<?php echo CCA_STRING; ?>-notice notice-error"><div class="notice-icon"></div><div class="notice-text"><?php _e('Fill in the required fields and "Save changes".', 'cloud-customer-area'); ?></div></div>
    4850    <?php } elseif( empty( $access_token->access_token ) ) {
    49     echo '<br><a class="button button-hero load-customize hide-if-no-customize" href="'.CCA_googleApi_connectUrl().'">'.__('Connect to Google Drive', 'cloud-customer-area').'</a>';
     51    echo '<br><a class="button button-hero load-customize hide-if-no-customize" href="'.$google_api->getConnectUrl().'">'.__('Connect to Google Drive', 'cloud-customer-area').'</a>';
    5052} else { ?>
    5153    <div class="<?php echo CCA_STRING; ?>-notice notice-success"><div class="notice-icon"></div><div class="notice-text"><?php _e('Google Drive connected.', 'cloud-customer-area'); ?></div></div>
  • cloud-customer-area/trunk/readme.txt

    r2793340 r2796544  
    55Requires at least: 4.0
    66Tested up to: 6.0
    7 Stable tag: 1.1.9
     7Stable tag: 2.0.3
    88Requires PHP: 5.6
    99License: GPLv2 or later
     
    101101== Changelog ==
    102102
     103= 2.0.3 =
     104* Download using javascript to bypass download limits
     105
    103106= 1.1.9 =
    104107* WP 6.0 compatibility;
     
    133136
    134137== Upgrade Notice ==
     138
     139= 2.0.3 =
     140Download using javascript to bypass download limits
    135141
    136142= 1.1.9 =
Note: See TracChangeset for help on using the changeset viewer.