elFinderVolumeDriver.class.php 98 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702
  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. * Driver id
  17. * Must be started from letter and contains [a-z0-9]
  18. * Used as part of volume id
  19. *
  20. * @var string
  21. **/
  22. protected $driverId = 'a';
  23. /**
  24. * Volume id - used as prefix for files hashes
  25. *
  26. * @var string
  27. **/
  28. protected $id = '';
  29. /**
  30. * Flag - volume "mounted" and available
  31. *
  32. * @var bool
  33. **/
  34. protected $mounted = false;
  35. /**
  36. * Root directory path
  37. *
  38. * @var string
  39. **/
  40. protected $root = '';
  41. /**
  42. * Root basename | alias
  43. *
  44. * @var string
  45. **/
  46. protected $rootName = '';
  47. /**
  48. * Default directory to open
  49. *
  50. * @var string
  51. **/
  52. protected $startPath = '';
  53. /**
  54. * Base URL
  55. *
  56. * @var string
  57. **/
  58. protected $URL = '';
  59. /**
  60. * Thumbnails dir path
  61. *
  62. * @var string
  63. **/
  64. protected $tmbPath = '';
  65. /**
  66. * Is thumbnails dir writable
  67. *
  68. * @var bool
  69. **/
  70. protected $tmbPathWritable = false;
  71. /**
  72. * Thumbnails base URL
  73. *
  74. * @var string
  75. **/
  76. protected $tmbURL = '';
  77. /**
  78. * Thumbnails size in px
  79. *
  80. * @var int
  81. **/
  82. protected $tmbSize = 48;
  83. /**
  84. * Image manipulation lib name
  85. * auto|imagick|mogtify|gd
  86. *
  87. * @var string
  88. **/
  89. protected $imgLib = 'auto';
  90. /**
  91. * Library to crypt files name
  92. *
  93. * @var string
  94. **/
  95. protected $cryptLib = '';
  96. /**
  97. * Archivers config
  98. *
  99. * @var array
  100. **/
  101. protected $archivers = array(
  102. 'create' => array(),
  103. 'extract' => array()
  104. );
  105. /**
  106. * How many subdirs levels return for tree
  107. *
  108. * @var int
  109. **/
  110. protected $treeDeep = 1;
  111. /**
  112. * Errors from last failed action
  113. *
  114. * @var array
  115. **/
  116. protected $error = array();
  117. /**
  118. * Today 24:00 timestamp
  119. *
  120. * @var int
  121. **/
  122. protected $today = 0;
  123. /**
  124. * Yesterday 24:00 timestamp
  125. *
  126. * @var int
  127. **/
  128. protected $yesterday = 0;
  129. /**
  130. * Object configuration
  131. *
  132. * @var array
  133. **/
  134. protected $options = array(
  135. 'id' => '',
  136. // root directory path
  137. 'path' => '',
  138. // open this path on initial request instead of root path
  139. 'startPath' => '',
  140. // how many subdirs levels return per request
  141. 'treeDeep' => 1,
  142. // root url, not set to disable sending URL to client (replacement for old "fileURL" option)
  143. 'URL' => '',
  144. // directory separator. required by client to show paths correctly
  145. 'separator' => DIRECTORY_SEPARATOR,
  146. // library to crypt/uncrypt files names (not implemented)
  147. 'cryptLib' => '',
  148. // how to detect files mimetypes. (auto/internal/finfo/mime_content_type)
  149. 'mimeDetect' => 'auto',
  150. // mime.types file path (for mimeDetect==internal)
  151. 'mimefile' => '',
  152. // directory for thumbnails
  153. 'tmbPath' => '.tmb',
  154. // mode to create thumbnails dir
  155. 'tmbPathMode' => 0777,
  156. // thumbnails dir URL. Set it if store thumbnails outside root directory
  157. 'tmbURL' => '',
  158. // thumbnails size (px)
  159. 'tmbSize' => 48,
  160. // thumbnails crop (true - crop, false - scale image to fit thumbnail size)
  161. 'tmbCrop' => true,
  162. // thumbnails background color (hex #rrggbb or 'transparent')
  163. 'tmbBgColor' => '#ffffff',
  164. // image manipulations library
  165. 'imgLib' => 'auto',
  166. // on paste file - if true - old file will be replaced with new one, if false new file get name - original_name-number.ext
  167. 'copyOverwrite' => true,
  168. // if true - join new and old directories content on paste
  169. 'copyJoin' => true,
  170. // on upload - if true - old file will be replaced with new one, if false new file get name - original_name-number.ext
  171. 'uploadOverwrite' => true,
  172. // mimetypes allowed to upload
  173. 'uploadAllow' => array(),
  174. // mimetypes not allowed to upload
  175. 'uploadDeny' => array(),
  176. // order to proccess uploadAllow and uploadDeny options
  177. 'uploadOrder' => array('deny', 'allow'),
  178. // maximum upload file size. NOTE - this is size for every uploaded files
  179. 'uploadMaxSize' => 0,
  180. // files dates format
  181. 'dateFormat' => 'j M Y H:i',
  182. // files time format
  183. 'timeFormat' => 'H:i',
  184. // if true - every folder will be check for children folders, otherwise all folders will be marked as having subfolders
  185. 'checkSubfolders' => true,
  186. // allow to copy from this volume to other ones?
  187. 'copyFrom' => true,
  188. // allow to copy from other volumes to this one?
  189. 'copyTo' => true,
  190. // list of commands disabled on this root
  191. 'disabled' => array(),
  192. // regexp or function name to validate new file name
  193. 'acceptedName' => '/^[^\.].*/', //<-- DONT touch this! Use constructor options to overwrite it!
  194. // function/class method to control files permissions
  195. 'accessControl' => null,
  196. // some data required by access control
  197. 'accessControlData' => null,
  198. // default permissions. not set hidden/locked here - take no effect
  199. 'defaults' => array(
  200. 'read' => true,
  201. 'write' => true
  202. ),
  203. // files attributes
  204. 'attributes' => array(),
  205. // Allowed archive's mimetypes to create. Leave empty for all available types.
  206. 'archiveMimes' => array(),
  207. // Manual config for archivers. See example below. Leave empty for auto detect
  208. 'archivers' => array(),
  209. // plugin settings
  210. 'plugin' => array(),
  211. // required to fix bug on macos
  212. 'utf8fix' => false,
  213. // й ё Й Ё Ø Å
  214. 'utf8patterns' => array("\u0438\u0306", "\u0435\u0308", "\u0418\u0306", "\u0415\u0308", "\u00d8A", "\u030a"),
  215. 'utf8replace' => array("\u0439", "\u0451", "\u0419", "\u0401", "\u00d8", "\u00c5")
  216. );
  217. /**
  218. * Defaults permissions
  219. *
  220. * @var array
  221. **/
  222. protected $defaults = array(
  223. 'read' => true,
  224. 'write' => true,
  225. 'locked' => false,
  226. 'hidden' => false
  227. );
  228. /**
  229. * Access control function/class
  230. *
  231. * @var mixed
  232. **/
  233. protected $attributes = array();
  234. /**
  235. * Access control function/class
  236. *
  237. * @var mixed
  238. **/
  239. protected $access = null;
  240. /**
  241. * Mime types allowed to upload
  242. *
  243. * @var array
  244. **/
  245. protected $uploadAllow = array();
  246. /**
  247. * Mime types denied to upload
  248. *
  249. * @var array
  250. **/
  251. protected $uploadDeny = array();
  252. /**
  253. * Order to validate uploadAllow and uploadDeny
  254. *
  255. * @var array
  256. **/
  257. protected $uploadOrder = array();
  258. /**
  259. * Maximum allowed upload file size.
  260. * Set as number or string with unit - "10M", "500K", "1G"
  261. *
  262. * @var int|string
  263. **/
  264. protected $uploadMaxSize = 0;
  265. /**
  266. * Mimetype detect method
  267. *
  268. * @var string
  269. **/
  270. protected $mimeDetect = 'auto';
  271. /**
  272. * Flag - mimetypes from externail file was loaded
  273. *
  274. * @var bool
  275. **/
  276. private static $mimetypesLoaded = false;
  277. /**
  278. * Finfo object for mimeDetect == 'finfo'
  279. *
  280. * @var object
  281. **/
  282. protected $finfo = null;
  283. /**
  284. * List of disabled client's commands
  285. *
  286. * @var array
  287. **/
  288. protected $diabled = array();
  289. /**
  290. * default extensions/mimetypes for mimeDetect == 'internal'
  291. *
  292. * @var array
  293. **/
  294. protected static $mimetypes = array(
  295. // applications
  296. 'ai' => 'application/postscript',
  297. 'eps' => 'application/postscript',
  298. 'exe' => 'application/x-executable',
  299. 'doc' => 'application/vnd.ms-word',
  300. 'xls' => 'application/vnd.ms-excel',
  301. 'ppt' => 'application/vnd.ms-powerpoint',
  302. 'pps' => 'application/vnd.ms-powerpoint',
  303. 'pdf' => 'application/pdf',
  304. 'xml' => 'application/xml',
  305. 'swf' => 'application/x-shockwave-flash',
  306. 'torrent' => 'application/x-bittorrent',
  307. 'jar' => 'application/x-jar',
  308. // open office (finfo detect as application/zip)
  309. 'odt' => 'application/vnd.oasis.opendocument.text',
  310. 'ott' => 'application/vnd.oasis.opendocument.text-template',
  311. 'oth' => 'application/vnd.oasis.opendocument.text-web',
  312. 'odm' => 'application/vnd.oasis.opendocument.text-master',
  313. 'odg' => 'application/vnd.oasis.opendocument.graphics',
  314. 'otg' => 'application/vnd.oasis.opendocument.graphics-template',
  315. 'odp' => 'application/vnd.oasis.opendocument.presentation',
  316. 'otp' => 'application/vnd.oasis.opendocument.presentation-template',
  317. 'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
  318. 'ots' => 'application/vnd.oasis.opendocument.spreadsheet-template',
  319. 'odc' => 'application/vnd.oasis.opendocument.chart',
  320. 'odf' => 'application/vnd.oasis.opendocument.formula',
  321. 'odb' => 'application/vnd.oasis.opendocument.database',
  322. 'odi' => 'application/vnd.oasis.opendocument.image',
  323. 'oxt' => 'application/vnd.openofficeorg.extension',
  324. // MS office 2007 (finfo detect as application/zip)
  325. 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  326. 'docm' => 'application/vnd.ms-word.document.macroEnabled.12',
  327. 'dotx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
  328. 'dotm' => 'application/vnd.ms-word.template.macroEnabled.12',
  329. 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  330. 'xlsm' => 'application/vnd.ms-excel.sheet.macroEnabled.12',
  331. 'xltx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template',
  332. 'xltm' => 'application/vnd.ms-excel.template.macroEnabled.12',
  333. 'xlsb' => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12',
  334. 'xlam' => 'application/vnd.ms-excel.addin.macroEnabled.12',
  335. 'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
  336. 'pptm' => 'application/vnd.ms-powerpoint.presentation.macroEnabled.12',
  337. 'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
  338. 'ppsm' => 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12',
  339. 'potx' => 'application/vnd.openxmlformats-officedocument.presentationml.template',
  340. 'potm' => 'application/vnd.ms-powerpoint.template.macroEnabled.12',
  341. 'ppam' => 'application/vnd.ms-powerpoint.addin.macroEnabled.12',
  342. 'sldx' => 'application/vnd.openxmlformats-officedocument.presentationml.slide',
  343. 'sldm' => 'application/vnd.ms-powerpoint.slide.macroEnabled.12',
  344. // archives
  345. 'gz' => 'application/x-gzip',
  346. 'tgz' => 'application/x-gzip',
  347. 'bz' => 'application/x-bzip2',
  348. 'bz2' => 'application/x-bzip2',
  349. 'tbz' => 'application/x-bzip2',
  350. 'zip' => 'application/zip',
  351. 'rar' => 'application/x-rar',
  352. 'tar' => 'application/x-tar',
  353. '7z' => 'application/x-7z-compressed',
  354. // texts
  355. 'txt' => 'text/plain',
  356. 'php' => 'text/x-php',
  357. 'html' => 'text/html',
  358. 'htm' => 'text/html',
  359. 'js' => 'text/javascript',
  360. 'css' => 'text/css',
  361. 'rtf' => 'text/rtf',
  362. 'rtfd' => 'text/rtfd',
  363. 'py' => 'text/x-python',
  364. 'java' => 'text/x-java-source',
  365. 'rb' => 'text/x-ruby',
  366. 'sh' => 'text/x-shellscript',
  367. 'pl' => 'text/x-perl',
  368. 'xml' => 'text/xml',
  369. 'sql' => 'text/x-sql',
  370. 'c' => 'text/x-csrc',
  371. 'h' => 'text/x-chdr',
  372. 'cpp' => 'text/x-c++src',
  373. 'hh' => 'text/x-c++hdr',
  374. 'log' => 'text/plain',
  375. 'csv' => 'text/x-comma-separated-values',
  376. // images
  377. 'bmp' => 'image/x-ms-bmp',
  378. 'jpg' => 'image/jpeg',
  379. 'jpeg' => 'image/jpeg',
  380. 'gif' => 'image/gif',
  381. 'png' => 'image/png',
  382. 'tif' => 'image/tiff',
  383. 'tiff' => 'image/tiff',
  384. 'tga' => 'image/x-targa',
  385. 'psd' => 'image/vnd.adobe.photoshop',
  386. 'ai' => 'image/vnd.adobe.photoshop',
  387. 'xbm' => 'image/xbm',
  388. 'pxm' => 'image/pxm',
  389. //audio
  390. 'mp3' => 'audio/mpeg',
  391. 'mid' => 'audio/midi',
  392. 'ogg' => 'audio/ogg',
  393. 'oga' => 'audio/ogg',
  394. 'm4a' => 'audio/x-m4a',
  395. 'wav' => 'audio/wav',
  396. 'wma' => 'audio/x-ms-wma',
  397. // video
  398. 'avi' => 'video/x-msvideo',
  399. 'dv' => 'video/x-dv',
  400. 'mp4' => 'video/mp4',
  401. 'mpeg' => 'video/mpeg',
  402. 'mpg' => 'video/mpeg',
  403. 'mov' => 'video/quicktime',
  404. 'wm' => 'video/x-ms-wmv',
  405. 'flv' => 'video/x-flv',
  406. 'mkv' => 'video/x-matroska',
  407. 'webm' => 'video/webm',
  408. 'ogv' => 'video/ogg',
  409. 'ogm' => 'video/ogg'
  410. );
  411. /**
  412. * Directory separator - required by client
  413. *
  414. * @var string
  415. **/
  416. protected $separator = DIRECTORY_SEPARATOR;
  417. /**
  418. * Mimetypes allowed to display
  419. *
  420. * @var array
  421. **/
  422. protected $onlyMimes = array();
  423. /**
  424. * Store files moved or overwrited files info
  425. *
  426. * @var array
  427. **/
  428. protected $removed = array();
  429. /**
  430. * Cache storage
  431. *
  432. * @var array
  433. **/
  434. protected $cache = array();
  435. /**
  436. * Cache by folders
  437. *
  438. * @var array
  439. **/
  440. protected $dirsCache = array();
  441. /*********************************************************************/
  442. /* INITIALIZATION */
  443. /*********************************************************************/
  444. /**
  445. * Prepare driver before mount volume.
  446. * Return true if volume is ready.
  447. *
  448. * @return bool
  449. * @author Dmitry (dio) Levashov
  450. **/
  451. protected function init() {
  452. return true;
  453. }
  454. /**
  455. * Configure after successfull mount.
  456. * By default set thumbnails path and image manipulation library.
  457. *
  458. * @return void
  459. * @author Dmitry (dio) Levashov
  460. **/
  461. protected function configure() {
  462. // set thumbnails path
  463. $path = $this->options['tmbPath'];
  464. if ($path) {
  465. if (!file_exists($path)) {
  466. if (@mkdir($path)) {
  467. chmod($path, $this->options['tmbPathMode']);
  468. } else {
  469. $path = '';
  470. }
  471. }
  472. if (is_dir($path) && is_readable($path)) {
  473. $this->tmbPath = $path;
  474. $this->tmbPathWritable = is_writable($path);
  475. }
  476. }
  477. // set image manipulation library
  478. $type = preg_match('/^(imagick|gd|auto)$/i', $this->options['imgLib'])
  479. ? strtolower($this->options['imgLib'])
  480. : 'auto';
  481. if (($type == 'imagick' || $type == 'auto') && extension_loaded('imagick')) {
  482. $this->imgLib = 'imagick';
  483. } else {
  484. $this->imgLib = function_exists('gd_info') ? 'gd' : '';
  485. }
  486. }
  487. /*********************************************************************/
  488. /* PUBLIC API */
  489. /*********************************************************************/
  490. /**
  491. * Return driver id. Used as a part of volume id.
  492. *
  493. * @return string
  494. * @author Dmitry (dio) Levashov
  495. **/
  496. public function driverId() {
  497. return $this->driverId;
  498. }
  499. /**
  500. * Return volume id
  501. *
  502. * @return string
  503. * @author Dmitry (dio) Levashov
  504. **/
  505. public function id() {
  506. return $this->id;
  507. }
  508. /**
  509. * Return debug info for client
  510. *
  511. * @return array
  512. * @author Dmitry (dio) Levashov
  513. **/
  514. public function debug() {
  515. return array(
  516. 'id' => $this->id(),
  517. 'name' => strtolower(substr(get_class($this), strlen('elfinderdriver'))),
  518. 'mimeDetect' => $this->mimeDetect,
  519. 'imgLib' => $this->imgLib
  520. );
  521. }
  522. /**
  523. * "Mount" volume.
  524. * Return true if volume available for read or write,
  525. * false - otherwise
  526. *
  527. * @return bool
  528. * @author Dmitry (dio) Levashov
  529. * @author Alexey Sukhotin
  530. **/
  531. public function mount(array $opts) {
  532. if (!isset($opts['path']) || $opts['path'] === '') {
  533. return $this->setError('Path undefined.');;
  534. }
  535. $this->options = array_merge($this->options, $opts);
  536. $this->id = $this->driverId.(!empty($this->options['id']) ? $this->options['id'] : elFinder::$volumesCnt++).'_';
  537. $this->root = $this->_normpath($this->options['path']);
  538. $this->separator = isset($this->options['separator']) ? $this->options['separator'] : DIRECTORY_SEPARATOR;
  539. // default file attribute
  540. $this->defaults = array(
  541. 'read' => isset($this->options['defaults']['read']) ? !!$this->options['defaults']['read'] : true,
  542. 'write' => isset($this->options['defaults']['write']) ? !!$this->options['defaults']['write'] : true,
  543. 'locked' => false,
  544. 'hidden' => false
  545. );
  546. // root attributes
  547. $this->attributes[] = array(
  548. 'pattern' => '~^'.preg_quote(DIRECTORY_SEPARATOR).'$~',
  549. 'locked' => true,
  550. 'hidden' => false
  551. );
  552. // set files attributes
  553. if (!empty($this->options['attributes']) && is_array($this->options['attributes'])) {
  554. foreach ($this->options['attributes'] as $a) {
  555. // attributes must contain pattern and at least one rule
  556. if (!empty($a['pattern']) || count($a) > 1) {
  557. $this->attributes[] = $a;
  558. }
  559. }
  560. }
  561. if (!empty($this->options['accessControl']) && is_callable($this->options['accessControl'])) {
  562. $this->access = $this->options['accessControl'];
  563. }
  564. $this->today = mktime(0,0,0, date('m'), date('d'), date('Y'));
  565. $this->yesterday = $this->today-86400;
  566. // debug($this->attributes);
  567. if (!$this->init()) {
  568. return false;
  569. }
  570. // check some options is arrays
  571. $this->uploadAllow = isset($this->options['uploadAllow']) && is_array($this->options['uploadAllow'])
  572. ? $this->options['uploadAllow']
  573. : array();
  574. $this->uploadDeny = isset($this->options['uploadDeny']) && is_array($this->options['uploadDeny'])
  575. ? $this->options['uploadDeny']
  576. : array();
  577. if (is_string($this->options['uploadOrder'])) { // telephat_mode on, compatibility with 1.x
  578. $parts = explode(',', isset($this->options['uploadOrder']) ? $this->options['uploadOrder'] : 'deny,allow');
  579. $this->uploadOrder = array(trim($parts[0]), trim($parts[1]));
  580. } else { // telephat_mode off
  581. $this->uploadOrder = $this->options['uploadOrder'];
  582. }
  583. if (!empty($this->options['uploadMaxSize'])) {
  584. $size = ''.$this->options['uploadMaxSize'];
  585. $unit = strtolower(substr($size, strlen($size) - 1));
  586. $n = 1;
  587. switch ($unit) {
  588. case 'k':
  589. $n = 1024;
  590. break;
  591. case 'm':
  592. $n = 1048576;
  593. break;
  594. case 'g':
  595. $n = 1073741824;
  596. }
  597. $this->uploadMaxSize = intval($size)*$n;
  598. }
  599. $this->disabled = isset($this->options['disabled']) && is_array($this->options['disabled'])
  600. ? $this->options['disabled']
  601. : array();
  602. $this->cryptLib = $this->options['cryptLib'];
  603. $this->mimeDetect = $this->options['mimeDetect'];
  604. // find available mimetype detect method
  605. $type = strtolower($this->options['mimeDetect']);
  606. $type = preg_match('/^(finfo|mime_content_type|internal|auto)$/i', $type) ? $type : 'auto';
  607. $regexp = '/text\/x\-(php|c\+\+)/';
  608. if (($type == 'finfo' || $type == 'auto')
  609. && class_exists('finfo')) {
  610. $tmpFileInfo = @explode(';', @finfo_file(finfo_open(FILEINFO_MIME), __FILE__));
  611. } else {
  612. $tmpFileInfo = false;
  613. }
  614. if ($tmpFileInfo && preg_match($regexp, array_shift($tmpFileInfo))) {
  615. $type = 'finfo';
  616. $this->finfo = finfo_open(FILEINFO_MIME);
  617. } elseif (($type == 'mime_content_type' || $type == 'auto')
  618. && function_exists('mime_content_type')
  619. && preg_match($regexp, array_shift(explode(';', mime_content_type(__FILE__))))) {
  620. $type = 'mime_content_type';
  621. } else {
  622. $type = 'internal';
  623. }
  624. $this->mimeDetect = $type;
  625. // load mimes from external file for mimeDetect == 'internal'
  626. // based on Alexey Sukhotin idea and patch: http://elrte.org/redmine/issues/163
  627. // file must be in file directory or in parent one
  628. if ($this->mimeDetect == 'internal' && !self::$mimetypesLoaded) {
  629. self::$mimetypesLoaded = true;
  630. $this->mimeDetect = 'internal';
  631. $file = false;
  632. if (!empty($this->options['mimefile']) && file_exists($this->options['mimefile'])) {
  633. $file = $this->options['mimefile'];
  634. } elseif (file_exists(dirname(__FILE__).DIRECTORY_SEPARATOR.'mime.types')) {
  635. $file = dirname(__FILE__).DIRECTORY_SEPARATOR.'mime.types';
  636. } elseif (file_exists(dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'mime.types')) {
  637. $file = dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'mime.types';
  638. }
  639. if ($file && file_exists($file)) {
  640. $mimecf = file($file);
  641. foreach ($mimecf as $line_num => $line) {
  642. if (!preg_match('/^\s*#/', $line)) {
  643. $mime = preg_split('/\s+/', $line, -1, PREG_SPLIT_NO_EMPTY);
  644. for ($i = 1, $size = count($mime); $i < $size ; $i++) {
  645. if (!isset(self::$mimetypes[$mime[$i]])) {
  646. self::$mimetypes[$mime[$i]] = $mime[0];
  647. }
  648. }
  649. }
  650. }
  651. }
  652. }
  653. $this->rootName = empty($this->options['alias']) ? $this->_basename($this->root) : $this->options['alias'];
  654. $root = $this->stat($this->root);
  655. if (!$root) {
  656. return $this->setError('Root folder does not exists.');
  657. }
  658. if (!$root['read'] && !$root['write']) {
  659. return $this->setError('Root folder has not read and write permissions.');
  660. }
  661. // debug($root);
  662. if ($root['read']) {
  663. // check startPath - path to open by default instead of root
  664. if ($this->options['startPath']) {
  665. $start = $this->stat($this->options['startPath']);
  666. if (!empty($start)
  667. && $start['mime'] == 'directory'
  668. && $start['read']
  669. && empty($start['hidden'])
  670. && $this->_inpath($this->options['startPath'], $this->root)) {
  671. $this->startPath = $this->options['startPath'];
  672. if (substr($this->startPath, -1, 1) == $this->options['separator']) {
  673. $this->startPath = substr($this->startPath, 0, -1);
  674. }
  675. }
  676. }
  677. } else {
  678. $this->options['URL'] = '';
  679. $this->options['tmbURL'] = '';
  680. $this->options['tmbPath'] = '';
  681. // read only volume
  682. array_unshift($this->attributes, array(
  683. 'pattern' => '/.*/',
  684. 'read' => false
  685. ));
  686. }
  687. $this->treeDeep = $this->options['treeDeep'] > 0 ? (int)$this->options['treeDeep'] : 1;
  688. $this->tmbSize = $this->options['tmbSize'] > 0 ? (int)$this->options['tmbSize'] : 48;
  689. $this->URL = $this->options['URL'];
  690. if ($this->URL && preg_match("|[^/?&=]$|", $this->URL)) {
  691. $this->URL .= '/';
  692. }
  693. $this->tmbURL = !empty($this->options['tmbURL']) ? $this->options['tmbURL'] : '';
  694. if ($this->tmbURL && preg_match("|[^/?&=]$|", $this->tmbURL)) {
  695. $this->tmbURL .= '/';
  696. }
  697. $this->nameValidator = is_string($this->options['acceptedName']) && !empty($this->options['acceptedName'])
  698. ? $this->options['acceptedName']
  699. : '';
  700. $this->_checkArchivers();
  701. // manual control archive types to create
  702. if (!empty($this->options['archiveMimes']) && is_array($this->options['archiveMimes'])) {
  703. foreach ($this->archivers['create'] as $mime => $v) {
  704. if (!in_array($mime, $this->options['archiveMimes'])) {
  705. unset($this->archivers['create'][$mime]);
  706. }
  707. }
  708. }
  709. // manualy add archivers
  710. if (!empty($this->options['archivers']['create']) && is_array($this->options['archivers']['create'])) {
  711. foreach ($this->options['archivers']['create'] as $mime => $conf) {
  712. if (strpos($mime, 'application/') === 0
  713. && !empty($conf['cmd'])
  714. && isset($conf['argc'])
  715. && !empty($conf['ext'])
  716. && !isset($this->archivers['create'][$mime])) {
  717. $this->archivers['create'][$mime] = $conf;
  718. }
  719. }
  720. }
  721. if (!empty($this->options['archivers']['extract']) && is_array($this->options['archivers']['extract'])) {
  722. foreach ($this->options['archivers']['extract'] as $mime => $conf) {
  723. if (strpos($mime, 'application/') === 0
  724. && !empty($conf['cmd'])
  725. && isset($conf['argc'])
  726. && !empty($conf['ext'])
  727. && !isset($this->archivers['extract'][$mime])) {
  728. $this->archivers['extract'][$mime] = $conf;
  729. }
  730. }
  731. }
  732. $this->configure();
  733. // echo $this->uploadMaxSize;
  734. // echo $this->options['uploadMaxSize'];
  735. return $this->mounted = true;
  736. }
  737. /**
  738. * Some "unmount" stuffs - may be required by virtual fs
  739. *
  740. * @return void
  741. * @author Dmitry (dio) Levashov
  742. **/
  743. public function umount() {
  744. }
  745. /**
  746. * Return error message from last failed action
  747. *
  748. * @return array
  749. * @author Dmitry (dio) Levashov
  750. **/
  751. public function error() {
  752. return $this->error;
  753. }
  754. /**
  755. * Return Extention/MIME Table (elFinderVolumeDriver::$mimetypes)
  756. *
  757. * @return array
  758. * @author Naoki Sawada
  759. */
  760. public function getMimeTable() {
  761. return elFinderVolumeDriver::$mimetypes;
  762. }
  763. /**
  764. * Set mimetypes allowed to display to client
  765. *
  766. * @param array $mimes
  767. * @return void
  768. * @author Dmitry (dio) Levashov
  769. **/
  770. public function setMimesFilter($mimes) {
  771. if (is_array($mimes)) {
  772. $this->onlyMimes = $mimes;
  773. }
  774. }
  775. /**
  776. * Return root folder hash
  777. *
  778. * @return string
  779. * @author Dmitry (dio) Levashov
  780. **/
  781. public function root() {
  782. return $this->encode($this->root);
  783. }
  784. /**
  785. * Return root or startPath hash
  786. *
  787. * @return string
  788. * @author Dmitry (dio) Levashov
  789. **/
  790. public function defaultPath() {
  791. return $this->encode($this->startPath ? $this->startPath : $this->root);
  792. }
  793. /**
  794. * Return volume options required by client:
  795. *
  796. * @return array
  797. * @author Dmitry (dio) Levashov
  798. **/
  799. public function options($hash) {
  800. return array(
  801. 'path' => $this->_path($this->decode($hash)),
  802. 'url' => $this->URL,
  803. 'tmbUrl' => $this->tmbURL,
  804. 'disabled' => $this->disabled,
  805. 'separator' => $this->separator,
  806. 'copyOverwrite' => intval($this->options['copyOverwrite']),
  807. 'uploadMaxSize' => intval($this->uploadMaxSize),
  808. 'archivers' => array(
  809. // 'create' => array_keys($this->archivers['create']),
  810. // 'extract' => array_keys($this->archivers['extract']),
  811. 'create' => is_array($this->archivers['create']) ? array_keys($this->archivers['create']) : array(),
  812. 'extract' => is_array($this->archivers['extract']) ? array_keys($this->archivers['extract']) : array()
  813. )
  814. );
  815. }
  816. /**
  817. * Get plugin values of this options
  818. *
  819. * @param string $name Plugin name
  820. * @return NULL|array Plugin values
  821. * @author Naoki Sawada
  822. */
  823. public function getOptionsPlugin($name = '') {
  824. if ($name) {
  825. return isset($this->options['plugin'][$name])? $this->options['plugin'][$name] : array();
  826. } else {
  827. return $this->options['plugin'];
  828. }
  829. }
  830. /**
  831. * Return true if command disabled in options
  832. *
  833. * @param string $cmd command name
  834. * @return bool
  835. * @author Dmitry (dio) Levashov
  836. **/
  837. public function commandDisabled($cmd) {
  838. return in_array($cmd, $this->disabled);
  839. }
  840. /**
  841. * Return true if mime is required mimes list
  842. *
  843. * @param string $mime mime type to check
  844. * @param array $mimes allowed mime types list or not set to use client mimes list
  845. * @param bool|null $empty what to return on empty list
  846. * @return bool|null
  847. * @author Dmitry (dio) Levashov
  848. * @author Troex Nevelin
  849. **/
  850. public function mimeAccepted($mime, $mimes = array(), $empty = true) {
  851. $mimes = !empty($mimes) ? $mimes : $this->onlyMimes;
  852. if (empty($mimes)) {
  853. return $empty;
  854. }
  855. return $mime == 'directory'
  856. || in_array('all', $mimes)
  857. || in_array('All', $mimes)
  858. || in_array($mime, $mimes)
  859. || in_array(substr($mime, 0, strpos($mime, '/')), $mimes);
  860. }
  861. /**
  862. * Return true if voume is readable.
  863. *
  864. * @return bool
  865. * @author Dmitry (dio) Levashov
  866. **/
  867. public function isReadable() {
  868. $stat = $this->stat($this->root);
  869. return $stat['read'];
  870. }
  871. /**
  872. * Return true if copy from this volume allowed
  873. *
  874. * @return bool
  875. * @author Dmitry (dio) Levashov
  876. **/
  877. public function copyFromAllowed() {
  878. return !!$this->options['copyFrom'];
  879. }
  880. /**
  881. * Return file path related to root
  882. *
  883. * @param string $hash file hash
  884. * @return string
  885. * @author Dmitry (dio) Levashov
  886. **/
  887. public function path($hash) {
  888. return $this->_path($this->decode($hash));
  889. }
  890. /**
  891. * Return file real path if file exists
  892. *
  893. * @param string $hash file hash
  894. * @return string
  895. * @author Dmitry (dio) Levashov
  896. **/
  897. public function realpath($hash) {
  898. $path = $this->decode($hash);
  899. return $this->stat($path) ? $path : false;
  900. }
  901. /**
  902. * Return list of moved/overwrited files
  903. *
  904. * @return array
  905. * @author Dmitry (dio) Levashov
  906. **/
  907. public function removed() {
  908. return $this->removed;
  909. }
  910. /**
  911. * Clean removed files list
  912. *
  913. * @return void
  914. * @author Dmitry (dio) Levashov
  915. **/
  916. public function resetRemoved() {
  917. $this->removed = array();
  918. }
  919. /**
  920. * Return file/dir hash or first founded child hash with required attr == $val
  921. *
  922. * @param string $hash file hash
  923. * @param string $attr attribute name
  924. * @param bool $val attribute value
  925. * @return string|false
  926. * @author Dmitry (dio) Levashov
  927. **/
  928. public function closest($hash, $attr, $val) {
  929. return ($path = $this->closestByAttr($this->decode($hash), $attr, $val)) ? $this->encode($path) : false;
  930. }
  931. /**
  932. * Return file info or false on error
  933. *
  934. * @param string $hash file hash
  935. * @param bool $realpath add realpath field to file info
  936. * @return array|false
  937. * @author Dmitry (dio) Levashov
  938. **/
  939. public function file($hash) {
  940. $path = $this->decode($hash);
  941. return ($file = $this->stat($path)) ? $file : $this->setError(elFinder::ERROR_FILE_NOT_FOUND);
  942. if (($file = $this->stat($path)) != false) {
  943. if ($realpath) {
  944. $file['realpath'] = $path;
  945. }
  946. return $file;
  947. }
  948. return $this->setError(elFinder::ERROR_FILE_NOT_FOUND);
  949. }
  950. /**
  951. * Return folder info
  952. *
  953. * @param string $hash folder hash
  954. * @param bool $hidden return hidden file info
  955. * @return array|false
  956. * @author Dmitry (dio) Levashov
  957. **/
  958. public function dir($hash, $resolveLink=false) {
  959. if (($dir = $this->file($hash)) == false) {
  960. return $this->setError(elFinder::ERROR_DIR_NOT_FOUND);
  961. }
  962. if ($resolveLink && !empty($dir['thash'])) {
  963. $dir = $this->file($dir['thash']);
  964. }
  965. return $dir && $dir['mime'] == 'directory' && empty($dir['hidden'])
  966. ? $dir
  967. : $this->setError(elFinder::ERROR_NOT_DIR);
  968. }
  969. /**
  970. * Return directory content or false on error
  971. *
  972. * @param string $hash file hash
  973. * @return array|false
  974. * @author Dmitry (dio) Levashov
  975. **/
  976. public function scandir($hash) {
  977. if (($dir = $this->dir($hash)) == false) {
  978. return false;
  979. }
  980. return $dir['read']
  981. ? $this->getScandir($this->decode($hash))
  982. : $this->setError(elFinder::ERROR_PERM_DENIED);
  983. }
  984. /**
  985. * Return dir files names list
  986. *
  987. * @param string $hash file hash
  988. * @return array
  989. * @author Dmitry (dio) Levashov
  990. **/
  991. public function ls($hash) {
  992. if (($dir = $this->dir($hash)) == false || !$dir['read']) {
  993. return false;
  994. }
  995. $list = array();
  996. $path = $this->decode($hash);
  997. foreach ($this->getScandir($path) as $stat) {
  998. if (empty($stat['hidden']) && $this->mimeAccepted($stat['mime'])) {
  999. $list[] = $stat['name'];
  1000. }
  1001. }
  1002. return $list;
  1003. }
  1004. /**
  1005. * Return subfolders for required folder or false on error
  1006. *
  1007. * @param string $hash folder hash or empty string to get tree from root folder
  1008. * @param int $deep subdir deep
  1009. * @param string $exclude dir hash which subfolders must be exluded from result, required to not get stat twice on cwd subfolders
  1010. * @return array|false
  1011. * @author Dmitry (dio) Levashov
  1012. **/
  1013. public function tree($hash='', $deep=0, $exclude='') {
  1014. $path = $hash ? $this->decode($hash) : $this->root;
  1015. if (($dir = $this->stat($path)) == false || $dir['mime'] != 'directory') {
  1016. return false;
  1017. }
  1018. $dirs = $this->gettree($path, $deep > 0 ? $deep -1 : $this->treeDeep-1, $exclude ? $this->decode($exclude) : null);
  1019. array_unshift($dirs, $dir);
  1020. return $dirs;
  1021. }
  1022. /**
  1023. * Return part of dirs tree from required dir up to root dir
  1024. *
  1025. * @param string $hash directory hash
  1026. * @return array
  1027. * @author Dmitry (dio) Levashov
  1028. **/
  1029. public function parents($hash) {
  1030. if (($current = $this->dir($hash)) == false) {
  1031. return false;
  1032. }
  1033. $path = $this->decode($hash);
  1034. $tree = array();
  1035. while ($path && $path != $this->root) {
  1036. $path = $this->_dirname($path);
  1037. $stat = $this->stat($path);
  1038. if (!empty($stat['hidden']) || !$stat['read']) {
  1039. return false;
  1040. }
  1041. array_unshift($tree, $stat);
  1042. if ($path != $this->root) {
  1043. foreach ($this->gettree($path, 0) as $dir) {
  1044. if (!in_array($dir, $tree)) {
  1045. $tree[] = $dir;
  1046. }
  1047. }
  1048. }
  1049. }
  1050. return $tree ? $tree : array($current);
  1051. }
  1052. /**
  1053. * Create thumbnail for required file and return its name of false on failed
  1054. *
  1055. * @return string|false
  1056. * @author Dmitry (dio) Levashov
  1057. **/
  1058. public function tmb($hash) {
  1059. $path = $this->decode($hash);
  1060. $stat = $this->stat($path);
  1061. if (isset($stat['tmb'])) {
  1062. return $stat['tmb'] == "1" ? $this->createTmb($path, $stat) : $stat['tmb'];
  1063. }
  1064. return false;
  1065. }
  1066. /**
  1067. * Return file size / total directory size
  1068. *
  1069. * @param string file hash
  1070. * @return int
  1071. * @author Dmitry (dio) Levashov
  1072. **/
  1073. public function size($hash) {
  1074. return $this->countSize($this->decode($hash));
  1075. }
  1076. /**
  1077. * Open file for reading and return file pointer
  1078. *
  1079. * @param string file hash
  1080. * @return Resource
  1081. * @author Dmitry (dio) Levashov
  1082. **/
  1083. public function open($hash) {
  1084. if (($file = $this->file($hash)) == false
  1085. || $file['mime'] == 'directory') {
  1086. return false;
  1087. }
  1088. return $this->_fopen($this->decode($hash), 'rb');
  1089. }
  1090. /**
  1091. * Close file pointer
  1092. *
  1093. * @param Resource $fp file pointer
  1094. * @param string $hash file hash
  1095. * @return void
  1096. * @author Dmitry (dio) Levashov
  1097. **/
  1098. public function close($fp, $hash) {
  1099. $this->_fclose($fp, $this->decode($hash));
  1100. }
  1101. /**
  1102. * Create directory and return dir info
  1103. *
  1104. * @param string $dst destination directory
  1105. * @param string $name directory name
  1106. * @return array|false
  1107. * @author Dmitry (dio) Levashov
  1108. **/
  1109. public function mkdir($dst, $name) {
  1110. if ($this->commandDisabled('mkdir')) {
  1111. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1112. }
  1113. if (!$this->nameAccepted($name)) {
  1114. return $this->setError(elFinder::ERROR_INVALID_NAME);
  1115. }
  1116. if (($dir = $this->dir($dst)) == false) {
  1117. return $this->setError(elFinder::ERROR_TRGDIR_NOT_FOUND, '#'.$dst);
  1118. }
  1119. if (!$dir['write']) {
  1120. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1121. }
  1122. $path = $this->decode($dst);
  1123. $dst = $this->_joinPath($path, $name);
  1124. $stat = $this->stat($dst);
  1125. if (!empty($stat)) {
  1126. return $this->setError(elFinder::ERROR_EXISTS, $name);
  1127. }
  1128. $this->clearcache();
  1129. return ($path = $this->_mkdir($path, $name)) ? $this->stat($path) : false;
  1130. }
  1131. /**
  1132. * Create empty file and return its info
  1133. *
  1134. * @param string $dst destination directory
  1135. * @param string $name file name
  1136. * @return array|false
  1137. * @author Dmitry (dio) Levashov
  1138. **/
  1139. public function mkfile($dst, $name) {
  1140. if ($this->commandDisabled('mkfile')) {
  1141. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1142. }
  1143. if (!$this->nameAccepted($name)) {
  1144. return $this->setError(elFinder::ERROR_INVALID_NAME);
  1145. }
  1146. if (($dir = $this->dir($dst)) == false) {
  1147. return $this->setError(elFinder::ERROR_TRGDIR_NOT_FOUND, '#'.$dst);
  1148. }
  1149. $path = $this->decode($dst);
  1150. if (!$dir['write'] || !$this->allowCreate($path, $name)) {
  1151. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1152. }
  1153. if ($this->stat($this->_joinPath($path, $name))) {
  1154. return $this->setError(elFinder::ERROR_EXISTS, $name);
  1155. }
  1156. $this->clearcache();
  1157. return ($path = $this->_mkfile($path, $name)) ? $this->stat($path) : false;
  1158. }
  1159. /**
  1160. * Rename file and return file info
  1161. *
  1162. * @param string $hash file hash
  1163. * @param string $name new file name
  1164. * @return array|false
  1165. * @author Dmitry (dio) Levashov
  1166. **/
  1167. public function rename($hash, $name) {
  1168. if ($this->commandDisabled('rename')) {
  1169. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1170. }
  1171. if (!$this->nameAccepted($name)) {
  1172. return $this->setError(elFinder::ERROR_INVALID_NAME, $name);
  1173. }
  1174. if (!($file = $this->file($hash))) {
  1175. return $this->setError(elFinder::ERROR_FILE_NOT_FOUND);
  1176. }
  1177. if ($name == $file['name']) {
  1178. return $file;
  1179. }
  1180. if (!empty($file['locked'])) {
  1181. return $this->setError(elFinder::ERROR_LOCKED, $file['name']);
  1182. }
  1183. $path = $this->decode($hash);
  1184. $dir = $this->_dirname($path);
  1185. $stat = $this->stat($this->_joinPath($dir, $name));
  1186. if ($stat) {
  1187. return $this->setError(elFinder::ERROR_EXISTS, $name);
  1188. }
  1189. if (!$this->allowCreate($dir, $name)) {
  1190. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1191. }
  1192. $this->rmTmb($file); // remove old name tmbs, we cannot do this after dir move
  1193. if (($path = $this->_move($path, $dir, $name))) {
  1194. $this->clearcache();
  1195. return $this->stat($path);
  1196. }
  1197. return false;
  1198. }
  1199. /**
  1200. * Create file copy with suffix "copy number" and return its info
  1201. *
  1202. * @param string $hash file hash
  1203. * @param string $suffix suffix to add to file name
  1204. * @return array|false
  1205. * @author Dmitry (dio) Levashov
  1206. **/
  1207. public function duplicate($hash, $suffix='copy') {
  1208. if ($this->commandDisabled('duplicate')) {
  1209. return $this->setError(elFinder::ERROR_COPY, '#'.$hash, elFinder::ERROR_PERM_DENIED);
  1210. }
  1211. if (($file = $this->file($hash)) == false) {
  1212. return $this->setError(elFinder::ERROR_COPY, elFinder::ERROR_FILE_NOT_FOUND);
  1213. }
  1214. $path = $this->decode($hash);
  1215. $dir = $this->_dirname($path);
  1216. $name = $this->uniqueName($dir, $this->_basename($path), ' '.$suffix.' ');
  1217. if (!$this->allowCreate($dir, $name)) {
  1218. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1219. }
  1220. return ($path = $this->copy($path, $dir, $name)) == false
  1221. ? false
  1222. : $this->stat($path);
  1223. }
  1224. /**
  1225. * Save uploaded file.
  1226. * On success return array with new file stat and with removed file hash (if existed file was replaced)
  1227. *
  1228. * @param Resource $fp file pointer
  1229. * @param string $dst destination folder hash
  1230. * @param string $src file name
  1231. * @param string $tmpname file tmp name - required to detect mime type
  1232. * @return array|false
  1233. * @author Dmitry (dio) Levashov
  1234. **/
  1235. public function upload($fp, $dst, $name, $tmpname) {
  1236. if ($this->commandDisabled('upload')) {
  1237. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1238. }
  1239. if (($dir = $this->dir($dst)) == false) {
  1240. return $this->setError(elFinder::ERROR_TRGDIR_NOT_FOUND, '#'.$dst);
  1241. }
  1242. if (!$dir['write']) {
  1243. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1244. }
  1245. if (!$this->nameAccepted($name)) {
  1246. return $this->setError(elFinder::ERROR_INVALID_NAME);
  1247. }
  1248. $mime = $this->mimetype($this->mimeDetect == 'internal' ? $name : $tmpname, $name);
  1249. if ($mime == 'unknown' && $this->mimeDetect == 'internal') {
  1250. $mime = elFinderVolumeDriver::mimetypeInternalDetect($name);
  1251. }
  1252. // logic based on http://httpd.apache.org/docs/2.2/mod/mod_authz_host.html#order
  1253. $allow = $this->mimeAccepted($mime, $this->uploadAllow, null);
  1254. $deny = $this->mimeAccepted($mime, $this->uploadDeny, null);
  1255. $upload = true; // default to allow
  1256. if (strtolower($this->uploadOrder[0]) == 'allow') { // array('allow', 'deny'), default is to 'deny'
  1257. $upload = false; // default is deny
  1258. if (!$deny && ($allow === true)) { // match only allow
  1259. $upload = true;
  1260. }// else (both match | no match | match only deny) { deny }
  1261. } else { // array('deny', 'allow'), default is to 'allow' - this is the default rule
  1262. $upload = true; // default is allow
  1263. if (($deny === true) && !$allow) { // match only deny
  1264. $upload = false;
  1265. } // else (both match | no match | match only allow) { allow }
  1266. }
  1267. if (!$upload) {
  1268. return $this->setError(elFinder::ERROR_UPLOAD_FILE_MIME);
  1269. }
  1270. if ($this->uploadMaxSize > 0 && filesize($tmpname) > $this->uploadMaxSize) {
  1271. return $this->setError(elFinder::ERROR_UPLOAD_FILE_SIZE);
  1272. }
  1273. $dstpath = $this->decode($dst);
  1274. $test = $this->_joinPath($dstpath, $name);
  1275. $file = $this->stat($test);
  1276. $this->clearcache();
  1277. if ($file) { // file exists
  1278. if ($this->options['uploadOverwrite']) {
  1279. if (!$file['write']) {
  1280. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1281. } elseif ($file['mime'] == 'directory') {
  1282. return $this->setError(elFinder::ERROR_NOT_REPLACE, $name);
  1283. }
  1284. $this->remove($test);
  1285. } else {
  1286. $name = $this->uniqueName($dstpath, $name, '-', false);
  1287. }
  1288. }
  1289. $stat = array(
  1290. 'mime' => $mime,
  1291. 'width' => 0,
  1292. 'height' => 0,
  1293. 'size' => filesize($tmpname));
  1294. // $w = $h = 0;
  1295. if (strpos($mime, 'image') === 0 && ($s = getimagesize($tmpname))) {
  1296. $stat['width'] = $s[0];
  1297. $stat['height'] = $s[1];
  1298. }
  1299. // $this->clearcache();
  1300. if (($path = $this->_save($fp, $dstpath, $name, $stat)) == false) {
  1301. return false;
  1302. }
  1303. return $this->stat($path);
  1304. }
  1305. /**
  1306. * Paste files
  1307. *
  1308. * @param Object $volume source volume
  1309. * @param string $source file hash
  1310. * @param string $dst destination dir hash
  1311. * @param bool $rmSrc remove source after copy?
  1312. * @return array|false
  1313. * @author Dmitry (dio) Levashov
  1314. **/
  1315. public function paste($volume, $src, $dst, $rmSrc = false) {
  1316. $err = $rmSrc ? elFinder::ERROR_MOVE : elFinder::ERROR_COPY;
  1317. if ($this->commandDisabled('paste')) {
  1318. return $this->setError($err, '#'.$src, elFinder::ERROR_PERM_DENIED);
  1319. }
  1320. if (($file = $volume->file($src, $rmSrc)) == false) {
  1321. return $this->setError($err, '#'.$src, elFinder::ERROR_FILE_NOT_FOUND);
  1322. }
  1323. $name = $file['name'];
  1324. $errpath = $volume->path($src);
  1325. if (($dir = $this->dir($dst)) == false) {
  1326. return $this->setError($err, $errpath, elFinder::ERROR_TRGDIR_NOT_FOUND, '#'.$dst);
  1327. }
  1328. if (!$dir['write'] || !$file['read']) {
  1329. return $this->setError($err, $errpath, elFinder::ERROR_PERM_DENIED);
  1330. }
  1331. $destination = $this->decode($dst);
  1332. if (($test = $volume->closest($src, $rmSrc ? 'locked' : 'read', $rmSrc))) {
  1333. return $rmSrc
  1334. ? $this->setError($err, $errpath, elFinder::ERROR_LOCKED, $volume->path($test))
  1335. : $this->setError($err, $errpath, elFinder::ERROR_PERM_DENIED);
  1336. }
  1337. $test = $this->_joinPath($destination, $name);
  1338. $stat = $this->stat($test);
  1339. $this->clearcache();
  1340. if ($stat) {
  1341. if ($this->options['copyOverwrite']) {
  1342. // do not replace file with dir or dir with file
  1343. if (!$this->isSameType($file['mime'], $stat['mime'])) {
  1344. return $this->setError(elFinder::ERROR_NOT_REPLACE, $this->_path($test));
  1345. }
  1346. // existed file is not writable
  1347. if (!$stat['write']) {
  1348. return $this->setError($err, $errpath, elFinder::ERROR_PERM_DENIED);
  1349. }
  1350. // existed file locked or has locked child
  1351. if (($locked = $this->closestByAttr($test, 'locked', true))) {
  1352. return $this->setError(elFinder::ERROR_LOCKED, $this->_path($locked));
  1353. }
  1354. // target is entity file of alias
  1355. if ($volume == $this && ($test == @$file['target'] || $test == $this->decode($src))) {
  1356. return $this->setError(elFinder::ERROR_REPLACE, $errpath);
  1357. }
  1358. // remove existed file
  1359. if (!$this->remove($test)) {
  1360. return $this->setError(elFinder::ERROR_REPLACE, $this->_path($test));
  1361. }
  1362. } else {
  1363. $name = $this->uniqueName($destination, $name, ' ', false);
  1364. }
  1365. }
  1366. // copy/move inside current volume
  1367. if ($volume == $this) {
  1368. $source = $this->decode($src);
  1369. // do not copy into itself
  1370. if ($this->_inpath($destination, $source)) {
  1371. return $this->setError(elFinder::ERROR_COPY_INTO_ITSELF, $errpath);
  1372. }
  1373. $method = $rmSrc ? 'move' : 'copy';
  1374. return ($path = $this->$method($source, $destination, $name)) ? $this->stat($path) : false;
  1375. }
  1376. // copy/move from another volume
  1377. if (!$this->options['copyTo'] || !$volume->copyFromAllowed()) {
  1378. return $this->setError(elFinder::ERROR_COPY, $errpath, elFinder::ERROR_PERM_DENIED);
  1379. }
  1380. if (($path = $this->copyFrom($volume, $src, $destination, $name)) == false) {
  1381. return false;
  1382. }
  1383. if ($rmSrc) {
  1384. if ($volume->rm($src)) {
  1385. $this->removed[] = $file;
  1386. } else {
  1387. return $this->setError(elFinder::ERROR_MOVE, $errpath, elFinder::ERROR_RM_SRC);
  1388. }
  1389. }
  1390. return $this->stat($path);
  1391. }
  1392. /**
  1393. * Return file contents
  1394. *
  1395. * @param string $hash file hash
  1396. * @return string|false
  1397. * @author Dmitry (dio) Levashov
  1398. **/
  1399. public function getContents($hash) {
  1400. $file = $this->file($hash);
  1401. if (!$file) {
  1402. return $this->setError(elFinder::ERROR_FILE_NOT_FOUND);
  1403. }
  1404. if ($file['mime'] == 'directory') {
  1405. return $this->setError(elFinder::ERROR_NOT_FILE);
  1406. }
  1407. if (!$file['read']) {
  1408. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1409. }
  1410. return $this->_getContents($this->decode($hash));
  1411. }
  1412. /**
  1413. * Put content in text file and return file info.
  1414. *
  1415. * @param string $hash file hash
  1416. * @param string $content new file content
  1417. * @return array
  1418. * @author Dmitry (dio) Levashov
  1419. **/
  1420. public function putContents($hash, $content) {
  1421. if ($this->commandDisabled('edit')) {
  1422. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1423. }
  1424. $path = $this->decode($hash);
  1425. if (!($file = $this->file($hash))) {
  1426. return $this->setError(elFinder::ERROR_FILE_NOT_FOUND);
  1427. }
  1428. if (!$file['write']) {
  1429. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1430. }
  1431. $this->clearcache();
  1432. return $this->_filePutContents($path, $content) ? $this->stat($path) : false;
  1433. }
  1434. /**
  1435. * Extract files from archive
  1436. *
  1437. * @param string $hash archive hash
  1438. * @return array|bool
  1439. * @author Dmitry (dio) Levashov,
  1440. * @author Alexey Sukhotin
  1441. **/
  1442. public function extract($hash) {
  1443. if ($this->commandDisabled('extract')) {
  1444. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1445. }
  1446. if (($file = $this->file($hash)) == false) {
  1447. return $this->setError(elFinder::ERROR_FILE_NOT_FOUND);
  1448. }
  1449. $archiver = isset($this->archivers['extract'][$file['mime']])
  1450. ? $this->archivers['extract'][$file['mime']]
  1451. : false;
  1452. if (!$archiver) {
  1453. return $this->setError(elFinder::ERROR_NOT_ARCHIVE);
  1454. }
  1455. $path = $this->decode($hash);
  1456. $parent = $this->stat($this->_dirname($path));
  1457. if (!$file['read'] || !$parent['write']) {
  1458. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1459. }
  1460. $this->clearcache();
  1461. return ($path = $this->_extract($path, $archiver)) ? $this->stat($path) : false;
  1462. }
  1463. /**
  1464. * Add files to archive
  1465. *
  1466. * @return void
  1467. **/
  1468. public function archive($hashes, $mime) {
  1469. if ($this->commandDisabled('archive')) {
  1470. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1471. }
  1472. $archiver = isset($this->archivers['create'][$mime])
  1473. ? $this->archivers['create'][$mime]
  1474. : false;
  1475. if (!$archiver) {
  1476. return $this->setError(elFinder::ERROR_ARCHIVE_TYPE);
  1477. }
  1478. $files = array();
  1479. foreach ($hashes as $hash) {
  1480. if (($file = $this->file($hash)) == false) {
  1481. return $this->error(elFinder::ERROR_FILE_NOT_FOUND, '#'+$hash);
  1482. }
  1483. if (!$file['read']) {
  1484. return $this->error(elFinder::ERROR_PERM_DENIED);
  1485. }
  1486. $path = $this->decode($hash);
  1487. if (!isset($dir)) {
  1488. $dir = $this->_dirname($path);
  1489. $stat = $this->stat($dir);
  1490. if (!$stat['write']) {
  1491. return $this->error(elFinder::ERROR_PERM_DENIED);
  1492. }
  1493. }
  1494. $files[] = $this->_basename($path);
  1495. }
  1496. $name = (count($files) == 1 ? $files[0] : 'Archive').'.'.$archiver['ext'];
  1497. $name = $this->uniqueName($dir, $name, '');
  1498. $this->clearcache();
  1499. return ($path = $this->_archive($dir, $files, $name, $archiver)) ? $this->stat($path) : false;
  1500. }
  1501. /**
  1502. * Resize image
  1503. *
  1504. * @param string $hash image file
  1505. * @param int $width new width
  1506. * @param int $height new height
  1507. * @param int $x X start poistion for crop
  1508. * @param int $y Y start poistion for crop
  1509. * @param string $mode action how to mainpulate image
  1510. * @return array|false
  1511. * @author Dmitry (dio) Levashov
  1512. * @author Alexey Sukhotin
  1513. * @author nao-pon
  1514. * @author Troex Nevelin
  1515. **/
  1516. public function resize($hash, $width, $height, $x, $y, $mode = 'resize', $bg = '', $degree = 0) {
  1517. if ($this->commandDisabled('resize')) {
  1518. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1519. }
  1520. if (($file = $this->file($hash)) == false) {
  1521. return $this->setError(elFinder::ERROR_FILE_NOT_FOUND);
  1522. }
  1523. if (!$file['write'] || !$file['read']) {
  1524. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1525. }
  1526. $path = $this->decode($hash);
  1527. if (!$this->canResize($path, $file)) {
  1528. return $this->setError(elFinder::ERROR_UNSUPPORT_TYPE);
  1529. }
  1530. switch($mode) {
  1531. case 'propresize':
  1532. $result = $this->imgResize($path, $width, $height, true, true);
  1533. break;
  1534. case 'crop':
  1535. $result = $this->imgCrop($path, $width, $height, $x, $y);
  1536. break;
  1537. case 'fitsquare':
  1538. $result = $this->imgSquareFit($path, $width, $height, 'center', 'middle', ($bg ? $bg : $this->options['tmbBgColor']));
  1539. break;
  1540. case 'rotate':
  1541. $result = $this->imgRotate($path, $degree, ($bg ? $bg : $this->options['tmbBgColor']));
  1542. break;
  1543. default:
  1544. $result = $this->imgResize($path, $width, $height, false, true);
  1545. break;
  1546. }
  1547. if ($result) {
  1548. $this->rmTmb($file);
  1549. $this->clearcache();
  1550. return $this->stat($path);
  1551. }
  1552. return false;
  1553. }
  1554. /**
  1555. * Remove file/dir
  1556. *
  1557. * @param string $hash file hash
  1558. * @return bool
  1559. * @author Dmitry (dio) Levashov
  1560. **/
  1561. public function rm($hash) {
  1562. return $this->commandDisabled('rm')
  1563. ? array(elFinder::ERROR_ACCESS_DENIED)
  1564. : $this->remove($this->decode($hash));
  1565. }
  1566. /**
  1567. * Search files
  1568. *
  1569. * @param string $q search string
  1570. * @param array $mimes
  1571. * @return array
  1572. * @author Dmitry (dio) Levashov
  1573. **/
  1574. public function search($q, $mimes) {
  1575. return $this->doSearch($this->root, $q, $mimes);
  1576. }
  1577. /**
  1578. * Return image dimensions
  1579. *
  1580. * @param string $hash file hash
  1581. * @return array
  1582. * @author Dmitry (dio) Levashov
  1583. **/
  1584. public function dimensions($hash) {
  1585. if (($file = $this->file($hash)) == false) {
  1586. return false;
  1587. }
  1588. return $this->_dimensions($this->decode($hash), $file['mime']);
  1589. }
  1590. /**
  1591. * Return temp path
  1592. *
  1593. * @return string
  1594. * @author Naoki Sawada
  1595. */
  1596. public function getTempPath() {
  1597. if (@ $this->tmpPath) {
  1598. return $this->tmpPath;
  1599. } else if (@ $this->tmp) {
  1600. return $this->tmp;
  1601. } else if (@ $this->tmbPath) {
  1602. return $this->tmbPath;
  1603. } else {
  1604. return null;
  1605. }
  1606. }
  1607. /**
  1608. * (Make &) Get upload taget dirctory hash
  1609. *
  1610. * @param string $baseTargetHash
  1611. * @param string $path
  1612. * @param array $result
  1613. * @return boolean|string
  1614. * @author Naoki Sawada
  1615. */
  1616. public function getUploadTaget($baseTargetHash, $path, & $result) {
  1617. $base = $this->decode($baseTargetHash);
  1618. $targetHash = $baseTargetHash;
  1619. $path = ltrim($path, '/');
  1620. $dirs = explode('/', $path);
  1621. array_pop($dirs);
  1622. foreach($dirs as $dir) {
  1623. $targetPath = $this->_joinPath($base, $dir);
  1624. if (! $_realpath = $this->realpath($this->encode($targetPath))) {
  1625. if ($stat = $this->mkdir($targetHash, $dir)) {
  1626. $result['added'][] = $stat;
  1627. $targetHash = $stat['hash'];
  1628. $base = $this->decode($targetHash);
  1629. } else {
  1630. return false;
  1631. }
  1632. } else {
  1633. $targetHash = $this->encode($_realpath);
  1634. if ($this->dir($targetHash)) {
  1635. $base = $this->decode($targetHash);
  1636. } else {
  1637. return false;
  1638. }
  1639. }
  1640. }
  1641. return $targetHash;
  1642. }
  1643. /**
  1644. * Save error message
  1645. *
  1646. * @param array error
  1647. * @return false
  1648. * @author Dmitry(dio) Levashov
  1649. **/
  1650. protected function setError($error) {
  1651. $this->error = array();
  1652. foreach (func_get_args() as $err) {
  1653. if (is_array($err)) {
  1654. $this->error = array_merge($this->error, $err);
  1655. } else {
  1656. $this->error[] = $err;
  1657. }
  1658. }
  1659. // $this->error = is_array($error) ? $error : func_get_args();
  1660. return false;
  1661. }
  1662. /*********************************************************************/
  1663. /* FS API */
  1664. /*********************************************************************/
  1665. /***************** paths *******************/
  1666. /**
  1667. * Encode path into hash
  1668. *
  1669. * @param string file path
  1670. * @return string
  1671. * @author Dmitry (dio) Levashov
  1672. * @author Troex Nevelin
  1673. **/
  1674. protected function encode($path) {
  1675. if ($path !== '') {
  1676. // cut ROOT from $path for security reason, even if hacker decodes the path he will not know the root
  1677. $p = $this->_relpath($path);
  1678. // if reqesting root dir $path will be empty, then assign '/' as we cannot leave it blank for crypt
  1679. if ($p === '') {
  1680. $p = DIRECTORY_SEPARATOR;
  1681. }
  1682. // TODO crypt path and return hash
  1683. $hash = $this->crypt($p);
  1684. // hash is used as id in HTML that means it must contain vaild chars
  1685. // make base64 html safe and append prefix in begining
  1686. $hash = strtr(base64_encode($hash), '+/=', '-_.');
  1687. // remove dots '.' at the end, before it was '=' in base64
  1688. $hash = rtrim($hash, '.');
  1689. // append volume id to make hash unique
  1690. return $this->id.$hash;
  1691. }
  1692. }
  1693. /**
  1694. * Decode path from hash
  1695. *
  1696. * @param string file hash
  1697. * @return string
  1698. * @author Dmitry (dio) Levashov
  1699. * @author Troex Nevelin
  1700. **/
  1701. protected function decode($hash) {
  1702. if (strpos($hash, $this->id) === 0) {
  1703. // cut volume id after it was prepended in encode
  1704. $h = substr($hash, strlen($this->id));
  1705. // replace HTML safe base64 to normal
  1706. $h = base64_decode(strtr($h, '-_.', '+/='));
  1707. // TODO uncrypt hash and return path
  1708. $path = $this->uncrypt($h);
  1709. // append ROOT to path after it was cut in encode
  1710. return $this->_abspath($path);//$this->root.($path == DIRECTORY_SEPARATOR ? '' : DIRECTORY_SEPARATOR.$path);
  1711. }
  1712. }
  1713. /**
  1714. * Return crypted path
  1715. * Not implemented
  1716. *
  1717. * @param string path
  1718. * @return mixed
  1719. * @author Dmitry (dio) Levashov
  1720. **/
  1721. protected function crypt($path) {
  1722. return $path;
  1723. }
  1724. /**
  1725. * Return uncrypted path
  1726. * Not implemented
  1727. *
  1728. * @param mixed hash
  1729. * @return mixed
  1730. * @author Dmitry (dio) Levashov
  1731. **/
  1732. protected function uncrypt($hash) {
  1733. return $hash;
  1734. }
  1735. /**
  1736. * Validate file name based on $this->options['acceptedName'] regexp
  1737. *
  1738. * @param string $name file name
  1739. * @return bool
  1740. * @author Dmitry (dio) Levashov
  1741. **/
  1742. protected function nameAccepted($name) {
  1743. if ($this->nameValidator) {
  1744. if (function_exists($this->nameValidator)) {
  1745. $f = $this->nameValidator;
  1746. return $f($name);
  1747. }
  1748. return preg_match($this->nameValidator, $name);
  1749. }
  1750. return true;
  1751. }
  1752. /**
  1753. * Return new unique name based on file name and suffix
  1754. *
  1755. * @param string $path file path
  1756. * @param string $suffix suffix append to name
  1757. * @return string
  1758. * @author Dmitry (dio) Levashov
  1759. **/
  1760. public function uniqueName($dir, $name, $suffix = ' copy', $checkNum=true) {
  1761. $ext = '';
  1762. if (preg_match('/\.((tar\.(gz|bz|bz2|z|lzo))|cpio\.gz|ps\.gz|xcf\.(gz|bz2)|[a-z0-9]{1,4})$/i', $name, $m)) {
  1763. $ext = '.'.$m[1];
  1764. $name = substr($name, 0, strlen($name)-strlen($m[0]));
  1765. }
  1766. if ($checkNum && preg_match('/('.$suffix.')(\d*)$/i', $name, $m)) {
  1767. $i = (int)$m[2];
  1768. $name = substr($name, 0, strlen($name)-strlen($m[2]));
  1769. } else {
  1770. $i = 1;
  1771. $name .= $suffix;
  1772. }
  1773. $max = $i+100000;
  1774. while ($i <= $max) {
  1775. $n = $name.($i > 0 ? $i : '').$ext;
  1776. if (!$this->stat($this->_joinPath($dir, $n))) {
  1777. $this->clearcache();
  1778. return $n;
  1779. }
  1780. $i++;
  1781. }
  1782. return $name.md5($dir).$ext;
  1783. }
  1784. /*********************** file stat *********************/
  1785. /**
  1786. * Check file attribute
  1787. *
  1788. * @param string $path file path
  1789. * @param string $name attribute name (read|write|locked|hidden)
  1790. * @param bool $val attribute value returned by file system
  1791. * @return bool
  1792. * @author Dmitry (dio) Levashov
  1793. **/
  1794. protected function attr($path, $name, $val=null) {
  1795. if (!isset($this->defaults[$name])) {
  1796. return false;
  1797. }
  1798. $perm = null;
  1799. if ($this->access) {
  1800. $perm = call_user_func($this->access, $name, $path, $this->options['accessControlData'], $this);
  1801. if ($perm !== null) {
  1802. return !!$perm;
  1803. }
  1804. }
  1805. if ($this->separator != '/') {
  1806. $path = str_replace($this->separator, '/', $this->_relpath($path));
  1807. } else {
  1808. $path = $this->_relpath($path);
  1809. }
  1810. $path = '/'.$path;
  1811. for ($i = 0, $c = count($this->attributes); $i < $c; $i++) {
  1812. $attrs = $this->attributes[$i];
  1813. if (isset($attrs[$name]) && isset($attrs['pattern']) && preg_match($attrs['pattern'], $path)) {
  1814. $perm = $attrs[$name];
  1815. }
  1816. }
  1817. return $perm === null ? (is_null($val)? $this->defaults[$name] : $val) : !!$perm;
  1818. }
  1819. /**
  1820. * Return true if file with given name can be created in given folder.
  1821. *
  1822. * @param string $dir parent dir path
  1823. * @param string $name new file name
  1824. * @return bool
  1825. * @author Dmitry (dio) Levashov
  1826. **/
  1827. protected function allowCreate($dir, $name) {
  1828. $path = $this->_joinPath($dir, $name);
  1829. $perm = null;
  1830. if ($this->access) {
  1831. $perm = call_user_func($this->access, 'write', $path, $this->options['accessControlData'], $this);
  1832. if ($perm !== null) {
  1833. return !!$perm;
  1834. }
  1835. }
  1836. $testPath = $this->separator.$this->_relpath($path);
  1837. for ($i = 0, $c = count($this->attributes); $i < $c; $i++) {
  1838. $attrs = $this->attributes[$i];
  1839. if (isset($attrs['write']) && isset($attrs['pattern']) && preg_match($attrs['pattern'], $testPath)) {
  1840. $perm = $attrs['write'];
  1841. }
  1842. }
  1843. return $perm === null ? true : $perm;
  1844. }
  1845. /**
  1846. * Return fileinfo
  1847. *
  1848. * @param string $path file cache
  1849. * @return array
  1850. * @author Dmitry (dio) Levashov
  1851. **/
  1852. protected function stat($path) {
  1853. if ($path === false) {
  1854. return false;
  1855. }
  1856. return isset($this->cache[$path])
  1857. ? $this->cache[$path]
  1858. : $this->updateCache($path, $this->_stat($path));
  1859. }
  1860. /**
  1861. * Put file stat in cache and return it
  1862. *
  1863. * @param string $path file path
  1864. * @param array $stat file stat
  1865. * @return array
  1866. * @author Dmitry (dio) Levashov
  1867. **/
  1868. protected function updateCache($path, $stat) {
  1869. if (empty($stat) || !is_array($stat)) {
  1870. return $this->cache[$path] = array();
  1871. }
  1872. $stat['hash'] = $this->encode($path);
  1873. $root = $path == $this->root;
  1874. if ($root) {
  1875. $stat['volumeid'] = $this->id;
  1876. if ($this->rootName) {
  1877. $stat['name'] = $this->rootName;
  1878. }
  1879. } else {
  1880. if (!isset($stat['name']) || !strlen($stat['name'])) {
  1881. $stat['name'] = $this->_basename($path);
  1882. }
  1883. if (empty($stat['phash'])) {
  1884. $stat['phash'] = $this->encode($this->_dirname($path));
  1885. }
  1886. }
  1887. // fix name if required
  1888. if ($this->options['utf8fix'] && $this->options['utf8patterns'] && $this->options['utf8replace']) {
  1889. $stat['name'] = json_decode(str_replace($this->options['utf8patterns'], $this->options['utf8replace'], json_encode($stat['name'])));
  1890. }
  1891. if (empty($stat['mime'])) {
  1892. $stat['mime'] = $this->mimetype($stat['name']);
  1893. }
  1894. // @todo move dateformat to client
  1895. // $stat['date'] = isset($stat['ts'])
  1896. // ? $this->formatDate($stat['ts'])
  1897. // : 'unknown';
  1898. if (!isset($stat['size'])) {
  1899. $stat['size'] = 'unknown';
  1900. }
  1901. $stat['read'] = intval($this->attr($path, 'read', isset($stat['read']) ? !!$stat['read'] : null));
  1902. $stat['write'] = intval($this->attr($path, 'write', isset($stat['write']) ? !!$stat['write'] : null));
  1903. if ($root) {
  1904. $stat['locked'] = 1;
  1905. } elseif ($this->attr($path, 'locked', !empty($stat['locked']))) {
  1906. $stat['locked'] = 1;
  1907. } else {
  1908. unset($stat['locked']);
  1909. }
  1910. if ($root) {
  1911. unset($stat['hidden']);
  1912. } elseif ($this->attr($path, 'hidden', !empty($stat['hidden']))
  1913. || !$this->mimeAccepted($stat['mime'])) {
  1914. $stat['hidden'] = $root ? 0 : 1;
  1915. } else {
  1916. unset($stat['hidden']);
  1917. }
  1918. if ($stat['read'] && empty($stat['hidden'])) {
  1919. if ($stat['mime'] == 'directory') {
  1920. // for dir - check for subdirs
  1921. if ($this->options['checkSubfolders']) {
  1922. if (isset($stat['dirs'])) {
  1923. if ($stat['dirs']) {
  1924. $stat['dirs'] = 1;
  1925. } else {
  1926. unset($stat['dirs']);
  1927. }
  1928. } elseif (!empty($stat['alias']) && !empty($stat['target'])) {
  1929. $stat['dirs'] = isset($this->cache[$stat['target']])
  1930. ? intval(isset($this->cache[$stat['target']]['dirs']))
  1931. : $this->_subdirs($stat['target']);
  1932. } elseif ($this->_subdirs($path)) {
  1933. $stat['dirs'] = 1;
  1934. }
  1935. } else {
  1936. $stat['dirs'] = 1;
  1937. }
  1938. } else {
  1939. // for files - check for thumbnails
  1940. $p = isset($stat['target']) ? $stat['target'] : $path;
  1941. if ($this->tmbURL && !isset($stat['tmb']) && $this->canCreateTmb($p, $stat)) {
  1942. $tmb = $this->gettmb($p, $stat);
  1943. $stat['tmb'] = $tmb ? $tmb : 1;
  1944. }
  1945. }
  1946. }
  1947. if (!empty($stat['alias']) && !empty($stat['target'])) {
  1948. $stat['thash'] = $this->encode($stat['target']);
  1949. unset($stat['target']);
  1950. }
  1951. return $this->cache[$path] = $stat;
  1952. }
  1953. /**
  1954. * Get stat for folder content and put in cache
  1955. *
  1956. * @param string $path
  1957. * @return void
  1958. * @author Dmitry (dio) Levashov
  1959. **/
  1960. protected function cacheDir($path) {
  1961. $this->dirsCache[$path] = array();
  1962. foreach ($this->_scandir($path) as $p) {
  1963. if (($stat = $this->stat($p)) && empty($stat['hidden'])) {
  1964. $this->dirsCache[$path][] = $p;
  1965. }
  1966. }
  1967. }
  1968. /**
  1969. * Clean cache
  1970. *
  1971. * @return void
  1972. * @author Dmitry (dio) Levashov
  1973. **/
  1974. protected function clearcache() {
  1975. $this->cache = $this->dirsCache = array();
  1976. }
  1977. /**
  1978. * Return file mimetype
  1979. *
  1980. * @param string $path file path
  1981. * @return string
  1982. * @author Dmitry (dio) Levashov
  1983. **/
  1984. protected function mimetype($path, $name = '') {
  1985. $type = '';
  1986. if ($this->mimeDetect == 'finfo') {
  1987. if ($type = @finfo_file($this->finfo, $path)) {
  1988. if ($name === '') {
  1989. $name = $path;
  1990. }
  1991. $ext = (false === $pos = strrpos($name, '.')) ? '' : substr($name, $pos + 1);
  1992. if ($ext && preg_match('~^application/(?:octet-stream|(?:x-)?zip)~', $type)) {
  1993. if (isset(elFinderVolumeDriver::$mimetypes[$ext])) $type = elFinderVolumeDriver::$mimetypes[$ext];
  1994. }
  1995. }
  1996. } elseif ($type == 'mime_content_type') {
  1997. $type = mime_content_type($path);
  1998. } else {
  1999. $type = elFinderVolumeDriver::mimetypeInternalDetect($path);
  2000. }
  2001. $type = explode(';', $type);
  2002. $type = trim($type[0]);
  2003. if (in_array($type, array('application/x-empty', 'inode/x-empty'))) {
  2004. // finfo return this mime for empty files
  2005. $type = 'text/plain';
  2006. } elseif ($type == 'application/x-zip') {
  2007. // http://elrte.org/redmine/issues/163
  2008. $type = 'application/zip';
  2009. }
  2010. return $type == 'unknown' && $this->mimeDetect != 'internal'
  2011. ? elFinderVolumeDriver::mimetypeInternalDetect($path)
  2012. : $type;
  2013. }
  2014. /**
  2015. * Detect file mimetype using "internal" method
  2016. *
  2017. * @param string $path file path
  2018. * @return string
  2019. * @author Dmitry (dio) Levashov
  2020. **/
  2021. static protected function mimetypeInternalDetect($path) {
  2022. $pinfo = pathinfo($path);
  2023. $ext = isset($pinfo['extension']) ? strtolower($pinfo['extension']) : '';
  2024. return isset(elFinderVolumeDriver::$mimetypes[$ext]) ? elFinderVolumeDriver::$mimetypes[$ext] : 'unknown';
  2025. }
  2026. /**
  2027. * Return file/total directory size
  2028. *
  2029. * @param string $path file path
  2030. * @return int
  2031. * @author Dmitry (dio) Levashov
  2032. **/
  2033. protected function countSize($path) {
  2034. $stat = $this->stat($path);
  2035. if (empty($stat) || !$stat['read'] || !empty($stat['hidden'])) {
  2036. return 'unknown';
  2037. }
  2038. if ($stat['mime'] != 'directory') {
  2039. return $stat['size'];
  2040. }
  2041. $subdirs = $this->options['checkSubfolders'];
  2042. $this->options['checkSubfolders'] = true;
  2043. $result = 0;
  2044. foreach ($this->getScandir($path) as $stat) {
  2045. $size = $stat['mime'] == 'directory' && $stat['read']
  2046. ? $this->countSize($this->_joinPath($path, $stat['name']))
  2047. : (isset($stat['size']) ? intval($stat['size']) : 0);
  2048. if ($size > 0) {
  2049. $result += $size;
  2050. }
  2051. }
  2052. $this->options['checkSubfolders'] = $subdirs;
  2053. return $result;
  2054. }
  2055. /**
  2056. * Return true if all mimes is directory or files
  2057. *
  2058. * @param string $mime1 mimetype
  2059. * @param string $mime2 mimetype
  2060. * @return bool
  2061. * @author Dmitry (dio) Levashov
  2062. **/
  2063. protected function isSameType($mime1, $mime2) {
  2064. return ($mime1 == 'directory' && $mime1 == $mime2) || ($mime1 != 'directory' && $mime2 != 'directory');
  2065. }
  2066. /**
  2067. * If file has required attr == $val - return file path,
  2068. * If dir has child with has required attr == $val - return child path
  2069. *
  2070. * @param string $path file path
  2071. * @param string $attr attribute name
  2072. * @param bool $val attribute value
  2073. * @return string|false
  2074. * @author Dmitry (dio) Levashov
  2075. **/
  2076. protected function closestByAttr($path, $attr, $val) {
  2077. $stat = $this->stat($path);
  2078. if (empty($stat)) {
  2079. return false;
  2080. }
  2081. $v = isset($stat[$attr]) ? $stat[$attr] : false;
  2082. if ($v == $val) {
  2083. return $path;
  2084. }
  2085. return $stat['mime'] == 'directory'
  2086. ? $this->childsByAttr($path, $attr, $val)
  2087. : false;
  2088. }
  2089. /**
  2090. * Return first found children with required attr == $val
  2091. *
  2092. * @param string $path file path
  2093. * @param string $attr attribute name
  2094. * @param bool $val attribute value
  2095. * @return string|false
  2096. * @author Dmitry (dio) Levashov
  2097. **/
  2098. protected function childsByAttr($path, $attr, $val) {
  2099. foreach ($this->_scandir($path) as $p) {
  2100. if (($_p = $this->closestByAttr($p, $attr, $val)) != false) {
  2101. return $_p;
  2102. }
  2103. }
  2104. return false;
  2105. }
  2106. /***************** get content *******************/
  2107. /**
  2108. * Return required dir's files info.
  2109. * If onlyMimes is set - return only dirs and files of required mimes
  2110. *
  2111. * @param string $path dir path
  2112. * @return array
  2113. * @author Dmitry (dio) Levashov
  2114. **/
  2115. protected function getScandir($path) {
  2116. $files = array();
  2117. !isset($this->dirsCache[$path]) && $this->cacheDir($path);
  2118. foreach ($this->dirsCache[$path] as $p) {
  2119. if (($stat = $this->stat($p)) && empty($stat['hidden'])) {
  2120. $files[] = $stat;
  2121. }
  2122. }
  2123. return $files;
  2124. }
  2125. /**
  2126. * Return subdirs tree
  2127. *
  2128. * @param string $path parent dir path
  2129. * @param int $deep tree deep
  2130. * @return array
  2131. * @author Dmitry (dio) Levashov
  2132. **/
  2133. protected function gettree($path, $deep, $exclude='') {
  2134. $dirs = array();
  2135. !isset($this->dirsCache[$path]) && $this->cacheDir($path);
  2136. foreach ($this->dirsCache[$path] as $p) {
  2137. $stat = $this->stat($p);
  2138. if ($stat && empty($stat['hidden']) && $p != $exclude && $stat['mime'] == 'directory') {
  2139. $dirs[] = $stat;
  2140. if ($deep > 0 && !empty($stat['dirs'])) {
  2141. $dirs = array_merge($dirs, $this->gettree($p, $deep-1));
  2142. }
  2143. }
  2144. }
  2145. return $dirs;
  2146. }
  2147. /**
  2148. * Recursive files search
  2149. *
  2150. * @param string $path dir path
  2151. * @param string $q search string
  2152. * @param array $mimes
  2153. * @return array
  2154. * @author Dmitry (dio) Levashov
  2155. **/
  2156. protected function doSearch($path, $q, $mimes) {
  2157. $result = array();
  2158. foreach($this->_scandir($path) as $p) {
  2159. $stat = $this->stat($p);
  2160. if (!$stat) { // invalid links
  2161. continue;
  2162. }
  2163. if (!empty($stat['hidden']) || !$this->mimeAccepted($stat['mime'])) {
  2164. continue;
  2165. }
  2166. $name = $stat['name'];
  2167. if ($this->stripos($name, $q) !== false) {
  2168. $stat['path'] = $this->_path($p);
  2169. if ($this->URL && !isset($stat['url'])) {
  2170. $stat['url'] = $this->URL . str_replace($this->separator, '/', substr($p, strlen($this->root) + 1));
  2171. }
  2172. $result[] = $stat;
  2173. }
  2174. if ($stat['mime'] == 'directory' && $stat['read'] && !isset($stat['alias'])) {
  2175. $result = array_merge($result, $this->doSearch($p, $q, $mimes));
  2176. }
  2177. }
  2178. return $result;
  2179. }
  2180. /********************** manuipulations ******************/
  2181. /**
  2182. * Copy file/recursive copy dir only in current volume.
  2183. * Return new file path or false.
  2184. *
  2185. * @param string $src source path
  2186. * @param string $dst destination dir path
  2187. * @param string $name new file name (optionaly)
  2188. * @return string|false
  2189. * @author Dmitry (dio) Levashov
  2190. **/
  2191. protected function copy($src, $dst, $name) {
  2192. $srcStat = $this->stat($src);
  2193. $this->clearcache();
  2194. if (!empty($srcStat['thash'])) {
  2195. $target = $this->decode($srcStat['thash']);
  2196. $stat = $this->stat($target);
  2197. $this->clearcache();
  2198. return $stat && $this->_symlink($target, $dst, $name)
  2199. ? $this->_joinPath($dst, $name)
  2200. : $this->setError(elFinder::ERROR_COPY, $this->_path($src));
  2201. }
  2202. if ($srcStat['mime'] == 'directory') {
  2203. $test = $this->stat($this->_joinPath($dst, $name));
  2204. if (($test && $test['mime'] != 'directory') || !$this->_mkdir($dst, $name)) {
  2205. return $this->setError(elFinder::ERROR_COPY, $this->_path($src));
  2206. }
  2207. $dst = $this->_joinPath($dst, $name);
  2208. foreach ($this->getScandir($src) as $stat) {
  2209. if (empty($stat['hidden'])) {
  2210. $name = $stat['name'];
  2211. if (!$this->copy($this->_joinPath($src, $name), $dst, $name)) {
  2212. $this->remove($dst, true); // fall back
  2213. return false;
  2214. }
  2215. }
  2216. }
  2217. $this->clearcache();
  2218. return $dst;
  2219. }
  2220. return $this->_copy($src, $dst, $name)
  2221. ? $this->_joinPath($dst, $name)
  2222. : $this->setError(elFinder::ERROR_COPY, $this->_path($src));
  2223. }
  2224. /**
  2225. * Move file
  2226. * Return new file path or false.
  2227. *
  2228. * @param string $src source path
  2229. * @param string $dst destination dir path
  2230. * @param string $name new file name
  2231. * @return string|false
  2232. * @author Dmitry (dio) Levashov
  2233. **/
  2234. protected function move($src, $dst, $name) {
  2235. $stat = $this->stat($src);
  2236. $stat['realpath'] = $src;
  2237. $this->rmTmb($stat); // can not do rmTmb() after _move()
  2238. $this->clearcache();
  2239. if ($this->_move($src, $dst, $name)) {
  2240. $this->removed[] = $stat;
  2241. return $this->_joinPath($dst, $name);
  2242. }
  2243. return $this->setError(elFinder::ERROR_MOVE, $this->_path($src));
  2244. }
  2245. /**
  2246. * Copy file from another volume.
  2247. * Return new file path or false.
  2248. *
  2249. * @param Object $volume source volume
  2250. * @param string $src source file hash
  2251. * @param string $destination destination dir path
  2252. * @param string $name file name
  2253. * @return string|false
  2254. * @author Dmitry (dio) Levashov
  2255. **/
  2256. protected function copyFrom($volume, $src, $destination, $name) {
  2257. if (($source = $volume->file($src)) == false) {
  2258. return $this->setError(elFinder::ERROR_COPY, '#'.$src, $volume->error());
  2259. }
  2260. $errpath = $volume->path($src);
  2261. if (!$this->nameAccepted($source['name'])) {
  2262. return $this->setError(elFinder::ERROR_COPY, $errpath, elFinder::ERROR_INVALID_NAME);
  2263. }
  2264. if (!$source['read']) {
  2265. return $this->setError(elFinder::ERROR_COPY, $errpath, elFinder::ERROR_PERM_DENIED);
  2266. }
  2267. if ($source['mime'] == 'directory') {
  2268. $stat = $this->stat($this->_joinPath($destination, $name));
  2269. $this->clearcache();
  2270. if ((!$stat || $stat['mime'] != 'directory') && !$this->_mkdir($destination, $name)) {
  2271. return $this->setError(elFinder::ERROR_COPY, $errpath);
  2272. }
  2273. $path = $this->_joinPath($destination, $name);
  2274. foreach ($volume->scandir($src) as $entr) {
  2275. if (!$this->copyFrom($volume, $entr['hash'], $path, $entr['name'])) {
  2276. return false;
  2277. }
  2278. }
  2279. } else {
  2280. // $mime = $source['mime'];
  2281. // $w = $h = 0;
  2282. if (($dim = $volume->dimensions($src))) {
  2283. $s = explode('x', $dim);
  2284. $source['width'] = $s[0];
  2285. $source['height'] = $s[1];
  2286. }
  2287. if (($fp = $volume->open($src)) == false
  2288. || ($path = $this->_save($fp, $destination, $name, $source)) == false) {
  2289. $fp && $volume->close($fp, $src);
  2290. return $this->setError(elFinder::ERROR_COPY, $errpath);
  2291. }
  2292. $volume->close($fp, $src);
  2293. }
  2294. return $path;
  2295. }
  2296. /**
  2297. * Remove file/ recursive remove dir
  2298. *
  2299. * @param string $path file path
  2300. * @param bool $force try to remove even if file locked
  2301. * @return bool
  2302. * @author Dmitry (dio) Levashov
  2303. **/
  2304. protected function remove($path, $force = false) {
  2305. $stat = $this->stat($path);
  2306. if (empty($stat)) {
  2307. return $this->setError(elFinder::ERROR_RM, $this->_path($path), elFinder::ERROR_FILE_NOT_FOUND);
  2308. }
  2309. $stat['realpath'] = $path;
  2310. $this->rmTmb($stat);
  2311. $this->clearcache();
  2312. if (!$force && !empty($stat['locked'])) {
  2313. return $this->setError(elFinder::ERROR_LOCKED, $this->_path($path));
  2314. }
  2315. if ($stat['mime'] == 'directory') {
  2316. foreach ($this->_scandir($path) as $p) {
  2317. $name = $this->_basename($p);
  2318. if ($name != '.' && $name != '..' && !$this->remove($p)) {
  2319. return false;
  2320. }
  2321. }
  2322. if (!$this->_rmdir($path)) {
  2323. return $this->setError(elFinder::ERROR_RM, $this->_path($path));
  2324. }
  2325. } else {
  2326. if (!$this->_unlink($path)) {
  2327. return $this->setError(elFinder::ERROR_RM, $this->_path($path));
  2328. }
  2329. }
  2330. $this->removed[] = $stat;
  2331. return true;
  2332. }
  2333. /************************* thumbnails **************************/
  2334. /**
  2335. * Return thumbnail file name for required file
  2336. *
  2337. * @param array $stat file stat
  2338. * @return string
  2339. * @author Dmitry (dio) Levashov
  2340. **/
  2341. protected function tmbname($stat) {
  2342. return $stat['hash'].$stat['ts'].'.png';
  2343. }
  2344. /**
  2345. * Return thumnbnail name if exists
  2346. *
  2347. * @param string $path file path
  2348. * @param array $stat file stat
  2349. * @return string|false
  2350. * @author Dmitry (dio) Levashov
  2351. **/
  2352. protected function gettmb($path, $stat) {
  2353. if ($this->tmbURL && $this->tmbPath) {
  2354. // file itself thumnbnail
  2355. if (strpos($path, $this->tmbPath) === 0) {
  2356. return basename($path);
  2357. }
  2358. $name = $this->tmbname($stat);
  2359. if (file_exists($this->tmbPath.DIRECTORY_SEPARATOR.$name)) {
  2360. return $name;
  2361. }
  2362. }
  2363. return false;
  2364. }
  2365. /**
  2366. * Return true if thumnbnail for required file can be created
  2367. *
  2368. * @param string $path thumnbnail path
  2369. * @param array $stat file stat
  2370. * @return string|bool
  2371. * @author Dmitry (dio) Levashov
  2372. **/
  2373. protected function canCreateTmb($path, $stat) {
  2374. return $this->tmbPathWritable
  2375. && strpos($path, $this->tmbPath) === false // do not create thumnbnail for thumnbnail
  2376. && $this->imgLib
  2377. && strpos($stat['mime'], 'image') === 0
  2378. && ($this->imgLib == 'gd' ? $stat['mime'] == 'image/jpeg' || $stat['mime'] == 'image/png' || $stat['mime'] == 'image/gif' : true);
  2379. }
  2380. /**
  2381. * Return true if required file can be resized.
  2382. * By default - the same as canCreateTmb
  2383. *
  2384. * @param string $path thumnbnail path
  2385. * @param array $stat file stat
  2386. * @return string|bool
  2387. * @author Dmitry (dio) Levashov
  2388. **/
  2389. protected function canResize($path, $stat) {
  2390. return $this->canCreateTmb($path, $stat);
  2391. }
  2392. /**
  2393. * Create thumnbnail and return it's URL on success
  2394. *
  2395. * @param string $path file path
  2396. * @param string $mime file mime type
  2397. * @return string|false
  2398. * @author Dmitry (dio) Levashov
  2399. **/
  2400. protected function createTmb($path, $stat) {
  2401. if (!$stat || !$this->canCreateTmb($path, $stat)) {
  2402. return false;
  2403. }
  2404. $name = $this->tmbname($stat);
  2405. $tmb = $this->tmbPath.DIRECTORY_SEPARATOR.$name;
  2406. // copy image into tmbPath so some drivers does not store files on local fs
  2407. if (($src = $this->_fopen($path, 'rb')) == false) {
  2408. return false;
  2409. }
  2410. if (($trg = fopen($tmb, 'wb')) == false) {
  2411. $this->_fclose($src, $path);
  2412. return false;
  2413. }
  2414. while (!feof($src)) {
  2415. fwrite($trg, fread($src, 8192));
  2416. }
  2417. $this->_fclose($src, $path);
  2418. fclose($trg);
  2419. $result = false;
  2420. $tmbSize = $this->tmbSize;
  2421. if (($s = getimagesize($tmb)) == false) {
  2422. return false;
  2423. }
  2424. /* If image smaller or equal thumbnail size - just fitting to thumbnail square */
  2425. if ($s[0] <= $tmbSize && $s[1] <= $tmbSize) {
  2426. $result = $this->imgSquareFit($tmb, $tmbSize, $tmbSize, 'center', 'middle', $this->options['tmbBgColor'], 'png' );
  2427. } else {
  2428. if ($this->options['tmbCrop']) {
  2429. /* Resize and crop if image bigger than thumbnail */
  2430. if (!(($s[0] > $tmbSize && $s[1] <= $tmbSize) || ($s[0] <= $tmbSize && $s[1] > $tmbSize) ) || ($s[0] > $tmbSize && $s[1] > $tmbSize)) {
  2431. $result = $this->imgResize($tmb, $tmbSize, $tmbSize, true, false, 'png');
  2432. }
  2433. if (($s = getimagesize($tmb)) != false) {
  2434. $x = $s[0] > $tmbSize ? intval(($s[0] - $tmbSize)/2) : 0;
  2435. $y = $s[1] > $tmbSize ? intval(($s[1] - $tmbSize)/2) : 0;
  2436. $result = $this->imgCrop($tmb, $tmbSize, $tmbSize, $x, $y, 'png');
  2437. }
  2438. } else {
  2439. $result = $this->imgResize($tmb, $tmbSize, $tmbSize, true, true, 'png');
  2440. }
  2441. $result = $this->imgSquareFit($tmb, $tmbSize, $tmbSize, 'center', 'middle', $this->options['tmbBgColor'], 'png' );
  2442. }
  2443. if (!$result) {
  2444. unlink($tmb);
  2445. return false;
  2446. }
  2447. return $name;
  2448. }
  2449. /**
  2450. * Resize image
  2451. *
  2452. * @param string $path image file
  2453. * @param int $width new width
  2454. * @param int $height new height
  2455. * @param bool $keepProportions crop image
  2456. * @param bool $resizeByBiggerSide resize image based on bigger side if true
  2457. * @param string $destformat image destination format
  2458. * @return string|false
  2459. * @author Dmitry (dio) Levashov
  2460. * @author Alexey Sukhotin
  2461. **/
  2462. protected function imgResize($path, $width, $height, $keepProportions = false, $resizeByBiggerSide = true, $destformat = null) {
  2463. if (($s = @getimagesize($path)) == false) {
  2464. return false;
  2465. }
  2466. $result = false;
  2467. list($size_w, $size_h) = array($width, $height);
  2468. if ($keepProportions == true) {
  2469. list($orig_w, $orig_h, $new_w, $new_h) = array($s[0], $s[1], $width, $height);
  2470. /* Calculating image scale width and height */
  2471. $xscale = $orig_w / $new_w;
  2472. $yscale = $orig_h / $new_h;
  2473. /* Resizing by biggest side */
  2474. if ($resizeByBiggerSide) {
  2475. if ($orig_w > $orig_h) {
  2476. $size_h = $orig_h * $width / $orig_w;
  2477. $size_w = $width;
  2478. } else {
  2479. $size_w = $orig_w * $height / $orig_h;
  2480. $size_h = $height;
  2481. }
  2482. } else {
  2483. if ($orig_w > $orig_h) {
  2484. $size_w = $orig_w * $height / $orig_h;
  2485. $size_h = $height;
  2486. } else {
  2487. $size_h = $orig_h * $width / $orig_w;
  2488. $size_w = $width;
  2489. }
  2490. }
  2491. }
  2492. switch ($this->imgLib) {
  2493. case 'imagick':
  2494. try {
  2495. $img = new imagick($path);
  2496. } catch (Exception $e) {
  2497. return false;
  2498. }
  2499. $img->resizeImage($size_w, $size_h, Imagick::FILTER_LANCZOS, true);
  2500. $result = $img->writeImage($path);
  2501. return $result ? $path : false;
  2502. break;
  2503. case 'gd':
  2504. $img = self::gdImageCreate($path,$s['mime']);
  2505. if ($img && false != ($tmp = imagecreatetruecolor($size_w, $size_h))) {
  2506. self::gdImageBackground($tmp,$this->options['tmbBgColor']);
  2507. if (!imagecopyresampled($tmp, $img, 0, 0, 0, 0, $size_w, $size_h, $s[0], $s[1])) {
  2508. return false;
  2509. }
  2510. $result = self::gdImage($tmp, $path, $destformat, $s['mime']);
  2511. imagedestroy($img);
  2512. imagedestroy($tmp);
  2513. return $result ? $path : false;
  2514. }
  2515. break;
  2516. }
  2517. return false;
  2518. }
  2519. /**
  2520. * Crop image
  2521. *
  2522. * @param string $path image file
  2523. * @param int $width crop width
  2524. * @param int $height crop height
  2525. * @param bool $x crop left offset
  2526. * @param bool $y crop top offset
  2527. * @param string $destformat image destination format
  2528. * @return string|false
  2529. * @author Dmitry (dio) Levashov
  2530. * @author Alexey Sukhotin
  2531. **/
  2532. protected function imgCrop($path, $width, $height, $x, $y, $destformat = null) {
  2533. if (($s = @getimagesize($path)) == false) {
  2534. return false;
  2535. }
  2536. $result = false;
  2537. switch ($this->imgLib) {
  2538. case 'imagick':
  2539. try {
  2540. $img = new imagick($path);
  2541. } catch (Exception $e) {
  2542. return false;
  2543. }
  2544. $img->cropImage($width, $height, $x, $y);
  2545. $result = $img->writeImage($path);
  2546. return $result ? $path : false;
  2547. break;
  2548. case 'gd':
  2549. $img = self::gdImageCreate($path,$s['mime']);
  2550. if ($img && false != ($tmp = imagecreatetruecolor($width, $height))) {
  2551. self::gdImageBackground($tmp,$this->options['tmbBgColor']);
  2552. $size_w = $width;
  2553. $size_h = $height;
  2554. if ($s[0] < $width || $s[1] < $height) {
  2555. $size_w = $s[0];
  2556. $size_h = $s[1];
  2557. }
  2558. if (!imagecopy($tmp, $img, 0, 0, $x, $y, $size_w, $size_h)) {
  2559. return false;
  2560. }
  2561. $result = self::gdImage($tmp, $path, $destformat, $s['mime']);
  2562. imagedestroy($img);
  2563. imagedestroy($tmp);
  2564. return $result ? $path : false;
  2565. }
  2566. break;
  2567. }
  2568. return false;
  2569. }
  2570. /**
  2571. * Put image to square
  2572. *
  2573. * @param string $path image file
  2574. * @param int $width square width
  2575. * @param int $height square height
  2576. * @param int $align reserved
  2577. * @param int $valign reserved
  2578. * @param string $bgcolor square background color in #rrggbb format
  2579. * @param string $destformat image destination format
  2580. * @return string|false
  2581. * @author Dmitry (dio) Levashov
  2582. * @author Alexey Sukhotin
  2583. **/
  2584. protected function imgSquareFit($path, $width, $height, $align = 'center', $valign = 'middle', $bgcolor = '#0000ff', $destformat = null) {
  2585. if (($s = @getimagesize($path)) == false) {
  2586. return false;
  2587. }
  2588. $result = false;
  2589. /* Coordinates for image over square aligning */
  2590. $y = ceil(abs($height - $s[1]) / 2);
  2591. $x = ceil(abs($width - $s[0]) / 2);
  2592. switch ($this->imgLib) {
  2593. case 'imagick':
  2594. try {
  2595. $img = new imagick($path);
  2596. } catch (Exception $e) {
  2597. return false;
  2598. }
  2599. $img1 = new Imagick();
  2600. $img1->newImage($width, $height, new ImagickPixel($bgcolor));
  2601. $img1->setImageColorspace($img->getImageColorspace());
  2602. $img1->setImageFormat($destformat != null ? $destformat : $img->getFormat());
  2603. $img1->compositeImage( $img, imagick::COMPOSITE_OVER, $x, $y );
  2604. $result = $img1->writeImage($path);
  2605. return $result ? $path : false;
  2606. break;
  2607. case 'gd':
  2608. $img = self::gdImageCreate($path,$s['mime']);
  2609. if ($img && false != ($tmp = imagecreatetruecolor($width, $height))) {
  2610. self::gdImageBackground($tmp,$bgcolor);
  2611. if (!imagecopy($tmp, $img, $x, $y, 0, 0, $s[0], $s[1])) {
  2612. return false;
  2613. }
  2614. $result = self::gdImage($tmp, $path, $destformat, $s['mime']);
  2615. imagedestroy($img);
  2616. imagedestroy($tmp);
  2617. return $result ? $path : false;
  2618. }
  2619. break;
  2620. }
  2621. return false;
  2622. }
  2623. /**
  2624. * Rotate image
  2625. *
  2626. * @param string $path image file
  2627. * @param int $degree rotete degrees
  2628. * @param string $bgcolor square background color in #rrggbb format
  2629. * @param string $destformat image destination format
  2630. * @return string|false
  2631. * @author nao-pon
  2632. * @author Troex Nevelin
  2633. **/
  2634. protected function imgRotate($path, $degree, $bgcolor = '#ffffff', $destformat = null) {
  2635. if (($s = @getimagesize($path)) == false) {
  2636. return false;
  2637. }
  2638. $result = false;
  2639. switch ($this->imgLib) {
  2640. case 'imagick':
  2641. try {
  2642. $img = new imagick($path);
  2643. } catch (Exception $e) {
  2644. return false;
  2645. }
  2646. $img->rotateImage(new ImagickPixel($bgcolor), $degree);
  2647. $result = $img->writeImage($path);
  2648. return $result ? $path : false;
  2649. break;
  2650. case 'gd':
  2651. $img = self::gdImageCreate($path,$s['mime']);
  2652. $degree = 360 - $degree;
  2653. list($r, $g, $b) = sscanf($bgcolor, "#%02x%02x%02x");
  2654. $bgcolor = imagecolorallocate($img, $r, $g, $b);
  2655. $tmp = imageRotate($img, $degree, (int)$bgcolor);
  2656. $result = self::gdImage($tmp, $path, $destformat, $s['mime']);
  2657. imageDestroy($img);
  2658. imageDestroy($tmp);
  2659. return $result ? $path : false;
  2660. break;
  2661. }
  2662. return false;
  2663. }
  2664. /**
  2665. * Execute shell command
  2666. *
  2667. * @param string $command command line
  2668. * @param array $output stdout strings
  2669. * @param array $return_var process exit code
  2670. * @param array $error_output stderr strings
  2671. * @return int exit code
  2672. * @author Alexey Sukhotin
  2673. **/
  2674. protected function procExec($command , array &$output = null, &$return_var = -1, array &$error_output = null) {
  2675. $descriptorspec = array(
  2676. 0 => array("pipe", "r"), // stdin
  2677. 1 => array("pipe", "w"), // stdout
  2678. 2 => array("pipe", "w") // stderr
  2679. );
  2680. $process = proc_open($command, $descriptorspec, $pipes, null, null);
  2681. if (is_resource($process)) {
  2682. fclose($pipes[0]);
  2683. $tmpout = '';
  2684. $tmperr = '';
  2685. $output = stream_get_contents($pipes[1]);
  2686. $error_output = stream_get_contents($pipes[2]);
  2687. fclose($pipes[1]);
  2688. fclose($pipes[2]);
  2689. $return_var = proc_close($process);
  2690. }
  2691. return $return_var;
  2692. }
  2693. /**
  2694. * Remove thumbnail, also remove recursively if stat is directory
  2695. *
  2696. * @param string $stat file stat
  2697. * @return void
  2698. * @author Dmitry (dio) Levashov
  2699. * @author Naoki Sawada
  2700. * @author Troex Nevelin
  2701. **/
  2702. protected function rmTmb($stat) {
  2703. if ($stat['mime'] === 'directory') {
  2704. foreach ($this->_scandir($this->decode($stat['hash'])) as $p) {
  2705. $name = $this->_basename($p);
  2706. $name != '.' && $name != '..' && $this->rmTmb($this->stat($p));
  2707. }
  2708. } else if (!empty($stat['tmb']) && $stat['tmb'] != "1") {
  2709. $tmb = $this->tmbPath.DIRECTORY_SEPARATOR.$stat['tmb'];
  2710. file_exists($tmb) && @unlink($tmb);
  2711. clearstatcache();
  2712. }
  2713. }
  2714. /**
  2715. * Create an gd image according to the specified mime type
  2716. *
  2717. * @param string $path image file
  2718. * @param string $mime
  2719. * @return gd image resource identifier
  2720. */
  2721. protected function gdImageCreate($path,$mime){
  2722. switch($mime){
  2723. case 'image/jpeg':
  2724. return imagecreatefromjpeg($path);
  2725. case 'image/png':
  2726. return imagecreatefrompng($path);
  2727. case 'image/gif':
  2728. return imagecreatefromgif($path);
  2729. case 'image/xbm':
  2730. return imagecreatefromxbm($path);
  2731. }
  2732. return false;
  2733. }
  2734. /**
  2735. * Output gd image to file
  2736. *
  2737. * @param resource $image gd image resource
  2738. * @param string $filename The path to save the file to.
  2739. * @param string $destformat The Image type to use for $filename
  2740. * @param string $mime The original image mime type
  2741. */
  2742. protected function gdImage($image, $filename, $destformat, $mime ){
  2743. if ($destformat == 'jpg' || ($destformat == null && $mime == 'image/jpeg')) {
  2744. return imagejpeg($image, $filename, 100);
  2745. }
  2746. if ($destformat == 'gif' || ($destformat == null && $mime == 'image/gif')) {
  2747. return imagegif($image, $filename, 7);
  2748. }
  2749. return imagepng($image, $filename, 7);
  2750. }
  2751. /**
  2752. * Assign the proper background to a gd image
  2753. *
  2754. * @param resource $image gd image resource
  2755. * @param string $bgcolor background color in #rrggbb format
  2756. */
  2757. protected function gdImageBackground($image, $bgcolor){
  2758. if( $bgcolor == 'transparent' ){
  2759. imagesavealpha($image,true);
  2760. $bgcolor1 = imagecolorallocatealpha($image, 255, 255, 255, 127);
  2761. }else{
  2762. list($r, $g, $b) = sscanf($bgcolor, "#%02x%02x%02x");
  2763. $bgcolor1 = imagecolorallocate($image, $r, $g, $b);
  2764. }
  2765. imagefill($image, 0, 0, $bgcolor1);
  2766. }
  2767. /*********************** misc *************************/
  2768. /**
  2769. * Return smart formatted date
  2770. *
  2771. * @param int $ts file timestamp
  2772. * @return string
  2773. * @author Dmitry (dio) Levashov
  2774. **/
  2775. // protected function formatDate($ts) {
  2776. // if ($ts > $this->today) {
  2777. // return 'Today '.date($this->options['timeFormat'], $ts);
  2778. // }
  2779. //
  2780. // if ($ts > $this->yesterday) {
  2781. // return 'Yesterday '.date($this->options['timeFormat'], $ts);
  2782. // }
  2783. //
  2784. // return date($this->options['dateFormat'], $ts);
  2785. // }
  2786. /**
  2787. * Find position of first occurrence of string in a string with multibyte support
  2788. *
  2789. * @param string $haystack The string being checked.
  2790. * @param string $needle The string to find in haystack.
  2791. * @param int $offset The search offset. If it is not specified, 0 is used.
  2792. * @return int|bool
  2793. * @author Alexey Sukhotin
  2794. **/
  2795. protected function stripos($haystack , $needle , $offset = 0) {
  2796. if (function_exists('mb_stripos')) {
  2797. return mb_stripos($haystack , $needle , $offset);
  2798. } else if (function_exists('mb_strtolower') && function_exists('mb_strpos')) {
  2799. return mb_strpos(mb_strtolower($haystack), mb_strtolower($needle), $offset);
  2800. }
  2801. return stripos($haystack , $needle , $offset);
  2802. }
  2803. /**
  2804. * Get server side available archivers
  2805. *
  2806. * @param bool $use_cache
  2807. * @return array
  2808. */
  2809. protected function getArchivers($use_cache = true) {
  2810. if (!function_exists('proc_open')) {
  2811. return array();
  2812. }
  2813. if ($use_cache && isset($_SESSION['ELFINDER_ARCHIVERS_CACHE']) && is_array($_SESSION['ELFINDER_ARCHIVERS_CACHE'])) {
  2814. return $_SESSION['ELFINDER_ARCHIVERS_CACHE'];
  2815. }
  2816. $arcs = array(
  2817. 'create' => array(),
  2818. 'extract' => array()
  2819. );
  2820. $this->procExec('tar --version', $o, $ctar);
  2821. if ($ctar == 0) {
  2822. $arcs['create']['application/x-tar'] = array('cmd' => 'tar', 'argc' => '-cf', 'ext' => 'tar');
  2823. $arcs['extract']['application/x-tar'] = array('cmd' => 'tar', 'argc' => '-xf', 'ext' => 'tar');
  2824. unset($o);
  2825. $test = $this->procExec('gzip --version', $o, $c);
  2826. if ($c == 0) {
  2827. $arcs['create']['application/x-gzip'] = array('cmd' => 'tar', 'argc' => '-czf', 'ext' => 'tgz');
  2828. $arcs['extract']['application/x-gzip'] = array('cmd' => 'tar', 'argc' => '-xzf', 'ext' => 'tgz');
  2829. }
  2830. unset($o);
  2831. $test = $this->procExec('bzip2 --version', $o, $c);
  2832. if ($c == 0) {
  2833. $arcs['create']['application/x-bzip2'] = array('cmd' => 'tar', 'argc' => '-cjf', 'ext' => 'tbz');
  2834. $arcs['extract']['application/x-bzip2'] = array('cmd' => 'tar', 'argc' => '-xjf', 'ext' => 'tbz');
  2835. }
  2836. }
  2837. unset($o);
  2838. $this->procExec('zip -v', $o, $c);
  2839. if ($c == 0) {
  2840. $arcs['create']['application/zip'] = array('cmd' => 'zip', 'argc' => '-r9', 'ext' => 'zip');
  2841. }
  2842. unset($o);
  2843. $this->procExec('unzip --help', $o, $c);
  2844. if ($c == 0) {
  2845. $arcs['extract']['application/zip'] = array('cmd' => 'unzip', 'argc' => '', 'ext' => 'zip');
  2846. }
  2847. unset($o);
  2848. $this->procExec('rar --version', $o, $c);
  2849. if ($c == 0 || $c == 7) {
  2850. $arcs['create']['application/x-rar'] = array('cmd' => 'rar', 'argc' => 'a -inul', 'ext' => 'rar');
  2851. $arcs['extract']['application/x-rar'] = array('cmd' => 'rar', 'argc' => 'x -y', 'ext' => 'rar');
  2852. } else {
  2853. unset($o);
  2854. $test = $this->procExec('unrar', $o, $c);
  2855. if ($c==0 || $c == 7) {
  2856. $arcs['extract']['application/x-rar'] = array('cmd' => 'unrar', 'argc' => 'x -y', 'ext' => 'rar');
  2857. }
  2858. }
  2859. unset($o);
  2860. $this->procExec('7za --help', $o, $c);
  2861. if ($c == 0) {
  2862. $arcs['create']['application/x-7z-compressed'] = array('cmd' => '7za', 'argc' => 'a', 'ext' => '7z');
  2863. $arcs['extract']['application/x-7z-compressed'] = array('cmd' => '7za', 'argc' => 'e -y', 'ext' => '7z');
  2864. if (empty($arcs['create']['application/x-gzip'])) {
  2865. $arcs['create']['application/x-gzip'] = array('cmd' => '7za', 'argc' => 'a -tgzip', 'ext' => 'tar.gz');
  2866. }
  2867. if (empty($arcs['extract']['application/x-gzip'])) {
  2868. $arcs['extract']['application/x-gzip'] = array('cmd' => '7za', 'argc' => 'e -tgzip -y', 'ext' => 'tar.gz');
  2869. }
  2870. if (empty($arcs['create']['application/x-bzip2'])) {
  2871. $arcs['create']['application/x-bzip2'] = array('cmd' => '7za', 'argc' => 'a -tbzip2', 'ext' => 'tar.bz');
  2872. }
  2873. if (empty($arcs['extract']['application/x-bzip2'])) {
  2874. $arcs['extract']['application/x-bzip2'] = array('cmd' => '7za', 'argc' => 'a -tbzip2 -y', 'ext' => 'tar.bz');
  2875. }
  2876. if (empty($arcs['create']['application/zip'])) {
  2877. $arcs['create']['application/zip'] = array('cmd' => '7za', 'argc' => 'a -tzip -l', 'ext' => 'zip');
  2878. }
  2879. if (empty($arcs['extract']['application/zip'])) {
  2880. $arcs['extract']['application/zip'] = array('cmd' => '7za', 'argc' => 'e -tzip -y', 'ext' => 'zip');
  2881. }
  2882. if (empty($arcs['create']['application/x-tar'])) {
  2883. $arcs['create']['application/x-tar'] = array('cmd' => '7za', 'argc' => 'a -ttar -l', 'ext' => 'tar');
  2884. }
  2885. if (empty($arcs['extract']['application/x-tar'])) {
  2886. $arcs['extract']['application/x-tar'] = array('cmd' => '7za', 'argc' => 'e -ttar -y', 'ext' => 'tar');
  2887. }
  2888. } else if (substr(PHP_OS,0,3) === 'WIN') {
  2889. // check `7z` for Windows server.
  2890. unset($o);
  2891. $this->procExec('7z', $o, $c);
  2892. if ($c == 0) {
  2893. $arcs['create']['application/x-7z-compressed'] = array('cmd' => '7z', 'argc' => 'a -mx0', 'ext' => '7z');
  2894. $arcs['extract']['application/x-7z-compressed'] = array('cmd' => '7z', 'argc' => 'x -y', 'ext' => '7z');
  2895. if (empty($arcs['create']['application/x-gzip'])) {
  2896. $arcs['create']['application/x-gzip'] = array('cmd' => '7z', 'argc' => 'a -tgzip -mx0', 'ext' => 'tar.gz');
  2897. }
  2898. if (empty($arcs['extract']['application/x-gzip'])) {
  2899. $arcs['extract']['application/x-gzip'] = array('cmd' => '7z', 'argc' => 'x -tgzip -y', 'ext' => 'tar.gz');
  2900. }
  2901. if (empty($arcs['create']['application/x-bzip2'])) {
  2902. $arcs['create']['application/x-bzip2'] = array('cmd' => '7z', 'argc' => 'a -tbzip2 -mx0', 'ext' => 'tar.bz');
  2903. }
  2904. if (empty($arcs['extract']['application/x-bzip2'])) {
  2905. $arcs['extract']['application/x-bzip2'] = array('cmd' => '7z', 'argc' => 'x -tbzip2 -y', 'ext' => 'tar.bz');
  2906. }
  2907. if (empty($arcs['create']['application/zip'])) {
  2908. $arcs['create']['application/zip'] = array('cmd' => '7z', 'argc' => 'a -tzip -l -mx0', 'ext' => 'zip');
  2909. }
  2910. if (empty($arcs['extract']['application/zip'])) {
  2911. $arcs['extract']['application/zip'] = array('cmd' => '7z', 'argc' => 'x -tzip -y', 'ext' => 'zip');
  2912. }
  2913. if (empty($arcs['create']['application/x-tar'])) {
  2914. $arcs['create']['application/x-tar'] = array('cmd' => '7z', 'argc' => 'a -ttar -l -mx0', 'ext' => 'tar');
  2915. }
  2916. if (empty($arcs['extract']['application/x-tar'])) {
  2917. $arcs['extract']['application/x-tar'] = array('cmd' => '7z', 'argc' => 'x -ttar -y', 'ext' => 'tar');
  2918. }
  2919. if (empty($arcs['create']['application/x-rar'])) {
  2920. $arcs['create']['application/x-rar'] = array('cmd' => '7z', 'argc' => 'a -trar -l -mx0', 'ext' => 'rar');
  2921. }
  2922. if (empty($arcs['extract']['application/x-rar'])) {
  2923. $arcs['extract']['application/x-rar'] = array('cmd' => '7z', 'argc' => 'x -trar -y', 'ext' => 'rar');
  2924. }
  2925. }
  2926. }
  2927. $_SESSION['ELFINDER_ARCHIVERS_CACHE'] = $arcs;
  2928. return $arcs;
  2929. }
  2930. /**==================================* abstract methods *====================================**/
  2931. /*********************** paths/urls *************************/
  2932. /**
  2933. * Return parent directory path
  2934. *
  2935. * @param string $path file path
  2936. * @return string
  2937. * @author Dmitry (dio) Levashov
  2938. **/
  2939. abstract protected function _dirname($path);
  2940. /**
  2941. * Return file name
  2942. *
  2943. * @param string $path file path
  2944. * @return string
  2945. * @author Dmitry (dio) Levashov
  2946. **/
  2947. abstract protected function _basename($path);
  2948. /**
  2949. * Join dir name and file name and return full path.
  2950. * Some drivers (db) use int as path - so we give to concat path to driver itself
  2951. *
  2952. * @param string $dir dir path
  2953. * @param string $name file name
  2954. * @return string
  2955. * @author Dmitry (dio) Levashov
  2956. **/
  2957. abstract protected function _joinPath($dir, $name);
  2958. /**
  2959. * Return normalized path
  2960. *
  2961. * @param string $path file path
  2962. * @return string
  2963. * @author Dmitry (dio) Levashov
  2964. **/
  2965. abstract protected function _normpath($path);
  2966. /**
  2967. * Return file path related to root dir
  2968. *
  2969. * @param string $path file path
  2970. * @return string
  2971. * @author Dmitry (dio) Levashov
  2972. **/
  2973. abstract protected function _relpath($path);
  2974. /**
  2975. * Convert path related to root dir into real path
  2976. *
  2977. * @param string $path rel file path
  2978. * @return string
  2979. * @author Dmitry (dio) Levashov
  2980. **/
  2981. abstract protected function _abspath($path);
  2982. /**
  2983. * Return fake path started from root dir.
  2984. * Required to show path on client side.
  2985. *
  2986. * @param string $path file path
  2987. * @return string
  2988. * @author Dmitry (dio) Levashov
  2989. **/
  2990. abstract protected function _path($path);
  2991. /**
  2992. * Return true if $path is children of $parent
  2993. *
  2994. * @param string $path path to check
  2995. * @param string $parent parent path
  2996. * @return bool
  2997. * @author Dmitry (dio) Levashov
  2998. **/
  2999. abstract protected function _inpath($path, $parent);
  3000. /**
  3001. * Return stat for given path.
  3002. * Stat contains following fields:
  3003. * - (int) size file size in b. required
  3004. * - (int) ts file modification time in unix time. required
  3005. * - (string) mime mimetype. required for folders, others - optionally
  3006. * - (bool) read read permissions. required
  3007. * - (bool) write write permissions. required
  3008. * - (bool) locked is object locked. optionally
  3009. * - (bool) hidden is object hidden. optionally
  3010. * - (string) alias for symlinks - link target path relative to root path. optionally
  3011. * - (string) target for symlinks - link target path. optionally
  3012. *
  3013. * If file does not exists - returns empty array or false.
  3014. *
  3015. * @param string $path file path
  3016. * @return array|false
  3017. * @author Dmitry (dio) Levashov
  3018. **/
  3019. abstract protected function _stat($path);
  3020. /***************** file stat ********************/
  3021. /**
  3022. * Return true if path is dir and has at least one childs directory
  3023. *
  3024. * @param string $path dir path
  3025. * @return bool
  3026. * @author Dmitry (dio) Levashov
  3027. **/
  3028. abstract protected function _subdirs($path);
  3029. /**
  3030. * Return object width and height
  3031. * Ususaly used for images, but can be realize for video etc...
  3032. *
  3033. * @param string $path file path
  3034. * @param string $mime file mime type
  3035. * @return string
  3036. * @author Dmitry (dio) Levashov
  3037. **/
  3038. abstract protected function _dimensions($path, $mime);
  3039. /******************** file/dir content *********************/
  3040. /**
  3041. * Return files list in directory
  3042. *
  3043. * @param string $path dir path
  3044. * @return array
  3045. * @author Dmitry (dio) Levashov
  3046. **/
  3047. abstract protected function _scandir($path);
  3048. /**
  3049. * Open file and return file pointer
  3050. *
  3051. * @param string $path file path
  3052. * @param bool $write open file for writing
  3053. * @return resource|false
  3054. * @author Dmitry (dio) Levashov
  3055. **/
  3056. abstract protected function _fopen($path, $mode="rb");
  3057. /**
  3058. * Close opened file
  3059. *
  3060. * @param resource $fp file pointer
  3061. * @param string $path file path
  3062. * @return bool
  3063. * @author Dmitry (dio) Levashov
  3064. **/
  3065. abstract protected function _fclose($fp, $path='');
  3066. /******************** file/dir manipulations *************************/
  3067. /**
  3068. * Create dir and return created dir path or false on failed
  3069. *
  3070. * @param string $path parent dir path
  3071. * @param string $name new directory name
  3072. * @return string|bool
  3073. * @author Dmitry (dio) Levashov
  3074. **/
  3075. abstract protected function _mkdir($path, $name);
  3076. /**
  3077. * Create file and return it's path or false on failed
  3078. *
  3079. * @param string $path parent dir path
  3080. * @param string $name new file name
  3081. * @return string|bool
  3082. * @author Dmitry (dio) Levashov
  3083. **/
  3084. abstract protected function _mkfile($path, $name);
  3085. /**
  3086. * Create symlink
  3087. *
  3088. * @param string $source file to link to
  3089. * @param string $targetDir folder to create link in
  3090. * @param string $name symlink name
  3091. * @return bool
  3092. * @author Dmitry (dio) Levashov
  3093. **/
  3094. abstract protected function _symlink($source, $targetDir, $name);
  3095. /**
  3096. * Copy file into another file (only inside one volume)
  3097. *
  3098. * @param string $source source file path
  3099. * @param string $target target dir path
  3100. * @param string $name file name
  3101. * @return bool
  3102. * @author Dmitry (dio) Levashov
  3103. **/
  3104. abstract protected function _copy($source, $targetDir, $name);
  3105. /**
  3106. * Move file into another parent dir.
  3107. * Return new file path or false.
  3108. *
  3109. * @param string $source source file path
  3110. * @param string $target target dir path
  3111. * @param string $name file name
  3112. * @return string|bool
  3113. * @author Dmitry (dio) Levashov
  3114. **/
  3115. abstract protected function _move($source, $targetDir, $name);
  3116. /**
  3117. * Remove file
  3118. *
  3119. * @param string $path file path
  3120. * @return bool
  3121. * @author Dmitry (dio) Levashov
  3122. **/
  3123. abstract protected function _unlink($path);
  3124. /**
  3125. * Remove dir
  3126. *
  3127. * @param string $path dir path
  3128. * @return bool
  3129. * @author Dmitry (dio) Levashov
  3130. **/
  3131. abstract protected function _rmdir($path);
  3132. /**
  3133. * Create new file and write into it from file pointer.
  3134. * Return new file path or false on error.
  3135. *
  3136. * @param resource $fp file pointer
  3137. * @param string $dir target dir path
  3138. * @param string $name file name
  3139. * @param array $stat file stat (required by some virtual fs)
  3140. * @return bool|string
  3141. * @author Dmitry (dio) Levashov
  3142. **/
  3143. abstract protected function _save($fp, $dir, $name, $stat);
  3144. /**
  3145. * Get file contents
  3146. *
  3147. * @param string $path file path
  3148. * @return string|false
  3149. * @author Dmitry (dio) Levashov
  3150. **/
  3151. abstract protected function _getContents($path);
  3152. /**
  3153. * Write a string to a file
  3154. *
  3155. * @param string $path file path
  3156. * @param string $content new file content
  3157. * @return bool
  3158. * @author Dmitry (dio) Levashov
  3159. **/
  3160. abstract protected function _filePutContents($path, $content);
  3161. /**
  3162. * Extract files from archive
  3163. *
  3164. * @param string $path file path
  3165. * @param array $arc archiver options
  3166. * @return bool
  3167. * @author Dmitry (dio) Levashov,
  3168. * @author Alexey Sukhotin
  3169. **/
  3170. abstract protected function _extract($path, $arc);
  3171. /**
  3172. * Create archive and return its path
  3173. *
  3174. * @param string $dir target dir
  3175. * @param array $files files names list
  3176. * @param string $name archive name
  3177. * @param array $arc archiver options
  3178. * @return string|bool
  3179. * @author Dmitry (dio) Levashov,
  3180. * @author Alexey Sukhotin
  3181. **/
  3182. abstract protected function _archive($dir, $files, $name, $arc);
  3183. /**
  3184. * Detect available archivers
  3185. *
  3186. * @return void
  3187. * @author Dmitry (dio) Levashov,
  3188. * @author Alexey Sukhotin
  3189. **/
  3190. abstract protected function _checkArchivers();
  3191. } // END class