Skip to content
This repository has been archived by the owner on Dec 1, 2017. It is now read-only.

Commit

Permalink
Completed load balancer system.
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabio Woxxy committed Aug 6, 2011
1 parent 7dcaa15 commit 746bb54
Show file tree
Hide file tree
Showing 15 changed files with 679 additions and 239 deletions.
2 changes: 1 addition & 1 deletion .htaccess
@@ -1,6 +1,6 @@
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|content|robots\.txt|favicon\.ico)
RewriteCond $1 !^(index\.php|assets|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
156 changes: 58 additions & 98 deletions application/controllers/admin/balancer.php
Expand Up @@ -67,82 +67,82 @@ function _submit($post, $form)
*
* @author Woxxy
*/
function balancers($id = NULL)
function balancers()
{
if (is_null($id))
{
$this->viewdata["function_title"] = _("Master");

// create a form
$balancers = new Loadbalancer();
$balancers->get();
$data["balancers"] = $balancers;

// print out
$this->viewdata["main_content_view"] = $this->load->view("admin/loadbalancer/balancers_list.php", $data, TRUE);
$this->load->view("admin/default.php", $this->viewdata);
}
else
{
$balancer = new Loadbalancer($id);
if($balancer->result_count() != 1)
{
show_404();
}
$data["balancer"] = $balancer;
$table = ormer($balancer);
$data["table"] = tabler($table, FALSE, TRUE, TRUE);


$this->viewdata["main_content_view"] = $this->load->view("admin/loadbalancer/balancer.php", $data, TRUE);
$this->load->view("admin/default.php", $this->viewdata);
}
}


function balancer_add()
{
$this->viewdata["function_title"] = _("Add new");

if ($this->input->post())
{
$loadbalancer = new Loadbalancer();
if (!$loadbalancer->from_array($this->input->post(), array('url', 'ip', 'key'), TRUE))
$result = array();
if ($urls = $this->input->post('url'))
{
if (!$loadbalancer->valid)
$priorities = $this->input->post('priority');
if (is_array($urls))
{
set_notice('error', _("The values you submitted aren't matching the requested. Check the fields."));
foreach ($urls as $key => $item)
{
if (!$item)
{
unset($urls[$key]);
break;
}
if ($priorities[$key] >= 0 && $priorities[$key] <= 100)
{
$result[] = array('url' => $item, 'priority' => $priorities[$key]);
}
}
}
$result = serialize($result);

$this->db->from('preferences');
$this->db->where(array('name' => 'fs_balancer_clients'));
if ($this->db->count_all_results() == 1)
{
$this->db->update('preferences', array('value' => $result), array('name' => 'fs_balancer_clients'));
}
else
{
$this->db->insert('preferences', array('name' => 'fs_balancer_clients', 'value' => $result));
}
set_notice('error', _("Couldn't create the new entry"));
}
else
if ($this->input->post('fs_balancer_ips'))
{
redirect('/admin/loadbalancer/balancers/' . $loadbalancer->id);
$result = serialize($this->input->post('fs_balancer_ips'));

$this->db->from('preferences');
$this->db->where(array('name' => 'fs_balancer_ips'));
if ($this->db->count_all_results() == 1)
{
$this->db->update('preferences', array('value' => $result), array('name' => 'fs_balancer_ips'));
}
else
{
$this->db->insert('preferences', array('name' => 'fs_balancer_ips', 'value' => $result));
}
}
}

$loadbalancer = new Loadbalancer();
$table = ormer($loadbalancer);
$data["table"] = tabler($table, FALSE, TRUE, TRUE);
load_settings();
}

$this->viewdata["main_content_view"] = $this->load->view("admin/loadbalancer/add_new.php", $data, TRUE);
$data["balancers"] = unserialize(get_setting('fs_balancer_clients'));
$data["ips"] = unserialize(get_setting('fs_balancer_ips'));
$this->viewdata["main_content_view"] = $this->load->view("admin/loadbalancer/balancers_list.php", $data, TRUE);
$this->load->view("admin/default.php", $this->viewdata);
}
function balancer_remove($id)


function _check_client($url)
{
if(!isAjax())
$result = @file_get_contents($url . '/api/status/status/format/json');
if (is_null($result))
{
show_404();
return array('error' => _("Unavailable"));
}
$balancer = new Loadbalancer($id);
if ($balancer->result_count() != 1)

$result = json_decode($result, TRUE);

if (isset($result["error"]))
{
show_404();

}
$balancer->delete();
echo json_encode(array('href' => site_url('/admin/balancer/balancers/')));
}


Expand Down Expand Up @@ -171,51 +171,11 @@ function client()
)
);

$form[] = array(
_('Security key'),
array(
'type' => 'input',
'name' => 'fs_balancer_master_key_on_client',
'id' => 'site_title',
'placeholder' => _('required'),
'preferences' => 'fs_gen',
'help' => _('Used to pair client with master')
)
);

$form[] = array(
_('Allow autoupdating'),
array(
'type' => 'checkbox',
'name' => 'fs_balancer_master_key',
'id' => 'site_title',
'placeholder' => _('required'),
'preferences' => 'fs_gen',
'help' => _('Updates itself to the same version as the master FoOlSlide.')
)
);

if ($post = $this->input->post())
{
$this->_submit($post, $form);
}

if (!get_setting('fs_balancer_client_key'))
{
$security_key = md5(time() . uniqid());
$this->_submit(array('fs_balancer_client_key' => $security_key), array(array("", array('name' => 'fs_balancer_client_key'))));
}

