upload
[pear] / HTML / FlexyFramework / JsonPage.php
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4: */
3 // +----------------------------------------------------------------------+
4 // | PHP Version 4                                                        |
5 // +----------------------------------------------------------------------+
6 // | Copyright (c) 1997-2002 The PHP Group                                |
7 // +----------------------------------------------------------------------+
8 // | This source file is subject to version 2.02 of the PHP license,      |
9 // | that is bundled with this package in the file LICENSE, and is        |
10 // | available at through the world-wide-web at                           |
11 // | http://www.php.net/license/2_02.txt.                                 |
12 // | If you did not receive a copy of the PHP license and are unable to   |
13 // | obtain it through the world-wide-web, please send a note to          |
14 // | license@php.net so we can mail you a copy immediately.               |
15 // +----------------------------------------------------------------------+
16 // | Authors:  Alan Knowles <alan@akbkhome.com>                           |
17 // +----------------------------------------------------------------------+
18 //
19 // $Id: Page.php,v 1.5 2003/02/22 01:52:50 alan Exp $
20 //
21 // A Base Page Class for use with HTML_Template_Flexy 
22 // You could write one of these which used another template engine.
23 //
24
25
26
27 /**
28 * The Base Page class - extend and override the methods to implement your own pages.
29 *
30 *
31 */
32
33 class HTML_FlexyFramework_JsonPage  {
34     
35     
36      
37     
38     /* ---- Variables set by Page loader -------- */
39     
40    
41         
42     /**
43     * baseURL, that can be prefixed to URL's to ensure that they correctly relate to application
44     * (set by page loader)
45     * @var string
46     * @access public
47     */
48     var $baseURL; 
49     /**
50     * rootURL, the base installation directory - can be used to get images directories.
51     * (set by page loader)
52     * @var string
53     * @access public
54     */
55     var $rootURL; 
56  
57     /**
58     * the full request string used by the getCacheID().
59     * (set by page loader)
60     * @var string
61     * @access public
62     */
63     var $request; // clean page request for page
64      /**
65     * Authentication Check method
66     * Override this with a straight return for pages that do not require authentication
67     *
68     * By default 
69     *   a) redirects to login if authenticaiton fails
70     *   b) checks to see if a isAdmin method exists on the auth object
71     *       if it does see if the user is admin and let them in.
72     *       otherwise access denied error is raised
73     *   c) lets them in.
74     * 
75     * 
76     *
77     * @return   none or string redirection to another page.
78     * @access   public
79     */
80  
81     function getAuth() {
82          
83         
84         return false;
85         
86     }
87      
88     /**
89     * The default page handler
90     * by default relays to get(), or post() methods depending on the request.
91     *
92     * Override this if you do not handle get or post differently.
93     * 
94     * 
95     * @param   string  request, the remainder of the request not handled by the object.
96     *
97     * @return   none|string none = handled, string = redirect to another page = eg. data/list
98     * @access   public
99     */
100   
101     function start($request,$isRedirect=false,$args=array()) 
102     { 
103         if (!$isRedirect && !empty($_SERVER["REQUEST_METHOD"]) && $_SERVER["REQUEST_METHOD"] == "POST") {
104             return $this->post($request,$args);
105         } else {
106             return $this->get($request,$args);
107         }
108     }
109     /**
110     * The get page handler
111     *
112     * Override this if you want to handle get requests 
113     * 
114     * 
115     * @param   string  request, the remainder of the request not handled by the object.
116     *
117     * @return   none|string none = handled, string = redirect to another page = eg. data/list
118     * @access   public
119     */
120     function get($request) 
121     {
122     }
123      /**
124     * The post page handler
125     *
126     * Override this if you want to handle get requests 
127     * 
128     * 
129     * @param   string  request, the remainder of the request not handled by the object.
130     *
131     * @return   none|string none = handled, string = redirect to another page = eg. data/list
132     * @access   public
133     */
134    function post($request) 
135    {
136    }
137    
138    
139     function jerr($str, $errors=array()) // standard error reporting..
140     {
141         
142         if (isset($_SERVER['CONTENT_TYPE']) && preg_match('#multipart/form-data#i', $_SERVER['CONTENT_TYPE'])) {
143             header('Content-type: text/html');
144             echo "<HTML><HEAD></HEAD><BODY>";
145             echo  json_encode(array(
146                     'success'=> false, 
147                     'errorMsg' => $str,
148                      'message' => $str, // compate with exeption / loadexception.
149
150                     'errors' => $errors ? $errors : true, // used by forms to flag errors.
151                     'authFailure' => !empty($errors['authFailure']),
152                 ));
153             echo "</BODY></HTML>";
154             exit;
155         }
156         header('Content-type: application/json');
157         echo json_encode(array(
158             'success'=> false, 
159             'data'=> array(), 
160             'errorMsg' => $str,
161             'message' => $str, // compate with exeption / loadexception.
162             'errors' => $errors ? $errors : true, // used by forms to flag errors.
163             'authFailure' => !empty($errors['authFailure']),
164         ));
165         exit;
166         
167     }
168     function jok($str)
169     {
170         
171         
172         if (isset($_SERVER['CONTENT_TYPE']) && preg_match('#multipart/form-data#i', $_SERVER['CONTENT_TYPE'])) {
173             header('Content-type: text/html');
174             echo "<HTML><HEAD></HEAD><BODY>";
175             echo  json_encode(array('success'=> true, 'data' => $str));
176             echo "</BODY></HTML>";
177             exit;
178         }
179         header('Content-type: application/json');
180         echo json_encode(array('success'=> true, 'data' => $str));
181         exit;
182         
183     }
184     function jdata($ar,$total=false, $extra=array())
185     {
186         
187         if ($total == false) {
188             $total = count($ar);
189         }
190         $extra=  $extra ? $extra : array();
191         header('Content-type: application/json');
192         echo json_enocde(array('success' =>  true, 'total'=> $total, 'data' => $ar) + $extra);
193         exit;
194         
195     }
196     
197     
198    
199    
200     
201      
202     /**
203     * The master Output layer.
204     * 
205     * compiles the template
206     * if no caching - just runs the template.
207     * otherwise stores it in the cache.
208     * 
209     * you dont normally need to override this.
210     * 
211     * called by the page loader.
212     * @access   public
213     */
214     
215     
216     
217     function output() 
218     {
219         $this->jerr("Output staged reached - you should output in get/post");
220         
221     }
222       
223     
224 }
225
226