public function onResponseSent() { if (!$this->settings->hasSetting("debug_log") or !$this->environment->request()) { return; } $path = $this->environment->request()->path(); if (count($path) > 0 and strcasecmp($path[0], "debug") == 0) { return; } $log = $this->settings->setting("debug_log"); $handle = @fopen($log, "a"); if (!$handle) { Logging::logError("Could not write to log file: " . $log); return; } $trace = Logging::getTrace(); try { foreach ($trace as $d) { fwrite($handle, Util::toString($d)); } fclose($handle); } catch (Exception $e) { Logging::logError("Could not write to log file: " . $log); Logging::logException($e); } }
public function send($to, $subject, $message, $from = NULL, $attachments = NULL) { if (!$this->enabled) { return; } $isHtml = stripos($message, "<html>") !== FALSE; $f = $from != NULL ? $from : $this->env->settings()->setting("mail_notification_from"); $validRecipients = $this->getValidRecipients($to); if (count($validRecipients) === 0) { Logging::logDebug("No valid recipient email addresses, no mail sent"); return; } if (Logging::isDebug()) { Logging::logDebug("Sending mail from [" . $f . "] to [" . Util::array2str($validRecipients) . "]: [" . $message . "]"); } set_include_path("vendor/PHPMailer" . DIRECTORY_SEPARATOR . PATH_SEPARATOR . get_include_path()); require 'class.phpmailer.php'; $mailer = new PHPMailer(); $smtp = $this->env->settings()->setting("mail_smtp"); if ($smtp != NULL and isset($smtp["host"])) { $mailer->isSMTP(); $mailer->Host = $smtp["host"]; if (isset($smtp["username"]) and isset($smtp["password"])) { $mailer->SMTPAuth = true; $mailer->Username = $smtp["username"]; $mailer->Password = $smtp["password"]; } if (isset($smtp["secure"])) { $mailer->SMTPSecure = $smtp["secure"]; } } $mailer->From = $f; foreach ($validRecipients as $recipient) { $mailer->addBCC($recipient["email"], $recipient["name"]); } if (!$isHtml) { $mailer->WordWrap = 50; } else { $mailer->isHTML(true); } if ($attachments != NULL) { //TODO use stream foreach ($attachments as $attachment) { $mailer->addAttachment($attachment); } } $mailer->Subject = $subject; $mailer->Body = $message; try { if (!$mailer->send()) { Logging::logError('Message could not be sent: ' . $mailer->ErrorInfo); return FALSE; } return TRUE; } catch (Exception $e) { Logging::logError('Message could not be sent: ' . $e); return FALSE; } }
private function sendNotifications($notifications, $e) { Logging::logDebug("NOTIFICATOR: Found " . count($notifications) . " notifications for event: " . $e); if (!$this->env->features()->isFeatureEnabled("mail_notification")) { Logging::logError("Mail notification not enabled, notifications not sent"); return; } foreach ($notifications as $notification) { $this->sendNotification($notification, $e); } }
function globalErrorHandler($errno, $errstr, $errfile, $errline) { global $responseHandler; $info = "PHP error #" . $errno . ", " . $errstr . " (" . $errfile . ":" . $errline . ")"; Logging::logError($info . "\n" . Util::array2str(debug_backtrace())); if ($responseHandler == NULL) { $responseHandler = new ResponseHandler(new OutputHandler()); } $responseHandler->unknownServerError($info); die; }
function globalErrorHandler($errno, $errstr, $errfile, $errline) { global $responseHandler; $info = "PHP error #" . $errno . ", " . $errstr . " (" . $errfile . ":" . $errline . ")"; if (Logging::isDebug()) { $data = debug_backtrace(); } else { $msg = "Backtrace disabled as it may contain passwords. "; $msg .= "Enable 'debug' setting in configuration.php to see "; $msg .= "backtrace."; $data = array("NOTE" => $msg); } Logging::logError($info . "\n" . Util::array2str($data)); if ($responseHandler == NULL) { $responseHandler = new ResponseHandler(new OutputHandler()); } $responseHandler->unknownServerError($info); die; }
public function processGet() { if (count($this->path) > 2 or strcmp($this->path[0], 'items') != 0 and strcmp($this->path[0], 'all') != 0) { throw $this->invalidRequestException(); } if (strcmp($this->path[0], 'all') == 0) { $shares = $this->handler()->getUserShares(); $items = array(); $invalid = array(); foreach ($shares as $uk => $u) { foreach ($u as $ik => $i) { if (array_key_exists($ik, $items) || in_array($ik, $invalid)) { continue; } $item = NULL; try { $item = $this->item($ik); } catch (ServiceException $se) { Logging::logError("Invalid share item: " . $ik); $invalid[] = $ik; $items[$ik] = array("id" => $ik, "name" => "-"); continue; } if (!$item->exists()) { Logging::logError("Invalid share item (item does not exist): " . $ik); $invalid[] = $ik; $items[$ik] = array("id" => $ik, "name" => "-"); continue; } $items[$ik] = $item->data(); } } $this->response()->success(array("shares" => $shares, "items" => $items, "invalid" => $invalid)); return; } $itemId = $this->path[1]; if (strpos($itemId, "_") < 0) { $this->item($itemId); } $this->response()->success($this->handler()->getShares($itemId)); }
private function addUserProperties($id, $name, $plugin) { $groups = $plugin->getSetting("groups", array()); if (count($groups) > 0) { $existing = array(); foreach ($this->env->configuration()->getAllUserGroups() as $group) { if (in_array($group['id'], $groups)) { $existing[] = $group['id']; } } if (count($existing) > 0) { $this->env->configuration()->addUsersGroups($id, $existing); } } $folders = $plugin->getSetting("folders", array()); if (count($folders) > 0) { $existing = array(); foreach ($this->env->configuration()->getFolders() as $folder) { if (in_array($folder['id'], $folders)) { $existing[] = $folder['id']; } } if (count($existing) > 0) { $this->env->configuration()->addUserFolders($id, $existing); } } $userFolder = $plugin->getSetting("user_folder", NULL); if ($userFolder == NULL) { return; } // automatic user folder if (!isset($userFolder["path"])) { Logging::logError("Registration: missing configuration for user folder"); return; } $basePath = $userFolder["path"]; $folderName = $name; if (isset($userFolder["folder_name"])) { $folderName = $userFolder["folder_name"]; } $folderPath = rtrim($basePath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $name; $fs = $this->env->filesystem()->filesystem(array("path" => $folderPath, "name" => $folderName), FALSE); if ($fs->exists()) { Logging::logError("Registration: user folder [" . $folderPath . "] already exists, not added"); return; } if (!$fs->create()) { Logging::logError("Registration: user folder [" . $folderPath . "] could not be created, not added"); return; } $folderId = $this->env->configuration()->addFolder($name, $folderPath); $this->env->configuration()->addUserFolder($id, $folderId, $folderName); $fs = $this->env->filesystem()->filesystem(array("id" => $folderId, "path" => $folderPath, "name" => $folderName), FALSE); $this->env->configuration()->addItemPermission($fs->root()->id(), Authentication::PERMISSION_VALUE_READWRITE, $id); if (isset($userFolder["add_to_users"]) and count($userFolder["add_to_users"]) > 0) { $users = $userFolder["add_to_users"]; $existing = array(); foreach ($this->env->configuration()->getAllUsers() as $user) { if (in_array($user['id'], $users)) { $existing[] = $user['id']; } } if (count($existing) > 0) { $this->env->configuration()->addFolderUsers($folderId, $existing); } } }
private function onPhase($phase) { $this->processor->setPhase($phase); switch ($phase) { case 'db': $this->onPhaseDatabase(); break; case 'admin': $this->onPhaseAdmin(); break; case 'success': $this->processor->showPage("success"); break; default: Logging::logError("Invalid installer phase: " . $phase); die; } }
private function folderSizeRecursively($nativePath) { $files = scandir($nativePath); if (!$files) { throw new ServiceException("INVALID_PATH", $this->path); } //$ignored = $this->ignoredItems($this->publicPath($nativePath)); $size = 0; foreach ($files as $i => $name) { if (substr($name, 0, 1) == '.') { continue; } $fullPath = self::joinPath($nativePath, $name); if ($this->isItemIgnored($nativePath, $name, $fullPath)) { continue; } if (is_link($fullPath) and !file_exists($fullPath)) { Logging::logError("Symbolic link broken: " . $fullPath); continue; } if (is_dir($fullPath)) { $size = $size + $this->folderSizeRecursively($fullPath); continue; } $size = $size + filesize($fullPath); } return $size; }
private function processCustomPrepareGet($type, $id, $share) { if (!array_key_exists($type, $this->customShareHandlers)) { Logging::logError("No custom share handler found: " . $type); die; } $handler = $this->customShareHandlers[$type]; return $handler->processPrepareGetShare($id, $share); }
public function getUserByNameOrEmail($name) { $result = $this->db->query(sprintf("SELECT id, name, user_type, lang, email FROM " . $this->db->table("user") . " WHERE (name='%s' or email='%s') and is_group=0", $this->db->string($name), $this->db->string($name))); $matches = $result->count(); if ($matches === 0) { Logging::logError("No user found with name or email[" . $name . "]"); return NULL; } if ($matches > 1) { Logging::logError("Duplicate user found with name or email [" . $name . "]"); return FALSE; } return $result->firstRow(); }
public function processPost() { if (count($this->path) > 1) { throw $this->invalidRequestException(); } $data = $this->request->data; if (count($this->path) == 1 and $this->path[0] == "query") { $this->env->authentication()->assertAdmin(); $res = $this->handler()->processShareQuery($data); $items = array(); $invalid = array(); $nonFs = array(); foreach ($res["data"] as $s) { $s["active"] = $s["active"] == "1"; //process item $ik = $s["item_id"]; if (array_key_exists($ik, $items) || in_array($ik, $invalid)) { continue; } if (strpos($ik, "_") !== FALSE) { $s["nonfs"] = TRUE; $parts = explode("_", $ik); $item = $this->handler()->getCustomShareItem($parts[0], $parts[1]); if ($item == NULL) { continue; } $nonFs[] = array("id" => $ik, "type" => $parts[0], "name" => $item["name"]); continue; } $item = NULL; try { $item = $this->item($ik); } catch (ServiceException $se) { $s["invalid"] = TRUE; Logging::logError("Invalid share item: " . $ik); $invalid[] = $ik; $items[$ik] = array("id" => $ik, "name" => "-"); continue; } if (!$item->exists()) { $s["invalid"] = TRUE; Logging::logError("Invalid share item (item does not exist): " . $ik); $invalid[] = $ik; $items[$ik] = array("id" => $ik, "name" => "-"); continue; } $items[$ik] = $item->data(); } $res["items"] = $items; $res["invalid"] = $invalid; $res["nonfs"] = $nonFs; $this->response()->success($res); return; } if (!isset($data["item"]) or !isset($data["name"])) { throw $this->invalidRequestException("No data"); } if (count($this->path) != 0) { throw $this->invalidRequestException(); } $itemId = $data["item"]; if ($data["expiration"] and !is_numeric($data["expiration"])) { throw $this->invalidRequestException("Invalid datatype: expiration"); } $this->handler()->addShare($itemId, $data["name"], $data["type"], $data["expiration"], isset($data["active"]) ? $data["active"] : TRUE, $data["restriction"]); $this->response()->success($this->handler()->getShares($itemId)); }
private function addUserProperties($id, $registration, $plugin) { $name = $registration["name"]; $groups = $plugin->getSetting("groups", array()); if (count($groups) > 0) { $existing = array(); foreach ($this->env->configuration()->getAllUserGroups() as $group) { if (in_array($group['id'], $groups)) { $existing[] = $group['id']; } } if (count($existing) > 0) { $this->env->configuration()->addUsersGroups($id, $existing); } } $permissions = $plugin->getSetting("permissions", NULL); // add user default/generic permissions if ($permissions != NULL) { if (!is_array($permissions)) { $permissions = array("filesystem_item_access" => $permissions); } Logging::logDebug("Setting user permissions: " . Util::array2str($permissions)); foreach ($permissions as $pk => $pv) { //TODO validate permission key (pv) and value (pv) $this->env->permissions()->addGenericPermission($pk, $id, $pv); } } $folders = $plugin->getSetting("folders", array()); $folderIds = array(); $folderProps = array(); if (Util::isAssocArray($folders)) { $folderIds = array_keys($folders); $folderProps = $folders; } else { $folderIds = $folders; } if (count($folderIds) > 0) { $existing = array(); foreach ($this->env->configuration()->getFolders() as $folder) { if (in_array($folder['id'], $folderIds)) { $existing[] = $folder['id']; } } if (count($existing) > 0) { //$this->env->configuration()->addUserFolders($id, $existing); foreach ($existing as $f) { $fname = NULL; $fp = array_key_exists($f, $folderProps) ? $folderProps[$f] : array(); if (array_key_exists("name", $fp)) { $fname = $fp["name"]; } Logging::logDebug("Assigning user folder: " . $f . " (" . $fname . ")"); $this->env->configuration()->addUserFolder($id, $f, $fname); // add folder permissions if (array_key_exists("permissions", $fp)) { $fs = $this->env->filesystem()->filesystem($this->env->configuration()->getFolder($f)); $permissions = $fp["permissions"]; if (!is_array($permissions)) { $permissions = array("filesystem_item_access" => $permissions); } Logging::logDebug("Setting folder " . $f . " permissions: " . Util::array2str($permissions)); foreach ($permissions as $pk => $pv) { //TODO validate permission key (pv) and value (pv) $this->env->permissions()->addFilesystemPermission($fs->root(), $pk, $id, $pv); } } } } } $userFolder = $plugin->getSetting("user_folder", NULL); if ($userFolder == NULL) { return; } // automatic user folder if (!isset($userFolder["path"])) { Logging::logError("Registration: missing configuration for user folder"); return; } $basePath = $userFolder["path"]; $folderName = $name; if (isset($userFolder["folder_name"])) { $folderName = $userFolder["folder_name"]; } $folderPath = rtrim($basePath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $name; $type = "local"; $uf = array("type" => $type, "path" => $folderPath, "name" => $folderName); Logging::logDebug("Creating user folder " . Util::array2str($uf)); $fs = $this->env->filesystem()->filesystem($uf, FALSE); if ($fs->exists()) { Logging::logError("Registration: user folder [" . $folderPath . "] already exists, not added"); return; } if (!$fs->create()) { Logging::logError("Registration: user folder [" . $folderPath . "] could not be created, not added"); return; } $uf["id"] = $this->env->configuration()->addFolder($name, $folderPath); $this->env->configuration()->addUserFolder($id, $uf["id"], $uf["name"]); $fs = $this->env->filesystem()->filesystem($uf, FALSE); $fsroot = $fs->root(); // add user folder permissions $permissions = array(); if (isset($userFolder["permissions"])) { $permissions = $userFolder["permissions"]; if (!is_array($permissions)) { $permissions = array("filesystem_item_access" => $permissions); } } else { $permissions = array("filesystem_item_access" => FilesystemController::PERMISSION_LEVEL_READWRITEDELETE); } Logging::logDebug("Setting user folder permissions: " . Util::array2str($permissions)); foreach ($permissions as $pk => $pv) { //TODO validate permission key (pv) and value (pv) $this->env->permissions()->addFilesystemPermission($fsroot, $pk, $id, $pv); } // assign folder to other users if (isset($userFolder["add_to_users"]) and count($userFolder["add_to_users"]) > 0) { $users = $userFolder["add_to_users"]; $existing = array(); foreach ($this->env->configuration()->getAllUsers() as $user) { if (in_array($user['id'], $users)) { $existing[] = $user['id']; } } if (count($existing) > 0) { $this->env->configuration()->addFolderUsers($uf["id"], $existing); } } $this->env->events()->onEvent(RegistrationEvent::userFolderCreated($id, $registration['name'], $registration['email'], $registration["id"], $fsroot)); }
private function doRestoreItem($i, $originalItem) { $item = $this->env->filesystem()->item($i["item_id"]); //restore file/folder $src = $this->getItemPath($i["id"], $item->isFile()); $target = $originalItem->internalPath(); Logging::logDebug("Restoring item " . $i["id"] . " -> " . $target); if (!rename($src, $target)) { Logging::logError("Could not restore item from trash to " . $target); return FALSE; } //restore item id $this->env->filesystem()->itemIdProvider()->move($item, $i["folder_id"] . ":/" . $i["path"]); $this->dao()->removeItem($i["id"]); return $originalItem; }