Advertisement
badlogic

API Cashing

Aug 22nd, 2016
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.83 KB | None | 0 0
  1. Creating cashing API
  2.  
  3. API using curl
  4.  
  5. /**
  6. *
  7. * Gets url param from POST / AJAX param
  8. * url: should be a valid URL
  9. */
  10. public function state()
  11. {
  12.  
  13.    if ($_SERVER['REQUEST_METHOD'] === 'POST' && empty($_POST)) {
  14.        $_POST = json_decode(file_get_contents('php://input'), true);
  15.    }
  16.  
  17.    $url = array_var($_POST, 'url');
  18.  
  19.    $response = array(
  20.        'url' => $url
  21.    );
  22.  
  23.    if ( $url && filter_var($url, FILTER_VALIDATE_URL) ) {
  24.        $ch = curl_init();
  25.        curl_setopt($ch, CURLOPT_URL, $url);
  26.        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  27.        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
  28.        curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  29.        curl_exec($ch);
  30.  
  31.        $response = curl_getinfo($ch);
  32.  
  33.        curl_close($ch);
  34.    }
  35.  
  36.    $this->response($response);
  37.  
  38. }
  39.  
  40. // servers
  41. function caching_servers()
  42. {
  43.    $siteTools = $GLOBALS['VoipServers'];
  44.  
  45.    if (SERVER === 'adhoc' || SERVER === 'preview') {
  46.        $siteTools = array(
  47.            'site-tools.adhoc'
  48.        );
  49.    } else if (SERVER === 'development') {
  50.        $siteTools = array(
  51.            'site-tools.development'
  52.        );
  53.    }
  54.  
  55.    tpl_assign('voipServers', $siteTools);
  56. }
  57.  
  58.  
  59. views
  60.  
  61. <div class="content-box">
  62.    <div class="content-box-header"><h3>Caching servers</h3></div>
  63.    <div class="content-box-content">
  64.        <table id="site-table" class="default">
  65.  
  66.            <thead>
  67.                <tr>
  68.  
  69.                    <th>IP Address</th>
  70.                    <th>APC</th>
  71.                    <th>Status</th>
  72.                    <th>Memcache Link</th>
  73.  
  74.                </tr>
  75.            </thead>
  76.  
  77.            <tbody>
  78.                <?php foreach($voipServers as $index => $server): ?>
  79.                    <tr>
  80.                        <td>
  81.                            <?php echo link_to($server, 'http://' . $server, array('target' => '_blank')); ?>
  82.                        </td>
  83.                        <td>
  84.                            <?php echo link_to('APC Info', 'http://' . $server . '/apc/index.php', array('class' => 'button', 'target' => '_blank')); ?>
  85.                        </td>
  86.                        <td>
  87.                            <span data-server-url="<?php echo 'http://' . $server; ?>" class="server-state" id="server-state-<?php echo $index; ?>"></span>
  88.                        </td>
  89.                        <td>
  90.                            <?php echo link_to('Memcache Info', 'http://' . $server . '/memcache/index.php', array('class' => 'button', 'target' => '_blank')); ?>
  91.                        </td>
  92.                    </tr>
  93.                <?php endforeach; ?>
  94.                <tr>
  95.                    <td>&nbsp;</td>
  96.                    <td>
  97.                        <?php echo link_to('Clear All User', '#', array('class' => 'button clear_user_server', 'data-servers' => json_encode($voipServers))); ?>
  98.                        <?php echo link_to('Clear All System', '#', array('class' => 'button clear_system_server', 'data-servers' => json_encode($voipServers))); ?>
  99.                        <?php echo link_to('Clear All', '#', array('class' => 'button clear_all_server', 'data-servers' => json_encode($voipServers))); ?>
  100.                    </td>
  101.                    <td>
  102.                        <button id="check-server-status" class="button">Check</button>
  103.                    </td>
  104.                    <td>&nbsp;</td>
  105.                </tr>
  106.            </tbody>
  107.  
  108.        </table>
  109.  
  110.    </div>
  111. </div>
  112.  
  113.  
  114. <?php if(false): ?> <script> <?php endif; ?>
  115.  
  116. <?php
  117.    ob_start();
  118. ?>
  119.  
  120.    $('.clear_user_server').click(function() {
  121.        deleteApc($(this).data('servers'), 'user', $(this));
  122.    });
  123.  
  124.    $('.clear_system_server').click(function() {
  125.        deleteApc($(this).data('servers'), 'system', $(this));
  126.    });
  127.  
  128.    $('.clear_all_server').click(function() {
  129.        deleteApc($(this).data('servers'), 'all', $(this));
  130.    });
  131.  
  132.    $('#check-server-status').click(function(){
  133.  
  134.        function setStateInitial(elem) {
  135.            elem.attr('class', 'server-state');
  136.        }
  137.  
  138.        function setStateRequesting(elem) {
  139.            setStateInitial(elem);
  140.            elem.addClass('server-state-requesting');
  141.        }
  142.  
  143.        function setStateSuccess(elem) {
  144.            setStateInitial(elem);
  145.            elem.addClass('server-state-success');
  146.        }
  147.  
  148.        function setStateError(elem) {
  149.            setStateInitial(elem);
  150.            elem.addClass('server-state-error');
  151.        }
  152.  
  153.        $('.server-state').each(function(i, obj) {
  154.            var elem = $(obj);
  155.            var url = elem.attr('data-server-url');
  156.  
  157.            $.ajax({
  158.                url: '/api/caching/state',
  159.                type: 'POST',
  160.                data: {
  161.                    'url': url
  162.                },
  163.                beforeSend: function() {
  164.                    // make icon yellow
  165.                    setStateRequesting(elem);
  166.                },
  167.                success: function(response) {
  168.                    // make icon green or red depending on http_code
  169.                    console.log(response.url + ':' + response.http_code);
  170.  
  171.                    if ( response.http_code == 200 ) {
  172.                        setStateSuccess(elem);
  173.                    } else {
  174.                        setStateError(elem);
  175.                    }
  176.                },
  177.                error: function(error) {
  178.                    console.error(error);
  179.                    setStateError(elem);
  180.                }
  181.            });
  182.        });
  183.    });
  184.  
  185.    var deleteApc = function(servers, type, button) {
  186.        var errorButton = function() {
  187.            button.html('Error');
  188.            button.attr('style', 'background: red!important');
  189.        }
  190.  
  191.        var origButton = function(origText) {
  192.            button.html(origText);
  193.            button.removeAttr('disabled');
  194.        }
  195.  
  196.        if (!button.is('[disabled]')) {
  197.  
  198.            var origText = button.html();
  199.            button.attr("disabled", "disabled");
  200.            button.html('Deleting...');
  201.  
  202.            $.ajax({
  203.                url : '/api/caching/delete_apc',
  204.                type: 'POST',
  205.                data : {'type': type, 'servers': servers},
  206.                success: function(data) {
  207.  
  208.                    var res = data;
  209.  
  210.                    if (res.status === 'success') {
  211.                        button.html('Success');
  212.                    } else {
  213.                        errorButton();
  214.                    }
  215.  
  216.                    setTimeout(function() {
  217.                        origButton(origText);
  218.                    }, 5000);
  219.                },
  220.                error: function () {
  221.                    errorButton();
  222.  
  223.                    setTimeout(function() {
  224.                        origButton(origText);
  225.                    }, 5000);
  226.                }
  227.            });
  228.        }
  229.    }
  230.  
  231. <?php
  232.    $js = ob_get_clean();
  233. ?>
  234.  
  235. <?php if(false): ?> </script> <?php endif; ?>
  236.  
  237. <?php add_inline_javascript_to_page($js); ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement