Improve json_return and move it to future
[infodrom.org/service.infodrom.org] / src / future.php
1 <?php
2 function __autoload($class)
3 {
4   if (!defined('CLASS_PATH')) throw new Exception('Class path not defined');
5
6   $fname = CLASS_PATH . '/'.strtolower($class).'.class.php';
7
8   if (!file_exists($fname)) throw new Exception(sprintf('Class %s not found', $class));
9
10   require_once($fname);
11 }
12
13 function json_return($data)
14 {
15   header('Content-type: application/json; charset="UTF-8"');
16
17   $encoded = json_encode($data);
18
19   if ($encoded === false) {
20     error_log('Wrong encoding: ' . var_export($encoded,true));
21     return json_encode(array('status' => false,
22                              'error' => 'Cannot encode return values, see server log.'));
23   } else {
24     echo $encoded;
25   }
26
27   exit();
28 }
29
30
31 global $db;
32
33 $dsn = sprintf('%s:host=%s;dbname=%s', DBDRIVER, DBHOST, DBNAME);
34 $db = new Database(DBDRIVER, DBHOST, DBNAME, DBUSER, DBPASS);
35 if (defined('MAIL_ERROR')) $db->setErrorMail(MAIL_ERROR);
36 ?>