~george-edison55/jsstudio/addons_site

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php

//=======================================================================
//                            index.php
//                  Copyright (C) 2011 Nathan Osman
//
// This program 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.
//
// This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
//=======================================================================

// All controllers must derive from BaseController
require_once 'base_controller.php';

class JSStudioAddons
{
    private $controller = 'addons'; // the default controller
    private $action     = 'index';  // and the default action
    private $arguments  = array();
    
    private function FatalError($details)
    {
        header('Content-type: text/plain');
        echo "This web application has encountered a serious internal error.\n" .
             "Please try reloading the page or contact <admin@quickmediasolutions.com> if you continue to encounter this error.\n\n" .
             'Details: ' . $details;
        exit;
    }
    
    // Converts one_class to OneClass
    private function GetClassName($raw_name)
    {
        return str_replace(' ', '', ucwords(str_replace('_', ' ', $raw_name))) . 'Controller';
    }
    
    private function ParseURL()
    {
        // The original requested path is passed to us
        // in a $_GET variable as 'url'.
        $url_parts = explode('/', $_GET['url']);
        
        // Grab the parameters that were supplied
        if(count($url_parts) > 0 && $url_parts[0] != '')
        {
            $this->controller = $url_parts[0];
            if(count($url_parts) > 1 && $url_parts[1] != '')
                $this->action = $url_parts[1];
        }
        
        // Make sure that there were no bad characters in the
        // controller and the action.
        if(!preg_match('/^\w+$/', $this->controller) ||
           !preg_match('/^\w+$/', $this->action))
            $this->FatalError('The request URL is invalid.');
        
        // Now package the rest of the URL into $arguments
        $this->arguments = $url_parts;
        array_splice($this->arguments, 0, 2);
    }
    
    public function RunController()
    {
        // We have a controller name... check for the file's
        // existence in the controller's folder.
        $controller_filename = "controllers/{$this->controller}.php";
        if(!is_file($controller_filename))
            $this->FatalError("The '{$this->controller}' controller does not exist.");
        
        // Load the controller file
        require $controller_filename;
        
        // Now attempt to find the appropriate class in the controller
        $class_name = $this->GetClassName($this->controller);
        if(class_exists($class_name) === FALSE)
            $this->FatalError("The class '$class_name' does not exist within the file '$controller_filename'.");
        
        // Create an instance of the class
        $controller_instance = new $class_name($this->controller);
        
        // Make sure the class derives from BaseController
        if(!$controller_instance instanceof BaseController)
            $this->FatalError("The class '$class_name' must derive from BaseController.");
        
        // Make sure the specified method exists within the class
        $method_name = $this->action;
        if(method_exists($class_name, $method_name) === FALSE)
            $this->FatalError("The class '$class_name' does not contain a method for the action '$method_name'.");
        
        // Okay, now invoke the method
        call_user_func_array(array($controller_instance, $method_name),
                             $this->arguments);
        
        // Return the view variables that were created by the controller
        return $controller_instance->GetViewVariables();
    }
    
    private function DisplayView($view_variables)
    {
        // Make sure that the view exists
        $view_filename = "views/{$this->controller}/{$this->action}.inc";
        if(!is_file($view_filename))
            $this->FatalError("The view for the action {$this->action} is missing.");
        
        // Now we need to import all of the variables
        // in $view_variables into the local scope.
        foreach($view_variables as $key =>$value)
            $$key = $value;
        
        // Now run the view
        require_once $view_filename;
    }
    
    public function Go()
    {
        // Start by parsing the URL
        $this->ParseURL();
        
        // These two functions are wrapped in a try
        // block so that we can display any exceptions
        // that occur to the user
        try
        {
            // Now run the selected controller
            $view_vars = $this->RunController();
            
            // ...and lastly display the view
            $this->DisplayView($view_vars);
        }
        catch(Exception $e)
        {
            $this->FatalError($e->getMessage());
        }
    }
}

$addons = new JSStudioAddons();
$addons->Go();

?>