Resolves open issue 629

This commit is contained in:
Timothy Webb committed 2020-09-02 19:08:20 -04:00
1 parent bb4f963812
commit 3a5146ea02
7448 files changed
+1 -1041060

No files matched your search

-42
View File
@@ -1,42 +0,0 @@
<?php
/**
* BoxBilling
*
* @copyright BoxBilling, Inc (http://www.boxbilling.com)
* @license Apache-2.0
*
* Copyright BoxBilling, Inc
* This source file is subject to the Apache-2.0 License that is bundled
* with this source code in the file LICENSE
*/
/**
* Example module Admin API
*
* API can be access only by admins
*/
namespace Box\Mod\Example\Api;
class Admin extends \Api_Abstract
{
/**
* Return list of example objects
*
* @return string[]
*/
public function get_something($data)
{
$result = array(
'apple',
'google',
'facebook',
);
if(isset($data['microsoft'])) {
$result[] = 'microsoft';
}
return $result;
}
}
-50
View File
@@ -1,50 +0,0 @@
<?php
/**
* BoxBilling
*
* @copyright BoxBilling, Inc (http://www.boxbilling.com)
* @license Apache-2.0
*
* Copyright BoxBilling, Inc
* This source file is subject to the Apache-2.0 License that is bundled
* with this source code in the file LICENSE
*/
/**
* All public methods in this class are exposed to client using API.
* Always think what kind of information you are exposing.
*/
namespace Box\Mod\Example\Api;
class Client extends \Api_Abstract
{
/**
* From client API you can call any other module API
*
* This method will collect data from all APIs and merge
* into one result.
*
* Be careful not to expose sensitive data from Admin API.
*/
public function get_info($data)
{
// call custom event hook. All active modules will be notified
$this->di['events_manager']->fire(array('event'=>'onAfterClientCalledExampleModule', 'params'=>array('key'=>'value')));
// Log message
$this->di['logger']->info('Log message to log file');
$systemService = $this->di['mod_service']('System');
$clientService = $this->di['mod_service']('Client');
$type = $this->di['array_get']($data, 'type', 'info');
return array(
'data' => $data,
'version' => $systemService->getVersion(),
'profile' => $clientService->toApiArray($this->di['loggedin_client']),
'messages' => $systemService->getMessages($type)
);
}
}
-77
View File
@@ -1,77 +0,0 @@
<?php
/**
* BoxBilling
*
* @copyright BoxBilling, Inc (http://www.boxbilling.com)
* @license Apache-2.0
*
* Copyright BoxBilling, Inc
* This source file is subject to the Apache-2.0 License that is bundled
* with this source code in the file LICENSE
*/
/**
* All public methods in this class are exposed to public. Always think
* what kind of information you are exposing. Emails, passwords and other
* information should NOT be returned by functions in this class
*
* This module can be called from API or in template
*
*/
namespace Box\Mod\Example\Api;
class Guest extends \Api_Abstract
{
/**
* Get readme.md file contents
*/
public function readme($data)
{
$readme = $this->di['tools']->file_get_contents(BB_PATH_MODS . '/Example/README.md');
return $readme;
}
/**
* Get Top Songs from iTunes Store
* @param int $limit - define songs limit
*/
public function top_songs($data)
{
$limit = $this->di['array_get']($data, 'limit', 10);
$feed = "http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/topsongs/limit=$limit/xml";
$top_songs = $this->di['tools']->file_get_contents($feed);
$xml = simplexml_load_string($top_songs);
$array = array();
$this->_convertXmlObjToArr($xml, $array);
return $array;
}
private function _convertXmlObjToArr($obj, &$arr)
{
$children = $obj->children();
foreach ($children as $elementName => $node)
{
$nextIdx = count($arr);
$arr[$nextIdx] = array();
$arr[$nextIdx]['_name'] = strtolower((string)$elementName);
$arr[$nextIdx]['_attributes'] = array();
$attributes = $node->attributes();
foreach ($attributes as $attributeName => $attributeValue)
{
$attribName = strtolower(trim((string)$attributeName));
$attribVal = trim((string)$attributeValue);
$arr[$nextIdx]['_attributes'][$attribName] = $attribVal;
}
$text = (string)$node;
$text = trim($text);
if (strlen($text) > 0)
{
$arr[$nextIdx]['_text'] = $text;
}
$arr[$nextIdx]['_children'] = array();
$this->_convertXmlObjToArr($node, $arr[$nextIdx]['_children']);
}
return;
}
}
-126
View File
@@ -1,126 +0,0 @@
<?php
/**
* BoxBilling
*
* @copyright BoxBilling, Inc (http://www.boxbilling.com)
* @license Apache-2.0
*
* Copyright BoxBilling, Inc
* This source file is subject to the Apache-2.0 License that is bundled
* with this source code in the file LICENSE
*/
/**
* This file connects BoxBilling admin area interface and API
* Class does not extend any other class
*/
namespace Box\Mod\Example\Controller;
class Admin implements \Box\InjectionAwareInterface
{
protected $di;
/**
* @param mixed $di
*/
public function setDi($di)
{
$this->di = $di;
}
/**
* @return mixed
*/
public function getDi()
{
return $this->di;
}
/**
* This method registers menu items in admin area navigation block
* This navigation is cached in bb-data/cache/{hash}. To see changes please
* remove the file
*
* @return array
*/
public function fetchNavigation()
{
return array(
'group' => array(
'index' => 1500, // menu sort order
'location' => 'example', // menu group identificator for subitems
'label' => 'Example module', // menu group title
'class' => 'example', // used for css styling menu item
),
'subpages'=> array(
array(
'location' => 'example', // place this module in extensions group
'label' => 'Example module submenu',
'index' => 1500,
'uri' => $this->di['url']->adminLink('example'),
'class' => '',
),
),
);
}
/**
* Methods maps admin areas urls to corresponding methods
* Always use your module prefix to avoid conflicts with other modules
* in future
*
*
* @example $app->get('/example/test', 'get_test', null, get_class($this)); // calls get_test method on this class
* @example $app->get('/example/:id', 'get_index', array('id'=>'[0-9]+'), get_class($this));
* @param \Box_App $app
*/
public function register(\Box_App &$app)
{
$app->get('/example', 'get_index', array(), get_class($this));
$app->get('/example/test', 'get_test', array(), get_class($this));
$app->get('/example/user/:id', 'get_user', array('id'=>'[0-9]+'), get_class($this));
$app->get('/example/api', 'get_api', array(), get_class($this));
}
public function get_index(\Box_App $app)
{
// always call this method to validate if admin is logged in
$this->di['is_admin_logged'];
return $app->render('mod_example_index');
}
public function get_test(\Box_App $app)
{
// always call this method to validate if admin is logged in
$this->di['is_admin_logged'];
$params = array();
$params['youparamname'] = 'yourparamvalue';
return $app->render('mod_example_index', $params);
}
public function get_user(\Box_App $app, $id)
{
// always call this method to validate if admin is logged in
$this->di['is_admin_logged'];
$params = array();
$params['userid'] = $id;
return $app->render('mod_example_index', $params);
}
public function get_api(\Box_App $app, $id)
{
// always call this method to validate if admin is logged in
$api = $this->di['api_admin'];
$list_from_controller = $api->example_get_something();
$params = array();
$params['api_example'] = true;
$params['list_from_controller'] = $list_from_controller;
return $app->render('mod_example_index', $params);
}
}
-64
View File
@@ -1,64 +0,0 @@
<?php
/**
* BoxBilling
*
* @copyright BoxBilling, Inc (http://www.boxbilling.com)
* @license Apache-2.0
*
* Copyright BoxBilling, Inc
* This source file is subject to the Apache-2.0 License that is bundled
* with this source code in the file LICENSE
*/
/**
* This file connects BoxBilling client area interface and API
* Class does not extend any other class
*/
namespace Box\Mod\Example\Controller;
class Client implements \Box\InjectionAwareInterface
{
protected $di;
/**
* @param mixed $di
*/
public function setDi($di)
{
$this->di = $di;
}
/**
* @return mixed
*/
public function getDi()
{
return $this->di;
}
/**
* Methods maps client areas urls to corresponding methods
* Always use your module prefix to avoid conflicts with other modules
* in future
*
* @param \Box_App $app - returned by reference
*/
public function register(\Box_App &$app)
{
$app->get('/example', 'get_index', array(), get_class($this));
$app->get('/example/protected', 'get_protected', array(), get_class($this));
}
public function get_index(\Box_App $app)
{
return $app->render('mod_example_index');
}
public function get_protected(\Box_App $app)
{
// call $this->di['is_client_logged'] method to validate if client is logged in
$this->di['is_client_logged'];
return $app->render('mod_example_index', array('show_protected'=>true));
}
}
-38
View File
@@ -1,38 +0,0 @@
# Example extension readme file
Module purpose is to provide a starting point for developer to get started
creating his own BoxBilling module.
Explore the files and comments in the code to better understand the structure
of module. Contact Development helpdesk at www.boxbilling.com if you need more
information.
In general modules are used to extend BoxBilling basic functionality.
All modules can access other modules via API or database
More about extensions at https://extensions.boxbilling.com/article/getting-started
# BoxBilling module requirements
## Required
* Folder must contain **manifest.json** file to describe itself
## Optional
* **README.md** - file for installation and getting started instructions
* Folder **html_admin** - for admin area templates, to store custom *.phtml files
* Folder **html_client** - for client area templates, to store custom *.phtml files
###### Controller folder
* **Admin.php** - if module has install/uninstall instructions or
admin area interface
* **Client.php** - if module has client area interface
###### Api folder
* **Admin.php** - file for Admin API
* **Client.php** - file for Client API
* **Guest.php** - file for Guest API
# Tips
We recommend to host your extensions on public [github.com](http://github.com) repository
-251
View File
@@ -1,251 +0,0 @@
<?php
/**
* BoxBilling
*
* @copyright BoxBilling, Inc (http://www.boxbilling.com)
* @license Apache-2.0
*
* Copyright BoxBilling, Inc
* This source file is subject to the Apache-2.0 License that is bundled
* with this source code in the file LICENSE
*/
/**
* This file is a delegate for module. Class does not extend any other class
*
* All methods provided in this example are optional, but function names are
* still reserved.
*
*/
namespace Box\Mod\Example;
class Service
{
protected $di;
/**
* @param mixed $di
*/
public function setDi($di)
{
$this->di = $di;
}
/**
* Method to install module. In most cases you will provide your own
* database table or tables to store extension related data.
*
* If your extension is not very complicated then extension_meta
* database table might be enough.
*
* @return bool
* @throws \Box_Exception
*/
public function install()
{
// execute sql script if needed
$db = $this->di['db'];
$db->exec("SELECT NOW()");
//throw new \Box_Exception("Throw exception to terminate module installation process with a message", array(), 123);
return true;
}
/**
* Method to uninstall module.
*
* @return bool
* @throws \Box_Exception
*/
public function uninstall()
{
//throw new \Box_Exception("Throw exception to terminate module uninstallation process with a message", array(), 124);
return true;
}
/**
* Method to update module. When you release new version to
* extensions.boxbilling.com then this method will be called
* after new files are placed.
*
* @param array $manifest - information about new module version
* @return bool
* @throws \Box_Exception
*/
public function update($manifest)
{
//throw new \Box_Exception("Throw exception to terminate module update process with a message", array(), 125);
return true;
}
/**
* Method is used to create search query for paginated list.
* Usually there is one paginated list per module
*
* @param array $data
* @return array() = list of 2 parameters: array($sql, $params)
*/
public function getSearchQuery($data)
{
$params = array();
$sql="SELECT meta_key, meta_value
FROM extension_meta
WHERE extension = 'example' ";
$client_id = $this->di['array_get']($data, 'client_id', NULL);
if(NULL !== $client_id) {
$sql .= ' AND client_id = :client_id';
$params[':client_id'] = $client_id;
}
$sql .= ' ORDER BY created_at DESC';
return array($sql, $params);
}
/**
* Methods is a delegate for one database row.
*
* @param array $row - array representing one database row
* @param string $role - guest|client|admin who is calling this method
* @param bool $deep - true|false deep or light version of result to return to API
*
* @return array
*/
public function toApiArray($row, $role = 'guest', $deep = true)
{
return $row;
}
/**
* Example event hook. Any module can hook to any BoxBilling event and perform actions
*
* Make sure extension is enabled before testing this event.
*
* NOTE: IF you have BB_DEBUG mode set to TRUE then all events with params
* are logged to bb-data/log/hook_*.log file. Check this file to see what
* kind of parameters are passed to event.
*
* In this example we are going to count how many times client failed
* to enter correct login details
*
* @param \Box_Event $event
* @return void
*
* @throws \Box_Exception
*/
public static function onEventClientLoginFailed(\Box_Event $event)
{
//getting Dependency Injector
$di = $event->getDi();
//@note almost in all cases you will need Admin API
$api = $di['api_admin'];
//sometimes you may need guest API
//$api_guest = $di['api_guest'];
$params = $event->getParameters();
//@note To debug parameters by throwing an exception
//throw new Exception(print_r($params, 1));
// Use RedBean ORM in any place of BoxBilling where API call is not enough
// First we need to find if we already have a counter for this IP
// We will use extension_meta table to store this data.
$values = array(
'ext' => 'example',
'rel_type' => 'ip',
'rel_id' => $params['ip'],
'meta_key' => 'counter',
);
$meta = $di['db']->findOne('extension_meta', 'extension = :ext AND rel_type = :rel_type AND rel_id = :rel_id AND meta_key = :meta_key', $values);
if(!$meta) {
$meta = $di['db']->dispense('extension_meta');
//$count->client_id = null; // client id is not known in this situation
$meta->extension = 'mod_example';
$meta->rel_type = 'ip';
$meta->rel_id = $params['ip'];
$meta->meta_key = 'counter';
$meta->created_at = date('Y-m-d H:i:s');
}
$meta->meta_value = $meta->meta_value + 1;
$meta->updated_at = date('Y-m-d H:i:s');
$di['db']->store($meta);
// Now we can perform task depending on how many times wrong details were entered
// We can log event if it repeats for 2 time
if($meta->meta_value > 2) {
$api->activity_log(array('m' => 'Client failed to enter correct login details ' . $meta->meta_value . ' time(s)'));
}
// if client gets funky, we block him
if($meta->meta_value > 30) {
throw new \Box_Exception('You have failed to login too many times. Contact support.');
}
}
/**
* This event hook is registered in example module client API call
* @param \Box_Event $event
*/
public static function onAfterClientCalledExampleModule(\Box_Event $event)
{
//error_log('Called event from example module');
$di = $event->getDi();
$params = $event->getParameters();
$meta = $di['db']->dispense('extension_meta');
$meta->extension = 'mod_example';
$meta->meta_key = 'event_params';
$meta->meta_value = json_encode($params);
$meta->created_at = date('Y-m-d H:i:s');
$meta->updated_at = date('Y-m-d H:i:s');
$di['db']->store($meta);
}
/**
* Example event hook for public ticket and set event return value
* @param \Box_Event $event
*/
public static function onBeforeGuestPublicTicketOpen(\Box_Event $event)
{
/* Uncomment lines below in order to see this function in action */
/*
$data = $event->getParameters();
$data['status'] = 'closed';
$data['subject'] = 'Altered subject';
$data['message'] = 'Altered text';
$event->setReturnValue($data);
*/
}
/**
* Example email sending
* @param \Box_Event $event
*/
public static function onAfterClientOrderCreate(\Box_Event $event)
{
/* Uncomment lines below in order to see this function in action */
/*
$di = $event->getDi();
$api = $di['api_admin'];
$params = $event->getParameters();
$email = array();
$email['to_client'] = $params['client_id'];
$email['code'] = 'mod_example_email'; //@see bb-modules/Example/html_email/mod_example_email.phtml
// these parameters are available in email template
$email['order'] = $api->order_get(array('id'=>$params['id']));
$email['other'] = 'any other value';
$api->email_template_send($email);
*/
}
}
@@ -1,76 +0,0 @@
{% extends request.ajax ? "layout_blank.phtml" : "layout_default.phtml" %}
{% block meta_title %}{% trans 'Example module' %}{% endblock %}
{% set active_menu = 'example' %}
{% block content %}
<div class="widget">
<div class="head">
<h5>{% trans 'Example module title' %}</h5>
</div>
<div class="help">
<h3>{% trans 'This is an example extension content' %}</h3>
</div>
<div class="body">
<a href="{{ 'example'|alink }}">Example module index</a> <br/>
<a href="{{ 'extension/settings/example'|alink }}">Example settings page</a> <br/>
<a href="{{ 'example'|alink }}/test">Test link with static URL</a> <br/>
<a href="{{ 'example'|alink }}/user/1">Test link location with parameter 1</a> <br/>
<a href="{{ 'example'|alink }}/user/2">Test link location with parameter 2</a> <br/>
<a href="{{ 'example'|alink }}/user/3">Test link location with parameter 3</a> <br/>
<a href="{{ 'example'|alink }}/api">API example</a>
<a href="{{ 'example'|link }}" target="_blank">Client area link example</a>
</div>
{# Check if example parameters passed to template file #}
{% if youparamname %}
<div class="help">
<h3>{% trans 'Parameters from Controller' %}</h3>
</div>
<div class="body">
<p>You have passed parameter youparamname: <strong>{{ youparamname }}</strong></p>
</div>
{% endif %}
{# Check if example parameters passed to template file #}
{% if userid %}
<div class="help">
<h3>{% trans 'Parameters from URL' %}</h3>
</div>
<div class="body">
<p>You have passed parameter userid: <strong>{{ userid }}</strong></p>
</div>
{% endif %}
{% if api_example %}
{# API example #}
<div class="help">
<h3>{% trans 'API example' %}</h3>
</div>
<div class="body">
<h5 class="pt12">Data from API and passed to template from controller</h5>
<pre>{% debug list_from_controller %}</pre>
<h5 class="pt12">Data from API accessed directly from template file</h5>
{% set list = admin.example_get_something({"microsoft":1}) %}
<pre>{% debug list %}</pre>
</div>
{% endif %}
</div>
<div class="widget">
<div class="head">
<h5>{% trans 'README' %}</h5>
</div>
<div class="body list arrowBlue">
{{ guest.example_readme|markdown }}
</div>
</div>
{% endblock %}
@@ -1,53 +0,0 @@
{% extends request.ajax ? "layout_blank.phtml" : "layout_default.phtml" %}
{% import "macro_functions.phtml" as mf %}
{% block meta_title %}Example extension settings{% endblock %}
{% set active_menu = 'system' %}
{% block breadcrumbs %}
<ul>
<li class="firstB"><a href="{{ '/'|alink }}">{% trans 'Home' %}</a></li>
<li><a href="{{ 'system'|alink }}">{% trans 'Settings' %}</a></li>
<li class="lastB">{% trans 'Example module settings' %}</li>
</ul>
{% endblock %}
{% block content %}
<div class="widget">
<div class="head">
<h5 class="iCog">{% trans 'Example extension settings' %}</h5>
</div>
{% set params = admin.extension_config_get({"ext":"mod_example"}) %}
<form method="post" action="{{ 'api/admin/extension/config_save'|link }}" class="mainForm api-form" data-api-msg="{% trans 'Configuration updated' %}">
<div class="help">
<h3>{% trans 'Example extension settings' %}</h3>
<p>{% trans 'Every extension can have settings page. Only requirement is to have mod_example_settings.phtml page in html_admin folder' %}</p>
</div>
<fieldset>
<div class="rowElem noborder">
<label>{% trans 'Parameter title' %}</label>
<div class="formRight">
<input type="text" name="custom_param" value="{{params.custom_param}}" placeholder="{% trans 'Example custom value' %}"/>
</div>
<div class="fix"></div>
</div>
<div class="rowElem">
<label>{% trans 'Public Parameter title' %}</label>
<div class="formRight">
<input type="text" name="public[param]" value="{{params.public.param}}" placeholder="{% trans 'This setting will be accessible by Guest API' %}"/>
</div>
<div class="fix"></div>
</div>
<input type="submit" value="{% trans 'Update' %}" class="greyishBtn submitForm" />
<input type="hidden" name="ext" value="mod_example" />
</fieldset>
</form>
</div>
{% endblock %}
@@ -1,48 +0,0 @@
{% extends request.ajax ? "layout_blank.phtml" : "layout_default.phtml" %}
{% block meta_title %}{% trans 'Example module' %}{% endblock %}
{% block breadcrumb %} <li class="active">{% trans 'Example module' %}</li>{% endblock %}
{% block page_header %}
<article class="page-header">
<h1>{% trans 'Example module title' %}</h1>
</article>
{% endblock%}
{% block content %}
<div class="row">
<article class="span12 data-block">
<div class="data-container">
<header>
<h1>{% trans 'Example module title' %}</h1>
<p>{% trans 'Example module description' %}</p>
</header>
<section>
{{ guest.example_readme|markdown }}
{% if client %}
<a class="btn btn-primary" href="{{ 'example/protected'|link }}">{% trans 'See protected content' %}</a>
{% else %}
<a class="btn btn-primary" href="{{ 'login'|link }}">{% trans 'Login to client area' %}</a>
{% endif %}
</section>
</div>
</article>
</div>
{% if show_protected %}
<div class="row">
<article class="span12 data-block">
<div class="data-container">
<header>
<h2>{% trans 'This content can be seen only if client is logged in' %}</h2>
</header>
<section>
<p>{% trans 'Your protected content' %}</p>
<p><a class="btn btn-inverse" href="{{ 'client/me'|link }}">{% trans 'Manage profile' %}</a></p>
</section>
</div>
</article>
</div>
{% endif %}
{% endblock %}
@@ -1,11 +0,0 @@
{% block subject %}Example Email Subject{% endblock %}
{% block content %}
{% apply markdown %}
Hi {{ c.first_name }},
This is a test message from BoxBilling.com
{{ guest.system_company.signature }}
{% endapply %}
{% endblock %}
Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

-12
View File
@@ -1,12 +0,0 @@
{
"id": "example",
"type": "mod",
"name": "Example module",
"description": "Open source extension for BoxBilling developer to get started.",
"icon_url": "icon.png",
"homepage_url": "http://github.com/boxbilling/",
"author": "BoxBilling",
"author_url": "http://extensions.boxbilling.com/",
"license": "GPL version 2 or later - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html",
"version": "2.0.0"
}