<?php

require '../config.php';
dol_include_once('/twiiitor/class/twiiitor.class.php');
dol_include_once('/projet/class/project.class.php');
dol_include_once('/product/class/product.class.php');
$get = GETPOST('get');
$put = GETPOST('put');
switch ($get) {
    case 'search-user':
        __out(_search_user(GETPOST('q')), 'json');
        break;
    case 'search-tag':
        __out(_search_tag(GETPOST('q')), 'json');
        break;
    case 'search-element':
        __out(_search_element(GETPOST('q')), 'json');
        break;
    case 'comments':
        print _comments(GETPOST('id'), GETPOST('ref'), GETPOST('element'));
        break;
    case 'graph':
        __out(_graph(GETPOST('id'), GETPOST('ref'), GETPOST('element')), 'json');
        break;
}
switch ($put) {
    case 'comment':
        print _comment(GETPOST('id'), GETPOST('ref'), GETPOST('element'), GETPOST('comment'));
        break;
}
function _graph($fk_object, $ref, $element)
}
function _search_user($str)
{
    global $db, $CONF;
    $sql = "select * from " . $CONF['userdb']['users_table'] . " where username='******' or user_civlid='{$str}' or user_email='{$str}' ";
    $res = $db->sql_query($sql);
    $nr = $db->sql_numrows($res);
    if ($nr > 1) {
        return false;
    }
    return $res;
}
if (isset($_POST['uce'])) {
    $str = addslashes($_POST['uce']);
    //var_dump(_search_user($str));
    if ($res = mysql_fetch_assoc(_search_user($str))) {
        $ltime = time();
        $emailtime = $res['user_emailtime'];
        // gen a new password with 6 char long;
        //$emailtime=0;
        if ($emailtime + $CONF['userdb']['edit']['password_change_expire_time'] < $ltime) {
            //  print "$emailtime | $ltime";
            $actkey = md5(uniqid(rand(), true));
            $newpass = generatePassword($CONF['userdb']['edit']['password_minlength']);
            if (function_exists('leonardo_hash')) {
                $newPassword = leonardo_hash($newpass);
            } else {
                $newPassword = md5($newpass);
            }
            $sql = "UPDATE " . $CONF['userdb']['users_table'] . " set user_emailtime='" . time() . "', user_newpasswd='" . $newPassword . "',user_actkey='{$actkey}' where " . $CONF['userdb']['user_id_field'] . "=" . $res['user_id'];
            if ($db->sql_query($sql)) {