From 74e56dad1b78ac08693623787bc8c06610e61b99 Mon Sep 17 00:00:00 2001 From: justinhunt Date: Thu, 13 Jun 2019 22:27:21 +0900 Subject: [PATCH] updated so can properly use cloudpoodll and fixed template version instructions --- CHANGES.txt | 4 + classes/constants.php | 15 ++++ classes/generico_utils.php | 164 +++++++++++++++++++++++++++++++++--- filter.php | 2 +- lang/en/filter_generico.php | 14 ++- poodllloader.html | 2 +- refreshtoken.php | 44 ++++++++++ settings.php | 15 +++- version.php | 4 +- 9 files changed, 245 insertions(+), 19 deletions(-) create mode 100644 classes/constants.php create mode 100644 refreshtoken.php diff --git a/CHANGES.txt b/CHANGES.txt index 409e7b9..99075b4 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,5 +1,9 @@ Change List ========= +Version 1.4.09(Build 2019061400) +-Fixed cloud poodll token fetch (had been requiring Poodll) +-Added cloud poodll refresh +-Clarified template version format Version 1.4.08 (Build 2019022600) -Added a capabilities check feature to Generico, viewcapability="" and hidecapability="" diff --git a/classes/constants.php b/classes/constants.php new file mode 100644 index 0000000..96d6516 --- /dev/null +++ b/classes/constants.php @@ -0,0 +1,15 @@ +wwwroot); + $theregisteredurl = strtolower($theurl); + $theregisteredurl =trim($theregisteredurl); + + //add http:// or https:// to URLs that do not have it + if(strpos($theregisteredurl,'https://')!==0 && + strpos($theregisteredurl,'http://')!==0){ + $theregisteredurl= 'https://' . $theregisteredurl; + } + + //if neither parsed successfully, thats a no straight up + $wwwroot_bits = parse_url($thewwwroot); + $registered_bits = parse_url($theregisteredurl); + if(!$wwwroot_bits || ! $registered_bits){ + return self::CLOUDPOODLL_IS_UNREGISTERED; + } + + //get the subdomain widlcard address, ie *.a.b.c.d.com + $wildcard_subdomain_wwwroot=''; + if(array_key_exists('host',$wwwroot_bits)){ + $wildcardparts = explode('.',$wwwroot_bits['host']); + $wildcardparts[0]='*'; + $wildcard_subdomain_wwwroot = implode('.',$wildcardparts); + }else{ + return self::CLOUDPOODLL_IS_UNREGISTERED; + } + + //match either the exact domain or the wildcard domain or fail + if(array_key_exists('host', $registered_bits)){ + //this will cover exact matches and path matches + if($registered_bits['host'] === $wwwroot_bits['host']){ + $this->validated = true; + return self::CLOUDPOODLL_IS_REGISTERED; + //this will cover subdomain matches but only for institution bigdog and enterprise license + }elseif(($registered_bits['host']=== $wildcard_subdomain_wwwroot) && $wildcardok) + { + //yay we are registered!!!! + return self::CLOUDPOODLL_IS_REGISTERED; + }else{ + return self::CLOUDPOODLL_IS_UNREGISTERED; + } + }else{ + return self::CLOUDPOODLL_IS_UNREGISTERED; + } + } + + //This is called from the settings page and we do not want to make calls out to cloud.poodll.com on settings + //page load, for performance and stability issues. So if the cache is empty and/or no token, we just show a + //"refresh token" link + public function fetch_token_for_display($apiuser,$apisecret){ + global $CFG; + + //First check that we have an API id and secret + //refresh token + $refresh = \html_writer::link($CFG->wwwroot . '/filter/generico/refreshtoken.php', + get_string('refreshtoken',constants::MOD_FRANKY)) . '
'; + + + $message = ''; + $apiuser = trim($apiuser); + $apisecret = trim($apisecret); + if(empty($apiuser)){ + $message .= get_string('noapiuser',constants::MOD_FRANKY) . '
'; + } + if(empty($apisecret)){ + $message .= get_string('noapisecret',constants::MOD_FRANKY); + } + + if(!empty($message)){ + return $refresh . $message; + } + + //Fetch from cache and process the results and display + $cache = \cache::make_from_params(\cache_store::MODE_APPLICATION, constants::MOD_FRANKY, 'token'); + $tokenobject = $cache->get('recentpoodlltoken'); + + //if we have no token object the creds were wrong ... or something + if(!($tokenobject)){ + $message = get_string('notokenincache',constants::MOD_FRANKY); + //if we have an object but its no good, creds werer wrong ..or something + }elseif(!property_exists($tokenobject,'token') || empty($tokenobject->token)){ + $message = get_string('credentialsinvalid',constants::MOD_FRANKY); + //if we do not have subs, then we are on a very old token or something is wrong, just get out of here. + }elseif(!property_exists($tokenobject,'subs')){ + $message = 'No subscriptions found at all'; + } + if(!empty($message)){ + return $refresh . $message; + } + + //we have enough info to display a report. Lets go. + foreach ($tokenobject->subs as $sub){ + $sub->expiredate = date('d/m/Y',$sub->expiredate); + $message .= get_string('displaysubs',constants::MOD_FRANKY, $sub) . '
'; + } + //Is site authorised + $haveauthsite=false; + foreach ($tokenobject->sites as $site) { + if($this->check_registered_url($site)==self::CLOUDPOODLL_IS_REGISTERED){ + $haveauthsite=true; + break; + } + } + if(!$haveauthsite){ + $message .= get_string('appnotauthorised',constants::MOD_FRANKY) . '
'; + }else { + + //Is app authorised + if (in_array(constants::MOD_FRANKY, $tokenobject->apps)) { + $message .= get_string('appauthorised', constants::MOD_FRANKY) . '
'; + } else { + $message .= get_string('appnotauthorised', constants::MOD_FRANKY) . '
'; + } + } + return $refresh . $message; + } + //We need a Poodll token to make cloudpoodll happen - public static function fetch_token($apiuser, $apisecret) + public static function fetch_token($apiuser, $apisecret,$force=false) { - - $cache = \cache::make_from_params(\cache_store::MODE_APPLICATION, 'filter_generico', 'token'); + $cache = \cache::make_from_params(\cache_store::MODE_APPLICATION, constants::MOD_FRANKY, 'token'); $tokenobject = $cache->get('recentpoodlltoken'); $tokenuser = $cache->get('recentpoodlluser'); + $apiuser = trim($apiuser); + $apisecret = trim($apisecret); //if we got a token and its less than expiry time // use the cached one - if($tokenobject && $tokenuser && $tokenuser==$apiuser){ + if($tokenobject && $tokenuser && $tokenuser==$apiuser && !$force){ if($tokenobject->validuntil == 0 || $tokenobject->validuntil > time()){ - return $tokenobject->token; + return $tokenobject; } } // Send the request & save response to $resp $token_url ="https://cloud.poodll.com/local/cpapi/poodlltoken.php"; - $postdata=array('username'=>$apiuser,'password'=>$apisecret,'service'=>'cloud_poodll'); + $postdata=array( + 'username'=>$apiuser, + 'password'=>$apisecret, + 'service'=>'cloud_poodll'); $token_response = self::curl_fetch($token_url,$postdata); if ($token_response) { $resp_object = json_decode($token_response); @@ -182,23 +314,31 @@ public static function fetch_token($apiuser, $apisecret) $validuntil = 0; } - //cache the token - $tokenobject = new \stdClass(); - $tokenobject->token = $token; + //make sure the token has all the bits in it we expect before caching it + $tokenobject = $resp_object;//new \stdClass(); $tokenobject->validuntil = $validuntil; + if(!property_exists($tokenobject,'subs')){ + $tokenobject->subs = false; + } + if(!property_exists($tokenobject,'apps')){ + $tokenobject->apps = false; + } + if(!property_exists($tokenobject,'sites')){ + $tokenobject->sites = false; + } $cache->set('recentpoodlltoken', $tokenobject); $cache->set('recentpoodlluser', $apiuser); }else{ - $token = ''; + $tokenobject = false; if($resp_object && property_exists($resp_object,'error')) { //ERROR = $resp_object->error } } }else{ - $token=''; + $tokenobject=false; } - return $token; + return $tokenobject; } //we use curl to fetch transcripts from AWS and Tokens from cloudpoodll diff --git a/filter.php b/filter.php index 7d7d923..714a0ed 100644 --- a/filter.php +++ b/filter.php @@ -189,7 +189,7 @@ function filter_generico_callback(array $link){ if(strpos($genericotemplate,'@@CLOUDPOODLLTOKEN@@') && !empty($conf['cpapiuser']) && !empty($conf['cpapisecret'])){ - $token = \filter_poodll\poodlltools::fetch_token($conf['cpapiuser'],$conf['cpapisecret']); + $token = \filter_generico\generico_utils::fetch_token($conf['cpapiuser'],$conf['cpapisecret']); $genericotemplate = str_replace('@@CLOUDPOODLLTOKEN@@',$token,$genericotemplate); } diff --git a/lang/en/filter_generico.php b/lang/en/filter_generico.php index a062ec2..fb19e07 100644 --- a/lang/en/filter_generico.php +++ b/lang/en/filter_generico.php @@ -90,7 +90,7 @@ $string['templaterequirejsshim'] = ' Require Shim export'; $string['templaterequirejsshim_desc'] = ' Leave blank unless you know what shimming is'; $string['templateversion'] = 'The version of this template {$a}'; -$string['templateversion_desc'] = 'When sharing templates it is best to maintain a clear version per release. The version format is up to you.'; +$string['templateversion_desc'] = 'Use semantic versioning e.g 1.0.0. Generico will show an update button when the preset version is greater than the template version.';; $string['templatealternate'] = 'Alternate content'; $string['templatealternate_desc'] = 'Content that can be used when the custom and uploaded CSS and javascript content is not available. Currently this is used when the template is processed by a webservice, probably for content on the mobile app'; $string['templatealternate_end'] = 'Alternate content end (template {$a})'; @@ -104,7 +104,17 @@ //cloud poodll settings $string['cpapi_heading'] = 'Cloud Poodll API Settings'; $string['cpapi_heading_desc'] = "Cloud Poodll allows you to embed recorders direct from cloud.poodll.com in widgets. This is optional and you do not need to fill this in."; -$string['cpapiuser'] = 'Cloud Poodll Username'; +$string['cpapiuser'] = 'Cloud Poodll API User'; $string['cpapiuser_details'] = 'This is the same as your username at Poodll.com.'; $string['cpapisecret'] = 'Cloud Poodll API Secret'; $string['cpapisecret_details'] = "This is a special secret key that can be generated from the API tab in your members area on Poodll.com. "; + +//CLOUD POODLL API summary display info +$string['displaysubs'] = '{$a->subscriptionname} : expires {$a->expiredate}'; +$string['noapiuser'] = "No API username entered."; +$string['noapisecret'] = "No API secret entered."; +$string['credentialsinvalid'] = "The API username and secret entered could not be used to get access. Please check them."; +$string['appauthorised']= "Cloud Poodll is authorised for this site."; +$string['appnotauthorised']= "Cloud Poodll is not authorised for this site."; +$string['refreshtoken']= "Refresh Cloud Poodll license information."; +$string['notokenincache']= "Refresh Cloud Poodll license information to see details."; \ No newline at end of file diff --git a/poodllloader.html b/poodllloader.html index 6a64abb..5f9cd33 100644 --- a/poodllloader.html +++ b/poodllloader.html @@ -11,7 +11,7 @@ - +