Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Creating cashing API
- API using curl
- /**
- *
- * Gets url param from POST / AJAX param
- * url: should be a valid URL
- */
- public function state()
- {
- if ($_SERVER['REQUEST_METHOD'] === 'POST' && empty($_POST)) {
- $_POST = json_decode(file_get_contents('php://input'), true);
- }
- $url = array_var($_POST, 'url');
- $response = array(
- 'url' => $url
- );
- if ( $url && filter_var($url, FILTER_VALIDATE_URL) ) {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
- curl_setopt($ch, CURLOPT_TIMEOUT, 5);
- curl_exec($ch);
- $response = curl_getinfo($ch);
- curl_close($ch);
- }
- $this->response($response);
- }
- // servers
- function caching_servers()
- {
- $siteTools = $GLOBALS['VoipServers'];
- if (SERVER === 'adhoc' || SERVER === 'preview') {
- $siteTools = array(
- 'site-tools.adhoc'
- );
- } else if (SERVER === 'development') {
- $siteTools = array(
- 'site-tools.development'
- );
- }
- tpl_assign('voipServers', $siteTools);
- }
- views
- <div class="content-box">
- <div class="content-box-header"><h3>Caching servers</h3></div>
- <div class="content-box-content">
- <table id="site-table" class="default">
- <thead>
- <tr>
- <th>IP Address</th>
- <th>APC</th>
- <th>Status</th>
- <th>Memcache Link</th>
- </tr>
- </thead>
- <tbody>
- <?php foreach($voipServers as $index => $server): ?>
- <tr>
- <td>
- <?php echo link_to($server, 'http://' . $server, array('target' => '_blank')); ?>
- </td>
- <td>
- <?php echo link_to('APC Info', 'http://' . $server . '/apc/index.php', array('class' => 'button', 'target' => '_blank')); ?>
- </td>
- <td>
- <span data-server-url="<?php echo 'http://' . $server; ?>" class="server-state" id="server-state-<?php echo $index; ?>"></span>
- </td>
- <td>
- <?php echo link_to('Memcache Info', 'http://' . $server . '/memcache/index.php', array('class' => 'button', 'target' => '_blank')); ?>
- </td>
- </tr>
- <?php endforeach; ?>
- <tr>
- <td> </td>
- <td>
- <?php echo link_to('Clear All User', '#', array('class' => 'button clear_user_server', 'data-servers' => json_encode($voipServers))); ?>
- <?php echo link_to('Clear All System', '#', array('class' => 'button clear_system_server', 'data-servers' => json_encode($voipServers))); ?>
- <?php echo link_to('Clear All', '#', array('class' => 'button clear_all_server', 'data-servers' => json_encode($voipServers))); ?>
- </td>
- <td>
- <button id="check-server-status" class="button">Check</button>
- </td>
- <td> </td>
- </tr>
- </tbody>
- </table>
- </div>
- </div>
- <?php if(false): ?> <script> <?php endif; ?>
- <?php
- ob_start();
- ?>
- $('.clear_user_server').click(function() {
- deleteApc($(this).data('servers'), 'user', $(this));
- });
- $('.clear_system_server').click(function() {
- deleteApc($(this).data('servers'), 'system', $(this));
- });
- $('.clear_all_server').click(function() {
- deleteApc($(this).data('servers'), 'all', $(this));
- });
- $('#check-server-status').click(function(){
- function setStateInitial(elem) {
- elem.attr('class', 'server-state');
- }
- function setStateRequesting(elem) {
- setStateInitial(elem);
- elem.addClass('server-state-requesting');
- }
- function setStateSuccess(elem) {
- setStateInitial(elem);
- elem.addClass('server-state-success');
- }
- function setStateError(elem) {
- setStateInitial(elem);
- elem.addClass('server-state-error');
- }
- $('.server-state').each(function(i, obj) {
- var elem = $(obj);
- var url = elem.attr('data-server-url');
- $.ajax({
- url: '/api/caching/state',
- type: 'POST',
- data: {
- 'url': url
- },
- beforeSend: function() {
- // make icon yellow
- setStateRequesting(elem);
- },
- success: function(response) {
- // make icon green or red depending on http_code
- console.log(response.url + ':' + response.http_code);
- if ( response.http_code == 200 ) {
- setStateSuccess(elem);
- } else {
- setStateError(elem);
- }
- },
- error: function(error) {
- console.error(error);
- setStateError(elem);
- }
- });
- });
- });
- var deleteApc = function(servers, type, button) {
- var errorButton = function() {
- button.html('Error');
- button.attr('style', 'background: red!important');
- }
- var origButton = function(origText) {
- button.html(origText);
- button.removeAttr('disabled');
- }
- if (!button.is('[disabled]')) {
- var origText = button.html();
- button.attr("disabled", "disabled");
- button.html('Deleting...');
- $.ajax({
- url : '/api/caching/delete_apc',
- type: 'POST',
- data : {'type': type, 'servers': servers},
- success: function(data) {
- var res = data;
- if (res.status === 'success') {
- button.html('Success');
- } else {
- errorButton();
- }
- setTimeout(function() {
- origButton(origText);
- }, 5000);
- },
- error: function () {
- errorButton();
- setTimeout(function() {
- origButton(origText);
- }, 5000);
- }
- });
- }
- }
- <?php
- $js = ob_get_clean();
- ?>
- <?php if(false): ?> </script> <?php endif; ?>
- <?php add_inline_javascript_to_page($js); ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement