<?php
/*
OsShare v1 ,
Coded By Paimpozhil B. , SaravanaKumar M.S.
*/
class UsersController extends AppController {
var $name = 'Users';
var $helpers = array('Html', 'Form' ,'Pagination','Time','Cache');
var $uses = array('User','Profile','Sitesetting','Slaveserver','Video','Picture');
var $components = array ('Pagination');
function index() {
$this->User->recursive = 0;
$data = $this->dolayout();
$priv = $data['privilege'];
if ($priv=='user' || $priv =='mod')
{
$this->redirect(array('action'=>'userindex'), null, true);
}
elseif ($priv =='admin')
{
$this->layout = "admin";
$this->redirect(array('action'=>'adminindex'), null, true);
}
// $this->set('users', $this->paginate());
}
function forgot()
{
$this->layout = "default";
if(!empty($this->data)) {
$this->cleanUpFields();
$this->data["User"]["doj"] = date("Y-m-d");
if($id) {
$this->Profile->create();
$this->Profile->save(array ( "Profile" => array ( "FirstName" => $this->data["User"]["username"] , "user_id" => $this->User->id ) ),false);
$this->Session->setFlash('The User has been saved');
$this->redirect(array('action'=>'index'), null, true);
} else {
$this->Session->setFlash('The User could not be saved. Please, try again.');
}
}
}
function photo_upload($id = null)
{
$this->dolayout();
$this->set('id',$id);
}
function tupload($id = null)
{
$data = $this->dolayout();
$thumb_qr = $this->User->findbyid($id);
if($data["username"] != $thumb_qr["User"]["username"])
{
$this->Session->SetFlash("Invalid User");
$this->redirect(array('action'=>'index','controller'=>'pages'));
}
$ssettings = $this->Sitesetting->findbyid(1);
$slave = $this->Slaveserver->findbyid($ssettings["Sitesetting"]["ActiveSlave"]);
$activeslave = $slave["Slaveserver"];
if ($_FILES['Filedata']['name'])
{
$uploadDir = WWW_ROOT . 'files'.DS;
$uploadFile = $uploadDir . md5(str_replace(" ","_",(addslashes($_FILES['Filedata']['name']))));
$ext = substr(strrchr($_FILES['Filedata']['name'], "."), 1);
move_uploaded_file($_FILES['Filedata']['tmp_name'], $uploadFile.".".$ext);
$hash = $thumb_qr["User"]["id"] . md5(time() . str_replace(" ","_",addslashes($uploadFile))) ;
$org_path = $uploadFile.".".$ext;
$thumbfile = basename($thumb_qr["User"]["username"])."P.jpg";
$thumbpath = $uploadDir.$thumbfile;
if($ssettings["Sitesetting"]["Pic_Soft"] == 'ImageMagick'){
$this->create_thumbnail($org_path,$thumbpath);
}
else{
$this->gd_thumbnail($org_path,$thumbpath,$ext);
}
$newfilename = $thumbpath;
if($activeslave["ftpuser"]=="sameserver" && $activeslave["ftppass"]=="sameserver" && $activeslave["ftphost"]=="sameserver")
{
rename($newfilename,$activeslave["ftpdirectory"]. $thumbfile);
$ftpstatus = "File Has been sent to the slave <br />";
$this->set('ftpstatus',$ftpstatus);
}
else
{
//FTP it to slave
$conn_id = ftp_connect($activeslave["ftphost"],21,10);
if($conn_id)
{
$login_result = ftp_login($conn_id, $activeslave["ftpuser"], $activeslave["ftppass"]);
if ((!$conn_id) || (!$login_result)) { // check connection
$ftpstatus = "Connected <br />";
} else {
// echo "Connected to $ftp_server, for user $ftp_user_name <br />";
}
ftp_chdir($conn_id, $activeslave["ftpdirectory"] );
$upload = ftp_put($conn_id, $thumbfile , $newfilename , FTP_BINARY); // upload the file
if (!$upload) { // check upload status
$ftpstatus = "Uploading Failed <br />";
} else {
}
ftp_close($conn_id); // close the FTP stream
$this->set('ftpstatus',$ftpstatus);
}
}
$thumbnailpin_path = $activeslave["httppath"] .$thumbfile;
$thumb_qr["User"]["avator"] = $thumbnailpin_path;
if($this->User->save($thumb_qr,false))
{
$this->Session->SetFlash("Success<br>Please Refresh The Page");
if($data['privilege']=='admin')
$this->redirect(array('action'=>'adminhome'));
else
$this->redirect(array('action'=>'userindex'));
}
}
}
function forgotpassword()
{
$this->layout = "default";
if($this->data)
{
$data = $this->data["User"];
// debug($this->data);
$udetails = $this->User->findbyemail($data["email"]);
// debug($udetails["User"]);
$date = $data['dob_year'] . '-' . $data['dob_month'] . '-' . $data['dob_day'];
$ddatearr = split(" ",$udetails["User"]["dob"]);
$ddate = $ddatearr[0];
if($date == $ddate && $udetails["User"]["gender"] == $data["gender"])
{
mail($udetails["User"]["email"],"Your logins to " . $this->sitename , "Your Login Details :- \n Username : " . $udetails["User"]["username"] . "\n Password : " . $udetails["User"]["password"] , "From: " . $this->adminemail );
}
else
{
$this->set('error',1);
}
}
}
function userindex()
{
$this->dolayout();
$data = $this->Session->read('User');
$id = $data["id"];
$this->set('site',$this->ssettings);
$user = $this->User->read(null, $id);
$this->set('user',$user);
$this->set('head_title',$user['User']['username']."'s index");
// $this->set('user', $this->User->read(null, $id));
}
function adminhome()
{
$data = $this->dolayout();
/* $condition = "`User`.privilege = 'user'";
$no_users = $this->User->findcount($condition);
$this->set('no_users',$no_users);
$condition = "`User`.privilege = 'mod'";
$no_mods = $this->User->findcount($condition);
$this->set('no_mods',$no_mods);
$condition = "`Video`.is_encoded = 1";
$no_videos = $this->Video->findcount($condition);
$this->set('no_videos',$no_videos);
$condition = "`Picture`.is_corrupted = 1";
$no_pictures = $this->Picture->findcount($condition);
$this->set('no_pictures',$no_pictures);*/
$id = $data["id"];
$this->set('site',$this->ssettings);
// debug($this->ssettings);
// $ssettings = $this->Sitesetting->findbyid(1);
$user = $this->User->read(null, $id);
$this->set('user',$user);
$this->set('head_title',"Admin Home");
}
function adminindex()
{
$this->layout = "admin";
$this->User->recursive = 0;
$this->set('users', $this->paginate());
$this->set('head_title',"Admin Index");
}
function view($id = null) {
$this->layout = "admin";
if(!$id) {
$this->Session->setFlash('Invalid User.');
$this->redirect(array('action'=>'index'), null, true);
}
$this->set('user', $this->User->read(null, $id));
}
function add() {
$this->layout = "admin";
if(!empty($this->data)) {
$this->cleanUpFields();
$this->User->create();
$this->data["User"]["doj"] = date("Y-m-d h-i-s");
if($this->User->save($this->data)) {
$this->Profile->create();
$this->Profile->save(array ( "Profile" => array ( "FirstName" => $this->data["User"]["username"] , "user_id" => $this->User->id ) ),false);
$this->Session->setFlash('The User has been saved');
$this->redirect(array('action'=>'index'), null, true);
} else {
$this->Session->setFlash('The User could not be saved. Please, try again.');
}
}
$this->set('head_title',"Add New User");
}
function signup() {
$this->layout = "default";
if(!empty($this->data)) {
$this->cleanUpFields();
$this->User->create();
$this->data["User"]["doj"] = date("Y-m-d");
$this->data["User"]["privilege"] = "user";
$this->data["User"]["status"] = "a";
$check_user = $this->User->findbyusername($this->data["User"]["username"]);
$check_email = $this->User->findbyemail($this->data["User"]["email"]);
if($check_user['User']['username'] == $this->data["User"]["username"])
{
$this->Session->setFlash('Username already exist,Please change it');
$this->redirect(array('action'=>'signup','controller'=>'users'), null, true);
}
if($check_email['User']['email'] == $this->data["User"]["email"])
{
$this->Session->setFlash('Username already exist,Please change it');
$this->redirect(array('action'=>'signup','controller'=>'users'), null, true);
}
if($this->User->save($this->data)) {
$this->Profile->create();
$this->Profile->save(array ( "Profile" => array ( "FirstName" => $this->data["User"]["username"] , "user_id" => $this->User->id ) ),false);
$this->Session->setFlash('You Have Signed Up, Please Login');
$this->redirect(array('action'=>'home','controller'=>'pages'), null, true);
} else {
$this->Session->setFlash('The User could not be saved. Please, try again.');
}
}
$this->set('head_title',"Registration");
}
function useredit($id = null) {
$this->dolayout();
$data = $this->Session->read('User');
$id = $data["id"];
$this->set('user', $this->User->read(null, $id));
if(!$id && empty($this->data)) {
$this->Session->setFlash('Invalid User');
$this->redirect(array('action'=>'index'), null, true);
}
if(!empty($this->data)) {
$this->cleanUpFields();
if($this->User->save($this->data)) {
$this->Session->setFlash('The User saved');
$this->redirect(array('action'=>'index'), null, true);
} else {
$this->Session->setFlash('The User could not be saved. Please, try again.');
}
}
if(empty($this->data)) {
$this->data = $this->User->read(null, $id);
}
}
function userdelete($id = null) {
$data = $this->Session->read('User');
$id = $data["id"];
if(!$id) {
$this->Session->setFlash('Invalid id for User');
$this->redirect(array('action'=>'index'), null, true);
}
if($this->User->del($id)) {
$profile_to_del = $this->Profile->findbyuser_id($id);
$this->Profile->del($profile_to_del["Profile"]["id"]);
$this->Session->setFlash('User #'.$id.' deleted');
$this->redirect(array('action'=>'index'), null, true);
}
}
function edit($id = null) {
$this->layout = "admin";
if(!$id && empty($this->data)) {
$this->Session->setFlash('Invalid User');
$this->redirect(array('action'=>'index'), null, true);
}
if(!empty($this->data)) {
$this->cleanUpFields();
if($this->User->save($this->data)) {
$this->Session->setFlash('The User saved');
$this->redirect(array('action'=>'index'), null, true);
} else {
$this->Session->setFlash('The User could not be saved. Please, try again.');
}
}
if(empty($this->data)) {
$this->data = $this->User->read(null, $id);
}
}
function delete($id = null) {
if(!$id) {
$this->Session->setFlash('Invalid id for User');
$this->redirect(array('action'=>'index'), null, true);
}
if($this->User->del($id)) {
$profile_to_del = $this->Profile->findbyuser_id($id);
$this->Profile->del($profile_to_del["Profile"]["id"]);
$this->Session->setFlash('User #'.$id.' deleted');
$this->redirect(array('action'=>'index'), null, true);
}
}
function login()
{
$this->layout = "default";
if(!empty($this->data))
{
$this->cleanUpFields();
if(($user = $this->User->validateLogin($this->data['User'])) == true)
{
$this->Session->write('User', $user);
$this->Session->setFlash('Welcome to the Member area');
$this->redirect(base64_decode($this->data["User"]["next"]));
exit();
}
else
{
$this->Session->setFlash('Sorry, the information you\'ve entered is incorrect.');
}
}
if(isset($this->params["named"]["next"]))
$this->set('next',$this->params["named"]["next"]);
else
$this->set('next',base64_encode("index"));
}
function logout()
{
$this->Session->destroy('user');
$this->Session->setFlash('You\'ve successfully logged out.');
$this->redirect('login');
}
function rview($id = null) {
$this->Users->recursive = 0;
$users = $this->User->Video->findall("`Video`.is_encoded = 1 GROUP BY `User`.id ",NULL,NULL,NULL,NULL,0);
$videos[]=NULL;
foreach(array_keys($users) as $key)
{
$user =& $users[$key];
$user["Video"] = $this->User->Video->find("`Video`.is_encoded = 1 AND `Video`.user_id = " . $user["User"]["id"] , NULL, "`Video`.views Desc",NULL,NULL,0);
$videos[$user['User']['id']] = $user["Video"];
}
$users = $this->fix_width_cate($users,'Video','Video','name',10);
$users = $this->fix_width_cate($users,'Video','User','username',5);
// debug($users);
$this->set('users',$users);
$data = $this->dolayout();
$this->Pagination->total = $this->User->findcount();
list($order,$limit,$page) = $this->Pagination->init();
$this->set('svideos',$videos );
// debug($videos);
//rss
if(isset($this->params["named"]["output"]))
{
// Configure::write('debug', '0');
if($this->params["named"]["output"]=="rss")
{
$setting = $this->Sitesetting->findbyid(1);
$this->set("ssetting",$setting["Sitesetting"]);
$this->render('rss','rss');
}
}
}
}
?>