* Copyright (c) 2010, 2011, 2012
* Thorsten Glaser <t.glaser@tarent.de>
* Copyright 2010-2011, Alain Peyrat - Alcatel-Lucent
+ * Copyright 2013, Franck Villaume - TrivialDev
*
* This file is part of FusionForge. FusionForge is free software;
* you can redistribute it and/or modify it under the terms of the
}
}
+/* util_is_valid_repository_name() - Verifies whether a repository name is valid
+ *
+ * @param string The name to verify
+ * @returns true on success/false on error
+ *
+ */
+function util_is_valid_repository_name ($file) {
+ //bad char test
+ $invalidchars = preg_replace("/[-A-Z0-9+_\.]/i","",$file);
+
+ if (!empty($invalidchars)) {
+ return false;
+ }
+ if (strstr($file,'..')) {
+ return false;
+ }
+ return true;
+}
+
/**
* valid_hostname() - Validates a hostname string to make sure it doesn't contain invalid characters
*
function check_email_available($group, $email, &$response) {
// Check if a mailing list with same name already exists
- $mlFactory = new MailingListFactory($group);
- if (!$mlFactory || !is_object($mlFactory) || $mlFactory->isError()) {
- $response .= $mlFactory->getErrorMessage();
- return false;
- }
- $mlArray = $mlFactory->getMailingLists();
- if ($mlFactory->isError()) {
- $response .= $mlFactory->getErrorMessage();
- return false;
- }
- for ($j = 0; $j < count($mlArray); $j++) {
- $currentList =& $mlArray[$j];
- if ($email == $currentList->getName()) {
- $response .= _('Error: a mailing list with the same email address already exists.');
+ if ($group->usesMail()) {
+ $mlFactory = new MailingListFactory($group);
+ if (!$mlFactory || !is_object($mlFactory) || $mlFactory->isError()) {
+ $response .= $mlFactory->getErrorMessage();
+ return false;
+ }
+ $mlArray = $mlFactory->getMailingLists();
+ if ($mlFactory->isError()) {
+ $response .= $mlFactory->getErrorMessage();
return false;
}
+ for ($j = 0; $j < count($mlArray); $j++) {
+ $currentList =& $mlArray[$j];
+ if ($email == $currentList->getName()) {
+ $response .= _('Error: a mailing list with the same email address already exists.');
+ return false;
+ }
+ }
}
// Check if a forum with same name already exists
- $ff = new ForumFactory($group);
- if (!$ff || !is_object($ff) || $ff->isError()) {
- $response .= $ff->getErrorMessage();
- return false;
- }
- $farr = $ff->getForums();
- $prefix = $group->getUnixName() . '-';
- for ($j = 0; $j < count($farr); $j++) {
- if (is_object($farr[$j])) {
- if ($email == $prefix . $farr[$j]->getName()) {
- $response .= _('Error: a forum with the same email address already exists.');
- return false;
+ if ($group->usesForum()) {
+ $ff = new ForumFactory($group);
+ if (!$ff || !is_object($ff) || $ff->isError()) {
+ $response .= $ff->getErrorMessage();
+ return false;
+ }
+ $farr = $ff->getForums();
+ $prefix = $group->getUnixName() . '-';
+ for ($j = 0; $j < count($farr); $j++) {
+ if (is_object($farr[$j])) {
+ if ($email == $prefix . $farr[$j]->getName()) {
+ $response .= _('Error: a forum with the same email address already exists.');
+ return false;
+ }
}
}
}
}
/**
- * Adds the Javascript file to the lits to be used
- * @param unknown_type $js
+ * Adds the Javascript file to the list to be used
+ * @param string $js
*/
function use_javascript($js) {
return $GLOBALS['HTML']->addJavascript($js);
}
/**
- * TODO: Enter description here ...
- * @param unknown_type $script
- * @param unknown_type $default_content_type
- * @return Ambiguous
+ * util_negociate_alternate_content_types() - Manage content-type negociation based on 'script_accepted_types' hooks
+ * @param string $script
+ * @param string $default_content_type
+ * @param string $forced_content_type
+ * @return string
*/
-function util_negociate_alternate_content_types($script, $default_content_type) {
-
- // Invoke plugins' hooks 'script_accepted_types' to discover which alternate content types they would accept for /users/...
- $hook_params = array();
- $hook_params['script'] = $script;
- $hook_params['accepted_types'] = array();
- plugin_hook_by_reference('script_accepted_types', $hook_params);
- if (count($hook_params['accepted_types'])) {
- // By default, text/html is accepted
- $accepted_types = array($default_content_type);
- $new_accepted_types = $hook_params['accepted_types'];
- $accepted_types = array_merge($accepted_types, $new_accepted_types);
-
- // PEAR::HTTP (for negotiateMimeType())
- require_once 'HTTP.php';
-
- // negociate accepted content-type depending on the preferred ones declared by client
- $http=new HTTP();
- $content_type = $http->negotiateMimeType($accepted_types, false);
- } else {
- $content_type = $default_content_type;
+function util_negociate_alternate_content_types($script, $default_content_type, $forced_content_type=false) {
+
+ $content_type = $default_content_type;
+
+ // we can force the content-type to be returned automaticall if necessary
+ if ($forced_content_type) {
+ // TODO ideally, in this case we could try and apply the negociation to see if it matches
+ // one provided by the hooks, but negotiateMimeType() doesn't allow this so for the moment,
+ // we just force it whatever the hooks support
+ $content_type = $forced_content_type;
+ }
+ else {
+ // Invoke plugins' hooks 'script_accepted_types' to discover which alternate content types they would accept for /users/...
+ $hook_params = array();
+ $hook_params['script'] = $script;
+ $hook_params['accepted_types'] = array();
+
+ plugin_hook_by_reference('script_accepted_types', $hook_params);
+
+ if (count($hook_params['accepted_types'])) {
+ // By default, text/html is accepted
+ $accepted_types = array($default_content_type);
+ $new_accepted_types = $hook_params['accepted_types'];
+ $accepted_types = array_merge($accepted_types, $new_accepted_types);
+
+ // PEAR::HTTP (for negotiateMimeType())
+ require_once 'HTTP.php';
+
+ // negociate accepted content-type depending on the preferred ones declared by client
+ $http=new HTTP();
+ $content_type = $http->negotiateMimeType($accepted_types, false);
+ }
}
return $content_type;
}
return $text;
}
+function util_is_html($string) {
+ return (strip_tags(util_unconvert_htmlspecialchars($string)) != $string);
+}
+
+function util_init_messages() {
+ global $feedback, $warning_msg, $error_msg;
+
+ if (PHP_SAPI == 'cli') {
+ $feedback = $warning_msg = $error_msg = '';
+ } else {
+ $feedback = getStringFromCookie('feedback', '');
+ if ($feedback) setcookie('feedback', '', time()-3600, '/');
+
+ $warning_msg = getStringFromCookie('warning_msg', '');
+ if ($warning_msg) setcookie('warning_msg', '', time()-3600, '/');
+
+ $error_msg = getStringFromCookie('error_msg', '');
+ if ($error_msg) setcookie('error_msg', '', time()-3600, '/');
+ }
+}
+
+function util_save_messages() {
+ global $feedback, $warning_msg, $error_msg;
+
+ setcookie('feedback', $feedback, time() + 10, '/');
+ setcookie('warning_msg', $warning_msg, time() + 10, '/');
+ setcookie('error_msg', $error_msg, time() + 10, '/');
+}
+
+/**
+ * util_create_file_with_contents() — Securely create (or replace) a file with given contents
+ *
+ * @param string $path Path of the file to be created
+ * @param string $contents Contents of the file
+ *
+ * @return boolean FALSE on error
+ */
+function util_create_file_with_contents($path, $contents) {
+ if (file_exists($path) && !unlink($path)) {
+ return false;
+ }
+ $handle = fopen($path, "x+");
+ if ($handle == false) {
+ return false;
+ }
+ fwrite($handle, $contents);
+ fclose($handle);
+ return true;
+}
+
+/**
+ * Create a directory in the system temp directory with a hard-to-predict name.
+ * Does not have the guarantees of the actual BSD libc function or Python tempfile function.
+ * @param string $suffix Append to the new directory's name
+ * @param string $prefix Prepend to the new directory's name
+ * @return string The path of the new directory.
+ *
+ * Mostly taken from https://gist.github.com/1407245 as a "temporary"
+ * workaround to https://bugs.php.net/bug.php?id=49211
+ */
+function util_mkdtemp($suffix = '', $prefix = 'tmp') {
+ $tempdir = sys_get_temp_dir();
+ for ($i=0; $i<5; $i++) {
+ $id = strtr(base64_encode(util_randbytes(6)), '+/', '-_');
+ $path = "{$tempdir}/{$prefix}{$id}{$suffix}";
+ if (mkdir($path, 0700)) {
+ return $path;
+ }
+ }
+ return false;
+}
+
+/**
+ * Run a function with only the permissions of a given Unix user
+ * Function can be an anonymous
+ * Optional arguments in an array
+ * @param string Unix user name
+ * @param function function to run (possibly anonymous)
+ * @param array parameters
+ * @return boolean true on success, false on error
+ */
+function util_sudo_effective_user($username, $function, $params=array()) {
+ $saved_egid = posix_getegid();
+ $saved_euid = posix_geteuid();
+
+ $userinfo = posix_getpwnam($username);
+ if ($userinfo === False) {
+ return False;
+ }
+ if (posix_setegid($userinfo['gid']) &&
+ ($saved_euid != 0 || posix_initgroups($username, $userinfo['gid'])) &&
+ posix_seteuid($userinfo['uid'])) {
+ $function($params);
+ }
+
+ posix_setegid($saved_egid);
+ posix_seteuid($saved_euid);
+ if ($saved_euid == 0)
+ posix_initgroups("root", 0);
+}
// Local Variables:
// mode: php