/** * Renames/Moves a file or directory * * @access public * @param string $source Path to the source file or directory * @param string $dest The destination path * @param bool $overwrite Overwrite files if exists * @return bool True if success, False otherwise * @see http://www.php.net/rename */ static function rename($source, $dest, $overwrite = true) { $result = false; if (file_exists($source)) { if (file_exists($dest)) { if (is_dir($source)) { if (false !== ($hDir = @opendir($source))) { while (false !== ($file = @readdir($hDir))) { if ($file == '.' || $file == '..') { continue; } $result = Jaws_Utils::rename($source . DIRECTORY_SEPARATOR . $file, $dest . DIRECTORY_SEPARATOR . $file, $overwrite); if (!$result) { break; } } closedir($hDir); Jaws_Utils::delete($source); } } else { if (!$overwrite) { $destinfo = pathinfo($dest); $dest = $destinfo['dirname'] . DIRECTORY_SEPARATOR . $destinfo['filename'] . '_' . uniqid(floor(microtime() * 1000)); if (isset($destinfo['extension']) && !empty($destinfo['extension'])) { $dest .= '.' . $destinfo['extension']; } } $result = @rename($source, $dest); if ($result) { $result = $dest; } } } else { $result = @rename($source, $dest); if ($result) { $result = $dest; } } } return $result; }
/** * Send message * * @access public * @param integer $user User id * @param array $messageData Message data * @return mixed Message Id or Jaws_Error on failure */ function SendMessage($user, $messageData) { $table = Jaws_ORM::getInstance(); // merge recipient users & groups to an array $recipient_users = array(); if (trim($messageData['recipient_users']) == '0' || !empty($messageData['recipient_users'])) { if (trim($messageData['recipient_users']) == '0') { $table = $table->table('users'); $recipient_users = $table->select('id:integer')->fetchColumn(); } else { $recipient_users = explode(",", $messageData['recipient_users']); } } if (!empty($messageData['recipient_groups'])) { $recipient_groups = explode(",", $messageData['recipient_groups']); $table = $table->table('users_groups'); $table->select('user_id:integer'); $table->join('groups', 'groups.id', 'users_groups.group_id'); $table->where('group_id', $recipient_groups, 'in'); $group_users = $table->and()->where('groups.owner', $user)->fetchColumn(); if (!empty($group_users) && count($group_users) > 0) { $recipient_users = array_merge($recipient_users, $group_users); } } $recipient_users = array_unique($recipient_users); // validation input fields if (empty($messageData['subject']) || $messageData['folder'] != PrivateMessage_Info::PRIVATEMESSAGE_FOLDER_DRAFT && (empty($recipient_users) || count($recipient_users) <= 0)) { return Jaws_Error::raiseError(_t('PRIVATEMESSAGE_MESSAGE_INCOMPLETE_FIELDS'), __FUNCTION__, JAWS_ERROR_NOTICE); } $mTable = $table->table('pm_messages'); //Start Transaction $mTable->beginTransaction(); $messageIds = array(); $data = array(); $data['folder'] = $messageData['folder']; $data['subject'] = $messageData['subject']; $data['body'] = $messageData['body']; $data['attachments'] = isset($messageData['attachments']) ? count($messageData['attachments']) : 0; $data['recipient_users'] = $messageData['recipient_users']; $data['recipient_groups'] = isset($messageData['recipient_groups']) ? $messageData['recipient_groups'] : null; $data['update_time'] = time(); // Detect notification, draft or publish? $is_notification = $messageData['folder'] == PrivateMessage_Info::PRIVATEMESSAGE_FOLDER_NOTIFICATIONS; if ($messageData['folder'] == PrivateMessage_Info::PRIVATEMESSAGE_FOLDER_DRAFT) { if (empty($messageData['id'])) { // save new draft message $data['from'] = $user; $data['to'] = 0; $data['read'] = true; $data['insert_time'] = time(); $senderMessageId = $mTable->insert($data)->exec(); } else { // update old message info $senderMessageId = $messageData['id']; $mTable->update($data)->where('id', $senderMessageId)->exec(); } } else { // First insert a message in sender's outbox if (empty($messageData['id'])) { // new message if ($is_notification) { $senderMessageId = 0; } else { $data['folder'] = PrivateMessage_Info::PRIVATEMESSAGE_FOLDER_OUTBOX; $data['from'] = $user; $data['to'] = 0; $data['read'] = true; $data['insert_time'] = time(); $senderMessageId = $mTable->insert($data)->exec(); } } else { // update message $mTable->update($data)->where('id', $messageData['id'])->exec(); $senderMessageId = $messageData['id']; } // Insert message for every recipient if (!empty($recipient_users) && count($recipient_users) > 0) { $table = $table->table('pm_messages'); $from = $is_notification ? 0 : $user; $data['folder'] = $messageData['folder']; foreach ($recipient_users as $recipient_user) { $data['insert_time'] = time(); $data['from'] = $from; $data['to'] = $recipient_user; $data['read'] = false; $messageId = $table->insert($data)->exec(); if (Jaws_Error::IsError($messageId)) { //Rollback Transaction $table->rollback(); return false; } $messageIds[] = $messageId; // send notification on new private message if (!$is_notification) { $params = array(); $params['key'] = crc32('PrivateMessage' . $senderMessageId); $params['title'] = _t('PRIVATEMESSAGE_NEW_MESSAGE_NOTIFICATION_TITLE'); $params['summary'] = _t('PRIVATEMESSAGE_NEW_MESSAGE_NOTIFICATION'); $params['description'] = _t('PRIVATEMESSAGE_NEW_MESSAGE_NOTIFICATION_DESC', $data['subject']); $params['user'] = (int) $recipient_user; $this->gadget->event->shout('Notify', $params); } } } } // Insert attachments info if (!empty($messageData['attachments']) && count($messageData['attachments']) > 0) { $maData = array(); $pm_dir = JAWS_DATA . 'pm' . DIRECTORY_SEPARATOR . 'attachments' . DIRECTORY_SEPARATOR; foreach ($messageData['attachments'] as $attachment) { // check new attachments file -- we must copy tmp files to correct location if (is_array($attachment)) { $src_filepath = Jaws_Utils::upload_tmp_dir() . '/' . $attachment['filename']; $dest_filepath = $pm_dir . $attachment['filename']; if (!file_exists($src_filepath)) { continue; } if (!file_exists($pm_dir)) { if (!Jaws_Utils::mkdir($pm_dir)) { return new Jaws_Error(_t('GLOBAL_ERROR_FAILED_CREATING_DIR', JAWS_DATA)); } } $cres = Jaws_Utils::rename($src_filepath, $dest_filepath); Jaws_Utils::delete($src_filepath); if ($cres) { $aData = array('title' => $attachment['title'], 'filename' => $attachment['filename'], 'filesize' => $attachment['filesize'], 'filetype' => $attachment['filetype']); $table = $table->table('pm_attachments'); $attachmentId = $table->insert($aData)->exec(); if (Jaws_Error::IsError($attachmentId)) { //Rollback Transaction $table->rollback(); return false; } // Add sender message Id to pm_message_attachment table $maData[] = array('message' => $senderMessageId, 'attachment' => $attachmentId); // Add recipient message Id to pm_message_attachment table foreach ($messageIds as $messageId) { $maData[] = array('message' => $messageId, 'attachment' => $attachmentId); } } } else { // Add sender message Id to pm_message_attachment table $maData[] = array('message' => $senderMessageId, 'attachment' => $attachment); // Add recipient message Id to pm_message_attachment table foreach ($messageIds as $messageId) { $maData[] = array('message' => $messageId, 'attachment' => $attachment); } } } if (!empty($maData) && count($maData) > 0) { $table = $table->table('pm_message_attachment'); $res = $table->insertAll(array('message', 'attachment'), $maData)->exec(); if (Jaws_Error::IsError($res)) { //Rollback Transaction $table->rollback(); return false; } } else { //Rollback Transaction $table->rollback(); return false; } } //Commit Transaction $mTable->commit(); return $senderMessageId; }
/** * Logs the message to a file specified on the dest parameter * * @access public * @param string $priority How to log * @param string $file Filename * @param string $line File line number * @param string $msg Message to log * @param string $opts Options(log file name, ...) * @return void */ function LogToFile($priority, $file, $line, $msg, $opts) { if (isset($opts['file'])) { $logfile = $opts['file']; } else { trigger_error("You need to set at least the filename for Jaws_Log::LogToFile", E_USER_ERROR); } // log file rotation if (isset($opts['size']) && @filesize($logfile) >= $opts['size']) { Jaws_Utils::rename($logfile, $logfile . '.' . time()); } if (false !== ($fh = @fopen($logfile, 'a+'))) { fwrite($fh, $this->getLogString($priority, $file, $line, $msg) . "\n"); fclose($fh); } }
/** * Updates file * * @access public * @return array Response array */ function UpdateFile() { try { // Validate data $data = jaws()->request->fetch(array('id', 'title', 'description', 'parent', 'hidden', 'user_filename', 'host_filename', 'filetype', 'filesize')); if (empty($data['title'])) { throw new Exception(_t('DIRECTORY_ERROR_INCOMPLETE_DATA')); } $data['title'] = Jaws_XSS::defilter($data['title']); $data['description'] = Jaws_XSS::defilter($data['description']); $model = $this->gadget->model->loadAdmin('Files'); // Validate file $id = (int) $data['id']; $file = $model->GetFile($id); if (Jaws_Error::IsError($file)) { throw new Exception($file->getMessage()); } // Upload file $path = $GLOBALS['app']->getDataURL('directory'); if (!is_dir($path)) { if (!Jaws_Utils::mkdir($path, 2)) { throw new Exception('DIRECTORY_ERROR_FILE_UPLOAD'); } } $res = Jaws_Utils::UploadFiles($_FILES, $path, '', null); if (Jaws_Error::IsError($res)) { throw new Exception($res->getMessage()); } else { if ($res !== false) { $data['host_filename'] = $res['file'][0]['host_filename']; $data['user_filename'] = $res['file'][0]['user_filename']; $data['filetype'] = $res['file'][0]['host_filetype']; $data['filesize'] = $res['file'][0]['host_filesize']; } else { if ($data['host_filename'] === ':nochange:') { unset($data['host_filename']); } else { if (empty($data['host_filename'])) { throw new Exception(_t('DIRECTORY_ERROR_FILE_UPLOAD')); } else { $filename = Jaws_Utils::upload_tmp_dir() . '/' . $data['host_filename']; if (file_exists($filename)) { $target = $path . '/' . $data['host_filename']; $res = Jaws_Utils::rename($filename, $target, false); if ($res === false) { throw new Exception(_t('DIRECTORY_ERROR_FILE_UPLOAD')); } $data['host_filename'] = basename($res); } else { throw new Exception(_t('DIRECTORY_ERROR_FILE_UPLOAD')); } } } } } // Update file in database unset($data['user']); $data['updatetime'] = time(); $data['hidden'] = $data['hidden'] ? true : false; $model = $this->gadget->model->loadAdmin('Files'); $res = $model->Update($id, $data); if (Jaws_Error::IsError($res)) { throw new Exception(_t('DIRECTORY_ERROR_FILE_UPDATE')); } // Update Tags if (Jaws_Gadget::IsGadgetInstalled('Tags')) { $tags = jaws()->request->fetch('tags'); $tModel = Jaws_Gadget::getInstance('Tags')->model->loadAdmin('Tags'); $tModel->UpdateReferenceTags('Directory', 'file', $id, !$data['hidden'], time(), $tags); } } catch (Exception $e) { return $GLOBALS['app']->Session->GetResponse($e->getMessage(), RESPONSE_ERROR); } return $GLOBALS['app']->Session->GetResponse(_t('DIRECTORY_NOTICE_FILE_UPDATED'), RESPONSE_NOTICE); }
/** * Updates file * * @access public * @return array Response array */ function UpdateFile() { try { // Validate data $data = jaws()->request->fetch(array('id', 'title', 'description', 'parent', 'url', 'filename', 'filetype', 'filesize')); if (empty($data['title'])) { throw new Exception(_t('DIRECTORY_ERROR_INCOMPLETE_DATA')); } $data['title'] = Jaws_XSS::defilter($data['title']); $data['description'] = Jaws_XSS::defilter($data['description']); $model = $this->gadget->model->load('Files'); // Validate file $id = (int) $data['id']; $file = $model->GetFile($id); if (Jaws_Error::IsError($file)) { throw new Exception($file->getMessage()); } // Validate user $user = (int) $GLOBALS['app']->Session->GetAttribute('user'); if ($file['user'] != $user) { throw new Exception(_t('DIRECTORY_ERROR_FILE_UPDATE')); } // Upload file if ($file['user'] != $file['owner']) { // is shortcut unset($data['parent'], $data['url'], $data['filename']); unset($data['filetype'], $data['filesize']); } else { $path = $GLOBALS['app']->getDataURL('directory/' . $user); if (!is_dir($path)) { if (!Jaws_Utils::mkdir($path, 2)) { throw new Exception('DIRECTORY_ERROR_FILE_UPLOAD'); } } $res = Jaws_Utils::UploadFiles($_FILES, $path); if (Jaws_Error::IsError($res)) { throw new Exception($res->getMessage()); } else { if ($res !== false) { $data['filename'] = $res['file'][0]['host_filename']; $data['filetype'] = $res['file'][0]['host_filetype']; $data['filesize'] = $res['file'][0]['host_filesize']; } else { if ($data['filename'] === ':nochange:') { unset($data['filename']); } else { if (empty($data['filename'])) { throw new Exception(_t('DIRECTORY_ERROR_FILE_UPLOAD')); } else { $filename = Jaws_Utils::upload_tmp_dir() . '/' . $data['filename']; if (file_exists($filename)) { $target = $path . '/' . $data['filename']; $res = Jaws_Utils::rename($filename, $target, false); if ($res === false) { throw new Exception(_t('DIRECTORY_ERROR_FILE_UPLOAD')); } $data['filename'] = basename($res); } else { throw new Exception(_t('DIRECTORY_ERROR_FILE_UPLOAD')); } } } } } } // Update file in database $data['updatetime'] = time(); $model = $this->gadget->model->load('Files'); $res = $model->Update($id, $data); if (Jaws_Error::IsError($res)) { throw new Exception(_t('DIRECTORY_ERROR_FILE_UPDATE')); } // Update shortcuts if ($file['shared']) { $shortcut = array(); $shortcut['url'] = $data['url']; $shortcut['filename'] = $data['filename']; $shortcut['filetype'] = $data['filetype']; $shortcut['filesize'] = $data['filesize']; $shortcut['updatetime'] = $data['updatetime']; $model->UpdateShortcuts($id, $shortcut); } } catch (Exception $e) { return $GLOBALS['app']->Session->GetResponse($e->getMessage(), RESPONSE_ERROR); } return $GLOBALS['app']->Session->GetResponse(_t('DIRECTORY_NOTICE_FILE_UPDATED'), RESPONSE_NOTICE); }