123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568 |
- <?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) {
-
- $position = strpos($uri, '://');
- $scheme = $position ? substr($uri, 0, $position) : FALSE;
- 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',
- );
- }
|