elFinderVolumeDriver.class.php 191 KB

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