Example #1
0
 public static function getDb()
 {
     if (self::$db !== null) {
         return self::$db;
     }
     $config = self::$config;
     return self::$db = new PDO($config['dsn'], $config['username'], $config['password'], array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8';"));
 }
Example #2
0
 public function addContent($content)
 {
     $info = compact('content');
     $info['comment'] = $this;
     Addition::create($info);
     // generate activity
     $info = array('user' => $this->user, 'action' => 'add', 'object' => $this, 'link' => $this);
     $act = Activity::create($info);
     // inform all stack holders
     $conds = array('comment=?' => array($this->id));
     $users = Sdb::fetch('DISTINCT(user)', Discuss::table(), $conds);
     $info = array('activity' => $act);
     foreach ($users as $u) {
         if ($u == $this->user) {
             break;
             // 不需要通知本人
         }
         $info['user'] = $u;
         Timeline::create($info);
     }
 }
Example #3
0
/**
 * @author  ryan <*****@*****.**>
 */
function _init()
{
    $config = $GLOBALS['config'];
    Sdb::setConfig($config['db']);
    // login
    $GLOBALS['user'] = $user = User::loggingUser();
    // but the var here should be long such as $logging_user
    if ($user === false) {
        $has_login = false;
    } else {
        $has_login = true;
    }
    $GLOBALS['has_login'] = $has_login;
    // login check
    $controller = $GLOBALS['controller'];
    if (in_array($controller, $config['login_page']) && !$has_login) {
        redirect("login?back={$controller}/{$target}");
    }
    $GLOBALS['nav'] = $navs = build_nav($config['navs']['admin']);
    $page['description'] = $config['description'];
    $page['keywords'] = $config['keywords'];
}
Example #4
0
 public static function check($username, $password)
 {
     $conds = array('username=? AND password=?' => array($username, md5($password)));
     $info = Sdb::fetchRow('*', self::table(), $conds);
     return $info ? new self($info) : false;
 }
Example #5
0
<?php

$tables_to_clear = array(User::table(), Customer::table(), Product::table(), Cart::table(), Address::table(), UserLog::table(), Account::table(), AccountHistory::table());
$db = Sdb::getDb();
foreach ($tables_to_clear as $table) {
    $db->exec("TRUNCATE TABLE {$table}");
}
if (_get('exit')) {
    echo '<script src="static/hide.js"></script>';
    echo '<div class="conclusion pass">All Clear!</div>';
    exit;
}
Example #6
0
 public function del()
 {
     Sdb::del(self::table(), $this->selfCond());
 }
Example #7
0
<?php

require 'lib.php';
// functions for test
Sdb::setConfig($config['db']);
// clear side effects for all
// unset all session
foreach ($_SESSION as $key => $value) {
    unset($_SESSION[$key]);
}
if (isset($_GET['u'])) {
    redirect();
}
require_once CORE_ROOT . 'BasicModel.php';
// clear db entries that was insert by test
include 'clear_db.php';
$all_pass = true;
begin_test();
test(1, 1, 'test for 1 === 1');
begin_test();
$username = '******';
$password = '******';
$realname = '小池';
$phone = '13711231212';
$email = '*****@*****.**';
$info = compact('username', 'password', 'realname', 'phone', 'email');
$customer = Customer::create($info);
test(1, 1, array('name' => 'register Customer, db'));
begin_test();
test(User::check($username, $password), true, array('name' => 'User::check($username, $password)'));
begin_test();
Example #8
0
 public function commentedByUser(User $user)
 {
     $conds = array('user=? AND teacher=?' => array($user->id, $this->id));
     $id = Sdb::fetchRow('id', Comment::table(), $conds);
     return $id ? new Comment($id) : false;
 }
Example #9
0
 public function count()
 {
     $field = count($this->tables) > 1 ? "{$this->table}.id" : '*';
     if ($this->distinct) {
         $field = "DISTINCT({$field})";
     }
     $field = "count({$field})";
     if ($this->conds) {
         $condStr = implode(' AND ', array_keys($this->conds));
         $a = array_filter(array_values($this->conds), function ($v) {
             return $v !== false && $v !== null;
         });
         $values = array();
         foreach ($a as $v) {
             if (is_array($v)) {
                 $values += $v;
             } else {
                 $values[] = $v;
             }
         }
         $conds = array($condStr => $values);
     } else {
         $conds = '';
     }
     $arr = Sdb::fetch($field, $this->tables, $conds);
     return $arr[0];
 }