1
|
|
|
<?php defined('SYSPATH') or die('No direct access allowed.'); |
2
|
|
|
/** |
3
|
|
|
* Contains examples of various Kohana library examples. You can access these |
4
|
|
|
* samples in your own installation of Kohana by going to ROOT_URL/examples. |
5
|
|
|
* This controller should NOT be used in production. It is for demonstration |
6
|
|
|
* purposes only! |
7
|
|
|
* |
8
|
|
|
* $Id: examples.php 4298 2009-04-30 17:06:05Z kiall $ |
9
|
|
|
* |
10
|
|
|
* @package Core |
11
|
|
|
* @author Kohana Team |
12
|
|
|
* @copyright (c) 2007-2008 Kohana Team |
13
|
|
|
* @license http://kohanaphp.com/license.html |
14
|
|
|
*/ |
15
|
|
|
class Examples_Controller extends Controller |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
// Do not allow to run in production |
19
|
|
|
const ALLOW_PRODUCTION = false; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Displays a list of available examples |
23
|
|
|
*/ |
24
|
|
|
public function index() |
25
|
|
|
{ |
26
|
|
|
// Get the methods that are only in this class and not the parent class. |
27
|
|
|
$examples = array_diff( |
28
|
|
|
get_class_methods(__CLASS__), |
29
|
|
|
get_class_methods(get_parent_class($this)) |
30
|
|
|
); |
31
|
|
|
|
32
|
|
|
sort($examples); |
33
|
|
|
|
34
|
|
|
echo "<strong>Examples:</strong>\n"; |
35
|
|
|
echo "<ul>\n"; |
36
|
|
|
|
37
|
|
|
foreach ($examples as $method) { |
38
|
|
|
if ($method == __FUNCTION__) { |
39
|
|
|
continue; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
echo '<li>'.html::anchor('examples/'.$method, $method)."</li>\n"; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
echo "</ul>\n"; |
46
|
|
|
echo '<p>'.Kohana::lang('core.stats_footer')."</p>\n"; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Demonstrates how to archive a directory. First enable the archive module |
51
|
|
|
*/ |
52
|
|
|
//public function archive($build = FALSE) |
53
|
|
|
//{ |
54
|
|
|
// if ($build === 'build') |
55
|
|
|
// { |
56
|
|
|
// // Load archive |
57
|
|
|
// $archive = new Archive('zip'); |
58
|
|
|
|
59
|
|
|
// // Download the application/views directory |
60
|
|
|
// $archive->add(APPPATH.'views/', 'app_views/', TRUE); |
61
|
|
|
|
62
|
|
|
// // Download the built archive |
63
|
|
|
// $archive->download('test.zip'); |
64
|
|
|
// } |
65
|
|
|
// else |
66
|
|
|
// { |
67
|
|
|
// echo html::anchor(Router::$current_uri.'/build', 'Download views'); |
68
|
|
|
// } |
69
|
|
|
//} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Demonstrates how to parse RSS feeds by using DOMDocument. |
73
|
|
|
*/ |
74
|
|
|
public function rss() |
75
|
|
|
{ |
76
|
|
|
// Parse an external atom feed |
77
|
|
|
$feed = feed::parse('http://dev.kohanaphp.com/projects/kohana2/activity.atom'); |
78
|
|
|
|
79
|
|
|
// Show debug info |
80
|
|
|
echo Kohana::debug($feed); |
81
|
|
|
|
82
|
|
|
echo Kohana::lang('core.stats_footer'); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Demonstrates the Session library and using session data. |
87
|
|
|
*/ |
88
|
|
|
public function session() |
|
|
|
|
89
|
|
|
{ |
90
|
|
|
// Gets the singleton instance of the Session library |
91
|
|
|
$s = Session::instance(); |
|
|
|
|
92
|
|
|
|
93
|
|
|
echo 'SESSID: <pre>'.session_id()."</pre>\n"; |
94
|
|
|
|
95
|
|
|
echo '<pre>'.print_r($_SESSION, true)."</pre>\n"; |
96
|
|
|
|
97
|
|
|
echo '<br/>{execution_time} seconds'; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Demonstrates how to use the form helper with the Validation libraryfor file uploads . |
102
|
|
|
*/ |
103
|
|
|
public function form() |
|
|
|
|
104
|
|
|
{ |
105
|
|
|
// Anything submitted? |
106
|
|
|
if ($_POST) { |
107
|
|
|
// Merge the globals into our validation object. |
108
|
|
|
$post = Validation::factory(array_merge($_POST, $_FILES)); |
109
|
|
|
|
110
|
|
|
// Ensure upload helper is correctly configured, config/upload.php contains default entries. |
111
|
|
|
// Uploads can be required or optional, but should be valid |
112
|
|
|
$post->add_rules('imageup1', 'upload::required', 'upload::valid', 'upload::type[gif,jpg,png]', 'upload::size[1M]'); |
113
|
|
|
$post->add_rules('imageup2', 'upload::required', 'upload::valid', 'upload::type[gif,jpg,png]', 'upload::size[1M]'); |
114
|
|
|
|
115
|
|
|
// Alternative syntax for multiple file upload validation rules |
116
|
|
|
//$post->add_rules('imageup.*', 'upload::required', 'upload::valid', 'upload::type[gif,jpg,png]', 'upload::size[1M]'); |
117
|
|
|
|
118
|
|
|
if ($post->validate()) { |
119
|
|
|
// It worked! |
120
|
|
|
// Move (and rename) the files from php upload folder to configured application folder |
121
|
|
|
upload::save('imageup1'); |
122
|
|
|
upload::save('imageup2'); |
123
|
|
|
echo 'Validation successfull, check your upload folder!'; |
124
|
|
|
} else { |
125
|
|
|
// You got validation errors |
126
|
|
|
echo '<p>validation errors: '.var_export($post->errors(), true).'</p>'; |
127
|
|
|
echo Kohana::debug($post); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
// Display the form |
132
|
|
|
echo form::open('examples/form', array('enctype' => 'multipart/form-data')); |
133
|
|
|
echo form::label('imageup', 'Image Uploads').':<br/>'; |
134
|
|
|
// Use discrete upload fields |
135
|
|
|
// Alternative syntax for multiple file uploads |
136
|
|
|
// echo form::upload('imageup[]').'<br/>'; |
137
|
|
|
|
138
|
|
|
echo form::upload('imageup1').'<br/>'; |
139
|
|
|
echo form::upload('imageup2').'<br/>'; |
140
|
|
|
echo form::submit('upload', 'Upload!'); |
141
|
|
|
echo form::close(); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Demontrates how to use the Validation library to validate an arbitrary array. |
146
|
|
|
*/ |
147
|
|
|
public function validation() |
148
|
|
|
{ |
149
|
|
|
// To demonstrate Validation being able to validate any array, I will |
150
|
|
|
// be using a pre-built array. When you load validation with no arguments |
151
|
|
|
// it will default to validating the POST array. |
152
|
|
|
$data = array( |
153
|
|
|
'user' => 'hello', |
154
|
|
|
'pass' => 'bigsecret', |
155
|
|
|
'reme' => '1' |
156
|
|
|
); |
157
|
|
|
|
158
|
|
|
$validation = new Validation($data); |
159
|
|
|
|
160
|
|
|
$validation->add_rules('user', 'required', 'length[1,12]')->pre_filter('trim', 'user'); |
161
|
|
|
$validation->add_rules('pass', 'required')->post_filter('sha1', 'pass'); |
162
|
|
|
$validation->add_rules('reme', 'required'); |
163
|
|
|
|
164
|
|
|
$result = $validation->validate(); |
|
|
|
|
165
|
|
|
|
166
|
|
|
var_dump($validation->errors()); |
|
|
|
|
167
|
|
|
var_dump($validation->as_array()); |
168
|
|
|
|
169
|
|
|
// Yay! |
170
|
|
|
echo '{execution_time} ALL DONE!'; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Demontrates how to use the Captcha library. |
175
|
|
|
*/ |
176
|
|
|
public function captcha() |
|
|
|
|
177
|
|
|
{ |
178
|
|
|
// Look at the counters for valid and invalid |
179
|
|
|
// responses in the Session Profiler. |
180
|
|
|
new Profiler; |
181
|
|
|
|
182
|
|
|
// Load Captcha library, you can supply the name |
183
|
|
|
// of the config group you would like to use. |
184
|
|
|
$captcha = new Captcha; |
185
|
|
|
|
186
|
|
|
// Ban bots (that accept session cookies) after 50 invalid responses. |
187
|
|
|
// Be careful not to ban real people though! Set the threshold high enough. |
188
|
|
|
if ($captcha->invalid_count() > 49) { |
189
|
|
|
exit('Bye! Stupid bot.'); |
|
|
|
|
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
// Form submitted |
193
|
|
|
if ($_POST) { |
194
|
|
|
// Captcha::valid() is a static method that can be used as a Validation rule also. |
195
|
|
|
if (Captcha::valid($this->input->post('captcha_response'))) { |
|
|
|
|
196
|
|
|
echo '<p style="color:green">Good answer!</p>'; |
197
|
|
|
} else { |
198
|
|
|
echo '<p style="color:red">Wrong answer!</p>'; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
// Validate other fields here |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
// Show form |
205
|
|
|
echo form::open(); |
206
|
|
|
echo '<p>Other form fields here...</p>'; |
207
|
|
|
|
208
|
|
|
// Don't show Captcha anymore after the user has given enough valid |
209
|
|
|
// responses. The "enough" count is set in the captcha config. |
210
|
|
|
if (! $captcha->promoted()) { |
211
|
|
|
echo '<p>'; |
212
|
|
|
echo $captcha->render(); // Shows the Captcha challenge (image/riddle/etc) |
213
|
|
|
echo '</p>'; |
214
|
|
|
echo form::input('captcha_response'); |
215
|
|
|
} else { |
216
|
|
|
echo '<p>You have been promoted to human.</p>'; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
// Close form |
220
|
|
|
echo form::submit(array('value' => 'Check')); |
221
|
|
|
echo form::close(); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Demonstrates the features of the Database library. |
226
|
|
|
* |
227
|
|
|
* Table Structure: |
228
|
|
|
* CREATE TABLE `pages` ( |
229
|
|
|
* `id` mediumint( 9 ) NOT NULL AUTO_INCREMENT , |
230
|
|
|
* `page_name` varchar( 100 ) NOT NULL , |
231
|
|
|
* `title` varchar( 255 ) NOT NULL , |
232
|
|
|
* `content` longtext NOT NULL , |
233
|
|
|
* `menu` tinyint( 1 ) NOT NULL default '0', |
234
|
|
|
* `filename` varchar( 255 ) NOT NULL , |
235
|
|
|
* `order` mediumint( 9 ) NOT NULL , |
236
|
|
|
* `date` int( 11 ) NOT NULL , |
237
|
|
|
* `child_of` mediumint( 9 ) NOT NULL default '0', |
238
|
|
|
* PRIMARY KEY ( `id` ) , |
239
|
|
|
* UNIQUE KEY `filename` ( `filename` ) |
240
|
|
|
* ) ENGINE = MYISAM DEFAULT CHARSET = utf8 PACK_KEYS =0; |
241
|
|
|
* |
242
|
|
|
*/ |
243
|
|
|
public function database() |
244
|
|
|
{ |
245
|
|
|
$db = new Database; |
|
|
|
|
246
|
|
|
|
247
|
|
|
$table = 'pages'; |
248
|
|
|
echo 'Does the '.$table.' table exist? '; |
249
|
|
|
if ($db->table_exists($table)) { |
250
|
|
|
echo '<p>YES! Lets do some work =)</p>'; |
251
|
|
|
|
252
|
|
|
$query = $db->select('DISTINCT pages.*')->from($table)->get(); |
253
|
|
|
echo $db->last_query(); |
254
|
|
|
echo '<h3>Iterate through the result:</h3>'; |
255
|
|
|
foreach ($query as $item) { |
256
|
|
|
echo '<p>'.$item->title.'</p>'; |
257
|
|
|
} |
258
|
|
|
echo '<h3>Numrows using count(): '.count($query).'</h3>'; |
259
|
|
|
echo 'Table Listing:<pre>'.print_r($db->list_tables(), true).'</pre>'; |
260
|
|
|
|
261
|
|
|
echo '<h3>Try Query Binding with objects:</h3>'; |
262
|
|
|
$sql = 'SELECT * FROM '.$table.' WHERE id = ?'; |
263
|
|
|
$query = $db->query($sql, array(1)); |
264
|
|
|
echo '<p>'.$db->last_query().'</p>'; |
265
|
|
|
$query->result(true); |
266
|
|
|
foreach ($query as $item) { |
267
|
|
|
echo '<pre>'.print_r($item, true).'</pre>'; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
echo '<h3>Try Query Binding with arrays (returns both associative and numeric because I pass MYSQL_BOTH to result():</h3>'; |
271
|
|
|
$sql = 'SELECT * FROM '.$table.' WHERE id = ?'; |
272
|
|
|
$query = $db->query($sql, array(1)); |
273
|
|
|
echo '<p>'.$db->last_query().'</p>'; |
274
|
|
|
$query->result(false, MYSQL_BOTH); |
275
|
|
|
foreach ($query as $item) { |
276
|
|
|
echo '<pre>'.print_r($item, true).'</pre>'; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
echo '<h3>Look, we can also manually advance the result pointer!</h3>'; |
280
|
|
|
$query = $db->select('title')->from($table)->get(); |
281
|
|
|
echo 'First:<pre>'.print_r($query->current(), true).'</pre><br />'; |
282
|
|
|
$query->next(); |
283
|
|
|
echo 'Second:<pre>'.print_r($query->current(), true).'</pre><br />'; |
284
|
|
|
$query->next(); |
285
|
|
|
echo 'Third:<pre>'.print_r($query->current(), true).'</pre>'; |
286
|
|
|
echo '<h3>And we can reset it to the beginning:</h3>'; |
287
|
|
|
$query->rewind(); |
288
|
|
|
echo 'Rewound:<pre>'.print_r($query->current(), true).'</pre>'; |
289
|
|
|
|
290
|
|
|
echo '<p>Number of rows using count_records(): '.$db->count_records('pages').'</p>'; |
291
|
|
|
} else { |
292
|
|
|
echo 'NO! The '.$table.' table doesn\'t exist, so we can\'t continue =( '; |
293
|
|
|
} |
294
|
|
|
echo "<br/><br/>\n"; |
295
|
|
|
echo 'done in {execution_time} seconds'; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Demonstrates how to use the Pagination library and Pagination styles. |
300
|
|
|
*/ |
301
|
|
|
public function pagination() |
302
|
|
|
{ |
303
|
|
|
$pagination = new Pagination(array( |
304
|
|
|
// Base_url will default to the current URI |
305
|
|
|
// 'base_url' => 'welcome/pagination_example/page/x', |
306
|
|
|
|
307
|
|
|
// The URI segment (integer) in which the pagination number can be found |
308
|
|
|
// The URI segment (string) that precedes the pagination number (aka "label") |
309
|
|
|
'uri_segment' => 'page', |
310
|
|
|
|
311
|
|
|
// You could also use the query string for pagination instead of the URI segments |
312
|
|
|
// Just set this to the $_GET key that contains the page number |
313
|
|
|
// 'query_string' => 'page', |
314
|
|
|
|
315
|
|
|
// The total items to paginate through (probably need to use a database COUNT query here) |
316
|
|
|
'total_items' => 254, |
317
|
|
|
|
318
|
|
|
// The amount of items you want to display per page |
319
|
|
|
'items_per_page' => 10, |
320
|
|
|
|
321
|
|
|
// The pagination style: classic (default), digg, extended or punbb |
322
|
|
|
// Easily add your own styles to views/pagination and point to the view name here |
323
|
|
|
'style' => 'classic', |
324
|
|
|
|
325
|
|
|
// If there is only one page, completely hide all pagination elements |
326
|
|
|
// Pagination->render() will return an empty string |
327
|
|
|
'auto_hide' => true, |
328
|
|
|
)); |
329
|
|
|
|
330
|
|
|
// Just echo to display the links (__toString() rocks!) |
331
|
|
|
echo 'Classic style: '.$pagination; |
332
|
|
|
|
333
|
|
|
// You can also use the render() method and pick a style on the fly if you want |
334
|
|
|
echo '<hr /> Digg style: ', $pagination->render('digg'); |
335
|
|
|
echo '<hr /> Extended style: ', $pagination->render('extended'); |
336
|
|
|
echo '<hr /> PunBB style: ', $pagination->render('punbb'); |
337
|
|
|
echo 'done in {execution_time} seconds'; |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
/** |
341
|
|
|
* Demonstrates the User_Agent library. |
342
|
|
|
*/ |
343
|
|
|
public function user_agent() |
344
|
|
|
{ |
345
|
|
|
foreach (array('agent', 'browser', 'version') as $key) { |
346
|
|
|
echo $key.': '.Kohana::user_agent($key).'<br/>'."\n"; |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
echo "<br/><br/>\n"; |
350
|
|
|
echo 'done in {execution_time} seconds'; |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* Demonstrates the Payment library. |
355
|
|
|
*/ |
356
|
|
|
/*function payment() |
357
|
|
|
{ |
358
|
|
|
$credit_card = new Payment; |
359
|
|
|
|
360
|
|
|
// You can also pass the driver name to the library to use multiple ones: |
361
|
|
|
$credit_card = new Payment('Paypal'); |
362
|
|
|
$credit_card = new Payment('Authorize'); |
363
|
|
|
|
364
|
|
|
// You can specify one parameter at a time: |
365
|
|
|
$credit_card->login = 'this'; |
366
|
|
|
$credit_card->first_name = 'Jeremy'; |
367
|
|
|
$credit_card->last_name = 'Bush'; |
368
|
|
|
$credit_card->card_num = '1234567890'; |
369
|
|
|
$credit_card->exp_date = '0910'; |
370
|
|
|
$credit_card->amount = '478.41'; |
371
|
|
|
|
372
|
|
|
// Or you can also set fields with an array and the <Payment.set_fields> method: |
373
|
|
|
$credit_card->set_fields(array('login' => 'test', |
374
|
|
|
'first_name' => 'Jeremy', |
375
|
|
|
'last_name' => 'Bush', |
376
|
|
|
'card_num' => '1234567890', |
377
|
|
|
'exp_date' => '0910', |
378
|
|
|
'amount' => '487.41')); |
379
|
|
|
|
380
|
|
|
echo '<pre>'.print_r($credit_card, true).'</pre>'; |
381
|
|
|
|
382
|
|
|
echo 'Success? '; |
383
|
|
|
echo ($response = $credit_card->process() == TRUE) ? 'YES!' : $response; |
384
|
|
|
}*/ |
385
|
|
|
|
386
|
|
|
public function calendar() |
387
|
|
|
{ |
388
|
|
|
$profiler = new Profiler; |
|
|
|
|
389
|
|
|
|
390
|
|
|
$calendar = new Calendar($this->input->get('month', date('m')), $this->input->get('year', date('Y'))); |
391
|
|
|
$calendar->attach($calendar->event() |
392
|
|
|
->condition('year', 2008) |
393
|
|
|
->condition('month', 8) |
394
|
|
|
->condition('day', 8) |
395
|
|
|
->output(html::anchor('http://forum.kohanaphp.com/comments.php?DiscussionID=275', 'Learning about Kohana Calendar'))); |
396
|
|
|
|
397
|
|
|
echo $calendar->render(); |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
/** |
401
|
|
|
* Demonstrates how to use the Image libarary.. |
402
|
|
|
*/ |
403
|
|
|
public function image() |
404
|
|
|
{ |
405
|
|
|
// For testing only, save the new image in DOCROOT |
406
|
|
|
$dir = realpath(DOCROOT); |
407
|
|
|
|
408
|
|
|
// Original Image filename |
409
|
|
|
$image = DOCROOT.'kohana.png'; |
410
|
|
|
|
411
|
|
|
// Create an instance of Image, with file |
412
|
|
|
// The orginal image is not affected |
413
|
|
|
$image = new Image($image); |
414
|
|
|
|
415
|
|
|
// Most methods are chainable |
416
|
|
|
// Resize the image, crop the center left |
417
|
|
|
$image->resize(200, 100)->crop(150, 50, 'center', 'left'); |
418
|
|
|
|
419
|
|
|
// Display image in browser. |
420
|
|
|
// Keep the actions, to be applied when saving the image. |
421
|
|
|
$image->render($keep_actions = true); |
422
|
|
|
|
423
|
|
|
// Save the image, as a jpeg |
424
|
|
|
// Here the actions will be discarded, by default. |
425
|
|
|
$image->save($dir.'/mypic_thumb.jpg'); |
426
|
|
|
|
427
|
|
|
//echo Kohana::debug($image); |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
/** |
431
|
|
|
* Demonstrates how to use vendor software with Kohana. |
432
|
|
|
*/ |
433
|
|
|
public function vendor() |
434
|
|
|
{ |
435
|
|
|
// Let's do a little Markdown shall we. |
436
|
|
|
$br = "\n\n"; |
|
|
|
|
437
|
|
|
$output = '#Marked Down!#'.$br; |
438
|
|
|
$output .= 'This **_markup_** is created *on-the-fly*, by '; |
439
|
|
|
$output .= '[php-markdown-extra](http://michelf.com/projects/php-markdown/extra)'.$br; |
440
|
|
|
$output .= 'It\'s *great* for user <input> & writing about `<HTML>`'.$br; |
441
|
|
|
$output .= 'It\'s also good at footnotes :-) [^1]'.$br; |
442
|
|
|
$output .= '[^1]: A footnote.'; |
443
|
|
|
|
444
|
|
|
// looks in system/vendor for Markdown.php |
445
|
|
|
require Kohana::find_file('vendor', 'Markdown'); |
446
|
|
|
|
447
|
|
|
echo Markdown($output); |
448
|
|
|
|
449
|
|
|
echo 'done in {execution_time} seconds'; |
450
|
|
|
} |
451
|
|
|
} // End Examples |
452
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: