public function execute()
 {
     $id = $this->getId();
     $this->contact = $contact = new waContact($id);
     // Show an uploaded image to crop?
     if (waRequest::get('uploaded')) {
         // Is there an uploaded file in session?
         $photoEditors = $this->getStorage()->read('photoEditors');
         if (isset($photoEditors[$id]) && file_exists($photoEditors[$id])) {
             $url = $this->getPreviewUrl(basename($photoEditors[$id]));
             $this->view->assign('oldPreview', $url);
             $this->view->assign('oldImage', $url);
         }
     } else {
         // Is there a photo for this contact?
         $filename = wa()->getDataPath(waContact::getPhotoDir($id) . "{$contact['photo']}.original.jpg", TRUE);
         if (file_exists($filename)) {
             $this->view->assign('oldPreview', $contact->getPhoto());
             $this->view->assign('oldImage', $contact->getPhoto('original'));
             $this->view->assign('orig', 1);
         }
     }
     $this->view->assign('contactId', $id);
     $this->view->assign('logged_user_id', wa()->getUser()->getId());
     $this->view->assign('contact', $contact);
     $this->view->assign('env', wa()->getEnv());
     $this->assignUrls();
 }
 public function execute()
 {
     $contact = wa()->getUser();
     $contact['photo'] = 0;
     $contact->save();
     $oldDir = wa()->getDataPath(waContact::getPhotoDir($contact->getId()), true, 'contacts', false);
     if (file_exists($oldDir)) {
         waFiles::delete($oldDir);
     }
     $this->response = array('done' => 1);
 }
예제 #3
0
 /**
  * Userpic
  */
 public function photoAction()
 {
     $id = (int) waRequest::get('id');
     if (!$id) {
         $id = $this->getUser()->getId();
     }
     $contact = new waContact($id);
     $rand = $contact['photo'];
     $file = wa()->getDataPath(waContact::getPhotoDir($id) . "{$rand}.original.jpg", TRUE, 'contacts');
     $size = waRequest::get('size');
     if (!file_exists($file)) {
         $size = (int) $size;
         if (!in_array($size, array(20, 32, 50, 96))) {
             $size = 96;
         }
         waFiles::readFile($this->getConfig()->getRootPath() . '/wa-content/img/userpic' . $size . '.jpg');
     } else {
         // original file
         if ($size == 'original') {
             waFiles::readFile($file);
         } elseif ($size == 'full') {
             $file = str_replace('.original.jpg', '.jpg', $file);
             waFiles::readFile($file);
         } else {
             if (!$size) {
                 $size = '96x96';
             }
             $size_parts = explode('x', $size, 2);
             $size_parts[0] = (int) $size_parts[0];
             if (!isset($size_parts[1])) {
                 $size_parts[1] = $size_parts[0];
             } else {
                 $size_parts[1] = (int) $size_parts[1];
             }
             if (!$size_parts[0] || !$size_parts[1]) {
                 $size_parts = array(96, 96);
             }
             $size = $size_parts[0] . 'x' . $size_parts[1];
             $thumb_file = str_replace('.original.jpg', '.' . $size . '.jpg', $file);
             $file = str_replace('.original.jpg', '.jpg', $file);
             if (!file_exists($thumb_file) || filemtime($thumb_file) < filemtime($file)) {
                 waImage::factory($file)->resize($size_parts[0], $size_parts[1])->save($thumb_file);
                 clearstatcache();
             }
             waFiles::readFile($thumb_file);
         }
     }
 }
 public function execute()
 {
     $id = $this->getId();
     // Delete the old photos if they exist
     $oldDir = wa()->getDataPath(waContact::getPhotoDir($id), TRUE);
     if (file_exists($oldDir)) {
         waFiles::delete($oldDir);
     }
     // Update record in DB for this user
     $contact = new waContact($id);
     $contact['photo'] = 0;
     $contact->save();
     // Update recent history to reload thumbnail correctly (if not called from personal account)
     if (wa()->getUser()->get('is_user')) {
         $history = new contactsHistoryModel();
         $history->save('/contact/' . $id, null, null, '--');
     }
     $this->response = array('done' => 1, 'url' => $contact->getPhoto());
 }
예제 #5
0
 /**
  * @return bool
  */
 protected function saveFromPost($form, $contact)
 {
     $data = $form->post();
     if (!$data || !is_array($data)) {
         return false;
     }
     // save photo before all
     $photo_file = waRequest::file('photo_file');
     if (array_key_exists('photo', $data)) {
         if ($photo_file->uploaded() && ($avatar = $photo_file->waImage())) {
             // add/update photo
             $square = min($avatar->height, $avatar->width);
             // setPhoto with crop
             $rand = mt_rand();
             $path = wa()->getDataPath(waContact::getPhotoDir($contact->getId()), true, 'contacts', false);
             // delete old image
             if (file_exists($path)) {
                 waFiles::delete($path);
             }
             waFiles::create($path);
             $filename = $path . $rand . ".original.jpg";
             waFiles::create($filename);
             waImage::factory($photo_file)->save($filename, 90);
             $filename = $path . $rand . ".jpg";
             waFiles::create($filename);
             waImage::factory($photo_file)->crop($square, $square)->save($filename, 90);
             waContactFields::getStorage('waContactInfoStorage')->set($contact, array('photo' => $rand));
         } elseif (empty($data['photo'])) {
             // remove photo
             $contact->set('photo', "");
         }
         $this->form->values['photo'] = $data['photo'] = $contact->get('photo');
     }
     // Validation
     if (!$form->isValid($contact)) {
         return false;
     }
     // Password validation
     if (!empty($data['password']) && $data['password'] !== $data['password_confirm']) {
         $form->errors('password', _ws('Passwords do not match'));
         return false;
     } elseif (empty($data['password']) || empty($data['password_confirm'])) {
         unset($data['password']);
     }
     unset($data['password_confirm']);
     // get old data for logging
     if ($this->contact) {
         $old_data = array();
         foreach ($data as $field_id => $field_value) {
             $old_data[$field_id] = $this->contact->get($field_id);
         }
     }
     foreach ($data as $field => $value) {
         $contact->set($field, $value);
     }
     $errors = $contact->save();
     // If something went wrong during save for any reason,
     // show it to user. In theory it shouldn't but better be safe.
     if ($errors) {
         foreach ($errors as $field => $errs) {
             foreach ($errs as $e) {
                 $form->errors($field, $e);
             }
         }
         return false;
     }
     // get new data for logging
     $new_data = array();
     foreach ($data as $field_id => $field_value) {
         if (!isset($errors[$field_id])) {
             $new_data[$field_id] = $this->contact->get($field_id);
         }
     }
     $this->logProfileEdit($old_data, $new_data);
     return true;
 }
 public function execute()
 {
     $this->response = array();
     // Initialize all needed post vars as $vars in current namespace
     foreach (array('x1', 'y1', 'x2', 'y2', 'w', 'h', 'ww', 'orig') as $var) {
         if (null === (${$var} = (int) waRequest::post($var))) {
             // $$ black magic...
             $this->response['error'] = 'wrong parameters';
             return;
         }
     }
     $id = $this->getId();
     $contact = new waContact($id);
     // Path to file we need to crop
     $rand = mt_rand();
     $dir = waContact::getPhotoDir($id, true);
     $filename = wa()->getDataPath("{$dir}{$rand}.original.jpg", true, 'contacts');
     $oldDir = wa()->getDataPath("{$dir}", true, 'contacts');
     $no_old_photo = false;
     if (!$orig) {
         // Delete the old photos if they exist
         if (file_exists($oldDir)) {
             waFiles::delete($oldDir);
             $no_old_photo = true;
         }
         waFiles::create($oldDir);
         // Is there an uploaded file in session?
         $photoEditors = $this->getStorage()->read('photoEditors');
         if (!isset($photoEditors[$id]) || !file_exists($photoEditors[$id])) {
             $this->response['error'] = 'Photo editor session is not found or already expired.';
             return;
         }
         $newFile = $photoEditors[$id];
         // Save the original image in jpeg for future use
         try {
             $img = waImage::factory($newFile)->save($filename);
         } catch (Exception $e) {
             $this->response['error'] = 'Unable to save new file ' . $filename . ' (' . pathinfo($filename, PATHINFO_EXTENSION) . ') as jpeg: ' . $e->getMessage();
             return;
         }
         // Remove uploaded file
         unset($photoEditors[$id]);
         $this->getStorage()->write('photoEditors', $photoEditors);
         unlink($newFile);
     } else {
         // cropping an old file. Move it temporarily to temp dir to delete all cached thumbnails
         $oldFile = wa()->getDataPath("{$dir}{$contact['photo']}.original.jpg", TRUE, 'contacts');
         $tempOldFile = wa()->getTempPath("{$id}/{$rand}.original.jpg", 'contacts');
         waFiles::move($oldFile, $tempOldFile);
         // Delete thumbnails
         if (file_exists($oldDir)) {
             waFiles::delete($oldDir);
         }
         waFiles::create($oldDir);
         // return original image to its proper place
         waFiles::move($tempOldFile, $filename);
     }
     if (!file_exists($filename)) {
         $this->response['error'] = 'Image to crop not found (check directory access rights).';
         return;
     }
     // Crop and save selected area
     $croppedFilename = wa()->getDataPath("{$dir}{$rand}.jpg", TRUE, 'contacts');
     try {
         $img = waImage::factory($filename);
         $scale = $img->width / $ww;
         $img->crop(floor($w * $scale), floor($h * $scale), floor($x1 * $scale), floor($y1 * $scale))->save($croppedFilename);
     } catch (Exception $e) {
         $this->response['error'] = 'Unable to crop an image: ' . $e->getMessage();
         return;
     }
     // Update record in DB for this user
     $contact['photo'] = $rand;
     $contact->save();
     if ($no_old_photo) {
         $old_app = null;
         if (wa()->getApp() !== 'contacts') {
             $old_app = wa()->getApp();
             waSystem::setActive('contacts');
         }
         $this->logAction('photo_add', null, $contact->getId());
         if ($old_app) {
             waSystem::setActive($old_app);
         }
     }
     // Update recent history to reload thumbnail correctly (if not called from personal account)
     if (wa()->getUser()->get('is_user')) {
         $history = new contactsHistoryModel();
         $history->save('/contact/' . $id, null, null, '--');
     }
     $this->response = array('url' => $contact->getPhoto());
 }
 /**
  * Merge given contacts into master contact, save, send merge event, then delete slaves.
  *
  * !!! Probably should move it into something like contactsHelper
  *
  * @param array $merge_ids list of contact ids
  * @param int $master_id contact id to merge others into
  * @return array
  */
 public static function merge($merge_ids, $master_id)
 {
     $merge_ids[] = $master_id;
     // List of contacts to merge
     $collection = new contactsCollection('id/' . implode(',', $merge_ids));
     $contacts_data = $collection->getContacts('*');
     // Master contact data
     if (!$master_id || !isset($contacts_data[$master_id])) {
         throw new waException('No contact to merge into.');
     }
     $master_data = $contacts_data[$master_id];
     unset($contacts_data[$master_id]);
     $master = new waContact($master_id);
     $result = array('total_requested' => count($contacts_data) + 1, 'total_merged' => 0, 'error' => '', 'users' => 0);
     if ($master_data['photo']) {
         $filename = wa()->getDataPath(waContact::getPhotoDir($master_data['id']) . "{$master_data['photo']}.original.jpg", true, 'contacts');
         if (!file_exists($filename)) {
             $master_data['photo'] = null;
         }
     }
     $data_fields = waContactFields::getAll('enabled');
     $check_duplicates = array();
     // field_id => true
     $update_photo = null;
     // if need to update photo here it is file paths
     // merge loop
     foreach ($contacts_data as $id => $info) {
         if ($info['is_user'] > 0) {
             $result['users']++;
             unset($contacts_data[$id]);
             continue;
         }
         foreach ($data_fields as $f => $field) {
             if (!empty($info[$f])) {
                 if ($field->isMulti()) {
                     $master->add($f, $info[$f]);
                     $check_duplicates[$f] = true;
                 } else {
                     // Field does not allow multiple values.
                     // Set value if no value yet.
                     if (empty($master_data[$f])) {
                         $master[$f] = $master_data[$f] = $info[$f];
                     }
                 }
             }
         }
         // photo
         if (!$master_data['photo'] && $info['photo'] && !$update_photo) {
             $filename_original = wa()->getDataPath(waContact::getPhotoDir($info['id']) . "{$info['photo']}.original.jpg", true, 'contacts');
             if (file_exists($filename_original)) {
                 $update_photo = array('original' => $filename_original);
                 $filename_crop = wa()->getDataPath(waContact::getPhotoDir($info['id']) . "{$info['photo']}.jpg", true, 'contacts');
                 if (file_exists($filename_crop)) {
                     $update_photo['crop'] = $filename_crop;
                 }
             }
         }
         // birthday parts
         if (!empty($data_fields['birthday'])) {
             foreach (array('birth_day', 'birth_month', 'birth_year') as $f) {
                 if (empty($master_data[$f]) && !empty($info[$f])) {
                     $master[$f] = $master_data[$f] = $info[$f];
                 }
             }
         }
     }
     // Remove duplicates
     foreach (array_keys($check_duplicates) as $f) {
         $values = $master[$f];
         if (!is_array($values) || count($values) <= 1) {
             continue;
         }
         $unique_values = array();
         // md5 => true
         foreach ($values as $k => $v) {
             if (is_array($v)) {
                 if (isset($v['value']) && is_string($v['value'])) {
                     $v = $v['value'];
                 } else {
                     unset($v['ext'], $v['status']);
                     ksort($v);
                     $v = serialize($v);
                 }
             }
             $hash = md5(mb_strtolower($v));
             if (!empty($unique_values[$hash])) {
                 unset($values[$k]);
                 continue;
             }
             $unique_values[$hash] = true;
         }
         $master[$f] = array_values($values);
     }
     // Save master contact
     $errors = $master->save(array(), 42);
     // 42 == do not validate anything at all
     if ($errors) {
         $errormsg = array();
         foreach ($errors as $field => $err) {
             if (!is_array($err)) {
                 $err = array($err);
             }
             foreach ($err as $str) {
                 $errormsg[] = $field . ': ' . $str;
             }
         }
         $result['error'] = implode("\n<br>", $errormsg);
         return $result;
     }
     // Merge categories
     $category_ids = array();
     $ccm = new waContactCategoriesModel();
     foreach ($ccm->getContactsCategories($merge_ids) as $cid => $cats) {
         $category_ids += array_flip($cats);
     }
     $category_ids = array_keys($category_ids);
     $ccm->add($master_id, $category_ids);
     // update photo
     if ($update_photo) {
         $rand = mt_rand();
         $path = wa()->getDataPath(waContact::getPhotoDir($master['id']), true, 'contacts', false);
         // delete old image
         if (file_exists($path)) {
             waFiles::delete($path);
         }
         waFiles::create($path);
         $filename = $path . "/" . $rand . ".original.jpg";
         waFiles::create($filename);
         waImage::factory($update_photo['original'])->save($filename, 90);
         if (!empty($update_photo['crop'])) {
             $filename = $path . "/" . $rand . ".jpg";
             waFiles::create($filename);
             waImage::factory($update_photo['crop'])->save($filename, 90);
         } else {
             waFiles::copy($filename, $path . "/" . $rand . ".jpg");
         }
         $master->save(array('photo' => $rand));
     }
     $result['total_merged'] = count($contacts_data) + 1;
     $contact_ids = array_keys($contacts_data);
     // wa_log
     $log_model = new waLogModel();
     $log_model->updateByField('contact_id', $contact_ids, array('contact_id' => $master_id));
     // wa_login_log
     $login_log_model = new waLoginLogModel();
     $login_log_model->updateByField('contact_id', $contact_ids, array('contact_id' => $master_id));
     // Merge event
     $params = array('contacts' => $contact_ids, 'id' => $master_data['id']);
     wa()->event(array('contacts', 'merge'), $params);
     // Delete all merged contacts
     $contact_model = new waContactModel();
     $contact_model->delete($contact_ids, false);
     // false == do not trigger event
     $history_model = new contactsHistoryModel();
     foreach ($contact_ids as $contact_id) {
         $history_model->deleteByField(array('type' => 'add', 'hash' => '/contact/' . $contact_id));
     }
     return $result;
 }
예제 #8
0
$file = explode("/", $file);
if (count($file) != 4) {
    header("Location: {$root_url}wa-content/img/userpic96.jpg");
    exit;
}
$request_file = $file[3];
$contact_id = (int) $file[2];
$file = explode(".", $file[3]);
if (!$contact_id || count($file) != 3) {
    header("Location: {$root_url}wa-content/img/userpic96.jpg");
    exit;
}
$size = explode("x", $file[1]);
if (count($size) != 2 || !$size[0] || !$size[1]) {
    header("Location: {$root_url}wa-content/img/userpic96.jpg");
    exit;
}
$file = $file[0] . ".jpg";
$path = wa()->getDataPath(waContact::getPhotoDir($contact_id), true, 'contacts', false);
$filepath = "{$path}{$file}";
if (!file_exists($filepath)) {
    header("Location: {$root_url}wa-content/img/userpic96.jpg");
    exit;
}
if ($size[0] == $size[1]) {
    waImage::factory($filepath)->resize($size[0], $size[1])->save($path . $request_file);
} else {
    waImage::factory($filepath)->resize($size[0], $size[1], waImage::INVERSE)->crop($size[0], $size[1])->save($path . $request_file);
}
clearstatcache();
waFiles::readFile($path . $request_file);
예제 #9
0
if (file_exists($file)) {
    include($file);
} else {
    header("HTTP/1.0 404 Not Found");
}
');
waFiles::copy(wa()->getAppPath('lib/config/data/.htaccess', 'contacts'), $path . '/.htaccess');
$old_path = wa()->getDataPath('photo', true, 'contacts', false);
if (file_exists($old_path) && is_dir($old_path)) {
    $model = new waModel();
    $all_success = true;
    $contact_ids = $model->query("SELECT id FROM wa_contact WHERE photo > 0")->fetchAll(null, true);
    foreach ($contact_ids as $contact_id) {
        $old_filepath = $old_path . '/' . $contact_id;
        if (file_exists($old_filepath) && is_dir($old_filepath)) {
            $filepath = wa()->getDataPath(waContact::getPhotoDir($contact_id), true, 'contacts');
            try {
                $success = @waFiles::move($old_filepath, $filepath);
                $all_success = $all_success && $success;
            } catch (Exception $e) {
                $all_success = false;
            }
        }
    }
    if ($all_success) {
        try {
            waFiles::delete(wa()->getDataPath('photo', true, 'contacts', false));
        } catch (waException $e) {
        }
    }
}