elFinderVolumeDriver.class.php 254 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612
  1. <?php
  2. /**
  3. * Base class for elFinder volume.
  4. * Provide 2 layers:
  5. * 1. Public API (commands)
  6. * 2. abstract fs API
  7. * All abstract methods begin with "_"
  8. *
  9. * @author Dmitry (dio) Levashov
  10. * @author Troex Nevelin
  11. * @author Alexey Sukhotin
  12. * @method netmountPrepare(array $options)
  13. * @method postNetmount(array $options)
  14. */
  15. abstract class elFinderVolumeDriver
  16. {
  17. /**
  18. * Net mount key
  19. *
  20. * @var string
  21. **/
  22. public $netMountKey = '';
  23. /**
  24. * Request args
  25. * $_POST or $_GET values
  26. *
  27. * @var array
  28. */
  29. protected $ARGS = array();
  30. /**
  31. * Driver id
  32. * Must be started from letter and contains [a-z0-9]
  33. * Used as part of volume id
  34. *
  35. * @var string
  36. **/
  37. protected $driverId = 'a';
  38. /**
  39. * Volume id - used as prefix for files hashes
  40. *
  41. * @var string
  42. **/
  43. protected $id = '';
  44. /**
  45. * Flag - volume "mounted" and available
  46. *
  47. * @var bool
  48. **/
  49. protected $mounted = false;
  50. /**
  51. * Root directory path
  52. *
  53. * @var string
  54. **/
  55. protected $root = '';
  56. /**
  57. * Root basename | alias
  58. *
  59. * @var string
  60. **/
  61. protected $rootName = '';
  62. /**
  63. * Default directory to open
  64. *
  65. * @var string
  66. **/
  67. protected $startPath = '';
  68. /**
  69. * Base URL
  70. *
  71. * @var string
  72. **/
  73. protected $URL = '';
  74. /**
  75. * Path to temporary directory
  76. *
  77. * @var string
  78. */
  79. protected $tmp;
  80. /**
  81. * A file save destination path when a temporary content URL is required
  82. * on a network volume or the like
  83. * If not specified, it tries to use "Connector Path/../files/.tmb".
  84. *
  85. * @var string
  86. */
  87. protected $tmpLinkPath = '';
  88. /**
  89. * A file save destination URL when a temporary content URL is required
  90. * on a network volume or the like
  91. * If not specified, it tries to use "Connector URL/../files/.tmb".
  92. *
  93. * @var string
  94. */
  95. protected $tmpLinkUrl = '';
  96. /**
  97. * Thumbnails dir path
  98. *
  99. * @var string
  100. **/
  101. protected $tmbPath = '';
  102. /**
  103. * Is thumbnails dir writable
  104. *
  105. * @var bool
  106. **/
  107. protected $tmbPathWritable = false;
  108. /**
  109. * Thumbnails base URL
  110. *
  111. * @var string
  112. **/
  113. protected $tmbURL = '';
  114. /**
  115. * Thumbnails size in px
  116. *
  117. * @var int
  118. **/
  119. protected $tmbSize = 48;
  120. /**
  121. * Image manipulation lib name
  122. * auto|imagick|gd|convert
  123. *
  124. * @var string
  125. **/
  126. protected $imgLib = 'auto';
  127. /**
  128. * Video to Image converter
  129. *
  130. * @var array
  131. */
  132. protected $imgConverter = array();
  133. /**
  134. * Library to crypt files name
  135. *
  136. * @var string
  137. **/
  138. protected $cryptLib = '';
  139. /**
  140. * Archivers config
  141. *
  142. * @var array
  143. **/
  144. protected $archivers = array(
  145. 'create' => array(),
  146. 'extract' => array()
  147. );
  148. /**
  149. * Static var of $this->options['maxArcFilesSize']
  150. *
  151. * @var int|string
  152. */
  153. protected static $maxArcFilesSize;
  154. /**
  155. * Server character encoding
  156. *
  157. * @var string or null
  158. **/
  159. protected $encoding = null;
  160. /**
  161. * How many subdirs levels return for tree
  162. *
  163. * @var int
  164. **/
  165. protected $treeDeep = 1;
  166. /**
  167. * Errors from last failed action
  168. *
  169. * @var array
  170. **/
  171. protected $error = array();
  172. /**
  173. * Today 24:00 timestamp
  174. *
  175. * @var int
  176. **/
  177. protected $today = 0;
  178. /**
  179. * Yesterday 24:00 timestamp
  180. *
  181. * @var int
  182. **/
  183. protected $yesterday = 0;
  184. /**
  185. * Force make dirctory on extract
  186. *
  187. * @var int
  188. **/
  189. protected $extractToNewdir = 'auto';
  190. /**
  191. * Object configuration
  192. *
  193. * @var array
  194. **/
  195. protected $options = array(
  196. // Driver ID (Prefix of volume ID), Normally, the value specified for each volume driver is used.
  197. 'driverId' => '',
  198. // Id (Suffix of volume ID), Normally, the number incremented according to the specified number of volumes is used.
  199. 'id' => '',
  200. // revision id of root directory that uses for caching control of root stat
  201. 'rootRev' => '',
  202. // driver type it uses volume root's CSS class name. e.g. 'group' -> Adds 'elfinder-group' to CSS class name.
  203. 'type' => '',
  204. // root directory path
  205. 'path' => '',
  206. // Folder hash value on elFinder to be the parent of this volume
  207. 'phash' => '',
  208. // Folder hash value on elFinder to trash bin of this volume, it require 'copyJoin' to true
  209. 'trashHash' => '',
  210. // open this path on initial request instead of root path
  211. 'startPath' => '',
  212. // how many subdirs levels return per request
  213. 'treeDeep' => 1,
  214. // root url, not set to URL via the connector. If you want to hide the file URL, do not set this value. (replacement for old "fileURL" option)
  215. 'URL' => '',
  216. // enable onetime URL to a file - (true, false, 'auto' (true if a temporary directory is available) or callable (A function that return onetime URL))
  217. 'onetimeUrl' => 'auto',
  218. // directory link url to own manager url with folder hash (`true`, `false`, `'hide'`(No show) or default `'auto'`: URL is empty then `true` else `false`)
  219. 'dirUrlOwn' => 'auto',
  220. // directory separator. required by client to show paths correctly
  221. 'separator' => DIRECTORY_SEPARATOR,
  222. // Use '/' as directory separator when the path hash encode/decode on the Windows server too
  223. 'winHashFix' => false,
  224. // Server character encoding (default is '': UTF-8)
  225. 'encoding' => '',
  226. // for convert character encoding (default is '': Not change locale)
  227. 'locale' => '',
  228. // URL of volume icon image
  229. 'icon' => '',
  230. // CSS Class of volume root in tree
  231. 'rootCssClass' => '',
  232. // Items to disable session caching
  233. 'noSessionCache' => array(),
  234. // enable i18n folder name that convert name to elFinderInstance.messages['folder_'+name]
  235. 'i18nFolderName' => false,
  236. // Search timeout (sec)
  237. 'searchTimeout' => 30,
  238. // Search exclusion directory regex pattern (require demiliter e.g. '#/path/to/exclude_directory#i')
  239. 'searchExDirReg' => '',
  240. // library to crypt/uncrypt files names (not implemented)
  241. 'cryptLib' => '',
  242. // how to detect files mimetypes. (auto/internal/finfo/mime_content_type)
  243. 'mimeDetect' => 'auto',
  244. // mime.types file path (for mimeDetect==internal)
  245. 'mimefile' => '',
  246. // Static extension/MIME of general server side scripts to security issues
  247. 'staticMineMap' => array(
  248. 'php:*' => 'text/x-php',
  249. 'pht:*' => 'text/x-php',
  250. 'php3:*' => 'text/x-php',
  251. 'php4:*' => 'text/x-php',
  252. 'php5:*' => 'text/x-php',
  253. 'php7:*' => 'text/x-php',
  254. 'phtml:*' => 'text/x-php',
  255. 'cgi:*' => 'text/x-httpd-cgi',
  256. 'pl:*' => 'text/x-perl',
  257. 'asp:*' => 'text/x-asap',
  258. 'aspx:*' => 'text/x-asap',
  259. 'py:*' => 'text/x-python',
  260. 'rb:*' => 'text/x-ruby',
  261. 'jsp:*' => 'text/x-jsp'
  262. ),
  263. // mime type normalize map : Array '[ext]:[detected mime type]' => '[normalized mime]'
  264. 'mimeMap' => array(
  265. 'md:application/x-genesis-rom' => 'text/x-markdown',
  266. 'md:text/plain' => 'text/x-markdown',
  267. 'markdown:text/plain' => 'text/x-markdown',
  268. 'css:text/x-asm' => 'text/css',
  269. 'css:text/plain' => 'text/css',
  270. 'csv:text/plain' => 'text/csv',
  271. 'java:text/x-c' => 'text/x-java-source',
  272. 'json:text/plain' => 'application/json',
  273. 'sql:text/plain' => 'text/x-sql',
  274. 'rtf:text/rtf' => 'application/rtf',
  275. 'rtfd:text/rtfd' => 'application/rtfd',
  276. 'ico:image/vnd.microsoft.icon' => 'image/x-icon',
  277. 'svg:text/plain' => 'image/svg+xml',
  278. 'pxd:application/octet-stream' => 'image/x-pixlr-data',
  279. 'dng:image/tiff' => 'image/x-adobe-dng',
  280. 'sketch:application/zip' => 'image/x-sketch',
  281. 'sketch:application/octet-stream' => 'image/x-sketch',
  282. 'xcf:application/octet-stream' => 'image/x-xcf',
  283. 'amr:application/octet-stream' => 'audio/amr',
  284. 'm4a:video/mp4' => 'audio/mp4',
  285. 'oga:application/ogg' => 'audio/ogg',
  286. 'ogv:application/ogg' => 'video/ogg',
  287. 'zip:application/x-zip' => 'application/zip',
  288. 'm3u8:text/plain' => 'application/x-mpegURL',
  289. 'mpd:text/plain' => 'application/dash+xml',
  290. 'mpd:application/xml' => 'application/dash+xml',
  291. '*:application/x-dosexec' => 'application/x-executable',
  292. 'doc:application/vnd.ms-office' => 'application/msword',
  293. 'xls:application/vnd.ms-office' => 'application/vnd.ms-excel',
  294. 'ppt:application/vnd.ms-office' => 'application/vnd.ms-powerpoint',
  295. 'yml:text/plain' => 'text/x-yaml',
  296. 'ai:application/pdf' => 'application/postscript',
  297. 'cgm:text/plain' => 'image/cgm',
  298. 'dxf:text/plain' => 'image/vnd.dxf',
  299. 'dds:application/octet-stream' => 'image/vnd-ms.dds',
  300. 'hpgl:text/plain' => 'application/vnd.hp-hpgl',
  301. 'igs:text/plain' => 'model/iges',
  302. 'iges:text/plain' => 'model/iges',
  303. 'plt:application/octet-stream' => 'application/plt',
  304. 'plt:text/plain' => 'application/plt',
  305. 'sat:text/plain' => 'application/sat',
  306. 'step:text/plain' => 'application/step',
  307. 'stp:text/plain' => 'application/step'
  308. ),
  309. // An option to add MimeMap to the `mimeMap` option
  310. // Array '[ext]:[detected mime type]' => '[normalized mime]'
  311. 'additionalMimeMap' => array(),
  312. // MIME-Type of filetype detected as unknown
  313. 'mimeTypeUnknown' => 'application/octet-stream',
  314. // MIME regex of send HTTP header "Content-Disposition: inline" or allow preview in quicklook
  315. // '.' is allow inline of all of MIME types
  316. // '$^' is not allow inline of all of MIME types
  317. 'dispInlineRegex' => '^(?:(?:video|audio)|image/(?!.+\+xml)|application/(?:ogg|x-mpegURL|dash\+xml)|(?:text/plain|application/pdf)$)',
  318. // temporary content URL's base path
  319. 'tmpLinkPath' => '',
  320. // temporary content URL's base URL
  321. 'tmpLinkUrl' => '',
  322. // directory for thumbnails
  323. 'tmbPath' => '.tmb',
  324. // mode to create thumbnails dir
  325. 'tmbPathMode' => 0777,
  326. // thumbnails dir URL. Set it if store thumbnails outside root directory
  327. 'tmbURL' => '',
  328. // thumbnails size (px)
  329. 'tmbSize' => 48,
  330. // thumbnails crop (true - crop, false - scale image to fit thumbnail size)
  331. 'tmbCrop' => true,
  332. // thumbnail URL require custom data as the GET query
  333. 'tmbReqCustomData' => false,
  334. // thumbnails background color (hex #rrggbb or 'transparent')
  335. 'tmbBgColor' => 'transparent',
  336. // image rotate fallback background color (hex #rrggbb)
  337. 'bgColorFb' => '#ffffff',
  338. // image manipulations library (imagick|gd|convert|auto|none, none - Does not check the image library at all.)
  339. 'imgLib' => 'auto',
  340. // Fallback self image to thumbnail (nothing imgLib)
  341. 'tmbFbSelf' => true,
  342. // Video to Image converters ['TYPE or MIME' => ['func' => function($file){ /* Converts $file to Image */ return true; }, 'maxlen' => (int)TransferLength]]
  343. 'imgConverter' => array(),
  344. // Max length of transfer to image converter
  345. 'tmbVideoConvLen' => 10000000,
  346. // Captre point seccond
  347. 'tmbVideoConvSec' => 6,
  348. // Life time (hour) for thumbnail garbage collection ("0" means no GC)
  349. 'tmbGcMaxlifeHour' => 0,
  350. // Percentage of garbage collection executed for thumbnail creation command ("1" means "1%")
  351. 'tmbGcPercentage' => 1,
  352. // Resource path of fallback icon images defailt: php/resouces
  353. 'resourcePath' => '',
  354. // Jpeg image saveing quality
  355. 'jpgQuality' => 100,
  356. // Save as progressive JPEG on image editing
  357. 'jpgProgressive' => true,
  358. // enable to get substitute image with command `dim`
  359. 'substituteImg' => true,
  360. // on paste file - if true - old file will be replaced with new one, if false new file get name - original_name-number.ext
  361. 'copyOverwrite' => true,
  362. // if true - join new and old directories content on paste
  363. 'copyJoin' => true,
  364. // on upload - if true - old file will be replaced with new one, if false new file get name - original_name-number.ext
  365. 'uploadOverwrite' => true,
  366. // mimetypes allowed to upload
  367. 'uploadAllow' => array(),
  368. // mimetypes not allowed to upload
  369. 'uploadDeny' => array(),
  370. // order to process uploadAllow and uploadDeny options
  371. 'uploadOrder' => array('deny', 'allow'),
  372. // maximum upload file size. NOTE - this is size for every uploaded files
  373. 'uploadMaxSize' => 0,
  374. // maximum number of chunked upload connection. `-1` to disable chunked upload
  375. 'uploadMaxConn' => 3,
  376. // maximum get file size. NOTE - Maximum value is 50% of PHP memory_limit
  377. 'getMaxSize' => 0,
  378. // files dates format
  379. 'dateFormat' => 'j M Y H:i',
  380. // files time format
  381. 'timeFormat' => 'H:i',
  382. // if true - every folder will be check for children folders, -1 - every folder will be check asynchronously, false - all folders will be marked as having subfolders
  383. 'checkSubfolders' => true, // true, false or -1
  384. // allow to copy from this volume to other ones?
  385. 'copyFrom' => true,
  386. // allow to copy from other volumes to this one?
  387. 'copyTo' => true,
  388. // cmd duplicate suffix format e.g. '_%s_' to without spaces
  389. 'duplicateSuffix' => ' %s ',
  390. // unique name numbar format e.g. '(%d)' to (1), (2)...
  391. 'uniqueNumFormat' => '%d',
  392. // list of commands disabled on this root
  393. 'disabled' => array(),
  394. // enable file owner, group & mode info, `false` to inactivate "chmod" command.
  395. 'statOwner' => false,
  396. // allow exec chmod of read-only files
  397. 'allowChmodReadOnly' => false,
  398. // regexp or function name to validate new file name
  399. 'acceptedName' => '/^[^\.].*/', // Notice: overwritten it in some volume drivers contractor
  400. // regexp or function name to validate new directory name
  401. 'acceptedDirname' => '', // used `acceptedName` if empty value
  402. // function/class method to control files permissions
  403. 'accessControl' => null,
  404. // some data required by access control
  405. 'accessControlData' => null,
  406. // root stat that return without asking the system when mounted and not the current volume. Query to the system with false. array|false
  407. 'rapidRootStat' => array(
  408. 'read' => true,
  409. 'write' => true,
  410. 'locked' => false,
  411. 'hidden' => false,
  412. 'size' => 0, // Unknown
  413. 'ts' => 0, // Unknown
  414. 'dirs' => -1, // Check on demand for subdirectories
  415. 'mime' => 'directory'
  416. ),
  417. // default permissions.
  418. 'defaults' => array(
  419. 'read' => true,
  420. 'write' => true,
  421. 'locked' => false,
  422. 'hidden' => false
  423. ),
  424. // files attributes
  425. 'attributes' => array(),
  426. // max allowed archive files size (0 - no limit)
  427. 'maxArcFilesSize' => '2G',
  428. // Allowed archive's mimetypes to create. Leave empty for all available types.
  429. 'archiveMimes' => array(),
  430. // Manual config for archivers. See example below. Leave empty for auto detect
  431. 'archivers' => array(),
  432. // Use Archive function for remote volume
  433. 'useRemoteArchive' => false,
  434. // plugin settings
  435. 'plugin' => array(),
  436. // Is support parent directory time stamp update on add|remove|rename item
  437. // Default `null` is auto detection that is LocalFileSystem, FTP or Dropbox are `true`
  438. 'syncChkAsTs' => null,
  439. // Long pooling sync checker function for syncChkAsTs is true
  440. // Calls with args (TARGET DIRCTORY PATH, STAND-BY(sec), OLD TIMESTAMP, VOLUME DRIVER INSTANCE, ELFINDER INSTANCE)
  441. // This function must return the following values. Changed: New Timestamp or Same: Old Timestamp or Error: false
  442. // Default `null` is try use elFinderVolumeLocalFileSystem::localFileSystemInotify() on LocalFileSystem driver
  443. // another driver use elFinder stat() checker
  444. 'syncCheckFunc' => null,
  445. // Long polling sync stand-by time (sec)
  446. 'plStandby' => 30,
  447. // Sleep time (sec) for elFinder stat() checker (syncChkAsTs is true)
  448. 'tsPlSleep' => 10,
  449. // Sleep time (sec) for elFinder ls() checker (syncChkAsTs is false)
  450. 'lsPlSleep' => 30,
  451. // Client side sync interval minimum (ms)
  452. // Default `null` is auto set to ('tsPlSleep' or 'lsPlSleep') * 1000
  453. // `0` to disable auto sync
  454. 'syncMinMs' => null,
  455. // required to fix bug on macos
  456. // However, we recommend to use the Normalizer plugin instead this option
  457. 'utf8fix' => false,
  458. // й ё Й Ё Ø Å
  459. 'utf8patterns' => array("\u0438\u0306", "\u0435\u0308", "\u0418\u0306", "\u0415\u0308", "\u00d8A", "\u030a"),
  460. 'utf8replace' => array("\u0439", "\u0451", "\u0419", "\u0401", "\u00d8", "\u00c5"),
  461. // cache control HTTP headers for commands `file` and `get`
  462. 'cacheHeaders' => array(
  463. 'Cache-Control: max-age=3600',
  464. 'Expires:',
  465. 'Pragma:'
  466. ),
  467. // Header to use to accelerate sending local files to clients (e.g. 'X-Sendfile', 'X-Accel-Redirect')
  468. 'xsendfile' => '',
  469. // Root path to xsendfile target. Probably, this is required for 'X-Accel-Redirect' on Nginx.
  470. 'xsendfilePath' => ''
  471. );
  472. /**
  473. * Defaults permissions
  474. *
  475. * @var array
  476. **/
  477. protected $defaults = array(
  478. 'read' => true,
  479. 'write' => true,
  480. 'locked' => false,
  481. 'hidden' => false
  482. );
  483. /**
  484. * Access control function/class
  485. *
  486. * @var mixed
  487. **/
  488. protected $attributes = array();
  489. /**
  490. * Access control function/class
  491. *
  492. * @var mixed
  493. **/
  494. protected $access = null;
  495. /**
  496. * Mime types allowed to upload
  497. *
  498. * @var array
  499. **/
  500. protected $uploadAllow = array();
  501. /**
  502. * Mime types denied to upload
  503. *
  504. * @var array
  505. **/
  506. protected $uploadDeny = array();
  507. /**
  508. * Order to validate uploadAllow and uploadDeny
  509. *
  510. * @var array
  511. **/
  512. protected $uploadOrder = array();
  513. /**
  514. * Maximum allowed upload file size.
  515. * Set as number or string with unit - "10M", "500K", "1G"
  516. *
  517. * @var int|string
  518. **/
  519. protected $uploadMaxSize = 0;
  520. /**
  521. * Run time setting of overwrite items on upload
  522. *
  523. * @var string
  524. */
  525. protected $uploadOverwrite = true;
  526. /**
  527. * Maximum allowed get file size.
  528. * Set as number or string with unit - "10M", "500K", "1G"
  529. *
  530. * @var int|string
  531. **/
  532. protected $getMaxSize = -1;
  533. /**
  534. * Mimetype detect method
  535. *
  536. * @var string
  537. **/
  538. protected $mimeDetect = 'auto';
  539. /**
  540. * Flag - mimetypes from externail file was loaded
  541. *
  542. * @var bool
  543. **/
  544. private static $mimetypesLoaded = false;
  545. /**
  546. * Finfo resource for mimeDetect == 'finfo'
  547. *
  548. * @var resource
  549. **/
  550. protected $finfo = null;
  551. /**
  552. * List of disabled client's commands
  553. *
  554. * @var array
  555. **/
  556. protected $disabled = array();
  557. /**
  558. * overwrite extensions/mimetypes to mime.types
  559. *
  560. * @var array
  561. **/
  562. protected static $mimetypes = array(
  563. // applications
  564. 'exe' => 'application/x-executable',
  565. 'jar' => 'application/x-jar',
  566. // archives
  567. 'gz' => 'application/x-gzip',
  568. 'tgz' => 'application/x-gzip',
  569. 'tbz' => 'application/x-bzip2',
  570. 'rar' => 'application/x-rar',
  571. // texts
  572. 'php' => 'text/x-php',
  573. 'js' => 'text/javascript',
  574. 'rtfd' => 'application/rtfd',
  575. 'py' => 'text/x-python',
  576. 'rb' => 'text/x-ruby',
  577. 'sh' => 'text/x-shellscript',
  578. 'pl' => 'text/x-perl',
  579. 'xml' => 'text/xml',
  580. 'c' => 'text/x-csrc',
  581. 'h' => 'text/x-chdr',
  582. 'cpp' => 'text/x-c++src',
  583. 'hh' => 'text/x-c++hdr',
  584. 'md' => 'text/x-markdown',
  585. 'markdown' => 'text/x-markdown',
  586. 'yml' => 'text/x-yaml',
  587. // images
  588. 'bmp' => 'image/x-ms-bmp',
  589. 'tga' => 'image/x-targa',
  590. 'xbm' => 'image/xbm',
  591. 'pxm' => 'image/pxm',
  592. //audio
  593. 'wav' => 'audio/wav',
  594. // video
  595. 'dv' => 'video/x-dv',
  596. 'wm' => 'video/x-ms-wmv',
  597. 'ogm' => 'video/ogg',
  598. 'm2ts' => 'video/MP2T',
  599. 'mts' => 'video/MP2T',
  600. 'ts' => 'video/MP2T',
  601. 'm3u8' => 'application/x-mpegURL',
  602. 'mpd' => 'application/dash+xml'
  603. );
  604. /**
  605. * Directory separator - required by client
  606. *
  607. * @var string
  608. **/
  609. protected $separator = DIRECTORY_SEPARATOR;
  610. /**
  611. * Directory separator for decode/encode hash
  612. *
  613. * @var string
  614. **/
  615. protected $separatorForHash = '';
  616. /**
  617. * System Root path (Unix like: '/', Windows: '\', 'C:\' or 'D:\'...)
  618. *
  619. * @var string
  620. **/
  621. protected $systemRoot = DIRECTORY_SEPARATOR;
  622. /**
  623. * Mimetypes allowed to display
  624. *
  625. * @var array
  626. **/
  627. protected $onlyMimes = array();
  628. /**
  629. * Store files moved or overwrited files info
  630. *
  631. * @var array
  632. **/
  633. protected $removed = array();
  634. /**
  635. * Store files added files info
  636. *
  637. * @var array
  638. **/
  639. protected $added = array();
  640. /**
  641. * Cache storage
  642. *
  643. * @var array
  644. **/
  645. protected $cache = array();
  646. /**
  647. * Cache by folders
  648. *
  649. * @var array
  650. **/
  651. protected $dirsCache = array();
  652. /**
  653. * You should use `$this->sessionCache['subdirs']` instead
  654. *
  655. * @var array
  656. * @deprecated
  657. */
  658. protected $subdirsCache = array();
  659. /**
  660. * This volume session cache
  661. *
  662. * @var array
  663. */
  664. protected $sessionCache;
  665. /**
  666. * Session caching item list
  667. *
  668. * @var array
  669. */
  670. protected $sessionCaching = array('rootstat' => true, 'subdirs' => true);
  671. /**
  672. * elFinder session wrapper object
  673. *
  674. * @var elFinderSessionInterface
  675. */
  676. protected $session;
  677. /**
  678. * Search start time
  679. *
  680. * @var int
  681. */
  682. protected $searchStart;
  683. /**
  684. * Current query word on doSearch
  685. *
  686. * @var array
  687. **/
  688. protected $doSearchCurrentQuery = array();
  689. /**
  690. * Is root modified (for clear root stat cache)
  691. *
  692. * @var bool
  693. */
  694. protected $rootModified = false;
  695. /**
  696. * Is disable of command `url`
  697. *
  698. * @var string
  699. */
  700. protected $disabledGetUrl = false;
  701. /**
  702. * Accepted filename validator
  703. *
  704. * @var string | callable
  705. */
  706. protected $nameValidator;
  707. /**
  708. * Accepted dirname validator
  709. *
  710. * @var string | callable
  711. */
  712. protected $dirnameValidator;
  713. /**
  714. * This request require online state
  715. *
  716. * @var boolean
  717. */
  718. protected $needOnline;
  719. /*********************************************************************/
  720. /* INITIALIZATION */
  721. /*********************************************************************/
  722. /**
  723. * Sets the need online.
  724. *
  725. * @param boolean $state The state
  726. */
  727. public function setNeedOnline($state = null)
  728. {
  729. if ($state !== null) {
  730. $this->needOnline = (bool)$state;
  731. return;
  732. }
  733. $need = false;
  734. $arg = $this->ARGS;
  735. $id = $this->id;
  736. $target = !empty($arg['target'])? $arg['target'] : (!empty($arg['dst'])? $arg['dst'] : '');
  737. $targets = !empty($arg['targets'])? $arg['targets'] : array();
  738. if (!is_array($targets)) {
  739. $targets = array($targets);
  740. }
  741. if ($target && strpos($target, $id) === 0) {
  742. $need = true;
  743. } else if ($targets) {
  744. foreach($targets as $t) {
  745. if ($t && strpos($t, $id) === 0) {
  746. $need = true;
  747. break;
  748. }
  749. }
  750. }
  751. $this->needOnline = $need;
  752. }
  753. /**
  754. * Prepare driver before mount volume.
  755. * Return true if volume is ready.
  756. *
  757. * @return bool
  758. * @author Dmitry (dio) Levashov
  759. **/
  760. protected function init()
  761. {
  762. return true;
  763. }
  764. /**
  765. * Configure after successfull mount.
  766. * By default set thumbnails path and image manipulation library.
  767. *
  768. * @return void
  769. * @throws elFinderAbortException
  770. * @author Dmitry (dio) Levashov
  771. */
  772. protected function configure()
  773. {
  774. // set thumbnails path
  775. $path = $this->options['tmbPath'];
  776. if ($path) {
  777. if (!file_exists($path)) {
  778. if (mkdir($path)) {
  779. chmod($path, $this->options['tmbPathMode']);
  780. } else {
  781. $path = '';
  782. }
  783. }
  784. if (is_dir($path) && is_readable($path)) {
  785. $this->tmbPath = $path;
  786. $this->tmbPathWritable = is_writable($path);
  787. }
  788. }
  789. // set resouce path
  790. if (!is_dir($this->options['resourcePath'])) {
  791. $this->options['resourcePath'] = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'resources';
  792. }
  793. // set image manipulation library
  794. $type = preg_match('/^(imagick|gd|convert|auto|none)$/i', $this->options['imgLib'])
  795. ? strtolower($this->options['imgLib'])
  796. : 'auto';
  797. if ($type === 'none') {
  798. $this->imgLib = '';
  799. } else {
  800. if (($type === 'imagick' || $type === 'auto') && extension_loaded('imagick')) {
  801. $this->imgLib = 'imagick';
  802. } else if (($type === 'gd' || $type === 'auto') && function_exists('gd_info')) {
  803. $this->imgLib = 'gd';
  804. } else {
  805. $convertCache = 'imgLibConvert';
  806. if (($convertCmd = $this->session->get($convertCache, false)) !== false) {
  807. $this->imgLib = $convertCmd;
  808. } else {
  809. $this->imgLib = ($this->procExec(ELFINDER_CONVERT_PATH . ' -version') === 0) ? 'convert' : '';
  810. $this->session->set($convertCache, $this->imgLib);
  811. }
  812. }
  813. if ($type !== 'auto' && $this->imgLib === '') {
  814. // fallback
  815. $this->imgLib = extension_loaded('imagick') ? 'imagick' : (function_exists('gd_info') ? 'gd' : '');
  816. }
  817. }
  818. // check video to img converter
  819. if (!empty($this->options['imgConverter']) && is_array($this->options['imgConverter'])) {
  820. foreach ($this->options['imgConverter'] as $_type => $_converter) {
  821. if (isset($_converter['func'])) {
  822. $this->imgConverter[strtolower($_type)] = $_converter;
  823. }
  824. }
  825. }
  826. if (!isset($this->imgConverter['video'])) {
  827. $videoLibCache = 'videoLib';
  828. if (($videoLibCmd = $this->session->get($videoLibCache, false)) === false) {
  829. $videoLibCmd = ($this->procExec(ELFINDER_FFMPEG_PATH . ' -version') === 0) ? 'ffmpeg' : '';
  830. $this->session->set($videoLibCache, $videoLibCmd);
  831. }
  832. if ($videoLibCmd) {
  833. $this->imgConverter['video'] = array(
  834. 'func' => array($this, $videoLibCmd . 'ToImg'),
  835. 'maxlen' => $this->options['tmbVideoConvLen']
  836. );
  837. }
  838. }
  839. // check onetimeUrl
  840. if (strtolower($this->options['onetimeUrl']) === 'auto') {
  841. $this->options['onetimeUrl'] = elFinder::getStaticVar('commonTempPath')? true : false;
  842. }
  843. // check archivers
  844. if (empty($this->archivers['create'])) {
  845. $this->disabled[] = 'archive';
  846. }
  847. if (empty($this->archivers['extract'])) {
  848. $this->disabled[] = 'extract';
  849. }
  850. $_arc = $this->getArchivers();
  851. if (empty($_arc['create'])) {
  852. $this->disabled[] = 'zipdl';
  853. }
  854. if ($this->options['maxArcFilesSize']) {
  855. $this->options['maxArcFilesSize'] = elFinder::getIniBytes('', $this->options['maxArcFilesSize']);
  856. }
  857. self::$maxArcFilesSize = $this->options['maxArcFilesSize'];
  858. // check 'statOwner' for command `chmod`
  859. if (empty($this->options['statOwner'])) {
  860. $this->disabled[] = 'chmod';
  861. }
  862. // check 'mimeMap'
  863. if (!is_array($this->options['mimeMap'])) {
  864. $this->options['mimeMap'] = array();
  865. }
  866. if (is_array($this->options['staticMineMap']) && $this->options['staticMineMap']) {
  867. $this->options['mimeMap'] = array_merge($this->options['mimeMap'], $this->options['staticMineMap']);
  868. }
  869. if (is_array($this->options['additionalMimeMap']) && $this->options['additionalMimeMap']) {
  870. $this->options['mimeMap'] = array_merge($this->options['mimeMap'], $this->options['additionalMimeMap']);
  871. }
  872. // check 'url' in disabled commands
  873. if (in_array('url', $this->disabled)) {
  874. $this->disabledGetUrl = true;
  875. }
  876. // set run time setting uploadOverwrite
  877. $this->uploadOverwrite = $this->options['uploadOverwrite'];
  878. }
  879. /**
  880. * @deprecated
  881. */
  882. protected function sessionRestart()
  883. {
  884. $this->sessionCache = $this->session->start()->get($this->id, array());
  885. return true;
  886. }
  887. /*********************************************************************/
  888. /* PUBLIC API */
  889. /*********************************************************************/
  890. /**
  891. * Return driver id. Used as a part of volume id.
  892. *
  893. * @return string
  894. * @author Dmitry (dio) Levashov
  895. **/
  896. public function driverId()
  897. {
  898. return $this->driverId;
  899. }
  900. /**
  901. * Return volume id
  902. *
  903. * @return string
  904. * @author Dmitry (dio) Levashov
  905. **/
  906. public function id()
  907. {
  908. return $this->id;
  909. }
  910. /**
  911. * Assign elFinder session wrapper object
  912. *
  913. * @param $session elFinderSessionInterface
  914. */
  915. public function setSession($session)
  916. {
  917. $this->session = $session;
  918. }
  919. /**
  920. * Get elFinder sesson wrapper object
  921. *
  922. * @return object The session object
  923. */
  924. public function getSession()
  925. {
  926. return $this->session;
  927. }
  928. /**
  929. * Save session cache data
  930. * Calls this function before umount this volume on elFinder::exec()
  931. *
  932. * @return void
  933. */
  934. public function saveSessionCache()
  935. {
  936. $this->session->set($this->id, $this->sessionCache);
  937. }
  938. /**
  939. * Return debug info for client
  940. *
  941. * @return array
  942. * @author Dmitry (dio) Levashov
  943. **/
  944. public function debug()
  945. {
  946. return array(
  947. 'id' => $this->id(),
  948. 'name' => strtolower(substr(get_class($this), strlen('elfinderdriver'))),
  949. 'mimeDetect' => $this->mimeDetect,
  950. 'imgLib' => $this->imgLib
  951. );
  952. }
  953. /**
  954. * chmod a file or folder
  955. *
  956. * @param string $hash file or folder hash to chmod
  957. * @param string $mode octal string representing new permissions
  958. *
  959. * @return array|false
  960. * @author David Bartle
  961. **/
  962. public function chmod($hash, $mode)
  963. {
  964. if ($this->commandDisabled('chmod')) {
  965. return $this->setError(elFinder::ERROR_PERM_DENIED);
  966. }
  967. if (!($file = $this->file($hash))) {
  968. return $this->setError(elFinder::ERROR_FILE_NOT_FOUND);
  969. }
  970. if (!$this->options['allowChmodReadOnly']) {
  971. if (!$this->attr($this->decode($hash), 'write', null, ($file['mime'] === 'directory'))) {
  972. return $this->setError(elFinder::ERROR_PERM_DENIED, $file['name']);
  973. }
  974. }
  975. $path = $this->decode($hash);
  976. $write = $file['write'];
  977. if ($this->convEncOut(!$this->_chmod($this->convEncIn($path), $mode))) {
  978. return $this->setError(elFinder::ERROR_PERM_DENIED, $file['name']);
  979. }
  980. $this->clearstatcache();
  981. if ($path == $this->root) {
  982. $this->rootModified = true;
  983. }
  984. if ($file = $this->stat($path)) {
  985. $files = array($file);
  986. if ($file['mime'] === 'directory' && $write !== $file['write']) {
  987. foreach ($this->getScandir($path) as $stat) {
  988. if ($this->mimeAccepted($stat['mime'])) {
  989. $files[] = $stat;
  990. }
  991. }
  992. }
  993. return $files;
  994. } else {
  995. return $this->setError(elFinder::ERROR_FILE_NOT_FOUND);
  996. }
  997. }
  998. /**
  999. * stat a file or folder for elFinder cmd exec
  1000. *
  1001. * @param string $hash file or folder hash to chmod
  1002. *
  1003. * @return array
  1004. * @author Naoki Sawada
  1005. **/
  1006. public function fstat($hash)
  1007. {
  1008. $path = $this->decode($hash);
  1009. return $this->stat($path);
  1010. }
  1011. /**
  1012. * Clear PHP stat cache & all of inner stat caches
  1013. */
  1014. public function clearstatcache()
  1015. {
  1016. clearstatcache();
  1017. $this->clearcache();
  1018. }
  1019. /**
  1020. * Clear inner stat caches for target hash
  1021. *
  1022. * @param string $hash
  1023. */
  1024. public function clearcaches($hash = null)
  1025. {
  1026. if ($hash === null) {
  1027. $this->clearcache();
  1028. } else {
  1029. $path = $this->decode($hash);
  1030. unset($this->cache[$path], $this->dirsCache[$path]);
  1031. }
  1032. }
  1033. /**
  1034. * "Mount" volume.
  1035. * Return true if volume available for read or write,
  1036. * false - otherwise
  1037. *
  1038. * @param array $opts
  1039. *
  1040. * @return bool
  1041. * @throws elFinderAbortException
  1042. * @author Dmitry (dio) Levashov
  1043. * @author Alexey Sukhotin
  1044. */
  1045. public function mount(array $opts)
  1046. {
  1047. $this->options = array_merge($this->options, $opts);
  1048. if (!isset($this->options['path']) || $this->options['path'] === '') {
  1049. return $this->setError('Path undefined.');
  1050. }
  1051. if (!$this->session) {
  1052. return $this->setError('Session wrapper dose not set. Need to `$volume->setSession(elFinderSessionInterface);` before mount.');
  1053. }
  1054. if (!($this->session instanceof elFinderSessionInterface)) {
  1055. return $this->setError('Session wrapper instance must be "elFinderSessionInterface".');
  1056. }
  1057. // set driverId
  1058. if (!empty($this->options['driverId'])) {
  1059. $this->driverId = $this->options['driverId'];
  1060. }
  1061. $this->id = $this->driverId . (!empty($this->options['id']) ? $this->options['id'] : elFinder::$volumesCnt++) . '_';
  1062. $this->root = $this->normpathCE($this->options['path']);
  1063. $this->separator = isset($this->options['separator']) ? $this->options['separator'] : DIRECTORY_SEPARATOR;
  1064. if (!empty($this->options['winHashFix'])) {
  1065. $this->separatorForHash = ($this->separator !== '/') ? '/' : '';
  1066. }
  1067. $this->systemRoot = isset($this->options['systemRoot']) ? $this->options['systemRoot'] : $this->separator;
  1068. // set ARGS
  1069. $this->ARGS = $_SERVER['REQUEST_METHOD'] === 'POST' ? $_POST : $_GET;
  1070. $argInit = !empty($this->ARGS['init']);
  1071. // set $this->needOnline
  1072. if (!is_bool($this->needOnline)) {
  1073. $this->setNeedOnline();
  1074. }
  1075. // session cache
  1076. if ($argInit) {
  1077. $this->session->set($this->id, array());
  1078. }
  1079. $this->sessionCache = $this->session->get($this->id, array());
  1080. // default file attribute
  1081. $this->defaults = array(
  1082. 'read' => isset($this->options['defaults']['read']) ? !!$this->options['defaults']['read'] : true,
  1083. 'write' => isset($this->options['defaults']['write']) ? !!$this->options['defaults']['write'] : true,
  1084. 'locked' => isset($this->options['defaults']['locked']) ? !!$this->options['defaults']['locked'] : false,
  1085. 'hidden' => isset($this->options['defaults']['hidden']) ? !!$this->options['defaults']['hidden'] : false
  1086. );
  1087. // root attributes
  1088. $this->attributes[] = array(
  1089. 'pattern' => '~^' . preg_quote($this->separator) . '$~',
  1090. 'locked' => true,
  1091. 'hidden' => false
  1092. );
  1093. // set files attributes
  1094. if (!empty($this->options['attributes']) && is_array($this->options['attributes'])) {
  1095. foreach ($this->options['attributes'] as $a) {
  1096. // attributes must contain pattern and at least one rule
  1097. if (!empty($a['pattern']) || (is_array($a) && count($a) > 1)) {
  1098. $this->attributes[] = $a;
  1099. }
  1100. }
  1101. }
  1102. if (!empty($this->options['accessControl']) && is_callable($this->options['accessControl'])) {
  1103. $this->access = $this->options['accessControl'];
  1104. }
  1105. $this->today = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
  1106. $this->yesterday = $this->today - 86400;
  1107. if (!$this->init()) {
  1108. return false;
  1109. }
  1110. // set server encoding
  1111. if (!empty($this->options['encoding']) && strtoupper($this->options['encoding']) !== 'UTF-8') {
  1112. $this->encoding = $this->options['encoding'];
  1113. } else {
  1114. $this->encoding = null;
  1115. }
  1116. // check some options is arrays
  1117. $this->uploadAllow = isset($this->options['uploadAllow']) && is_array($this->options['uploadAllow'])
  1118. ? $this->options['uploadAllow']
  1119. : array();
  1120. $this->uploadDeny = isset($this->options['uploadDeny']) && is_array($this->options['uploadDeny'])
  1121. ? $this->options['uploadDeny']
  1122. : array();
  1123. $this->options['uiCmdMap'] = (isset($this->options['uiCmdMap']) && is_array($this->options['uiCmdMap']))
  1124. ? $this->options['uiCmdMap']
  1125. : array();
  1126. if (is_string($this->options['uploadOrder'])) { // telephat_mode on, compatibility with 1.x
  1127. $parts = explode(',', isset($this->options['uploadOrder']) ? $this->options['uploadOrder'] : 'deny,allow');
  1128. $this->uploadOrder = array(trim($parts[0]), trim($parts[1]));
  1129. } else { // telephat_mode off
  1130. $this->uploadOrder = !empty($this->options['uploadOrder']) ? $this->options['uploadOrder'] : array('deny', 'allow');
  1131. }
  1132. if (!empty($this->options['uploadMaxSize'])) {
  1133. $this->uploadMaxSize = elFinder::getIniBytes('', $this->options['uploadMaxSize']);
  1134. }
  1135. // Set maximum to PHP_INT_MAX
  1136. if (!defined('PHP_INT_MAX')) {
  1137. define('PHP_INT_MAX', 2147483647);
  1138. }
  1139. if ($this->uploadMaxSize < 1 || $this->uploadMaxSize > PHP_INT_MAX) {
  1140. $this->uploadMaxSize = PHP_INT_MAX;
  1141. }
  1142. // Set to get maximum size to 50% of memory_limit
  1143. $memLimit = elFinder::getIniBytes('memory_limit') / 2;
  1144. if ($memLimit > 0) {
  1145. $this->getMaxSize = empty($this->options['getMaxSize']) ? $memLimit : min($memLimit, elFinder::getIniBytes('', $this->options['getMaxSize']));
  1146. } else {
  1147. $this->getMaxSize = -1;
  1148. }
  1149. $this->disabled = isset($this->options['disabled']) && is_array($this->options['disabled'])
  1150. ? array_values(array_diff($this->options['disabled'], array('open'))) // 'open' is required
  1151. : array();
  1152. $this->cryptLib = $this->options['cryptLib'];
  1153. $this->mimeDetect = $this->options['mimeDetect'];
  1154. // find available mimetype detect method
  1155. $type = strtolower($this->options['mimeDetect']);
  1156. $type = preg_match('/^(finfo|mime_content_type|internal|auto)$/i', $type) ? $type : 'auto';
  1157. $regexp = '/text\/x\-(php|c\+\+)/';
  1158. if (($type == 'finfo' || $type == 'auto')
  1159. && class_exists('finfo', false)) {
  1160. $tmpFileInfo = explode(';', finfo_file(finfo_open(FILEINFO_MIME), __FILE__));
  1161. } else {
  1162. $tmpFileInfo = false;
  1163. }
  1164. $type = 'internal';
  1165. if ($tmpFileInfo && preg_match($regexp, array_shift($tmpFileInfo))) {
  1166. $type = 'finfo';
  1167. $this->finfo = finfo_open(FILEINFO_MIME);
  1168. } elseif (($type == 'mime_content_type' || $type == 'auto') && function_exists('mime_content_type')) {
  1169. $_mimetypes = explode(';', mime_content_type(__FILE__));
  1170. if (preg_match($regexp, array_shift($_mimetypes))) {
  1171. $type = 'mime_content_type';
  1172. }
  1173. }
  1174. $this->mimeDetect = $type;
  1175. // load mimes from external file for mimeDetect == 'internal'
  1176. // based on Alexey Sukhotin idea and patch: http://elrte.org/redmine/issues/163
  1177. // file must be in file directory or in parent one
  1178. if ($this->mimeDetect === 'internal' && !elFinderVolumeDriver::$mimetypesLoaded) {
  1179. elFinderVolumeDriver::loadMimeTypes(!empty($this->options['mimefile']) ? $this->options['mimefile'] : '');
  1180. }
  1181. $this->rootName = empty($this->options['alias']) ? $this->basenameCE($this->root) : $this->options['alias'];
  1182. // This get's triggered if $this->root == '/' and alias is empty.
  1183. // Maybe modify _basename instead?
  1184. if ($this->rootName === '') $this->rootName = $this->separator;
  1185. $this->_checkArchivers();
  1186. $root = $this->stat($this->root);
  1187. if (!$root) {
  1188. return $this->setError('Root folder does not exist.');
  1189. }
  1190. if (!$root['read'] && !$root['write']) {
  1191. return $this->setError('Root folder has not read and write permissions.');
  1192. }
  1193. if ($root['read']) {
  1194. if ($argInit) {
  1195. // check startPath - path to open by default instead of root
  1196. $startPath = $this->options['startPath'] ? $this->normpathCE($this->options['startPath']) : '';
  1197. if ($startPath) {
  1198. $start = $this->stat($startPath);
  1199. if (!empty($start)
  1200. && $start['mime'] == 'directory'
  1201. && $start['read']
  1202. && empty($start['hidden'])
  1203. && $this->inpathCE($startPath, $this->root)) {
  1204. $this->startPath = $startPath;
  1205. if (substr($this->startPath, -1, 1) == $this->options['separator']) {
  1206. $this->startPath = substr($this->startPath, 0, -1);
  1207. }
  1208. }
  1209. }
  1210. }
  1211. } else {
  1212. $this->options['URL'] = '';
  1213. $this->options['tmbURL'] = '';
  1214. $this->options['tmbPath'] = '';
  1215. // read only volume
  1216. array_unshift($this->attributes, array(
  1217. 'pattern' => '/.*/',
  1218. 'read' => false
  1219. ));
  1220. }
  1221. $this->treeDeep = $this->options['treeDeep'] > 0 ? (int)$this->options['treeDeep'] : 1;
  1222. $this->tmbSize = $this->options['tmbSize'] > 0 ? (int)$this->options['tmbSize'] : 48;
  1223. $this->URL = $this->options['URL'];
  1224. if ($this->URL && preg_match("|[^/?&=]$|", $this->URL)) {
  1225. $this->URL .= '/';
  1226. }
  1227. $dirUrlOwn = strtolower($this->options['dirUrlOwn']);
  1228. if ($dirUrlOwn === 'auto') {
  1229. $this->options['dirUrlOwn'] = $this->URL ? false : true;
  1230. } else if ($dirUrlOwn === 'hide') {
  1231. $this->options['dirUrlOwn'] = 'hide';
  1232. } else {
  1233. $this->options['dirUrlOwn'] = (bool)$this->options['dirUrlOwn'];
  1234. }
  1235. $this->tmbURL = !empty($this->options['tmbURL']) ? $this->options['tmbURL'] : '';
  1236. if ($this->tmbURL && $this->tmbURL !== 'self' && preg_match("|[^/?&=]$|", $this->tmbURL)) {
  1237. $this->tmbURL .= '/';
  1238. }
  1239. $this->nameValidator = !empty($this->options['acceptedName']) && (is_string($this->options['acceptedName']) || is_callable($this->options['acceptedName']))
  1240. ? $this->options['acceptedName']
  1241. : '';
  1242. $this->dirnameValidator = !empty($this->options['acceptedDirname']) && (is_callable($this->options['acceptedDirname']) || (is_string($this->options['acceptedDirname']) && preg_match($this->options['acceptedDirname'], '') !== false))
  1243. ? $this->options['acceptedDirname']
  1244. : $this->nameValidator;
  1245. // enabling archivers['create'] with options['useRemoteArchive']
  1246. if ($this->options['useRemoteArchive'] && empty($this->archivers['create']) && $this->getTempPath()) {
  1247. $_archivers = $this->getArchivers();
  1248. $this->archivers['create'] = $_archivers['create'];
  1249. }
  1250. // manual control archive types to create
  1251. if (!empty($this->options['archiveMimes']) && is_array($this->options['archiveMimes'])) {
  1252. foreach ($this->archivers['create'] as $mime => $v) {
  1253. if (!in_array($mime, $this->options['archiveMimes'])) {
  1254. unset($this->archivers['create'][$mime]);
  1255. }
  1256. }
  1257. }
  1258. // manualy add archivers
  1259. if (!empty($this->options['archivers']['create']) && is_array($this->options['archivers']['create'])) {
  1260. foreach ($this->options['archivers']['create'] as $mime => $conf) {
  1261. if (strpos($mime, 'application/') === 0
  1262. && !empty($conf['cmd'])
  1263. && isset($conf['argc'])
  1264. && !empty($conf['ext'])
  1265. && !isset($this->archivers['create'][$mime])) {
  1266. $this->archivers['create'][$mime] = $conf;
  1267. }
  1268. }
  1269. }
  1270. if (!empty($this->options['archivers']['extract']) && is_array($this->options['archivers']['extract'])) {
  1271. foreach ($this->options['archivers']['extract'] as $mime => $conf) {
  1272. if (strpos($mime, 'application/') === 0
  1273. && !empty($conf['cmd'])
  1274. && isset($conf['argc'])
  1275. && !empty($conf['ext'])
  1276. && !isset($this->archivers['extract'][$mime])) {
  1277. $this->archivers['extract'][$mime] = $conf;
  1278. }
  1279. }
  1280. }
  1281. if (!empty($this->options['noSessionCache']) && is_array($this->options['noSessionCache'])) {
  1282. foreach ($this->options['noSessionCache'] as $_key) {
  1283. $this->sessionCaching[$_key] = false;
  1284. unset($this->sessionCache[$_key]);
  1285. }
  1286. }
  1287. if ($this->sessionCaching['subdirs']) {
  1288. if (!isset($this->sessionCache['subdirs'])) {
  1289. $this->sessionCache['subdirs'] = array();
  1290. }
  1291. }
  1292. $this->configure();
  1293. // Normalize disabled (array_merge`for type array of JSON)
  1294. $this->disabled = array_values(array_unique($this->disabled));
  1295. // fix sync interval
  1296. if ($this->options['syncMinMs'] !== 0) {
  1297. $this->options['syncMinMs'] = max($this->options[$this->options['syncChkAsTs'] ? 'tsPlSleep' : 'lsPlSleep'] * 1000, intval($this->options['syncMinMs']));
  1298. }
  1299. // ` copyJoin` is required for the trash function
  1300. if ($this->options['trashHash'] && empty($this->options['copyJoin'])) {
  1301. $this->options['trashHash'] = '';
  1302. }
  1303. // set tmpLinkPath
  1304. if (elFinder::$tmpLinkPath && !$this->options['tmpLinkPath']) {
  1305. if (is_writeable(elFinder::$tmpLinkPath)) {
  1306. $this->options['tmpLinkPath'] = elFinder::$tmpLinkPath;
  1307. } else {
  1308. elFinder::$tmpLinkPath = '';
  1309. }
  1310. }
  1311. if ($this->options['tmpLinkPath'] && is_writable($this->options['tmpLinkPath'])) {
  1312. $this->tmpLinkPath = realpath($this->options['tmpLinkPath']);
  1313. } else if (!$this->tmpLinkPath && $this->tmbURL && $this->tmbPath) {
  1314. $this->tmpLinkPath = $this->tmbPath;
  1315. $this->options['tmpLinkUrl'] = $this->tmbURL;
  1316. } else if (!$this->options['URL'] && is_writable('../files/.tmb')) {
  1317. $this->tmpLinkPath = realpath('../files/.tmb');
  1318. $this->options['tmpLinkUrl'] = '';
  1319. if (!elFinder::$tmpLinkPath) {
  1320. elFinder::$tmpLinkPath = $this->tmpLinkPath;
  1321. elFinder::$tmpLinkUrl = '';
  1322. }
  1323. }
  1324. // set tmpLinkUrl
  1325. if (elFinder::$tmpLinkUrl && !$this->options['tmpLinkUrl']) {
  1326. $this->options['tmpLinkUrl'] = elFinder::$tmpLinkUrl;
  1327. }
  1328. if ($this->options['tmpLinkUrl']) {
  1329. $this->tmpLinkUrl = $this->options['tmpLinkUrl'];
  1330. }
  1331. if ($this->tmpLinkPath && !$this->tmpLinkUrl) {
  1332. $cur = realpath('./');
  1333. $i = 0;
  1334. while ($cur !== $this->systemRoot && strpos($this->tmpLinkPath, $cur) !== 0) {
  1335. $i++;
  1336. $cur = dirname($cur);
  1337. }
  1338. list($req) = explode('?', $_SERVER['REQUEST_URI']);
  1339. $reqs = explode('/', dirname($req));
  1340. $uri = join('/', array_slice($reqs, 0, count($reqs) - 1)) . substr($this->tmpLinkPath, strlen($cur));
  1341. $https = (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off');
  1342. $this->tmpLinkUrl = ($https ? 'https://' : 'http://')
  1343. . $_SERVER['SERVER_NAME'] // host
  1344. . (((!$https && $_SERVER['SERVER_PORT'] == 80) || ($https && $_SERVER['SERVER_PORT'] == 443)) ? '' : (':' . $_SERVER['SERVER_PORT'])) // port
  1345. . $uri;
  1346. if (!elFinder::$tmpLinkUrl) {
  1347. elFinder::$tmpLinkUrl = $this->tmpLinkUrl;
  1348. }
  1349. }
  1350. // remove last '/'
  1351. if ($this->tmpLinkPath) {
  1352. $this->tmpLinkPath = rtrim($this->tmpLinkPath, '/');
  1353. }
  1354. if ($this->tmpLinkUrl) {
  1355. $this->tmpLinkUrl = rtrim($this->tmpLinkUrl, '/');
  1356. }
  1357. // to update options cache
  1358. $this->updateCache($this->root, $root);
  1359. return $this->mounted = true;
  1360. }
  1361. /**
  1362. * Some "unmount" stuffs - may be required by virtual fs
  1363. *
  1364. * @return void
  1365. * @author Dmitry (dio) Levashov
  1366. **/
  1367. public function umount()
  1368. {
  1369. }
  1370. /**
  1371. * Remove session cache of this volume
  1372. */
  1373. public function clearSessionCache()
  1374. {
  1375. $this->sessionCache = array();
  1376. }
  1377. /**
  1378. * Return error message from last failed action
  1379. *
  1380. * @return array
  1381. * @author Dmitry (dio) Levashov
  1382. **/
  1383. public function error()
  1384. {
  1385. return $this->error;
  1386. }
  1387. /**
  1388. * Return is uploadable that given file name
  1389. *
  1390. * @param string $name file name
  1391. * @param bool $allowUnknown
  1392. *
  1393. * @return bool
  1394. * @author Naoki Sawada
  1395. **/
  1396. public function isUploadableByName($name, $allowUnknown = false)
  1397. {
  1398. $mimeByName = $this->mimetype($name, true);
  1399. return (($allowUnknown && $mimeByName === 'unknown') || $this->allowPutMime($mimeByName));
  1400. }
  1401. /**
  1402. * Return Extention/MIME Table (elFinderVolumeDriver::$mimetypes)
  1403. *
  1404. * @return array
  1405. * @author Naoki Sawada
  1406. */
  1407. public function getMimeTable()
  1408. {
  1409. // load mime.types
  1410. if (!elFinderVolumeDriver::$mimetypesLoaded) {
  1411. elFinderVolumeDriver::loadMimeTypes();
  1412. }
  1413. return elFinderVolumeDriver::$mimetypes;
  1414. }
  1415. /**
  1416. * Return file extention detected by MIME type
  1417. *
  1418. * @param string $mime MIME type
  1419. * @param string $suffix Additional suffix
  1420. *
  1421. * @return string
  1422. * @author Naoki Sawada
  1423. */
  1424. public function getExtentionByMime($mime, $suffix = '')
  1425. {
  1426. static $extTable = null;
  1427. if (is_null($extTable)) {
  1428. $extTable = array_flip(array_unique($this->getMimeTable()));
  1429. foreach ($this->options['mimeMap'] as $pair => $_mime) {
  1430. list($ext) = explode(':', $pair);
  1431. if ($ext !== '*' && !isset($extTable[$_mime])) {
  1432. $extTable[$_mime] = $ext;
  1433. }
  1434. }
  1435. }
  1436. if ($mime && isset($extTable[$mime])) {
  1437. return $suffix ? ($extTable[$mime] . $suffix) : $extTable[$mime];
  1438. }
  1439. return '';
  1440. }
  1441. /**
  1442. * Set mimetypes allowed to display to client
  1443. *
  1444. * @param array $mimes
  1445. *
  1446. * @return void
  1447. * @author Dmitry (dio) Levashov
  1448. **/
  1449. public function setMimesFilter($mimes)
  1450. {
  1451. if (is_array($mimes)) {
  1452. $this->onlyMimes = $mimes;
  1453. }
  1454. }
  1455. /**
  1456. * Return root folder hash
  1457. *
  1458. * @return string
  1459. * @author Dmitry (dio) Levashov
  1460. **/
  1461. public function root()
  1462. {
  1463. return $this->encode($this->root);
  1464. }
  1465. /**
  1466. * Return root path
  1467. *
  1468. * @return string
  1469. * @author Naoki Sawada
  1470. **/
  1471. public function getRootPath()
  1472. {
  1473. return $this->root;
  1474. }
  1475. /**
  1476. * Return target path hash
  1477. *
  1478. * @param string $path
  1479. * @param string $name
  1480. *
  1481. * @author Naoki Sawada
  1482. * @return string
  1483. */
  1484. public function getHash($path, $name = '')
  1485. {
  1486. if ($name !== '') {
  1487. $path = $this->joinPathCE($path, $name);
  1488. }
  1489. return $this->encode($path);
  1490. }
  1491. /**
  1492. * Return decoded path of target hash
  1493. * This method do not check the stat of target
  1494. * Use method `realpath()` to do check of the stat of target
  1495. *
  1496. * @param string $hash
  1497. *
  1498. * @author Naoki Sawada
  1499. * @return string
  1500. */
  1501. public function getPath($hash)
  1502. {
  1503. return $this->decode($hash);
  1504. }
  1505. /**
  1506. * Return root or startPath hash
  1507. *
  1508. * @return string
  1509. * @author Dmitry (dio) Levashov
  1510. **/
  1511. public function defaultPath()
  1512. {
  1513. return $this->encode($this->startPath ? $this->startPath : $this->root);
  1514. }
  1515. /**
  1516. * Return volume options required by client:
  1517. *
  1518. * @param $hash
  1519. *
  1520. * @return array
  1521. * @author Dmitry (dio) Levashov
  1522. */
  1523. public function options($hash)
  1524. {
  1525. $create = $createext = array();
  1526. if (isset($this->archivers['create']) && is_array($this->archivers['create'])) {
  1527. foreach ($this->archivers['create'] as $m => $v) {
  1528. $create[] = $m;
  1529. $createext[$m] = $v['ext'];
  1530. }
  1531. }
  1532. $opts = array(
  1533. 'path' => $hash ? $this->path($hash) : '',
  1534. 'url' => $this->URL,
  1535. 'tmbUrl' => (!$this->imgLib && $this->options['tmbFbSelf']) ? 'self' : $this->tmbURL,
  1536. 'disabled' => $this->disabled,
  1537. 'separator' => $this->separator,
  1538. 'copyOverwrite' => intval($this->options['copyOverwrite']),
  1539. 'uploadOverwrite' => intval($this->options['uploadOverwrite']),
  1540. 'uploadMaxSize' => intval($this->uploadMaxSize),
  1541. 'uploadMaxConn' => intval($this->options['uploadMaxConn']),
  1542. 'uploadMime' => array(
  1543. 'firstOrder' => isset($this->uploadOrder[0]) ? $this->uploadOrder[0] : 'deny',
  1544. 'allow' => $this->uploadAllow,
  1545. 'deny' => $this->uploadDeny
  1546. ),
  1547. 'dispInlineRegex' => $this->options['dispInlineRegex'],
  1548. 'jpgQuality' => intval($this->options['jpgQuality']),
  1549. 'archivers' => array(
  1550. 'create' => $create,
  1551. 'extract' => isset($this->archivers['extract']) && is_array($this->archivers['extract']) ? array_keys($this->archivers['extract']) : array(),
  1552. 'createext' => $createext
  1553. ),
  1554. 'uiCmdMap' => (isset($this->options['uiCmdMap']) && is_array($this->options['uiCmdMap'])) ? $this->options['uiCmdMap'] : array(),
  1555. 'syncChkAsTs' => intval($this->options['syncChkAsTs']),
  1556. 'syncMinMs' => intval($this->options['syncMinMs']),
  1557. 'i18nFolderName' => intval($this->options['i18nFolderName']),
  1558. 'tmbCrop' => intval($this->options['tmbCrop']),
  1559. 'tmbReqCustomData' => (bool)$this->options['tmbReqCustomData'],
  1560. 'substituteImg' => (bool)$this->options['substituteImg'],
  1561. 'onetimeUrl' => (bool)$this->options['onetimeUrl'],
  1562. );
  1563. if (!empty($this->options['trashHash'])) {
  1564. $opts['trashHash'] = $this->options['trashHash'];
  1565. }
  1566. if ($hash === null) {
  1567. // call from getRootStatExtra()
  1568. if (!empty($this->options['icon'])) {
  1569. $opts['icon'] = $this->options['icon'];
  1570. }
  1571. if (!empty($this->options['rootCssClass'])) {
  1572. $opts['csscls'] = $this->options['rootCssClass'];
  1573. }
  1574. if (isset($this->options['netkey'])) {
  1575. $opts['netkey'] = $this->options['netkey'];
  1576. }
  1577. }
  1578. return $opts;
  1579. }
  1580. /**
  1581. * Get option value of this volume
  1582. *
  1583. * @param string $name target option name
  1584. *
  1585. * @return NULL|mixed target option value
  1586. * @author Naoki Sawada
  1587. */
  1588. public function getOption($name)
  1589. {
  1590. return isset($this->options[$name]) ? $this->options[$name] : null;
  1591. }
  1592. /**
  1593. * Get plugin values of this options
  1594. *
  1595. * @param string $name Plugin name
  1596. *
  1597. * @return NULL|array Plugin values
  1598. * @author Naoki Sawada
  1599. */
  1600. public function getOptionsPlugin($name = '')
  1601. {
  1602. if ($name) {
  1603. return isset($this->options['plugin'][$name]) ? $this->options['plugin'][$name] : array();
  1604. } else {
  1605. return $this->options['plugin'];
  1606. }
  1607. }
  1608. /**
  1609. * Return true if command disabled in options
  1610. *
  1611. * @param string $cmd command name
  1612. *
  1613. * @return bool
  1614. * @author Dmitry (dio) Levashov
  1615. **/
  1616. public function commandDisabled($cmd)
  1617. {
  1618. return in_array($cmd, $this->disabled);
  1619. }
  1620. /**
  1621. * Return true if mime is required mimes list
  1622. *
  1623. * @param string $mime mime type to check
  1624. * @param array $mimes allowed mime types list or not set to use client mimes list
  1625. * @param bool|null $empty what to return on empty list
  1626. *
  1627. * @return bool|null
  1628. * @author Dmitry (dio) Levashov
  1629. * @author Troex Nevelin
  1630. **/
  1631. public function mimeAccepted($mime, $mimes = null, $empty = true)
  1632. {
  1633. $mimes = is_array($mimes) ? $mimes : $this->onlyMimes;
  1634. if (empty($mimes)) {
  1635. return $empty;
  1636. }
  1637. return $mime == 'directory'
  1638. || in_array('all', $mimes)
  1639. || in_array('All', $mimes)
  1640. || in_array($mime, $mimes)
  1641. || in_array(substr($mime, 0, strpos($mime, '/')), $mimes);
  1642. }
  1643. /**
  1644. * Return true if voume is readable.
  1645. *
  1646. * @return bool
  1647. * @author Dmitry (dio) Levashov
  1648. **/
  1649. public function isReadable()
  1650. {
  1651. $stat = $this->stat($this->root);
  1652. return $stat['read'];
  1653. }
  1654. /**
  1655. * Return true if copy from this volume allowed
  1656. *
  1657. * @return bool
  1658. * @author Dmitry (dio) Levashov
  1659. **/
  1660. public function copyFromAllowed()
  1661. {
  1662. return !!$this->options['copyFrom'];
  1663. }
  1664. /**
  1665. * Return file path related to root with convert encoging
  1666. *
  1667. * @param string $hash file hash
  1668. *
  1669. * @return string
  1670. * @author Dmitry (dio) Levashov
  1671. **/
  1672. public function path($hash)
  1673. {
  1674. return $this->convEncOut($this->_path($this->convEncIn($this->decode($hash))));
  1675. }
  1676. /**
  1677. * Return file real path if file exists
  1678. *
  1679. * @param string $hash file hash
  1680. *
  1681. * @return string | false
  1682. * @author Dmitry (dio) Levashov
  1683. **/
  1684. public function realpath($hash)
  1685. {
  1686. $path = $this->decode($hash);
  1687. return $this->stat($path) ? $path : false;
  1688. }
  1689. /**
  1690. * Return list of moved/overwrited files
  1691. *
  1692. * @return array
  1693. * @author Dmitry (dio) Levashov
  1694. **/
  1695. public function removed()
  1696. {
  1697. if ($this->removed) {
  1698. $unsetSubdir = isset($this->sessionCache['subdirs']) ? true : false;
  1699. foreach ($this->removed as $item) {
  1700. if ($item['mime'] === 'directory') {
  1701. $path = $this->decode($item['hash']);
  1702. if ($unsetSubdir) {
  1703. unset($this->sessionCache['subdirs'][$path]);
  1704. }
  1705. if ($item['phash'] !== '') {
  1706. $parent = $this->decode($item['phash']);
  1707. unset($this->cache[$parent]);
  1708. if ($this->root === $parent) {
  1709. $this->sessionCache['rootstat'] = array();
  1710. }
  1711. if ($unsetSubdir) {
  1712. unset($this->sessionCache['subdirs'][$parent]);
  1713. }
  1714. }
  1715. }
  1716. }
  1717. $this->removed = array_values($this->removed);
  1718. }
  1719. return $this->removed;
  1720. }
  1721. /**
  1722. * Return list of added files
  1723. *
  1724. * @deprecated
  1725. * @return array
  1726. * @author Naoki Sawada
  1727. **/
  1728. public function added()
  1729. {
  1730. return $this->added;
  1731. }
  1732. /**
  1733. * Clean removed files list
  1734. *
  1735. * @return void
  1736. * @author Dmitry (dio) Levashov
  1737. **/
  1738. public function resetRemoved()
  1739. {
  1740. $this->resetResultStat();
  1741. }
  1742. /**
  1743. * Clean added/removed files list
  1744. *
  1745. * @return void
  1746. **/
  1747. public function resetResultStat()
  1748. {
  1749. $this->removed = array();
  1750. $this->added = array();
  1751. }
  1752. /**
  1753. * Return file/dir hash or first founded child hash with required attr == $val
  1754. *
  1755. * @param string $hash file hash
  1756. * @param string $attr attribute name
  1757. * @param bool $val attribute value
  1758. *
  1759. * @return string|false
  1760. * @author Dmitry (dio) Levashov
  1761. **/
  1762. public function closest($hash, $attr, $val)
  1763. {
  1764. return ($path = $this->closestByAttr($this->decode($hash), $attr, $val)) ? $this->encode($path) : false;
  1765. }
  1766. /**
  1767. * Return file info or false on error
  1768. *
  1769. * @param string $hash file hash
  1770. *
  1771. * @return array|false
  1772. * @internal param bool $realpath add realpath field to file info
  1773. * @author Dmitry (dio) Levashov
  1774. */
  1775. public function file($hash)
  1776. {
  1777. $file = $this->stat($this->decode($hash));
  1778. return ($file) ? $file : $this->setError(elFinder::ERROR_FILE_NOT_FOUND);
  1779. }
  1780. /**
  1781. * Return folder info
  1782. *
  1783. * @param string $hash folder hash
  1784. * @param bool $resolveLink
  1785. *
  1786. * @return array|false
  1787. * @internal param bool $hidden return hidden file info
  1788. * @author Dmitry (dio) Levashov
  1789. */
  1790. public function dir($hash, $resolveLink = false)
  1791. {
  1792. if (($dir = $this->file($hash)) == false) {
  1793. return $this->setError(elFinder::ERROR_DIR_NOT_FOUND);
  1794. }
  1795. if ($resolveLink && !empty($dir['thash'])) {
  1796. $dir = $this->file($dir['thash']);
  1797. }
  1798. return $dir && $dir['mime'] == 'directory' && empty($dir['hidden'])
  1799. ? $dir
  1800. : $this->setError(elFinder::ERROR_NOT_DIR);
  1801. }
  1802. /**
  1803. * Return directory content or false on error
  1804. *
  1805. * @param string $hash file hash
  1806. *
  1807. * @return array|false
  1808. * @author Dmitry (dio) Levashov
  1809. **/
  1810. public function scandir($hash)
  1811. {
  1812. if (($dir = $this->dir($hash)) == false) {
  1813. return false;
  1814. }
  1815. $path = $this->decode($hash);
  1816. if ($res = $dir['read']
  1817. ? $this->getScandir($path)
  1818. : $this->setError(elFinder::ERROR_PERM_DENIED)) {
  1819. $dirs = null;
  1820. if ($this->sessionCaching['subdirs'] && isset($this->sessionCache['subdirs'][$path])) {
  1821. $dirs = $this->sessionCache['subdirs'][$path];
  1822. }
  1823. if ($dirs !== null || (isset($dir['dirs']) && $dir['dirs'] != 1)) {
  1824. $_dir = $dir;
  1825. if ($dirs || $this->subdirs($hash)) {
  1826. $dir['dirs'] = 1;
  1827. } else {
  1828. unset($dir['dirs']);
  1829. }
  1830. if ($dir !== $_dir) {
  1831. $this->updateCache($path, $dir);
  1832. }
  1833. }
  1834. }
  1835. return $res;
  1836. }
  1837. /**
  1838. * Return dir files names list
  1839. *
  1840. * @param string $hash file hash
  1841. * @param null $intersect
  1842. *
  1843. * @return array|false
  1844. * @author Dmitry (dio) Levashov
  1845. */
  1846. public function ls($hash, $intersect = null)
  1847. {
  1848. if (($dir = $this->dir($hash)) == false || !$dir['read']) {
  1849. return false;
  1850. }
  1851. $list = array();
  1852. $path = $this->decode($hash);
  1853. $check = array();
  1854. if ($intersect) {
  1855. $check = array_flip($intersect);
  1856. }
  1857. foreach ($this->getScandir($path) as $stat) {
  1858. if (empty($stat['hidden']) && (!$check || isset($check[$stat['name']])) && $this->mimeAccepted($stat['mime'])) {
  1859. $list[$stat['hash']] = $stat['name'];
  1860. }
  1861. }
  1862. return $list;
  1863. }
  1864. /**
  1865. * Return subfolders for required folder or false on error
  1866. *
  1867. * @param string $hash folder hash or empty string to get tree from root folder
  1868. * @param int $deep subdir deep
  1869. * @param string $exclude dir hash which subfolders must be exluded from result, required to not get stat twice on cwd subfolders
  1870. *
  1871. * @return array|false
  1872. * @author Dmitry (dio) Levashov
  1873. **/
  1874. public function tree($hash = '', $deep = 0, $exclude = '')
  1875. {
  1876. $path = $hash ? $this->decode($hash) : $this->root;
  1877. if (($dir = $this->stat($path)) == false || $dir['mime'] != 'directory') {
  1878. return false;
  1879. }
  1880. $dirs = $this->gettree($path, $deep > 0 ? $deep - 1 : $this->treeDeep - 1, $exclude ? $this->decode($exclude) : null);
  1881. array_unshift($dirs, $dir);
  1882. return $dirs;
  1883. }
  1884. /**
  1885. * Return part of dirs tree from required dir up to root dir
  1886. *
  1887. * @param string $hash directory hash
  1888. * @param bool|null $lineal only lineal parents
  1889. *
  1890. * @return array|false
  1891. * @throws elFinderAbortException
  1892. * @author Dmitry (dio) Levashov
  1893. */
  1894. public function parents($hash, $lineal = false)
  1895. {
  1896. if (($current = $this->dir($hash)) == false) {
  1897. return false;
  1898. }
  1899. $args = func_get_args();
  1900. // checks 3rd param `$until` (elFinder >= 2.1.24)
  1901. $until = '';
  1902. if (isset($args[2])) {
  1903. $until = $args[2];
  1904. }
  1905. $path = $this->decode($hash);
  1906. $tree = array();
  1907. while ($path && $path != $this->root) {
  1908. elFinder::checkAborted();
  1909. $path = $this->dirnameCE($path);
  1910. if (!($stat = $this->stat($path)) || !empty($stat['hidden']) || !$stat['read']) {
  1911. return false;
  1912. }
  1913. array_unshift($tree, $stat);
  1914. if (!$lineal) {
  1915. foreach ($this->gettree($path, 0) as $dir) {
  1916. elFinder::checkAborted();
  1917. if (!isset($tree[$dir['hash']])) {
  1918. $tree[$dir['hash']] = $dir;
  1919. }
  1920. }
  1921. }
  1922. if ($until && $until === $this->encode($path)) {
  1923. break;
  1924. }
  1925. }
  1926. return $tree ? array_values($tree) : array($current);
  1927. }
  1928. /**
  1929. * Create thumbnail for required file and return its name or false on failed
  1930. *
  1931. * @param $hash
  1932. *
  1933. * @return false|string
  1934. * @throws ImagickException
  1935. * @throws elFinderAbortException
  1936. * @author Dmitry (dio) Levashov
  1937. */
  1938. public function tmb($hash)
  1939. {
  1940. $path = $this->decode($hash);
  1941. $stat = $this->stat($path);
  1942. if (isset($stat['tmb'])) {
  1943. $res = $stat['tmb'] == "1" ? $this->createTmb($path, $stat) : $stat['tmb'];
  1944. if (!$res) {
  1945. list($type) = explode('/', $stat['mime']);
  1946. $fallback = $this->options['resourcePath'] . DIRECTORY_SEPARATOR . strtolower($type) . '.png';
  1947. if (is_file($fallback)) {
  1948. $res = $this->tmbname($stat);
  1949. if (!copy($fallback, $this->tmbPath . DIRECTORY_SEPARATOR . $res)) {
  1950. $res = false;
  1951. }
  1952. }
  1953. }
  1954. // tmb garbage collection
  1955. if ($res && $this->options['tmbGcMaxlifeHour'] && $this->options['tmbGcPercentage'] > 0) {
  1956. $rand = mt_rand(1, 10000);
  1957. if ($rand <= $this->options['tmbGcPercentage'] * 100) {
  1958. register_shutdown_function(array('elFinder', 'GlobGC'), $this->tmbPath . DIRECTORY_SEPARATOR . '*.png', $this->options['tmbGcMaxlifeHour'] * 3600);
  1959. }
  1960. }
  1961. return $res;
  1962. }
  1963. return false;
  1964. }
  1965. /**
  1966. * Return file size / total directory size
  1967. *
  1968. * @param string file hash
  1969. *
  1970. * @return array
  1971. * @throws elFinderAbortException
  1972. * @author Dmitry (dio) Levashov
  1973. */
  1974. public function size($hash)
  1975. {
  1976. return $this->countSize($this->decode($hash));
  1977. }
  1978. /**
  1979. * Open file for reading and return file pointer
  1980. *
  1981. * @param string file hash
  1982. *
  1983. * @return Resource|false
  1984. * @author Dmitry (dio) Levashov
  1985. **/
  1986. public function open($hash)
  1987. {
  1988. if (($file = $this->file($hash)) == false
  1989. || $file['mime'] == 'directory') {
  1990. return false;
  1991. }
  1992. // check extra option for network stream pointer
  1993. if (func_num_args() > 1) {
  1994. $opts = func_get_arg(1);
  1995. } else {
  1996. $opts = array();
  1997. }
  1998. return $this->fopenCE($this->decode($hash), 'rb', $opts);
  1999. }
  2000. /**
  2001. * Close file pointer
  2002. *
  2003. * @param Resource $fp file pointer
  2004. * @param string $hash file hash
  2005. *
  2006. * @return void
  2007. * @author Dmitry (dio) Levashov
  2008. **/
  2009. public function close($fp, $hash)
  2010. {
  2011. $this->fcloseCE($fp, $this->decode($hash));
  2012. }
  2013. /**
  2014. * Create directory and return dir info
  2015. *
  2016. * @param string $dsthash destination directory hash
  2017. * @param string $name directory name
  2018. *
  2019. * @return array|false
  2020. * @author Dmitry (dio) Levashov
  2021. **/
  2022. public function mkdir($dsthash, $name)
  2023. {
  2024. if ($this->commandDisabled('mkdir')) {
  2025. return $this->setError(elFinder::ERROR_PERM_DENIED);
  2026. }
  2027. if (!$this->nameAccepted($name, true)) {
  2028. return $this->setError(elFinder::ERROR_INVALID_DIRNAME);
  2029. }
  2030. if (($dir = $this->dir($dsthash)) == false) {
  2031. return $this->setError(elFinder::ERROR_TRGDIR_NOT_FOUND, '#' . $dsthash);
  2032. }
  2033. $path = $this->decode($dsthash);
  2034. if (!$dir['write'] || !$this->allowCreate($path, $name, true)) {
  2035. return $this->setError(elFinder::ERROR_PERM_DENIED);
  2036. }
  2037. $dst = $this->joinPathCE($path, $name);
  2038. $stat = $this->isNameExists($dst);
  2039. if (!empty($stat)) {
  2040. return $this->setError(elFinder::ERROR_EXISTS, $name);
  2041. }
  2042. $this->clearcache();
  2043. $mkpath = $this->convEncOut($this->_mkdir($this->convEncIn($path), $this->convEncIn($name)));
  2044. if ($mkpath) {
  2045. $this->clearstatcache();
  2046. $this->updateSubdirsCache($path, true);
  2047. $this->updateSubdirsCache($mkpath, false);
  2048. }
  2049. return $mkpath ? $this->stat($mkpath) : false;
  2050. }
  2051. /**
  2052. * Create empty file and return its info
  2053. *
  2054. * @param string $dst destination directory
  2055. * @param string $name file name
  2056. *
  2057. * @return array|false
  2058. * @author Dmitry (dio) Levashov
  2059. **/
  2060. public function mkfile($dst, $name)
  2061. {
  2062. if ($this->commandDisabled('mkfile')) {
  2063. return $this->setError(elFinder::ERROR_PERM_DENIED);
  2064. }
  2065. if (!$this->nameAccepted($name, false)) {
  2066. return $this->setError(elFinder::ERROR_INVALID_NAME);
  2067. }
  2068. $mimeByName = $this->mimetype($name, true);
  2069. if ($mimeByName && !$this->allowPutMime($mimeByName)) {
  2070. return $this->setError(elFinder::ERROR_UPLOAD_FILE_MIME, $name);
  2071. }
  2072. if (($dir = $this->dir($dst)) == false) {
  2073. return $this->setError(elFinder::ERROR_TRGDIR_NOT_FOUND, '#' . $dst);
  2074. }
  2075. $path = $this->decode($dst);
  2076. if (!$dir['write'] || !$this->allowCreate($path, $name, false)) {
  2077. return $this->setError(elFinder::ERROR_PERM_DENIED);
  2078. }
  2079. if ($this->isNameExists($this->joinPathCE($path, $name))) {
  2080. return $this->setError(elFinder::ERROR_EXISTS, $name);
  2081. }
  2082. $this->clearcache();
  2083. $res = false;
  2084. if ($path = $this->convEncOut($this->_mkfile($this->convEncIn($path), $this->convEncIn($name)))) {
  2085. $this->clearstatcache();
  2086. $res = $this->stat($path);
  2087. }
  2088. return $res;
  2089. }
  2090. /**
  2091. * Rename file and return file info
  2092. *
  2093. * @param string $hash file hash
  2094. * @param string $name new file name
  2095. *
  2096. * @return array|false
  2097. * @throws elFinderAbortException
  2098. * @author Dmitry (dio) Levashov
  2099. */
  2100. public function rename($hash, $name)
  2101. {
  2102. if ($this->commandDisabled('rename')) {
  2103. return $this->setError(elFinder::ERROR_PERM_DENIED);
  2104. }
  2105. if (!($file = $this->file($hash))) {
  2106. return $this->setError(elFinder::ERROR_FILE_NOT_FOUND);
  2107. }
  2108. if ($name === $file['name']) {
  2109. return $file;
  2110. }
  2111. if (!empty($this->options['netkey']) && !empty($file['isroot'])) {
  2112. // change alias of netmount root
  2113. $rootKey = $this->getRootstatCachekey();
  2114. // delete old cache data
  2115. if ($this->sessionCaching['rootstat']) {
  2116. unset($this->sessionCaching['rootstat'][$rootKey]);
  2117. }
  2118. if (elFinder::$instance->updateNetVolumeOption($this->options['netkey'], 'alias', $name)) {
  2119. $this->clearcache();
  2120. $this->rootName = $this->options['alias'] = $name;
  2121. return $this->stat($this->root);
  2122. } else {
  2123. return $this->setError(elFinder::ERROR_TRGDIR_NOT_FOUND, $name);
  2124. }
  2125. }
  2126. if (!empty($file['locked'])) {
  2127. return $this->setError(elFinder::ERROR_LOCKED, $file['name']);
  2128. }
  2129. $isDir = ($file['mime'] === 'directory');
  2130. if (!$this->nameAccepted($name, $isDir)) {
  2131. return $this->setError($isDir ? elFinder::ERROR_INVALID_DIRNAME : elFinder::ERROR_INVALID_NAME);
  2132. }
  2133. if (!$isDir) {
  2134. $mimeByName = $this->mimetype($name, true);
  2135. if ($mimeByName && !$this->allowPutMime($mimeByName)) {
  2136. return $this->setError(elFinder::ERROR_UPLOAD_FILE_MIME, $name);
  2137. }
  2138. }
  2139. $path = $this->decode($hash);
  2140. $dir = $this->dirnameCE($path);
  2141. $stat = $this->isNameExists($this->joinPathCE($dir, $name));
  2142. if ($stat) {
  2143. return $this->setError(elFinder::ERROR_EXISTS, $name);
  2144. }
  2145. if (!$this->allowCreate($dir, $name, ($file['mime'] === 'directory'))) {
  2146. return $this->setError(elFinder::ERROR_PERM_DENIED);
  2147. }
  2148. $this->rmTmb($file); // remove old name tmbs, we cannot do this after dir move
  2149. if ($path = $this->convEncOut($this->_move($this->convEncIn($path), $this->convEncIn($dir), $this->convEncIn($name)))) {
  2150. $this->clearcache();
  2151. return $this->stat($path);
  2152. }
  2153. return false;
  2154. }
  2155. /**
  2156. * Create file copy with suffix "copy number" and return its info
  2157. *
  2158. * @param string $hash file hash
  2159. * @param string $suffix suffix to add to file name
  2160. *
  2161. * @return array|false
  2162. * @throws elFinderAbortException
  2163. * @author Dmitry (dio) Levashov
  2164. */
  2165. public function duplicate($hash, $suffix = 'copy')
  2166. {
  2167. if ($this->commandDisabled('duplicate')) {
  2168. return $this->setError(elFinder::ERROR_COPY, '#' . $hash, elFinder::ERROR_PERM_DENIED);
  2169. }
  2170. if (($file = $this->file($hash)) == false) {
  2171. return $this->setError(elFinder::ERROR_COPY, elFinder::ERROR_FILE_NOT_FOUND);
  2172. }
  2173. $path = $this->decode($hash);
  2174. $dir = $this->dirnameCE($path);
  2175. $name = $this->uniqueName($dir, $file['name'], sprintf($this->options['duplicateSuffix'], $suffix));
  2176. if (!$this->allowCreate($dir, $name, ($file['mime'] === 'directory'))) {
  2177. return $this->setError(elFinder::ERROR_PERM_DENIED);
  2178. }
  2179. return ($path = $this->copy($path, $dir, $name)) == false
  2180. ? false
  2181. : $this->stat($path);
  2182. }
  2183. /**
  2184. * Save uploaded file.
  2185. * On success return array with new file stat and with removed file hash (if existed file was replaced)
  2186. *
  2187. * @param Resource $fp file pointer
  2188. * @param string $dst destination folder hash
  2189. * @param $name
  2190. * @param string $tmpname file tmp name - required to detect mime type
  2191. * @param array $hashes exists files hash array with filename as key
  2192. *
  2193. * @return array|false
  2194. * @throws elFinderAbortException
  2195. * @internal param string $src file name
  2196. * @author Dmitry (dio) Levashov
  2197. */
  2198. public function upload($fp, $dst, $name, $tmpname, $hashes = array())
  2199. {
  2200. if ($this->commandDisabled('upload')) {
  2201. return $this->setError(elFinder::ERROR_PERM_DENIED);
  2202. }
  2203. if (($dir = $this->dir($dst)) == false) {
  2204. return $this->setError(elFinder::ERROR_TRGDIR_NOT_FOUND, '#' . $dst);
  2205. }
  2206. if (empty($dir['write'])) {
  2207. return $this->setError(elFinder::ERROR_PERM_DENIED);
  2208. }
  2209. if (!$this->nameAccepted($name, false)) {
  2210. return $this->setError(elFinder::ERROR_INVALID_NAME);
  2211. }
  2212. $mimeByName = '';
  2213. if ($this->mimeDetect === 'internal') {
  2214. $mime = $this->mimetype($tmpname, $name);
  2215. } else {
  2216. $mime = $this->mimetype($tmpname, $name);
  2217. $mimeByName = $this->mimetype($name, true);
  2218. if ($mime === 'unknown') {
  2219. $mime = $mimeByName;
  2220. }
  2221. }
  2222. if (!$this->allowPutMime($mime) || ($mimeByName && !$this->allowPutMime($mimeByName))) {
  2223. return $this->setError(elFinder::ERROR_UPLOAD_FILE_MIME, '(' . $mime . ')');
  2224. }
  2225. $tmpsize = (int)sprintf('%u', filesize($tmpname));
  2226. if ($this->uploadMaxSize > 0 && $tmpsize > $this->uploadMaxSize) {
  2227. return $this->setError(elFinder::ERROR_UPLOAD_FILE_SIZE);
  2228. }
  2229. $dstpath = $this->decode($dst);
  2230. if (isset($hashes[$name])) {
  2231. $test = $this->decode($hashes[$name]);
  2232. $file = $this->stat($test);
  2233. } else {
  2234. $test = $this->joinPathCE($dstpath, $name);
  2235. $file = $this->isNameExists($test);
  2236. }
  2237. $this->clearcache();
  2238. if ($file && $file['name'] === $name) { // file exists and check filename for item ID based filesystem
  2239. if ($this->uploadOverwrite) {
  2240. if (!$file['write']) {
  2241. return $this->setError(elFinder::ERROR_PERM_DENIED);
  2242. } elseif ($file['mime'] == 'directory') {
  2243. return $this->setError(elFinder::ERROR_NOT_REPLACE, $name);
  2244. }
  2245. $this->remove($test);
  2246. } else {
  2247. $name = $this->uniqueName($dstpath, $name, '-', false);
  2248. }
  2249. }
  2250. $stat = array(
  2251. 'mime' => $mime,
  2252. 'width' => 0,
  2253. 'height' => 0,
  2254. 'size' => $tmpsize);
  2255. // $w = $h = 0;
  2256. if (strpos($mime, 'image') === 0 && ($s = getimagesize($tmpname))) {
  2257. $stat['width'] = $s[0];
  2258. $stat['height'] = $s[1];
  2259. }
  2260. // $this->clearcache();
  2261. if (($path = $this->saveCE($fp, $dstpath, $name, $stat)) == false) {
  2262. return false;
  2263. }
  2264. $stat = $this->stat($path);
  2265. // Try get URL
  2266. if (empty($stat['url']) && ($url = $this->getContentUrl($stat['hash']))) {
  2267. $stat['url'] = $url;
  2268. }
  2269. return $stat;
  2270. }
  2271. /**
  2272. * Paste files
  2273. *
  2274. * @param Object $volume source volume
  2275. * @param $src
  2276. * @param string $dst destination dir hash
  2277. * @param bool $rmSrc remove source after copy?
  2278. * @param array $hashes
  2279. *
  2280. * @return array|false
  2281. * @throws elFinderAbortException
  2282. * @internal param string $source file hash
  2283. * @author Dmitry (dio) Levashov
  2284. */
  2285. public function paste($volume, $src, $dst, $rmSrc = false, $hashes = array())
  2286. {
  2287. $err = $rmSrc ? elFinder::ERROR_MOVE : elFinder::ERROR_COPY;
  2288. if ($this->commandDisabled('paste')) {
  2289. return $this->setError($err, '#' . $src, elFinder::ERROR_PERM_DENIED);
  2290. }
  2291. if (($file = $volume->file($src, $rmSrc)) == false) {
  2292. return $this->setError($err, '#' . $src, elFinder::ERROR_FILE_NOT_FOUND);
  2293. }
  2294. $name = $file['name'];
  2295. $errpath = $volume->path($file['hash']);
  2296. if (($dir = $this->dir($dst)) == false) {
  2297. return $this->setError($err, $errpath, elFinder::ERROR_TRGDIR_NOT_FOUND, '#' . $dst);
  2298. }
  2299. if (!$dir['write'] || !$file['read']) {
  2300. return $this->setError($err, $errpath, elFinder::ERROR_PERM_DENIED);
  2301. }
  2302. $destination = $this->decode($dst);
  2303. if (($test = $volume->closest($src, $rmSrc ? 'locked' : 'read', $rmSrc))) {
  2304. return $rmSrc
  2305. ? $this->setError($err, $errpath, elFinder::ERROR_LOCKED, $volume->path($test))
  2306. : $this->setError($err, $errpath, empty($file['thash']) ? elFinder::ERROR_PERM_DENIED : elFinder::ERROR_MKOUTLINK);
  2307. }
  2308. if (isset($hashes[$name])) {
  2309. $test = $this->decode($hashes[$name]);
  2310. $stat = $this->stat($test);
  2311. } else {
  2312. $test = $this->joinPathCE($destination, $name);
  2313. $stat = $this->isNameExists($test);
  2314. }
  2315. $this->clearcache();
  2316. $dstDirExists = false;
  2317. if ($stat && $stat['name'] === $name) { // file exists and check filename for item ID based filesystem
  2318. if ($this->options['copyOverwrite']) {
  2319. // do not replace file with dir or dir with file
  2320. if (!$this->isSameType($file['mime'], $stat['mime'])) {
  2321. return $this->setError(elFinder::ERROR_NOT_REPLACE, $this->path($stat['hash']));
  2322. }
  2323. // existed file is not writable
  2324. if (empty($stat['write'])) {
  2325. return $this->setError($err, $errpath, elFinder::ERROR_PERM_DENIED);
  2326. }
  2327. if ($this->options['copyJoin']) {
  2328. if (!empty($stat['locked'])) {
  2329. return $this->setError(elFinder::ERROR_LOCKED, $this->path($stat['hash']));
  2330. }
  2331. } else {
  2332. // existed file locked or has locked child
  2333. if (($locked = $this->closestByAttr($test, 'locked', true))) {
  2334. $stat = $this->stat($locked);
  2335. return $this->setError(elFinder::ERROR_LOCKED, $this->path($stat['hash']));
  2336. }
  2337. }
  2338. // target is entity file of alias
  2339. if ($volume === $this && ((isset($file['target']) && $test == $file['target']) || $test == $this->decode($src))) {
  2340. return $this->setError(elFinder::ERROR_REPLACE, $errpath);
  2341. }
  2342. // remove existed file
  2343. if (!$this->options['copyJoin'] || $stat['mime'] !== 'directory') {
  2344. if (!$this->remove($test)) {
  2345. return $this->setError(elFinder::ERROR_REPLACE, $this->path($stat['hash']));
  2346. }
  2347. } else if ($stat['mime'] === 'directory') {
  2348. $dstDirExists = true;
  2349. }
  2350. } else {
  2351. $name = $this->uniqueName($destination, $name, ' ', false);
  2352. }
  2353. }
  2354. // copy/move inside current volume
  2355. if ($volume === $this) { // changing == operand to === fixes issue #1285 - Paul Canning 24/03/2016
  2356. $source = $this->decode($src);
  2357. // do not copy into itself
  2358. if ($this->inpathCE($destination, $source)) {
  2359. return $this->setError(elFinder::ERROR_COPY_ITSELF, $errpath);
  2360. }
  2361. $rmDir = false;
  2362. if ($rmSrc) {
  2363. if ($dstDirExists) {
  2364. $rmDir = true;
  2365. $method = 'copy';
  2366. } else {
  2367. $method = 'move';
  2368. }
  2369. } else {
  2370. $method = 'copy';
  2371. }
  2372. $this->clearcache();
  2373. if ($res = ($path = $this->$method($source, $destination, $name)) ? $this->stat($path) : false) {
  2374. if ($rmDir) {
  2375. $this->remove($source);
  2376. }
  2377. } else {
  2378. return false;
  2379. }
  2380. } else {
  2381. // copy/move from another volume
  2382. if (!$this->options['copyTo'] || !$volume->copyFromAllowed()) {
  2383. return $this->setError(elFinder::ERROR_COPY, $errpath, elFinder::ERROR_PERM_DENIED);
  2384. }
  2385. $this->error = array();
  2386. if (($path = $this->copyFrom($volume, $src, $destination, $name)) == false) {
  2387. return false;
  2388. }
  2389. if ($rmSrc && !$this->error()) {
  2390. if (!$volume->rm($src)) {
  2391. if ($volume->file($src)) {
  2392. $this->addError(elFinder::ERROR_RM_SRC);
  2393. } else {
  2394. $this->removed[] = $file;
  2395. }
  2396. }
  2397. }
  2398. $res = $this->stat($path);
  2399. }
  2400. return $res;
  2401. }
  2402. /**
  2403. * Return path info array to archive of target items
  2404. *
  2405. * @param array $hashes
  2406. *
  2407. * @return array|false
  2408. * @throws Exception
  2409. * @author Naoki Sawada
  2410. */
  2411. public function zipdl($hashes)
  2412. {
  2413. if ($this->commandDisabled('zipdl')) {
  2414. return $this->setError(elFinder::ERROR_PERM_DENIED);
  2415. }
  2416. $archivers = $this->getArchivers();
  2417. $cmd = null;
  2418. if (!$archivers || empty($archivers['create'])) {
  2419. return false;
  2420. }
  2421. $archivers = $archivers['create'];
  2422. if (!$archivers) {
  2423. return false;
  2424. }
  2425. $file = $mime = '';
  2426. foreach (array('zip', 'tgz') as $ext) {
  2427. $mime = $this->mimetype('file.' . $ext, true);
  2428. if (isset($archivers[$mime])) {
  2429. $cmd = $archivers[$mime];
  2430. break;
  2431. }
  2432. }
  2433. if (!$cmd) {
  2434. $cmd = array_shift($archivers);
  2435. if (!empty($ext)) {
  2436. $mime = $this->mimetype('file.' . $ext, true);
  2437. }
  2438. }
  2439. $ext = $cmd['ext'];
  2440. $res = false;
  2441. $mixed = false;
  2442. $hashes = array_values($hashes);
  2443. $dirname = dirname(str_replace($this->separator, DIRECTORY_SEPARATOR, $this->path($hashes[0])));
  2444. $cnt = count($hashes);
  2445. if ($cnt > 1) {
  2446. for ($i = 1; $i < $cnt; $i++) {
  2447. if ($dirname !== dirname(str_replace($this->separator, DIRECTORY_SEPARATOR, $this->path($hashes[$i])))) {
  2448. $mixed = true;
  2449. break;
  2450. }
  2451. }
  2452. }
  2453. if ($mixed || $this->root == $this->dirnameCE($this->decode($hashes[0]))) {
  2454. $prefix = $this->rootName;
  2455. } else {
  2456. $prefix = basename($dirname);
  2457. }
  2458. if ($dir = $this->getItemsInHand($hashes)) {
  2459. $tmppre = (substr(PHP_OS, 0, 3) === 'WIN') ? 'zd-' : 'elfzdl-';
  2460. $pdir = dirname($dir);
  2461. // garbage collection (expire 2h)
  2462. register_shutdown_function(array('elFinder', 'GlobGC'), $pdir . DIRECTORY_SEPARATOR . $tmppre . '*', 7200);
  2463. $files = self::localScandir($dir);
  2464. if ($files && ($arc = tempnam($dir, $tmppre))) {
  2465. unlink($arc);
  2466. $arc = $arc . '.' . $ext;
  2467. $name = basename($arc);
  2468. if ($arc = $this->makeArchive($dir, $files, $name, $cmd)) {
  2469. $file = tempnam($pdir, $tmppre);
  2470. unlink($file);
  2471. $res = rename($arc, $file);
  2472. $this->rmdirRecursive($dir);
  2473. }
  2474. }
  2475. }
  2476. return $res ? array('path' => $file, 'ext' => $ext, 'mime' => $mime, 'prefix' => $prefix) : false;
  2477. }
  2478. /**
  2479. * Return file contents
  2480. *
  2481. * @param string $hash file hash
  2482. *
  2483. * @return string|false
  2484. * @author Dmitry (dio) Levashov
  2485. **/
  2486. public function getContents($hash)
  2487. {
  2488. $file = $this->file($hash);
  2489. if (!$file) {
  2490. return $this->setError(elFinder::ERROR_FILE_NOT_FOUND);
  2491. }
  2492. if ($file['mime'] == 'directory') {
  2493. return $this->setError(elFinder::ERROR_NOT_FILE);
  2494. }
  2495. if (!$file['read']) {
  2496. return $this->setError(elFinder::ERROR_PERM_DENIED);
  2497. }
  2498. if ($this->getMaxSize > 0 && $file['size'] > $this->getMaxSize) {
  2499. return $this->setError(elFinder::ERROR_UPLOAD_FILE_SIZE);
  2500. }
  2501. return $file['size'] ? $this->_getContents($this->convEncIn($this->decode($hash), true)) : '';
  2502. }
  2503. /**
  2504. * Put content in text file and return file info.
  2505. *
  2506. * @param string $hash file hash
  2507. * @param string $content new file content
  2508. *
  2509. * @return array|false
  2510. * @author Dmitry (dio) Levashov
  2511. **/
  2512. public function putContents($hash, $content)
  2513. {
  2514. if ($this->commandDisabled('edit')) {
  2515. return $this->setError(elFinder::ERROR_PERM_DENIED);
  2516. }
  2517. $path = $this->decode($hash);
  2518. if (!($file = $this->file($hash))) {
  2519. return $this->setError(elFinder::ERROR_FILE_NOT_FOUND);
  2520. }
  2521. if (!$file['write']) {
  2522. return $this->setError(elFinder::ERROR_PERM_DENIED);
  2523. }
  2524. // check data cheme
  2525. if (preg_match('~^\0data:(.+?/.+?);base64,~', $content, $m)) {
  2526. $dMime = $m[1];
  2527. if ($file['size'] > 0 && $dMime !== $file['mime']) {
  2528. return $this->setError(elFinder::ERROR_PERM_DENIED);
  2529. }
  2530. $content = base64_decode(substr($content, strlen($m[0])));
  2531. }
  2532. // check MIME
  2533. $name = $this->basenameCE($path);
  2534. $mime = '';
  2535. $mimeByName = $this->mimetype($name, true);
  2536. if ($this->mimeDetect !== 'internal') {
  2537. if ($tp = $this->tmpfile()) {
  2538. fwrite($tp, $content);
  2539. $info = stream_get_meta_data($tp);
  2540. $filepath = $info['uri'];
  2541. $mime = $this->mimetype($filepath, $name);
  2542. fclose($tp);
  2543. }
  2544. }
  2545. if (!$this->allowPutMime($mimeByName) || ($mime && !$this->allowPutMime($mime))) {
  2546. return $this->setError(elFinder::ERROR_UPLOAD_FILE_MIME);
  2547. }
  2548. $this->clearcache();
  2549. $res = false;
  2550. if ($this->convEncOut($this->_filePutContents($this->convEncIn($path), $content))) {
  2551. $this->rmTmb($file);
  2552. $this->clearstatcache();
  2553. $res = $this->stat($path);
  2554. }
  2555. return $res;
  2556. }
  2557. /**
  2558. * Extract files from archive
  2559. *
  2560. * @param string $hash archive hash
  2561. * @param null $makedir
  2562. *
  2563. * @return array|bool
  2564. * @author Dmitry (dio) Levashov,
  2565. * @author Alexey Sukhotin
  2566. */
  2567. public function extract($hash, $makedir = null)
  2568. {
  2569. if ($this->commandDisabled('extract')) {
  2570. return $this->setError(elFinder::ERROR_PERM_DENIED);
  2571. }
  2572. if (($file = $this->file($hash)) == false) {
  2573. return $this->setError(elFinder::ERROR_FILE_NOT_FOUND);
  2574. }
  2575. $archiver = isset($this->archivers['extract'][$file['mime']])
  2576. ? $this->archivers['extract'][$file['mime']]
  2577. : array();
  2578. if (!$archiver) {
  2579. return $this->setError(elFinder::ERROR_NOT_ARCHIVE);
  2580. }
  2581. $path = $this->decode($hash);
  2582. $parent = $this->stat($this->dirnameCE($path));
  2583. if (!$file['read'] || !$parent['write']) {
  2584. return $this->setError(elFinder::ERROR_PERM_DENIED);
  2585. }
  2586. $this->clearcache();
  2587. $this->extractToNewdir = is_null($makedir) ? 'auto' : (bool)$makedir;
  2588. if ($path = $this->convEncOut($this->_extract($this->convEncIn($path), $archiver))) {
  2589. if (is_array($path)) {
  2590. foreach ($path as $_k => $_p) {
  2591. $path[$_k] = $this->stat($_p);
  2592. }
  2593. } else {
  2594. $path = $this->stat($path);
  2595. }
  2596. return $path;
  2597. } else {
  2598. return false;
  2599. }
  2600. }
  2601. /**
  2602. * Add files to archive
  2603. *
  2604. * @param $hashes
  2605. * @param $mime
  2606. * @param string $name
  2607. *
  2608. * @return array|bool
  2609. * @throws Exception
  2610. */
  2611. public function archive($hashes, $mime, $name = '')
  2612. {
  2613. if ($this->commandDisabled('archive')) {
  2614. return $this->setError(elFinder::ERROR_PERM_DENIED);
  2615. }
  2616. if ($name !== '' && !$this->nameAccepted($name, false)) {
  2617. return $this->setError(elFinder::ERROR_INVALID_NAME);
  2618. }
  2619. $archiver = isset($this->archivers['create'][$mime])
  2620. ? $this->archivers['create'][$mime]
  2621. : array();
  2622. if (!$archiver) {
  2623. return $this->setError(elFinder::ERROR_ARCHIVE_TYPE);
  2624. }
  2625. $files = array();
  2626. $useRemoteArchive = !empty($this->options['useRemoteArchive']);
  2627. $dir = '';
  2628. foreach ($hashes as $hash) {
  2629. if (($file = $this->file($hash)) == false) {
  2630. return $this->setError(elFinder::ERROR_FILE_NOT_FOUND, '#' . $hash);
  2631. }
  2632. if (!$file['read']) {
  2633. return $this->setError(elFinder::ERROR_PERM_DENIED);
  2634. }
  2635. $path = $this->decode($hash);
  2636. if ($dir === '') {
  2637. $dir = $this->dirnameCE($path);
  2638. $stat = $this->stat($dir);
  2639. if (!$stat['write']) {
  2640. return $this->setError(elFinder::ERROR_PERM_DENIED);
  2641. }
  2642. }
  2643. $files[] = $useRemoteArchive ? $hash : $this->basenameCE($path);
  2644. }
  2645. if ($name === '') {
  2646. $name = count($files) == 1 ? $files[0] : 'Archive';
  2647. } else {
  2648. $name = str_replace(array('/', '\\'), '_', preg_replace('/\.' . preg_quote($archiver['ext'], '/') . '$/i', '', $name));
  2649. }
  2650. $name .= '.' . $archiver['ext'];
  2651. $name = $this->uniqueName($dir, $name, '');
  2652. $this->clearcache();
  2653. if ($useRemoteArchive) {
  2654. return ($path = $this->remoteArchive($files, $name, $archiver)) ? $this->stat($path) : false;
  2655. } else {
  2656. return ($path = $this->convEncOut($this->_archive($this->convEncIn($dir), $this->convEncIn($files), $this->convEncIn($name), $archiver))) ? $this->stat($path) : false;
  2657. }
  2658. }
  2659. /**
  2660. * Create an archive from remote items
  2661. *
  2662. * @param array $hashes files hashes list
  2663. * @param string $name archive name
  2664. * @param array $arc archiver options
  2665. *
  2666. * @return string|boolean path of created archive
  2667. * @throws Exception
  2668. */
  2669. protected function remoteArchive($hashes, $name, $arc)
  2670. {
  2671. $resPath = false;
  2672. $file0 = $this->file($hashes[0]);
  2673. if ($file0 && ($dir = $this->getItemsInHand($hashes))) {
  2674. $files = self::localScandir($dir);
  2675. if ($files) {
  2676. if ($arc = $this->makeArchive($dir, $files, $name, $arc)) {
  2677. if ($fp = fopen($arc, 'rb')) {
  2678. $fstat = stat($arc);
  2679. $stat = array(
  2680. 'size' => $fstat['size'],
  2681. 'ts' => $fstat['mtime'],
  2682. 'mime' => $this->mimetype($arc, $name)
  2683. );
  2684. $path = $this->decode($file0['phash']);
  2685. $resPath = $this->saveCE($fp, $path, $name, $stat);
  2686. fclose($fp);
  2687. }
  2688. }
  2689. }
  2690. $this->rmdirRecursive($dir);
  2691. }
  2692. return $resPath;
  2693. }
  2694. /**
  2695. * Resize image
  2696. *
  2697. * @param string $hash image file
  2698. * @param int $width new width
  2699. * @param int $height new height
  2700. * @param int $x X start poistion for crop
  2701. * @param int $y Y start poistion for crop
  2702. * @param string $mode action how to mainpulate image
  2703. * @param string $bg background color
  2704. * @param int $degree rotete degree
  2705. * @param int $jpgQuality JEPG quality (1-100)
  2706. *
  2707. * @return array|false
  2708. * @throws ImagickException
  2709. * @throws elFinderAbortException
  2710. * @author Dmitry (dio) Levashov
  2711. * @author Alexey Sukhotin
  2712. * @author nao-pon
  2713. * @author Troex Nevelin
  2714. */
  2715. public function resize($hash, $width, $height, $x, $y, $mode = 'resize', $bg = '', $degree = 0, $jpgQuality = null)
  2716. {
  2717. if ($this->commandDisabled('resize')) {
  2718. return $this->setError(elFinder::ERROR_PERM_DENIED);
  2719. }
  2720. if ($mode === 'rotate' && $degree == 0) {
  2721. return array('losslessRotate' => ($this->procExec(ELFINDER_EXIFTRAN_PATH . ' -h') === 0 || $this->procExec(ELFINDER_JPEGTRAN_PATH . ' -version') === 0));
  2722. }
  2723. if (($file = $this->file($hash)) == false) {
  2724. return $this->setError(elFinder::ERROR_FILE_NOT_FOUND);
  2725. }
  2726. if (!$file['write'] || !$file['read']) {
  2727. return $this->setError(elFinder::ERROR_PERM_DENIED);
  2728. }
  2729. $path = $this->decode($hash);
  2730. $work_path = $this->getWorkFile($this->encoding ? $this->convEncIn($path, true) : $path);
  2731. if (!$work_path || !is_writable($work_path)) {
  2732. if ($work_path && $path !== $work_path && is_file($work_path)) {
  2733. unlink($work_path);
  2734. }
  2735. return $this->setError(elFinder::ERROR_PERM_DENIED);
  2736. }
  2737. if ($this->imgLib !== 'imagick' && $this->imgLib !== 'convert') {
  2738. if (elFinder::isAnimationGif($work_path)) {
  2739. return $this->setError(elFinder::ERROR_UNSUPPORT_TYPE);
  2740. }
  2741. }
  2742. if (elFinder::isAnimationPng($work_path)) {
  2743. return $this->setError(elFinder::ERROR_UNSUPPORT_TYPE);
  2744. }
  2745. switch ($mode) {
  2746. case 'propresize':
  2747. $result = $this->imgResize($work_path, $width, $height, true, true, null, $jpgQuality);
  2748. break;
  2749. case 'crop':
  2750. $result = $this->imgCrop($work_path, $width, $height, $x, $y, null, $jpgQuality);
  2751. break;
  2752. case 'fitsquare':
  2753. $result = $this->imgSquareFit($work_path, $width, $height, 'center', 'middle', ($bg ? $bg : $this->options['tmbBgColor']), null, $jpgQuality);
  2754. break;
  2755. case 'rotate':
  2756. $result = $this->imgRotate($work_path, $degree, ($bg ? $bg : $this->options['bgColorFb']), null, $jpgQuality);
  2757. break;
  2758. default:
  2759. $result = $this->imgResize($work_path, $width, $height, false, true, null, $jpgQuality);
  2760. break;
  2761. }
  2762. $ret = false;
  2763. if ($result) {
  2764. $this->rmTmb($file);
  2765. $this->clearstatcache();
  2766. $fstat = stat($work_path);
  2767. $imgsize = getimagesize($work_path);
  2768. if ($path !== $work_path) {
  2769. $file['size'] = $fstat['size'];
  2770. $file['ts'] = $fstat['mtime'];
  2771. if ($imgsize) {
  2772. $file['width'] = $imgsize[0];
  2773. $file['height'] = $imgsize[1];
  2774. }
  2775. if ($fp = fopen($work_path, 'rb')) {
  2776. $ret = $this->saveCE($fp, $this->dirnameCE($path), $this->basenameCE($path), $file);
  2777. fclose($fp);
  2778. }
  2779. } else {
  2780. $ret = true;
  2781. }
  2782. if ($ret) {
  2783. $this->clearcache();
  2784. $ret = $this->stat($path);
  2785. if ($imgsize) {
  2786. $ret['width'] = $imgsize[0];
  2787. $ret['height'] = $imgsize[1];
  2788. }
  2789. }
  2790. }
  2791. if ($path !== $work_path) {
  2792. is_file($work_path) && unlink($work_path);
  2793. }
  2794. return $ret;
  2795. }
  2796. /**
  2797. * Remove file/dir
  2798. *
  2799. * @param string $hash file hash
  2800. *
  2801. * @return bool
  2802. * @throws elFinderAbortException
  2803. * @author Dmitry (dio) Levashov
  2804. */
  2805. public function rm($hash)
  2806. {
  2807. return $this->commandDisabled('rm')
  2808. ? $this->setError(elFinder::ERROR_PERM_DENIED)
  2809. : $this->remove($this->decode($hash));
  2810. }
  2811. /**
  2812. * Search files
  2813. *
  2814. * @param string $q search string
  2815. * @param array $mimes
  2816. * @param null $hash
  2817. *
  2818. * @return array
  2819. * @throws elFinderAbortException
  2820. * @author Dmitry (dio) Levashov
  2821. */
  2822. public function search($q, $mimes, $hash = null)
  2823. {
  2824. $res = array();
  2825. $matchMethod = null;
  2826. $args = func_get_args();
  2827. if (!empty($args[3])) {
  2828. $matchMethod = 'searchMatch' . $args[3];
  2829. if (!is_callable(array($this, $matchMethod))) {
  2830. return array();
  2831. }
  2832. }
  2833. $dir = null;
  2834. if ($hash) {
  2835. $dir = $this->decode($hash);
  2836. $stat = $this->stat($dir);
  2837. if (!$stat || $stat['mime'] !== 'directory' || !$stat['read']) {
  2838. $q = '';
  2839. }
  2840. }
  2841. if ($mimes && $this->onlyMimes) {
  2842. $mimes = array_intersect($mimes, $this->onlyMimes);
  2843. if (!$mimes) {
  2844. $q = '';
  2845. }
  2846. }
  2847. $this->searchStart = time();
  2848. $qs = preg_split('/"([^"]+)"| +/', $q, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
  2849. $query = $excludes = array();
  2850. foreach ($qs as $_q) {
  2851. $_q = trim($_q);
  2852. if ($_q !== '') {
  2853. if ($_q[0] === '-') {
  2854. if (isset($_q[1])) {
  2855. $excludes[] = substr($_q, 1);
  2856. }
  2857. } else {
  2858. $query[] = $_q;
  2859. }
  2860. }
  2861. }
  2862. if (!$query) {
  2863. $q = '';
  2864. } else {
  2865. $q = join(' ', $query);
  2866. $this->doSearchCurrentQuery = array(
  2867. 'q' => $q,
  2868. 'excludes' => $excludes,
  2869. 'matchMethod' => $matchMethod
  2870. );
  2871. }
  2872. if ($q === '' || $this->commandDisabled('search')) {
  2873. return $res;
  2874. }
  2875. // valided regex $this->options['searchExDirReg']
  2876. if ($this->options['searchExDirReg']) {
  2877. if (false === preg_match($this->options['searchExDirReg'], '')) {
  2878. $this->options['searchExDirReg'] = '';
  2879. }
  2880. }
  2881. // check the leaf root too
  2882. if (!$mimes && (is_null($dir) || $dir == $this->root)) {
  2883. $rootStat = $this->stat($this->root);
  2884. if (!empty($rootStat['phash'])) {
  2885. if ($this->stripos($rootStat['name'], $q) !== false) {
  2886. $res = array($rootStat);
  2887. }
  2888. }
  2889. }
  2890. return array_merge($res, $this->doSearch(is_null($dir) ? $this->root : $dir, $q, $mimes));
  2891. }
  2892. /**
  2893. * Return image dimensions
  2894. *
  2895. * @param string $hash file hash
  2896. *
  2897. * @return array|string
  2898. * @author Dmitry (dio) Levashov
  2899. **/
  2900. public function dimensions($hash)
  2901. {
  2902. if (($file = $this->file($hash)) == false) {
  2903. return false;
  2904. }
  2905. // Throw additional parameters for some drivers
  2906. if (func_num_args() > 1) {
  2907. $args = func_get_arg(1);
  2908. } else {
  2909. $args = array();
  2910. }
  2911. return $this->convEncOut($this->_dimensions($this->convEncIn($this->decode($hash)), $file['mime'], $args));
  2912. }
  2913. /**
  2914. * Return has subdirs
  2915. *
  2916. * @param string $hash file hash
  2917. *
  2918. * @return bool
  2919. * @author Naoki Sawada
  2920. **/
  2921. public function subdirs($hash)
  2922. {
  2923. return (bool)$this->subdirsCE($this->decode($hash));
  2924. }
  2925. /**
  2926. * Return content URL (for netmout volume driver)
  2927. * If file.url == 1 requests from JavaScript client with XHR
  2928. *
  2929. * @param string $hash file hash
  2930. * @param array $options options array
  2931. *
  2932. * @return boolean|string
  2933. * @author Naoki Sawada
  2934. */
  2935. public function getContentUrl($hash, $options = array())
  2936. {
  2937. if (($file = $this->file($hash)) === false) {
  2938. return false;
  2939. }
  2940. if (!empty($options['onetime']) && $this->options['onetimeUrl']) {
  2941. if (is_callable($this->options['onetimeUrl'])) {
  2942. return call_user_func_array($this->options['onetimeUrl'], array($file, $options, $this));
  2943. } else {
  2944. $ret = false;
  2945. if ($tmpdir = elFinder::getStaticVar('commonTempPath')) {
  2946. if ($source = $this->open($hash)) {
  2947. if ($_dat = tempnam($tmpdir, 'ELF')) {
  2948. $token = md5($_dat . session_id());
  2949. $dat = $tmpdir . DIRECTORY_SEPARATOR . 'ELF' . $token;
  2950. if (rename($_dat, $dat)) {
  2951. $info = stream_get_meta_data($source);
  2952. if (!empty($info['uri'])) {
  2953. $tmp = $info['uri'];
  2954. } else {
  2955. $tmp = tempnam($tmpdir, 'ELF');
  2956. if ($dest = fopen($tmp, 'wb')) {
  2957. if (!stream_copy_to_stream($source, $dest)) {
  2958. $tmp = false;
  2959. }
  2960. fclose($dest);
  2961. }
  2962. }
  2963. $this->close($source, $hash);
  2964. if ($tmp) {
  2965. $info = array(
  2966. 'file' => base64_encode($tmp),
  2967. 'name' => $file['name'],
  2968. 'mime' => $file['mime'],
  2969. 'ts' => $file['ts']
  2970. );
  2971. if (file_put_contents($dat, json_encode($info))) {
  2972. $conUrl = elFinder::getConnectorUrl();
  2973. $ret = $conUrl . (strpos($conUrl, '?') !== false? '&' : '?') . 'cmd=file&onetime=1&target=' . $token;
  2974. }
  2975. }
  2976. if (!$ret) {
  2977. unlink($dat);
  2978. }
  2979. } else {
  2980. unlink($_dat);
  2981. }
  2982. }
  2983. }
  2984. }
  2985. return $ret;
  2986. }
  2987. }
  2988. if (empty($file['url']) && $this->URL) {
  2989. $path = str_replace($this->separator, '/', substr($this->decode($hash), strlen(rtrim($this->root, '/' . $this->separator)) + 1));
  2990. if ($this->encoding) {
  2991. $path = $this->convEncIn($path, true);
  2992. }
  2993. $path = str_replace('%2F', '/', rawurlencode($path));
  2994. return $this->URL . $path;
  2995. } else {
  2996. $ret = false;
  2997. if (!empty($file['url']) && $file['url'] != 1) {
  2998. return $file['url'];
  2999. } else if (!empty($options['temporary']) && ($tempInfo = $this->getTempLinkInfo('temp_' . md5($hash . session_id())))) {
  3000. if (is_readable($tempInfo['path'])) {
  3001. touch($tempInfo['path']);
  3002. $ret = $tempInfo['url'] . '?' . rawurlencode($file['name']);
  3003. } else if ($source = $this->open($hash)) {
  3004. if ($dest = fopen($tempInfo['path'], 'wb')) {
  3005. if (stream_copy_to_stream($source, $dest)) {
  3006. $ret = $tempInfo['url'] . '?' . rawurlencode($file['name']);
  3007. }
  3008. fclose($dest);
  3009. }
  3010. $this->close($source, $hash);
  3011. }
  3012. }
  3013. return $ret;
  3014. }
  3015. }
  3016. /**
  3017. * Get temporary contents link infomation
  3018. *
  3019. * @param string $name
  3020. *
  3021. * @return boolean|array
  3022. * @author Naoki Sawada
  3023. */
  3024. public function getTempLinkInfo($name = null)
  3025. {
  3026. if ($this->tmpLinkPath) {
  3027. if (!$name) {
  3028. $name = 'temp_' . md5($_SERVER['REMOTE_ADDR'] . (string)microtime(true));
  3029. } else if (substr($name, 0, 5) !== 'temp_') {
  3030. $name = 'temp_' . $name;
  3031. }
  3032. register_shutdown_function(array('elFinder', 'GlobGC'), $this->tmpLinkPath . DIRECTORY_SEPARATOR . 'temp_*', elFinder::$tmpLinkLifeTime);
  3033. return array(
  3034. 'path' => $path = $this->tmpLinkPath . DIRECTORY_SEPARATOR . $name,
  3035. 'url' => $this->tmpLinkUrl . '/' . rawurlencode($name)
  3036. );
  3037. }
  3038. return false;
  3039. }
  3040. /**
  3041. * Get URL of substitute image by request args `substitute` or 4th argument $maxSize
  3042. *
  3043. * @param string $target Target hash
  3044. * @param array $srcSize Size info array [width, height]
  3045. * @param resource $srcfp Source file file pointer
  3046. * @param integer $maxSize Maximum pixel of substitute image
  3047. *
  3048. * @return boolean
  3049. * @throws ImagickException
  3050. * @throws elFinderAbortException
  3051. */
  3052. public function getSubstituteImgLink($target, $srcSize, $srcfp = null, $maxSize = null)
  3053. {
  3054. $url = false;
  3055. $file = $this->file($target);
  3056. $force = !in_array($file['mime'], array('image/jpeg', 'image/png', 'image/gif'));
  3057. if (!$maxSize) {
  3058. $args = elFinder::$currentArgs;
  3059. if (!empty($args['substitute'])) {
  3060. $maxSize = $args['substitute'];
  3061. }
  3062. }
  3063. if ($maxSize && $srcSize[0] && $srcSize[1]) {
  3064. if ($this->getOption('substituteImg')) {
  3065. $maxSize = intval($maxSize);
  3066. $zoom = min(($maxSize / $srcSize[0]), ($maxSize / $srcSize[1]));
  3067. if ($force || $zoom < 1) {
  3068. $width = round($srcSize[0] * $zoom);
  3069. $height = round($srcSize[1] * $zoom);
  3070. $jpgQuality = 50;
  3071. $preserveExif = false;
  3072. $unenlarge = true;
  3073. $checkAnimated = true;
  3074. $destformat = $file['mime'] === 'image/jpeg'? null : 'png';
  3075. if (!$srcfp) {
  3076. elFinder::checkAborted();
  3077. $srcfp = $this->open($target);
  3078. }
  3079. if ($srcfp && ($tempLink = $this->getTempLinkInfo())) {
  3080. elFinder::checkAborted();
  3081. $dest = fopen($tempLink['path'], 'wb');
  3082. if ($dest && stream_copy_to_stream($srcfp, $dest)) {
  3083. fclose($dest);
  3084. if ($this->imageUtil('resize', $tempLink['path'], compact('width', 'height', 'jpgQuality', 'preserveExif', 'unenlarge', 'checkAnimated', 'destformat'))) {
  3085. $url = $tempLink['url'];
  3086. // set expire to 1 min left
  3087. touch($tempLink['path'], time() - elFinder::$tmpLinkLifeTime + 60);
  3088. } else {
  3089. unlink($tempLink['path']);
  3090. }
  3091. }
  3092. $this->close($srcfp, $target);
  3093. }
  3094. }
  3095. }
  3096. }
  3097. return $url;
  3098. }
  3099. /**
  3100. * Return temp path
  3101. *
  3102. * @return string
  3103. * @author Naoki Sawada
  3104. */
  3105. public function getTempPath()
  3106. {
  3107. $tempPath = null;
  3108. if (isset($this->tmpPath) && $this->tmpPath && is_writable($this->tmpPath)) {
  3109. $tempPath = $this->tmpPath;
  3110. } else if (isset($this->tmp) && $this->tmp && is_writable($this->tmp)) {
  3111. $tempPath = $this->tmp;
  3112. } else if (elFinder::getStaticVar('commonTempPath') && is_writable(elFinder::getStaticVar('commonTempPath'))) {
  3113. $tempPath = elFinder::getStaticVar('commonTempPath');
  3114. } else if (function_exists('sys_get_temp_dir')) {
  3115. $tempPath = sys_get_temp_dir();
  3116. } else if ($this->tmbPathWritable) {
  3117. $tempPath = $this->tmbPath;
  3118. }
  3119. if ($tempPath && DIRECTORY_SEPARATOR !== '/') {
  3120. $tempPath = str_replace('/', DIRECTORY_SEPARATOR, $tempPath);
  3121. }
  3122. return $tempPath;
  3123. }
  3124. /**
  3125. * (Make &) Get upload taget dirctory hash
  3126. *
  3127. * @param string $baseTargetHash
  3128. * @param string $path
  3129. * @param array $result
  3130. *
  3131. * @return boolean|string
  3132. * @author Naoki Sawada
  3133. */
  3134. public function getUploadTaget($baseTargetHash, $path, & $result)
  3135. {
  3136. $base = $this->decode($baseTargetHash);
  3137. $targetHash = $baseTargetHash;
  3138. $path = ltrim($path, $this->separator);
  3139. $dirs = explode($this->separator, $path);
  3140. array_pop($dirs);
  3141. foreach ($dirs as $dir) {
  3142. $targetPath = $this->joinPathCE($base, $dir);
  3143. if (!$_realpath = $this->realpath($this->encode($targetPath))) {
  3144. if ($stat = $this->mkdir($targetHash, $dir)) {
  3145. $result['added'][] = $stat;
  3146. $targetHash = $stat['hash'];
  3147. $base = $this->decode($targetHash);
  3148. } else {
  3149. return false;
  3150. }
  3151. } else {
  3152. $targetHash = $this->encode($_realpath);
  3153. if ($this->dir($targetHash)) {
  3154. $base = $this->decode($targetHash);
  3155. } else {
  3156. return false;
  3157. }
  3158. }
  3159. }
  3160. return $targetHash;
  3161. }
  3162. /**
  3163. * Return this uploadMaxSize value
  3164. *
  3165. * @return integer
  3166. * @author Naoki Sawada
  3167. */
  3168. public function getUploadMaxSize()
  3169. {
  3170. return $this->uploadMaxSize;
  3171. }
  3172. public function setUploadOverwrite($var)
  3173. {
  3174. $this->uploadOverwrite = (bool)$var;
  3175. }
  3176. /**
  3177. * Image file utility
  3178. *
  3179. * @param string $mode 'resize', 'rotate', 'propresize', 'crop', 'fitsquare'
  3180. * @param string $src Image file local path
  3181. * @param array $options excute options
  3182. *
  3183. * @return bool
  3184. * @throws ImagickException
  3185. * @throws elFinderAbortException
  3186. * @author Naoki Sawada
  3187. */
  3188. public function imageUtil($mode, $src, $options = array())
  3189. {
  3190. if (!isset($options['jpgQuality'])) {
  3191. $options['jpgQuality'] = intval($this->options['jpgQuality']);
  3192. }
  3193. if (!isset($options['bgcolor'])) {
  3194. $options['bgcolor'] = '#ffffff';
  3195. }
  3196. if (!isset($options['bgColorFb'])) {
  3197. $options['bgColorFb'] = $this->options['bgColorFb'];
  3198. }
  3199. $destformat = !empty($options['destformat'])? $options['destformat'] : null;
  3200. // check 'width' ,'height'
  3201. if (in_array($mode, array('resize', 'propresize', 'crop', 'fitsquare'))) {
  3202. if (empty($options['width']) || empty($options['height'])) {
  3203. return false;
  3204. }
  3205. }
  3206. if (!empty($options['checkAnimated'])) {
  3207. if ($this->imgLib !== 'imagick' && $this->imgLib !== 'convert') {
  3208. if (elFinder::isAnimationGif($src)) {
  3209. return false;
  3210. }
  3211. }
  3212. if (elFinder::isAnimationPng($src)) {
  3213. return false;
  3214. }
  3215. }
  3216. switch ($mode) {
  3217. case 'rotate':
  3218. if (empty($options['degree'])) {
  3219. return true;
  3220. }
  3221. return (bool)$this->imgRotate($src, $options['degree'], $options['bgColorFb'], $destformat, $options['jpgQuality']);
  3222. case 'resize':
  3223. return (bool)$this->imgResize($src, $options['width'], $options['height'], false, true, $destformat, $options['jpgQuality'], $options);
  3224. case 'propresize':
  3225. return (bool)$this->imgResize($src, $options['width'], $options['height'], true, true, $destformat, $options['jpgQuality'], $options);
  3226. case 'crop':
  3227. if (isset($options['x']) && isset($options['y'])) {
  3228. return (bool)$this->imgCrop($src, $options['width'], $options['height'], $options['x'], $options['y'], $destformat, $options['jpgQuality']);
  3229. }
  3230. break;
  3231. case 'fitsquare':
  3232. return (bool)$this->imgSquareFit($src, $options['width'], $options['height'], 'center', 'middle', $options['bgcolor'], $destformat, $options['jpgQuality']);
  3233. }
  3234. return false;
  3235. }
  3236. /**
  3237. * Convert Video To Image by ffmpeg
  3238. *
  3239. * @param string $file video source file path
  3240. * @param array $stat file stat array
  3241. * @param object $self volume driver object
  3242. * @param int $ss start seconds
  3243. *
  3244. * @return bool
  3245. * @throws elFinderAbortException
  3246. * @author Naoki Sawada
  3247. */
  3248. public function ffmpegToImg($file, $stat, $self, $ss = null)
  3249. {
  3250. $name = basename($file);
  3251. $path = dirname($file);
  3252. $tmp = $path . DIRECTORY_SEPARATOR . md5($name);
  3253. // register auto delete on shutdown
  3254. $GLOBALS['elFinderTempFiles'][$tmp] = true;
  3255. if (rename($file, $tmp)) {
  3256. if ($ss === null) {
  3257. // specific start time by file name (xxx^[sec].[extention] - video^3.mp4)
  3258. if (preg_match('/\^(\d+(?:\.\d+)?)\.[^.]+$/', $stat['name'], $_m)) {
  3259. $ss = $_m[1];
  3260. } else {
  3261. $ss = $this->options['tmbVideoConvSec'];
  3262. }
  3263. }
  3264. $cmd = sprintf(ELFINDER_FFMPEG_PATH . ' -i %s -ss 00:00:%.3f -vframes 1 -f image2 -- %s', escapeshellarg($tmp), $ss, escapeshellarg($file));
  3265. $r = ($this->procExec($cmd) === 0);
  3266. clearstatcache();
  3267. if ($r && $ss > 0 && !file_exists($file)) {
  3268. // Retry by half of $ss
  3269. $ss = max(intval($ss / 2), 0);
  3270. rename($tmp, $file);
  3271. $r = $this->ffmpegToImg($file, $stat, $self, $ss);
  3272. } else {
  3273. unlink($tmp);
  3274. }
  3275. return $r;
  3276. }
  3277. return false;
  3278. }
  3279. /**
  3280. * Creates a temporary file and return file pointer
  3281. *
  3282. * @return resource|boolean
  3283. */
  3284. public function tmpfile()
  3285. {
  3286. if ($tmp = $this->getTempFile()) {
  3287. return fopen($tmp, 'wb');
  3288. }
  3289. return false;
  3290. }
  3291. /**
  3292. * Save error message
  3293. *
  3294. * @param array error
  3295. *
  3296. * @return boolean false
  3297. * @author Naoki Sawada
  3298. **/
  3299. protected function setError()
  3300. {
  3301. $this->error = array();
  3302. $this->addError(func_get_args());
  3303. return false;
  3304. }
  3305. /**
  3306. * Add error message
  3307. *
  3308. * @param array error
  3309. *
  3310. * @return false
  3311. * @author Dmitry(dio) Levashov
  3312. **/
  3313. protected function addError()
  3314. {
  3315. foreach (func_get_args() as $err) {
  3316. if (is_array($err)) {
  3317. foreach($err as $er) {
  3318. $this->addError($er);
  3319. }
  3320. } else {
  3321. $this->error[] = (string)$err;
  3322. }
  3323. }
  3324. return false;
  3325. }
  3326. /*********************************************************************/
  3327. /* FS API */
  3328. /*********************************************************************/
  3329. /***************** server encoding support *******************/
  3330. /**
  3331. * Return parent directory path (with convert encoding)
  3332. *
  3333. * @param string $path file path
  3334. *
  3335. * @return string
  3336. * @author Naoki Sawada
  3337. **/
  3338. protected function dirnameCE($path)
  3339. {
  3340. $dirname = (!$this->encoding) ? $this->_dirname($path) : $this->convEncOut($this->_dirname($this->convEncIn($path)));
  3341. // check to infinite loop prevention
  3342. return ($dirname != $path) ? $dirname : '';
  3343. }
  3344. /**
  3345. * Return file name (with convert encoding)
  3346. *
  3347. * @param string $path file path
  3348. *
  3349. * @return string
  3350. * @author Naoki Sawada
  3351. **/
  3352. protected function basenameCE($path)
  3353. {
  3354. return (!$this->encoding) ? $this->_basename($path) : $this->convEncOut($this->_basename($this->convEncIn($path)));
  3355. }
  3356. /**
  3357. * Join dir name and file name and return full path. (with convert encoding)
  3358. * Some drivers (db) use int as path - so we give to concat path to driver itself
  3359. *
  3360. * @param string $dir dir path
  3361. * @param string $name file name
  3362. *
  3363. * @return string
  3364. * @author Naoki Sawada
  3365. **/
  3366. protected function joinPathCE($dir, $name)
  3367. {
  3368. return (!$this->encoding) ? $this->_joinPath($dir, $name) : $this->convEncOut($this->_joinPath($this->convEncIn($dir), $this->convEncIn($name)));
  3369. }
  3370. /**
  3371. * Return normalized path (with convert encoding)
  3372. *
  3373. * @param string $path file path
  3374. *
  3375. * @return string
  3376. * @author Naoki Sawada
  3377. **/
  3378. protected function normpathCE($path)
  3379. {
  3380. return (!$this->encoding) ? $this->_normpath($path) : $this->convEncOut($this->_normpath($this->convEncIn($path)));
  3381. }
  3382. /**
  3383. * Return file path related to root dir (with convert encoding)
  3384. *
  3385. * @param string $path file path
  3386. *
  3387. * @return string
  3388. * @author Naoki Sawada
  3389. **/
  3390. protected function relpathCE($path)
  3391. {
  3392. return (!$this->encoding) ? $this->_relpath($path) : $this->convEncOut($this->_relpath($this->convEncIn($path)));
  3393. }
  3394. /**
  3395. * Convert path related to root dir into real path (with convert encoding)
  3396. *
  3397. * @param string $path rel file path
  3398. *
  3399. * @return string
  3400. * @author Naoki Sawada
  3401. **/
  3402. protected function abspathCE($path)
  3403. {
  3404. return (!$this->encoding) ? $this->_abspath($path) : $this->convEncOut($this->_abspath($this->convEncIn($path)));
  3405. }
  3406. /**
  3407. * Return true if $path is children of $parent (with convert encoding)
  3408. *
  3409. * @param string $path path to check
  3410. * @param string $parent parent path
  3411. *
  3412. * @return bool
  3413. * @author Naoki Sawada
  3414. **/
  3415. protected function inpathCE($path, $parent)
  3416. {
  3417. return (!$this->encoding) ? $this->_inpath($path, $parent) : $this->convEncOut($this->_inpath($this->convEncIn($path), $this->convEncIn($parent)));
  3418. }
  3419. /**
  3420. * Open file and return file pointer (with convert encoding)
  3421. *
  3422. * @param string $path file path
  3423. * @param string $mode
  3424. *
  3425. * @return false|resource
  3426. * @internal param bool $write open file for writing
  3427. * @author Naoki Sawada
  3428. */
  3429. protected function fopenCE($path, $mode = 'rb')
  3430. {
  3431. // check extra option for network stream pointer
  3432. if (func_num_args() > 2) {
  3433. $opts = func_get_arg(2);
  3434. } else {
  3435. $opts = array();
  3436. }
  3437. return (!$this->encoding) ? $this->_fopen($path, $mode, $opts) : $this->convEncOut($this->_fopen($this->convEncIn($path), $mode, $opts));
  3438. }
  3439. /**
  3440. * Close opened file (with convert encoding)
  3441. *
  3442. * @param resource $fp file pointer
  3443. * @param string $path file path
  3444. *
  3445. * @return bool
  3446. * @author Naoki Sawada
  3447. **/
  3448. protected function fcloseCE($fp, $path = '')
  3449. {
  3450. return (!$this->encoding) ? $this->_fclose($fp, $path) : $this->convEncOut($this->_fclose($fp, $this->convEncIn($path)));
  3451. }
  3452. /**
  3453. * Create new file and write into it from file pointer. (with convert encoding)
  3454. * Return new file path or false on error.
  3455. *
  3456. * @param resource $fp file pointer
  3457. * @param string $dir target dir path
  3458. * @param string $name file name
  3459. * @param array $stat file stat (required by some virtual fs)
  3460. *
  3461. * @return bool|string
  3462. * @author Naoki Sawada
  3463. **/
  3464. protected function saveCE($fp, $dir, $name, $stat)
  3465. {
  3466. $res = (!$this->encoding) ? $this->_save($fp, $dir, $name, $stat) : $this->convEncOut($this->_save($fp, $this->convEncIn($dir), $this->convEncIn($name), $this->convEncIn($stat)));
  3467. if ($res !== false) {
  3468. $this->clearstatcache();
  3469. }
  3470. return $res;
  3471. }
  3472. /**
  3473. * Return true if path is dir and has at least one childs directory (with convert encoding)
  3474. *
  3475. * @param string $path dir path
  3476. *
  3477. * @return bool
  3478. * @author Naoki Sawada
  3479. **/
  3480. protected function subdirsCE($path)
  3481. {
  3482. if ($this->sessionCaching['subdirs']) {
  3483. if (isset($this->sessionCache['subdirs'][$path]) && !$this->isMyReload()) {
  3484. return $this->sessionCache['subdirs'][$path];
  3485. }
  3486. }
  3487. $hasdir = (bool)((!$this->encoding) ? $this->_subdirs($path) : $this->convEncOut($this->_subdirs($this->convEncIn($path))));
  3488. $this->updateSubdirsCache($path, $hasdir);
  3489. return $hasdir;
  3490. }
  3491. /**
  3492. * Return files list in directory (with convert encoding)
  3493. *
  3494. * @param string $path dir path
  3495. *
  3496. * @return array
  3497. * @author Naoki Sawada
  3498. **/
  3499. protected function scandirCE($path)
  3500. {
  3501. return (!$this->encoding) ? $this->_scandir($path) : $this->convEncOut($this->_scandir($this->convEncIn($path)));
  3502. }
  3503. /**
  3504. * Create symlink (with convert encoding)
  3505. *
  3506. * @param string $source file to link to
  3507. * @param string $targetDir folder to create link in
  3508. * @param string $name symlink name
  3509. *
  3510. * @return bool
  3511. * @author Naoki Sawada
  3512. **/
  3513. protected function symlinkCE($source, $targetDir, $name)
  3514. {
  3515. return (!$this->encoding) ? $this->_symlink($source, $targetDir, $name) : $this->convEncOut($this->_symlink($this->convEncIn($source), $this->convEncIn($targetDir), $this->convEncIn($name)));
  3516. }
  3517. /***************** paths *******************/
  3518. /**
  3519. * Encode path into hash
  3520. *
  3521. * @param string file path
  3522. *
  3523. * @return string
  3524. * @author Dmitry (dio) Levashov
  3525. * @author Troex Nevelin
  3526. **/
  3527. protected function encode($path)
  3528. {
  3529. if ($path !== '') {
  3530. // cut ROOT from $path for security reason, even if hacker decodes the path he will not know the root
  3531. $p = $this->relpathCE($path);
  3532. // if reqesting root dir $path will be empty, then assign '/' as we cannot leave it blank for crypt
  3533. if ($p === '') {
  3534. $p = $this->separator;
  3535. }
  3536. // change separator
  3537. if ($this->separatorForHash) {
  3538. $p = str_replace($this->separator, $this->separatorForHash, $p);
  3539. }
  3540. // TODO crypt path and return hash
  3541. $hash = $this->crypt($p);
  3542. // hash is used as id in HTML that means it must contain vaild chars
  3543. // make base64 html safe and append prefix in begining
  3544. $hash = strtr(base64_encode($hash), '+/=', '-_.');
  3545. // remove dots '.' at the end, before it was '=' in base64
  3546. $hash = rtrim($hash, '.');
  3547. // append volume id to make hash unique
  3548. return $this->id . $hash;
  3549. }
  3550. //TODO: Add return statement here
  3551. }
  3552. /**
  3553. * Decode path from hash
  3554. *
  3555. * @param string file hash
  3556. *
  3557. * @return string
  3558. * @author Dmitry (dio) Levashov
  3559. * @author Troex Nevelin
  3560. **/
  3561. protected function decode($hash)
  3562. {
  3563. if (strpos($hash, $this->id) === 0) {
  3564. // cut volume id after it was prepended in encode
  3565. $h = substr($hash, strlen($this->id));
  3566. // replace HTML safe base64 to normal
  3567. $h = base64_decode(strtr($h, '-_.', '+/='));
  3568. // TODO uncrypt hash and return path
  3569. $path = $this->uncrypt($h);
  3570. // change separator
  3571. if ($this->separatorForHash) {
  3572. $path = str_replace($this->separatorForHash, $this->separator, $path);
  3573. }
  3574. // append ROOT to path after it was cut in encode
  3575. return $this->abspathCE($path);//$this->root.($path === $this->separator ? '' : $this->separator.$path);
  3576. }
  3577. return '';
  3578. }
  3579. /**
  3580. * Return crypted path
  3581. * Not implemented
  3582. *
  3583. * @param string path
  3584. *
  3585. * @return mixed
  3586. * @author Dmitry (dio) Levashov
  3587. **/
  3588. protected function crypt($path)
  3589. {
  3590. return $path;
  3591. }
  3592. /**
  3593. * Return uncrypted path
  3594. * Not implemented
  3595. *
  3596. * @param mixed hash
  3597. *
  3598. * @return mixed
  3599. * @author Dmitry (dio) Levashov
  3600. **/
  3601. protected function uncrypt($hash)
  3602. {
  3603. return $hash;
  3604. }
  3605. /**
  3606. * Validate file name based on $this->options['acceptedName'] regexp or function
  3607. *
  3608. * @param string $name file name
  3609. * @param bool $isDir
  3610. *
  3611. * @return bool
  3612. * @author Dmitry (dio) Levashov
  3613. */
  3614. protected function nameAccepted($name, $isDir = false)
  3615. {
  3616. if (json_encode($name) === false) {
  3617. return false;
  3618. }
  3619. $nameValidator = $isDir ? $this->dirnameValidator : $this->nameValidator;
  3620. if ($nameValidator) {
  3621. if (is_callable($nameValidator)) {
  3622. $res = call_user_func($nameValidator, $name);
  3623. return $res;
  3624. }
  3625. if (preg_match($nameValidator, '') !== false) {
  3626. return preg_match($nameValidator, $name);
  3627. }
  3628. }
  3629. return true;
  3630. }
  3631. /**
  3632. * Return session rootstat cache key
  3633. *
  3634. * @return string
  3635. */
  3636. protected function getRootstatCachekey()
  3637. {
  3638. return md5($this->root . (isset($this->options['alias']) ? $this->options['alias'] : ''));
  3639. }
  3640. /**
  3641. * Return new unique name based on file name and suffix
  3642. *
  3643. * @param $dir
  3644. * @param $name
  3645. * @param string $suffix suffix append to name
  3646. * @param bool $checkNum
  3647. * @param int $start
  3648. *
  3649. * @return string
  3650. * @internal param string $path file path
  3651. * @author Dmitry (dio) Levashov
  3652. */
  3653. public function uniqueName($dir, $name, $suffix = ' copy', $checkNum = true, $start = 1)
  3654. {
  3655. static $lasts = null;
  3656. if ($lasts === null) {
  3657. $lasts = array();
  3658. }
  3659. $ext = '';
  3660. $splits = elFinder::splitFileExtention($name);
  3661. if ($splits[1]) {
  3662. $ext = '.' . $splits[1];
  3663. $name = $splits[0];
  3664. }
  3665. if ($checkNum && preg_match('/(' . preg_quote($suffix, '/') . ')(\d*)$/i', $name, $m)) {
  3666. $i = (int)$m[2];
  3667. $name = substr($name, 0, strlen($name) - strlen($m[2]));
  3668. } else {
  3669. $i = $start;
  3670. $name .= $suffix;
  3671. }
  3672. $max = $i + 100000;
  3673. if (isset($lasts[$name])) {
  3674. $i = max($i, $lasts[$name]);
  3675. }
  3676. while ($i <= $max) {
  3677. $n = $name . ($i > 0 ? sprintf($this->options['uniqueNumFormat'], $i) : '') . $ext;
  3678. if (!$this->isNameExists($this->joinPathCE($dir, $n))) {
  3679. $this->clearcache();
  3680. $lasts[$name] = ++$i;
  3681. return $n;
  3682. }
  3683. $i++;
  3684. }
  3685. return $name . md5($dir) . $ext;
  3686. }
  3687. /**
  3688. * Converts character encoding from UTF-8 to server's one
  3689. *
  3690. * @param mixed $var target string or array var
  3691. * @param bool $restoreLocale do retore global locale, default is false
  3692. * @param string $unknown replaces character for unknown
  3693. *
  3694. * @return mixed
  3695. * @author Naoki Sawada
  3696. */
  3697. public function convEncIn($var = null, $restoreLocale = false, $unknown = '_')
  3698. {
  3699. return (!$this->encoding) ? $var : $this->convEnc($var, 'UTF-8', $this->encoding, $this->options['locale'], $restoreLocale, $unknown);
  3700. }
  3701. /**
  3702. * Converts character encoding from server's one to UTF-8
  3703. *
  3704. * @param mixed $var target string or array var
  3705. * @param bool $restoreLocale do retore global locale, default is true
  3706. * @param string $unknown replaces character for unknown
  3707. *
  3708. * @return mixed
  3709. * @author Naoki Sawada
  3710. */
  3711. public function convEncOut($var = null, $restoreLocale = true, $unknown = '_')
  3712. {
  3713. return (!$this->encoding) ? $var : $this->convEnc($var, $this->encoding, 'UTF-8', $this->options['locale'], $restoreLocale, $unknown);
  3714. }
  3715. /**
  3716. * Converts character encoding (base function)
  3717. *
  3718. * @param mixed $var target string or array var
  3719. * @param string $from from character encoding
  3720. * @param string $to to character encoding
  3721. * @param string $locale local locale
  3722. * @param $restoreLocale
  3723. * @param string $unknown replaces character for unknown
  3724. *
  3725. * @return mixed
  3726. */
  3727. protected function convEnc($var, $from, $to, $locale, $restoreLocale, $unknown = '_')
  3728. {
  3729. if (strtoupper($from) !== strtoupper($to)) {
  3730. if ($locale) {
  3731. setlocale(LC_ALL, $locale);
  3732. }
  3733. if (is_array($var)) {
  3734. $_ret = array();
  3735. foreach ($var as $_k => $_v) {
  3736. $_ret[$_k] = $this->convEnc($_v, $from, $to, '', false, $unknown = '_');
  3737. }
  3738. $var = $_ret;
  3739. } else {
  3740. $_var = false;
  3741. if (is_string($var)) {
  3742. $_var = $var;
  3743. $errlev = error_reporting();
  3744. error_reporting($errlev ^ E_NOTICE);
  3745. if (false !== ($_var = iconv($from, $to . '//TRANSLIT', $_var))) {
  3746. $_var = str_replace('?', $unknown, $_var);
  3747. }
  3748. error_reporting($errlev);
  3749. }
  3750. if ($_var !== false) {
  3751. $var = $_var;
  3752. }
  3753. }
  3754. if ($restoreLocale) {
  3755. setlocale(LC_ALL, elFinder::$locale);
  3756. }
  3757. }
  3758. return $var;
  3759. }
  3760. /**
  3761. * Normalize MIME-Type by options['mimeMap']
  3762. *
  3763. * @param string $type MIME-Type
  3764. * @param string $name Filename
  3765. * @param string $ext File extention without first dot (optional)
  3766. *
  3767. * @return string Normalized MIME-Type
  3768. */
  3769. public function mimeTypeNormalize($type, $name, $ext = '')
  3770. {
  3771. if ($ext === '') {
  3772. $ext = (false === $pos = strrpos($name, '.')) ? '' : substr($name, $pos + 1);
  3773. }
  3774. $_checkKey = strtolower($ext . ':' . $type);
  3775. if ($type === '') {
  3776. $_keylen = strlen($_checkKey);
  3777. foreach ($this->options['mimeMap'] as $_key => $_type) {
  3778. if (substr($_key, 0, $_keylen) === $_checkKey) {
  3779. $type = $_type;
  3780. break;
  3781. }
  3782. }
  3783. } else if (isset($this->options['mimeMap'][$_checkKey])) {
  3784. $type = $this->options['mimeMap'][$_checkKey];
  3785. } else {
  3786. $_checkKey = strtolower($ext . ':*');
  3787. if (isset($this->options['mimeMap'][$_checkKey])) {
  3788. $type = $this->options['mimeMap'][$_checkKey];
  3789. } else {
  3790. $_checkKey = strtolower('*:' . $type);
  3791. if (isset($this->options['mimeMap'][$_checkKey])) {
  3792. $type = $this->options['mimeMap'][$_checkKey];
  3793. }
  3794. }
  3795. }
  3796. return $type;
  3797. }
  3798. /*********************** util mainly for inheritance class *********************/
  3799. /**
  3800. * Get temporary filename. Tempfile will be removed when after script execution finishes or exit() is called.
  3801. * When needing the unique file to a path, give $path to parameter.
  3802. *
  3803. * @param string $path for get unique file to a path
  3804. *
  3805. * @return string|false
  3806. * @author Naoki Sawada
  3807. */
  3808. protected function getTempFile($path = '')
  3809. {
  3810. static $cache = array();
  3811. $key = '';
  3812. if ($path !== '') {
  3813. $key = $this->id . '#' . $path;
  3814. if (isset($cache[$key])) {
  3815. return $cache[$key];
  3816. }
  3817. }
  3818. if ($tmpdir = $this->getTempPath()) {
  3819. $name = tempnam($tmpdir, 'ELF');
  3820. if ($key) {
  3821. $cache[$key] = $name;
  3822. }
  3823. // register auto delete on shutdown
  3824. $GLOBALS['elFinderTempFiles'][$name] = true;
  3825. return $name;
  3826. }
  3827. return false;
  3828. }
  3829. /**
  3830. * File path of local server side work file path
  3831. *
  3832. * @param string $path path need convert encoding to server encoding
  3833. *
  3834. * @return string
  3835. * @author Naoki Sawada
  3836. */
  3837. protected function getWorkFile($path)
  3838. {
  3839. if ($wfp = $this->tmpfile()) {
  3840. if ($fp = $this->_fopen($path)) {
  3841. while (!feof($fp)) {
  3842. fwrite($wfp, fread($fp, 8192));
  3843. }
  3844. $info = stream_get_meta_data($wfp);
  3845. fclose($wfp);
  3846. if ($info && !empty($info['uri'])) {
  3847. return $info['uri'];
  3848. }
  3849. }
  3850. }
  3851. return false;
  3852. }
  3853. /**
  3854. * Get image size array with `dimensions`
  3855. *
  3856. * @param string $path path need convert encoding to server encoding
  3857. * @param string $mime file mime type
  3858. *
  3859. * @return array|false
  3860. * @throws ImagickException
  3861. * @throws elFinderAbortException
  3862. */
  3863. public function getImageSize($path, $mime = '')
  3864. {
  3865. $size = false;
  3866. if ($mime === '' || strtolower(substr($mime, 0, 5)) === 'image') {
  3867. if ($work = $this->getWorkFile($path)) {
  3868. if ($size = getimagesize($work)) {
  3869. $size['dimensions'] = $size[0] . 'x' . $size[1];
  3870. $srcfp = fopen($work, 'rb');
  3871. $cArgs = elFinder::$currentArgs;
  3872. if (!empty($cArgs['target']) && $subImgLink = $this->getSubstituteImgLink($cArgs['target'], $size, $srcfp)) {
  3873. $size['url'] = $subImgLink;
  3874. }
  3875. }
  3876. }
  3877. is_file($work) && unlink($work);
  3878. }
  3879. return $size;
  3880. }
  3881. /**
  3882. * Delete dirctory trees
  3883. *
  3884. * @param string $localpath path need convert encoding to server encoding
  3885. *
  3886. * @return boolean
  3887. * @throws elFinderAbortException
  3888. * @author Naoki Sawada
  3889. */
  3890. protected function delTree($localpath)
  3891. {
  3892. foreach ($this->_scandir($localpath) as $p) {
  3893. elFinder::checkAborted();
  3894. $stat = $this->stat($this->convEncOut($p));
  3895. $this->convEncIn();
  3896. ($stat['mime'] === 'directory') ? $this->delTree($p) : $this->_unlink($p);
  3897. }
  3898. $res = $this->_rmdir($localpath);
  3899. $res && $this->clearstatcache();
  3900. return $res;
  3901. }
  3902. /**
  3903. * Copy items to a new temporary directory on the local server
  3904. *
  3905. * @param array $hashes target hashes
  3906. * @param string $dir destination directory (for recurcive)
  3907. * @param string $canLink it can use link() (for recurcive)
  3908. *
  3909. * @return string|false saved path name
  3910. * @throws elFinderAbortException
  3911. * @author Naoki Sawada
  3912. */
  3913. protected function getItemsInHand($hashes, $dir = null, $canLink = null)
  3914. {
  3915. static $banChrs = null;
  3916. static $totalSize = 0;
  3917. if (is_null($banChrs)) {
  3918. $banChrs = DIRECTORY_SEPARATOR !== '/'? array('\\', '/', ':', '*', '?', '"', '<', '>', '|') : array('\\', '/');
  3919. }
  3920. if (is_null($dir)) {
  3921. $totalSize = 0;
  3922. if (!$tmpDir = $this->getTempPath()) {
  3923. return false;
  3924. }
  3925. $dir = tempnam($tmpDir, 'elf');
  3926. if (!unlink($dir) || !mkdir($dir, 0700, true)) {
  3927. return false;
  3928. }
  3929. register_shutdown_function(array($this, 'rmdirRecursive'), $dir);
  3930. }
  3931. if (is_null($canLink)) {
  3932. $canLink = ($this instanceof elFinderVolumeLocalFileSystem);
  3933. }
  3934. elFinder::checkAborted();
  3935. $res = true;
  3936. $files = array();
  3937. foreach ($hashes as $hash) {
  3938. if (($file = $this->file($hash)) == false) {
  3939. continue;
  3940. }
  3941. if (!$file['read']) {
  3942. continue;
  3943. }
  3944. $name = $file['name'];
  3945. // remove ctrl characters
  3946. $name = preg_replace('/[[:cntrl:]]+/', '', $name);
  3947. // replace ban characters
  3948. $name = str_replace($banChrs, '_', $name);
  3949. // for call from search results
  3950. if (isset($files[$name])) {
  3951. $name = preg_replace('/^(.*?)(\..*)?$/', '$1_' . $files[$name]++ . '$2', $name);
  3952. } else {
  3953. $files[$name] = 1;
  3954. }
  3955. $target = $dir . DIRECTORY_SEPARATOR . $name;
  3956. if ($file['mime'] === 'directory') {
  3957. $chashes = array();
  3958. $_files = $this->scandir($hash);
  3959. foreach ($_files as $_file) {
  3960. if ($file['read']) {
  3961. $chashes[] = $_file['hash'];
  3962. }
  3963. }
  3964. if (($res = mkdir($target, 0700, true)) && $chashes) {
  3965. $res = $this->getItemsInHand($chashes, $target, $canLink);
  3966. }
  3967. if (!$res) {
  3968. break;
  3969. }
  3970. !empty($file['ts']) && touch($target, $file['ts']);
  3971. } else {
  3972. $path = $this->decode($hash);
  3973. if (!$canLink || !($canLink = $this->localFileSystemSymlink($path, $target))) {
  3974. if (file_exists($target)) {
  3975. unlink($target);
  3976. }
  3977. if ($fp = $this->fopenCE($path)) {
  3978. if ($tfp = fopen($target, 'wb')) {
  3979. $totalSize += stream_copy_to_stream($fp, $tfp);
  3980. fclose($tfp);
  3981. }
  3982. !empty($file['ts']) && touch($target, $file['ts']);
  3983. $this->fcloseCE($fp, $path);
  3984. }
  3985. } else {
  3986. $totalSize += filesize($path);
  3987. }
  3988. if ($this->options['maxArcFilesSize'] > 0 && $this->options['maxArcFilesSize'] < $totalSize) {
  3989. $res = $this->setError(elFinder::ERROR_ARC_MAXSIZE);
  3990. }
  3991. }
  3992. }
  3993. return $res ? $dir : false;
  3994. }
  3995. /*********************** file stat *********************/
  3996. /**
  3997. * Check file attribute
  3998. *
  3999. * @param string $path file path
  4000. * @param string $name attribute name (read|write|locked|hidden)
  4001. * @param bool $val attribute value returned by file system
  4002. * @param bool $isDir path is directory (true: directory, false: file)
  4003. *
  4004. * @return bool
  4005. * @author Dmitry (dio) Levashov
  4006. **/
  4007. protected function attr($path, $name, $val = null, $isDir = null)
  4008. {
  4009. if (!isset($this->defaults[$name])) {
  4010. return false;
  4011. }
  4012. $relpath = $this->relpathCE($path);
  4013. if ($this->separator !== '/') {
  4014. $relpath = str_replace($this->separator, '/', $relpath);
  4015. }
  4016. $relpath = '/' . $relpath;
  4017. $perm = null;
  4018. if ($this->access) {
  4019. $perm = call_user_func($this->access, $name, $path, $this->options['accessControlData'], $this, $isDir, $relpath);
  4020. if ($perm !== null) {
  4021. return !!$perm;
  4022. }
  4023. }
  4024. foreach ($this->attributes as $attrs) {
  4025. if (isset($attrs[$name]) && isset($attrs['pattern']) && preg_match($attrs['pattern'], $relpath)) {
  4026. $perm = $attrs[$name];
  4027. break;
  4028. }
  4029. }
  4030. return $perm === null ? (is_null($val) ? $this->defaults[$name] : $val) : !!$perm;
  4031. }
  4032. /**
  4033. * Return true if file with given name can be created in given folder.
  4034. *
  4035. * @param string $dir parent dir path
  4036. * @param string $name new file name
  4037. * @param null $isDir
  4038. *
  4039. * @return bool
  4040. * @author Dmitry (dio) Levashov
  4041. */
  4042. protected function allowCreate($dir, $name, $isDir = null)
  4043. {
  4044. return $this->attr($this->joinPathCE($dir, $name), 'write', true, $isDir);
  4045. }
  4046. /**
  4047. * Return true if file MIME type can save with check uploadOrder config.
  4048. *
  4049. * @param string $mime
  4050. *
  4051. * @return boolean
  4052. */
  4053. protected function allowPutMime($mime)
  4054. {
  4055. // logic based on http://httpd.apache.org/docs/2.2/mod/mod_authz_host.html#order
  4056. $allow = $this->mimeAccepted($mime, $this->uploadAllow, null);
  4057. $deny = $this->mimeAccepted($mime, $this->uploadDeny, null);
  4058. if (strtolower($this->uploadOrder[0]) == 'allow') { // array('allow', 'deny'), default is to 'deny'
  4059. $res = false; // default is deny
  4060. if (!$deny && ($allow === true)) { // match only allow
  4061. $res = true;
  4062. }// else (both match | no match | match only deny) { deny }
  4063. } else { // array('deny', 'allow'), default is to 'allow' - this is the default rule
  4064. $res = true; // default is allow
  4065. if (($deny === true) && !$allow) { // match only deny
  4066. $res = false;
  4067. } // else (both match | no match | match only allow) { allow }
  4068. }
  4069. return $res;
  4070. }
  4071. /**
  4072. * Return fileinfo
  4073. *
  4074. * @param string $path file cache
  4075. *
  4076. * @return array|bool
  4077. * @author Dmitry (dio) Levashov
  4078. **/
  4079. protected function stat($path)
  4080. {
  4081. if ($path === false || is_null($path)) {
  4082. return false;
  4083. }
  4084. $is_root = ($path == $this->root);
  4085. if ($is_root) {
  4086. $rootKey = $this->getRootstatCachekey();
  4087. if ($this->sessionCaching['rootstat'] && !isset($this->sessionCache['rootstat'])) {
  4088. $this->sessionCache['rootstat'] = array();
  4089. }
  4090. if (!isset($this->cache[$path]) && !$this->isMyReload()) {
  4091. // need $path as key for netmount/netunmount
  4092. if ($this->sessionCaching['rootstat'] && isset($this->sessionCache['rootstat'][$rootKey])) {
  4093. if ($ret = $this->sessionCache['rootstat'][$rootKey]) {
  4094. if ($this->options['rootRev'] === $ret['rootRev']) {
  4095. if (isset($this->options['phash'])) {
  4096. $ret['isroot'] = 1;
  4097. $ret['phash'] = $this->options['phash'];
  4098. }
  4099. return $ret;
  4100. }
  4101. }
  4102. }
  4103. }
  4104. }
  4105. $rootSessCache = false;
  4106. if (isset($this->cache[$path])) {
  4107. $ret = $this->cache[$path];
  4108. } else {
  4109. if ($is_root && !empty($this->options['rapidRootStat']) && is_array($this->options['rapidRootStat']) && !$this->needOnline) {
  4110. $ret = $this->updateCache($path, $this->options['rapidRootStat'], true);
  4111. } else {
  4112. $ret = $this->updateCache($path, $this->convEncOut($this->_stat($this->convEncIn($path))), true);
  4113. if ($is_root && !empty($rootKey) && $this->sessionCaching['rootstat']) {
  4114. $rootSessCache = true;
  4115. }
  4116. }
  4117. }
  4118. if ($is_root) {
  4119. if ($ret) {
  4120. $this->rootModified = false;
  4121. if ($rootSessCache) {
  4122. $this->sessionCache['rootstat'][$rootKey] = $ret;
  4123. }
  4124. if (isset($this->options['phash'])) {
  4125. $ret['isroot'] = 1;
  4126. $ret['phash'] = $this->options['phash'];
  4127. }
  4128. } else if (!empty($rootKey) && $this->sessionCaching['rootstat']) {
  4129. unset($this->sessionCache['rootstat'][$rootKey]);
  4130. }
  4131. }
  4132. return $ret;
  4133. }
  4134. /**
  4135. * Get root stat extra key values
  4136. *
  4137. * @return array stat extras
  4138. * @author Naoki Sawada
  4139. */
  4140. protected function getRootStatExtra()
  4141. {
  4142. $stat = array();
  4143. if ($this->rootName) {
  4144. $stat['name'] = $this->rootName;
  4145. }
  4146. $stat['rootRev'] = $this->options['rootRev'];
  4147. $stat['options'] = $this->options(null);
  4148. return $stat;
  4149. }
  4150. /**
  4151. * Return fileinfo based on filename
  4152. * For item ID based path file system
  4153. * Please override if needed on each drivers
  4154. *
  4155. * @param string $path file cache
  4156. *
  4157. * @return array
  4158. */
  4159. protected function isNameExists($path)
  4160. {
  4161. return $this->stat($path);
  4162. }
  4163. /**
  4164. * Put file stat in cache and return it
  4165. *
  4166. * @param string $path file path
  4167. * @param array $stat file stat
  4168. *
  4169. * @return array
  4170. * @author Dmitry (dio) Levashov
  4171. **/
  4172. protected function updateCache($path, $stat)
  4173. {
  4174. if (empty($stat) || !is_array($stat)) {
  4175. return $this->cache[$path] = array();
  4176. }
  4177. if (func_num_args() > 2) {
  4178. $fromStat = func_get_arg(2);
  4179. } else {
  4180. $fromStat = false;
  4181. }
  4182. $stat['hash'] = $this->encode($path);
  4183. $root = $path == $this->root;
  4184. $parent = '';
  4185. if ($root) {
  4186. $stat = array_merge($stat, $this->getRootStatExtra());
  4187. } else {
  4188. if (!isset($stat['name']) || $stat['name'] === '') {
  4189. $stat['name'] = $this->basenameCE($path);
  4190. }
  4191. if (empty($stat['phash'])) {
  4192. $parent = $this->dirnameCE($path);
  4193. $stat['phash'] = $this->encode($parent);
  4194. } else {
  4195. $parent = $this->decode($stat['phash']);
  4196. }
  4197. }
  4198. // name check
  4199. if (isset($stat['name']) && !$jeName = json_encode($stat['name'])) {
  4200. return $this->cache[$path] = array();
  4201. }
  4202. // fix name if required
  4203. if ($this->options['utf8fix'] && $this->options['utf8patterns'] && $this->options['utf8replace']) {
  4204. $stat['name'] = json_decode(str_replace($this->options['utf8patterns'], $this->options['utf8replace'], $jeName));
  4205. }
  4206. if (!isset($stat['size'])) {
  4207. $stat['size'] = 'unknown';
  4208. }
  4209. $mime = isset($stat['mime']) ? $stat['mime'] : '';
  4210. if ($isDir = ($mime === 'directory')) {
  4211. $stat['volumeid'] = $this->id;
  4212. } else {
  4213. if (empty($stat['mime']) || $stat['size'] == 0) {
  4214. $stat['mime'] = $this->mimetype($stat['name'], true, $stat['size'], $mime);
  4215. } else {
  4216. $stat['mime'] = $this->mimeTypeNormalize($stat['mime'], $stat['name']);
  4217. }
  4218. }
  4219. $stat['read'] = intval($this->attr($path, 'read', isset($stat['read']) ? !!$stat['read'] : null, $isDir));
  4220. $stat['write'] = intval($this->attr($path, 'write', isset($stat['write']) ? !!$stat['write'] : null, $isDir));
  4221. if ($root) {
  4222. $stat['locked'] = 1;
  4223. if ($this->options['type'] !== '') {
  4224. $stat['type'] = $this->options['type'];
  4225. }
  4226. } else {
  4227. // lock when parent directory is not writable
  4228. if (!isset($stat['locked'])) {
  4229. $pstat = $this->stat($parent);
  4230. if (isset($pstat['write']) && !$pstat['write']) {
  4231. $stat['locked'] = true;
  4232. }
  4233. }
  4234. if ($this->attr($path, 'locked', isset($stat['locked']) ? !!$stat['locked'] : null, $isDir)) {
  4235. $stat['locked'] = 1;
  4236. } else {
  4237. unset($stat['locked']);
  4238. }
  4239. }
  4240. if ($root) {
  4241. unset($stat['hidden']);
  4242. } elseif ($this->attr($path, 'hidden', isset($stat['hidden']) ? !!$stat['hidden'] : null, $isDir)
  4243. || !$this->mimeAccepted($stat['mime'])) {
  4244. $stat['hidden'] = 1;
  4245. } else {
  4246. unset($stat['hidden']);
  4247. }
  4248. if ($stat['read'] && empty($stat['hidden'])) {
  4249. if ($isDir) {
  4250. // caching parent's subdirs
  4251. if ($parent) {
  4252. $this->updateSubdirsCache($parent, true);
  4253. }
  4254. // for dir - check for subdirs
  4255. if ($this->options['checkSubfolders']) {
  4256. if (!isset($stat['dirs']) && intval($this->options['checkSubfolders']) === -1) {
  4257. $stat['dirs'] = -1;
  4258. }
  4259. if (isset($stat['dirs'])) {
  4260. if ($stat['dirs']) {
  4261. if ($stat['dirs'] == -1) {
  4262. $stat['dirs'] = ($this->sessionCaching['subdirs'] && isset($this->sessionCache['subdirs'][$path])) ? (int)$this->sessionCache['subdirs'][$path] : -1;
  4263. } else {
  4264. $stat['dirs'] = 1;
  4265. }
  4266. } else {
  4267. unset($stat['dirs']);
  4268. }
  4269. } elseif (!empty($stat['alias']) && !empty($stat['target'])) {
  4270. $stat['dirs'] = isset($this->cache[$stat['target']])
  4271. ? intval(isset($this->cache[$stat['target']]['dirs']))
  4272. : $this->subdirsCE($stat['target']);
  4273. } elseif ($this->subdirsCE($path)) {
  4274. $stat['dirs'] = 1;
  4275. }
  4276. } else {
  4277. $stat['dirs'] = 1;
  4278. }
  4279. if ($this->options['dirUrlOwn'] === true) {
  4280. // Set `null` to use the client option `commandsOptions.info.nullUrlDirLinkSelf = true`
  4281. $stat['url'] = null;
  4282. } else if ($this->options['dirUrlOwn'] === 'hide') {
  4283. // to hide link in info dialog of the elFinder client
  4284. $stat['url'] = '';
  4285. }
  4286. } else {
  4287. // for files - check for thumbnails
  4288. $p = isset($stat['target']) ? $stat['target'] : $path;
  4289. if ($this->tmbURL && !isset($stat['tmb']) && $this->canCreateTmb($p, $stat)) {
  4290. $tmb = $this->gettmb($p, $stat);
  4291. $stat['tmb'] = $tmb ? $tmb : 1;
  4292. }
  4293. }
  4294. if (!isset($stat['url']) && $this->URL && $this->encoding) {
  4295. $_path = str_replace($this->separator, '/', substr($path, strlen($this->root) + 1));
  4296. $stat['url'] = rtrim($this->URL, '/') . '/' . str_replace('%2F', '/', rawurlencode((substr(PHP_OS, 0, 3) === 'WIN') ? $_path : $this->convEncIn($_path, true)));
  4297. }
  4298. } else {
  4299. if ($isDir) {
  4300. unset($stat['dirs']);
  4301. }
  4302. }
  4303. if (!empty($stat['alias']) && !empty($stat['target'])) {
  4304. $stat['thash'] = $this->encode($stat['target']);
  4305. //$this->cache[$stat['target']] = $stat;
  4306. unset($stat['target']);
  4307. }
  4308. $this->cache[$path] = $stat;
  4309. if (!$fromStat && $root && $this->sessionCaching['rootstat']) {
  4310. // to update session cache
  4311. $this->stat($path);
  4312. }
  4313. return $stat;
  4314. }
  4315. /**
  4316. * Get stat for folder content and put in cache
  4317. *
  4318. * @param string $path
  4319. *
  4320. * @return void
  4321. * @author Dmitry (dio) Levashov
  4322. **/
  4323. protected function cacheDir($path)
  4324. {
  4325. $this->dirsCache[$path] = array();
  4326. $hasDir = false;
  4327. foreach ($this->scandirCE($path) as $p) {
  4328. if (($stat = $this->stat($p)) && empty($stat['hidden'])) {
  4329. if (!$hasDir && $stat['mime'] === 'directory') {
  4330. $hasDir = true;
  4331. }
  4332. $this->dirsCache[$path][] = $p;
  4333. }
  4334. }
  4335. $this->updateSubdirsCache($path, $hasDir);
  4336. }
  4337. /**
  4338. * Clean cache
  4339. *
  4340. * @return void
  4341. * @author Dmitry (dio) Levashov
  4342. **/
  4343. protected function clearcache()
  4344. {
  4345. $this->cache = $this->dirsCache = array();
  4346. }
  4347. /**
  4348. * Return file mimetype
  4349. *
  4350. * @param string $path file path
  4351. * @param string|bool $name
  4352. * @param integer $size
  4353. * @param string $mime was notified from the volume driver
  4354. *
  4355. * @return string
  4356. * @author Dmitry (dio) Levashov
  4357. */
  4358. protected function mimetype($path, $name = '', $size = null, $mime = null)
  4359. {
  4360. $type = '';
  4361. $nameCheck = false;
  4362. if ($name === '') {
  4363. $name = $path;
  4364. } else if ($name === true) {
  4365. $name = $path;
  4366. $nameCheck = true;
  4367. }
  4368. if (!$this instanceof elFinderVolumeLocalFileSystem) {
  4369. $nameCheck = true;
  4370. }
  4371. $ext = (false === $pos = strrpos($name, '.')) ? '' : strtolower(substr($name, $pos + 1));
  4372. if (!$nameCheck && $size === null) {
  4373. $size = file_exists($path) ? filesize($path) : -1;
  4374. }
  4375. if (!$nameCheck && is_readable($path) && $size > 0) {
  4376. // detecting by contents
  4377. if ($this->mimeDetect === 'finfo') {
  4378. $type = finfo_file($this->finfo, $path);
  4379. } else if ($this->mimeDetect === 'mime_content_type') {
  4380. $type = mime_content_type($path);
  4381. }
  4382. if ($type) {
  4383. $type = explode(';', $type);
  4384. $type = trim($type[0]);
  4385. if ($ext && preg_match('~^application/(?:octet-stream|(?:x-)?zip|xml)$~', $type)) {
  4386. // load default MIME table file "mime.types"
  4387. if (!elFinderVolumeDriver::$mimetypesLoaded) {
  4388. elFinderVolumeDriver::loadMimeTypes();
  4389. }
  4390. if (isset(elFinderVolumeDriver::$mimetypes[$ext])) {
  4391. $type = elFinderVolumeDriver::$mimetypes[$ext];
  4392. }
  4393. } else if ($ext === 'js' && preg_match('~^text/~', $type)) {
  4394. $type = 'text/javascript';
  4395. }
  4396. }
  4397. }
  4398. if (!$type) {
  4399. // detecting by filename
  4400. $type = elFinderVolumeDriver::mimetypeInternalDetect($name);
  4401. if ($type === 'unknown') {
  4402. if ($mime) {
  4403. $type = $mime;
  4404. } else {
  4405. $type = ($size == 0) ? 'text/plain' : $this->options['mimeTypeUnknown'];
  4406. }
  4407. }
  4408. }
  4409. // mime type normalization
  4410. $type = $this->mimeTypeNormalize($type, $name, $ext);
  4411. return $type;
  4412. }
  4413. /**
  4414. * Load file of mime.types
  4415. *
  4416. * @param string $mimeTypesFile The mime types file
  4417. */
  4418. static protected function loadMimeTypes($mimeTypesFile = '')
  4419. {
  4420. if (!elFinderVolumeDriver::$mimetypesLoaded) {
  4421. elFinderVolumeDriver::$mimetypesLoaded = true;
  4422. $file = false;
  4423. if (!empty($mimeTypesFile) && file_exists($mimeTypesFile)) {
  4424. $file = $mimeTypesFile;
  4425. } elseif (elFinder::$defaultMimefile && file_exists(elFinder::$defaultMimefile)) {
  4426. $file = elFinder::$defaultMimefile;
  4427. } elseif (file_exists(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mime.types')) {
  4428. $file = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mime.types';
  4429. } elseif (file_exists(dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'mime.types')) {
  4430. $file = dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'mime.types';
  4431. }
  4432. if ($file && file_exists($file)) {
  4433. $mimecf = file($file);
  4434. foreach ($mimecf as $line_num => $line) {
  4435. if (!preg_match('/^\s*#/', $line)) {
  4436. $mime = preg_split('/\s+/', $line, -1, PREG_SPLIT_NO_EMPTY);
  4437. for ($i = 1, $size = count($mime); $i < $size; $i++) {
  4438. if (!isset(self::$mimetypes[$mime[$i]])) {
  4439. self::$mimetypes[$mime[$i]] = $mime[0];
  4440. }
  4441. }
  4442. }
  4443. }
  4444. }
  4445. }
  4446. }
  4447. /**
  4448. * Detect file mimetype using "internal" method or Loading mime.types with $path = ''
  4449. *
  4450. * @param string $path file path
  4451. *
  4452. * @return string
  4453. * @author Dmitry (dio) Levashov
  4454. **/
  4455. static protected function mimetypeInternalDetect($path = '')
  4456. {
  4457. // load default MIME table file "mime.types"
  4458. if (!elFinderVolumeDriver::$mimetypesLoaded) {
  4459. elFinderVolumeDriver::loadMimeTypes();
  4460. }
  4461. $ext = '';
  4462. if ($path) {
  4463. $pinfo = pathinfo($path);
  4464. $ext = isset($pinfo['extension']) ? strtolower($pinfo['extension']) : '';
  4465. }
  4466. return ($ext && isset(elFinderVolumeDriver::$mimetypes[$ext])) ? elFinderVolumeDriver::$mimetypes[$ext] : 'unknown';
  4467. }
  4468. /**
  4469. * Return file/total directory size infomation
  4470. *
  4471. * @param string $path file path
  4472. *
  4473. * @return array
  4474. * @throws elFinderAbortException
  4475. * @author Dmitry (dio) Levashov
  4476. */
  4477. protected function countSize($path)
  4478. {
  4479. elFinder::checkAborted();
  4480. $result = array('size' => 0, 'files' => 0, 'dirs' => 0);
  4481. $stat = $this->stat($path);
  4482. if (empty($stat) || !$stat['read'] || !empty($stat['hidden'])) {
  4483. $result['size'] = 'unknown';
  4484. return $result;
  4485. }
  4486. if ($stat['mime'] !== 'directory') {
  4487. $result['size'] = intval($stat['size']);
  4488. $result['files'] = 1;
  4489. return $result;
  4490. }
  4491. $result['dirs'] = 1;
  4492. $subdirs = $this->options['checkSubfolders'];
  4493. $this->options['checkSubfolders'] = true;
  4494. foreach ($this->getScandir($path) as $stat) {
  4495. if ($isDir = ($stat['mime'] === 'directory' && $stat['read'])) {
  4496. ++$result['dirs'];
  4497. } else {
  4498. ++$result['files'];
  4499. }
  4500. $res = $isDir
  4501. ? $this->countSize($this->decode($stat['hash']))
  4502. : (isset($stat['size']) ? array('size' => intval($stat['size'])) : array());
  4503. if (!empty($res['size']) && is_numeric($res['size'])) {
  4504. $result['size'] += $res['size'];
  4505. }
  4506. if (!empty($res['files']) && is_numeric($res['files'])) {
  4507. $result['files'] += $res['files'];
  4508. }
  4509. if (!empty($res['dirs']) && is_numeric($res['dirs'])) {
  4510. $result['dirs'] += $res['dirs'];
  4511. --$result['dirs'];
  4512. }
  4513. }
  4514. $this->options['checkSubfolders'] = $subdirs;
  4515. return $result;
  4516. }
  4517. /**
  4518. * Return true if all mimes is directory or files
  4519. *
  4520. * @param string $mime1 mimetype
  4521. * @param string $mime2 mimetype
  4522. *
  4523. * @return bool
  4524. * @author Dmitry (dio) Levashov
  4525. **/
  4526. protected function isSameType($mime1, $mime2)
  4527. {
  4528. return ($mime1 == 'directory' && $mime1 == $mime2) || ($mime1 != 'directory' && $mime2 != 'directory');
  4529. }
  4530. /**
  4531. * If file has required attr == $val - return file path,
  4532. * If dir has child with has required attr == $val - return child path
  4533. *
  4534. * @param string $path file path
  4535. * @param string $attr attribute name
  4536. * @param bool $val attribute value
  4537. *
  4538. * @return string|false
  4539. * @author Dmitry (dio) Levashov
  4540. **/
  4541. protected function closestByAttr($path, $attr, $val)
  4542. {
  4543. $stat = $this->stat($path);
  4544. if (empty($stat)) {
  4545. return false;
  4546. }
  4547. $v = isset($stat[$attr]) ? $stat[$attr] : false;
  4548. if ($v == $val) {
  4549. return $path;
  4550. }
  4551. return $stat['mime'] == 'directory'
  4552. ? $this->childsByAttr($path, $attr, $val)
  4553. : false;
  4554. }
  4555. /**
  4556. * Return first found children with required attr == $val
  4557. *
  4558. * @param string $path file path
  4559. * @param string $attr attribute name
  4560. * @param bool $val attribute value
  4561. *
  4562. * @return string|false
  4563. * @author Dmitry (dio) Levashov
  4564. **/
  4565. protected function childsByAttr($path, $attr, $val)
  4566. {
  4567. foreach ($this->scandirCE($path) as $p) {
  4568. if (($_p = $this->closestByAttr($p, $attr, $val)) != false) {
  4569. return $_p;
  4570. }
  4571. }
  4572. return false;
  4573. }
  4574. protected function isMyReload($target = '', $ARGtarget = '')
  4575. {
  4576. if ($this->rootModified || (!empty($this->ARGS['cmd']) && $this->ARGS['cmd'] === 'parents')) {
  4577. return true;
  4578. }
  4579. if (!empty($this->ARGS['reload'])) {
  4580. if ($ARGtarget === '') {
  4581. $ARGtarget = isset($this->ARGS['target']) ? $this->ARGS['target']
  4582. : ((isset($this->ARGS['targets']) && is_array($this->ARGS['targets']) && count($this->ARGS['targets']) === 1) ?
  4583. $this->ARGS['targets'][0] : '');
  4584. }
  4585. if ($ARGtarget !== '') {
  4586. $ARGtarget = strval($ARGtarget);
  4587. if ($target === '') {
  4588. return (strpos($ARGtarget, $this->id) === 0);
  4589. } else {
  4590. $target = strval($target);
  4591. return ($target === $ARGtarget);
  4592. }
  4593. }
  4594. }
  4595. return false;
  4596. }
  4597. /**
  4598. * Update subdirs cache data
  4599. *
  4600. * @param string $path
  4601. * @param bool $subdirs
  4602. *
  4603. * @return void
  4604. */
  4605. protected function updateSubdirsCache($path, $subdirs)
  4606. {
  4607. if (isset($this->cache[$path])) {
  4608. if ($subdirs) {
  4609. $this->cache[$path]['dirs'] = 1;
  4610. } else {
  4611. unset($this->cache[$path]['dirs']);
  4612. }
  4613. }
  4614. if ($this->sessionCaching['subdirs']) {
  4615. $this->sessionCache['subdirs'][$path] = $subdirs;
  4616. }
  4617. if ($this->sessionCaching['rootstat'] && $path == $this->root) {
  4618. unset($this->sessionCache['rootstat'][$this->getRootstatCachekey()]);
  4619. }
  4620. }
  4621. /***************** get content *******************/
  4622. /**
  4623. * Return required dir's files info.
  4624. * If onlyMimes is set - return only dirs and files of required mimes
  4625. *
  4626. * @param string $path dir path
  4627. *
  4628. * @return array
  4629. * @author Dmitry (dio) Levashov
  4630. **/
  4631. protected function getScandir($path)
  4632. {
  4633. $files = array();
  4634. !isset($this->dirsCache[$path]) && $this->cacheDir($path);
  4635. foreach ($this->dirsCache[$path] as $p) {
  4636. if (($stat = $this->stat($p)) && empty($stat['hidden'])) {
  4637. $files[] = $stat;
  4638. }
  4639. }
  4640. return $files;
  4641. }
  4642. /**
  4643. * Return subdirs tree
  4644. *
  4645. * @param string $path parent dir path
  4646. * @param int $deep tree deep
  4647. * @param string $exclude
  4648. *
  4649. * @return array
  4650. * @author Dmitry (dio) Levashov
  4651. */
  4652. protected function gettree($path, $deep, $exclude = '')
  4653. {
  4654. $dirs = array();
  4655. !isset($this->dirsCache[$path]) && $this->cacheDir($path);
  4656. foreach ($this->dirsCache[$path] as $p) {
  4657. $stat = $this->stat($p);
  4658. if ($stat && empty($stat['hidden']) && $p != $exclude && $stat['mime'] == 'directory') {
  4659. $dirs[] = $stat;
  4660. if ($deep > 0 && !empty($stat['dirs'])) {
  4661. $dirs = array_merge($dirs, $this->gettree($p, $deep - 1));
  4662. }
  4663. }
  4664. }
  4665. return $dirs;
  4666. }
  4667. /**
  4668. * Recursive files search
  4669. *
  4670. * @param string $path dir path
  4671. * @param string $q search string
  4672. * @param array $mimes
  4673. *
  4674. * @return array
  4675. * @throws elFinderAbortException
  4676. * @author Dmitry (dio) Levashov
  4677. */
  4678. protected function doSearch($path, $q, $mimes)
  4679. {
  4680. $result = array();
  4681. $matchMethod = empty($this->doSearchCurrentQuery['matchMethod']) ? 'searchMatchName' : $this->doSearchCurrentQuery['matchMethod'];
  4682. $timeout = $this->options['searchTimeout'] ? $this->searchStart + $this->options['searchTimeout'] : 0;
  4683. if ($timeout && $timeout < time()) {
  4684. $this->setError(elFinder::ERROR_SEARCH_TIMEOUT, $this->path($this->encode($path)));
  4685. return $result;
  4686. }
  4687. foreach ($this->scandirCE($path) as $p) {
  4688. elFinder::extendTimeLimit($this->options['searchTimeout'] + 30);
  4689. if ($timeout && ($this->error || $timeout < time())) {
  4690. !$this->error && $this->setError(elFinder::ERROR_SEARCH_TIMEOUT, $this->path($this->encode($path)));
  4691. break;
  4692. }
  4693. $stat = $this->stat($p);
  4694. if (!$stat) { // invalid links
  4695. continue;
  4696. }
  4697. if (!empty($stat['hidden']) || !$this->mimeAccepted($stat['mime'], $mimes)) {
  4698. continue;
  4699. }
  4700. $name = $stat['name'];
  4701. if ($this->doSearchCurrentQuery['excludes']) {
  4702. foreach ($this->doSearchCurrentQuery['excludes'] as $exclude) {
  4703. if ($this->stripos($name, $exclude) !== false) {
  4704. continue 2;
  4705. }
  4706. }
  4707. }
  4708. if ((!$mimes || $stat['mime'] !== 'directory') && $this->$matchMethod($name, $q, $p) !== false) {
  4709. $stat['path'] = $this->path($stat['hash']);
  4710. if ($this->URL && !isset($stat['url'])) {
  4711. $path = str_replace($this->separator, '/', substr($p, strlen($this->root) + 1));
  4712. if ($this->encoding) {
  4713. $path = str_replace('%2F', '/', rawurlencode($this->convEncIn($path, true)));
  4714. } else {
  4715. $path = str_replace('%2F', '/', rawurlencode($path));
  4716. }
  4717. $stat['url'] = $this->URL . $path;
  4718. }
  4719. $result[] = $stat;
  4720. }
  4721. if ($stat['mime'] == 'directory' && $stat['read'] && !isset($stat['alias'])) {
  4722. if (!$this->options['searchExDirReg'] || !preg_match($this->options['searchExDirReg'], $p)) {
  4723. $result = array_merge($result, $this->doSearch($p, $q, $mimes));
  4724. }
  4725. }
  4726. }
  4727. return $result;
  4728. }
  4729. /********************** manuipulations ******************/
  4730. /**
  4731. * Copy file/recursive copy dir only in current volume.
  4732. * Return new file path or false.
  4733. *
  4734. * @param string $src source path
  4735. * @param string $dst destination dir path
  4736. * @param string $name new file name (optionaly)
  4737. *
  4738. * @return string|false
  4739. * @throws elFinderAbortException
  4740. * @author Dmitry (dio) Levashov
  4741. */
  4742. protected function copy($src, $dst, $name)
  4743. {
  4744. elFinder::checkAborted();
  4745. $srcStat = $this->stat($src);
  4746. if (!empty($srcStat['thash'])) {
  4747. $target = $this->decode($srcStat['thash']);
  4748. if (!$this->inpathCE($target, $this->root)) {
  4749. return $this->setError(elFinder::ERROR_COPY, $this->path($srcStat['hash']), elFinder::ERROR_MKOUTLINK);
  4750. }
  4751. $stat = $this->stat($target);
  4752. $this->clearcache();
  4753. return $stat && $this->symlinkCE($target, $dst, $name)
  4754. ? $this->joinPathCE($dst, $name)
  4755. : $this->setError(elFinder::ERROR_COPY, $this->path($srcStat['hash']));
  4756. }
  4757. if ($srcStat['mime'] === 'directory') {
  4758. $testStat = $this->isNameExists($this->joinPathCE($dst, $name));
  4759. $this->clearcache();
  4760. if (($testStat && $testStat['mime'] !== 'directory') || (!$testStat && !$testStat = $this->mkdir($this->encode($dst), $name))) {
  4761. return $this->setError(elFinder::ERROR_COPY, $this->path($srcStat['hash']));
  4762. }
  4763. $dst = $this->decode($testStat['hash']);
  4764. // start time
  4765. $stime = microtime(true);
  4766. foreach ($this->getScandir($src) as $stat) {
  4767. if (empty($stat['hidden'])) {
  4768. // current time
  4769. $ctime = microtime(true);
  4770. if (($ctime - $stime) > 2) {
  4771. $stime = $ctime;
  4772. elFinder::checkAborted();
  4773. }
  4774. $name = $stat['name'];
  4775. $_src = $this->decode($stat['hash']);
  4776. if (!$this->copy($_src, $dst, $name)) {
  4777. $this->remove($dst, true); // fall back
  4778. return $this->setError($this->error, elFinder::ERROR_COPY, $this->_path($src));
  4779. }
  4780. }
  4781. }
  4782. $this->added[] = $testStat;
  4783. return $dst;
  4784. }
  4785. if ($this->options['copyJoin']) {
  4786. $test = $this->joinPathCE($dst, $name);
  4787. if ($this->isNameExists($test)) {
  4788. $this->remove($test);
  4789. }
  4790. }
  4791. if ($res = $this->convEncOut($this->_copy($this->convEncIn($src), $this->convEncIn($dst), $this->convEncIn($name)))) {
  4792. $path = is_string($res) ? $res : $this->joinPathCE($dst, $name);
  4793. $this->clearstatcache();
  4794. $this->added[] = $this->stat($path);
  4795. return $path;
  4796. }
  4797. return $this->setError(elFinder::ERROR_COPY, $this->path($srcStat['hash']));
  4798. }
  4799. /**
  4800. * Move file
  4801. * Return new file path or false.
  4802. *
  4803. * @param string $src source path
  4804. * @param string $dst destination dir path
  4805. * @param string $name new file name
  4806. *
  4807. * @return string|false
  4808. * @throws elFinderAbortException
  4809. * @author Dmitry (dio) Levashov
  4810. */
  4811. protected function move($src, $dst, $name)
  4812. {
  4813. $stat = $this->stat($src);
  4814. $stat['realpath'] = $src;
  4815. $this->rmTmb($stat); // can not do rmTmb() after _move()
  4816. $this->clearcache();
  4817. if ($res = $this->convEncOut($this->_move($this->convEncIn($src), $this->convEncIn($dst), $this->convEncIn($name)))) {
  4818. $this->clearstatcache();
  4819. if ($stat['mime'] === 'directory') {
  4820. $this->updateSubdirsCache($dst, true);
  4821. }
  4822. $path = is_string($res) ? $res : $this->joinPathCE($dst, $name);
  4823. $this->added[] = $this->stat($path);
  4824. $this->removed[] = $stat;
  4825. return $path;
  4826. }
  4827. return $this->setError(elFinder::ERROR_MOVE, $this->path($stat['hash']));
  4828. }
  4829. /**
  4830. * Copy file from another volume.
  4831. * Return new file path or false.
  4832. *
  4833. * @param Object $volume source volume
  4834. * @param string $src source file hash
  4835. * @param string $destination destination dir path
  4836. * @param string $name file name
  4837. *
  4838. * @return string|false
  4839. * @throws elFinderAbortException
  4840. * @author Dmitry (dio) Levashov
  4841. */
  4842. protected function copyFrom($volume, $src, $destination, $name)
  4843. {
  4844. elFinder::checkAborted();
  4845. if (($source = $volume->file($src)) == false) {
  4846. return $this->addError(elFinder::ERROR_COPY, '#' . $src, $volume->error());
  4847. }
  4848. $srcIsDir = ($source['mime'] === 'directory');
  4849. $errpath = $volume->path($source['hash']);
  4850. $errors = array();
  4851. try {
  4852. $thash = $this->encode($destination);
  4853. elFinder::$instance->trigger('paste.copyfrom', array(&$thash, &$name, '', elFinder::$instance, $this), $errors);
  4854. } catch (elFinderTriggerException $e) {
  4855. return $this->addError(elFinder::ERROR_COPY, $name, $errors);
  4856. }
  4857. if (!$this->nameAccepted($name, $srcIsDir)) {
  4858. return $this->addError(elFinder::ERROR_COPY, $name, $srcIsDir ? elFinder::ERROR_INVALID_DIRNAME : elFinder::ERROR_INVALID_NAME);
  4859. }
  4860. if (!$this->allowCreate($destination, $name, $srcIsDir)) {
  4861. return $this->addError(elFinder::ERROR_COPY, $name, elFinder::ERROR_PERM_DENIED);
  4862. }
  4863. if (!$source['read']) {
  4864. return $this->addError(elFinder::ERROR_COPY, $errpath, elFinder::ERROR_PERM_DENIED);
  4865. }
  4866. if ($srcIsDir) {
  4867. $test = $this->isNameExists($this->joinPathCE($destination, $name));
  4868. $this->clearcache();
  4869. if (($test && $test['mime'] != 'directory') || (!$test && !$test = $this->mkdir($this->encode($destination), $name))) {
  4870. return $this->addError(elFinder::ERROR_COPY, $errpath);
  4871. }
  4872. //$path = $this->joinPathCE($destination, $name);
  4873. $path = $this->decode($test['hash']);
  4874. foreach ($volume->scandir($src) as $entr) {
  4875. $this->copyFrom($volume, $entr['hash'], $path, $entr['name']);
  4876. }
  4877. $this->added[] = $test;
  4878. } else {
  4879. // size check
  4880. if (!isset($source['size']) || $source['size'] > $this->uploadMaxSize) {
  4881. return $this->setError(elFinder::ERROR_UPLOAD_FILE_SIZE);
  4882. }
  4883. // MIME check
  4884. $mimeByName = $this->mimetype($source['name'], true);
  4885. if ($source['mime'] === $mimeByName) {
  4886. $mimeByName = '';
  4887. }
  4888. if (!$this->allowPutMime($source['mime']) || ($mimeByName && !$this->allowPutMime($mimeByName))) {
  4889. return $this->addError(elFinder::ERROR_UPLOAD_FILE_MIME, $errpath);
  4890. }
  4891. if (strpos($source['mime'], 'image') === 0 && ($dim = $volume->dimensions($src))) {
  4892. if (is_array($dim)) {
  4893. $dim = isset($dim['dim']) ? $dim['dim'] : null;
  4894. }
  4895. if ($dim) {
  4896. $s = explode('x', $dim);
  4897. $source['width'] = $s[0];
  4898. $source['height'] = $s[1];
  4899. }
  4900. }
  4901. if (($fp = $volume->open($src)) == false
  4902. || ($path = $this->saveCE($fp, $destination, $name, $source)) == false) {
  4903. $fp && $volume->close($fp, $src);
  4904. return $this->addError(elFinder::ERROR_COPY, $errpath);
  4905. }
  4906. $volume->close($fp, $src);
  4907. $this->added[] = $this->stat($path);;
  4908. }
  4909. return $path;
  4910. }
  4911. /**
  4912. * Remove file/ recursive remove dir
  4913. *
  4914. * @param string $path file path
  4915. * @param bool $force try to remove even if file locked
  4916. *
  4917. * @return bool
  4918. * @throws elFinderAbortException
  4919. * @author Dmitry (dio) Levashov
  4920. */
  4921. protected function remove($path, $force = false)
  4922. {
  4923. $stat = $this->stat($path);
  4924. if (empty($stat)) {
  4925. return $this->setError(elFinder::ERROR_RM, $path, elFinder::ERROR_FILE_NOT_FOUND);
  4926. }
  4927. $stat['realpath'] = $path;
  4928. $this->rmTmb($stat);
  4929. $this->clearcache();
  4930. if (!$force && !empty($stat['locked'])) {
  4931. return $this->setError(elFinder::ERROR_LOCKED, $this->path($stat['hash']));
  4932. }
  4933. if ($stat['mime'] == 'directory' && empty($stat['thash'])) {
  4934. $ret = $this->delTree($this->convEncIn($path));
  4935. $this->convEncOut();
  4936. if (!$ret) {
  4937. return $this->setError(elFinder::ERROR_RM, $this->path($stat['hash']));
  4938. }
  4939. } else {
  4940. if ($this->convEncOut(!$this->_unlink($this->convEncIn($path)))) {
  4941. return $this->setError(elFinder::ERROR_RM, $this->path($stat['hash']));
  4942. }
  4943. $this->clearstatcache();
  4944. }
  4945. $this->removed[] = $stat;
  4946. return true;
  4947. }
  4948. /************************* thumbnails **************************/
  4949. /**
  4950. * Return thumbnail file name for required file
  4951. *
  4952. * @param array $stat file stat
  4953. *
  4954. * @return string
  4955. * @author Dmitry (dio) Levashov
  4956. **/
  4957. protected function tmbname($stat)
  4958. {
  4959. $name = $stat['hash'] . (isset($stat['ts']) ? $stat['ts'] : '') . '.png';
  4960. if (strlen($name) > 255) {
  4961. $name = $this->id . md5($stat['hash']) . $stat['ts'] . '.png';
  4962. }
  4963. return $name;
  4964. }
  4965. /**
  4966. * Return thumnbnail name if exists
  4967. *
  4968. * @param string $path file path
  4969. * @param array $stat file stat
  4970. *
  4971. * @return string|false
  4972. * @author Dmitry (dio) Levashov
  4973. **/
  4974. protected function gettmb($path, $stat)
  4975. {
  4976. if ($this->tmbURL && $this->tmbPath) {
  4977. // file itself thumnbnail
  4978. if (strpos($path, $this->tmbPath) === 0) {
  4979. return basename($path);
  4980. }
  4981. $name = $this->tmbname($stat);
  4982. $tmb = $this->tmbPath . DIRECTORY_SEPARATOR . $name;
  4983. if (file_exists($tmb)) {
  4984. if ($this->options['tmbGcMaxlifeHour'] && $this->options['tmbGcPercentage'] > 0) {
  4985. touch($tmb);
  4986. }
  4987. return $name;
  4988. }
  4989. }
  4990. return false;
  4991. }
  4992. /**
  4993. * Return true if thumnbnail for required file can be created
  4994. *
  4995. * @param string $path thumnbnail path
  4996. * @param array $stat file stat
  4997. * @param bool $checkTmbPath
  4998. *
  4999. * @return string|bool
  5000. * @author Dmitry (dio) Levashov
  5001. **/
  5002. protected function canCreateTmb($path, $stat, $checkTmbPath = true)
  5003. {
  5004. static $gdMimes = null;
  5005. static $imgmgPS = null;
  5006. if ($gdMimes === null) {
  5007. $gdMimes = array_flip(array('image/jpeg', 'image/png', 'image/gif', 'image/x-ms-bmp'));
  5008. $imgmgPS = array_flip(array('application/postscript', 'application/pdf'));
  5009. }
  5010. if ((!$checkTmbPath || $this->tmbPathWritable)
  5011. && (!$this->tmbPath || strpos($path, $this->tmbPath) === false) // do not create thumnbnail for thumnbnail
  5012. ) {
  5013. $mime = strtolower($stat['mime']);
  5014. list($type) = explode('/', $mime);
  5015. if (!empty($this->imgConverter)) {
  5016. if (isset($this->imgConverter[$mime])) {
  5017. return true;
  5018. }
  5019. if (isset($this->imgConverter[$type])) {
  5020. return true;
  5021. }
  5022. }
  5023. return $this->imgLib
  5024. && (
  5025. ($type === 'image' && ($this->imgLib === 'gd' ? isset($gdMimes[$stat['mime']]) : true))
  5026. ||
  5027. (ELFINDER_IMAGEMAGICK_PS && isset($imgmgPS[$stat['mime']]) && $this->imgLib !== 'gd')
  5028. );
  5029. }
  5030. return false;
  5031. }
  5032. /**
  5033. * Return true if required file can be resized.
  5034. * By default - the same as canCreateTmb
  5035. *
  5036. * @param string $path thumnbnail path
  5037. * @param array $stat file stat
  5038. *
  5039. * @return string|bool
  5040. * @author Dmitry (dio) Levashov
  5041. **/
  5042. protected function canResize($path, $stat)
  5043. {
  5044. return $this->canCreateTmb($path, $stat, false);
  5045. }
  5046. /**
  5047. * Create thumnbnail and return it's URL on success
  5048. *
  5049. * @param string $path file path
  5050. * @param $stat
  5051. *
  5052. * @return false|string
  5053. * @internal param string $mime file mime type
  5054. * @throws elFinderAbortException
  5055. * @throws ImagickException
  5056. * @author Dmitry (dio) Levashov
  5057. */
  5058. protected function createTmb($path, $stat)
  5059. {
  5060. if (!$stat || !$this->canCreateTmb($path, $stat)) {
  5061. return false;
  5062. }
  5063. $name = $this->tmbname($stat);
  5064. $tmb = $this->tmbPath . DIRECTORY_SEPARATOR . $name;
  5065. $maxlength = -1;
  5066. $imgConverter = null;
  5067. // check imgConverter
  5068. $mime = strtolower($stat['mime']);
  5069. list($type) = explode('/', $mime);
  5070. if (isset($this->imgConverter[$mime])) {
  5071. $imgConverter = $this->imgConverter[$mime]['func'];
  5072. if (!empty($this->imgConverter[$mime]['maxlen'])) {
  5073. $maxlength = intval($this->imgConverter[$mime]['maxlen']);
  5074. }
  5075. } else if (isset($this->imgConverter[$type])) {
  5076. $imgConverter = $this->imgConverter[$type]['func'];
  5077. if (!empty($this->imgConverter[$type]['maxlen'])) {
  5078. $maxlength = intval($this->imgConverter[$type]['maxlen']);
  5079. }
  5080. }
  5081. if ($imgConverter && !is_callable($imgConverter)) {
  5082. return false;
  5083. }
  5084. // copy image into tmbPath so some drivers does not store files on local fs
  5085. if (($src = $this->fopenCE($path, 'rb')) == false) {
  5086. return false;
  5087. }
  5088. if (($trg = fopen($tmb, 'wb')) == false) {
  5089. $this->fcloseCE($src, $path);
  5090. return false;
  5091. }
  5092. stream_copy_to_stream($src, $trg, $maxlength);
  5093. $this->fcloseCE($src, $path);
  5094. fclose($trg);
  5095. // call imgConverter
  5096. if ($imgConverter) {
  5097. if (!call_user_func_array($imgConverter, array($tmb, $stat, $this))) {
  5098. file_exists($tmb) && unlink($tmb);
  5099. return false;
  5100. }
  5101. }
  5102. $result = false;
  5103. $tmbSize = $this->tmbSize;
  5104. if ($this->imgLib === 'imagick') {
  5105. try {
  5106. $imagickTest = new imagick($tmb . '[0]');
  5107. $imagickTest->clear();
  5108. $imagickTest = true;
  5109. } catch (Exception $e) {
  5110. $imagickTest = false;
  5111. }
  5112. }
  5113. if (($this->imgLib === 'imagick' && !$imagickTest) || ($s = getimagesize($tmb)) === false) {
  5114. if ($this->imgLib === 'imagick') {
  5115. $bgcolor = $this->options['tmbBgColor'];
  5116. if ($bgcolor === 'transparent') {
  5117. $bgcolor = 'rgba(255, 255, 255, 0.0)';
  5118. }
  5119. try {
  5120. $imagick = new imagick();
  5121. $imagick->setBackgroundColor(new ImagickPixel($bgcolor));
  5122. $imagick->readImage($this->getExtentionByMime($stat['mime'], ':') . $tmb . '[0]');
  5123. try {
  5124. $imagick->trimImage(0);
  5125. } catch (Exception $e) {
  5126. }
  5127. $imagick->setImageFormat('png');
  5128. $imagick->writeImage($tmb);
  5129. $imagick->clear();
  5130. if (($s = getimagesize($tmb)) !== false) {
  5131. $result = true;
  5132. }
  5133. } catch (Exception $e) {
  5134. }
  5135. } else if ($this->imgLib === 'convert') {
  5136. $convParams = $this->imageMagickConvertPrepare($tmb, 'png', 100, array(), $stat['mime']);
  5137. $cmd = sprintf('%s -colorspace sRGB -trim -- %s %s', ELFINDER_CONVERT_PATH, $convParams['quotedPath'], $convParams['quotedDstPath']);
  5138. $result = false;
  5139. if ($this->procExec($cmd) === 0) {
  5140. if (($s = getimagesize($tmb)) !== false) {
  5141. $result = true;
  5142. }
  5143. }
  5144. }
  5145. if (!$result) {
  5146. // fallback imgLib to GD
  5147. if (function_exists('gd_info') && ($s = getimagesize($tmb))) {
  5148. $this->imgLib = 'gd';
  5149. } else {
  5150. file_exists($tmb) && unlink($tmb);
  5151. return false;
  5152. }
  5153. }
  5154. }
  5155. /* If image smaller or equal thumbnail size - just fitting to thumbnail square */
  5156. if ($s[0] <= $tmbSize && $s[1] <= $tmbSize) {
  5157. $result = $this->imgSquareFit($tmb, $tmbSize, $tmbSize, 'center', 'middle', $this->options['tmbBgColor'], 'png');
  5158. } else {
  5159. if ($this->options['tmbCrop']) {
  5160. $result = $tmb;
  5161. /* Resize and crop if image bigger than thumbnail */
  5162. if (!(($s[0] > $tmbSize && $s[1] <= $tmbSize) || ($s[0] <= $tmbSize && $s[1] > $tmbSize)) || ($s[0] > $tmbSize && $s[1] > $tmbSize)) {
  5163. $result = $this->imgResize($tmb, $tmbSize, $tmbSize, true, false, 'png');
  5164. }
  5165. if ($result && ($s = getimagesize($tmb)) != false) {
  5166. $x = $s[0] > $tmbSize ? intval(($s[0] - $tmbSize) / 2) : 0;
  5167. $y = $s[1] > $tmbSize ? intval(($s[1] - $tmbSize) / 2) : 0;
  5168. $result = $this->imgCrop($result, $tmbSize, $tmbSize, $x, $y, 'png');
  5169. } else {
  5170. $result = false;
  5171. }
  5172. } else {
  5173. $result = $this->imgResize($tmb, $tmbSize, $tmbSize, true, true, 'png');
  5174. }
  5175. if ($result) {
  5176. if ($s = getimagesize($tmb)) {
  5177. if ($s[0] !== $tmbSize || $s[1] !== $tmbSize) {
  5178. $result = $this->imgSquareFit($result, $tmbSize, $tmbSize, 'center', 'middle', $this->options['tmbBgColor'], 'png');
  5179. }
  5180. }
  5181. }
  5182. }
  5183. if (!$result) {
  5184. unlink($tmb);
  5185. return false;
  5186. }
  5187. return $name;
  5188. }
  5189. /**
  5190. * Resize image
  5191. *
  5192. * @param string $path image file
  5193. * @param int $width new width
  5194. * @param int $height new height
  5195. * @param bool $keepProportions crop image
  5196. * @param bool $resizeByBiggerSide resize image based on bigger side if true
  5197. * @param string $destformat image destination format
  5198. * @param int $jpgQuality JEPG quality (1-100)
  5199. * @param array $options Other extra options
  5200. *
  5201. * @return string|false
  5202. * @throws elFinderAbortException
  5203. * @author Dmitry (dio) Levashov
  5204. * @author Alexey Sukhotin
  5205. */
  5206. protected function imgResize($path, $width, $height, $keepProportions = false, $resizeByBiggerSide = true, $destformat = null, $jpgQuality = null, $options = array())
  5207. {
  5208. if (($s = getimagesize($path)) == false) {
  5209. return false;
  5210. }
  5211. if (!$jpgQuality) {
  5212. $jpgQuality = $this->options['jpgQuality'];
  5213. }
  5214. list($orig_w, $orig_h) = array($s[0], $s[1]);
  5215. list($size_w, $size_h) = array($width, $height);
  5216. if (empty($options['unenlarge']) || $orig_w > $size_w || $orig_h > $size_h) {
  5217. if ($keepProportions == true) {
  5218. /* Resizing by biggest side */
  5219. if ($resizeByBiggerSide) {
  5220. if ($orig_w > $orig_h) {
  5221. $size_h = round($orig_h * $width / $orig_w);
  5222. $size_w = $width;
  5223. } else {
  5224. $size_w = round($orig_w * $height / $orig_h);
  5225. $size_h = $height;
  5226. }
  5227. } else {
  5228. if ($orig_w > $orig_h) {
  5229. $size_w = round($orig_w * $height / $orig_h);
  5230. $size_h = $height;
  5231. } else {
  5232. $size_h = round($orig_h * $width / $orig_w);
  5233. $size_w = $width;
  5234. }
  5235. }
  5236. }
  5237. } else {
  5238. $size_w = $orig_w;
  5239. $size_h = $orig_h;
  5240. }
  5241. elFinder::extendTimeLimit(300);
  5242. switch ($this->imgLib) {
  5243. case 'imagick':
  5244. try {
  5245. $img = new imagick($path);
  5246. } catch (Exception $e) {
  5247. return false;
  5248. }
  5249. // Imagick::FILTER_BOX faster than FILTER_LANCZOS so use for createTmb
  5250. // resize bench: http://app-mgng.rhcloud.com/9
  5251. // resize sample: http://www.dylanbeattie.net/magick/filters/result.html
  5252. $filter = ($destformat === 'png' /* createTmb */) ? Imagick::FILTER_BOX : Imagick::FILTER_LANCZOS;
  5253. $ani = ($img->getNumberImages() > 1);
  5254. if ($ani && is_null($destformat)) {
  5255. $img = $img->coalesceImages();
  5256. do {
  5257. $img->resizeImage($size_w, $size_h, $filter, 1);
  5258. } while ($img->nextImage());
  5259. $img->optimizeImageLayers();
  5260. $result = $img->writeImages($path, true);
  5261. } else {
  5262. if ($ani) {
  5263. $img->setFirstIterator();
  5264. }
  5265. if (strtoupper($img->getImageFormat()) === 'JPEG') {
  5266. $img->setImageCompression(imagick::COMPRESSION_JPEG);
  5267. $img->setImageCompressionQuality($jpgQuality);
  5268. if (isset($options['preserveExif']) && !$options['preserveExif']) {
  5269. try {
  5270. $orientation = $img->getImageOrientation();
  5271. } catch (ImagickException $e) {
  5272. $orientation = 0;
  5273. }
  5274. $img->stripImage();
  5275. if ($orientation) {
  5276. $img->setImageOrientation($orientation);
  5277. }
  5278. }
  5279. if ($this->options['jpgProgressive']) {
  5280. $img->setInterlaceScheme(Imagick::INTERLACE_PLANE);
  5281. }
  5282. }
  5283. $img->resizeImage($size_w, $size_h, $filter, true);
  5284. if ($destformat) {
  5285. $result = $this->imagickImage($img, $path, $destformat, $jpgQuality);
  5286. } else {
  5287. $result = $img->writeImage($path);
  5288. }
  5289. }
  5290. $img->clear();
  5291. return $result ? $path : false;
  5292. break;
  5293. case 'convert':
  5294. extract($this->imageMagickConvertPrepare($path, $destformat, $jpgQuality, $s));
  5295. /**
  5296. * @var string $ani
  5297. * @var string $index
  5298. * @var string $coalesce
  5299. * @var string $deconstruct
  5300. * @var string $jpgQuality
  5301. * @var string $quotedPath
  5302. * @var string $quotedDstPath
  5303. * @var string $interlace
  5304. */
  5305. $filter = ($destformat === 'png' /* createTmb */) ? '-filter Box' : '-filter Lanczos';
  5306. $strip = (isset($options['preserveExif']) && !$options['preserveExif']) ? ' -strip' : '';
  5307. $cmd = sprintf('%s %s%s%s%s%s %s -geometry %dx%d! %s %s', ELFINDER_CONVERT_PATH, $quotedPath, $coalesce, $jpgQuality, $strip, $interlace, $filter, $size_w, $size_h, $deconstruct, $quotedDstPath);
  5308. $result = false;
  5309. if ($this->procExec($cmd) === 0) {
  5310. $result = true;
  5311. }
  5312. return $result ? $path : false;
  5313. break;
  5314. case 'gd':
  5315. elFinder::expandMemoryForGD(array($s, array($size_w, $size_h)));
  5316. $img = $this->gdImageCreate($path, $s['mime']);
  5317. if ($img && false != ($tmp = imagecreatetruecolor($size_w, $size_h))) {
  5318. $bgNum = false;
  5319. if ($s[2] === IMAGETYPE_GIF && (!$destformat || $destformat === 'gif')) {
  5320. $bgIdx = imagecolortransparent($img);
  5321. if ($bgIdx !== -1) {
  5322. $c = imagecolorsforindex($img, $bgIdx);
  5323. $bgNum = imagecolorallocate($tmp, $c['red'], $c['green'], $c['blue']);
  5324. imagefill($tmp, 0, 0, $bgNum);
  5325. imagecolortransparent($tmp, $bgNum);
  5326. }
  5327. }
  5328. if ($bgNum === false) {
  5329. $this->gdImageBackground($tmp, 'transparent');
  5330. }
  5331. if (!imagecopyresampled($tmp, $img, 0, 0, 0, 0, $size_w, $size_h, $s[0], $s[1])) {
  5332. return false;
  5333. }
  5334. $result = $this->gdImage($tmp, $path, $destformat, $s['mime'], $jpgQuality);
  5335. imagedestroy($img);
  5336. imagedestroy($tmp);
  5337. return $result ? $path : false;
  5338. }
  5339. break;
  5340. }
  5341. return false;
  5342. }
  5343. /**
  5344. * Crop image
  5345. *
  5346. * @param string $path image file
  5347. * @param int $width crop width
  5348. * @param int $height crop height
  5349. * @param bool $x crop left offset
  5350. * @param bool $y crop top offset
  5351. * @param string $destformat image destination format
  5352. * @param int $jpgQuality JEPG quality (1-100)
  5353. *
  5354. * @return string|false
  5355. * @throws elFinderAbortException
  5356. * @author Dmitry (dio) Levashov
  5357. * @author Alexey Sukhotin
  5358. */
  5359. protected function imgCrop($path, $width, $height, $x, $y, $destformat = null, $jpgQuality = null)
  5360. {
  5361. if (($s = getimagesize($path)) == false) {
  5362. return false;
  5363. }
  5364. if (!$jpgQuality) {
  5365. $jpgQuality = $this->options['jpgQuality'];
  5366. }
  5367. elFinder::extendTimeLimit(300);
  5368. switch ($this->imgLib) {
  5369. case 'imagick':
  5370. try {
  5371. $img = new imagick($path);
  5372. } catch (Exception $e) {
  5373. return false;
  5374. }
  5375. $ani = ($img->getNumberImages() > 1);
  5376. if ($ani && is_null($destformat)) {
  5377. $img = $img->coalesceImages();
  5378. do {
  5379. $img->setImagePage($s[0], $s[1], 0, 0);
  5380. $img->cropImage($width, $height, $x, $y);
  5381. $img->setImagePage($width, $height, 0, 0);
  5382. } while ($img->nextImage());
  5383. $img->optimizeImageLayers();
  5384. $result = $img->writeImages($path, true);
  5385. } else {
  5386. if ($ani) {
  5387. $img->setFirstIterator();
  5388. }
  5389. $img->setImagePage($s[0], $s[1], 0, 0);
  5390. $img->cropImage($width, $height, $x, $y);
  5391. $img->setImagePage($width, $height, 0, 0);
  5392. $result = $this->imagickImage($img, $path, $destformat, $jpgQuality);
  5393. }
  5394. $img->clear();
  5395. return $result ? $path : false;
  5396. break;
  5397. case 'convert':
  5398. extract($this->imageMagickConvertPrepare($path, $destformat, $jpgQuality, $s));
  5399. /**
  5400. * @var string $ani
  5401. * @var string $index
  5402. * @var string $coalesce
  5403. * @var string $deconstruct
  5404. * @var string $jpgQuality
  5405. * @var string $quotedPath
  5406. * @var string $quotedDstPath
  5407. * @var string $interlace
  5408. */
  5409. $cmd = sprintf('%s %s%s%s%s -crop %dx%d+%d+%d%s %s', ELFINDER_CONVERT_PATH, $quotedPath, $coalesce, $jpgQuality, $interlace, $width, $height, $x, $y, $deconstruct, $quotedDstPath);
  5410. $result = false;
  5411. if ($this->procExec($cmd) === 0) {
  5412. $result = true;
  5413. }
  5414. return $result ? $path : false;
  5415. break;
  5416. case 'gd':
  5417. elFinder::expandMemoryForGD(array($s, array($width, $height)));
  5418. $img = $this->gdImageCreate($path, $s['mime']);
  5419. if ($img && false != ($tmp = imagecreatetruecolor($width, $height))) {
  5420. $bgNum = false;
  5421. if ($s[2] === IMAGETYPE_GIF && (!$destformat || $destformat === 'gif')) {
  5422. $bgIdx = imagecolortransparent($img);
  5423. if ($bgIdx !== -1) {
  5424. $c = imagecolorsforindex($img, $bgIdx);
  5425. $bgNum = imagecolorallocate($tmp, $c['red'], $c['green'], $c['blue']);
  5426. imagefill($tmp, 0, 0, $bgNum);
  5427. imagecolortransparent($tmp, $bgNum);
  5428. }
  5429. }
  5430. if ($bgNum === false) {
  5431. $this->gdImageBackground($tmp, 'transparent');
  5432. }
  5433. $size_w = $width;
  5434. $size_h = $height;
  5435. if ($s[0] < $width || $s[1] < $height) {
  5436. $size_w = $s[0];
  5437. $size_h = $s[1];
  5438. }
  5439. if (!imagecopy($tmp, $img, 0, 0, $x, $y, $size_w, $size_h)) {
  5440. return false;
  5441. }
  5442. $result = $this->gdImage($tmp, $path, $destformat, $s['mime'], $jpgQuality);
  5443. imagedestroy($img);
  5444. imagedestroy($tmp);
  5445. return $result ? $path : false;
  5446. }
  5447. break;
  5448. }
  5449. return false;
  5450. }
  5451. /**
  5452. * Put image to square
  5453. *
  5454. * @param string $path image file
  5455. * @param int $width square width
  5456. * @param int $height square height
  5457. * @param int|string $align reserved
  5458. * @param int|string $valign reserved
  5459. * @param string $bgcolor square background color in #rrggbb format
  5460. * @param string $destformat image destination format
  5461. * @param int $jpgQuality JEPG quality (1-100)
  5462. *
  5463. * @return false|string
  5464. * @throws ImagickException
  5465. * @throws elFinderAbortException
  5466. * @author Dmitry (dio) Levashov
  5467. * @author Alexey Sukhotin
  5468. */
  5469. protected function imgSquareFit($path, $width, $height, $align = 'center', $valign = 'middle', $bgcolor = '#0000ff', $destformat = null, $jpgQuality = null)
  5470. {
  5471. if (($s = getimagesize($path)) == false) {
  5472. return false;
  5473. }
  5474. $result = false;
  5475. /* Coordinates for image over square aligning */
  5476. $y = ceil(abs($height - $s[1]) / 2);
  5477. $x = ceil(abs($width - $s[0]) / 2);
  5478. if (!$jpgQuality) {
  5479. $jpgQuality = $this->options['jpgQuality'];
  5480. }
  5481. elFinder::extendTimeLimit(300);
  5482. switch ($this->imgLib) {
  5483. case 'imagick':
  5484. try {
  5485. $img = new imagick($path);
  5486. } catch (Exception $e) {
  5487. return false;
  5488. }
  5489. if ($bgcolor === 'transparent') {
  5490. $bgcolor = 'rgba(255, 255, 255, 0.0)';
  5491. }
  5492. $ani = ($img->getNumberImages() > 1);
  5493. if ($ani && is_null($destformat)) {
  5494. $img1 = new Imagick();
  5495. $img1->setFormat('gif');
  5496. $img = $img->coalesceImages();
  5497. do {
  5498. $gif = new Imagick();
  5499. $gif->newImage($width, $height, new ImagickPixel($bgcolor));
  5500. $gif->setImageColorspace($img->getImageColorspace());
  5501. $gif->setImageFormat('gif');
  5502. $gif->compositeImage($img, imagick::COMPOSITE_OVER, $x, $y);
  5503. $gif->setImageDelay($img->getImageDelay());
  5504. $gif->setImageIterations($img->getImageIterations());
  5505. $img1->addImage($gif);
  5506. $gif->clear();
  5507. } while ($img->nextImage());
  5508. $img1->optimizeImageLayers();
  5509. $result = $img1->writeImages($path, true);
  5510. } else {
  5511. if ($ani) {
  5512. $img->setFirstIterator();
  5513. }
  5514. $img1 = new Imagick();
  5515. $img1->newImage($width, $height, new ImagickPixel($bgcolor));
  5516. $img1->setImageColorspace($img->getImageColorspace());
  5517. $img1->compositeImage($img, imagick::COMPOSITE_OVER, $x, $y);
  5518. $result = $this->imagickImage($img1, $path, $destformat, $jpgQuality);
  5519. }
  5520. $img1->clear();
  5521. $img->clear();
  5522. return $result ? $path : false;
  5523. break;
  5524. case 'convert':
  5525. extract($this->imageMagickConvertPrepare($path, $destformat, $jpgQuality, $s));
  5526. /**
  5527. * @var string $ani
  5528. * @var string $index
  5529. * @var string $coalesce
  5530. * @var string $deconstruct
  5531. * @var string $jpgQuality
  5532. * @var string $quotedPath
  5533. * @var string $quotedDstPath
  5534. * @var string $interlace
  5535. */
  5536. if ($bgcolor === 'transparent') {
  5537. $bgcolor = 'rgba(255, 255, 255, 0.0)';
  5538. }
  5539. $cmd = sprintf('%s -size %dx%d "xc:%s" png:- | convert%s%s%s png:- %s -geometry +%d+%d -compose over -composite%s %s', ELFINDER_CONVERT_PATH, $width, $height, $bgcolor, $coalesce, $jpgQuality, $interlace, $quotedPath, $x, $y, $deconstruct, $quotedDstPath);
  5540. $result = false;
  5541. if ($this->procExec($cmd) === 0) {
  5542. $result = true;
  5543. }
  5544. return $result ? $path : false;
  5545. break;
  5546. case 'gd':
  5547. elFinder::expandMemoryForGD(array($s, array($width, $height)));
  5548. $img = $this->gdImageCreate($path, $s['mime']);
  5549. if ($img && false != ($tmp = imagecreatetruecolor($width, $height))) {
  5550. $this->gdImageBackground($tmp, $bgcolor);
  5551. if ($bgcolor === 'transparent' && ($destformat === 'png' || $s[2] === IMAGETYPE_PNG)) {
  5552. $bgNum = imagecolorallocatealpha($tmp, 255, 255, 255, 127);
  5553. imagefill($tmp, 0, 0, $bgNum);
  5554. }
  5555. if (!imagecopy($tmp, $img, $x, $y, 0, 0, $s[0], $s[1])) {
  5556. return false;
  5557. }
  5558. $result = $this->gdImage($tmp, $path, $destformat, $s['mime'], $jpgQuality);
  5559. imagedestroy($img);
  5560. imagedestroy($tmp);
  5561. return $result ? $path : false;
  5562. }
  5563. break;
  5564. }
  5565. return false;
  5566. }
  5567. /**
  5568. * Rotate image
  5569. *
  5570. * @param string $path image file
  5571. * @param int $degree rotete degrees
  5572. * @param string $bgcolor square background color in #rrggbb format
  5573. * @param string $destformat image destination format
  5574. * @param int $jpgQuality JEPG quality (1-100)
  5575. *
  5576. * @return string|false
  5577. * @throws elFinderAbortException
  5578. * @author nao-pon
  5579. * @author Troex Nevelin
  5580. */
  5581. protected function imgRotate($path, $degree, $bgcolor = '#ffffff', $destformat = null, $jpgQuality = null)
  5582. {
  5583. if (($s = getimagesize($path)) == false || $degree % 360 === 0) {
  5584. return false;
  5585. }
  5586. $result = false;
  5587. // try lossless rotate
  5588. if ($degree % 90 === 0 && in_array($s[2], array(IMAGETYPE_JPEG, IMAGETYPE_JPEG2000))) {
  5589. $count = ($degree / 90) % 4;
  5590. $exiftran = array(
  5591. 1 => '-9',
  5592. 2 => '-1',
  5593. 3 => '-2'
  5594. );
  5595. $jpegtran = array(
  5596. 1 => '90',
  5597. 2 => '180',
  5598. 3 => '270'
  5599. );
  5600. $quotedPath = escapeshellarg($path);
  5601. $cmds = array();
  5602. if ($this->procExec(ELFINDER_EXIFTRAN_PATH . ' -h') === 0) {
  5603. $cmds[] = ELFINDER_EXIFTRAN_PATH . ' -i ' . $exiftran[$count] . ' -- ' . $quotedPath;
  5604. }
  5605. if ($this->procExec(ELFINDER_JPEGTRAN_PATH . ' -version') === 0) {
  5606. $cmds[] = ELFINDER_JPEGTRAN_PATH . ' -rotate ' . $jpegtran[$count] . ' -copy all -outfile ' . $quotedPath . ' -- ' . $quotedPath;
  5607. }
  5608. foreach ($cmds as $cmd) {
  5609. if ($this->procExec($cmd) === 0) {
  5610. $result = true;
  5611. break;
  5612. }
  5613. }
  5614. if ($result) {
  5615. return $path;
  5616. }
  5617. }
  5618. if (!$jpgQuality) {
  5619. $jpgQuality = $this->options['jpgQuality'];
  5620. }
  5621. elFinder::extendTimeLimit(300);
  5622. switch ($this->imgLib) {
  5623. case 'imagick':
  5624. try {
  5625. $img = new imagick($path);
  5626. } catch (Exception $e) {
  5627. return false;
  5628. }
  5629. if ($s[2] === IMAGETYPE_GIF || $s[2] === IMAGETYPE_PNG) {
  5630. $bgcolor = 'rgba(255, 255, 255, 0.0)';
  5631. }
  5632. if ($img->getNumberImages() > 1) {
  5633. $img = $img->coalesceImages();
  5634. do {
  5635. $img->rotateImage(new ImagickPixel($bgcolor), $degree);
  5636. } while ($img->nextImage());
  5637. $img->optimizeImageLayers();
  5638. $result = $img->writeImages($path, true);
  5639. } else {
  5640. $img->rotateImage(new ImagickPixel($bgcolor), $degree);
  5641. $result = $this->imagickImage($img, $path, $destformat, $jpgQuality);
  5642. }
  5643. $img->clear();
  5644. return $result ? $path : false;
  5645. break;
  5646. case 'convert':
  5647. extract($this->imageMagickConvertPrepare($path, $destformat, $jpgQuality, $s));
  5648. /**
  5649. * @var string $ani
  5650. * @var string $index
  5651. * @var string $coalesce
  5652. * @var string $deconstruct
  5653. * @var string $jpgQuality
  5654. * @var string $quotedPath
  5655. * @var string $quotedDstPath
  5656. * @var string $interlace
  5657. */
  5658. if ($s[2] === IMAGETYPE_GIF || $s[2] === IMAGETYPE_PNG) {
  5659. $bgcolor = 'rgba(255, 255, 255, 0.0)';
  5660. }
  5661. $cmd = sprintf('%s%s%s%s -background "%s" -rotate %d%s -- %s %s', ELFINDER_CONVERT_PATH, $coalesce, $jpgQuality, $interlace, $bgcolor, $degree, $deconstruct, $quotedPath, $quotedDstPath);
  5662. $result = false;
  5663. if ($this->procExec($cmd) === 0) {
  5664. $result = true;
  5665. }
  5666. return $result ? $path : false;
  5667. break;
  5668. case 'gd':
  5669. elFinder::expandMemoryForGD(array($s, $s));
  5670. $img = $this->gdImageCreate($path, $s['mime']);
  5671. $degree = 360 - $degree;
  5672. $bgNum = -1;
  5673. $bgIdx = false;
  5674. if ($s[2] === IMAGETYPE_GIF) {
  5675. $bgIdx = imagecolortransparent($img);
  5676. if ($bgIdx !== -1) {
  5677. $c = imagecolorsforindex($img, $bgIdx);
  5678. $w = imagesx($img);
  5679. $h = imagesy($img);
  5680. $newImg = imagecreatetruecolor($w, $h);
  5681. imagepalettecopy($newImg, $img);
  5682. $bgNum = imagecolorallocate($newImg, $c['red'], $c['green'], $c['blue']);
  5683. imagefill($newImg, 0, 0, $bgNum);
  5684. imagecolortransparent($newImg, $bgNum);
  5685. imagecopy($newImg, $img, 0, 0, 0, 0, $w, $h);
  5686. imagedestroy($img);
  5687. $img = $newImg;
  5688. $newImg = null;
  5689. }
  5690. } else if ($s[2] === IMAGETYPE_PNG) {
  5691. $bgNum = imagecolorallocatealpha($img, 255, 255, 255, 127);
  5692. }
  5693. if ($bgNum === -1) {
  5694. list($r, $g, $b) = sscanf($bgcolor, "#%02x%02x%02x");
  5695. $bgNum = imagecolorallocate($img, $r, $g, $b);
  5696. }
  5697. $tmp = imageRotate($img, $degree, $bgNum);
  5698. if ($bgIdx !== -1) {
  5699. imagecolortransparent($tmp, $bgNum);
  5700. }
  5701. $result = $this->gdImage($tmp, $path, $destformat, $s['mime'], $jpgQuality);
  5702. imageDestroy($img);
  5703. imageDestroy($tmp);
  5704. return $result ? $path : false;
  5705. break;
  5706. }
  5707. return false;
  5708. }
  5709. /**
  5710. * Execute shell command
  5711. *
  5712. * @param string $command command line
  5713. * @param string $output stdout strings
  5714. * @param int $return_var process exit code
  5715. * @param string $error_output stderr strings
  5716. *
  5717. * @return int exit code
  5718. * @throws elFinderAbortException
  5719. * @author Alexey Sukhotin
  5720. */
  5721. protected function procExec($command, &$output = '', &$return_var = -1, &$error_output = '')
  5722. {
  5723. return elFinder::procExec($command, $output, $return_var, $error_output);
  5724. }
  5725. /**
  5726. * Remove thumbnail, also remove recursively if stat is directory
  5727. *
  5728. * @param array $stat file stat
  5729. *
  5730. * @return void
  5731. * @throws elFinderAbortException
  5732. * @author Dmitry (dio) Levashov
  5733. * @author Naoki Sawada
  5734. * @author Troex Nevelin
  5735. */
  5736. protected function rmTmb($stat)
  5737. {
  5738. if ($this->tmbPathWritable) {
  5739. if ($stat['mime'] === 'directory') {
  5740. foreach ($this->scandirCE($this->decode($stat['hash'])) as $p) {
  5741. elFinder::extendTimeLimit(30);
  5742. $name = $this->basenameCE($p);
  5743. $name != '.' && $name != '..' && $this->rmTmb($this->stat($p));
  5744. }
  5745. } else if (!empty($stat['tmb']) && $stat['tmb'] != "1") {
  5746. $tmb = $this->tmbPath . DIRECTORY_SEPARATOR . rawurldecode($stat['tmb']);
  5747. file_exists($tmb) && unlink($tmb);
  5748. clearstatcache();
  5749. }
  5750. }
  5751. }
  5752. /**
  5753. * Create an gd image according to the specified mime type
  5754. *
  5755. * @param string $path image file
  5756. * @param string $mime
  5757. *
  5758. * @return resource|false GD image resource identifier
  5759. */
  5760. protected function gdImageCreate($path, $mime)
  5761. {
  5762. switch ($mime) {
  5763. case 'image/jpeg':
  5764. return imagecreatefromjpeg($path);
  5765. case 'image/png':
  5766. return imagecreatefrompng($path);
  5767. case 'image/gif':
  5768. return imagecreatefromgif($path);
  5769. case 'image/x-ms-bmp':
  5770. if (!function_exists('imagecreatefrombmp')) {
  5771. include_once dirname(__FILE__) . '/libs/GdBmp.php';
  5772. }
  5773. return imagecreatefrombmp($path);
  5774. case 'image/xbm':
  5775. return imagecreatefromxbm($path);
  5776. case 'image/xpm':
  5777. return imagecreatefromxpm($path);
  5778. }
  5779. return false;
  5780. }
  5781. /**
  5782. * Output gd image to file
  5783. *
  5784. * @param resource $image gd image resource
  5785. * @param string $filename The path to save the file to.
  5786. * @param string $destformat The Image type to use for $filename
  5787. * @param string $mime The original image mime type
  5788. * @param int $jpgQuality JEPG quality (1-100)
  5789. *
  5790. * @return bool
  5791. */
  5792. protected function gdImage($image, $filename, $destformat, $mime, $jpgQuality = null)
  5793. {
  5794. if (!$jpgQuality) {
  5795. $jpgQuality = $this->options['jpgQuality'];
  5796. }
  5797. if ($destformat) {
  5798. switch ($destformat) {
  5799. case 'jpg':
  5800. $mime = 'image/jpeg';
  5801. break;
  5802. case 'gif':
  5803. $mime = 'image/gif';
  5804. break;
  5805. case 'png':
  5806. default:
  5807. $mime = 'image/png';
  5808. break;
  5809. }
  5810. }
  5811. switch ($mime) {
  5812. case 'image/gif':
  5813. return imagegif($image, $filename);
  5814. case 'image/jpeg':
  5815. if ($this->options['jpgProgressive']) {
  5816. imageinterlace($image, true);
  5817. }
  5818. return imagejpeg($image, $filename, $jpgQuality);
  5819. case 'image/wbmp':
  5820. return imagewbmp($image, $filename);
  5821. case 'image/png':
  5822. default:
  5823. return imagepng($image, $filename);
  5824. }
  5825. }
  5826. /**
  5827. * Output imagick image to file
  5828. *
  5829. * @param imagick $img imagick image resource
  5830. * @param string $filename The path to save the file to.
  5831. * @param string $destformat The Image type to use for $filename
  5832. * @param int $jpgQuality JEPG quality (1-100)
  5833. *
  5834. * @return bool
  5835. */
  5836. protected function imagickImage($img, $filename, $destformat, $jpgQuality = null)
  5837. {
  5838. if (!$jpgQuality) {
  5839. $jpgQuality = $this->options['jpgQuality'];
  5840. }
  5841. try {
  5842. if ($destformat) {
  5843. if ($destformat === 'gif') {
  5844. $img->setImageFormat('gif');
  5845. } else if ($destformat === 'png') {
  5846. $img->setImageFormat('png');
  5847. } else if ($destformat === 'jpg') {
  5848. $img->setImageFormat('jpeg');
  5849. }
  5850. }
  5851. if (strtoupper($img->getImageFormat()) === 'JPEG') {
  5852. $img->setImageCompression(imagick::COMPRESSION_JPEG);
  5853. $img->setImageCompressionQuality($jpgQuality);
  5854. if ($this->options['jpgProgressive']) {
  5855. $img->setInterlaceScheme(Imagick::INTERLACE_PLANE);
  5856. }
  5857. try {
  5858. $orientation = $img->getImageOrientation();
  5859. } catch (ImagickException $e) {
  5860. $orientation = 0;
  5861. }
  5862. $img->stripImage();
  5863. if ($orientation) {
  5864. $img->setImageOrientation($orientation);
  5865. }
  5866. }
  5867. $result = $img->writeImage($filename);
  5868. } catch (Exception $e) {
  5869. $result = false;
  5870. }
  5871. return $result;
  5872. }
  5873. /**
  5874. * Assign the proper background to a gd image
  5875. *
  5876. * @param resource $image gd image resource
  5877. * @param string $bgcolor background color in #rrggbb format
  5878. */
  5879. protected function gdImageBackground($image, $bgcolor)
  5880. {
  5881. if ($bgcolor === 'transparent') {
  5882. imagealphablending($image, false);
  5883. imagesavealpha($image, true);
  5884. } else {
  5885. list($r, $g, $b) = sscanf($bgcolor, "#%02x%02x%02x");
  5886. $bgcolor1 = imagecolorallocate($image, $r, $g, $b);
  5887. imagefill($image, 0, 0, $bgcolor1);
  5888. }
  5889. }
  5890. /**
  5891. * Prepare variables for exec convert of ImageMagick
  5892. *
  5893. * @param string $path
  5894. * @param string $destformat
  5895. * @param int $jpgQuality
  5896. * @param array $imageSize
  5897. * @param null $mime
  5898. *
  5899. * @return array
  5900. * @throws elFinderAbortException
  5901. */
  5902. protected function imageMagickConvertPrepare($path, $destformat, $jpgQuality, $imageSize = null, $mime = null)
  5903. {
  5904. if (is_null($imageSize)) {
  5905. $imageSize = getimagesize($path);
  5906. }
  5907. if (is_null($mime)) {
  5908. $mime = $this->mimetype($path);
  5909. }
  5910. $srcType = $this->getExtentionByMime($mime, ':');
  5911. $ani = false;
  5912. if (preg_match('/^(?:gif|png|ico)/', $srcType)) {
  5913. $cmd = ELFINDER_IDENTIFY_PATH . ' -- ' . escapeshellarg($srcType . $path);
  5914. if ($this->procExec($cmd, $o) === 0) {
  5915. $ani = preg_split('/(?:\r\n|\n|\r)/', trim($o));
  5916. if (count($ani) < 2) {
  5917. $ani = false;
  5918. }
  5919. }
  5920. }
  5921. $coalesce = $index = $interlace = '';
  5922. $deconstruct = ' +repage';
  5923. if ($ani && $destformat !== 'png'/* not createTmb */) {
  5924. if (is_null($destformat)) {
  5925. $coalesce = ' -coalesce -repage 0x0';
  5926. $deconstruct = ' +repage -deconstruct -layers optimize';
  5927. } else if ($imageSize) {
  5928. if ($srcType === 'ico:') {
  5929. $index = '[0]';
  5930. foreach ($ani as $_i => $_info) {
  5931. if (preg_match('/ (\d+)x(\d+) /', $_info, $m)) {
  5932. if ($m[1] == $imageSize[0] && $m[2] == $imageSize[1]) {
  5933. $index = '[' . $_i . ']';
  5934. break;
  5935. }
  5936. }
  5937. }
  5938. }
  5939. }
  5940. } else {
  5941. $index = '[0]';
  5942. }
  5943. if ($imageSize && ($imageSize[2] === IMAGETYPE_JPEG || $imageSize[2] === IMAGETYPE_JPEG2000)) {
  5944. $jpgQuality = ' -quality ' . $jpgQuality;
  5945. if ($this->options['jpgProgressive']) {
  5946. $interlace = ' -interlace Plane';
  5947. }
  5948. } else {
  5949. $jpgQuality = '';
  5950. }
  5951. $quotedPath = escapeshellarg($srcType . $path . $index);
  5952. $quotedDstPath = escapeshellarg(($destformat ? ($destformat . ':') : $srcType) . $path);
  5953. return compact('ani', 'index', 'coalesce', 'deconstruct', 'jpgQuality', 'quotedPath', 'quotedDstPath', 'interlace');
  5954. }
  5955. /*********************** misc *************************/
  5956. /**
  5957. * Find position of first occurrence of string in a string with multibyte support
  5958. *
  5959. * @param string $haystack The string being checked.
  5960. * @param string $needle The string to find in haystack.
  5961. * @param int $offset The search offset. If it is not specified, 0 is used.
  5962. *
  5963. * @return int|bool
  5964. * @author Alexey Sukhotin
  5965. **/
  5966. protected function stripos($haystack, $needle, $offset = 0)
  5967. {
  5968. if (function_exists('mb_stripos')) {
  5969. return mb_stripos($haystack, $needle, $offset, 'UTF-8');
  5970. } else if (function_exists('mb_strtolower') && function_exists('mb_strpos')) {
  5971. return mb_strpos(mb_strtolower($haystack, 'UTF-8'), mb_strtolower($needle, 'UTF-8'), $offset);
  5972. }
  5973. return stripos($haystack, $needle, $offset);
  5974. }
  5975. /**
  5976. * Default serach match method (name match)
  5977. *
  5978. * @param String $name Item name
  5979. * @param String $query Query word
  5980. * @param String $path Item path
  5981. *
  5982. * @return bool @return bool
  5983. */
  5984. protected function searchMatchName($name, $query, $path)
  5985. {
  5986. return $this->stripos($name, $query) !== false;
  5987. }
  5988. /**
  5989. * Get server side available archivers
  5990. *
  5991. * @param bool $use_cache
  5992. *
  5993. * @return array
  5994. * @throws elFinderAbortException
  5995. */
  5996. protected function getArchivers($use_cache = true)
  5997. {
  5998. $sessionKey = 'archivers';
  5999. if ($use_cache) {
  6000. if (isset($this->options['archivers']) && is_array($this->options['archivers']) && $this->options['archivers']) {
  6001. $cache = $this->options['archivers'];
  6002. } else {
  6003. $cache = elFinder::$archivers;
  6004. }
  6005. if ($cache) {
  6006. return $cache;
  6007. } else {
  6008. if ($cache = $this->session->get($sessionKey, array())) {
  6009. return elFinder::$archivers = $cache;
  6010. }
  6011. }
  6012. }
  6013. $arcs = array(
  6014. 'create' => array(),
  6015. 'extract' => array()
  6016. );
  6017. if ($this->procExec('') === 0) {
  6018. $this->procExec(ELFINDER_TAR_PATH . ' --version', $o, $ctar);
  6019. if ($ctar == 0) {
  6020. $arcs['create']['application/x-tar'] = array('cmd' => ELFINDER_TAR_PATH, 'argc' => '-chf', 'ext' => 'tar');
  6021. $arcs['extract']['application/x-tar'] = array('cmd' => ELFINDER_TAR_PATH, 'argc' => '-xf', 'ext' => 'tar', 'toSpec' => '-C ', 'getsize' => array('argc' => '-xvf', 'toSpec' => '--to-stdout|wc -c', 'regex' => '/^.+(?:\r\n|\n|\r)[^\r\n0-9]*([0-9]+)[^\r\n]*$/s', 'replace' => '$1'));
  6022. unset($o);
  6023. $this->procExec(ELFINDER_GZIP_PATH . ' --version', $o, $c);
  6024. if ($c == 0) {
  6025. $arcs['create']['application/x-gzip'] = array('cmd' => ELFINDER_TAR_PATH, 'argc' => '-czhf', 'ext' => 'tgz');
  6026. $arcs['extract']['application/x-gzip'] = array('cmd' => ELFINDER_TAR_PATH, 'argc' => '-xzf', 'ext' => 'tgz', 'toSpec' => '-C ', 'getsize' => array('argc' => '-xvf', 'toSpec' => '--to-stdout|wc -c', 'regex' => '/^.+(?:\r\n|\n|\r)[^\r\n0-9]*([0-9]+)[^\r\n]*$/s', 'replace' => '$1'));
  6027. }
  6028. unset($o);
  6029. $this->procExec(ELFINDER_BZIP2_PATH . ' --version', $o, $c);
  6030. if ($c == 0) {
  6031. $arcs['create']['application/x-bzip2'] = array('cmd' => ELFINDER_TAR_PATH, 'argc' => '-cjhf', 'ext' => 'tbz');
  6032. $arcs['extract']['application/x-bzip2'] = array('cmd' => ELFINDER_TAR_PATH, 'argc' => '-xjf', 'ext' => 'tbz', 'toSpec' => '-C ', 'getsize' => array('argc' => '-xvf', 'toSpec' => '--to-stdout|wc -c', 'regex' => '/^.+(?:\r\n|\n|\r)[^\r\n0-9]*([0-9]+)[^\r\n]*$/s', 'replace' => '$1'));
  6033. }
  6034. unset($o);
  6035. $this->procExec(ELFINDER_XZ_PATH . ' --version', $o, $c);
  6036. if ($c == 0) {
  6037. $arcs['create']['application/x-xz'] = array('cmd' => ELFINDER_TAR_PATH, 'argc' => '-cJhf', 'ext' => 'xz');
  6038. $arcs['extract']['application/x-xz'] = array('cmd' => ELFINDER_TAR_PATH, 'argc' => '-xJf', 'ext' => 'xz', 'toSpec' => '-C ', 'getsize' => array('argc' => '-xvf', 'toSpec' => '--to-stdout|wc -c', 'regex' => '/^.+(?:\r\n|\n|\r)[^\r\n0-9]*([0-9]+)[^\r\n]*$/s', 'replace' => '$1'));
  6039. }
  6040. }
  6041. unset($o);
  6042. $this->procExec(ELFINDER_ZIP_PATH . ' -h', $o, $c);
  6043. if ($c == 0) {
  6044. $arcs['create']['application/zip'] = array('cmd' => ELFINDER_ZIP_PATH, 'argc' => '-r9 -q', 'ext' => 'zip');
  6045. }
  6046. unset($o);
  6047. $this->procExec(ELFINDER_UNZIP_PATH . ' --help', $o, $c);
  6048. if ($c == 0) {
  6049. $arcs['extract']['application/zip'] = array('cmd' => ELFINDER_UNZIP_PATH, 'argc' => '-q', 'ext' => 'zip', 'toSpec' => '-d ', 'getsize' => array('argc' => '-Z -t', 'regex' => '/^.+?,\s?([0-9]+).+$/', 'replace' => '$1'));
  6050. }
  6051. unset($o);
  6052. $this->procExec(ELFINDER_RAR_PATH . ' --version', $o, $c);
  6053. if ($c == 0 || $c == 7) {
  6054. $arcs['create']['application/x-rar'] = array('cmd' => ELFINDER_RAR_PATH, 'argc' => 'a -inul', 'ext' => 'rar');
  6055. }
  6056. unset($o);
  6057. $this->procExec(ELFINDER_UNRAR_PATH, $o, $c);
  6058. if ($c == 0 || $c == 7) {
  6059. $arcs['extract']['application/x-rar'] = array('cmd' => ELFINDER_UNRAR_PATH, 'argc' => 'x -y', 'ext' => 'rar', 'toSpec' => '', 'getsize' => array('argc' => 'l', 'regex' => '/^.+(?:\r\n|\n|\r)[^\r\n0-9]+[0-9]+[^\r\n0-9]+([0-9]+)[^\r\n]+$/s', 'replace' => '$1'));
  6060. }
  6061. unset($o);
  6062. $this->procExec(ELFINDER_7Z_PATH, $o, $c);
  6063. if ($c == 0) {
  6064. $arcs['create']['application/x-7z-compressed'] = array('cmd' => ELFINDER_7Z_PATH, 'argc' => 'a', 'ext' => '7z');
  6065. $arcs['extract']['application/x-7z-compressed'] = array('cmd' => ELFINDER_7Z_PATH, 'argc' => 'x -y', 'ext' => '7z', 'toSpec' => '-o', 'getsize' => array('argc' => 'l', 'regex' => '/^.+(?:\r\n|\n|\r)[^\r\n0-9]+([0-9]+)[^\r\n]+$/s', 'replace' => '$1'));
  6066. if (empty($arcs['create']['application/zip'])) {
  6067. $arcs['create']['application/zip'] = array('cmd' => ELFINDER_7Z_PATH, 'argc' => 'a -tzip', 'ext' => 'zip');
  6068. }
  6069. if (empty($arcs['extract']['application/zip'])) {
  6070. $arcs['extract']['application/zip'] = array('cmd' => ELFINDER_7Z_PATH, 'argc' => 'x -tzip -y', 'ext' => 'zip', 'toSpec' => '-o', 'getsize' => array('argc' => 'l', 'regex' => '/^.+(?:\r\n|\n|\r)[^\r\n0-9]+([0-9]+)[^\r\n]+$/s', 'replace' => '$1'));
  6071. }
  6072. if (empty($arcs['create']['application/x-tar'])) {
  6073. $arcs['create']['application/x-tar'] = array('cmd' => ELFINDER_7Z_PATH, 'argc' => 'a -ttar', 'ext' => 'tar');
  6074. }
  6075. if (empty($arcs['extract']['application/x-tar'])) {
  6076. $arcs['extract']['application/x-tar'] = array('cmd' => ELFINDER_7Z_PATH, 'argc' => 'x -ttar -y', 'ext' => 'tar', 'toSpec' => '-o', 'getsize' => array('argc' => 'l', 'regex' => '/^.+(?:\r\n|\n|\r)[^\r\n0-9]+([0-9]+)[^\r\n]+$/s', 'replace' => '$1'));
  6077. }
  6078. if (substr(PHP_OS, 0, 3) === 'WIN' && empty($arcs['extract']['application/x-rar'])) {
  6079. $arcs['extract']['application/x-rar'] = array('cmd' => ELFINDER_7Z_PATH, 'argc' => 'x -trar -y', 'ext' => 'rar', 'toSpec' => '-o', 'getsize' => array('argc' => 'l', 'regex' => '/^.+(?:\r\n|\n|\r)[^\r\n0-9]+([0-9]+)[^\r\n]+$/s', 'replace' => '$1'));
  6080. }
  6081. }
  6082. }
  6083. // Use PHP ZipArchive Class
  6084. if (class_exists('ZipArchive', false)) {
  6085. if (empty($arcs['create']['application/zip'])) {
  6086. $arcs['create']['application/zip'] = array('cmd' => 'phpfunction', 'argc' => array('self', 'zipArchiveZip'), 'ext' => 'zip');
  6087. }
  6088. if (empty($arcs['extract']['application/zip'])) {
  6089. $arcs['extract']['application/zip'] = array('cmd' => 'phpfunction', 'argc' => array('self', 'zipArchiveUnzip'), 'ext' => 'zip');
  6090. }
  6091. }
  6092. $this->session->set($sessionKey, $arcs);
  6093. return elFinder::$archivers = $arcs;
  6094. }
  6095. /**
  6096. * Resolve relative / (Unix-like)absolute path
  6097. *
  6098. * @param string $path target path
  6099. * @param string $base base path
  6100. *
  6101. * @return string
  6102. */
  6103. protected function getFullPath($path, $base)
  6104. {
  6105. $separator = $this->separator;
  6106. $systemroot = $this->systemRoot;
  6107. $base = (string)$base;
  6108. if ($base[0] === $separator && substr($base, 0, strlen($systemroot)) !== $systemroot) {
  6109. $base = $systemroot . substr($base, 1);
  6110. }
  6111. if ($base !== $systemroot) {
  6112. $base = rtrim($base, $separator);
  6113. }
  6114. // 'Here'
  6115. if ($path === '' || $path === '.' . $separator) return $base;
  6116. $sepquoted = preg_quote($separator, '#');
  6117. if (substr($path, 0, 3) === '..' . $separator) {
  6118. $path = $base . $separator . $path;
  6119. }
  6120. // normalize `/../`
  6121. $normreg = '#(' . $sepquoted . ')[^' . $sepquoted . ']+' . $sepquoted . '\.\.' . $sepquoted . '#'; // '#(/)[^\/]+/\.\./#'
  6122. while (preg_match($normreg, $path)) {
  6123. $path = preg_replace($normreg, '$1', $path, 1);
  6124. }
  6125. if ($path !== $systemroot) {
  6126. $path = rtrim($path, $separator);
  6127. }
  6128. // Absolute path
  6129. if ($path[0] === $separator || strpos($path, $systemroot) === 0) {
  6130. return $path;
  6131. }
  6132. $preg_separator = '#' . $sepquoted . '#';
  6133. // Relative path from 'Here'
  6134. if (substr($path, 0, 2) === '.' . $separator || $path[0] !== '.') {
  6135. $arrn = preg_split($preg_separator, $path, -1, PREG_SPLIT_NO_EMPTY);
  6136. if ($arrn[0] !== '.') {
  6137. array_unshift($arrn, '.');
  6138. }
  6139. $arrn[0] = rtrim($base, $separator);
  6140. return join($separator, $arrn);
  6141. }
  6142. return $path;
  6143. }
  6144. /**
  6145. * Remove directory recursive on local file system
  6146. *
  6147. * @param string $dir Target dirctory path
  6148. *
  6149. * @return boolean
  6150. * @throws elFinderAbortException
  6151. * @author Naoki Sawada
  6152. */
  6153. public function rmdirRecursive($dir)
  6154. {
  6155. return self::localRmdirRecursive($dir);
  6156. }
  6157. /**
  6158. * Create archive and return its path
  6159. *
  6160. * @param string $dir target dir
  6161. * @param array $files files names list
  6162. * @param string $name archive name
  6163. * @param array $arc archiver options
  6164. *
  6165. * @return string|bool
  6166. * @throws elFinderAbortException
  6167. * @author Dmitry (dio) Levashov,
  6168. * @author Alexey Sukhotin
  6169. * @author Naoki Sawada
  6170. */
  6171. protected function makeArchive($dir, $files, $name, $arc)
  6172. {
  6173. if ($arc['cmd'] === 'phpfunction') {
  6174. if (is_callable($arc['argc'])) {
  6175. call_user_func_array($arc['argc'], array($dir, $files, $name));
  6176. }
  6177. } else {
  6178. $cwd = getcwd();
  6179. if (chdir($dir)) {
  6180. foreach ($files as $i => $file) {
  6181. $files[$i] = '.' . DIRECTORY_SEPARATOR . basename($file);
  6182. }
  6183. $files = array_map('escapeshellarg', $files);
  6184. $cmd = $arc['cmd'] . ' ' . $arc['argc'] . ' ' . escapeshellarg($name) . ' ' . implode(' ', $files);
  6185. $this->procExec($cmd, $o, $c);
  6186. chdir($cwd);
  6187. } else {
  6188. return false;
  6189. }
  6190. }
  6191. $path = $dir . DIRECTORY_SEPARATOR . $name;
  6192. return file_exists($path) ? $path : false;
  6193. }
  6194. /**
  6195. * Unpack archive
  6196. *
  6197. * @param string $path archive path
  6198. * @param array $arc archiver command and arguments (same as in $this->archivers)
  6199. * @param bool|string $mode bool: remove archive ( unlink($path) ) | string: extract to directory
  6200. *
  6201. * @return void
  6202. * @throws elFinderAbortException
  6203. * @author Dmitry (dio) Levashov
  6204. * @author Alexey Sukhotin
  6205. * @author Naoki Sawada
  6206. */
  6207. protected function unpackArchive($path, $arc, $mode = true)
  6208. {
  6209. if (is_string($mode)) {
  6210. $dir = $mode;
  6211. $chdir = null;
  6212. $remove = false;
  6213. } else {
  6214. $dir = dirname($path);
  6215. $chdir = $dir;
  6216. $remove = $mode;
  6217. }
  6218. $dir = realpath($dir);
  6219. $path = realpath($path);
  6220. if ($arc['cmd'] === 'phpfunction') {
  6221. if (is_callable($arc['argc'])) {
  6222. call_user_func_array($arc['argc'], array($path, $dir));
  6223. }
  6224. } else {
  6225. $cwd = getcwd();
  6226. if (!$chdir || chdir($dir)) {
  6227. if (!empty($arc['getsize'])) {
  6228. // Check total file size after extraction
  6229. $getsize = $arc['getsize'];
  6230. if (is_array($getsize) && !empty($getsize['regex']) && !empty($getsize['replace'])) {
  6231. $cmd = $arc['cmd'] . ' ' . $getsize['argc'] . ' ' . escapeshellarg($path) . (!empty($getsize['toSpec'])? (' ' . $getsize['toSpec']): '');
  6232. $this->procExec($cmd, $o, $c);
  6233. if ($o) {
  6234. $size = preg_replace($getsize['regex'], $getsize['replace'], trim($o));
  6235. $comp = function_exists('bccomp')? 'bccomp' : 'strnatcmp';
  6236. if (!empty($this->options['maxArcFilesSize'])) {
  6237. if ($comp($size, (string)$this->options['maxArcFilesSize']) > 0) {
  6238. throw new Exception(elFinder::ERROR_ARC_MAXSIZE);
  6239. }
  6240. }
  6241. }
  6242. unset($o, $c);
  6243. }
  6244. }
  6245. if ($chdir) {
  6246. $cmd = $arc['cmd'] . ' ' . $arc['argc'] . ' ' . escapeshellarg(basename($path));
  6247. } else {
  6248. $cmd = $arc['cmd'] . ' ' . $arc['argc'] . ' ' . escapeshellarg($path) . ' ' . $arc['toSpec'] . escapeshellarg($dir);
  6249. }
  6250. $this->procExec($cmd, $o, $c);
  6251. $chdir && chdir($cwd);
  6252. }
  6253. }
  6254. $remove && unlink($path);
  6255. }
  6256. /**
  6257. * Check and filter the extracted items
  6258. *
  6259. * @param string $path target local path
  6260. * @param array $checks types to check default: ['symlink', 'name', 'writable', 'mime']
  6261. *
  6262. * @return array ['symlinks' => [], 'names' => [], 'writables' => [], 'mimes' => [], 'rmNames' => [], 'totalSize' => 0]
  6263. * @throws elFinderAbortException
  6264. * @throws Exception
  6265. * @author Naoki Sawada
  6266. */
  6267. protected function checkExtractItems($path, $checks = null)
  6268. {
  6269. if (is_null($checks) || !is_array($checks)) {
  6270. $checks = array('symlink', 'name', 'writable', 'mime');
  6271. }
  6272. $chkSymlink = in_array('symlink', $checks);
  6273. $chkName = in_array('name', $checks);
  6274. $chkWritable = in_array('writable', $checks);
  6275. $chkMime = in_array('mime', $checks);
  6276. $res = array(
  6277. 'symlinks' => array(),
  6278. 'names' => array(),
  6279. 'writables' => array(),
  6280. 'mimes' => array(),
  6281. 'rmNames' => array(),
  6282. 'totalSize' => 0
  6283. );
  6284. if (is_dir($path)) {
  6285. $files = self::localScandir($path);
  6286. } else {
  6287. $files = array(basename($path));
  6288. $path = dirname($path);
  6289. }
  6290. foreach ($files as $name) {
  6291. $p = $path . DIRECTORY_SEPARATOR . $name;
  6292. $utf8Name = elFinder::$instance->utf8Encode($name);
  6293. if ($name !== $utf8Name) {
  6294. $fsSame = false;
  6295. if ($this->encoding) {
  6296. // test as fs encoding
  6297. $_utf8 = @iconv($this->encoding, 'utf-8//IGNORE', $name);
  6298. if (@iconv('utf-8', $this->encoding.'//IGNORE', $_utf8) === $name) {
  6299. $fsSame = true;
  6300. $utf8Name = $_utf8;
  6301. } else {
  6302. $_name = $this->convEncIn($utf8Name, true);
  6303. }
  6304. } else {
  6305. $_name = $utf8Name;
  6306. }
  6307. if (!$fsSame && rename($p, $path . DIRECTORY_SEPARATOR . $_name)) {
  6308. $name = $_name;
  6309. $p = $path . DIRECTORY_SEPARATOR . $name;
  6310. }
  6311. }
  6312. if (!is_readable($p)) {
  6313. // Perhaps a symbolic link to open_basedir restricted location
  6314. self::localRmdirRecursive($p);
  6315. $res['symlinks'][] = $p;
  6316. $res['rmNames'][] = $utf8Name;
  6317. continue;
  6318. }
  6319. if ($chkSymlink && is_link($p)) {
  6320. self::localRmdirRecursive($p);
  6321. $res['symlinks'][] = $p;
  6322. $res['rmNames'][] = $utf8Name;
  6323. continue;
  6324. }
  6325. $isDir = is_dir($p);
  6326. if ($chkName && !$this->nameAccepted($name, $isDir)) {
  6327. self::localRmdirRecursive($p);
  6328. $res['names'][] = $p;
  6329. $res['rmNames'][] = $utf8Name;
  6330. continue;
  6331. }
  6332. if ($chkWritable && !$this->attr($p, 'write', null, $isDir)) {
  6333. self::localRmdirRecursive($p);
  6334. $res['writables'][] = $p;
  6335. $res['rmNames'][] = $utf8Name;
  6336. continue;
  6337. }
  6338. if ($isDir) {
  6339. $cRes = $this->checkExtractItems($p, $checks);
  6340. foreach ($cRes as $k => $v) {
  6341. if (is_array($v)) {
  6342. $res[$k] = array_merge($res[$k], $cRes[$k]);
  6343. } else {
  6344. $res[$k] += $cRes[$k];
  6345. }
  6346. }
  6347. } else {
  6348. if ($chkMime && ($mimeByName = elFinderVolumeDriver::mimetypeInternalDetect($name)) && !$this->allowPutMime($mimeByName)) {
  6349. self::localRmdirRecursive($p);
  6350. $res['mimes'][] = $p;
  6351. $res['rmNames'][] = $utf8Name;
  6352. continue;
  6353. }
  6354. $res['totalSize'] += (int)sprintf('%u', filesize($p));
  6355. }
  6356. }
  6357. $res['rmNames'] = array_unique($res['rmNames']);
  6358. return $res;
  6359. }
  6360. /**
  6361. * Return files of target directory that is dotfiles excludes.
  6362. *
  6363. * @param string $dir target directory path
  6364. *
  6365. * @return array
  6366. * @throws Exception
  6367. * @author Naoki Sawada
  6368. */
  6369. protected static function localScandir($dir)
  6370. {
  6371. // PHP function scandir() is not work well in specific environment. I dont know why.
  6372. // ref. https://github.com/Studio-42/elFinder/issues/1248
  6373. $files = array();
  6374. if ($dh = opendir($dir)) {
  6375. while (false !== ($file = readdir($dh))) {
  6376. if ($file !== '.' && $file !== '..') {
  6377. $files[] = $file;
  6378. }
  6379. }
  6380. closedir($dh);
  6381. } else {
  6382. throw new Exception('Can not open local directory.');
  6383. }
  6384. return $files;
  6385. }
  6386. /**
  6387. * Remove directory recursive on local file system
  6388. *
  6389. * @param string $dir Target dirctory path
  6390. *
  6391. * @return boolean
  6392. * @throws elFinderAbortException
  6393. * @author Naoki Sawada
  6394. */
  6395. protected static function localRmdirRecursive($dir)
  6396. {
  6397. // try system command
  6398. if (is_callable('exec')) {
  6399. $o = '';
  6400. $r = 1;
  6401. if (substr(PHP_OS, 0, 3) === 'WIN') {
  6402. if (!is_link($dir) && is_dir($dir)) {
  6403. exec('rd /S /Q ' . escapeshellarg($dir), $o, $r);
  6404. } else {
  6405. exec('del /F /Q ' . escapeshellarg($dir), $o, $r);
  6406. }
  6407. } else {
  6408. exec('rm -rf ' . escapeshellarg($dir), $o, $r);
  6409. }
  6410. if ($r === 0) {
  6411. return true;
  6412. }
  6413. }
  6414. if (!is_link($dir) && is_dir($dir)) {
  6415. chmod($dir, 0777);
  6416. if ($handle = opendir($dir)) {
  6417. while (false !== ($file = readdir($handle))) {
  6418. if ($file === '.' || $file === '..') {
  6419. continue;
  6420. }
  6421. elFinder::extendTimeLimit(30);
  6422. $path = $dir . DIRECTORY_SEPARATOR . $file;
  6423. if (!is_link($dir) && is_dir($path)) {
  6424. self::localRmdirRecursive($path);
  6425. } else {
  6426. chmod($path, 0666);
  6427. unlink($path);
  6428. }
  6429. }
  6430. closedir($handle);
  6431. }
  6432. return rmdir($dir);
  6433. } else {
  6434. chmod($dir, 0666);
  6435. return unlink($dir);
  6436. }
  6437. }
  6438. /**
  6439. * Move item recursive on local file system
  6440. *
  6441. * @param string $src
  6442. * @param string $target
  6443. * @param bool $overWrite
  6444. * @param bool $copyJoin
  6445. *
  6446. * @return boolean
  6447. * @throws elFinderAbortException
  6448. * @throws Exception
  6449. * @author Naoki Sawada
  6450. */
  6451. protected static function localMoveRecursive($src, $target, $overWrite = true, $copyJoin = true)
  6452. {
  6453. $res = false;
  6454. if (!file_exists($target)) {
  6455. return rename($src, $target);
  6456. }
  6457. if (!$copyJoin || !is_dir($target)) {
  6458. if ($overWrite) {
  6459. if (is_dir($target)) {
  6460. $del = self::localRmdirRecursive($target);
  6461. } else {
  6462. $del = unlink($target);
  6463. }
  6464. if ($del) {
  6465. return rename($src, $target);
  6466. }
  6467. }
  6468. } else {
  6469. foreach (self::localScandir($src) as $item) {
  6470. $res |= self::localMoveRecursive($src . DIRECTORY_SEPARATOR . $item, $target . DIRECTORY_SEPARATOR . $item, $overWrite, $copyJoin);
  6471. }
  6472. }
  6473. return (bool)$res;
  6474. }
  6475. /**
  6476. * Create Zip archive using PHP class ZipArchive
  6477. *
  6478. * @param string $dir target dir
  6479. * @param array $files files names list
  6480. * @param string|object $zipPath Zip archive name
  6481. *
  6482. * @return bool
  6483. * @author Naoki Sawada
  6484. */
  6485. protected static function zipArchiveZip($dir, $files, $zipPath)
  6486. {
  6487. try {
  6488. if ($start = is_string($zipPath)) {
  6489. $zip = new ZipArchive();
  6490. if ($zip->open($dir . DIRECTORY_SEPARATOR . $zipPath, ZipArchive::CREATE) !== true) {
  6491. $zip = false;
  6492. }
  6493. } else {
  6494. $zip = $zipPath;
  6495. }
  6496. if ($zip) {
  6497. foreach ($files as $file) {
  6498. $path = $dir . DIRECTORY_SEPARATOR . $file;
  6499. if (is_dir($path)) {
  6500. $zip->addEmptyDir($file);
  6501. $_files = array();
  6502. if ($handle = opendir($path)) {
  6503. while (false !== ($entry = readdir($handle))) {
  6504. if ($entry !== "." && $entry !== "..") {
  6505. $_files[] = $file . DIRECTORY_SEPARATOR . $entry;
  6506. }
  6507. }
  6508. closedir($handle);
  6509. }
  6510. if ($_files) {
  6511. self::zipArchiveZip($dir, $_files, $zip);
  6512. }
  6513. } else {
  6514. $zip->addFile($path, $file);
  6515. }
  6516. }
  6517. $start && $zip->close();
  6518. }
  6519. } catch (Exception $e) {
  6520. return false;
  6521. }
  6522. return true;
  6523. }
  6524. /**
  6525. * Unpack Zip archive using PHP class ZipArchive
  6526. *
  6527. * @param string $zipPath Zip archive name
  6528. * @param string $toDir Extract to path
  6529. *
  6530. * @return bool
  6531. * @author Naoki Sawada
  6532. */
  6533. protected static function zipArchiveUnzip($zipPath, $toDir)
  6534. {
  6535. try {
  6536. $zip = new ZipArchive();
  6537. if ($zip->open($zipPath) === true) {
  6538. // Check total file size after extraction
  6539. $num = $zip->numFiles;
  6540. $size = 0;
  6541. $maxSize = empty(self::$maxArcFilesSize)? '' : (string)self::$maxArcFilesSize;
  6542. $comp = function_exists('bccomp')? 'bccomp' : 'strnatcmp';
  6543. for ($i = 0; $i < $num; $i++) {
  6544. $stat = $zip->statIndex($i);
  6545. $size += $stat['size'];
  6546. if (strpos((string)$size, 'E') !== false) {
  6547. // Cannot handle values exceeding PHP_INT_MAX
  6548. throw new Exception(elFinder::ERROR_ARC_MAXSIZE);
  6549. }
  6550. if (!$maxSize) {
  6551. if ($comp($size, $maxSize) > 0) {
  6552. throw new Exception(elFinder::ERROR_ARC_MAXSIZE);
  6553. }
  6554. }
  6555. }
  6556. // do extract
  6557. $zip->extractTo($toDir);
  6558. $zip->close();
  6559. }
  6560. } catch (Exception $e) {
  6561. throw $e;
  6562. }
  6563. return true;
  6564. }
  6565. /**
  6566. * Recursive symlinks search
  6567. *
  6568. * @param string $path file/dir path
  6569. *
  6570. * @return bool
  6571. * @throws Exception
  6572. * @author Dmitry (dio) Levashov
  6573. */
  6574. protected static function localFindSymlinks($path)
  6575. {
  6576. if (is_link($path)) {
  6577. return true;
  6578. }
  6579. if (is_dir($path)) {
  6580. foreach (self::localScandir($path) as $name) {
  6581. $p = $path . DIRECTORY_SEPARATOR . $name;
  6582. if (is_link($p)) {
  6583. return true;
  6584. }
  6585. if (is_dir($p) && self::localFindSymlinks($p)) {
  6586. return true;
  6587. }
  6588. }
  6589. }
  6590. return false;
  6591. }
  6592. /**==================================* abstract methods *====================================**/
  6593. /*********************** paths/urls *************************/
  6594. /**
  6595. * Return parent directory path
  6596. *
  6597. * @param string $path file path
  6598. *
  6599. * @return string
  6600. * @author Dmitry (dio) Levashov
  6601. **/
  6602. abstract protected function _dirname($path);
  6603. /**
  6604. * Return file name
  6605. *
  6606. * @param string $path file path
  6607. *
  6608. * @return string
  6609. * @author Dmitry (dio) Levashov
  6610. **/
  6611. abstract protected function _basename($path);
  6612. /**
  6613. * Join dir name and file name and return full path.
  6614. * Some drivers (db) use int as path - so we give to concat path to driver itself
  6615. *
  6616. * @param string $dir dir path
  6617. * @param string $name file name
  6618. *
  6619. * @return string
  6620. * @author Dmitry (dio) Levashov
  6621. **/
  6622. abstract protected function _joinPath($dir, $name);
  6623. /**
  6624. * Return normalized path
  6625. *
  6626. * @param string $path file path
  6627. *
  6628. * @return string
  6629. * @author Dmitry (dio) Levashov
  6630. **/
  6631. abstract protected function _normpath($path);
  6632. /**
  6633. * Return file path related to root dir
  6634. *
  6635. * @param string $path file path
  6636. *
  6637. * @return string
  6638. * @author Dmitry (dio) Levashov
  6639. **/
  6640. abstract protected function _relpath($path);
  6641. /**
  6642. * Convert path related to root dir into real path
  6643. *
  6644. * @param string $path rel file path
  6645. *
  6646. * @return string
  6647. * @author Dmitry (dio) Levashov
  6648. **/
  6649. abstract protected function _abspath($path);
  6650. /**
  6651. * Return fake path started from root dir.
  6652. * Required to show path on client side.
  6653. *
  6654. * @param string $path file path
  6655. *
  6656. * @return string
  6657. * @author Dmitry (dio) Levashov
  6658. **/
  6659. abstract protected function _path($path);
  6660. /**
  6661. * Return true if $path is children of $parent
  6662. *
  6663. * @param string $path path to check
  6664. * @param string $parent parent path
  6665. *
  6666. * @return bool
  6667. * @author Dmitry (dio) Levashov
  6668. **/
  6669. abstract protected function _inpath($path, $parent);
  6670. /**
  6671. * Return stat for given path.
  6672. * Stat contains following fields:
  6673. * - (int) size file size in b. required
  6674. * - (int) ts file modification time in unix time. required
  6675. * - (string) mime mimetype. required for folders, others - optionally
  6676. * - (bool) read read permissions. required
  6677. * - (bool) write write permissions. required
  6678. * - (bool) locked is object locked. optionally
  6679. * - (bool) hidden is object hidden. optionally
  6680. * - (string) alias for symlinks - link target path relative to root path. optionally
  6681. * - (string) target for symlinks - link target path. optionally
  6682. * If file does not exists - returns empty array or false.
  6683. *
  6684. * @param string $path file path
  6685. *
  6686. * @return array|false
  6687. * @author Dmitry (dio) Levashov
  6688. **/
  6689. abstract protected function _stat($path);
  6690. /***************** file stat ********************/
  6691. /**
  6692. * Return true if path is dir and has at least one childs directory
  6693. *
  6694. * @param string $path dir path
  6695. *
  6696. * @return bool
  6697. * @author Dmitry (dio) Levashov
  6698. **/
  6699. abstract protected function _subdirs($path);
  6700. /**
  6701. * Return object width and height
  6702. * Ususaly used for images, but can be realize for video etc...
  6703. *
  6704. * @param string $path file path
  6705. * @param string $mime file mime type
  6706. *
  6707. * @return string
  6708. * @author Dmitry (dio) Levashov
  6709. **/
  6710. abstract protected function _dimensions($path, $mime);
  6711. /******************** file/dir content *********************/
  6712. /**
  6713. * Return files list in directory
  6714. *
  6715. * @param string $path dir path
  6716. *
  6717. * @return array
  6718. * @author Dmitry (dio) Levashov
  6719. **/
  6720. abstract protected function _scandir($path);
  6721. /**
  6722. * Open file and return file pointer
  6723. *
  6724. * @param string $path file path
  6725. * @param string $mode open mode
  6726. *
  6727. * @return resource|false
  6728. * @author Dmitry (dio) Levashov
  6729. **/
  6730. abstract protected function _fopen($path, $mode = "rb");
  6731. /**
  6732. * Close opened file
  6733. *
  6734. * @param resource $fp file pointer
  6735. * @param string $path file path
  6736. *
  6737. * @return bool
  6738. * @author Dmitry (dio) Levashov
  6739. **/
  6740. abstract protected function _fclose($fp, $path = '');
  6741. /******************** file/dir manipulations *************************/
  6742. /**
  6743. * Create dir and return created dir path or false on failed
  6744. *
  6745. * @param string $path parent dir path
  6746. * @param string $name new directory name
  6747. *
  6748. * @return string|bool
  6749. * @author Dmitry (dio) Levashov
  6750. **/
  6751. abstract protected function _mkdir($path, $name);
  6752. /**
  6753. * Create file and return it's path or false on failed
  6754. *
  6755. * @param string $path parent dir path
  6756. * @param string $name new file name
  6757. *
  6758. * @return string|bool
  6759. * @author Dmitry (dio) Levashov
  6760. **/
  6761. abstract protected function _mkfile($path, $name);
  6762. /**
  6763. * Create symlink
  6764. *
  6765. * @param string $source file to link to
  6766. * @param string $targetDir folder to create link in
  6767. * @param string $name symlink name
  6768. *
  6769. * @return bool
  6770. * @author Dmitry (dio) Levashov
  6771. **/
  6772. abstract protected function _symlink($source, $targetDir, $name);
  6773. /**
  6774. * Copy file into another file (only inside one volume)
  6775. *
  6776. * @param string $source source file path
  6777. * @param $targetDir
  6778. * @param string $name file name
  6779. *
  6780. * @return bool|string
  6781. * @internal param string $target target dir path
  6782. * @author Dmitry (dio) Levashov
  6783. */
  6784. abstract protected function _copy($source, $targetDir, $name);
  6785. /**
  6786. * Move file into another parent dir.
  6787. * Return new file path or false.
  6788. *
  6789. * @param string $source source file path
  6790. * @param $targetDir
  6791. * @param string $name file name
  6792. *
  6793. * @return bool|string
  6794. * @internal param string $target target dir path
  6795. * @author Dmitry (dio) Levashov
  6796. */
  6797. abstract protected function _move($source, $targetDir, $name);
  6798. /**
  6799. * Remove file
  6800. *
  6801. * @param string $path file path
  6802. *
  6803. * @return bool
  6804. * @author Dmitry (dio) Levashov
  6805. **/
  6806. abstract protected function _unlink($path);
  6807. /**
  6808. * Remove dir
  6809. *
  6810. * @param string $path dir path
  6811. *
  6812. * @return bool
  6813. * @author Dmitry (dio) Levashov
  6814. **/
  6815. abstract protected function _rmdir($path);
  6816. /**
  6817. * Create new file and write into it from file pointer.
  6818. * Return new file path or false on error.
  6819. *
  6820. * @param resource $fp file pointer
  6821. * @param string $dir target dir path
  6822. * @param string $name file name
  6823. * @param array $stat file stat (required by some virtual fs)
  6824. *
  6825. * @return bool|string
  6826. * @author Dmitry (dio) Levashov
  6827. **/
  6828. abstract protected function _save($fp, $dir, $name, $stat);
  6829. /**
  6830. * Get file contents
  6831. *
  6832. * @param string $path file path
  6833. *
  6834. * @return string|false
  6835. * @author Dmitry (dio) Levashov
  6836. **/
  6837. abstract protected function _getContents($path);
  6838. /**
  6839. * Write a string to a file
  6840. *
  6841. * @param string $path file path
  6842. * @param string $content new file content
  6843. *
  6844. * @return bool
  6845. * @author Dmitry (dio) Levashov
  6846. **/
  6847. abstract protected function _filePutContents($path, $content);
  6848. /**
  6849. * Extract files from archive
  6850. *
  6851. * @param string $path file path
  6852. * @param array $arc archiver options
  6853. *
  6854. * @return bool
  6855. * @author Dmitry (dio) Levashov,
  6856. * @author Alexey Sukhotin
  6857. **/
  6858. abstract protected function _extract($path, $arc);
  6859. /**
  6860. * Create archive and return its path
  6861. *
  6862. * @param string $dir target dir
  6863. * @param array $files files names list
  6864. * @param string $name archive name
  6865. * @param array $arc archiver options
  6866. *
  6867. * @return string|bool
  6868. * @author Dmitry (dio) Levashov,
  6869. * @author Alexey Sukhotin
  6870. **/
  6871. abstract protected function _archive($dir, $files, $name, $arc);
  6872. /**
  6873. * Detect available archivers
  6874. *
  6875. * @return void
  6876. * @author Dmitry (dio) Levashov,
  6877. * @author Alexey Sukhotin
  6878. **/
  6879. abstract protected function _checkArchivers();
  6880. /**
  6881. * Change file mode (chmod)
  6882. *
  6883. * @param string $path file path
  6884. * @param string $mode octal string such as '0755'
  6885. *
  6886. * @return bool
  6887. * @author David Bartle,
  6888. **/
  6889. abstract protected function _chmod($path, $mode);
  6890. } // END class