Skip to content

Commit

Permalink
updated so can properly use cloudpoodll and fixed template version in…
Browse files Browse the repository at this point in the history
…structions
  • Loading branch information
justinhunt committed Jun 13, 2019
1 parent 8290a8f commit 74e56da
Show file tree
Hide file tree
Showing 9 changed files with 245 additions and 19 deletions.
4 changes: 4 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -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=""
Expand Down
15 changes: 15 additions & 0 deletions classes/constants.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/**
* Created by PhpStorm.
* User: ishineguy
* Date: 2018/06/16
* Time: 19:31
*/

namespace filter_generico;


class constants
{
const MOD_FRANKY='filter_generico';
}
164 changes: 152 additions & 12 deletions classes/generico_utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ class generico_utils
{
const FILTER_GENERICO_TEMPLATE_COUNT = 20;

const CLOUDPOODLL_VAL_BY_REGCODE = 1;
const CLOUDPOODLL_VAL_BY_APICREDS = 2;
const CLOUDPOODLL_IS_REGISTERED = 1;
const CLOUDPOODLL_IS_UNREGISTERED = 0;
const CLOUDPOODLL_IS_EXPIRED = 2;


public static function fetch_emptyproparray(){
$proparray=array();
Expand Down Expand Up @@ -149,25 +155,151 @@ public static function update_revision() {
set_config('revision', time(), 'filter_generico');
}

protected function check_registered_url($theurl,$wildcardok=true){
global $CFG;

//get arrays of the wwwroot and registered url
//just in case, lowercase'ify them
$thewwwroot = strtolower($CFG->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)) . '<br>';


$message = '';
$apiuser = trim($apiuser);
$apisecret = trim($apisecret);
if(empty($apiuser)){
$message .= get_string('noapiuser',constants::MOD_FRANKY) . '<br>';
}
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) . '<br>';
}
//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) . '<br>';
}else {

//Is app authorised
if (in_array(constants::MOD_FRANKY, $tokenobject->apps)) {
$message .= get_string('appauthorised', constants::MOD_FRANKY) . '<br>';
} else {
$message .= get_string('appnotauthorised', constants::MOD_FRANKY) . '<br>';
}
}
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);
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
14 changes: 12 additions & 2 deletions lang/en/filter_generico.php
Original file line number Diff line number Diff line change
Expand Up @@ -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})';
Expand All @@ -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 <a href='https://support.poodll.com/support/solutions/articles/19000083076-cloud-poodll-api-secret'>API tab</a> 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.";
2 changes: 1 addition & 1 deletion poodllloader.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<script type="text/javascript" src="https://cloud.poodll.com/local/cpapi/fastpoodllconfig.js?v=001"></script>
<script type="text/javascript" src="https://cloud.poodll.com/local/cpapi/lib/requirejs/require.js"></script>
<script type="text/javascript" src="https://cloud.poodll.com/local/cpapi/poodllloader.min.js?v=001"></script>
<script type="text/javascript" src="https://cloud.poodll.com/local/cpapi/poodllloader.min.js?v=002"></script>


<style type="text/css">
Expand Down
44 changes: 44 additions & 0 deletions refreshtoken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* A token refreshing helper for Read Aloud
*
*
* @package filter_poodll
* @copyright Justin Hunt ([email protected])
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/


require_once(dirname(dirname(dirname(__FILE__))).'/config.php');

use \filter_generico\constants;

require_login(0, false);
$systemcontext = context_system::instance();

if(has_capability('moodle/site:config',$systemcontext)){
$apiuser = get_config(constants::MOD_FRANKY,'cpapiuser');
$apisecret=get_config(constants::MOD_FRANKY,'cpapisecret');
$force=true;
if($apiuser && $apisecret) {
$gu = new \filter_generico\generico_utils();
$gu->fetch_token($apiuser, $apisecret, $force);
}
}
redirect($CFG->wwwroot . '/admin/settings.php?section=filtersettinggenerico');
15 changes: 14 additions & 1 deletion settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

defined('MOODLE_INTERNAL') || die;

use filter_generico\constants;

$settings = null;

if (is_siteadmin()) {
Expand Down Expand Up @@ -51,8 +53,19 @@
$settings_page->add(new admin_setting_heading('filter_generico_cpapi_settings', get_string('cpapi_heading', 'filter_generico'), get_string('cpapi_heading_desc', 'filter_generico')));
$settings_page->add(new admin_setting_configtext('filter_generico/cpapiuser', get_string('cpapiuser', 'filter_generico'),
get_string('cpapiuser_details', 'filter_generico'), ''));
//we show a summary of the users apps if we can get the info
$apiuser=get_config(constants::MOD_FRANKY,'cpapiuser');
$apisecret=get_config(constants::MOD_FRANKY,'cpapisecret');
if($apiuser && $apisecret) {
$gu = new \filter_generico\generico_utils();
$tokeninfo = $gu->fetch_token_for_display($apiuser, $apisecret);
}else{
$tokeninfo = get_string('cpapisecret_details', constants::MOD_FRANKY);
}
$settings_page->add(new admin_setting_configtext('filter_generico/cpapisecret', get_string('cpapisecret', 'filter_generico'),
get_string('cpapisecret_details', 'filter_generico'), ''));
$tokeninfo, ''));



//add page to category
$ADMIN->add($generico_category_name, $settings_page);
Expand Down
4 changes: 2 additions & 2 deletions version.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@

defined('MOODLE_INTERNAL') || die();

$plugin->version = 2019022600; // The current plugin version (Date: YYYYMMDDXX)
$plugin->version = 2019061400; // The current plugin version (Date: YYYYMMDDXX)
$plugin->requires = 2011070100; // Requires this Moodle version
$plugin->component = 'filter_generico'; // Full name of the plugin (used for diagnostics)
$plugin->maturity = MATURITY_STABLE;
$plugin->release = 'Version 1.4.08(Build 2019022600)';
$plugin->release = 'Version 1.4.09(Build 2019061400)';

0 comments on commit 74e56da

Please sign in to comment.