1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566 |
- <?php
- require_once DRUPAL_ROOT . '/includes/stream_wrappers.inc';
- define('FILE_CREATE_DIRECTORY', 1);
- define('FILE_MODIFY_PERMISSIONS', 2);
- define('FILE_EXISTS_RENAME', 0);
- define('FILE_EXISTS_REPLACE', 1);
- define('FILE_EXISTS_ERROR', 2);
- define('FILE_STATUS_PERMANENT', 1);
- function file_get_stream_wrappers($filter = STREAM_WRAPPERS_ALL) {
- $wrappers_storage = &drupal_static(__FUNCTION__);
- if (!isset($wrappers_storage)) {
- $wrappers = module_invoke_all('stream_wrappers');
- foreach ($wrappers as $scheme => $info) {
-
- $wrappers[$scheme] += array('type' => STREAM_WRAPPERS_NORMAL);
- }
- drupal_alter('stream_wrappers', $wrappers);
- $existing = stream_get_wrappers();
- foreach ($wrappers as $scheme => $info) {
-
- if (in_array('DrupalStreamWrapperInterface', class_implements($info['class']), TRUE)) {
-
- if (in_array($scheme, $existing, TRUE)) {
- $wrappers[$scheme]['override'] = TRUE;
- stream_wrapper_unregister($scheme);
- }
- else {
- $wrappers[$scheme]['override'] = FALSE;
- }
- if (($info['type'] & STREAM_WRAPPERS_LOCAL) == STREAM_WRAPPERS_LOCAL) {
- stream_wrapper_register($scheme, $info['class']);
- }
- else {
- stream_wrapper_register($scheme, $info['class'], STREAM_IS_URL);
- }
- }
-
- $wrappers_storage[STREAM_WRAPPERS_ALL][$scheme] = $wrappers[$scheme];
- if (($info['type'] & STREAM_WRAPPERS_WRITE_VISIBLE) == STREAM_WRAPPERS_WRITE_VISIBLE) {
- $wrappers_storage[STREAM_WRAPPERS_WRITE_VISIBLE][$scheme] = $wrappers[$scheme];
- }
- }
- }
- if (!isset($wrappers_storage[$filter])) {
- $wrappers_storage[$filter] = array();
- foreach ($wrappers_storage[STREAM_WRAPPERS_ALL] as $scheme => $info) {
-
- if (($info['type'] & $filter) == $filter) {
- $wrappers_storage[$filter][$scheme] = $info;
- }
- }
- }
- return $wrappers_storage[$filter];
- }
- function file_stream_wrapper_get_class($scheme) {
- $wrappers = file_get_stream_wrappers();
- return empty($wrappers[$scheme]) ? FALSE : $wrappers[$scheme]['class'];
- }
- function file_uri_scheme($uri) {
- $position = strpos($uri, '://');
- return $position ? substr($uri, 0, $position) : FALSE;
- }
- function file_stream_wrapper_valid_scheme($scheme) {
-
- $class = file_stream_wrapper_get_class($scheme);
- if (class_exists($class)) {
- return TRUE;
- }
- else {
- return FALSE;
- }
- }
- function file_uri_target($uri) {
- $data = explode('://', $uri, 2);
-
- return count($data) == 2 ? trim($data[1], '\/') : FALSE;
- }
- function file_default_scheme() {
- return variable_get('file_default_scheme', 'public');
- }
- function file_stream_wrapper_uri_normalize($uri) {
- $scheme = file_uri_scheme($uri);
- if ($scheme && file_stream_wrapper_valid_scheme($scheme)) {
- $target = file_uri_target($uri);
- if ($target !== FALSE) {
- $uri = $scheme . '://' . $target;
- }
- }
- return $uri;
- }
- function file_stream_wrapper_get_instance_by_uri($uri) {
- $scheme = file_uri_scheme($uri);
- $class = file_stream_wrapper_get_class($scheme);
- if (class_exists($class)) {
- $instance = new $class();
- $instance->setUri($uri);
- return $instance;
- }
- else {
- return FALSE;
- }
- }
- function file_stream_wrapper_get_instance_by_scheme($scheme) {
- $class = file_stream_wrapper_get_class($scheme);
- if (class_exists($class)) {
- $instance = new $class();
- $instance->setUri($scheme . '://');
- return $instance;
- }
- else {
- return FALSE;
- }
- }
- function file_create_url($uri) {
-
-
- drupal_alter('file_url', $uri);
- $scheme = file_uri_scheme($uri);
- if (!$scheme) {
-
-
-
-
-
-
-
- if (drupal_substr($uri, 0, 1) == '/') {
- return $uri;
- }
- else {
-
-
- return $GLOBALS['base_url'] . '/' . drupal_encode_path($uri);
- }
- }
- elseif ($scheme == 'http' || $scheme == 'https') {
-
-
- return $uri;
- }
- else {
-
- if ($wrapper = file_stream_wrapper_get_instance_by_uri($uri)) {
- return $wrapper->getExternalUrl();
- }
- else {
- return FALSE;
- }
- }
- }
- function file_prepare_directory(&$directory, $options = FILE_MODIFY_PERMISSIONS) {
- if (!file_stream_wrapper_valid_scheme(file_uri_scheme($directory))) {
-
- $directory = rtrim($directory, '/\\');
- }
-
- if (!is_dir($directory)) {
-
-
- if (($options & FILE_CREATE_DIRECTORY) && @drupal_mkdir($directory, NULL, TRUE)) {
- return drupal_chmod($directory);
- }
- return FALSE;
- }
-
- $writable = is_writable($directory);
- if (!$writable && ($options & FILE_MODIFY_PERMISSIONS)) {
- return drupal_chmod($directory);
- }
- return $writable;
- }
- function file_ensure_htaccess() {
- file_create_htaccess('public://', FALSE);
- if (variable_get('file_private_path', FALSE)) {
- file_create_htaccess('private://', TRUE);
- }
- file_create_htaccess('temporary://', TRUE);
- }
- function file_create_htaccess($directory, $private = TRUE, $force_overwrite = FALSE) {
- if (file_uri_scheme($directory)) {
- $directory = file_stream_wrapper_uri_normalize($directory);
- }
- else {
- $directory = rtrim($directory, '/\\');
- }
- $htaccess_path = $directory . '/.htaccess';
- if (file_exists($htaccess_path) && !$force_overwrite) {
-
- return;
- }
- $htaccess_lines = file_htaccess_lines($private);
-
- if (file_put_contents($htaccess_path, $htaccess_lines)) {
- drupal_chmod($htaccess_path, 0444);
- }
- else {
- $variables = array('%directory' => $directory, '!htaccess' => '<br />' . nl2br(check_plain($htaccess_lines)));
- watchdog('security', "Security warning: Couldn't write .htaccess file. Please create a .htaccess file in your %directory directory which contains the following lines: <code>!htaccess</code>", $variables, WATCHDOG_ERROR);
- }
- }
- function file_htaccess_lines($private = TRUE) {
- $lines = <<<EOF
- # Turn off all options we don't need.
- Options None
- Options +FollowSymLinks
- # Set the catch-all handler to prevent scripts from being executed.
- SetHandler Drupal_Security_Do_Not_Remove_See_SA_2006_006
- <Files *>
- # Override the handler again if we're run later in the evaluation list.
- SetHandler Drupal_Security_Do_Not_Remove_See_SA_2013_003
- </Files>
- # If we know how to do it safely, disable the PHP engine entirely.
- <IfModule mod_php5.c>
- php_flag engine off
- </IfModule>
- EOF;
- if ($private) {
- $lines = "Deny from all\n\n" . $lines;
- }
- return $lines;
- }
- function file_load_multiple($fids = array(), $conditions = array()) {
- return entity_load('file', $fids, $conditions);
- }
- function file_load($fid) {
- $files = file_load_multiple(array($fid), array());
- return reset($files);
- }
- function file_save(stdClass $file) {
- $file->timestamp = REQUEST_TIME;
- $file->filesize = filesize($file->uri);
-
- if (!empty($file->fid) && !isset($file->original)) {
- $file->original = entity_load_unchanged('file', $file->fid);
- }
- module_invoke_all('file_presave', $file);
- module_invoke_all('entity_presave', $file, 'file');
- if (empty($file->fid)) {
- drupal_write_record('file_managed', $file);
-
- module_invoke_all('file_insert', $file);
- module_invoke_all('entity_insert', $file, 'file');
- }
- else {
- drupal_write_record('file_managed', $file, 'fid');
-
- module_invoke_all('file_update', $file);
- module_invoke_all('entity_update', $file, 'file');
- }
-
- unset($file->original);
-
- entity_get_controller('file')->resetCache(array($file->fid));
- return $file;
- }
- function file_usage_list(stdClass $file) {
- $result = db_select('file_usage', 'f')
- ->fields('f', array('module', 'type', 'id', 'count'))
- ->condition('fid', $file->fid)
- ->condition('count', 0, '>')
- ->execute();
- $references = array();
- foreach ($result as $usage) {
- $references[$usage->module][$usage->type][$usage->id] = $usage->count;
- }
- return $references;
- }
- function file_usage_add(stdClass $file, $module, $type, $id, $count = 1) {
- db_merge('file_usage')
- ->key(array(
- 'fid' => $file->fid,
- 'module' => $module,
- 'type' => $type,
- 'id' => $id,
- ))
- ->fields(array('count' => $count))
- ->expression('count', 'count + :count', array(':count' => $count))
- ->execute();
- }
- function file_usage_delete(stdClass $file, $module, $type = NULL, $id = NULL, $count = 1) {
-
- $query = db_delete('file_usage')
- ->condition('module', $module)
- ->condition('fid', $file->fid);
- if ($type && $id) {
- $query
- ->condition('type', $type)
- ->condition('id', $id);
- }
- if ($count) {
- $query->condition('count', $count, '<=');
- }
- $result = $query->execute();
-
- if (!$result && $count > 0) {
- $query = db_update('file_usage')
- ->condition('module', $module)
- ->condition('fid', $file->fid);
- if ($type && $id) {
- $query
- ->condition('type', $type)
- ->condition('id', $id);
- }
- $query->expression('count', 'count - :count', array(':count' => $count));
- $query->execute();
- }
- }
- function file_copy(stdClass $source, $destination = NULL, $replace = FILE_EXISTS_RENAME) {
- if (!file_valid_uri($destination)) {
- if (($realpath = drupal_realpath($source->uri)) !== FALSE) {
- watchdog('file', 'File %file (%realpath) could not be copied, because the destination %destination is invalid. This is often caused by improper use of file_copy() or a missing stream wrapper.', array('%file' => $source->uri, '%realpath' => $realpath, '%destination' => $destination));
- }
- else {
- watchdog('file', 'File %file could not be copied, because the destination %destination is invalid. This is often caused by improper use of file_copy() or a missing stream wrapper.', array('%file' => $source->uri, '%destination' => $destination));
- }
- drupal_set_message(t('The specified file %file could not be copied, because the destination is invalid. More information is available in the system log.', array('%file' => $source->uri)), 'error');
- return FALSE;
- }
- if ($uri = file_unmanaged_copy($source->uri, $destination, $replace)) {
- $file = clone $source;
- $file->fid = NULL;
- $file->uri = $uri;
- $file->filename = drupal_basename($uri);
-
- if ($replace == FILE_EXISTS_REPLACE) {
- $existing_files = file_load_multiple(array(), array('uri' => $uri));
- if (count($existing_files)) {
- $existing = reset($existing_files);
- $file->fid = $existing->fid;
- $file->filename = $existing->filename;
- }
- }
-
-
- elseif ($replace == FILE_EXISTS_RENAME && is_file($destination)) {
- $file->filename = drupal_basename($destination);
- }
- $file = file_save($file);
-
- module_invoke_all('file_copy', $file, $source);
- return $file;
- }
- return FALSE;
- }
- function file_valid_uri($uri) {
-
- $uri_scheme = file_uri_scheme($uri);
- if (empty($uri_scheme) || !file_stream_wrapper_valid_scheme($uri_scheme)) {
- return FALSE;
- }
- return TRUE;
- }
- function file_unmanaged_copy($source, $destination = NULL, $replace = FILE_EXISTS_RENAME) {
- $original_source = $source;
- $original_destination = $destination;
-
- if (!file_exists($source)) {
-
- drupal_set_message(t('The specified file %file could not be copied, because no file by that name exists. Please check that you supplied the correct filename.', array('%file' => $original_source)), 'error');
- if (($realpath = drupal_realpath($original_source)) !== FALSE) {
- watchdog('file', 'File %file (%realpath) could not be copied because it does not exist.', array('%file' => $original_source, '%realpath' => $realpath));
- }
- else {
- watchdog('file', 'File %file could not be copied because it does not exist.', array('%file' => $original_source));
- }
- return FALSE;
- }
-
- if (!isset($destination)) {
- $destination = file_build_uri(drupal_basename($source));
- }
-
- if (file_prepare_directory($destination)) {
-
- $destination = file_stream_wrapper_uri_normalize($destination . '/' . drupal_basename($source));
- }
- else {
-
- $dirname = drupal_dirname($destination);
- if (!file_prepare_directory($dirname)) {
-
- watchdog('file', 'File %file could not be copied, because the destination directory %destination is not configured correctly.', array('%file' => $original_source, '%destination' => $dirname));
- drupal_set_message(t('The specified file %file could not be copied, because the destination directory is not properly configured. This may be caused by a problem with file or directory permissions. More information is available in the system log.', array('%file' => $original_source)), 'error');
- return FALSE;
- }
- }
-
- $destination = file_destination($destination, $replace);
- if ($destination === FALSE) {
- drupal_set_message(t('The file %file could not be copied because a file by that name already exists in the destination directory.', array('%file' => $original_source)), 'error');
- watchdog('file', 'File %file could not be copied because a file by that name already exists in the destination directory (%directory)', array('%file' => $original_source, '%directory' => $destination));
- return FALSE;
- }
-
- $real_source = drupal_realpath($source);
- $real_destination = drupal_realpath($destination);
- if ($source == $destination || ($real_source !== FALSE) && ($real_source == $real_destination)) {
- drupal_set_message(t('The specified file %file was not copied because it would overwrite itself.', array('%file' => $source)), 'error');
- watchdog('file', 'File %file could not be copied because it would overwrite itself.', array('%file' => $source));
- return FALSE;
- }
-
- file_ensure_htaccess();
-
- if (!@copy($source, $destination)) {
-
-
- if ($real_source === FALSE || $real_destination === FALSE || !@copy($real_source, $real_destination)) {
- watchdog('file', 'The specified file %file could not be copied to %destination.', array('%file' => $source, '%destination' => $destination), WATCHDOG_ERROR);
- return FALSE;
- }
- }
-
- drupal_chmod($destination);
- return $destination;
- }
- function file_build_uri($path) {
- $uri = file_default_scheme() . '://' . $path;
- return file_stream_wrapper_uri_normalize($uri);
- }
- function file_destination($destination, $replace) {
- if (file_exists($destination)) {
- switch ($replace) {
- case FILE_EXISTS_REPLACE:
-
- break;
- case FILE_EXISTS_RENAME:
- $basename = drupal_basename($destination);
- $directory = drupal_dirname($destination);
- $destination = file_create_filename($basename, $directory);
- break;
- case FILE_EXISTS_ERROR:
-
- return FALSE;
- }
- }
- return $destination;
- }
- function file_move(stdClass $source, $destination = NULL, $replace = FILE_EXISTS_RENAME) {
- if (!file_valid_uri($destination)) {
- if (($realpath = drupal_realpath($source->uri)) !== FALSE) {
- watchdog('file', 'File %file (%realpath) could not be moved, because the destination %destination is invalid. This may be caused by improper use of file_move() or a missing stream wrapper.', array('%file' => $source->uri, '%realpath' => $realpath, '%destination' => $destination));
- }
- else {
- watchdog('file', 'File %file could not be moved, because the destination %destination is invalid. This may be caused by improper use of file_move() or a missing stream wrapper.', array('%file' => $source->uri, '%destination' => $destination));
- }
- drupal_set_message(t('The specified file %file could not be moved, because the destination is invalid. More information is available in the system log.', array('%file' => $source->uri)), 'error');
- return FALSE;
- }
- if ($uri = file_unmanaged_move($source->uri, $destination, $replace)) {
- $delete_source = FALSE;
- $file = clone $source;
- $file->uri = $uri;
-
- if ($replace == FILE_EXISTS_REPLACE) {
- $existing_files = file_load_multiple(array(), array('uri' => $uri));
- if (count($existing_files)) {
- $existing = reset($existing_files);
- $delete_source = TRUE;
- $file->fid = $existing->fid;
- }
- }
-
-
- elseif ($replace == FILE_EXISTS_RENAME && is_file($destination)) {
- $file->filename = drupal_basename($destination);
- }
- $file = file_save($file);
-
- module_invoke_all('file_move', $file, $source);
- if ($delete_source) {
-
- file_delete($source);
- }
- return $file;
- }
- return FALSE;
- }
- function file_unmanaged_move($source, $destination = NULL, $replace = FILE_EXISTS_RENAME) {
- $filepath = file_unmanaged_copy($source, $destination, $replace);
- if ($filepath == FALSE || file_unmanaged_delete($source) == FALSE) {
- return FALSE;
- }
- return $filepath;
- }
- function file_munge_filename($filename, $extensions, $alerts = TRUE) {
- $original = $filename;
-
- if (!variable_get('allow_insecure_uploads', 0)) {
-
- $filename = str_replace(chr(0), '', $filename);
- $whitelist = array_unique(explode(' ', strtolower(trim($extensions))));
-
-
- $filename_parts = explode('.', $filename);
- $new_filename = array_shift($filename_parts);
- $final_extension = array_pop($filename_parts);
-
-
-
- foreach ($filename_parts as $filename_part) {
- $new_filename .= '.' . $filename_part;
- if (!in_array(strtolower($filename_part), $whitelist) && preg_match("/^[a-zA-Z]{2,5}\d?$/", $filename_part)) {
- $new_filename .= '_';
- }
- }
- $filename = $new_filename . '.' . $final_extension;
- if ($alerts && $original != $filename) {
- drupal_set_message(t('For security reasons, your upload has been renamed to %filename.', array('%filename' => $filename)));
- }
- }
- return $filename;
- }
- function file_unmunge_filename($filename) {
- return str_replace('_.', '.', $filename);
- }
- function file_create_filename($basename, $directory) {
-
-
- $basename = preg_replace('/[\x00-\x1F]/u', '_', $basename);
- if (substr(PHP_OS, 0, 3) == 'WIN') {
-
- $basename = str_replace(array(':', '*', '?', '"', '<', '>', '|'), '_', $basename);
- }
-
- if (substr($directory, -1) == '/') {
- $separator = '';
- }
- else {
- $separator = '/';
- }
- $destination = $directory . $separator . $basename;
- if (file_exists($destination)) {
-
- $pos = strrpos($basename, '.');
- if ($pos !== FALSE) {
- $name = substr($basename, 0, $pos);
- $ext = substr($basename, $pos);
- }
- else {
- $name = $basename;
- $ext = '';
- }
- $counter = 0;
- do {
- $destination = $directory . $separator . $name . '_' . $counter++ . $ext;
- } while (file_exists($destination));
- }
- return $destination;
- }
- function file_delete(stdClass $file, $force = FALSE) {
- if (!file_valid_uri($file->uri)) {
- if (($realpath = drupal_realpath($file->uri)) !== FALSE) {
- watchdog('file', 'File %file (%realpath) could not be deleted because it is not a valid URI. This may be caused by improper use of file_delete() or a missing stream wrapper.', array('%file' => $file->uri, '%realpath' => $realpath));
- }
- else {
- watchdog('file', 'File %file could not be deleted because it is not a valid URI. This may be caused by improper use of file_delete() or a missing stream wrapper.', array('%file' => $file->uri));
- }
- drupal_set_message(t('The specified file %file could not be deleted, because it is not a valid URI. More information is available in the system log.', array('%file' => $file->uri)), 'error');
- return FALSE;
- }
-
-
-
- if (!$force && ($references = file_usage_list($file))) {
- return $references;
- }
-
- module_invoke_all('file_delete', $file);
- module_invoke_all('entity_delete', $file, 'file');
-
-
- if (file_unmanaged_delete($file->uri)) {
- db_delete('file_managed')->condition('fid', $file->fid)->execute();
- db_delete('file_usage')->condition('fid', $file->fid)->execute();
- entity_get_controller('file')->resetCache();
- return TRUE;
- }
- return FALSE;
- }
- function file_unmanaged_delete($path) {
- if (is_dir($path)) {
- watchdog('file', '%path is a directory and cannot be removed using file_unmanaged_delete().', array('%path' => $path), WATCHDOG_ERROR);
- return FALSE;
- }
- if (is_file($path)) {
- return drupal_unlink($path);
- }
-
-
- if (!file_exists($path)) {
- watchdog('file', 'The file %path was not deleted, because it does not exist.', array('%path' => $path), WATCHDOG_NOTICE);
- return TRUE;
- }
-
-
- watchdog('file', 'The file %path is not of a recognized type so it was not deleted.', array('%path' => $path), WATCHDOG_ERROR);
- return FALSE;
- }
- function file_unmanaged_delete_recursive($path) {
- if (is_dir($path)) {
- $dir = dir($path);
- while (($entry = $dir->read()) !== FALSE) {
- if ($entry == '.' || $entry == '..') {
- continue;
- }
- $entry_path = $path . '/' . $entry;
- file_unmanaged_delete_recursive($entry_path);
- }
- $dir->close();
- return drupal_rmdir($path);
- }
- return file_unmanaged_delete($path);
- }
- function file_space_used($uid = NULL, $status = FILE_STATUS_PERMANENT) {
- $query = db_select('file_managed', 'f');
- $query->condition('f.status', $status);
- $query->addExpression('SUM(f.filesize)', 'filesize');
- if (isset($uid)) {
- $query->condition('f.uid', $uid);
- }
- return $query->execute()->fetchField();
- }
- function file_save_upload($form_field_name, $validators = array(), $destination = FALSE, $replace = FILE_EXISTS_RENAME) {
- global $user;
- static $upload_cache;
-
-
- if (isset($upload_cache[$form_field_name])) {
- return $upload_cache[$form_field_name];
- }
-
- if (empty($_FILES['files']['name'][$form_field_name])) {
- return NULL;
- }
-
-
-
- switch ($_FILES['files']['error'][$form_field_name]) {
- case UPLOAD_ERR_INI_SIZE:
- case UPLOAD_ERR_FORM_SIZE:
- drupal_set_message(t('The file %file could not be saved, because it exceeds %maxsize, the maximum allowed size for uploads.', array('%file' => $_FILES['files']['name'][$form_field_name], '%maxsize' => format_size(file_upload_max_size()))), 'error');
- return FALSE;
- case UPLOAD_ERR_PARTIAL:
- case UPLOAD_ERR_NO_FILE:
- drupal_set_message(t('The file %file could not be saved, because the upload did not complete.', array('%file' => $_FILES['files']['name'][$form_field_name])), 'error');
- return FALSE;
- case UPLOAD_ERR_OK:
-
-
- if (is_uploaded_file($_FILES['files']['tmp_name'][$form_field_name])) {
- break;
- }
-
- default:
- drupal_set_message(t('The file %file could not be saved. An unknown error has occurred.', array('%file' => $_FILES['files']['name'][$form_field_name])), 'error');
- return FALSE;
- }
-
- $file = new stdClass();
- $file->uid = $user->uid;
- $file->status = 0;
- $file->filename = trim(drupal_basename($_FILES['files']['name'][$form_field_name]), '.');
- $file->uri = $_FILES['files']['tmp_name'][$form_field_name];
- $file->filemime = file_get_mimetype($file->filename);
- $file->filesize = $_FILES['files']['size'][$form_field_name];
- $extensions = '';
- if (isset($validators['file_validate_extensions'])) {
- if (isset($validators['file_validate_extensions'][0])) {
-
- $extensions = $validators['file_validate_extensions'][0];
- }
- else {
-
-
-
- unset($validators['file_validate_extensions']);
- }
- }
- else {
-
-
- $extensions = 'jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp';
- $validators['file_validate_extensions'] = array();
- $validators['file_validate_extensions'][0] = $extensions;
- }
- if (!empty($extensions)) {
-
-
- $file->filename = file_munge_filename($file->filename, $extensions);
- }
-
-
-
-
- if (!variable_get('allow_insecure_uploads', 0) && preg_match('/\.(php|pl|py|cgi|asp|js)(\.|$)/i', $file->filename) && (substr($file->filename, -4) != '.txt')) {
- $file->filemime = 'text/plain';
- $file->uri .= '.txt';
- $file->filename .= '.txt';
-
-
- if (!empty($extensions)) {
- $validators['file_validate_extensions'][0] .= ' txt';
- drupal_set_message(t('For security reasons, your upload has been renamed to %filename.', array('%filename' => $file->filename)));
- }
- }
-
- if (empty($destination)) {
- $destination = 'temporary://';
- }
-
- $destination_scheme = file_uri_scheme($destination);
- if (!$destination_scheme || !file_stream_wrapper_valid_scheme($destination_scheme)) {
- drupal_set_message(t('The file could not be uploaded, because the destination %destination is invalid.', array('%destination' => $destination)), 'error');
- return FALSE;
- }
- $file->source = $form_field_name;
-
- if (substr($destination, -1) != '/') {
- $destination .= '/';
- }
- $file->destination = file_destination($destination . $file->filename, $replace);
-
-
- if ($file->destination === FALSE) {
- drupal_set_message(t('The file %source could not be uploaded because a file by that name already exists in the destination %directory.', array('%source' => $form_field_name, '%directory' => $destination)), 'error');
- return FALSE;
- }
-
- $validators['file_validate_name_length'] = array();
-
- $errors = file_validate($file, $validators);
-
- if (!empty($errors)) {
- $message = t('The specified file %name could not be uploaded.', array('%name' => $file->filename));
- if (count($errors) > 1) {
- $message .= theme('item_list', array('items' => $errors));
- }
- else {
- $message .= ' ' . array_pop($errors);
- }
- form_set_error($form_field_name, $message);
- return FALSE;
- }
-
-
-
- $file->uri = $file->destination;
- if (!drupal_move_uploaded_file($_FILES['files']['tmp_name'][$form_field_name], $file->uri)) {
- form_set_error($form_field_name, t('File upload error. Could not move uploaded file.'));
- watchdog('file', 'Upload error. Could not move uploaded file %file to destination %destination.', array('%file' => $file->filename, '%destination' => $file->uri));
- return FALSE;
- }
-
- drupal_chmod($file->uri);
-
- if ($replace == FILE_EXISTS_REPLACE) {
- $existing_files = file_load_multiple(array(), array('uri' => $file->uri));
- if (count($existing_files)) {
- $existing = reset($existing_files);
- $file->fid = $existing->fid;
- }
- }
-
- if ($file = file_save($file)) {
-
- $upload_cache[$form_field_name] = $file;
- return $file;
- }
- return FALSE;
- }
- function drupal_move_uploaded_file($filename, $uri) {
- $result = @move_uploaded_file($filename, $uri);
-
-
-
- if (!$result) {
- if ($realpath = drupal_realpath($uri)) {
- $result = move_uploaded_file($filename, $realpath);
- }
- else {
- $result = move_uploaded_file($filename, $uri);
- }
- }
- return $result;
- }
- function file_validate(stdClass &$file, $validators = array()) {
-
- $errors = array();
- foreach ($validators as $function => $args) {
- if (function_exists($function)) {
- array_unshift($args, $file);
- $errors = array_merge($errors, call_user_func_array($function, $args));
- }
- }
-
- return array_merge($errors, module_invoke_all('file_validate', $file));
- }
- function file_validate_name_length(stdClass $file) {
- $errors = array();
- if (empty($file->filename)) {
- $errors[] = t("The file's name is empty. Please give a name to the file.");
- }
- if (strlen($file->filename) > 240) {
- $errors[] = t("The file's name exceeds the 240 characters limit. Please rename the file and try again.");
- }
- return $errors;
- }
- function file_validate_extensions(stdClass $file, $extensions) {
- $errors = array();
- $regex = '/\.(' . preg_replace('/ +/', '|', preg_quote($extensions)) . ')$/i';
- if (!preg_match($regex, $file->filename)) {
- $errors[] = t('Only files with the following extensions are allowed: %files-allowed.', array('%files-allowed' => $extensions));
- }
- return $errors;
- }
- function file_validate_size(stdClass $file, $file_limit = 0, $user_limit = 0) {
- global $user;
- $errors = array();
- if ($file_limit && $file->filesize > $file_limit) {
- $errors[] = t('The file is %filesize exceeding the maximum file size of %maxsize.', array('%filesize' => format_size($file->filesize), '%maxsize' => format_size($file_limit)));
- }
-
- if ($user_limit && (file_space_used($user->uid) + $file->filesize) > $user_limit) {
- $errors[] = t('The file is %filesize which would exceed your disk quota of %quota.', array('%filesize' => format_size($file->filesize), '%quota' => format_size($user_limit)));
- }
- return $errors;
- }
- function file_validate_is_image(stdClass $file) {
- $errors = array();
- $info = image_get_info($file->uri);
- if (!$info || empty($info['extension'])) {
- $errors[] = t('Only JPEG, PNG and GIF images are allowed.');
- }
- return $errors;
- }
- function file_validate_image_resolution(stdClass $file, $maximum_dimensions = 0, $minimum_dimensions = 0) {
- $errors = array();
-
- if ($info = image_get_info($file->uri)) {
- if ($maximum_dimensions) {
-
- list($width, $height) = explode('x', $maximum_dimensions);
- if ($info['width'] > $width || $info['height'] > $height) {
-
- if ($image = image_load($file->uri)) {
- image_scale($image, $width, $height);
- image_save($image);
- $file->filesize = $image->info['file_size'];
- drupal_set_message(t('The image was resized to fit within the maximum allowed dimensions of %dimensions pixels.', array('%dimensions' => $maximum_dimensions)));
- }
- else {
- $errors[] = t('The image is too large; the maximum dimensions are %dimensions pixels.', array('%dimensions' => $maximum_dimensions));
- }
- }
- }
- if ($minimum_dimensions) {
-
- list($width, $height) = explode('x', $minimum_dimensions);
- if ($info['width'] < $width || $info['height'] < $height) {
- $errors[] = t('The image is too small; the minimum dimensions are %dimensions pixels.', array('%dimensions' => $minimum_dimensions));
- }
- }
- }
- return $errors;
- }
- function file_save_data($data, $destination = NULL, $replace = FILE_EXISTS_RENAME) {
- global $user;
- if (empty($destination)) {
- $destination = file_default_scheme() . '://';
- }
- if (!file_valid_uri($destination)) {
- watchdog('file', 'The data could not be saved because the destination %destination is invalid. This may be caused by improper use of file_save_data() or a missing stream wrapper.', array('%destination' => $destination));
- drupal_set_message(t('The data could not be saved, because the destination is invalid. More information is available in the system log.'), 'error');
- return FALSE;
- }
- if ($uri = file_unmanaged_save_data($data, $destination, $replace)) {
-
- $file = new stdClass();
- $file->fid = NULL;
- $file->uri = $uri;
- $file->filename = drupal_basename($uri);
- $file->filemime = file_get_mimetype($file->uri);
- $file->uid = $user->uid;
- $file->status = FILE_STATUS_PERMANENT;
-
- if ($replace == FILE_EXISTS_REPLACE) {
- $existing_files = file_load_multiple(array(), array('uri' => $uri));
- if (count($existing_files)) {
- $existing = reset($existing_files);
- $file->fid = $existing->fid;
- $file->filename = $existing->filename;
- }
- }
-
-
- elseif ($replace == FILE_EXISTS_RENAME && is_file($destination)) {
- $file->filename = drupal_basename($destination);
- }
- return file_save($file);
- }
- return FALSE;
- }
- function file_unmanaged_save_data($data, $destination = NULL, $replace = FILE_EXISTS_RENAME) {
-
- $temp_name = drupal_tempnam('temporary://', 'file');
- if (file_put_contents($temp_name, $data) === FALSE) {
- drupal_set_message(t('The file could not be created.'), 'error');
- return FALSE;
- }
-
- return file_unmanaged_move($temp_name, $destination, $replace);
- }
- function file_transfer($uri, $headers) {
- if (ob_get_level()) {
- ob_end_clean();
- }
- foreach ($headers as $name => $value) {
- drupal_add_http_header($name, $value);
- }
- drupal_send_headers();
- $scheme = file_uri_scheme($uri);
-
- if ($scheme && file_stream_wrapper_valid_scheme($scheme) && $fd = fopen($uri, 'rb')) {
- while (!feof($fd)) {
- print fread($fd, 1024);
- }
- fclose($fd);
- }
- else {
- drupal_not_found();
- }
- drupal_exit();
- }
- function file_download() {
-
- $args = func_get_args();
- $scheme = array_shift($args);
- $target = implode('/', $args);
- $uri = $scheme . '://' . $target;
- if (file_stream_wrapper_valid_scheme($scheme) && file_exists($uri)) {
- $headers = file_download_headers($uri);
- if (count($headers)) {
- file_transfer($uri, $headers);
- }
- drupal_access_denied();
- }
- else {
- drupal_not_found();
- }
- drupal_exit();
- }
- function file_download_headers($uri) {
-
-
-
-
- $headers = array();
- foreach (module_implements('file_download') as $module) {
- $function = $module . '_file_download';
- $result = $function($uri);
- if ($result == -1) {
-
- $headers = array();
- break;
- }
- if (isset($result) && is_array($result)) {
- $headers = array_merge($headers, $result);
- }
- }
- return $headers;
- }
- function file_download_access($uri) {
- return count(file_download_headers($uri)) > 0;
- }
- function file_scan_directory($dir, $mask, $options = array(), $depth = 0) {
-
- $options += array(
- 'nomask' => '/(\.\.?|CVS)$/',
- 'callback' => 0,
- 'recurse' => TRUE,
- 'key' => 'uri',
- 'min_depth' => 0,
- );
- $options['key'] = in_array($options['key'], array('uri', 'filename', 'name')) ? $options['key'] : 'uri';
- $files = array();
- if (is_dir($dir) && $handle = opendir($dir)) {
- while (FALSE !== ($filename = readdir($handle))) {
- if (!preg_match($options['nomask'], $filename) && $filename[0] != '.') {
- $uri = "$dir/$filename";
- $uri = file_stream_wrapper_uri_normalize($uri);
- if (is_dir($uri) && $options['recurse']) {
-
- $files = array_merge(file_scan_directory($uri, $mask, $options, $depth + 1), $files);
- }
- elseif ($depth >= $options['min_depth'] && preg_match($mask, $filename)) {
-
-
- $file = new stdClass();
- $file->uri = $uri;
- $file->filename = $filename;
- $file->name = pathinfo($filename, PATHINFO_FILENAME);
- $key = $options['key'];
- $files[$file->$key] = $file;
- if ($options['callback']) {
- $options['callback']($uri);
- }
- }
- }
- }
- closedir($handle);
- }
- return $files;
- }
- function file_upload_max_size() {
- static $max_size = -1;
- if ($max_size < 0) {
-
- $max_size = parse_size(ini_get('post_max_size'));
-
-
- $upload_max = parse_size(ini_get('upload_max_filesize'));
- if ($upload_max > 0 && $upload_max < $max_size) {
- $max_size = $upload_max;
- }
- }
- return $max_size;
- }
- function file_get_mimetype($uri, $mapping = NULL) {
- if ($wrapper = file_stream_wrapper_get_instance_by_uri($uri)) {
- return $wrapper->getMimeType($uri, $mapping);
- }
- else {
-
-
- return DrupalLocalStreamWrapper::getMimeType($uri, $mapping);
- }
- }
- function drupal_chmod($uri, $mode = NULL) {
- if (!isset($mode)) {
- if (is_dir($uri)) {
- $mode = variable_get('file_chmod_directory', 0775);
- }
- else {
- $mode = variable_get('file_chmod_file', 0664);
- }
- }
-
-
-
- if ($wrapper = file_stream_wrapper_get_instance_by_uri($uri)) {
- if ($wrapper->chmod($mode)) {
- return TRUE;
- }
- }
- else {
- if (@chmod($uri, $mode)) {
- return TRUE;
- }
- }
- watchdog('file', 'The file permissions could not be set on %uri.', array('%uri' => $uri), WATCHDOG_ERROR);
- return FALSE;
- }
- function drupal_unlink($uri, $context = NULL) {
- $scheme = file_uri_scheme($uri);
- if ((!$scheme || !file_stream_wrapper_valid_scheme($scheme)) && (substr(PHP_OS, 0, 3) == 'WIN')) {
- chmod($uri, 0600);
- }
- if ($context) {
- return unlink($uri, $context);
- }
- else {
- return unlink($uri);
- }
- }
- function drupal_realpath($uri) {
-
-
-
- if ($wrapper = file_stream_wrapper_get_instance_by_uri($uri)) {
- return $wrapper->realpath();
- }
-
-
-
-
- elseif (!empty($uri)) {
- return realpath($uri);
- }
- return FALSE;
- }
- function drupal_dirname($uri) {
- $scheme = file_uri_scheme($uri);
- if ($scheme && file_stream_wrapper_valid_scheme($scheme)) {
- return file_stream_wrapper_get_instance_by_scheme($scheme)->dirname($uri);
- }
- else {
- return dirname($uri);
- }
- }
- function drupal_basename($uri, $suffix = NULL) {
- $separators = '/';
- if (DIRECTORY_SEPARATOR != '/') {
-
- $separators .= DIRECTORY_SEPARATOR;
- }
-
- $uri = rtrim($uri, $separators);
-
-
- $filename = preg_match('@[^' . preg_quote($separators, '@') . ']+$@', $uri, $matches) ? $matches[0] : '';
-
- if ($suffix) {
- $filename = preg_replace('@' . preg_quote($suffix, '@') . '$@', '', $filename);
- }
- return $filename;
- }
- function drupal_mkdir($uri, $mode = NULL, $recursive = FALSE, $context = NULL) {
- if (!isset($mode)) {
- $mode = variable_get('file_chmod_directory', 0775);
- }
- if (!isset($context)) {
- return mkdir($uri, $mode, $recursive);
- }
- else {
- return mkdir($uri, $mode, $recursive, $context);
- }
- }
- function drupal_rmdir($uri, $context = NULL) {
- $scheme = file_uri_scheme($uri);
- if ((!$scheme || !file_stream_wrapper_valid_scheme($scheme)) && (substr(PHP_OS, 0, 3) == 'WIN')) {
- chmod($uri, 0700);
- }
- if ($context) {
- return rmdir($uri, $context);
- }
- else {
- return rmdir($uri);
- }
- }
- function drupal_tempnam($directory, $prefix) {
- $scheme = file_uri_scheme($directory);
- if ($scheme && file_stream_wrapper_valid_scheme($scheme)) {
- $wrapper = file_stream_wrapper_get_instance_by_scheme($scheme);
- if ($filename = tempnam($wrapper->getDirectoryPath(), $prefix)) {
- return $scheme . '://' . drupal_basename($filename);
- }
- else {
- return FALSE;
- }
- }
- else {
-
- return tempnam($directory, $prefix);
- }
- }
- function file_directory_temp() {
- $temporary_directory = variable_get('file_temporary_path', NULL);
- if (empty($temporary_directory)) {
- $directories = array();
-
- if (ini_get('upload_tmp_dir')) {
- $directories[] = ini_get('upload_tmp_dir');
- }
-
- if (substr(PHP_OS, 0, 3) == 'WIN') {
- $directories[] = 'c:\\windows\\temp';
- $directories[] = 'c:\\winnt\\temp';
- }
- else {
- $directories[] = '/tmp';
- }
-
-
-
- if (function_exists('sys_get_temp_dir')) {
- $directories[] = sys_get_temp_dir();
- }
- foreach ($directories as $directory) {
- if (is_dir($directory) && is_writable($directory)) {
- $temporary_directory = $directory;
- break;
- }
- }
- if (empty($temporary_directory)) {
-
- $temporary_directory = variable_get('file_public_path', conf_path() . '/files') . '/tmp';
-
-
-
-
- $temporary_directory = str_replace('\\', '/', $temporary_directory);
- }
-
- variable_set('file_temporary_path', $temporary_directory);
- }
- return $temporary_directory;
- }
- function file_get_content_headers($file) {
- $name = mime_header_encode($file->filename);
- $type = mime_header_encode($file->filemime);
- return array(
- 'Content-Type' => $type,
- 'Content-Length' => $file->filesize,
- 'Cache-Control' => 'private',
- );
- }
|