if ($post = $this->input->post())
{
$this->_submit($post, $form);
}

$form[] = array(
_('Security key'),
get_setting('fs_balancer_client_key')
);

// create a form
$table = tabler($form, FALSE);
$data['table'] = $table;
Expand Down
2 changes: 1 addition & 1 deletion application/controllers/admin/members.php
Expand Up @@ -19,7 +19,7 @@ function __construct()
*/
function index()
{
redirect('/admin/members/you');
redirect('/admin/members/members');
}


Expand Down
14 changes: 10 additions & 4 deletions application/controllers/admin/series.php
Expand Up @@ -9,7 +9,12 @@ function __construct()
{
parent::__construct();
if (!($this->tank_auth->is_allowed()))
redirect('admin');
redirect('account');

// if this is a load balancer, let's not allow people in the series tab
if (get_setting('fs_balancer_master_url'))
redirect('/admin/members');

$this->load->model('files_model');
$this->load->library('pagination');
$this->viewdata['controller_title'] = _("Series");
Expand Down Expand Up @@ -264,13 +269,14 @@ function add_new($stub = "")
function upload()
{
$info = array();

// compatibility for flash uploader and browser not supporting multiple upload
if(is_array($_FILES['Filedata']) && !is_array($_FILES['Filedata']['tmp_name'])) {
if (is_array($_FILES['Filedata']) && !is_array($_FILES['Filedata']['tmp_name']))
{
$_FILES['Filedata']['tmp_name'] = array($_FILES['Filedata']['tmp_name']);
$_FILES['Filedata']['name'] = array($_FILES['Filedata']['name']);
}

for ($file = 0; $file < count($_FILES['Filedata']['tmp_name']); $file++)
{
$valid = explode('|', 'png|zip|rar|gif|jpg|jpeg');
Expand Down
48 changes: 36 additions & 12 deletions application/controllers/api/reader.php
Expand Up @@ -26,7 +26,8 @@ function comics_get()
if ($comics->result_count() > 0)
{
$result = array();
foreach ($comics->all as $key => $comic) {
foreach ($comics->all as $key => $comic)
{
$result['comics'][$key] = $comic->to_array();
$result['comics'][$key]["thumb_url"] = $comic->get_thumb();
}
Expand All @@ -49,20 +50,32 @@ function comics_get()
*/
function comic_get()
{
// check that the id is at least a valid number
$this->_check_id();
if ($this->get('id'))
{
//// check that the id is at least a valid number
$this->_check_id();

// get the comic
$comic = new Comic();
$comic->where('id', $this->get('id'))->limit(1)->get();
// get the comic
$comic = new Comic();
$comic->where('id', $this->get('id'))->limit(1)->get();
}
else if ($this->get('uniqid') && $this->get('stub'))
{ // mostly used for load balancer
$comic = new Comic();
$comic->where('stub', $this->get('stub'))->where('uniqid', $this->get('uniqid'))->limit(1)->get();
}
else
{
$this->response(array('error' => _('You didn\'t use the necessary parameters')), 404);
}

if ($comic->result_count() == 1)
{
$chapters = new Chapter();
$chapters->where('comic_id', $comic->id)->get();
$chapters->get_teams();
$result = array();

$result["comic"] = $comic->to_array();
$result["comic"]["thumb_url"] = $comic->get_thumb();

Expand All @@ -73,6 +86,17 @@ function comic_get()
$result['chapters'][$key]['comic'] = $comic->to_array();
$result['chapters'][$key]['chapter'] = $chapter->to_array();

// if it's requested, throw in also the pages (for load balancer)
if ($this->get('chapter_stub') == $chapter->stub
&& $this->get('chapter_uniqid') == $chapter->uniqid)
{
$pages = new Page();
$pages->where('chapter_id', $chapter->id)->get();
$result["chapters"][$key]["chapter"]["pages"] = $pages->all_to_array();
}



// teams is a normal array, can't use $team->all_to_array()
foreach ($chapter->teams as $team)
{
Expand Down Expand Up @@ -128,7 +152,7 @@ function chapters_get()
$result['chapters'][$key]['teams'][] = $item->to_array();
}
}

// all good
$this->response($result, 200); // 200 being the HTTP response code
}
Expand Down Expand Up @@ -171,7 +195,7 @@ function chapter_get()
{
$result['teams'][] = $team->to_array();
}

// this time we get the pages
$result['pages'] = $chapter->get_pages();

Expand Down Expand Up @@ -224,7 +248,7 @@ function team_get()
// get also all chapters with the joints by the team
$chapters->or_where('joint_id', $joint->joint_id);
}

// filter for the page and the order
$this->_orderby($chapters);
$this->_page_to_offset($chapters);
Expand Down Expand Up @@ -299,7 +323,7 @@ function joint_get()
// grab all the chapters from the same joint
$chapters = new Chapter();
$chapters->where('joint_id', $joint->joint_id);

// apply the limit and orderby filters
$this->_orderby($chapters);
$this->_page_to_offset($chapters);
Expand All @@ -326,5 +350,5 @@ function joint_get()
}
}


}
8 changes: 2 additions & 6 deletions application/controllers/api/status.php
Expand Up @@ -4,17 +4,13 @@

class Status extends REST_Controller
{

function status_get()
{
$result = array();
$result["title"] = get_setting('fs_gen_site_title');
$result["version"] = get_setting('fs_priv_version');
$this->response($result, 200); // 200 being the HTTP response code
}

function cron_get()
{
$this->response(array('success' => _('Cron triggered')), 200);
}


}

0 comments on commit 746bb54

Please sign in to comment.