Example #1
0
function insertdata($table, $data, $special = 0)
{
    //global $tpl;
    global $memcache;
    global $l;
    global $tpl;
    global $user_data;
    global $getmonth;
    $objResponse = new xajaxResponse();
    //include('settings/template.php');
    include 'settings/tables.php';
    if ($user_data == '' || !isset($user_data)) {
        require_once 'lib/functions/get_userdata.php';
    }
    //enter new diary entry
    if ($table == $tbl_diary) {
        if ($special == 1) {
            $html = $tpl->fetch("modules/improve/diary/add_pin.tpl");
            //         $objResponse->assign("add_pin","style.className",'pin');
            $objResponse->assign("add_pin", "innerHTML", $html);
            $objResponse->includeScript("js/pinterest.js");
            $objResponse->call("m_reload");
        } else {
            if ($data['image_file'] != '') {
                $objResponse->script("document.forms['insert'].submit();");
            } else {
                $mysqldate = date('Y-m-d H:i:s', time());
                $diary_note = mysql_real_escape_string(strip_tags($data['note']));
                //insert new entry
                $diary = new ModifyEntry();
                $diary->table = $table;
                $diary->cols = 'userID, entry, date';
                $diary->values = " '" . $user_data['ID'] . "', '" . $diary_note . "', '" . $mysqldate . "' ";
                $diary->insert();
                unset($diary);
                //TODO check if first entry and if yes update user array with start_month and start_year
                //refresh content
                include "lib/functions/fetch_diary.php";
                $tpl->assign('ay_diary', $ay_diary);
                $tpl->assign('at_least_one_entry', 1);
                //else $tpl->assign('at_least_one_entry', 0);
                //TODO why is this needed now!?
                $tpl->assign('user_data', $user_data);
                $html = $tpl->fetch("modules/home/diary_entries.tpl");
                $objResponse->assign("diary_entries", "innerHTML", $html);
                $objResponse->assign("add_pin", "innerHTML", '');
                $objResponse->includeScript("js/pinterest.js");
                $objResponse->call("m_reload");
            }
        }
    } elseif ($table == $tbl_goals) {
        $mysqldate = date('Y-m-d H:i:s', time());
        $note = strip_tags(mysql_real_escape_string($data['note']));
        //insert new entry
        $goals = new ModifyEntry();
        $goals->table = $table;
        $goals->cols = 'userID, goal, created';
        $goals->values = " '" . $user_data['ID'] . "', '" . $note . "', '" . $mysqldate . "' ";
        $goals->insert();
        unset($goals);
        //refresh content
        $html1 = $tpl->fetch("modules/improve/goals/sortfields.tpl");
        $objResponse->assign("sortfields", "innerHTML", $html1);
        include "lib/functions/fetch_goals.php";
        $tpl->assign('ay_goals', $ay_goals);
        $html2 = $tpl->fetch("modules/improve/goals/goal_entries.tpl");
        $objResponse->assign("goal_entries", "innerHTML", $html2);
        $objResponse->call("reset_input");
    } elseif ($table == $tbl_distorted_thoughts) {
        $mysqldate = date('Y-m-d H:i:s', time());
        $thought = strip_tags(mysql_real_escape_string($data['thought']));
        $response = strip_tags(mysql_real_escape_string($data['response']));
        $c_action = strip_tags(mysql_real_escape_string($data['c_action']));
        //insert new entry
        $thoughts = new ModifyEntry();
        $thoughts->table = $table;
        $thoughts->cols = 'userID, thought, response, action, created';
        $thoughts->values = " '" . $user_data['ID'] . "', '" . $thought . "', '" . $response . "', '" . $c_action . "', '" . $mysqldate . "' ";
        $thoughts->insert();
        unset($thoughts);
        //refresh content
        include "lib/functions/fetch_thoughts.php";
        $tpl->assign('ay_thoughts', $ay_thoughts);
        $html = $tpl->fetch("modules/improve/distorted_thoughts/thought_entries.tpl");
        $objResponse->assign("thought_entries", "innerHTML", $html);
        $objResponse->call("reset_input");
    } elseif ($table == $tbl_da_scale_results || $table == $tbl_bd_scale_results) {
        $i = 0;
        $dataValid = 1;
        $mysqldate = date('Y-m-d H:i:s', time());
        //check if all items have been answered
        for ($i = 1; $i <= $data['items_total']; $i++) {
            if (!isset($data[$i])) {
                $objResponse->alert('Please answer all items');
                // TODO put string in language file
                $dataValid = 0;
                break;
            }
        }
        //TODO store aggregated values in user table
        //all items have been answered
        if ($dataValid == 1) {
            $bd_total_score = 0;
            $da_total_score[0] = $da_total_score[1] = $da_total_score[2] = $da_total_score[3] = $da_total_score[4] = $da_total_score[5] = $da_total_score[6] = 0;
            //insert new entry
            $scale_data = new ModifyEntry();
            $scale_data->table = $table;
            for ($i = 1; $i <= $data['items_total']; $i++) {
                if (isset($data[$i])) {
                    $scale_data->cols = 'userID, itemID, value, date';
                    $scale_data->values = " '" . $user_data['ID'] . "', '" . $i . "', '" . $data[$i] . "', '" . $mysqldate . "' ";
                    $scale_data->insert();
                    if ($scale_data->errno() > 0) {
                        break;
                    }
                    if ($table == $tbl_da_scale_results) {
                        switch (true) {
                            case $i <= 5:
                                $da_total_score[0] = $da_total_score[0] + $data[$i];
                                break;
                            case $i <= 10:
                                $da_total_score[1] = $da_total_score[1] + $data[$i];
                                break;
                            case $i <= 15:
                                $da_total_score[2] = $da_total_score[2] + $data[$i];
                                break;
                            case $i <= 20:
                                $da_total_score[3] = $da_total_score[3] + $data[$i];
                                break;
                            case $i <= 25:
                                $da_total_score[4] = $da_total_score[4] + $data[$i];
                                break;
                            case $i <= 30:
                                $da_total_score[5] = $da_total_score[5] + $data[$i];
                                break;
                            case $i <= 35:
                                $da_total_score[6] = $da_total_score[6] + $data[$i];
                                break;
                        }
                    }
                    if ($table == $tbl_bd_scale_results) {
                        $bd_total_score = $bd_total_score + $data[$i];
                    }
                }
            }
            unset($scale_data);
            $scale_data = new ModifyEntry();
            $scale_data->table = $tbl_users;
            if ($table == $tbl_da_scale_results) {
                ksort($da_total_score);
            }
            if ($table == $tbl_da_scale_results) {
                $scale_data->changes = " da_latest_score = '" . serialize($da_total_score) . "' ";
            } else {
                $scale_data->changes = " bd_latest_score = '" . $bd_total_score . "' ";
            }
            $scale_data->condition = " ID = '" . $user_data['ID'] . "' ";
            $scale_data->update();
            if ($scale_data->errno() > 0) {
                break;
            }
            if ($table == $tbl_da_scale_results) {
                $user_data['da_latest_score'] = serialize($da_total_score);
            }
            if ($table == $tbl_bd_scale_results) {
                $user_data['bd_latest_score'] = $bd_total_score;
            }
            if (mod_memcache == 1) {
                $memcache->replace($mem_key1, $user_data, false);
            } else {
                $_SESSION['$mem_key1'] = $user_data;
            }
        }
        //update cached data in memcache or session
        //$l["token"] = substr($_COOKIE["l"], 3, -35);
        if ($table == $tbl_da_scale_results) {
            $mem_key2 = "da_scale_data_" . $l["token"];
            $mem_key2a = "da_scale_sep_strings_" . $l["token"];
            $mem_key2b = "da_scale_sep_dates_" . $l["token"];
            if (mod_memcache == 1) {
                $memcache->delete($mem_key2);
                $memcache->delete($mem_key2a);
                $memcache->delete($mem_key2b);
            } else {
                unset($_SESSION['$mem_key2']);
                unset($_SESSION['$mem_key2a']);
                unset($_SESSION['$mem_key2b']);
            }
        }
        if ($table == $tbl_bd_scale_results) {
            $mem_key3 = "bd_scale_data_" . $l["token"];
            if (mod_memcache == 1) {
                $memcache->delete($mem_key3);
            } else {
                unset($_SESSION['$mem_key3']);
            }
        }
        //redirect to overview/result page
        if ($dataValid == 1 && $scale_data->errno() == 0 && $table == $tbl_da_scale_results) {
            $objResponse->redirect(ROOT_DIR . 'analyze/da_scale/index.html');
        }
        if ($dataValid == 1 && $scale_data->errno() == 0 && $table == $tbl_bd_scale_results) {
            $objResponse->redirect(ROOT_DIR . 'analyze/bd_scale/index.html');
        }
        unset($scale_data);
    }
    return $objResponse;
}
Example #2
0
function registerUser($data, $action)
{
    global $db;
    global $debug_mode;
    $objResponse = new xajaxResponse();
    include 'settings/tables.php';
    $reg_failure = "";
    $email = trim(stripslashes(mysqli_real_escape_string($db, $data['email'])));
    $firstname = trim(stripslashes(mysqli_real_escape_string($db, $data['firstname'])));
    if ($action == 'register') {
        $password = trim(stripslashes(mysqli_real_escape_string($db, md5($data['password']))));
        $password2 = trim(stripslashes(mysqli_real_escape_string($db, md5($data['password2']))));
        $beta_code = trim(stripslashes(mysqli_real_escape_string($db, md5($data['betacode']))));
        //generate activation code
        $act_code = md5(uniqid(rand()));
        $token = time() . uniqid();
        //check if email is already registered
        $checkemail = new CheckExist();
        $checkemail->tableE = $tbl_users;
        $checkemail->conditionE = " UserEmail = '" . $email . "'  ";
        $CheckData = $checkemail->exist();
        //error handling and validation
        if (!preg_match('/^([a-zA-Z0-9])+([\\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\\.[a-zA-Z0-9_-]+)+/', $email)) {
            $reg_failure = "Email not valid.";
        } elseif (strlen($email) < 5) {
            $reg_failure = "Email not valid.";
        } elseif ($CheckData > 0) {
            $reg_failure = "Email already taken.";
        } elseif ($password != $password2) {
            $reg_failure = "Passwords do not match. Please correct your information and try it again.";
        } elseif (strlen($data['password']) < 5 || strlen($data['password2']) < 5) {
            $reg_failure = "Password is too short. Please correct your information and try it again.";
        } elseif (strlen($firstname) < 3) {
            $reg_failure = "Firstname is too short. Please correct your information and try it again.";
        } elseif ($beta_code != 'X2DH38u3z') {
            $reg_failure = "Beta Access Code not valid. Please correct your information and try it again.";
        }
        //registration validation successful
        if ($reg_failure == "") {
            //create new user in database
            $user_register = new ModifyEntry();
            $user_register->table = $tbl_users;
            $user_register->cols = 'UserToken, UserEmail, UserPass, firstname, activation_code, language';
            $user_register->values = " '{$token}', '{$email}', '{$password}', '{$firstname}', '{$act_code}', 'EN' ";
            $user_register->insert();
            if ($user_register->errno() == 0) {
                //send email confirmation with activation link to user
                $Header = "MIME-Version: 1.0\n";
                $Header .= "Content-type: text/plain; charset=utf-8\n";
                $Header .= "From: noreply@happify.com";
                //$from    = "From: admin@wiwistud.de";
                //activation.html?c=bebf08f7af79422ced07be79c2f2f5c0&t=13140575844e52ed70736d2
                $act_link = ROOT_DIR . "logon/activation.html?c=" . $act_code . "&t=" . $token;
                $subject = "Activation link happify.com";
                $message = "Activation link: " . $act_link;
                if ($debug_mode != "OFF") {
                    require "../phpmailer/class.phpmailer.php";
                    $mail = new PHPMailer();
                    $mail->IsSMTP();
                    // send via SMTP
                    //IsSMTP(); // send via SMTP
                    $mail->SMTPSecure = "ssl";
                    // sets the prefix to the servier
                    $mail->Host = "smtp.gmail.com";
                    // sets GMAIL as the SMTP server
                    $mail->Port = 465;
                    $mail->SMTPDebug = 0;
                    $mail->SMTPAuth = true;
                    // turn on SMTP authentication
                    $mail->Username = "******";
                    // SMTP username
                    $mail->Password = "******";
                    // SMTP password
                    $webmaster_email = "*****@*****.**";
                    //Reply to this email ID
                    //$email="*****@*****.**"; // Recipients email ID
                    //$name=$firstname; // Recipient's name
                    //$mail->From = $webmaster_email;
                    //$mail->FromName = "Happify";
                    $mail->SetFrom($webmaster_email, "Happify");
                    $mail->AddAddress($email, $firstname);
                    //$mail->AddReplyTo($webmaster_email,"Webmaster");
                    //$mail->WordWrap = 50; // set word wrap
                    //$mail->AddAttachment("/var/tmp/file.tar.gz"); // attachment
                    //$mail->AddAttachment("/tmp/image.jpg", "new.jpg"); // attachment
                    //$mail->IsHTML(true); // send as HTML
                    $mail->Subject = $subject;
                    $mail->Body = $message;
                    //HTML Body
                    $mail->AltBody = $message;
                    //Text Body
                    $mail->Send();
                } else {
                    mail($email, $subject, $message, $Header);
                }
                $objResponse->assign("form_reg", "style.display", 'none');
                $objResponse->assign("reg_success", "style.display", 'block');
                unset($user_register);
            } else {
                $objResponse->assign("p_reg_failure", "style.display", 'block');
                $objResponse->assign("p_reg_failure", "innerHTML", "Registration not successful. Please try again.");
                // TODO move string to language file
            }
        } else {
            $objResponse->assign("p_reg_failure", "style.display", 'block');
            $objResponse->assign("p_reg_failure", "innerHTML", $reg_failure);
        }
    } else {
        if ($action == 'notify') {
            //check if email is already registered
            $checkemail = new CheckExist();
            $checkemail->tableE = $tbl_notify;
            $checkemail->conditionE = " UserEmail = '" . $email . "'  ";
            $CheckData = $checkemail->exist();
            //TODO why does it not work on server
            if (!preg_match('/^([a-zA-Z0-9])+([\\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\\.[a-zA-Z0-9_-]+)+/', $email)) {
                $reg_failure = "Email not valid.";
            }
            // TODO move string to language file
            if ($CheckData > 0) {
                $reg_failure = "Email already registered.";
            }
            // TODO move string to language file
            //registration validation successful
            if ($reg_failure == "") {
                //$firstname = $data['firstname'];
                //create new user in database
                $user_register = new ModifyEntry();
                $user_register->table = $tbl_notify;
                $user_register->cols = 'UserEmail';
                $user_register->values = " '" . $email . "' ";
                $user_register->insert();
                if ($user_register->errno() == 0) {
                    //send email confirmation with activation link to user
                    $Header = "MIME-Version: 1.0\n";
                    $Header .= "Content-type: text/plain; charset=utf-8\n";
                    $Header .= "From: noreply@myhappifier.info";
                    $subject = "myhappifier.info Launch";
                    $message = "Hi! \r\n\n  \nThank you for your registration. We will inform you as soon as Happify launches!\r\n\r\n\n              \n              \nUntil then: stay happy!";
                    if ($debug_mode != "OFF") {
                        require "../phpmailer/class.phpmailer.php";
                        $mail = new PHPMailer();
                        $mail->IsSMTP();
                        // send via SMTP
                        //IsSMTP(); // send via SMTP
                        $mail->SMTPSecure = "ssl";
                        // sets the prefix to the server
                        $mail->Host = "smtp.gmail.com";
                        // sets GMAIL as the SMTP server
                        $mail->Port = 465;
                        $mail->SMTPDebug = 0;
                        $mail->SMTPAuth = true;
                        // turn on SMTP authentication
                        $mail->Username = "******";
                        // SMTP username
                        $mail->Password = "******";
                        // SMTP password
                        $webmaster_email = "*****@*****.**";
                        //Reply to this email ID
                        //$email="*****@*****.**"; // Recipients email ID
                        //$name=$firstname; // Recipient's name
                        //$mail->From = $webmaster_email;
                        //$mail->FromName = "Happify";
                        $mail->SetFrom($webmaster_email, "Happify");
                        $mail->AddAddress($email, $firstname);
                        //$mail->AddReplyTo($webmaster_email,"Webmaster");
                        //$mail->WordWrap = 50; // set word wrap
                        //$mail->AddAttachment("/var/tmp/file.tar.gz"); // attachment
                        //$mail->AddAttachment("/tmp/image.jpg", "new.jpg"); // attachment
                        //$mail->IsHTML(true); // send as HTML
                        $mail->Subject = $subject;
                        $mail->Body = $message;
                        //HTML Body
                        $mail->AltBody = $message;
                        //Text Body
                        $mail->Send();
                        //mail does not work
                    } else {
                        mail($email, $subject, $message, $Header);
                    }
                    $objResponse->assign("form_reg", "style.display", 'none');
                    $objResponse->assign("reg_success", "style.display", 'block');
                    unset($user_register);
                } else {
                    $objResponse->assign("p_reg_failure", "style.display", 'block');
                    $objResponse->assign("p_reg_failure", "innerHTML", "Registration not successful. Please try again.");
                    // TODO move string to language file
                }
            } else {
                $objResponse->assign("p_reg_failure", "style.display", 'block');
                $objResponse->assign("p_reg_failure", "innerHTML", $reg_failure);
            }
        }
    }
    return $objResponse;
}