/[dtapublic]/projs/dtats/trunk/projs/20161008_web_page_thumbnail_make/pics_filenames_canonize.php
ViewVC logotype

Contents of /projs/dtats/trunk/projs/20161008_web_page_thumbnail_make/pics_filenames_canonize.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 112 - (show annotations) (download)
Sat Dec 31 19:45:55 2016 UTC (7 years, 5 months ago) by dashley
File size: 47773 byte(s)
Update filenames for clarity.
1 <?php
2 //-------------------------------------------------------------------------------------------------
3 //$Header$
4 //-------------------------------------------------------------------------------------------------
5 //This source code and any program in which it is compiled/used is provided under the MIT
6 //LICENSE (full license text below).
7 //-------------------------------------------------------------------------------------------------
8 //Copyright (c) 2016 David T. Ashley
9 //
10 //Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
11 //associated documentation files (the "Software"), to deal in the Software without restriction,
12 //including without limitation the rights to use, copy, modify, merge, publish, distribute,
13 //sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
14 //furnished to do so, subject to the following conditions:
15 //
16 //The above copyright notice and this permission notice shall be included in all copies or
17 //substantial portions of the Software.
18 //
19 //THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
20 //NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 //NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
22 //DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 //OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 //-------------------------------------------------------------------------------------------------
25 //This program, a PHP script, modifies all filenames in a directory to a canonical
26 //form (no upper-case letters or unusual characters). This program is part of a
27 //3-program suite designed to collectively create a web page directly from
28 //digital camera files (and of course the web page can be customized by
29 //hand-editing after it is automatically generated). The supported process for
30 //creating a web page involves 3 programs (rather than 1) primarily to circumvent
31 //the involuntary process termination that will occur in some shared environments
32 //if a process consumes too much CPU time.
33 //
34 //The 3 programs in the 3-program suite are:
35 //
36 // filenames_canonize.php (this program):
37 // Converts all names of files in a directory to lower-case, and makes
38 // substitutions for any unusual characters.
39 //
40 // thumbnails_make.php:
41 // Creates thumbnails from recognized image types. The thumbnails are
42 // named relative to the original image file with the suffix "_small".
43 // Only 20 files are converted on each invocation of the program, to
44 // avoid the involuntary process termination that typically occurs in
45 // a shared hosting environment when a process uses too much CPU time.
46 // The program should be run repeatedly until it indicates that it has
47 // no more thumbnails to create.
48 //
49 // If any full-sized photos are modified, any corresponding thumbnails
50 // should be deleted and thumbnails_make.php and indexfile_make.php
51 // should be run again.
52 //
53 // indexfile_make.php
54 // Scans a directory and makes an index file ("index2.php") displaying
55 // all the thumbnail images, each of which link to the corresponding
56 // full-sized image. The index file is tailored to Dave Ashley's
57 // needs, but the created file can be edited and most of the content
58 // pasted into an HTML file. To avoid the accidental loss of
59 // information, any existing "index2.php" file is renamed out of the
60 // way.
61 //
62 //This script is designed to be run manually (rather than automatically invoked
63 //as a result of a web page request). It was written in PHP for convenience
64 //simply because DreamHost (the web hosting company Dave Ashley uses) has as part
65 //of its hosting environment PHP with the ImageMagick library compiled in.
66 //
67 //Usually, this script is invoked using "php <path>/filenames_canonize.php", but
68 //the method of invocation may vary based on computing platform details.
69 //--------------------------------------------------------------------------------
70 //This program is free software: you can redistribute it and/or modify
71 //it under the terms of the GNU General Public License as published by
72 //the Free Software Foundation, either version 3 of the License, or
73 //(at your option) any later version.
74 //
75 //This program is distributed in the hope that it will be useful,
76 //but WITHOUT ANY WARRANTY; without even the implied warranty of
77 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
78 //GNU General Public License for more details.
79 //
80 //The GNU General Public License is reproduced below, and also is
81 //available at http://www.gnu.org/licenses/.
82 //--------------------------------------------------------------------------------
83 // GNU GENERAL PUBLIC LICENSE
84 // Version 3, 29 June 2007
85 //
86 // Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
87 // Everyone is permitted to copy and distribute verbatim copies
88 // of this license document, but changing it is not allowed.
89 //
90 // Preamble
91 //
92 // The GNU General Public License is a free, copyleft license for
93 // software and other kinds of works.
94 //
95 // The licenses for most software and other practical works are designed
96 // to take away your freedom to share and change the works. By contrast,
97 // the GNU General Public License is intended to guarantee your freedom to
98 // share and change all versions of a program--to make sure it remains free
99 // software for all its users. We, the Free Software Foundation, use the
100 // GNU General Public License for most of our software; it applies also to
101 // any other work released this way by its authors. You can apply it to
102 // your programs, too.
103 //
104 // When we speak of free software, we are referring to freedom, not
105 // price. Our General Public Licenses are designed to make sure that you
106 // have the freedom to distribute copies of free software (and charge for
107 // them if you wish), that you receive source code or can get it if you
108 // want it, that you can change the software or use pieces of it in new
109 // free programs, and that you know you can do these things.
110 //
111 // To protect your rights, we need to prevent others from denying you
112 // these rights or asking you to surrender the rights. Therefore, you have
113 // certain responsibilities if you distribute copies of the software, or if
114 // you modify it: responsibilities to respect the freedom of others.
115 //
116 // For example, if you distribute copies of such a program, whether
117 // gratis or for a fee, you must pass on to the recipients the same
118 // freedoms that you received. You must make sure that they, too, receive
119 // or can get the source code. And you must show them these terms so they
120 // know their rights.
121 //
122 // Developers that use the GNU GPL protect your rights with two steps:
123 // (1) assert copyright on the software, and (2) offer you this License
124 // giving you legal permission to copy, distribute and/or modify it.
125 //
126 // For the developers' and authors' protection, the GPL clearly explains
127 // that there is no warranty for this free software. For both users' and
128 // authors' sake, the GPL requires that modified versions be marked as
129 // changed, so that their problems will not be attributed erroneously to
130 // authors of previous versions.
131 //
132 // Some devices are designed to deny users access to install or run
133 // modified versions of the software inside them, although the manufacturer
134 // can do so. This is fundamentally incompatible with the aim of
135 // protecting users' freedom to change the software. The systematic
136 // pattern of such abuse occurs in the area of products for individuals to
137 // use, which is precisely where it is most unacceptable. Therefore, we
138 // have designed this version of the GPL to prohibit the practice for those
139 // products. If such problems arise substantially in other domains, we
140 // stand ready to extend this provision to those domains in future versions
141 // of the GPL, as needed to protect the freedom of users.
142 //
143 // Finally, every program is threatened constantly by software patents.
144 // States should not allow patents to restrict development and use of
145 // software on general-purpose computers, but in those that do, we wish to
146 // avoid the special danger that patents applied to a free program could
147 // make it effectively proprietary. To prevent this, the GPL assures that
148 // patents cannot be used to render the program non-free.
149 //
150 // The precise terms and conditions for copying, distribution and
151 // modification follow.
152 //
153 // TERMS AND CONDITIONS
154 //
155 // 0. Definitions.
156 //
157 // "This License" refers to version 3 of the GNU General Public License.
158 //
159 // "Copyright" also means copyright-like laws that apply to other kinds of
160 // works, such as semiconductor masks.
161 //
162 // "The Program" refers to any copyrightable work licensed under this
163 // License. Each licensee is addressed as "you". "Licensees" and
164 // "recipients" may be individuals or organizations.
165 //
166 // To "modify" a work means to copy from or adapt all or part of the work
167 // in a fashion requiring copyright permission, other than the making of an
168 // exact copy. The resulting work is called a "modified version" of the
169 // earlier work or a work "based on" the earlier work.
170 //
171 // A "covered work" means either the unmodified Program or a work based
172 // on the Program.
173 //
174 // To "propagate" a work means to do anything with it that, without
175 // permission, would make you directly or secondarily liable for
176 // infringement under applicable copyright law, except executing it on a
177 // computer or modifying a private copy. Propagation includes copying,
178 // distribution (with or without modification), making available to the
179 // public, and in some countries other activities as well.
180 //
181 // To "convey" a work means any kind of propagation that enables other
182 // parties to make or receive copies. Mere interaction with a user through
183 // a computer network, with no transfer of a copy, is not conveying.
184 //
185 // An interactive user interface displays "Appropriate Legal Notices"
186 // to the extent that it includes a convenient and prominently visible
187 // feature that (1) displays an appropriate copyright notice, and (2)
188 // tells the user that there is no warranty for the work (except to the
189 // extent that warranties are provided), that licensees may convey the
190 // work under this License, and how to view a copy of this License. If
191 // the interface presents a list of user commands or options, such as a
192 // menu, a prominent item in the list meets this criterion.
193 //
194 // 1. Source Code.
195 //
196 // The "source code" for a work means the preferred form of the work
197 // for making modifications to it. "Object code" means any non-source
198 // form of a work.
199 //
200 // A "Standard Interface" means an interface that either is an official
201 // standard defined by a recognized standards body, or, in the case of
202 // interfaces specified for a particular programming language, one that
203 // is widely used among developers working in that language.
204 //
205 // The "System Libraries" of an executable work include anything, other
206 // than the work as a whole, that (a) is included in the normal form of
207 // packaging a Major Component, but which is not part of that Major
208 // Component, and (b) serves only to enable use of the work with that
209 // Major Component, or to implement a Standard Interface for which an
210 // implementation is available to the public in source code form. A
211 // "Major Component", in this context, means a major essential component
212 // (kernel, window system, and so on) of the specific operating system
213 // (if any) on which the executable work runs, or a compiler used to
214 // produce the work, or an object code interpreter used to run it.
215 //
216 // The "Corresponding Source" for a work in object code form means all
217 // the source code needed to generate, install, and (for an executable
218 // work) run the object code and to modify the work, including scripts to
219 // control those activities. However, it does not include the work's
220 // System Libraries, or general-purpose tools or generally available free
221 // programs which are used unmodified in performing those activities but
222 // which are not part of the work. For example, Corresponding Source
223 // includes interface definition files associated with source files for
224 // the work, and the source code for shared libraries and dynamically
225 // linked subprograms that the work is specifically designed to require,
226 // such as by intimate data communication or control flow between those
227 // subprograms and other parts of the work.
228 //
229 // The Corresponding Source need not include anything that users
230 // can regenerate automatically from other parts of the Corresponding
231 // Source.
232 //
233 // The Corresponding Source for a work in source code form is that
234 // same work.
235 //
236 // 2. Basic Permissions.
237 //
238 // All rights granted under this License are granted for the term of
239 // copyright on the Program, and are irrevocable provided the stated
240 // conditions are met. This License explicitly affirms your unlimited
241 // permission to run the unmodified Program. The output from running a
242 // covered work is covered by this License only if the output, given its
243 // content, constitutes a covered work. This License acknowledges your
244 // rights of fair use or other equivalent, as provided by copyright law.
245 //
246 // You may make, run and propagate covered works that you do not
247 // convey, without conditions so long as your license otherwise remains
248 // in force. You may convey covered works to others for the sole purpose
249 // of having them make modifications exclusively for you, or provide you
250 // with facilities for running those works, provided that you comply with
251 // the terms of this License in conveying all material for which you do
252 // not control copyright. Those thus making or running the covered works
253 // for you must do so exclusively on your behalf, under your direction
254 // and control, on terms that prohibit them from making any copies of
255 // your copyrighted material outside their relationship with you.
256 //
257 // Conveying under any other circumstances is permitted solely under
258 // the conditions stated below. Sublicensing is not allowed; section 10
259 // makes it unnecessary.
260 //
261 // 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
262 //
263 // No covered work shall be deemed part of an effective technological
264 // measure under any applicable law fulfilling obligations under article
265 // 11 of the WIPO copyright treaty adopted on 20 December 1996, or
266 // similar laws prohibiting or restricting circumvention of such
267 // measures.
268 //
269 // When you convey a covered work, you waive any legal power to forbid
270 // circumvention of technological measures to the extent such circumvention
271 // is effected by exercising rights under this License with respect to
272 // the covered work, and you disclaim any intention to limit operation or
273 // modification of the work as a means of enforcing, against the work's
274 // users, your or third parties' legal rights to forbid circumvention of
275 // technological measures.
276 //
277 // 4. Conveying Verbatim Copies.
278 //
279 // You may convey verbatim copies of the Program's source code as you
280 // receive it, in any medium, provided that you conspicuously and
281 // appropriately publish on each copy an appropriate copyright notice;
282 // keep intact all notices stating that this License and any
283 // non-permissive terms added in accord with section 7 apply to the code;
284 // keep intact all notices of the absence of any warranty; and give all
285 // recipients a copy of this License along with the Program.
286 //
287 // You may charge any price or no price for each copy that you convey,
288 // and you may offer support or warranty protection for a fee.
289 //
290 // 5. Conveying Modified Source Versions.
291 //
292 // You may convey a work based on the Program, or the modifications to
293 // produce it from the Program, in the form of source code under the
294 // terms of section 4, provided that you also meet all of these conditions:
295 //
296 // a) The work must carry prominent notices stating that you modified
297 // it, and giving a relevant date.
298 //
299 // b) The work must carry prominent notices stating that it is
300 // released under this License and any conditions added under section
301 // 7. This requirement modifies the requirement in section 4 to
302 // "keep intact all notices".
303 //
304 // c) You must license the entire work, as a whole, under this
305 // License to anyone who comes into possession of a copy. This
306 // License will therefore apply, along with any applicable section 7
307 // additional terms, to the whole of the work, and all its parts,
308 // regardless of how they are packaged. This License gives no
309 // permission to license the work in any other way, but it does not
310 // invalidate such permission if you have separately received it.
311 //
312 // d) If the work has interactive user interfaces, each must display
313 // Appropriate Legal Notices; however, if the Program has interactive
314 // interfaces that do not display Appropriate Legal Notices, your
315 // work need not make them do so.
316 //
317 // A compilation of a covered work with other separate and independent
318 // works, which are not by their nature extensions of the covered work,
319 // and which are not combined with it such as to form a larger program,
320 // in or on a volume of a storage or distribution medium, is called an
321 // "aggregate" if the compilation and its resulting copyright are not
322 // used to limit the access or legal rights of the compilation's users
323 // beyond what the individual works permit. Inclusion of a covered work
324 // in an aggregate does not cause this License to apply to the other
325 // parts of the aggregate.
326 //
327 // 6. Conveying Non-Source Forms.
328 //
329 // You may convey a covered work in object code form under the terms
330 // of sections 4 and 5, provided that you also convey the
331 // machine-readable Corresponding Source under the terms of this License,
332 // in one of these ways:
333 //
334 // a) Convey the object code in, or embodied in, a physical product
335 // (including a physical distribution medium), accompanied by the
336 // Corresponding Source fixed on a durable physical medium
337 // customarily used for software interchange.
338 //
339 // b) Convey the object code in, or embodied in, a physical product
340 // (including a physical distribution medium), accompanied by a
341 // written offer, valid for at least three years and valid for as
342 // long as you offer spare parts or customer support for that product
343 // model, to give anyone who possesses the object code either (1) a
344 // copy of the Corresponding Source for all the software in the
345 // product that is covered by this License, on a durable physical
346 // medium customarily used for software interchange, for a price no
347 // more than your reasonable cost of physically performing this
348 // conveying of source, or (2) access to copy the
349 // Corresponding Source from a network server at no charge.
350 //
351 // c) Convey individual copies of the object code with a copy of the
352 // written offer to provide the Corresponding Source. This
353 // alternative is allowed only occasionally and noncommercially, and
354 // only if you received the object code with such an offer, in accord
355 // with subsection 6b.
356 //
357 // d) Convey the object code by offering access from a designated
358 // place (gratis or for a charge), and offer equivalent access to the
359 // Corresponding Source in the same way through the same place at no
360 // further charge. You need not require recipients to copy the
361 // Corresponding Source along with the object code. If the place to
362 // copy the object code is a network server, the Corresponding Source
363 // may be on a different server (operated by you or a third party)
364 // that supports equivalent copying facilities, provided you maintain
365 // clear directions next to the object code saying where to find the
366 // Corresponding Source. Regardless of what server hosts the
367 // Corresponding Source, you remain obligated to ensure that it is
368 // available for as long as needed to satisfy these requirements.
369 //
370 // e) Convey the object code using peer-to-peer transmission, provided
371 // you inform other peers where the object code and Corresponding
372 // Source of the work are being offered to the general public at no
373 // charge under subsection 6d.
374 //
375 // A separable portion of the object code, whose source code is excluded
376 // from the Corresponding Source as a System Library, need not be
377 // included in conveying the object code work.
378 //
379 // A "User Product" is either (1) a "consumer product", which means any
380 // tangible personal property which is normally used for personal, family,
381 // or household purposes, or (2) anything designed or sold for incorporation
382 // into a dwelling. In determining whether a product is a consumer product,
383 // doubtful cases shall be resolved in favor of coverage. For a particular
384 // product received by a particular user, "normally used" refers to a
385 // typical or common use of that class of product, regardless of the status
386 // of the particular user or of the way in which the particular user
387 // actually uses, or expects or is expected to use, the product. A product
388 // is a consumer product regardless of whether the product has substantial
389 // commercial, industrial or non-consumer uses, unless such uses represent
390 // the only significant mode of use of the product.
391 //
392 // "Installation Information" for a User Product means any methods,
393 // procedures, authorization keys, or other information required to install
394 // and execute modified versions of a covered work in that User Product from
395 // a modified version of its Corresponding Source. The information must
396 // suffice to ensure that the continued functioning of the modified object
397 // code is in no case prevented or interfered with solely because
398 // modification has been made.
399 //
400 // If you convey an object code work under this section in, or with, or
401 // specifically for use in, a User Product, and the conveying occurs as
402 // part of a transaction in which the right of possession and use of the
403 // User Product is transferred to the recipient in perpetuity or for a
404 // fixed term (regardless of how the transaction is characterized), the
405 // Corresponding Source conveyed under this section must be accompanied
406 // by the Installation Information. But this requirement does not apply
407 // if neither you nor any third party retains the ability to install
408 // modified object code on the User Product (for example, the work has
409 // been installed in ROM).
410 //
411 // The requirement to provide Installation Information does not include a
412 // requirement to continue to provide support service, warranty, or updates
413 // for a work that has been modified or installed by the recipient, or for
414 // the User Product in which it has been modified or installed. Access to a
415 // network may be denied when the modification itself materially and
416 // adversely affects the operation of the network or violates the rules and
417 // protocols for communication across the network.
418 //
419 // Corresponding Source conveyed, and Installation Information provided,
420 // in accord with this section must be in a format that is publicly
421 // documented (and with an implementation available to the public in
422 // source code form), and must require no special password or key for
423 // unpacking, reading or copying.
424 //
425 // 7. Additional Terms.
426 //
427 // "Additional permissions" are terms that supplement the terms of this
428 // License by making exceptions from one or more of its conditions.
429 // Additional permissions that are applicable to the entire Program shall
430 // be treated as though they were included in this License, to the extent
431 // that they are valid under applicable law. If additional permissions
432 // apply only to part of the Program, that part may be used separately
433 // under those permissions, but the entire Program remains governed by
434 // this License without regard to the additional permissions.
435 //
436 // When you convey a copy of a covered work, you may at your option
437 // remove any additional permissions from that copy, or from any part of
438 // it. (Additional permissions may be written to require their own
439 // removal in certain cases when you modify the work.) You may place
440 // additional permissions on material, added by you to a covered work,
441 // for which you have or can give appropriate copyright permission.
442 //
443 // Notwithstanding any other provision of this License, for material you
444 // add to a covered work, you may (if authorized by the copyright holders of
445 // that material) supplement the terms of this License with terms:
446 //
447 // a) Disclaiming warranty or limiting liability differently from the
448 // terms of sections 15 and 16 of this License; or
449 //
450 // b) Requiring preservation of specified reasonable legal notices or
451 // author attributions in that material or in the Appropriate Legal
452 // Notices displayed by works containing it; or
453 //
454 // c) Prohibiting misrepresentation of the origin of that material, or
455 // requiring that modified versions of such material be marked in
456 // reasonable ways as different from the original version; or
457 //
458 // d) Limiting the use for publicity purposes of names of licensors or
459 // authors of the material; or
460 //
461 // e) Declining to grant rights under trademark law for use of some
462 // trade names, trademarks, or service marks; or
463 //
464 // f) Requiring indemnification of licensors and authors of that
465 // material by anyone who conveys the material (or modified versions of
466 // it) with contractual assumptions of liability to the recipient, for
467 // any liability that these contractual assumptions directly impose on
468 // those licensors and authors.
469 //
470 // All other non-permissive additional terms are considered "further
471 // restrictions" within the meaning of section 10. If the Program as you
472 // received it, or any part of it, contains a notice stating that it is
473 // governed by this License along with a term that is a further
474 // restriction, you may remove that term. If a license document contains
475 // a further restriction but permits relicensing or conveying under this
476 // License, you may add to a covered work material governed by the terms
477 // of that license document, provided that the further restriction does
478 // not survive such relicensing or conveying.
479 //
480 // If you add terms to a covered work in accord with this section, you
481 // must place, in the relevant source files, a statement of the
482 // additional terms that apply to those files, or a notice indicating
483 // where to find the applicable terms.
484 //
485 // Additional terms, permissive or non-permissive, may be stated in the
486 // form of a separately written license, or stated as exceptions;
487 // the above requirements apply either way.
488 //
489 // 8. Termination.
490 //
491 // You may not propagate or modify a covered work except as expressly
492 // provided under this License. Any attempt otherwise to propagate or
493 // modify it is void, and will automatically terminate your rights under
494 // this License (including any patent licenses granted under the third
495 // paragraph of section 11).
496 //
497 // However, if you cease all violation of this License, then your
498 // license from a particular copyright holder is reinstated (a)
499 // provisionally, unless and until the copyright holder explicitly and
500 // finally terminates your license, and (b) permanently, if the copyright
501 // holder fails to notify you of the violation by some reasonable means
502 // prior to 60 days after the cessation.
503 //
504 // Moreover, your license from a particular copyright holder is
505 // reinstated permanently if the copyright holder notifies you of the
506 // violation by some reasonable means, this is the first time you have
507 // received notice of violation of this License (for any work) from that
508 // copyright holder, and you cure the violation prior to 30 days after
509 // your receipt of the notice.
510 //
511 // Termination of your rights under this section does not terminate the
512 // licenses of parties who have received copies or rights from you under
513 // this License. If your rights have been terminated and not permanently
514 // reinstated, you do not qualify to receive new licenses for the same
515 // material under section 10.
516 //
517 // 9. Acceptance Not Required for Having Copies.
518 //
519 // You are not required to accept this License in order to receive or
520 // run a copy of the Program. Ancillary propagation of a covered work
521 // occurring solely as a consequence of using peer-to-peer transmission
522 // to receive a copy likewise does not require acceptance. However,
523 // nothing other than this License grants you permission to propagate or
524 // modify any covered work. These actions infringe copyright if you do
525 // not accept this License. Therefore, by modifying or propagating a
526 // covered work, you indicate your acceptance of this License to do so.
527 //
528 // 10. Automatic Licensing of Downstream Recipients.
529 //
530 // Each time you convey a covered work, the recipient automatically
531 // receives a license from the original licensors, to run, modify and
532 // propagate that work, subject to this License. You are not responsible
533 // for enforcing compliance by third parties with this License.
534 //
535 // An "entity transaction" is a transaction transferring control of an
536 // organization, or substantially all assets of one, or subdividing an
537 // organization, or merging organizations. If propagation of a covered
538 // work results from an entity transaction, each party to that
539 // transaction who receives a copy of the work also receives whatever
540 // licenses to the work the party's predecessor in interest had or could
541 // give under the previous paragraph, plus a right to possession of the
542 // Corresponding Source of the work from the predecessor in interest, if
543 // the predecessor has it or can get it with reasonable efforts.
544 //
545 // You may not impose any further restrictions on the exercise of the
546 // rights granted or affirmed under this License. For example, you may
547 // not impose a license fee, royalty, or other charge for exercise of
548 // rights granted under this License, and you may not initiate litigation
549 // (including a cross-claim or counterclaim in a lawsuit) alleging that
550 // any patent claim is infringed by making, using, selling, offering for
551 // sale, or importing the Program or any portion of it.
552 //
553 // 11. Patents.
554 //
555 // A "contributor" is a copyright holder who authorizes use under this
556 // License of the Program or a work on which the Program is based. The
557 // work thus licensed is called the contributor's "contributor version".
558 //
559 // A contributor's "essential patent claims" are all patent claims
560 // owned or controlled by the contributor, whether already acquired or
561 // hereafter acquired, that would be infringed by some manner, permitted
562 // by this License, of making, using, or selling its contributor version,
563 // but do not include claims that would be infringed only as a
564 // consequence of further modification of the contributor version. For
565 // purposes of this definition, "control" includes the right to grant
566 // patent sublicenses in a manner consistent with the requirements of
567 // this License.
568 //
569 // Each contributor grants you a non-exclusive, worldwide, royalty-free
570 // patent license under the contributor's essential patent claims, to
571 // make, use, sell, offer for sale, import and otherwise run, modify and
572 // propagate the contents of its contributor version.
573 //
574 // In the following three paragraphs, a "patent license" is any express
575 // agreement or commitment, however denominated, not to enforce a patent
576 // (such as an express permission to practice a patent or covenant not to
577 // sue for patent infringement). To "grant" such a patent license to a
578 // party means to make such an agreement or commitment not to enforce a
579 // patent against the party.
580 //
581 // If you convey a covered work, knowingly relying on a patent license,
582 // and the Corresponding Source of the work is not available for anyone
583 // to copy, free of charge and under the terms of this License, through a
584 // publicly available network server or other readily accessible means,
585 // then you must either (1) cause the Corresponding Source to be so
586 // available, or (2) arrange to deprive yourself of the benefit of the
587 // patent license for this particular work, or (3) arrange, in a manner
588 // consistent with the requirements of this License, to extend the patent
589 // license to downstream recipients. "Knowingly relying" means you have
590 // actual knowledge that, but for the patent license, your conveying the
591 // covered work in a country, or your recipient's use of the covered work
592 // in a country, would infringe one or more identifiable patents in that
593 // country that you have reason to believe are valid.
594 //
595 // If, pursuant to or in connection with a single transaction or
596 // arrangement, you convey, or propagate by procuring conveyance of, a
597 // covered work, and grant a patent license to some of the parties
598 // receiving the covered work authorizing them to use, propagate, modify
599 // or convey a specific copy of the covered work, then the patent license
600 // you grant is automatically extended to all recipients of the covered
601 // work and works based on it.
602 //
603 // A patent license is "discriminatory" if it does not include within
604 // the scope of its coverage, prohibits the exercise of, or is
605 // conditioned on the non-exercise of one or more of the rights that are
606 // specifically granted under this License. You may not convey a covered
607 // work if you are a party to an arrangement with a third party that is
608 // in the business of distributing software, under which you make payment
609 // to the third party based on the extent of your activity of conveying
610 // the work, and under which the third party grants, to any of the
611 // parties who would receive the covered work from you, a discriminatory
612 // patent license (a) in connection with copies of the covered work
613 // conveyed by you (or copies made from those copies), or (b) primarily
614 // for and in connection with specific products or compilations that
615 // contain the covered work, unless you entered into that arrangement,
616 // or that patent license was granted, prior to 28 March 2007.
617 //
618 // Nothing in this License shall be construed as excluding or limiting
619 // any implied license or other defenses to infringement that may
620 // otherwise be available to you under applicable patent law.
621 //
622 // 12. No Surrender of Others' Freedom.
623 //
624 // If conditions are imposed on you (whether by court order, agreement or
625 // otherwise) that contradict the conditions of this License, they do not
626 // excuse you from the conditions of this License. If you cannot convey a
627 // covered work so as to satisfy simultaneously your obligations under this
628 // License and any other pertinent obligations, then as a consequence you may
629 // not convey it at all. For example, if you agree to terms that obligate you
630 // to collect a royalty for further conveying from those to whom you convey
631 // the Program, the only way you could satisfy both those terms and this
632 // License would be to refrain entirely from conveying the Program.
633 //
634 // 13. Use with the GNU Affero General Public License.
635 //
636 // Notwithstanding any other provision of this License, you have
637 // permission to link or combine any covered work with a work licensed
638 // under version 3 of the GNU Affero General Public License into a single
639 // combined work, and to convey the resulting work. The terms of this
640 // License will continue to apply to the part which is the covered work,
641 // but the special requirements of the GNU Affero General Public License,
642 // section 13, concerning interaction through a network will apply to the
643 // combination as such.
644 //
645 // 14. Revised Versions of this License.
646 //
647 // The Free Software Foundation may publish revised and/or new versions of
648 // the GNU General Public License from time to time. Such new versions will
649 // be similar in spirit to the present version, but may differ in detail to
650 // address new problems or concerns.
651 //
652 // Each version is given a distinguishing version number. If the
653 // Program specifies that a certain numbered version of the GNU General
654 // Public License "or any later version" applies to it, you have the
655 // option of following the terms and conditions either of that numbered
656 // version or of any later version published by the Free Software
657 // Foundation. If the Program does not specify a version number of the
658 // GNU General Public License, you may choose any version ever published
659 // by the Free Software Foundation.
660 //
661 // If the Program specifies that a proxy can decide which future
662 // versions of the GNU General Public License can be used, that proxy's
663 // public statement of acceptance of a version permanently authorizes you
664 // to choose that version for the Program.
665 //
666 // Later license versions may give you additional or different
667 // permissions. However, no additional obligations are imposed on any
668 // author or copyright holder as a result of your choosing to follow a
669 // later version.
670 //
671 // 15. Disclaimer of Warranty.
672 //
673 // THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
674 // APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
675 // HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
676 // OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
677 // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
678 // PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
679 // IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
680 // ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
681 //
682 // 16. Limitation of Liability.
683 //
684 // IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
685 // WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
686 // THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
687 // GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
688 // USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
689 // DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
690 // PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
691 // EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
692 // SUCH DAMAGES.
693 //
694 // 17. Interpretation of Sections 15 and 16.
695 //
696 // If the disclaimer of warranty and limitation of liability provided
697 // above cannot be given local legal effect according to their terms,
698 // reviewing courts shall apply local law that most closely approximates
699 // an absolute waiver of all civil liability in connection with the
700 // Program, unless a warranty or assumption of liability accompanies a
701 // copy of the Program in return for a fee.
702 //--------------------------------------------------------------------------------
703 //C O N F I G U R A T I O N
704 //--------------------------------------------------------------------------------
705 //Configuration switch combinations were not tested. E-mail me any program
706 //corrections (dashley@gmail.com).
707 define ("CFG_PROGNAME", "filenames_canonize.php");
708 //Number of characters per line preferred for console output.
709 define ("CFG_CONSOLE_STD_LINE_LEN", 78);
710 //Number of characters per line preferred for console output.
711 define (
712 "CFG_FILENAME_SUBSTITUTION_MAP",
713 ".." //Dots unchanged.
714 .
715 "aabbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz" //LC letters unchanged.
716 .
717 "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz" //UC converted to LC.
718 .
719 "00112233445566778899" //Digits unchanged.
720 .
721 "-_" //Hyphens become underscores,
722 //and anything unmapped
723 //becomes an underscore.
724 );
725 //Pairs of characters, the first is the character to match and the second
726 //is the replacement. Any characters not found in the map will be replaced
727 //by the last character of the constant.
728 //--------------------------------------------------------------------------------
729 //Repeats a character to the console output a certain number of times.
730 function rep_char_con($c, $n)
731 {
732 while ($n--)
733 echo $c;
734 }
735 //--------------------------------------------------------------------------------
736 //Writes a standard thick horizontal line to the console.
737 function hor_line_thick()
738 {
739 rep_char_con("=", CFG_CONSOLE_STD_LINE_LEN);
740 echo "\n";
741 }
742 //--------------------------------------------------------------------------------
743 //Writes a standard thin horizontal line to the console.
744 function hor_line_thin()
745 {
746 rep_char_con("-", CFG_CONSOLE_STD_LINE_LEN);
747 echo "\n";
748 }
749 //--------------------------------------------------------------------------------
750 //Returns an array of all files in the working directory.
751 //If no files can be found, returns FALSE.
752 function get_file_names_in_dir()
753 {
754 //Get directory list.
755 $rv = scandir (".");
756
757 //If the list is empty, something went wrong. Return FALSE.
758 if ($rv === FALSE)
759 return FALSE;
760
761 return $rv;
762 }
763 //--------------------------------------------------------------------------------
764 //Canonizes a filename by performing character substitutions.
765 function canonize($in_fname)
766 {
767 //Well, the algorithm below is O(N^2) or worse ... I knew there was a reason
768 //that computers get faster every year.
769 $out_fname = "";
770
771 for ($i = 0; $i < strlen($in_fname); $i++)
772 {
773 $map = CFG_FILENAME_SUBSTITUTION_MAP;
774 $c = substr($in_fname, $i, 1);
775
776 $sub_made = FALSE;
777 while (strlen($map) >= 2)
778 {
779 if (substr($map, 0, 1) == $c)
780 {
781 $c = substr($map, 1, 1);
782 $sub_made = TRUE;
783 break;
784 }
785 else
786 {
787 $map = substr($map, 2);
788 }
789 }
790
791 if (! $sub_made)
792 {
793 $c = substr(CFG_FILENAME_SUBSTITUTION_MAP, strlen(CFG_FILENAME_SUBSTITUTION_MAP) - 1, 1);
794 //echo "Default sub made: " . $c . "\n";
795 }
796
797 $out_fname = $out_fname . $c;
798 }
799
800 return $out_fname;
801 }
802 //--------------------------------------------------------------------------------
803 //Write introductory message.
804 hor_line_thick();
805 echo CFG_PROGNAME . " Copyright (C) 2015 David T. Ashley\n";
806 echo "This program comes with ABSOLUTELY NO WARRANTY; and is licensed under\n";
807 echo "the GNU General Public License, Version 3. A copy of this license is\n";
808 echo "provided in the source code of this program.\n";
809 hor_line_thin();
810 //-----------------------------------------------------------------------------
811 //Get and emit the names of everything in the directory.
812 $file_list = get_file_names_in_dir();
813 if ($file_list === FALSE)
814 {
815 echo "List of files from PHP function scandir() is empty.\n";
816 echo "Serious internal error, or nothing to do. Script cannot continue.\n";
817 hor_line_thick();
818 exit(1);
819 }
820 else
821 {
822 echo "Files in working directory (unsorted, unfiltered, "
823 .
824 count($file_list)
825 .
826 " files):\n";
827 for ($i = 0; $i < count($file_list); $i++)
828 echo " " . sprintf("[%5d]", $i) . " " . $file_list[$i] . "\n";
829 }
830 hor_line_thin();
831 //-----------------------------------------------------------------------------
832 //Remove everything that is not a file we can rename.
833 $temp_list = $file_list;
834 unset($file_list);
835 $n = 0;
836 for ($i = 0; $i < count($temp_list); $i++)
837 {
838 //echo "Checking " . $temp_list[$i] . "\n";
839
840 if (strcmp($temp_list[$i], ".") == 0)
841 {
842 //. entry, not a file.
843 }
844 else if (strcmp($temp_list[$i], "..") == 0)
845 {
846 //.. entry, not a file.
847 }
848 else if (is_file($temp_list[$i]))
849 {
850 //This is a regular file.
851 $file_list[] = $temp_list[$i];
852 $n++;
853 }
854 }
855
856 if ($n == 0)
857 $file_list = FALSE;
858
859 unset($n);
860 unset($temp_list);
861 //-----------------------------------------------------------------------------
862 //If there is nothing to do, end the script.
863 if ($file_list === FALSE)
864 {
865 echo "No files to rename.\n";
866 hor_line_thick();
867 exit(0);
868 }
869 //-----------------------------------------------------------------------------
870 //Sort the list. This is a non-event. The only rationale for sorting is that
871 //it ensures that the same set of files will be processed in the same order,
872 //regardless of the order provided by the underlying OS internals.
873 sort($file_list);
874 //-----------------------------------------------------------------------------
875 //Emit the names we now have.
876 echo "Files in working directory (directory entries removed, sorted, "
877 .
878 count($file_list)
879 .
880 " files):\n";
881 for ($i = 0; $i < count($file_list); $i++)
882 echo " " . sprintf("[%5d]", $i) . " " . $file_list[$i] . "\n";
883 hor_line_thin();
884 //-----------------------------------------------------------------------------
885 //For each file in the directory, form a filename which would be the result
886 //of applying character substitutions and any other corrections.
887 for ($i = 0; $i < count($file_list); $i++)
888 {
889 $rename_target[$i] = canonize($file_list[$i]);
890
891 //echo $file_list[$i] . " " . $rename_target[$i] . "\n";
892 }
893 //-----------------------------------------------------------------------------
894 //In all those cases where the name of the file as we would like it to be is
895 //different from the name of the file as it exists, rename it. Watch for
896 //collisions, because renaming on top of a file represents the loss of
897 //data.
898 $n = 0;
899 for ($i = 0; $i < count($file_list); $i++)
900 {
901 if (strcmp($file_list[$i], $rename_target[$i]) != 0)
902 $n++;
903 }
904
905 if ($n == 0)
906 {
907 echo "No files need to be renamed.\n";
908 hor_line_thin();
909 }
910 else
911 {
912 echo "Renaming " . $n . " file(s).\n";
913 hor_line_thin();
914
915 $count = 0;
916 for ($i = 0; $i < count($file_list); $i++)
917 {
918 if (strcmp($file_list[$i], $rename_target[$i]) != 0)
919 {
920 $count++;
921 echo "[" . sprintf("%5d/%5d", $count, $n) . "]"
922 .
923 " Renaming " . $file_list[$i] . " to " . $rename_target[$i] . " ... ";
924 if (file_exists($rename_target[$i]))
925 {
926 echo "Aborted.\nRename target already exists.\n";
927 echo "Choose non-colliding filenames and run this script again.\n";
928 echo "Script terminating.\n";
929 hor_line_thin();
930 exit(1);
931 }
932
933 $result = rename($file_list[$i], $rename_target[$i]);
934 if ($result)
935 {
936 echo "Done.\n";
937 hor_line_thin();
938 }
939 else
940 {
941 echo "Failed.\nScript must terminate.\n";
942 hor_line_thick();
943 exit(1);
944 }
945 }
946 }
947 }
948 //-----------------------------------------------------------------------------
949 echo CFG_PROGNAME . " execution ends.\n";
950 hor_line_thick();
951 //--------------------------------------------------------------------------------
952 //End of File
953 //--------------------------------------------------------------------------------
954 ?>

Properties

Name Value
svn:eol-style native
svn:executable *
svn:keywords Header

dashley@gmail.com
ViewVC Help
Powered by ViewVC 1.1.25