" .
$userlang["projectassignedtext"] .
" " . $url . "manageproject.php?action=showproject&id=$add";
// send email
$themail = new emailer($settings);
$themail->send_mail($user["email"], $subject , $mailcontent);
}
}
}
if($company > 0)
{
echo $company . " " . $add;
$companyObj->assign($company,$add);
}
header("Location: manageproject.php?action=showproject&id=$add");
}
} elseif ($action == "closepro") {
if ($project->close($id)) {
echo "ok";
// header("Location: admin.php?action=projects&mode=closed");
}
} elseif ($action == "openpro") {
if ($project->open($id)) {
header("Location: admin.php?action=projects&mode=opened");
}
} elseif ($action == "deletepro") {
if ($project->del($id)) {
// header("Location: admin.php?action=projects&mode=deleted");
echo "ok";
}
} elseif ($action == "customers") {
$classes = array("overview" => "overview",
"customer" => "active",
"system" => "system",
"users" => "users"
);
$title = $langfile['customeradministration'];
$template->assign("title", $title);
$template->assign("classes", $classes);
$allcust = $companyObj->getAllCompanies();
// $clopros = $project->getProjects(0, 10000);
/* $i = 0;
$users = $user->getAllUsers(1000000);
if (!empty($opros)) {
foreach($opros as $opro) {
$membs = $project->getProjectMembers($opro["ID"], 1000);
$opros[$i]['members'] = $membs;
$i = $i + 1;
}
$template->assign("opros", $opros);
}*/
$template->assign("allcust", $allcust);
$template->display("admincustomers.tpl");
} elseif ($action == "addcust") {
if (!$userpermissions["admin"]["add"]) {
$errtxt = $langfile["nopermission"];
$noperm = $langfile["accessdenied"];
$template->assign("errortext", "$errtxt $noperm");
$template->display("error.tpl");
die();
}
if (!$end) {
$end = 0;
}
$data = array('company' => getArrayVal($_POST, "company"),
'contact' => getArrayVal($_POST, "contact"),
'email' => getArrayVal($_POST, "email"),
'phone' => getArrayVal($_POST, "tel1"),
'mobile' => getArrayVal($_POST, "tel2"),
'url' => getArrayVal($_POST, "web"),
'address' => getArrayVal($_POST, "address"),
'zip' => getArrayVal($_POST, "zip"),
'city' => getArrayVal($_POST, "city"),
'country' => getArrayVal($_POST, "country"),
'state' => getArrayVal($_POST, "state"),
'desc' => getArrayVal($_POST, "desc")
);
$add = $companyObj->add($data);
if ($add)
header("Location: admin.php?action=customers&mode=added");
}
elseif($action == "assigncustomer")
{
}
elseif ($action == "system") {
$classes = array("overview" => "overview",
"system" => "active",
"users" => "users"
);
$languages_fin = array();
foreach($languages as $lang) {
$fin = countLanguageStrings($lang);
if (!empty($langfile[$lang])) {
$lang2 = $langfile[$lang];
$lang2 .= " (" . $fin . "%)";
$fin = array("val" => $lang, "str" => $lang2);
} else {
$lang2 = $lang . " (" . $fin . "%)";
$fin = array("val" => $lang, "str" => $lang2);
}
array_push($languages_fin, $fin);
}
$msgcount = getArrayVal($_GET, "msg");
$peoplecount = getArrayVal($_GET, "peop");
$procount = getArrayVal($_GET, "pro");
$taskcount = getArrayVal($_GET, "tsk");
$template->assign("msgcount", $msgcount);
$template->assign("peoplecount", $peoplecount);
$template->assign("procount", $procount);
$template->assign("taskcount", $taskcount);
$template->assign("languages_fin", $languages_fin);
$title = $langfile["systemadministration"];
$template->assign("title", $title);
$template->assign("classes", $classes);
$sets = $theset->getSettings();
$templates = $theset->getTemplates();
$themes = $theset->getThemes($settings["template"]);
$template->assign("themes", $themes);
$template->assign("settings", $sets);
$timezones = DateTimeZone::listIdentifiers();
$template->assign("timezones", $timezones);
$template->assign("templates", $templates);
$template->display("editsettings.tpl");
} elseif ($action == "editsets") {
$theme = getArrayVal($_POST, "theme");
if ($theset->editSettings($name, $subtitle, $locale, $timezone, $dateformat, $templ, $theme, $rssuser, $rsspass)) {
$handle = opendir($template->compile_dir);
while (false !== ($file = readdir($handle))) {
if ($file != "." and $file != "..") {
unlink(CL_ROOT . "/" . $template->compile_dir . "/" . $file);
}
}
$_SESSION["userlocale"] = $locale;
$users = $user->getAllUsers(100000);
foreach($users as $theuser) {
// set the new locale for all the users
$user->edit($theuser["ID"], $theuser["name"], $theuser["realname"], $theuser["email"], $theuser["tel1"], $theuser["tel2"], $theuser["company"], $theuser["zip"], $theuser["gender"], $theuser["url"], $theuser["adress"], $theuser["adress2"], $theuser["state"], $theuser["country"], $theuser["tags"], $locale, "", $theuser["rate"]);
}
header("Location: admin.php?action=system&mode=edited");
}
} elseif ($action == "editmailsets") {
$status = getArrayVal($_POST, "mailstatus");
$mailfrom = getArrayVal($_POST, "mailfrommail");
$mailfromname = getArrayVal($_POST, "mailfromname");
$method = getArrayVal($_POST, "mailmethod");
$server = getArrayVal($_POST, "server");
$mailuser = getArrayVal($_POST, "mailuser");
$mailpass = getArrayVal($_POST, "mailpass");
if ($theset->editMailsettings($status, $mailfrom, $mailfromname, $method, $server, $mailuser, $mailpass)) {
header("Location: admin.php?action=system&mode=edited");
}
}
?>
Collabtive-2.0/api.php 0000664 0000000 0000000 00000007562 12372520637 0014742 0 ustar 00root root 0000000 0000000 The API is deactivated. The API does not properly enforce permissions yet. For example every user can request every project / task etc. To activate the API remove the die() statement in api.php line 38. DO NOT USE THE API ON PRODUCTION SERVERS !");
$userobj = new user();
$usr = $_GET["username"];
$pass = $_GET["pass"];
$pass = urldecode($pass);
$usr = urldecode($usr);
if (!$userobj->login($usr, $pass)) {
die("not authorized");
}
// variables
$action = getArrayVal($_GET, "action");
$id = getArrayVal($_GET, "id");
$user = getArrayVal($_GET, "user");
// output in xml or json
$mode = getArrayVal($_GET, "mode");
// create new array to xml converter
$xml = new toXml();
// Projects
if ($action == "project.get") {
$obj = (object) new project();
$theData = $obj->getProject($id);
$theRootNode = "project";
} elseif ($action == "myprojects.get") {
$obj = (object) new project();
$theData = $obj->getMyProjects($id);
$theRootNode = "myprojects";
} elseif ($action == "project.members.get") {
$obj = (object) new project();
$theData = $obj->getProjectMembers($id);
$theRootNode = "members";
}
// Users
elseif ($action == "user.profile.get") {
$obj = (object) new user();
$theData = $obj->getProfile($id);
$theRootNode = "user";
} elseif ($action == "user.id.get") {
$obj = (object) new user();
$theData = $obj->getId($id);
$theRootNode = "user";
} elseif ($action == "user.list.get") {
$obj = (object) new user();
$theData = $obj->getAllUsers(100000);
$theRootNode = "userlist";
} elseif ($action == "user.tasks.get") {
$obj = (object) new task();
$project = new project();
$myprojects = $project->getMyProjects($user);
$theData = array();
foreach($myprojects as $proj) {
$theArr = $obj->getAllMyProjectTasks($proj["ID"], 10000, $user);
if (!empty($theArr)) {
foreach($theArr as $task) {
array_push($theData, array("ID" => $task["ID"], "name" => $task["title"]));
}
}
}
$theRootNode = "tasks";
}
// convert to XML or JSON
if ($mode == "json") {
$theXml = $xml->arrToJSON($theData);
} else {
$theXml = $xml->arrToXml($theData, $theRootNode);
}
// output to the user
echo $theXml;
?>
Collabtive-2.0/changelog.txt 0000664 0000000 0000000 00000136157 12372520637 0016153 0 ustar 00root root 0000000 0000000 Collabtive 2.0
+ Fixed notice on desktop that resulted from missing network connection during update check
+ Fixed deactivation of error reporting during update
+ Merge existing members and new members when adding project
+ Error reporting globally set to E_WARNING
+ Changed redirection after project creation from desktop or project administration to project dashboard
+ Improved translations: German, Polish, Farsi, Norwegian (Bokmal & Nynorsk), Simplified Chinese
+ Improved project tree's handling for special characters
+ Removed file upload from the add reply form. Now only previously uploaded files can be attached to replies.
+ Lots of code beautification in templates files
+ Make filemanager behave more like explorer (i.e. no pagination - show all files in each folder at once)
+ Revert to gridview as the default file view (this was changed in 1.2 b/c of issues)
+ Added edge case for SSL detection
+ Removed .htaccess file as it was causing trouble with some servers
+ Added start date functionality for tasks
+ Fixed several typos in style_form.css
+ Fixed typo in paginate-first function
+ Fixed cancel button for deleting folders in list view
+ Corrected label for edit button in project members view ("edit file" -> "edit user")
+ Changed permission for adding users to an existing project from "administration" to "project edit"
+ Implemented HTML Purifier filter for all user input. This will give us STRONG protection against all kinds of XSS attacks, while preserving rich formating options. It will also enhance well formedness.
+ Implemented some Content-Security-Protection header options. No framing,only load css from same origin, only load .js files from same origin, only load images from same origin
+ Remove force-compile option from template engine. This disabled caching and caused performance problems for some users.
+ Add compileAllConfig() to force a recompile of the config files in certain cases.
+ Fixed possible sql vulnerability in manageajax.php
+ Dont expose plain filesystem links to uploaded files. Downloads are routed through download.php
+ Added AES encryption of uploaded files
+ Added CSS theme functionality. Now the CSS and image files for each template set reside in a /themes/ subfolder. This way, there can be CSS only variations of templates created more easily.
+ Change color of tab info string, making it more readable
+ Set cache directory for HTML purifier definitions to /files/standard/ics/
+ Messages can now be edited asyncronously
+ Messages can now be replied to asyncronously
+ Implemented a new structure for the desktop. It is now an accordeon where clicking each section slides it open and closes all others.
+ Implemented a new structure for the project dashboard. It is now an accordeon where clicking each section slides it open and closes all others.
+ Customer profiles can be added in admin->customer
+ A customer can be assigned to projects
+ Remove user pictures from the onlinelist
+ Added translation of install-readme: Simplified Chinese
+ Removed unfunctional links from the day numbers in the desktop calendar
+ Fixed white page when editing of task failed.
+ Fixed conversion of umlauts when uploading files.
+ Fixed accordeon in tasklist view
+ Fixed a bug where the list view in project->files could not be selected if the user's role had file:add deactivated.
+ Files in subfolders can now also be attached to messages
+ Fixed filetype detection in message::getAttachedFiles - needs more refactoring to use file::getFile
+ Fixed a problem with file uploads and visibility string
+ Fixed cancel button submitting the form in task->edit
+ Localized project tree labels
+ Localized label for name in list view for files
+ Added a new CSS theme: spring
+ Fixed various display bugs
Collabtive 1.2
+ Added translations: Vietnamese
+ Improved translations: Spanish
+ Added collapsible tree showing the structure of a project (milestones, tasklists, tasks, messages)
+ Removed Basecamp importer (didn't work in a long time, see https://github.com/philippK-de/Collabtive/issues/2 )
+ Milestones can now have a start date set when created. Start date can also be edited.
+ Removed links to the my-projects/tasks/messages blocks in block titles on the desktop
+ Milestones can now be deleted asynchronously
+ Milestones can now be closed asynchronously
+ Milestones are now grouped into "current" , "late" and "upcoming" milestones in project->milestones
+ Closed tasklists now receive their own white bg block in projects->tasklists. Better sets the block apart from active tl's
+ Removed tags from the add message form. Tags will be removed from Collabtive in order to simplify things.
+ Removed file upload from the add message form. Now only previously uploaded files can be attached to messages.
+ Implemented tree view for projects
+ Fixed display problem with Smarty where pages weren't listed as before
+ Improved notification emails. They are now much more informative / better localized (StephanRichter)
+ Implemented iCal auth (StephanRichter)
+ Fixed bug where certain dates (like 1.9.2013) could not be selected in date picker (StephanRichter)
+ Added htaccess file to deny folder listing in the file folder (StephanRichter)
+ Switched from a background-color gradient to a solid color
+ Fixed a bug where the timetracker report would return a server error (http://collabtive.o-dyn.de/forum/viewtopic.php?f=11&t=11505)
+ Start date for a timetracker entry can now be set manually
+ Fixed a mssing (int) typecast in managetimetracker.php.
+ Timetracker start date can now be manually set
Collabtive 1.1
+ Fixed bug in tasklist editing, which would automatically remove the tasklist's assignment to a given milestone
+ Fixed notice in user editing form which showed up when an available language's name was not translated in the selected language
+ Updated the template engine to Smarty3 (mwirtz)
+ Updated the bundled TCPDF to 6.0 (mwirtz)
+ Changed class.mylog to show 25 entries per log page instead of 10
+ Changed inserting of default values to settings table to a single query (from a foreach loop) in install.php
+ Fixed an error where any registered user could delete other users' profiles
+ Fixed an error where other files than pictures (for example PHP scripts) could be uploaded as the user's avatar
+ Fixed a possible SQL injection in the timetracker reporting
+ Fixed text in empty project message PDF export
+ Fixed bug with empty timetracker PDF exports
+ Improved replying to messages by putting as editable default title "Re:
"
+ Fixed UI bug in project's tasklist view for tasks with multiple users
+ Changed UI string "Send" to "Save"
+ Made sure the database uses UTF-8 for the connection
+ Fixed links in e-mail notifications for new project / message
+ When editing closed tasks/projects in tables, the edit form is now displayed in the top block, like for open tasks/projects
+ Fixed UI glitches with "Never due" field in project add/edit forms
+ Updated image handling to remain compatible with PHP 5.4+
+ Removed unused field "folder description" from form for adding folders
+ Made field "folder name" required in form for adding folders
+ Made installer more secure by checking for pre-existing installation in each step of installation (not only on 1st)
Collabtive 1.0
+ Added translations: Hebrew, Norwegian (Nynorsk), Taiwanese
+ Improved translations: Albanian, Arabic, Bulgarian, Danish, Farsi, French, German, Greek, Hungarian, Italian, Norwegian (Bokmål), Polish, Portuguese (Brazilian), Slovak, Serbian, Turkish
+ Added translations of install-readme: Hungarian
+ Improved translations of install-readme: Arabic
+ Changed name of autoload function to cl_autoload and added it with spl_autoload_register() to autoload list, to not interfere other autoloaders
+ Implemented automatic update notification.
+ Completely re-written the PDF export. It now uses a custom subclass of TCPDF to directly draw the tables for the reports from arrays instead of using an HTML intermediate.
This greatly speeds up PDF generation (esp when many lines are exported) and generates much smaller PDFs.
+ Fixed problem with missing "webcolor" variable in TCPDF
+ Improved the project log PDF report by merging fields, improving shading and adding automatic font stretching.
+ Added new PDF report to the "my tasks" block on the desktop. Now a user can export all his tasks to PDF.
+ Re-enabled permissions checking when deleting a timetracker entry. This got disabled for testing purposes and was commited to trunk. Disabling it posed a possible security risk.
+ Added permissions checking to timetracker:add and timetracker:edit . Permissions set in the useraccount where not properly enforced for the timetracker before.
+ Rewrote the settings functionality to work with a key/value table as the datastore. This makes extensibility easier.
+ Ported all database access from mysql_ to PDO. This became necessary because mysql_ will be deprecated in future versions of PHP.
It uses stored procedures for all UPDATE and INSERT queries which brings improved security and performance (caching) advantages.
Also, PDO supports a plethora of databases which should make supporting other databases than mysql much easier than before. (Contributed by Mwirtz)
+ Started porting SELECT queries to use stored procedures as well. This brings security and performance (caching) advantages.
+ Ported install.php and update.php to also use PDO
+ Replaced the Excel Export by a more robust CSV export. The excel export was based on an outdated library and not fully compatible with newer versions of Excel and OpenOffice.
With CSV Collabtive can now export to a widely used serialisation format that can easily be read into Excel, OpenOffice, and many more.
+ Switched to using a precompiled static CSS file in production instead of dynamically compiling it at runtime. This saves many call to the php interpreter.
+ Removed the tabs "my tasks", "my projects", "my messages" from the tabs bar on the desktop.
Those tabs were redundant for quite some time now. All their information is, in condensed form, also available on the desktop. they can be reinstated by editing tabsmenue-deskt.tpl
+ Fixed a bug preventing people from uploading avatar images
+ Changed the truncation of project titles and task titles on the desktop slightly to prevent http://collabtive.o-dyn.de/forum/viewtopic.php?f=11&t=8750#p17251
+ Made timetracker show closed tasks in addition to open tasks when editing a task ( http://collabtive.o-dyn.de/forum/viewtopic.php?f=11&t=8749 )
+ When clicking on a folder, set the folder in the select menu for "add file" and "add folder" to the clicked folder.
This way you can upload into a folder you just clicked straight away.
+ Removed the "visibility" functionality for folders ...finally
+ Removed IE7 compatibility
+ Files: Made listview/gridview switch respect the currently selected folder when switching views
+ Made the uploader respect the currently selected folder when refreshing. Now after an upload is complete the view will refresh the subfolder to which a file has been uploaded and not always the main folder.
+ When editing tasks or projects in tables (i.e. on the desktop) the edit form is now displayed in the top block where the add project/task form is also displayed. This eliminates the need for a dedicated "edit" page.
+ Enabled multi-byte safe truncation for strings (makes PHP extension "mbstring" required)
+ Properly enforce user permissions for uploading files, moving files (file_edit), deleting folders (file_del), adding folders (file_add) on the controller/script level. Not only on the template/view level.
+ Added RTL enabled version of the default theme. Contributed by mrawi.
+ Implemented "view" permission. It enables you to define roles that can only view certain parts of a project. For example a role that can view milestones, tasks and messages, but not files and the timetracker in a project.
+ Added "Add task" slider to the "my tasks" block on the desktop. This way a user can add tasks to himself straight from the desktop.
+ Fix shading of the content table. When clicking "add task" or "add project" on the desktop the table with existing tasks / projects is now correctly shaded.
+ Made the page update.php translatable
+ Improved the design of the installer/updater
+ Fixed a notice in system administration / user profile when a language string was not translated
+ Fixed a bug that asked for the "add" permission instead of the "reply" permission before posting a reply
+ Fixed a bug where the datepicker would not keep the dateformat set in the systemsettings and would revert to d.m.Y when changing months.
+ Fixed a notice when displaying messages
+ Fixed link in the email notifications for message-replies to point to the replied to message instead of the reply
+ Send email notifications to all assigned users when creating a project, not just when adding users afterwards.
+ Bulleted lists now displayed correctly... finally.
+ Added mimetype icon for PNG images uploaded with IE which is image/x-png and not image/png
+ Fixed a notice when searching
+ Fixed a bug where clicking cancel in admin->projects->assign user would add an empty user (removed the cancel button).
+ Fixed minor glitch in the form for adding a timetracker entry
+ Substituted deprecated PHP function split() used in e-mail handling by PHP function explode()
+ Made sure to only offer the password reset function (via e-mail) when e-mail notifications are activated
+ Made the asynchronous refresh after file upload is complete a bit quicker. the uploader used to wait 1s before refreshing - this is now reduced to 500ms.
Collabtive 0.7.6
+ Added Estonian translation
+ Fixed timetracker filtering
+ Fixed a problem in countLanguageStrings() that would throw warnings on the system admin page when there was an empty folder in the /language/ folder
+ Files can now be moved up in the folder hierarchy by dragging them onto the "folder up" icon / titlebar
+ Display the complete path of the folder in the add folder ("Parent folder") and add file ("folder") forms, instead of just the foldernames
+ Display the complete path in the titlebar of the file manager, instead of just the current folder
+ Filemanager can now be switched between Gridview (default) and listview (new)
+ Set sensible defaults for time formating in admin->system
+ Implemented asynchronous file upload, with real progress indicator (for browsers that can support it)
+ Removed open ID login option. it was based on a terribly outdated library that only supports openID 1.0, which possibly poses a security problem.
+ Improved file type checking for avatar uploads to prevent non-image files from being uploaded to the avatar directory. This improves security.
+ Removed some dead code paths from manageproject.php
+ Added a security check to install.php to prevent it from being used after Collabtive has already been installed.
+ Fixed a bug where, when clicking "delete" on a folder and answering the "are you sure" prompt with "no", the folder would be faded out as if deleted (though not really deleted)
Collabtive 0.7.5
+ Improved code comments
+ Streamlined user interfaces of the forms for adding and editing a milestone
+ Fixed bug in the project timetracker filter so the filtered list of timetracker entries is now sorted by date as it should be
+ Timetracker filter in user profiles now allows for selecting multiple projects instead of just a single project
+ The quickjump menu in the sidebar now has a fixed width at 100%
+ Fixed a bug where clicking cancel on the add task form would submit the form
+ Made the sliding parts of the UI a bit snappier
+ Cleaned up the desktop: only show the calendar if there are projects available
+ Extended limits for milestones
+ Fixed bug where the text of a message or task would not display correctly when adding / editing it in the single item view
+ Improved translations: Chinese, Russian
+ Refactored method getAllUsers in class.user.php
+ PHP date() compatibility for the JS calendar / datepicker
+ Fixed a small display glitch in winter
+ Disabled browserinternal forms checking to prevent conflicts with the built in forms validator
+ Added Beta version of a JSON / XML API.
+ Fixed display bug in the timetracker report
+ Optimized asynchronous queries to check for new chat messages and onlinelist updates when idle (i.e. when not browsing through collabtive). It uses a decay mechanism now to reduce the number of ajax requests to the server. This should reduce DB utilisation.
+ Compressed some large JS assets, that were previously served uncompressed, (like prototype.php) with YUIcompressor, reducing the amount of JS code to be loaded by the user
+ Fixed a broken implementation of Date.parse() in IE by using a JS version of strtotime() for the calendar
+ Fix a glitch were online users would briefly not show up in the online list
+ Fixed a bug where the "select file" in the add file dialog could not be clicked
+ Made the asyncronous deleting of items (tasks, projects) a bit faster
+ Improved the styling of the colorpicker and imagechoser in tinymce
+ Removed unwanted margin from textareas
Collabtive 0.7
+ Changed assign-to field in add task form to multi-select (was missing only when you clicked a date on the calender and chose "add task")
+ Optimized user profiles to not show the blocks for projects and time tracking when there are no entries
+ Made the use of the field Company in user profiles more user friendly
+ Added missing string "hello" to e-mail notification which a new user receives
+ Optimized the user add form to not show a label for projects, if no projects have been added, yet
+ Extended sortability of tables on desktop to My tasks: sortable by Task / Project / Days left, show mouse when hovering over column title
+ New feature: added a quickfinder to the sidebar, allowing the users to quickly switch to any of their other open projects
+ Optimized table column widths to show more of the entries in the main column
+ Optimized table alignment (content should align right, if the column only contains digits)
+ Added truncation to some views where it was missing
+ Optimized display of time track entries in user profiles
+ Optimized truncation in several views (e.g. now longer chunks of file names are shown)
+ Optimized log to show more of the entries' titles, to log actions applied to folders, to display deleted files' names, to not display log entries for actions concerning time tracking (for privacy reasons), and to log user assigned / withdrawn from project
+ Fixed a warning by correcting some calls to templates from managemilestone.tpl
+ Fixed the "cancel" button on the add project form to not submit the form.
+ Optimized task add form: Now in the assign field, the current user is always pre-selected, which often will save a click
+ Optimized e-mail notification when a task is added, so that the adding person does not get an e-mail
+ Added (fake) progress bar that replaces the file upload button when upload is in progress
+ Added link titles for milestone names in the project milestones view
+ Added permission check for showing the delete button for attached files in My Messages and single message view
+ Fixed reloading after deleting of attached files in My message view
+ Added alt text to the button in the project files view which takes you a level higher in the folders hierarchy
+ Fixed a notice in project messages
+ Fixed a bug in the project files lists and all message files lists: When a file is deleted, its icon now neatly fades out and is replaced by the other files moving up
+ Added new "winter" template. It is based on "standard" and provides a lighter alternative. Replaces the unmaintained "frost" template.
+ Fixed wrong image file path resulting in missing image in system message for folder added
+ Fixed wrong image file path resulting in missing image in system message for reply added
+ Added XSS protection for strings input by the user in getArrayVal()
+ Added function to edit a user's hourly rate if you are an admin
+ Updated bundled TC_PDF class to 5.9.038
+ Updated the bundled tinyMCE to 3.4.2, took alignment option out of toolbar because it seems pretty useless
+ Removed unused search modal HTML code
+ Enabled rounded borders in IE9 (CSS)
+ Corrected hard-coded string "Budget" on project dashboard to translated string
+ Added function to edit project budget
+ Optimized strings shown during hovering in several views
+ Corrected regular expression in timetracker in order to allow for single digit hours and to dismiss things like 10:60 or 25:00
+ Corrected color-coding for projects with due date in the future on dashboard
+ Removed 'Days left' from dashboard for projects without due date
+ Color-coding for projects without due dates on desktop and project administration now same as for projects with due date in the future
+ More string sanitizing in class.user.php
+ Added comments
+ Improved security in chat component (added typecast to int)
+ Corrected typo in tables optimization queries and added missing queries in update script
+ Fixed bug in files table which prevented .docx files from being stored in the database
+ New function: User is now enabled to reset his/her password when he/she forgot it
+ Added missing e-mail notification function when replying to a message
+ Added missing image files for lytebox feature
+ New translations: Slovenian, Croatian, Farsi
+ 100% completed translations: Danish, German, Greek, English, Spanish, Finnish, French, Croatian, Italian, Norwegian, Dutch, Polish, Portuguese, Brazilian Portuguese, Romanian, Russian, Serbian
+ Improved security: open .php files as plaintext only
+ Improved sorting of projects on desktop, now sorted chronologically by default
+ Improved message display: Avatar now appears next to body of text thus making better use of the given space
+ Fixed bug which prevented choosing existing project files as attachments for a reply
+ Added title for install script
+ Beautified code of several template files
Collabtive 0.6.5
+ Fixed bug when assigning tasks while email notification is deactivated / off
+ Added name of the project to the desktop calendar overlay
+ Fixed date format in calendar overlay on desktop
+ Improved translations: French, Portuguese, Czech, Brasilian Portuguese, Polished
+ Modified max no. of users to show on projects, now 10000 instead of 100
+ Remove the option "All" from the task edit form on single task view
+ Fixed a bug where a folder wouldnt be visible if visible = all was selected
+ Fixed a bug where every timetracker entry was tracked for "today" regardless of selection
+ Removed some debug output in managefile.php
+ Added check for userpermission of file editing, so the edit icon will not show up
any longer in case of missing permission
+ Added missing truncation to project title on the mymessages view
+ Corrected file->delete link for attached files in the message view
+ Backported never due for projects to the frost theme
Collabtive 0.6.4
+ Fixed a path problem with the filesize() function
+ Fixed a bug preventing Safari Users from creating tasks
+ Fixed a bug where an empty user could be assigned to a project
+ Corrected HTML Errors in project->people and project->timetracker views
+ Fixed a bug when editing a task
+ Added generic boder-radius to the CSS so all browsers that support it will display rounded borders (previously only Firefox and Webkit)
+ Removed keyboard shortcuts. they caused confusion and were probably rarely used
+ Buttons in the mainmenue now stay highlighted when an area is selected
+ Fixed a bug where users would not get an email notify when a task is assigned
+ Usernames on multiply asssigned tasks are now properly linked to each users profile
+ Fixed a problem preventing Safari and Chrome users from creating users.
+ Fixed a possible security flaw in the chat component
+ Added missing truncation to the project name in the "my tasks" column on the desktop, and the my tasks view
+ Added date of day to the timetracker PDF export per user
+ Fixed bug where no emails would be send when assigning new users to a task. Also refactored code for assigning users.
+ Fixed a possible problem in the mailer class.
+ Removed 2 unneeded tinyMCE plugins. This further reduces the amount of code to load for tiny MCE
+ Show the calendar on the desktop for all users, not just admins.
+ Updated list of languages in which the install-readme.txt is available
+ Added Swedish translation of the install-readme.txt
+ Fixed folder visibility
Collabtive 0.6.3
+ Cleaned up user profile: company only displayed if available
+ Added option "all" to visibility selection of a new folder
+ Fixed bug where display of members in project members area and user administration was restricted to 10 users
+ Removed mini calendar from sidebar since it served no real purpose
+ Fixed a bug in tasklist editing where not all active milestones were available to assign the list to.
+ Added Favicon to standard theme
+ Added TinyMCE editor options to message edit form, reply form, milestone add form, and milestone edit form
+ Dropped update support for updating from versions earlier than 0.5
+ Removed calls to deprecated function set_magic_quotes_runtime() in class tcpdf (PDF export)
+ Renamed language file folders for Chinese, Greek, Ukranian, Galician, Japanese, and Czech so they
match those used in TinyMCE
+ Complete translation to English, German, Romanian, and Bulgarian
+ Added readme in Italian
+ Added English manual
+ Added comments
+ Replaced chat icon in frost theme with a slightly darker one
+ Fixed file upload bug where file would be uploaded to the server but not to the database
+ Fixed a bug where asignee would not be set correctly upon adding a task
+ Fixed a bug where editing of a task could result in the task being assigned to no user
+ Fixed a bug where editing of a tasklist enforced assigning the list to a milestone
+ Fixed a bug where project budget would automatically be reset to 0 upon editing the project
+ Cleaned up project view: budget only displayed if > 0; description only displayed if available.
+ Cleaned up tasklist view: description only displayed if available.
+ Cleaned up message view: replies only displayed if available; removed delete icon, which did not work properly anyway.
+ Fixed a bug in email notification for task assignment
+ Fixed a bug in user timetracking filter (last day of selection not discarded any more)
+ Fixed upload paths
+ Fixed blank page when trying to install from SVN
+ Fixed problem where user with project-add-permission but without admin-permission added a project would get an error message
+ Added permission check for closing tasklist
+ Added page title for tasklist edit form, message edit form, and reply form
+ Added system message on desktop when creating, deleting, or closing a project
+ Added optimization queries for database tables 'roles' and 'roles_assigned' in update script
+ Code cleanup
@descartes
+ Fixed standard template
+ Fixed version number in the footer
+ Updated language files to match fixes
+ Fixed a bug in user management
+ Fixed typo when viewing tasklists
@whisperwind
+ Fixed minor issue where deleting user
+ Fixed an autocompletition issue where editing users
+ Fixed truncating problem where using multibyte characters
+ Added login by email in addition to login by username
+ Remove some more calls to session_is_registred in favour of isset($_SESSION)
+ Made mb_string extension mandatory during installation
+ Fixed a problem with long project/task names
@avychodil
+ Fixed wrong image path in message replies
Collabtive 0.6.2
+ Downgraded Prototype to 1.6.0.3 to avoid problems with IE.
+ Fixed a bug when filtering timetracker
+ Fixed "milestones tab unvisible for non-admins"
+ Removed property required = "0" from non-required form fields. This should improve Opera compatibility.
+ Fixed some wrong image paths.
+ Fixed a bug when editing a task.
+ Properly implemented assign task to some/all users of a project
+ Added complete russian translation
+ Fixed small display bug in single tasklist view
Collabtive 0.6.1
+ Fixed some bugs in the frost theme
+ Fixed a UTF-8 entity bug in the mailer component
+ Included missing picture for the upload progressbar
+ Removed some more calls to deprecated session_is_registered() in favor of isset($_SESSION)
+ Fixed a display Bug in Safari/Webkit
+ Fixed role editing in frost
+ Fixed a UI glitch in the login screen where to login-button would disappear onmouseover.
+ Included missing pictures for pagination arrows
+ Fixed inserting default roles fails when updating from an old version due to a missing
field
+ Fixed a bug where the calendar view would not be displayed on the project page.
+ Tasks without an end-date no longer show as a lot of days overdue
+ Fixed a bug in the formvalidator where it would not accept certain valid e-mail adresses
+ Improved Catalan translation
+ Fixed a security bug where any user could delete any file or folder in projects he belonged to.
+ Fixed editing timetracker data did not show the tinyMCE editor (mloeffen)
+ Fixed a bug in TinyMCE textfields where newlines were inserted after editing
+ File manager now stores who uploaded a file in the database. Uploader is displayed in the file list view.
+ First version of a plugin implementation
+ Fixed wrong version number in frost theme
Collabtive 0.6
+ Fixed a bug where file description of files in subfolders could not be edited.
+ Added "add project" functionality to the projects block on the desktop.
+ Fixed a bug preventing the attaching of existing files to messages.
+ Refactoring of class project. Removed a lot of unneeded code.
+ Refactoring of class tasklist. Removed a lot of unneeded code.
+ Refactoring of class message. Removed some uneeded code.
+ Refactoring of class milestone. Removed a lot of unneeded code.
+ Refactoring of class datei. Removed some unneeded code.
+ Fixed a bug in class milestone, preventing milestones with end date "today" from showing up when there where no other milestones.
+ Fixed a bug in the calculation of days left of milestones.
+ When closing or deleting stuff (tasks, milestones, messages, etc) asynchronously, the alternating background color of the table rows is properly restored.
+ Date format can now be configured to be d.m.Y or m/d/Y
+ Updated Prototype library to 1.6, Scriptaculous to 1.8., finally.
+ Fixed a bug where the filetype icon of a file would not be displayed (instead "?" icon).
+ Fixed a bug when deleting folders.
+ Fixed display of filecount when 0 files are present.
+ Implemented unlimited subfolders for files.
+ Implemented moving files between folders via drag and drop.
+ Implemented access control on a per-file basis.
+ Removed deprecated function session_register() from user::login(), now relying on $_SESSION only.
+ Implemented E-Mail notify when uploading new files
+ Tasks can now be assigned to multiple users
+ Added (fake) progress indicator when uploading files
+ Fixed a bad string replacement in install.php, preventing some users from installing
+ Fixed some MySQL queries that caused problems on some configurations (mloeffen)
Collabtive 0.5.5
+ Roles may now be edited
+ Added some missing strings (role added, edited, etc)
+ Fixed display of days left for projects without due date
+ Fixed more installer problems
+ Implemented RSS feed for project messages
+ New theme: "Frost" included. Contributed by Kemie Guaida.
+ Backported some changes to the frost theme
+ Improved themeability of some ajax elements
+ Updated PDF library to TCPDF 4.5.39
+ Removed unneeded PDF fonts, reducing the package size
+ PDF exports of messages now show the postdate of each message
+ Implemented PDF export of the "my messages" view.
+ Implemented PDF export for single messages
+ Fixed a bug where PDF export would insert empty pages between pages with content
+ More TinyMCE cleanups (unneeded packages removed)
+ Fixed some small UI glitches
+ Removed Basecamp import from the installer to reduce confusion
+ Fixed a bug where tasks could not be re-opened due to improper permissions
+ Fixed a bug where users could not be de-assigned from a project due to improper permissions
+ Users can now be assigned to projects on the admin->projects view in the project details (again).
+ Implemented email notifications when posting messages.
+ Added userpermissions for chat functionality (i.e. stop clients chatting ;) )
+ Made get methods of class milestone consistently return 10 results by default (instead of some 5, some 10)
+ Implemented simple company field in the userprofile.
+ Refactor class task to use internal method getTask() in all get methods, instead of inline code to retrieve tasks.
+ Messages are now asynchronously deleted
+ Improved Basecamp importer to use the new permissions system
+ Fixed a bug where project->tasklists would show no closed tasklists if there are no open ones
+ Added company field to the userprofile
+ Addec company field to vCard export
Collabtive 0.5
+ Fixed problem in the installer on IIS
+ Make most methods return associative arrays only , instead of numeric and associative indexed arrays.
This creates less memory overhead, and simplifies the conversion to XML
+ First version of an XML API included
+ Improved PDF Export (full UTF8 support for PDF reports)
+ Implemented PDF export for project->messages
+ When creating a new user, the user is notified via email
+ Fixed a bug in admin->users and project->users (missing pagination)
+ Fixed some UI glitches
+ Fixed Imagelist in tinyMCE editor not displaying files without a title
+ Fixed short PHP tags in style_main.php (standard theme)
+ Fixed wrong breadcrumb link in single message view
+ Made randon number generator use mt_rand(). Numbers are attached to uploaded files.
+ Implemented new, role-based, user permissions system
+ Started porting smarty classes to strict PHP5
+ New locales: Arabian, Slovak, Swedish, Ukrainian, Finish, Catalan
+ Improved Basecamp import: Assign currently logged in user to imported projects (not just the imported users)
+ Basecamp import now imports replies to messages, as well as the messages themselves.
+ Basecamp import now properly imports task-titles , not only the textbody
+ Fixed Basecamp import in standard template (admin->system and installer)
+ Fixed a bug where userprofiles would get mangled up when changing system settings in admin->system
+ Edit project on project view now correctly shows wyswyg editor
+ Show more tasks on "my tasks" view on the desktop
+ Removed many unneeded TinyMCE Plugins from the package
+ Implemented properly styled error messages
+ Implemented Username and Password for access to RSS Feeds
+ Tasks may now be added by clicking on the day numbers on the project view calendar
Collabtive 0.4.9.1
+ Fixed display bug in mac/safari (admin->user)
+ Fixed a bug when changing the picture of users, as admin
+ Cleaned up problems in the userprofile
+ fixed bug preventing avatar pictures from being uploaded in Internet Explorer
+ Improved folder handling
+ Improved timetracker reporting (normal users can now see their own hours)
+ Folderexport now possible in the file explorer
+ Fixed a bug in timetracker report pagination (project view)
+ Fixed a bug in the JS calender on the desktop in IE
+ Fixed some wrong links in the new
Collabtive 0.4.9
+ Clear the template cache when changing themes
+ Added option Mail from name
+ Implemented ajaxified close element and delete element
+ Updated to TinyMCE 3.2
+ Fixed a bug when changing the avatar of other users, as admin
+ Implemented hourly rate setting for Users
+ Implemented Budget setting for Projects
+ Pagination now themeable
+ Implemented imagelist in TinyMCE editor. When inserting a picture, using tinyMCE a list of pictures populated from the uploaded files is presented
+ Implemented re-written JS based calendar and datepicker
+ Added chinese (simplified) localisation
+ Implemented configurable Date format
+ Fixed some security vulnerabilities
+ Fixed link to tasks RSS feed
+ Fixed detection of own URL on MS IIS
+ Added profile fields for phone and mobile to the userprofile
+ Added company information
+ Added Yahoo map to the userprofile showing the user's location using Yahoo geocoding web API
+ Implemented new calendar for milestones on the desktop
+ Completely re-designed standard theme. Classic theme added.
+ Implemented keyboard Shortcuts (Ctrl-D,Ctrl-T,Ctrl-M,Ctrl-P,esc)
+ Timetracking report is now in ascending chronological order
Collabtive 0.4.8
+ Updated TinyMCE to 3.1
+ Messages can be attached to milestones
+ Fixed "my messages" view to include tags selection
+ Fixed "my messages" view to include tags display
+ Email alerts improved (now completely localised, available for add task and add project)
+ Support for using custom SMTP servers with Email Alerts
+ Fixed: bug in form validator -> it now accepts email adresses containing numbers
+ Improved: display of dependent items in milestones (removed unneeded comma)
+ Fixed: bug in the installer, that made timetracking unusable
+ Timezone can now be set independent of the server timezone in Admin -> System configuration
+ Cleaned up code in init.php
+ Removed redundant call to getAvailableLanguages() in init.php. -> more efficient
+ Implemented support for subfolders
+ When an administrator changes the global system language, all users language settings are updated accordingly.
+ Made link in the user profile clickable
+ Made Email in the user profile clickable
+ Fixed: bulleted lists created from the wyswyg editor (they now actually include bullets ;) )
+ Fixed: various UI glitches
+ Moved the short project report to the "description" area of the project details on projectview
+ Fixed: Only display tagcloud when there actually are tagged items in the project
+ Changed the task title to mandatory , and the text as optional
+ Added spanish readme
+ Beginngins of an XML / JSON API (class toXml)
Collabtive 0.4.7
+ Implemented tagcloud on the projectview
+ Fixed a bug in timetracker:add on windows
+ Cleaned up pathing in init.php (pointed out by jcorreia)
+ Fixed a bug in the updater, preventing messages to be posted
+ Improved installer to check for PHP 5.1 instead of only PHP5
+ Implemented OpenID login
+ Users can now be tagged
+ When deassigning a user from a project, remaining tasks of that user can be re-assigned to another user, or deleted
+ Multisite setup now possible
+ Fixed typo in JS files (character encoding = UTF8 , not :utf8)
+ Timetracking reports now include the comments
+ Implemented preliminary email alerts support
+ Alpha Version of a Google Gadget included
+ Fixed link to tasklist on My Tasks->Done Tasks
+ Updated to the latest Smarty release (2.6.19)
+ Improved Basecamp import
+ Fixed security flaw in admin.php
+ Milestones view on project->milestones can now be toggled between list and calendar view
Collabtive 0.4.6
+ Fixed a bug preventing user from logging in
+ Added dutch locale
+ Added lituanian locale
+ Added measuring of localizing completeness for each locale (translated vs untranslated ratio)
+ Language selectors now show completely localized language names ("English", "German") instead of the locale codes ("en","de")
+ Fixed display Bug in IE7 on the timetracking report (project/user)
+ When deleting a user, all timetracking for that user is deleted, too.
+ Installer cimpletely localized now
+ Additional locales supported in the installer (Spanish, Italian, Japanese, Dutch)
+ Cleaned up language files (removing redundant / unused strings)
+ Running on an SSL/HTTPS connection now properly supported
+ Fix bug when Collabtive is installed with an empty DB password
+ Fixed missing curly brace in chat.js
+ Improved protection against SQL injections
+ Implemented tagging for files and messages
+ Edit user permissions as Admin
+ Datepicker supports all supported locales now.
+ Implemented Basecamp import functionality.
+ Included Basecamp importer in the installer
+ When deleting a project, all tasks from that project are deleted too (fixed typo).
+ Fixed display bug in the "My projects" block when logged in as a non-admin user.
Collabtive 0.4.5
+ Vcard export of userprofiles
+ Fixed a bug in the milestones timeline
+ Added profile fields URL, Gender, ZIP
+ Improved Formvalidator to support regular expressions
+ Timetracker form now checks for correct time format (hh:mm)
+ Updated Tiny MCE to latest version (3.0.5) ; Solves Bug in Safari
+ Removed some unused Tiny MCE plugins from the package.
+ Improved Task management code
+ Messages can be edited again
+ Admins can now edit the passwords of all users.
+ Timetracking for Tasks now possible.
+ Project reporting can now be filtered for tasks
+ Project reporting can now be filtered for users
+ User reporting can now be filtered for projects
+ PDF Reports show the name of the project / user they were generated for
+ PDF and Excel Reports show tasks
+ Reorganized order of buttons in the projectview menu
+ Reorganized order of blocks on the desktop
+ Projectlog can be exported to excel
+ Added Accordeon in Projects Block on the desktop (shows projects description)
+ Fixed some page titles
+ Fixed security bug when uploading avatars (only pictures are accepted, now)
+ Introduced new Userrole: Client. Clients have read-only access to the system. They can also not see any internal messages.
+ When deleting a user, it can now be chosen if the tasks of this user shall be deleted or re-assigned to another user.
+ Polished edges in the installer template
+ Support for locales with more than 2 characters (like pt_br, es_gl, etc)
+ Added turkish locale
+ Added japanese locale
Collabtive 0.4
+ Closed tasks can now be edited again
+ RSS feeds are encoded in UTF8
+ Implemented search functionality (class.search.php + managesearch.php)
+ Ontype search added
+ Closed tasks are visible on the mytasks view even if they have < 1 days "left" under "closed tasks"
+ Fixed wrong link for ZIP export of projectfiles
+ Implemented add search to browser-searchbox for FF2/3 and IE7/8
+ Implemented searchplugin autodiscovery for FF2/3 and IE7/8
+ Project log can be exported to PDF
+ Progressmeter improved: The smaller part of the pie should be moving always .
+ Project progress report as PDF implemented
+ Collapsing the milestones block is now persistent throughout desktop / project view
+ Fixed "Out of range error" on INSERT queries with MySQL5 on Windows
+ Tooltips for milestone details
+ Object (Message, Task, Project, File, etc) descriptions are now properly formatted (added nl2br modifier)
+ Made state of blocks (open / collapsed) persistent for remaining (project,timetracker,log) blocks on project view
+ Improved consistency of language settings (when creating a new user, the system default language is used)
+ Page title of project view now includes Project name
+ My messages Block on the desktop shows only messages from open projects now
+ Refactored database code
+ Added stripslashes() when reading strings from MySQL
+ Added My Messages RSS Feed
+ Added RSS Autodiscovery
+ New messages can now also be added from the My Messages view
+ New Collabtive icon as favicon
+ Changed formblock toggle from appear to blind
+ Fixed notices in iCal export
+ Ical feed can now be imported to ms outlook to (outlook is a bit "special" when parsing ical)
+ Removed favicon option
+ Implemented Systemsounds for Login, Logout and Error
+ Implemented filter for Timetracker report (only display a certain timeframe). Exported reports are filtered, too.
+ Improved styling of the onlinelist
+ Fixed a bug in thumb.php when running on error_reporting(E_ALL)
+ Unified Export icon, that expands on mouseover and shows available export options (RSS , PDF, XLS , etc)
+ Implemented 154 Unit tests to test all classes against regressions automatically. We use Simpletest for this. Fixed many small bugs in the process.
+ My Messages block on the desktop only shown if there actually are messages.
+ Class documentation translated to english
+ Improved localisation in the installer, installing with other systemdefault languages than english works properly now.
+ Made Collabtive run flawless when setting error_reporting(E_STRICT) / Strict mode.
+ Improve security by making config.php non-writable again after the installer has written to it (CHMOD 0755).
+ Added confirm() when deleting closed tasklists
+ Added confirm() when deleting projects from the "my projects" view.
+ Removed project edit, and del for non-admin users on my projects view(were non-functional anyway)
+ First page of the installer better localized
+ HTML Form in installer step2 styled correctly
Collabtive 0.3.6
+ When closing a project, close any objects belonging to it, too
+ When closing a tasklist, close all tasks on the list too
+ When deleting a tasklist, delete all the tasks on the list too
+ When deleting a project, timetracking for this project is deleted too
+ ZIP Format export for projectfiles
+ Fixed uploading/attaching files when replying to a message through the single page form
+ Fixed existing file selector for attaching files when replying
+ Fixed closed objects view
+ Email Address now optional when editing a user
+ Various fields now only visible when present, in profile view
+ When uploading multiple files, they can now have multiple titles
+ Removed duplicated variable declarations in managetasklist.php
+ Show progressindicator only if there is at least 1 finished task
+ Make tasklist names on my tasks view clickable
+ Milestone titles in the overviews are now truncated after 30 characters
+ Added confirmdel string to french locale
+ Replying messages is possible without attaching files
+ New global JS functions delRow() (delete a table row), delEle() (delete any element), systemMsg() (fade in a block and create a timer to fadeout again)
+ Closed tasks in tasklist view are now sorted DESC (latest task shows up first)
+ Project deadline / end date can now be entered
+ Project view shows remaining days until deadline, instead of startdate
+ Fixed problem with file upload paths (http://www.collabtive.o-dyn.de/forum/viewtopic.php?f=11&t=80)
+ PHP Scripts are now uploaded as Textfiles. This prevents random script execution and enables easy viewing as text
+ Duplicate email address / username results in an errorpage (not in a blank page)
+ Removed a duplicated Constant definition in install.php
+ Fixed page title in userprofile view to be multilanguage
+ Pagetitles now finally _completely_ localized
+ Fixed Default system locale->user locale relation
Collabtive 0.3.5
+ Add Flash progressmeter on project view
+ Attach files when replying a message
+ Show replies only when there are > 0 replies in the message view
+ All my Messages view
+ Eventmessages are faded out after 7 seconds
+ Correctly display lightbox on filelinks in the single message view
+ Large code cleanup (no more E_NOTICE)
+ Fixed datepicker when using english locale
+ Add pagination at the project->files view
+ Add projects of a user block to the userprofile , for admins
+ Fixed task title truncating on desktop
+ Updated update.php to reflect changes in 0.3.5
+ Added simple timetracking
+ Changed Profile link in the main navigation from edit profile , to view profile
+ Current time can now be auto entered in the timetracker form onclick.
+ User password can now be changed
+ Activity log is now paginated
+ Excel export for timetracker
+ PDF export for timetracker
+ Fixed pagination to not always "remember" the last page visited. Caused problems when previously visited page doesn't exist anymore.
+ Fixed a 4545possible security vulnerability in class.datei.php
+ Fixed localisation for eventmessages in the filemanager
+ Added a Javascript confirm popup to all delete actions, to avoid accidential deletes
+ Added a Timetracking Tab in the projectview
+ Put all strings from MySQL through stripslashes() to reverse mysql_real_escape_string on add
+ Installer checks for templates_c to be present and writable at startup
+ Installer has better errorhandling
+ Added danish locale
Collabtive-2.0/config/ 0000775 0000000 0000000 00000000000 12372520637 0014713 5 ustar 00root root 0000000 0000000 Collabtive-2.0/config/standard/ 0000775 0000000 0000000 00000000000 12372520637 0016513 5 ustar 00root root 0000000 0000000 Collabtive-2.0/config/standard/config.php 0000664 0000000 0000000 00000000105 12372520637 0020465 0 ustar 00root root 0000000 0000000
Collabtive-2.0/dav.php 0000664 0000000 0000000 00000013766 12372520637 0014746 0 ustar 00root root 0000000 0000000 myPath = $myPath;
$this->proObj = new project();
$this->fileObj = new datei();
}
public function createFile($name, $data = null)
{
$newPath = $this->myPath . '/' . $name;
file_put_contents($newPath, $data);
$this->fileObj->add_file(basename($newPath), "", 1, 0, "", $newPath, "", "");
}
/**
* Creates a new subdirectory
*
* @param string $name
* @return void
*/
public function createDirectory($name)
{
$newPath = $this->myPath . '/' . $name;
mkdir($newPath);
}
/**
* Deletes all files in this directory, and then itself
*
* @return void
*/
public function delete()
{
foreach($this->getChildren() as $child) $child->delete();
rmdir($this->path);
}
function getChildren()
{
$children = array();
// Loop through the directory, and create objects for each node
foreach(scandir($this->myPath) as $node) {
// Ignoring files staring with .
if ($node[0] === '.') continue;
$children[] = $this->getChild($node);
}
return $children;
}
function getChild($name)
{
if (strstr($name, "-")) {
$name = explode("-", $name);
$name = $name[1];
}
$path = $this->myPath . '/' . $name;
// We have to throw a NotFound exception if the file didn't exist
if (!file_exists($path)) die('The file with name: ' . $name . ' could not be found');
// Some added security
if ($name[0] == '.') throw new Sabre_DAV_Exception_NotFound('Access denied');
if (is_dir($path)) {
return new MyDirectory($path);
} else {
return new MyFile($path);
}
}
function childExists($name)
{
if (strstr($name, "-")) {
$name = explode("-", $name);
$name = $name[1];
}
return file_exists($this->myPath . '/' . $name);
}
function getName()
{
$tmpname = (int) basename($this->myPath);
if ($tmpname > 0) {
$user = $_SESSION["userid"];
if (chkproject($user, $tmpname)) {
$name = $this->proObj->getProject($tmpname);
$name = $name["name"];
if ($name and $tmpname) {
return $name . "-" . $tmpname;
}
}
} else {
return basename($this->myPath);
}
}
/**
* Returns available diskspace information
*
* @return array
*/
public function getQuotaInfo()
{
return array(
disk_total_space($this->myPath) - disk_free_space($this->myPath),
disk_free_space($this->myPath)
);
}
}
class MyFile extends Sabre_DAV_FS_File implements Sabre_DAV_IFile {
private $myPath;
function __construct($myPath)
{
$this->myPath = $myPath;
}
public function put($data)
{
file_put_contents($this->$myPath, $data);
}
function getName()
{
return basename($this->myPath);
}
function get()
{
return fopen($this->myPath, 'r');
}
function getSize()
{
return filesize($this->myPath);
}
function getETag()
{
return '"' . md5_file($this->myPath) . '"';
}
/**
* Returns the mime-type for a file
*
* If null is returned, we'll assume application/octet-stream
*
* @return mixed
*/
public function getContentType()
{
return null;
}
public function delete()
{
unlink($this->$myPath);
}
}
$auth = new Sabre_HTTP_BasicAuth();
$result = $auth->getUserPass();
$aUser = $result[0];
$aPass = $result[1];
$userObj = new user();
$profile = $userObj->getProfile($userObj->getId($aUser));
if (!$profile) {
$auth->requireLogin();
echo "Username doesn't exist!\n";
die();
}
if ($profile["pass"] != sha1(trim($aPass))) {
$auth->requireLogin();
echo "Wrong password!\n";
die();
}
$userObj->login($aUser, $aPass);
/*
if (!$result || $result[0]!=$u || $result[1]!=$p) {
$auth->requireLogin();
echo "Authentication required\n";
die();
}
*/
// Now we're creating a whole bunch of objects
// Change public to something else, if you are using a different directory for your files
// $rootDirectory = new Sabre_DAV_FS_Directory('files/standard');
$rootDirectory = new MyDirectory('files/standard');
// The server object is responsible for making sense out of the WebDAV protocol
// Now we create an ObjectTree, which dispatches all requests to your newly created file system
$objectTree = new Sabre_DAV_ObjectTree($rootDirectory);
// The object tree needs in turn to be passed to the server class
$server = new Sabre_DAV_Server($rootDirectory);
// If your server is not on your webroot, make sure the following line has the correct information
// $server->setBaseUri('/~evert/mydavfolder'); // if its in some kind of home directory
$server->setBaseUri('/test/dav.php/'); // if you can't use mod_rewrite, use server.php as a base uri
// $server->setBaseUri('/'); // ideally, SabreDAV lives on a root directory with mod_rewrite sending every request to server.php
// The lock manager is reponsible for making sure users don't overwrite each others changes. Change 'data' to a different
// directory, if you're storing your data somewhere else.
$lockBackend = new Sabre_DAV_Locks_Backend_File('files/davdata/');
$lockPlugin = new Sabre_DAV_Locks_Plugin($lockBackend);
// $server->addPlugin($lockPlugin);
$plugin = new Sabre_DAV_Browser_Plugin();
$server->addPlugin($plugin);
// All we need to do now, is to fire up the server
$server->exec();
?> Collabtive-2.0/include/ 0000775 0000000 0000000 00000000000 12372520637 0015071 5 ustar 00root root 0000000 0000000 Collabtive-2.0/include/Config_File.class.php 0000664 0000000 0000000 00000030706 12372520637 0021060 0 ustar 00root root 0000000 0000000
* @access public
* @package Smarty
*/
/* $Id: Config_File.class.php 2702 2007-03-08 19:11:22Z mohrt $ */
/**
* Config file reading class
* @package Smarty
*/
class Config_File {
/**#@+
* Options
* @var boolean
*/
/**
* Controls whether variables with the same name overwrite each other.
*/
var $overwrite = true;
/**
* Controls whether config values of on/true/yes and off/false/no get
* converted to boolean values automatically.
*/
var $booleanize = true;
/**
* Controls whether hidden config sections/vars are read from the file.
*/
var $read_hidden = true;
/**
* Controls whether or not to fix mac or dos formatted newlines.
* If set to true, \r or \r\n will be changed to \n.
*/
var $fix_newlines = true;
/**#@-*/
/** @access private */
var $_config_path = "";
var $_config_data = array();
/**#@-*/
/**
* Constructs a new config file class.
*
* @param string $config_path (optional) path to the config files
*/
function Config_File($config_path = NULL)
{
if (isset($config_path))
$this->set_path($config_path);
}
/**
* Set the path where configuration files can be found.
*
* @param string $config_path path to the config files
*/
function set_path($config_path)
{
if (!empty($config_path)) {
if (!is_string($config_path) || !file_exists($config_path) || !is_dir($config_path)) {
$this->_trigger_error_msg("Bad config file path '$config_path'");
return;
}
if(substr($config_path, -1) != DIRECTORY_SEPARATOR) {
$config_path .= DIRECTORY_SEPARATOR;
}
$this->_config_path = $config_path;
}
}
/**
* Retrieves config info based on the file, section, and variable name.
*
* @param string $file_name config file to get info for
* @param string $section_name (optional) section to get info for
* @param string $var_name (optional) variable to get info for
* @return string|array a value or array of values
*/
function get($file_name, $section_name = NULL, $var_name = NULL)
{
if (empty($file_name)) {
$this->_trigger_error_msg('Empty config file name');
return;
} else {
$file_name = $this->_config_path . $file_name;
if (!isset($this->_config_data[$file_name]))
$this->load_file($file_name, false);
}
if (!empty($var_name)) {
if (empty($section_name)) {
return $this->_config_data[$file_name]["vars"][$var_name];
} else {
if(isset($this->_config_data[$file_name]["sections"][$section_name]["vars"][$var_name]))
return $this->_config_data[$file_name]["sections"][$section_name]["vars"][$var_name];
else
return array();
}
} else {
if (empty($section_name)) {
return (array)$this->_config_data[$file_name]["vars"];
} else {
if(isset($this->_config_data[$file_name]["sections"][$section_name]["vars"]))
return (array)$this->_config_data[$file_name]["sections"][$section_name]["vars"];
else
return array();
}
}
}
/**
* Retrieves config info based on the key.
*
* @param $file_name string config key (filename/section/var)
* @return string|array same as get()
* @uses get() retrieves information from config file and returns it
*/
function &get_key($config_key)
{
list($file_name, $section_name, $var_name) = explode('/', $config_key, 3);
$result = &$this->get($file_name, $section_name, $var_name);
return $result;
}
/**
* Get all loaded config file names.
*
* @return array an array of loaded config file names
*/
function get_file_names()
{
return array_keys($this->_config_data);
}
/**
* Get all section names from a loaded file.
*
* @param string $file_name config file to get section names from
* @return array an array of section names from the specified file
*/
function get_section_names($file_name)
{
$file_name = $this->_config_path . $file_name;
if (!isset($this->_config_data[$file_name])) {
$this->_trigger_error_msg("Unknown config file '$file_name'");
return;
}
return array_keys($this->_config_data[$file_name]["sections"]);
}
/**
* Get all global or section variable names.
*
* @param string $file_name config file to get info for
* @param string $section_name (optional) section to get info for
* @return array an array of variables names from the specified file/section
*/
function get_var_names($file_name, $section = NULL)
{
if (empty($file_name)) {
$this->_trigger_error_msg('Empty config file name');
return;
} else if (!isset($this->_config_data[$file_name])) {
$this->_trigger_error_msg("Unknown config file '$file_name'");
return;
}
if (empty($section))
return array_keys($this->_config_data[$file_name]["vars"]);
else
return array_keys($this->_config_data[$file_name]["sections"][$section]["vars"]);
}
/**
* Clear loaded config data for a certain file or all files.
*
* @param string $file_name file to clear config data for
*/
function clear($file_name = NULL)
{
if ($file_name === NULL)
$this->_config_data = array();
else if (isset($this->_config_data[$file_name]))
$this->_config_data[$file_name] = array();
}
/**
* Load a configuration file manually.
*
* @param string $file_name file name to load
* @param boolean $prepend_path whether current config path should be
* prepended to the filename
*/
function load_file($file_name, $prepend_path = true)
{
if ($prepend_path && $this->_config_path != "")
$config_file = $this->_config_path . $file_name;
else
$config_file = $file_name;
ini_set('track_errors', true);
$fp = @fopen($config_file, "r");
if (!is_resource($fp)) {
$this->_trigger_error_msg("Could not open config file '$config_file'");
return false;
}
$contents = ($size = filesize($config_file)) ? fread($fp, $size) : '';
fclose($fp);
$this->_config_data[$config_file] = $this->parse_contents($contents);
return true;
}
/**
* Store the contents of a file manually.
*
* @param string $config_file file name of the related contents
* @param string $contents the file-contents to parse
*/
function set_file_contents($config_file, $contents)
{
$this->_config_data[$config_file] = $this->parse_contents($contents);
return true;
}
/**
* parse the source of a configuration file manually.
*
* @param string $contents the file-contents to parse
*/
function parse_contents($contents)
{
if($this->fix_newlines) {
// fix mac/dos formatted newlines
$contents = preg_replace('!\r\n?!', "\n", $contents);
}
$config_data = array();
$config_data['sections'] = array();
$config_data['vars'] = array();
/* reference to fill with data */
$vars =& $config_data['vars'];
/* parse file line by line */
preg_match_all('!^.*\r?\n?!m', $contents, $match);
$lines = $match[0];
for ($i=0, $count=count($lines); $i<$count; $i++) {
$line = $lines[$i];
if (empty($line)) continue;
if ( substr($line, 0, 1) == '[' && preg_match('!^\[(.*?)\]!', $line, $match) ) {
/* section found */
if (substr($match[1], 0, 1) == '.') {
/* hidden section */
if ($this->read_hidden) {
$section_name = substr($match[1], 1);
} else {
/* break reference to $vars to ignore hidden section */
unset($vars);
$vars = array();
continue;
}
} else {
$section_name = $match[1];
}
if (!isset($config_data['sections'][$section_name]))
$config_data['sections'][$section_name] = array('vars' => array());
$vars =& $config_data['sections'][$section_name]['vars'];
continue;
}
if (preg_match('/^\s*(\.?\w+)\s*=\s*(.*)/s', $line, $match)) {
/* variable found */
$var_name = rtrim($match[1]);
if (strpos($match[2], '"""') === 0) {
/* handle multiline-value */
$lines[$i] = substr($match[2], 3);
$var_value = '';
while ($i<$count) {
if (($pos = strpos($lines[$i], '"""')) === false) {
$var_value .= $lines[$i++];
} else {
/* end of multiline-value */
$var_value .= substr($lines[$i], 0, $pos);
break;
}
}
$booleanize = false;
} else {
/* handle simple value */
$var_value = preg_replace('/^([\'"])(.*)\1$/', '\2', rtrim($match[2]));
$booleanize = $this->booleanize;
}
$this->_set_config_var($vars, $var_name, $var_value, $booleanize);
}
/* else unparsable line / means it is a comment / means ignore it */
}
return $config_data;
}
/**#@+ @access private */
/**
* @param array &$container
* @param string $var_name
* @param mixed $var_value
* @param boolean $booleanize determines whether $var_value is converted to
* to true/false
*/
function _set_config_var(&$container, $var_name, $var_value, $booleanize)
{
if (substr($var_name, 0, 1) == '.') {
if (!$this->read_hidden)
return;
else
$var_name = substr($var_name, 1);
}
if (!preg_match("/^[a-zA-Z_]\w*$/", $var_name)) {
$this->_trigger_error_msg("Bad variable name '$var_name'");
return;
}
if ($booleanize) {
if (preg_match("/^(on|true|yes)$/i", $var_value))
$var_value = true;
else if (preg_match("/^(off|false|no)$/i", $var_value))
$var_value = false;
}
if (!isset($container[$var_name]) || $this->overwrite)
$container[$var_name] = $var_value;
else {
settype($container[$var_name], 'array');
$container[$var_name][] = $var_value;
}
}
/**
* @uses trigger_error() creates a PHP warning/error
* @param string $error_msg
* @param integer $error_type one of
*/
function _trigger_error_msg($error_msg, $error_type = E_USER_WARNING)
{
trigger_error("Config_File error: $error_msg", $error_type);
}
/**#@-*/
}
?>
Collabtive-2.0/include/HTMLPurifier.standalone.php 0000664 0000000 0000000 00002452751 12372520637 0022223 0 ustar 00root root 0000000 0000000 config = HTMLPurifier_Config::create($config);
$this->strategy = new HTMLPurifier_Strategy_Core();
}
/**
* Adds a filter to process the output. First come first serve
*
* @param HTMLPurifier_Filter $filter HTMLPurifier_Filter object
*/
public function addFilter($filter)
{
trigger_error(
'HTMLPurifier->addFilter() is deprecated, use configuration directives' .
' in the Filter namespace or Filter.Custom',
E_USER_WARNING
);
$this->filters[] = $filter;
}
/**
* Filters an HTML snippet/document to be XSS-free and standards-compliant.
*
* @param string $html String of HTML to purify
* @param HTMLPurifier_Config $config Config object for this operation,
* if omitted, defaults to the config object specified during this
* object's construction. The parameter can also be any type
* that HTMLPurifier_Config::create() supports.
*
* @return string Purified HTML
*/
public function purify($html, $config = null)
{
// :TODO: make the config merge in, instead of replace
$config = $config ? HTMLPurifier_Config::create($config) : $this->config;
// implementation is partially environment dependant, partially
// configuration dependant
$lexer = HTMLPurifier_Lexer::create($config);
$context = new HTMLPurifier_Context();
// setup HTML generator
$this->generator = new HTMLPurifier_Generator($config, $context);
$context->register('Generator', $this->generator);
// set up global context variables
if ($config->get('Core.CollectErrors')) {
// may get moved out if other facilities use it
$language_factory = HTMLPurifier_LanguageFactory::instance();
$language = $language_factory->create($config, $context);
$context->register('Locale', $language);
$error_collector = new HTMLPurifier_ErrorCollector($context);
$context->register('ErrorCollector', $error_collector);
}
// setup id_accumulator context, necessary due to the fact that
// AttrValidator can be called from many places
$id_accumulator = HTMLPurifier_IDAccumulator::build($config, $context);
$context->register('IDAccumulator', $id_accumulator);
$html = HTMLPurifier_Encoder::convertToUTF8($html, $config, $context);
// setup filters
$filter_flags = $config->getBatch('Filter');
$custom_filters = $filter_flags['Custom'];
unset($filter_flags['Custom']);
$filters = array();
foreach ($filter_flags as $filter => $flag) {
if (!$flag) {
continue;
}
if (strpos($filter, '.') !== false) {
continue;
}
$class = "HTMLPurifier_Filter_$filter";
$filters[] = new $class;
}
foreach ($custom_filters as $filter) {
// maybe "HTMLPurifier_Filter_$filter", but be consistent with AutoFormat
$filters[] = $filter;
}
$filters = array_merge($filters, $this->filters);
// maybe prepare(), but later
for ($i = 0, $filter_size = count($filters); $i < $filter_size; $i++) {
$html = $filters[$i]->preFilter($html, $config, $context);
}
// purified HTML
$html =
$this->generator->generateFromTokens(
// list of tokens
$this->strategy->execute(
// list of un-purified tokens
$lexer->tokenizeHTML(
// un-purified HTML
$html,
$config,
$context
),
$config,
$context
)
);
for ($i = $filter_size - 1; $i >= 0; $i--) {
$html = $filters[$i]->postFilter($html, $config, $context);
}
$html = HTMLPurifier_Encoder::convertFromUTF8($html, $config, $context);
$this->context =& $context;
return $html;
}
/**
* Filters an array of HTML snippets
*
* @param string[] $array_of_html Array of html snippets
* @param HTMLPurifier_Config $config Optional config object for this operation.
* See HTMLPurifier::purify() for more details.
*
* @return string[] Array of purified HTML
*/
public function purifyArray($array_of_html, $config = null)
{
$context_array = array();
foreach ($array_of_html as $key => $html) {
$array_of_html[$key] = $this->purify($html, $config);
$context_array[$key] = $this->context;
}
$this->context = $context_array;
return $array_of_html;
}
/**
* Singleton for enforcing just one HTML Purifier in your system
*
* @param HTMLPurifier|HTMLPurifier_Config $prototype Optional prototype
* HTMLPurifier instance to overload singleton with,
* or HTMLPurifier_Config instance to configure the
* generated version with.
*
* @return HTMLPurifier
*/
public static function instance($prototype = null)
{
if (!self::$instance || $prototype) {
if ($prototype instanceof HTMLPurifier) {
self::$instance = $prototype;
} elseif ($prototype) {
self::$instance = new HTMLPurifier($prototype);
} else {
self::$instance = new HTMLPurifier();
}
}
return self::$instance;
}
/**
* Singleton for enforcing just one HTML Purifier in your system
*
* @param HTMLPurifier|HTMLPurifier_Config $prototype Optional prototype
* HTMLPurifier instance to overload singleton with,
* or HTMLPurifier_Config instance to configure the
* generated version with.
*
* @return HTMLPurifier
* @note Backwards compatibility, see instance()
*/
public static function getInstance($prototype = null)
{
return HTMLPurifier::instance($prototype);
}
}
/**
* Converts a stream of HTMLPurifier_Token into an HTMLPurifier_Node,
* and back again.
*
* @note This transformation is not an equivalence. We mutate the input
* token stream to make it so; see all [MUT] markers in code.
*/
class HTMLPurifier_Arborize
{
public static function arborize($tokens, $config, $context) {
$definition = $config->getHTMLDefinition();
$parent = new HTMLPurifier_Token_Start($definition->info_parent);
$stack = array($parent->toNode());
foreach ($tokens as $token) {
$token->skip = null; // [MUT]
$token->carryover = null; // [MUT]
if ($token instanceof HTMLPurifier_Token_End) {
$token->start = null; // [MUT]
$r = array_pop($stack);
assert($r->name === $token->name);
assert(empty($token->attr));
$r->endCol = $token->col;
$r->endLine = $token->line;
$r->endArmor = $token->armor;
continue;
}
$node = $token->toNode();
$stack[count($stack)-1]->children[] = $node;
if ($token instanceof HTMLPurifier_Token_Start) {
$stack[] = $node;
}
}
assert(count($stack) == 1);
return $stack[0];
}
public static function flatten($node, $config, $context) {
$level = 0;
$nodes = array($level => new HTMLPurifier_Queue(array($node)));
$closingTokens = array();
$tokens = array();
do {
while (!$nodes[$level]->isEmpty()) {
$node = $nodes[$level]->shift(); // FIFO
list($start, $end) = $node->toTokenPair();
if ($level > 0) {
$tokens[] = $start;
}
if ($end !== NULL) {
$closingTokens[$level][] = $end;
}
if ($node instanceof HTMLPurifier_Node_Element) {
$level++;
$nodes[$level] = new HTMLPurifier_Queue();
foreach ($node->children as $childNode) {
$nodes[$level]->push($childNode);
}
}
}
$level--;
if ($level && isset($closingTokens[$level])) {
while ($token = array_pop($closingTokens[$level])) {
$tokens[] = $token;
}
}
} while ($level > 0);
return $tokens;
}
}
/**
* Defines common attribute collections that modules reference
*/
class HTMLPurifier_AttrCollections
{
/**
* Associative array of attribute collections, indexed by name.
* @type array
*/
public $info = array();
/**
* Performs all expansions on internal data for use by other inclusions
* It also collects all attribute collection extensions from
* modules
* @param HTMLPurifier_AttrTypes $attr_types HTMLPurifier_AttrTypes instance
* @param HTMLPurifier_HTMLModule[] $modules Hash array of HTMLPurifier_HTMLModule members
*/
public function __construct($attr_types, $modules)
{
// load extensions from the modules
foreach ($modules as $module) {
foreach ($module->attr_collections as $coll_i => $coll) {
if (!isset($this->info[$coll_i])) {
$this->info[$coll_i] = array();
}
foreach ($coll as $attr_i => $attr) {
if ($attr_i === 0 && isset($this->info[$coll_i][$attr_i])) {
// merge in includes
$this->info[$coll_i][$attr_i] = array_merge(
$this->info[$coll_i][$attr_i],
$attr
);
continue;
}
$this->info[$coll_i][$attr_i] = $attr;
}
}
}
// perform internal expansions and inclusions
foreach ($this->info as $name => $attr) {
// merge attribute collections that include others
$this->performInclusions($this->info[$name]);
// replace string identifiers with actual attribute objects
$this->expandIdentifiers($this->info[$name], $attr_types);
}
}
/**
* Takes a reference to an attribute associative array and performs
* all inclusions specified by the zero index.
* @param array &$attr Reference to attribute array
*/
public function performInclusions(&$attr)
{
if (!isset($attr[0])) {
return;
}
$merge = $attr[0];
$seen = array(); // recursion guard
// loop through all the inclusions
for ($i = 0; isset($merge[$i]); $i++) {
if (isset($seen[$merge[$i]])) {
continue;
}
$seen[$merge[$i]] = true;
// foreach attribute of the inclusion, copy it over
if (!isset($this->info[$merge[$i]])) {
continue;
}
foreach ($this->info[$merge[$i]] as $key => $value) {
if (isset($attr[$key])) {
continue;
} // also catches more inclusions
$attr[$key] = $value;
}
if (isset($this->info[$merge[$i]][0])) {
// recursion
$merge = array_merge($merge, $this->info[$merge[$i]][0]);
}
}
unset($attr[0]);
}
/**
* Expands all string identifiers in an attribute array by replacing
* them with the appropriate values inside HTMLPurifier_AttrTypes
* @param array &$attr Reference to attribute array
* @param HTMLPurifier_AttrTypes $attr_types HTMLPurifier_AttrTypes instance
*/
public function expandIdentifiers(&$attr, $attr_types)
{
// because foreach will process new elements we add, make sure we
// skip duplicates
$processed = array();
foreach ($attr as $def_i => $def) {
// skip inclusions
if ($def_i === 0) {
continue;
}
if (isset($processed[$def_i])) {
continue;
}
// determine whether or not attribute is required
if ($required = (strpos($def_i, '*') !== false)) {
// rename the definition
unset($attr[$def_i]);
$def_i = trim($def_i, '*');
$attr[$def_i] = $def;
}
$processed[$def_i] = true;
// if we've already got a literal object, move on
if (is_object($def)) {
// preserve previous required
$attr[$def_i]->required = ($required || $attr[$def_i]->required);
continue;
}
if ($def === false) {
unset($attr[$def_i]);
continue;
}
if ($t = $attr_types->get($def)) {
$attr[$def_i] = $t;
$attr[$def_i]->required = $required;
} else {
unset($attr[$def_i]);
}
}
}
}
/**
* Base class for all validating attribute definitions.
*
* This family of classes forms the core for not only HTML attribute validation,
* but also any sort of string that needs to be validated or cleaned (which
* means CSS properties and composite definitions are defined here too).
* Besides defining (through code) what precisely makes the string valid,
* subclasses are also responsible for cleaning the code if possible.
*/
abstract class HTMLPurifier_AttrDef
{
/**
* Tells us whether or not an HTML attribute is minimized.
* Has no meaning in other contexts.
* @type bool
*/
public $minimized = false;
/**
* Tells us whether or not an HTML attribute is required.
* Has no meaning in other contexts
* @type bool
*/
public $required = false;
/**
* Validates and cleans passed string according to a definition.
*
* @param string $string String to be validated and cleaned.
* @param HTMLPurifier_Config $config Mandatory HTMLPurifier_Config object.
* @param HTMLPurifier_Context $context Mandatory HTMLPurifier_Context object.
*/
abstract public function validate($string, $config, $context);
/**
* Convenience method that parses a string as if it were CDATA.
*
* This method process a string in the manner specified at
* by removing
* leading and trailing whitespace, ignoring line feeds, and replacing
* carriage returns and tabs with spaces. While most useful for HTML
* attributes specified as CDATA, it can also be applied to most CSS
* values.
*
* @note This method is not entirely standards compliant, as trim() removes
* more types of whitespace than specified in the spec. In practice,
* this is rarely a problem, as those extra characters usually have
* already been removed by HTMLPurifier_Encoder.
*
* @warning This processing is inconsistent with XML's whitespace handling
* as specified by section 3.3.3 and referenced XHTML 1.0 section
* 4.7. However, note that we are NOT necessarily
* parsing XML, thus, this behavior may still be correct. We
* assume that newlines have been normalized.
*/
public function parseCDATA($string)
{
$string = trim($string);
$string = str_replace(array("\n", "\t", "\r"), ' ', $string);
return $string;
}
/**
* Factory method for creating this class from a string.
* @param string $string String construction info
* @return HTMLPurifier_AttrDef Created AttrDef object corresponding to $string
*/
public function make($string)
{
// default implementation, return a flyweight of this object.
// If $string has an effect on the returned object (i.e. you
// need to overload this method), it is best
// to clone or instantiate new copies. (Instantiation is safer.)
return $this;
}
/**
* Removes spaces from rgb(0, 0, 0) so that shorthand CSS properties work
* properly. THIS IS A HACK!
* @param string $string a CSS colour definition
* @return string
*/
protected function mungeRgb($string)
{
return preg_replace('/rgb\((\d+)\s*,\s*(\d+)\s*,\s*(\d+)\)/', 'rgb(\1,\2,\3)', $string);
}
/**
* Parses a possibly escaped CSS string and returns the "pure"
* version of it.
*/
protected function expandCSSEscape($string)
{
// flexibly parse it
$ret = '';
for ($i = 0, $c = strlen($string); $i < $c; $i++) {
if ($string[$i] === '\\') {
$i++;
if ($i >= $c) {
$ret .= '\\';
break;
}
if (ctype_xdigit($string[$i])) {
$code = $string[$i];
for ($a = 1, $i++; $i < $c && $a < 6; $i++, $a++) {
if (!ctype_xdigit($string[$i])) {
break;
}
$code .= $string[$i];
}
// We have to be extremely careful when adding
// new characters, to make sure we're not breaking
// the encoding.
$char = HTMLPurifier_Encoder::unichr(hexdec($code));
if (HTMLPurifier_Encoder::cleanUTF8($char) === '') {
continue;
}
$ret .= $char;
if ($i < $c && trim($string[$i]) !== '') {
$i--;
}
continue;
}
if ($string[$i] === "\n") {
continue;
}
}
$ret .= $string[$i];
}
return $ret;
}
}
/**
* Processes an entire attribute array for corrections needing multiple values.
*
* Occasionally, a certain attribute will need to be removed and popped onto
* another value. Instead of creating a complex return syntax for
* HTMLPurifier_AttrDef, we just pass the whole attribute array to a
* specialized object and have that do the special work. That is the
* family of HTMLPurifier_AttrTransform.
*
* An attribute transformation can be assigned to run before or after
* HTMLPurifier_AttrDef validation. See HTMLPurifier_HTMLDefinition for
* more details.
*/
abstract class HTMLPurifier_AttrTransform
{
/**
* Abstract: makes changes to the attributes dependent on multiple values.
*
* @param array $attr Assoc array of attributes, usually from
* HTMLPurifier_Token_Tag::$attr
* @param HTMLPurifier_Config $config Mandatory HTMLPurifier_Config object.
* @param HTMLPurifier_Context $context Mandatory HTMLPurifier_Context object
* @return array Processed attribute array.
*/
abstract public function transform($attr, $config, $context);
/**
* Prepends CSS properties to the style attribute, creating the
* attribute if it doesn't exist.
* @param array &$attr Attribute array to process (passed by reference)
* @param string $css CSS to prepend
*/
public function prependCSS(&$attr, $css)
{
$attr['style'] = isset($attr['style']) ? $attr['style'] : '';
$attr['style'] = $css . $attr['style'];
}
/**
* Retrieves and removes an attribute
* @param array &$attr Attribute array to process (passed by reference)
* @param mixed $key Key of attribute to confiscate
* @return mixed
*/
public function confiscateAttr(&$attr, $key)
{
if (!isset($attr[$key])) {
return null;
}
$value = $attr[$key];
unset($attr[$key]);
return $value;
}
}
/**
* Provides lookup array of attribute types to HTMLPurifier_AttrDef objects
*/
class HTMLPurifier_AttrTypes
{
/**
* Lookup array of attribute string identifiers to concrete implementations.
* @type HTMLPurifier_AttrDef[]
*/
protected $info = array();
/**
* Constructs the info array, supplying default implementations for attribute
* types.
*/
public function __construct()
{
// XXX This is kind of poor, since we don't actually /clone/
// instances; instead, we use the supplied make() attribute. So,
// the underlying class must know how to deal with arguments.
// With the old implementation of Enum, that ignored its
// arguments when handling a make dispatch, the IAlign
// definition wouldn't work.
// pseudo-types, must be instantiated via shorthand
$this->info['Enum'] = new HTMLPurifier_AttrDef_Enum();
$this->info['Bool'] = new HTMLPurifier_AttrDef_HTML_Bool();
$this->info['CDATA'] = new HTMLPurifier_AttrDef_Text();
$this->info['ID'] = new HTMLPurifier_AttrDef_HTML_ID();
$this->info['Length'] = new HTMLPurifier_AttrDef_HTML_Length();
$this->info['MultiLength'] = new HTMLPurifier_AttrDef_HTML_MultiLength();
$this->info['NMTOKENS'] = new HTMLPurifier_AttrDef_HTML_Nmtokens();
$this->info['Pixels'] = new HTMLPurifier_AttrDef_HTML_Pixels();
$this->info['Text'] = new HTMLPurifier_AttrDef_Text();
$this->info['URI'] = new HTMLPurifier_AttrDef_URI();
$this->info['LanguageCode'] = new HTMLPurifier_AttrDef_Lang();
$this->info['Color'] = new HTMLPurifier_AttrDef_HTML_Color();
$this->info['IAlign'] = self::makeEnum('top,middle,bottom,left,right');
$this->info['LAlign'] = self::makeEnum('top,bottom,left,right');
$this->info['FrameTarget'] = new HTMLPurifier_AttrDef_HTML_FrameTarget();
// unimplemented aliases
$this->info['ContentType'] = new HTMLPurifier_AttrDef_Text();
$this->info['ContentTypes'] = new HTMLPurifier_AttrDef_Text();
$this->info['Charsets'] = new HTMLPurifier_AttrDef_Text();
$this->info['Character'] = new HTMLPurifier_AttrDef_Text();
// "proprietary" types
$this->info['Class'] = new HTMLPurifier_AttrDef_HTML_Class();
// number is really a positive integer (one or more digits)
// FIXME: ^^ not always, see start and value of list items
$this->info['Number'] = new HTMLPurifier_AttrDef_Integer(false, false, true);
}
private static function makeEnum($in)
{
return new HTMLPurifier_AttrDef_Clone(new HTMLPurifier_AttrDef_Enum(explode(',', $in)));
}
/**
* Retrieves a type
* @param string $type String type name
* @return HTMLPurifier_AttrDef Object AttrDef for type
*/
public function get($type)
{
// determine if there is any extra info tacked on
if (strpos($type, '#') !== false) {
list($type, $string) = explode('#', $type, 2);
} else {
$string = '';
}
if (!isset($this->info[$type])) {
trigger_error('Cannot retrieve undefined attribute type ' . $type, E_USER_ERROR);
return;
}
return $this->info[$type]->make($string);
}
/**
* Sets a new implementation for a type
* @param string $type String type name
* @param HTMLPurifier_AttrDef $impl Object AttrDef for type
*/
public function set($type, $impl)
{
$this->info[$type] = $impl;
}
}
/**
* Validates the attributes of a token. Doesn't manage required attributes
* very well. The only reason we factored this out was because RemoveForeignElements
* also needed it besides ValidateAttributes.
*/
class HTMLPurifier_AttrValidator
{
/**
* Validates the attributes of a token, mutating it as necessary.
* that has valid tokens
* @param HTMLPurifier_Token $token Token to validate.
* @param HTMLPurifier_Config $config Instance of HTMLPurifier_Config
* @param HTMLPurifier_Context $context Instance of HTMLPurifier_Context
*/
public function validateToken($token, $config, $context)
{
$definition = $config->getHTMLDefinition();
$e =& $context->get('ErrorCollector', true);
// initialize IDAccumulator if necessary
$ok =& $context->get('IDAccumulator', true);
if (!$ok) {
$id_accumulator = HTMLPurifier_IDAccumulator::build($config, $context);
$context->register('IDAccumulator', $id_accumulator);
}
// initialize CurrentToken if necessary
$current_token =& $context->get('CurrentToken', true);
if (!$current_token) {
$context->register('CurrentToken', $token);
}
if (!$token instanceof HTMLPurifier_Token_Start &&
!$token instanceof HTMLPurifier_Token_Empty
) {
return;
}
// create alias to global definition array, see also $defs
// DEFINITION CALL
$d_defs = $definition->info_global_attr;
// don't update token until the very end, to ensure an atomic update
$attr = $token->attr;
// do global transformations (pre)
// nothing currently utilizes this
foreach ($definition->info_attr_transform_pre as $transform) {
$attr = $transform->transform($o = $attr, $config, $context);
if ($e) {
if ($attr != $o) {
$e->send(E_NOTICE, 'AttrValidator: Attributes transformed', $o, $attr);
}
}
}
// do local transformations only applicable to this element (pre)
// ex.
to
foreach ($definition->info[$token->name]->attr_transform_pre as $transform) {
$attr = $transform->transform($o = $attr, $config, $context);
if ($e) {
if ($attr != $o) {
$e->send(E_NOTICE, 'AttrValidator: Attributes transformed', $o, $attr);
}
}
}
// create alias to this element's attribute definition array, see
// also $d_defs (global attribute definition array)
// DEFINITION CALL
$defs = $definition->info[$token->name]->attr;
$attr_key = false;
$context->register('CurrentAttr', $attr_key);
// iterate through all the attribute keypairs
// Watch out for name collisions: $key has previously been used
foreach ($attr as $attr_key => $value) {
// call the definition
if (isset($defs[$attr_key])) {
// there is a local definition defined
if ($defs[$attr_key] === false) {
// We've explicitly been told not to allow this element.
// This is usually when there's a global definition
// that must be overridden.
// Theoretically speaking, we could have a
// AttrDef_DenyAll, but this is faster!
$result = false;
} else {
// validate according to the element's definition
$result = $defs[$attr_key]->validate(
$value,
$config,
$context
);
}
} elseif (isset($d_defs[$attr_key])) {
// there is a global definition defined, validate according
// to the global definition
$result = $d_defs[$attr_key]->validate(
$value,
$config,
$context
);
} else {
// system never heard of the attribute? DELETE!
$result = false;
}
// put the results into effect
if ($result === false || $result === null) {
// this is a generic error message that should replaced
// with more specific ones when possible
if ($e) {
$e->send(E_ERROR, 'AttrValidator: Attribute removed');
}
// remove the attribute
unset($attr[$attr_key]);
} elseif (is_string($result)) {
// generally, if a substitution is happening, there
// was some sort of implicit correction going on. We'll
// delegate it to the attribute classes to say exactly what.
// simple substitution
$attr[$attr_key] = $result;
} else {
// nothing happens
}
// we'd also want slightly more complicated substitution
// involving an array as the return value,
// although we're not sure how colliding attributes would
// resolve (certain ones would be completely overriden,
// others would prepend themselves).
}
$context->destroy('CurrentAttr');
// post transforms
// global (error reporting untested)
foreach ($definition->info_attr_transform_post as $transform) {
$attr = $transform->transform($o = $attr, $config, $context);
if ($e) {
if ($attr != $o) {
$e->send(E_NOTICE, 'AttrValidator: Attributes transformed', $o, $attr);
}
}
}
// local (error reporting untested)
foreach ($definition->info[$token->name]->attr_transform_post as $transform) {
$attr = $transform->transform($o = $attr, $config, $context);
if ($e) {
if ($attr != $o) {
$e->send(E_NOTICE, 'AttrValidator: Attributes transformed', $o, $attr);
}
}
}
$token->attr = $attr;
// destroy CurrentToken if we made it ourselves
if (!$current_token) {
$context->destroy('CurrentToken');
}
}
}
// constants are slow, so we use as few as possible
if (!defined('HTMLPURIFIER_PREFIX')) {
define('HTMLPURIFIER_PREFIX', dirname(__FILE__) . '/standalone');
set_include_path(HTMLPURIFIER_PREFIX . PATH_SEPARATOR . get_include_path());
}
// accomodations for versions earlier than 5.0.2
// borrowed from PHP_Compat, LGPL licensed, by Aidan Lister
if (!defined('PHP_EOL')) {
switch (strtoupper(substr(PHP_OS, 0, 3))) {
case 'WIN':
define('PHP_EOL', "\r\n");
break;
case 'DAR':
define('PHP_EOL', "\r");
break;
default:
define('PHP_EOL', "\n");
}
}
/**
* Bootstrap class that contains meta-functionality for HTML Purifier such as
* the autoload function.
*
* @note
* This class may be used without any other files from HTML Purifier.
*/
class HTMLPurifier_Bootstrap
{
/**
* Autoload function for HTML Purifier
* @param string $class Class to load
* @return bool
*/
public static function autoload($class)
{
$file = HTMLPurifier_Bootstrap::getPath($class);
if (!$file) {
return false;
}
// Technically speaking, it should be ok and more efficient to
// just do 'require', but Antonio Parraga reports that with
// Zend extensions such as Zend debugger and APC, this invariant
// may be broken. Since we have efficient alternatives, pay
// the cost here and avoid the bug.
require_once HTMLPURIFIER_PREFIX . '/' . $file;
return true;
}
/**
* Returns the path for a specific class.
* @param string $class Class path to get
* @return string
*/
public static function getPath($class)
{
if (strncmp('HTMLPurifier', $class, 12) !== 0) {
return false;
}
// Custom implementations
if (strncmp('HTMLPurifier_Language_', $class, 22) === 0) {
$code = str_replace('_', '-', substr($class, 22));
$file = 'HTMLPurifier/Language/classes/' . $code . '.php';
} else {
$file = str_replace('_', '/', $class) . '.php';
}
if (!file_exists(HTMLPURIFIER_PREFIX . '/' . $file)) {
return false;
}
return $file;
}
/**
* "Pre-registers" our autoloader on the SPL stack.
*/
public static function registerAutoload()
{
$autoload = array('HTMLPurifier_Bootstrap', 'autoload');
if (($funcs = spl_autoload_functions()) === false) {
spl_autoload_register($autoload);
} elseif (function_exists('spl_autoload_unregister')) {
if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
// prepend flag exists, no need for shenanigans
spl_autoload_register($autoload, true, true);
} else {
$buggy = version_compare(PHP_VERSION, '5.2.11', '<');
$compat = version_compare(PHP_VERSION, '5.1.2', '<=') &&
version_compare(PHP_VERSION, '5.1.0', '>=');
foreach ($funcs as $func) {
if ($buggy && is_array($func)) {
// :TRICKY: There are some compatibility issues and some
// places where we need to error out
$reflector = new ReflectionMethod($func[0], $func[1]);
if (!$reflector->isStatic()) {
throw new Exception(
'HTML Purifier autoloader registrar is not compatible
with non-static object methods due to PHP Bug #44144;
Please do not use HTMLPurifier.autoload.php (or any
file that includes this file); instead, place the code:
spl_autoload_register(array(\'HTMLPurifier_Bootstrap\', \'autoload\'))
after your own autoloaders.'
);
}
// Suprisingly, spl_autoload_register supports the
// Class::staticMethod callback format, although call_user_func doesn't
if ($compat) {
$func = implode('::', $func);
}
}
spl_autoload_unregister($func);
}
spl_autoload_register($autoload);
foreach ($funcs as $func) {
spl_autoload_register($func);
}
}
}
}
}
/**
* Super-class for definition datatype objects, implements serialization
* functions for the class.
*/
abstract class HTMLPurifier_Definition
{
/**
* Has setup() been called yet?
* @type bool
*/
public $setup = false;
/**
* If true, write out the final definition object to the cache after
* setup. This will be true only if all invocations to get a raw
* definition object are also optimized. This does not cause file
* system thrashing because on subsequent calls the cached object
* is used and any writes to the raw definition object are short
* circuited. See enduser-customize.html for the high-level
* picture.
* @type bool
*/
public $optimized = null;
/**
* What type of definition is it?
* @type string
*/
public $type;
/**
* Sets up the definition object into the final form, something
* not done by the constructor
* @param HTMLPurifier_Config $config
*/
abstract protected function doSetup($config);
/**
* Setup function that aborts if already setup
* @param HTMLPurifier_Config $config
*/
public function setup($config)
{
if ($this->setup) {
return;
}
$this->setup = true;
$this->doSetup($config);
}
}
/**
* Defines allowed CSS attributes and what their values are.
* @see HTMLPurifier_HTMLDefinition
*/
class HTMLPurifier_CSSDefinition extends HTMLPurifier_Definition
{
public $type = 'CSS';
/**
* Assoc array of attribute name to definition object.
* @type HTMLPurifier_AttrDef[]
*/
public $info = array();
/**
* Constructs the info array. The meat of this class.
* @param HTMLPurifier_Config $config
*/
protected function doSetup($config)
{
$this->info['text-align'] = new HTMLPurifier_AttrDef_Enum(
array('left', 'right', 'center', 'justify'),
false
);
$border_style =
$this->info['border-bottom-style'] =
$this->info['border-right-style'] =
$this->info['border-left-style'] =
$this->info['border-top-style'] = new HTMLPurifier_AttrDef_Enum(
array(
'none',
'hidden',
'dotted',
'dashed',
'solid',
'double',
'groove',
'ridge',
'inset',
'outset'
),
false
);
$this->info['border-style'] = new HTMLPurifier_AttrDef_CSS_Multiple($border_style);
$this->info['clear'] = new HTMLPurifier_AttrDef_Enum(
array('none', 'left', 'right', 'both'),
false
);
$this->info['float'] = new HTMLPurifier_AttrDef_Enum(
array('none', 'left', 'right'),
false
);
$this->info['font-style'] = new HTMLPurifier_AttrDef_Enum(
array('normal', 'italic', 'oblique'),
false
);
$this->info['font-variant'] = new HTMLPurifier_AttrDef_Enum(
array('normal', 'small-caps'),
false
);
$uri_or_none = new HTMLPurifier_AttrDef_CSS_Composite(
array(
new HTMLPurifier_AttrDef_Enum(array('none')),
new HTMLPurifier_AttrDef_CSS_URI()
)
);
$this->info['list-style-position'] = new HTMLPurifier_AttrDef_Enum(
array('inside', 'outside'),
false
);
$this->info['list-style-type'] = new HTMLPurifier_AttrDef_Enum(
array(
'disc',
'circle',
'square',
'decimal',
'lower-roman',
'upper-roman',
'lower-alpha',
'upper-alpha',
'none'
),
false
);
$this->info['list-style-image'] = $uri_or_none;
$this->info['list-style'] = new HTMLPurifier_AttrDef_CSS_ListStyle($config);
$this->info['text-transform'] = new HTMLPurifier_AttrDef_Enum(
array('capitalize', 'uppercase', 'lowercase', 'none'),
false
);
$this->info['color'] = new HTMLPurifier_AttrDef_CSS_Color();
$this->info['background-image'] = $uri_or_none;
$this->info['background-repeat'] = new HTMLPurifier_AttrDef_Enum(
array('repeat', 'repeat-x', 'repeat-y', 'no-repeat')
);
$this->info['background-attachment'] = new HTMLPurifier_AttrDef_Enum(
array('scroll', 'fixed')
);
$this->info['background-position'] = new HTMLPurifier_AttrDef_CSS_BackgroundPosition();
$border_color =
$this->info['border-top-color'] =
$this->info['border-bottom-color'] =
$this->info['border-left-color'] =
$this->info['border-right-color'] =
$this->info['background-color'] = new HTMLPurifier_AttrDef_CSS_Composite(
array(
new HTMLPurifier_AttrDef_Enum(array('transparent')),
new HTMLPurifier_AttrDef_CSS_Color()
)
);
$this->info['background'] = new HTMLPurifier_AttrDef_CSS_Background($config);
$this->info['border-color'] = new HTMLPurifier_AttrDef_CSS_Multiple($border_color);
$border_width =
$this->info['border-top-width'] =
$this->info['border-bottom-width'] =
$this->info['border-left-width'] =
$this->info['border-right-width'] = new HTMLPurifier_AttrDef_CSS_Composite(
array(
new HTMLPurifier_AttrDef_Enum(array('thin', 'medium', 'thick')),
new HTMLPurifier_AttrDef_CSS_Length('0') //disallow negative
)
);
$this->info['border-width'] = new HTMLPurifier_AttrDef_CSS_Multiple($border_width);
$this->info['letter-spacing'] = new HTMLPurifier_AttrDef_CSS_Composite(
array(
new HTMLPurifier_AttrDef_Enum(array('normal')),
new HTMLPurifier_AttrDef_CSS_Length()
)
);
$this->info['word-spacing'] = new HTMLPurifier_AttrDef_CSS_Composite(
array(
new HTMLPurifier_AttrDef_Enum(array('normal')),
new HTMLPurifier_AttrDef_CSS_Length()
)
);
$this->info['font-size'] = new HTMLPurifier_AttrDef_CSS_Composite(
array(
new HTMLPurifier_AttrDef_Enum(
array(
'xx-small',
'x-small',
'small',
'medium',
'large',
'x-large',
'xx-large',
'larger',
'smaller'
)
),
new HTMLPurifier_AttrDef_CSS_Percentage(),
new HTMLPurifier_AttrDef_CSS_Length()
)
);
$this->info['line-height'] = new HTMLPurifier_AttrDef_CSS_Composite(
array(
new HTMLPurifier_AttrDef_Enum(array('normal')),
new HTMLPurifier_AttrDef_CSS_Number(true), // no negatives
new HTMLPurifier_AttrDef_CSS_Length('0'),
new HTMLPurifier_AttrDef_CSS_Percentage(true)
)
);
$margin =
$this->info['margin-top'] =
$this->info['margin-bottom'] =
$this->info['margin-left'] =
$this->info['margin-right'] = new HTMLPurifier_AttrDef_CSS_Composite(
array(
new HTMLPurifier_AttrDef_CSS_Length(),
new HTMLPurifier_AttrDef_CSS_Percentage(),
new HTMLPurifier_AttrDef_Enum(array('auto'))
)
);
$this->info['margin'] = new HTMLPurifier_AttrDef_CSS_Multiple($margin);
// non-negative
$padding =
$this->info['padding-top'] =
$this->info['padding-bottom'] =
$this->info['padding-left'] =
$this->info['padding-right'] = new HTMLPurifier_AttrDef_CSS_Composite(
array(
new HTMLPurifier_AttrDef_CSS_Length('0'),
new HTMLPurifier_AttrDef_CSS_Percentage(true)
)
);
$this->info['padding'] = new HTMLPurifier_AttrDef_CSS_Multiple($padding);
$this->info['text-indent'] = new HTMLPurifier_AttrDef_CSS_Composite(
array(
new HTMLPurifier_AttrDef_CSS_Length(),
new HTMLPurifier_AttrDef_CSS_Percentage()
)
);
$trusted_wh = new HTMLPurifier_AttrDef_CSS_Composite(
array(
new HTMLPurifier_AttrDef_CSS_Length('0'),
new HTMLPurifier_AttrDef_CSS_Percentage(true),
new HTMLPurifier_AttrDef_Enum(array('auto'))
)
);
$max = $config->get('CSS.MaxImgLength');
$this->info['width'] =
$this->info['height'] =
$max === null ?
$trusted_wh :
new HTMLPurifier_AttrDef_Switch(
'img',
// For img tags:
new HTMLPurifier_AttrDef_CSS_Composite(
array(
new HTMLPurifier_AttrDef_CSS_Length('0', $max),
new HTMLPurifier_AttrDef_Enum(array('auto'))
)
),
// For everyone else:
$trusted_wh
);
$this->info['text-decoration'] = new HTMLPurifier_AttrDef_CSS_TextDecoration();
$this->info['font-family'] = new HTMLPurifier_AttrDef_CSS_FontFamily();
// this could use specialized code
$this->info['font-weight'] = new HTMLPurifier_AttrDef_Enum(
array(
'normal',
'bold',
'bolder',
'lighter',
'100',
'200',
'300',
'400',
'500',
'600',
'700',
'800',
'900'
),
false
);
// MUST be called after other font properties, as it references
// a CSSDefinition object
$this->info['font'] = new HTMLPurifier_AttrDef_CSS_Font($config);
// same here
$this->info['border'] =
$this->info['border-bottom'] =
$this->info['border-top'] =
$this->info['border-left'] =
$this->info['border-right'] = new HTMLPurifier_AttrDef_CSS_Border($config);
$this->info['border-collapse'] = new HTMLPurifier_AttrDef_Enum(
array('collapse', 'separate')
);
$this->info['caption-side'] = new HTMLPurifier_AttrDef_Enum(
array('top', 'bottom')
);
$this->info['table-layout'] = new HTMLPurifier_AttrDef_Enum(
array('auto', 'fixed')
);
$this->info['vertical-align'] = new HTMLPurifier_AttrDef_CSS_Composite(
array(
new HTMLPurifier_AttrDef_Enum(
array(
'baseline',
'sub',
'super',
'top',
'text-top',
'middle',
'bottom',
'text-bottom'
)
),
new HTMLPurifier_AttrDef_CSS_Length(),
new HTMLPurifier_AttrDef_CSS_Percentage()
)
);
$this->info['border-spacing'] = new HTMLPurifier_AttrDef_CSS_Multiple(new HTMLPurifier_AttrDef_CSS_Length(), 2);
// These CSS properties don't work on many browsers, but we live
// in THE FUTURE!
$this->info['white-space'] = new HTMLPurifier_AttrDef_Enum(
array('nowrap', 'normal', 'pre', 'pre-wrap', 'pre-line')
);
if ($config->get('CSS.Proprietary')) {
$this->doSetupProprietary($config);
}
if ($config->get('CSS.AllowTricky')) {
$this->doSetupTricky($config);
}
if ($config->get('CSS.Trusted')) {
$this->doSetupTrusted($config);
}
$allow_important = $config->get('CSS.AllowImportant');
// wrap all attr-defs with decorator that handles !important
foreach ($this->info as $k => $v) {
$this->info[$k] = new HTMLPurifier_AttrDef_CSS_ImportantDecorator($v, $allow_important);
}
$this->setupConfigStuff($config);
}
/**
* @param HTMLPurifier_Config $config
*/
protected function doSetupProprietary($config)
{
// Internet Explorer only scrollbar colors
$this->info['scrollbar-arrow-color'] = new HTMLPurifier_AttrDef_CSS_Color();
$this->info['scrollbar-base-color'] = new HTMLPurifier_AttrDef_CSS_Color();
$this->info['scrollbar-darkshadow-color'] = new HTMLPurifier_AttrDef_CSS_Color();
$this->info['scrollbar-face-color'] = new HTMLPurifier_AttrDef_CSS_Color();
$this->info['scrollbar-highlight-color'] = new HTMLPurifier_AttrDef_CSS_Color();
$this->info['scrollbar-shadow-color'] = new HTMLPurifier_AttrDef_CSS_Color();
// technically not proprietary, but CSS3, and no one supports it
$this->info['opacity'] = new HTMLPurifier_AttrDef_CSS_AlphaValue();
$this->info['-moz-opacity'] = new HTMLPurifier_AttrDef_CSS_AlphaValue();
$this->info['-khtml-opacity'] = new HTMLPurifier_AttrDef_CSS_AlphaValue();
// only opacity, for now
$this->info['filter'] = new HTMLPurifier_AttrDef_CSS_Filter();
// more CSS3
$this->info['page-break-after'] =
$this->info['page-break-before'] = new HTMLPurifier_AttrDef_Enum(
array(
'auto',
'always',
'avoid',
'left',
'right'
)
);
$this->info['page-break-inside'] = new HTMLPurifier_AttrDef_Enum(array('auto', 'avoid'));
}
/**
* @param HTMLPurifier_Config $config
*/
protected function doSetupTricky($config)
{
$this->info['display'] = new HTMLPurifier_AttrDef_Enum(
array(
'inline',
'block',
'list-item',
'run-in',
'compact',
'marker',
'table',
'inline-block',
'inline-table',
'table-row-group',
'table-header-group',
'table-footer-group',
'table-row',
'table-column-group',
'table-column',
'table-cell',
'table-caption',
'none'
)
);
$this->info['visibility'] = new HTMLPurifier_AttrDef_Enum(
array('visible', 'hidden', 'collapse')
);
$this->info['overflow'] = new HTMLPurifier_AttrDef_Enum(array('visible', 'hidden', 'auto', 'scroll'));
}
/**
* @param HTMLPurifier_Config $config
*/
protected function doSetupTrusted($config)
{
$this->info['position'] = new HTMLPurifier_AttrDef_Enum(
array('static', 'relative', 'absolute', 'fixed')
);
$this->info['top'] =
$this->info['left'] =
$this->info['right'] =
$this->info['bottom'] = new HTMLPurifier_AttrDef_CSS_Composite(
array(
new HTMLPurifier_AttrDef_CSS_Length(),
new HTMLPurifier_AttrDef_CSS_Percentage(),
new HTMLPurifier_AttrDef_Enum(array('auto')),
)
);
$this->info['z-index'] = new HTMLPurifier_AttrDef_CSS_Composite(
array(
new HTMLPurifier_AttrDef_Integer(),
new HTMLPurifier_AttrDef_Enum(array('auto')),
)
);
}
/**
* Performs extra config-based processing. Based off of
* HTMLPurifier_HTMLDefinition.
* @param HTMLPurifier_Config $config
* @todo Refactor duplicate elements into common class (probably using
* composition, not inheritance).
*/
protected function setupConfigStuff($config)
{
// setup allowed elements
$support = "(for information on implementing this, see the " .
"support forums) ";
$allowed_properties = $config->get('CSS.AllowedProperties');
if ($allowed_properties !== null) {
foreach ($this->info as $name => $d) {
if (!isset($allowed_properties[$name])) {
unset($this->info[$name]);
}
unset($allowed_properties[$name]);
}
// emit errors
foreach ($allowed_properties as $name => $d) {
// :TODO: Is this htmlspecialchars() call really necessary?
$name = htmlspecialchars($name);
trigger_error("Style attribute '$name' is not supported $support", E_USER_WARNING);
}
}
$forbidden_properties = $config->get('CSS.ForbiddenProperties');
if ($forbidden_properties !== null) {
foreach ($this->info as $name => $d) {
if (isset($forbidden_properties[$name])) {
unset($this->info[$name]);
}
}
}
}
}
/**
* Defines allowed child nodes and validates nodes against it.
*/
abstract class HTMLPurifier_ChildDef
{
/**
* Type of child definition, usually right-most part of class name lowercase.
* Used occasionally in terms of context.
* @type string
*/
public $type;
/**
* Indicates whether or not an empty array of children is okay.
*
* This is necessary for redundant checking when changes affecting
* a child node may cause a parent node to now be disallowed.
* @type bool
*/
public $allow_empty;
/**
* Lookup array of all elements that this definition could possibly allow.
* @type array
*/
public $elements = array();
/**
* Get lookup of tag names that should not close this element automatically.
* All other elements will do so.
* @param HTMLPurifier_Config $config HTMLPurifier_Config object
* @return array
*/
public function getAllowedElements($config)
{
return $this->elements;
}
/**
* Validates nodes according to definition and returns modification.
*
* @param HTMLPurifier_Node[] $children Array of HTMLPurifier_Node
* @param HTMLPurifier_Config $config HTMLPurifier_Config object
* @param HTMLPurifier_Context $context HTMLPurifier_Context object
* @return bool|array true to leave nodes as is, false to remove parent node, array of replacement children
*/
abstract public function validateChildren($children, $config, $context);
}
/**
* Configuration object that triggers customizable behavior.
*
* @warning This class is strongly defined: that means that the class
* will fail if an undefined directive is retrieved or set.
*
* @note Many classes that could (although many times don't) use the
* configuration object make it a mandatory parameter. This is
* because a configuration object should always be forwarded,
* otherwise, you run the risk of missing a parameter and then
* being stumped when a configuration directive doesn't work.
*
* @todo Reconsider some of the public member variables
*/
class HTMLPurifier_Config
{
/**
* HTML Purifier's version
* @type string
*/
public $version = '4.6.0';
/**
* Whether or not to automatically finalize
* the object if a read operation is done.
* @type bool
*/
public $autoFinalize = true;
// protected member variables
/**
* Namespace indexed array of serials for specific namespaces.
* @see getSerial() for more info.
* @type string[]
*/
protected $serials = array();
/**
* Serial for entire configuration object.
* @type string
*/
protected $serial;
/**
* Parser for variables.
* @type HTMLPurifier_VarParser_Flexible
*/
protected $parser = null;
/**
* Reference HTMLPurifier_ConfigSchema for value checking.
* @type HTMLPurifier_ConfigSchema
* @note This is public for introspective purposes. Please don't
* abuse!
*/
public $def;
/**
* Indexed array of definitions.
* @type HTMLPurifier_Definition[]
*/
protected $definitions;
/**
* Whether or not config is finalized.
* @type bool
*/
protected $finalized = false;
/**
* Property list containing configuration directives.
* @type array
*/
protected $plist;
/**
* Whether or not a set is taking place due to an alias lookup.
* @type bool
*/
private $aliasMode;
/**
* Set to false if you do not want line and file numbers in errors.
* (useful when unit testing). This will also compress some errors
* and exceptions.
* @type bool
*/
public $chatty = true;
/**
* Current lock; only gets to this namespace are allowed.
* @type string
*/
private $lock;
/**
* Constructor
* @param HTMLPurifier_ConfigSchema $definition ConfigSchema that defines
* what directives are allowed.
* @param HTMLPurifier_PropertyList $parent
*/
public function __construct($definition, $parent = null)
{
$parent = $parent ? $parent : $definition->defaultPlist;
$this->plist = new HTMLPurifier_PropertyList($parent);
$this->def = $definition; // keep a copy around for checking
$this->parser = new HTMLPurifier_VarParser_Flexible();
}
/**
* Convenience constructor that creates a config object based on a mixed var
* @param mixed $config Variable that defines the state of the config
* object. Can be: a HTMLPurifier_Config() object,
* an array of directives based on loadArray(),
* or a string filename of an ini file.
* @param HTMLPurifier_ConfigSchema $schema Schema object
* @return HTMLPurifier_Config Configured object
*/
public static function create($config, $schema = null)
{
if ($config instanceof HTMLPurifier_Config) {
// pass-through
return $config;
}
if (!$schema) {
$ret = HTMLPurifier_Config::createDefault();
} else {
$ret = new HTMLPurifier_Config($schema);
}
if (is_string($config)) {
$ret->loadIni($config);
} elseif (is_array($config)) $ret->loadArray($config);
return $ret;
}
/**
* Creates a new config object that inherits from a previous one.
* @param HTMLPurifier_Config $config Configuration object to inherit from.
* @return HTMLPurifier_Config object with $config as its parent.
*/
public static function inherit(HTMLPurifier_Config $config)
{
return new HTMLPurifier_Config($config->def, $config->plist);
}
/**
* Convenience constructor that creates a default configuration object.
* @return HTMLPurifier_Config default object.
*/
public static function createDefault()
{
$definition = HTMLPurifier_ConfigSchema::instance();
$config = new HTMLPurifier_Config($definition);
return $config;
}
/**
* Retrieves a value from the configuration.
*
* @param string $key String key
* @param mixed $a
*
* @return mixed
*/
public function get($key, $a = null)
{
if ($a !== null) {
$this->triggerError(
"Using deprecated API: use \$config->get('$key.$a') instead",
E_USER_WARNING
);
$key = "$key.$a";
}
if (!$this->finalized) {
$this->autoFinalize();
}
if (!isset($this->def->info[$key])) {
// can't add % due to SimpleTest bug
$this->triggerError(
'Cannot retrieve value of undefined directive ' . htmlspecialchars($key),
E_USER_WARNING
);
return;
}
if (isset($this->def->info[$key]->isAlias)) {
$d = $this->def->info[$key];
$this->triggerError(
'Cannot get value from aliased directive, use real name ' . $d->key,
E_USER_ERROR
);
return;
}
if ($this->lock) {
list($ns) = explode('.', $key);
if ($ns !== $this->lock) {
$this->triggerError(
'Cannot get value of namespace ' . $ns . ' when lock for ' .
$this->lock .
' is active, this probably indicates a Definition setup method ' .
'is accessing directives that are not within its namespace',
E_USER_ERROR
);
return;
}
}
return $this->plist->get($key);
}
/**
* Retrieves an array of directives to values from a given namespace
*
* @param string $namespace String namespace
*
* @return array
*/
public function getBatch($namespace)
{
if (!$this->finalized) {
$this->autoFinalize();
}
$full = $this->getAll();
if (!isset($full[$namespace])) {
$this->triggerError(
'Cannot retrieve undefined namespace ' .
htmlspecialchars($namespace),
E_USER_WARNING
);
return;
}
return $full[$namespace];
}
/**
* Returns a SHA-1 signature of a segment of the configuration object
* that uniquely identifies that particular configuration
*
* @param string $namespace Namespace to get serial for
*
* @return string
* @note Revision is handled specially and is removed from the batch
* before processing!
*/
public function getBatchSerial($namespace)
{
if (empty($this->serials[$namespace])) {
$batch = $this->getBatch($namespace);
unset($batch['DefinitionRev']);
$this->serials[$namespace] = sha1(serialize($batch));
}
return $this->serials[$namespace];
}
/**
* Returns a SHA-1 signature for the entire configuration object
* that uniquely identifies that particular configuration
*
* @return string
*/
public function getSerial()
{
if (empty($this->serial)) {
$this->serial = sha1(serialize($this->getAll()));
}
return $this->serial;
}
/**
* Retrieves all directives, organized by namespace
*
* @warning This is a pretty inefficient function, avoid if you can
*/
public function getAll()
{
if (!$this->finalized) {
$this->autoFinalize();
}
$ret = array();
foreach ($this->plist->squash() as $name => $value) {
list($ns, $key) = explode('.', $name, 2);
$ret[$ns][$key] = $value;
}
return $ret;
}
/**
* Sets a value to configuration.
*
* @param string $key key
* @param mixed $value value
* @param mixed $a
*/
public function set($key, $value, $a = null)
{
if (strpos($key, '.') === false) {
$namespace = $key;
$directive = $value;
$value = $a;
$key = "$key.$directive";
$this->triggerError("Using deprecated API: use \$config->set('$key', ...) instead", E_USER_NOTICE);
} else {
list($namespace) = explode('.', $key);
}
if ($this->isFinalized('Cannot set directive after finalization')) {
return;
}
if (!isset($this->def->info[$key])) {
$this->triggerError(
'Cannot set undefined directive ' . htmlspecialchars($key) . ' to value',
E_USER_WARNING
);
return;
}
$def = $this->def->info[$key];
if (isset($def->isAlias)) {
if ($this->aliasMode) {
$this->triggerError(
'Double-aliases not allowed, please fix '.
'ConfigSchema bug with' . $key,
E_USER_ERROR
);
return;
}
$this->aliasMode = true;
$this->set($def->key, $value);
$this->aliasMode = false;
$this->triggerError("$key is an alias, preferred directive name is {$def->key}", E_USER_NOTICE);
return;
}
// Raw type might be negative when using the fully optimized form
// of stdclass, which indicates allow_null == true
$rtype = is_int($def) ? $def : $def->type;
if ($rtype < 0) {
$type = -$rtype;
$allow_null = true;
} else {
$type = $rtype;
$allow_null = isset($def->allow_null);
}
try {
$value = $this->parser->parse($value, $type, $allow_null);
} catch (HTMLPurifier_VarParserException $e) {
$this->triggerError(
'Value for ' . $key . ' is of invalid type, should be ' .
HTMLPurifier_VarParser::getTypeName($type),
E_USER_WARNING
);
return;
}
if (is_string($value) && is_object($def)) {
// resolve value alias if defined
if (isset($def->aliases[$value])) {
$value = $def->aliases[$value];
}
// check to see if the value is allowed
if (isset($def->allowed) && !isset($def->allowed[$value])) {
$this->triggerError(
'Value not supported, valid values are: ' .
$this->_listify($def->allowed),
E_USER_WARNING
);
return;
}
}
$this->plist->set($key, $value);
// reset definitions if the directives they depend on changed
// this is a very costly process, so it's discouraged
// with finalization
if ($namespace == 'HTML' || $namespace == 'CSS' || $namespace == 'URI') {
$this->definitions[$namespace] = null;
}
$this->serials[$namespace] = false;
}
/**
* Convenience function for error reporting
*
* @param array $lookup
*
* @return string
*/
private function _listify($lookup)
{
$list = array();
foreach ($lookup as $name => $b) {
$list[] = $name;
}
return implode(', ', $list);
}
/**
* Retrieves object reference to the HTML definition.
*
* @param bool $raw Return a copy that has not been setup yet. Must be
* called before it's been setup, otherwise won't work.
* @param bool $optimized If true, this method may return null, to
* indicate that a cached version of the modified
* definition object is available and no further edits
* are necessary. Consider using
* maybeGetRawHTMLDefinition, which is more explicitly
* named, instead.
*
* @return HTMLPurifier_HTMLDefinition
*/
public function getHTMLDefinition($raw = false, $optimized = false)
{
return $this->getDefinition('HTML', $raw, $optimized);
}
/**
* Retrieves object reference to the CSS definition
*
* @param bool $raw Return a copy that has not been setup yet. Must be
* called before it's been setup, otherwise won't work.
* @param bool $optimized If true, this method may return null, to
* indicate that a cached version of the modified
* definition object is available and no further edits
* are necessary. Consider using
* maybeGetRawCSSDefinition, which is more explicitly
* named, instead.
*
* @return HTMLPurifier_CSSDefinition
*/
public function getCSSDefinition($raw = false, $optimized = false)
{
return $this->getDefinition('CSS', $raw, $optimized);
}
/**
* Retrieves object reference to the URI definition
*
* @param bool $raw Return a copy that has not been setup yet. Must be
* called before it's been setup, otherwise won't work.
* @param bool $optimized If true, this method may return null, to
* indicate that a cached version of the modified
* definition object is available and no further edits
* are necessary. Consider using
* maybeGetRawURIDefinition, which is more explicitly
* named, instead.
*
* @return HTMLPurifier_URIDefinition
*/
public function getURIDefinition($raw = false, $optimized = false)
{
return $this->getDefinition('URI', $raw, $optimized);
}
/**
* Retrieves a definition
*
* @param string $type Type of definition: HTML, CSS, etc
* @param bool $raw Whether or not definition should be returned raw
* @param bool $optimized Only has an effect when $raw is true. Whether
* or not to return null if the result is already present in
* the cache. This is off by default for backwards
* compatibility reasons, but you need to do things this
* way in order to ensure that caching is done properly.
* Check out enduser-customize.html for more details.
* We probably won't ever change this default, as much as the
* maybe semantics is the "right thing to do."
*
* @throws HTMLPurifier_Exception
* @return HTMLPurifier_Definition
*/
public function getDefinition($type, $raw = false, $optimized = false)
{
if ($optimized && !$raw) {
throw new HTMLPurifier_Exception("Cannot set optimized = true when raw = false");
}
if (!$this->finalized) {
$this->autoFinalize();
}
// temporarily suspend locks, so we can handle recursive definition calls
$lock = $this->lock;
$this->lock = null;
$factory = HTMLPurifier_DefinitionCacheFactory::instance();
$cache = $factory->create($type, $this);
$this->lock = $lock;
if (!$raw) {
// full definition
// ---------------
// check if definition is in memory
if (!empty($this->definitions[$type])) {
$def = $this->definitions[$type];
// check if the definition is setup
if ($def->setup) {
return $def;
} else {
$def->setup($this);
if ($def->optimized) {
$cache->add($def, $this);
}
return $def;
}
}
// check if definition is in cache
$def = $cache->get($this);
if ($def) {
// definition in cache, save to memory and return it
$this->definitions[$type] = $def;
return $def;
}
// initialize it
$def = $this->initDefinition($type);
// set it up
$this->lock = $type;
$def->setup($this);
$this->lock = null;
// save in cache
$cache->add($def, $this);
// return it
return $def;
} else {
// raw definition
// --------------
// check preconditions
$def = null;
if ($optimized) {
if (is_null($this->get($type . '.DefinitionID'))) {
// fatally error out if definition ID not set
throw new HTMLPurifier_Exception(
"Cannot retrieve raw version without specifying %$type.DefinitionID"
);
}
}
if (!empty($this->definitions[$type])) {
$def = $this->definitions[$type];
if ($def->setup && !$optimized) {
$extra = $this->chatty ?
" (try moving this code block earlier in your initialization)" :
"";
throw new HTMLPurifier_Exception(
"Cannot retrieve raw definition after it has already been setup" .
$extra
);
}
if ($def->optimized === null) {
$extra = $this->chatty ? " (try flushing your cache)" : "";
throw new HTMLPurifier_Exception(
"Optimization status of definition is unknown" . $extra
);
}
if ($def->optimized !== $optimized) {
$msg = $optimized ? "optimized" : "unoptimized";
$extra = $this->chatty ?
" (this backtrace is for the first inconsistent call, which was for a $msg raw definition)"
: "";
throw new HTMLPurifier_Exception(
"Inconsistent use of optimized and unoptimized raw definition retrievals" . $extra
);
}
}
// check if definition was in memory
if ($def) {
if ($def->setup) {
// invariant: $optimized === true (checked above)
return null;
} else {
return $def;
}
}
// if optimized, check if definition was in cache
// (because we do the memory check first, this formulation
// is prone to cache slamming, but I think
// guaranteeing that either /all/ of the raw
// setup code or /none/ of it is run is more important.)
if ($optimized) {
// This code path only gets run once; once we put
// something in $definitions (which is guaranteed by the
// trailing code), we always short-circuit above.
$def = $cache->get($this);
if ($def) {
// save the full definition for later, but don't
// return it yet
$this->definitions[$type] = $def;
return null;
}
}
// check invariants for creation
if (!$optimized) {
if (!is_null($this->get($type . '.DefinitionID'))) {
if ($this->chatty) {
$this->triggerError(
'Due to a documentation error in previous version of HTML Purifier, your ' .
'definitions are not being cached. If this is OK, you can remove the ' .
'%$type.DefinitionRev and %$type.DefinitionID declaration. Otherwise, ' .
'modify your code to use maybeGetRawDefinition, and test if the returned ' .
'value is null before making any edits (if it is null, that means that a ' .
'cached version is available, and no raw operations are necessary). See ' .
'' .
'Customize for more details',
E_USER_WARNING
);
} else {
$this->triggerError(
"Useless DefinitionID declaration",
E_USER_WARNING
);
}
}
}
// initialize it
$def = $this->initDefinition($type);
$def->optimized = $optimized;
return $def;
}
throw new HTMLPurifier_Exception("The impossible happened!");
}
/**
* Initialise definition
*
* @param string $type What type of definition to create
*
* @return HTMLPurifier_CSSDefinition|HTMLPurifier_HTMLDefinition|HTMLPurifier_URIDefinition
* @throws HTMLPurifier_Exception
*/
private function initDefinition($type)
{
// quick checks failed, let's create the object
if ($type == 'HTML') {
$def = new HTMLPurifier_HTMLDefinition();
} elseif ($type == 'CSS') {
$def = new HTMLPurifier_CSSDefinition();
} elseif ($type == 'URI') {
$def = new HTMLPurifier_URIDefinition();
} else {
throw new HTMLPurifier_Exception(
"Definition of $type type not supported"
);
}
$this->definitions[$type] = $def;
return $def;
}
public function maybeGetRawDefinition($name)
{
return $this->getDefinition($name, true, true);
}
public function maybeGetRawHTMLDefinition()
{
return $this->getDefinition('HTML', true, true);
}
public function maybeGetRawCSSDefinition()
{
return $this->getDefinition('CSS', true, true);
}
public function maybeGetRawURIDefinition()
{
return $this->getDefinition('URI', true, true);
}
/**
* Loads configuration values from an array with the following structure:
* Namespace.Directive => Value
*
* @param array $config_array Configuration associative array
*/
public function loadArray($config_array)
{
if ($this->isFinalized('Cannot load directives after finalization')) {
return;
}
foreach ($config_array as $key => $value) {
$key = str_replace('_', '.', $key);
if (strpos($key, '.') !== false) {
$this->set($key, $value);
} else {
$namespace = $key;
$namespace_values = $value;
foreach ($namespace_values as $directive => $value2) {
$this->set($namespace .'.'. $directive, $value2);
}
}
}
}
/**
* Returns a list of array(namespace, directive) for all directives
* that are allowed in a web-form context as per an allowed
* namespaces/directives list.
*
* @param array $allowed List of allowed namespaces/directives
* @param HTMLPurifier_ConfigSchema $schema Schema to use, if not global copy
*
* @return array
*/
public static function getAllowedDirectivesForForm($allowed, $schema = null)
{
if (!$schema) {
$schema = HTMLPurifier_ConfigSchema::instance();
}
if ($allowed !== true) {
if (is_string($allowed)) {
$allowed = array($allowed);
}
$allowed_ns = array();
$allowed_directives = array();
$blacklisted_directives = array();
foreach ($allowed as $ns_or_directive) {
if (strpos($ns_or_directive, '.') !== false) {
// directive
if ($ns_or_directive[0] == '-') {
$blacklisted_directives[substr($ns_or_directive, 1)] = true;
} else {
$allowed_directives[$ns_or_directive] = true;
}
} else {
// namespace
$allowed_ns[$ns_or_directive] = true;
}
}
}
$ret = array();
foreach ($schema->info as $key => $def) {
list($ns, $directive) = explode('.', $key, 2);
if ($allowed !== true) {
if (isset($blacklisted_directives["$ns.$directive"])) {
continue;
}
if (!isset($allowed_directives["$ns.$directive"]) && !isset($allowed_ns[$ns])) {
continue;
}
}
if (isset($def->isAlias)) {
continue;
}
if ($directive == 'DefinitionID' || $directive == 'DefinitionRev') {
continue;
}
$ret[] = array($ns, $directive);
}
return $ret;
}
/**
* Loads configuration values from $_GET/$_POST that were posted
* via ConfigForm
*
* @param array $array $_GET or $_POST array to import
* @param string|bool $index Index/name that the config variables are in
* @param array|bool $allowed List of allowed namespaces/directives
* @param bool $mq_fix Boolean whether or not to enable magic quotes fix
* @param HTMLPurifier_ConfigSchema $schema Schema to use, if not global copy
*
* @return mixed
*/
public static function loadArrayFromForm($array, $index = false, $allowed = true, $mq_fix = true, $schema = null)
{
$ret = HTMLPurifier_Config::prepareArrayFromForm($array, $index, $allowed, $mq_fix, $schema);
$config = HTMLPurifier_Config::create($ret, $schema);
return $config;
}
/**
* Merges in configuration values from $_GET/$_POST to object. NOT STATIC.
*
* @param array $array $_GET or $_POST array to import
* @param string|bool $index Index/name that the config variables are in
* @param array|bool $allowed List of allowed namespaces/directives
* @param bool $mq_fix Boolean whether or not to enable magic quotes fix
*/
public function mergeArrayFromForm($array, $index = false, $allowed = true, $mq_fix = true)
{
$ret = HTMLPurifier_Config::prepareArrayFromForm($array, $index, $allowed, $mq_fix, $this->def);
$this->loadArray($ret);
}
/**
* Prepares an array from a form into something usable for the more
* strict parts of HTMLPurifier_Config
*
* @param array $array $_GET or $_POST array to import
* @param string|bool $index Index/name that the config variables are in
* @param array|bool $allowed List of allowed namespaces/directives
* @param bool $mq_fix Boolean whether or not to enable magic quotes fix
* @param HTMLPurifier_ConfigSchema $schema Schema to use, if not global copy
*
* @return array
*/
public static function prepareArrayFromForm($array, $index = false, $allowed = true, $mq_fix = true, $schema = null)
{
if ($index !== false) {
$array = (isset($array[$index]) && is_array($array[$index])) ? $array[$index] : array();
}
$mq = $mq_fix && function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc();
$allowed = HTMLPurifier_Config::getAllowedDirectivesForForm($allowed, $schema);
$ret = array();
foreach ($allowed as $key) {
list($ns, $directive) = $key;
$skey = "$ns.$directive";
if (!empty($array["Null_$skey"])) {
$ret[$ns][$directive] = null;
continue;
}
if (!isset($array[$skey])) {
continue;
}
$value = $mq ? stripslashes($array[$skey]) : $array[$skey];
$ret[$ns][$directive] = $value;
}
return $ret;
}
/**
* Loads configuration values from an ini file
*
* @param string $filename Name of ini file
*/
public function loadIni($filename)
{
if ($this->isFinalized('Cannot load directives after finalization')) {
return;
}
$array = parse_ini_file($filename, true);
$this->loadArray($array);
}
/**
* Checks whether or not the configuration object is finalized.
*
* @param string|bool $error String error message, or false for no error
*
* @return bool
*/
public function isFinalized($error = false)
{
if ($this->finalized && $error) {
$this->triggerError($error, E_USER_ERROR);
}
return $this->finalized;
}
/**
* Finalizes configuration only if auto finalize is on and not
* already finalized
*/
public function autoFinalize()
{
if ($this->autoFinalize) {
$this->finalize();
} else {
$this->plist->squash(true);
}
}
/**
* Finalizes a configuration object, prohibiting further change
*/
public function finalize()
{
$this->finalized = true;
$this->parser = null;
}
/**
* Produces a nicely formatted error message by supplying the
* stack frame information OUTSIDE of HTMLPurifier_Config.
*
* @param string $msg An error message
* @param int $no An error number
*/
protected function triggerError($msg, $no)
{
// determine previous stack frame
$extra = '';
if ($this->chatty) {
$trace = debug_backtrace();
// zip(tail(trace), trace) -- but PHP is not Haskell har har
for ($i = 0, $c = count($trace); $i < $c - 1; $i++) {
// XXX this is not correct on some versions of HTML Purifier
if ($trace[$i + 1]['class'] === 'HTMLPurifier_Config') {
continue;
}
$frame = $trace[$i];
$extra = " invoked on line {$frame['line']} in file {$frame['file']}";
break;
}
}
trigger_error($msg . $extra, $no);
}
/**
* Returns a serialized form of the configuration object that can
* be reconstituted.
*
* @return string
*/
public function serialize()
{
$this->getDefinition('HTML');
$this->getDefinition('CSS');
$this->getDefinition('URI');
return serialize($this);
}
}
/**
* Configuration definition, defines directives and their defaults.
*/
class HTMLPurifier_ConfigSchema
{
/**
* Defaults of the directives and namespaces.
* @type array
* @note This shares the exact same structure as HTMLPurifier_Config::$conf
*/
public $defaults = array();
/**
* The default property list. Do not edit this property list.
* @type array
*/
public $defaultPlist;
/**
* Definition of the directives.
* The structure of this is:
*
* array(
* 'Namespace' => array(
* 'Directive' => new stdclass(),
* )
* )
*
* The stdclass may have the following properties:
*
* - If isAlias isn't set:
* - type: Integer type of directive, see HTMLPurifier_VarParser for definitions
* - allow_null: If set, this directive allows null values
* - aliases: If set, an associative array of value aliases to real values
* - allowed: If set, a lookup array of allowed (string) values
* - If isAlias is set:
* - namespace: Namespace this directive aliases to
* - name: Directive name this directive aliases to
*
* In certain degenerate cases, stdclass will actually be an integer. In
* that case, the value is equivalent to an stdclass with the type
* property set to the integer. If the integer is negative, type is
* equal to the absolute value of integer, and allow_null is true.
*
* This class is friendly with HTMLPurifier_Config. If you need introspection
* about the schema, you're better of using the ConfigSchema_Interchange,
* which uses more memory but has much richer information.
* @type array
*/
public $info = array();
/**
* Application-wide singleton
* @type HTMLPurifier_ConfigSchema
*/
protected static $singleton;
public function __construct()
{
$this->defaultPlist = new HTMLPurifier_PropertyList();
}
/**
* Unserializes the default ConfigSchema.
* @return HTMLPurifier_ConfigSchema
*/
public static function makeFromSerial()
{
$contents = file_get_contents(HTMLPURIFIER_PREFIX . '/HTMLPurifier/ConfigSchema/schema.ser');
$r = unserialize($contents);
if (!$r) {
$hash = sha1($contents);
trigger_error("Unserialization of configuration schema failed, sha1 of file was $hash", E_USER_ERROR);
}
return $r;
}
/**
* Retrieves an instance of the application-wide configuration definition.
* @param HTMLPurifier_ConfigSchema $prototype
* @return HTMLPurifier_ConfigSchema
*/
public static function instance($prototype = null)
{
if ($prototype !== null) {
HTMLPurifier_ConfigSchema::$singleton = $prototype;
} elseif (HTMLPurifier_ConfigSchema::$singleton === null || $prototype === true) {
HTMLPurifier_ConfigSchema::$singleton = HTMLPurifier_ConfigSchema::makeFromSerial();
}
return HTMLPurifier_ConfigSchema::$singleton;
}
/**
* Defines a directive for configuration
* @warning Will fail of directive's namespace is defined.
* @warning This method's signature is slightly different from the legacy
* define() static method! Beware!
* @param string $key Name of directive
* @param mixed $default Default value of directive
* @param string $type Allowed type of the directive. See
* HTMLPurifier_DirectiveDef::$type for allowed values
* @param bool $allow_null Whether or not to allow null values
*/
public function add($key, $default, $type, $allow_null)
{
$obj = new stdclass();
$obj->type = is_int($type) ? $type : HTMLPurifier_VarParser::$types[$type];
if ($allow_null) {
$obj->allow_null = true;
}
$this->info[$key] = $obj;
$this->defaults[$key] = $default;
$this->defaultPlist->set($key, $default);
}
/**
* Defines a directive value alias.
*
* Directive value aliases are convenient for developers because it lets
* them set a directive to several values and get the same result.
* @param string $key Name of Directive
* @param array $aliases Hash of aliased values to the real alias
*/
public function addValueAliases($key, $aliases)
{
if (!isset($this->info[$key]->aliases)) {
$this->info[$key]->aliases = array();
}
foreach ($aliases as $alias => $real) {
$this->info[$key]->aliases[$alias] = $real;
}
}
/**
* Defines a set of allowed values for a directive.
* @warning This is slightly different from the corresponding static
* method definition.
* @param string $key Name of directive
* @param array $allowed Lookup array of allowed values
*/
public function addAllowedValues($key, $allowed)
{
$this->info[$key]->allowed = $allowed;
}
/**
* Defines a directive alias for backwards compatibility
* @param string $key Directive that will be aliased
* @param string $new_key Directive that the alias will be to
*/
public function addAlias($key, $new_key)
{
$obj = new stdclass;
$obj->key = $new_key;
$obj->isAlias = true;
$this->info[$key] = $obj;
}
/**
* Replaces any stdclass that only has the type property with type integer.
*/
public function postProcess()
{
foreach ($this->info as $key => $v) {
if (count((array) $v) == 1) {
$this->info[$key] = $v->type;
} elseif (count((array) $v) == 2 && isset($v->allow_null)) {
$this->info[$key] = -$v->type;
}
}
}
}
/**
* @todo Unit test
*/
class HTMLPurifier_ContentSets
{
/**
* List of content set strings (pipe separators) indexed by name.
* @type array
*/
public $info = array();
/**
* List of content set lookups (element => true) indexed by name.
* @type array
* @note This is in HTMLPurifier_HTMLDefinition->info_content_sets
*/
public $lookup = array();
/**
* Synchronized list of defined content sets (keys of info).
* @type array
*/
protected $keys = array();
/**
* Synchronized list of defined content values (values of info).
* @type array
*/
protected $values = array();
/**
* Merges in module's content sets, expands identifiers in the content
* sets and populates the keys, values and lookup member variables.
* @param HTMLPurifier_HTMLModule[] $modules List of HTMLPurifier_HTMLModule
*/
public function __construct($modules)
{
if (!is_array($modules)) {
$modules = array($modules);
}
// populate content_sets based on module hints
// sorry, no way of overloading
foreach ($modules as $module) {
foreach ($module->content_sets as $key => $value) {
$temp = $this->convertToLookup($value);
if (isset($this->lookup[$key])) {
// add it into the existing content set
$this->lookup[$key] = array_merge($this->lookup[$key], $temp);
} else {
$this->lookup[$key] = $temp;
}
}
}
$old_lookup = false;
while ($old_lookup !== $this->lookup) {
$old_lookup = $this->lookup;
foreach ($this->lookup as $i => $set) {
$add = array();
foreach ($set as $element => $x) {
if (isset($this->lookup[$element])) {
$add += $this->lookup[$element];
unset($this->lookup[$i][$element]);
}
}
$this->lookup[$i] += $add;
}
}
foreach ($this->lookup as $key => $lookup) {
$this->info[$key] = implode(' | ', array_keys($lookup));
}
$this->keys = array_keys($this->info);
$this->values = array_values($this->info);
}
/**
* Accepts a definition; generates and assigns a ChildDef for it
* @param HTMLPurifier_ElementDef $def HTMLPurifier_ElementDef reference
* @param HTMLPurifier_HTMLModule $module Module that defined the ElementDef
*/
public function generateChildDef(&$def, $module)
{
if (!empty($def->child)) { // already done!
return;
}
$content_model = $def->content_model;
if (is_string($content_model)) {
// Assume that $this->keys is alphanumeric
$def->content_model = preg_replace_callback(
'/\b(' . implode('|', $this->keys) . ')\b/',
array($this, 'generateChildDefCallback'),
$content_model
);
//$def->content_model = str_replace(
// $this->keys, $this->values, $content_model);
}
$def->child = $this->getChildDef($def, $module);
}
public function generateChildDefCallback($matches)
{
return $this->info[$matches[0]];
}
/**
* Instantiates a ChildDef based on content_model and content_model_type
* member variables in HTMLPurifier_ElementDef
* @note This will also defer to modules for custom HTMLPurifier_ChildDef
* subclasses that need content set expansion
* @param HTMLPurifier_ElementDef $def HTMLPurifier_ElementDef to have ChildDef extracted
* @param HTMLPurifier_HTMLModule $module Module that defined the ElementDef
* @return HTMLPurifier_ChildDef corresponding to ElementDef
*/
public function getChildDef($def, $module)
{
$value = $def->content_model;
if (is_object($value)) {
trigger_error(
'Literal object child definitions should be stored in '.
'ElementDef->child not ElementDef->content_model',
E_USER_NOTICE
);
return $value;
}
switch ($def->content_model_type) {
case 'required':
return new HTMLPurifier_ChildDef_Required($value);
case 'optional':
return new HTMLPurifier_ChildDef_Optional($value);
case 'empty':
return new HTMLPurifier_ChildDef_Empty();
case 'custom':
return new HTMLPurifier_ChildDef_Custom($value);
}
// defer to its module
$return = false;
if ($module->defines_child_def) { // save a func call
$return = $module->getChildDef($def);
}
if ($return !== false) {
return $return;
}
// error-out
trigger_error(
'Could not determine which ChildDef class to instantiate',
E_USER_ERROR
);
return false;
}
/**
* Converts a string list of elements separated by pipes into
* a lookup array.
* @param string $string List of elements
* @return array Lookup array of elements
*/
protected function convertToLookup($string)
{
$array = explode('|', str_replace(' ', '', $string));
$ret = array();
foreach ($array as $k) {
$ret[$k] = true;
}
return $ret;
}
}
/**
* Registry object that contains information about the current context.
* @warning Is a bit buggy when variables are set to null: it thinks
* they don't exist! So use false instead, please.
* @note Since the variables Context deals with may not be objects,
* references are very important here! Do not remove!
*/
class HTMLPurifier_Context
{
/**
* Private array that stores the references.
* @type array
*/
private $_storage = array();
/**
* Registers a variable into the context.
* @param string $name String name
* @param mixed $ref Reference to variable to be registered
*/
public function register($name, &$ref)
{
if (array_key_exists($name, $this->_storage)) {
trigger_error(
"Name $name produces collision, cannot re-register",
E_USER_ERROR
);
return;
}
$this->_storage[$name] =& $ref;
}
/**
* Retrieves a variable reference from the context.
* @param string $name String name
* @param bool $ignore_error Boolean whether or not to ignore error
* @return mixed
*/
public function &get($name, $ignore_error = false)
{
if (!array_key_exists($name, $this->_storage)) {
if (!$ignore_error) {
trigger_error(
"Attempted to retrieve non-existent variable $name",
E_USER_ERROR
);
}
$var = null; // so we can return by reference
return $var;
}
return $this->_storage[$name];
}
/**
* Destroys a variable in the context.
* @param string $name String name
*/
public function destroy($name)
{
if (!array_key_exists($name, $this->_storage)) {
trigger_error(
"Attempted to destroy non-existent variable $name",
E_USER_ERROR
);
return;
}
unset($this->_storage[$name]);
}
/**
* Checks whether or not the variable exists.
* @param string $name String name
* @return bool
*/
public function exists($name)
{
return array_key_exists($name, $this->_storage);
}
/**
* Loads a series of variables from an associative array
* @param array $context_array Assoc array of variables to load
*/
public function loadArray($context_array)
{
foreach ($context_array as $key => $discard) {
$this->register($key, $context_array[$key]);
}
}
}
/**
* Abstract class representing Definition cache managers that implements
* useful common methods and is a factory.
* @todo Create a separate maintenance file advanced users can use to
* cache their custom HTMLDefinition, which can be loaded
* via a configuration directive
* @todo Implement memcached
*/
abstract class HTMLPurifier_DefinitionCache
{
/**
* @type string
*/
public $type;
/**
* @param string $type Type of definition objects this instance of the
* cache will handle.
*/
public function __construct($type)
{
$this->type = $type;
}
/**
* Generates a unique identifier for a particular configuration
* @param HTMLPurifier_Config $config Instance of HTMLPurifier_Config
* @return string
*/
public function generateKey($config)
{
return $config->version . ',' . // possibly replace with function calls
$config->getBatchSerial($this->type) . ',' .
$config->get($this->type . '.DefinitionRev');
}
/**
* Tests whether or not a key is old with respect to the configuration's
* version and revision number.
* @param string $key Key to test
* @param HTMLPurifier_Config $config Instance of HTMLPurifier_Config to test against
* @return bool
*/
public function isOld($key, $config)
{
if (substr_count($key, ',') < 2) {
return true;
}
list($version, $hash, $revision) = explode(',', $key, 3);
$compare = version_compare($version, $config->version);
// version mismatch, is always old
if ($compare != 0) {
return true;
}
// versions match, ids match, check revision number
if ($hash == $config->getBatchSerial($this->type) &&
$revision < $config->get($this->type . '.DefinitionRev')) {
return true;
}
return false;
}
/**
* Checks if a definition's type jives with the cache's type
* @note Throws an error on failure
* @param HTMLPurifier_Definition $def Definition object to check
* @return bool true if good, false if not
*/
public function checkDefType($def)
{
if ($def->type !== $this->type) {
trigger_error("Cannot use definition of type {$def->type} in cache for {$this->type}");
return false;
}
return true;
}
/**
* Adds a definition object to the cache
* @param HTMLPurifier_Definition $def
* @param HTMLPurifier_Config $config
*/
abstract public function add($def, $config);
/**
* Unconditionally saves a definition object to the cache
* @param HTMLPurifier_Definition $def
* @param HTMLPurifier_Config $config
*/
abstract public function set($def, $config);
/**
* Replace an object in the cache
* @param HTMLPurifier_Definition $def
* @param HTMLPurifier_Config $config
*/
abstract public function replace($def, $config);
/**
* Retrieves a definition object from the cache
* @param HTMLPurifier_Config $config
*/
abstract public function get($config);
/**
* Removes a definition object to the cache
* @param HTMLPurifier_Config $config
*/
abstract public function remove($config);
/**
* Clears all objects from cache
* @param HTMLPurifier_Config $config
*/
abstract public function flush($config);
/**
* Clears all expired (older version or revision) objects from cache
* @note Be carefuly implementing this method as flush. Flush must
* not interfere with other Definition types, and cleanup()
* should not be repeatedly called by userland code.
* @param HTMLPurifier_Config $config
*/
abstract public function cleanup($config);
}
/**
* Responsible for creating definition caches.
*/
class HTMLPurifier_DefinitionCacheFactory
{
/**
* @type array
*/
protected $caches = array('Serializer' => array());
/**
* @type array
*/
protected $implementations = array();
/**
* @type HTMLPurifier_DefinitionCache_Decorator[]
*/
protected $decorators = array();
/**
* Initialize default decorators
*/
public function setup()
{
$this->addDecorator('Cleanup');
}
/**
* Retrieves an instance of global definition cache factory.
* @param HTMLPurifier_DefinitionCacheFactory $prototype
* @return HTMLPurifier_DefinitionCacheFactory
*/
public static function instance($prototype = null)
{
static $instance;
if ($prototype !== null) {
$instance = $prototype;
} elseif ($instance === null || $prototype === true) {
$instance = new HTMLPurifier_DefinitionCacheFactory();
$instance->setup();
}
return $instance;
}
/**
* Registers a new definition cache object
* @param string $short Short name of cache object, for reference
* @param string $long Full class name of cache object, for construction
*/
public function register($short, $long)
{
$this->implementations[$short] = $long;
}
/**
* Factory method that creates a cache object based on configuration
* @param string $type Name of definitions handled by cache
* @param HTMLPurifier_Config $config Config instance
* @return mixed
*/
public function create($type, $config)
{
$method = $config->get('Cache.DefinitionImpl');
if ($method === null) {
return new HTMLPurifier_DefinitionCache_Null($type);
}
if (!empty($this->caches[$method][$type])) {
return $this->caches[$method][$type];
}
if (isset($this->implementations[$method]) &&
class_exists($class = $this->implementations[$method], false)) {
$cache = new $class($type);
} else {
if ($method != 'Serializer') {
trigger_error("Unrecognized DefinitionCache $method, using Serializer instead", E_USER_WARNING);
}
$cache = new HTMLPurifier_DefinitionCache_Serializer($type);
}
foreach ($this->decorators as $decorator) {
$new_cache = $decorator->decorate($cache);
// prevent infinite recursion in PHP 4
unset($cache);
$cache = $new_cache;
}
$this->caches[$method][$type] = $cache;
return $this->caches[$method][$type];
}
/**
* Registers a decorator to add to all new cache objects
* @param HTMLPurifier_DefinitionCache_Decorator|string $decorator An instance or the name of a decorator
*/
public function addDecorator($decorator)
{
if (is_string($decorator)) {
$class = "HTMLPurifier_DefinitionCache_Decorator_$decorator";
$decorator = new $class;
}
$this->decorators[$decorator->name] = $decorator;
}
}
/**
* Represents a document type, contains information on which modules
* need to be loaded.
* @note This class is inspected by Printer_HTMLDefinition->renderDoctype.
* If structure changes, please update that function.
*/
class HTMLPurifier_Doctype
{
/**
* Full name of doctype
* @type string
*/
public $name;
/**
* List of standard modules (string identifiers or literal objects)
* that this doctype uses
* @type array
*/
public $modules = array();
/**
* List of modules to use for tidying up code
* @type array
*/
public $tidyModules = array();
/**
* Is the language derived from XML (i.e. XHTML)?
* @type bool
*/
public $xml = true;
/**
* List of aliases for this doctype
* @type array
*/
public $aliases = array();
/**
* Public DTD identifier
* @type string
*/
public $dtdPublic;
/**
* System DTD identifier
* @type string
*/
public $dtdSystem;
public function __construct(
$name = null,
$xml = true,
$modules = array(),
$tidyModules = array(),
$aliases = array(),
$dtd_public = null,
$dtd_system = null
) {
$this->name = $name;
$this->xml = $xml;
$this->modules = $modules;
$this->tidyModules = $tidyModules;
$this->aliases = $aliases;
$this->dtdPublic = $dtd_public;
$this->dtdSystem = $dtd_system;
}
}
class HTMLPurifier_DoctypeRegistry
{
/**
* Hash of doctype names to doctype objects.
* @type array
*/
protected $doctypes;
/**
* Lookup table of aliases to real doctype names.
* @type array
*/
protected $aliases;
/**
* Registers a doctype to the registry
* @note Accepts a fully-formed doctype object, or the
* parameters for constructing a doctype object
* @param string $doctype Name of doctype or literal doctype object
* @param bool $xml
* @param array $modules Modules doctype will load
* @param array $tidy_modules Modules doctype will load for certain modes
* @param array $aliases Alias names for doctype
* @param string $dtd_public
* @param string $dtd_system
* @return HTMLPurifier_Doctype Editable registered doctype
*/
public function register(
$doctype,
$xml = true,
$modules = array(),
$tidy_modules = array(),
$aliases = array(),
$dtd_public = null,
$dtd_system = null
) {
if (!is_array($modules)) {
$modules = array($modules);
}
if (!is_array($tidy_modules)) {
$tidy_modules = array($tidy_modules);
}
if (!is_array($aliases)) {
$aliases = array($aliases);
}
if (!is_object($doctype)) {
$doctype = new HTMLPurifier_Doctype(
$doctype,
$xml,
$modules,
$tidy_modules,
$aliases,
$dtd_public,
$dtd_system
);
}
$this->doctypes[$doctype->name] = $doctype;
$name = $doctype->name;
// hookup aliases
foreach ($doctype->aliases as $alias) {
if (isset($this->doctypes[$alias])) {
continue;
}
$this->aliases[$alias] = $name;
}
// remove old aliases
if (isset($this->aliases[$name])) {
unset($this->aliases[$name]);
}
return $doctype;
}
/**
* Retrieves reference to a doctype of a certain name
* @note This function resolves aliases
* @note When possible, use the more fully-featured make()
* @param string $doctype Name of doctype
* @return HTMLPurifier_Doctype Editable doctype object
*/
public function get($doctype)
{
if (isset($this->aliases[$doctype])) {
$doctype = $this->aliases[$doctype];
}
if (!isset($this->doctypes[$doctype])) {
trigger_error('Doctype ' . htmlspecialchars($doctype) . ' does not exist', E_USER_ERROR);
$anon = new HTMLPurifier_Doctype($doctype);
return $anon;
}
return $this->doctypes[$doctype];
}
/**
* Creates a doctype based on a configuration object,
* will perform initialization on the doctype
* @note Use this function to get a copy of doctype that config
* can hold on to (this is necessary in order to tell
* Generator whether or not the current document is XML
* based or not).
* @param HTMLPurifier_Config $config
* @return HTMLPurifier_Doctype
*/
public function make($config)
{
return clone $this->get($this->getDoctypeFromConfig($config));
}
/**
* Retrieves the doctype from the configuration object
* @param HTMLPurifier_Config $config
* @return string
*/
public function getDoctypeFromConfig($config)
{
// recommended test
$doctype = $config->get('HTML.Doctype');
if (!empty($doctype)) {
return $doctype;
}
$doctype = $config->get('HTML.CustomDoctype');
if (!empty($doctype)) {
return $doctype;
}
// backwards-compatibility
if ($config->get('HTML.XHTML')) {
$doctype = 'XHTML 1.0';
} else {
$doctype = 'HTML 4.01';
}
if ($config->get('HTML.Strict')) {
$doctype .= ' Strict';
} else {
$doctype .= ' Transitional';
}
return $doctype;
}
}
/**
* Structure that stores an HTML element definition. Used by
* HTMLPurifier_HTMLDefinition and HTMLPurifier_HTMLModule.
* @note This class is inspected by HTMLPurifier_Printer_HTMLDefinition.
* Please update that class too.
* @warning If you add new properties to this class, you MUST update
* the mergeIn() method.
*/
class HTMLPurifier_ElementDef
{
/**
* Does the definition work by itself, or is it created solely
* for the purpose of merging into another definition?
* @type bool
*/
public $standalone = true;
/**
* Associative array of attribute name to HTMLPurifier_AttrDef.
* @type array
* @note Before being processed by HTMLPurifier_AttrCollections
* when modules are finalized during
* HTMLPurifier_HTMLDefinition->setup(), this array may also
* contain an array at index 0 that indicates which attribute
* collections to load into the full array. It may also
* contain string indentifiers in lieu of HTMLPurifier_AttrDef,
* see HTMLPurifier_AttrTypes on how they are expanded during
* HTMLPurifier_HTMLDefinition->setup() processing.
*/
public $attr = array();
// XXX: Design note: currently, it's not possible to override
// previously defined AttrTransforms without messing around with
// the final generated config. This is by design; a previous version
// used an associated list of attr_transform, but it was extremely
// easy to accidentally override other attribute transforms by
// forgetting to specify an index (and just using 0.) While we
// could check this by checking the index number and complaining,
// there is a second problem which is that it is not at all easy to
// tell when something is getting overridden. Combine this with a
// codebase where this isn't really being used, and it's perfect for
// nuking.
/**
* List of tags HTMLPurifier_AttrTransform to be done before validation.
* @type array
*/
public $attr_transform_pre = array();
/**
* List of tags HTMLPurifier_AttrTransform to be done after validation.
* @type array
*/
public $attr_transform_post = array();
/**
* HTMLPurifier_ChildDef of this tag.
* @type HTMLPurifier_ChildDef
*/
public $child;
/**
* Abstract string representation of internal ChildDef rules.
* @see HTMLPurifier_ContentSets for how this is parsed and then transformed
* into an HTMLPurifier_ChildDef.
* @warning This is a temporary variable that is not available after
* being processed by HTMLDefinition
* @type string
*/
public $content_model;
/**
* Value of $child->type, used to determine which ChildDef to use,
* used in combination with $content_model.
* @warning This must be lowercase
* @warning This is a temporary variable that is not available after
* being processed by HTMLDefinition
* @type string
*/
public $content_model_type;
/**
* Does the element have a content model (#PCDATA | Inline)*? This
* is important for chameleon ins and del processing in
* HTMLPurifier_ChildDef_Chameleon. Dynamically set: modules don't
* have to worry about this one.
* @type bool
*/
public $descendants_are_inline = false;
/**
* List of the names of required attributes this element has.
* Dynamically populated by HTMLPurifier_HTMLDefinition::getElement()
* @type array
*/
public $required_attr = array();
/**
* Lookup table of tags excluded from all descendants of this tag.
* @type array
* @note SGML permits exclusions for all descendants, but this is
* not possible with DTDs or XML Schemas. W3C has elected to
* use complicated compositions of content_models to simulate
* exclusion for children, but we go the simpler, SGML-style
* route of flat-out exclusions, which correctly apply to
* all descendants and not just children. Note that the XHTML
* Modularization Abstract Modules are blithely unaware of such
* distinctions.
*/
public $excludes = array();
/**
* This tag is explicitly auto-closed by the following tags.
* @type array
*/
public $autoclose = array();
/**
* If a foreign element is found in this element, test if it is
* allowed by this sub-element; if it is, instead of closing the
* current element, place it inside this element.
* @type string
*/
public $wrap;
/**
* Whether or not this is a formatting element affected by the
* "Active Formatting Elements" algorithm.
* @type bool
*/
public $formatting;
/**
* Low-level factory constructor for creating new standalone element defs
*/
public static function create($content_model, $content_model_type, $attr)
{
$def = new HTMLPurifier_ElementDef();
$def->content_model = $content_model;
$def->content_model_type = $content_model_type;
$def->attr = $attr;
return $def;
}
/**
* Merges the values of another element definition into this one.
* Values from the new element def take precedence if a value is
* not mergeable.
* @param HTMLPurifier_ElementDef $def
*/
public function mergeIn($def)
{
// later keys takes precedence
foreach ($def->attr as $k => $v) {
if ($k === 0) {
// merge in the includes
// sorry, no way to override an include
foreach ($v as $v2) {
$this->attr[0][] = $v2;
}
continue;
}
if ($v === false) {
if (isset($this->attr[$k])) {
unset($this->attr[$k]);
}
continue;
}
$this->attr[$k] = $v;
}
$this->_mergeAssocArray($this->excludes, $def->excludes);
$this->attr_transform_pre = array_merge($this->attr_transform_pre, $def->attr_transform_pre);
$this->attr_transform_post = array_merge($this->attr_transform_post, $def->attr_transform_post);
if (!empty($def->content_model)) {
$this->content_model =
str_replace("#SUPER", $this->content_model, $def->content_model);
$this->child = false;
}
if (!empty($def->content_model_type)) {
$this->content_model_type = $def->content_model_type;
$this->child = false;
}
if (!is_null($def->child)) {
$this->child = $def->child;
}
if (!is_null($def->formatting)) {
$this->formatting = $def->formatting;
}
if ($def->descendants_are_inline) {
$this->descendants_are_inline = $def->descendants_are_inline;
}
}
/**
* Merges one array into another, removes values which equal false
* @param $a1 Array by reference that is merged into
* @param $a2 Array that merges into $a1
*/
private function _mergeAssocArray(&$a1, $a2)
{
foreach ($a2 as $k => $v) {
if ($v === false) {
if (isset($a1[$k])) {
unset($a1[$k]);
}
continue;
}
$a1[$k] = $v;
}
}
}
/**
* A UTF-8 specific character encoder that handles cleaning and transforming.
* @note All functions in this class should be static.
*/
class HTMLPurifier_Encoder
{
/**
* Constructor throws fatal error if you attempt to instantiate class
*/
private function __construct()
{
trigger_error('Cannot instantiate encoder, call methods statically', E_USER_ERROR);
}
/**
* Error-handler that mutes errors, alternative to shut-up operator.
*/
public static function muteErrorHandler()
{
}
/**
* iconv wrapper which mutes errors, but doesn't work around bugs.
* @param string $in Input encoding
* @param string $out Output encoding
* @param string $text The text to convert
* @return string
*/
public static function unsafeIconv($in, $out, $text)
{
set_error_handler(array('HTMLPurifier_Encoder', 'muteErrorHandler'));
$r = iconv($in, $out, $text);
restore_error_handler();
return $r;
}
/**
* iconv wrapper which mutes errors and works around bugs.
* @param string $in Input encoding
* @param string $out Output encoding
* @param string $text The text to convert
* @param int $max_chunk_size
* @return string
*/
public static function iconv($in, $out, $text, $max_chunk_size = 8000)
{
$code = self::testIconvTruncateBug();
if ($code == self::ICONV_OK) {
return self::unsafeIconv($in, $out, $text);
} elseif ($code == self::ICONV_TRUNCATES) {
// we can only work around this if the input character set
// is utf-8
if ($in == 'utf-8') {
if ($max_chunk_size < 4) {
trigger_error('max_chunk_size is too small', E_USER_WARNING);
return false;
}
// split into 8000 byte chunks, but be careful to handle
// multibyte boundaries properly
if (($c = strlen($text)) <= $max_chunk_size) {
return self::unsafeIconv($in, $out, $text);
}
$r = '';
$i = 0;
while (true) {
if ($i + $max_chunk_size >= $c) {
$r .= self::unsafeIconv($in, $out, substr($text, $i));
break;
}
// wibble the boundary
if (0x80 != (0xC0 & ord($text[$i + $max_chunk_size]))) {
$chunk_size = $max_chunk_size;
} elseif (0x80 != (0xC0 & ord($text[$i + $max_chunk_size - 1]))) {
$chunk_size = $max_chunk_size - 1;
} elseif (0x80 != (0xC0 & ord($text[$i + $max_chunk_size - 2]))) {
$chunk_size = $max_chunk_size - 2;
} elseif (0x80 != (0xC0 & ord($text[$i + $max_chunk_size - 3]))) {
$chunk_size = $max_chunk_size - 3;
} else {
return false; // rather confusing UTF-8...
}
$chunk = substr($text, $i, $chunk_size); // substr doesn't mind overlong lengths
$r .= self::unsafeIconv($in, $out, $chunk);
$i += $chunk_size;
}
return $r;
} else {
return false;
}
} else {
return false;
}
}
/**
* Cleans a UTF-8 string for well-formedness and SGML validity
*
* It will parse according to UTF-8 and return a valid UTF8 string, with
* non-SGML codepoints excluded.
*
* @param string $str The string to clean
* @param bool $force_php
* @return string
*
* @note Just for reference, the non-SGML code points are 0 to 31 and
* 127 to 159, inclusive. However, we allow code points 9, 10
* and 13, which are the tab, line feed and carriage return
* respectively. 128 and above the code points map to multibyte
* UTF-8 representations.
*
* @note Fallback code adapted from utf8ToUnicode by Henri Sivonen and
* hsivonen@iki.fi at under the
* LGPL license. Notes on what changed are inside, but in general,
* the original code transformed UTF-8 text into an array of integer
* Unicode codepoints. Understandably, transforming that back to
* a string would be somewhat expensive, so the function was modded to
* directly operate on the string. However, this discourages code
* reuse, and the logic enumerated here would be useful for any
* function that needs to be able to understand UTF-8 characters.
* As of right now, only smart lossless character encoding converters
* would need that, and I'm probably not going to implement them.
* Once again, PHP 6 should solve all our problems.
*/
public static function cleanUTF8($str, $force_php = false)
{
// UTF-8 validity is checked since PHP 4.3.5
// This is an optimization: if the string is already valid UTF-8, no
// need to do PHP stuff. 99% of the time, this will be the case.
// The regexp matches the XML char production, as well as well as excluding
// non-SGML codepoints U+007F to U+009F
if (preg_match(
'/^[\x{9}\x{A}\x{D}\x{20}-\x{7E}\x{A0}-\x{D7FF}\x{E000}-\x{FFFD}\x{10000}-\x{10FFFF}]*$/Du',
$str
)) {
return $str;
}
$mState = 0; // cached expected number of octets after the current octet
// until the beginning of the next UTF8 character sequence
$mUcs4 = 0; // cached Unicode character
$mBytes = 1; // cached expected number of octets in the current sequence
// original code involved an $out that was an array of Unicode
// codepoints. Instead of having to convert back into UTF-8, we've
// decided to directly append valid UTF-8 characters onto a string
// $out once they're done. $char accumulates raw bytes, while $mUcs4
// turns into the Unicode code point, so there's some redundancy.
$out = '';
$char = '';
$len = strlen($str);
for ($i = 0; $i < $len; $i++) {
$in = ord($str{$i});
$char .= $str[$i]; // append byte to char
if (0 == $mState) {
// When mState is zero we expect either a US-ASCII character
// or a multi-octet sequence.
if (0 == (0x80 & ($in))) {
// US-ASCII, pass straight through.
if (($in <= 31 || $in == 127) &&
!($in == 9 || $in == 13 || $in == 10) // save \r\t\n
) {
// control characters, remove
} else {
$out .= $char;
}
// reset
$char = '';
$mBytes = 1;
} elseif (0xC0 == (0xE0 & ($in))) {
// First octet of 2 octet sequence
$mUcs4 = ($in);
$mUcs4 = ($mUcs4 & 0x1F) << 6;
$mState = 1;
$mBytes = 2;
} elseif (0xE0 == (0xF0 & ($in))) {
// First octet of 3 octet sequence
$mUcs4 = ($in);
$mUcs4 = ($mUcs4 & 0x0F) << 12;
$mState = 2;
$mBytes = 3;
} elseif (0xF0 == (0xF8 & ($in))) {
// First octet of 4 octet sequence
$mUcs4 = ($in);
$mUcs4 = ($mUcs4 & 0x07) << 18;
$mState = 3;
$mBytes = 4;
} elseif (0xF8 == (0xFC & ($in))) {
// First octet of 5 octet sequence.
//
// This is illegal because the encoded codepoint must be
// either:
// (a) not the shortest form or
// (b) outside the Unicode range of 0-0x10FFFF.
// Rather than trying to resynchronize, we will carry on
// until the end of the sequence and let the later error
// handling code catch it.
$mUcs4 = ($in);
$mUcs4 = ($mUcs4 & 0x03) << 24;
$mState = 4;
$mBytes = 5;
} elseif (0xFC == (0xFE & ($in))) {
// First octet of 6 octet sequence, see comments for 5
// octet sequence.
$mUcs4 = ($in);
$mUcs4 = ($mUcs4 & 1) << 30;
$mState = 5;
$mBytes = 6;
} else {
// Current octet is neither in the US-ASCII range nor a
// legal first octet of a multi-octet sequence.
$mState = 0;
$mUcs4 = 0;
$mBytes = 1;
$char = '';
}
} else {
// When mState is non-zero, we expect a continuation of the
// multi-octet sequence
if (0x80 == (0xC0 & ($in))) {
// Legal continuation.
$shift = ($mState - 1) * 6;
$tmp = $in;
$tmp = ($tmp & 0x0000003F) << $shift;
$mUcs4 |= $tmp;
if (0 == --$mState) {
// End of the multi-octet sequence. mUcs4 now contains
// the final Unicode codepoint to be output
// Check for illegal sequences and codepoints.
// From Unicode 3.1, non-shortest form is illegal
if (((2 == $mBytes) && ($mUcs4 < 0x0080)) ||
((3 == $mBytes) && ($mUcs4 < 0x0800)) ||
((4 == $mBytes) && ($mUcs4 < 0x10000)) ||
(4 < $mBytes) ||
// From Unicode 3.2, surrogate characters = illegal
(($mUcs4 & 0xFFFFF800) == 0xD800) ||
// Codepoints outside the Unicode range are illegal
($mUcs4 > 0x10FFFF)
) {
} elseif (0xFEFF != $mUcs4 && // omit BOM
// check for valid Char unicode codepoints
(
0x9 == $mUcs4 ||
0xA == $mUcs4 ||
0xD == $mUcs4 ||
(0x20 <= $mUcs4 && 0x7E >= $mUcs4) ||
// 7F-9F is not strictly prohibited by XML,
// but it is non-SGML, and thus we don't allow it
(0xA0 <= $mUcs4 && 0xD7FF >= $mUcs4) ||
(0x10000 <= $mUcs4 && 0x10FFFF >= $mUcs4)
)
) {
$out .= $char;
}
// initialize UTF8 cache (reset)
$mState = 0;
$mUcs4 = 0;
$mBytes = 1;
$char = '';
}
} else {
// ((0xC0 & (*in) != 0x80) && (mState != 0))
// Incomplete multi-octet sequence.
// used to result in complete fail, but we'll reset
$mState = 0;
$mUcs4 = 0;
$mBytes = 1;
$char ='';
}
}
}
return $out;
}
/**
* Translates a Unicode codepoint into its corresponding UTF-8 character.
* @note Based on Feyd's function at
* ,
* which is in public domain.
* @note While we're going to do code point parsing anyway, a good
* optimization would be to refuse to translate code points that
* are non-SGML characters. However, this could lead to duplication.
* @note This is very similar to the unichr function in
* maintenance/generate-entity-file.php (although this is superior,
* due to its sanity checks).
*/
// +----------+----------+----------+----------+
// | 33222222 | 22221111 | 111111 | |
// | 10987654 | 32109876 | 54321098 | 76543210 | bit
// +----------+----------+----------+----------+
// | | | | 0xxxxxxx | 1 byte 0x00000000..0x0000007F
// | | | 110yyyyy | 10xxxxxx | 2 byte 0x00000080..0x000007FF
// | | 1110zzzz | 10yyyyyy | 10xxxxxx | 3 byte 0x00000800..0x0000FFFF
// | 11110www | 10wwzzzz | 10yyyyyy | 10xxxxxx | 4 byte 0x00010000..0x0010FFFF
// +----------+----------+----------+----------+
// | 00000000 | 00011111 | 11111111 | 11111111 | Theoretical upper limit of legal scalars: 2097151 (0x001FFFFF)
// | 00000000 | 00010000 | 11111111 | 11111111 | Defined upper limit of legal scalar codes
// +----------+----------+----------+----------+
public static function unichr($code)
{
if ($code > 1114111 or $code < 0 or
($code >= 55296 and $code <= 57343) ) {
// bits are set outside the "valid" range as defined
// by UNICODE 4.1.0
return '';
}
$x = $y = $z = $w = 0;
if ($code < 128) {
// regular ASCII character
$x = $code;
} else {
// set up bits for UTF-8
$x = ($code & 63) | 128;
if ($code < 2048) {
$y = (($code & 2047) >> 6) | 192;
} else {
$y = (($code & 4032) >> 6) | 128;
if ($code < 65536) {
$z = (($code >> 12) & 15) | 224;
} else {
$z = (($code >> 12) & 63) | 128;
$w = (($code >> 18) & 7) | 240;
}
}
}
// set up the actual character
$ret = '';
if ($w) {
$ret .= chr($w);
}
if ($z) {
$ret .= chr($z);
}
if ($y) {
$ret .= chr($y);
}
$ret .= chr($x);
return $ret;
}
/**
* @return bool
*/
public static function iconvAvailable()
{
static $iconv = null;
if ($iconv === null) {
$iconv = function_exists('iconv') && self::testIconvTruncateBug() != self::ICONV_UNUSABLE;
}
return $iconv;
}
/**
* Convert a string to UTF-8 based on configuration.
* @param string $str The string to convert
* @param HTMLPurifier_Config $config
* @param HTMLPurifier_Context $context
* @return string
*/
public static function convertToUTF8($str, $config, $context)
{
$encoding = $config->get('Core.Encoding');
if ($encoding === 'utf-8') {
return $str;
}
static $iconv = null;
if ($iconv === null) {
$iconv = self::iconvAvailable();
}
if ($iconv && !$config->get('Test.ForceNoIconv')) {
// unaffected by bugs, since UTF-8 support all characters
$str = self::unsafeIconv($encoding, 'utf-8//IGNORE', $str);
if ($str === false) {
// $encoding is not a valid encoding
trigger_error('Invalid encoding ' . $encoding, E_USER_ERROR);
return '';
}
// If the string is bjorked by Shift_JIS or a similar encoding
// that doesn't support all of ASCII, convert the naughty
// characters to their true byte-wise ASCII/UTF-8 equivalents.
$str = strtr($str, self::testEncodingSupportsASCII($encoding));
return $str;
} elseif ($encoding === 'iso-8859-1') {
$str = utf8_encode($str);
return $str;
}
$bug = HTMLPurifier_Encoder::testIconvTruncateBug();
if ($bug == self::ICONV_OK) {
trigger_error('Encoding not supported, please install iconv', E_USER_ERROR);
} else {
trigger_error(
'You have a buggy version of iconv, see https://bugs.php.net/bug.php?id=48147 ' .
'and http://sourceware.org/bugzilla/show_bug.cgi?id=13541',
E_USER_ERROR
);
}
}
/**
* Converts a string from UTF-8 based on configuration.
* @param string $str The string to convert
* @param HTMLPurifier_Config $config
* @param HTMLPurifier_Context $context
* @return string
* @note Currently, this is a lossy conversion, with unexpressable
* characters being omitted.
*/
public static function convertFromUTF8($str, $config, $context)
{
$encoding = $config->get('Core.Encoding');
if ($escape = $config->get('Core.EscapeNonASCIICharacters')) {
$str = self::convertToASCIIDumbLossless($str);
}
if ($encoding === 'utf-8') {
return $str;
}
static $iconv = null;
if ($iconv === null) {
$iconv = self::iconvAvailable();
}
if ($iconv && !$config->get('Test.ForceNoIconv')) {
// Undo our previous fix in convertToUTF8, otherwise iconv will barf
$ascii_fix = self::testEncodingSupportsASCII($encoding);
if (!$escape && !empty($ascii_fix)) {
$clear_fix = array();
foreach ($ascii_fix as $utf8 => $native) {
$clear_fix[$utf8] = '';
}
$str = strtr($str, $clear_fix);
}
$str = strtr($str, array_flip($ascii_fix));
// Normal stuff
$str = self::iconv('utf-8', $encoding . '//IGNORE', $str);
return $str;
} elseif ($encoding === 'iso-8859-1') {
$str = utf8_decode($str);
return $str;
}
trigger_error('Encoding not supported', E_USER_ERROR);
// You might be tempted to assume that the ASCII representation
// might be OK, however, this is *not* universally true over all
// encodings. So we take the conservative route here, rather
// than forcibly turn on %Core.EscapeNonASCIICharacters
}
/**
* Lossless (character-wise) conversion of HTML to ASCII
* @param string $str UTF-8 string to be converted to ASCII
* @return string ASCII encoded string with non-ASCII character entity-ized
* @warning Adapted from MediaWiki, claiming fair use: this is a common
* algorithm. If you disagree with this license fudgery,
* implement it yourself.
* @note Uses decimal numeric entities since they are best supported.
* @note This is a DUMB function: it has no concept of keeping
* character entities that the projected character encoding
* can allow. We could possibly implement a smart version
* but that would require it to also know which Unicode
* codepoints the charset supported (not an easy task).
* @note Sort of with cleanUTF8() but it assumes that $str is
* well-formed UTF-8
*/
public static function convertToASCIIDumbLossless($str)
{
$bytesleft = 0;
$result = '';
$working = 0;
$len = strlen($str);
for ($i = 0; $i < $len; $i++) {
$bytevalue = ord($str[$i]);
if ($bytevalue <= 0x7F) { //0xxx xxxx
$result .= chr($bytevalue);
$bytesleft = 0;
} elseif ($bytevalue <= 0xBF) { //10xx xxxx
$working = $working << 6;
$working += ($bytevalue & 0x3F);
$bytesleft--;
if ($bytesleft <= 0) {
$result .= "" . $working . ";";
}
} elseif ($bytevalue <= 0xDF) { //110x xxxx
$working = $bytevalue & 0x1F;
$bytesleft = 1;
} elseif ($bytevalue <= 0xEF) { //1110 xxxx
$working = $bytevalue & 0x0F;
$bytesleft = 2;
} else { //1111 0xxx
$working = $bytevalue & 0x07;
$bytesleft = 3;
}
}
return $result;
}
/** No bugs detected in iconv. */
const ICONV_OK = 0;
/** Iconv truncates output if converting from UTF-8 to another
* character set with //IGNORE, and a non-encodable character is found */
const ICONV_TRUNCATES = 1;
/** Iconv does not support //IGNORE, making it unusable for
* transcoding purposes */
const ICONV_UNUSABLE = 2;
/**
* glibc iconv has a known bug where it doesn't handle the magic
* //IGNORE stanza correctly. In particular, rather than ignore
* characters, it will return an EILSEQ after consuming some number
* of characters, and expect you to restart iconv as if it were
* an E2BIG. Old versions of PHP did not respect the errno, and
* returned the fragment, so as a result you would see iconv
* mysteriously truncating output. We can work around this by
* manually chopping our input into segments of about 8000
* characters, as long as PHP ignores the error code. If PHP starts
* paying attention to the error code, iconv becomes unusable.
*
* @return int Error code indicating severity of bug.
*/
public static function testIconvTruncateBug()
{
static $code = null;
if ($code === null) {
// better not use iconv, otherwise infinite loop!
$r = self::unsafeIconv('utf-8', 'ascii//IGNORE', "\xCE\xB1" . str_repeat('a', 9000));
if ($r === false) {
$code = self::ICONV_UNUSABLE;
} elseif (($c = strlen($r)) < 9000) {
$code = self::ICONV_TRUNCATES;
} elseif ($c > 9000) {
trigger_error(
'Your copy of iconv is extremely buggy. Please notify HTML Purifier maintainers: ' .
'include your iconv version as per phpversion()',
E_USER_ERROR
);
} else {
$code = self::ICONV_OK;
}
}
return $code;
}
/**
* This expensive function tests whether or not a given character
* encoding supports ASCII. 7/8-bit encodings like Shift_JIS will
* fail this test, and require special processing. Variable width
* encodings shouldn't ever fail.
*
* @param string $encoding Encoding name to test, as per iconv format
* @param bool $bypass Whether or not to bypass the precompiled arrays.
* @return Array of UTF-8 characters to their corresponding ASCII,
* which can be used to "undo" any overzealous iconv action.
*/
public static function testEncodingSupportsASCII($encoding, $bypass = false)
{
// All calls to iconv here are unsafe, proof by case analysis:
// If ICONV_OK, no difference.
// If ICONV_TRUNCATE, all calls involve one character inputs,
// so bug is not triggered.
// If ICONV_UNUSABLE, this call is irrelevant
static $encodings = array();
if (!$bypass) {
if (isset($encodings[$encoding])) {
return $encodings[$encoding];
}
$lenc = strtolower($encoding);
switch ($lenc) {
case 'shift_jis':
return array("\xC2\xA5" => '\\', "\xE2\x80\xBE" => '~');
case 'johab':
return array("\xE2\x82\xA9" => '\\');
}
if (strpos($lenc, 'iso-8859-') === 0) {
return array();
}
}
$ret = array();
if (self::unsafeIconv('UTF-8', $encoding, 'a') === false) {
return false;
}
for ($i = 0x20; $i <= 0x7E; $i++) { // all printable ASCII chars
$c = chr($i); // UTF-8 char
$r = self::unsafeIconv('UTF-8', "$encoding//IGNORE", $c); // initial conversion
if ($r === '' ||
// This line is needed for iconv implementations that do not
// omit characters that do not exist in the target character set
($r === $c && self::unsafeIconv($encoding, 'UTF-8//IGNORE', $r) !== $c)
) {
// Reverse engineer: what's the UTF-8 equiv of this byte
// sequence? This assumes that there's no variable width
// encoding that doesn't support ASCII.
$ret[self::unsafeIconv($encoding, 'UTF-8//IGNORE', $c)] = $c;
}
}
$encodings[$encoding] = $ret;
return $ret;
}
}
/**
* Object that provides entity lookup table from entity name to character
*/
class HTMLPurifier_EntityLookup
{
/**
* Assoc array of entity name to character represented.
* @type array
*/
public $table;
/**
* Sets up the entity lookup table from the serialized file contents.
* @param bool $file
* @note The serialized contents are versioned, but were generated
* using the maintenance script generate_entity_file.php
* @warning This is not in constructor to help enforce the Singleton
*/
public function setup($file = false)
{
if (!$file) {
$file = HTMLPURIFIER_PREFIX . '/HTMLPurifier/EntityLookup/entities.ser';
}
$this->table = unserialize(file_get_contents($file));
}
/**
* Retrieves sole instance of the object.
* @param bool|HTMLPurifier_EntityLookup $prototype Optional prototype of custom lookup table to overload with.
* @return HTMLPurifier_EntityLookup
*/
public static function instance($prototype = false)
{
// no references, since PHP doesn't copy unless modified
static $instance = null;
if ($prototype) {
$instance = $prototype;
} elseif (!$instance) {
$instance = new HTMLPurifier_EntityLookup();
$instance->setup();
}
return $instance;
}
}
// if want to implement error collecting here, we'll need to use some sort
// of global data (probably trigger_error) because it's impossible to pass
// $config or $context to the callback functions.
/**
* Handles referencing and derefencing character entities
*/
class HTMLPurifier_EntityParser
{
/**
* Reference to entity lookup table.
* @type HTMLPurifier_EntityLookup
*/
protected $_entity_lookup;
/**
* Callback regex string for parsing entities.
* @type string
*/
protected $_substituteEntitiesRegex =
'/&(?:[#]x([a-fA-F0-9]+)|[#]0*(\d+)|([A-Za-z_:][A-Za-z0-9.\-_:]*));?/';
// 1. hex 2. dec 3. string (XML style)
/**
* Decimal to parsed string conversion table for special entities.
* @type array
*/
protected $_special_dec2str =
array(
34 => '"',
38 => '&',
39 => "'",
60 => '<',
62 => '>'
);
/**
* Stripped entity names to decimal conversion table for special entities.
* @type array
*/
protected $_special_ent2dec =
array(
'quot' => 34,
'amp' => 38,
'lt' => 60,
'gt' => 62
);
/**
* Substitutes non-special entities with their parsed equivalents. Since
* running this whenever you have parsed character is t3h 5uck, we run
* it before everything else.
*
* @param string $string String to have non-special entities parsed.
* @return string Parsed string.
*/
public function substituteNonSpecialEntities($string)
{
// it will try to detect missing semicolons, but don't rely on it
return preg_replace_callback(
$this->_substituteEntitiesRegex,
array($this, 'nonSpecialEntityCallback'),
$string
);
}
/**
* Callback function for substituteNonSpecialEntities() that does the work.
*
* @param array $matches PCRE matches array, with 0 the entire match, and
* either index 1, 2 or 3 set with a hex value, dec value,
* or string (respectively).
* @return string Replacement string.
*/
protected function nonSpecialEntityCallback($matches)
{
// replaces all but big five
$entity = $matches[0];
$is_num = (@$matches[0][1] === '#');
if ($is_num) {
$is_hex = (@$entity[2] === 'x');
$code = $is_hex ? hexdec($matches[1]) : (int) $matches[2];
// abort for special characters
if (isset($this->_special_dec2str[$code])) {
return $entity;
}
return HTMLPurifier_Encoder::unichr($code);
} else {
if (isset($this->_special_ent2dec[$matches[3]])) {
return $entity;
}
if (!$this->_entity_lookup) {
$this->_entity_lookup = HTMLPurifier_EntityLookup::instance();
}
if (isset($this->_entity_lookup->table[$matches[3]])) {
return $this->_entity_lookup->table[$matches[3]];
} else {
return $entity;
}
}
}
/**
* Substitutes only special entities with their parsed equivalents.
*
* @notice We try to avoid calling this function because otherwise, it
* would have to be called a lot (for every parsed section).
*
* @param string $string String to have non-special entities parsed.
* @return string Parsed string.
*/
public function substituteSpecialEntities($string)
{
return preg_replace_callback(
$this->_substituteEntitiesRegex,
array($this, 'specialEntityCallback'),
$string
);
}
/**
* Callback function for substituteSpecialEntities() that does the work.
*
* This callback has same syntax as nonSpecialEntityCallback().
*
* @param array $matches PCRE-style matches array, with 0 the entire match, and
* either index 1, 2 or 3 set with a hex value, dec value,
* or string (respectively).
* @return string Replacement string.
*/
protected function specialEntityCallback($matches)
{
$entity = $matches[0];
$is_num = (@$matches[0][1] === '#');
if ($is_num) {
$is_hex = (@$entity[2] === 'x');
$int = $is_hex ? hexdec($matches[1]) : (int) $matches[2];
return isset($this->_special_dec2str[$int]) ?
$this->_special_dec2str[$int] :
$entity;
} else {
return isset($this->_special_ent2dec[$matches[3]]) ?
$this->_special_ent2dec[$matches[3]] :
$entity;
}
}
}
/**
* Error collection class that enables HTML Purifier to report HTML
* problems back to the user
*/
class HTMLPurifier_ErrorCollector
{
/**
* Identifiers for the returned error array. These are purposely numeric
* so list() can be used.
*/
const LINENO = 0;
const SEVERITY = 1;
const MESSAGE = 2;
const CHILDREN = 3;
/**
* @type array
*/
protected $errors;
/**
* @type array
*/
protected $_current;
/**
* @type array
*/
protected $_stacks = array(array());
/**
* @type HTMLPurifier_Language
*/
protected $locale;
/**
* @type HTMLPurifier_Generator
*/
protected $generator;
/**
* @type HTMLPurifier_Context
*/
protected $context;
/**
* @type array
*/
protected $lines = array();
/**
* @param HTMLPurifier_Context $context
*/
public function __construct($context)
{
$this->locale =& $context->get('Locale');
$this->context = $context;
$this->_current =& $this->_stacks[0];
$this->errors =& $this->_stacks[0];
}
/**
* Sends an error message to the collector for later use
* @param int $severity Error severity, PHP error style (don't use E_USER_)
* @param string $msg Error message text
*/
public function send($severity, $msg)
{
$args = array();
if (func_num_args() > 2) {
$args = func_get_args();
array_shift($args);
unset($args[0]);
}
$token = $this->context->get('CurrentToken', true);
$line = $token ? $token->line : $this->context->get('CurrentLine', true);
$col = $token ? $token->col : $this->context->get('CurrentCol', true);
$attr = $this->context->get('CurrentAttr', true);
// perform special substitutions, also add custom parameters
$subst = array();
if (!is_null($token)) {
$args['CurrentToken'] = $token;
}
if (!is_null($attr)) {
$subst['$CurrentAttr.Name'] = $attr;
if (isset($token->attr[$attr])) {
$subst['$CurrentAttr.Value'] = $token->attr[$attr];
}
}
if (empty($args)) {
$msg = $this->locale->getMessage($msg);
} else {
$msg = $this->locale->formatMessage($msg, $args);
}
if (!empty($subst)) {
$msg = strtr($msg, $subst);
}
// (numerically indexed)
$error = array(
self::LINENO => $line,
self::SEVERITY => $severity,
self::MESSAGE => $msg,
self::CHILDREN => array()
);
$this->_current[] = $error;
// NEW CODE BELOW ...
// Top-level errors are either:
// TOKEN type, if $value is set appropriately, or
// "syntax" type, if $value is null
$new_struct = new HTMLPurifier_ErrorStruct();
$new_struct->type = HTMLPurifier_ErrorStruct::TOKEN;
if ($token) {
$new_struct->value = clone $token;
}
if (is_int($line) && is_int($col)) {
if (isset($this->lines[$line][$col])) {
$struct = $this->lines[$line][$col];
} else {
$struct = $this->lines[$line][$col] = $new_struct;
}
// These ksorts may present a performance problem
ksort($this->lines[$line], SORT_NUMERIC);
} else {
if (isset($this->lines[-1])) {
$struct = $this->lines[-1];
} else {
$struct = $this->lines[-1] = $new_struct;
}
}
ksort($this->lines, SORT_NUMERIC);
// Now, check if we need to operate on a lower structure
if (!empty($attr)) {
$struct = $struct->getChild(HTMLPurifier_ErrorStruct::ATTR, $attr);
if (!$struct->value) {
$struct->value = array($attr, 'PUT VALUE HERE');
}
}
if (!empty($cssprop)) {
$struct = $struct->getChild(HTMLPurifier_ErrorStruct::CSSPROP, $cssprop);
if (!$struct->value) {
// if we tokenize CSS this might be a little more difficult to do
$struct->value = array($cssprop, 'PUT VALUE HERE');
}
}
// Ok, structs are all setup, now time to register the error
$struct->addError($severity, $msg);
}
/**
* Retrieves raw error data for custom formatter to use
*/
public function getRaw()
{
return $this->errors;
}
/**
* Default HTML formatting implementation for error messages
* @param HTMLPurifier_Config $config Configuration, vital for HTML output nature
* @param array $errors Errors array to display; used for recursion.
* @return string
*/
public function getHTMLFormatted($config, $errors = null)
{
$ret = array();
$this->generator = new HTMLPurifier_Generator($config, $this->context);
if ($errors === null) {
$errors = $this->errors;
}
// 'At line' message needs to be removed
// generation code for new structure goes here. It needs to be recursive.
foreach ($this->lines as $line => $col_array) {
if ($line == -1) {
continue;
}
foreach ($col_array as $col => $struct) {
$this->_renderStruct($ret, $struct, $line, $col);
}
}
if (isset($this->lines[-1])) {
$this->_renderStruct($ret, $this->lines[-1]);
}
if (empty($errors)) {
return '
' . $this->locale->getMessage('ErrorCollector: No errors') . '
';
// W3C uses an icon to indicate the severity of the error.
$error = $this->locale->getErrorName($severity);
$string .= "$error ";
if (!is_null($line) && !is_null($col)) {
$string .= "Line $line, Column $col: ";
} else {
$string .= 'End of Document: ';
}
$string .= '' . $this->generator->escape($msg) . ' ';
$string .= '
';
// Here, have a marker for the character on the column appropriate.
// Be sure to clip extremely long lines.
//$string .= '
';
//$string .= '';
//$string .= '
';
$ret[] = $string;
}
foreach ($current->children as $array) {
$context[] = $current;
$stack = array_merge($stack, array_reverse($array, true));
for ($i = count($array); $i > 0; $i--) {
$context_stack[] = $context;
}
}
}
}
}
/**
* Records errors for particular segments of an HTML document such as tokens,
* attributes or CSS properties. They can contain error structs (which apply
* to components of what they represent), but their main purpose is to hold
* errors applying to whatever struct is being used.
*/
class HTMLPurifier_ErrorStruct
{
/**
* Possible values for $children first-key. Note that top-level structures
* are automatically token-level.
*/
const TOKEN = 0;
const ATTR = 1;
const CSSPROP = 2;
/**
* Type of this struct.
* @type string
*/
public $type;
/**
* Value of the struct we are recording errors for. There are various
* values for this:
* - TOKEN: Instance of HTMLPurifier_Token
* - ATTR: array('attr-name', 'value')
* - CSSPROP: array('prop-name', 'value')
* @type mixed
*/
public $value;
/**
* Errors registered for this structure.
* @type array
*/
public $errors = array();
/**
* Child ErrorStructs that are from this structure. For example, a TOKEN
* ErrorStruct would contain ATTR ErrorStructs. This is a multi-dimensional
* array in structure: [TYPE]['identifier']
* @type array
*/
public $children = array();
/**
* @param string $type
* @param string $id
* @return mixed
*/
public function getChild($type, $id)
{
if (!isset($this->children[$type][$id])) {
$this->children[$type][$id] = new HTMLPurifier_ErrorStruct();
$this->children[$type][$id]->type = $type;
}
return $this->children[$type][$id];
}
/**
* @param int $severity
* @param string $message
*/
public function addError($severity, $message)
{
$this->errors[] = array($severity, $message);
}
}
/**
* Global exception class for HTML Purifier; any exceptions we throw
* are from here.
*/
class HTMLPurifier_Exception extends Exception
{
}
/**
* Represents a pre or post processing filter on HTML Purifier's output
*
* Sometimes, a little ad-hoc fixing of HTML has to be done before
* it gets sent through HTML Purifier: you can use filters to acheive
* this effect. For instance, YouTube videos can be preserved using
* this manner. You could have used a decorator for this task, but
* PHP's support for them is not terribly robust, so we're going
* to just loop through the filters.
*
* Filters should be exited first in, last out. If there are three filters,
* named 1, 2 and 3, the order of execution should go 1->preFilter,
* 2->preFilter, 3->preFilter, purify, 3->postFilter, 2->postFilter,
* 1->postFilter.
*
* @note Methods are not declared abstract as it is perfectly legitimate
* for an implementation not to want anything to happen on a step
*/
class HTMLPurifier_Filter
{
/**
* Name of the filter for identification purposes.
* @type string
*/
public $name;
/**
* Pre-processor function, handles HTML before HTML Purifier
* @param string $html
* @param HTMLPurifier_Config $config
* @param HTMLPurifier_Context $context
* @return string
*/
public function preFilter($html, $config, $context)
{
return $html;
}
/**
* Post-processor function, handles HTML after HTML Purifier
* @param string $html
* @param HTMLPurifier_Config $config
* @param HTMLPurifier_Context $context
* @return string
*/
public function postFilter($html, $config, $context)
{
return $html;
}
}
/**
* Generates HTML from tokens.
* @todo Refactor interface so that configuration/context is determined
* upon instantiation, no need for messy generateFromTokens() calls
* @todo Make some of the more internal functions protected, and have
* unit tests work around that
*/
class HTMLPurifier_Generator
{
/**
* Whether or not generator should produce XML output.
* @type bool
*/
private $_xhtml = true;
/**
* :HACK: Whether or not generator should comment the insides of )#si',
array($this, 'scriptCallback'),
$html
);
}
$html = $this->normalize($html, $config, $context);
$cursor = 0; // our location in the text
$inside_tag = false; // whether or not we're parsing the inside of a tag
$array = array(); // result array
// This is also treated to mean maintain *column* numbers too
$maintain_line_numbers = $config->get('Core.MaintainLineNumbers');
if ($maintain_line_numbers === null) {
// automatically determine line numbering by checking
// if error collection is on
$maintain_line_numbers = $config->get('Core.CollectErrors');
}
if ($maintain_line_numbers) {
$current_line = 1;
$current_col = 0;
$length = strlen($html);
} else {
$current_line = false;
$current_col = false;
$length = false;
}
$context->register('CurrentLine', $current_line);
$context->register('CurrentCol', $current_col);
$nl = "\n";
// how often to manually recalculate. This will ALWAYS be right,
// but it's pretty wasteful. Set to 0 to turn off
$synchronize_interval = $config->get('Core.DirectLexLineNumberSyncInterval');
$e = false;
if ($config->get('Core.CollectErrors')) {
$e =& $context->get('ErrorCollector');
}
// for testing synchronization
$loops = 0;
while (++$loops) {
// $cursor is either at the start of a token, or inside of
// a tag (i.e. there was a < immediately before it), as indicated
// by $inside_tag
if ($maintain_line_numbers) {
// $rcursor, however, is always at the start of a token.
$rcursor = $cursor - (int)$inside_tag;
// Column number is cheap, so we calculate it every round.
// We're interested at the *end* of the newline string, so
// we need to add strlen($nl) == 1 to $nl_pos before subtracting it
// from our "rcursor" position.
$nl_pos = strrpos($html, $nl, $rcursor - $length);
$current_col = $rcursor - (is_bool($nl_pos) ? 0 : $nl_pos + 1);
// recalculate lines
if ($synchronize_interval && // synchronization is on
$cursor > 0 && // cursor is further than zero
$loops % $synchronize_interval === 0) { // time to synchronize!
$current_line = 1 + $this->substrCount($html, $nl, 0, $cursor);
}
}
$position_next_lt = strpos($html, '<', $cursor);
$position_next_gt = strpos($html, '>', $cursor);
// triggers on "asdf" but not "asdf "
// special case to set up context
if ($position_next_lt === $cursor) {
$inside_tag = true;
$cursor++;
}
if (!$inside_tag && $position_next_lt !== false) {
// We are not inside tag and there still is another tag to parse
$token = new
HTMLPurifier_Token_Text(
$this->parseData(
substr(
$html,
$cursor,
$position_next_lt - $cursor
)
)
);
if ($maintain_line_numbers) {
$token->rawPosition($current_line, $current_col);
$current_line += $this->substrCount($html, $nl, $cursor, $position_next_lt - $cursor);
}
$array[] = $token;
$cursor = $position_next_lt + 1;
$inside_tag = true;
continue;
} elseif (!$inside_tag) {
// We are not inside tag but there are no more tags
// If we're already at the end, break
if ($cursor === strlen($html)) {
break;
}
// Create Text of rest of string
$token = new
HTMLPurifier_Token_Text(
$this->parseData(
substr(
$html,
$cursor
)
)
);
if ($maintain_line_numbers) {
$token->rawPosition($current_line, $current_col);
}
$array[] = $token;
break;
} elseif ($inside_tag && $position_next_gt !== false) {
// We are in tag and it is well formed
// Grab the internals of the tag
$strlen_segment = $position_next_gt - $cursor;
if ($strlen_segment < 1) {
// there's nothing to process!
$token = new HTMLPurifier_Token_Text('<');
$cursor++;
continue;
}
$segment = substr($html, $cursor, $strlen_segment);
if ($segment === false) {
// somehow, we attempted to access beyond the end of
// the string, defense-in-depth, reported by Nate Abele
break;
}
// Check if it's a comment
if (substr($segment, 0, 3) === '!--') {
// re-determine segment length, looking for -->
$position_comment_end = strpos($html, '-->', $cursor);
if ($position_comment_end === false) {
// uh oh, we have a comment that extends to
// infinity. Can't be helped: set comment
// end position to end of string
if ($e) {
$e->send(E_WARNING, 'Lexer: Unclosed comment');
}
$position_comment_end = strlen($html);
$end = true;
} else {
$end = false;
}
$strlen_segment = $position_comment_end - $cursor;
$segment = substr($html, $cursor, $strlen_segment);
$token = new
HTMLPurifier_Token_Comment(
substr(
$segment,
3,
$strlen_segment - 3
)
);
if ($maintain_line_numbers) {
$token->rawPosition($current_line, $current_col);
$current_line += $this->substrCount($html, $nl, $cursor, $strlen_segment);
}
$array[] = $token;
$cursor = $end ? $position_comment_end : $position_comment_end + 3;
$inside_tag = false;
continue;
}
// Check if it's an end tag
$is_end_tag = (strpos($segment, '/') === 0);
if ($is_end_tag) {
$type = substr($segment, 1);
$token = new HTMLPurifier_Token_End($type);
if ($maintain_line_numbers) {
$token->rawPosition($current_line, $current_col);
$current_line += $this->substrCount($html, $nl, $cursor, $position_next_gt - $cursor);
}
$array[] = $token;
$inside_tag = false;
$cursor = $position_next_gt + 1;
continue;
}
// Check leading character is alnum, if not, we may
// have accidently grabbed an emoticon. Translate into
// text and go our merry way
if (!ctype_alpha($segment[0])) {
// XML: $segment[0] !== '_' && $segment[0] !== ':'
if ($e) {
$e->send(E_NOTICE, 'Lexer: Unescaped lt');
}
$token = new HTMLPurifier_Token_Text('<');
if ($maintain_line_numbers) {
$token->rawPosition($current_line, $current_col);
$current_line += $this->substrCount($html, $nl, $cursor, $position_next_gt - $cursor);
}
$array[] = $token;
$inside_tag = false;
continue;
}
// Check if it is explicitly self closing, if so, remove
// trailing slash. Remember, we could have a tag like , so
// any later token processing scripts must convert improperly
// classified EmptyTags from StartTags.
$is_self_closing = (strrpos($segment, '/') === $strlen_segment - 1);
if ($is_self_closing) {
$strlen_segment--;
$segment = substr($segment, 0, $strlen_segment);
}
// Check if there are any attributes
$position_first_space = strcspn($segment, $this->_whitespace);
if ($position_first_space >= $strlen_segment) {
if ($is_self_closing) {
$token = new HTMLPurifier_Token_Empty($segment);
} else {
$token = new HTMLPurifier_Token_Start($segment);
}
if ($maintain_line_numbers) {
$token->rawPosition($current_line, $current_col);
$current_line += $this->substrCount($html, $nl, $cursor, $position_next_gt - $cursor);
}
$array[] = $token;
$inside_tag = false;
$cursor = $position_next_gt + 1;
continue;
}
// Grab out all the data
$type = substr($segment, 0, $position_first_space);
$attribute_string =
trim(
substr(
$segment,
$position_first_space
)
);
if ($attribute_string) {
$attr = $this->parseAttributeString(
$attribute_string,
$config,
$context
);
} else {
$attr = array();
}
if ($is_self_closing) {
$token = new HTMLPurifier_Token_Empty($type, $attr);
} else {
$token = new HTMLPurifier_Token_Start($type, $attr);
}
if ($maintain_line_numbers) {
$token->rawPosition($current_line, $current_col);
$current_line += $this->substrCount($html, $nl, $cursor, $position_next_gt - $cursor);
}
$array[] = $token;
$cursor = $position_next_gt + 1;
$inside_tag = false;
continue;
} else {
// inside tag, but there's no ending > sign
if ($e) {
$e->send(E_WARNING, 'Lexer: Missing gt');
}
$token = new
HTMLPurifier_Token_Text(
'<' .
$this->parseData(
substr($html, $cursor)
)
);
if ($maintain_line_numbers) {
$token->rawPosition($current_line, $current_col);
}
// no cursor scroll? Hmm...
$array[] = $token;
break;
}
break;
}
$context->destroy('CurrentLine');
$context->destroy('CurrentCol');
return $array;
}
/**
* PHP 5.0.x compatible substr_count that implements offset and length
* @param string $haystack
* @param string $needle
* @param int $offset
* @param int $length
* @return int
*/
protected function substrCount($haystack, $needle, $offset, $length)
{
static $oldVersion;
if ($oldVersion === null) {
$oldVersion = version_compare(PHP_VERSION, '5.1', '<');
}
if ($oldVersion) {
$haystack = substr($haystack, $offset, $length);
return substr_count($haystack, $needle);
} else {
return substr_count($haystack, $needle, $offset, $length);
}
}
/**
* Takes the inside of an HTML tag and makes an assoc array of attributes.
*
* @param string $string Inside of tag excluding name.
* @param HTMLPurifier_Config $config
* @param HTMLPurifier_Context $context
* @return array Assoc array of attributes.
*/
public function parseAttributeString($string, $config, $context)
{
$string = (string)$string; // quick typecast
if ($string == '') {
return array();
} // no attributes
$e = false;
if ($config->get('Core.CollectErrors')) {
$e =& $context->get('ErrorCollector');
}
// let's see if we can abort as quickly as possible
// one equal sign, no spaces => one attribute
$num_equal = substr_count($string, '=');
$has_space = strpos($string, ' ');
if ($num_equal === 0 && !$has_space) {
// bool attribute
return array($string => $string);
} elseif ($num_equal === 1 && !$has_space) {
// only one attribute
list($key, $quoted_value) = explode('=', $string);
$quoted_value = trim($quoted_value);
if (!$key) {
if ($e) {
$e->send(E_ERROR, 'Lexer: Missing attribute key');
}
return array();
}
if (!$quoted_value) {
return array($key => '');
}
$first_char = @$quoted_value[0];
$last_char = @$quoted_value[strlen($quoted_value) - 1];
$same_quote = ($first_char == $last_char);
$open_quote = ($first_char == '"' || $first_char == "'");
if ($same_quote && $open_quote) {
// well behaved
$value = substr($quoted_value, 1, strlen($quoted_value) - 2);
} else {
// not well behaved
if ($open_quote) {
if ($e) {
$e->send(E_ERROR, 'Lexer: Missing end quote');
}
$value = substr($quoted_value, 1);
} else {
$value = $quoted_value;
}
}
if ($value === false) {
$value = '';
}
return array($key => $this->parseData($value));
}
// setup loop environment
$array = array(); // return assoc array of attributes
$cursor = 0; // current position in string (moves forward)
$size = strlen($string); // size of the string (stays the same)
// if we have unquoted attributes, the parser expects a terminating
// space, so let's guarantee that there's always a terminating space.
$string .= ' ';
$old_cursor = -1;
while ($cursor < $size) {
if ($old_cursor >= $cursor) {
throw new Exception("Infinite loop detected");
}
$old_cursor = $cursor;
$cursor += ($value = strspn($string, $this->_whitespace, $cursor));
// grab the key
$key_begin = $cursor; //we're currently at the start of the key
// scroll past all characters that are the key (not whitespace or =)
$cursor += strcspn($string, $this->_whitespace . '=', $cursor);
$key_end = $cursor; // now at the end of the key
$key = substr($string, $key_begin, $key_end - $key_begin);
if (!$key) {
if ($e) {
$e->send(E_ERROR, 'Lexer: Missing attribute key');
}
$cursor += 1 + strcspn($string, $this->_whitespace, $cursor + 1); // prevent infinite loop
continue; // empty key
}
// scroll past all whitespace
$cursor += strspn($string, $this->_whitespace, $cursor);
if ($cursor >= $size) {
$array[$key] = $key;
break;
}
// if the next character is an equal sign, we've got a regular
// pair, otherwise, it's a bool attribute
$first_char = @$string[$cursor];
if ($first_char == '=') {
// key="value"
$cursor++;
$cursor += strspn($string, $this->_whitespace, $cursor);
if ($cursor === false) {
$array[$key] = '';
break;
}
// we might be in front of a quote right now
$char = @$string[$cursor];
if ($char == '"' || $char == "'") {
// it's quoted, end bound is $char
$cursor++;
$value_begin = $cursor;
$cursor = strpos($string, $char, $cursor);
$value_end = $cursor;
} else {
// it's not quoted, end bound is whitespace
$value_begin = $cursor;
$cursor += strcspn($string, $this->_whitespace, $cursor);
$value_end = $cursor;
}
// we reached a premature end
if ($cursor === false) {
$cursor = $size;
$value_end = $cursor;
}
$value = substr($string, $value_begin, $value_end - $value_begin);
if ($value === false) {
$value = '';
}
$array[$key] = $this->parseData($value);
$cursor++;
} else {
// boolattr
if ($key !== '') {
$array[$key] = $key;
} else {
// purely theoretical
if ($e) {
$e->send(E_ERROR, 'Lexer: Missing attribute key');
}
}
}
}
return $array;
}
}
/**
* Concrete comment node class.
*/
class HTMLPurifier_Node_Comment extends HTMLPurifier_Node
{
/**
* Character data within comment.
* @type string
*/
public $data;
/**
* @type bool
*/
public $is_whitespace = true;
/**
* Transparent constructor.
*
* @param string $data String comment data.
* @param int $line
* @param int $col
*/
public function __construct($data, $line = null, $col = null)
{
$this->data = $data;
$this->line = $line;
$this->col = $col;
}
public function toTokenPair() {
return array(new HTMLPurifier_Token_Comment($this->data, $this->line, $this->col), null);
}
}
/**
* Concrete element node class.
*/
class HTMLPurifier_Node_Element extends HTMLPurifier_Node
{
/**
* The lower-case name of the tag, like 'a', 'b' or 'blockquote'.
*
* @note Strictly speaking, XML tags are case sensitive, so we shouldn't
* be lower-casing them, but these tokens cater to HTML tags, which are
* insensitive.
* @type string
*/
public $name;
/**
* Associative array of the node's attributes.
* @type array
*/
public $attr = array();
/**
* List of child elements.
* @type array
*/
public $children = array();
/**
* Does this use the form or the form, i.e.
* is it a pair of start/end tokens or an empty token.
* @bool
*/
public $empty = false;
public $endCol = null, $endLine = null, $endArmor = array();
public function __construct($name, $attr = array(), $line = null, $col = null, $armor = array()) {
$this->name = $name;
$this->attr = $attr;
$this->line = $line;
$this->col = $col;
$this->armor = $armor;
}
public function toTokenPair() {
// XXX inefficiency here, normalization is not necessary
if ($this->empty) {
return array(new HTMLPurifier_Token_Empty($this->name, $this->attr, $this->line, $this->col, $this->armor), null);
} else {
$start = new HTMLPurifier_Token_Start($this->name, $this->attr, $this->line, $this->col, $this->armor);
$end = new HTMLPurifier_Token_End($this->name, array(), $this->endLine, $this->endCol, $this->endArmor);
//$end->start = $start;
return array($start, $end);
}
}
}
/**
* Concrete text token class.
*
* Text tokens comprise of regular parsed character data (PCDATA) and raw
* character data (from the CDATA sections). Internally, their
* data is parsed with all entities expanded. Surprisingly, the text token
* does have a "tag name" called #PCDATA, which is how the DTD represents it
* in permissible child nodes.
*/
class HTMLPurifier_Node_Text extends HTMLPurifier_Node
{
/**
* PCDATA tag name compatible with DTD, see
* HTMLPurifier_ChildDef_Custom for details.
* @type string
*/
public $name = '#PCDATA';
/**
* @type string
*/
public $data;
/**< Parsed character data of text. */
/**
* @type bool
*/
public $is_whitespace;
/**< Bool indicating if node is whitespace. */
/**
* Constructor, accepts data and determines if it is whitespace.
* @param string $data String parsed character data.
* @param int $line
* @param int $col
*/
public function __construct($data, $is_whitespace, $line = null, $col = null)
{
$this->data = $data;
$this->is_whitespace = $is_whitespace;
$this->line = $line;
$this->col = $col;
}
public function toTokenPair() {
return array(new HTMLPurifier_Token_Text($this->data, $this->line, $this->col), null);
}
}
/**
* Composite strategy that runs multiple strategies on tokens.
*/
abstract class HTMLPurifier_Strategy_Composite extends HTMLPurifier_Strategy
{
/**
* List of strategies to run tokens through.
* @type HTMLPurifier_Strategy[]
*/
protected $strategies = array();
/**
* @param HTMLPurifier_Token[] $tokens
* @param HTMLPurifier_Config $config
* @param HTMLPurifier_Context $context
* @return HTMLPurifier_Token[]
*/
public function execute($tokens, $config, $context)
{
foreach ($this->strategies as $strategy) {
$tokens = $strategy->execute($tokens, $config, $context);
}
return $tokens;
}
}
/**
* Core strategy composed of the big four strategies.
*/
class HTMLPurifier_Strategy_Core extends HTMLPurifier_Strategy_Composite
{
public function __construct()
{
$this->strategies[] = new HTMLPurifier_Strategy_RemoveForeignElements();
$this->strategies[] = new HTMLPurifier_Strategy_MakeWellFormed();
$this->strategies[] = new HTMLPurifier_Strategy_FixNesting();
$this->strategies[] = new HTMLPurifier_Strategy_ValidateAttributes();
}
}
/**
* Takes a well formed list of tokens and fixes their nesting.
*
* HTML elements dictate which elements are allowed to be their children,
* for example, you can't have a p tag in a span tag. Other elements have
* much more rigorous definitions: tables, for instance, require a specific
* order for their elements. There are also constraints not expressible by
* document type definitions, such as the chameleon nature of ins/del
* tags and global child exclusions.
*
* The first major objective of this strategy is to iterate through all
* the nodes and determine whether or not their children conform to the
* element's definition. If they do not, the child definition may
* optionally supply an amended list of elements that is valid or
* require that the entire node be deleted (and the previous node
* rescanned).
*
* The second objective is to ensure that explicitly excluded elements of
* an element do not appear in its children. Code that accomplishes this
* task is pervasive through the strategy, though the two are distinct tasks
* and could, theoretically, be seperated (although it's not recommended).
*
* @note Whether or not unrecognized children are silently dropped or
* translated into text depends on the child definitions.
*
* @todo Enable nodes to be bubbled out of the structure. This is
* easier with our new algorithm.
*/
class HTMLPurifier_Strategy_FixNesting extends HTMLPurifier_Strategy
{
/**
* @param HTMLPurifier_Token[] $tokens
* @param HTMLPurifier_Config $config
* @param HTMLPurifier_Context $context
* @return array|HTMLPurifier_Token[]
*/
public function execute($tokens, $config, $context)
{
//####################################################################//
// Pre-processing
// O(n) pass to convert to a tree, so that we can efficiently
// refer to substrings
$top_node = HTMLPurifier_Arborize::arborize($tokens, $config, $context);
// get a copy of the HTML definition
$definition = $config->getHTMLDefinition();
$excludes_enabled = !$config->get('Core.DisableExcludes');
// setup the context variable 'IsInline', for chameleon processing
// is 'false' when we are not inline, 'true' when it must always
// be inline, and an integer when it is inline for a certain
// branch of the document tree
$is_inline = $definition->info_parent_def->descendants_are_inline;
$context->register('IsInline', $is_inline);
// setup error collector
$e =& $context->get('ErrorCollector', true);
//####################################################################//
// Loop initialization
// stack that contains all elements that are excluded
// it is organized by parent elements, similar to $stack,
// but it is only populated when an element with exclusions is
// processed, i.e. there won't be empty exclusions.
$exclude_stack = array($definition->info_parent_def->excludes);
// variable that contains the start token while we are processing
// nodes. This enables error reporting to do its job
$node = $top_node;
// dummy token
list($token, $d) = $node->toTokenPair();
$context->register('CurrentNode', $node);
$context->register('CurrentToken', $token);
//####################################################################//
// Loop
// We need to implement a post-order traversal iteratively, to
// avoid running into stack space limits. This is pretty tricky
// to reason about, so we just manually stack-ify the recursive
// variant:
//
// function f($node) {
// foreach ($node->children as $child) {
// f($child);
// }
// validate($node);
// }
//
// Thus, we will represent a stack frame as array($node,
// $is_inline, stack of children)
// e.g. array_reverse($node->children) - already processed
// children.
$parent_def = $definition->info_parent_def;
$stack = array(
array($top_node,
$parent_def->descendants_are_inline,
$parent_def->excludes, // exclusions
0)
);
while (!empty($stack)) {
list($node, $is_inline, $excludes, $ix) = array_pop($stack);
// recursive call
$go = false;
$def = empty($stack) ? $definition->info_parent_def : $definition->info[$node->name];
while (isset($node->children[$ix])) {
$child = $node->children[$ix++];
if ($child instanceof HTMLPurifier_Node_Element) {
$go = true;
$stack[] = array($node, $is_inline, $excludes, $ix);
$stack[] = array($child,
// ToDo: I don't think it matters if it's def or
// child_def, but double check this...
$is_inline || $def->descendants_are_inline,
empty($def->excludes) ? $excludes
: array_merge($excludes, $def->excludes),
0);
break;
}
};
if ($go) continue;
list($token, $d) = $node->toTokenPair();
// base case
if ($excludes_enabled && isset($excludes[$node->name])) {
$node->dead = true;
if ($e) $e->send(E_ERROR, 'Strategy_FixNesting: Node excluded');
} else {
// XXX I suppose it would be slightly more efficient to
// avoid the allocation here and have children
// strategies handle it
$children = array();
foreach ($node->children as $child) {
if (!$child->dead) $children[] = $child;
}
$result = $def->child->validateChildren($children, $config, $context);
if ($result === true) {
// nop
$node->children = $children;
} elseif ($result === false) {
$node->dead = true;
if ($e) $e->send(E_ERROR, 'Strategy_FixNesting: Node removed');
} else {
$node->children = $result;
if ($e) {
// XXX This will miss mutations of internal nodes. Perhaps defer to the child validators
if (empty($result) && !empty($children)) {
$e->send(E_ERROR, 'Strategy_FixNesting: Node contents removed');
} else if ($result != $children) {
$e->send(E_WARNING, 'Strategy_FixNesting: Node reorganized');
}
}
}
}
}
//####################################################################//
// Post-processing
// remove context variables
$context->destroy('IsInline');
$context->destroy('CurrentNode');
$context->destroy('CurrentToken');
//####################################################################//
// Return
return HTMLPurifier_Arborize::flatten($node, $config, $context);
}
}
/**
* Takes tokens makes them well-formed (balance end tags, etc.)
*
* Specification of the armor attributes this strategy uses:
*
* - MakeWellFormed_TagClosedError: This armor field is used to
* suppress tag closed errors for certain tokens [TagClosedSuppress],
* in particular, if a tag was generated automatically by HTML
* Purifier, we may rely on our infrastructure to close it for us
* and shouldn't report an error to the user [TagClosedAuto].
*/
class HTMLPurifier_Strategy_MakeWellFormed extends HTMLPurifier_Strategy
{
/**
* Array stream of tokens being processed.
* @type HTMLPurifier_Token[]
*/
protected $tokens;
/**
* Current token.
* @type HTMLPurifier_Token
*/
protected $token;
/**
* Zipper managing the true state.
* @type HTMLPurifier_Zipper
*/
protected $zipper;
/**
* Current nesting of elements.
* @type array
*/
protected $stack;
/**
* Injectors active in this stream processing.
* @type HTMLPurifier_Injector[]
*/
protected $injectors;
/**
* Current instance of HTMLPurifier_Config.
* @type HTMLPurifier_Config
*/
protected $config;
/**
* Current instance of HTMLPurifier_Context.
* @type HTMLPurifier_Context
*/
protected $context;
/**
* @param HTMLPurifier_Token[] $tokens
* @param HTMLPurifier_Config $config
* @param HTMLPurifier_Context $context
* @return HTMLPurifier_Token[]
* @throws HTMLPurifier_Exception
*/
public function execute($tokens, $config, $context)
{
$definition = $config->getHTMLDefinition();
// local variables
$generator = new HTMLPurifier_Generator($config, $context);
$escape_invalid_tags = $config->get('Core.EscapeInvalidTags');
// used for autoclose early abortion
$global_parent_allowed_elements = $definition->info_parent_def->child->getAllowedElements($config);
$e = $context->get('ErrorCollector', true);
$i = false; // injector index
list($zipper, $token) = HTMLPurifier_Zipper::fromArray($tokens);
if ($token === NULL) {
return array();
}
$reprocess = false; // whether or not to reprocess the same token
$stack = array();
// member variables
$this->stack =& $stack;
$this->tokens =& $tokens;
$this->token =& $token;
$this->zipper =& $zipper;
$this->config = $config;
$this->context = $context;
// context variables
$context->register('CurrentNesting', $stack);
$context->register('InputZipper', $zipper);
$context->register('CurrentToken', $token);
// -- begin INJECTOR --
$this->injectors = array();
$injectors = $config->getBatch('AutoFormat');
$def_injectors = $definition->info_injector;
$custom_injectors = $injectors['Custom'];
unset($injectors['Custom']); // special case
foreach ($injectors as $injector => $b) {
// XXX: Fix with a legitimate lookup table of enabled filters
if (strpos($injector, '.') !== false) {
continue;
}
$injector = "HTMLPurifier_Injector_$injector";
if (!$b) {
continue;
}
$this->injectors[] = new $injector;
}
foreach ($def_injectors as $injector) {
// assumed to be objects
$this->injectors[] = $injector;
}
foreach ($custom_injectors as $injector) {
if (!$injector) {
continue;
}
if (is_string($injector)) {
$injector = "HTMLPurifier_Injector_$injector";
$injector = new $injector;
}
$this->injectors[] = $injector;
}
// give the injectors references to the definition and context
// variables for performance reasons
foreach ($this->injectors as $ix => $injector) {
$error = $injector->prepare($config, $context);
if (!$error) {
continue;
}
array_splice($this->injectors, $ix, 1); // rm the injector
trigger_error("Cannot enable {$injector->name} injector because $error is not allowed", E_USER_WARNING);
}
// -- end INJECTOR --
// a note on reprocessing:
// In order to reduce code duplication, whenever some code needs
// to make HTML changes in order to make things "correct", the
// new HTML gets sent through the purifier, regardless of its
// status. This means that if we add a start token, because it
// was totally necessary, we don't have to update nesting; we just
// punt ($reprocess = true; continue;) and it does that for us.
// isset is in loop because $tokens size changes during loop exec
for (;;
// only increment if we don't need to reprocess
$reprocess ? $reprocess = false : $token = $zipper->next($token)) {
// check for a rewind
if (is_int($i)) {
// possibility: disable rewinding if the current token has a
// rewind set on it already. This would offer protection from
// infinite loop, but might hinder some advanced rewinding.
$rewind_offset = $this->injectors[$i]->getRewindOffset();
if (is_int($rewind_offset)) {
for ($j = 0; $j < $rewind_offset; $j++) {
if (empty($zipper->front)) break;
$token = $zipper->prev($token);
// indicate that other injectors should not process this token,
// but we need to reprocess it
unset($token->skip[$i]);
$token->rewind = $i;
if ($token instanceof HTMLPurifier_Token_Start) {
array_pop($this->stack);
} elseif ($token instanceof HTMLPurifier_Token_End) {
$this->stack[] = $token->start;
}
}
}
$i = false;
}
// handle case of document end
if ($token === NULL) {
// kill processing if stack is empty
if (empty($this->stack)) {
break;
}
// peek
$top_nesting = array_pop($this->stack);
$this->stack[] = $top_nesting;
// send error [TagClosedSuppress]
if ($e && !isset($top_nesting->armor['MakeWellFormed_TagClosedError'])) {
$e->send(E_NOTICE, 'Strategy_MakeWellFormed: Tag closed by document end', $top_nesting);
}
// append, don't splice, since this is the end
$token = new HTMLPurifier_Token_End($top_nesting->name);
// punt!
$reprocess = true;
continue;
}
//echo ' '; printZipper($zipper, $token);//printTokens($this->stack);
//flush();
// quick-check: if it's not a tag, no need to process
if (empty($token->is_tag)) {
if ($token instanceof HTMLPurifier_Token_Text) {
foreach ($this->injectors as $i => $injector) {
if (isset($token->skip[$i])) {
continue;
}
if ($token->rewind !== null && $token->rewind !== $i) {
continue;
}
// XXX fuckup
$r = $token;
$injector->handleText($r);
$token = $this->processToken($r, $i);
$reprocess = true;
break;
}
}
// another possibility is a comment
continue;
}
if (isset($definition->info[$token->name])) {
$type = $definition->info[$token->name]->child->type;
} else {
$type = false; // Type is unknown, treat accordingly
}
// quick tag checks: anything that's *not* an end tag
$ok = false;
if ($type === 'empty' && $token instanceof HTMLPurifier_Token_Start) {
// claims to be a start tag but is empty
$token = new HTMLPurifier_Token_Empty(
$token->name,
$token->attr,
$token->line,
$token->col,
$token->armor
);
$ok = true;
} elseif ($type && $type !== 'empty' && $token instanceof HTMLPurifier_Token_Empty) {
// claims to be empty but really is a start tag
// NB: this assignment is required
$old_token = $token;
$token = new HTMLPurifier_Token_End($token->name);
$token = $this->insertBefore(
new HTMLPurifier_Token_Start($old_token->name, $old_token->attr, $old_token->line, $old_token->col, $old_token->armor)
);
// punt (since we had to modify the input stream in a non-trivial way)
$reprocess = true;
continue;
} elseif ($token instanceof HTMLPurifier_Token_Empty) {
// real empty token
$ok = true;
} elseif ($token instanceof HTMLPurifier_Token_Start) {
// start tag
// ...unless they also have to close their parent
if (!empty($this->stack)) {
// Performance note: you might think that it's rather
// inefficient, recalculating the autoclose information
// for every tag that a token closes (since when we
// do an autoclose, we push a new token into the
// stream and then /process/ that, before
// re-processing this token.) But this is
// necessary, because an injector can make an
// arbitrary transformations to the autoclosing
// tokens we introduce, so things may have changed
// in the meantime. Also, doing the inefficient thing is
// "easy" to reason about (for certain perverse definitions
// of "easy")
$parent = array_pop($this->stack);
$this->stack[] = $parent;
$parent_def = null;
$parent_elements = null;
$autoclose = false;
if (isset($definition->info[$parent->name])) {
$parent_def = $definition->info[$parent->name];
$parent_elements = $parent_def->child->getAllowedElements($config);
$autoclose = !isset($parent_elements[$token->name]);
}
if ($autoclose && $definition->info[$token->name]->wrap) {
// Check if an element can be wrapped by another
// element to make it valid in a context (for
// example,
needs a
in between)
$wrapname = $definition->info[$token->name]->wrap;
$wrapdef = $definition->info[$wrapname];
$elements = $wrapdef->child->getAllowedElements($config);
if (isset($elements[$token->name]) && isset($parent_elements[$wrapname])) {
$newtoken = new HTMLPurifier_Token_Start($wrapname);
$token = $this->insertBefore($newtoken);
$reprocess = true;
continue;
}
}
$carryover = false;
if ($autoclose && $parent_def->formatting) {
$carryover = true;
}
if ($autoclose) {
// check if this autoclose is doomed to fail
// (this rechecks $parent, which his harmless)
$autoclose_ok = isset($global_parent_allowed_elements[$token->name]);
if (!$autoclose_ok) {
foreach ($this->stack as $ancestor) {
$elements = $definition->info[$ancestor->name]->child->getAllowedElements($config);
if (isset($elements[$token->name])) {
$autoclose_ok = true;
break;
}
if ($definition->info[$token->name]->wrap) {
$wrapname = $definition->info[$token->name]->wrap;
$wrapdef = $definition->info[$wrapname];
$wrap_elements = $wrapdef->child->getAllowedElements($config);
if (isset($wrap_elements[$token->name]) && isset($elements[$wrapname])) {
$autoclose_ok = true;
break;
}
}
}
}
if ($autoclose_ok) {
// errors need to be updated
$new_token = new HTMLPurifier_Token_End($parent->name);
$new_token->start = $parent;
// [TagClosedSuppress]
if ($e && !isset($parent->armor['MakeWellFormed_TagClosedError'])) {
if (!$carryover) {
$e->send(E_NOTICE, 'Strategy_MakeWellFormed: Tag auto closed', $parent);
} else {
$e->send(E_NOTICE, 'Strategy_MakeWellFormed: Tag carryover', $parent);
}
}
if ($carryover) {
$element = clone $parent;
// [TagClosedAuto]
$element->armor['MakeWellFormed_TagClosedError'] = true;
$element->carryover = true;
$token = $this->processToken(array($new_token, $token, $element));
} else {
$token = $this->insertBefore($new_token);
}
} else {
$token = $this->remove();
}
$reprocess = true;
continue;
}
}
$ok = true;
}
if ($ok) {
foreach ($this->injectors as $i => $injector) {
if (isset($token->skip[$i])) {
continue;
}
if ($token->rewind !== null && $token->rewind !== $i) {
continue;
}
$r = $token;
$injector->handleElement($r);
$token = $this->processToken($r, $i);
$reprocess = true;
break;
}
if (!$reprocess) {
// ah, nothing interesting happened; do normal processing
if ($token instanceof HTMLPurifier_Token_Start) {
$this->stack[] = $token;
} elseif ($token instanceof HTMLPurifier_Token_End) {
throw new HTMLPurifier_Exception(
'Improper handling of end tag in start code; possible error in MakeWellFormed'
);
}
}
continue;
}
// sanity check: we should be dealing with a closing tag
if (!$token instanceof HTMLPurifier_Token_End) {
throw new HTMLPurifier_Exception('Unaccounted for tag token in input stream, bug in HTML Purifier');
}
// make sure that we have something open
if (empty($this->stack)) {
if ($escape_invalid_tags) {
if ($e) {
$e->send(E_WARNING, 'Strategy_MakeWellFormed: Unnecessary end tag to text');
}
$token = new HTMLPurifier_Token_Text($generator->generateFromToken($token));
} else {
if ($e) {
$e->send(E_WARNING, 'Strategy_MakeWellFormed: Unnecessary end tag removed');
}
$token = $this->remove();
}
$reprocess = true;
continue;
}
// first, check for the simplest case: everything closes neatly.
// Eventually, everything passes through here; if there are problems
// we modify the input stream accordingly and then punt, so that
// the tokens get processed again.
$current_parent = array_pop($this->stack);
if ($current_parent->name == $token->name) {
$token->start = $current_parent;
foreach ($this->injectors as $i => $injector) {
if (isset($token->skip[$i])) {
continue;
}
if ($token->rewind !== null && $token->rewind !== $i) {
continue;
}
$r = $token;
$injector->handleEnd($r);
$token = $this->processToken($r, $i);
$this->stack[] = $current_parent;
$reprocess = true;
break;
}
continue;
}
// okay, so we're trying to close the wrong tag
// undo the pop previous pop
$this->stack[] = $current_parent;
// scroll back the entire nest, trying to find our tag.
// (feature could be to specify how far you'd like to go)
$size = count($this->stack);
// -2 because -1 is the last element, but we already checked that
$skipped_tags = false;
for ($j = $size - 2; $j >= 0; $j--) {
if ($this->stack[$j]->name == $token->name) {
$skipped_tags = array_slice($this->stack, $j);
break;
}
}
// we didn't find the tag, so remove
if ($skipped_tags === false) {
if ($escape_invalid_tags) {
if ($e) {
$e->send(E_WARNING, 'Strategy_MakeWellFormed: Stray end tag to text');
}
$token = new HTMLPurifier_Token_Text($generator->generateFromToken($token));
} else {
if ($e) {
$e->send(E_WARNING, 'Strategy_MakeWellFormed: Stray end tag removed');
}
$token = $this->remove();
}
$reprocess = true;
continue;
}
// do errors, in REVERSE $j order: a,b,c with
$c = count($skipped_tags);
if ($e) {
for ($j = $c - 1; $j > 0; $j--) {
// notice we exclude $j == 0, i.e. the current ending tag, from
// the errors... [TagClosedSuppress]
if (!isset($skipped_tags[$j]->armor['MakeWellFormed_TagClosedError'])) {
$e->send(E_NOTICE, 'Strategy_MakeWellFormed: Tag closed by element end', $skipped_tags[$j]);
}
}
}
// insert tags, in FORWARD $j order: c,b,a with
$replace = array($token);
for ($j = 1; $j < $c; $j++) {
// ...as well as from the insertions
$new_token = new HTMLPurifier_Token_End($skipped_tags[$j]->name);
$new_token->start = $skipped_tags[$j];
array_unshift($replace, $new_token);
if (isset($definition->info[$new_token->name]) && $definition->info[$new_token->name]->formatting) {
// [TagClosedAuto]
$element = clone $skipped_tags[$j];
$element->carryover = true;
$element->armor['MakeWellFormed_TagClosedError'] = true;
$replace[] = $element;
}
}
$token = $this->processToken($replace);
$reprocess = true;
continue;
}
$context->destroy('CurrentToken');
$context->destroy('CurrentNesting');
$context->destroy('InputZipper');
unset($this->injectors, $this->stack, $this->tokens);
return $zipper->toArray($token);
}
/**
* Processes arbitrary token values for complicated substitution patterns.
* In general:
*
* If $token is an array, it is a list of tokens to substitute for the
* current token. These tokens then get individually processed. If there
* is a leading integer in the list, that integer determines how many
* tokens from the stream should be removed.
*
* If $token is a regular token, it is swapped with the current token.
*
* If $token is false, the current token is deleted.
*
* If $token is an integer, that number of tokens (with the first token
* being the current one) will be deleted.
*
* @param HTMLPurifier_Token|array|int|bool $token Token substitution value
* @param HTMLPurifier_Injector|int $injector Injector that performed the substitution; default is if
* this is not an injector related operation.
* @throws HTMLPurifier_Exception
*/
protected function processToken($token, $injector = -1)
{
// normalize forms of token
if (is_object($token)) {
$token = array(1, $token);
}
if (is_int($token)) {
$token = array($token);
}
if ($token === false) {
$token = array(1);
}
if (!is_array($token)) {
throw new HTMLPurifier_Exception('Invalid token type from injector');
}
if (!is_int($token[0])) {
array_unshift($token, 1);
}
if ($token[0] === 0) {
throw new HTMLPurifier_Exception('Deleting zero tokens is not valid');
}
// $token is now an array with the following form:
// array(number nodes to delete, new node 1, new node 2, ...)
$delete = array_shift($token);
list($old, $r) = $this->zipper->splice($this->token, $delete, $token);
if ($injector > -1) {
// determine appropriate skips
$oldskip = isset($old[0]) ? $old[0]->skip : array();
foreach ($token as $object) {
$object->skip = $oldskip;
$object->skip[$injector] = true;
}
}
return $r;
}
/**
* Inserts a token before the current token. Cursor now points to
* this token. You must reprocess after this.
* @param HTMLPurifier_Token $token
*/
private function insertBefore($token)
{
// NB not $this->zipper->insertBefore(), due to positioning
// differences
$splice = $this->zipper->splice($this->token, 0, array($token));
return $splice[1];
}
/**
* Removes current token. Cursor now points to new token occupying previously
* occupied space. You must reprocess after this.
*/
private function remove()
{
return $this->zipper->delete();
}
}
/**
* Removes all unrecognized tags from the list of tokens.
*
* This strategy iterates through all the tokens and removes unrecognized
* tokens. If a token is not recognized but a TagTransform is defined for
* that element, the element will be transformed accordingly.
*/
class HTMLPurifier_Strategy_RemoveForeignElements extends HTMLPurifier_Strategy
{
/**
* @param HTMLPurifier_Token[] $tokens
* @param HTMLPurifier_Config $config
* @param HTMLPurifier_Context $context
* @return array|HTMLPurifier_Token[]
*/
public function execute($tokens, $config, $context)
{
$definition = $config->getHTMLDefinition();
$generator = new HTMLPurifier_Generator($config, $context);
$result = array();
$escape_invalid_tags = $config->get('Core.EscapeInvalidTags');
$remove_invalid_img = $config->get('Core.RemoveInvalidImg');
// currently only used to determine if comments should be kept
$trusted = $config->get('HTML.Trusted');
$comment_lookup = $config->get('HTML.AllowedComments');
$comment_regexp = $config->get('HTML.AllowedCommentsRegexp');
$check_comments = $comment_lookup !== array() || $comment_regexp !== null;
$remove_script_contents = $config->get('Core.RemoveScriptContents');
$hidden_elements = $config->get('Core.HiddenElements');
// remove script contents compatibility
if ($remove_script_contents === true) {
$hidden_elements['script'] = true;
} elseif ($remove_script_contents === false && isset($hidden_elements['script'])) {
unset($hidden_elements['script']);
}
$attr_validator = new HTMLPurifier_AttrValidator();
// removes tokens until it reaches a closing tag with its value
$remove_until = false;
// converts comments into text tokens when this is equal to a tag name
$textify_comments = false;
$token = false;
$context->register('CurrentToken', $token);
$e = false;
if ($config->get('Core.CollectErrors')) {
$e =& $context->get('ErrorCollector');
}
foreach ($tokens as $token) {
if ($remove_until) {
if (empty($token->is_tag) || $token->name !== $remove_until) {
continue;
}
}
if (!empty($token->is_tag)) {
// DEFINITION CALL
// before any processing, try to transform the element
if (isset($definition->info_tag_transform[$token->name])) {
$original_name = $token->name;
// there is a transformation for this tag
// DEFINITION CALL
$token = $definition->
info_tag_transform[$token->name]->transform($token, $config, $context);
if ($e) {
$e->send(E_NOTICE, 'Strategy_RemoveForeignElements: Tag transform', $original_name);
}
}
if (isset($definition->info[$token->name])) {
// mostly everything's good, but
// we need to make sure required attributes are in order
if (($token instanceof HTMLPurifier_Token_Start || $token instanceof HTMLPurifier_Token_Empty) &&
$definition->info[$token->name]->required_attr &&
($token->name != 'img' || $remove_invalid_img) // ensure config option still works
) {
$attr_validator->validateToken($token, $config, $context);
$ok = true;
foreach ($definition->info[$token->name]->required_attr as $name) {
if (!isset($token->attr[$name])) {
$ok = false;
break;
}
}
if (!$ok) {
if ($e) {
$e->send(
E_ERROR,
'Strategy_RemoveForeignElements: Missing required attribute',
$name
);
}
continue;
}
$token->armor['ValidateAttributes'] = true;
}
if (isset($hidden_elements[$token->name]) && $token instanceof HTMLPurifier_Token_Start) {
$textify_comments = $token->name;
} elseif ($token->name === $textify_comments && $token instanceof HTMLPurifier_Token_End) {
$textify_comments = false;
}
} elseif ($escape_invalid_tags) {
// invalid tag, generate HTML representation and insert in
if ($e) {
$e->send(E_WARNING, 'Strategy_RemoveForeignElements: Foreign element to text');
}
$token = new HTMLPurifier_Token_Text(
$generator->generateFromToken($token)
);
} else {
// check if we need to destroy all of the tag's children
// CAN BE GENERICIZED
if (isset($hidden_elements[$token->name])) {
if ($token instanceof HTMLPurifier_Token_Start) {
$remove_until = $token->name;
} elseif ($token instanceof HTMLPurifier_Token_Empty) {
// do nothing: we're still looking
} else {
$remove_until = false;
}
if ($e) {
$e->send(E_ERROR, 'Strategy_RemoveForeignElements: Foreign meta element removed');
}
} else {
if ($e) {
$e->send(E_ERROR, 'Strategy_RemoveForeignElements: Foreign element removed');
}
}
continue;
}
} elseif ($token instanceof HTMLPurifier_Token_Comment) {
// textify comments in script tags when they are allowed
if ($textify_comments !== false) {
$data = $token->data;
$token = new HTMLPurifier_Token_Text($data);
} elseif ($trusted || $check_comments) {
// always cleanup comments
$trailing_hyphen = false;
if ($e) {
// perform check whether or not there's a trailing hyphen
if (substr($token->data, -1) == '-') {
$trailing_hyphen = true;
}
}
$token->data = rtrim($token->data, '-');
$found_double_hyphen = false;
while (strpos($token->data, '--') !== false) {
$found_double_hyphen = true;
$token->data = str_replace('--', '-', $token->data);
}
if ($trusted || !empty($comment_lookup[trim($token->data)]) ||
($comment_regexp !== null && preg_match($comment_regexp, trim($token->data)))) {
// OK good
if ($e) {
if ($trailing_hyphen) {
$e->send(
E_NOTICE,
'Strategy_RemoveForeignElements: Trailing hyphen in comment removed'
);
}
if ($found_double_hyphen) {
$e->send(E_NOTICE, 'Strategy_RemoveForeignElements: Hyphens in comment collapsed');
}
}
} else {
if ($e) {
$e->send(E_NOTICE, 'Strategy_RemoveForeignElements: Comment removed');
}
continue;
}
} else {
// strip comments
if ($e) {
$e->send(E_NOTICE, 'Strategy_RemoveForeignElements: Comment removed');
}
continue;
}
} elseif ($token instanceof HTMLPurifier_Token_Text) {
} else {
continue;
}
$result[] = $token;
}
if ($remove_until && $e) {
// we removed tokens until the end, throw error
$e->send(E_ERROR, 'Strategy_RemoveForeignElements: Token removed to end', $remove_until);
}
$context->destroy('CurrentToken');
return $result;
}
}
/**
* Validate all attributes in the tokens.
*/
class HTMLPurifier_Strategy_ValidateAttributes extends HTMLPurifier_Strategy
{
/**
* @param HTMLPurifier_Token[] $tokens
* @param HTMLPurifier_Config $config
* @param HTMLPurifier_Context $context
* @return HTMLPurifier_Token[]
*/
public function execute($tokens, $config, $context)
{
// setup validator
$validator = new HTMLPurifier_AttrValidator();
$token = false;
$context->register('CurrentToken', $token);
foreach ($tokens as $key => $token) {
// only process tokens that have attributes,
// namely start and empty tags
if (!$token instanceof HTMLPurifier_Token_Start && !$token instanceof HTMLPurifier_Token_Empty) {
continue;
}
// skip tokens that are armored
if (!empty($token->armor['ValidateAttributes'])) {
continue;
}
// note that we have no facilities here for removing tokens
$validator->validateToken($token, $config, $context);
}
$context->destroy('CurrentToken');
return $tokens;
}
}
/**
* Transforms FONT tags to the proper form (SPAN with CSS styling)
*
* This transformation takes the three proprietary attributes of FONT and
* transforms them into their corresponding CSS attributes. These are color,
* face, and size.
*
* @note Size is an interesting case because it doesn't map cleanly to CSS.
* Thanks to
* http://style.cleverchimp.com/font_size_intervals/altintervals.html
* for reasonable mappings.
* @warning This doesn't work completely correctly; specifically, this
* TagTransform operates before well-formedness is enforced, so
* the "active formatting elements" algorithm doesn't get applied.
*/
class HTMLPurifier_TagTransform_Font extends HTMLPurifier_TagTransform
{
/**
* @type string
*/
public $transform_to = 'span';
/**
* @type array
*/
protected $_size_lookup = array(
'0' => 'xx-small',
'1' => 'xx-small',
'2' => 'small',
'3' => 'medium',
'4' => 'large',
'5' => 'x-large',
'6' => 'xx-large',
'7' => '300%',
'-1' => 'smaller',
'-2' => '60%',
'+1' => 'larger',
'+2' => '150%',
'+3' => '200%',
'+4' => '300%'
);
/**
* @param HTMLPurifier_Token_Tag $tag
* @param HTMLPurifier_Config $config
* @param HTMLPurifier_Context $context
* @return HTMLPurifier_Token_End|string
*/
public function transform($tag, $config, $context)
{
if ($tag instanceof HTMLPurifier_Token_End) {
$new_tag = clone $tag;
$new_tag->name = $this->transform_to;
return $new_tag;
}
$attr = $tag->attr;
$prepend_style = '';
// handle color transform
if (isset($attr['color'])) {
$prepend_style .= 'color:' . $attr['color'] . ';';
unset($attr['color']);
}
// handle face transform
if (isset($attr['face'])) {
$prepend_style .= 'font-family:' . $attr['face'] . ';';
unset($attr['face']);
}
// handle size transform
if (isset($attr['size'])) {
// normalize large numbers
if ($attr['size'] !== '') {
if ($attr['size']{0} == '+' || $attr['size']{0} == '-') {
$size = (int)$attr['size'];
if ($size < -2) {
$attr['size'] = '-2';
}
if ($size > 4) {
$attr['size'] = '+4';
}
} else {
$size = (int)$attr['size'];
if ($size > 7) {
$attr['size'] = '7';
}
}
}
if (isset($this->_size_lookup[$attr['size']])) {
$prepend_style .= 'font-size:' .
$this->_size_lookup[$attr['size']] . ';';
}
unset($attr['size']);
}
if ($prepend_style) {
$attr['style'] = isset($attr['style']) ?
$prepend_style . $attr['style'] :
$prepend_style;
}
$new_tag = clone $tag;
$new_tag->name = $this->transform_to;
$new_tag->attr = $attr;
return $new_tag;
}
}
/**
* Simple transformation, just change tag name to something else,
* and possibly add some styling. This will cover most of the deprecated
* tag cases.
*/
class HTMLPurifier_TagTransform_Simple extends HTMLPurifier_TagTransform
{
/**
* @type string
*/
protected $style;
/**
* @param string $transform_to Tag name to transform to.
* @param string $style CSS style to add to the tag
*/
public function __construct($transform_to, $style = null)
{
$this->transform_to = $transform_to;
$this->style = $style;
}
/**
* @param HTMLPurifier_Token_Tag $tag
* @param HTMLPurifier_Config $config
* @param HTMLPurifier_Context $context
* @return string
*/
public function transform($tag, $config, $context)
{
$new_tag = clone $tag;
$new_tag->name = $this->transform_to;
if (!is_null($this->style) &&
($new_tag instanceof HTMLPurifier_Token_Start || $new_tag instanceof HTMLPurifier_Token_Empty)
) {
$this->prependCSS($new_tag->attr, $this->style);
}
return $new_tag;
}
}
/**
* Concrete comment token class. Generally will be ignored.
*/
class HTMLPurifier_Token_Comment extends HTMLPurifier_Token
{
/**
* Character data within comment.
* @type string
*/
public $data;
/**
* @type bool
*/
public $is_whitespace = true;
/**
* Transparent constructor.
*
* @param string $data String comment data.
* @param int $line
* @param int $col
*/
public function __construct($data, $line = null, $col = null)
{
$this->data = $data;
$this->line = $line;
$this->col = $col;
}
public function toNode() {
return new HTMLPurifier_Node_Comment($this->data, $this->line, $this->col);
}
}
/**
* Abstract class of a tag token (start, end or empty), and its behavior.
*/
abstract class HTMLPurifier_Token_Tag extends HTMLPurifier_Token
{
/**
* Static bool marker that indicates the class is a tag.
*
* This allows us to check objects with !empty($obj->is_tag)
* without having to use a function call is_a().
* @type bool
*/
public $is_tag = true;
/**
* The lower-case name of the tag, like 'a', 'b' or 'blockquote'.
*
* @note Strictly speaking, XML tags are case sensitive, so we shouldn't
* be lower-casing them, but these tokens cater to HTML tags, which are
* insensitive.
* @type string
*/
public $name;
/**
* Associative array of the tag's attributes.
* @type array
*/
public $attr = array();
/**
* Non-overloaded constructor, which lower-cases passed tag name.
*
* @param string $name String name.
* @param array $attr Associative array of attributes.
* @param int $line
* @param int $col
* @param array $armor
*/
public function __construct($name, $attr = array(), $line = null, $col = null, $armor = array())
{
$this->name = ctype_lower($name) ? $name : strtolower($name);
foreach ($attr as $key => $value) {
// normalization only necessary when key is not lowercase
if (!ctype_lower($key)) {
$new_key = strtolower($key);
if (!isset($attr[$new_key])) {
$attr[$new_key] = $attr[$key];
}
if ($new_key !== $key) {
unset($attr[$key]);
}
}
}
$this->attr = $attr;
$this->line = $line;
$this->col = $col;
$this->armor = $armor;
}
public function toNode() {
return new HTMLPurifier_Node_Element($this->name, $this->attr, $this->line, $this->col, $this->armor);
}
}
/**
* Concrete empty token class.
*/
class HTMLPurifier_Token_Empty extends HTMLPurifier_Token_Tag
{
public function toNode() {
$n = parent::toNode();
$n->empty = true;
return $n;
}
}
/**
* Concrete end token class.
*
* @warning This class accepts attributes even though end tags cannot. This
* is for optimization reasons, as under normal circumstances, the Lexers
* do not pass attributes.
*/
class HTMLPurifier_Token_End extends HTMLPurifier_Token_Tag
{
/**
* Token that started this node.
* Added by MakeWellFormed. Please do not edit this!
* @type HTMLPurifier_Token
*/
public $start;
public function toNode() {
throw new Exception("HTMLPurifier_Token_End->toNode not supported!");
}
}
/**
* Concrete start token class.
*/
class HTMLPurifier_Token_Start extends HTMLPurifier_Token_Tag
{
}
/**
* Concrete text token class.
*
* Text tokens comprise of regular parsed character data (PCDATA) and raw
* character data (from the CDATA sections). Internally, their
* data is parsed with all entities expanded. Surprisingly, the text token
* does have a "tag name" called #PCDATA, which is how the DTD represents it
* in permissible child nodes.
*/
class HTMLPurifier_Token_Text extends HTMLPurifier_Token
{
/**
* @type string
*/
public $name = '#PCDATA';
/**< PCDATA tag name compatible with DTD. */
/**
* @type string
*/
public $data;
/**< Parsed character data of text. */
/**
* @type bool
*/
public $is_whitespace;
/**< Bool indicating if node is whitespace. */
/**
* Constructor, accepts data and determines if it is whitespace.
* @param string $data String parsed character data.
* @param int $line
* @param int $col
*/
public function __construct($data, $line = null, $col = null)
{
$this->data = $data;
$this->is_whitespace = ctype_space($data);
$this->line = $line;
$this->col = $col;
}
public function toNode() {
return new HTMLPurifier_Node_Text($this->data, $this->is_whitespace, $this->line, $this->col);
}
}
class HTMLPurifier_URIFilter_DisableExternal extends HTMLPurifier_URIFilter
{
/**
* @type string
*/
public $name = 'DisableExternal';
/**
* @type array
*/
protected $ourHostParts = false;
/**
* @param HTMLPurifier_Config $config
* @return void
*/
public function prepare($config)
{
$our_host = $config->getDefinition('URI')->host;
if ($our_host !== null) {
$this->ourHostParts = array_reverse(explode('.', $our_host));
}
}
/**
* @param HTMLPurifier_URI $uri Reference
* @param HTMLPurifier_Config $config
* @param HTMLPurifier_Context $context
* @return bool
*/
public function filter(&$uri, $config, $context)
{
if (is_null($uri->host)) {
return true;
}
if ($this->ourHostParts === false) {
return false;
}
$host_parts = array_reverse(explode('.', $uri->host));
foreach ($this->ourHostParts as $i => $x) {
if (!isset($host_parts[$i])) {
return false;
}
if ($host_parts[$i] != $this->ourHostParts[$i]) {
return false;
}
}
return true;
}
}
class HTMLPurifier_URIFilter_DisableExternalResources extends HTMLPurifier_URIFilter_DisableExternal
{
/**
* @type string
*/
public $name = 'DisableExternalResources';
/**
* @param HTMLPurifier_URI $uri
* @param HTMLPurifier_Config $config
* @param HTMLPurifier_Context $context
* @return bool
*/
public function filter(&$uri, $config, $context)
{
if (!$context->get('EmbeddedURI', true)) {
return true;
}
return parent::filter($uri, $config, $context);
}
}
class HTMLPurifier_URIFilter_DisableResources extends HTMLPurifier_URIFilter
{
/**
* @type string
*/
public $name = 'DisableResources';
/**
* @param HTMLPurifier_URI $uri
* @param HTMLPurifier_Config $config
* @param HTMLPurifier_Context $context
* @return bool
*/
public function filter(&$uri, $config, $context)
{
return !$context->get('EmbeddedURI', true);
}
}
// It's not clear to me whether or not Punycode means that hostnames
// do not have canonical forms anymore. As far as I can tell, it's
// not a problem (punycoding should be identity when no Unicode
// points are involved), but I'm not 100% sure
class HTMLPurifier_URIFilter_HostBlacklist extends HTMLPurifier_URIFilter
{
/**
* @type string
*/
public $name = 'HostBlacklist';
/**
* @type array
*/
protected $blacklist = array();
/**
* @param HTMLPurifier_Config $config
* @return bool
*/
public function prepare($config)
{
$this->blacklist = $config->get('URI.HostBlacklist');
return true;
}
/**
* @param HTMLPurifier_URI $uri
* @param HTMLPurifier_Config $config
* @param HTMLPurifier_Context $context
* @return bool
*/
public function filter(&$uri, $config, $context)
{
foreach ($this->blacklist as $blacklisted_host_fragment) {
if (strpos($uri->host, $blacklisted_host_fragment) !== false) {
return false;
}
}
return true;
}
}
// does not support network paths
class HTMLPurifier_URIFilter_MakeAbsolute extends HTMLPurifier_URIFilter
{
/**
* @type string
*/
public $name = 'MakeAbsolute';
/**
* @type
*/
protected $base;
/**
* @type array
*/
protected $basePathStack = array();
/**
* @param HTMLPurifier_Config $config
* @return bool
*/
public function prepare($config)
{
$def = $config->getDefinition('URI');
$this->base = $def->base;
if (is_null($this->base)) {
trigger_error(
'URI.MakeAbsolute is being ignored due to lack of ' .
'value for URI.Base configuration',
E_USER_WARNING
);
return false;
}
$this->base->fragment = null; // fragment is invalid for base URI
$stack = explode('/', $this->base->path);
array_pop($stack); // discard last segment
$stack = $this->_collapseStack($stack); // do pre-parsing
$this->basePathStack = $stack;
return true;
}
/**
* @param HTMLPurifier_URI $uri
* @param HTMLPurifier_Config $config
* @param HTMLPurifier_Context $context
* @return bool
*/
public function filter(&$uri, $config, $context)
{
if (is_null($this->base)) {
return true;
} // abort early
if ($uri->path === '' && is_null($uri->scheme) &&
is_null($uri->host) && is_null($uri->query) && is_null($uri->fragment)) {
// reference to current document
$uri = clone $this->base;
return true;
}
if (!is_null($uri->scheme)) {
// absolute URI already: don't change
if (!is_null($uri->host)) {
return true;
}
$scheme_obj = $uri->getSchemeObj($config, $context);
if (!$scheme_obj) {
// scheme not recognized
return false;
}
if (!$scheme_obj->hierarchical) {
// non-hierarchal URI with explicit scheme, don't change
return true;
}
// special case: had a scheme but always is hierarchical and had no authority
}
if (!is_null($uri->host)) {
// network path, don't bother
return true;
}
if ($uri->path === '') {
$uri->path = $this->base->path;
} elseif ($uri->path[0] !== '/') {
// relative path, needs more complicated processing
$stack = explode('/', $uri->path);
$new_stack = array_merge($this->basePathStack, $stack);
if ($new_stack[0] !== '' && !is_null($this->base->host)) {
array_unshift($new_stack, '');
}
$new_stack = $this->_collapseStack($new_stack);
$uri->path = implode('/', $new_stack);
} else {
// absolute path, but still we should collapse
$uri->path = implode('/', $this->_collapseStack(explode('/', $uri->path)));
}
// re-combine
$uri->scheme = $this->base->scheme;
if (is_null($uri->userinfo)) {
$uri->userinfo = $this->base->userinfo;
}
if (is_null($uri->host)) {
$uri->host = $this->base->host;
}
if (is_null($uri->port)) {
$uri->port = $this->base->port;
}
return true;
}
/**
* Resolve dots and double-dots in a path stack
* @param array $stack
* @return array
*/
private function _collapseStack($stack)
{
$result = array();
$is_folder = false;
for ($i = 0; isset($stack[$i]); $i++) {
$is_folder = false;
// absorb an internally duplicated slash
if ($stack[$i] == '' && $i && isset($stack[$i + 1])) {
continue;
}
if ($stack[$i] == '..') {
if (!empty($result)) {
$segment = array_pop($result);
if ($segment === '' && empty($result)) {
// error case: attempted to back out too far:
// restore the leading slash
$result[] = '';
} elseif ($segment === '..') {
$result[] = '..'; // cannot remove .. with ..
}
} else {
// relative path, preserve the double-dots
$result[] = '..';
}
$is_folder = true;
continue;
}
if ($stack[$i] == '.') {
// silently absorb
$is_folder = true;
continue;
}
$result[] = $stack[$i];
}
if ($is_folder) {
$result[] = '';
}
return $result;
}
}
class HTMLPurifier_URIFilter_Munge extends HTMLPurifier_URIFilter
{
/**
* @type string
*/
public $name = 'Munge';
/**
* @type bool
*/
public $post = true;
/**
* @type string
*/
private $target;
/**
* @type HTMLPurifier_URIParser
*/
private $parser;
/**
* @type bool
*/
private $doEmbed;
/**
* @type string
*/
private $secretKey;
/**
* @type array
*/
protected $replace = array();
/**
* @param HTMLPurifier_Config $config
* @return bool
*/
public function prepare($config)
{
$this->target = $config->get('URI.' . $this->name);
$this->parser = new HTMLPurifier_URIParser();
$this->doEmbed = $config->get('URI.MungeResources');
$this->secretKey = $config->get('URI.MungeSecretKey');
if ($this->secretKey && !function_exists('hash_hmac')) {
throw new Exception("Cannot use %URI.MungeSecretKey without hash_hmac support.");
}
return true;
}
/**
* @param HTMLPurifier_URI $uri
* @param HTMLPurifier_Config $config
* @param HTMLPurifier_Context $context
* @return bool
*/
public function filter(&$uri, $config, $context)
{
if ($context->get('EmbeddedURI', true) && !$this->doEmbed) {
return true;
}
$scheme_obj = $uri->getSchemeObj($config, $context);
if (!$scheme_obj) {
return true;
} // ignore unknown schemes, maybe another postfilter did it
if (!$scheme_obj->browsable) {
return true;
} // ignore non-browseable schemes, since we can't munge those in a reasonable way
if ($uri->isBenign($config, $context)) {
return true;
} // don't redirect if a benign URL
$this->makeReplace($uri, $config, $context);
$this->replace = array_map('rawurlencode', $this->replace);
$new_uri = strtr($this->target, $this->replace);
$new_uri = $this->parser->parse($new_uri);
// don't redirect if the target host is the same as the
// starting host
if ($uri->host === $new_uri->host) {
return true;
}
$uri = $new_uri; // overwrite
return true;
}
/**
* @param HTMLPurifier_URI $uri
* @param HTMLPurifier_Config $config
* @param HTMLPurifier_Context $context
*/
protected function makeReplace($uri, $config, $context)
{
$string = $uri->toString();
// always available
$this->replace['%s'] = $string;
$this->replace['%r'] = $context->get('EmbeddedURI', true);
$token = $context->get('CurrentToken', true);
$this->replace['%n'] = $token ? $token->name : null;
$this->replace['%m'] = $context->get('CurrentAttr', true);
$this->replace['%p'] = $context->get('CurrentCSSProperty', true);
// not always available
if ($this->secretKey) {
$this->replace['%t'] = hash_hmac("sha256", $string, $this->secretKey);
}
}
}
/**
* Implements safety checks for safe iframes.
*
* @warning This filter is *critical* for ensuring that %HTML.SafeIframe
* works safely.
*/
class HTMLPurifier_URIFilter_SafeIframe extends HTMLPurifier_URIFilter
{
/**
* @type string
*/
public $name = 'SafeIframe';
/**
* @type bool
*/
public $always_load = true;
/**
* @type string
*/
protected $regexp = null;
// XXX: The not so good bit about how this is all set up now is we
// can't check HTML.SafeIframe in the 'prepare' step: we have to
// defer till the actual filtering.
/**
* @param HTMLPurifier_Config $config
* @return bool
*/
public function prepare($config)
{
$this->regexp = $config->get('URI.SafeIframeRegexp');
return true;
}
/**
* @param HTMLPurifier_URI $uri
* @param HTMLPurifier_Config $config
* @param HTMLPurifier_Context $context
* @return bool
*/
public function filter(&$uri, $config, $context)
{
// check if filter not applicable
if (!$config->get('HTML.SafeIframe')) {
return true;
}
// check if the filter should actually trigger
if (!$context->get('EmbeddedURI', true)) {
return true;
}
$token = $context->get('CurrentToken', true);
if (!($token && $token->name == 'iframe')) {
return true;
}
// check if we actually have some whitelists enabled
if ($this->regexp === null) {
return false;
}
// actually check the whitelists
return preg_match($this->regexp, $uri->toString());
}
}
/**
* Implements data: URI for base64 encoded images supported by GD.
*/
class HTMLPurifier_URIScheme_data extends HTMLPurifier_URIScheme
{
/**
* @type bool
*/
public $browsable = true;
/**
* @type array
*/
public $allowed_types = array(
// you better write validation code for other types if you
// decide to allow them
'image/jpeg' => true,
'image/gif' => true,
'image/png' => true,
);
// this is actually irrelevant since we only write out the path
// component
/**
* @type bool
*/
public $may_omit_host = true;
/**
* @param HTMLPurifier_URI $uri
* @param HTMLPurifier_Config $config
* @param HTMLPurifier_Context $context
* @return bool
*/
public function doValidate(&$uri, $config, $context)
{
$result = explode(',', $uri->path, 2);
$is_base64 = false;
$charset = null;
$content_type = null;
if (count($result) == 2) {
list($metadata, $data) = $result;
// do some legwork on the metadata
$metas = explode(';', $metadata);
while (!empty($metas)) {
$cur = array_shift($metas);
if ($cur == 'base64') {
$is_base64 = true;
break;
}
if (substr($cur, 0, 8) == 'charset=') {
// doesn't match if there are arbitrary spaces, but
// whatever dude
if ($charset !== null) {
continue;
} // garbage
$charset = substr($cur, 8); // not used
} else {
if ($content_type !== null) {
continue;
} // garbage
$content_type = $cur;
}
}
} else {
$data = $result[0];
}
if ($content_type !== null && empty($this->allowed_types[$content_type])) {
return false;
}
if ($charset !== null) {
// error; we don't allow plaintext stuff
$charset = null;
}
$data = rawurldecode($data);
if ($is_base64) {
$raw_data = base64_decode($data);
} else {
$raw_data = $data;
}
// XXX probably want to refactor this into a general mechanism
// for filtering arbitrary content types
$file = tempnam("/tmp", "");
file_put_contents($file, $raw_data);
if (function_exists('exif_imagetype')) {
$image_code = exif_imagetype($file);
unlink($file);
} elseif (function_exists('getimagesize')) {
set_error_handler(array($this, 'muteErrorHandler'));
$info = getimagesize($file);
restore_error_handler();
unlink($file);
if ($info == false) {
return false;
}
$image_code = $info[2];
} else {
trigger_error("could not find exif_imagetype or getimagesize functions", E_USER_ERROR);
}
$real_content_type = image_type_to_mime_type($image_code);
if ($real_content_type != $content_type) {
// we're nice guys; if the content type is something else we
// support, change it over
if (empty($this->allowed_types[$real_content_type])) {
return false;
}
$content_type = $real_content_type;
}
// ok, it's kosher, rewrite what we need
$uri->userinfo = null;
$uri->host = null;
$uri->port = null;
$uri->fragment = null;
$uri->query = null;
$uri->path = "$content_type;base64," . base64_encode($raw_data);
return true;
}
/**
* @param int $errno
* @param string $errstr
*/
public function muteErrorHandler($errno, $errstr)
{
}
}
/**
* Validates file as defined by RFC 1630 and RFC 1738.
*/
class HTMLPurifier_URIScheme_file extends HTMLPurifier_URIScheme
{
/**
* Generally file:// URLs are not accessible from most
* machines, so placing them as an img src is incorrect.
* @type bool
*/
public $browsable = false;
/**
* Basically the *only* URI scheme for which this is true, since
* accessing files on the local machine is very common. In fact,
* browsers on some operating systems don't understand the
* authority, though I hear it is used on Windows to refer to
* network shares.
* @type bool
*/
public $may_omit_host = true;
/**
* @param HTMLPurifier_URI $uri
* @param HTMLPurifier_Config $config
* @param HTMLPurifier_Context $context
* @return bool
*/
public function doValidate(&$uri, $config, $context)
{
// Authentication method is not supported
$uri->userinfo = null;
// file:// makes no provisions for accessing the resource
$uri->port = null;
// While it seems to work on Firefox, the querystring has
// no possible effect and is thus stripped.
$uri->query = null;
return true;
}
}
/**
* Validates ftp (File Transfer Protocol) URIs as defined by generic RFC 1738.
*/
class HTMLPurifier_URIScheme_ftp extends HTMLPurifier_URIScheme
{
/**
* @type int
*/
public $default_port = 21;
/**
* @type bool
*/
public $browsable = true; // usually
/**
* @type bool
*/
public $hierarchical = true;
/**
* @param HTMLPurifier_URI $uri
* @param HTMLPurifier_Config $config
* @param HTMLPurifier_Context $context
* @return bool
*/
public function doValidate(&$uri, $config, $context)
{
$uri->query = null;
// typecode check
$semicolon_pos = strrpos($uri->path, ';'); // reverse
if ($semicolon_pos !== false) {
$type = substr($uri->path, $semicolon_pos + 1); // no semicolon
$uri->path = substr($uri->path, 0, $semicolon_pos);
$type_ret = '';
if (strpos($type, '=') !== false) {
// figure out whether or not the declaration is correct
list($key, $typecode) = explode('=', $type, 2);
if ($key !== 'type') {
// invalid key, tack it back on encoded
$uri->path .= '%3B' . $type;
} elseif ($typecode === 'a' || $typecode === 'i' || $typecode === 'd') {
$type_ret = ";type=$typecode";
}
} else {
$uri->path .= '%3B' . $type;
}
$uri->path = str_replace(';', '%3B', $uri->path);
$uri->path .= $type_ret;
}
return true;
}
}
/**
* Validates http (HyperText Transfer Protocol) as defined by RFC 2616
*/
class HTMLPurifier_URIScheme_http extends HTMLPurifier_URIScheme
{
/**
* @type int
*/
public $default_port = 80;
/**
* @type bool
*/
public $browsable = true;
/**
* @type bool
*/
public $hierarchical = true;
/**
* @param HTMLPurifier_URI $uri
* @param HTMLPurifier_Config $config
* @param HTMLPurifier_Context $context
* @return bool
*/
public function doValidate(&$uri, $config, $context)
{
$uri->userinfo = null;
return true;
}
}
/**
* Validates https (Secure HTTP) according to http scheme.
*/
class HTMLPurifier_URIScheme_https extends HTMLPurifier_URIScheme_http
{
/**
* @type int
*/
public $default_port = 443;
/**
* @type bool
*/
public $secure = true;
}
// VERY RELAXED! Shouldn't cause problems, not even Firefox checks if the
// email is valid, but be careful!
/**
* Validates mailto (for E-mail) according to RFC 2368
* @todo Validate the email address
* @todo Filter allowed query parameters
*/
class HTMLPurifier_URIScheme_mailto extends HTMLPurifier_URIScheme
{
/**
* @type bool
*/
public $browsable = false;
/**
* @type bool
*/
public $may_omit_host = true;
/**
* @param HTMLPurifier_URI $uri
* @param HTMLPurifier_Config $config
* @param HTMLPurifier_Context $context
* @return bool
*/
public function doValidate(&$uri, $config, $context)
{
$uri->userinfo = null;
$uri->host = null;
$uri->port = null;
// we need to validate path against RFC 2368's addr-spec
return true;
}
}
/**
* Validates news (Usenet) as defined by generic RFC 1738
*/
class HTMLPurifier_URIScheme_news extends HTMLPurifier_URIScheme
{
/**
* @type bool
*/
public $browsable = false;
/**
* @type bool
*/
public $may_omit_host = true;
/**
* @param HTMLPurifier_URI $uri
* @param HTMLPurifier_Config $config
* @param HTMLPurifier_Context $context
* @return bool
*/
public function doValidate(&$uri, $config, $context)
{
$uri->userinfo = null;
$uri->host = null;
$uri->port = null;
$uri->query = null;
// typecode check needed on path
return true;
}
}
/**
* Validates nntp (Network News Transfer Protocol) as defined by generic RFC 1738
*/
class HTMLPurifier_URIScheme_nntp extends HTMLPurifier_URIScheme
{
/**
* @type int
*/
public $default_port = 119;
/**
* @type bool
*/
public $browsable = false;
/**
* @param HTMLPurifier_URI $uri
* @param HTMLPurifier_Config $config
* @param HTMLPurifier_Context $context
* @return bool
*/
public function doValidate(&$uri, $config, $context)
{
$uri->userinfo = null;
$uri->query = null;
return true;
}
}
/**
* Performs safe variable parsing based on types which can be used by
* users. This may not be able to represent all possible data inputs,
* however.
*/
class HTMLPurifier_VarParser_Flexible extends HTMLPurifier_VarParser
{
/**
* @param mixed $var
* @param int $type
* @param bool $allow_null
* @return array|bool|float|int|mixed|null|string
* @throws HTMLPurifier_VarParserException
*/
protected function parseImplementation($var, $type, $allow_null)
{
if ($allow_null && $var === null) {
return null;
}
switch ($type) {
// Note: if code "breaks" from the switch, it triggers a generic
// exception to be thrown. Specific errors can be specifically
// done here.
case self::MIXED:
case self::ISTRING:
case self::STRING:
case self::TEXT:
case self::ITEXT:
return $var;
case self::INT:
if (is_string($var) && ctype_digit($var)) {
$var = (int)$var;
}
return $var;
case self::FLOAT:
if ((is_string($var) && is_numeric($var)) || is_int($var)) {
$var = (float)$var;
}
return $var;
case self::BOOL:
if (is_int($var) && ($var === 0 || $var === 1)) {
$var = (bool)$var;
} elseif (is_string($var)) {
if ($var == 'on' || $var == 'true' || $var == '1') {
$var = true;
} elseif ($var == 'off' || $var == 'false' || $var == '0') {
$var = false;
} else {
throw new HTMLPurifier_VarParserException("Unrecognized value '$var' for $type");
}
}
return $var;
case self::ALIST:
case self::HASH:
case self::LOOKUP:
if (is_string($var)) {
// special case: technically, this is an array with
// a single empty string item, but having an empty
// array is more intuitive
if ($var == '') {
return array();
}
if (strpos($var, "\n") === false && strpos($var, "\r") === false) {
// simplistic string to array method that only works
// for simple lists of tag names or alphanumeric characters
$var = explode(',', $var);
} else {
$var = preg_split('/(,|[\n\r]+)/', $var);
}
// remove spaces
foreach ($var as $i => $j) {
$var[$i] = trim($j);
}
if ($type === self::HASH) {
// key:value,key2:value2
$nvar = array();
foreach ($var as $keypair) {
$c = explode(':', $keypair, 2);
if (!isset($c[1])) {
continue;
}
$nvar[trim($c[0])] = trim($c[1]);
}
$var = $nvar;
}
}
if (!is_array($var)) {
break;
}
$keys = array_keys($var);
if ($keys === array_keys($keys)) {
if ($type == self::ALIST) {
return $var;
} elseif ($type == self::LOOKUP) {
$new = array();
foreach ($var as $key) {
$new[$key] = true;
}
return $new;
} else {
break;
}
}
if ($type === self::ALIST) {
trigger_error("Array list did not have consecutive integer indexes", E_USER_WARNING);
return array_values($var);
}
if ($type === self::LOOKUP) {
foreach ($var as $key => $value) {
if ($value !== true) {
trigger_error(
"Lookup array has non-true value at key '$key'; " .
"maybe your input array was not indexed numerically",
E_USER_WARNING
);
}
$var[$key] = true;
}
}
return $var;
default:
$this->errorInconsistent(__CLASS__, $type);
}
$this->errorGeneric($var, $type);
}
}
/**
* This variable parser uses PHP's internal code engine. Because it does
* this, it can represent all inputs; however, it is dangerous and cannot
* be used by users.
*/
class HTMLPurifier_VarParser_Native extends HTMLPurifier_VarParser
{
/**
* @param mixed $var
* @param int $type
* @param bool $allow_null
* @return null|string
*/
protected function parseImplementation($var, $type, $allow_null)
{
return $this->evalExpression($var);
}
/**
* @param string $expr
* @return mixed
* @throws HTMLPurifier_VarParserException
*/
protected function evalExpression($expr)
{
$var = null;
$result = eval("\$var = $expr;");
if ($result === false) {
throw new HTMLPurifier_VarParserException("Fatal error in evaluated code");
}
return $var;
}
}
Collabtive-2.0/include/SmartyPaginate.class.php 0000664 0000000 0000000 00000031425 12372520637 0021643 0 ustar 00root root 0000000 0000000
* @package SmartyPaginate
* @version 1.6
*/
class SmartyPaginate
{
/**
* Class Constructor
*/
function SmartyPaginate()
{
}
/**
* initialize the session data
*
* @param string $id the pagination id
* @param string $formvar the variable containing submitted pagination information
*/
static function connect($id = 'default', $formvar = null)
{
if (!isset($_SESSION['SmartyPaginate'][$id]))
{
SmartyPaginate::reset($id);
}
// use $_GET by default unless otherwise specified
$_formvar = isset($formvar) ? $formvar : $_GET;
// set the current page
$_total = SmartyPaginate::getTotal($id);
if (isset($_formvar[SmartyPaginate::getUrlVar($id)]) && $_formvar[SmartyPaginate::getUrlVar($id)] > 0 && (!isset($_total) || $_formvar[SmartyPaginate::getUrlVar($id)] <= $_total))
$_SESSION['SmartyPaginate'][$id]['current_item'] = $_formvar[$_SESSION['SmartyPaginate'][$id]['urlvar']];
}
/**
* see if session has been initialized
*
* @param string $id the pagination id
*/
function isConnected($id = 'default')
{
return isset($_SESSION['SmartyPaginate'][$id]);
}
/**
* reset/init the session data
*
* @param string $id the pagination id
*/
static function reset($id = 'default')
{
$_SESSION['SmartyPaginate'][$id] = array('item_limit' => 10,
'item_total' => null,
'current_item' => 1,
'urlvar' => 'next',
'url' => full_url(),
'prev_text' => 'prev',
'next_text' => 'next',
'first_text' => 'first',
'last_text' => 'last'
);
}
/**
* clear the SmartyPaginate session data
*
* @param string $id the pagination id
*/
static function disconnect($id = null)
{
if (isset($id))
unset($_SESSION['SmartyPaginate'][$id]);
else
unset($_SESSION['SmartyPaginate']);
}
/**
* set maximum number of items per page
*
* @param string $id the pagination id
*/
static function setLimit($limit, $id = 'default')
{
if (!preg_match('!^\d+$!', $limit))
{
trigger_error('SmartyPaginate setLimit: limit must be an integer.');
return false;
}
settype($limit, 'integer');
if ($limit < 1)
{
trigger_error('SmartyPaginate setLimit: limit must be greater than zero.');
return false;
}
$_SESSION['SmartyPaginate'][$id]['item_limit'] = $limit;
}
/**
* get maximum number of items per page
*
* @param string $id the pagination id
*/
static function getLimit($id = 'default')
{
return $_SESSION['SmartyPaginate'][$id]['item_limit'];
}
/**
* set the total number of items
*
* @param int $total the total number of items
* @param string $id the pagination id
*/
static function setTotal($total, $id = 'default')
{
if (!preg_match('!^\d+$!', $total))
{
trigger_error('SmartyPaginate setTotal: total must be an integer.');
return false;
}
settype($total, 'integer');
if ($total < 0)
{
trigger_error('SmartyPaginate setTotal: total must be positive.');
return false;
}
$_SESSION['SmartyPaginate'][$id]['item_total'] = $total;
}
/**
* get the total number of items
*
* @param string $id the pagination id
*/
static function getTotal($id = 'default')
{
return $_SESSION['SmartyPaginate'][$id]['item_total'];
}
/**
* set the url used in the links, default is $PHP_SELF
*
* @param string $url the pagination url
* @param string $id the pagination id
*/
static function setUrl($url, $id = 'default')
{
$_SESSION['SmartyPaginate'][$id]['url'] = $url;
}
/**
* get the url variable
*
* @param string $id the pagination id
*/
static function getUrl($id = 'default')
{
return $_SESSION['SmartyPaginate'][$id]['url'];
}
/**
* set the url variable ie. ?next=10
* ^^^^
*
* @param string $url url pagination varname
* @param string $id the pagination id
*/
static function setUrlVar($urlvar, $id = 'default')
{
$_SESSION['SmartyPaginate'][$id]['urlvar'] = $urlvar;
}
/**
* get the url variable
*
* @param string $id the pagination id
*/
static function getUrlVar($id = 'default')
{
return $_SESSION['SmartyPaginate'][$id]['urlvar'];
}
/**
* set the current item (usually done automatically by next/prev links)
*
* @param int $item index of the current item
* @param string $id the pagination id
*/
static function setCurrentItem($item, $id = 'default')
{
$_SESSION['SmartyPaginate'][$id]['current_item'] = $item;
}
/**
* get the current item
*
* @param string $id the pagination id
*/
static function getCurrentItem($id = 'default')
{
return $_SESSION['SmartyPaginate'][$id]['current_item'];
}
/**
* get the current item index
*
* @param string $id the pagination id
*/
static function getCurrentIndex($id = 'default')
{
return $_SESSION['SmartyPaginate'][$id]['current_item'] - 1;
}
/**
* get the last displayed item
*
* @param string $id the pagination id
*/
static function getLastItem($id = 'default')
{
$_total = SmartyPaginate::getTotal($id);
$_limit = SmartyPaginate::getLimit($id);
$_last = SmartyPaginate::getCurrentItem($id) + $_limit - 1;
return ($_last <= $_total) ? $_last : $_total;
}
/**
* assign $paginate var values
*
* @param obj $ &$smarty the smarty object reference
* @param string $var the name of the assigned var
* @param string $id the pagination id
*/
static function assign(&$smarty, $var = 'paginate', $id = 'default')
{
if (is_object($smarty) && (strtolower(get_class($smarty)) == 'smarty' || is_subclass_of($smarty, 'smarty')))
{
$_paginate['total'] = SmartyPaginate::getTotal($id);
$_paginate['first'] = SmartyPaginate::getCurrentItem($id);
$_paginate['last'] = SmartyPaginate::getLastItem($id);
$_paginate['page_current'] = ceil(SmartyPaginate::getLastItem($id) / SmartyPaginate::getLimit($id));
$_paginate['page_total'] = ceil(SmartyPaginate::getTotal($id) / SmartyPaginate::getLimit($id));
$_paginate['size'] = $_paginate['last'] - $_paginate['first'];
$_paginate['url'] = SmartyPaginate::getUrl($id);
$_paginate['urlvar'] = SmartyPaginate::getUrlVar($id);
$_paginate['current_item'] = SmartyPaginate::getCurrentItem($id);
// $_paginate['prev_text'] = SmartyPaginate::getPrevText($id);
// $_paginate['next_text'] = SmartyPaginate::getNextText($id);
$_paginate['prev_text'] = "<<";
$_paginate['next_text'] = ">>";
$_paginate['limit'] = SmartyPaginate::getLimit($id);
$_item = 1;
$_page = 1;
while ($_item <= $_paginate['total'])
{
$_paginate['page'][$_page]['number'] = $_page;
$_paginate['page'][$_page]['item_start'] = $_item;
$_paginate['page'][$_page]['item_end'] = ($_item + $_paginate['limit'] - 1 <= $_paginate['total']) ? $_item + $_paginate['limit'] - 1 : $_paginate['total'];
$_paginate['page'][$_page]['is_current'] = ($_item == $_paginate['current_item']);
$_item += $_paginate['limit'];
$_page++;
}
$smarty->assign($var, $_paginate);
}
else
{
trigger_error("SmartyPaginate: [assign] I need a valid Smarty object.");
return false;
}
}
/**
* set the default text for the "previous" page link
*
* @param string $text index of the current item
* @param string $id the pagination id
*/
static function setPrevText($text, $id = 'default')
{
$_SESSION['SmartyPaginate'][$id]['prev_text'] = $text;
}
/**
* get the default text for the "previous" page link
*
* @param string $id the pagination id
*/
static function getPrevText($id = 'default')
{
return $_SESSION['SmartyPaginate'][$id]['prev_text'];
}
/**
* set the text for the "next" page link
*
* @param string $text index of the current item
* @param string $id the pagination id
*/
static function setNextText($text, $id = 'default')
{
$_SESSION['SmartyPaginate'][$id]['next_text'] = $text;
}
/**
* get the default text for the "next" page link
*
* @param string $id the pagination id
*/
static function getNextText($id = 'default')
{
return $_SESSION['SmartyPaginate'][$id]['next_text'];
}
/**
* set the text for the "first" page link
*
* @param string $text index of the current item
* @param string $id the pagination id
*/
static function setFirstText($text, $id = 'default')
{
$_SESSION['SmartyPaginate'][$id]['first_text'] = $text;
}
/**
* get the default text for the "first" page link
*
* @param string $id the pagination id
*/
static function getFirstText($id = 'default')
{
return $_SESSION['SmartyPaginate'][$id]['first_text'];
}
/**
* set the text for the "last" page link
*
* @param string $text index of the current item
* @param string $id the pagination id
*/
static function setLastText($text, $id = 'default')
{
$_SESSION['SmartyPaginate'][$id]['last_text'] = $text;
}
/**
* get the default text for the "last" page link
*
* @param string $id the pagination id
*/
static function getLastText($id = 'default')
{
return $_SESSION['SmartyPaginate'][$id]['last_text'];
}
/**
* set default number of page groupings in {paginate_middle}
*
* @param string $id the pagination id
*/
static function setPageLimit($limit, $id = 'default')
{
if (!preg_match('!^\d+$!', $limit))
{
trigger_error('SmartyPaginate setPageLimit: limit must be an integer.');
return false;
}
settype($limit, 'integer');
if ($limit < 1)
{
trigger_error('SmartyPaginate setPageLimit: limit must be greater than zero.');
return false;
}
$_SESSION['SmartyPaginate'][$id]['page_limit'] = $limit;
}
/**
* get default number of page groupings in {paginate_middle}
*
* @param string $id the pagination id
*/
static function getPageLimit($id = 'default')
{
return $_SESSION['SmartyPaginate'][$id]['page_limit'];
}
/**
* get the previous page of items
*
* @param string $id the pagination id
*/
static function _getPrevPageItem($id = 'default')
{
$_prev_item = $_SESSION['SmartyPaginate'][$id]['current_item'] - $_SESSION['SmartyPaginate'][$id]['item_limit'];
return ($_prev_item > 0) ? $_prev_item : false;
}
/**
* get the previous page of items
*
* @param string $id the pagination id
*/
static function _getNextPageItem($id = 'default')
{
$_next_item = $_SESSION['SmartyPaginate'][$id]['current_item'] + $_SESSION['SmartyPaginate'][$id]['item_limit'];
return ($_next_item <= $_SESSION['SmartyPaginate'][$id]['item_total']) ? $_next_item : false;
}
}
?>
Collabtive-2.0/include/class.MYPDF.php 0000664 0000000 0000000 00000006046 12372520637 0017573 0 ustar 00root root 0000000 0000000
* @name MYPDF
* @version 1.0
* @package Collabtive
* @link http://www.o-dyn.de
* @license http://opensource.org/licenses/gpl-license.php GNU General Public License v3 or later
*/
class MYPDF extends TCPDF {
// String for the header
private $headerName;
// cellFill is expected to be an array containing a RGB triplet.
private $cellFill;
private $headerMargin = 20;
public function setup($headerName = "", array $cellFill = array())
{
// TCPDF boilerplate setup
$this->SetMargins(15, $this->headerMargin, 15);
$this->SetFooterMargin(PDF_MARGIN_FOOTER);
$this->SetFont(PDF_FONT_NAME_DATA, "", 11);
$this->SetAutoPageBreak(true, PDF_MARGIN_FOOTER);
$this->getAliasNbPages();
$this->AddPage();
// Set string for display above table
$this->headerName = $headerName;
// Set colored fill value for rows. cellFill is expected to be an array containing a RGB triplet. Default alternate is white.
$this->cellFill = $cellFill;
$this->Header();
}
public function Header()
{
// If header name is set print it out
if ($this->headerName) {
// make it big and bold
$this->setFontSize(22);
$this->SetFont('', 'B');
$this->Cell(0, 0, $this->headerName, 0, 1, "L", 0);
$this->headerMargin = $this->GetY() + 10;
// $this->Cell(0, 0, "", 0, 1, "L", 0);
}
}
public function table($header, $data)
{
// font restoration
$this->setFontSize(12);
$this->SetFillColor(255, 255, 255);
$this->SetTextColor(0);
$this->SetLineWidth(0.3);
$this->SetFont('', 'B');
// Calculate Headers
$num_headers = count($header);
$awidth = floor(180 / $num_headers);
for($i = 0; $i < $num_headers; $i++) {
if ($i > 0) {
$this->Cell($awidth, 7, $header[$i], 1, 0, 'C', 1, "", 1);
} else {
$this->Cell($awidth + 5, 7, $header[$i], 1, 0, 'C', 1, "", 1);
}
}
$this->Ln();
// Color and font restoration
if (!empty($this->cellFill)) {
$this->SetFillColor($this->cellFill[0], $this->cellFill[1], $this->cellFill[2]);
} else {
$this->SetFillColor(224, 235, 255);
}
$this->SetFont('');
$doFill = false;
// Loop through data array and for each line, draw cells according to the header count
foreach($data as $row) {
for($i = 0;$i < $num_headers;$i++) {
if ($i > 0) {
$this->Cell($awidth, 6, $row[$i], 1, 0, 'LR', $doFill, "", 1);
} else {
$this->Cell($awidth + 5, 6, $row[$i], 1, 0, 'LR', $doFill, "", 1);
}
}
// Reverse the value of dofill
$doFill = !$doFill;
$this->Ln();
}
$this->Cell(180, 0, '', 'T');
}
}
?> Collabtive-2.0/include/class.PHPmailer.php 0000664 0000000 0000000 00000155770 12372520637 0020546 0 ustar 00root root 0000000 0000000 ContentType = 'text/html';
} else {
$this->ContentType = 'text/plain';
}
}
/**
* Sets Mailer to send message using SMTP.
* @return void
*/
public function IsSMTP() {
$this->Mailer = 'smtp';
}
/**
* Sets Mailer to send message using PHP mail() function.
* @return void
*/
public function IsMail() {
$this->Mailer = 'mail';
}
/**
* Sets Mailer to send message using the $Sendmail program.
* @return void
*/
public function IsSendmail() {
$this->Mailer = 'sendmail';
}
/**
* Sets Mailer to send message using the qmail MTA.
* @return void
*/
public function IsQmail() {
$this->Sendmail = '/var/qmail/bin/sendmail';
$this->Mailer = 'sendmail';
}
/////////////////////////////////////////////////
// METHODS, RECIPIENTS
/////////////////////////////////////////////////
/**
* Adds a "To" address.
* @param string $address
* @param string $name
* @return void
*/
public function AddAddress($address, $name = '') {
$cur = count($this->to);
$this->to[$cur][0] = trim($address);
$this->to[$cur][1] = $name;
}
/**
* Adds a "Cc" address. Note: this function works
* with the SMTP mailer on win32, not with the "mail"
* mailer.
* @param string $address
* @param string $name
* @return void
*/
public function AddCC($address, $name = '') {
$cur = count($this->cc);
$this->cc[$cur][0] = trim($address);
$this->cc[$cur][1] = $name;
}
/**
* Adds a "Bcc" address. Note: this function works
* with the SMTP mailer on win32, not with the "mail"
* mailer.
* @param string $address
* @param string $name
* @return void
*/
public function AddBCC($address, $name = '') {
$cur = count($this->bcc);
$this->bcc[$cur][0] = trim($address);
$this->bcc[$cur][1] = $name;
}
/**
* Adds a "Reply-to" address.
* @param string $address
* @param string $name
* @return void
*/
public function AddReplyTo($address, $name = '') {
$cur = count($this->ReplyTo);
$this->ReplyTo[$cur][0] = trim($address);
$this->ReplyTo[$cur][1] = $name;
}
/////////////////////////////////////////////////
// METHODS, MAIL SENDING
/////////////////////////////////////////////////
/**
* Creates message and assigns Mailer. If the message is
* not sent successfully then it returns false. Use the ErrorInfo
* variable to view description of the error.
* @return bool
*/
public function Send() {
$header = '';
$body = '';
$result = true;
if((count($this->to) + count($this->cc) + count($this->bcc)) < 1) {
$this->SetError($this->Lang('provide_address'));
return false;
}
/* Set whether the message is multipart/alternative */
if(!empty($this->AltBody)) {
$this->ContentType = 'multipart/alternative';
}
$this->error_count = 0; // reset errors
$this->SetMessageType();
$header .= $this->CreateHeader();
$body = $this->CreateBody();
if($body == '') {
return false;
}
/* Choose the mailer */
switch($this->Mailer) {
case 'sendmail':
$result = $this->SendmailSend($header, $body);
break;
case 'smtp':
$result = $this->SmtpSend($header, $body);
break;
case 'mail':
$result = $this->MailSend($header, $body);
break;
default:
$result = $this->MailSend($header, $body);
break;
//$this->SetError($this->Mailer . $this->Lang('mailer_not_supported'));
//$result = false;
//break;
}
return $result;
}
/**
* Sends mail using the $Sendmail program.
* @access public
* @return bool
*/
public function SendmailSend($header, $body) {
if ($this->Sender != '') {
$sendmail = sprintf("%s -oi -f %s -t", escapeshellcmd($this->Sendmail), escapeshellarg($this->Sender));
} else {
$sendmail = sprintf("%s -oi -t", escapeshellcmd($this->Sendmail));
}
if(!@$mail = popen($sendmail, 'w')) {
$this->SetError($this->Lang('execute') . $this->Sendmail);
return false;
}
fputs($mail, $header);
fputs($mail, $body);
$result = pclose($mail);
if (version_compare(phpversion(), '4.2.3') == -1) {
$result = $result >> 8 & 0xFF;
}
if($result != 0) {
$this->SetError($this->Lang('execute') . $this->Sendmail);
return false;
}
return true;
}
/**
* Sends mail using the PHP mail() function.
* @access public
* @return bool
*/
public function MailSend($header, $body) {
$to = '';
for($i = 0; $i < count($this->to); $i++) {
if($i != 0) { $to .= ', '; }
$to .= $this->AddrFormat($this->to[$i]);
}
$toArr = explode(',', $to);
$params = sprintf("-oi -f %s", $this->Sender);
if ($this->Sender != '' && strlen(ini_get('safe_mode'))< 1) {
$old_from = ini_get('sendmail_from');
ini_set('sendmail_from', $this->Sender);
if ($this->SingleTo === true && count($toArr) > 1) {
foreach ($toArr as $key => $val) {
$rt = @mail($val, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header, $params);
}
} else {
$rt = @mail($to, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header, $params);
}
} else {
if ($this->SingleTo === true && count($toArr) > 1) {
foreach ($toArr as $key => $val) {
$rt = @mail($val, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header, $params);
}
} else {
$rt = @mail($to, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header);
}
}
if (isset($old_from)) {
ini_set('sendmail_from', $old_from);
}
if(!$rt) {
$this->SetError($this->Lang('instantiate'));
return false;
}
return true;
}
/**
* Sends mail via SMTP using PhpSMTP (Author:
* Chris Ryan). Returns bool. Returns false if there is a
* bad MAIL FROM, RCPT, or DATA input.
* @access public
* @return bool
*/
public function SmtpSend($header, $body) {
include_once($this->PluginDir . 'class.smtp.php');
$error = '';
$bad_rcpt = array();
if(!$this->SmtpConnect()) {
return false;
}
$smtp_from = ($this->Sender == '') ? $this->From : $this->Sender;
if(!$this->smtp->Mail($smtp_from)) {
$error = $this->Lang('from_failed') . $smtp_from;
$this->SetError($error);
$this->smtp->Reset();
return false;
}
/* Attempt to send attach all recipients */
for($i = 0; $i < count($this->to); $i++) {
if(!$this->smtp->Recipient($this->to[$i][0])) {
$bad_rcpt[] = $this->to[$i][0];
}
}
for($i = 0; $i < count($this->cc); $i++) {
if(!$this->smtp->Recipient($this->cc[$i][0])) {
$bad_rcpt[] = $this->cc[$i][0];
}
}
for($i = 0; $i < count($this->bcc); $i++) {
if(!$this->smtp->Recipient($this->bcc[$i][0])) {
$bad_rcpt[] = $this->bcc[$i][0];
}
}
if(count($bad_rcpt) > 0) { // Create error message
for($i = 0; $i < count($bad_rcpt); $i++) {
if($i != 0) {
$error .= ', ';
}
$error .= $bad_rcpt[$i];
}
$error = $this->Lang('recipients_failed') . $error;
$this->SetError($error);
$this->smtp->Reset();
return false;
}
if(!$this->smtp->Data($header . $body)) {
$this->SetError($this->Lang('data_not_accepted'));
$this->smtp->Reset();
return false;
}
if($this->SMTPKeepAlive == true) {
$this->smtp->Reset();
} else {
$this->SmtpClose();
}
return true;
}
/**
* Initiates a connection to an SMTP server. Returns false if the
* operation failed.
* @access public
* @return bool
*/
public function SmtpConnect() {
if($this->smtp == NULL) {
$this->smtp = new SMTP();
}
$this->smtp->do_debug = $this->SMTPDebug;
$hosts = explode(';', $this->Host);
$index = 0;
$connection = ($this->smtp->Connected());
/* Retry while there is no connection */
while($index < count($hosts) && $connection == false) {
$hostinfo = array();
if(eregi('^(.+):([0-9]+)$', $hosts[$index], $hostinfo)) {
$host = $hostinfo[1];
$port = $hostinfo[2];
} else {
$host = $hosts[$index];
$port = $this->Port;
}
$tls = ($this->SMTPSecure == 'tls');
$ssl = ($this->SMTPSecure == 'ssl');
if($this->smtp->Connect(($ssl ? 'ssl://':'').$host, $port, $this->Timeout)) {
$hello = ($this->Helo != '' ? $this->Hello : $this->ServerHostname());
$this->smtp->Hello($hello);
if($tls) {
if(!$this->smtp->StartTLS()) {
$this->SetError($this->Lang("tls"));
$this->smtp->Reset();
$connection = false;
}
//We must resend HELLO after tls negociation
$this->smtp->Hello($hello);
}
$connection = true;
if($this->SMTPAuth) {
if(!$this->smtp->Authenticate($this->Username, $this->Password)) {
$this->SetError($this->Lang('authenticate'));
$this->smtp->Reset();
$connection = false;
}
}
}
$index++;
}
if(!$connection) {
$this->SetError($this->Lang('connect_host'));
}
return $connection;
}
/**
* Closes the active SMTP session if one exists.
* @return void
*/
public function SmtpClose() {
if($this->smtp != NULL) {
if($this->smtp->Connected()) {
$this->smtp->Quit();
$this->smtp->Close();
}
}
}
/**
* Sets the language for all class error messages. Returns false
* if it cannot load the language file. The default language type
* is English.
* @param string $lang_type Type of language (e.g. Portuguese: "br")
* @param string $lang_path Path to the language file directory
* @access public
* @return bool
*/
function SetLanguage($lang_type = 'en', $lang_path = 'language/') {
if( !(@include $lang_path.'phpmailer.lang-'.$lang_type.'.php') ) {
$this->SetError('Could not load language file');
return false;
}
$this->language = $PHPMAILER_LANG;
return true;
}
/////////////////////////////////////////////////
// METHODS, MESSAGE CREATION
/////////////////////////////////////////////////
/**
* Creates recipient headers.
* @access public
* @return string
*/
public function AddrAppend($type, $addr) {
$addr_str = $type . ': ';
$addr_str .= $this->AddrFormat($addr[0]);
if(count($addr) > 1) {
for($i = 1; $i < count($addr); $i++) {
$addr_str .= ', ' . $this->AddrFormat($addr[$i]);
}
}
$addr_str .= $this->LE;
return $addr_str;
}
/**
* Formats an address correctly.
* @access public
* @return string
*/
public function AddrFormat($addr) {
if(empty($addr[1])) {
$formatted = $this->SecureHeader($addr[0]);
} else {
$formatted = $this->EncodeHeader($this->SecureHeader($addr[1]), 'phrase') . " <" . $this->SecureHeader($addr[0]) . ">";
}
return $formatted;
}
/**
* Wraps message for use with mailers that do not
* automatically perform wrapping and for quoted-printable.
* Original written by philippe.
* @access public
* @return string
*/
public function WrapText($message, $length, $qp_mode = false) {
$soft_break = ($qp_mode) ? sprintf(" =%s", $this->LE) : $this->LE;
// If utf-8 encoding is used, we will need to make sure we don't
// split multibyte characters when we wrap
$is_utf8 = (strtolower($this->CharSet) == "utf-8");
$message = $this->FixEOL($message);
if (substr($message, -1) == $this->LE) {
$message = substr($message, 0, -1);
}
$line = explode($this->LE, $message);
$message = '';
for ($i=0 ;$i < count($line); $i++) {
$line_part = explode(' ', $line[$i]);
$buf = '';
for ($e = 0; $e $length)) {
$space_left = $length - strlen($buf) - 1;
if ($e != 0) {
if ($space_left > 20) {
$len = $space_left;
if ($is_utf8) {
$len = $this->UTF8CharBoundary($word, $len);
} elseif (substr($word, $len - 1, 1) == "=") {
$len--;
} elseif (substr($word, $len - 2, 1) == "=") {
$len -= 2;
}
$part = substr($word, 0, $len);
$word = substr($word, $len);
$buf .= ' ' . $part;
$message .= $buf . sprintf("=%s", $this->LE);
} else {
$message .= $buf . $soft_break;
}
$buf = '';
}
while (strlen($word) > 0) {
$len = $length;
if ($is_utf8) {
$len = $this->UTF8CharBoundary($word, $len);
} elseif (substr($word, $len - 1, 1) == "=") {
$len--;
} elseif (substr($word, $len - 2, 1) == "=") {
$len -= 2;
}
$part = substr($word, 0, $len);
$word = substr($word, $len);
if (strlen($word) > 0) {
$message .= $part . sprintf("=%s", $this->LE);
} else {
$buf = $part;
}
}
} else {
$buf_o = $buf;
$buf .= ($e == 0) ? $word : (' ' . $word);
if (strlen($buf) > $length and $buf_o != '') {
$message .= $buf_o . $soft_break;
$buf = $word;
}
}
}
$message .= $buf . $this->LE;
}
return $message;
}
/**
* Finds last character boundary prior to maxLength in a utf-8
* quoted (printable) encoded string.
* Original written by Colin Brown.
* @access public
* @param string $encodedText utf-8 QP text
* @param int $maxLength find last character boundary prior to this length
* @return int
*/
public function UTF8CharBoundary($encodedText, $maxLength) {
$foundSplitPos = false;
$lookBack = 3;
while (!$foundSplitPos) {
$lastChunk = substr($encodedText, $maxLength - $lookBack, $lookBack);
$encodedCharPos = strpos($lastChunk, "=");
if ($encodedCharPos !== false) {
// Found start of encoded character byte within $lookBack block.
// Check the encoded byte value (the 2 chars after the '=')
$hex = substr($encodedText, $maxLength - $lookBack + $encodedCharPos + 1, 2);
$dec = hexdec($hex);
if ($dec < 128) { // Single byte character.
// If the encoded char was found at pos 0, it will fit
// otherwise reduce maxLength to start of the encoded char
$maxLength = ($encodedCharPos == 0) ? $maxLength :
$maxLength - ($lookBack - $encodedCharPos);
$foundSplitPos = true;
} elseif ($dec >= 192) { // First byte of a multi byte character
// Reduce maxLength to split at start of character
$maxLength = $maxLength - ($lookBack - $encodedCharPos);
$foundSplitPos = true;
} elseif ($dec < 192) { // Middle byte of a multi byte character, look further back
$lookBack += 3;
}
} else {
// No encoded character found
$foundSplitPos = true;
}
}
return $maxLength;
}
/**
* Set the body wrapping.
* @access public
* @return void
*/
public function SetWordWrap() {
if($this->WordWrap < 1) {
return;
}
switch($this->message_type) {
case 'alt':
/* fall through */
case 'alt_attachments':
$this->AltBody = $this->WrapText($this->AltBody, $this->WordWrap);
break;
default:
$this->Body = $this->WrapText($this->Body, $this->WordWrap);
break;
}
}
/**
* Assembles message header.
* @access public
* @return string
*/
public function CreateHeader() {
$result = '';
/* Set the boundaries */
$uniq_id = md5(uniqid(time()));
$this->boundary[1] = 'b1_' . $uniq_id;
$this->boundary[2] = 'b2_' . $uniq_id;
$result .= $this->HeaderLine('Date', $this->RFCDate());
if($this->Sender == '') {
$result .= $this->HeaderLine('Return-Path', trim($this->From));
} else {
$result .= $this->HeaderLine('Return-Path', trim($this->Sender));
}
/* To be created automatically by mail() */
if($this->Mailer != 'mail') {
if(count($this->to) > 0) {
$result .= $this->AddrAppend('To', $this->to);
} elseif (count($this->cc) == 0) {
$result .= $this->HeaderLine('To', 'undisclosed-recipients:;');
}
if(count($this->cc) > 0) {
$result .= $this->AddrAppend('Cc', $this->cc);
}
}
$from = array();
$from[0][0] = trim($this->From);
$from[0][1] = $this->FromName;
$result .= $this->AddrAppend('From', $from);
/* sendmail and mail() extract Cc from the header before sending */
if((($this->Mailer == 'sendmail') || ($this->Mailer == 'mail')) && (count($this->cc) > 0)) {
$result .= $this->AddrAppend('Cc', $this->cc);
}
/* sendmail and mail() extract Bcc from the header before sending */
if((($this->Mailer == 'sendmail') || ($this->Mailer == 'mail')) && (count($this->bcc) > 0)) {
$result .= $this->AddrAppend('Bcc', $this->bcc);
}
if(count($this->ReplyTo) > 0) {
$result .= $this->AddrAppend('Reply-to', $this->ReplyTo);
}
/* mail() sets the subject itself */
if($this->Mailer != 'mail') {
$result .= $this->HeaderLine('Subject', $this->EncodeHeader($this->SecureHeader($this->Subject)));
}
if($this->MessageID != '') {
$result .= $this->HeaderLine('Message-ID',$this->MessageID);
} else {
$result .= sprintf("Message-ID: <%s@%s>%s", $uniq_id, $this->ServerHostname(), $this->LE);
}
$result .= $this->HeaderLine('X-Priority', $this->Priority);
$result .= $this->HeaderLine('X-Mailer', 'Collabtive Groupware');
if($this->ConfirmReadingTo != '') {
$result .= $this->HeaderLine('Disposition-Notification-To', '<' . trim($this->ConfirmReadingTo) . '>');
}
// Add custom headers
for($index = 0; $index < count($this->CustomHeader); $index++) {
$result .= $this->HeaderLine(trim($this->CustomHeader[$index][0]), $this->EncodeHeader(trim($this->CustomHeader[$index][1])));
}
//$result .= $this->HeaderLine('MIME-Version', '1.0');
if (!$this->sign_key_file) {
$result .= $this->HeaderLine('MIME-Version', '1.0');
$result .= $this->GetMailMIME();
}
return $result;
}
/**
* Returns the message MIME.
* @access public
* @return string
*/
public function GetMailMIME() {
$result = '';
switch($this->message_type) {
case 'plain':
$result .= $this->HeaderLine('Content-Transfer-Encoding', $this->Encoding);
$result .= sprintf("Content-Type: %s; charset=\"%s\"", $this->ContentType, $this->CharSet);
break;
case 'attachments':
/* fall through */
case 'alt_attachments':
if($this->InlineImageExists()){
$result .= sprintf("Content-Type: %s;%s\ttype=\"text/html\";%s\tboundary=\"%s\"%s", 'multipart/related', $this->LE, $this->LE, $this->boundary[1], $this->LE);
} else {
$result .= $this->HeaderLine('Content-Type', 'multipart/mixed;');
$result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"');
}
break;
case 'alt':
$result .= $this->HeaderLine('Content-Type', 'multipart/alternative;');
$result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"');
break;
}
if($this->Mailer != 'mail') {
$result .= $this->LE.$this->LE;
}
return $result;
}
/**
* Assembles the message body. Returns an empty string on failure.
* @access public
* @return string
*/
public function CreateBody() {
$result = '';
if ($this->sign_key_file) {
$result .= $this->GetMailMIME();
}
$this->SetWordWrap();
switch($this->message_type) {
case 'alt':
$result .= $this->GetBoundary($this->boundary[1], '', 'text/plain', '');
$result .= $this->EncodeString($this->AltBody, $this->Encoding);
$result .= $this->LE.$this->LE;
$result .= $this->GetBoundary($this->boundary[1], '', 'text/html', '');
$result .= $this->EncodeString($this->Body, $this->Encoding);
$result .= $this->LE.$this->LE;
$result .= $this->EndBoundary($this->boundary[1]);
break;
case 'plain':
$result .= $this->EncodeString($this->Body, $this->Encoding);
break;
case 'attachments':
$result .= $this->GetBoundary($this->boundary[1], '', '', '');
$result .= $this->EncodeString($this->Body, $this->Encoding);
$result .= $this->LE;
$result .= $this->AttachAll();
break;
case 'alt_attachments':
$result .= sprintf("--%s%s", $this->boundary[1], $this->LE);
$result .= sprintf("Content-Type: %s;%s" . "\tboundary=\"%s\"%s", 'multipart/alternative', $this->LE, $this->boundary[2], $this->LE.$this->LE);
$result .= $this->GetBoundary($this->boundary[2], '', 'text/plain', '') . $this->LE; // Create text body
$result .= $this->EncodeString($this->AltBody, $this->Encoding);
$result .= $this->LE.$this->LE;
$result .= $this->GetBoundary($this->boundary[2], '', 'text/html', '') . $this->LE; // Create the HTML body
$result .= $this->EncodeString($this->Body, $this->Encoding);
$result .= $this->LE.$this->LE;
$result .= $this->EndBoundary($this->boundary[2]);
$result .= $this->AttachAll();
break;
}
if($this->IsError()) {
$result = '';
} else if ($this->sign_key_file) {
$file = tempnam("", "mail");
$fp = fopen($file, "w");
fwrite($fp, $result);
fclose($fp);
$signed = tempnam("", "signed");
if (@openssl_pkcs7_sign($file, $signed, "file://".$this->sign_key_file, array("file://".$this->sign_key_file, $this->sign_key_pass), null)) {
$fp = fopen($signed, "r");
$result = fread($fp, filesize($this->sign_key_file));
fclose($fp);
} else {
$this->SetError($this->Lang("signing").openssl_error_string());
$result = '';
}
unlink($file);
unlink($signed);
}
return $result;
}
/**
* Returns the start of a message boundary.
* @access public
*/
public function GetBoundary($boundary, $charSet, $contentType, $encoding) {
$result = '';
if($charSet == '') {
$charSet = $this->CharSet;
}
if($contentType == '') {
$contentType = $this->ContentType;
}
if($encoding == '') {
$encoding = $this->Encoding;
}
$result .= $this->TextLine('--' . $boundary);
$result .= sprintf("Content-Type: %s; charset = \"%s\"", $contentType, $charSet);
$result .= $this->LE;
$result .= $this->HeaderLine('Content-Transfer-Encoding', $encoding);
$result .= $this->LE;
return $result;
}
/**
* Returns the end of a message boundary.
* @access public
*/
public function EndBoundary($boundary) {
return $this->LE . '--' . $boundary . '--' . $this->LE;
}
/**
* Sets the message type.
* @access public
* @return void
*/
public function SetMessageType() {
if(count($this->attachment) < 1 && strlen($this->AltBody) < 1) {
$this->message_type = 'plain';
} else {
if(count($this->attachment) > 0) {
$this->message_type = 'attachments';
}
if(strlen($this->AltBody) > 0 && count($this->attachment) < 1) {
$this->message_type = 'alt';
}
if(strlen($this->AltBody) > 0 && count($this->attachment) > 0) {
$this->message_type = 'alt_attachments';
}
}
}
/* Returns a formatted header line.
* @access public
* @return string
*/
public function HeaderLine($name, $value) {
return $name . ': ' . $value . $this->LE;
}
/**
* Returns a formatted mail line.
* @access public
* @return string
*/
public function TextLine($value) {
return $value . $this->LE;
}
/////////////////////////////////////////////////
// CLASS METHODS, ATTACHMENTS
/////////////////////////////////////////////////
/**
* Adds an attachment from a path on the filesystem.
* Returns false if the file could not be found
* or accessed.
* @param string $path Path to the attachment.
* @param string $name Overrides the attachment name.
* @param string $encoding File encoding (see $Encoding).
* @param string $type File extension (MIME) type.
* @return bool
*/
public function AddAttachment($path, $name = '', $encoding = 'base64', $type = 'application/octet-stream') {
if(!@is_file($path)) {
$this->SetError($this->Lang('file_access') . $path);
return false;
}
$filename = basename($path);
if($name == '') {
$name = $filename;
}
$cur = count($this->attachment);
$this->attachment[$cur][0] = $path;
$this->attachment[$cur][1] = $filename;
$this->attachment[$cur][2] = $name;
$this->attachment[$cur][3] = $encoding;
$this->attachment[$cur][4] = $type;
$this->attachment[$cur][5] = false; // isStringAttachment
$this->attachment[$cur][6] = 'attachment';
$this->attachment[$cur][7] = 0;
return true;
}
/**
* Attaches all fs, string, and binary attachments to the message.
* Returns an empty string on failure.
* @access public
* @return string
*/
public function AttachAll() {
/* Return text of body */
$mime = array();
/* Add all attachments */
for($i = 0; $i < count($this->attachment); $i++) {
/* Check for string attachment */
$bString = $this->attachment[$i][5];
if ($bString) {
$string = $this->attachment[$i][0];
} else {
$path = $this->attachment[$i][0];
}
$filename = $this->attachment[$i][1];
$name = $this->attachment[$i][2];
$encoding = $this->attachment[$i][3];
$type = $this->attachment[$i][4];
$disposition = $this->attachment[$i][6];
$cid = $this->attachment[$i][7];
$mime[] = sprintf("--%s%s", $this->boundary[1], $this->LE);
$mime[] = sprintf("Content-Type: %s; name=\"%s\"%s", $type, $name, $this->LE);
$mime[] = sprintf("Content-Transfer-Encoding: %s%s", $encoding, $this->LE);
if($disposition == 'inline') {
$mime[] = sprintf("Content-ID: <%s>%s", $cid, $this->LE);
}
$mime[] = sprintf("Content-Disposition: %s; filename=\"%s\"%s", $disposition, $name, $this->LE.$this->LE);
/* Encode as string attachment */
if($bString) {
$mime[] = $this->EncodeString($string, $encoding);
if($this->IsError()) {
return '';
}
$mime[] = $this->LE.$this->LE;
} else {
$mime[] = $this->EncodeFile($path, $encoding);
if($this->IsError()) {
return '';
}
$mime[] = $this->LE.$this->LE;
}
}
$mime[] = sprintf("--%s--%s", $this->boundary[1], $this->LE);
return join('', $mime);
}
/**
* Encodes attachment in requested format. Returns an
* empty string on failure.
* @access public
* @return string
*/
public function EncodeFile ($path, $encoding = 'base64') {
if(!@$fd = fopen($path, 'rb')) {
$this->SetError($this->Lang('file_open') . $path);
return '';
}
$magic_quotes = get_magic_quotes_runtime();
set_magic_quotes_runtime(0);
$file_buffer = file_get_contents($path);
$file_buffer = $this->EncodeString($file_buffer, $encoding);
fclose($fd);
set_magic_quotes_runtime($magic_quotes);
return $file_buffer;
}
/**
* Encodes string to requested format. Returns an
* empty string on failure.
* @access public
* @return string
*/
public function EncodeString ($str, $encoding = 'base64') {
$encoded = '';
switch(strtolower($encoding)) {
case 'base64':
$encoded = chunk_split(base64_encode($str), 76, $this->LE);
break;
case '7bit':
case '8bit':
$encoded = $this->FixEOL($str);
if (substr($encoded, -(strlen($this->LE))) != $this->LE)
$encoded .= $this->LE;
break;
case 'binary':
$encoded = $str;
break;
case 'quoted-printable':
$encoded = $this->EncodeQP($str);
break;
default:
$this->SetError($this->Lang('encoding') . $encoding);
break;
}
return $encoded;
}
/**
* Encode a header string to best of Q, B, quoted or none.
* @access public
* @return string
*/
public function EncodeHeader ($str, $position = 'text') {
$x = 0;
switch (strtolower($position)) {
case 'phrase':
if (!preg_match('/[\200-\377]/', $str)) {
/* Can't use addslashes as we don't know what value has magic_quotes_sybase. */
$encoded = addcslashes($str, "\0..\37\177\\\"");
if (($str == $encoded) && !preg_match('/[^A-Za-z0-9!#$%&\'*+\/=?^_`{|}~ -]/', $str)) {
return ($encoded);
} else {
return ("\"$encoded\"");
}
}
$x = preg_match_all('/[^\040\041\043-\133\135-\176]/', $str, $matches);
break;
case 'comment':
$x = preg_match_all('/[()"]/', $str, $matches);
/* Fall-through */
case 'text':
default:
$x += preg_match_all('/[\000-\010\013\014\016-\037\177-\377]/', $str, $matches);
break;
}
if ($x == 0) {
return ($str);
}
$maxlen = 75 - 7 - strlen($this->CharSet);
/* Try to select the encoding which should produce the shortest output */
if (strlen($str)/3 < $x) {
$encoding = 'B';
if (function_exists('mb_strlen') && $this->HasMultiBytes($str)) {
// Use a custom function which correctly encodes and wraps long
// multibyte strings without breaking lines within a character
$encoded = $this->Base64EncodeWrapMB($str);
} else {
$encoded = base64_encode($str);
$maxlen -= $maxlen % 4;
$encoded = trim(chunk_split($encoded, $maxlen, "\n"));
}
} else {
$encoding = 'Q';
$encoded = $this->EncodeQ($str, $position);
$encoded = $this->WrapText($encoded, $maxlen, true);
$encoded = str_replace('='.$this->LE, "\n", trim($encoded));
}
$encoded = preg_replace('/^(.*)$/m', " =?".$this->CharSet."?$encoding?\\1?=", $encoded);
$encoded = trim(str_replace("\n", $this->LE, $encoded));
return $encoded;
}
/**
* Checks if a string contains multibyte characters.
* @access public
* @param string $str multi-byte text to wrap encode
* @return bool
*/
public function HasMultiBytes($str) {
if (function_exists('mb_strlen')) {
return (strlen($str) > mb_strlen($str, $this->CharSet));
} else { // Assume no multibytes (we can't handle without mbstring functions anyway)
return False;
}
}
/**
* Correctly encodes and wraps long multibyte strings for mail headers
* without breaking lines within a character.
* Adapted from a function by paravoid at http://uk.php.net/manual/en/function.mb-encode-mimeheader.php
* @access public
* @param string $str multi-byte text to wrap encode
* @return string
*/
public function Base64EncodeWrapMB($str) {
$start = "=?".$this->CharSet."?B?";
$end = "?=";
$encoded = "";
$mb_length = mb_strlen($str, $this->CharSet);
// Each line must have length <= 75, including $start and $end
$length = 75 - strlen($start) - strlen($end);
// Average multi-byte ratio
$ratio = $mb_length / strlen($str);
// Base64 has a 4:3 ratio
$offset = $avgLength = floor($length * $ratio * .75);
for ($i = 0; $i < $mb_length; $i += $offset) {
$lookBack = 0;
do {
$offset = $avgLength - $lookBack;
$chunk = mb_substr($str, $i, $offset, $this->CharSet);
$chunk = base64_encode($chunk);
$lookBack++;
}
while (strlen($chunk) > $length);
$encoded .= $chunk . $this->LE;
}
// Chomp the last linefeed
$encoded = substr($encoded, 0, -strlen($this->LE));
return $encoded;
}
/**
* Encode string to quoted-printable.
* @access public
* @param string $string the text to encode
* @param integer $line_max Number of chars allowed on a line before wrapping
* @return string
*/
public function EncodeQP( $input = '', $line_max = 76, $space_conv = false ) {
$hex = array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
$lines = preg_split('/(?:\r\n|\r|\n)/', $input);
$eol = "\r\n";
$escape = '=';
$output = '';
while( list(, $line) = each($lines) ) {
$linlen = strlen($line);
$newline = '';
for($i = 0; $i < $linlen; $i++) {
$c = substr( $line, $i, 1 );
$dec = ord( $c );
if ( ( $i == 0 ) && ( $dec == 46 ) ) { // convert first point in the line into =2E
$c = '=2E';
}
if ( $dec == 32 ) {
if ( $i == ( $linlen - 1 ) ) { // convert space at eol only
$c = '=20';
} else if ( $space_conv ) {
$c = '=20';
}
} elseif ( ($dec == 61) || ($dec < 32 ) || ($dec > 126) ) { // always encode "\t", which is *not* required
$h2 = floor($dec/16);
$h1 = floor($dec%16);
$c = $escape.$hex[$h2].$hex[$h1];
}
if ( (strlen($newline) + strlen($c)) >= $line_max ) { // CRLF is not counted
$output .= $newline.$escape.$eol; // soft line break; " =\r\n" is okay
$newline = '';
// check if newline first character will be point or not
if ( $dec == 46 ) {
$c = '=2E';
}
}
$newline .= $c;
} // end of for
$output .= $newline.$eol;
} // end of while
return trim($output);
}
/**
* Encode string to q encoding.
* @access public
* @return string
*/
public function EncodeQ ($str, $position = 'text') {
/* There should not be any EOL in the string */
$encoded = preg_replace("[\r\n]", '', $str);
switch (strtolower($position)) {
case 'phrase':
$encoded = preg_replace("/([^A-Za-z0-9!*+\/ -])/e", "'='.sprintf('%02X', ord('\\1'))", $encoded);
break;
case 'comment':
$encoded = preg_replace("/([\(\)\"])/e", "'='.sprintf('%02X', ord('\\1'))", $encoded);
case 'text':
default:
/* Replace every high ascii, control =, ? and _ characters */
$encoded = preg_replace('/([\000-\011\013\014\016-\037\075\077\137\177-\377])/e',
"'='.sprintf('%02X', ord('\\1'))", $encoded);
break;
}
/* Replace every spaces to _ (more readable than =20) */
$encoded = str_replace(' ', '_', $encoded);
return $encoded;
}
/**
* Adds a string or binary attachment (non-filesystem) to the list.
* This method can be used to attach ascii or binary data,
* such as a BLOB record from a database.
* @param string $string String attachment data.
* @param string $filename Name of the attachment.
* @param string $encoding File encoding (see $Encoding).
* @param string $type File extension (MIME) type.
* @return void
*/
public function AddStringAttachment($string, $filename, $encoding = 'base64', $type = 'application/octet-stream') {
/* Append to $attachment array */
$cur = count($this->attachment);
$this->attachment[$cur][0] = $string;
$this->attachment[$cur][1] = $filename;
$this->attachment[$cur][2] = $filename;
$this->attachment[$cur][3] = $encoding;
$this->attachment[$cur][4] = $type;
$this->attachment[$cur][5] = true; // isString
$this->attachment[$cur][6] = 'attachment';
$this->attachment[$cur][7] = 0;
}
/**
* Adds an embedded attachment. This can include images, sounds, and
* just about any other document. Make sure to set the $type to an
* image type. For JPEG images use "image/jpeg" and for GIF images
* use "image/gif".
* @param string $path Path to the attachment.
* @param string $cid Content ID of the attachment. Use this to identify
* the Id for accessing the image in an HTML form.
* @param string $name Overrides the attachment name.
* @param string $encoding File encoding (see $Encoding).
* @param string $type File extension (MIME) type.
* @return bool
*/
public function AddEmbeddedImage($path, $cid, $name = '', $encoding = 'base64', $type = 'application/octet-stream') {
if(!@is_file($path)) {
$this->SetError($this->Lang('file_access') . $path);
return false;
}
$filename = basename($path);
if($name == '') {
$name = $filename;
}
/* Append to $attachment array */
$cur = count($this->attachment);
$this->attachment[$cur][0] = $path;
$this->attachment[$cur][1] = $filename;
$this->attachment[$cur][2] = $name;
$this->attachment[$cur][3] = $encoding;
$this->attachment[$cur][4] = $type;
$this->attachment[$cur][5] = false;
$this->attachment[$cur][6] = 'inline';
$this->attachment[$cur][7] = $cid;
return true;
}
/**
* Returns true if an inline attachment is present.
* @access public
* @return bool
*/
public function InlineImageExists() {
$result = false;
for($i = 0; $i < count($this->attachment); $i++) {
if($this->attachment[$i][6] == 'inline') {
$result = true;
break;
}
}
return $result;
}
/////////////////////////////////////////////////
// CLASS METHODS, MESSAGE RESET
/////////////////////////////////////////////////
/**
* Clears all recipients assigned in the TO array. Returns void.
* @return void
*/
public function ClearAddresses() {
$this->to = array();
}
/**
* Clears all recipients assigned in the CC array. Returns void.
* @return void
*/
public function ClearCCs() {
$this->cc = array();
}
/**
* Clears all recipients assigned in the BCC array. Returns void.
* @return void
*/
public function ClearBCCs() {
$this->bcc = array();
}
/**
* Clears all recipients assigned in the ReplyTo array. Returns void.
* @return void
*/
public function ClearReplyTos() {
$this->ReplyTo = array();
}
/**
* Clears all recipients assigned in the TO, CC and BCC
* array. Returns void.
* @return void
*/
public function ClearAllRecipients() {
$this->to = array();
$this->cc = array();
$this->bcc = array();
}
/**
* Clears all previously set filesystem, string, and binary
* attachments. Returns void.
* @return void
*/
public function ClearAttachments() {
$this->attachment = array();
}
/**
* Clears all custom headers. Returns void.
* @return void
*/
public function ClearCustomHeaders() {
$this->CustomHeader = array();
}
/////////////////////////////////////////////////
// CLASS METHODS, MISCELLANEOUS
/////////////////////////////////////////////////
/**
* Adds the error message to the error container.
* Returns void.
* @access private
* @return void
*/
private function SetError($msg) {
$this->error_count++;
$this->ErrorInfo = $msg;
}
/**
* Returns the proper RFC 822 formatted date.
* @access private
* @return string
*/
private static function RFCDate() {
$tz = date('Z');
$tzs = ($tz < 0) ? '-' : '+';
$tz = abs($tz);
$tz = (int)($tz/3600)*100 + ($tz%3600)/60;
$result = sprintf("%s %s%04d", date('D, j M Y H:i:s'), $tzs, $tz);
return $result;
}
/**
* Returns the server hostname or 'localhost.localdomain' if unknown.
* @access private
* @return string
*/
private function ServerHostname() {
if (!empty($this->Hostname)) {
$result = $this->Hostname;
} elseif (isset($_SERVER['SERVER_NAME'])) {
$result = $_SERVER['SERVER_NAME'];
} else {
$result = "localhost.localdomain";
}
return $result;
}
/**
* Returns a message in the appropriate language.
* @access private
* @return string
*/
private function Lang($key) {
if(count($this->language) < 1) {
$this->SetLanguage('en'); // set the default language
}
if(isset($this->language[$key])) {
return $this->language[$key];
} else {
return 'Language string failed to load: ' . $key;
}
}
/**
* Returns true if an error occurred.
* @access public
* @return bool
*/
public function IsError() {
return ($this->error_count > 0);
}
/**
* Changes every end of line from CR or LF to CRLF.
* @access private
* @return string
*/
private function FixEOL($str) {
$str = str_replace("\r\n", "\n", $str);
$str = str_replace("\r", "\n", $str);
$str = str_replace("\n", $this->LE, $str);
return $str;
}
/**
* Adds a custom header.
* @access public
* @return void
*/
public function AddCustomHeader($custom_header) {
$this->CustomHeader[] = explode(':', $custom_header, 2);
}
/**
* Evaluates the message and returns modifications for inline images and backgrounds
* @access public
* @return $message
*/
public function MsgHTML($message,$basedir='') {
preg_match_all("/(src|background)=\"(.*)\"/Ui", $message, $images);
if(isset($images[2])) {
foreach($images[2] as $i => $url) {
// do not change urls for absolute images (thanks to corvuscorax)
if (!preg_match('/^[A-z][A-z]*:\/\//',$url)) {
$filename = basename($url);
$directory = dirname($url);
($directory == '.')?$directory='':'';
$cid = 'cid:' . md5($filename);
$fileParts = split("\.", $filename);
$ext = $fileParts[1];
$mimeType = $this->_mime_types($ext);
if ( strlen($basedir) > 1 && substr($basedir,-1) != '/') { $basedir .= '/'; }
if ( strlen($directory) > 1 && substr($basedir,-1) != '/') { $directory .= '/'; }
$this->AddEmbeddedImage($basedir.$directory.$filename, md5($filename), $filename, 'base64', $mimeType);
if ( $this->AddEmbeddedImage($basedir.$directory.$filename, md5($filename), $filename, 'base64',$mimeType) ) {
$message = preg_replace("/".$images[1][$i]."=\"".preg_quote($url, '/')."\"/Ui", $images[1][$i]."=\"".$cid."\"", $message);
}
}
}
}
$this->IsHTML(true);
$this->Body = $message;
$textMsg = trim(strip_tags(preg_replace('/<(head|title|style|script)[^>]*>.*?<\/\\1>/s','',$message)));
if ( !empty($textMsg) && empty($this->AltBody) ) {
$this->AltBody = $textMsg;
}
if ( empty($this->AltBody) ) {
$this->AltBody = 'To view this email message, open the email in with HTML compatibility!' . "\n\n";
}
}
/**
* Gets the mime type of the embedded or inline image
* @access public
* @return mime type of ext
*/
public function _mime_types($ext = '') {
$mimes = array(
'hqx' => 'application/mac-binhex40',
'cpt' => 'application/mac-compactpro',
'doc' => 'application/msword',
'bin' => 'application/macbinary',
'dms' => 'application/octet-stream',
'lha' => 'application/octet-stream',
'lzh' => 'application/octet-stream',
'exe' => 'application/octet-stream',
'class' => 'application/octet-stream',
'psd' => 'application/octet-stream',
'so' => 'application/octet-stream',
'sea' => 'application/octet-stream',
'dll' => 'application/octet-stream',
'oda' => 'application/oda',
'pdf' => 'application/pdf',
'ai' => 'application/postscript',
'eps' => 'application/postscript',
'ps' => 'application/postscript',
'smi' => 'application/smil',
'smil' => 'application/smil',
'mif' => 'application/vnd.mif',
'xls' => 'application/vnd.ms-excel',
'ppt' => 'application/vnd.ms-powerpoint',
'wbxml' => 'application/vnd.wap.wbxml',
'wmlc' => 'application/vnd.wap.wmlc',
'dcr' => 'application/x-director',
'dir' => 'application/x-director',
'dxr' => 'application/x-director',
'dvi' => 'application/x-dvi',
'gtar' => 'application/x-gtar',
'php' => 'application/x-httpd-php',
'php4' => 'application/x-httpd-php',
'php3' => 'application/x-httpd-php',
'phtml' => 'application/x-httpd-php',
'phps' => 'application/x-httpd-php-source',
'js' => 'application/x-javascript',
'swf' => 'application/x-shockwave-flash',
'sit' => 'application/x-stuffit',
'tar' => 'application/x-tar',
'tgz' => 'application/x-tar',
'xhtml' => 'application/xhtml+xml',
'xht' => 'application/xhtml+xml',
'zip' => 'application/zip',
'mid' => 'audio/midi',
'midi' => 'audio/midi',
'mpga' => 'audio/mpeg',
'mp2' => 'audio/mpeg',
'mp3' => 'audio/mpeg',
'aif' => 'audio/x-aiff',
'aiff' => 'audio/x-aiff',
'aifc' => 'audio/x-aiff',
'ram' => 'audio/x-pn-realaudio',
'rm' => 'audio/x-pn-realaudio',
'rpm' => 'audio/x-pn-realaudio-plugin',
'ra' => 'audio/x-realaudio',
'rv' => 'video/vnd.rn-realvideo',
'wav' => 'audio/x-wav',
'bmp' => 'image/bmp',
'gif' => 'image/gif',
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpeg',
'jpe' => 'image/jpeg',
'png' => 'image/png',
'tiff' => 'image/tiff',
'tif' => 'image/tiff',
'css' => 'text/css',
'html' => 'text/html',
'htm' => 'text/html',
'shtml' => 'text/html',
'txt' => 'text/plain',
'text' => 'text/plain',
'log' => 'text/plain',
'rtx' => 'text/richtext',
'rtf' => 'text/rtf',
'xml' => 'text/xml',
'xsl' => 'text/xml',
'mpeg' => 'video/mpeg',
'mpg' => 'video/mpeg',
'mpe' => 'video/mpeg',
'qt' => 'video/quicktime',
'mov' => 'video/quicktime',
'avi' => 'video/x-msvideo',
'movie' => 'video/x-sgi-movie',
'doc' => 'application/msword',
'word' => 'application/msword',
'xl' => 'application/excel',
'eml' => 'message/rfc822'
);
return ( ! isset($mimes[strtolower($ext)])) ? 'application/octet-stream' : $mimes[strtolower($ext)];
}
/**
* Set (or reset) Class Objects (variables)
*
* Usage Example:
* $page->set('X-Priority', '3');
*
* @access public
* @param string $name Parameter Name
* @param mixed $value Parameter Value
* NOTE: will not work with arrays, there are no arrays to set/reset
*/
public function set ( $name, $value = '' ) {
if ( isset($this->$name) ) {
$this->$name = $value;
} else {
$this->SetError('Cannot set or reset variable ' . $name);
return false;
}
}
/**
* Read a file from a supplied filename and return it.
*
* @access public
* @param string $filename Parameter File Name
*/
public function getFile($filename) {
$return = '';
if ($fp = fopen($filename, 'rb')) {
while (!feof($fp)) {
$return .= fread($fp, 1024);
}
fclose($fp);
return $return;
} else {
return false;
}
}
/**
* Strips newlines to prevent header injection.
* @access public
* @param string $str String
* @return string
*/
public function SecureHeader($str) {
$str = trim($str);
$str = str_replace("\r", "", $str);
$str = str_replace("\n", "", $str);
return $str;
}
/**
* Set the private key file and password to sign the message.
*
* @access public
* @param string $key_filename Parameter File Name
* @param string $key_pass Password for private key
*/
public function Sign($key_filename, $key_pass) {
$this->sign_key_file = $key_filename;
$this->sign_key_pass = $key_pass;
}
}
?>
Collabtive-2.0/include/class.PclZip.php 0000664 0000000 0000000 00000742421 12372520637 0020121 0 ustar 00root root 0000000 0000000 zipname = $p_zipname;
$this->zip_fd = 0;
$this->magic_quotes_status = -1;
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 1);
return;
}
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
// Function :
// create($p_filelist, $p_add_dir="", $p_remove_dir="")
// create($p_filelist, $p_option, $p_option_value, ...)
// Description :
// This method supports two different synopsis. The first one is historical.
// This method creates a Zip Archive. The Zip file is created in the
// filesystem. The files and directories indicated in $p_filelist
// are added in the archive. See the parameters description for the
// supported format of $p_filelist.
// When a directory is in the list, the directory and its content is added
// in the archive.
// In this synopsis, the function takes an optional variable list of
// options. See bellow the supported options.
// Parameters :
// $p_filelist : An array containing file or directory names, or
// a string containing one filename or one directory name, or
// a string containing a list of filenames and/or directory
// names separated by spaces.
// $p_add_dir : A path to add before the real path of the archived file,
// in order to have it memorized in the archive.
// $p_remove_dir : A path to remove from the real path of the file to archive,
// in order to have a shorter path memorized in the archive.
// When $p_add_dir and $p_remove_dir are set, $p_remove_dir
// is removed first, before $p_add_dir is added.
// Options :
// PCLZIP_OPT_ADD_PATH :
// PCLZIP_OPT_REMOVE_PATH :
// PCLZIP_OPT_REMOVE_ALL_PATH :
// PCLZIP_OPT_COMMENT :
// PCLZIP_CB_PRE_ADD :
// PCLZIP_CB_POST_ADD :
// Return Values :
// 0 on failure,
// The list of the added files, with a status of the add action.
// (see PclZip::listContent() for list entry format)
// --------------------------------------------------------------------------------
function create($p_filelist)
{
//--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, 'PclZip::create', "filelist='$p_filelist', ...");
$v_result=1;
// ----- Reset the error handler
$this->privErrorReset();
// ----- Set default values
$v_options = array();
$v_options[PCLZIP_OPT_NO_COMPRESSION] = FALSE;
// ----- Look for variable options arguments
$v_size = func_num_args();
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "$v_size arguments passed to the method");
// ----- Look for arguments
if ($v_size > 1) {
// ----- Get the arguments
$v_arg_list = func_get_args();
// ----- Remove from the options list the first argument
array_shift($v_arg_list);
$v_size--;
// ----- Look for first arg
if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Variable list of options detected");
// ----- Parse the options
$v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
array (PCLZIP_OPT_REMOVE_PATH => 'optional',
PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
PCLZIP_OPT_ADD_PATH => 'optional',
PCLZIP_CB_PRE_ADD => 'optional',
PCLZIP_CB_POST_ADD => 'optional',
PCLZIP_OPT_NO_COMPRESSION => 'optional',
PCLZIP_OPT_COMMENT => 'optional'
//, PCLZIP_OPT_CRYPT => 'optional'
));
if ($v_result != 1) {
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
return 0;
}
}
// ----- Look for 2 args
// Here we need to support the first historic synopsis of the
// method.
else {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Static synopsis");
// ----- Get the first argument
$v_options[PCLZIP_OPT_ADD_PATH] = $v_arg_list[0];
// ----- Look for the optional second argument
if ($v_size == 2) {
$v_options[PCLZIP_OPT_REMOVE_PATH] = $v_arg_list[1];
}
else if ($v_size > 2) {
PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER,
"Invalid number / type of arguments");
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
return 0;
}
}
}
// ----- Init
$v_string_list = array();
$v_att_list = array();
$v_filedescr_list = array();
$p_result_list = array();
// ----- Look if the $p_filelist is really an array
if (is_array($p_filelist)) {
// ----- Look if the first element is also an array
// This will mean that this is a file description entry
if (isset($p_filelist[0]) && is_array($p_filelist[0])) {
$v_att_list = $p_filelist;
}
// ----- The list is a list of string names
else {
$v_string_list = $p_filelist;
}
}
// ----- Look if the $p_filelist is a string
else if (is_string($p_filelist)) {
// ----- Create a list from the string
$v_string_list = explode(PCLZIP_SEPARATOR, $p_filelist);
}
// ----- Invalid variable type for $p_filelist
else {
PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type p_filelist");
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
return 0;
}
// ----- Reformat the string list
if (sizeof($v_string_list) != 0) {
foreach ($v_string_list as $v_string) {
if ($v_string != '') {
$v_att_list[][PCLZIP_ATT_FILE_NAME] = $v_string;
}
else {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Ignore an empty filename");
}
}
}
// ----- For each file in the list check the attributes
$v_supported_attributes
= array ( PCLZIP_ATT_FILE_NAME => 'mandatory'
,PCLZIP_ATT_FILE_NEW_SHORT_NAME => 'optional'
,PCLZIP_ATT_FILE_NEW_FULL_NAME => 'optional'
,PCLZIP_ATT_FILE_MTIME => 'optional'
,PCLZIP_ATT_FILE_CONTENT => 'optional'
,PCLZIP_ATT_FILE_COMMENT => 'optional'
);
foreach ($v_att_list as $v_entry) {
$v_result = $this->privFileDescrParseAtt($v_entry,
$v_filedescr_list[],
$v_options,
$v_supported_attributes);
if ($v_result != 1) {
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
return 0;
}
}
// ----- Expand the filelist (expand directories)
$v_result = $this->privFileDescrExpand($v_filedescr_list, $v_options);
if ($v_result != 1) {
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
return 0;
}
// ----- Call the create fct
$v_result = $this->privCreate($v_filedescr_list, $p_result_list, $v_options);
if ($v_result != 1) {
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
return 0;
}
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $p_result_list);
return $p_result_list;
}
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
// Function :
// add($p_filelist, $p_add_dir="", $p_remove_dir="")
// add($p_filelist, $p_option, $p_option_value, ...)
// Description :
// This method supports two synopsis. The first one is historical.
// This methods add the list of files in an existing archive.
// If a file with the same name already exists, it is added at the end of the
// archive, the first one is still present.
// If the archive does not exist, it is created.
// Parameters :
// $p_filelist : An array containing file or directory names, or
// a string containing one filename or one directory name, or
// a string containing a list of filenames and/or directory
// names separated by spaces.
// $p_add_dir : A path to add before the real path of the archived file,
// in order to have it memorized in the archive.
// $p_remove_dir : A path to remove from the real path of the file to archive,
// in order to have a shorter path memorized in the archive.
// When $p_add_dir and $p_remove_dir are set, $p_remove_dir
// is removed first, before $p_add_dir is added.
// Options :
// PCLZIP_OPT_ADD_PATH :
// PCLZIP_OPT_REMOVE_PATH :
// PCLZIP_OPT_REMOVE_ALL_PATH :
// PCLZIP_OPT_COMMENT :
// PCLZIP_OPT_ADD_COMMENT :
// PCLZIP_OPT_PREPEND_COMMENT :
// PCLZIP_CB_PRE_ADD :
// PCLZIP_CB_POST_ADD :
// Return Values :
// 0 on failure,
// The list of the added files, with a status of the add action.
// (see PclZip::listContent() for list entry format)
// --------------------------------------------------------------------------------
function add($p_filelist)
{
//--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, 'PclZip::add', "filelist='$p_filelist', ...");
$v_result=1;
// ----- Reset the error handler
$this->privErrorReset();
// ----- Set default values
$v_options = array();
$v_options[PCLZIP_OPT_NO_COMPRESSION] = FALSE;
// ----- Look for variable options arguments
$v_size = func_num_args();
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "$v_size arguments passed to the method");
// ----- Look for arguments
if ($v_size > 1) {
// ----- Get the arguments
$v_arg_list = func_get_args();
// ----- Remove form the options list the first argument
array_shift($v_arg_list);
$v_size--;
// ----- Look for first arg
if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Variable list of options detected");
// ----- Parse the options
$v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
array (PCLZIP_OPT_REMOVE_PATH => 'optional',
PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
PCLZIP_OPT_ADD_PATH => 'optional',
PCLZIP_CB_PRE_ADD => 'optional',
PCLZIP_CB_POST_ADD => 'optional',
PCLZIP_OPT_NO_COMPRESSION => 'optional',
PCLZIP_OPT_COMMENT => 'optional',
PCLZIP_OPT_ADD_COMMENT => 'optional',
PCLZIP_OPT_PREPEND_COMMENT => 'optional'
//, PCLZIP_OPT_CRYPT => 'optional'
));
if ($v_result != 1) {
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
return 0;
}
}
// ----- Look for 2 args
// Here we need to support the first historic synopsis of the
// method.
else {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Static synopsis");
// ----- Get the first argument
$v_options[PCLZIP_OPT_ADD_PATH] = $v_add_path = $v_arg_list[0];
// ----- Look for the optional second argument
if ($v_size == 2) {
$v_options[PCLZIP_OPT_REMOVE_PATH] = $v_arg_list[1];
}
else if ($v_size > 2) {
// ----- Error log
PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid number / type of arguments");
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
return 0;
}
}
}
// ----- Init
$v_string_list = array();
$v_att_list = array();
$v_filedescr_list = array();
$p_result_list = array();
// ----- Look if the $p_filelist is really an array
if (is_array($p_filelist)) {
// ----- Look if the first element is also an array
// This will mean that this is a file description entry
if (isset($p_filelist[0]) && is_array($p_filelist[0])) {
$v_att_list = $p_filelist;
}
// ----- The list is a list of string names
else {
$v_string_list = $p_filelist;
}
}
// ----- Look if the $p_filelist is a string
else if (is_string($p_filelist)) {
// ----- Create a list from the string
$v_string_list = explode(PCLZIP_SEPARATOR, $p_filelist);
}
// ----- Invalid variable type for $p_filelist
else {
PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type '".gettype($p_filelist)."' for p_filelist");
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
return 0;
}
// ----- Reformat the string list
if (sizeof($v_string_list) != 0) {
foreach ($v_string_list as $v_string) {
$v_att_list[][PCLZIP_ATT_FILE_NAME] = $v_string;
}
}
// ----- For each file in the list check the attributes
$v_supported_attributes
= array ( PCLZIP_ATT_FILE_NAME => 'mandatory'
,PCLZIP_ATT_FILE_NEW_SHORT_NAME => 'optional'
,PCLZIP_ATT_FILE_NEW_FULL_NAME => 'optional'
,PCLZIP_ATT_FILE_MTIME => 'optional'
,PCLZIP_ATT_FILE_CONTENT => 'optional'
,PCLZIP_ATT_FILE_COMMENT => 'optional'
);
foreach ($v_att_list as $v_entry) {
$v_result = $this->privFileDescrParseAtt($v_entry,
$v_filedescr_list[],
$v_options,
$v_supported_attributes);
if ($v_result != 1) {
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
return 0;
}
}
// ----- Expand the filelist (expand directories)
$v_result = $this->privFileDescrExpand($v_filedescr_list, $v_options);
if ($v_result != 1) {
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
return 0;
}
// ----- Call the create fct
$v_result = $this->privAdd($v_filedescr_list, $p_result_list, $v_options);
if ($v_result != 1) {
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
return 0;
}
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $p_result_list);
return $p_result_list;
}
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
// Function : listContent()
// Description :
// This public method, gives the list of the files and directories, with their
// properties.
// The properties of each entries in the list are (used also in other functions) :
// filename : Name of the file. For a create or add action it is the filename
// given by the user. For an extract function it is the filename
// of the extracted file.
// stored_filename : Name of the file / directory stored in the archive.
// size : Size of the stored file.
// compressed_size : Size of the file's data compressed in the archive
// (without the headers overhead)
// mtime : Last known modification date of the file (UNIX timestamp)
// comment : Comment associated with the file
// folder : true | false
// index : index of the file in the archive
// status : status of the action (depending of the action) :
// Values are :
// ok : OK !
// filtered : the file / dir is not extracted (filtered by user)
// already_a_directory : the file can not be extracted because a
// directory with the same name already exists
// write_protected : the file can not be extracted because a file
// with the same name already exists and is
// write protected
// newer_exist : the file was not extracted because a newer file exists
// path_creation_fail : the file is not extracted because the folder
// does not exists and can not be created
// write_error : the file was not extracted because there was a
// error while writing the file
// read_error : the file was not extracted because there was a error
// while reading the file
// invalid_header : the file was not extracted because of an archive
// format error (bad file header)
// Note that each time a method can continue operating when there
// is an action error on a file, the error is only logged in the file status.
// Return Values :
// 0 on an unrecoverable failure,
// The list of the files in the archive.
// --------------------------------------------------------------------------------
function listContent()
{
//--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, 'PclZip::listContent', "");
$v_result=1;
// ----- Reset the error handler
$this->privErrorReset();
// ----- Check archive
if (!$this->privCheckFormat()) {
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
return(0);
}
// ----- Call the extracting fct
$p_list = array();
if (($v_result = $this->privList($p_list)) != 1)
{
unset($p_list);
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0, PclZip::errorInfo());
return(0);
}
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $p_list);
return $p_list;
}
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
// Function :
// extract($p_path="./", $p_remove_path="")
// extract([$p_option, $p_option_value, ...])
// Description :
// This method supports two synopsis. The first one is historical.
// This method extract all the files / directories from the archive to the
// folder indicated in $p_path.
// If you want to ignore the 'root' part of path of the memorized files
// you can indicate this in the optional $p_remove_path parameter.
// By default, if a newer file with the same name already exists, the
// file is not extracted.
//
// If both PCLZIP_OPT_PATH and PCLZIP_OPT_ADD_PATH aoptions
// are used, the path indicated in PCLZIP_OPT_ADD_PATH is append
// at the end of the path value of PCLZIP_OPT_PATH.
// Parameters :
// $p_path : Path where the files and directories are to be extracted
// $p_remove_path : First part ('root' part) of the memorized path
// (if any similar) to remove while extracting.
// Options :
// PCLZIP_OPT_PATH :
// PCLZIP_OPT_ADD_PATH :
// PCLZIP_OPT_REMOVE_PATH :
// PCLZIP_OPT_REMOVE_ALL_PATH :
// PCLZIP_CB_PRE_EXTRACT :
// PCLZIP_CB_POST_EXTRACT :
// Return Values :
// 0 or a negative value on failure,
// The list of the extracted files, with a status of the action.
// (see PclZip::listContent() for list entry format)
// --------------------------------------------------------------------------------
function extract()
{
//--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::extract", "");
$v_result=1;
// ----- Reset the error handler
$this->privErrorReset();
// ----- Check archive
if (!$this->privCheckFormat()) {
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
return(0);
}
// ----- Set default values
$v_options = array();
// $v_path = "./";
$v_path = '';
$v_remove_path = "";
$v_remove_all_path = false;
// ----- Look for variable options arguments
$v_size = func_num_args();
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "$v_size arguments passed to the method");
// ----- Default values for option
$v_options[PCLZIP_OPT_EXTRACT_AS_STRING] = FALSE;
// ----- Look for arguments
if ($v_size > 0) {
// ----- Get the arguments
$v_arg_list = func_get_args();
// ----- Look for first arg
if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Variable list of options");
// ----- Parse the options
$v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
array (PCLZIP_OPT_PATH => 'optional',
PCLZIP_OPT_REMOVE_PATH => 'optional',
PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
PCLZIP_OPT_ADD_PATH => 'optional',
PCLZIP_CB_PRE_EXTRACT => 'optional',
PCLZIP_CB_POST_EXTRACT => 'optional',
PCLZIP_OPT_SET_CHMOD => 'optional',
PCLZIP_OPT_BY_NAME => 'optional',
PCLZIP_OPT_BY_EREG => 'optional',
PCLZIP_OPT_BY_PREG => 'optional',
PCLZIP_OPT_BY_INDEX => 'optional',
PCLZIP_OPT_EXTRACT_AS_STRING => 'optional',
PCLZIP_OPT_EXTRACT_IN_OUTPUT => 'optional',
PCLZIP_OPT_REPLACE_NEWER => 'optional'
,PCLZIP_OPT_STOP_ON_ERROR => 'optional'
,PCLZIP_OPT_EXTRACT_DIR_RESTRICTION => 'optional'
));
if ($v_result != 1) {
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
return 0;
}
// ----- Set the arguments
if (isset($v_options[PCLZIP_OPT_PATH])) {
$v_path = $v_options[PCLZIP_OPT_PATH];
}
if (isset($v_options[PCLZIP_OPT_REMOVE_PATH])) {
$v_remove_path = $v_options[PCLZIP_OPT_REMOVE_PATH];
}
if (isset($v_options[PCLZIP_OPT_REMOVE_ALL_PATH])) {
$v_remove_all_path = $v_options[PCLZIP_OPT_REMOVE_ALL_PATH];
}
if (isset($v_options[PCLZIP_OPT_ADD_PATH])) {
// ----- Check for '/' in last path char
if ((strlen($v_path) > 0) && (substr($v_path, -1) != '/')) {
$v_path .= '/';
}
$v_path .= $v_options[PCLZIP_OPT_ADD_PATH];
}
}
// ----- Look for 2 args
// Here we need to support the first historic synopsis of the
// method.
else {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Static synopsis");
// ----- Get the first argument
$v_path = $v_arg_list[0];
// ----- Look for the optional second argument
if ($v_size == 2) {
$v_remove_path = $v_arg_list[1];
}
else if ($v_size > 2) {
// ----- Error log
PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid number / type of arguments");
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0, PclZip::errorInfo());
return 0;
}
}
}
// ----- Trace
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "path='$v_path', remove_path='$v_remove_path', remove_all_path='".($v_remove_path?'true':'false')."'");
// ----- Call the extracting fct
$p_list = array();
$v_result = $this->privExtractByRule($p_list, $v_path, $v_remove_path,
$v_remove_all_path, $v_options);
if ($v_result < 1) {
unset($p_list);
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0, PclZip::errorInfo());
return(0);
}
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $p_list);
return $p_list;
}
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
// Function :
// extractByIndex($p_index, $p_path="./", $p_remove_path="")
// extractByIndex($p_index, [$p_option, $p_option_value, ...])
// Description :
// This method supports two synopsis. The first one is historical.
// This method is doing a partial extract of the archive.
// The extracted files or folders are identified by their index in the
// archive (from 0 to n).
// Note that if the index identify a folder, only the folder entry is
// extracted, not all the files included in the archive.
// Parameters :
// $p_index : A single index (integer) or a string of indexes of files to
// extract. The form of the string is "0,4-6,8-12" with only numbers
// and '-' for range or ',' to separate ranges. No spaces or ';'
// are allowed.
// $p_path : Path where the files and directories are to be extracted
// $p_remove_path : First part ('root' part) of the memorized path
// (if any similar) to remove while extracting.
// Options :
// PCLZIP_OPT_PATH :
// PCLZIP_OPT_ADD_PATH :
// PCLZIP_OPT_REMOVE_PATH :
// PCLZIP_OPT_REMOVE_ALL_PATH :
// PCLZIP_OPT_EXTRACT_AS_STRING : The files are extracted as strings and
// not as files.
// The resulting content is in a new field 'content' in the file
// structure.
// This option must be used alone (any other options are ignored).
// PCLZIP_CB_PRE_EXTRACT :
// PCLZIP_CB_POST_EXTRACT :
// Return Values :
// 0 on failure,
// The list of the extracted files, with a status of the action.
// (see PclZip::listContent() for list entry format)
// --------------------------------------------------------------------------------
//function extractByIndex($p_index, options...)
function extractByIndex($p_index)
{
//--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::extractByIndex", "index='$p_index', ...");
$v_result=1;
// ----- Reset the error handler
$this->privErrorReset();
// ----- Check archive
if (!$this->privCheckFormat()) {
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
return(0);
}
// ----- Set default values
$v_options = array();
// $v_path = "./";
$v_path = '';
$v_remove_path = "";
$v_remove_all_path = false;
// ----- Look for variable options arguments
$v_size = func_num_args();
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "$v_size arguments passed to the method");
// ----- Default values for option
$v_options[PCLZIP_OPT_EXTRACT_AS_STRING] = FALSE;
// ----- Look for arguments
if ($v_size > 1) {
// ----- Get the arguments
$v_arg_list = func_get_args();
// ----- Remove form the options list the first argument
array_shift($v_arg_list);
$v_size--;
// ----- Look for first arg
if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Variable list of options");
// ----- Parse the options
$v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
array (PCLZIP_OPT_PATH => 'optional',
PCLZIP_OPT_REMOVE_PATH => 'optional',
PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
PCLZIP_OPT_EXTRACT_AS_STRING => 'optional',
PCLZIP_OPT_ADD_PATH => 'optional',
PCLZIP_CB_PRE_EXTRACT => 'optional',
PCLZIP_CB_POST_EXTRACT => 'optional',
PCLZIP_OPT_SET_CHMOD => 'optional',
PCLZIP_OPT_REPLACE_NEWER => 'optional'
,PCLZIP_OPT_STOP_ON_ERROR => 'optional'
,PCLZIP_OPT_EXTRACT_DIR_RESTRICTION => 'optional'
));
if ($v_result != 1) {
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
return 0;
}
// ----- Set the arguments
if (isset($v_options[PCLZIP_OPT_PATH])) {
$v_path = $v_options[PCLZIP_OPT_PATH];
}
if (isset($v_options[PCLZIP_OPT_REMOVE_PATH])) {
$v_remove_path = $v_options[PCLZIP_OPT_REMOVE_PATH];
}
if (isset($v_options[PCLZIP_OPT_REMOVE_ALL_PATH])) {
$v_remove_all_path = $v_options[PCLZIP_OPT_REMOVE_ALL_PATH];
}
if (isset($v_options[PCLZIP_OPT_ADD_PATH])) {
// ----- Check for '/' in last path char
if ((strlen($v_path) > 0) && (substr($v_path, -1) != '/')) {
$v_path .= '/';
}
$v_path .= $v_options[PCLZIP_OPT_ADD_PATH];
}
if (!isset($v_options[PCLZIP_OPT_EXTRACT_AS_STRING])) {
$v_options[PCLZIP_OPT_EXTRACT_AS_STRING] = FALSE;
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Option PCLZIP_OPT_EXTRACT_AS_STRING not set.");
}
else {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Option PCLZIP_OPT_EXTRACT_AS_STRING set.");
}
}
// ----- Look for 2 args
// Here we need to support the first historic synopsis of the
// method.
else {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Static synopsis");
// ----- Get the first argument
$v_path = $v_arg_list[0];
// ----- Look for the optional second argument
if ($v_size == 2) {
$v_remove_path = $v_arg_list[1];
}
else if ($v_size > 2) {
// ----- Error log
PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid number / type of arguments");
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
return 0;
}
}
}
// ----- Trace
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "index='$p_index', path='$v_path', remove_path='$v_remove_path', remove_all_path='".($v_remove_path?'true':'false')."'");
// ----- Trick
// Here I want to reuse extractByRule(), so I need to parse the $p_index
// with privParseOptions()
$v_arg_trick = array (PCLZIP_OPT_BY_INDEX, $p_index);
$v_options_trick = array();
$v_result = $this->privParseOptions($v_arg_trick, sizeof($v_arg_trick), $v_options_trick,
array (PCLZIP_OPT_BY_INDEX => 'optional' ));
if ($v_result != 1) {
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
return 0;
}
$v_options[PCLZIP_OPT_BY_INDEX] = $v_options_trick[PCLZIP_OPT_BY_INDEX];
// ----- Call the extracting fct
if (($v_result = $this->privExtractByRule($p_list, $v_path, $v_remove_path, $v_remove_all_path, $v_options)) < 1) {
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0, PclZip::errorInfo());
return(0);
}
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $p_list);
return $p_list;
}
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
// Function :
// delete([$p_option, $p_option_value, ...])
// Description :
// This method removes files from the archive.
// If no parameters are given, then all the archive is emptied.
// Parameters :
// None or optional arguments.
// Options :
// PCLZIP_OPT_BY_INDEX :
// PCLZIP_OPT_BY_NAME :
// PCLZIP_OPT_BY_EREG :
// PCLZIP_OPT_BY_PREG :
// Return Values :
// 0 on failure,
// The list of the files which are still present in the archive.
// (see PclZip::listContent() for list entry format)
// --------------------------------------------------------------------------------
function delete()
{
//--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::delete", "");
$v_result=1;
// ----- Reset the error handler
$this->privErrorReset();
// ----- Check archive
if (!$this->privCheckFormat()) {
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
return(0);
}
// ----- Set default values
$v_options = array();
// ----- Look for variable options arguments
$v_size = func_num_args();
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "$v_size arguments passed to the method");
// ----- Look for arguments
if ($v_size > 0) {
// ----- Get the arguments
$v_arg_list = func_get_args();
// ----- Parse the options
$v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
array (PCLZIP_OPT_BY_NAME => 'optional',
PCLZIP_OPT_BY_EREG => 'optional',
PCLZIP_OPT_BY_PREG => 'optional',
PCLZIP_OPT_BY_INDEX => 'optional' ));
if ($v_result != 1) {
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
return 0;
}
}
// ----- Magic quotes trick
$this->privDisableMagicQuotes();
// ----- Call the delete fct
$v_list = array();
if (($v_result = $this->privDeleteByRule($v_list, $v_options)) != 1) {
$this->privSwapBackMagicQuotes();
unset($v_list);
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0, PclZip::errorInfo());
return(0);
}
// ----- Magic quotes trick
$this->privSwapBackMagicQuotes();
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_list);
return $v_list;
}
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
// Function : deleteByIndex()
// Description :
// ***** Deprecated *****
// delete(PCLZIP_OPT_BY_INDEX, $p_index) should be prefered.
// --------------------------------------------------------------------------------
function deleteByIndex($p_index)
{
//--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::deleteByIndex", "index='$p_index'");
$p_list = $this->delete(PCLZIP_OPT_BY_INDEX, $p_index);
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $p_list);
return $p_list;
}
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
// Function : properties()
// Description :
// This method gives the properties of the archive.
// The properties are :
// nb : Number of files in the archive
// comment : Comment associated with the archive file
// status : not_exist, ok
// Parameters :
// None
// Return Values :
// 0 on failure,
// An array with the archive properties.
// --------------------------------------------------------------------------------
function properties()
{
//--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::properties", "");
// ----- Reset the error handler
$this->privErrorReset();
// ----- Magic quotes trick
$this->privDisableMagicQuotes();
// ----- Check archive
if (!$this->privCheckFormat()) {
$this->privSwapBackMagicQuotes();
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
return(0);
}
// ----- Default properties
$v_prop = array();
$v_prop['comment'] = '';
$v_prop['nb'] = 0;
$v_prop['status'] = 'not_exist';
// ----- Look if file exists
if (@is_file($this->zipname))
{
// ----- Open the zip file
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
if (($this->zip_fd = @fopen($this->zipname, 'rb')) == 0)
{
$this->privSwapBackMagicQuotes();
// ----- Error log
PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \''.$this->zipname.'\' in binary read mode');
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), 0);
return 0;
}
// ----- Read the central directory informations
$v_central_dir = array();
if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
{
$this->privSwapBackMagicQuotes();
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
return 0;
}
// ----- Close the zip file
$this->privCloseFd();
// ----- Set the user attributes
$v_prop['comment'] = $v_central_dir['comment'];
$v_prop['nb'] = $v_central_dir['entries'];
$v_prop['status'] = 'ok';
}
// ----- Magic quotes trick
$this->privSwapBackMagicQuotes();
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_prop);
return $v_prop;
}
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
// Function : duplicate()
// Description :
// This method creates an archive by copying the content of an other one. If
// the archive already exist, it is replaced by the new one without any warning.
// Parameters :
// $p_archive : The filename of a valid archive, or
// a valid PclZip object.
// Return Values :
// 1 on success.
// 0 or a negative value on error (error code).
// --------------------------------------------------------------------------------
function duplicate($p_archive)
{
//--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::duplicate", "");
$v_result = 1;
// ----- Reset the error handler
$this->privErrorReset();
// ----- Look if the $p_archive is a PclZip object
if ((is_object($p_archive)) && (get_class($p_archive) == 'pclzip'))
{
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "The parameter is valid PclZip object '".$p_archive->zipname."'");
// ----- Duplicate the archive
$v_result = $this->privDuplicate($p_archive->zipname);
}
// ----- Look if the $p_archive is a string (so a filename)
else if (is_string($p_archive))
{
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "The parameter is a filename '$p_archive'");
// ----- Check that $p_archive is a valid zip file
// TBC : Should also check the archive format
if (!is_file($p_archive)) {
// ----- Error log
PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "No file with filename '".$p_archive."'");
$v_result = PCLZIP_ERR_MISSING_FILE;
}
else {
// ----- Duplicate the archive
$v_result = $this->privDuplicate($p_archive);
}
}
// ----- Invalid variable
else
{
// ----- Error log
PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type p_archive_to_add");
$v_result = PCLZIP_ERR_INVALID_PARAMETER;
}
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
// Function : merge()
// Description :
// This method merge the $p_archive_to_add archive at the end of the current
// one ($this).
// If the archive ($this) does not exist, the merge becomes a duplicate.
// If the $p_archive_to_add archive does not exist, the merge is a success.
// Parameters :
// $p_archive_to_add : It can be directly the filename of a valid zip archive,
// or a PclZip object archive.
// Return Values :
// 1 on success,
// 0 or negative values on error (see below).
// --------------------------------------------------------------------------------
function merge($p_archive_to_add)
{
//--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::merge", "");
$v_result = 1;
// ----- Reset the error handler
$this->privErrorReset();
// ----- Check archive
if (!$this->privCheckFormat()) {
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
return(0);
}
// ----- Look if the $p_archive_to_add is a PclZip object
if ((is_object($p_archive_to_add)) && (get_class($p_archive_to_add) == 'pclzip'))
{
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "The parameter is valid PclZip object");
// ----- Merge the archive
$v_result = $this->privMerge($p_archive_to_add);
}
// ----- Look if the $p_archive_to_add is a string (so a filename)
else if (is_string($p_archive_to_add))
{
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "The parameter is a filename");
// ----- Create a temporary archive
$v_object_archive = new PclZip($p_archive_to_add);
// ----- Merge the archive
$v_result = $this->privMerge($v_object_archive);
}
// ----- Invalid variable
else
{
// ----- Error log
PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type p_archive_to_add");
$v_result = PCLZIP_ERR_INVALID_PARAMETER;
}
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
// Function : errorCode()
// Description :
// Parameters :
// --------------------------------------------------------------------------------
function errorCode()
{
if (PCLZIP_ERROR_EXTERNAL == 1) {
return(PclErrorCode());
}
else {
return($this->error_code);
}
}
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
// Function : errorName()
// Description :
// Parameters :
// --------------------------------------------------------------------------------
function errorName($p_with_code=false)
{
$v_name = array ( PCLZIP_ERR_NO_ERROR => 'PCLZIP_ERR_NO_ERROR',
PCLZIP_ERR_WRITE_OPEN_FAIL => 'PCLZIP_ERR_WRITE_OPEN_FAIL',
PCLZIP_ERR_READ_OPEN_FAIL => 'PCLZIP_ERR_READ_OPEN_FAIL',
PCLZIP_ERR_INVALID_PARAMETER => 'PCLZIP_ERR_INVALID_PARAMETER',
PCLZIP_ERR_MISSING_FILE => 'PCLZIP_ERR_MISSING_FILE',
PCLZIP_ERR_FILENAME_TOO_LONG => 'PCLZIP_ERR_FILENAME_TOO_LONG',
PCLZIP_ERR_INVALID_ZIP => 'PCLZIP_ERR_INVALID_ZIP',
PCLZIP_ERR_BAD_EXTRACTED_FILE => 'PCLZIP_ERR_BAD_EXTRACTED_FILE',
PCLZIP_ERR_DIR_CREATE_FAIL => 'PCLZIP_ERR_DIR_CREATE_FAIL',
PCLZIP_ERR_BAD_EXTENSION => 'PCLZIP_ERR_BAD_EXTENSION',
PCLZIP_ERR_BAD_FORMAT => 'PCLZIP_ERR_BAD_FORMAT',
PCLZIP_ERR_DELETE_FILE_FAIL => 'PCLZIP_ERR_DELETE_FILE_FAIL',
PCLZIP_ERR_RENAME_FILE_FAIL => 'PCLZIP_ERR_RENAME_FILE_FAIL',
PCLZIP_ERR_BAD_CHECKSUM => 'PCLZIP_ERR_BAD_CHECKSUM',
PCLZIP_ERR_INVALID_ARCHIVE_ZIP => 'PCLZIP_ERR_INVALID_ARCHIVE_ZIP',
PCLZIP_ERR_MISSING_OPTION_VALUE => 'PCLZIP_ERR_MISSING_OPTION_VALUE',
PCLZIP_ERR_INVALID_OPTION_VALUE => 'PCLZIP_ERR_INVALID_OPTION_VALUE',
PCLZIP_ERR_UNSUPPORTED_COMPRESSION => 'PCLZIP_ERR_UNSUPPORTED_COMPRESSION',
PCLZIP_ERR_UNSUPPORTED_ENCRYPTION => 'PCLZIP_ERR_UNSUPPORTED_ENCRYPTION'
,PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE => 'PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE'
,PCLZIP_ERR_DIRECTORY_RESTRICTION => 'PCLZIP_ERR_DIRECTORY_RESTRICTION'
);
if (isset($v_name[$this->error_code])) {
$v_value = $v_name[$this->error_code];
}
else {
$v_value = 'NoName';
}
if ($p_with_code) {
return($v_value.' ('.$this->error_code.')');
}
else {
return($v_value);
}
}
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
// Function : errorInfo()
// Description :
// Parameters :
// --------------------------------------------------------------------------------
function errorInfo($p_full=false)
{
if (PCLZIP_ERROR_EXTERNAL == 1) {
return(PclErrorString());
}
else {
if ($p_full) {
return($this->errorName(true)." : ".$this->error_string);
}
else {
return($this->error_string." [code ".$this->error_code."]");
}
}
}
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
// ***** UNDER THIS LINE ARE DEFINED PRIVATE INTERNAL FUNCTIONS *****
// ***** *****
// ***** THESES FUNCTIONS MUST NOT BE USED DIRECTLY *****
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
// Function : privCheckFormat()
// Description :
// This method check that the archive exists and is a valid zip archive.
// Several level of check exists. (futur)
// Parameters :
// $p_level : Level of check. Default 0.
// 0 : Check the first bytes (magic codes) (default value))
// 1 : 0 + Check the central directory (futur)
// 2 : 1 + Check each file header (futur)
// Return Values :
// true on success,
// false on error, the error code is set.
// --------------------------------------------------------------------------------
function privCheckFormat($p_level=0)
{
//--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privCheckFormat", "");
$v_result = true;
// ----- Reset the file system cache
clearstatcache();
// ----- Reset the error handler
$this->privErrorReset();
// ----- Look if the file exits
if (!is_file($this->zipname)) {
// ----- Error log
PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "Missing archive file '".$this->zipname."'");
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, false, PclZip::errorInfo());
return(false);
}
// ----- Check that the file is readeable
if (!is_readable($this->zipname)) {
// ----- Error log
PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, "Unable to read archive '".$this->zipname."'");
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, false, PclZip::errorInfo());
return(false);
}
// ----- Check the magic code
// TBC
// ----- Check the central header
// TBC
// ----- Check each file header
// TBC
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
// Function : privParseOptions()
// Description :
// This internal methods reads the variable list of arguments ($p_options_list,
// $p_size) and generate an array with the options and values ($v_result_list).
// $v_requested_options contains the options that can be present and those that
// must be present.
// $v_requested_options is an array, with the option value as key, and 'optional',
// or 'mandatory' as value.
// Parameters :
// See above.
// Return Values :
// 1 on success.
// 0 on failure.
// --------------------------------------------------------------------------------
function privParseOptions(&$p_options_list, $p_size, &$v_result_list, $v_requested_options=false)
{
//--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privParseOptions", "");
$v_result=1;
// ----- Read the options
$i=0;
while ($i<$p_size) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Looking for table index $i, option = '".PclZipUtilOptionText($p_options_list[$i])."(".$p_options_list[$i].")'");
// ----- Check if the option is supported
if (!isset($v_requested_options[$p_options_list[$i]])) {
// ----- Error log
PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid optional parameter '".$p_options_list[$i]."' for this method");
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
return PclZip::errorCode();
}
// ----- Look for next option
switch ($p_options_list[$i]) {
// ----- Look for options that request a path value
case PCLZIP_OPT_PATH :
case PCLZIP_OPT_REMOVE_PATH :
case PCLZIP_OPT_ADD_PATH :
// ----- Check the number of parameters
if (($i+1) >= $p_size) {
// ----- Error log
PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
return PclZip::errorCode();
}
// ----- Get the value
$v_result_list[$p_options_list[$i]] = PclZipUtilTranslateWinPath($p_options_list[$i+1], FALSE);
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($p_options_list[$i])." = '".$v_result_list[$p_options_list[$i]]."'");
$i++;
break;
case PCLZIP_OPT_EXTRACT_DIR_RESTRICTION :
// ----- Check the number of parameters
if (($i+1) >= $p_size) {
// ----- Error log
PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
return PclZip::errorCode();
}
// ----- Get the value
if ( is_string($p_options_list[$i+1])
&& ($p_options_list[$i+1] != '')) {
$v_result_list[$p_options_list[$i]] = PclZipUtilTranslateWinPath($p_options_list[$i+1], FALSE);
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($p_options_list[$i])." = '".$v_result_list[$p_options_list[$i]]."'");
$i++;
}
else {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($p_options_list[$i])." set with an empty value is ignored.");
}
break;
// ----- Look for options that request an array of string for value
case PCLZIP_OPT_BY_NAME :
// ----- Check the number of parameters
if (($i+1) >= $p_size) {
// ----- Error log
PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
return PclZip::errorCode();
}
// ----- Get the value
if (is_string($p_options_list[$i+1])) {
$v_result_list[$p_options_list[$i]][0] = $p_options_list[$i+1];
}
else if (is_array($p_options_list[$i+1])) {
$v_result_list[$p_options_list[$i]] = $p_options_list[$i+1];
}
else {
// ----- Error log
PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Wrong parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
return PclZip::errorCode();
}
////--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($p_options_list[$i])." = '".$v_result_list[$p_options_list[$i]]."'");
$i++;
break;
// ----- Look for options that request an EREG or PREG expression
case PCLZIP_OPT_BY_EREG :
case PCLZIP_OPT_BY_PREG :
//case PCLZIP_OPT_CRYPT :
// ----- Check the number of parameters
if (($i+1) >= $p_size) {
// ----- Error log
PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
return PclZip::errorCode();
}
// ----- Get the value
if (is_string($p_options_list[$i+1])) {
$v_result_list[$p_options_list[$i]] = $p_options_list[$i+1];
}
else {
// ----- Error log
PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Wrong parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
return PclZip::errorCode();
}
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($p_options_list[$i])." = '".$v_result_list[$p_options_list[$i]]."'");
$i++;
break;
// ----- Look for options that takes a string
case PCLZIP_OPT_COMMENT :
case PCLZIP_OPT_ADD_COMMENT :
case PCLZIP_OPT_PREPEND_COMMENT :
// ----- Check the number of parameters
if (($i+1) >= $p_size) {
// ----- Error log
PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE,
"Missing parameter value for option '"
.PclZipUtilOptionText($p_options_list[$i])
."'");
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
return PclZip::errorCode();
}
// ----- Get the value
if (is_string($p_options_list[$i+1])) {
$v_result_list[$p_options_list[$i]] = $p_options_list[$i+1];
}
else {
// ----- Error log
PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE,
"Wrong parameter value for option '"
.PclZipUtilOptionText($p_options_list[$i])
."'");
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
return PclZip::errorCode();
}
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($p_options_list[$i])." = '".$v_result_list[$p_options_list[$i]]."'");
$i++;
break;
// ----- Look for options that request an array of index
case PCLZIP_OPT_BY_INDEX :
// ----- Check the number of parameters
if (($i+1) >= $p_size) {
// ----- Error log
PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
return PclZip::errorCode();
}
// ----- Get the value
$v_work_list = array();
if (is_string($p_options_list[$i+1])) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Index value is a string '".$p_options_list[$i+1]."'");
// ----- Remove spaces
$p_options_list[$i+1] = strtr($p_options_list[$i+1], ' ', '');
// ----- Parse items
$v_work_list = explode(",", $p_options_list[$i+1]);
}
else if (is_integer($p_options_list[$i+1])) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Index value is an integer '".$p_options_list[$i+1]."'");
$v_work_list[0] = $p_options_list[$i+1].'-'.$p_options_list[$i+1];
}
else if (is_array($p_options_list[$i+1])) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Index value is an array");
$v_work_list = $p_options_list[$i+1];
}
else {
// ----- Error log
PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Value must be integer, string or array for option '".PclZipUtilOptionText($p_options_list[$i])."'");
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
return PclZip::errorCode();
}
// ----- Reduce the index list
// each index item in the list must be a couple with a start and
// an end value : [0,3], [5-5], [8-10], ...
// ----- Check the format of each item
$v_sort_flag=false;
$v_sort_value=0;
for ($j=0; $j= $p_size) {
// ----- Error log
PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
return PclZip::errorCode();
}
// ----- Get the value
$v_result_list[$p_options_list[$i]] = $p_options_list[$i+1];
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($p_options_list[$i])." = '".$v_result_list[$p_options_list[$i]]."'");
$i++;
break;
// ----- Look for options that request a call-back
case PCLZIP_CB_PRE_EXTRACT :
case PCLZIP_CB_POST_EXTRACT :
case PCLZIP_CB_PRE_ADD :
case PCLZIP_CB_POST_ADD :
/* for futur use
case PCLZIP_CB_PRE_DELETE :
case PCLZIP_CB_POST_DELETE :
case PCLZIP_CB_PRE_LIST :
case PCLZIP_CB_POST_LIST :
*/
// ----- Check the number of parameters
if (($i+1) >= $p_size) {
// ----- Error log
PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
return PclZip::errorCode();
}
// ----- Get the value
$v_function_name = $p_options_list[$i+1];
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "call-back ".PclZipUtilOptionText($p_options_list[$i])." = '".$v_function_name."'");
// ----- Check that the value is a valid existing function
if (!function_exists($v_function_name)) {
// ----- Error log
PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Function '".$v_function_name."()' is not an existing function for option '".PclZipUtilOptionText($p_options_list[$i])."'");
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
return PclZip::errorCode();
}
// ----- Set the attribute
$v_result_list[$p_options_list[$i]] = $v_function_name;
$i++;
break;
default :
// ----- Error log
PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER,
"Unknown parameter '"
.$p_options_list[$i]."'");
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
return PclZip::errorCode();
}
// ----- Next options
$i++;
}
// ----- Look for mandatory options
if ($v_requested_options !== false) {
for ($key=reset($v_requested_options); $key=key($v_requested_options); $key=next($v_requested_options)) {
// ----- Look for mandatory option
if ($v_requested_options[$key] == 'mandatory') {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Detect a mandatory option : ".PclZipUtilOptionText($key)."(".$key.")");
// ----- Look if present
if (!isset($v_result_list[$key])) {
// ----- Error log
PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Missing mandatory parameter ".PclZipUtilOptionText($key)."(".$key.")");
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
return PclZip::errorCode();
}
}
}
}
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
// Function : privFileDescrParseAtt()
// Description :
// Parameters :
// Return Values :
// 1 on success.
// 0 on failure.
// --------------------------------------------------------------------------------
function privFileDescrParseAtt(&$p_file_list, &$p_filedescr, $v_options, $v_requested_options=false)
{
//--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privFileDescrParseAtt", "");
$v_result=1;
// ----- For each file in the list check the attributes
foreach ($p_file_list as $v_key => $v_value) {
// ----- Check if the option is supported
if (!isset($v_requested_options[$v_key])) {
// ----- Error log
PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid file attribute '".$v_key."' for this file");
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
return PclZip::errorCode();
}
// ----- Look for attribute
switch ($v_key) {
case PCLZIP_ATT_FILE_NAME :
if (!is_string($v_value)) {
PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type ".gettype($v_value).". String expected for attribute '".PclZipUtilOptionText($v_key)."'");
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
return PclZip::errorCode();
}
$p_filedescr['filename'] = PclZipUtilPathReduction($v_value);
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($v_key)." = '".$v_value."'");
if ($p_filedescr['filename'] == '') {
PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid empty filename for attribute '".PclZipUtilOptionText($v_key)."'");
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
return PclZip::errorCode();
}
break;
case PCLZIP_ATT_FILE_NEW_SHORT_NAME :
if (!is_string($v_value)) {
PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type ".gettype($v_value).". String expected for attribute '".PclZipUtilOptionText($v_key)."'");
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
return PclZip::errorCode();
}
$p_filedescr['new_short_name'] = PclZipUtilPathReduction($v_value);
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($v_key)." = '".$v_value."'");
if ($p_filedescr['new_short_name'] == '') {
PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid empty short filename for attribute '".PclZipUtilOptionText($v_key)."'");
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
return PclZip::errorCode();
}
break;
case PCLZIP_ATT_FILE_NEW_FULL_NAME :
if (!is_string($v_value)) {
PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type ".gettype($v_value).". String expected for attribute '".PclZipUtilOptionText($v_key)."'");
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
return PclZip::errorCode();
}
$p_filedescr['new_full_name'] = PclZipUtilPathReduction($v_value);
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($v_key)." = '".$v_value."'");
if ($p_filedescr['new_full_name'] == '') {
PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid empty full filename for attribute '".PclZipUtilOptionText($v_key)."'");
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
return PclZip::errorCode();
}
break;
// ----- Look for options that takes a string
case PCLZIP_ATT_FILE_COMMENT :
if (!is_string($v_value)) {
PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type ".gettype($v_value).". String expected for attribute '".PclZipUtilOptionText($v_key)."'");
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
return PclZip::errorCode();
}
$p_filedescr['comment'] = $v_value;
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($v_key)." = '".$v_value."'");
break;
case PCLZIP_ATT_FILE_MTIME :
if (!is_integer($v_value)) {
PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type ".gettype($v_value).". Integer expected for attribute '".PclZipUtilOptionText($v_key)."'");
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
return PclZip::errorCode();
}
$p_filedescr['mtime'] = $v_value;
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($v_key)." = '".$v_value."'");
break;
case PCLZIP_ATT_FILE_CONTENT :
$p_filedescr['content'] = $v_value;
////--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($v_key)." = '".$v_value."'");
break;
default :
// ----- Error log
PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER,
"Unknown parameter '".$v_key."'");
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
return PclZip::errorCode();
}
// ----- Look for mandatory options
if ($v_requested_options !== false) {
for ($key=reset($v_requested_options); $key=key($v_requested_options); $key=next($v_requested_options)) {
// ----- Look for mandatory option
if ($v_requested_options[$key] == 'mandatory') {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Detect a mandatory option : ".PclZipUtilOptionText($key)."(".$key.")");
// ----- Look if present
if (!isset($p_file_list[$key])) {
PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Missing mandatory parameter ".PclZipUtilOptionText($key)."(".$key.")");
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
return PclZip::errorCode();
}
}
}
}
// end foreach
}
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
// Function : privFileDescrExpand()
// Description :
// Parameters :
// Return Values :
// 1 on success.
// 0 on failure.
// --------------------------------------------------------------------------------
function privFileDescrExpand(&$p_filedescr_list, &$p_options)
{
//--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privFileDescrExpand", "");
$v_result=1;
// ----- Create a result list
$v_result_list = array();
// ----- Look each entry
for ($i=0; $iprivCalculateStoredFilename($v_descr, $p_options);
// ----- Add the descriptor in result list
$v_result_list[sizeof($v_result_list)] = $v_descr;
// ----- Look for folder
if ($v_descr['type'] == 'folder') {
// ----- List of items in folder
$v_dirlist_descr = array();
$v_dirlist_nb = 0;
if ($v_folder_handler = @opendir($v_descr['filename'])) {
while (($v_item_handler = @readdir($v_folder_handler)) !== false) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Looking for '".$v_item_handler."' in the directory");
// ----- Skip '.' and '..'
if (($v_item_handler == '.') || ($v_item_handler == '..')) {
continue;
}
// ----- Compose the full filename
$v_dirlist_descr[$v_dirlist_nb]['filename'] = $v_descr['filename'].'/'.$v_item_handler;
// ----- Look for different stored filename
// Because the name of the folder was changed, the name of the
// files/sub-folders also change
if ($v_descr['stored_filename'] != $v_descr['filename']) {
if ($v_descr['stored_filename'] != '') {
$v_dirlist_descr[$v_dirlist_nb]['new_full_name'] = $v_descr['stored_filename'].'/'.$v_item_handler;
}
else {
$v_dirlist_descr[$v_dirlist_nb]['new_full_name'] = $v_item_handler;
}
}
$v_dirlist_nb++;
}
@closedir($v_folder_handler);
}
else {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Unable to open dir '".$v_descr['filename']."' in read mode. Skipped.");
// TBC : unable to open folder in read mode
}
// ----- Expand each element of the list
if ($v_dirlist_nb != 0) {
// ----- Expand
if (($v_result = $this->privFileDescrExpand($v_dirlist_descr, $p_options)) != 1) {
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// ----- Concat the resulting list
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Merging result list (size '".sizeof($v_result_list)."') with dirlist (size '".sizeof($v_dirlist_descr)."')");
$v_result_list = array_merge($v_result_list, $v_dirlist_descr);
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "merged result list is size '".sizeof($v_result_list)."'");
}
else {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Nothing in this folder to expand.");
}
// ----- Free local array
unset($v_dirlist_descr);
}
}
// ----- Get the result list
$p_filedescr_list = $v_result_list;
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
// Function : privCreate()
// Description :
// Parameters :
// Return Values :
// --------------------------------------------------------------------------------
function privCreate($p_filedescr_list, &$p_result_list, &$p_options)
{
//--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privCreate", "list");
$v_result=1;
$v_list_detail = array();
// ----- Magic quotes trick
$this->privDisableMagicQuotes();
// ----- Open the file in write mode
if (($v_result = $this->privOpenFd('wb')) != 1)
{
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// ----- Add the list of files
$v_result = $this->privAddList($p_filedescr_list, $p_result_list, $p_options);
// ----- Close
$this->privCloseFd();
// ----- Magic quotes trick
$this->privSwapBackMagicQuotes();
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
// Function : privAdd()
// Description :
// Parameters :
// Return Values :
// --------------------------------------------------------------------------------
function privAdd($p_filedescr_list, &$p_result_list, &$p_options)
{
//--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privAdd", "list");
$v_result=1;
$v_list_detail = array();
// ----- Look if the archive exists or is empty
if ((!is_file($this->zipname)) || (filesize($this->zipname) == 0))
{
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Archive does not exist, or is empty, create it.");
// ----- Do a create
$v_result = $this->privCreate($p_filedescr_list, $p_result_list, $p_options);
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// ----- Magic quotes trick
$this->privDisableMagicQuotes();
// ----- Open the zip file
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
if (($v_result=$this->privOpenFd('rb')) != 1)
{
// ----- Magic quotes trick
$this->privSwapBackMagicQuotes();
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// ----- Read the central directory informations
$v_central_dir = array();
if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
{
$this->privCloseFd();
$this->privSwapBackMagicQuotes();
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// ----- Go to beginning of File
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position in file : ".ftell($this->zip_fd)."'");
@rewind($this->zip_fd);
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position in file : ".ftell($this->zip_fd)."'");
// ----- Creates a temporay file
$v_zip_temp_name = PCLZIP_TEMPORARY_DIR.uniqid('pclzip-').'.tmp';
// ----- Open the temporary file in write mode
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
if (($v_zip_temp_fd = @fopen($v_zip_temp_name, 'wb')) == 0)
{
$this->privCloseFd();
$this->privSwapBackMagicQuotes();
PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \''.$v_zip_temp_name.'\' in binary write mode');
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
return PclZip::errorCode();
}
// ----- Copy the files from the archive to the temporary file
// TBC : Here I should better append the file and go back to erase the central dir
$v_size = $v_central_dir['offset'];
while ($v_size != 0)
{
$v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Read $v_read_size bytes");
$v_buffer = fread($this->zip_fd, $v_read_size);
@fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
$v_size -= $v_read_size;
}
// ----- Swap the file descriptor
// Here is a trick : I swap the temporary fd with the zip fd, in order to use
// the following methods on the temporary fil and not the real archive
$v_swap = $this->zip_fd;
$this->zip_fd = $v_zip_temp_fd;
$v_zip_temp_fd = $v_swap;
// ----- Add the files
$v_header_list = array();
if (($v_result = $this->privAddFileList($p_filedescr_list, $v_header_list, $p_options)) != 1)
{
fclose($v_zip_temp_fd);
$this->privCloseFd();
@unlink($v_zip_temp_name);
$this->privSwapBackMagicQuotes();
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// ----- Store the offset of the central dir
$v_offset = @ftell($this->zip_fd);
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "New offset of central dir : $v_offset");
// ----- Copy the block of file headers from the old archive
$v_size = $v_central_dir['size'];
while ($v_size != 0)
{
$v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Read $v_read_size bytes");
$v_buffer = @fread($v_zip_temp_fd, $v_read_size);
@fwrite($this->zip_fd, $v_buffer, $v_read_size);
$v_size -= $v_read_size;
}
// ----- Create the Central Dir files header
for ($i=0, $v_count=0; $iprivWriteCentralFileHeader($v_header_list[$i])) != 1) {
fclose($v_zip_temp_fd);
$this->privCloseFd();
@unlink($v_zip_temp_name);
$this->privSwapBackMagicQuotes();
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
$v_count++;
}
// ----- Transform the header to a 'usable' info
$this->privConvertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
}
// ----- Zip file comment
$v_comment = $v_central_dir['comment'];
if (isset($p_options[PCLZIP_OPT_COMMENT])) {
$v_comment = $p_options[PCLZIP_OPT_COMMENT];
}
if (isset($p_options[PCLZIP_OPT_ADD_COMMENT])) {
$v_comment = $v_comment.$p_options[PCLZIP_OPT_ADD_COMMENT];
}
if (isset($p_options[PCLZIP_OPT_PREPEND_COMMENT])) {
$v_comment = $p_options[PCLZIP_OPT_PREPEND_COMMENT].$v_comment;
}
// ----- Calculate the size of the central header
$v_size = @ftell($this->zip_fd)-$v_offset;
// ----- Create the central dir footer
if (($v_result = $this->privWriteCentralHeader($v_count+$v_central_dir['entries'], $v_size, $v_offset, $v_comment)) != 1)
{
// ----- Reset the file list
unset($v_header_list);
$this->privSwapBackMagicQuotes();
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// ----- Swap back the file descriptor
$v_swap = $this->zip_fd;
$this->zip_fd = $v_zip_temp_fd;
$v_zip_temp_fd = $v_swap;
// ----- Close
$this->privCloseFd();
// ----- Close the temporary file
@fclose($v_zip_temp_fd);
// ----- Magic quotes trick
$this->privSwapBackMagicQuotes();
// ----- Delete the zip file
// TBC : I should test the result ...
@unlink($this->zipname);
// ----- Rename the temporary file
// TBC : I should test the result ...
//@rename($v_zip_temp_name, $this->zipname);
PclZipUtilRename($v_zip_temp_name, $this->zipname);
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
// Function : privOpenFd()
// Description :
// Parameters :
// --------------------------------------------------------------------------------
function privOpenFd($p_mode)
{
//--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privOpenFd", 'mode='.$p_mode);
$v_result=1;
// ----- Look if already open
if ($this->zip_fd != 0)
{
// ----- Error log
PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Zip file \''.$this->zipname.'\' already open');
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
return PclZip::errorCode();
}
// ----- Open the zip file
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Open file in '.$p_mode.' mode');
if (($this->zip_fd = @fopen($this->zipname, $p_mode)) == 0)
{
// ----- Error log
PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \''.$this->zipname.'\' in '.$p_mode.' mode');
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
return PclZip::errorCode();
}
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
// Function : privCloseFd()
// Description :
// Parameters :
// --------------------------------------------------------------------------------
function privCloseFd()
{
//--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privCloseFd", "");
$v_result=1;
if ($this->zip_fd != 0)
@fclose($this->zip_fd);
$this->zip_fd = 0;
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
// Function : privAddList()
// Description :
// $p_add_dir and $p_remove_dir will give the ability to memorize a path which is
// different from the real path of the file. This is usefull if you want to have PclTar
// running in any directory, and memorize relative path from an other directory.
// Parameters :
// $p_list : An array containing the file or directory names to add in the tar
// $p_result_list : list of added files with their properties (specially the status field)
// $p_add_dir : Path to add in the filename path archived
// $p_remove_dir : Path to remove in the filename path archived
// Return Values :
// --------------------------------------------------------------------------------
// function privAddList($p_list, &$p_result_list, $p_add_dir, $p_remove_dir, $p_remove_all_dir, &$p_options)
function privAddList($p_filedescr_list, &$p_result_list, &$p_options)
{
//--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privAddList", "list");
$v_result=1;
// ----- Add the files
$v_header_list = array();
if (($v_result = $this->privAddFileList($p_filedescr_list, $v_header_list, $p_options)) != 1)
{
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// ----- Store the offset of the central dir
$v_offset = @ftell($this->zip_fd);
// ----- Create the Central Dir files header
for ($i=0,$v_count=0; $iprivWriteCentralFileHeader($v_header_list[$i])) != 1) {
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
$v_count++;
}
// ----- Transform the header to a 'usable' info
$this->privConvertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
}
// ----- Zip file comment
$v_comment = '';
if (isset($p_options[PCLZIP_OPT_COMMENT])) {
$v_comment = $p_options[PCLZIP_OPT_COMMENT];
}
// ----- Calculate the size of the central header
$v_size = @ftell($this->zip_fd)-$v_offset;
// ----- Create the central dir footer
if (($v_result = $this->privWriteCentralHeader($v_count, $v_size, $v_offset, $v_comment)) != 1)
{
// ----- Reset the file list
unset($v_header_list);
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
// Function : privAddFileList()
// Description :
// Parameters :
// $p_filedescr_list : An array containing the file description
// or directory names to add in the zip
// $p_result_list : list of added files with their properties (specially the status field)
// Return Values :
// --------------------------------------------------------------------------------
function privAddFileList($p_filedescr_list, &$p_result_list, &$p_options)
{
//--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privAddFileList", "filedescr_list");
$v_result=1;
$v_header = array();
// ----- Recuperate the current number of elt in list
$v_nb = sizeof($p_result_list);
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Before add, list have ".$v_nb." elements");
// ----- Loop on the files
for ($j=0; ($jprivAddFile($p_filedescr_list[$j], $v_header,
$p_options);
if ($v_result != 1) {
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// ----- Store the file infos
$p_result_list[$v_nb++] = $v_header;
}
}
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "After add, list have ".$v_nb." elements");
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
// Function : privAddFile()
// Description :
// Parameters :
// Return Values :
// --------------------------------------------------------------------------------
function privAddFile($p_filedescr, &$p_header, &$p_options)
{
//--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privAddFile", "filename='".$p_filedescr['filename']."'");
$v_result=1;
// ----- Working variable
$p_filename = $p_filedescr['filename'];
// TBC : Already done in the fileAtt check ... ?
if ($p_filename == "") {
// ----- Error log
PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid file list parameter (invalid or empty list)");
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
return PclZip::errorCode();
}
// ----- Look for a stored different filename
/* TBC : Removed
if (isset($p_filedescr['stored_filename'])) {
$v_stored_filename = $p_filedescr['stored_filename'];
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, 'Stored filename is NOT the same "'.$v_stored_filename.'"');
}
else {
$v_stored_filename = $p_filedescr['stored_filename'];
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, 'Stored filename is the same');
}
*/
// ----- Set the file properties
clearstatcache();
$p_header['version'] = 20;
$p_header['version_extracted'] = 10;
$p_header['flag'] = 0;
$p_header['compression'] = 0;
$p_header['crc'] = 0;
$p_header['compressed_size'] = 0;
$p_header['filename_len'] = strlen($p_filename);
$p_header['extra_len'] = 0;
$p_header['disk'] = 0;
$p_header['internal'] = 0;
$p_header['offset'] = 0;
$p_header['filename'] = $p_filename;
// TBC : Removed $p_header['stored_filename'] = $v_stored_filename;
$p_header['stored_filename'] = $p_filedescr['stored_filename'];
$p_header['extra'] = '';
$p_header['status'] = 'ok';
$p_header['index'] = -1;
// ----- Look for regular file
if ($p_filedescr['type']=='file') {
$p_header['external'] = 0x00000000;
$p_header['size'] = filesize($p_filename);
}
// ----- Look for regular folder
else if ($p_filedescr['type']=='folder') {
$p_header['external'] = 0x00000010;
$p_header['mtime'] = filemtime($p_filename);
$p_header['size'] = filesize($p_filename);
}
// ----- Look for virtual file
else if ($p_filedescr['type'] == 'virtual_file') {
$p_header['external'] = 0x00000000;
$p_header['size'] = strlen($p_filedescr['content']);
}
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Header external extension '".sprintf("0x%X",$p_header['external'])."'");
// ----- Look for filetime
if (isset($p_filedescr['mtime'])) {
$p_header['mtime'] = $p_filedescr['mtime'];
}
else if ($p_filedescr['type'] == 'virtual_file') {
$p_header['mtime'] = mktime();
}
else {
$p_header['mtime'] = filemtime($p_filename);
}
// ------ Look for file comment
if (isset($p_filedescr['comment'])) {
$p_header['comment_len'] = strlen($p_filedescr['comment']);
$p_header['comment'] = $p_filedescr['comment'];
}
else {
$p_header['comment_len'] = 0;
$p_header['comment'] = '';
}
// ----- Look for pre-add callback
if (isset($p_options[PCLZIP_CB_PRE_ADD])) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "A pre-callback '".$p_options[PCLZIP_CB_PRE_ADD]."()') is defined for the extraction");
// ----- Generate a local information
$v_local_header = array();
$this->privConvertHeader2FileInfo($p_header, $v_local_header);
// ----- Call the callback
// Here I do not use call_user_func() because I need to send a reference to the
// header.
eval('$v_result = '.$p_options[PCLZIP_CB_PRE_ADD].'(PCLZIP_CB_PRE_ADD, $v_local_header);');
if ($v_result == 0) {
// ----- Change the file status
$p_header['status'] = "skipped";
$v_result = 1;
}
// ----- Update the informations
// Only some fields can be modified
if ($p_header['stored_filename'] != $v_local_header['stored_filename']) {
$p_header['stored_filename'] = PclZipUtilPathReduction($v_local_header['stored_filename']);
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "New stored filename is '".$p_header['stored_filename']."'");
}
}
// ----- Look for empty stored filename
if ($p_header['stored_filename'] == "") {
$p_header['status'] = "filtered";
}
// ----- Check the path length
if (strlen($p_header['stored_filename']) > 0xFF) {
$p_header['status'] = 'filename_too_long';
}
// ----- Look if no error, or file not skipped
if ($p_header['status'] == 'ok') {
// ----- Look for a file
// if (is_file($p_filename))
if ( ($p_filedescr['type'] == 'file')
|| ($p_filedescr['type'] == 'virtual_file')) {
// ----- Get content from real file
if ($p_filedescr['type'] == 'file') {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "'".$p_filename."' is a file");
// ----- Open the source file
if (($v_file = @fopen($p_filename, "rb")) == 0) {
PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, "Unable to open file '$p_filename' in binary read mode");
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
return PclZip::errorCode();
}
// ----- Read the file content
$v_content = @fread($v_file, $p_header['size']);
// ----- Close the file
@fclose($v_file);
}
else if ($p_filedescr['type'] == 'virtual_file') {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Add by string");
$v_content = $p_filedescr['content'];
}
// ----- Calculate the CRC
$p_header['crc'] = @crc32($v_content);
// ----- Look for no compression
if ($p_options[PCLZIP_OPT_NO_COMPRESSION]) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "File will not be compressed");
// ----- Set header parameters
$p_header['compressed_size'] = $p_header['size'];
$p_header['compression'] = 0;
}
// ----- Look for normal compression
else {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "File will be compressed");
// ----- Compress the content
$v_content = @gzdeflate($v_content);
// ----- Set header parameters
$p_header['compressed_size'] = strlen($v_content);
$p_header['compression'] = 8;
}
// ----- Look for encryption
/*
if ((isset($p_options[PCLZIP_OPT_CRYPT]))
&& ($p_options[PCLZIP_OPT_CRYPT] != "")) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "File need to be crypted ....");
// Should be a random header
$v_header = 'xxxxxxxxxxxx';
$v_content_compressed = PclZipUtilZipEncrypt($v_content_compressed,
$p_header['compressed_size'],
$v_header,
$p_header['crc'],
"test");
$p_header['compressed_size'] += 12;
$p_header['flag'] = 1;
// ----- Add the header to the data
$v_content_compressed = $v_header.$v_content_compressed;
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Size after header : ".strlen($v_content_compressed)."");
}
*/
// ----- Call the header generation
if (($v_result = $this->privWriteFileHeader($p_header)) != 1) {
@fclose($v_file);
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// ----- Write the compressed (or not) content
@fwrite($this->zip_fd, $v_content, $p_header['compressed_size']);
}
// ----- Look for a directory
else if ($p_filedescr['type'] == 'folder') {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "'".$p_filename."' is a folder");
// ----- Look for directory last '/'
if (@substr($p_header['stored_filename'], -1) != '/') {
$p_header['stored_filename'] .= '/';
}
// ----- Set the file properties
$p_header['size'] = 0;
//$p_header['external'] = 0x41FF0010; // Value for a folder : to be checked
$p_header['external'] = 0x00000010; // Value for a folder : to be checked
// ----- Call the header generation
if (($v_result = $this->privWriteFileHeader($p_header)) != 1)
{
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
}
}
// ----- Look for post-add callback
if (isset($p_options[PCLZIP_CB_POST_ADD])) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "A post-callback '".$p_options[PCLZIP_CB_POST_ADD]."()') is defined for the extraction");
// ----- Generate a local information
$v_local_header = array();
$this->privConvertHeader2FileInfo($p_header, $v_local_header);
// ----- Call the callback
// Here I do not use call_user_func() because I need to send a reference to the
// header.
eval('$v_result = '.$p_options[PCLZIP_CB_POST_ADD].'(PCLZIP_CB_POST_ADD, $v_local_header);');
if ($v_result == 0) {
// ----- Ignored
$v_result = 1;
}
// ----- Update the informations
// Nothing can be modified
}
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
// Function : privCalculateStoredFilename()
// Description :
// Based on file descriptor properties and global options, this method
// calculate the filename that will be stored in the archive.
// Parameters :
// Return Values :
// --------------------------------------------------------------------------------
function privCalculateStoredFilename(&$p_filedescr, &$p_options)
{
//--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privCalculateStoredFilename", "filename='".$p_filedescr['filename']."'");
$v_result=1;
// ----- Working variables
$p_filename = $p_filedescr['filename'];
if (isset($p_options[PCLZIP_OPT_ADD_PATH])) {
$p_add_dir = $p_options[PCLZIP_OPT_ADD_PATH];
}
else {
$p_add_dir = '';
}
if (isset($p_options[PCLZIP_OPT_REMOVE_PATH])) {
$p_remove_dir = $p_options[PCLZIP_OPT_REMOVE_PATH];
}
else {
$p_remove_dir = '';
}
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Remove path ='".$p_remove_dir."'");
if (isset($p_options[PCLZIP_OPT_REMOVE_ALL_PATH])) {
$p_remove_all_dir = $p_options[PCLZIP_OPT_REMOVE_ALL_PATH];
}
else {
$p_remove_all_dir = 0;
}
// ----- Look for full name change
if (isset($p_filedescr['new_full_name'])) {
$v_stored_filename = $p_filedescr['new_full_name'];
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Changing full name of '".$p_filename."' for '".$v_stored_filename."'");
}
// ----- Look for path and/or short name change
else {
// ----- Look for short name change
if (isset($p_filedescr['new_short_name'])) {
$v_path_info = pathinfo($p_filename);
$v_dir = '';
if ($v_path_info['dirname'] != '') {
$v_dir = $v_path_info['dirname'].'/';
}
$v_stored_filename = $v_dir.$p_filedescr['new_short_name'];
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Changing short name of '".$p_filename."' for '".$v_stored_filename."'");
}
else {
// ----- Calculate the stored filename
$v_stored_filename = $p_filename;
}
// ----- Look for all path to remove
if ($p_remove_all_dir) {
$v_stored_filename = basename($p_filename);
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Remove all path selected change '".$p_filename."' for '".$v_stored_filename."'");
}
// ----- Look for partial path remove
else if ($p_remove_dir != "") {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Partial path to remove");
if (substr($p_remove_dir, -1) != '/')
$p_remove_dir .= "/";
if ( (substr($p_filename, 0, 2) == "./")
|| (substr($p_remove_dir, 0, 2) == "./")) {
if ( (substr($p_filename, 0, 2) == "./")
&& (substr($p_remove_dir, 0, 2) != "./")) {
$p_remove_dir = "./".$p_remove_dir;
}
if ( (substr($p_filename, 0, 2) != "./")
&& (substr($p_remove_dir, 0, 2) == "./")) {
$p_remove_dir = substr($p_remove_dir, 2);
}
}
$v_compare = PclZipUtilPathInclusion($p_remove_dir,
$v_stored_filename);
if ($v_compare > 0) {
if ($v_compare == 2) {
$v_stored_filename = "";
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Path to remove is the current folder");
}
else {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Remove path '$p_remove_dir' in file '$v_stored_filename'");
$v_stored_filename = substr($v_stored_filename,
strlen($p_remove_dir));
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Result is '$v_stored_filename'");
}
}
}
// ----- Look for path to add
if ($p_add_dir != "") {
if (substr($p_add_dir, -1) == "/")
$v_stored_filename = $p_add_dir.$v_stored_filename;
else
$v_stored_filename = $p_add_dir."/".$v_stored_filename;
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Add path '$p_add_dir' in file '$p_filename' = '$v_stored_filename'");
}
}
// ----- Filename (reduce the path of stored name)
$v_stored_filename = PclZipUtilPathReduction($v_stored_filename);
$p_filedescr['stored_filename'] = $v_stored_filename;
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Stored filename will be '".$p_filedescr['stored_filename']."', strlen ".strlen($p_filedescr['stored_filename']));
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
// Function : privWriteFileHeader()
// Description :
// Parameters :
// Return Values :
// --------------------------------------------------------------------------------
function privWriteFileHeader(&$p_header)
{
//--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privWriteFileHeader", 'file="'.$p_header['filename'].'", stored as "'.$p_header['stored_filename'].'"');
$v_result=1;
// ----- Store the offset position of the file
$p_header['offset'] = ftell($this->zip_fd);
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, 'File offset of the header :'.$p_header['offset']);
// ----- Transform UNIX mtime to DOS format mdate/mtime
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Date : \''.date("d/m/y H:i:s", $p_header['mtime']).'\'');
$v_date = getdate($p_header['mtime']);
$v_mtime = ($v_date['hours']<<11) + ($v_date['minutes']<<5) + $v_date['seconds']/2;
$v_mdate = (($v_date['year']-1980)<<9) + ($v_date['mon']<<5) + $v_date['mday'];
// ----- Packed data
$v_binary_data = pack("VvvvvvVVVvv", 0x04034b50,
$p_header['version_extracted'], $p_header['flag'],
$p_header['compression'], $v_mtime, $v_mdate,
$p_header['crc'], $p_header['compressed_size'],
$p_header['size'],
strlen($p_header['stored_filename']),
$p_header['extra_len']);
// ----- Write the first 148 bytes of the header in the archive
fputs($this->zip_fd, $v_binary_data, 30);
// ----- Write the variable fields
if (strlen($p_header['stored_filename']) != 0)
{
fputs($this->zip_fd, $p_header['stored_filename'], strlen($p_header['stored_filename']));
}
if ($p_header['extra_len'] != 0)
{
fputs($this->zip_fd, $p_header['extra'], $p_header['extra_len']);
}
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
// Function : privWriteCentralFileHeader()
// Description :
// Parameters :
// Return Values :
// --------------------------------------------------------------------------------
function privWriteCentralFileHeader(&$p_header)
{
//--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privWriteCentralFileHeader", 'file="'.$p_header['filename'].'", stored as "'.$p_header['stored_filename'].'"');
$v_result=1;
// TBC
//for(reset($p_header); $key = key($p_header); next($p_header)) {
// //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "header[$key] = ".$p_header[$key]);
//}
// ----- Transform UNIX mtime to DOS format mdate/mtime
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Date : \''.date("d/m/y H:i:s", $p_header['mtime']).'\'');
$v_date = getdate($p_header['mtime']);
$v_mtime = ($v_date['hours']<<11) + ($v_date['minutes']<<5) + $v_date['seconds']/2;
$v_mdate = (($v_date['year']-1980)<<9) + ($v_date['mon']<<5) + $v_date['mday'];
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Comment size : \''.$p_header['comment_len'].'\'');
// ----- Packed data
$v_binary_data = pack("VvvvvvvVVVvvvvvVV", 0x02014b50,
$p_header['version'], $p_header['version_extracted'],
$p_header['flag'], $p_header['compression'],
$v_mtime, $v_mdate, $p_header['crc'],
$p_header['compressed_size'], $p_header['size'],
strlen($p_header['stored_filename']),
$p_header['extra_len'], $p_header['comment_len'],
$p_header['disk'], $p_header['internal'],
$p_header['external'], $p_header['offset']);
// ----- Write the 42 bytes of the header in the zip file
fputs($this->zip_fd, $v_binary_data, 46);
// ----- Write the variable fields
if (strlen($p_header['stored_filename']) != 0)
{
fputs($this->zip_fd, $p_header['stored_filename'], strlen($p_header['stored_filename']));
}
if ($p_header['extra_len'] != 0)
{
fputs($this->zip_fd, $p_header['extra'], $p_header['extra_len']);
}
if ($p_header['comment_len'] != 0)
{
fputs($this->zip_fd, $p_header['comment'], $p_header['comment_len']);
}
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
// Function : privWriteCentralHeader()
// Description :
// Parameters :
// Return Values :
// --------------------------------------------------------------------------------
function privWriteCentralHeader($p_nb_entries, $p_size, $p_offset, $p_comment)
{
//--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privWriteCentralHeader", 'nb_entries='.$p_nb_entries.', size='.$p_size.', offset='.$p_offset.', comment="'.$p_comment.'"');
$v_result=1;
// ----- Packed data
$v_binary_data = pack("VvvvvVVv", 0x06054b50, 0, 0, $p_nb_entries,
$p_nb_entries, $p_size,
$p_offset, strlen($p_comment));
// ----- Write the 22 bytes of the header in the zip file
fputs($this->zip_fd, $v_binary_data, 22);
// ----- Write the variable fields
if (strlen($p_comment) != 0)
{
fputs($this->zip_fd, $p_comment, strlen($p_comment));
}
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
// Function : privList()
// Description :
// Parameters :
// Return Values :
// --------------------------------------------------------------------------------
function privList(&$p_list)
{
//--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privList", "list");
$v_result=1;
// ----- Magic quotes trick
$this->privDisableMagicQuotes();
// ----- Open the zip file
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
if (($this->zip_fd = @fopen($this->zipname, 'rb')) == 0)
{
// ----- Magic quotes trick
$this->privSwapBackMagicQuotes();
// ----- Error log
PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \''.$this->zipname.'\' in binary read mode');
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
return PclZip::errorCode();
}
// ----- Read the central directory informations
$v_central_dir = array();
if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
{
$this->privSwapBackMagicQuotes();
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// ----- Go to beginning of Central Dir
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Offset : ".$v_central_dir['offset']."'");
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Position in file : ".ftell($this->zip_fd)."'");
@rewind($this->zip_fd);
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Position in file : ".ftell($this->zip_fd)."'");
if (@fseek($this->zip_fd, $v_central_dir['offset']))
{
$this->privSwapBackMagicQuotes();
// ----- Error log
PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
return PclZip::errorCode();
}
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Position in file : ".ftell($this->zip_fd)."'");
// ----- Read each entry
for ($i=0; $i<$v_central_dir['entries']; $i++)
{
// ----- Read the file header
if (($v_result = $this->privReadCentralFileHeader($v_header)) != 1)
{
$this->privSwapBackMagicQuotes();
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
$v_header['index'] = $i;
// ----- Get the only interesting attributes
$this->privConvertHeader2FileInfo($v_header, $p_list[$i]);
unset($v_header);
}
// ----- Close the zip file
$this->privCloseFd();
// ----- Magic quotes trick
$this->privSwapBackMagicQuotes();
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
// Function : privConvertHeader2FileInfo()
// Description :
// This function takes the file informations from the central directory
// entries and extract the interesting parameters that will be given back.
// The resulting file infos are set in the array $p_info
// $p_info['filename'] : Filename with full path. Given by user (add),
// extracted in the filesystem (extract).
// $p_info['stored_filename'] : Stored filename in the archive.
// $p_info['size'] = Size of the file.
// $p_info['compressed_size'] = Compressed size of the file.
// $p_info['mtime'] = Last modification date of the file.
// $p_info['comment'] = Comment associated with the file.
// $p_info['folder'] = true/false : indicates if the entry is a folder or not.
// $p_info['status'] = status of the action on the file.
// $p_info['crc'] = CRC of the file content.
// Parameters :
// Return Values :
// --------------------------------------------------------------------------------
function privConvertHeader2FileInfo($p_header, &$p_info)
{
//--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privConvertHeader2FileInfo", "Filename='".$p_header['filename']."'");
$v_result=1;
// ----- Get the interesting attributes
$p_info['filename'] = $p_header['filename'];
$p_info['stored_filename'] = $p_header['stored_filename'];
$p_info['size'] = $p_header['size'];
$p_info['compressed_size'] = $p_header['compressed_size'];
$p_info['mtime'] = $p_header['mtime'];
$p_info['comment'] = $p_header['comment'];
$p_info['folder'] = (($p_header['external']&0x00000010)==0x00000010);
$p_info['index'] = $p_header['index'];
$p_info['status'] = $p_header['status'];
$p_info['crc'] = $p_header['crc'];
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
// Function : privExtractByRule()
// Description :
// Extract a file or directory depending of rules (by index, by name, ...)
// Parameters :
// $p_file_list : An array where will be placed the properties of each
// extracted file
// $p_path : Path to add while writing the extracted files
// $p_remove_path : Path to remove (from the file memorized path) while writing the
// extracted files. If the path does not match the file path,
// the file is extracted with its memorized path.
// $p_remove_path does not apply to 'list' mode.
// $p_path and $p_remove_path are commulative.
// Return Values :
// 1 on success,0 or less on error (see error code list)
// --------------------------------------------------------------------------------
function privExtractByRule(&$p_file_list, $p_path, $p_remove_path, $p_remove_all_path, &$p_options)
{
//--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privExtractByRule", "path='$p_path', remove_path='$p_remove_path', remove_all_path='".($p_remove_all_path?'true':'false')."'");
$v_result=1;
// ----- Magic quotes trick
$this->privDisableMagicQuotes();
// ----- Check the path
if ( ($p_path == "")
|| ( (substr($p_path, 0, 1) != "/")
&& (substr($p_path, 0, 3) != "../")
&& (substr($p_path,1,2)!=":/")))
$p_path = "./".$p_path;
// ----- Reduce the path last (and duplicated) '/'
if (($p_path != "./") && ($p_path != "/"))
{
// ----- Look for the path end '/'
while (substr($p_path, -1) == "/")
{
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Destination path [$p_path] ends by '/'");
$p_path = substr($p_path, 0, strlen($p_path)-1);
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Modified to [$p_path]");
}
}
// ----- Look for path to remove format (should end by /)
if (($p_remove_path != "") && (substr($p_remove_path, -1) != '/'))
{
$p_remove_path .= '/';
}
$p_remove_path_size = strlen($p_remove_path);
// ----- Open the zip file
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
if (($v_result = $this->privOpenFd('rb')) != 1)
{
$this->privSwapBackMagicQuotes();
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// ----- Read the central directory informations
$v_central_dir = array();
if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
{
// ----- Close the zip file
$this->privCloseFd();
$this->privSwapBackMagicQuotes();
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// ----- Start at beginning of Central Dir
$v_pos_entry = $v_central_dir['offset'];
// ----- Read each entry
$j_start = 0;
for ($i=0, $v_nb_extracted=0; $i<$v_central_dir['entries']; $i++)
{
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Read next file header entry : '$i'");
// ----- Read next Central dir entry
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Position before rewind : ".ftell($this->zip_fd)."'");
@rewind($this->zip_fd);
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Position after rewind : ".ftell($this->zip_fd)."'");
if (@fseek($this->zip_fd, $v_pos_entry))
{
// ----- Close the zip file
$this->privCloseFd();
$this->privSwapBackMagicQuotes();
// ----- Error log
PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
return PclZip::errorCode();
}
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Position after fseek : ".ftell($this->zip_fd)."'");
// ----- Read the file header
$v_header = array();
if (($v_result = $this->privReadCentralFileHeader($v_header)) != 1)
{
// ----- Close the zip file
$this->privCloseFd();
$this->privSwapBackMagicQuotes();
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// ----- Store the index
$v_header['index'] = $i;
// ----- Store the file position
$v_pos_entry = ftell($this->zip_fd);
// ----- Look for the specific extract rules
$v_extract = false;
// ----- Look for extract by name rule
if ( (isset($p_options[PCLZIP_OPT_BY_NAME]))
&& ($p_options[PCLZIP_OPT_BY_NAME] != 0)) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Extract with rule 'ByName'");
// ----- Look if the filename is in the list
for ($j=0; ($j strlen($p_options[PCLZIP_OPT_BY_NAME][$j]))
&& (substr($v_header['stored_filename'], 0, strlen($p_options[PCLZIP_OPT_BY_NAME][$j])) == $p_options[PCLZIP_OPT_BY_NAME][$j])) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "The directory is in the file path");
$v_extract = true;
}
}
// ----- Look for a filename
elseif ($v_header['stored_filename'] == $p_options[PCLZIP_OPT_BY_NAME][$j]) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "The file is the right one.");
$v_extract = true;
}
}
}
// ----- Look for extract by ereg rule
else if ( (isset($p_options[PCLZIP_OPT_BY_EREG]))
&& ($p_options[PCLZIP_OPT_BY_EREG] != "")) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Extract by ereg '".$p_options[PCLZIP_OPT_BY_EREG]."'");
if (ereg($p_options[PCLZIP_OPT_BY_EREG], $v_header['stored_filename'])) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Filename match the regular expression");
$v_extract = true;
}
}
// ----- Look for extract by preg rule
else if ( (isset($p_options[PCLZIP_OPT_BY_PREG]))
&& ($p_options[PCLZIP_OPT_BY_PREG] != "")) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Extract with rule 'ByEreg'");
if (preg_match($p_options[PCLZIP_OPT_BY_PREG], $v_header['stored_filename'])) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Filename match the regular expression");
$v_extract = true;
}
}
// ----- Look for extract by index rule
else if ( (isset($p_options[PCLZIP_OPT_BY_INDEX]))
&& ($p_options[PCLZIP_OPT_BY_INDEX] != 0)) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Extract with rule 'ByIndex'");
// ----- Look if the index is in the list
for ($j=$j_start; ($j=$p_options[PCLZIP_OPT_BY_INDEX][$j]['start']) && ($i<=$p_options[PCLZIP_OPT_BY_INDEX][$j]['end'])) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Found as part of an index range");
$v_extract = true;
}
if ($i>=$p_options[PCLZIP_OPT_BY_INDEX][$j]['end']) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Do not look this index range for next loop");
$j_start = $j+1;
}
if ($p_options[PCLZIP_OPT_BY_INDEX][$j]['start']>$i) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Index range is greater than index, stop loop");
break;
}
}
}
// ----- Look for no rule, which means extract all the archive
else {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Extract with no rule (extract all)");
$v_extract = true;
}
// ----- Check compression method
if ( ($v_extract)
&& ( ($v_header['compression'] != 8)
&& ($v_header['compression'] != 0))) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Unsupported compression method (".$v_header['compression'].")");
$v_header['status'] = 'unsupported_compression';
// ----- Look for PCLZIP_OPT_STOP_ON_ERROR
if ( (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
&& ($p_options[PCLZIP_OPT_STOP_ON_ERROR]===true)) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "PCLZIP_OPT_STOP_ON_ERROR is selected, extraction will be stopped");
$this->privSwapBackMagicQuotes();
PclZip::privErrorLog(PCLZIP_ERR_UNSUPPORTED_COMPRESSION,
"Filename '".$v_header['stored_filename']."' is "
."compressed by an unsupported compression "
."method (".$v_header['compression'].") ");
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
return PclZip::errorCode();
}
}
// ----- Check encrypted files
if (($v_extract) && (($v_header['flag'] & 1) == 1)) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Unsupported file encryption");
$v_header['status'] = 'unsupported_encryption';
// ----- Look for PCLZIP_OPT_STOP_ON_ERROR
if ( (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
&& ($p_options[PCLZIP_OPT_STOP_ON_ERROR]===true)) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "PCLZIP_OPT_STOP_ON_ERROR is selected, extraction will be stopped");
$this->privSwapBackMagicQuotes();
PclZip::privErrorLog(PCLZIP_ERR_UNSUPPORTED_ENCRYPTION,
"Unsupported encryption for "
." filename '".$v_header['stored_filename']
."'");
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
return PclZip::errorCode();
}
}
// ----- Look for real extraction
if (($v_extract) && ($v_header['status'] != 'ok')) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "No need for extract");
$v_result = $this->privConvertHeader2FileInfo($v_header,
$p_file_list[$v_nb_extracted++]);
if ($v_result != 1) {
$this->privCloseFd();
$this->privSwapBackMagicQuotes();
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
$v_extract = false;
}
// ----- Look for real extraction
if ($v_extract)
{
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extracting file '".$v_header['filename']."', index '$i'");
// ----- Go to the file position
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position before rewind : ".ftell($this->zip_fd)."'");
@rewind($this->zip_fd);
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position after rewind : ".ftell($this->zip_fd)."'");
if (@fseek($this->zip_fd, $v_header['offset']))
{
// ----- Close the zip file
$this->privCloseFd();
$this->privSwapBackMagicQuotes();
// ----- Error log
PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
return PclZip::errorCode();
}
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position after fseek : ".ftell($this->zip_fd)."'");
// ----- Look for extraction as string
if ($p_options[PCLZIP_OPT_EXTRACT_AS_STRING]) {
// ----- Extracting the file
$v_result1 = $this->privExtractFileAsString($v_header, $v_string);
if ($v_result1 < 1) {
$this->privCloseFd();
$this->privSwapBackMagicQuotes();
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result1);
return $v_result1;
}
// ----- Get the only interesting attributes
if (($v_result = $this->privConvertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted])) != 1)
{
// ----- Close the zip file
$this->privCloseFd();
$this->privSwapBackMagicQuotes();
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// ----- Set the file content
$p_file_list[$v_nb_extracted]['content'] = $v_string;
// ----- Next extracted file
$v_nb_extracted++;
// ----- Look for user callback abort
if ($v_result1 == 2) {
break;
}
}
// ----- Look for extraction in standard output
elseif ( (isset($p_options[PCLZIP_OPT_EXTRACT_IN_OUTPUT]))
&& ($p_options[PCLZIP_OPT_EXTRACT_IN_OUTPUT])) {
// ----- Extracting the file in standard output
$v_result1 = $this->privExtractFileInOutput($v_header, $p_options);
if ($v_result1 < 1) {
$this->privCloseFd();
$this->privSwapBackMagicQuotes();
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result1);
return $v_result1;
}
// ----- Get the only interesting attributes
if (($v_result = $this->privConvertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted++])) != 1) {
$this->privCloseFd();
$this->privSwapBackMagicQuotes();
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// ----- Look for user callback abort
if ($v_result1 == 2) {
break;
}
}
// ----- Look for normal extraction
else {
// ----- Extracting the file
$v_result1 = $this->privExtractFile($v_header,
$p_path, $p_remove_path,
$p_remove_all_path,
$p_options);
if ($v_result1 < 1) {
$this->privCloseFd();
$this->privSwapBackMagicQuotes();
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result1);
return $v_result1;
}
// ----- Get the only interesting attributes
if (($v_result = $this->privConvertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted++])) != 1)
{
// ----- Close the zip file
$this->privCloseFd();
$this->privSwapBackMagicQuotes();
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// ----- Look for user callback abort
if ($v_result1 == 2) {
break;
}
}
}
}
// ----- Close the zip file
$this->privCloseFd();
$this->privSwapBackMagicQuotes();
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
// Function : privExtractFile()
// Description :
// Parameters :
// Return Values :
//
// 1 : ... ?
// PCLZIP_ERR_USER_ABORTED(2) : User ask for extraction stop in callback
// --------------------------------------------------------------------------------
function privExtractFile(&$p_entry, $p_path, $p_remove_path, $p_remove_all_path, &$p_options)
{
//--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, 'PclZip::privExtractFile', "path='$p_path', remove_path='$p_remove_path', remove_all_path='".($p_remove_all_path?'true':'false')."'");
$v_result=1;
// ----- Read the file header
if (($v_result = $this->privReadFileHeader($v_header)) != 1)
{
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Found file '".$v_header['filename']."', size '".$v_header['size']."'");
// ----- Check that the file header is coherent with $p_entry info
if ($this->privCheckFileHeaders($v_header, $p_entry) != 1) {
// TBC
}
// ----- Look for all path to remove
if ($p_remove_all_path == true) {
// ----- Look for folder entry that not need to be extracted
if (($p_entry['external']&0x00000010)==0x00000010) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "The entry is a folder : need to be filtered");
$p_entry['status'] = "filtered";
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "All path is removed");
// ----- Get the basename of the path
$p_entry['filename'] = basename($p_entry['filename']);
}
// ----- Look for path to remove
else if ($p_remove_path != "")
{
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Look for some path to remove");
if (PclZipUtilPathInclusion($p_remove_path, $p_entry['filename']) == 2)
{
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "The folder is the same as the removed path '".$p_entry['filename']."'");
// ----- Change the file status
$p_entry['status'] = "filtered";
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
$p_remove_path_size = strlen($p_remove_path);
if (substr($p_entry['filename'], 0, $p_remove_path_size) == $p_remove_path)
{
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Found path '$p_remove_path' to remove in file '".$p_entry['filename']."'");
// ----- Remove the path
$p_entry['filename'] = substr($p_entry['filename'], $p_remove_path_size);
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Resulting file is '".$p_entry['filename']."'");
}
}
// ----- Add the path
if ($p_path != '') {
$p_entry['filename'] = $p_path."/".$p_entry['filename'];
}
// ----- Check a base_dir_restriction
if (isset($p_options[PCLZIP_OPT_EXTRACT_DIR_RESTRICTION])) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Check the extract directory restriction");
$v_inclusion
= PclZipUtilPathInclusion($p_options[PCLZIP_OPT_EXTRACT_DIR_RESTRICTION],
$p_entry['filename']);
if ($v_inclusion == 0) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "PCLZIP_OPT_EXTRACT_DIR_RESTRICTION is selected, file is outside restriction");
PclZip::privErrorLog(PCLZIP_ERR_DIRECTORY_RESTRICTION,
"Filename '".$p_entry['filename']."' is "
."outside PCLZIP_OPT_EXTRACT_DIR_RESTRICTION");
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
return PclZip::errorCode();
}
}
// ----- Look for pre-extract callback
if (isset($p_options[PCLZIP_CB_PRE_EXTRACT])) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "A pre-callback '".$p_options[PCLZIP_CB_PRE_EXTRACT]."()') is defined for the extraction");
// ----- Generate a local information
$v_local_header = array();
$this->privConvertHeader2FileInfo($p_entry, $v_local_header);
// ----- Call the callback
// Here I do not use call_user_func() because I need to send a reference to the
// header.
eval('$v_result = '.$p_options[PCLZIP_CB_PRE_EXTRACT].'(PCLZIP_CB_PRE_EXTRACT, $v_local_header);');
if ($v_result == 0) {
// ----- Change the file status
$p_entry['status'] = "skipped";
$v_result = 1;
}
// ----- Look for abort result
if ($v_result == 2) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "User callback abort the extraction");
// ----- This status is internal and will be changed in 'skipped'
$p_entry['status'] = "aborted";
$v_result = PCLZIP_ERR_USER_ABORTED;
}
// ----- Update the informations
// Only some fields can be modified
$p_entry['filename'] = $v_local_header['filename'];
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "New filename is '".$p_entry['filename']."'");
}
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extracting file (with path) '".$p_entry['filename']."', size '$v_header[size]'");
// ----- Look if extraction should be done
if ($p_entry['status'] == 'ok') {
// ----- Look for specific actions while the file exist
if (file_exists($p_entry['filename']))
{
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "File '".$p_entry['filename']."' already exists");
// ----- Look if file is a directory
if (is_dir($p_entry['filename']))
{
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Existing file '".$p_entry['filename']."' is a directory");
// ----- Change the file status
$p_entry['status'] = "already_a_directory";
// ----- Look for PCLZIP_OPT_STOP_ON_ERROR
// For historical reason first PclZip implementation does not stop
// when this kind of error occurs.
if ( (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
&& ($p_options[PCLZIP_OPT_STOP_ON_ERROR]===true)) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "PCLZIP_OPT_STOP_ON_ERROR is selected, extraction will be stopped");
PclZip::privErrorLog(PCLZIP_ERR_ALREADY_A_DIRECTORY,
"Filename '".$p_entry['filename']."' is "
."already used by an existing directory");
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
return PclZip::errorCode();
}
}
// ----- Look if file is write protected
else if (!is_writeable($p_entry['filename']))
{
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Existing file '".$p_entry['filename']."' is write protected");
// ----- Change the file status
$p_entry['status'] = "write_protected";
// ----- Look for PCLZIP_OPT_STOP_ON_ERROR
// For historical reason first PclZip implementation does not stop
// when this kind of error occurs.
if ( (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
&& ($p_options[PCLZIP_OPT_STOP_ON_ERROR]===true)) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "PCLZIP_OPT_STOP_ON_ERROR is selected, extraction will be stopped");
PclZip::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL,
"Filename '".$p_entry['filename']."' exists "
."and is write protected");
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
return PclZip::errorCode();
}
}
// ----- Look if the extracted file is older
else if (filemtime($p_entry['filename']) > $p_entry['mtime'])
{
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Existing file '".$p_entry['filename']."' is newer (".date("l dS of F Y h:i:s A", filemtime($p_entry['filename'])).") than the extracted file (".date("l dS of F Y h:i:s A", $p_entry['mtime']).")");
// ----- Change the file status
if ( (isset($p_options[PCLZIP_OPT_REPLACE_NEWER]))
&& ($p_options[PCLZIP_OPT_REPLACE_NEWER]===true)) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "PCLZIP_OPT_REPLACE_NEWER is selected, file will be replaced");
}
else {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "File will not be replaced");
$p_entry['status'] = "newer_exist";
// ----- Look for PCLZIP_OPT_STOP_ON_ERROR
// For historical reason first PclZip implementation does not stop
// when this kind of error occurs.
if ( (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
&& ($p_options[PCLZIP_OPT_STOP_ON_ERROR]===true)) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "PCLZIP_OPT_STOP_ON_ERROR is selected, extraction will be stopped");
PclZip::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL,
"Newer version of '".$p_entry['filename']."' exists "
."and option PCLZIP_OPT_REPLACE_NEWER is not selected");
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
return PclZip::errorCode();
}
}
}
else {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Existing file '".$p_entry['filename']."' is older than the extrated one - will be replaced by the extracted one (".date("l dS of F Y h:i:s A", filemtime($p_entry['filename'])).") than the extracted file (".date("l dS of F Y h:i:s A", $p_entry['mtime']).")");
}
}
// ----- Check the directory availability and create it if necessary
else {
if ((($p_entry['external']&0x00000010)==0x00000010) || (substr($p_entry['filename'], -1) == '/'))
$v_dir_to_check = $p_entry['filename'];
else if (!strstr($p_entry['filename'], "/"))
$v_dir_to_check = "";
else
$v_dir_to_check = dirname($p_entry['filename']);
if (($v_result = $this->privDirCheck($v_dir_to_check, (($p_entry['external']&0x00000010)==0x00000010))) != 1) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Unable to create path for '".$p_entry['filename']."'");
// ----- Change the file status
$p_entry['status'] = "path_creation_fail";
// ----- Return
////--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
//return $v_result;
$v_result = 1;
}
}
}
// ----- Look if extraction should be done
if ($p_entry['status'] == 'ok') {
// ----- Do the extraction (if not a folder)
if (!(($p_entry['external']&0x00000010)==0x00000010))
{
// ----- Look for not compressed file
if ($p_entry['compression'] == 0) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extracting an un-compressed file");
// ----- Opening destination file
if (($v_dest_file = @fopen($p_entry['filename'], 'wb')) == 0)
{
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Error while opening '".$p_entry['filename']."' in write binary mode");
// ----- Change the file status
$p_entry['status'] = "write_error";
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Read '".$p_entry['size']."' bytes");
// ----- Read the file by PCLZIP_READ_BLOCK_SIZE octets blocks
$v_size = $p_entry['compressed_size'];
while ($v_size != 0)
{
$v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Read $v_read_size bytes");
$v_buffer = @fread($this->zip_fd, $v_read_size);
/* Try to speed up the code
$v_binary_data = pack('a'.$v_read_size, $v_buffer);
@fwrite($v_dest_file, $v_binary_data, $v_read_size);
*/
@fwrite($v_dest_file, $v_buffer, $v_read_size);
$v_size -= $v_read_size;
}
// ----- Closing the destination file
fclose($v_dest_file);
// ----- Change the file mtime
touch($p_entry['filename'], $p_entry['mtime']);
}
else {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extracting a compressed file (Compression method ".$p_entry['compression'].")");
// ----- TBC
// Need to be finished
if (($p_entry['flag'] & 1) == 1) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "File is encrypted");
/*
// ----- Read the encryption header
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Read 12 encryption header bytes");
$v_encryption_header = @fread($this->zip_fd, 12);
// ----- Read the encrypted & compressed file in a buffer
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Read '".($p_entry['compressed_size']-12)."' compressed & encrypted bytes");
$v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']-12);
// ----- Decrypt the buffer
$this->privDecrypt($v_encryption_header, $v_buffer,
$p_entry['compressed_size']-12, $p_entry['crc']);
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Buffer is '".$v_buffer."'");
*/
}
else {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Read '".$p_entry['compressed_size']."' compressed bytes");
// ----- Read the compressed file in a buffer (one shot)
$v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']);
}
// ----- Decompress the file
$v_file_content = @gzinflate($v_buffer);
unset($v_buffer);
if ($v_file_content === FALSE) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Unable to inflate compressed file");
// ----- Change the file status
// TBC
$p_entry['status'] = "error";
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// ----- Opening destination file
if (($v_dest_file = @fopen($p_entry['filename'], 'wb')) == 0) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Error while opening '".$p_entry['filename']."' in write binary mode");
// ----- Change the file status
$p_entry['status'] = "write_error";
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// ----- Write the uncompressed data
@fwrite($v_dest_file, $v_file_content, $p_entry['size']);
unset($v_file_content);
// ----- Closing the destination file
@fclose($v_dest_file);
// ----- Change the file mtime
@touch($p_entry['filename'], $p_entry['mtime']);
}
// ----- Look for chmod option
if (isset($p_options[PCLZIP_OPT_SET_CHMOD])) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "chmod option activated '".$p_options[PCLZIP_OPT_SET_CHMOD]."'");
// ----- Change the mode of the file
@chmod($p_entry['filename'], $p_options[PCLZIP_OPT_SET_CHMOD]);
}
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extraction done");
}
}
// ----- Change abort status
if ($p_entry['status'] == "aborted") {
$p_entry['status'] = "skipped";
}
// ----- Look for post-extract callback
elseif (isset($p_options[PCLZIP_CB_POST_EXTRACT])) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "A post-callback '".$p_options[PCLZIP_CB_POST_EXTRACT]."()') is defined for the extraction");
// ----- Generate a local information
$v_local_header = array();
$this->privConvertHeader2FileInfo($p_entry, $v_local_header);
// ----- Call the callback
// Here I do not use call_user_func() because I need to send a reference to the
// header.
eval('$v_result = '.$p_options[PCLZIP_CB_POST_EXTRACT].'(PCLZIP_CB_POST_EXTRACT, $v_local_header);');
// ----- Look for abort result
if ($v_result == 2) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "User callback abort the extraction");
$v_result = PCLZIP_ERR_USER_ABORTED;
}
}
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
// Function : privExtractFileInOutput()
// Description :
// Parameters :
// Return Values :
// --------------------------------------------------------------------------------
function privExtractFileInOutput(&$p_entry, &$p_options)
{
//--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, 'PclZip::privExtractFileInOutput', "");
$v_result=1;
// ----- Read the file header
if (($v_result = $this->privReadFileHeader($v_header)) != 1) {
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Found file '".$v_header['filename']."', size '".$v_header['size']."'");
// ----- Check that the file header is coherent with $p_entry info
if ($this->privCheckFileHeaders($v_header, $p_entry) != 1) {
// TBC
}
// ----- Look for pre-extract callback
if (isset($p_options[PCLZIP_CB_PRE_EXTRACT])) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "A pre-callback '".$p_options[PCLZIP_CB_PRE_EXTRACT]."()') is defined for the extraction");
// ----- Generate a local information
$v_local_header = array();
$this->privConvertHeader2FileInfo($p_entry, $v_local_header);
// ----- Call the callback
// Here I do not use call_user_func() because I need to send a reference to the
// header.
eval('$v_result = '.$p_options[PCLZIP_CB_PRE_EXTRACT].'(PCLZIP_CB_PRE_EXTRACT, $v_local_header);');
if ($v_result == 0) {
// ----- Change the file status
$p_entry['status'] = "skipped";
$v_result = 1;
}
// ----- Look for abort result
if ($v_result == 2) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "User callback abort the extraction");
// ----- This status is internal and will be changed in 'skipped'
$p_entry['status'] = "aborted";
$v_result = PCLZIP_ERR_USER_ABORTED;
}
// ----- Update the informations
// Only some fields can be modified
$p_entry['filename'] = $v_local_header['filename'];
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "New filename is '".$p_entry['filename']."'");
}
// ----- Trace
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extracting file (with path) '".$p_entry['filename']."', size '$v_header[size]'");
// ----- Look if extraction should be done
if ($p_entry['status'] == 'ok') {
// ----- Do the extraction (if not a folder)
if (!(($p_entry['external']&0x00000010)==0x00000010)) {
// ----- Look for not compressed file
if ($p_entry['compressed_size'] == $p_entry['size']) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extracting an un-compressed file");
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Reading '".$p_entry['size']."' bytes");
// ----- Read the file in a buffer (one shot)
$v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']);
// ----- Send the file to the output
echo $v_buffer;
unset($v_buffer);
}
else {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extracting a compressed file");
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Reading '".$p_entry['size']."' bytes");
// ----- Read the compressed file in a buffer (one shot)
$v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']);
// ----- Decompress the file
$v_file_content = gzinflate($v_buffer);
unset($v_buffer);
// ----- Send the file to the output
echo $v_file_content;
unset($v_file_content);
}
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extraction done");
}
}
// ----- Change abort status
if ($p_entry['status'] == "aborted") {
$p_entry['status'] = "skipped";
}
// ----- Look for post-extract callback
elseif (isset($p_options[PCLZIP_CB_POST_EXTRACT])) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "A post-callback '".$p_options[PCLZIP_CB_POST_EXTRACT]."()') is defined for the extraction");
// ----- Generate a local information
$v_local_header = array();
$this->privConvertHeader2FileInfo($p_entry, $v_local_header);
// ----- Call the callback
// Here I do not use call_user_func() because I need to send a reference to the
// header.
eval('$v_result = '.$p_options[PCLZIP_CB_POST_EXTRACT].'(PCLZIP_CB_POST_EXTRACT, $v_local_header);');
// ----- Look for abort result
if ($v_result == 2) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "User callback abort the extraction");
$v_result = PCLZIP_ERR_USER_ABORTED;
}
}
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
// Function : privExtractFileAsString()
// Description :
// Parameters :
// Return Values :
// --------------------------------------------------------------------------------
function privExtractFileAsString(&$p_entry, &$p_string)
{
//--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, 'PclZip::privExtractFileAsString', "p_entry['filename']='".$p_entry['filename']."'");
$v_result=1;
// ----- Read the file header
$v_header = array();
if (($v_result = $this->privReadFileHeader($v_header)) != 1)
{
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Found file '".$v_header['filename']."', size '".$v_header['size']."'");
// ----- Check that the file header is coherent with $p_entry info
if ($this->privCheckFileHeaders($v_header, $p_entry) != 1) {
// TBC
}
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extracting file in string (with path) '".$p_entry['filename']."', size '$v_header[size]'");
// ----- Do the extraction (if not a folder)
if (!(($p_entry['external']&0x00000010)==0x00000010))
{
// ----- Look for not compressed file
// if ($p_entry['compressed_size'] == $p_entry['size'])
if ($p_entry['compression'] == 0) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extracting an un-compressed file");
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Reading '".$p_entry['size']."' bytes");
// ----- Reading the file
$p_string = @fread($this->zip_fd, $p_entry['compressed_size']);
}
else {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extracting a compressed file (compression method '".$p_entry['compression']."')");
// ----- Reading the file
$v_data = @fread($this->zip_fd, $p_entry['compressed_size']);
// ----- Decompress the file
if (($p_string = @gzinflate($v_data)) === FALSE) {
// TBC
}
}
// ----- Trace
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extraction done");
}
else {
// TBC : error : can not extract a folder in a string
}
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
// Function : privReadFileHeader()
// Description :
// Parameters :
// Return Values :
// --------------------------------------------------------------------------------
function privReadFileHeader(&$p_header)
{
//--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privReadFileHeader", "");
$v_result=1;
// ----- Read the 4 bytes signature
$v_binary_data = @fread($this->zip_fd, 4);
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Binary data is : '".sprintf("%08x", $v_binary_data)."'");
$v_data = unpack('Vid', $v_binary_data);
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Binary signature is : '".sprintf("0x%08x", $v_data['id'])."'");
// ----- Check signature
if ($v_data['id'] != 0x04034b50)
{
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Invalid File header");
// ----- Error log
PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Invalid archive structure');
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
return PclZip::errorCode();
}
// ----- Read the first 42 bytes of the header
$v_binary_data = fread($this->zip_fd, 26);
// ----- Look for invalid block size
if (strlen($v_binary_data) != 26)
{
$p_header['filename'] = "";
$p_header['status'] = "invalid_header";
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Invalid block size : ".strlen($v_binary_data));
// ----- Error log
PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Invalid block size : ".strlen($v_binary_data));
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
return PclZip::errorCode();
}
// ----- Extract the values
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Header : '".$v_binary_data."'");
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Header (Hex) : '".bin2hex($v_binary_data)."'");
$v_data = unpack('vversion/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len', $v_binary_data);
// ----- Get filename
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "File name length : ".$v_data['filename_len']);
$p_header['filename'] = fread($this->zip_fd, $v_data['filename_len']);
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Filename : \''.$p_header['filename'].'\'');
// ----- Get extra_fields
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Extra field length : ".$v_data['extra_len']);
if ($v_data['extra_len'] != 0) {
$p_header['extra'] = fread($this->zip_fd, $v_data['extra_len']);
}
else {
$p_header['extra'] = '';
}
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Extra field : \''.bin2hex($p_header['extra']).'\'');
// ----- Extract properties
$p_header['version_extracted'] = $v_data['version'];
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Version need to extract : ('.$p_header['version_extracted'].') \''.($p_header['version_extracted']/10).'.'.($p_header['version_extracted']%10).'\'');
$p_header['compression'] = $v_data['compression'];
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Compression method : \''.$p_header['compression'].'\'');
$p_header['size'] = $v_data['size'];
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Size : \''.$p_header['size'].'\'');
$p_header['compressed_size'] = $v_data['compressed_size'];
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Compressed Size : \''.$p_header['compressed_size'].'\'');
$p_header['crc'] = $v_data['crc'];
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'CRC : \''.sprintf("0x%X", $p_header['crc']).'\'');
$p_header['flag'] = $v_data['flag'];
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Flag : \''.$p_header['flag'].'\'');
$p_header['filename_len'] = $v_data['filename_len'];
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Filename_len : \''.$p_header['filename_len'].'\'');
// ----- Recuperate date in UNIX format
$p_header['mdate'] = $v_data['mdate'];
$p_header['mtime'] = $v_data['mtime'];
if ($p_header['mdate'] && $p_header['mtime'])
{
// ----- Extract time
$v_hour = ($p_header['mtime'] & 0xF800) >> 11;
$v_minute = ($p_header['mtime'] & 0x07E0) >> 5;
$v_seconde = ($p_header['mtime'] & 0x001F)*2;
// ----- Extract date
$v_year = (($p_header['mdate'] & 0xFE00) >> 9) + 1980;
$v_month = ($p_header['mdate'] & 0x01E0) >> 5;
$v_day = $p_header['mdate'] & 0x001F;
// ----- Get UNIX date format
$p_header['mtime'] = mktime($v_hour, $v_minute, $v_seconde, $v_month, $v_day, $v_year);
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Date : \''.date("d/m/y H:i:s", $p_header['mtime']).'\'');
}
else
{
$p_header['mtime'] = time();
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Date is actual : \''.date("d/m/y H:i:s", $p_header['mtime']).'\'');
}
// TBC
//for(reset($v_data); $key = key($v_data); next($v_data)) {
// //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Attribut[$key] = ".$v_data[$key]);
//}
// ----- Set the stored filename
$p_header['stored_filename'] = $p_header['filename'];
// ----- Set the status field
$p_header['status'] = "ok";
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
// Function : privReadCentralFileHeader()
// Description :
// Parameters :
// Return Values :
// --------------------------------------------------------------------------------
function privReadCentralFileHeader(&$p_header)
{
//--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privReadCentralFileHeader", "");
$v_result=1;
// ----- Read the 4 bytes signature
$v_binary_data = @fread($this->zip_fd, 4);
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Binary data is : '".sprintf("%08x", $v_binary_data)."'");
$v_data = unpack('Vid', $v_binary_data);
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Binary signature is : '".sprintf("0x%08x", $v_data['id'])."'");
// ----- Check signature
if ($v_data['id'] != 0x02014b50)
{
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Invalid Central Dir File signature");
// ----- Error log
PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Invalid archive structure');
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
return PclZip::errorCode();
}
// ----- Read the first 42 bytes of the header
$v_binary_data = fread($this->zip_fd, 42);
// ----- Look for invalid block size
if (strlen($v_binary_data) != 42)
{
$p_header['filename'] = "";
$p_header['status'] = "invalid_header";
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Invalid block size : ".strlen($v_binary_data));
// ----- Error log
PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Invalid block size : ".strlen($v_binary_data));
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
return PclZip::errorCode();
}
// ----- Extract the values
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Header : '".$v_binary_data."'");
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Header (Hex) : '".bin2hex($v_binary_data)."'");
$p_header = unpack('vversion/vversion_extracted/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len/vcomment_len/vdisk/vinternal/Vexternal/Voffset', $v_binary_data);
// ----- Get filename
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "File name length : ".$p_header['filename_len']);
if ($p_header['filename_len'] != 0)
$p_header['filename'] = fread($this->zip_fd, $p_header['filename_len']);
else
$p_header['filename'] = '';
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Filename : \''.$p_header['filename'].'\'');
// ----- Get extra
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Extra length : ".$p_header['extra_len']);
if ($p_header['extra_len'] != 0)
$p_header['extra'] = fread($this->zip_fd, $p_header['extra_len']);
else
$p_header['extra'] = '';
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Extra : \''.$p_header['extra'].'\'');
// ----- Get comment
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Comment length : ".$p_header['comment_len']);
if ($p_header['comment_len'] != 0)
$p_header['comment'] = fread($this->zip_fd, $p_header['comment_len']);
else
$p_header['comment'] = '';
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Comment : \''.$p_header['comment'].'\'');
// ----- Extract properties
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Version : \''.($p_header['version']/10).'.'.($p_header['version']%10).'\'');
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Version need to extract : \''.($p_header['version_extracted']/10).'.'.($p_header['version_extracted']%10).'\'');
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Size : \''.$p_header['size'].'\'');
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Compressed Size : \''.$p_header['compressed_size'].'\'');
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'CRC : \''.sprintf("0x%X", $p_header['crc']).'\'');
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Flag : \''.$p_header['flag'].'\'');
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Offset : \''.$p_header['offset'].'\'');
// ----- Recuperate date in UNIX format
//if ($p_header['mdate'] && $p_header['mtime'])
// TBC : bug : this was ignoring time with 0/0/0
if (1)
{
// ----- Extract time
$v_hour = ($p_header['mtime'] & 0xF800) >> 11;
$v_minute = ($p_header['mtime'] & 0x07E0) >> 5;
$v_seconde = ($p_header['mtime'] & 0x001F)*2;
// ----- Extract date
$v_year = (($p_header['mdate'] & 0xFE00) >> 9) + 1980;
$v_month = ($p_header['mdate'] & 0x01E0) >> 5;
$v_day = $p_header['mdate'] & 0x001F;
// ----- Get UNIX date format
$p_header['mtime'] = @mktime($v_hour, $v_minute, $v_seconde, $v_month, $v_day, $v_year);
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Date : \''.date("d/m/y H:i:s", $p_header['mtime']).'\'');
}
else
{
$p_header['mtime'] = time();
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Date is actual : \''.date("d/m/y H:i:s", $p_header['mtime']).'\'');
}
// ----- Set the stored filename
$p_header['stored_filename'] = $p_header['filename'];
// ----- Set default status to ok
$p_header['status'] = 'ok';
// ----- Look if it is a directory
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Internal (Hex) : '".sprintf("Ox%04X", $p_header['internal'])."'");
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "External (Hex) : '".sprintf("Ox%04X", $p_header['external'])."' (".(($p_header['external']&0x00000010)==0x00000010?'is a folder':'is a file').')');
if (substr($p_header['filename'], -1) == '/') {
//$p_header['external'] = 0x41FF0010;
$p_header['external'] = 0x00000010;
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Force folder external : \''.sprintf("Ox%04X", $p_header['external']).'\'');
}
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Header of filename : \''.$p_header['filename'].'\'');
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
// Function : privCheckFileHeaders()
// Description :
// Parameters :
// Return Values :
// 1 on success,
// 0 on error;
// --------------------------------------------------------------------------------
function privCheckFileHeaders(&$p_local_header, &$p_central_header)
{
//--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privCheckFileHeaders", "");
$v_result=1;
// ----- Check the static values
// TBC
if ($p_local_header['filename'] != $p_central_header['filename']) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Bad check "filename" : TBC To Be Completed');
}
if ($p_local_header['version_extracted'] != $p_central_header['version_extracted']) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Bad check "version_extracted" : TBC To Be Completed');
}
if ($p_local_header['flag'] != $p_central_header['flag']) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Bad check "flag" : TBC To Be Completed');
}
if ($p_local_header['compression'] != $p_central_header['compression']) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Bad check "compression" : TBC To Be Completed');
}
if ($p_local_header['mtime'] != $p_central_header['mtime']) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Bad check "mtime" : TBC To Be Completed');
}
if ($p_local_header['filename_len'] != $p_central_header['filename_len']) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Bad check "filename_len" : TBC To Be Completed');
}
// ----- Look for flag bit 3
if (($p_local_header['flag'] & 8) == 8) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Purpose bit flag bit 3 set !');
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'File size, compression size and crc found in central header');
$p_local_header['size'] = $p_central_header['size'];
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Size : \''.$p_local_header['size'].'\'');
$p_local_header['compressed_size'] = $p_central_header['compressed_size'];
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Compressed Size : \''.$p_local_header['compressed_size'].'\'');
$p_local_header['crc'] = $p_central_header['crc'];
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'CRC : \''.sprintf("0x%X", $p_local_header['crc']).'\'');
}
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
// Function : privReadEndCentralDir()
// Description :
// Parameters :
// Return Values :
// --------------------------------------------------------------------------------
function privReadEndCentralDir(&$p_central_dir)
{
//--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privReadEndCentralDir", "");
$v_result=1;
// ----- Go to the end of the zip file
$v_size = filesize($this->zipname);
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Size of the file :$v_size");
@fseek($this->zip_fd, $v_size);
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Position at end of zip file : \''.ftell($this->zip_fd).'\'');
if (@ftell($this->zip_fd) != $v_size)
{
// ----- Error log
PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to go to the end of the archive \''.$this->zipname.'\'');
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
return PclZip::errorCode();
}
// ----- First try : look if this is an archive with no commentaries (most of the time)
// in this case the end of central dir is at 22 bytes of the file end
$v_found = 0;
if ($v_size > 26) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Look for central dir with no comment');
@fseek($this->zip_fd, $v_size-22);
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Position after min central position : \''.ftell($this->zip_fd).'\'');
if (($v_pos = @ftell($this->zip_fd)) != ($v_size-22))
{
// ----- Error log
PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to seek back to the middle of the archive \''.$this->zipname.'\'');
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
return PclZip::errorCode();
}
// ----- Read for bytes
$v_binary_data = @fread($this->zip_fd, 4);
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Binary data is : '".sprintf("%08x", $v_binary_data)."'");
$v_data = @unpack('Vid', $v_binary_data);
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Binary signature is : '".sprintf("0x%08x", $v_data['id'])."'");
// ----- Check signature
if ($v_data['id'] == 0x06054b50) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Found central dir at the default position.");
$v_found = 1;
}
$v_pos = ftell($this->zip_fd);
}
// ----- Go back to the maximum possible size of the Central Dir End Record
if (!$v_found) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Start extended search of end central dir');
$v_maximum_size = 65557; // 0xFFFF + 22;
if ($v_maximum_size > $v_size)
$v_maximum_size = $v_size;
@fseek($this->zip_fd, $v_size-$v_maximum_size);
if (@ftell($this->zip_fd) != ($v_size-$v_maximum_size))
{
// ----- Error log
PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to seek back to the middle of the archive \''.$this->zipname.'\'');
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
return PclZip::errorCode();
}
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Position after max central position : \''.ftell($this->zip_fd).'\'');
// ----- Read byte per byte in order to find the signature
$v_pos = ftell($this->zip_fd);
$v_bytes = 0x00000000;
while ($v_pos < $v_size)
{
// ----- Read a byte
$v_byte = @fread($this->zip_fd, 1);
// ----- Add the byte
$v_bytes = ($v_bytes << 8) | Ord($v_byte);
// ----- Compare the bytes
if ($v_bytes == 0x504b0506)
{
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Found End Central Dir signature at position : \''.ftell($this->zip_fd).'\'');
$v_pos++;
break;
}
$v_pos++;
}
// ----- Look if not found end of central dir
if ($v_pos == $v_size)
{
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Unable to find End of Central Dir Record signature");
// ----- Error log
PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Unable to find End of Central Dir Record signature");
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
return PclZip::errorCode();
}
}
// ----- Read the first 18 bytes of the header
$v_binary_data = fread($this->zip_fd, 18);
// ----- Look for invalid block size
if (strlen($v_binary_data) != 18)
{
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Invalid End of Central Dir Record size : ".strlen($v_binary_data));
// ----- Error log
PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Invalid End of Central Dir Record size : ".strlen($v_binary_data));
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
return PclZip::errorCode();
}
// ----- Extract the values
////--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Central Dir Record : '".$v_binary_data."'");
////--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Central Dir Record (Hex) : '".bin2hex($v_binary_data)."'");
$v_data = unpack('vdisk/vdisk_start/vdisk_entries/ventries/Vsize/Voffset/vcomment_size', $v_binary_data);
// ----- Check the global size
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Comment length : ".$v_data['comment_size']);
if (($v_pos + $v_data['comment_size'] + 18) != $v_size) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "The central dir is not at the end of the archive. Some trailing bytes exists after the archive.");
// ----- Removed in release 2.2 see readme file
// The check of the file size is a little too strict.
// Some bugs where found when a zip is encrypted/decrypted with 'crypt'.
// While decrypted, zip has training 0 bytes
if (0) {
// ----- Error log
PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT,
'The central dir is not at the end of the archive.'
.' Some trailing bytes exists after the archive.');
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
return PclZip::errorCode();
}
}
// ----- Get comment
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Comment size : \''.$v_data['comment_size'].'\'');
if ($v_data['comment_size'] != 0) {
$p_central_dir['comment'] = fread($this->zip_fd, $v_data['comment_size']);
}
else
$p_central_dir['comment'] = '';
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Comment : \''.$p_central_dir['comment'].'\'');
$p_central_dir['entries'] = $v_data['entries'];
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Nb of entries : \''.$p_central_dir['entries'].'\'');
$p_central_dir['disk_entries'] = $v_data['disk_entries'];
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Nb of entries for this disk : \''.$p_central_dir['disk_entries'].'\'');
$p_central_dir['offset'] = $v_data['offset'];
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Offset of Central Dir : \''.$p_central_dir['offset'].'\'');
$p_central_dir['size'] = $v_data['size'];
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Size of Central Dir : \''.$p_central_dir['size'].'\'');
$p_central_dir['disk'] = $v_data['disk'];
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Disk number : \''.$p_central_dir['disk'].'\'');
$p_central_dir['disk_start'] = $v_data['disk_start'];
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Start disk number : \''.$p_central_dir['disk_start'].'\'');
// TBC
//for(reset($p_central_dir); $key = key($p_central_dir); next($p_central_dir)) {
// //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "central_dir[$key] = ".$p_central_dir[$key]);
//}
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
// Function : privDeleteByRule()
// Description :
// Parameters :
// Return Values :
// --------------------------------------------------------------------------------
function privDeleteByRule(&$p_result_list, &$p_options)
{
//--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privDeleteByRule", "");
$v_result=1;
$v_list_detail = array();
// ----- Open the zip file
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
if (($v_result=$this->privOpenFd('rb')) != 1)
{
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// ----- Read the central directory informations
$v_central_dir = array();
if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
{
$this->privCloseFd();
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// ----- Go to beginning of File
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position in file : ".ftell($this->zip_fd)."'");
@rewind($this->zip_fd);
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position in file : ".ftell($this->zip_fd)."'");
// ----- Scan all the files
// ----- Start at beginning of Central Dir
$v_pos_entry = $v_central_dir['offset'];
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position before rewind : ".ftell($this->zip_fd)."'");
@rewind($this->zip_fd);
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position after rewind : ".ftell($this->zip_fd)."'");
if (@fseek($this->zip_fd, $v_pos_entry))
{
// ----- Close the zip file
$this->privCloseFd();
// ----- Error log
PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
return PclZip::errorCode();
}
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position after fseek : ".ftell($this->zip_fd)."'");
// ----- Read each entry
$v_header_list = array();
$j_start = 0;
for ($i=0, $v_nb_extracted=0; $i<$v_central_dir['entries']; $i++)
{
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Read next file header entry (index '$i')");
// ----- Read the file header
$v_header_list[$v_nb_extracted] = array();
if (($v_result = $this->privReadCentralFileHeader($v_header_list[$v_nb_extracted])) != 1)
{
// ----- Close the zip file
$this->privCloseFd();
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Filename (index '$i') : '".$v_header_list[$v_nb_extracted]['stored_filename']."'");
// ----- Store the index
$v_header_list[$v_nb_extracted]['index'] = $i;
// ----- Look for the specific extract rules
$v_found = false;
// ----- Look for extract by name rule
if ( (isset($p_options[PCLZIP_OPT_BY_NAME]))
&& ($p_options[PCLZIP_OPT_BY_NAME] != 0)) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Extract with rule 'ByName'");
// ----- Look if the filename is in the list
for ($j=0; ($j strlen($p_options[PCLZIP_OPT_BY_NAME][$j]))
&& (substr($v_header_list[$v_nb_extracted]['stored_filename'], 0, strlen($p_options[PCLZIP_OPT_BY_NAME][$j])) == $p_options[PCLZIP_OPT_BY_NAME][$j])) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "The directory is in the file path");
$v_found = true;
}
elseif ( (($v_header_list[$v_nb_extracted]['external']&0x00000010)==0x00000010) /* Indicates a folder */
&& ($v_header_list[$v_nb_extracted]['stored_filename'].'/' == $p_options[PCLZIP_OPT_BY_NAME][$j])) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "The entry is the searched directory");
$v_found = true;
}
}
// ----- Look for a filename
elseif ($v_header_list[$v_nb_extracted]['stored_filename'] == $p_options[PCLZIP_OPT_BY_NAME][$j]) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "The file is the right one.");
$v_found = true;
}
}
}
// ----- Look for extract by ereg rule
else if ( (isset($p_options[PCLZIP_OPT_BY_EREG]))
&& ($p_options[PCLZIP_OPT_BY_EREG] != "")) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Extract by ereg '".$p_options[PCLZIP_OPT_BY_EREG]."'");
if (ereg($p_options[PCLZIP_OPT_BY_EREG], $v_header_list[$v_nb_extracted]['stored_filename'])) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Filename match the regular expression");
$v_found = true;
}
}
// ----- Look for extract by preg rule
else if ( (isset($p_options[PCLZIP_OPT_BY_PREG]))
&& ($p_options[PCLZIP_OPT_BY_PREG] != "")) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Extract with rule 'ByEreg'");
if (preg_match($p_options[PCLZIP_OPT_BY_PREG], $v_header_list[$v_nb_extracted]['stored_filename'])) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Filename match the regular expression");
$v_found = true;
}
}
// ----- Look for extract by index rule
else if ( (isset($p_options[PCLZIP_OPT_BY_INDEX]))
&& ($p_options[PCLZIP_OPT_BY_INDEX] != 0)) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Extract with rule 'ByIndex'");
// ----- Look if the index is in the list
for ($j=$j_start; ($j=$p_options[PCLZIP_OPT_BY_INDEX][$j]['start']) && ($i<=$p_options[PCLZIP_OPT_BY_INDEX][$j]['end'])) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Found as part of an index range");
$v_found = true;
}
if ($i>=$p_options[PCLZIP_OPT_BY_INDEX][$j]['end']) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Do not look this index range for next loop");
$j_start = $j+1;
}
if ($p_options[PCLZIP_OPT_BY_INDEX][$j]['start']>$i) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Index range is greater than index, stop loop");
break;
}
}
}
else {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "No argument mean remove all file");
$v_found = true;
}
// ----- Look for deletion
if ($v_found)
{
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "File '".$v_header_list[$v_nb_extracted]['stored_filename']."', index '$i' need to be deleted");
unset($v_header_list[$v_nb_extracted]);
}
else
{
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "File '".$v_header_list[$v_nb_extracted]['stored_filename']."', index '$i' will not be deleted");
$v_nb_extracted++;
}
}
// ----- Look if something need to be deleted
if ($v_nb_extracted > 0) {
// ----- Creates a temporay file
$v_zip_temp_name = PCLZIP_TEMPORARY_DIR.uniqid('pclzip-').'.tmp';
// ----- Creates a temporary zip archive
$v_temp_zip = new PclZip($v_zip_temp_name);
// ----- Open the temporary zip file in write mode
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary write mode");
if (($v_result = $v_temp_zip->privOpenFd('wb')) != 1) {
$this->privCloseFd();
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// ----- Look which file need to be kept
for ($i=0; $izip_fd)."'");
@rewind($this->zip_fd);
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position after rewind : ".ftell($this->zip_fd)."'");
if (@fseek($this->zip_fd, $v_header_list[$i]['offset'])) {
// ----- Close the zip file
$this->privCloseFd();
$v_temp_zip->privCloseFd();
@unlink($v_zip_temp_name);
// ----- Error log
PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
return PclZip::errorCode();
}
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position after fseek : ".ftell($this->zip_fd)."'");
// ----- Read the file header
$v_local_header = array();
if (($v_result = $this->privReadFileHeader($v_local_header)) != 1) {
// ----- Close the zip file
$this->privCloseFd();
$v_temp_zip->privCloseFd();
@unlink($v_zip_temp_name);
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// ----- Check that local file header is same as central file header
if ($this->privCheckFileHeaders($v_local_header,
$v_header_list[$i]) != 1) {
// TBC
}
unset($v_local_header);
// ----- Write the file header
if (($v_result = $v_temp_zip->privWriteFileHeader($v_header_list[$i])) != 1) {
// ----- Close the zip file
$this->privCloseFd();
$v_temp_zip->privCloseFd();
@unlink($v_zip_temp_name);
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Offset for this file is '".$v_header_list[$i]['offset']."'");
// ----- Read/write the data block
if (($v_result = PclZipUtilCopyBlock($this->zip_fd, $v_temp_zip->zip_fd, $v_header_list[$i]['compressed_size'])) != 1) {
// ----- Close the zip file
$this->privCloseFd();
$v_temp_zip->privCloseFd();
@unlink($v_zip_temp_name);
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
}
// ----- Store the offset of the central dir
$v_offset = @ftell($v_temp_zip->zip_fd);
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "New offset of central dir : $v_offset");
// ----- Re-Create the Central Dir files header
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Creates the new central directory");
for ($i=0; $iprivWriteCentralFileHeader($v_header_list[$i])) != 1) {
$v_temp_zip->privCloseFd();
$this->privCloseFd();
@unlink($v_zip_temp_name);
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// ----- Transform the header to a 'usable' info
$v_temp_zip->privConvertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
}
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Creates the central directory footer");
// ----- Zip file comment
$v_comment = '';
if (isset($p_options[PCLZIP_OPT_COMMENT])) {
$v_comment = $p_options[PCLZIP_OPT_COMMENT];
}
// ----- Calculate the size of the central header
$v_size = @ftell($v_temp_zip->zip_fd)-$v_offset;
// ----- Create the central dir footer
if (($v_result = $v_temp_zip->privWriteCentralHeader(sizeof($v_header_list), $v_size, $v_offset, $v_comment)) != 1) {
// ----- Reset the file list
unset($v_header_list);
$v_temp_zip->privCloseFd();
$this->privCloseFd();
@unlink($v_zip_temp_name);
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// ----- Close
$v_temp_zip->privCloseFd();
$this->privCloseFd();
// ----- Delete the zip file
// TBC : I should test the result ...
@unlink($this->zipname);
// ----- Rename the temporary file
// TBC : I should test the result ...
//@rename($v_zip_temp_name, $this->zipname);
PclZipUtilRename($v_zip_temp_name, $this->zipname);
// ----- Destroy the temporary archive
unset($v_temp_zip);
}
// ----- Remove every files : reset the file
else if ($v_central_dir['entries'] != 0) {
$this->privCloseFd();
if (($v_result = $this->privOpenFd('wb')) != 1) {
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
if (($v_result = $this->privWriteCentralHeader(0, 0, 0, '')) != 1) {
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
$this->privCloseFd();
}
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
// Function : privDirCheck()
// Description :
// Check if a directory exists, if not it creates it and all the parents directory
// which may be useful.
// Parameters :
// $p_dir : Directory path to check.
// Return Values :
// 1 : OK
// -1 : Unable to create directory
// --------------------------------------------------------------------------------
function privDirCheck($p_dir, $p_is_dir=false)
{
$v_result = 1;
//--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privDirCheck", "entry='$p_dir', is_dir='".($p_is_dir?"true":"false")."'");
// ----- Remove the final '/'
if (($p_is_dir) && (substr($p_dir, -1)=='/'))
{
$p_dir = substr($p_dir, 0, strlen($p_dir)-1);
}
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Looking for entry '$p_dir'");
// ----- Check the directory availability
if ((is_dir($p_dir)) || ($p_dir == ""))
{
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, "'$p_dir' is a directory");
return 1;
}
// ----- Extract parent directory
$p_parent_dir = dirname($p_dir);
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Parent directory is '$p_parent_dir'");
// ----- Just a check
if ($p_parent_dir != $p_dir)
{
// ----- Look for parent directory
if ($p_parent_dir != "")
{
if (($v_result = $this->privDirCheck($p_parent_dir)) != 1)
{
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
}
}
// ----- Create the directory
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Create directory '$p_dir'");
if (!@mkdir($p_dir, 0777))
{
// ----- Error log
PclZip::privErrorLog(PCLZIP_ERR_DIR_CREATE_FAIL, "Unable to create directory '$p_dir'");
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
return PclZip::errorCode();
}
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result, "Directory '$p_dir' created");
return $v_result;
}
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
// Function : privMerge()
// Description :
// If $p_archive_to_add does not exist, the function exit with a success result.
// Parameters :
// Return Values :
// --------------------------------------------------------------------------------
function privMerge(&$p_archive_to_add)
{
//--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privMerge", "archive='".$p_archive_to_add->zipname."'");
$v_result=1;
// ----- Look if the archive_to_add exists
if (!is_file($p_archive_to_add->zipname))
{
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Archive to add does not exist. End of merge.");
// ----- Nothing to merge, so merge is a success
$v_result = 1;
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// ----- Look if the archive exists
if (!is_file($this->zipname))
{
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Archive does not exist, duplicate the archive_to_add.");
// ----- Do a duplicate
$v_result = $this->privDuplicate($p_archive_to_add->zipname);
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// ----- Open the zip file
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
if (($v_result=$this->privOpenFd('rb')) != 1)
{
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// ----- Read the central directory informations
$v_central_dir = array();
if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
{
$this->privCloseFd();
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// ----- Go to beginning of File
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position in zip : ".ftell($this->zip_fd)."'");
@rewind($this->zip_fd);
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position in zip : ".ftell($this->zip_fd)."'");
// ----- Open the archive_to_add file
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open archive_to_add in binary read mode");
if (($v_result=$p_archive_to_add->privOpenFd('rb')) != 1)
{
$this->privCloseFd();
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// ----- Read the central directory informations
$v_central_dir_to_add = array();
if (($v_result = $p_archive_to_add->privReadEndCentralDir($v_central_dir_to_add)) != 1)
{
$this->privCloseFd();
$p_archive_to_add->privCloseFd();
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// ----- Go to beginning of File
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position in archive_to_add : ".ftell($p_archive_to_add->zip_fd)."'");
@rewind($p_archive_to_add->zip_fd);
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position in archive_to_add : ".ftell($p_archive_to_add->zip_fd)."'");
// ----- Creates a temporay file
$v_zip_temp_name = PCLZIP_TEMPORARY_DIR.uniqid('pclzip-').'.tmp';
// ----- Open the temporary file in write mode
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
if (($v_zip_temp_fd = @fopen($v_zip_temp_name, 'wb')) == 0)
{
$this->privCloseFd();
$p_archive_to_add->privCloseFd();
PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \''.$v_zip_temp_name.'\' in binary write mode');
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
return PclZip::errorCode();
}
// ----- Copy the files from the archive to the temporary file
// TBC : Here I should better append the file and go back to erase the central dir
$v_size = $v_central_dir['offset'];
while ($v_size != 0)
{
$v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Read $v_read_size bytes");
$v_buffer = fread($this->zip_fd, $v_read_size);
@fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
$v_size -= $v_read_size;
}
// ----- Copy the files from the archive_to_add into the temporary file
$v_size = $v_central_dir_to_add['offset'];
while ($v_size != 0)
{
$v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Read $v_read_size bytes");
$v_buffer = fread($p_archive_to_add->zip_fd, $v_read_size);
@fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
$v_size -= $v_read_size;
}
// ----- Store the offset of the central dir
$v_offset = @ftell($v_zip_temp_fd);
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "New offset of central dir : $v_offset");
// ----- Copy the block of file headers from the old archive
$v_size = $v_central_dir['size'];
while ($v_size != 0)
{
$v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Read $v_read_size bytes");
$v_buffer = @fread($this->zip_fd, $v_read_size);
@fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
$v_size -= $v_read_size;
}
// ----- Copy the block of file headers from the archive_to_add
$v_size = $v_central_dir_to_add['size'];
while ($v_size != 0)
{
$v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Read $v_read_size bytes");
$v_buffer = @fread($p_archive_to_add->zip_fd, $v_read_size);
@fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
$v_size -= $v_read_size;
}
// ----- Merge the file comments
$v_comment = $v_central_dir['comment'].' '.$v_central_dir_to_add['comment'];
// ----- Calculate the size of the (new) central header
$v_size = @ftell($v_zip_temp_fd)-$v_offset;
// ----- Swap the file descriptor
// Here is a trick : I swap the temporary fd with the zip fd, in order to use
// the following methods on the temporary fil and not the real archive fd
$v_swap = $this->zip_fd;
$this->zip_fd = $v_zip_temp_fd;
$v_zip_temp_fd = $v_swap;
// ----- Create the central dir footer
if (($v_result = $this->privWriteCentralHeader($v_central_dir['entries']+$v_central_dir_to_add['entries'], $v_size, $v_offset, $v_comment)) != 1)
{
$this->privCloseFd();
$p_archive_to_add->privCloseFd();
@fclose($v_zip_temp_fd);
$this->zip_fd = null;
// ----- Reset the file list
unset($v_header_list);
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// ----- Swap back the file descriptor
$v_swap = $this->zip_fd;
$this->zip_fd = $v_zip_temp_fd;
$v_zip_temp_fd = $v_swap;
// ----- Close
$this->privCloseFd();
$p_archive_to_add->privCloseFd();
// ----- Close the temporary file
@fclose($v_zip_temp_fd);
// ----- Delete the zip file
// TBC : I should test the result ...
@unlink($this->zipname);
// ----- Rename the temporary file
// TBC : I should test the result ...
//@rename($v_zip_temp_name, $this->zipname);
PclZipUtilRename($v_zip_temp_name, $this->zipname);
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
// Function : privDuplicate()
// Description :
// Parameters :
// Return Values :
// --------------------------------------------------------------------------------
function privDuplicate($p_archive_filename)
{
//--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privDuplicate", "archive_filename='$p_archive_filename'");
$v_result=1;
// ----- Look if the $p_archive_filename exists
if (!is_file($p_archive_filename))
{
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Archive to duplicate does not exist. End of duplicate.");
// ----- Nothing to duplicate, so duplicate is a success.
$v_result = 1;
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// ----- Open the zip file
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
if (($v_result=$this->privOpenFd('wb')) != 1)
{
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// ----- Open the temporary file in write mode
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
if (($v_zip_temp_fd = @fopen($p_archive_filename, 'rb')) == 0)
{
$this->privCloseFd();
PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive file \''.$p_archive_filename.'\' in binary write mode');
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
return PclZip::errorCode();
}
// ----- Copy the files from the archive to the temporary file
// TBC : Here I should better append the file and go back to erase the central dir
$v_size = filesize($p_archive_filename);
while ($v_size != 0)
{
$v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Read $v_read_size bytes");
$v_buffer = fread($v_zip_temp_fd, $v_read_size);
@fwrite($this->zip_fd, $v_buffer, $v_read_size);
$v_size -= $v_read_size;
}
// ----- Close
$this->privCloseFd();
// ----- Close the temporary file
@fclose($v_zip_temp_fd);
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
// Function : privErrorLog()
// Description :
// Parameters :
// --------------------------------------------------------------------------------
function privErrorLog($p_error_code=0, $p_error_string='')
{
if (PCLZIP_ERROR_EXTERNAL == 1) {
PclError($p_error_code, $p_error_string);
}
else {
$this->error_code = $p_error_code;
$this->error_string = $p_error_string;
}
}
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
// Function : privErrorReset()
// Description :
// Parameters :
// --------------------------------------------------------------------------------
function privErrorReset()
{
if (PCLZIP_ERROR_EXTERNAL == 1) {
PclErrorReset();
}
else {
$this->error_code = 0;
$this->error_string = '';
}
}
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
// Function : privDecrypt()
// Description :
// Parameters :
// Return Values :
// --------------------------------------------------------------------------------
function privDecrypt($p_encryption_header, &$p_buffer, $p_size, $p_crc)
{
//--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, 'PclZip::privDecrypt', "size=".$p_size."");
$v_result=1;
// ----- To Be Modified ;-)
$v_pwd = "test";
$p_buffer = PclZipUtilZipDecrypt($p_buffer, $p_size, $p_encryption_header,
$p_crc, $v_pwd);
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
// Function : privDisableMagicQuotes()
// Description :
// Parameters :
// Return Values :
// --------------------------------------------------------------------------------
function privDisableMagicQuotes()
{
//--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, 'PclZip::privDisableMagicQuotes', "");
$v_result=1;
// ----- Look if function exists
if ( (!function_exists("get_magic_quotes_runtime"))
|| (!function_exists("set_magic_quotes_runtime"))) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Functions *et_magic_quotes_runtime are not supported");
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// ----- Look if already done
if ($this->magic_quotes_status != -1) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "magic_quote already disabled");
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// ----- Get and memorize the magic_quote value
$this->magic_quotes_status = @get_magic_quotes_runtime();
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Current magic_quotes_runtime status is '".($this->magic_quotes_status==0?'disable':'enable')."'");
// ----- Disable magic_quotes
if ($this->magic_quotes_status == 1) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Disable magic_quotes");
@set_magic_quotes_runtime(0);
}
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
// Function : privSwapBackMagicQuotes()
// Description :
// Parameters :
// Return Values :
// --------------------------------------------------------------------------------
function privSwapBackMagicQuotes()
{
//--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, 'PclZip::privSwapBackMagicQuotes', "");
$v_result=1;
// ----- Look if function exists
if ( (!function_exists("get_magic_quotes_runtime"))
|| (!function_exists("set_magic_quotes_runtime"))) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Functions *et_magic_quotes_runtime are not supported");
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// ----- Look if something to do
if ($this->magic_quotes_status != -1) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "magic_quote not modified");
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// ----- Swap back magic_quotes
if ($this->magic_quotes_status == 1) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Enable back magic_quotes");
@set_magic_quotes_runtime($this->magic_quotes_status);
}
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// --------------------------------------------------------------------------------
}
// End of class
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
// Function : PclZipUtilPathReduction()
// Description :
// Parameters :
// Return Values :
// --------------------------------------------------------------------------------
function PclZipUtilPathReduction($p_dir)
{
//--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZipUtilPathReduction", "dir='$p_dir'");
$v_result = "";
// ----- Look for not empty path
if ($p_dir != "") {
// ----- Explode path by directory names
$v_list = explode("/", $p_dir);
// ----- Study directories from last to first
$v_skip = 0;
for ($i=sizeof($v_list)-1; $i>=0; $i--) {
// ----- Look for current path
if ($v_list[$i] == ".") {
// ----- Ignore this directory
// Should be the first $i=0, but no check is done
}
else if ($v_list[$i] == "..") {
$v_skip++;
}
else if ($v_list[$i] == "") {
// ----- First '/' i.e. root slash
if ($i == 0) {
$v_result = "/".$v_result;
if ($v_skip > 0) {
// ----- It is an invalid path, so the path is not modified
// TBC
$v_result = $p_dir;
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Invalid path is unchanged");
$v_skip = 0;
}
}
// ----- Last '/' i.e. indicates a directory
else if ($i == (sizeof($v_list)-1)) {
$v_result = $v_list[$i];
}
// ----- Double '/' inside the path
else {
// ----- Ignore only the double '//' in path,
// but not the first and last '/'
}
}
else {
// ----- Look for item to skip
if ($v_skip > 0) {
$v_skip--;
}
else {
$v_result = $v_list[$i].($i!=(sizeof($v_list)-1)?"/".$v_result:"");
}
}
}
// ----- Look for skip
if ($v_skip > 0) {
while ($v_skip > 0) {
$v_result = '../'.$v_result;
$v_skip--;
}
}
}
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
// Function : PclZipUtilPathInclusion()
// Description :
// This function indicates if the path $p_path is under the $p_dir tree. Or,
// said in an other way, if the file or sub-dir $p_path is inside the dir
// $p_dir.
// The function indicates also if the path is exactly the same as the dir.
// This function supports path with duplicated '/' like '//', but does not
// support '.' or '..' statements.
// Parameters :
// Return Values :
// 0 if $p_path is not inside directory $p_dir
// 1 if $p_path is inside directory $p_dir
// 2 if $p_path is exactly the same as $p_dir
// --------------------------------------------------------------------------------
function PclZipUtilPathInclusion($p_dir, $p_path)
{
//--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZipUtilPathInclusion", "dir='$p_dir', path='$p_path'");
$v_result = 1;
// ----- Look for path beginning by ./
if ( ($p_dir == '.')
|| ((strlen($p_dir) >=2) && (substr($p_dir, 0, 2) == './'))) {
$p_dir = PclZipUtilTranslateWinPath(getcwd(), FALSE).'/'.substr($p_dir, 1);
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Replacing ./ by full path in p_dir '".$p_dir."'");
}
if ( ($p_path == '.')
|| ((strlen($p_path) >=2) && (substr($p_path, 0, 2) == './'))) {
$p_path = PclZipUtilTranslateWinPath(getcwd(), FALSE).'/'.substr($p_path, 1);
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Replacing ./ by full path in p_path '".$p_path."'");
}
// ----- Explode dir and path by directory separator
$v_list_dir = explode("/", $p_dir);
$v_list_dir_size = sizeof($v_list_dir);
$v_list_path = explode("/", $p_path);
$v_list_path_size = sizeof($v_list_path);
// ----- Study directories paths
$i = 0;
$j = 0;
while (($i < $v_list_dir_size) && ($j < $v_list_path_size) && ($v_result)) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Working on dir($i)='".$v_list_dir[$i]."' and path($j)='".$v_list_path[$j]."'");
// ----- Look for empty dir (path reduction)
if ($v_list_dir[$i] == '') {
$i++;
continue;
}
if ($v_list_path[$j] == '') {
$j++;
continue;
}
// ----- Compare the items
if (($v_list_dir[$i] != $v_list_path[$j]) && ($v_list_dir[$i] != '') && ( $v_list_path[$j] != '')) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Items ($i,$j) are different");
$v_result = 0;
}
// ----- Next items
$i++;
$j++;
}
// ----- Look if everything seems to be the same
if ($v_result) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Look for tie break");
// ----- Skip all the empty items
while (($j < $v_list_path_size) && ($v_list_path[$j] == '')) $j++;
while (($i < $v_list_dir_size) && ($v_list_dir[$i] == '')) $i++;
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Looking on dir($i)='".($i < $v_list_dir_size?$v_list_dir[$i]:'')."' and path($j)='".($j < $v_list_path_size?$v_list_path[$j]:'')."'");
if (($i >= $v_list_dir_size) && ($j >= $v_list_path_size)) {
// ----- There are exactly the same
$v_result = 2;
}
else if ($i < $v_list_dir_size) {
// ----- The path is shorter than the dir
$v_result = 0;
}
}
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
// Function : PclZipUtilCopyBlock()
// Description :
// Parameters :
// $p_mode : read/write compression mode
// 0 : src & dest normal
// 1 : src gzip, dest normal
// 2 : src normal, dest gzip
// 3 : src & dest gzip
// Return Values :
// --------------------------------------------------------------------------------
function PclZipUtilCopyBlock($p_src, $p_dest, $p_size, $p_mode=0)
{
//--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZipUtilCopyBlock", "size=$p_size, mode=$p_mode");
$v_result = 1;
if ($p_mode==0)
{
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Src offset before read :".(@ftell($p_src)));
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Dest offset before write :".(@ftell($p_dest)));
while ($p_size != 0)
{
$v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Read $v_read_size bytes");
$v_buffer = @fread($p_src, $v_read_size);
@fwrite($p_dest, $v_buffer, $v_read_size);
$p_size -= $v_read_size;
}
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Src offset after read :".(@ftell($p_src)));
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Dest offset after write :".(@ftell($p_dest)));
}
else if ($p_mode==1)
{
while ($p_size != 0)
{
$v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Read $v_read_size bytes");
$v_buffer = @gzread($p_src, $v_read_size);
@fwrite($p_dest, $v_buffer, $v_read_size);
$p_size -= $v_read_size;
}
}
else if ($p_mode==2)
{
while ($p_size != 0)
{
$v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Read $v_read_size bytes");
$v_buffer = @fread($p_src, $v_read_size);
@gzwrite($p_dest, $v_buffer, $v_read_size);
$p_size -= $v_read_size;
}
}
else if ($p_mode==3)
{
while ($p_size != 0)
{
$v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Read $v_read_size bytes");
$v_buffer = @gzread($p_src, $v_read_size);
@gzwrite($p_dest, $v_buffer, $v_read_size);
$p_size -= $v_read_size;
}
}
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
// Function : PclZipUtilRename()
// Description :
// This function tries to do a simple rename() function. If it fails, it
// tries to copy the $p_src file in a new $p_dest file and then unlink the
// first one.
// Parameters :
// $p_src : Old filename
// $p_dest : New filename
// Return Values :
// 1 on success, 0 on failure.
// --------------------------------------------------------------------------------
function PclZipUtilRename($p_src, $p_dest)
{
//--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZipUtilRename", "source=$p_src, destination=$p_dest");
$v_result = 1;
// ----- Try to rename the files
if (!@rename($p_src, $p_dest)) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Fail to rename file, try copy+unlink");
// ----- Try to copy & unlink the src
if (!@copy($p_src, $p_dest)) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Fail to copy file");
$v_result = 0;
}
else if (!@unlink($p_src)) {
//--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Fail to unlink old filename");
$v_result = 0;
}
}
// ----- Return
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
// Function : PclZipUtilOptionText()
// Description :
// Translate option value in text. Mainly for debug purpose.
// Parameters :
// $p_option : the option value.
// Return Values :
// The option text value.
// --------------------------------------------------------------------------------
function PclZipUtilOptionText($p_option)
{
//--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZipUtilOptionText", "option='".$p_option."'");
$v_list = get_defined_constants();
for (reset($v_list); $v_key = key($v_list); next($v_list)) {
$v_prefix = substr($v_key, 0, 10);
if (( ($v_prefix == 'PCLZIP_OPT')
|| ($v_prefix == 'PCLZIP_CB_')
|| ($v_prefix == 'PCLZIP_ATT'))
&& ($v_list[$v_key] == $p_option)) {
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_key);
return $v_key;
}
}
$v_result = 'Unknown';
//--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
return $v_result;
}
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
// Function : PclZipUtilTranslateWinPath()
// Description :
// Translate windows path by replacing '\' by '/' and optionally removing
// drive letter.
// Parameters :
// $p_path : path to translate.
// $p_remove_disk_letter : true | false
// Return Values :
// The path translated.
// --------------------------------------------------------------------------------
function PclZipUtilTranslateWinPath($p_path, $p_remove_disk_letter=true)
{
if (stristr(php_uname(), 'windows')) {
// ----- Look for potential disk letter
if (($p_remove_disk_letter) && (($v_position = strpos($p_path, ':')) != false)) {
$p_path = substr($p_path, $v_position+1);
}
// ----- Change potential windows directory separator
if ((strpos($p_path, '\\') > 0) || (substr($p_path, 0,1) == '\\')) {
$p_path = strtr($p_path, '\\', '/');
}
}
return $p_path;
}
// --------------------------------------------------------------------------------
?>
Collabtive-2.0/include/class.Smarty.php 0000664 0000000 0000000 00000131277 12372520637 0020200 0 ustar 00root root 0000000 0000000
* @author Uwe Tews
* @author Rodney Rehm
* @package Smarty
* @version 3.1.13
*/
/**
* define shorthand directory separator constant
*/
if (!defined('DS')) {
define('DS', DIRECTORY_SEPARATOR);
}
/**
* set SMARTY_DIR to absolute path to Smarty library files.
* Sets SMARTY_DIR only if user application has not already defined it.
*/
if (!defined('SMARTY_DIR')) {
define('SMARTY_DIR', dirname(__FILE__) . DS);
}
/**
* set SMARTY_SYSPLUGINS_DIR to absolute path to Smarty internal plugins.
* Sets SMARTY_SYSPLUGINS_DIR only if user application has not already defined it.
*/
if (!defined('SMARTY_SYSPLUGINS_DIR')) {
define('SMARTY_SYSPLUGINS_DIR', SMARTY_DIR . 'sysplugins' . DS);
}
if (!defined('SMARTY_PLUGINS_DIR')) {
define('SMARTY_PLUGINS_DIR', SMARTY_DIR . 'plugins' . DS);
}
if (!defined('SMARTY_MBSTRING')) {
define('SMARTY_MBSTRING', function_exists('mb_split'));
}
if (!defined('SMARTY_RESOURCE_CHAR_SET')) {
// UTF-8 can only be done properly when mbstring is available!
/**
* @deprecated in favor of Smarty::$_CHARSET
*/
define('SMARTY_RESOURCE_CHAR_SET', SMARTY_MBSTRING ? 'UTF-8' : 'ISO-8859-1');
}
if (!defined('SMARTY_RESOURCE_DATE_FORMAT')) {
/**
* @deprecated in favor of Smarty::$_DATE_FORMAT
*/
define('SMARTY_RESOURCE_DATE_FORMAT', '%b %e, %Y');
}
/**
* register the class autoloader
*/
if (!defined('SMARTY_SPL_AUTOLOAD')) {
define('SMARTY_SPL_AUTOLOAD', 0);
}
if (SMARTY_SPL_AUTOLOAD && set_include_path(get_include_path() . PATH_SEPARATOR . SMARTY_SYSPLUGINS_DIR) !== false) {
$registeredAutoLoadFunctions = spl_autoload_functions();
if (!isset($registeredAutoLoadFunctions['spl_autoload'])) {
spl_autoload_register();
}
} else {
spl_autoload_register('smartyAutoload');
}
/**
* Load always needed external class files
*/
include_once SMARTY_SYSPLUGINS_DIR.'smarty_internal_data.php';
include_once SMARTY_SYSPLUGINS_DIR.'smarty_internal_templatebase.php';
include_once SMARTY_SYSPLUGINS_DIR.'smarty_internal_template.php';
include_once SMARTY_SYSPLUGINS_DIR.'smarty_resource.php';
include_once SMARTY_SYSPLUGINS_DIR.'smarty_internal_resource_file.php';
include_once SMARTY_SYSPLUGINS_DIR.'smarty_cacheresource.php';
include_once SMARTY_SYSPLUGINS_DIR.'smarty_internal_cacheresource_file.php';
/**
* This is the main Smarty class
* @package Smarty
*/
class Smarty extends Smarty_Internal_TemplateBase {
/**#@+
* constant definitions
*/
/**
* smarty version
*/
const SMARTY_VERSION = 'Smarty-3.1.13';
/**
* define variable scopes
*/
const SCOPE_LOCAL = 0;
const SCOPE_PARENT = 1;
const SCOPE_ROOT = 2;
const SCOPE_GLOBAL = 3;
/**
* define caching modes
*/
const CACHING_OFF = 0;
const CACHING_LIFETIME_CURRENT = 1;
const CACHING_LIFETIME_SAVED = 2;
/**
* define compile check modes
*/
const COMPILECHECK_OFF = 0;
const COMPILECHECK_ON = 1;
const COMPILECHECK_CACHEMISS = 2;
/**
* modes for handling of "" tags in templates.
*/
const PHP_PASSTHRU = 0; //-> print tags as plain text
const PHP_QUOTE = 1; //-> escape tags as entities
const PHP_REMOVE = 2; //-> escape tags as entities
const PHP_ALLOW = 3; //-> escape tags as entities
/**
* filter types
*/
const FILTER_POST = 'post';
const FILTER_PRE = 'pre';
const FILTER_OUTPUT = 'output';
const FILTER_VARIABLE = 'variable';
/**
* plugin types
*/
const PLUGIN_FUNCTION = 'function';
const PLUGIN_BLOCK = 'block';
const PLUGIN_COMPILER = 'compiler';
const PLUGIN_MODIFIER = 'modifier';
const PLUGIN_MODIFIERCOMPILER = 'modifiercompiler';
/**#@-*/
/**
* assigned global tpl vars
*/
public static $global_tpl_vars = array();
/**
* error handler returned by set_error_hanlder() in Smarty::muteExpectedErrors()
*/
public static $_previous_error_handler = null;
/**
* contains directories outside of SMARTY_DIR that are to be muted by muteExpectedErrors()
*/
public static $_muted_directories = array();
/**
* Flag denoting if Multibyte String functions are available
*/
public static $_MBSTRING = SMARTY_MBSTRING;
/**
* The character set to adhere to (e.g. "UTF-8")
*/
public static $_CHARSET = SMARTY_RESOURCE_CHAR_SET;
/**
* The date format to be used internally
* (accepts date() and strftime())
*/
public static $_DATE_FORMAT = SMARTY_RESOURCE_DATE_FORMAT;
/**
* Flag denoting if PCRE should run in UTF-8 mode
*/
public static $_UTF8_MODIFIER = 'u';
/**
* Flag denoting if operating system is windows
*/
public static $_IS_WINDOWS = false;
/**#@+
* variables
*/
/**
* auto literal on delimiters with whitspace
* @var boolean
*/
public $auto_literal = true;
/**
* display error on not assigned variables
* @var boolean
*/
public $error_unassigned = false;
/**
* look up relative filepaths in include_path
* @var boolean
*/
public $use_include_path = false;
/**
* template directory
* @var array
*/
private $template_dir = array();
/**
* joined template directory string used in cache keys
* @var string
*/
public $joined_template_dir = null;
/**
* joined config directory string used in cache keys
* @var string
*/
public $joined_config_dir = null;
/**
* default template handler
* @var callable
*/
public $default_template_handler_func = null;
/**
* default config handler
* @var callable
*/
public $default_config_handler_func = null;
/**
* default plugin handler
* @var callable
*/
public $default_plugin_handler_func = null;
/**
* compile directory
* @var string
*/
private $compile_dir = null;
/**
* plugins directory
* @var array
*/
private $plugins_dir = array();
/**
* cache directory
* @var string
*/
private $cache_dir = null;
/**
* config directory
* @var array
*/
private $config_dir = array();
/**
* force template compiling?
* @var boolean
*/
public $force_compile = false;
/**
* check template for modifications?
* @var boolean
*/
public $compile_check = true;
/**
* use sub dirs for compiled/cached files?
* @var boolean
*/
public $use_sub_dirs = false;
/**
* allow ambiguous resources (that are made unique by the resource handler)
* @var boolean
*/
public $allow_ambiguous_resources = false;
/**
* caching enabled
* @var boolean
*/
public $caching = false;
/**
* merge compiled includes
* @var boolean
*/
public $merge_compiled_includes = false;
/**
* cache lifetime in seconds
* @var integer
*/
public $cache_lifetime = 3600;
/**
* force cache file creation
* @var boolean
*/
public $force_cache = false;
/**
* Set this if you want different sets of cache files for the same
* templates.
*
* @var string
*/
public $cache_id = null;
/**
* Set this if you want different sets of compiled files for the same
* templates.
*
* @var string
*/
public $compile_id = null;
/**
* template left-delimiter
* @var string
*/
public $left_delimiter = "{";
/**
* template right-delimiter
* @var string
*/
public $right_delimiter = "}";
/**#@+
* security
*/
/**
* class name
*
* This should be instance of Smarty_Security.
*
* @var string
* @see Smarty_Security
*/
public $security_class = 'Smarty_Security';
/**
* implementation of security class
*
* @var Smarty_Security
*/
public $security_policy = null;
/**
* controls handling of PHP-blocks
*
* @var integer
*/
public $php_handling = self::PHP_PASSTHRU;
/**
* controls if the php template file resource is allowed
*
* @var bool
*/
public $allow_php_templates = false;
/**
* Should compiled-templates be prevented from being called directly?
*
* {@internal
* Currently used by Smarty_Internal_Template only.
* }}
*
* @var boolean
*/
public $direct_access_security = true;
/**#@-*/
/**
* debug mode
*
* Setting this to true enables the debug-console.
*
* @var boolean
*/
public $debugging = false;
/**
* This determines if debugging is enable-able from the browser.
*
*
NONE => no debugging control allowed
*
URL => enable debugging when SMARTY_DEBUG is found in the URL.
*
* @var string
*/
public $debugging_ctrl = 'NONE';
/**
* Name of debugging URL-param.
*
* Only used when $debugging_ctrl is set to 'URL'.
* The name of the URL-parameter that activates debugging.
*
* @var type
*/
public $smarty_debug_id = 'SMARTY_DEBUG';
/**
* Path of debug template.
* @var string
*/
public $debug_tpl = null;
/**
* When set, smarty uses this value as error_reporting-level.
* @var int
*/
public $error_reporting = null;
/**
* Internal flag for getTags()
* @var boolean
*/
public $get_used_tags = false;
/**#@+
* config var settings
*/
/**
* Controls whether variables with the same name overwrite each other.
* @var boolean
*/
public $config_overwrite = true;
/**
* Controls whether config values of on/true/yes and off/false/no get converted to boolean.
* @var boolean
*/
public $config_booleanize = true;
/**
* Controls whether hidden config sections/vars are read from the file.
* @var boolean
*/
public $config_read_hidden = false;
/**#@-*/
/**#@+
* resource locking
*/
/**
* locking concurrent compiles
* @var boolean
*/
public $compile_locking = true;
/**
* Controls whether cache resources should emply locking mechanism
* @var boolean
*/
public $cache_locking = false;
/**
* seconds to wait for acquiring a lock before ignoring the write lock
* @var float
*/
public $locking_timeout = 10;
/**#@-*/
/**
* global template functions
* @var array
*/
public $template_functions = array();
/**
* resource type used if none given
*
* Must be an valid key of $registered_resources.
* @var string
*/
public $default_resource_type = 'file';
/**
* caching type
*
* Must be an element of $cache_resource_types.
*
* @var string
*/
public $caching_type = 'file';
/**
* internal config properties
* @var array
*/
public $properties = array();
/**
* config type
* @var string
*/
public $default_config_type = 'file';
/**
* cached template objects
* @var array
*/
public $template_objects = array();
/**
* check If-Modified-Since headers
* @var boolean
*/
public $cache_modified_check = false;
/**
* registered plugins
* @var array
*/
public $registered_plugins = array();
/**
* plugin search order
* @var array
*/
public $plugin_search_order = array('function', 'block', 'compiler', 'class');
/**
* registered objects
* @var array
*/
public $registered_objects = array();
/**
* registered classes
* @var array
*/
public $registered_classes = array();
/**
* registered filters
* @var array
*/
public $registered_filters = array();
/**
* registered resources
* @var array
*/
public $registered_resources = array();
/**
* resource handler cache
* @var array
*/
public $_resource_handlers = array();
/**
* registered cache resources
* @var array
*/
public $registered_cache_resources = array();
/**
* cache resource handler cache
* @var array
*/
public $_cacheresource_handlers = array();
/**
* autoload filter
* @var array
*/
public $autoload_filters = array();
/**
* default modifier
* @var array
*/
public $default_modifiers = array();
/**
* autoescape variable output
* @var boolean
*/
public $escape_html = false;
/**
* global internal smarty vars
* @var array
*/
public static $_smarty_vars = array();
/**
* start time for execution time calculation
* @var int
*/
public $start_time = 0;
/**
* default file permissions
* @var int
*/
public $_file_perms = 0644;
/**
* default dir permissions
* @var int
*/
public $_dir_perms = 0771;
/**
* block tag hierarchy
* @var array
*/
public $_tag_stack = array();
/**
* self pointer to Smarty object
* @var Smarty
*/
public $smarty;
/**
* required by the compiler for BC
* @var string
*/
public $_current_file = null;
/**
* internal flag to enable parser debugging
* @var bool
*/
public $_parserdebug = false;
/**
* Saved parameter of merged templates during compilation
*
* @var array
*/
public $merged_templates_func = array();
/**#@-*/
/**
* Initialize new Smarty object
*
*/
public function __construct()
{
// selfpointer needed by some other class methods
$this->smarty = $this;
if (is_callable('mb_internal_encoding')) {
mb_internal_encoding(Smarty::$_CHARSET);
}
$this->start_time = microtime(true);
// set default dirs
$this->setTemplateDir('.' . DS . 'templates' . DS)
->setCompileDir('.' . DS . 'templates_c' . DS)
->setPluginsDir(SMARTY_PLUGINS_DIR)
->setCacheDir('.' . DS . 'cache' . DS)
->setConfigDir('.' . DS . 'configs' . DS);
$this->debug_tpl = 'file:' . dirname(__FILE__) . '/debug.tpl';
if (isset($_SERVER['SCRIPT_NAME'])) {
$this->assignGlobal('SCRIPT_NAME', $_SERVER['SCRIPT_NAME']);
}
}
/**
* Class destructor
*/
public function __destruct()
{
// intentionally left blank
}
/**
* <> set selfpointer on cloned object
*/
public function __clone()
{
$this->smarty = $this;
}
/**
* <> Generic getter.
*
* Calls the appropriate getter function.
* Issues an E_USER_NOTICE if no valid getter is found.
*
* @param string $name property name
* @return mixed
*/
public function __get($name)
{
$allowed = array(
'template_dir' => 'getTemplateDir',
'config_dir' => 'getConfigDir',
'plugins_dir' => 'getPluginsDir',
'compile_dir' => 'getCompileDir',
'cache_dir' => 'getCacheDir',
);
if (isset($allowed[$name])) {
return $this->{$allowed[$name]}();
} else {
trigger_error('Undefined property: '. get_class($this) .'::$'. $name, E_USER_NOTICE);
}
}
/**
* <> Generic setter.
*
* Calls the appropriate setter function.
* Issues an E_USER_NOTICE if no valid setter is found.
*
* @param string $name property name
* @param mixed $value parameter passed to setter
*/
public function __set($name, $value)
{
$allowed = array(
'template_dir' => 'setTemplateDir',
'config_dir' => 'setConfigDir',
'plugins_dir' => 'setPluginsDir',
'compile_dir' => 'setCompileDir',
'cache_dir' => 'setCacheDir',
);
if (isset($allowed[$name])) {
$this->{$allowed[$name]}($value);
} else {
trigger_error('Undefined property: ' . get_class($this) . '::$' . $name, E_USER_NOTICE);
}
}
/**
* Check if a template resource exists
*
* @param string $resource_name template name
* @return boolean status
*/
public function templateExists($resource_name)
{
// create template object
$save = $this->template_objects;
$tpl = new $this->template_class($resource_name, $this);
// check if it does exists
$result = $tpl->source->exists;
$this->template_objects = $save;
return $result;
}
/**
* Returns a single or all global variables
*
* @param object $smarty
* @param string $varname variable name or null
* @return string variable value or or array of variables
*/
public function getGlobal($varname = null)
{
if (isset($varname)) {
if (isset(self::$global_tpl_vars[$varname])) {
return self::$global_tpl_vars[$varname]->value;
} else {
return '';
}
} else {
$_result = array();
foreach (self::$global_tpl_vars AS $key => $var) {
$_result[$key] = $var->value;
}
return $_result;
}
}
/**
* Empty cache folder
*
* @param integer $exp_time expiration time
* @param string $type resource type
* @return integer number of cache files deleted
*/
function clearAllCache($exp_time = null, $type = null)
{
// load cache resource and call clearAll
$_cache_resource = Smarty_CacheResource::load($this, $type);
Smarty_CacheResource::invalidLoadedCache($this);
return $_cache_resource->clearAll($this, $exp_time);
}
/**
* Empty cache for a specific template
*
* @param string $template_name template name
* @param string $cache_id cache id
* @param string $compile_id compile id
* @param integer $exp_time expiration time
* @param string $type resource type
* @return integer number of cache files deleted
*/
public function clearCache($template_name, $cache_id = null, $compile_id = null, $exp_time = null, $type = null)
{
// load cache resource and call clear
$_cache_resource = Smarty_CacheResource::load($this, $type);
Smarty_CacheResource::invalidLoadedCache($this);
return $_cache_resource->clear($this, $template_name, $cache_id, $compile_id, $exp_time);
}
/**
* Loads security class and enables security
*
* @param string|Smarty_Security $security_class if a string is used, it must be class-name
* @return Smarty current Smarty instance for chaining
* @throws SmartyException when an invalid class name is provided
*/
public function enableSecurity($security_class = null)
{
if ($security_class instanceof Smarty_Security) {
$this->security_policy = $security_class;
return $this;
} elseif (is_object($security_class)) {
throw new SmartyException("Class '" . get_class($security_class) . "' must extend Smarty_Security.");
}
if ($security_class == null) {
$security_class = $this->security_class;
}
if (!class_exists($security_class)) {
throw new SmartyException("Security class '$security_class' is not defined");
} elseif ($security_class !== 'Smarty_Security' && !is_subclass_of($security_class, 'Smarty_Security')) {
throw new SmartyException("Class '$security_class' must extend Smarty_Security.");
} else {
$this->security_policy = new $security_class($this);
}
return $this;
}
/**
* Disable security
* @return Smarty current Smarty instance for chaining
*/
public function disableSecurity()
{
$this->security_policy = null;
return $this;
}
/**
* Set template directory
*
* @param string|array $template_dir directory(s) of template sources
* @return Smarty current Smarty instance for chaining
*/
public function setTemplateDir($template_dir)
{
$this->template_dir = array();
foreach ((array) $template_dir as $k => $v) {
$this->template_dir[$k] = rtrim($v, '/\\') . DS;
}
$this->joined_template_dir = join(DIRECTORY_SEPARATOR, $this->template_dir);
return $this;
}
/**
* Add template directory(s)
*
* @param string|array $template_dir directory(s) of template sources
* @param string $key of the array element to assign the template dir to
* @return Smarty current Smarty instance for chaining
* @throws SmartyException when the given template directory is not valid
*/
public function addTemplateDir($template_dir, $key=null)
{
// make sure we're dealing with an array
$this->template_dir = (array) $this->template_dir;
if (is_array($template_dir)) {
foreach ($template_dir as $k => $v) {
if (is_int($k)) {
// indexes are not merged but appended
$this->template_dir[] = rtrim($v, '/\\') . DS;
} else {
// string indexes are overridden
$this->template_dir[$k] = rtrim($v, '/\\') . DS;
}
}
} elseif ($key !== null) {
// override directory at specified index
$this->template_dir[$key] = rtrim($template_dir, '/\\') . DS;
} else {
// append new directory
$this->template_dir[] = rtrim($template_dir, '/\\') . DS;
}
$this->joined_template_dir = join(DIRECTORY_SEPARATOR, $this->template_dir);
return $this;
}
/**
* Get template directories
*
* @param mixed index of directory to get, null to get all
* @return array|string list of template directories, or directory of $index
*/
public function getTemplateDir($index=null)
{
if ($index !== null) {
return isset($this->template_dir[$index]) ? $this->template_dir[$index] : null;
}
return (array)$this->template_dir;
}
/**
* Set config directory
*
* @param string|array $template_dir directory(s) of configuration sources
* @return Smarty current Smarty instance for chaining
*/
public function setConfigDir($config_dir)
{
$this->config_dir = array();
foreach ((array) $config_dir as $k => $v) {
$this->config_dir[$k] = rtrim($v, '/\\') . DS;
}
$this->joined_config_dir = join(DIRECTORY_SEPARATOR, $this->config_dir);
return $this;
}
/**
* Add config directory(s)
*
* @param string|array $config_dir directory(s) of config sources
* @param string key of the array element to assign the config dir to
* @return Smarty current Smarty instance for chaining
*/
public function addConfigDir($config_dir, $key=null)
{
// make sure we're dealing with an array
$this->config_dir = (array) $this->config_dir;
if (is_array($config_dir)) {
foreach ($config_dir as $k => $v) {
if (is_int($k)) {
// indexes are not merged but appended
$this->config_dir[] = rtrim($v, '/\\') . DS;
} else {
// string indexes are overridden
$this->config_dir[$k] = rtrim($v, '/\\') . DS;
}
}
} elseif( $key !== null ) {
// override directory at specified index
$this->config_dir[$key] = rtrim($config_dir, '/\\') . DS;
} else {
// append new directory
$this->config_dir[] = rtrim($config_dir, '/\\') . DS;
}
$this->joined_config_dir = join(DIRECTORY_SEPARATOR, $this->config_dir);
return $this;
}
/**
* Get config directory
*
* @param mixed index of directory to get, null to get all
* @return array|string configuration directory
*/
public function getConfigDir($index=null)
{
if ($index !== null) {
return isset($this->config_dir[$index]) ? $this->config_dir[$index] : null;
}
return (array)$this->config_dir;
}
/**
* Set plugins directory
*
* @param string|array $plugins_dir directory(s) of plugins
* @return Smarty current Smarty instance for chaining
*/
public function setPluginsDir($plugins_dir)
{
$this->plugins_dir = array();
foreach ((array)$plugins_dir as $k => $v) {
$this->plugins_dir[$k] = rtrim($v, '/\\') . DS;
}
return $this;
}
/**
* Adds directory of plugin files
*
* @param object $smarty
* @param string $ |array $ plugins folder
* @return Smarty current Smarty instance for chaining
*/
public function addPluginsDir($plugins_dir)
{
// make sure we're dealing with an array
$this->plugins_dir = (array) $this->plugins_dir;
if (is_array($plugins_dir)) {
foreach ($plugins_dir as $k => $v) {
if (is_int($k)) {
// indexes are not merged but appended
$this->plugins_dir[] = rtrim($v, '/\\') . DS;
} else {
// string indexes are overridden
$this->plugins_dir[$k] = rtrim($v, '/\\') . DS;
}
}
} else {
// append new directory
$this->plugins_dir[] = rtrim($plugins_dir, '/\\') . DS;
}
$this->plugins_dir = array_unique($this->plugins_dir);
return $this;
}
/**
* Get plugin directories
*
* @return array list of plugin directories
*/
public function getPluginsDir()
{
return (array)$this->plugins_dir;
}
/**
* Set compile directory
*
* @param string $compile_dir directory to store compiled templates in
* @return Smarty current Smarty instance for chaining
*/
public function setCompileDir($compile_dir)
{
$this->compile_dir = rtrim($compile_dir, '/\\') . DS;
if (!isset(Smarty::$_muted_directories[$this->compile_dir])) {
Smarty::$_muted_directories[$this->compile_dir] = null;
}
return $this;
}
/**
* Get compiled directory
*
* @return string path to compiled templates
*/
public function getCompileDir()
{
return $this->compile_dir;
}
/**
* Set cache directory
*
* @param string $cache_dir directory to store cached templates in
* @return Smarty current Smarty instance for chaining
*/
public function setCacheDir($cache_dir)
{
$this->cache_dir = rtrim($cache_dir, '/\\') . DS;
if (!isset(Smarty::$_muted_directories[$this->cache_dir])) {
Smarty::$_muted_directories[$this->cache_dir] = null;
}
return $this;
}
/**
* Get cache directory
*
* @return string path of cache directory
*/
public function getCacheDir()
{
return $this->cache_dir;
}
/**
* Set default modifiers
*
* @param array|string $modifiers modifier or list of modifiers to set
* @return Smarty current Smarty instance for chaining
*/
public function setDefaultModifiers($modifiers)
{
$this->default_modifiers = (array) $modifiers;
return $this;
}
/**
* Add default modifiers
*
* @param array|string $modifiers modifier or list of modifiers to add
* @return Smarty current Smarty instance for chaining
*/
public function addDefaultModifiers($modifiers)
{
if (is_array($modifiers)) {
$this->default_modifiers = array_merge($this->default_modifiers, $modifiers);
} else {
$this->default_modifiers[] = $modifiers;
}
return $this;
}
/**
* Get default modifiers
*
* @return array list of default modifiers
*/
public function getDefaultModifiers()
{
return $this->default_modifiers;
}
/**
* Set autoload filters
*
* @param array $filters filters to load automatically
* @param string $type "pre", "output", … specify the filter type to set. Defaults to none treating $filters' keys as the appropriate types
* @return Smarty current Smarty instance for chaining
*/
public function setAutoloadFilters($filters, $type=null)
{
if ($type !== null) {
$this->autoload_filters[$type] = (array) $filters;
} else {
$this->autoload_filters = (array) $filters;
}
return $this;
}
/**
* Add autoload filters
*
* @param array $filters filters to load automatically
* @param string $type "pre", "output", … specify the filter type to set. Defaults to none treating $filters' keys as the appropriate types
* @return Smarty current Smarty instance for chaining
*/
public function addAutoloadFilters($filters, $type=null)
{
if ($type !== null) {
if (!empty($this->autoload_filters[$type])) {
$this->autoload_filters[$type] = array_merge($this->autoload_filters[$type], (array) $filters);
} else {
$this->autoload_filters[$type] = (array) $filters;
}
} else {
foreach ((array) $filters as $key => $value) {
if (!empty($this->autoload_filters[$key])) {
$this->autoload_filters[$key] = array_merge($this->autoload_filters[$key], (array) $value);
} else {
$this->autoload_filters[$key] = (array) $value;
}
}
}
return $this;
}
/**
* Get autoload filters
*
* @param string $type type of filter to get autoloads for. Defaults to all autoload filters
* @return array array( 'type1' => array( 'filter1', 'filter2', … ) ) or array( 'filter1', 'filter2', …) if $type was specified
*/
public function getAutoloadFilters($type=null)
{
if ($type !== null) {
return isset($this->autoload_filters[$type]) ? $this->autoload_filters[$type] : array();
}
return $this->autoload_filters;
}
/**
* return name of debugging template
*
* @return string
*/
public function getDebugTemplate()
{
return $this->debug_tpl;
}
/**
* set the debug template
*
* @param string $tpl_name
* @return Smarty current Smarty instance for chaining
* @throws SmartyException if file is not readable
*/
public function setDebugTemplate($tpl_name)
{
if (!is_readable($tpl_name)) {
throw new SmartyException("Unknown file '{$tpl_name}'");
}
$this->debug_tpl = $tpl_name;
return $this;
}
/**
* creates a template object
*
* @param string $template the resource handle of the template file
* @param mixed $cache_id cache id to be used with this template
* @param mixed $compile_id compile id to be used with this template
* @param object $parent next higher level of Smarty variables
* @param boolean $do_clone flag is Smarty object shall be cloned
* @return object template object
*/
public function createTemplate($template, $cache_id = null, $compile_id = null, $parent = null, $do_clone = true)
{
if (!empty($cache_id) && (is_object($cache_id) || is_array($cache_id))) {
$parent = $cache_id;
$cache_id = null;
}
if (!empty($parent) && is_array($parent)) {
$data = $parent;
$parent = null;
} else {
$data = null;
}
// default to cache_id and compile_id of Smarty object
$cache_id = $cache_id === null ? $this->cache_id : $cache_id;
$compile_id = $compile_id === null ? $this->compile_id : $compile_id;
// already in template cache?
if ($this->allow_ambiguous_resources) {
$_templateId = Smarty_Resource::getUniqueTemplateName($this, $template) . $cache_id . $compile_id;
} else {
$_templateId = $this->joined_template_dir . '#' . $template . $cache_id . $compile_id;
}
if (isset($_templateId[150])) {
$_templateId = sha1($_templateId);
}
if ($do_clone) {
if (isset($this->template_objects[$_templateId])) {
// return cached template object
$tpl = clone $this->template_objects[$_templateId];
$tpl->smarty = clone $tpl->smarty;
$tpl->parent = $parent;
$tpl->tpl_vars = array();
$tpl->config_vars = array();
} else {
$tpl = new $this->template_class($template, clone $this, $parent, $cache_id, $compile_id);
}
} else {
if (isset($this->template_objects[$_templateId])) {
// return cached template object
$tpl = $this->template_objects[$_templateId];
$tpl->parent = $parent;
$tpl->tpl_vars = array();
$tpl->config_vars = array();
} else {
$tpl = new $this->template_class($template, $this, $parent, $cache_id, $compile_id);
}
}
// fill data if present
if (!empty($data) && is_array($data)) {
// set up variable values
foreach ($data as $_key => $_val) {
$tpl->tpl_vars[$_key] = new Smarty_variable($_val);
}
}
return $tpl;
}
/**
* Takes unknown classes and loads plugin files for them
* class name format: Smarty_PluginType_PluginName
* plugin filename format: plugintype.pluginname.php
*
* @param string $plugin_name class plugin name to load
* @param bool $check check if already loaded
* @return string |boolean filepath of loaded file or false
*/
public function loadPlugin($plugin_name, $check = true)
{
// if function or class exists, exit silently (already loaded)
if ($check && (is_callable($plugin_name) || class_exists($plugin_name, false))) {
return true;
}
// Plugin name is expected to be: Smarty_[Type]_[Name]
$_name_parts = explode('_', $plugin_name, 3);
// class name must have three parts to be valid plugin
// count($_name_parts) < 3 === !isset($_name_parts[2])
if (!isset($_name_parts[2]) || strtolower($_name_parts[0]) !== 'smarty') {
throw new SmartyException("plugin {$plugin_name} is not a valid name format");
return false;
}
// if type is "internal", get plugin from sysplugins
if (strtolower($_name_parts[1]) == 'internal') {
$file = SMARTY_SYSPLUGINS_DIR . strtolower($plugin_name) . '.php';
if (file_exists($file)) {
require_once($file);
return $file;
} else {
return false;
}
}
// plugin filename is expected to be: [type].[name].php
$_plugin_filename = "{$_name_parts[1]}.{$_name_parts[2]}.php";
$_stream_resolve_include_path = function_exists('stream_resolve_include_path');
// loop through plugin dirs and find the plugin
foreach($this->getPluginsDir() as $_plugin_dir) {
$names = array(
$_plugin_dir . $_plugin_filename,
$_plugin_dir . strtolower($_plugin_filename),
);
foreach ($names as $file) {
if (file_exists($file)) {
require_once($file);
return $file;
}
if ($this->use_include_path && !preg_match('/^([\/\\\\]|[a-zA-Z]:[\/\\\\])/', $_plugin_dir)) {
// try PHP include_path
if ($_stream_resolve_include_path) {
$file = stream_resolve_include_path($file);
} else {
$file = Smarty_Internal_Get_Include_Path::getIncludePath($file);
}
if ($file !== false) {
require_once($file);
return $file;
}
}
}
}
// no plugin loaded
return false;
}
/**
* Compile all template files
*
* @param string $extension file extension
* @param bool $force_compile force all to recompile
* @param int $time_limit
* @param int $max_errors
* @return integer number of template files recompiled
*/
public function compileAllTemplates($extention = '.tpl', $force_compile = false, $time_limit = 0, $max_errors = null)
{
return Smarty_Internal_Utility::compileAllTemplates($extention, $force_compile, $time_limit, $max_errors, $this);
}
/**
* Compile all config files
*
* @param string $extension file extension
* @param bool $force_compile force all to recompile
* @param int $time_limit
* @param int $max_errors
* @return integer number of template files recompiled
*/
public function compileAllConfig($extention = '.conf', $force_compile = false, $time_limit = 0, $max_errors = null)
{
return Smarty_Internal_Utility::compileAllConfig($extention, $force_compile, $time_limit, $max_errors, $this);
}
/**
* Delete compiled template file
*
* @param string $resource_name template name
* @param string $compile_id compile id
* @param integer $exp_time expiration time
* @return integer number of template files deleted
*/
public function clearCompiledTemplate($resource_name = null, $compile_id = null, $exp_time = null)
{
return Smarty_Internal_Utility::clearCompiledTemplate($resource_name, $compile_id, $exp_time, $this);
}
/**
* Return array of tag/attributes of all tags used by an template
*
* @param object $templae template object
* @return array of tag/attributes
*/
public function getTags(Smarty_Internal_Template $template)
{
return Smarty_Internal_Utility::getTags($template);
}
/**
* Run installation test
*
* @param array $errors Array to write errors into, rather than outputting them
* @return boolean true if setup is fine, false if something is wrong
*/
public function testInstall(&$errors=null)
{
return Smarty_Internal_Utility::testInstall($this, $errors);
}
/**
* Error Handler to mute expected messages
*
* @link http://php.net/set_error_handler
* @param integer $errno Error level
* @return boolean
*/
public static function mutingErrorHandler($errno, $errstr, $errfile, $errline, $errcontext)
{
$_is_muted_directory = false;
// add the SMARTY_DIR to the list of muted directories
if (!isset(Smarty::$_muted_directories[SMARTY_DIR])) {
$smarty_dir = realpath(SMARTY_DIR);
if ($smarty_dir !== false) {
Smarty::$_muted_directories[SMARTY_DIR] = array(
'file' => $smarty_dir,
'length' => strlen($smarty_dir),
);
}
}
// walk the muted directories and test against $errfile
foreach (Smarty::$_muted_directories as $key => &$dir) {
if (!$dir) {
// resolve directory and length for speedy comparisons
$file = realpath($key);
if ($file === false) {
// this directory does not exist, remove and skip it
unset(Smarty::$_muted_directories[$key]);
continue;
}
$dir = array(
'file' => $file,
'length' => strlen($file),
);
}
if (!strncmp($errfile, $dir['file'], $dir['length'])) {
$_is_muted_directory = true;
break;
}
}
// pass to next error handler if this error did not occur inside SMARTY_DIR
// or the error was within smarty but masked to be ignored
if (!$_is_muted_directory || ($errno && $errno & error_reporting())) {
if (Smarty::$_previous_error_handler) {
return call_user_func(Smarty::$_previous_error_handler, $errno, $errstr, $errfile, $errline, $errcontext);
} else {
return false;
}
}
}
/**
* Enable error handler to mute expected messages
*
* @return void
*/
public static function muteExpectedErrors()
{
/*
error muting is done because some people implemented custom error_handlers using
http://php.net/set_error_handler and for some reason did not understand the following paragraph:
It is important to remember that the standard PHP error handler is completely bypassed for the
error types specified by error_types unless the callback function returns FALSE.
error_reporting() settings will have no effect and your error handler will be called regardless -
however you are still able to read the current value of error_reporting and act appropriately.
Of particular note is that this value will be 0 if the statement that caused the error was
prepended by the @ error-control operator.
Smarty deliberately uses @filemtime() over file_exists() and filemtime() in some places. Reasons include
- @filemtime() is almost twice as fast as using an additional file_exists()
- between file_exists() and filemtime() a possible race condition is opened,
which does not exist using the simple @filemtime() approach.
*/
$error_handler = array('Smarty', 'mutingErrorHandler');
$previous = set_error_handler($error_handler);
// avoid dead loops
if ($previous !== $error_handler) {
Smarty::$_previous_error_handler = $previous;
}
}
/**
* Disable error handler muting expected messages
*
* @return void
*/
public static function unmuteExpectedErrors()
{
restore_error_handler();
}
}
// Check if we're running on windows
Smarty::$_IS_WINDOWS = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
// let PCRE (preg_*) treat strings as ISO-8859-1 if we're not dealing with UTF-8
if (Smarty::$_CHARSET !== 'UTF-8') {
Smarty::$_UTF8_MODIFIER = '';
}
/**
* Smarty exception class
* @package Smarty
*/
class SmartyException extends Exception {
public static $escape = true;
public function __construct($message) {
$this->message = self::$escape ? htmlentities($message) : $message;
}
}
/**
* Smarty compiler exception class
* @package Smarty
*/
class SmartyCompilerException extends SmartyException {
}
/**
* Autoloader
*/
function smartyAutoload($class)
{
$_class = strtolower($class);
$_classes = array(
'smarty_config_source' => true,
'smarty_config_compiled' => true,
'smarty_security' => true,
'smarty_cacheresource' => true,
'smarty_cacheresource_custom' => true,
'smarty_cacheresource_keyvaluestore' => true,
'smarty_resource' => true,
'smarty_resource_custom' => true,
'smarty_resource_uncompiled' => true,
'smarty_resource_recompiled' => true,
);
if (!strncmp($_class, 'smarty_internal_', 16) || isset($_classes[$_class])) {
include SMARTY_SYSPLUGINS_DIR . $_class . '.php';
}
}
?>
Collabtive-2.0/include/class.TCPDF.php 0000664 0000000 0000000 00003303711 12372520637 0017556 0 ustar 00root root 0000000 0000000 .
//
// See LICENSE.TXT file for more information.
// -------------------------------------------------------------------
//
// Description :
// This is a PHP class for generating PDF documents without requiring external extensions.
//
// NOTE:
// This class was originally derived in 2002 from the Public
// Domain FPDF class by Olivier Plathey (http://www.fpdf.org),
// but now is almost entirely rewritten and contains thousands of
// new lines of code and hundreds new features.
//
// Main features:
// * no external libraries are required for the basic functions;
// * all standard page formats, custom page formats, custom margins and units of measure;
// * UTF-8 Unicode and Right-To-Left languages;
// * TrueTypeUnicode, TrueType, Type1 and CID-0 fonts;
// * font subsetting;
// * methods to publish some XHTML + CSS code, Javascript and Forms;
// * images, graphic (geometric figures) and transformation methods;
// * supports JPEG, PNG and SVG images natively, all images supported by GD (GD, GD2, GD2PART, GIF, JPEG, PNG, BMP, XBM, XPM) and all images supported via ImagMagick (http://www.imagemagick.org/www/formats.html)
// * 1D and 2D barcodes: CODE 39, ANSI MH10.8M-1983, USD-3, 3 of 9, CODE 93, USS-93, Standard 2 of 5, Interleaved 2 of 5, CODE 128 A/B/C, 2 and 5 Digits UPC-Based Extention, EAN 8, EAN 13, UPC-A, UPC-E, MSI, POSTNET, PLANET, RMS4CC (Royal Mail 4-state Customer Code), CBC (Customer Bar Code), KIX (Klant index - Customer index), Intelligent Mail Barcode, Onecode, USPS-B-3200, CODABAR, CODE 11, PHARMACODE, PHARMACODE TWO-TRACKS, Datamatrix, QR-Code, PDF417;
// * JPEG and PNG ICC profiles, Grayscale, RGB, CMYK, Spot Colors and Transparencies;
// * automatic page header and footer management;
// * document encryption up to 256 bit and digital signature certifications;
// * transactions to UNDO commands;
// * PDF annotations, including links, text and file attachments;
// * text rendering modes (fill, stroke and clipping);
// * multiple columns mode;
// * no-write page regions;
// * bookmarks, named destinations and table of content;
// * text hyphenation;
// * text stretching and spacing (tracking);
// * automatic page break, line break and text alignments including justification;
// * automatic page numbering and page groups;
// * move and delete pages;
// * page compression (requires php-zlib extension);
// * XOBject Templates;
// * Layers and object visibility.
// * PDF/A-1b support.
//
// -----------------------------------------------------------
// THANKS TO:
//
// Olivier Plathey (http://www.fpdf.org) for original FPDF.
// Efthimios Mavrogeorgiadis (emavro@yahoo.com) for suggestions on RTL language support.
// Klemen Vodopivec (http://www.fpdf.de/downloads/addons/37/) for Encryption algorithm.
// Warren Sherliker (wsherliker@gmail.com) for better image handling.
// dullus for text Justification.
// Bob Vincent (pillarsdotnet@users.sourceforge.net) for
value attribute.
// Patrick Benny for text stretch suggestion on Cell().
// Johannes Gntert for JavaScript support.
// Denis Van Nuffelen for Dynamic Form.
// Jacek Czekaj for multibyte justification
// Anthony Ferrara for the reintroduction of legacy image methods.
// Sourceforge user 1707880 (hucste) for line-trough mode.
// Larry Stanbery for page groups.
// Martin Hall-May for transparency.
// Aaron C. Spike for Polycurve method.
// Mohamad Ali Golkar, Saleh AlMatrafe, Charles Abbott for Arabic and Persian support.
// Moritz Wagner and Andreas Wurmser for graphic functions.
// Andrew Whitehead for core fonts support.
// Esteban Jol Marn for OpenType font conversion.
// Teus Hagen for several suggestions and fixes.
// Yukihiro Nakadaira for CID-0 CJK fonts fixes.
// Kosmas Papachristos for some CSS improvements.
// Marcel Partap for some fixes.
// Won Kyu Park for several suggestions, fixes and patches.
// Dominik Dzienia for QR-code support.
// Laurent Minguet for some suggestions.
// Christian Deligant for some suggestions and fixes.
// Travis Harris for crop mark suggestion.
// Aleksey Kuznetsov for some suggestions and text shadows.
// Jim Hanlon for several suggestions and patches.
// Anyone else that has reported a bug or sent a suggestion.
//============================================================+
/**
* @file
* This is a PHP class for generating PDF documents without requiring external extensions.
* TCPDF project (http://www.tcpdf.org) was originally derived in 2002 from the Public Domain FPDF class by Olivier Plathey (http://www.fpdf.org), but now is almost entirely rewritten.
*
TCPDF main features are:
*
*
no external libraries are required for the basic functions;
*
all standard page formats, custom page formats, custom margins and units of measure;
*
UTF-8 Unicode and Right-To-Left languages;
*
TrueTypeUnicode, TrueType, Type1 and CID-0 fonts;
*
font subsetting;
*
methods to publish some XHTML + CSS code, Javascript and Forms;
*
images, graphic (geometric figures) and transformation methods;
*
supports JPEG, PNG and SVG images natively, all images supported by GD (GD, GD2, GD2PART, GIF, JPEG, PNG, BMP, XBM, XPM) and all images supported via ImagMagick (http://www.imagemagick.org/www/formats.html)
*
1D and 2D barcodes: CODE 39, ANSI MH10.8M-1983, USD-3, 3 of 9, CODE 93, USS-93, Standard 2 of 5, Interleaved 2 of 5, CODE 128 A/B/C, 2 and 5 Digits UPC-Based Extention, EAN 8, EAN 13, UPC-A, UPC-E, MSI, POSTNET, PLANET, RMS4CC (Royal Mail 4-state Customer Code), CBC (Customer Bar Code), KIX (Klant index - Customer index), Intelligent Mail Barcode, Onecode, USPS-B-3200, CODABAR, CODE 11, PHARMACODE, PHARMACODE TWO-TRACKS, Datamatrix, QR-Code, PDF417;
*
JPEG and PNG ICC profiles, Grayscale, RGB, CMYK, Spot Colors and Transparencies;
*
automatic page header and footer management;
*
document encryption up to 256 bit and digital signature certifications;
*
transactions to UNDO commands;
*
PDF annotations, including links, text and file attachments;
*
text rendering modes (fill, stroke and clipping);
*
multiple columns mode;
*
no-write page regions;
*
bookmarks, named destinations and table of content;
*
text hyphenation;
*
text stretching and spacing (tracking);
*
automatic page break, line break and text alignments including justification;
*
automatic page numbering and page groups;
*
move and delete pages;
*
page compression (requires php-zlib extension);
*
XOBject Templates;
*
Layers and object visibility;
*
PDF/A-1b support.
*
* Tools to encode your unicode fonts are on fonts/utils directory.
* @package com.tecnick.tcpdf
* @author Nicola Asuni
* @version 6.0.011
*/
if (!defined('K_TCPDF_EXTERNAL_CONFIG')) {
// DOCUMENT_ROOT fix for IIS Webserver
if ((!isset($_SERVER['DOCUMENT_ROOT'])) OR (empty($_SERVER['DOCUMENT_ROOT']))) {
if(isset($_SERVER['SCRIPT_FILENAME'])) {
$_SERVER['DOCUMENT_ROOT'] = str_replace( '\\', '/', substr($_SERVER['SCRIPT_FILENAME'], 0, 0-strlen($_SERVER['PHP_SELF'])));
} elseif(isset($_SERVER['PATH_TRANSLATED'])) {
$_SERVER['DOCUMENT_ROOT'] = str_replace( '\\', '/', substr(str_replace('\\\\', '\\', $_SERVER['PATH_TRANSLATED']), 0, 0-strlen($_SERVER['PHP_SELF'])));
} else {
// define here your DOCUMENT_ROOT path if the previous fails (e.g. '/var/www')
$_SERVER['DOCUMENT_ROOT'] = '/var/www';
}
}
// be sure that the end slash is present
$_SERVER['DOCUMENT_ROOT'] = str_replace('//', '/', $_SERVER['DOCUMENT_ROOT'].'/');
// Automatic calculation for the following K_PATH_MAIN constant
$k_path_main = realpath(dirname(__FILE__))."/";
/**
* Installation path (/var/www/tcpdf/).
* By default it is automatically calculated but you can also set it as a fixed string to improve performances.
*/
define ('K_PATH_MAIN', $k_path_main);
// Automatic calculation for the following K_PATH_URL constant
$k_path_url = $k_path_main; // default value for console mode
if (isset($_SERVER['HTTP_HOST']) AND (!empty($_SERVER['HTTP_HOST']))) {
if(isset($_SERVER['HTTPS']) AND (!empty($_SERVER['HTTPS'])) AND strtolower($_SERVER['HTTPS'])!='off') {
$k_path_url = 'https://';
} else {
$k_path_url = 'http://';
}
$k_path_url .= $_SERVER['HTTP_HOST'];
$k_path_url .= str_replace( '\\', '/', substr($_SERVER['PHP_SELF'], 0, -24));
}
/**
* URL path to tcpdf installation folder (http://localhost/tcpdf/).
* By default it is automatically calculated but you can also set it as a fixed string to improve performances.
*/
define ('K_PATH_URL', $k_path_url);
/**
* path for PDF fonts
* use K_PATH_MAIN.'fonts/old/' for old non-UTF8 fonts
*/
define ('K_PATH_FONTS', K_PATH_MAIN.'font/');
/**
* cache directory for temporary files (full path)
*/
define ('K_PATH_CACHE', K_PATH_MAIN.'cache/');
/**
* cache directory for temporary files (url path)
*/
define ('K_PATH_URL_CACHE', K_PATH_URL.'cache/');
/**
*images directory
*/
define ('K_PATH_IMAGES', K_PATH_MAIN.'images/');
/**
* blank image
*/
define ('K_BLANK_IMAGE', K_PATH_IMAGES.'_blank.png');
/**
* page format
*/
define ('PDF_PAGE_FORMAT', 'A4');
/**
* page orientation (P=portrait, L=landscape)
*/
define ('PDF_PAGE_ORIENTATION', 'P');
/**
* document creator
*/
define ('PDF_CREATOR', 'Collabtive');
/**
* document author
*/
define ('PDF_AUTHOR', '');
/**
* header title
*/
define ('PDF_HEADER_TITLE', ' ');
/**
* header description string
*/
define ('PDF_HEADER_STRING', "");
/**
* image logo
*/
define ('PDF_HEADER_LOGO', '');
/**
* header logo image width [mm]
*/
define ('PDF_HEADER_LOGO_WIDTH', 30);
/**
* document unit of measure [pt=point, mm=millimeter, cm=centimeter, in=inch]
*/
define ('PDF_UNIT', 'mm');
/**
* header margin
*/
define ('PDF_MARGIN_HEADER', 5);
/**
* footer margin
*/
define ('PDF_MARGIN_FOOTER', 10);
/**
* top margin
*/
define ('PDF_MARGIN_TOP', 25);
/**
* bottom margin
*/
define ('PDF_MARGIN_BOTTOM', 17);
/**
* left margin
*/
define ('PDF_MARGIN_LEFT', 15);
/**
* right margin
*/
define ('PDF_MARGIN_RIGHT', 15);
/**
* default main font name
*/
define ('PDF_FONT_NAME_MAIN', 'helvetica');
/**
* default main font size
*/
define ('PDF_FONT_SIZE_MAIN', 10);
/**
* default data font name
*/
define ('PDF_FONT_NAME_DATA', 'freeserif');
/**
* default data font size
*/
define ('PDF_FONT_SIZE_DATA', 8);
/**
* default monospaced font name
*/
define ('PDF_FONT_MONOSPACED', 'courier');
/**
* ratio used to adjust the conversion of pixels to user units
*/
define ('PDF_IMAGE_SCALE_RATIO', 4);
/**
* magnification factor for titles
*/
define('HEAD_MAGNIFICATION', 0);
/**
* height of cell respect font height
*/
define('K_CELL_HEIGHT_RATIO', 1.25);
/**
* title magnification respect main font size
*/
define('K_TITLE_MAGNIFICATION', 1.3);
/**
* reduction factor for small font
*/
define('K_SMALL_RATIO', 2/3);
/**
* set to true to enable the special procedure used to avoid the overlappind of symbols on Thai language
*/
define('K_THAI_TOPCHARS', true);
/**
* if true allows to call TCPDF methods using HTML syntax
* IMPORTANT: For security reason, disable this feature if you are printing user HTML content.
*/
define('K_TCPDF_CALLS_IN_HTML', true);
/**
* if true adn PHP version is greater than 5, then the Error() method throw new exception instead of terminating the execution.
*/
define('K_TCPDF_THROW_EXCEPTION_ERROR', false);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// TCPDF static font methods and data
require_once(dirname(__FILE__).'/include/tcpdf_font_data.php');
// TCPDF static font methods and data
require_once(dirname(__FILE__).'/include/tcpdf_fonts.php');
// TCPDF static color methods and data
require(dirname(__FILE__).'/include/tcpdf_colors.php');
// TCPDF static image methods and data
require(dirname(__FILE__).'/include/tcpdf_images.php');
// TCPDF static methods and data
require_once(dirname(__FILE__).'/include/tcpdf_static.php');
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
/**
* @class TCPDF
* PHP class for generating PDF documents without requiring external extensions.
* TCPDF project (http://www.tcpdf.org) has been originally derived in 2002 from the Public Domain FPDF class by Olivier Plathey (http://www.fpdf.org), but now is almost entirely rewritten.
* @package com.tecnick.tcpdf
* @brief PHP class for generating PDF documents without requiring external extensions.
* @version 6.0.011
* @author Nicola Asuni - info@tecnick.com
*/
class TCPDF {
// Protected properties
/**
* Current page number.
* @protected
*/
protected $page;
/**
* Current object number.
* @protected
*/
protected $n;
/**
* Array of object offsets.
* @protected
*/
protected $offsets = array();
/**
* Array of object IDs for each page.
* @protected
*/
protected $pageobjects = array();
/**
* Buffer holding in-memory PDF.
* @protected
*/
protected $buffer;
/**
* Array containing pages.
* @protected
*/
protected $pages = array();
/**
* Current document state.
* @protected
*/
protected $state;
/**
* Compression flag.
* @protected
*/
protected $compress;
/**
* Current page orientation (P = Portrait, L = Landscape).
* @protected
*/
protected $CurOrientation;
/**
* Page dimensions.
* @protected
*/
protected $pagedim = array();
/**
* Scale factor (number of points in user unit).
* @protected
*/
protected $k;
/**
* Width of page format in points.
* @protected
*/
protected $fwPt;
/**
* Height of page format in points.
* @protected
*/
protected $fhPt;
/**
* Current width of page in points.
* @protected
*/
protected $wPt;
/**
* Current height of page in points.
* @protected
*/
protected $hPt;
/**
* Current width of page in user unit.
* @protected
*/
protected $w;
/**
* Current height of page in user unit.
* @protected
*/
protected $h;
/**
* Left margin.
* @protected
*/
protected $lMargin;
/**
* Right margin.
* @protected
*/
protected $rMargin;
/**
* Cell left margin (used by regions).
* @protected
*/
protected $clMargin;
/**
* Cell right margin (used by regions).
* @protected
*/
protected $crMargin;
/**
* Top margin.
* @protected
*/
protected $tMargin;
/**
* Page break margin.
* @protected
*/
protected $bMargin;
/**
* Array of cell internal paddings ('T' => top, 'R' => right, 'B' => bottom, 'L' => left).
* @since 5.9.000 (2010-10-03)
* @protected
*/
protected $cell_padding = array('T' => 0, 'R' => 0, 'B' => 0, 'L' => 0);
/**
* Array of cell margins ('T' => top, 'R' => right, 'B' => bottom, 'L' => left).
* @since 5.9.000 (2010-10-04)
* @protected
*/
protected $cell_margin = array('T' => 0, 'R' => 0, 'B' => 0, 'L' => 0);
/**
* Current horizontal position in user unit for cell positioning.
* @protected
*/
protected $x;
/**
* Current vertical position in user unit for cell positioning.
* @protected
*/
protected $y;
/**
* Height of last cell printed.
* @protected
*/
protected $lasth;
/**
* Line width in user unit.
* @protected
*/
protected $LineWidth;
/**
* Array of standard font names.
* @protected
*/
protected $CoreFonts;
/**
* Array of used fonts.
* @protected
*/
protected $fonts = array();
/**
* Array of font files.
* @protected
*/
protected $FontFiles = array();
/**
* Array of encoding differences.
* @protected
*/
protected $diffs = array();
/**
* Array of used images.
* @protected
*/
protected $images = array();
/**
* Array of cached files.
* @protected
*/
protected $cached_files = array();
/**
* Array of Annotations in pages.
* @protected
*/
protected $PageAnnots = array();
/**
* Array of internal links.
* @protected
*/
protected $links = array();
/**
* Current font family.
* @protected
*/
protected $FontFamily;
/**
* Current font style.
* @protected
*/
protected $FontStyle;
/**
* Current font ascent (distance between font top and baseline).
* @protected
* @since 2.8.000 (2007-03-29)
*/
protected $FontAscent;
/**
* Current font descent (distance between font bottom and baseline).
* @protected
* @since 2.8.000 (2007-03-29)
*/
protected $FontDescent;
/**
* Underlining flag.
* @protected
*/
protected $underline;
/**
* Overlining flag.
* @protected
*/
protected $overline;
/**
* Current font info.
* @protected
*/
protected $CurrentFont;
/**
* Current font size in points.
* @protected
*/
protected $FontSizePt;
/**
* Current font size in user unit.
* @protected
*/
protected $FontSize;
/**
* Commands for drawing color.
* @protected
*/
protected $DrawColor;
/**
* Commands for filling color.
* @protected
*/
protected $FillColor;
/**
* Commands for text color.
* @protected
*/
protected $TextColor;
/**
* Indicates whether fill and text colors are different.
* @protected
*/
protected $ColorFlag;
/**
* Automatic page breaking.
* @protected
*/
protected $AutoPageBreak;
/**
* Threshold used to trigger page breaks.
* @protected
*/
protected $PageBreakTrigger;
/**
* Flag set when processing page header.
* @protected
*/
protected $InHeader = false;
/**
* Flag set when processing page footer.
* @protected
*/
protected $InFooter = false;
/**
* Zoom display mode.
* @protected
*/
protected $ZoomMode;
/**
* Layout display mode.
* @protected
*/
protected $LayoutMode;
/**
* If true set the document information dictionary in Unicode.
* @protected
*/
protected $docinfounicode = true;
/**
* Document title.
* @protected
*/
protected $title = '';
/**
* Document subject.
* @protected
*/
protected $subject = '';
/**
* Document author.
* @protected
*/
protected $author = '';
/**
* Document keywords.
* @protected
*/
protected $keywords = '';
/**
* Document creator.
* @protected
*/
protected $creator = '';
/**
* Starting page number.
* @protected
*/
protected $starting_page_number = 1;
/**
* The right-bottom (or left-bottom for RTL) corner X coordinate of last inserted image.
* @since 2002-07-31
* @author Nicola Asuni
* @protected
*/
protected $img_rb_x;
/**
* The right-bottom corner Y coordinate of last inserted image.
* @since 2002-07-31
* @author Nicola Asuni
* @protected
*/
protected $img_rb_y;
/**
* Adjusting factor to convert pixels to user units.
* @since 2004-06-14
* @author Nicola Asuni
* @protected
*/
protected $imgscale = 1;
/**
* Boolean flag set to true when the input text is unicode (require unicode fonts).
* @since 2005-01-02
* @author Nicola Asuni
* @protected
*/
protected $isunicode = false;
/**
* PDF version.
* @since 1.5.3
* @protected
*/
protected $PDFVersion = '1.7';
/**
* ID of the stored default header template (-1 = not set).
* @protected
*/
protected $header_xobjid = -1;
/**
* If true reset the Header Xobject template at each page
* @protected
*/
protected $header_xobj_autoreset = false;
/**
* Minimum distance between header and top page margin.
* @protected
*/
protected $header_margin;
/**
* Minimum distance between footer and bottom page margin.
* @protected
*/
protected $footer_margin;
/**
* Original left margin value.
* @protected
* @since 1.53.0.TC013
*/
protected $original_lMargin;
/**
* Original right margin value.
* @protected
* @since 1.53.0.TC013
*/
protected $original_rMargin;
/**
* Default font used on page header.
* @protected
*/
protected $header_font;
/**
* Default font used on page footer.
* @protected
*/
protected $footer_font;
/**
* Language templates.
* @protected
*/
protected $l;
/**
* Barcode to print on page footer (only if set).
* @protected
*/
protected $barcode = false;
/**
* Boolean flag to print/hide page header.
* @protected
*/
protected $print_header = true;
/**
* Boolean flag to print/hide page footer.
* @protected
*/
protected $print_footer = true;
/**
* Header image logo.
* @protected
*/
protected $header_logo = '';
/**
* Width of header image logo in user units.
* @protected
*/
protected $header_logo_width = 30;
/**
* Title to be printed on default page header.
* @protected
*/
protected $header_title = '';
/**
* String to pring on page header after title.
* @protected
*/
protected $header_string = '';
/**
* Color for header text (RGB array).
* @since 5.9.174 (2012-07-25)
* @protected
*/
protected $header_text_color = array(0,0,0);
/**
* Color for header line (RGB array).
* @since 5.9.174 (2012-07-25)
* @protected
*/
protected $header_line_color = array(0,0,0);
/**
* Color for footer text (RGB array).
* @since 5.9.174 (2012-07-25)
* @protected
*/
protected $footer_text_color = array(0,0,0);
/**
* Color for footer line (RGB array).
* @since 5.9.174 (2012-07-25)
* @protected
*/
protected $footer_line_color = array(0,0,0);
/**
* Text shadow data array.
* @since 5.9.174 (2012-07-25)
* @protected
*/
protected $txtshadow = array('enabled'=>false, 'depth_w'=>0, 'depth_h'=>0, 'color'=>false, 'opacity'=>1, 'blend_mode'=>'Normal');
/**
* Default number of columns for html table.
* @protected
*/
protected $default_table_columns = 4;
// variables for html parser
/**
* HTML PARSER: array to store current link and rendering styles.
* @protected
*/
protected $HREF = array();
/**
* List of available fonts on filesystem.
* @protected
*/
protected $fontlist = array();
/**
* Current foreground color.
* @protected
*/
protected $fgcolor;
/**
* HTML PARSER: array of boolean values, true in case of ordered list (OL), false otherwise.
* @protected
*/
protected $listordered = array();
/**
* HTML PARSER: array count list items on nested lists.
* @protected
*/
protected $listcount = array();
/**
* HTML PARSER: current list nesting level.
* @protected
*/
protected $listnum = 0;
/**
* HTML PARSER: indent amount for lists.
* @protected
*/
protected $listindent = 0;
/**
* HTML PARSER: current list indententation level.
* @protected
*/
protected $listindentlevel = 0;
/**
* Current background color.
* @protected
*/
protected $bgcolor;
/**
* Temporary font size in points.
* @protected
*/
protected $tempfontsize = 10;
/**
* Spacer string for LI tags.
* @protected
*/
protected $lispacer = '';
/**
* Default encoding.
* @protected
* @since 1.53.0.TC010
*/
protected $encoding = 'UTF-8';
/**
* PHP internal encoding.
* @protected
* @since 1.53.0.TC016
*/
protected $internal_encoding;
/**
* Boolean flag to indicate if the document language is Right-To-Left.
* @protected
* @since 2.0.000
*/
protected $rtl = false;
/**
* Boolean flag used to force RTL or LTR string direction.
* @protected
* @since 2.0.000
*/
protected $tmprtl = false;
// --- Variables used for document encryption:
/**
* IBoolean flag indicating whether document is protected.
* @protected
* @since 2.0.000 (2008-01-02)
*/
protected $encrypted;
/**
* Array containing encryption settings.
* @protected
* @since 5.0.005 (2010-05-11)
*/
protected $encryptdata = array();
/**
* Last RC4 key encrypted (cached for optimisation).
* @protected
* @since 2.0.000 (2008-01-02)
*/
protected $last_enc_key;
/**
* Last RC4 computed key.
* @protected
* @since 2.0.000 (2008-01-02)
*/
protected $last_enc_key_c;
/**
* File ID (used on document trailer).
* @protected
* @since 5.0.005 (2010-05-12)
*/
protected $file_id;
// --- bookmark ---
/**
* Outlines for bookmark.
* @protected
* @since 2.1.002 (2008-02-12)
*/
protected $outlines = array();
/**
* Outline root for bookmark.
* @protected
* @since 2.1.002 (2008-02-12)
*/
protected $OutlineRoot;
// --- javascript and form ---
/**
* Javascript code.
* @protected
* @since 2.1.002 (2008-02-12)
*/
protected $javascript = '';
/**
* Javascript counter.
* @protected
* @since 2.1.002 (2008-02-12)
*/
protected $n_js;
/**
* line trough state
* @protected
* @since 2.8.000 (2008-03-19)
*/
protected $linethrough;
/**
* Array with additional document-wide usage rights for the document.
* @protected
* @since 5.8.014 (2010-08-23)
*/
protected $ur = array();
/**
* DPI (Dot Per Inch) Document Resolution (do not change).
* @protected
* @since 3.0.000 (2008-03-27)
*/
protected $dpi = 72;
/**
* Array of page numbers were a new page group was started (the page numbers are the keys of the array).
* @protected
* @since 3.0.000 (2008-03-27)
*/
protected $newpagegroup = array();
/**
* Array that contains the number of pages in each page group.
* @protected
* @since 3.0.000 (2008-03-27)
*/
protected $pagegroups = array();
/**
* Current page group number.
* @protected
* @since 3.0.000 (2008-03-27)
*/
protected $currpagegroup = 0;
/**
* Array of transparency objects and parameters.
* @protected
* @since 3.0.000 (2008-03-27)
*/
protected $extgstates;
/**
* Set the default JPEG compression quality (1-100).
* @protected
* @since 3.0.000 (2008-03-27)
*/
protected $jpeg_quality;
/**
* Default cell height ratio.
* @protected
* @since 3.0.014 (2008-05-23)
*/
protected $cell_height_ratio = K_CELL_HEIGHT_RATIO;
/**
* PDF viewer preferences.
* @protected
* @since 3.1.000 (2008-06-09)
*/
protected $viewer_preferences;
/**
* A name object specifying how the document should be displayed when opened.
* @protected
* @since 3.1.000 (2008-06-09)
*/
protected $PageMode;
/**
* Array for storing gradient information.
* @protected
* @since 3.1.000 (2008-06-09)
*/
protected $gradients = array();
/**
* Array used to store positions inside the pages buffer (keys are the page numbers).
* @protected
* @since 3.2.000 (2008-06-26)
*/
protected $intmrk = array();
/**
* Array used to store positions inside the pages buffer (keys are the page numbers).
* @protected
* @since 5.7.000 (2010-08-03)
*/
protected $bordermrk = array();
/**
* Array used to store page positions to track empty pages (keys are the page numbers).
* @protected
* @since 5.8.007 (2010-08-18)
*/
protected $emptypagemrk = array();
/**
* Array used to store content positions inside the pages buffer (keys are the page numbers).
* @protected
* @since 4.6.021 (2009-07-20)
*/
protected $cntmrk = array();
/**
* Array used to store footer positions of each page.
* @protected
* @since 3.2.000 (2008-07-01)
*/
protected $footerpos = array();
/**
* Array used to store footer length of each page.
* @protected
* @since 4.0.014 (2008-07-29)
*/
protected $footerlen = array();
/**
* Boolean flag to indicate if a new line is created.
* @protected
* @since 3.2.000 (2008-07-01)
*/
protected $newline = true;
/**
* End position of the latest inserted line.
* @protected
* @since 3.2.000 (2008-07-01)
*/
protected $endlinex = 0;
/**
* PDF string for width value of the last line.
* @protected
* @since 4.0.006 (2008-07-16)
*/
protected $linestyleWidth = '';
/**
* PDF string for CAP value of the last line.
* @protected
* @since 4.0.006 (2008-07-16)
*/
protected $linestyleCap = '0 J';
/**
* PDF string for join value of the last line.
* @protected
* @since 4.0.006 (2008-07-16)
*/
protected $linestyleJoin = '0 j';
/**
* PDF string for dash value of the last line.
* @protected
* @since 4.0.006 (2008-07-16)
*/
protected $linestyleDash = '[] 0 d';
/**
* Boolean flag to indicate if marked-content sequence is open.
* @protected
* @since 4.0.013 (2008-07-28)
*/
protected $openMarkedContent = false;
/**
* Count the latest inserted vertical spaces on HTML.
* @protected
* @since 4.0.021 (2008-08-24)
*/
protected $htmlvspace = 0;
/**
* Array of Spot colors.
* @protected
* @since 4.0.024 (2008-09-12)
*/
protected $spot_colors = array();
/**
* Symbol used for HTML unordered list items.
* @protected
* @since 4.0.028 (2008-09-26)
*/
protected $lisymbol = '';
/**
* String used to mark the beginning and end of EPS image blocks.
* @protected
* @since 4.1.000 (2008-10-18)
*/
protected $epsmarker = 'x#!#EPS#!#x';
/**
* Array of transformation matrix.
* @protected
* @since 4.2.000 (2008-10-29)
*/
protected $transfmatrix = array();
/**
* Current key for transformation matrix.
* @protected
* @since 4.8.005 (2009-09-17)
*/
protected $transfmatrix_key = 0;
/**
* Booklet mode for double-sided pages.
* @protected
* @since 4.2.000 (2008-10-29)
*/
protected $booklet = false;
/**
* Epsilon value used for float calculations.
* @protected
* @since 4.2.000 (2008-10-29)
*/
protected $feps = 0.005;
/**
* Array used for custom vertical spaces for HTML tags.
* @protected
* @since 4.2.001 (2008-10-30)
*/
protected $tagvspaces = array();
/**
* HTML PARSER: custom indent amount for lists. Negative value means disabled.
* @protected
* @since 4.2.007 (2008-11-12)
*/
protected $customlistindent = -1;
/**
* Boolean flag to indicate if the border of the cell sides that cross the page should be removed.
* @protected
* @since 4.2.010 (2008-11-14)
*/
protected $opencell = true;
/**
* Array of files to embedd.
* @protected
* @since 4.4.000 (2008-12-07)
*/
protected $embeddedfiles = array();
/**
* Boolean flag to indicate if we are inside a PRE tag.
* @protected
* @since 4.4.001 (2008-12-08)
*/
protected $premode = false;
/**
* Array used to store positions of graphics transformation blocks inside the page buffer.
* keys are the page numbers
* @protected
* @since 4.4.002 (2008-12-09)
*/
protected $transfmrk = array();
/**
* Default color for html links.
* @protected
* @since 4.4.003 (2008-12-09)
*/
protected $htmlLinkColorArray = array(0, 0, 255);
/**
* Default font style to add to html links.
* @protected
* @since 4.4.003 (2008-12-09)
*/
protected $htmlLinkFontStyle = 'U';
/**
* Counts the number of pages.
* @protected
* @since 4.5.000 (2008-12-31)
*/
protected $numpages = 0;
/**
* Array containing page lengths in bytes.
* @protected
* @since 4.5.000 (2008-12-31)
*/
protected $pagelen = array();
/**
* Counts the number of pages.
* @protected
* @since 4.5.000 (2008-12-31)
*/
protected $numimages = 0;
/**
* Store the image keys.
* @protected
* @since 4.5.000 (2008-12-31)
*/
protected $imagekeys = array();
/**
* Length of the buffer in bytes.
* @protected
* @since 4.5.000 (2008-12-31)
*/
protected $bufferlen = 0;
/**
* If true enables disk caching.
* @protected
* @since 4.5.000 (2008-12-31)
*/
protected $diskcache = false;
/**
* Counts the number of fonts.
* @protected
* @since 4.5.000 (2009-01-02)
*/
protected $numfonts = 0;
/**
* Store the font keys.
* @protected
* @since 4.5.000 (2009-01-02)
*/
protected $fontkeys = array();
/**
* Store the font object IDs.
* @protected
* @since 4.8.001 (2009-09-09)
*/
protected $font_obj_ids = array();
/**
* Store the fage status (true when opened, false when closed).
* @protected
* @since 4.5.000 (2009-01-02)
*/
protected $pageopen = array();
/**
* Default monospace font.
* @protected
* @since 4.5.025 (2009-03-10)
*/
protected $default_monospaced_font = 'courier';
/**
* Cloned copy of the current class object.
* @protected
* @since 4.5.029 (2009-03-19)
*/
protected $objcopy;
/**
* Array used to store the lengths of cache files.
* @protected
* @since 4.5.029 (2009-03-19)
*/
protected $cache_file_length = array();
/**
* Table header content to be repeated on each new page.
* @protected
* @since 4.5.030 (2009-03-20)
*/
protected $thead = '';
/**
* Margins used for table header.
* @protected
* @since 4.5.030 (2009-03-20)
*/
protected $theadMargins = array();
/**
* Boolean flag to enable document digital signature.
* @protected
* @since 4.6.005 (2009-04-24)
*/
protected $sign = false;
/**
* Digital signature data.
* @protected
* @since 4.6.005 (2009-04-24)
*/
protected $signature_data = array();
/**
* Digital signature max length.
* @protected
* @since 4.6.005 (2009-04-24)
*/
protected $signature_max_length = 11742;
/**
* Data for digital signature appearance.
* @protected
* @since 5.3.011 (2010-06-16)
*/
protected $signature_appearance = array('page' => 1, 'rect' => '0 0 0 0');
/**
* Array of empty digital signature appearances.
* @protected
* @since 5.9.101 (2011-07-06)
*/
protected $empty_signature_appearance = array();
/**
* Regular expression used to find blank characters (required for word-wrapping).
* @protected
* @since 4.6.006 (2009-04-28)
*/
protected $re_spaces = '/[^\S\xa0]/';
/**
* Array of $re_spaces parts.
* @protected
* @since 5.5.011 (2010-07-09)
*/
protected $re_space = array('p' => '[^\S\xa0]', 'm' => '');
/**
* Digital signature object ID.
* @protected
* @since 4.6.022 (2009-06-23)
*/
protected $sig_obj_id = 0;
/**
* ID of page objects.
* @protected
* @since 4.7.000 (2009-08-29)
*/
protected $page_obj_id = array();
/**
* List of form annotations IDs.
* @protected
* @since 4.8.000 (2009-09-07)
*/
protected $form_obj_id = array();
/**
* Deafult Javascript field properties. Possible values are described on official Javascript for Acrobat API reference. Annotation options can be directly specified using the 'aopt' entry.
* @protected
* @since 4.8.000 (2009-09-07)
*/
protected $default_form_prop = array('lineWidth'=>1, 'borderStyle'=>'solid', 'fillColor'=>array(255, 255, 255), 'strokeColor'=>array(128, 128, 128));
/**
* Javascript objects array.
* @protected
* @since 4.8.000 (2009-09-07)
*/
protected $js_objects = array();
/**
* Current form action (used during XHTML rendering).
* @protected
* @since 4.8.000 (2009-09-07)
*/
protected $form_action = '';
/**
* Current form encryption type (used during XHTML rendering).
* @protected
* @since 4.8.000 (2009-09-07)
*/
protected $form_enctype = 'application/x-www-form-urlencoded';
/**
* Current method to submit forms.
* @protected
* @since 4.8.000 (2009-09-07)
*/
protected $form_mode = 'post';
/**
* List of fonts used on form fields (fontname => fontkey).
* @protected
* @since 4.8.001 (2009-09-09)
*/
protected $annotation_fonts = array();
/**
* List of radio buttons parent objects.
* @protected
* @since 4.8.001 (2009-09-09)
*/
protected $radiobutton_groups = array();
/**
* List of radio group objects IDs.
* @protected
* @since 4.8.001 (2009-09-09)
*/
protected $radio_groups = array();
/**
* Text indentation value (used for text-indent CSS attribute).
* @protected
* @since 4.8.006 (2009-09-23)
*/
protected $textindent = 0;
/**
* Store page number when startTransaction() is called.
* @protected
* @since 4.8.006 (2009-09-23)
*/
protected $start_transaction_page = 0;
/**
* Store Y position when startTransaction() is called.
* @protected
* @since 4.9.001 (2010-03-28)
*/
protected $start_transaction_y = 0;
/**
* True when we are printing the thead section on a new page.
* @protected
* @since 4.8.027 (2010-01-25)
*/
protected $inthead = false;
/**
* Array of column measures (width, space, starting Y position).
* @protected
* @since 4.9.001 (2010-03-28)
*/
protected $columns = array();
/**
* Number of colums.
* @protected
* @since 4.9.001 (2010-03-28)
*/
protected $num_columns = 1;
/**
* Current column number.
* @protected
* @since 4.9.001 (2010-03-28)
*/
protected $current_column = 0;
/**
* Starting page for columns.
* @protected
* @since 4.9.001 (2010-03-28)
*/
protected $column_start_page = 0;
/**
* Maximum page and column selected.
* @protected
* @since 5.8.000 (2010-08-11)
*/
protected $maxselcol = array('page' => 0, 'column' => 0);
/**
* Array of: X difference between table cell x start and starting page margin, cellspacing, cellpadding.
* @protected
* @since 5.8.000 (2010-08-11)
*/
protected $colxshift = array('x' => 0, 's' => array('H' => 0, 'V' => 0), 'p' => array('L' => 0, 'T' => 0, 'R' => 0, 'B' => 0));
/**
* Text rendering mode: 0 = Fill text; 1 = Stroke text; 2 = Fill, then stroke text; 3 = Neither fill nor stroke text (invisible); 4 = Fill text and add to path for clipping; 5 = Stroke text and add to path for clipping; 6 = Fill, then stroke text and add to path for clipping; 7 = Add text to path for clipping.
* @protected
* @since 4.9.008 (2010-04-03)
*/
protected $textrendermode = 0;
/**
* Text stroke width in doc units.
* @protected
* @since 4.9.008 (2010-04-03)
*/
protected $textstrokewidth = 0;
/**
* Current stroke color.
* @protected
* @since 4.9.008 (2010-04-03)
*/
protected $strokecolor;
/**
* Default unit of measure for document.
* @protected
* @since 5.0.000 (2010-04-22)
*/
protected $pdfunit = 'mm';
/**
* Boolean flag true when we are on TOC (Table Of Content) page.
* @protected
*/
protected $tocpage = false;
/**
* Boolean flag: if true convert vector images (SVG, EPS) to raster image using GD or ImageMagick library.
* @protected
* @since 5.0.000 (2010-04-26)
*/
protected $rasterize_vector_images = false;
/**
* Boolean flag: if true enables font subsetting by default.
* @protected
* @since 5.3.002 (2010-06-07)
*/
protected $font_subsetting = true;
/**
* Array of default graphic settings.
* @protected
* @since 5.5.008 (2010-07-02)
*/
protected $default_graphic_vars = array();
/**
* Array of XObjects.
* @protected
* @since 5.8.014 (2010-08-23)
*/
protected $xobjects = array();
/**
* Boolean value true when we are inside an XObject.
* @protected
* @since 5.8.017 (2010-08-24)
*/
protected $inxobj = false;
/**
* Current XObject ID.
* @protected
* @since 5.8.017 (2010-08-24)
*/
protected $xobjid = '';
/**
* Percentage of character stretching.
* @protected
* @since 5.9.000 (2010-09-29)
*/
protected $font_stretching = 100;
/**
* Increases or decreases the space between characters in a text by the specified amount (tracking).
* @protected
* @since 5.9.000 (2010-09-29)
*/
protected $font_spacing = 0;
/**
* Array of no-write regions.
* ('page' => page number or empy for current page, 'xt' => X top, 'yt' => Y top, 'xb' => X bottom, 'yb' => Y bottom, 'side' => page side 'L' = left or 'R' = right)
* @protected
* @since 5.9.003 (2010-10-14)
*/
protected $page_regions = array();
/**
* Boolean value true when page region check is active.
* @protected
*/
protected $check_page_regions = true;
/**
* Array of PDF layers data.
* @protected
* @since 5.9.102 (2011-07-13)
*/
protected $pdflayers = array();
/**
* A dictionary of names and corresponding destinations (Dests key on document Catalog).
* @protected
* @since 5.9.097 (2011-06-23)
*/
protected $dests = array();
/**
* Object ID for Named Destinations
* @protected
* @since 5.9.097 (2011-06-23)
*/
protected $n_dests;
/**
* Embedded Files Names
* @protected
* @since 5.9.204 (2013-01-23)
*/
protected $efnames = array();
/**
* Directory used for the last SVG image.
* @protected
* @since 5.0.000 (2010-05-05)
*/
protected $svgdir = '';
/**
* Deafult unit of measure for SVG.
* @protected
* @since 5.0.000 (2010-05-02)
*/
protected $svgunit = 'px';
/**
* Array of SVG gradients.
* @protected
* @since 5.0.000 (2010-05-02)
*/
protected $svggradients = array();
/**
* ID of last SVG gradient.
* @protected
* @since 5.0.000 (2010-05-02)
*/
protected $svggradientid = 0;
/**
* Boolean value true when in SVG defs group.
* @protected
* @since 5.0.000 (2010-05-02)
*/
protected $svgdefsmode = false;
/**
* Array of SVG defs.
* @protected
* @since 5.0.000 (2010-05-02)
*/
protected $svgdefs = array();
/**
* Boolean value true when in SVG clipPath tag.
* @protected
* @since 5.0.000 (2010-04-26)
*/
protected $svgclipmode = false;
/**
* Array of SVG clipPath commands.
* @protected
* @since 5.0.000 (2010-05-02)
*/
protected $svgclippaths = array();
/**
* Array of SVG clipPath tranformation matrix.
* @protected
* @since 5.8.022 (2010-08-31)
*/
protected $svgcliptm = array();
/**
* ID of last SVG clipPath.
* @protected
* @since 5.0.000 (2010-05-02)
*/
protected $svgclipid = 0;
/**
* SVG text.
* @protected
* @since 5.0.000 (2010-05-02)
*/
protected $svgtext = '';
/**
* SVG text properties.
* @protected
* @since 5.8.013 (2010-08-23)
*/
protected $svgtextmode = array();
/**
* Array of SVG properties.
* @protected
* @since 5.0.000 (2010-05-02)
*/
protected $svgstyles = array(array(
'alignment-baseline' => 'auto',
'baseline-shift' => 'baseline',
'clip' => 'auto',
'clip-path' => 'none',
'clip-rule' => 'nonzero',
'color' => 'black',
'color-interpolation' => 'sRGB',
'color-interpolation-filters' => 'linearRGB',
'color-profile' => 'auto',
'color-rendering' => 'auto',
'cursor' => 'auto',
'direction' => 'ltr',
'display' => 'inline',
'dominant-baseline' => 'auto',
'enable-background' => 'accumulate',
'fill' => 'black',
'fill-opacity' => 1,
'fill-rule' => 'nonzero',
'filter' => 'none',
'flood-color' => 'black',
'flood-opacity' => 1,
'font' => '',
'font-family' => 'helvetica',
'font-size' => 'medium',
'font-size-adjust' => 'none',
'font-stretch' => 'normal',
'font-style' => 'normal',
'font-variant' => 'normal',
'font-weight' => 'normal',
'glyph-orientation-horizontal' => '0deg',
'glyph-orientation-vertical' => 'auto',
'image-rendering' => 'auto',
'kerning' => 'auto',
'letter-spacing' => 'normal',
'lighting-color' => 'white',
'marker' => '',
'marker-end' => 'none',
'marker-mid' => 'none',
'marker-start' => 'none',
'mask' => 'none',
'opacity' => 1,
'overflow' => 'auto',
'pointer-events' => 'visiblePainted',
'shape-rendering' => 'auto',
'stop-color' => 'black',
'stop-opacity' => 1,
'stroke' => 'none',
'stroke-dasharray' => 'none',
'stroke-dashoffset' => 0,
'stroke-linecap' => 'butt',
'stroke-linejoin' => 'miter',
'stroke-miterlimit' => 4,
'stroke-opacity' => 1,
'stroke-width' => 1,
'text-anchor' => 'start',
'text-decoration' => 'none',
'text-rendering' => 'auto',
'unicode-bidi' => 'normal',
'visibility' => 'visible',
'word-spacing' => 'normal',
'writing-mode' => 'lr-tb',
'text-color' => 'black',
'transfmatrix' => array(1, 0, 0, 1, 0, 0)
));
/**
* If true force sRGB color profile for all document.
* @protected
* @since 5.9.121 (2011-09-28)
*/
protected $force_srgb = false;
/**
* If true set the document to PDF/A mode.
* @protected
* @since 5.9.121 (2011-09-27)
*/
protected $pdfa_mode = false;
/**
* Document creation date-time
* @protected
* @since 5.9.152 (2012-03-22)
*/
protected $doc_creation_timestamp;
/**
* Document modification date-time
* @protected
* @since 5.9.152 (2012-03-22)
*/
protected $doc_modification_timestamp;
/**
* Custom XMP data.
* @protected
* @since 5.9.128 (2011-10-06)
*/
protected $custom_xmp = '';
/**
* Overprint mode array.
* (Check the "Entries in a Graphics State Parameter Dictionary" on PDF 32000-1:2008).
* @protected
* @since 5.9.152 (2012-03-23)
*/
protected $overprint = array('OP' => false, 'op' => false, 'OPM' => 0);
/**
* Alpha mode array.
* (Check the "Entries in a Graphics State Parameter Dictionary" on PDF 32000-1:2008).
* @protected
* @since 5.9.152 (2012-03-23)
*/
protected $alpha = array('CA' => 1, 'ca' => 1, 'BM' => '/Normal', 'AIS' => false);
/**
* Define the page boundaries boxes to be set on document.
* @protected
* @since 5.9.152 (2012-03-23)
*/
protected $page_boxes = array('MediaBox', 'CropBox', 'BleedBox', 'TrimBox', 'ArtBox');
/**
* If true print TCPDF meta link.
* @protected
* @since 5.9.152 (2012-03-23)
*/
protected $tcpdflink = true;
/**
* Cache array for computed GD gamma values.
* @protected
* @since 5.9.1632 (2012-06-05)
*/
protected $gdgammacache = array();
//------------------------------------------------------------
// METHODS
//------------------------------------------------------------
/**
* This is the class constructor.
* It allows to set up the page format, the orientation and the measure unit used in all the methods (except for the font sizes).
* @param $orientation (string) page orientation. Possible values are (case insensitive):
P or Portrait (default)
L or Landscape
'' (empty string) for automatic orientation
* @param $unit (string) User measure unit. Possible values are:
pt: point
mm: millimeter (default)
cm: centimeter
in: inch
A point equals 1/72 of inch, that is to say about 0.35 mm (an inch being 2.54 cm). This is a very common unit in typography; font sizes are expressed in that unit.
* @param $format (mixed) The format used for pages. It can be either: one of the string values specified at getPageSizeFromFormat() or an array of parameters specified at setPageFormat().
* @param $unicode (boolean) TRUE means that the input text is unicode (default = true)
* @param $encoding (string) Charset encoding (used only when converting back html entities); default is UTF-8.
* @param $diskcache (boolean) If TRUE reduce the RAM memory usage by caching temporary data on filesystem (slower).
* @param $pdfa (boolean) If TRUE set the document to PDF/A mode.
* @public
* @see getPageSizeFromFormat(), setPageFormat()
*/
public function __construct($orientation='P', $unit='mm', $format='A4', $unicode=true, $encoding='UTF-8', $diskcache=false, $pdfa=false) {
/* Set internal character encoding to ASCII */
if (function_exists('mb_internal_encoding') AND mb_internal_encoding()) {
$this->internal_encoding = mb_internal_encoding();
mb_internal_encoding('ASCII');
}
$this->font_obj_ids = array();
$this->page_obj_id = array();
$this->form_obj_id = array();
// set pdf/a mode
$this->pdfa_mode = $pdfa;
$this->force_srgb = false;
// set disk caching
$this->diskcache = $diskcache ? true : false;
// set language direction
$this->rtl = false;
$this->tmprtl = false;
// some checks
$this->_dochecks();
// initialization of properties
$this->isunicode = $unicode;
$this->page = 0;
$this->transfmrk[0] = array();
$this->pagedim = array();
$this->n = 2;
$this->buffer = '';
$this->pages = array();
$this->state = 0;
$this->fonts = array();
$this->FontFiles = array();
$this->diffs = array();
$this->images = array();
$this->links = array();
$this->gradients = array();
$this->InFooter = false;
$this->lasth = 0;
$this->FontFamily = defined('PDF_FONT_NAME_MAIN')?PDF_FONT_NAME_MAIN:'helvetica';
$this->FontStyle = '';
$this->FontSizePt = 12;
$this->underline = false;
$this->overline = false;
$this->linethrough = false;
$this->DrawColor = '0 G';
$this->FillColor = '0 g';
$this->TextColor = '0 g';
$this->ColorFlag = false;
$this->pdflayers = array();
// encryption values
$this->encrypted = false;
$this->last_enc_key = '';
// standard Unicode fonts
$this->CoreFonts = array(
'courier'=>'Courier',
'courierB'=>'Courier-Bold',
'courierI'=>'Courier-Oblique',
'courierBI'=>'Courier-BoldOblique',
'helvetica'=>'Helvetica',
'helveticaB'=>'Helvetica-Bold',
'helveticaI'=>'Helvetica-Oblique',
'helveticaBI'=>'Helvetica-BoldOblique',
'times'=>'Times-Roman',
'timesB'=>'Times-Bold',
'timesI'=>'Times-Italic',
'timesBI'=>'Times-BoldItalic',
'symbol'=>'Symbol',
'zapfdingbats'=>'ZapfDingbats'
);
// set scale factor
$this->setPageUnit($unit);
// set page format and orientation
$this->setPageFormat($format, $orientation);
// page margins (1 cm)
$margin = 28.35 / $this->k;
$this->SetMargins($margin, $margin);
$this->clMargin = $this->lMargin;
$this->crMargin = $this->rMargin;
// internal cell padding
$cpadding = $margin / 10;
$this->setCellPaddings($cpadding, 0, $cpadding, 0);
// cell margins
$this->setCellMargins(0, 0, 0, 0);
// line width (0.2 mm)
$this->LineWidth = 0.57 / $this->k;
$this->linestyleWidth = sprintf('%F w', ($this->LineWidth * $this->k));
$this->linestyleCap = '0 J';
$this->linestyleJoin = '0 j';
$this->linestyleDash = '[] 0 d';
// automatic page break
$this->SetAutoPageBreak(true, (2 * $margin));
// full width display mode
$this->SetDisplayMode('fullwidth');
// compression
$this->SetCompression();
// set default PDF version number
$this->setPDFVersion();
$this->tcpdflink = true;
$this->encoding = $encoding;
$this->HREF = array();
$this->getFontsList();
$this->fgcolor = array('R' => 0, 'G' => 0, 'B' => 0);
$this->strokecolor = array('R' => 0, 'G' => 0, 'B' => 0);
$this->bgcolor = array('R' => 255, 'G' => 255, 'B' => 255);
$this->extgstates = array();
$this->setTextShadow();
// user's rights
$this->sign = false;
$this->ur['enabled'] = false;
$this->ur['document'] = '/FullSave';
$this->ur['annots'] = '/Create/Delete/Modify/Copy/Import/Export';
$this->ur['form'] = '/Add/Delete/FillIn/Import/Export/SubmitStandalone/SpawnTemplate';
$this->ur['signature'] = '/Modify';
$this->ur['ef'] = '/Create/Delete/Modify/Import';
$this->ur['formex'] = '';
$this->signature_appearance = array('page' => 1, 'rect' => '0 0 0 0');
$this->empty_signature_appearance = array();
// set default JPEG quality
$this->jpeg_quality = 75;
// initialize some settings
TCPDF_FONTS::utf8Bidi(array(''), '', false, $this->isunicode, $this->CurrentFont);
// set default font
$this->SetFont($this->FontFamily, $this->FontStyle, $this->FontSizePt);
// check if PCRE Unicode support is enabled
if ($this->isunicode AND (@preg_match('/\pL/u', 'a') == 1)) {
// PCRE unicode support is turned ON
// \p{Z} or \p{Separator}: any kind of Unicode whitespace or invisible separator.
// \p{Lo} or \p{Other_Letter}: a Unicode letter or ideograph that does not have lowercase and uppercase variants.
// \p{Lo} is needed because Chinese characters are packed next to each other without spaces in between.
//$this->setSpacesRE('/[^\S\P{Z}\P{Lo}\xa0]/u');
$this->setSpacesRE('/[^\S\P{Z}\xa0]/u');
} else {
// PCRE unicode support is turned OFF
$this->setSpacesRE('/[^\S\xa0]/');
}
$this->default_form_prop = array('lineWidth'=>1, 'borderStyle'=>'solid', 'fillColor'=>array(255, 255, 255), 'strokeColor'=>array(128, 128, 128));
// set file ID for trailer
$serformat = (is_array($format) ? serialize($format) : $format);
$this->file_id = md5(TCPDF_STATIC::getRandomSeed('TCPDF'.$orientation.$unit.$serformat.$encoding));
// set document creation and modification timestamp
$this->doc_creation_timestamp = time();
$this->doc_modification_timestamp = $this->doc_creation_timestamp;
// get default graphic vars
$this->default_graphic_vars = $this->getGraphicVars();
$this->header_xobj_autoreset = false;
$this->custom_xmp = '';
}
/**
* Default destructor.
* @public
* @since 1.53.0.TC016
*/
public function __destruct() {
// restore internal encoding
if (isset($this->internal_encoding) AND !empty($this->internal_encoding)) {
mb_internal_encoding($this->internal_encoding);
}
// unset all class variables
$this->_destroy(true);
}
/**
* Set the units of measure for the document.
* @param $unit (string) User measure unit. Possible values are:
pt: point
mm: millimeter (default)
cm: centimeter
in: inch
A point equals 1/72 of inch, that is to say about 0.35 mm (an inch being 2.54 cm). This is a very common unit in typography; font sizes are expressed in that unit.
* @public
* @since 3.0.015 (2008-06-06)
*/
public function setPageUnit($unit) {
$unit = strtolower($unit);
//Set scale factor
switch ($unit) {
// points
case 'px':
case 'pt': {
$this->k = 1;
break;
}
// millimeters
case 'mm': {
$this->k = $this->dpi / 25.4;
break;
}
// centimeters
case 'cm': {
$this->k = $this->dpi / 2.54;
break;
}
// inches
case 'in': {
$this->k = $this->dpi;
break;
}
// unsupported unit
default : {
$this->Error('Incorrect unit: '.$unit);
break;
}
}
$this->pdfunit = $unit;
if (isset($this->CurOrientation)) {
$this->setPageOrientation($this->CurOrientation);
}
}
/**
* Change the format of the current page
* @param $format (mixed) The format used for pages. It can be either: one of the string values specified at getPageSizeFromFormat() documentation or an array of two numners (width, height) or an array containing the following measures and options:
*
['format'] = page format name (one of the above);
*
['Rotate'] : The number of degrees by which the page shall be rotated clockwise when displayed or printed. The value shall be a multiple of 90.
*
['PZ'] : The page's preferred zoom (magnification) factor.
*
['MediaBox'] : the boundaries of the physical medium on which the page shall be displayed or printed:
*
['MediaBox']['llx'] : lower-left x coordinate in points
*
['MediaBox']['lly'] : lower-left y coordinate in points
*
['MediaBox']['urx'] : upper-right x coordinate in points
*
['MediaBox']['ury'] : upper-right y coordinate in points
*
['CropBox'] : the visible region of default user space:
*
['CropBox']['llx'] : lower-left x coordinate in points
*
['CropBox']['lly'] : lower-left y coordinate in points
*
['CropBox']['urx'] : upper-right x coordinate in points
*
['CropBox']['ury'] : upper-right y coordinate in points
*
['BleedBox'] : the region to which the contents of the page shall be clipped when output in a production environment:
*
['BleedBox']['llx'] : lower-left x coordinate in points
*
['BleedBox']['lly'] : lower-left y coordinate in points
*
['BleedBox']['urx'] : upper-right x coordinate in points
*
['BleedBox']['ury'] : upper-right y coordinate in points
*
['TrimBox'] : the intended dimensions of the finished page after trimming:
*
['TrimBox']['llx'] : lower-left x coordinate in points
*
['TrimBox']['lly'] : lower-left y coordinate in points
*
['TrimBox']['urx'] : upper-right x coordinate in points
*
['TrimBox']['ury'] : upper-right y coordinate in points
*
['ArtBox'] : the extent of the page's meaningful content:
*
['ArtBox']['llx'] : lower-left x coordinate in points
*
['ArtBox']['lly'] : lower-left y coordinate in points
*
['ArtBox']['urx'] : upper-right x coordinate in points
*
['ArtBox']['ury'] : upper-right y coordinate in points
*
['BoxColorInfo'] :specify the colours and other visual characteristics that should be used in displaying guidelines on the screen for each of the possible page boundaries other than the MediaBox:
*
['BoxColorInfo'][BOXTYPE]['C'] : an array of three numbers in the range 0-255, representing the components in the DeviceRGB colour space.
*
['BoxColorInfo'][BOXTYPE]['W'] : the guideline width in default user units
*
['BoxColorInfo'][BOXTYPE]['S'] : the guideline style: S = Solid; D = Dashed
*
['BoxColorInfo'][BOXTYPE]['D'] : dash array defining a pattern of dashes and gaps to be used in drawing dashed guidelines
*
['trans'] : the style and duration of the visual transition to use when moving from another page to the given page during a presentation
*
['trans']['Dur'] : The page's display duration (also called its advance timing): the maximum length of time, in seconds, that the page shall be displayed during presentations before the viewer application shall automatically advance to the next page.
['trans']['D'] : The duration of the transition effect, in seconds.
*
['trans']['Dm'] : (Split and Blinds transition styles only) The dimension in which the specified transition effect shall occur: H = Horizontal, V = Vertical. Default value: H.
*
['trans']['M'] : (Split, Box and Fly transition styles only) The direction of motion for the specified transition effect: I = Inward from the edges of the page, O = Outward from the center of the pageDefault value: I.
*
['trans']['Di'] : (Wipe, Glitter, Fly, Cover, Uncover and Push transition styles only) The direction in which the specified transition effect shall moves, expressed in degrees counterclockwise starting from a left-to-right direction. If the value is a number, it shall be one of: 0 = Left to right, 90 = Bottom to top (Wipe only), 180 = Right to left (Wipe only), 270 = Top to bottom, 315 = Top-left to bottom-right (Glitter only). If the value is a name, it shall be None, which is relevant only for the Fly transition when the value of SS is not 1.0. Default value: 0.
*
['trans']['SS'] : (Fly transition style only) The starting or ending scale at which the changes shall be drawn. If M specifies an inward transition, the scale of the changes drawn shall progress from SS to 1.0 over the course of the transition. If M specifies an outward transition, the scale of the changes drawn shall progress from 1.0 to SS over the course of the transition. Default: 1.0.
*
['trans']['B'] : (Fly transition style only) If true, the area that shall be flown in is rectangular and opaque. Default: false.
*
* @param $orientation (string) page orientation. Possible values are (case insensitive):
*
P or Portrait (default)
*
L or Landscape
*
'' (empty string) for automatic orientation
*
* @protected
* @since 3.0.015 (2008-06-06)
* @see getPageSizeFromFormat()
*/
protected function setPageFormat($format, $orientation='P') {
if (!empty($format) AND isset($this->pagedim[$this->page])) {
// remove inherited values
unset($this->pagedim[$this->page]);
}
if (is_string($format)) {
// get page measures from format name
$pf = TCPDF_STATIC::getPageSizeFromFormat($format);
$this->fwPt = $pf[0];
$this->fhPt = $pf[1];
} else {
// the boundaries of the physical medium on which the page shall be displayed or printed
if (isset($format['MediaBox'])) {
$this->pagedim = TCPDF_STATIC::setPageBoxes($this->page, 'MediaBox', $format['MediaBox']['llx'], $format['MediaBox']['lly'], $format['MediaBox']['urx'], $format['MediaBox']['ury'], false, $this->k, $this->pagedim);
$this->fwPt = (($format['MediaBox']['urx'] - $format['MediaBox']['llx']) * $this->k);
$this->fhPt = (($format['MediaBox']['ury'] - $format['MediaBox']['lly']) * $this->k);
} else {
if (isset($format[0]) AND is_numeric($format[0]) AND isset($format[1]) AND is_numeric($format[1])) {
$pf = array(($format[0] * $this->k), ($format[1] * $this->k));
} else {
if (!isset($format['format'])) {
// default value
$format['format'] = 'A4';
}
$pf = TCPDF_STATIC::getPageSizeFromFormat($format['format']);
}
$this->fwPt = $pf[0];
$this->fhPt = $pf[1];
$this->pagedim = TCPDF_STATIC::setPageBoxes($this->page, 'MediaBox', 0, 0, $this->fwPt, $this->fhPt, true, $this->k, $this->pagedim);
}
// the visible region of default user space
if (isset($format['CropBox'])) {
$this->pagedim = TCPDF_STATIC::setPageBoxes($this->page, 'CropBox', $format['CropBox']['llx'], $format['CropBox']['lly'], $format['CropBox']['urx'], $format['CropBox']['ury'], false, $this->k, $this->pagedim);
}
// the region to which the contents of the page shall be clipped when output in a production environment
if (isset($format['BleedBox'])) {
$this->pagedim = TCPDF_STATIC::setPageBoxes($this->page, 'BleedBox', $format['BleedBox']['llx'], $format['BleedBox']['lly'], $format['BleedBox']['urx'], $format['BleedBox']['ury'], false, $this->k, $this->pagedim);
}
// the intended dimensions of the finished page after trimming
if (isset($format['TrimBox'])) {
$this->pagedim = TCPDF_STATIC::setPageBoxes($this->page, 'TrimBox', $format['TrimBox']['llx'], $format['TrimBox']['lly'], $format['TrimBox']['urx'], $format['TrimBox']['ury'], false, $this->k, $this->pagedim);
}
// the page's meaningful content (including potential white space)
if (isset($format['ArtBox'])) {
$this->pagedim = TCPDF_STATIC::setPageBoxes($this->page, 'ArtBox', $format['ArtBox']['llx'], $format['ArtBox']['lly'], $format['ArtBox']['urx'], $format['ArtBox']['ury'], false, $this->k, $this->pagedim);
}
// specify the colours and other visual characteristics that should be used in displaying guidelines on the screen for the various page boundaries
if (isset($format['BoxColorInfo'])) {
$this->pagedim[$this->page]['BoxColorInfo'] = $format['BoxColorInfo'];
}
if (isset($format['Rotate']) AND (($format['Rotate'] % 90) == 0)) {
// The number of degrees by which the page shall be rotated clockwise when displayed or printed. The value shall be a multiple of 90.
$this->pagedim[$this->page]['Rotate'] = intval($format['Rotate']);
}
if (isset($format['PZ'])) {
// The page's preferred zoom (magnification) factor
$this->pagedim[$this->page]['PZ'] = floatval($format['PZ']);
}
if (isset($format['trans'])) {
// The style and duration of the visual transition to use when moving from another page to the given page during a presentation
if (isset($format['trans']['Dur'])) {
// The page's display duration
$this->pagedim[$this->page]['trans']['Dur'] = floatval($format['trans']['Dur']);
}
$stansition_styles = array('Split', 'Blinds', 'Box', 'Wipe', 'Dissolve', 'Glitter', 'R', 'Fly', 'Push', 'Cover', 'Uncover', 'Fade');
if (isset($format['trans']['S']) AND in_array($format['trans']['S'], $stansition_styles)) {
// The transition style that shall be used when moving to this page from another during a presentation
$this->pagedim[$this->page]['trans']['S'] = $format['trans']['S'];
$valid_effect = array('Split', 'Blinds');
$valid_vals = array('H', 'V');
if (isset($format['trans']['Dm']) AND in_array($format['trans']['S'], $valid_effect) AND in_array($format['trans']['Dm'], $valid_vals)) {
$this->pagedim[$this->page]['trans']['Dm'] = $format['trans']['Dm'];
}
$valid_effect = array('Split', 'Box', 'Fly');
$valid_vals = array('I', 'O');
if (isset($format['trans']['M']) AND in_array($format['trans']['S'], $valid_effect) AND in_array($format['trans']['M'], $valid_vals)) {
$this->pagedim[$this->page]['trans']['M'] = $format['trans']['M'];
}
$valid_effect = array('Wipe', 'Glitter', 'Fly', 'Cover', 'Uncover', 'Push');
if (isset($format['trans']['Di']) AND in_array($format['trans']['S'], $valid_effect)) {
if (((($format['trans']['Di'] == 90) OR ($format['trans']['Di'] == 180)) AND ($format['trans']['S'] == 'Wipe'))
OR (($format['trans']['Di'] == 315) AND ($format['trans']['S'] == 'Glitter'))
OR (($format['trans']['Di'] == 0) OR ($format['trans']['Di'] == 270))) {
$this->pagedim[$this->page]['trans']['Di'] = intval($format['trans']['Di']);
}
}
if (isset($format['trans']['SS']) AND ($format['trans']['S'] == 'Fly')) {
$this->pagedim[$this->page]['trans']['SS'] = floatval($format['trans']['SS']);
}
if (isset($format['trans']['B']) AND ($format['trans']['B'] === true) AND ($format['trans']['S'] == 'Fly')) {
$this->pagedim[$this->page]['trans']['B'] = 'true';
}
} else {
$this->pagedim[$this->page]['trans']['S'] = 'R';
}
if (isset($format['trans']['D'])) {
// The duration of the transition effect, in seconds
$this->pagedim[$this->page]['trans']['D'] = floatval($format['trans']['D']);
} else {
$this->pagedim[$this->page]['trans']['D'] = 1;
}
}
}
$this->setPageOrientation($orientation);
}
/**
* Set page orientation.
* @param $orientation (string) page orientation. Possible values are (case insensitive):
P or Portrait (default)
L or Landscape
'' (empty string) for automatic orientation
* @param $autopagebreak (boolean) Boolean indicating if auto-page-break mode should be on or off.
* @param $bottommargin (float) bottom margin of the page.
* @public
* @since 3.0.015 (2008-06-06)
*/
public function setPageOrientation($orientation, $autopagebreak='', $bottommargin='') {
if (!isset($this->pagedim[$this->page]['MediaBox'])) {
// the boundaries of the physical medium on which the page shall be displayed or printed
$this->pagedim = TCPDF_STATIC::setPageBoxes($this->page, 'MediaBox', 0, 0, $this->fwPt, $this->fhPt, true, $this->k, $this->pagedim);
}
if (!isset($this->pagedim[$this->page]['CropBox'])) {
// the visible region of default user space
$this->pagedim = TCPDF_STATIC::setPageBoxes($this->page, 'CropBox', $this->pagedim[$this->page]['MediaBox']['llx'], $this->pagedim[$this->page]['MediaBox']['lly'], $this->pagedim[$this->page]['MediaBox']['urx'], $this->pagedim[$this->page]['MediaBox']['ury'], true, $this->k, $this->pagedim);
}
if (!isset($this->pagedim[$this->page]['BleedBox'])) {
// the region to which the contents of the page shall be clipped when output in a production environment
$this->pagedim = TCPDF_STATIC::setPageBoxes($this->page, 'BleedBox', $this->pagedim[$this->page]['CropBox']['llx'], $this->pagedim[$this->page]['CropBox']['lly'], $this->pagedim[$this->page]['CropBox']['urx'], $this->pagedim[$this->page]['CropBox']['ury'], true, $this->k, $this->pagedim);
}
if (!isset($this->pagedim[$this->page]['TrimBox'])) {
// the intended dimensions of the finished page after trimming
$this->pagedim = TCPDF_STATIC::setPageBoxes($this->page, 'TrimBox', $this->pagedim[$this->page]['CropBox']['llx'], $this->pagedim[$this->page]['CropBox']['lly'], $this->pagedim[$this->page]['CropBox']['urx'], $this->pagedim[$this->page]['CropBox']['ury'], true, $this->k, $this->pagedim);
}
if (!isset($this->pagedim[$this->page]['ArtBox'])) {
// the page's meaningful content (including potential white space)
$this->pagedim = TCPDF_STATIC::setPageBoxes($this->page, 'ArtBox', $this->pagedim[$this->page]['CropBox']['llx'], $this->pagedim[$this->page]['CropBox']['lly'], $this->pagedim[$this->page]['CropBox']['urx'], $this->pagedim[$this->page]['CropBox']['ury'], true, $this->k, $this->pagedim);
}
if (!isset($this->pagedim[$this->page]['Rotate'])) {
// The number of degrees by which the page shall be rotated clockwise when displayed or printed. The value shall be a multiple of 90.
$this->pagedim[$this->page]['Rotate'] = 0;
}
if (!isset($this->pagedim[$this->page]['PZ'])) {
// The page's preferred zoom (magnification) factor
$this->pagedim[$this->page]['PZ'] = 1;
}
if ($this->fwPt > $this->fhPt) {
// landscape
$default_orientation = 'L';
} else {
// portrait
$default_orientation = 'P';
}
$valid_orientations = array('P', 'L');
if (empty($orientation)) {
$orientation = $default_orientation;
} else {
$orientation = strtoupper($orientation{0});
}
if (in_array($orientation, $valid_orientations) AND ($orientation != $default_orientation)) {
$this->CurOrientation = $orientation;
$this->wPt = $this->fhPt;
$this->hPt = $this->fwPt;
} else {
$this->CurOrientation = $default_orientation;
$this->wPt = $this->fwPt;
$this->hPt = $this->fhPt;
}
if ((abs($this->pagedim[$this->page]['MediaBox']['urx'] - $this->hPt) < $this->feps) AND (abs($this->pagedim[$this->page]['MediaBox']['ury'] - $this->wPt) < $this->feps)){
// swap X and Y coordinates (change page orientation)
$this->pagedim = TCPDF_STATIC::swapPageBoxCoordinates($this->page, $this->pagedim);
}
$this->w = ($this->wPt / $this->k);
$this->h = ($this->hPt / $this->k);
if (TCPDF_STATIC::empty_string($autopagebreak)) {
if (isset($this->AutoPageBreak)) {
$autopagebreak = $this->AutoPageBreak;
} else {
$autopagebreak = true;
}
}
if (TCPDF_STATIC::empty_string($bottommargin)) {
if (isset($this->bMargin)) {
$bottommargin = $this->bMargin;
} else {
// default value = 2 cm
$bottommargin = 2 * 28.35 / $this->k;
}
}
$this->SetAutoPageBreak($autopagebreak, $bottommargin);
// store page dimensions
$this->pagedim[$this->page]['w'] = $this->wPt;
$this->pagedim[$this->page]['h'] = $this->hPt;
$this->pagedim[$this->page]['wk'] = $this->w;
$this->pagedim[$this->page]['hk'] = $this->h;
$this->pagedim[$this->page]['tm'] = $this->tMargin;
$this->pagedim[$this->page]['bm'] = $bottommargin;
$this->pagedim[$this->page]['lm'] = $this->lMargin;
$this->pagedim[$this->page]['rm'] = $this->rMargin;
$this->pagedim[$this->page]['pb'] = $autopagebreak;
$this->pagedim[$this->page]['or'] = $this->CurOrientation;
$this->pagedim[$this->page]['olm'] = $this->original_lMargin;
$this->pagedim[$this->page]['orm'] = $this->original_rMargin;
}
/**
* Set regular expression to detect withespaces or word separators.
* The pattern delimiter must be the forward-slash character "/".
* Some example patterns are:
*
* Non-Unicode or missing PCRE unicode support: "/[^\S\xa0]/"
* Unicode and PCRE unicode support: "/[^\S\P{Z}\xa0]/u"
* Unicode and PCRE unicode support in Chinese mode: "/[^\S\P{Z}\P{Lo}\xa0]/u"
* if PCRE unicode support is turned ON ("\P" is the negate class of "\p"):
* "\p{Z}" or "\p{Separator}": any kind of Unicode whitespace or invisible separator.
* "\p{Lo}" or "\p{Other_Letter}": a Unicode letter or ideograph that does not have lowercase and uppercase variants.
* "\p{Lo}" is needed for Chinese characters because are packed next to each other without spaces in between.
*
* @param $re (string) regular expression (leave empty for default).
* @public
* @since 4.6.016 (2009-06-15)
*/
public function setSpacesRE($re='/[^\S\xa0]/') {
$this->re_spaces = $re;
$re_parts = explode('/', $re);
// get pattern parts
$this->re_space = array();
if (isset($re_parts[1]) AND !empty($re_parts[1])) {
$this->re_space['p'] = $re_parts[1];
} else {
$this->re_space['p'] = '[\s]';
}
// set pattern modifiers
if (isset($re_parts[2]) AND !empty($re_parts[2])) {
$this->re_space['m'] = $re_parts[2];
} else {
$this->re_space['m'] = '';
}
}
/**
* Enable or disable Right-To-Left language mode
* @param $enable (Boolean) if true enable Right-To-Left language mode.
* @param $resetx (Boolean) if true reset the X position on direction change.
* @public
* @since 2.0.000 (2008-01-03)
*/
public function setRTL($enable, $resetx=true) {
$enable = $enable ? true : false;
$resetx = ($resetx AND ($enable != $this->rtl));
$this->rtl = $enable;
$this->tmprtl = false;
if ($resetx) {
$this->Ln(0);
}
}
/**
* Return the RTL status
* @return boolean
* @public
* @since 4.0.012 (2008-07-24)
*/
public function getRTL() {
return $this->rtl;
}
/**
* Force temporary RTL language direction
* @param $mode (mixed) can be false, 'L' for LTR or 'R' for RTL
* @public
* @since 2.1.000 (2008-01-09)
*/
public function setTempRTL($mode) {
$newmode = false;
switch (strtoupper($mode)) {
case 'LTR':
case 'L': {
if ($this->rtl) {
$newmode = 'L';
}
break;
}
case 'RTL':
case 'R': {
if (!$this->rtl) {
$newmode = 'R';
}
break;
}
case false:
default: {
$newmode = false;
break;
}
}
$this->tmprtl = $newmode;
}
/**
* Return the current temporary RTL status
* @return boolean
* @public
* @since 4.8.014 (2009-11-04)
*/
public function isRTLTextDir() {
return ($this->rtl OR ($this->tmprtl == 'R'));
}
/**
* Set the last cell height.
* @param $h (float) cell height.
* @author Nicola Asuni
* @public
* @since 1.53.0.TC034
*/
public function setLastH($h) {
$this->lasth = $h;
}
/**
* Reset the last cell height.
* @public
* @since 5.9.000 (2010-10-03)
*/
public function resetLastH() {
$this->lasth = ($this->FontSize * $this->cell_height_ratio) + $this->cell_padding['T'] + $this->cell_padding['B'];
}
/**
* Get the last cell height.
* @return last cell height
* @public
* @since 4.0.017 (2008-08-05)
*/
public function getLastH() {
return $this->lasth;
}
/**
* Set the adjusting factor to convert pixels to user units.
* @param $scale (float) adjusting factor to convert pixels to user units.
* @author Nicola Asuni
* @public
* @since 1.5.2
*/
public function setImageScale($scale) {
$this->imgscale = $scale;
}
/**
* Returns the adjusting factor to convert pixels to user units.
* @return float adjusting factor to convert pixels to user units.
* @author Nicola Asuni
* @public
* @since 1.5.2
*/
public function getImageScale() {
return $this->imgscale;
}
/**
* Returns an array of page dimensions:
*
$this->pagedim[$this->page]['w'] = page width in points
$this->pagedim[$this->page]['h'] = height in points
$this->pagedim[$this->page]['wk'] = page width in user units
$this->pagedim[$this->page]['hk'] = page height in user units
$this->pagedim[$this->page]['tm'] = top margin
$this->pagedim[$this->page]['bm'] = bottom margin
$this->pagedim[$this->page]['lm'] = left margin
$this->pagedim[$this->page]['rm'] = right margin
$this->pagedim[$this->page]['pb'] = auto page break
$this->pagedim[$this->page]['olm'] = original left margin
$this->pagedim[$this->page]['orm'] = original right margin
$this->pagedim[$this->page]['Rotate'] = The number of degrees by which the page shall be rotated clockwise when displayed or printed. The value shall be a multiple of 90.
$this->pagedim[$this->page]['PZ'] = The page's preferred zoom (magnification) factor.
$this->pagedim[$this->page]['trans'] : the style and duration of the visual transition to use when moving from another page to the given page during a presentation
$this->pagedim[$this->page]['trans']['Dur'] = The page's display duration (also called its advance timing): the maximum length of time, in seconds, that the page shall be displayed during presentations before the viewer application shall automatically advance to the next page.
$this->pagedim[$this->page]['trans']['D'] = The duration of the transition effect, in seconds.
$this->pagedim[$this->page]['trans']['Dm'] = (Split and Blinds transition styles only) The dimension in which the specified transition effect shall occur: H = Horizontal, V = Vertical. Default value: H.
$this->pagedim[$this->page]['trans']['M'] = (Split, Box and Fly transition styles only) The direction of motion for the specified transition effect: I = Inward from the edges of the page, O = Outward from the center of the pageDefault value: I.
$this->pagedim[$this->page]['trans']['Di'] = (Wipe, Glitter, Fly, Cover, Uncover and Push transition styles only) The direction in which the specified transition effect shall moves, expressed in degrees counterclockwise starting from a left-to-right direction. If the value is a number, it shall be one of: 0 = Left to right, 90 = Bottom to top (Wipe only), 180 = Right to left (Wipe only), 270 = Top to bottom, 315 = Top-left to bottom-right (Glitter only). If the value is a name, it shall be None, which is relevant only for the Fly transition when the value of SS is not 1.0. Default value: 0.
$this->pagedim[$this->page]['trans']['SS'] = (Fly transition style only) The starting or ending scale at which the changes shall be drawn. If M specifies an inward transition, the scale of the changes drawn shall progress from SS to 1.0 over the course of the transition. If M specifies an outward transition, the scale of the changes drawn shall progress from 1.0 to SS over the course of the transition. Default: 1.0.
$this->pagedim[$this->page]['trans']['B'] = (Fly transition style only) If true, the area that shall be flown in is rectangular and opaque. Default: false.
$this->pagedim[$this->page]['MediaBox'] : the boundaries of the physical medium on which the page shall be displayed or printed
$this->pagedim[$this->page]['MediaBox']['llx'] = lower-left x coordinate in points
$this->pagedim[$this->page]['MediaBox']['lly'] = lower-left y coordinate in points
$this->pagedim[$this->page]['MediaBox']['urx'] = upper-right x coordinate in points
$this->pagedim[$this->page]['MediaBox']['ury'] = upper-right y coordinate in points
$this->pagedim[$this->page]['CropBox'] : the visible region of default user space
$this->pagedim[$this->page]['CropBox']['llx'] = lower-left x coordinate in points
$this->pagedim[$this->page]['CropBox']['lly'] = lower-left y coordinate in points
$this->pagedim[$this->page]['CropBox']['urx'] = upper-right x coordinate in points
$this->pagedim[$this->page]['CropBox']['ury'] = upper-right y coordinate in points
$this->pagedim[$this->page]['BleedBox'] : the region to which the contents of the page shall be clipped when output in a production environment
$this->pagedim[$this->page]['BleedBox']['llx'] = lower-left x coordinate in points
$this->pagedim[$this->page]['BleedBox']['lly'] = lower-left y coordinate in points
$this->pagedim[$this->page]['BleedBox']['urx'] = upper-right x coordinate in points
$this->pagedim[$this->page]['BleedBox']['ury'] = upper-right y coordinate in points
$this->pagedim[$this->page]['TrimBox'] : the intended dimensions of the finished page after trimming
$this->pagedim[$this->page]['TrimBox']['llx'] = lower-left x coordinate in points
$this->pagedim[$this->page]['TrimBox']['lly'] = lower-left y coordinate in points
$this->pagedim[$this->page]['TrimBox']['urx'] = upper-right x coordinate in points
$this->pagedim[$this->page]['TrimBox']['ury'] = upper-right y coordinate in points
$this->pagedim[$this->page]['ArtBox'] : the extent of the page's meaningful content
$this->pagedim[$this->page]['ArtBox']['llx'] = lower-left x coordinate in points
$this->pagedim[$this->page]['ArtBox']['lly'] = lower-left y coordinate in points
$this->pagedim[$this->page]['ArtBox']['urx'] = upper-right x coordinate in points
$this->pagedim[$this->page]['ArtBox']['ury'] = upper-right y coordinate in points
* @param $pagenum (int) page number (empty = current page)
* @return array of page dimensions.
* @author Nicola Asuni
* @public
* @since 4.5.027 (2009-03-16)
*/
public function getPageDimensions($pagenum='') {
if (empty($pagenum)) {
$pagenum = $this->page;
}
return $this->pagedim[$pagenum];
}
/**
* Returns the page width in units.
* @param $pagenum (int) page number (empty = current page)
* @return int page width.
* @author Nicola Asuni
* @public
* @since 1.5.2
* @see getPageDimensions()
*/
public function getPageWidth($pagenum='') {
if (empty($pagenum)) {
return $this->w;
}
return $this->pagedim[$pagenum]['w'];
}
/**
* Returns the page height in units.
* @param $pagenum (int) page number (empty = current page)
* @return int page height.
* @author Nicola Asuni
* @public
* @since 1.5.2
* @see getPageDimensions()
*/
public function getPageHeight($pagenum='') {
if (empty($pagenum)) {
return $this->h;
}
return $this->pagedim[$pagenum]['h'];
}
/**
* Returns the page break margin.
* @param $pagenum (int) page number (empty = current page)
* @return int page break margin.
* @author Nicola Asuni
* @public
* @since 1.5.2
* @see getPageDimensions()
*/
public function getBreakMargin($pagenum='') {
if (empty($pagenum)) {
return $this->bMargin;
}
return $this->pagedim[$pagenum]['bm'];
}
/**
* Returns the scale factor (number of points in user unit).
* @return int scale factor.
* @author Nicola Asuni
* @public
* @since 1.5.2
*/
public function getScaleFactor() {
return $this->k;
}
/**
* Defines the left, top and right margins.
* @param $left (float) Left margin.
* @param $top (float) Top margin.
* @param $right (float) Right margin. Default value is the left one.
* @param $keepmargins (boolean) if true overwrites the default page margins
* @public
* @since 1.0
* @see SetLeftMargin(), SetTopMargin(), SetRightMargin(), SetAutoPageBreak()
*/
public function SetMargins($left, $top, $right=-1, $keepmargins=false) {
//Set left, top and right margins
$this->lMargin = $left;
$this->tMargin = $top;
if ($right == -1) {
$right = $left;
}
$this->rMargin = $right;
if ($keepmargins) {
// overwrite original values
$this->original_lMargin = $this->lMargin;
$this->original_rMargin = $this->rMargin;
}
}
/**
* Defines the left margin. The method can be called before creating the first page. If the current abscissa gets out of page, it is brought back to the margin.
* @param $margin (float) The margin.
* @public
* @since 1.4
* @see SetTopMargin(), SetRightMargin(), SetAutoPageBreak(), SetMargins()
*/
public function SetLeftMargin($margin) {
//Set left margin
$this->lMargin = $margin;
if (($this->page > 0) AND ($this->x < $margin)) {
$this->x = $margin;
}
}
/**
* Defines the top margin. The method can be called before creating the first page.
* @param $margin (float) The margin.
* @public
* @since 1.5
* @see SetLeftMargin(), SetRightMargin(), SetAutoPageBreak(), SetMargins()
*/
public function SetTopMargin($margin) {
//Set top margin
$this->tMargin = $margin;
if (($this->page > 0) AND ($this->y < $margin)) {
$this->y = $margin;
}
}
/**
* Defines the right margin. The method can be called before creating the first page.
* @param $margin (float) The margin.
* @public
* @since 1.5
* @see SetLeftMargin(), SetTopMargin(), SetAutoPageBreak(), SetMargins()
*/
public function SetRightMargin($margin) {
$this->rMargin = $margin;
if (($this->page > 0) AND ($this->x > ($this->w - $margin))) {
$this->x = $this->w - $margin;
}
}
/**
* Set the same internal Cell padding for top, right, bottom, left-
* @param $pad (float) internal padding.
* @public
* @since 2.1.000 (2008-01-09)
* @see getCellPaddings(), setCellPaddings()
*/
public function SetCellPadding($pad) {
if ($pad >= 0) {
$this->cell_padding['L'] = $pad;
$this->cell_padding['T'] = $pad;
$this->cell_padding['R'] = $pad;
$this->cell_padding['B'] = $pad;
}
}
/**
* Set the internal Cell paddings.
* @param $left (float) left padding
* @param $top (float) top padding
* @param $right (float) right padding
* @param $bottom (float) bottom padding
* @public
* @since 5.9.000 (2010-10-03)
* @see getCellPaddings(), SetCellPadding()
*/
public function setCellPaddings($left='', $top='', $right='', $bottom='') {
if (($left !== '') AND ($left >= 0)) {
$this->cell_padding['L'] = $left;
}
if (($top !== '') AND ($top >= 0)) {
$this->cell_padding['T'] = $top;
}
if (($right !== '') AND ($right >= 0)) {
$this->cell_padding['R'] = $right;
}
if (($bottom !== '') AND ($bottom >= 0)) {
$this->cell_padding['B'] = $bottom;
}
}
/**
* Get the internal Cell padding array.
* @return array of padding values
* @public
* @since 5.9.000 (2010-10-03)
* @see setCellPaddings(), SetCellPadding()
*/
public function getCellPaddings() {
return $this->cell_padding;
}
/**
* Set the internal Cell margins.
* @param $left (float) left margin
* @param $top (float) top margin
* @param $right (float) right margin
* @param $bottom (float) bottom margin
* @public
* @since 5.9.000 (2010-10-03)
* @see getCellMargins()
*/
public function setCellMargins($left='', $top='', $right='', $bottom='') {
if (($left !== '') AND ($left >= 0)) {
$this->cell_margin['L'] = $left;
}
if (($top !== '') AND ($top >= 0)) {
$this->cell_margin['T'] = $top;
}
if (($right !== '') AND ($right >= 0)) {
$this->cell_margin['R'] = $right;
}
if (($bottom !== '') AND ($bottom >= 0)) {
$this->cell_margin['B'] = $bottom;
}
}
/**
* Get the internal Cell margin array.
* @return array of margin values
* @public
* @since 5.9.000 (2010-10-03)
* @see setCellMargins()
*/
public function getCellMargins() {
return $this->cell_margin;
}
/**
* Adjust the internal Cell padding array to take account of the line width.
* @param $brd (mixed) Indicates if borders must be drawn around the cell. The value can be a number:
0: no border (default)
1: frame
or a string containing some or all of the following characters (in any order):
L: left
T: top
R: right
B: bottom
or an array of line styles for each border group - for example: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
* @return array of adjustments
* @public
* @since 5.9.000 (2010-10-03)
*/
protected function adjustCellPadding($brd=0) {
if (empty($brd)) {
return;
}
if (is_string($brd)) {
// convert string to array
$slen = strlen($brd);
$newbrd = array();
for ($i = 0; $i < $slen; ++$i) {
$newbrd[$brd[$i]] = true;
}
$brd = $newbrd;
} elseif (($brd === 1) OR ($brd === true) OR (is_numeric($brd) AND (intval($brd) > 0))) {
$brd = array('LRTB' => true);
}
if (!is_array($brd)) {
return;
}
// store current cell padding
$cp = $this->cell_padding;
// select border mode
if (isset($brd['mode'])) {
$mode = $brd['mode'];
unset($brd['mode']);
} else {
$mode = 'normal';
}
// process borders
foreach ($brd as $border => $style) {
$line_width = $this->LineWidth;
if (is_array($style) AND isset($style['width'])) {
// get border width
$line_width = $style['width'];
}
$adj = 0; // line width inside the cell
switch ($mode) {
case 'ext': {
$adj = 0;
break;
}
case 'int': {
$adj = $line_width;
break;
}
case 'normal':
default: {
$adj = ($line_width / 2);
break;
}
}
// correct internal cell padding if required to avoid overlap between text and lines
if ((strpos($border,'T') !== false) AND ($this->cell_padding['T'] < $adj)) {
$this->cell_padding['T'] = $adj;
}
if ((strpos($border,'R') !== false) AND ($this->cell_padding['R'] < $adj)) {
$this->cell_padding['R'] = $adj;
}
if ((strpos($border,'B') !== false) AND ($this->cell_padding['B'] < $adj)) {
$this->cell_padding['B'] = $adj;
}
if ((strpos($border,'L') !== false) AND ($this->cell_padding['L'] < $adj)) {
$this->cell_padding['L'] = $adj;
}
}
return array('T' => ($this->cell_padding['T'] - $cp['T']), 'R' => ($this->cell_padding['R'] - $cp['R']), 'B' => ($this->cell_padding['B'] - $cp['B']), 'L' => ($this->cell_padding['L'] - $cp['L']));
}
/**
* Enables or disables the automatic page breaking mode. When enabling, the second parameter is the distance from the bottom of the page that defines the triggering limit. By default, the mode is on and the margin is 2 cm.
* @param $auto (boolean) Boolean indicating if mode should be on or off.
* @param $margin (float) Distance from the bottom of the page.
* @public
* @since 1.0
* @see Cell(), MultiCell(), AcceptPageBreak()
*/
public function SetAutoPageBreak($auto, $margin=0) {
$this->AutoPageBreak = $auto ? true : false;
$this->bMargin = $margin;
$this->PageBreakTrigger = $this->h - $margin;
}
/**
* Return the auto-page-break mode (true or false).
* @return boolean auto-page-break mode
* @public
* @since 5.9.088
*/
public function getAutoPageBreak() {
return $this->AutoPageBreak;
}
/**
* Defines the way the document is to be displayed by the viewer.
* @param $zoom (mixed) The zoom to use. It can be one of the following string values or a number indicating the zooming factor to use.
fullpage: displays the entire page on screen
fullwidth: uses maximum width of window
real: uses real size (equivalent to 100% zoom)
default: uses viewer default mode
* @param $layout (string) The page layout. Possible values are:
SinglePage Display one page at a time
OneColumn Display the pages in one column
TwoColumnLeft Display the pages in two columns, with odd-numbered pages on the left
TwoColumnRight Display the pages in two columns, with odd-numbered pages on the right
TwoPageLeft (PDF 1.5) Display the pages two at a time, with odd-numbered pages on the left
TwoPageRight (PDF 1.5) Display the pages two at a time, with odd-numbered pages on the right
* @param $mode (string) A name object specifying how the document should be displayed when opened:
UseNone Neither document outline nor thumbnail images visible
UseOutlines Document outline visible
UseThumbs Thumbnail images visible
FullScreen Full-screen mode, with no menu bar, window controls, or any other window visible
UseOC (PDF 1.5) Optional content group panel visible
* @public
* @since 1.2
*/
public function SetDisplayMode($zoom, $layout='SinglePage', $mode='UseNone') {
if (($zoom == 'fullpage') OR ($zoom == 'fullwidth') OR ($zoom == 'real') OR ($zoom == 'default') OR (!is_string($zoom))) {
$this->ZoomMode = $zoom;
} else {
$this->Error('Incorrect zoom display mode: '.$zoom);
}
$this->LayoutMode = TCPDF_STATIC::getPageLayoutMode($layout);
$this->PageMode = TCPDF_STATIC::getPageMode($mode);
}
/**
* Activates or deactivates page compression. When activated, the internal representation of each page is compressed, which leads to a compression ratio of about 2 for the resulting document. Compression is on by default.
* Note: the Zlib extension is required for this feature. If not present, compression will be turned off.
* @param $compress (boolean) Boolean indicating if compression must be enabled.
* @public
* @since 1.4
*/
public function SetCompression($compress=true) {
if (function_exists('gzcompress')) {
$this->compress = $compress ? true : false;
} else {
$this->compress = false;
}
}
/**
* Set flag to force sRGB_IEC61966-2.1 black scaled ICC color profile for the whole document.
* @param $mode (boolean) If true force sRGB output intent.
* @public
* @since 5.9.121 (2011-09-28)
*/
public function setSRGBmode($mode=false) {
$this->force_srgb = $mode ? true : false;
}
/**
* Turn on/off Unicode mode for document information dictionary (meta tags).
* This has effect only when unicode mode is set to false.
* @param $unicode (boolean) if true set the meta information in Unicode
* @since 5.9.027 (2010-12-01)
* @public
*/
public function SetDocInfoUnicode($unicode=true) {
$this->docinfounicode = $unicode ? true : false;
}
/**
* Defines the title of the document.
* @param $title (string) The title.
* @public
* @since 1.2
* @see SetAuthor(), SetCreator(), SetKeywords(), SetSubject()
*/
public function SetTitle($title) {
$this->title = $title;
}
/**
* Defines the subject of the document.
* @param $subject (string) The subject.
* @public
* @since 1.2
* @see SetAuthor(), SetCreator(), SetKeywords(), SetTitle()
*/
public function SetSubject($subject) {
$this->subject = $subject;
}
/**
* Defines the author of the document.
* @param $author (string) The name of the author.
* @public
* @since 1.2
* @see SetCreator(), SetKeywords(), SetSubject(), SetTitle()
*/
public function SetAuthor($author) {
$this->author = $author;
}
/**
* Associates keywords with the document, generally in the form 'keyword1 keyword2 ...'.
* @param $keywords (string) The list of keywords.
* @public
* @since 1.2
* @see SetAuthor(), SetCreator(), SetSubject(), SetTitle()
*/
public function SetKeywords($keywords) {
$this->keywords = $keywords;
}
/**
* Defines the creator of the document. This is typically the name of the application that generates the PDF.
* @param $creator (string) The name of the creator.
* @public
* @since 1.2
* @see SetAuthor(), SetKeywords(), SetSubject(), SetTitle()
*/
public function SetCreator($creator) {
$this->creator = $creator;
}
/**
* This method is automatically called in case of fatal error; it simply outputs the message and halts the execution. An inherited class may override it to customize the error handling but should always halt the script, or the resulting document would probably be invalid.
* 2004-06-11 :: Nicola Asuni : changed bold tag with strong
* @param $msg (string) The error message
* @public
* @since 1.0
*/
public function Error($msg) {
// unset all class variables
$this->_destroy(true);
$phpmainver = PHP_VERSION;
// exit program and print error
if ((intval($phpmainver[0]) < 5) OR !defined('K_TCPDF_THROW_EXCEPTION_ERROR') OR !K_TCPDF_THROW_EXCEPTION_ERROR) {
die('TCPDF ERROR: '.$msg);
} else {
throw new Exception('TCPDF ERROR: '.$msg);
}
}
/**
* This method begins the generation of the PDF document.
* It is not necessary to call it explicitly because AddPage() does it automatically.
* Note: no page is created by this method
* @public
* @since 1.0
* @see AddPage(), Close()
*/
public function Open() {
$this->state = 1;
}
/**
* Terminates the PDF document.
* It is not necessary to call this method explicitly because Output() does it automatically.
* If the document contains no page, AddPage() is called to prevent from getting an invalid document.
* @public
* @since 1.0
* @see Open(), Output()
*/
public function Close() {
if ($this->state == 3) {
return;
}
if ($this->page == 0) {
$this->AddPage();
}
$this->endLayer();
if ($this->tcpdflink) {
// save current graphic settings
$gvars = $this->getGraphicVars();
$this->setEqualColumns();
$this->lastpage(true);
$this->SetAutoPageBreak(false);
$this->x = 0;
$this->y = $this->h - (1 / $this->k);
$this->lMargin = 0;
$this->_out('q');
$font = defined('PDF_FONT_NAME_MAIN')?PDF_FONT_NAME_MAIN:'helvetica';
$this->SetFont($font, '', 1);
$this->setTextRenderingMode(0, false, false);
$msg = "\x50\x6f\x77\x65\x72\x65\x64\x20\x62\x79\x20\x54\x43\x50\x44\x46\x20\x28\x77\x77\x77\x2e\x74\x63\x70\x64\x66\x2e\x6f\x72\x67\x29";
$lnk = "\x68\x74\x74\x70\x3a\x2f\x2f\x77\x77\x77\x2e\x74\x63\x70\x64\x66\x2e\x6f\x72\x67";
$this->Cell(0, 0, $msg, 0, 0, 'L', 0, $lnk, 0, false, 'D', 'B');
$this->_out('Q');
// restore graphic settings
$this->setGraphicVars($gvars);
}
// close page
$this->endPage();
// close document
$this->_enddoc();
// unset all class variables (except critical ones)
$this->_destroy(false);
}
/**
* Move pointer at the specified document page and update page dimensions.
* @param $pnum (int) page number (1 ... numpages)
* @param $resetmargins (boolean) if true reset left, right, top margins and Y position.
* @public
* @since 2.1.000 (2008-01-07)
* @see getPage(), lastpage(), getNumPages()
*/
public function setPage($pnum, $resetmargins=false) {
if (($pnum == $this->page) AND ($this->state == 2)) {
return;
}
if (($pnum > 0) AND ($pnum <= $this->numpages)) {
$this->state = 2;
// save current graphic settings
//$gvars = $this->getGraphicVars();
$oldpage = $this->page;
$this->page = $pnum;
$this->wPt = $this->pagedim[$this->page]['w'];
$this->hPt = $this->pagedim[$this->page]['h'];
$this->w = $this->pagedim[$this->page]['wk'];
$this->h = $this->pagedim[$this->page]['hk'];
$this->tMargin = $this->pagedim[$this->page]['tm'];
$this->bMargin = $this->pagedim[$this->page]['bm'];
$this->original_lMargin = $this->pagedim[$this->page]['olm'];
$this->original_rMargin = $this->pagedim[$this->page]['orm'];
$this->AutoPageBreak = $this->pagedim[$this->page]['pb'];
$this->CurOrientation = $this->pagedim[$this->page]['or'];
$this->SetAutoPageBreak($this->AutoPageBreak, $this->bMargin);
// restore graphic settings
//$this->setGraphicVars($gvars);
if ($resetmargins) {
$this->lMargin = $this->pagedim[$this->page]['olm'];
$this->rMargin = $this->pagedim[$this->page]['orm'];
$this->SetY($this->tMargin);
} else {
// account for booklet mode
if ($this->pagedim[$this->page]['olm'] != $this->pagedim[$oldpage]['olm']) {
$deltam = $this->pagedim[$this->page]['olm'] - $this->pagedim[$this->page]['orm'];
$this->lMargin += $deltam;
$this->rMargin -= $deltam;
}
}
} else {
$this->Error('Wrong page number on setPage() function: '.$pnum);
}
}
/**
* Reset pointer to the last document page.
* @param $resetmargins (boolean) if true reset left, right, top margins and Y position.
* @public
* @since 2.0.000 (2008-01-04)
* @see setPage(), getPage(), getNumPages()
*/
public function lastPage($resetmargins=false) {
$this->setPage($this->getNumPages(), $resetmargins);
}
/**
* Get current document page number.
* @return int page number
* @public
* @since 2.1.000 (2008-01-07)
* @see setPage(), lastpage(), getNumPages()
*/
public function getPage() {
return $this->page;
}
/**
* Get the total number of insered pages.
* @return int number of pages
* @public
* @since 2.1.000 (2008-01-07)
* @see setPage(), getPage(), lastpage()
*/
public function getNumPages() {
return $this->numpages;
}
/**
* Adds a new TOC (Table Of Content) page to the document.
* @param $orientation (string) page orientation.
* @param $format (mixed) The format used for pages. It can be either: one of the string values specified at getPageSizeFromFormat() or an array of parameters specified at setPageFormat().
* @param $keepmargins (boolean) if true overwrites the default page margins with the current margins
* @public
* @since 5.0.001 (2010-05-06)
* @see AddPage(), startPage(), endPage(), endTOCPage()
*/
public function addTOCPage($orientation='', $format='', $keepmargins=false) {
$this->AddPage($orientation, $format, $keepmargins, true);
}
/**
* Terminate the current TOC (Table Of Content) page
* @public
* @since 5.0.001 (2010-05-06)
* @see AddPage(), startPage(), endPage(), addTOCPage()
*/
public function endTOCPage() {
$this->endPage(true);
}
/**
* Adds a new page to the document. If a page is already present, the Footer() method is called first to output the footer (if enabled). Then the page is added, the current position set to the top-left corner according to the left and top margins (or top-right if in RTL mode), and Header() is called to display the header (if enabled).
* The origin of the coordinate system is at the top-left corner (or top-right for RTL) and increasing ordinates go downwards.
* @param $orientation (string) page orientation. Possible values are (case insensitive):
P or PORTRAIT (default)
L or LANDSCAPE
* @param $format (mixed) The format used for pages. It can be either: one of the string values specified at getPageSizeFromFormat() or an array of parameters specified at setPageFormat().
* @param $keepmargins (boolean) if true overwrites the default page margins with the current margins
* @param $tocpage (boolean) if true set the tocpage state to true (the added page will be used to display Table Of Content).
* @public
* @since 1.0
* @see startPage(), endPage(), addTOCPage(), endTOCPage(), getPageSizeFromFormat(), setPageFormat()
*/
public function AddPage($orientation='', $format='', $keepmargins=false, $tocpage=false) {
if ($this->inxobj) {
// we are inside an XObject template
return;
}
if (!isset($this->original_lMargin) OR $keepmargins) {
$this->original_lMargin = $this->lMargin;
}
if (!isset($this->original_rMargin) OR $keepmargins) {
$this->original_rMargin = $this->rMargin;
}
// terminate previous page
$this->endPage();
// start new page
$this->startPage($orientation, $format, $tocpage);
}
/**
* Terminate the current page
* @param $tocpage (boolean) if true set the tocpage state to false (end the page used to display Table Of Content).
* @public
* @since 4.2.010 (2008-11-14)
* @see AddPage(), startPage(), addTOCPage(), endTOCPage()
*/
public function endPage($tocpage=false) {
// check if page is already closed
if (($this->page == 0) OR ($this->numpages > $this->page) OR (!$this->pageopen[$this->page])) {
return;
}
// print page footer
$this->setFooter();
// close page
$this->_endpage();
// mark page as closed
$this->pageopen[$this->page] = false;
if ($tocpage) {
$this->tocpage = false;
}
}
/**
* Starts a new page to the document. The page must be closed using the endPage() function.
* The origin of the coordinate system is at the top-left corner and increasing ordinates go downwards.
* @param $orientation (string) page orientation. Possible values are (case insensitive):
P or PORTRAIT (default)
L or LANDSCAPE
* @param $format (mixed) The format used for pages. It can be either: one of the string values specified at getPageSizeFromFormat() or an array of parameters specified at setPageFormat().
* @param $tocpage (boolean) if true the page is designated to contain the Table-Of-Content.
* @since 4.2.010 (2008-11-14)
* @see AddPage(), endPage(), addTOCPage(), endTOCPage(), getPageSizeFromFormat(), setPageFormat()
* @public
*/
public function startPage($orientation='', $format='', $tocpage=false) {
if ($tocpage) {
$this->tocpage = true;
}
// move page numbers of documents to be attached
if ($this->tocpage) {
// move reference to unexistent pages (used for page attachments)
// adjust outlines
$tmpoutlines = $this->outlines;
foreach ($tmpoutlines as $key => $outline) {
if ($outline['p'] > $this->numpages) {
$this->outlines[$key]['p'] = ($outline['p'] + 1);
}
}
// adjust dests
$tmpdests = $this->dests;
foreach ($tmpdests as $key => $dest) {
if ($dest['p'] > $this->numpages) {
$this->dests[$key]['p'] = ($dest['p'] + 1);
}
}
// adjust links
$tmplinks = $this->links;
foreach ($tmplinks as $key => $link) {
if ($link[0] > $this->numpages) {
$this->links[$key][0] = ($link[0] + 1);
}
}
}
if ($this->numpages > $this->page) {
// this page has been already added
$this->setPage($this->page + 1);
$this->SetY($this->tMargin);
return;
}
// start a new page
if ($this->state == 0) {
$this->Open();
}
++$this->numpages;
$this->swapMargins($this->booklet);
// save current graphic settings
$gvars = $this->getGraphicVars();
// start new page
$this->_beginpage($orientation, $format);
// mark page as open
$this->pageopen[$this->page] = true;
// restore graphic settings
$this->setGraphicVars($gvars);
// mark this point
$this->setPageMark();
// print page header
$this->setHeader();
// restore graphic settings
$this->setGraphicVars($gvars);
// mark this point
$this->setPageMark();
// print table header (if any)
$this->setTableHeader();
// set mark for empty page check
$this->emptypagemrk[$this->page]= $this->pagelen[$this->page];
}
/**
* Set start-writing mark on current page stream used to put borders and fills.
* Borders and fills are always created after content and inserted on the position marked by this method.
* This function must be called after calling Image() function for a background image.
* Background images must be always inserted before calling Multicell() or WriteHTMLCell() or WriteHTML() functions.
* @public
* @since 4.0.016 (2008-07-30)
*/
public function setPageMark() {
$this->intmrk[$this->page] = $this->pagelen[$this->page];
$this->bordermrk[$this->page] = $this->intmrk[$this->page];
$this->setContentMark();
}
/**
* Set start-writing mark on selected page.
* Borders and fills are always created after content and inserted on the position marked by this method.
* @param $page (int) page number (default is the current page)
* @protected
* @since 4.6.021 (2009-07-20)
*/
protected function setContentMark($page=0) {
if ($page <= 0) {
$page = $this->page;
}
if (isset($this->footerlen[$page])) {
$this->cntmrk[$page] = $this->pagelen[$page] - $this->footerlen[$page];
} else {
$this->cntmrk[$page] = $this->pagelen[$page];
}
}
/**
* Set header data.
* @param $ln (string) header image logo
* @param $lw (string) header image logo width in mm
* @param $ht (string) string to print as title on document header
* @param $hs (string) string to print on document header
* @param $tc (array) RGB array color for text.
* @param $lc (array) RGB array color for line.
* @public
*/
public function setHeaderData($ln='', $lw=0, $ht='', $hs='', $tc=array(0,0,0), $lc=array(0,0,0)) {
$this->header_logo = $ln;
$this->header_logo_width = $lw;
$this->header_title = $ht;
$this->header_string = $hs;
$this->header_text_color = $tc;
$this->header_line_color = $lc;
}
/**
* Set footer data.
* @param $tc (array) RGB array color for text.
* @param $lc (array) RGB array color for line.
* @public
*/
public function setFooterData($tc=array(0,0,0), $lc=array(0,0,0)) {
$this->footer_text_color = $tc;
$this->footer_line_color = $lc;
}
/**
* Returns header data:
*
$ret['logo'] = logo image
$ret['logo_width'] = width of the image logo in user units
$ret['title'] = header title
$ret['string'] = header description string
* @return array()
* @public
* @since 4.0.012 (2008-07-24)
*/
public function getHeaderData() {
$ret = array();
$ret['logo'] = $this->header_logo;
$ret['logo_width'] = $this->header_logo_width;
$ret['title'] = $this->header_title;
$ret['string'] = $this->header_string;
$ret['text_color'] = $this->header_text_color;
$ret['line_color'] = $this->header_line_color;
return $ret;
}
/**
* Set header margin.
* (minimum distance between header and top page margin)
* @param $hm (int) distance in user units
* @public
*/
public function setHeaderMargin($hm=10) {
$this->header_margin = $hm;
}
/**
* Returns header margin in user units.
* @return float
* @since 4.0.012 (2008-07-24)
* @public
*/
public function getHeaderMargin() {
return $this->header_margin;
}
/**
* Set footer margin.
* (minimum distance between footer and bottom page margin)
* @param $fm (int) distance in user units
* @public
*/
public function setFooterMargin($fm=10) {
$this->footer_margin = $fm;
}
/**
* Returns footer margin in user units.
* @return float
* @since 4.0.012 (2008-07-24)
* @public
*/
public function getFooterMargin() {
return $this->footer_margin;
}
/**
* Set a flag to print page header.
* @param $val (boolean) set to true to print the page header (default), false otherwise.
* @public
*/
public function setPrintHeader($val=true) {
$this->print_header = $val ? true : false;
}
/**
* Set a flag to print page footer.
* @param $val (boolean) set to true to print the page footer (default), false otherwise.
* @public
*/
public function setPrintFooter($val=true) {
$this->print_footer = $val ? true : false;
}
/**
* Return the right-bottom (or left-bottom for RTL) corner X coordinate of last inserted image
* @return float
* @public
*/
public function getImageRBX() {
return $this->img_rb_x;
}
/**
* Return the right-bottom (or left-bottom for RTL) corner Y coordinate of last inserted image
* @return float
* @public
*/
public function getImageRBY() {
return $this->img_rb_y;
}
/**
* Reset the xobject template used by Header() method.
* @public
*/
public function resetHeaderTemplate() {
$this->header_xobjid = -1;
}
/**
* Set a flag to automatically reset the xobject template used by Header() method at each page.
* @param $val (boolean) set to true to reset Header xobject template at each page, false otherwise.
* @public
*/
public function setHeaderTemplateAutoreset($val=true) {
$this->header_xobj_autoreset = $val ? true : false;
}
/**
* This method is used to render the page header.
* It is automatically called by AddPage() and could be overwritten in your own inherited class.
* @public
*/
public function Header() {
if ($this->header_xobjid < 0) {
// start a new XObject Template
$this->header_xobjid = $this->startTemplate($this->w, $this->tMargin);
$headerfont = $this->getHeaderFont();
$headerdata = $this->getHeaderData();
$this->y = $this->header_margin;
if ($this->rtl) {
$this->x = $this->w - $this->original_rMargin;
} else {
$this->x = $this->original_lMargin;
}
if (($headerdata['logo']) AND ($headerdata['logo'] != K_BLANK_IMAGE)) {
$imgtype = TCPDF_IMAGES::getImageFileType(K_PATH_IMAGES.$headerdata['logo']);
if (($imgtype == 'eps') OR ($imgtype == 'ai')) {
$this->ImageEps(K_PATH_IMAGES.$headerdata['logo'], '', '', $headerdata['logo_width']);
} elseif ($imgtype == 'svg') {
$this->ImageSVG(K_PATH_IMAGES.$headerdata['logo'], '', '', $headerdata['logo_width']);
} else {
$this->Image(K_PATH_IMAGES.$headerdata['logo'], '', '', $headerdata['logo_width']);
}
$imgy = $this->getImageRBY();
} else {
$imgy = $this->y;
}
$cell_height = round(($this->cell_height_ratio * $headerfont[2]) / $this->k, 2);
// set starting margin for text data cell
if ($this->getRTL()) {
$header_x = $this->original_rMargin + ($headerdata['logo_width'] * 1.1);
} else {
$header_x = $this->original_lMargin + ($headerdata['logo_width'] * 1.1);
}
$cw = $this->w - $this->original_lMargin - $this->original_rMargin - ($headerdata['logo_width'] * 1.1);
$this->SetTextColorArray($this->header_text_color);
// header title
$this->SetFont($headerfont[0], 'B', $headerfont[2] + 1);
$this->SetX($header_x);
$this->Cell($cw, $cell_height, $headerdata['title'], 0, 1, '', 0, '', 0);
// header string
$this->SetFont($headerfont[0], $headerfont[1], $headerfont[2]);
$this->SetX($header_x);
$this->MultiCell($cw, $cell_height, $headerdata['string'], 0, '', 0, 1, '', '', true, 0, false, true, 0, 'T', false);
// print an ending header line
$this->SetLineStyle(array('width' => 0.85 / $this->k, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => $headerdata['line_color']));
$this->SetY((2.835 / $this->k) + max($imgy, $this->y));
if ($this->rtl) {
$this->SetX($this->original_rMargin);
} else {
$this->SetX($this->original_lMargin);
}
$this->Cell(($this->w - $this->original_lMargin - $this->original_rMargin), 0, '', 'T', 0, 'C');
$this->endTemplate();
}
// print header template
$x = 0;
$dx = 0;
if (!$this->header_xobj_autoreset AND $this->booklet AND (($this->page % 2) == 0)) {
// adjust margins for booklet mode
$dx = ($this->original_lMargin - $this->original_rMargin);
}
if ($this->rtl) {
$x = $this->w + $dx;
} else {
$x = 0 + $dx;
}
$this->printTemplate($this->header_xobjid, $x, 0, 0, 0, '', '', false);
if ($this->header_xobj_autoreset) {
// reset header xobject template at each page
$this->header_xobjid = -1;
}
}
/**
* This method is used to render the page footer.
* It is automatically called by AddPage() and could be overwritten in your own inherited class.
* @public
*/
public function Footer() {
$cur_y = $this->y;
$this->SetTextColorArray($this->footer_text_color);
//set style for cell border
$line_width = (0.85 / $this->k);
$this->SetLineStyle(array('width' => $line_width, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => $this->footer_line_color));
//print document barcode
$barcode = $this->getBarcode();
if (!empty($barcode)) {
$this->Ln($line_width);
$barcode_width = round(($this->w - $this->original_lMargin - $this->original_rMargin) / 3);
$style = array(
'position' => $this->rtl?'R':'L',
'align' => $this->rtl?'R':'L',
'stretch' => false,
'fitwidth' => true,
'cellfitalign' => '',
'border' => false,
'padding' => 0,
'fgcolor' => array(0,0,0),
'bgcolor' => false,
'text' => false
);
$this->write1DBarcode($barcode, 'C128', '', $cur_y + $line_width, '', (($this->footer_margin / 3) - $line_width), 0.3, $style, '');
}
$w_page = isset($this->l['w_page']) ? $this->l['w_page'].' ' : '';
if (empty($this->pagegroups)) {
$pagenumtxt = $w_page.$this->getAliasNumPage().' / '.$this->getAliasNbPages();
} else {
$pagenumtxt = $w_page.$this->getPageNumGroupAlias().' / '.$this->getPageGroupAlias();
}
$this->SetY($cur_y);
//Print page number
if ($this->getRTL()) {
$this->SetX($this->original_rMargin);
$this->Cell(0, 0, $pagenumtxt, 'T', 0, 'L');
} else {
$this->SetX($this->original_lMargin);
$this->Cell(0, 0, $this->getAliasRightShift().$pagenumtxt, 'T', 0, 'R');
}
}
/**
* This method is used to render the page header.
* @protected
* @since 4.0.012 (2008-07-24)
*/
protected function setHeader() {
if (!$this->print_header OR ($this->state != 2)) {
return;
}
$this->InHeader = true;
$this->setGraphicVars($this->default_graphic_vars);
$temp_thead = $this->thead;
$temp_theadMargins = $this->theadMargins;
$lasth = $this->lasth;
$this->_out('q');
$this->rMargin = $this->original_rMargin;
$this->lMargin = $this->original_lMargin;
$this->SetCellPadding(0);
//set current position
if ($this->rtl) {
$this->SetXY($this->original_rMargin, $this->header_margin);
} else {
$this->SetXY($this->original_lMargin, $this->header_margin);
}
$this->SetFont($this->header_font[0], $this->header_font[1], $this->header_font[2]);
$this->Header();
//restore position
if ($this->rtl) {
$this->SetXY($this->original_rMargin, $this->tMargin);
} else {
$this->SetXY($this->original_lMargin, $this->tMargin);
}
$this->_out('Q');
$this->lasth = $lasth;
$this->thead = $temp_thead;
$this->theadMargins = $temp_theadMargins;
$this->newline = false;
$this->InHeader = false;
}
/**
* This method is used to render the page footer.
* @protected
* @since 4.0.012 (2008-07-24)
*/
protected function setFooter() {
if ($this->state != 2) {
return;
}
$this->InFooter = true;
// save current graphic settings
$gvars = $this->getGraphicVars();
// mark this point
$this->footerpos[$this->page] = $this->pagelen[$this->page];
$this->_out("\n");
if ($this->print_footer) {
$this->setGraphicVars($this->default_graphic_vars);
$this->current_column = 0;
$this->num_columns = 1;
$temp_thead = $this->thead;
$temp_theadMargins = $this->theadMargins;
$lasth = $this->lasth;
$this->_out('q');
$this->rMargin = $this->original_rMargin;
$this->lMargin = $this->original_lMargin;
$this->SetCellPadding(0);
//set current position
$footer_y = $this->h - $this->footer_margin;
if ($this->rtl) {
$this->SetXY($this->original_rMargin, $footer_y);
} else {
$this->SetXY($this->original_lMargin, $footer_y);
}
$this->SetFont($this->footer_font[0], $this->footer_font[1], $this->footer_font[2]);
$this->Footer();
//restore position
if ($this->rtl) {
$this->SetXY($this->original_rMargin, $this->tMargin);
} else {
$this->SetXY($this->original_lMargin, $this->tMargin);
}
$this->_out('Q');
$this->lasth = $lasth;
$this->thead = $temp_thead;
$this->theadMargins = $temp_theadMargins;
}
// restore graphic settings
$this->setGraphicVars($gvars);
$this->current_column = $gvars['current_column'];
$this->num_columns = $gvars['num_columns'];
// calculate footer length
$this->footerlen[$this->page] = $this->pagelen[$this->page] - $this->footerpos[$this->page] + 1;
$this->InFooter = false;
}
/**
* Check if we are on the page body (excluding page header and footer).
* @return true if we are not in page header nor in page footer, false otherwise.
* @protected
* @since 5.9.091 (2011-06-15)
*/
protected function inPageBody() {
return (($this->InHeader === false) AND ($this->InFooter === false));
}
/**
* This method is used to render the table header on new page (if any).
* @protected
* @since 4.5.030 (2009-03-25)
*/
protected function setTableHeader() {
if ($this->num_columns > 1) {
// multi column mode
return;
}
if (isset($this->theadMargins['top'])) {
// restore the original top-margin
$this->tMargin = $this->theadMargins['top'];
$this->pagedim[$this->page]['tm'] = $this->tMargin;
$this->y = $this->tMargin;
}
if (!TCPDF_STATIC::empty_string($this->thead) AND (!$this->inthead)) {
// set margins
$prev_lMargin = $this->lMargin;
$prev_rMargin = $this->rMargin;
$prev_cell_padding = $this->cell_padding;
$this->lMargin = $this->theadMargins['lmargin'] + ($this->pagedim[$this->page]['olm'] - $this->pagedim[$this->theadMargins['page']]['olm']);
$this->rMargin = $this->theadMargins['rmargin'] + ($this->pagedim[$this->page]['orm'] - $this->pagedim[$this->theadMargins['page']]['orm']);
$this->cell_padding = $this->theadMargins['cell_padding'];
if ($this->rtl) {
$this->x = $this->w - $this->rMargin;
} else {
$this->x = $this->lMargin;
}
// account for special "cell" mode
if ($this->theadMargins['cell']) {
if ($this->rtl) {
$this->x -= $this->cell_padding['R'];
} else {
$this->x += $this->cell_padding['L'];
}
}
// print table header
$this->writeHTML($this->thead, false, false, false, false, '');
// set new top margin to skip the table headers
if (!isset($this->theadMargins['top'])) {
$this->theadMargins['top'] = $this->tMargin;
}
// store end of header position
if (!isset($this->columns[0]['th'])) {
$this->columns[0]['th'] = array();
}
$this->columns[0]['th']['\''.$this->page.'\''] = $this->y;
$this->tMargin = $this->y;
$this->pagedim[$this->page]['tm'] = $this->tMargin;
$this->lasth = 0;
$this->lMargin = $prev_lMargin;
$this->rMargin = $prev_rMargin;
$this->cell_padding = $prev_cell_padding;
}
}
/**
* Returns the current page number.
* @return int page number
* @public
* @since 1.0
* @see getAliasNbPages()
*/
public function PageNo() {
return $this->page;
}
/**
* Defines a new spot color.
* It can be expressed in RGB components or gray scale.
* The method can be called before the first page is created and the value is retained from page to page.
* @param $name (string) Full name of the spot color.
* @param $c (float) Cyan color for CMYK. Value between 0 and 100.
* @param $m (float) Magenta color for CMYK. Value between 0 and 100.
* @param $y (float) Yellow color for CMYK. Value between 0 and 100.
* @param $k (float) Key (Black) color for CMYK. Value between 0 and 100.
* @public
* @since 4.0.024 (2008-09-12)
* @see SetDrawSpotColor(), SetFillSpotColor(), SetTextSpotColor()
*/
public function AddSpotColor($name, $c, $m, $y, $k) {
if (!isset($this->spot_colors[$name])) {
$i = (1 + count($this->spot_colors));
$this->spot_colors[$name] = array('C' => $c, 'M' => $m, 'Y' => $y, 'K' => $k, 'name' => $name, 'i' => $i);
}
}
/**
* Set the spot color for the specified type ('draw', 'fill', 'text').
* @param $type (string) Type of object affected by this color: ('draw', 'fill', 'text').
* @param $name (string) Name of the spot color.
* @param $tint (float) Intensity of the color (from 0 to 100 ; 100 = full intensity by default).
* @return (string) PDF color command.
* @public
* @since 5.9.125 (2011-10-03)
*/
public function setSpotColor($type, $name, $tint=100) {
$spotcolor = TCPDF_COLORS::getSpotColor($name, $this->spot_colors);
if ($spotcolor === false) {
$this->Error('Undefined spot color: '.$name.', you must add it on the spotcolors.php file.');
}
$tint = (max(0, min(100, $tint)) / 100);
$pdfcolor = sprintf('/CS%d ', $this->spot_colors[$name]['i']);
switch ($type) {
case 'draw': {
$pdfcolor .= sprintf('CS %F SCN', $tint);
$this->DrawColor = $pdfcolor;
$this->strokecolor = $spotcolor;
break;
}
case 'fill': {
$pdfcolor .= sprintf('cs %F scn', $tint);
$this->FillColor = $pdfcolor;
$this->bgcolor = $spotcolor;
break;
}
case 'text': {
$pdfcolor .= sprintf('cs %F scn', $tint);
$this->TextColor = $pdfcolor;
$this->fgcolor = $spotcolor;
break;
}
}
$this->ColorFlag = ($this->FillColor != $this->TextColor);
if ($this->state == 2) {
$this->_out($pdfcolor);
}
if ($this->inxobj) {
// we are inside an XObject template
$this->xobjects[$this->xobjid]['spot_colors'][$name] = $this->spot_colors[$name];
}
return $pdfcolor;
}
/**
* Defines the spot color used for all drawing operations (lines, rectangles and cell borders).
* @param $name (string) Name of the spot color.
* @param $tint (float) Intensity of the color (from 0 to 100 ; 100 = full intensity by default).
* @public
* @since 4.0.024 (2008-09-12)
* @see AddSpotColor(), SetFillSpotColor(), SetTextSpotColor()
*/
public function SetDrawSpotColor($name, $tint=100) {
$this->setSpotColor('draw', $name, $tint);
}
/**
* Defines the spot color used for all filling operations (filled rectangles and cell backgrounds).
* @param $name (string) Name of the spot color.
* @param $tint (float) Intensity of the color (from 0 to 100 ; 100 = full intensity by default).
* @public
* @since 4.0.024 (2008-09-12)
* @see AddSpotColor(), SetDrawSpotColor(), SetTextSpotColor()
*/
public function SetFillSpotColor($name, $tint=100) {
$this->setSpotColor('fill', $name, $tint);
}
/**
* Defines the spot color used for text.
* @param $name (string) Name of the spot color.
* @param $tint (int) Intensity of the color (from 0 to 100 ; 100 = full intensity by default).
* @public
* @since 4.0.024 (2008-09-12)
* @see AddSpotColor(), SetDrawSpotColor(), SetFillSpotColor()
*/
public function SetTextSpotColor($name, $tint=100) {
$this->setSpotColor('text', $name, $tint);
}
/**
* Set the color array for the specified type ('draw', 'fill', 'text').
* It can be expressed in RGB, CMYK or GRAY SCALE components.
* The method can be called before the first page is created and the value is retained from page to page.
* @param $type (string) Type of object affected by this color: ('draw', 'fill', 'text').
* @param $color (array) Array of colors (1=gray, 3=RGB, 4=CMYK or 5=spotcolor=CMYK+name values).
* @param $ret (boolean) If true do not send the PDF command.
* @return (string) The PDF command or empty string.
* @public
* @since 3.1.000 (2008-06-11)
*/
public function setColorArray($type, $color, $ret=false) {
if (is_array($color)) {
$color = array_values($color);
// component: grey, RGB red or CMYK cyan
$c = isset($color[0]) ? $color[0] : -1;
// component: RGB green or CMYK magenta
$m = isset($color[1]) ? $color[1] : -1;
// component: RGB blue or CMYK yellow
$y = isset($color[2]) ? $color[2] : -1;
// component: CMYK black
$k = isset($color[3]) ? $color[3] : -1;
// color name
$name = isset($color[4]) ? $color[4] : '';
if ($c >= 0) {
return $this->setColor($type, $c, $m, $y, $k, $ret, $name);
}
}
return '';
}
/**
* Defines the color used for all drawing operations (lines, rectangles and cell borders).
* It can be expressed in RGB, CMYK or GRAY SCALE components.
* The method can be called before the first page is created and the value is retained from page to page.
* @param $color (array) Array of colors (1, 3 or 4 values).
* @param $ret (boolean) If true do not send the PDF command.
* @return string the PDF command
* @public
* @since 3.1.000 (2008-06-11)
* @see SetDrawColor()
*/
public function SetDrawColorArray($color, $ret=false) {
return $this->setColorArray('draw', $color, $ret);
}
/**
* Defines the color used for all filling operations (filled rectangles and cell backgrounds).
* It can be expressed in RGB, CMYK or GRAY SCALE components.
* The method can be called before the first page is created and the value is retained from page to page.
* @param $color (array) Array of colors (1, 3 or 4 values).
* @param $ret (boolean) If true do not send the PDF command.
* @public
* @since 3.1.000 (2008-6-11)
* @see SetFillColor()
*/
public function SetFillColorArray($color, $ret=false) {
return $this->setColorArray('fill', $color, $ret);
}
/**
* Defines the color used for text. It can be expressed in RGB components or gray scale.
* The method can be called before the first page is created and the value is retained from page to page.
* @param $color (array) Array of colors (1, 3 or 4 values).
* @param $ret (boolean) If true do not send the PDF command.
* @public
* @since 3.1.000 (2008-6-11)
* @see SetFillColor()
*/
public function SetTextColorArray($color, $ret=false) {
return $this->setColorArray('text', $color, $ret);
}
/**
* Defines the color used by the specified type ('draw', 'fill', 'text').
* @param $type (string) Type of object affected by this color: ('draw', 'fill', 'text').
* @param $col1 (float) GRAY level for single color, or Red color for RGB (0-255), or CYAN color for CMYK (0-100).
* @param $col2 (float) GREEN color for RGB (0-255), or MAGENTA color for CMYK (0-100).
* @param $col3 (float) BLUE color for RGB (0-255), or YELLOW color for CMYK (0-100).
* @param $col4 (float) KEY (BLACK) color for CMYK (0-100).
* @param $ret (boolean) If true do not send the command.
* @param $name (string) spot color name (if any)
* @return (string) The PDF command or empty string.
* @public
* @since 5.9.125 (2011-10-03)
*/
public function setColor($type, $col1=0, $col2=-1, $col3=-1, $col4=-1, $ret=false, $name='') {
// set default values
if (!is_numeric($col1)) {
$col1 = 0;
}
if (!is_numeric($col2)) {
$col2 = -1;
}
if (!is_numeric($col3)) {
$col3 = -1;
}
if (!is_numeric($col4)) {
$col4 = -1;
}
// set color by case
$suffix = '';
if (($col2 == -1) AND ($col3 == -1) AND ($col4 == -1)) {
// Grey scale
$col1 = max(0, min(255, $col1));
$intcolor = array('G' => $col1);
$pdfcolor = sprintf('%F ', ($col1 / 255));
$suffix = 'g';
} elseif ($col4 == -1) {
// RGB
$col1 = max(0, min(255, $col1));
$col2 = max(0, min(255, $col2));
$col3 = max(0, min(255, $col3));
$intcolor = array('R' => $col1, 'G' => $col2, 'B' => $col3);
$pdfcolor = sprintf('%F %F %F ', ($col1 / 255), ($col2 / 255), ($col3 / 255));
$suffix = 'rg';
} else {
$col1 = max(0, min(100, $col1));
$col2 = max(0, min(100, $col2));
$col3 = max(0, min(100, $col3));
$col4 = max(0, min(100, $col4));
if (empty($name)) {
// CMYK
$intcolor = array('C' => $col1, 'M' => $col2, 'Y' => $col3, 'K' => $col4);
$pdfcolor = sprintf('%F %F %F %F ', ($col1 / 100), ($col2 / 100), ($col3 / 100), ($col4 / 100));
$suffix = 'k';
} else {
// SPOT COLOR
$intcolor = array('C' => $col1, 'M' => $col2, 'Y' => $col3, 'K' => $col4, 'name' => $name);
$this->AddSpotColor($name, $col1, $col2, $col3, $col4);
$pdfcolor = $this->setSpotColor($type, $name, 100);
}
}
switch ($type) {
case 'draw': {
$pdfcolor .= strtoupper($suffix);
$this->DrawColor = $pdfcolor;
$this->strokecolor = $intcolor;
break;
}
case 'fill': {
$pdfcolor .= $suffix;
$this->FillColor = $pdfcolor;
$this->bgcolor = $intcolor;
break;
}
case 'text': {
$pdfcolor .= $suffix;
$this->TextColor = $pdfcolor;
$this->fgcolor = $intcolor;
break;
}
}
$this->ColorFlag = ($this->FillColor != $this->TextColor);
if (($type != 'text') AND ($this->state == 2)) {
if (!$ret) {
$this->_out($pdfcolor);
}
return $pdfcolor;
}
return '';
}
/**
* Defines the color used for all drawing operations (lines, rectangles and cell borders). It can be expressed in RGB components or gray scale. The method can be called before the first page is created and the value is retained from page to page.
* @param $col1 (float) GRAY level for single color, or Red color for RGB (0-255), or CYAN color for CMYK (0-100).
* @param $col2 (float) GREEN color for RGB (0-255), or MAGENTA color for CMYK (0-100).
* @param $col3 (float) BLUE color for RGB (0-255), or YELLOW color for CMYK (0-100).
* @param $col4 (float) KEY (BLACK) color for CMYK (0-100).
* @param $ret (boolean) If true do not send the command.
* @param $name (string) spot color name (if any)
* @return string the PDF command
* @public
* @since 1.3
* @see SetDrawColorArray(), SetFillColor(), SetTextColor(), Line(), Rect(), Cell(), MultiCell()
*/
public function SetDrawColor($col1=0, $col2=-1, $col3=-1, $col4=-1, $ret=false, $name='') {
return $this->setColor('draw', $col1, $col2, $col3, $col4, $ret, $name);
}
/**
* Defines the color used for all filling operations (filled rectangles and cell backgrounds). It can be expressed in RGB components or gray scale. The method can be called before the first page is created and the value is retained from page to page.
* @param $col1 (float) GRAY level for single color, or Red color for RGB (0-255), or CYAN color for CMYK (0-100).
* @param $col2 (float) GREEN color for RGB (0-255), or MAGENTA color for CMYK (0-100).
* @param $col3 (float) BLUE color for RGB (0-255), or YELLOW color for CMYK (0-100).
* @param $col4 (float) KEY (BLACK) color for CMYK (0-100).
* @param $ret (boolean) If true do not send the command.
* @param $name (string) Spot color name (if any).
* @return (string) The PDF command.
* @public
* @since 1.3
* @see SetFillColorArray(), SetDrawColor(), SetTextColor(), Rect(), Cell(), MultiCell()
*/
public function SetFillColor($col1=0, $col2=-1, $col3=-1, $col4=-1, $ret=false, $name='') {
return $this->setColor('fill', $col1, $col2, $col3, $col4, $ret, $name);
}
/**
* Defines the color used for text. It can be expressed in RGB components or gray scale. The method can be called before the first page is created and the value is retained from page to page.
* @param $col1 (float) GRAY level for single color, or Red color for RGB (0-255), or CYAN color for CMYK (0-100).
* @param $col2 (float) GREEN color for RGB (0-255), or MAGENTA color for CMYK (0-100).
* @param $col3 (float) BLUE color for RGB (0-255), or YELLOW color for CMYK (0-100).
* @param $col4 (float) KEY (BLACK) color for CMYK (0-100).
* @param $ret (boolean) If true do not send the command.
* @param $name (string) Spot color name (if any).
* @return (string) Empty string.
* @public
* @since 1.3
* @see SetTextColorArray(), SetDrawColor(), SetFillColor(), Text(), Cell(), MultiCell()
*/
public function SetTextColor($col1=0, $col2=-1, $col3=-1, $col4=-1, $ret=false, $name='') {
return $this->setColor('text', $col1, $col2, $col3, $col4, $ret, $name);
}
/**
* Returns the length of a string in user unit. A font must be selected.
* @param $s (string) The string whose length is to be computed
* @param $fontname (string) Family font. It can be either a name defined by AddFont() or one of the standard families. It is also possible to pass an empty string, in that case, the current family is retained.
* @param $fontstyle (string) Font style. Possible values are (case insensitive):
empty string: regular
B: bold
I: italic
U: underline
D: line-trough
O: overline
or any combination. The default value is regular.
* @param $fontsize (float) Font size in points. The default value is the current size.
* @param $getarray (boolean) if true returns an array of characters widths, if false returns the total length.
* @return mixed int total string length or array of characted widths
* @author Nicola Asuni
* @public
* @since 1.2
*/
public function GetStringWidth($s, $fontname='', $fontstyle='', $fontsize=0, $getarray=false) {
return $this->GetArrStringWidth(TCPDF_FONTS::utf8Bidi(TCPDF_FONTS::UTF8StringToArray($s, $this->isunicode, $this->CurrentFont), $s, $this->tmprtl, $this->isunicode, $this->CurrentFont), $fontname, $fontstyle, $fontsize, $getarray);
}
/**
* Returns the string length of an array of chars in user unit or an array of characters widths. A font must be selected.
* @param $sa (string) The array of chars whose total length is to be computed
* @param $fontname (string) Family font. It can be either a name defined by AddFont() or one of the standard families. It is also possible to pass an empty string, in that case, the current family is retained.
* @param $fontstyle (string) Font style. Possible values are (case insensitive):
empty string: regular
B: bold
I: italic
U: underline
D: line trough
O: overline
or any combination. The default value is regular.
* @param $fontsize (float) Font size in points. The default value is the current size.
* @param $getarray (boolean) if true returns an array of characters widths, if false returns the total length.
* @return mixed int total string length or array of characted widths
* @author Nicola Asuni
* @public
* @since 2.4.000 (2008-03-06)
*/
public function GetArrStringWidth($sa, $fontname='', $fontstyle='', $fontsize=0, $getarray=false) {
// store current values
if (!TCPDF_STATIC::empty_string($fontname)) {
$prev_FontFamily = $this->FontFamily;
$prev_FontStyle = $this->FontStyle;
$prev_FontSizePt = $this->FontSizePt;
$this->SetFont($fontname, $fontstyle, $fontsize, '', 'default', false);
}
// convert UTF-8 array to Latin1 if required
if ($this->isunicode AND (!$this->isUnicodeFont())) {
$sa = TCPDF_FONTS::UTF8ArrToLatin1Arr($sa);
}
$w = 0; // total width
$wa = array(); // array of characters widths
foreach ($sa as $ck => $char) {
// character width
$cw = $this->GetCharWidth($char, isset($sa[($ck + 1)]));
$wa[] = $cw;
$w += $cw;
}
// restore previous values
if (!TCPDF_STATIC::empty_string($fontname)) {
$this->SetFont($prev_FontFamily, $prev_FontStyle, $prev_FontSizePt, '', 'default', false);
}
if ($getarray) {
return $wa;
}
return $w;
}
/**
* Returns the length of the char in user unit for the current font considering current stretching and spacing (tracking).
* @param $char (int) The char code whose length is to be returned
* @param $notlast (boolean) If false ignore the font-spacing.
* @return float char width
* @author Nicola Asuni
* @public
* @since 2.4.000 (2008-03-06)
*/
public function GetCharWidth($char, $notlast=true) {
// get raw width
$chw = $this->getRawCharWidth($char);
if (($this->font_spacing < 0) OR (($this->font_spacing > 0) AND $notlast)) {
// increase/decrease font spacing
$chw += $this->font_spacing;
}
if ($this->font_stretching != 100) {
// fixed stretching mode
$chw *= ($this->font_stretching / 100);
}
return $chw;
}
/**
* Returns the length of the char in user unit for the current font.
* @param $char (int) The char code whose length is to be returned
* @return float char width
* @author Nicola Asuni
* @public
* @since 5.9.000 (2010-09-28)
*/
public function getRawCharWidth($char) {
if ($char == 173) {
// SHY character will not be printed
return (0);
}
if (isset($this->CurrentFont['cw'][$char])) {
$w = $this->CurrentFont['cw'][$char];
} elseif (isset($this->CurrentFont['dw'])) {
// default width
$w = $this->CurrentFont['dw'];
} elseif (isset($this->CurrentFont['cw'][32])) {
// default width
$w = $this->CurrentFont['cw'][32];
} else {
$w = 600;
}
return $this->getAbsFontMeasure($w);
}
/**
* Returns the numbero of characters in a string.
* @param $s (string) The input string.
* @return int number of characters
* @public
* @since 2.0.0001 (2008-01-07)
*/
public function GetNumChars($s) {
if ($this->isUnicodeFont()) {
return count(TCPDF_FONTS::UTF8StringToArray($s, $this->isunicode, $this->CurrentFont));
}
return strlen($s);
}
/**
* Fill the list of available fonts ($this->fontlist).
* @protected
* @since 4.0.013 (2008-07-28)
*/
protected function getFontsList() {
if (($fontsdir = opendir(TCPDF_FONTS::_getfontpath())) !== false) {
while (($file = readdir($fontsdir)) !== false) {
if (substr($file, -4) == '.php') {
array_push($this->fontlist, strtolower(basename($file, '.php')));
}
}
closedir($fontsdir);
}
}
/**
* Returns the unicode caracter specified by the value
* @param $c (int) UTF-8 value
* @return Returns the specified character.
* @since 2.3.000 (2008-03-05)
* @public
* @deprecated
*/
public function unichr($c) {
return TCPDF_FONTS::unichr($c, $this->isunicode);
}
/**
* Convert and add the selected TrueType or Type1 font to the fonts folder (that must be writeable).
* @param $fontfile (string) Font file (full path).
* @param $fonttype (string) Font type. Leave empty for autodetect mode. Valid values are: TrueTypeUnicode, TrueType, Type1, CID0JP = CID-0 Japanese, CID0KR = CID-0 Korean, CID0CS = CID-0 Chinese Simplified, CID0CT = CID-0 Chinese Traditional.
* @param $enc (string) Name of the encoding table to use. Leave empty for default mode. Omit this parameter for TrueType Unicode and symbolic fonts like Symbol or ZapfDingBats.
* @param $flags (int) Unsigned 32-bit integer containing flags specifying various characteristics of the font (PDF32000:2008 - 9.8.2 Font Descriptor Flags): +1 for fixed font; +4 for symbol or +32 for non-symbol; +64 for italic. Fixed and Italic mode are generally autodetected so you have to set it to 32 = non-symbolic font (default) or 4 = symbolic font.
* @param $outpath (string) Output path for generated font files (must be writeable by the web server). Leave empty for default font folder.
* @param $platid (int) Platform ID for CMAP table to extract (when building a Unicode font for Windows this value should be 3, for Macintosh should be 1).
* @param $encid (int) Encoding ID for CMAP table to extract (when building a Unicode font for Windows this value should be 1, for Macintosh should be 0). When Platform ID is 3, legal values for Encoding ID are: 0=Symbol, 1=Unicode, 2=ShiftJIS, 3=PRC, 4=Big5, 5=Wansung, 6=Johab, 7=Reserved, 8=Reserved, 9=Reserved, 10=UCS-4.
* @param $addcbbox (boolean) If true includes the character bounding box information on the php font file.
* @return (string) TCPDF font name.
* @author Nicola Asuni
* @since 5.9.123 (2010-09-30)
* @public
* @deprecated
*/
public function addTTFfont($fontfile, $fonttype='', $enc='', $flags=32, $outpath='', $platid=3, $encid=1, $addcbbox=false) {
return TCPDF_FONTS::addTTFfont($fontfile, $fonttype, $enc, $flags, $outpath, $platid, $encid, $addcbbox);
}
/**
* Imports a TrueType, Type1, core, or CID0 font and makes it available.
* It is necessary to generate a font definition file first (read /fonts/utils/README.TXT).
* The definition file (and the font file itself when embedding) must be present either in the current directory or in the one indicated by K_PATH_FONTS if the constant is defined. If it could not be found, the error "Could not include font definition file" is generated.
* @param $family (string) Font family. The name can be chosen arbitrarily. If it is a standard family name, it will override the corresponding font.
* @param $style (string) Font style. Possible values are (case insensitive):
empty string: regular (default)
B: bold
I: italic
BI or IB: bold italic
* @param $fontfile (string) The font definition file. By default, the name is built from the family and style, in lower case with no spaces.
* @return array containing the font data, or false in case of error.
* @param $subset (mixed) if true embedd only a subset of the font (stores only the information related to the used characters); if false embedd full font; if 'default' uses the default value set using setFontSubsetting(). This option is valid only for TrueTypeUnicode fonts. If you want to enable users to change the document, set this parameter to false. If you subset the font, the person who receives your PDF would need to have your same font in order to make changes to your PDF. The file size of the PDF would also be smaller because you are embedding only part of a font.
* @public
* @since 1.5
* @see SetFont(), setFontSubsetting()
*/
public function AddFont($family, $style='', $fontfile='', $subset='default') {
if ($subset === 'default') {
$subset = $this->font_subsetting;
}
if ($this->pdfa_mode) {
$subset = false;
}
if (TCPDF_STATIC::empty_string($family)) {
if (!TCPDF_STATIC::empty_string($this->FontFamily)) {
$family = $this->FontFamily;
} else {
$this->Error('Empty font family');
}
}
// move embedded styles on $style
if (substr($family, -1) == 'I') {
$style .= 'I';
$family = substr($family, 0, -1);
}
if (substr($family, -1) == 'B') {
$style .= 'B';
$family = substr($family, 0, -1);
}
// normalize family name
$family = strtolower($family);
if ((!$this->isunicode) AND ($family == 'arial')) {
$family = 'helvetica';
}
if (($family == 'symbol') OR ($family == 'zapfdingbats')) {
$style = '';
}
if ($this->pdfa_mode AND (isset($this->CoreFonts[$family]))) {
// all fonts must be embedded
$family = 'pdfa'.$family;
}
$tempstyle = strtoupper($style);
$style = '';
// underline
if (strpos($tempstyle, 'U') !== false) {
$this->underline = true;
} else {
$this->underline = false;
}
// line-through (deleted)
if (strpos($tempstyle, 'D') !== false) {
$this->linethrough = true;
} else {
$this->linethrough = false;
}
// overline
if (strpos($tempstyle, 'O') !== false) {
$this->overline = true;
} else {
$this->overline = false;
}
// bold
if (strpos($tempstyle, 'B') !== false) {
$style .= 'B';
}
// oblique
if (strpos($tempstyle, 'I') !== false) {
$style .= 'I';
}
$bistyle = $style;
$fontkey = $family.$style;
$font_style = $style.($this->underline ? 'U' : '').($this->linethrough ? 'D' : '').($this->overline ? 'O' : '');
$fontdata = array('fontkey' => $fontkey, 'family' => $family, 'style' => $font_style);
// check if the font has been already added
$fb = $this->getFontBuffer($fontkey);
if ($fb !== false) {
if ($this->inxobj) {
// we are inside an XObject template
$this->xobjects[$this->xobjid]['fonts'][$fontkey] = $fb['i'];
}
return $fontdata;
}
if (isset($type)) {
unset($type);
}
if (isset($cw)) {
unset($cw);
}
// get specified font directory (if any)
$fontdir = false;
if (!TCPDF_STATIC::empty_string($fontfile)) {
$fontdir = dirname($fontfile);
if (TCPDF_STATIC::empty_string($fontdir) OR ($fontdir == '.')) {
$fontdir = '';
} else {
$fontdir .= '/';
}
}
$missing_style = false; // true when the font style variation is missing
// search and include font file
if (TCPDF_STATIC::empty_string($fontfile) OR (!file_exists($fontfile))) {
// build a standard filenames for specified font
$tmp_fontfile = str_replace(' ', '', $family).strtolower($style).'.php';
// search files on various directories
if (($fontdir !== false) AND file_exists($fontdir.$tmp_fontfile)) {
$fontfile = $fontdir.$tmp_fontfile;
} elseif (file_exists(TCPDF_FONTS::_getfontpath().$tmp_fontfile)) {
$fontfile = TCPDF_FONTS::_getfontpath().$tmp_fontfile;
} elseif (file_exists($tmp_fontfile)) {
$fontfile = $tmp_fontfile;
} elseif (!TCPDF_STATIC::empty_string($style)) {
$missing_style = true;
// try to remove the style part
$tmp_fontfile = str_replace(' ', '', $family).'.php';
if (($fontdir !== false) AND file_exists($fontdir.$tmp_fontfile)) {
$fontfile = $fontdir.$tmp_fontfile;
} elseif (file_exists(TCPDF_FONTS::_getfontpath().$tmp_fontfile)) {
$fontfile = TCPDF_FONTS::_getfontpath().$tmp_fontfile;
} else {
$fontfile = $tmp_fontfile;
}
}
}
// include font file
if (file_exists($fontfile)) {
include($fontfile);
} else {
$this->Error('Could not include font definition file: '.$family.'');
}
// check font parameters
if ((!isset($type)) OR (!isset($cw))) {
$this->Error('The font definition file has a bad format: '.$fontfile.'');
}
// SET default parameters
if (!isset($file) OR TCPDF_STATIC::empty_string($file)) {
$file = '';
}
if (!isset($enc) OR TCPDF_STATIC::empty_string($enc)) {
$enc = '';
}
if (!isset($cidinfo) OR TCPDF_STATIC::empty_string($cidinfo)) {
$cidinfo = array('Registry'=>'Adobe', 'Ordering'=>'Identity', 'Supplement'=>0);
$cidinfo['uni2cid'] = array();
}
if (!isset($ctg) OR TCPDF_STATIC::empty_string($ctg)) {
$ctg = '';
}
if (!isset($desc) OR TCPDF_STATIC::empty_string($desc)) {
$desc = array();
}
if (!isset($up) OR TCPDF_STATIC::empty_string($up)) {
$up = -100;
}
if (!isset($ut) OR TCPDF_STATIC::empty_string($ut)) {
$ut = 50;
}
if (!isset($cw) OR TCPDF_STATIC::empty_string($cw)) {
$cw = array();
}
if (!isset($dw) OR TCPDF_STATIC::empty_string($dw)) {
// set default width
if (isset($desc['MissingWidth']) AND ($desc['MissingWidth'] > 0)) {
$dw = $desc['MissingWidth'];
} elseif (isset($cw[32])) {
$dw = $cw[32];
} else {
$dw = 600;
}
}
++$this->numfonts;
if ($type == 'core') {
$name = $this->CoreFonts[$fontkey];
$subset = false;
} elseif (($type == 'TrueType') OR ($type == 'Type1')) {
$subset = false;
} elseif ($type == 'TrueTypeUnicode') {
$enc = 'Identity-H';
} elseif ($type == 'cidfont0') {
if ($this->pdfa_mode) {
$this->Error('All fonts must be embedded in PDF/A mode!');
}
} else {
$this->Error('Unknow font type: '.$type.'');
}
// set name if unset
if (!isset($name) OR empty($name)) {
$name = $fontkey;
}
// create artificial font style variations if missing (only works with non-embedded fonts)
if (($type != 'core') AND $missing_style) {
// style variations
$styles = array('' => '', 'B' => ',Bold', 'I' => ',Italic', 'BI' => ',BoldItalic');
$name .= $styles[$bistyle];
// artificial bold
if (strpos($bistyle, 'B') !== false) {
if (isset($desc['StemV'])) {
// from normal to bold
$desc['StemV'] = round($desc['StemV'] * 1.75);
} else {
// bold
$desc['StemV'] = 123;
}
}
// artificial italic
if (strpos($bistyle, 'I') !== false) {
if (isset($desc['ItalicAngle'])) {
$desc['ItalicAngle'] -= 11;
} else {
$desc['ItalicAngle'] = -11;
}
if (isset($desc['Flags'])) {
$desc['Flags'] |= 64; //bit 7
} else {
$desc['Flags'] = 64;
}
}
}
// check if the array of characters bounding boxes is defined
if (!isset($cbbox)) {
$cbbox = array();
}
// initialize subsetchars
$subsetchars = array_fill(0, 255, true);
$this->setFontBuffer($fontkey, array('fontkey' => $fontkey, 'i' => $this->numfonts, 'type' => $type, 'name' => $name, 'desc' => $desc, 'up' => $up, 'ut' => $ut, 'cw' => $cw, 'cbbox' => $cbbox, 'dw' => $dw, 'enc' => $enc, 'cidinfo' => $cidinfo, 'file' => $file, 'ctg' => $ctg, 'subset' => $subset, 'subsetchars' => $subsetchars));
if ($this->inxobj) {
// we are inside an XObject template
$this->xobjects[$this->xobjid]['fonts'][$fontkey] = $this->numfonts;
}
if (isset($diff) AND (!empty($diff))) {
//Search existing encodings
$d = 0;
$nb = count($this->diffs);
for ($i=1; $i <= $nb; ++$i) {
if ($this->diffs[$i] == $diff) {
$d = $i;
break;
}
}
if ($d == 0) {
$d = $nb + 1;
$this->diffs[$d] = $diff;
}
$this->setFontSubBuffer($fontkey, 'diff', $d);
}
if (!TCPDF_STATIC::empty_string($file)) {
if (!isset($this->FontFiles[$file])) {
if ((strcasecmp($type,'TrueType') == 0) OR (strcasecmp($type, 'TrueTypeUnicode') == 0)) {
$this->FontFiles[$file] = array('length1' => $originalsize, 'fontdir' => $fontdir, 'subset' => $subset, 'fontkeys' => array($fontkey));
} elseif ($type != 'core') {
$this->FontFiles[$file] = array('length1' => $size1, 'length2' => $size2, 'fontdir' => $fontdir, 'subset' => $subset, 'fontkeys' => array($fontkey));
}
} else {
// update fontkeys that are sharing this font file
$this->FontFiles[$file]['subset'] = ($this->FontFiles[$file]['subset'] AND $subset);
if (!in_array($fontkey, $this->FontFiles[$file]['fontkeys'])) {
$this->FontFiles[$file]['fontkeys'][] = $fontkey;
}
}
}
return $fontdata;
}
/**
* Sets the font used to print character strings.
* The font can be either a standard one or a font added via the AddFont() method. Standard fonts use Windows encoding cp1252 (Western Europe).
* The method can be called before the first page is created and the font is retained from page to page.
* If you just wish to change the current font size, it is simpler to call SetFontSize().
* Note: for the standard fonts, the font metric files must be accessible. There are three possibilities for this:
They are in the current directory (the one where the running script lies)
They are in one of the directories defined by the include_path parameter
They are in the directory defined by the K_PATH_FONTS constant
* @param $family (string) Family font. It can be either a name defined by AddFont() or one of the standard Type1 families (case insensitive):
times (Times-Roman)
timesb (Times-Bold)
timesi (Times-Italic)
timesbi (Times-BoldItalic)
helvetica (Helvetica)
helveticab (Helvetica-Bold)
helveticai (Helvetica-Oblique)
helveticabi (Helvetica-BoldOblique)
courier (Courier)
courierb (Courier-Bold)
courieri (Courier-Oblique)
courierbi (Courier-BoldOblique)
symbol (Symbol)
zapfdingbats (ZapfDingbats)
It is also possible to pass an empty string. In that case, the current family is retained.
* @param $style (string) Font style. Possible values are (case insensitive):
empty string: regular
B: bold
I: italic
U: underline
D: line trough
O: overline
or any combination. The default value is regular. Bold and italic styles do not apply to Symbol and ZapfDingbats basic fonts or other fonts when not defined.
* @param $size (float) Font size in points. The default value is the current size. If no size has been specified since the beginning of the document, the value taken is 12
* @param $fontfile (string) The font definition file. By default, the name is built from the family and style, in lower case with no spaces.
* @param $subset (mixed) if true embedd only a subset of the font (stores only the information related to the used characters); if false embedd full font; if 'default' uses the default value set using setFontSubsetting(). This option is valid only for TrueTypeUnicode fonts. If you want to enable users to change the document, set this parameter to false. If you subset the font, the person who receives your PDF would need to have your same font in order to make changes to your PDF. The file size of the PDF would also be smaller because you are embedding only part of a font.
* @param $out (boolean) if true output the font size command, otherwise only set the font properties.
* @author Nicola Asuni
* @public
* @since 1.0
* @see AddFont(), SetFontSize()
*/
public function SetFont($family, $style='', $size=null, $fontfile='', $subset='default', $out=true) {
//Select a font; size given in points
if ($size === null) {
$size = $this->FontSizePt;
}
if ($size < 0) {
$size = 0;
}
// try to add font (if not already added)
$fontdata = $this->AddFont($family, $style, $fontfile, $subset);
$this->FontFamily = $fontdata['family'];
$this->FontStyle = $fontdata['style'];
if (isset($this->CurrentFont['fontkey']) AND isset($this->CurrentFont['subsetchars'])) {
// save subset chars of the previous font
$this->setFontSubBuffer($this->CurrentFont['fontkey'], 'subsetchars', $this->CurrentFont['subsetchars']);
}
$this->CurrentFont = $this->getFontBuffer($fontdata['fontkey']);
$this->SetFontSize($size, $out);
}
/**
* Defines the size of the current font.
* @param $size (float) The font size in points.
* @param $out (boolean) if true output the font size command, otherwise only set the font properties.
* @public
* @since 1.0
* @see SetFont()
*/
public function SetFontSize($size, $out=true) {
// font size in points
$this->FontSizePt = $size;
// font size in user units
$this->FontSize = $size / $this->k;
// calculate some font metrics
if (isset($this->CurrentFont['desc']['FontBBox'])) {
$bbox = explode(' ', substr($this->CurrentFont['desc']['FontBBox'], 1, -1));
$font_height = ((intval($bbox[3]) - intval($bbox[1])) * $size / 1000);
} else {
$font_height = $size * 1.219;
}
if (isset($this->CurrentFont['desc']['Ascent']) AND ($this->CurrentFont['desc']['Ascent'] > 0)) {
$font_ascent = ($this->CurrentFont['desc']['Ascent'] * $size / 1000);
}
if (isset($this->CurrentFont['desc']['Descent']) AND ($this->CurrentFont['desc']['Descent'] <= 0)) {
$font_descent = (- $this->CurrentFont['desc']['Descent'] * $size / 1000);
}
if (!isset($font_ascent) AND !isset($font_descent)) {
// core font
$font_ascent = 0.76 * $font_height;
$font_descent = $font_height - $font_ascent;
} elseif (!isset($font_descent)) {
$font_descent = $font_height - $font_ascent;
} elseif (!isset($font_ascent)) {
$font_ascent = $font_height - $font_descent;
}
$this->FontAscent = ($font_ascent / $this->k);
$this->FontDescent = ($font_descent / $this->k);
if ($out AND ($this->page > 0) AND (isset($this->CurrentFont['i'])) AND ($this->state == 2)) {
$this->_out(sprintf('BT /F%d %F Tf ET', $this->CurrentFont['i'], $this->FontSizePt));
}
}
/**
* Returns the bounding box of the current font in user units.
* @return array
* @public
* @since 5.9.152 (2012-03-23)
*/
public function getFontBBox() {
$fbbox = array();
if (isset($this->CurrentFont['desc']['FontBBox'])) {
$tmpbbox = explode(' ', substr($this->CurrentFont['desc']['FontBBox'], 1, -1));
$fbbox = array_map(array($this,'getAbsFontMeasure'), $tmpbbox);
} else {
// Find max width
if (isset($this->CurrentFont['desc']['MaxWidth'])) {
$maxw = $this->getAbsFontMeasure(intval($this->CurrentFont['desc']['MaxWidth']));
} else {
$maxw = 0;
if (isset($this->CurrentFont['desc']['MissingWidth'])) {
$maxw = max($maxw, $this->CurrentFont['desc']['MissingWidth']);
}
if (isset($this->CurrentFont['desc']['AvgWidth'])) {
$maxw = max($maxw, $this->CurrentFont['desc']['AvgWidth']);
}
if (isset($this->CurrentFont['dw'])) {
$maxw = max($maxw, $this->CurrentFont['dw']);
}
foreach ($this->CurrentFont['cw'] as $char => $w) {
$maxw = max($maxw, $w);
}
if ($maxw == 0) {
$maxw = 600;
}
$maxw = $this->getAbsFontMeasure($maxw);
}
$fbbox = array(0, (0 - $this->FontDescent), $maxw, $this->FontAscent);
}
return $fbbox;
}
/**
* Convert a relative font measure into absolute value.
* @param $s (int) Font measure.
* @return float Absolute measure.
* @since 5.9.186 (2012-09-13)
*/
public function getAbsFontMeasure($s) {
return ($s * $this->FontSize / 1000);
}
/**
* Returns the glyph bounding box of the specified character in the current font in user units.
* @param $char (int) Input character code.
* @return mixed array(xMin, yMin, xMax, yMax) or FALSE if not defined.
* @since 5.9.186 (2012-09-13)
*/
public function getCharBBox($char) {
if (isset($this->CurrentFont['cbbox'][$char])) {
return array_map(array($this,'getAbsFontMeasure'), $this->CurrentFont['cbbox'][intval($char)]);
}
return false;
}
/**
* Return the font descent value
* @param $font (string) font name
* @param $style (string) font style
* @param $size (float) The size (in points)
* @return int font descent
* @public
* @author Nicola Asuni
* @since 4.9.003 (2010-03-30)
*/
public function getFontDescent($font, $style='', $size=0) {
$fontdata = $this->AddFont($font, $style);
$fontinfo = $this->getFontBuffer($fontdata['fontkey']);
if (isset($fontinfo['desc']['Descent']) AND ($fontinfo['desc']['Descent'] <= 0)) {
$descent = (- $fontinfo['desc']['Descent'] * $size / 1000);
} else {
$descent = (1.219 * 0.24 * $size);
}
return ($descent / $this->k);
}
/**
* Return the font ascent value.
* @param $font (string) font name
* @param $style (string) font style
* @param $size (float) The size (in points)
* @return int font ascent
* @public
* @author Nicola Asuni
* @since 4.9.003 (2010-03-30)
*/
public function getFontAscent($font, $style='', $size=0) {
$fontdata = $this->AddFont($font, $style);
$fontinfo = $this->getFontBuffer($fontdata['fontkey']);
if (isset($fontinfo['desc']['Ascent']) AND ($fontinfo['desc']['Ascent'] > 0)) {
$ascent = ($fontinfo['desc']['Ascent'] * $size / 1000);
} else {
$ascent = 1.219 * 0.76 * $size;
}
return ($ascent / $this->k);
}
/**
* Return true in the character is present in the specified font.
* @param $char (mixed) Character to check (integer value or string)
* @param $font (string) Font name (family name).
* @param $style (string) Font style.
* @return (boolean) true if the char is defined, false otherwise.
* @public
* @since 5.9.153 (2012-03-28)
*/
public function isCharDefined($char, $font='', $style='') {
if (is_string($char)) {
// get character code
$char = TCPDF_FONTS::UTF8StringToArray($char, $this->isunicode, $this->CurrentFont);
$char = $char[0];
}
if (TCPDF_STATIC::empty_string($font)) {
if (TCPDF_STATIC::empty_string($style)) {
return (isset($this->CurrentFont['cw'][intval($char)]));
}
$font = $this->FontFamily;
}
$fontdata = $this->AddFont($font, $style);
$fontinfo = $this->getFontBuffer($fontdata['fontkey']);
return (isset($fontinfo['cw'][intval($char)]));
}
/**
* Replace missing font characters on selected font with specified substitutions.
* @param $text (string) Text to process.
* @param $font (string) Font name (family name).
* @param $style (string) Font style.
* @param $subs (array) Array of possible character substitutions. The key is the character to check (integer value) and the value is a single intege value or an array of possible substitutes.
* @return (string) Processed text.
* @public
* @since 5.9.153 (2012-03-28)
*/
public function replaceMissingChars($text, $font='', $style='', $subs=array()) {
if (empty($subs)) {
return $text;
}
if (TCPDF_STATIC::empty_string($font)) {
$font = $this->FontFamily;
}
$fontdata = $this->AddFont($font, $style);
$fontinfo = $this->getFontBuffer($fontdata['fontkey']);
$uniarr = TCPDF_FONTS::UTF8StringToArray($text, $this->isunicode, $this->CurrentFont);
foreach ($uniarr as $k => $chr) {
if (!isset($fontinfo['cw'][$chr])) {
// this character is missing on the selected font
if (isset($subs[$chr])) {
// we have available substitutions
if (is_array($subs[$chr])) {
foreach($subs[$chr] as $s) {
if (isset($fontinfo['cw'][$s])) {
$uniarr[$k] = $s;
break;
}
}
} elseif (isset($fontinfo['cw'][$subs[$chr]])) {
$uniarr[$k] = $subs[$chr];
}
}
}
}
return TCPDF_FONTS::UniArrSubString(TCPDF_FONTS::UTF8ArrayToUniArray($uniarr, $this->isunicode));
}
/**
* Defines the default monospaced font.
* @param $font (string) Font name.
* @public
* @since 4.5.025
*/
public function SetDefaultMonospacedFont($font) {
$this->default_monospaced_font = $font;
}
/**
* Creates a new internal link and returns its identifier. An internal link is a clickable area which directs to another place within the document.
* The identifier can then be passed to Cell(), Write(), Image() or Link(). The destination is defined with SetLink().
* @public
* @since 1.5
* @see Cell(), Write(), Image(), Link(), SetLink()
*/
public function AddLink() {
//Create a new internal link
$n = count($this->links) + 1;
$this->links[$n] = array(0, 0);
return $n;
}
/**
* Defines the page and position a link points to.
* @param $link (int) The link identifier returned by AddLink()
* @param $y (float) Ordinate of target position; -1 indicates the current position. The default value is 0 (top of page)
* @param $page (int) Number of target page; -1 indicates the current page. This is the default value
* @public
* @since 1.5
* @see AddLink()
*/
public function SetLink($link, $y=0, $page=-1) {
if ($y == -1) {
$y = $this->y;
}
if ($page == -1) {
$page = $this->page;
}
$this->links[$link] = array($page, $y);
}
/**
* Puts a link on a rectangular area of the page.
* Text or image links are generally put via Cell(), Write() or Image(), but this method can be useful for instance to define a clickable area inside an image.
* @param $x (float) Abscissa of the upper-left corner of the rectangle
* @param $y (float) Ordinate of the upper-left corner of the rectangle
* @param $w (float) Width of the rectangle
* @param $h (float) Height of the rectangle
* @param $link (mixed) URL or identifier returned by AddLink()
* @param $spaces (int) number of spaces on the text to link
* @public
* @since 1.5
* @see AddLink(), Annotation(), Cell(), Write(), Image()
*/
public function Link($x, $y, $w, $h, $link, $spaces=0) {
$this->Annotation($x, $y, $w, $h, $link, array('Subtype'=>'Link'), $spaces);
}
/**
* Puts a markup annotation on a rectangular area of the page.
* !!!!THE ANNOTATION SUPPORT IS NOT YET FULLY IMPLEMENTED !!!!
* @param $x (float) Abscissa of the upper-left corner of the rectangle
* @param $y (float) Ordinate of the upper-left corner of the rectangle
* @param $w (float) Width of the rectangle
* @param $h (float) Height of the rectangle
* @param $text (string) annotation text or alternate content
* @param $opt (array) array of options (see section 8.4 of PDF reference 1.7).
* @param $spaces (int) number of spaces on the text to link
* @public
* @since 4.0.018 (2008-08-06)
*/
public function Annotation($x, $y, $w, $h, $text, $opt=array('Subtype'=>'Text'), $spaces=0) {
if ($this->inxobj) {
// store parameters for later use on template
$this->xobjects[$this->xobjid]['annotations'][] = array('x' => $x, 'y' => $y, 'w' => $w, 'h' => $h, 'text' => $text, 'opt' => $opt, 'spaces' => $spaces);
return;
}
if ($x === '') {
$x = $this->x;
}
if ($y === '') {
$y = $this->y;
}
// check page for no-write regions and adapt page margins if necessary
list($x, $y) = $this->checkPageRegions($h, $x, $y);
// recalculate coordinates to account for graphic transformations
if (isset($this->transfmatrix) AND !empty($this->transfmatrix)) {
for ($i=$this->transfmatrix_key; $i > 0; --$i) {
$maxid = count($this->transfmatrix[$i]) - 1;
for ($j=$maxid; $j >= 0; --$j) {
$ctm = $this->transfmatrix[$i][$j];
if (isset($ctm['a'])) {
$x = $x * $this->k;
$y = ($this->h - $y) * $this->k;
$w = $w * $this->k;
$h = $h * $this->k;
// top left
$xt = $x;
$yt = $y;
$x1 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
$y1 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
// top right
$xt = $x + $w;
$yt = $y;
$x2 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
$y2 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
// bottom left
$xt = $x;
$yt = $y - $h;
$x3 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
$y3 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
// bottom right
$xt = $x + $w;
$yt = $y - $h;
$x4 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
$y4 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
// new coordinates (rectangle area)
$x = min($x1, $x2, $x3, $x4);
$y = max($y1, $y2, $y3, $y4);
$w = (max($x1, $x2, $x3, $x4) - $x) / $this->k;
$h = ($y - min($y1, $y2, $y3, $y4)) / $this->k;
$x = $x / $this->k;
$y = $this->h - ($y / $this->k);
}
}
}
}
if ($this->page <= 0) {
$page = 1;
} else {
$page = $this->page;
}
if (!isset($this->PageAnnots[$page])) {
$this->PageAnnots[$page] = array();
}
$this->PageAnnots[$page][] = array('n' => ++$this->n, 'x' => $x, 'y' => $y, 'w' => $w, 'h' => $h, 'txt' => $text, 'opt' => $opt, 'numspaces' => $spaces);
if (!$this->pdfa_mode) {
if ((($opt['Subtype'] == 'FileAttachment') OR ($opt['Subtype'] == 'Sound')) AND (!TCPDF_STATIC::empty_string($opt['FS']))
AND (file_exists($opt['FS']) OR TCPDF_STATIC::isValidURL($opt['FS']))
AND (!isset($this->embeddedfiles[basename($opt['FS'])]))) {
$this->embeddedfiles[basename($opt['FS'])] = array('f' => ++$this->n, 'n' => ++$this->n, 'file' => $opt['FS']);
}
}
// Add widgets annotation's icons
if (isset($opt['mk']['i']) AND file_exists($opt['mk']['i'])) {
$this->Image($opt['mk']['i'], '', '', 10, 10, '', '', '', false, 300, '', false, false, 0, false, true);
}
if (isset($opt['mk']['ri']) AND file_exists($opt['mk']['ri'])) {
$this->Image($opt['mk']['ri'], '', '', 0, 0, '', '', '', false, 300, '', false, false, 0, false, true);
}
if (isset($opt['mk']['ix']) AND file_exists($opt['mk']['ix'])) {
$this->Image($opt['mk']['ix'], '', '', 0, 0, '', '', '', false, 300, '', false, false, 0, false, true);
}
}
/**
* Embedd the attached files.
* @since 4.4.000 (2008-12-07)
* @protected
* @see Annotation()
*/
protected function _putEmbeddedFiles() {
if ($this->pdfa_mode) {
// embedded files are not allowed in PDF/A mode
return;
}
reset($this->embeddedfiles);
foreach ($this->embeddedfiles as $filename => $filedata) {
// update name tree
$this->efnames[$filename] = $filedata['f'].' 0 R';
// embedded file specification object
$out = $this->_getobj($filedata['f'])."\n";
$out .= '<_datastring($filename, $filedata['f']).' /EF <> >>';
$out .= "\n".'endobj';
$this->_out($out);
// embedded file object
$data = file_get_contents($filedata['file']);
$filter = '';
$rawsize = strlen($data);
if ($this->compress) {
$data = gzcompress($data);
$filter = ' /Filter /FlateDecode';
}
$stream = $this->_getrawstream($data, $filedata['n']);
$out = $this->_getobj($filedata['n'])."\n";
$out .= '<< /Type /EmbeddedFile'.$filter.' /Length '.strlen($stream).' /Params <> >>';
$out .= ' stream'."\n".$stream."\n".'endstream';
$out .= "\n".'endobj';
$this->_out($out);
}
}
/**
* Prints a text cell at the specified position.
* This method allows to place a string precisely on the page.
* @param $x (float) Abscissa of the cell origin
* @param $y (float) Ordinate of the cell origin
* @param $txt (string) String to print
* @param $fstroke (int) outline size in user units (false = disable)
* @param $fclip (boolean) if true activate clipping mode (you must call StartTransform() before this function and StopTransform() to stop the clipping tranformation).
* @param $ffill (boolean) if true fills the text
* @param $border (mixed) Indicates if borders must be drawn around the cell. The value can be a number:
0: no border (default)
1: frame
or a string containing some or all of the following characters (in any order):
L: left
T: top
R: right
B: bottom
or an array of line styles for each border group - for example: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
* @param $ln (int) Indicates where the current position should go after the call. Possible values are:
0: to the right (or left for RTL languages)
1: to the beginning of the next line
2: below
Putting 1 is equivalent to putting 0 and calling Ln() just after. Default value: 0.
* @param $align (string) Allows to center or align the text. Possible values are:
L or empty string: left align (default value)
C: center
R: right align
J: justify
* @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
* @param $link (mixed) URL or identifier returned by AddLink().
* @param $stretch (int) font stretch mode:
0 = disabled
1 = horizontal scaling only if text is larger than cell width
2 = forced horizontal scaling to fit cell width
3 = character spacing only if text is larger than cell width
4 = forced character spacing to fit cell width
General font stretching and scaling values will be preserved when possible.
* @param $ignore_min_height (boolean) if true ignore automatic minimum height value.
* @param $calign (string) cell vertical alignment relative to the specified Y value. Possible values are:
T : cell top
A : font top
L : font baseline
D : font bottom
B : cell bottom
* @param $valign (string) text vertical alignment inside the cell. Possible values are:
T : top
C : center
B : bottom
* @param $rtloff (boolean) if true uses the page top-left corner as origin of axis for $x and $y initial position.
* @public
* @since 1.0
* @see Cell(), Write(), MultiCell(), WriteHTML(), WriteHTMLCell()
*/
public function Text($x, $y, $txt, $fstroke=false, $fclip=false, $ffill=true, $border=0, $ln=0, $align='', $fill=false, $link='', $stretch=0, $ignore_min_height=false, $calign='T', $valign='M', $rtloff=false) {
$textrendermode = $this->textrendermode;
$textstrokewidth = $this->textstrokewidth;
$this->setTextRenderingMode($fstroke, $ffill, $fclip);
$this->SetXY($x, $y, $rtloff);
$this->Cell(0, 0, $txt, $border, $ln, $align, $fill, $link, $stretch, $ignore_min_height, $calign, $valign);
// restore previous rendering mode
$this->textrendermode = $textrendermode;
$this->textstrokewidth = $textstrokewidth;
}
/**
* Whenever a page break condition is met, the method is called, and the break is issued or not depending on the returned value.
* The default implementation returns a value according to the mode selected by SetAutoPageBreak().
* This method is called automatically and should not be called directly by the application.
* @return boolean
* @public
* @since 1.4
* @see SetAutoPageBreak()
*/
public function AcceptPageBreak() {
if ($this->num_columns > 1) {
// multi column mode
if ($this->current_column < ($this->num_columns - 1)) {
// go to next column
$this->selectColumn($this->current_column + 1);
} elseif ($this->AutoPageBreak) {
// add a new page
$this->AddPage();
// set first column
$this->selectColumn(0);
}
// avoid page breaking from checkPageBreak()
return false;
}
return $this->AutoPageBreak;
}
/**
* Add page if needed.
* @param $h (float) Cell height. Default value: 0.
* @param $y (mixed) starting y position, leave empty for current position.
* @param $addpage (boolean) if true add a page, otherwise only return the true/false state
* @return boolean true in case of page break, false otherwise.
* @since 3.2.000 (2008-07-01)
* @protected
*/
protected function checkPageBreak($h=0, $y='', $addpage=true) {
if (TCPDF_STATIC::empty_string($y)) {
$y = $this->y;
}
$current_page = $this->page;
if ((($y + $h) > $this->PageBreakTrigger) AND ($this->inPageBody()) AND ($this->AcceptPageBreak())) {
if ($addpage) {
//Automatic page break
$x = $this->x;
$this->AddPage($this->CurOrientation);
$this->y = $this->tMargin;
$oldpage = $this->page - 1;
if ($this->rtl) {
if ($this->pagedim[$this->page]['orm'] != $this->pagedim[$oldpage]['orm']) {
$this->x = $x - ($this->pagedim[$this->page]['orm'] - $this->pagedim[$oldpage]['orm']);
} else {
$this->x = $x;
}
} else {
if ($this->pagedim[$this->page]['olm'] != $this->pagedim[$oldpage]['olm']) {
$this->x = $x + ($this->pagedim[$this->page]['olm'] - $this->pagedim[$oldpage]['olm']);
} else {
$this->x = $x;
}
}
}
return true;
}
if ($current_page != $this->page) {
// account for columns mode
return true;
}
return false;
}
/**
* Prints a cell (rectangular area) with optional borders, background color and character string. The upper-left corner of the cell corresponds to the current position. The text can be aligned or centered. After the call, the current position moves to the right or to the next line. It is possible to put a link on the text.
* If automatic page breaking is enabled and the cell goes beyond the limit, a page break is done before outputting.
* @param $w (float) Cell width. If 0, the cell extends up to the right margin.
* @param $h (float) Cell height. Default value: 0.
* @param $txt (string) String to print. Default value: empty string.
* @param $border (mixed) Indicates if borders must be drawn around the cell. The value can be a number:
0: no border (default)
1: frame
or a string containing some or all of the following characters (in any order):
L: left
T: top
R: right
B: bottom
or an array of line styles for each border group - for example: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
* @param $ln (int) Indicates where the current position should go after the call. Possible values are:
0: to the right (or left for RTL languages)
1: to the beginning of the next line
2: below
Putting 1 is equivalent to putting 0 and calling Ln() just after. Default value: 0.
* @param $align (string) Allows to center or align the text. Possible values are:
L or empty string: left align (default value)
C: center
R: right align
J: justify
* @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
* @param $link (mixed) URL or identifier returned by AddLink().
* @param $stretch (int) font stretch mode:
0 = disabled
1 = horizontal scaling only if text is larger than cell width
2 = forced horizontal scaling to fit cell width
3 = character spacing only if text is larger than cell width
4 = forced character spacing to fit cell width
General font stretching and scaling values will be preserved when possible.
* @param $ignore_min_height (boolean) if true ignore automatic minimum height value.
* @param $calign (string) cell vertical alignment relative to the specified Y value. Possible values are:
T : cell top
C : center
B : cell bottom
A : font top
L : font baseline
D : font bottom
* @param $valign (string) text vertical alignment inside the cell. Possible values are:
T : top
C : center
B : bottom
* @public
* @since 1.0
* @see SetFont(), SetDrawColor(), SetFillColor(), SetTextColor(), SetLineWidth(), AddLink(), Ln(), MultiCell(), Write(), SetAutoPageBreak()
*/
public function Cell($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link='', $stretch=0, $ignore_min_height=false, $calign='T', $valign='M') {
$prev_cell_margin = $this->cell_margin;
$prev_cell_padding = $this->cell_padding;
$this->adjustCellPadding($border);
if (!$ignore_min_height) {
$min_cell_height = ($this->FontSize * $this->cell_height_ratio) + $this->cell_padding['T'] + $this->cell_padding['B'];
if ($h < $min_cell_height) {
$h = $min_cell_height;
}
}
$this->checkPageBreak($h + $this->cell_margin['T'] + $this->cell_margin['B']);
// apply text shadow if enabled
if ($this->txtshadow['enabled']) {
// save data
$x = $this->x;
$y = $this->y;
$bc = $this->bgcolor;
$fc = $this->fgcolor;
$sc = $this->strokecolor;
$alpha = $this->alpha;
// print shadow
$this->x += $this->txtshadow['depth_w'];
$this->y += $this->txtshadow['depth_h'];
$this->SetFillColorArray($this->txtshadow['color']);
$this->SetTextColorArray($this->txtshadow['color']);
$this->SetDrawColorArray($this->txtshadow['color']);
if ($this->txtshadow['opacity'] != $alpha['CA']) {
$this->setAlpha($this->txtshadow['opacity'], $this->txtshadow['blend_mode']);
}
if ($this->state == 2) {
$this->_out($this->getCellCode($w, $h, $txt, $border, $ln, $align, $fill, $link, $stretch, true, $calign, $valign));
}
//restore data
$this->x = $x;
$this->y = $y;
$this->SetFillColorArray($bc);
$this->SetTextColorArray($fc);
$this->SetDrawColorArray($sc);
if ($this->txtshadow['opacity'] != $alpha['CA']) {
$this->setAlpha($alpha['CA'], $alpha['BM'], $alpha['ca'], $alpha['AIS']);
}
}
if ($this->state == 2) {
$this->_out($this->getCellCode($w, $h, $txt, $border, $ln, $align, $fill, $link, $stretch, true, $calign, $valign));
}
$this->cell_padding = $prev_cell_padding;
$this->cell_margin = $prev_cell_margin;
}
/**
* Returns the PDF string code to print a cell (rectangular area) with optional borders, background color and character string. The upper-left corner of the cell corresponds to the current position. The text can be aligned or centered. After the call, the current position moves to the right or to the next line. It is possible to put a link on the text.
* If automatic page breaking is enabled and the cell goes beyond the limit, a page break is done before outputting.
* @param $w (float) Cell width. If 0, the cell extends up to the right margin.
* @param $h (float) Cell height. Default value: 0.
* @param $txt (string) String to print. Default value: empty string.
* @param $border (mixed) Indicates if borders must be drawn around the cell. The value can be a number:
0: no border (default)
1: frame
or a string containing some or all of the following characters (in any order):
L: left
T: top
R: right
B: bottom
or an array of line styles for each border group - for example: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
* @param $ln (int) Indicates where the current position should go after the call. Possible values are:
0: to the right (or left for RTL languages)
1: to the beginning of the next line
2: below
Putting 1 is equivalent to putting 0 and calling Ln() just after. Default value: 0.
* @param $align (string) Allows to center or align the text. Possible values are:
L or empty string: left align (default value)
C: center
R: right align
J: justify
* @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
* @param $link (mixed) URL or identifier returned by AddLink().
* @param $stretch (int) font stretch mode:
0 = disabled
1 = horizontal scaling only if text is larger than cell width
2 = forced horizontal scaling to fit cell width
3 = character spacing only if text is larger than cell width
4 = forced character spacing to fit cell width
General font stretching and scaling values will be preserved when possible.
* @param $ignore_min_height (boolean) if true ignore automatic minimum height value.
* @param $calign (string) cell vertical alignment relative to the specified Y value. Possible values are:
T : cell top
C : center
B : cell bottom
A : font top
L : font baseline
D : font bottom
* @param $valign (string) text vertical alignment inside the cell. Possible values are:
T : top
M : middle
B : bottom
* @return string containing cell code
* @protected
* @since 1.0
* @see Cell()
*/
protected function getCellCode($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link='', $stretch=0, $ignore_min_height=false, $calign='T', $valign='M') {
// replace 'NO-BREAK SPACE' (U+00A0) character with a simple space
$txt = str_replace(TCPDF_FONTS::unichr(160, $this->isunicode), ' ', $txt);
$prev_cell_margin = $this->cell_margin;
$prev_cell_padding = $this->cell_padding;
$txt = TCPDF_STATIC::removeSHY($txt, $this->isunicode);
$rs = ''; //string to be returned
$this->adjustCellPadding($border);
if (!$ignore_min_height) {
$min_cell_height = ($this->FontSize * $this->cell_height_ratio) + $this->cell_padding['T'] + $this->cell_padding['B'];
if ($h < $min_cell_height) {
$h = $min_cell_height;
}
}
$k = $this->k;
// check page for no-write regions and adapt page margins if necessary
list($this->x, $this->y) = $this->checkPageRegions($h, $this->x, $this->y);
if ($this->rtl) {
$x = $this->x - $this->cell_margin['R'];
} else {
$x = $this->x + $this->cell_margin['L'];
}
$y = $this->y + $this->cell_margin['T'];
$prev_font_stretching = $this->font_stretching;
$prev_font_spacing = $this->font_spacing;
// cell vertical alignment
switch ($calign) {
case 'A': {
// font top
switch ($valign) {
case 'T': {
// top
$y -= $this->cell_padding['T'];
break;
}
case 'B': {
// bottom
$y -= ($h - $this->cell_padding['B'] - $this->FontAscent - $this->FontDescent);
break;
}
default:
case 'C':
case 'M': {
// center
$y -= (($h - $this->FontAscent - $this->FontDescent) / 2);
break;
}
}
break;
}
case 'L': {
// font baseline
switch ($valign) {
case 'T': {
// top
$y -= ($this->cell_padding['T'] + $this->FontAscent);
break;
}
case 'B': {
// bottom
$y -= ($h - $this->cell_padding['B'] - $this->FontDescent);
break;
}
default:
case 'C':
case 'M': {
// center
$y -= (($h + $this->FontAscent - $this->FontDescent) / 2);
break;
}
}
break;
}
case 'D': {
// font bottom
switch ($valign) {
case 'T': {
// top
$y -= ($this->cell_padding['T'] + $this->FontAscent + $this->FontDescent);
break;
}
case 'B': {
// bottom
$y -= ($h - $this->cell_padding['B']);
break;
}
default:
case 'C':
case 'M': {
// center
$y -= (($h + $this->FontAscent + $this->FontDescent) / 2);
break;
}
}
break;
}
case 'B': {
// cell bottom
$y -= $h;
break;
}
case 'C':
case 'M': {
// cell center
$y -= ($h / 2);
break;
}
default:
case 'T': {
// cell top
break;
}
}
// text vertical alignment
switch ($valign) {
case 'T': {
// top
$yt = $y + $this->cell_padding['T'];
break;
}
case 'B': {
// bottom
$yt = $y + $h - $this->cell_padding['B'] - $this->FontAscent - $this->FontDescent;
break;
}
default:
case 'C':
case 'M': {
// center
$yt = $y + (($h - $this->FontAscent - $this->FontDescent) / 2);
break;
}
}
$basefonty = $yt + $this->FontAscent;
if (TCPDF_STATIC::empty_string($w) OR ($w <= 0)) {
if ($this->rtl) {
$w = $x - $this->lMargin;
} else {
$w = $this->w - $this->rMargin - $x;
}
}
$s = '';
// fill and borders
if (is_string($border) AND (strlen($border) == 4)) {
// full border
$border = 1;
}
if ($fill OR ($border == 1)) {
if ($fill) {
$op = ($border == 1) ? 'B' : 'f';
} else {
$op = 'S';
}
if ($this->rtl) {
$xk = (($x - $w) * $k);
} else {
$xk = ($x * $k);
}
$s .= sprintf('%F %F %F %F re %s ', $xk, (($this->h - $y) * $k), ($w * $k), (-$h * $k), $op);
}
// draw borders
$s .= $this->getCellBorder($x, $y, $w, $h, $border);
if ($txt != '') {
$txt2 = $txt;
if ($this->isunicode) {
if (($this->CurrentFont['type'] == 'core') OR ($this->CurrentFont['type'] == 'TrueType') OR ($this->CurrentFont['type'] == 'Type1')) {
$txt2 = TCPDF_FONTS::UTF8ToLatin1($txt2, $this->isunicode, $this->CurrentFont);
} else {
$unicode = TCPDF_FONTS::UTF8StringToArray($txt, $this->isunicode, $this->CurrentFont); // array of UTF-8 unicode values
$unicode = TCPDF_FONTS::utf8Bidi($unicode, '', $this->tmprtl, $this->isunicode, $this->CurrentFont);
// replace thai chars (if any)
if (defined('K_THAI_TOPCHARS') AND (K_THAI_TOPCHARS == true)) {
// number of chars
$numchars = count($unicode);
// po pla, for far, for fan
$longtail = array(0x0e1b, 0x0e1d, 0x0e1f);
// do chada, to patak
$lowtail = array(0x0e0e, 0x0e0f);
// mai hun arkad, sara i, sara ii, sara ue, sara uee
$upvowel = array(0x0e31, 0x0e34, 0x0e35, 0x0e36, 0x0e37);
// mai ek, mai tho, mai tri, mai chattawa, karan
$tonemark = array(0x0e48, 0x0e49, 0x0e4a, 0x0e4b, 0x0e4c);
// sara u, sara uu, pinthu
$lowvowel = array(0x0e38, 0x0e39, 0x0e3a);
$output = array();
for ($i = 0; $i < $numchars; $i++) {
if (($unicode[$i] >= 0x0e00) && ($unicode[$i] <= 0x0e5b)) {
$ch0 = $unicode[$i];
$ch1 = ($i > 0) ? $unicode[($i - 1)] : 0;
$ch2 = ($i > 1) ? $unicode[($i - 2)] : 0;
$chn = ($i < ($numchars - 1)) ? $unicode[($i + 1)] : 0;
if (in_array($ch0, $tonemark)) {
if ($chn == 0x0e33) {
// sara um
if (in_array($ch1, $longtail)) {
// tonemark at upper left
$output[] = $this->replaceChar($ch0, (0xf713 + $ch0 - 0x0e48));
} else {
// tonemark at upper right (normal position)
$output[] = $ch0;
}
} elseif (in_array($ch1, $longtail) OR (in_array($ch2, $longtail) AND in_array($ch1, $lowvowel))) {
// tonemark at lower left
$output[] = $this->replaceChar($ch0, (0xf705 + $ch0 - 0x0e48));
} elseif (in_array($ch1, $upvowel)) {
if (in_array($ch2, $longtail)) {
// tonemark at upper left
$output[] = $this->replaceChar($ch0, (0xf713 + $ch0 - 0x0e48));
} else {
// tonemark at upper right (normal position)
$output[] = $ch0;
}
} else {
// tonemark at lower right
$output[] = $this->replaceChar($ch0, (0xf70a + $ch0 - 0x0e48));
}
} elseif (($ch0 == 0x0e33) AND (in_array($ch1, $longtail) OR (in_array($ch2, $longtail) AND in_array($ch1, $tonemark)))) {
// add lower left nikhahit and sara aa
if ($this->isCharDefined(0xf711) AND $this->isCharDefined(0x0e32)) {
$output[] = 0xf711;
$this->CurrentFont['subsetchars'][0xf711] = true;
$output[] = 0x0e32;
$this->CurrentFont['subsetchars'][0x0e32] = true;
} else {
$output[] = $ch0;
}
} elseif (in_array($ch1, $longtail)) {
if ($ch0 == 0x0e31) {
// lower left mai hun arkad
$output[] = $this->replaceChar($ch0, 0xf710);
} elseif (in_array($ch0, $upvowel)) {
// lower left
$output[] = $this->replaceChar($ch0, (0xf701 + $ch0 - 0x0e34));
} elseif ($ch0 == 0x0e47) {
// lower left mai tai koo
$output[] = $this->replaceChar($ch0, 0xf712);
} else {
// normal character
$output[] = $ch0;
}
} elseif (in_array($ch1, $lowtail) AND in_array($ch0, $lowvowel)) {
// lower vowel
$output[] = $this->replaceChar($ch0, (0xf718 + $ch0 - 0x0e38));
} elseif (($ch0 == 0x0e0d) AND in_array($chn, $lowvowel)) {
// yo ying without lower part
$output[] = $this->replaceChar($ch0, 0xf70f);
} elseif (($ch0 == 0x0e10) AND in_array($chn, $lowvowel)) {
// tho santan without lower part
$output[] = $this->replaceChar($ch0, 0xf700);
} else {
$output[] = $ch0;
}
} else {
// non-thai character
$output[] = $unicode[$i];
}
}
$unicode = $output;
// update font subsetchars
$this->setFontSubBuffer($this->CurrentFont['fontkey'], 'subsetchars', $this->CurrentFont['subsetchars']);
} // end of K_THAI_TOPCHARS
$txt2 = TCPDF_FONTS::arrUTF8ToUTF16BE($unicode, false);
}
}
$txt2 = TCPDF_STATIC::_escape($txt2);
// get current text width (considering general font stretching and spacing)
$txwidth = $this->GetStringWidth($txt);
$width = $txwidth;
// check for stretch mode
if ($stretch > 0) {
// calculate ratio between cell width and text width
if ($width <= 0) {
$ratio = 1;
} else {
$ratio = (($w - $this->cell_padding['L'] - $this->cell_padding['R']) / $width);
}
// check if stretching is required
if (($ratio < 1) OR (($ratio > 1) AND (($stretch % 2) == 0))) {
// the text will be stretched to fit cell width
if ($stretch > 2) {
// set new character spacing
$this->font_spacing += ($w - $this->cell_padding['L'] - $this->cell_padding['R'] - $width) / (max(($this->GetNumChars($txt) - 1), 1) * ($this->font_stretching / 100));
} else {
// set new horizontal stretching
$this->font_stretching *= $ratio;
}
// recalculate text width (the text fills the entire cell)
$width = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
// reset alignment
$align = '';
}
}
if ($this->font_stretching != 100) {
// apply font stretching
$rs .= sprintf('BT %F Tz ET ', $this->font_stretching);
}
if ($this->font_spacing != 0) {
// increase/decrease font spacing
$rs .= sprintf('BT %F Tc ET ', ($this->font_spacing * $this->k));
}
if ($this->ColorFlag AND ($this->textrendermode < 4)) {
$s .= 'q '.$this->TextColor.' ';
}
// rendering mode
$s .= sprintf('BT %d Tr %F w ET ', $this->textrendermode, ($this->textstrokewidth * $this->k));
// count number of spaces
$ns = substr_count($txt, chr(32));
// Justification
$spacewidth = 0;
if (($align == 'J') AND ($ns > 0)) {
if ($this->isUnicodeFont()) {
// get string width without spaces
$width = $this->GetStringWidth(str_replace(' ', '', $txt));
// calculate average space width
$spacewidth = -1000 * ($w - $width - $this->cell_padding['L'] - $this->cell_padding['R']) / ($ns?$ns:1) / $this->FontSize;
if ($this->font_stretching != 100) {
// word spacing is affected by stretching
$spacewidth /= ($this->font_stretching / 100);
}
// set word position to be used with TJ operator
$txt2 = str_replace(chr(0).chr(32), ') '.sprintf('%F', $spacewidth).' (', $txt2);
$unicode_justification = true;
} else {
// get string width
$width = $txwidth;
// new space width
$spacewidth = (($w - $width - $this->cell_padding['L'] - $this->cell_padding['R']) / ($ns?$ns:1)) * $this->k;
if ($this->font_stretching != 100) {
// word spacing (Tw) is affected by stretching
$spacewidth /= ($this->font_stretching / 100);
}
// set word spacing
$rs .= sprintf('BT %F Tw ET ', $spacewidth);
}
$width = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
}
// replace carriage return characters
$txt2 = str_replace("\r", ' ', $txt2);
switch ($align) {
case 'C': {
$dx = ($w - $width) / 2;
break;
}
case 'R': {
if ($this->rtl) {
$dx = $this->cell_padding['R'];
} else {
$dx = $w - $width - $this->cell_padding['R'];
}
break;
}
case 'L': {
if ($this->rtl) {
$dx = $w - $width - $this->cell_padding['L'];
} else {
$dx = $this->cell_padding['L'];
}
break;
}
case 'J':
default: {
if ($this->rtl) {
$dx = $this->cell_padding['R'];
} else {
$dx = $this->cell_padding['L'];
}
break;
}
}
if ($this->rtl) {
$xdx = $x - $dx - $width;
} else {
$xdx = $x + $dx;
}
$xdk = $xdx * $k;
// print text
$s .= sprintf('BT %F %F Td [(%s)] TJ ET', $xdk, (($this->h - $basefonty) * $k), $txt2);
if (isset($uniblock)) {
// print overlapping characters as separate string
$xshift = 0; // horizontal shift
$ty = (($this->h - $basefonty + (0.2 * $this->FontSize)) * $k);
$spw = (($w - $txwidth - $this->cell_padding['L'] - $this->cell_padding['R']) / ($ns?$ns:1));
foreach ($uniblock as $uk => $uniarr) {
if (($uk % 2) == 0) {
// x space to skip
if ($spacewidth != 0) {
// justification shift
$xshift += (count(array_keys($uniarr, 32)) * $spw);
}
$xshift += $this->GetArrStringWidth($uniarr); // + shift justification
} else {
// character to print
$topchr = TCPDF_FONTS::arrUTF8ToUTF16BE($uniarr, false);
$topchr = TCPDF_STATIC::_escape($topchr);
$s .= sprintf(' BT %F %F Td [(%s)] TJ ET', ($xdk + ($xshift * $k)), $ty, $topchr);
}
}
}
if ($this->underline) {
$s .= ' '.$this->_dounderlinew($xdx, $basefonty, $width);
}
if ($this->linethrough) {
$s .= ' '.$this->_dolinethroughw($xdx, $basefonty, $width);
}
if ($this->overline) {
$s .= ' '.$this->_dooverlinew($xdx, $basefonty, $width);
}
if ($this->ColorFlag AND ($this->textrendermode < 4)) {
$s .= ' Q';
}
if ($link) {
$this->Link($xdx, $yt, $width, ($this->FontAscent + $this->FontDescent), $link, $ns);
}
}
// output cell
if ($s) {
// output cell
$rs .= $s;
if ($this->font_spacing != 0) {
// reset font spacing mode
$rs .= ' BT 0 Tc ET';
}
if ($this->font_stretching != 100) {
// reset font stretching mode
$rs .= ' BT 100 Tz ET';
}
}
// reset word spacing
if (!$this->isUnicodeFont() AND ($align == 'J')) {
$rs .= ' BT 0 Tw ET';
}
// reset stretching and spacing
$this->font_stretching = $prev_font_stretching;
$this->font_spacing = $prev_font_spacing;
$this->lasth = $h;
if ($ln > 0) {
//Go to the beginning of the next line
$this->y = $y + $h + $this->cell_margin['B'];
if ($ln == 1) {
if ($this->rtl) {
$this->x = $this->w - $this->rMargin;
} else {
$this->x = $this->lMargin;
}
}
} else {
// go left or right by case
if ($this->rtl) {
$this->x = $x - $w - $this->cell_margin['L'];
} else {
$this->x = $x + $w + $this->cell_margin['R'];
}
}
$gstyles = ''.$this->linestyleWidth.' '.$this->linestyleCap.' '.$this->linestyleJoin.' '.$this->linestyleDash.' '.$this->DrawColor.' '.$this->FillColor."\n";
$rs = $gstyles.$rs;
$this->cell_padding = $prev_cell_padding;
$this->cell_margin = $prev_cell_margin;
return $rs;
}
/**
* Replace a char if is defined on the current font.
* @param $oldchar (int) Integer code (unicode) of the character to replace.
* @param $newchar (int) Integer code (unicode) of the new character.
* @return int the replaced char or the old char in case the new char i not defined
* @protected
* @since 5.9.167 (2012-06-22)
*/
protected function replaceChar($oldchar, $newchar) {
if ($this->isCharDefined($newchar)) {
// add the new char on the subset list
$this->CurrentFont['subsetchars'][$newchar] = true;
// return the new character
return $newchar;
}
// return the old char
return $oldchar;
}
/**
* Returns the code to draw the cell border
* @param $x (float) X coordinate.
* @param $y (float) Y coordinate.
* @param $w (float) Cell width.
* @param $h (float) Cell height.
* @param $brd (mixed) Indicates if borders must be drawn around the cell. The value can be a number:
0: no border (default)
1: frame
or a string containing some or all of the following characters (in any order):
L: left
T: top
R: right
B: bottom
or an array of line styles for each border group - for example: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
* @return string containing cell border code
* @protected
* @see SetLineStyle()
* @since 5.7.000 (2010-08-02)
*/
protected function getCellBorder($x, $y, $w, $h, $brd) {
$s = ''; // string to be returned
if (empty($brd)) {
return $s;
}
if ($brd == 1) {
$brd = array('LRTB' => true);
}
// calculate coordinates for border
$k = $this->k;
if ($this->rtl) {
$xeL = ($x - $w) * $k;
$xeR = $x * $k;
} else {
$xeL = $x * $k;
$xeR = ($x + $w) * $k;
}
$yeL = (($this->h - ($y + $h)) * $k);
$yeT = (($this->h - $y) * $k);
$xeT = $xeL;
$xeB = $xeR;
$yeR = $yeT;
$yeB = $yeL;
if (is_string($brd)) {
// convert string to array
$slen = strlen($brd);
$newbrd = array();
for ($i = 0; $i < $slen; ++$i) {
$newbrd[$brd[$i]] = array('cap' => 'square', 'join' => 'miter');
}
$brd = $newbrd;
}
if (isset($brd['mode'])) {
$mode = $brd['mode'];
unset($brd['mode']);
} else {
$mode = 'normal';
}
foreach ($brd as $border => $style) {
if (is_array($style) AND !empty($style)) {
// apply border style
$prev_style = $this->linestyleWidth.' '.$this->linestyleCap.' '.$this->linestyleJoin.' '.$this->linestyleDash.' '.$this->DrawColor.' ';
$s .= $this->SetLineStyle($style, true)."\n";
}
switch ($mode) {
case 'ext': {
$off = (($this->LineWidth / 2) * $k);
$xL = $xeL - $off;
$xR = $xeR + $off;
$yT = $yeT + $off;
$yL = $yeL - $off;
$xT = $xL;
$xB = $xR;
$yR = $yT;
$yB = $yL;
$w += $this->LineWidth;
$h += $this->LineWidth;
break;
}
case 'int': {
$off = ($this->LineWidth / 2) * $k;
$xL = $xeL + $off;
$xR = $xeR - $off;
$yT = $yeT - $off;
$yL = $yeL + $off;
$xT = $xL;
$xB = $xR;
$yR = $yT;
$yB = $yL;
$w -= $this->LineWidth;
$h -= $this->LineWidth;
break;
}
case 'normal':
default: {
$xL = $xeL;
$xT = $xeT;
$xB = $xeB;
$xR = $xeR;
$yL = $yeL;
$yT = $yeT;
$yB = $yeB;
$yR = $yeR;
break;
}
}
// draw borders by case
if (strlen($border) == 4) {
$s .= sprintf('%F %F %F %F re S ', $xT, $yT, ($w * $k), (-$h * $k));
} elseif (strlen($border) == 3) {
if (strpos($border,'B') === false) { // LTR
$s .= sprintf('%F %F m ', $xL, $yL);
$s .= sprintf('%F %F l ', $xT, $yT);
$s .= sprintf('%F %F l ', $xR, $yR);
$s .= sprintf('%F %F l ', $xB, $yB);
$s .= 'S ';
} elseif (strpos($border,'L') === false) { // TRB
$s .= sprintf('%F %F m ', $xT, $yT);
$s .= sprintf('%F %F l ', $xR, $yR);
$s .= sprintf('%F %F l ', $xB, $yB);
$s .= sprintf('%F %F l ', $xL, $yL);
$s .= 'S ';
} elseif (strpos($border,'T') === false) { // RBL
$s .= sprintf('%F %F m ', $xR, $yR);
$s .= sprintf('%F %F l ', $xB, $yB);
$s .= sprintf('%F %F l ', $xL, $yL);
$s .= sprintf('%F %F l ', $xT, $yT);
$s .= 'S ';
} elseif (strpos($border,'R') === false) { // BLT
$s .= sprintf('%F %F m ', $xB, $yB);
$s .= sprintf('%F %F l ', $xL, $yL);
$s .= sprintf('%F %F l ', $xT, $yT);
$s .= sprintf('%F %F l ', $xR, $yR);
$s .= 'S ';
}
} elseif (strlen($border) == 2) {
if ((strpos($border,'L') !== false) AND (strpos($border,'T') !== false)) { // LT
$s .= sprintf('%F %F m ', $xL, $yL);
$s .= sprintf('%F %F l ', $xT, $yT);
$s .= sprintf('%F %F l ', $xR, $yR);
$s .= 'S ';
} elseif ((strpos($border,'T') !== false) AND (strpos($border,'R') !== false)) { // TR
$s .= sprintf('%F %F m ', $xT, $yT);
$s .= sprintf('%F %F l ', $xR, $yR);
$s .= sprintf('%F %F l ', $xB, $yB);
$s .= 'S ';
} elseif ((strpos($border,'R') !== false) AND (strpos($border,'B') !== false)) { // RB
$s .= sprintf('%F %F m ', $xR, $yR);
$s .= sprintf('%F %F l ', $xB, $yB);
$s .= sprintf('%F %F l ', $xL, $yL);
$s .= 'S ';
} elseif ((strpos($border,'B') !== false) AND (strpos($border,'L') !== false)) { // BL
$s .= sprintf('%F %F m ', $xB, $yB);
$s .= sprintf('%F %F l ', $xL, $yL);
$s .= sprintf('%F %F l ', $xT, $yT);
$s .= 'S ';
} elseif ((strpos($border,'L') !== false) AND (strpos($border,'R') !== false)) { // LR
$s .= sprintf('%F %F m ', $xL, $yL);
$s .= sprintf('%F %F l ', $xT, $yT);
$s .= 'S ';
$s .= sprintf('%F %F m ', $xR, $yR);
$s .= sprintf('%F %F l ', $xB, $yB);
$s .= 'S ';
} elseif ((strpos($border,'T') !== false) AND (strpos($border,'B') !== false)) { // TB
$s .= sprintf('%F %F m ', $xT, $yT);
$s .= sprintf('%F %F l ', $xR, $yR);
$s .= 'S ';
$s .= sprintf('%F %F m ', $xB, $yB);
$s .= sprintf('%F %F l ', $xL, $yL);
$s .= 'S ';
}
} else { // strlen($border) == 1
if (strpos($border,'L') !== false) { // L
$s .= sprintf('%F %F m ', $xL, $yL);
$s .= sprintf('%F %F l ', $xT, $yT);
$s .= 'S ';
} elseif (strpos($border,'T') !== false) { // T
$s .= sprintf('%F %F m ', $xT, $yT);
$s .= sprintf('%F %F l ', $xR, $yR);
$s .= 'S ';
} elseif (strpos($border,'R') !== false) { // R
$s .= sprintf('%F %F m ', $xR, $yR);
$s .= sprintf('%F %F l ', $xB, $yB);
$s .= 'S ';
} elseif (strpos($border,'B') !== false) { // B
$s .= sprintf('%F %F m ', $xB, $yB);
$s .= sprintf('%F %F l ', $xL, $yL);
$s .= 'S ';
}
}
if (is_array($style) AND !empty($style)) {
// reset border style to previous value
$s .= "\n".$this->linestyleWidth.' '.$this->linestyleCap.' '.$this->linestyleJoin.' '.$this->linestyleDash.' '.$this->DrawColor."\n";
}
}
return $s;
}
/**
* This method allows printing text with line breaks.
* They can be automatic (as soon as the text reaches the right border of the cell) or explicit (via the \n character). As many cells as necessary are output, one below the other.
* Text can be aligned, centered or justified. The cell block can be framed and the background painted.
* @param $w (float) Width of cells. If 0, they extend up to the right margin of the page.
* @param $h (float) Cell minimum height. The cell extends automatically if needed.
* @param $txt (string) String to print
* @param $border (mixed) Indicates if borders must be drawn around the cell. The value can be a number:
0: no border (default)
1: frame
or a string containing some or all of the following characters (in any order):
L: left
T: top
R: right
B: bottom
or an array of line styles for each border group - for example: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
* @param $align (string) Allows to center or align the text. Possible values are:
L or empty string: left align
C: center
R: right align
J: justification (default value when $ishtml=false)
* @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
* @param $ln (int) Indicates where the current position should go after the call. Possible values are:
0: to the right
1: to the beginning of the next line [DEFAULT]
2: below
* @param $x (float) x position in user units
* @param $y (float) y position in user units
* @param $reseth (boolean) if true reset the last cell height (default true).
* @param $stretch (int) font stretch mode:
0 = disabled
1 = horizontal scaling only if text is larger than cell width
2 = forced horizontal scaling to fit cell width
3 = character spacing only if text is larger than cell width
4 = forced character spacing to fit cell width
General font stretching and scaling values will be preserved when possible.
* @param $ishtml (boolean) INTERNAL USE ONLY -- set to true if $txt is HTML content (default = false). Never set this parameter to true, use instead writeHTMLCell() or writeHTML() methods.
* @param $autopadding (boolean) if true, uses internal padding and automatically adjust it to account for line width.
* @param $maxh (float) maximum height. It should be >= $h and less then remaining space to the bottom of the page, or 0 for disable this feature. This feature works only when $ishtml=false.
* @param $valign (string) Vertical alignment of text (requires $maxh = $h > 0). Possible values are:
T: TOP
M: middle
B: bottom
. This feature works only when $ishtml=false and the cell must fit in a single page.
* @param $fitcell (boolean) if true attempt to fit all the text within the cell by reducing the font size (do not work in HTML mode).
* @return int Return the number of cells or 1 for html mode.
* @public
* @since 1.3
* @see SetFont(), SetDrawColor(), SetFillColor(), SetTextColor(), SetLineWidth(), Cell(), Write(), SetAutoPageBreak()
*/
public function MultiCell($w, $h, $txt, $border=0, $align='J', $fill=false, $ln=1, $x='', $y='', $reseth=true, $stretch=0, $ishtml=false, $autopadding=true, $maxh=0, $valign='T', $fitcell=false) {
$prev_cell_margin = $this->cell_margin;
$prev_cell_padding = $this->cell_padding;
// adjust internal padding
$this->adjustCellPadding($border);
$mc_padding = $this->cell_padding;
$mc_margin = $this->cell_margin;
$this->cell_padding['T'] = 0;
$this->cell_padding['B'] = 0;
$this->setCellMargins(0, 0, 0, 0);
if (TCPDF_STATIC::empty_string($this->lasth) OR $reseth) {
// reset row height
$this->resetLastH();
}
if (!TCPDF_STATIC::empty_string($y)) {
$this->SetY($y);
} else {
$y = $this->GetY();
}
$resth = 0;
if (($h > 0) AND $this->inPageBody() AND (($y + $h + $mc_margin['T'] + $mc_margin['B']) > $this->PageBreakTrigger)) {
// spit cell in more pages/columns
$newh = ($this->PageBreakTrigger - $y);
$resth = ($h - $newh); // cell to be printed on the next page/column
$h = $newh;
}
// get current page number
$startpage = $this->page;
// get current column
$startcolumn = $this->current_column;
if (!TCPDF_STATIC::empty_string($x)) {
$this->SetX($x);
} else {
$x = $this->GetX();
}
// check page for no-write regions and adapt page margins if necessary
list($x, $y) = $this->checkPageRegions(0, $x, $y);
// apply margins
$oy = $y + $mc_margin['T'];
if ($this->rtl) {
$ox = ($this->w - $x - $mc_margin['R']);
} else {
$ox = ($x + $mc_margin['L']);
}
$this->x = $ox;
$this->y = $oy;
// set width
if (TCPDF_STATIC::empty_string($w) OR ($w <= 0)) {
if ($this->rtl) {
$w = ($this->x - $this->lMargin - $mc_margin['L']);
} else {
$w = ($this->w - $this->x - $this->rMargin - $mc_margin['R']);
}
}
// store original margin values
$lMargin = $this->lMargin;
$rMargin = $this->rMargin;
if ($this->rtl) {
$this->rMargin = ($this->w - $this->x);
$this->lMargin = ($this->x - $w);
} else {
$this->lMargin = ($this->x);
$this->rMargin = ($this->w - $this->x - $w);
}
$this->clMargin = $this->lMargin;
$this->crMargin = $this->rMargin;
if ($autopadding) {
// add top padding
$this->y += $mc_padding['T'];
}
if ($ishtml) { // ******* Write HTML text
$this->writeHTML($txt, true, false, $reseth, true, $align);
$nl = 1;
} else { // ******* Write simple text
$prev_FontSizePt = $this->FontSizePt;
// vertical alignment
if ($maxh > 0) {
// get text height
$text_height = $this->getStringHeight($w, $txt, $reseth, $autopadding, $mc_padding, $border);
if ($fitcell) {
// try to reduce font size to fit text on cell (use a quick search algorithm)
$fmin = 1;
$fmax = $this->FontSizePt;
$prev_text_height = $text_height;
$maxit = 100; // max number of iterations
while ($maxit > 0) {
$fmid = (($fmax + $fmin) / 2);
$this->SetFontSize($fmid, false);
$this->resetLastH();
$text_height = $this->getStringHeight($w, $txt, $reseth, $autopadding, $mc_padding, $border);
if (($text_height == $maxh) OR (($text_height < $maxh) AND ($fmin >= ($fmax - 0.01)))) {
break;
} elseif ($text_height < $maxh) {
$fmin = $fmid;
} else {
$fmax = $fmid;
}
--$maxit;
}
$this->SetFontSize($this->FontSizePt);
}
if ($text_height < $maxh) {
if ($valign == 'M') {
// text vertically centered
$this->y += (($maxh - $text_height) / 2);
} elseif ($valign == 'B') {
// text vertically aligned on bottom
$this->y += ($maxh - $text_height);
}
}
}
$nl = $this->Write($this->lasth, $txt, '', 0, $align, true, $stretch, false, true, $maxh, 0, $mc_margin);
if ($fitcell) {
// restore font size
$this->SetFontSize($prev_FontSizePt);
}
}
if ($autopadding) {
// add bottom padding
$this->y += $mc_padding['B'];
}
// Get end-of-text Y position
$currentY = $this->y;
// get latest page number
$endpage = $this->page;
if ($resth > 0) {
$skip = ($endpage - $startpage);
$tmpresth = $resth;
while ($tmpresth > 0) {
if ($skip <= 0) {
// add a page (or trig AcceptPageBreak() for multicolumn mode)
$this->checkPageBreak($this->PageBreakTrigger + 1);
}
if ($this->num_columns > 1) {
$tmpresth -= ($this->h - $this->y - $this->bMargin);
} else {
$tmpresth -= ($this->h - $this->tMargin - $this->bMargin);
}
--$skip;
}
$currentY = $this->y;
$endpage = $this->page;
}
// get latest column
$endcolumn = $this->current_column;
if ($this->num_columns == 0) {
$this->num_columns = 1;
}
// disable page regions check
$check_page_regions = $this->check_page_regions;
$this->check_page_regions = false;
// get border modes
$border_start = TCPDF_STATIC::getBorderMode($border, $position='start', $this->opencell);
$border_end = TCPDF_STATIC::getBorderMode($border, $position='end', $this->opencell);
$border_middle = TCPDF_STATIC::getBorderMode($border, $position='middle', $this->opencell);
// design borders around HTML cells.
for ($page = $startpage; $page <= $endpage; ++$page) { // for each page
$ccode = '';
$this->setPage($page);
if ($this->num_columns < 2) {
// single-column mode
$this->SetX($x);
$this->y = $this->tMargin;
}
// account for margin changes
if ($page > $startpage) {
if (($this->rtl) AND ($this->pagedim[$page]['orm'] != $this->pagedim[$startpage]['orm'])) {
$this->x -= ($this->pagedim[$page]['orm'] - $this->pagedim[$startpage]['orm']);
} elseif ((!$this->rtl) AND ($this->pagedim[$page]['olm'] != $this->pagedim[$startpage]['olm'])) {
$this->x += ($this->pagedim[$page]['olm'] - $this->pagedim[$startpage]['olm']);
}
}
if ($startpage == $endpage) {
// single page
for ($column = $startcolumn; $column <= $endcolumn; ++$column) { // for each column
$this->selectColumn($column);
if ($this->rtl) {
$this->x -= $mc_margin['R'];
} else {
$this->x += $mc_margin['L'];
}
if ($startcolumn == $endcolumn) { // single column
$cborder = $border;
$h = max($h, ($currentY - $oy));
$this->y = $oy;
} elseif ($column == $startcolumn) { // first column
$cborder = $border_start;
$this->y = $oy;
$h = $this->h - $this->y - $this->bMargin;
} elseif ($column == $endcolumn) { // end column
$cborder = $border_end;
$h = $currentY - $this->y;
if ($resth > $h) {
$h = $resth;
}
} else { // middle column
$cborder = $border_middle;
$h = $this->h - $this->y - $this->bMargin;
$resth -= $h;
}
$ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
} // end for each column
} elseif ($page == $startpage) { // first page
for ($column = $startcolumn; $column < $this->num_columns; ++$column) { // for each column
$this->selectColumn($column);
if ($this->rtl) {
$this->x -= $mc_margin['R'];
} else {
$this->x += $mc_margin['L'];
}
if ($column == $startcolumn) { // first column
$cborder = $border_start;
$this->y = $oy;
$h = $this->h - $this->y - $this->bMargin;
} else { // middle column
$cborder = $border_middle;
$h = $this->h - $this->y - $this->bMargin;
$resth -= $h;
}
$ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
} // end for each column
} elseif ($page == $endpage) { // last page
for ($column = 0; $column <= $endcolumn; ++$column) { // for each column
$this->selectColumn($column);
if ($this->rtl) {
$this->x -= $mc_margin['R'];
} else {
$this->x += $mc_margin['L'];
}
if ($column == $endcolumn) {
// end column
$cborder = $border_end;
$h = $currentY - $this->y;
if ($resth > $h) {
$h = $resth;
}
} else {
// middle column
$cborder = $border_middle;
$h = $this->h - $this->y - $this->bMargin;
$resth -= $h;
}
$ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
} // end for each column
} else { // middle page
for ($column = 0; $column < $this->num_columns; ++$column) { // for each column
$this->selectColumn($column);
if ($this->rtl) {
$this->x -= $mc_margin['R'];
} else {
$this->x += $mc_margin['L'];
}
$cborder = $border_middle;
$h = $this->h - $this->y - $this->bMargin;
$resth -= $h;
$ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
} // end for each column
}
if ($cborder OR $fill) {
$offsetlen = strlen($ccode);
// draw border and fill
if ($this->inxobj) {
// we are inside an XObject template
if (end($this->xobjects[$this->xobjid]['transfmrk']) !== false) {
$pagemarkkey = key($this->xobjects[$this->xobjid]['transfmrk']);
$pagemark = $this->xobjects[$this->xobjid]['transfmrk'][$pagemarkkey];
$this->xobjects[$this->xobjid]['transfmrk'][$pagemarkkey] += $offsetlen;
} else {
$pagemark = $this->xobjects[$this->xobjid]['intmrk'];
$this->xobjects[$this->xobjid]['intmrk'] += $offsetlen;
}
$pagebuff = $this->xobjects[$this->xobjid]['outdata'];
$pstart = substr($pagebuff, 0, $pagemark);
$pend = substr($pagebuff, $pagemark);
$this->xobjects[$this->xobjid]['outdata'] = $pstart.$ccode.$pend;
} else {
if (end($this->transfmrk[$this->page]) !== false) {
$pagemarkkey = key($this->transfmrk[$this->page]);
$pagemark = $this->transfmrk[$this->page][$pagemarkkey];
$this->transfmrk[$this->page][$pagemarkkey] += $offsetlen;
} elseif ($this->InFooter) {
$pagemark = $this->footerpos[$this->page];
$this->footerpos[$this->page] += $offsetlen;
} else {
$pagemark = $this->intmrk[$this->page];
$this->intmrk[$this->page] += $offsetlen;
}
$pagebuff = $this->getPageBuffer($this->page);
$pstart = substr($pagebuff, 0, $pagemark);
$pend = substr($pagebuff, $pagemark);
$this->setPageBuffer($this->page, $pstart.$ccode.$pend);
}
}
} // end for each page
// restore page regions check
$this->check_page_regions = $check_page_regions;
// Get end-of-cell Y position
$currentY = $this->GetY();
// restore previous values
if ($this->num_columns > 1) {
$this->selectColumn();
} else {
// restore original margins
$this->lMargin = $lMargin;
$this->rMargin = $rMargin;
if ($this->page > $startpage) {
// check for margin variations between pages (i.e. booklet mode)
$dl = ($this->pagedim[$this->page]['olm'] - $this->pagedim[$startpage]['olm']);
$dr = ($this->pagedim[$this->page]['orm'] - $this->pagedim[$startpage]['orm']);
if (($dl != 0) OR ($dr != 0)) {
$this->lMargin += $dl;
$this->rMargin += $dr;
}
}
}
if ($ln > 0) {
//Go to the beginning of the next line
$this->SetY($currentY + $mc_margin['B']);
if ($ln == 2) {
$this->SetX($x + $w + $mc_margin['L'] + $mc_margin['R']);
}
} else {
// go left or right by case
$this->setPage($startpage);
$this->y = $y;
$this->SetX($x + $w + $mc_margin['L'] + $mc_margin['R']);
}
$this->setContentMark();
$this->cell_padding = $prev_cell_padding;
$this->cell_margin = $prev_cell_margin;
$this->clMargin = $this->lMargin;
$this->crMargin = $this->rMargin;
return $nl;
}
/**
* This method return the estimated number of lines for print a simple text string using Multicell() method.
* @param $txt (string) String for calculating his height
* @param $w (float) Width of cells. If 0, they extend up to the right margin of the page.
* @param $reseth (boolean) if true reset the last cell height (default false).
* @param $autopadding (boolean) if true, uses internal padding and automatically adjust it to account for line width (default true).
* @param $cellpadding (float) Internal cell padding, if empty uses default cell padding.
* @param $border (mixed) Indicates if borders must be drawn around the cell. The value can be a number:
0: no border (default)
1: frame
or a string containing some or all of the following characters (in any order):
L: left
T: top
R: right
B: bottom
or an array of line styles for each border group - for example: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
* @return float Return the minimal height needed for multicell method for printing the $txt param.
* @author Alexander Escalona Fernndez, Nicola Asuni
* @public
* @since 4.5.011
*/
public function getNumLines($txt, $w=0, $reseth=false, $autopadding=true, $cellpadding='', $border=0) {
if ($txt === '') {
// empty string
return 1;
}
// adjust internal padding
$prev_cell_padding = $this->cell_padding;
$prev_lasth = $this->lasth;
if (is_array($cellpadding)) {
$this->cell_padding = $cellpadding;
}
$this->adjustCellPadding($border);
if (TCPDF_STATIC::empty_string($w) OR ($w <= 0)) {
if ($this->rtl) {
$w = $this->x - $this->lMargin;
} else {
$w = $this->w - $this->rMargin - $this->x;
}
}
$wmax = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
if ($reseth) {
// reset row height
$this->resetLastH();
}
$lines = 1;
$sum = 0;
$chars = TCPDF_FONTS::utf8Bidi(TCPDF_FONTS::UTF8StringToArray($txt, $this->isunicode, $this->CurrentFont), $txt, $this->tmprtl, $this->isunicode, $this->CurrentFont);
$charsWidth = $this->GetArrStringWidth($chars, '', '', 0, true);
$length = count($chars);
$lastSeparator = -1;
for ($i = 0; $i < $length; ++$i) {
$charWidth = $charsWidth[$i];
if (preg_match($this->re_spaces, TCPDF_FONTS::unichr($chars[$i], $this->isunicode))) {
$lastSeparator = $i;
}
if ((($sum + $charWidth) > $wmax) OR ($chars[$i] == 10)) {
++$lines;
if ($chars[$i] == 10) {
$lastSeparator = -1;
$sum = 0;
} elseif ($lastSeparator != -1) {
$i = $lastSeparator;
$lastSeparator = -1;
$sum = 0;
} else {
$sum = $charWidth;
}
} else {
$sum += $charWidth;
}
}
if ($chars[($length - 1)] == 10) {
--$lines;
}
$this->cell_padding = $prev_cell_padding;
$this->lasth = $prev_lasth;
return $lines;
}
/**
* This method return the estimated height needed for printing a simple text string using the Multicell() method.
* Generally, if you want to know the exact height for a block of content you can use the following alternative technique:
* @pre
* // store current object
* $pdf->startTransaction();
* // store starting values
* $start_y = $pdf->GetY();
* $start_page = $pdf->getPage();
* // call your printing functions with your parameters
* // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
* $pdf->MultiCell($w=0, $h=0, $txt, $border=1, $align='L', $fill=false, $ln=1, $x='', $y='', $reseth=true, $stretch=0, $ishtml=false, $autopadding=true, $maxh=0);
* // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
* // get the new Y
* $end_y = $pdf->GetY();
* $end_page = $pdf->getPage();
* // calculate height
* $height = 0;
* if ($end_page == $start_page) {
* $height = $end_y - $start_y;
* } else {
* for ($page=$start_page; $page <= $end_page; ++$page) {
* $this->setPage($page);
* if ($page == $start_page) {
* // first page
* $height = $this->h - $start_y - $this->bMargin;
* } elseif ($page == $end_page) {
* // last page
* $height = $end_y - $this->tMargin;
* } else {
* $height = $this->h - $this->tMargin - $this->bMargin;
* }
* }
* }
* // restore previous object
* $pdf = $pdf->rollbackTransaction();
*
* @param $w (float) Width of cells. If 0, they extend up to the right margin of the page.
* @param $txt (string) String for calculating his height
* @param $reseth (boolean) if true reset the last cell height (default false).
* @param $autopadding (boolean) if true, uses internal padding and automatically adjust it to account for line width (default true).
* @param $cellpadding (float) Internal cell padding, if empty uses default cell padding.
* @param $border (mixed) Indicates if borders must be drawn around the cell. The value can be a number:
0: no border (default)
1: frame
or a string containing some or all of the following characters (in any order):
L: left
T: top
R: right
B: bottom
or an array of line styles for each border group - for example: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
* @return float Return the minimal height needed for multicell method for printing the $txt param.
* @author Nicola Asuni, Alexander Escalona Fernndez
* @public
*/
public function getStringHeight($w, $txt, $reseth=false, $autopadding=true, $cellpadding='', $border=0) {
// adjust internal padding
$prev_cell_padding = $this->cell_padding;
$prev_lasth = $this->lasth;
if (is_array($cellpadding)) {
$this->cell_padding = $cellpadding;
}
$this->adjustCellPadding($border);
$lines = $this->getNumLines($txt, $w, $reseth, $autopadding, $cellpadding, $border);
$height = $lines * ($this->FontSize * $this->cell_height_ratio);
if ($autopadding) {
// add top and bottom padding
$height += ($this->cell_padding['T'] + $this->cell_padding['B']);
}
$this->cell_padding = $prev_cell_padding;
$this->lasth = $prev_lasth;
return $height;
}
/**
* This method prints text from the current position.
* @param $h (float) Line height
* @param $txt (string) String to print
* @param $link (mixed) URL or identifier returned by AddLink()
* @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
* @param $align (string) Allows to center or align the text. Possible values are:
L or empty string: left align (default value)
C: center
R: right align
J: justify
* @param $ln (boolean) if true set cursor at the bottom of the line, otherwise set cursor at the top of the line.
* @param $stretch (int) font stretch mode:
0 = disabled
1 = horizontal scaling only if text is larger than cell width
2 = forced horizontal scaling to fit cell width
3 = character spacing only if text is larger than cell width
4 = forced character spacing to fit cell width
General font stretching and scaling values will be preserved when possible.
* @param $firstline (boolean) if true prints only the first line and return the remaining string.
* @param $firstblock (boolean) if true the string is the starting of a line.
* @param $maxh (float) maximum height. The remaining unprinted text will be returned. It should be >= $h and less then remaining space to the bottom of the page, or 0 for disable this feature.
* @param $wadj (float) first line width will be reduced by this amount (used in HTML mode).
* @param $margin (array) margin array of the parent container
* @return mixed Return the number of cells or the remaining string if $firstline = true.
* @public
* @since 1.5
*/
public function Write($h, $txt, $link='', $fill=false, $align='', $ln=false, $stretch=0, $firstline=false, $firstblock=false, $maxh=0, $wadj=0, $margin='') {
// check page for no-write regions and adapt page margins if necessary
list($this->x, $this->y) = $this->checkPageRegions($h, $this->x, $this->y);
if (strlen($txt) == 0) {
// fix empty text
$txt = ' ';
}
if ($margin === '') {
// set default margins
$margin = $this->cell_margin;
}
// remove carriage returns
$s = str_replace("\r", '', $txt);
// check if string contains arabic text
if (preg_match(TCPDF_FONT_DATA::$uni_RE_PATTERN_ARABIC, $s)) {
$arabic = true;
} else {
$arabic = false;
}
// check if string contains RTL text
if ($arabic OR ($this->tmprtl == 'R') OR preg_match(TCPDF_FONT_DATA::$uni_RE_PATTERN_RTL, $s)) {
$rtlmode = true;
} else {
$rtlmode = false;
}
// get a char width
$chrwidth = $this->GetCharWidth(46); // dot character
// get array of unicode values
$chars = TCPDF_FONTS::UTF8StringToArray($s, $this->isunicode, $this->CurrentFont);
// calculate maximum width for a single character on string
$chrw = $this->GetArrStringWidth($chars, '', '', 0, true);
array_walk($chrw, array($this, 'getRawCharWidth'));
$maxchwidth = max($chrw);
// get array of chars
$uchars = TCPDF_FONTS::UTF8ArrayToUniArray($chars, $this->isunicode);
// get the number of characters
$nb = count($chars);
// replacement for SHY character (minus symbol)
$shy_replacement = 45;
$shy_replacement_char = TCPDF_FONTS::unichr($shy_replacement, $this->isunicode);
// widht for SHY replacement
$shy_replacement_width = $this->GetCharWidth($shy_replacement);
// max Y
$maxy = $this->y + $maxh - $h - $this->cell_padding['T'] - $this->cell_padding['B'];
// page width
$pw = $w = $this->w - $this->lMargin - $this->rMargin;
// calculate remaining line width ($w)
if ($this->rtl) {
$w = $this->x - $this->lMargin;
} else {
$w = $this->w - $this->rMargin - $this->x;
}
// max column width
$wmax = ($w - $wadj);
if (!$firstline) {
$wmax -= ($this->cell_padding['L'] + $this->cell_padding['R']);
}
if ((!$firstline) AND (($chrwidth > $wmax) OR ($maxchwidth > $wmax))) {
// the maximum width character do not fit on column
return '';
}
// minimum row height
$row_height = max($h, $this->FontSize * $this->cell_height_ratio);
$start_page = $this->page;
$i = 0; // character position
$j = 0; // current starting position
$sep = -1; // position of the last blank space
$shy = false; // true if the last blank is a soft hypen (SHY)
$l = 0; // current string length
$nl = 0; //number of lines
$linebreak = false;
$pc = 0; // previous character
// for each character
while ($i < $nb) {
if (($maxh > 0) AND ($this->y >= $maxy) ) {
break;
}
//Get the current character
$c = $chars[$i];
if ($c == 10) { // 10 = "\n" = new line
//Explicit line break
if ($align == 'J') {
if ($this->rtl) {
$talign = 'R';
} else {
$talign = 'L';
}
} else {
$talign = $align;
}
$tmpstr = TCPDF_FONTS::UniArrSubString($uchars, $j, $i);
if ($firstline) {
$startx = $this->x;
$tmparr = array_slice($chars, $j, ($i - $j));
if ($rtlmode) {
$tmparr = TCPDF_FONTS::utf8Bidi($tmparr, $tmpstr, $this->tmprtl, $this->isunicode, $this->CurrentFont);
}
$linew = $this->GetArrStringWidth($tmparr);
unset($tmparr);
if ($this->rtl) {
$this->endlinex = $startx - $linew;
} else {
$this->endlinex = $startx + $linew;
}
$w = $linew;
$tmpcellpadding = $this->cell_padding;
if ($maxh == 0) {
$this->SetCellPadding(0);
}
}
if ($firstblock AND $this->isRTLTextDir()) {
$tmpstr = $this->stringRightTrim($tmpstr);
}
// Skip newlines at the begining of a page or column
if (!empty($tmpstr) OR ($this->y < ($this->PageBreakTrigger - $row_height))) {
$this->Cell($w, $h, $tmpstr, 0, 1, $talign, $fill, $link, $stretch);
}
unset($tmpstr);
if ($firstline) {
$this->cell_padding = $tmpcellpadding;
return (TCPDF_FONTS::UniArrSubString($uchars, $i));
}
++$nl;
$j = $i + 1;
$l = 0;
$sep = -1;
$shy = false;
// account for margin changes
if ((($this->y + $this->lasth) > $this->PageBreakTrigger) AND ($this->inPageBody())) {
$this->AcceptPageBreak();
if ($this->rtl) {
$this->x -= $margin['R'];
} else {
$this->x += $margin['L'];
}
$this->lMargin += $margin['L'];
$this->rMargin += $margin['R'];
}
$w = $this->getRemainingWidth();
$wmax = ($w - $this->cell_padding['L'] - $this->cell_padding['R']);
} else {
// 160 is the non-breaking space.
// 173 is SHY (Soft Hypen).
// \p{Z} or \p{Separator}: any kind of Unicode whitespace or invisible separator.
// \p{Lo} or \p{Other_Letter}: a Unicode letter or ideograph that does not have lowercase and uppercase variants.
// \p{Lo} is needed because Chinese characters are packed next to each other without spaces in between.
if (($c != 160)
AND (($c == 173)
OR preg_match($this->re_spaces, TCPDF_FONTS::unichr($c, $this->isunicode))
OR (($c == 45)
AND ($i < ($nb - 1))
AND @preg_match('/[\p{L}]/'.$this->re_space['m'], TCPDF_FONTS::unichr($pc, $this->isunicode))
AND @preg_match('/[\p{L}]/'.$this->re_space['m'], TCPDF_FONTS::unichr($chars[($i + 1)], $this->isunicode))
)
)
) {
// update last blank space position
$sep = $i;
// check if is a SHY
if (($c == 173) OR ($c == 45)) {
$shy = true;
if ($pc == 45) {
$tmp_shy_replacement_width = 0;
$tmp_shy_replacement_char = '';
} else {
$tmp_shy_replacement_width = $shy_replacement_width;
$tmp_shy_replacement_char = $shy_replacement_char;
}
} else {
$shy = false;
}
}
// update string length
if ($this->isUnicodeFont() AND ($arabic)) {
// with bidirectional algorithm some chars may be changed affecting the line length
// *** very slow ***
$l = $this->GetArrStringWidth(TCPDF_FONTS::utf8Bidi(array_slice($chars, $j, ($i - $j)), '', $this->tmprtl, $this->isunicode, $this->CurrentFont));
} else {
$l += $this->GetCharWidth($c);
}
if (($l > $wmax) OR (($c == 173) AND (($l + $tmp_shy_replacement_width) > $wmax)) ) {
// we have reached the end of column
if ($sep == -1) {
// check if the line was already started
if (($this->rtl AND ($this->x <= ($this->w - $this->rMargin - $this->cell_padding['R'] - $margin['R'] - $chrwidth)))
OR ((!$this->rtl) AND ($this->x >= ($this->lMargin + $this->cell_padding['L'] + $margin['L'] + $chrwidth)))) {
// print a void cell and go to next line
$this->Cell($w, $h, '', 0, 1);
$linebreak = true;
if ($firstline) {
return (TCPDF_FONTS::UniArrSubString($uchars, $j));
}
} else {
// truncate the word because do not fit on column
$tmpstr = TCPDF_FONTS::UniArrSubString($uchars, $j, $i);
if ($firstline) {
$startx = $this->x;
$tmparr = array_slice($chars, $j, ($i - $j));
if ($rtlmode) {
$tmparr = TCPDF_FONTS::utf8Bidi($tmparr, $tmpstr, $this->tmprtl, $this->isunicode, $this->CurrentFont);
}
$linew = $this->GetArrStringWidth($tmparr);
unset($tmparr);
if ($this->rtl) {
$this->endlinex = $startx - $linew;
} else {
$this->endlinex = $startx + $linew;
}
$w = $linew;
$tmpcellpadding = $this->cell_padding;
if ($maxh == 0) {
$this->SetCellPadding(0);
}
}
if ($firstblock AND $this->isRTLTextDir()) {
$tmpstr = $this->stringRightTrim($tmpstr);
}
$this->Cell($w, $h, $tmpstr, 0, 1, $align, $fill, $link, $stretch);
unset($tmpstr);
if ($firstline) {
$this->cell_padding = $tmpcellpadding;
return (TCPDF_FONTS::UniArrSubString($uchars, $i));
}
$j = $i;
--$i;
}
} else {
// word wrapping
if ($this->rtl AND (!$firstblock) AND ($sep < $i)) {
$endspace = 1;
} else {
$endspace = 0;
}
// check the length of the next string
$strrest = TCPDF_FONTS::UniArrSubString($uchars, ($sep + $endspace));
$nextstr = preg_split('/'.$this->re_space['p'].'/'.$this->re_space['m'], $this->stringTrim($strrest));
if (isset($nextstr[0]) AND ($this->GetStringWidth($nextstr[0]) > $pw)) {
// truncate the word because do not fit on a full page width
$tmpstr = TCPDF_FONTS::UniArrSubString($uchars, $j, $i);
if ($firstline) {
$startx = $this->x;
$tmparr = array_slice($chars, $j, ($i - $j));
if ($rtlmode) {
$tmparr = TCPDF_FONTS::utf8Bidi($tmparr, $tmpstr, $this->tmprtl, $this->isunicode, $this->CurrentFont);
}
$linew = $this->GetArrStringWidth($tmparr);
unset($tmparr);
if ($this->rtl) {
$this->endlinex = ($startx - $linew);
} else {
$this->endlinex = ($startx + $linew);
}
$w = $linew;
$tmpcellpadding = $this->cell_padding;
if ($maxh == 0) {
$this->SetCellPadding(0);
}
}
if ($firstblock AND $this->isRTLTextDir()) {
$tmpstr = $this->stringRightTrim($tmpstr);
}
$this->Cell($w, $h, $tmpstr, 0, 1, $align, $fill, $link, $stretch);
unset($tmpstr);
if ($firstline) {
$this->cell_padding = $tmpcellpadding;
return (TCPDF_FONTS::UniArrSubString($uchars, $i));
}
$j = $i;
--$i;
} else {
// word wrapping
if ($shy) {
// add hypen (minus symbol) at the end of the line
$shy_width = $tmp_shy_replacement_width;
if ($this->rtl) {
$shy_char_left = $tmp_shy_replacement_char;
$shy_char_right = '';
} else {
$shy_char_left = '';
$shy_char_right = $tmp_shy_replacement_char;
}
} else {
$shy_width = 0;
$shy_char_left = '';
$shy_char_right = '';
}
$tmpstr = TCPDF_FONTS::UniArrSubString($uchars, $j, ($sep + $endspace));
if ($firstline) {
$startx = $this->x;
$tmparr = array_slice($chars, $j, (($sep + $endspace) - $j));
if ($rtlmode) {
$tmparr = TCPDF_FONTS::utf8Bidi($tmparr, $tmpstr, $this->tmprtl, $this->isunicode, $this->CurrentFont);
}
$linew = $this->GetArrStringWidth($tmparr);
unset($tmparr);
if ($this->rtl) {
$this->endlinex = $startx - $linew - $shy_width;
} else {
$this->endlinex = $startx + $linew + $shy_width;
}
$w = $linew;
$tmpcellpadding = $this->cell_padding;
if ($maxh == 0) {
$this->SetCellPadding(0);
}
}
// print the line
if ($firstblock AND $this->isRTLTextDir()) {
$tmpstr = $this->stringRightTrim($tmpstr);
}
$this->Cell($w, $h, $shy_char_left.$tmpstr.$shy_char_right, 0, 1, $align, $fill, $link, $stretch);
unset($tmpstr);
if ($firstline) {
if ($chars[$sep] == 45) {
$endspace += 1;
}
// return the remaining text
$this->cell_padding = $tmpcellpadding;
return (TCPDF_FONTS::UniArrSubString($uchars, ($sep + $endspace)));
}
$i = $sep;
$sep = -1;
$shy = false;
$j = ($i + 1);
}
}
// account for margin changes
if ((($this->y + $this->lasth) > $this->PageBreakTrigger) AND ($this->inPageBody())) {
$this->AcceptPageBreak();
if ($this->rtl) {
$this->x -= $margin['R'];
} else {
$this->x += $margin['L'];
}
$this->lMargin += $margin['L'];
$this->rMargin += $margin['R'];
}
$w = $this->getRemainingWidth();
$wmax = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
if ($linebreak) {
$linebreak = false;
} else {
++$nl;
$l = 0;
}
}
}
// save last character
$pc = $c;
++$i;
} // end while i < nb
// print last substring (if any)
if ($l > 0) {
switch ($align) {
case 'J':
case 'C': {
$w = $w;
break;
}
case 'L': {
if ($this->rtl) {
$w = $w;
} else {
$w = $l;
}
break;
}
case 'R': {
if ($this->rtl) {
$w = $l;
} else {
$w = $w;
}
break;
}
default: {
$w = $l;
break;
}
}
$tmpstr = TCPDF_FONTS::UniArrSubString($uchars, $j, $nb);
if ($firstline) {
$startx = $this->x;
$tmparr = array_slice($chars, $j, ($nb - $j));
if ($rtlmode) {
$tmparr = TCPDF_FONTS::utf8Bidi($tmparr, $tmpstr, $this->tmprtl, $this->isunicode, $this->CurrentFont);
}
$linew = $this->GetArrStringWidth($tmparr);
unset($tmparr);
if ($this->rtl) {
$this->endlinex = $startx - $linew;
} else {
$this->endlinex = $startx + $linew;
}
$w = $linew;
$tmpcellpadding = $this->cell_padding;
if ($maxh == 0) {
$this->SetCellPadding(0);
}
}
if ($firstblock AND $this->isRTLTextDir()) {
$tmpstr = $this->stringRightTrim($tmpstr);
}
$this->Cell($w, $h, $tmpstr, 0, $ln, $align, $fill, $link, $stretch);
unset($tmpstr);
if ($firstline) {
$this->cell_padding = $tmpcellpadding;
return (TCPDF_FONTS::UniArrSubString($uchars, $nb));
}
++$nl;
}
if ($firstline) {
return '';
}
return $nl;
}
/**
* Returns the remaining width between the current position and margins.
* @return int Return the remaining width
* @protected
*/
protected function getRemainingWidth() {
list($this->x, $this->y) = $this->checkPageRegions(0, $this->x, $this->y);
if ($this->rtl) {
return ($this->x - $this->lMargin);
} else {
return ($this->w - $this->rMargin - $this->x);
}
}
/**
* Set the block dimensions accounting for page breaks and page/column fitting
* @param $w (float) width
* @param $h (float) height
* @param $x (float) X coordinate
* @param $y (float) Y coodiante
* @param $fitonpage (boolean) if true the block is resized to not exceed page dimensions.
* @return array($w, $h, $x, $y)
* @protected
* @since 5.5.009 (2010-07-05)
*/
protected function fitBlock($w, $h, $x, $y, $fitonpage=false) {
if ($w <= 0) {
// set maximum width
$w = ($this->w - $this->lMargin - $this->rMargin);
}
if ($h <= 0) {
// set maximum height
$h = ($this->PageBreakTrigger - $this->tMargin);
}
// resize the block to be vertically contained on a single page or single column
if ($fitonpage OR $this->AutoPageBreak) {
$ratio_wh = ($w / $h);
if ($h > ($this->PageBreakTrigger - $this->tMargin)) {
$h = $this->PageBreakTrigger - $this->tMargin;
$w = ($h * $ratio_wh);
}
// resize the block to be horizontally contained on a single page or single column
if ($fitonpage) {
$maxw = ($this->w - $this->lMargin - $this->rMargin);
if ($w > $maxw) {
$w = $maxw;
$h = ($w / $ratio_wh);
}
}
}
// Check whether we need a new page or new column first as this does not fit
$prev_x = $this->x;
$prev_y = $this->y;
if ($this->checkPageBreak($h, $y) OR ($this->y < $prev_y)) {
$y = $this->y;
if ($this->rtl) {
$x += ($prev_x - $this->x);
} else {
$x += ($this->x - $prev_x);
}
$this->newline = true;
}
// resize the block to be contained on the remaining available page or column space
if ($fitonpage) {
$ratio_wh = ($w / $h);
if (($y + $h) > $this->PageBreakTrigger) {
$h = $this->PageBreakTrigger - $y;
$w = ($h * $ratio_wh);
}
if ((!$this->rtl) AND (($x + $w) > ($this->w - $this->rMargin))) {
$w = $this->w - $this->rMargin - $x;
$h = ($w / $ratio_wh);
} elseif (($this->rtl) AND (($x - $w) < ($this->lMargin))) {
$w = $x - $this->lMargin;
$h = ($w / $ratio_wh);
}
}
return array($w, $h, $x, $y);
}
/**
* Puts an image in the page.
* The upper-left corner must be given.
* The dimensions can be specified in different ways:
*
explicit width and height (expressed in user unit)
*
one explicit dimension, the other being calculated automatically in order to keep the original proportions
*
no explicit dimension, in which case the image is put at 72 dpi
* Supported formats are JPEG and PNG images whitout GD library and all images supported by GD: GD, GD2, GD2PART, GIF, JPEG, PNG, BMP, XBM, XPM;
* The format can be specified explicitly or inferred from the file extension.
* It is possible to put a link on the image.
* Remark: if an image is used several times, only one copy will be embedded in the file.
* @param $file (string) Name of the file containing the image or a '@' character followed by the image data string. To link an image without embedding it on the document, set an asterisk character before the URL (i.e.: '*http://www.example.com/image.jpg').
* @param $x (float) Abscissa of the upper-left corner (LTR) or upper-right corner (RTL).
* @param $y (float) Ordinate of the upper-left corner (LTR) or upper-right corner (RTL).
* @param $w (float) Width of the image in the page. If not specified or equal to zero, it is automatically calculated.
* @param $h (float) Height of the image in the page. If not specified or equal to zero, it is automatically calculated.
* @param $type (string) Image format. Possible values are (case insensitive): JPEG and PNG (whitout GD library) and all images supported by GD: GD, GD2, GD2PART, GIF, JPEG, PNG, BMP, XBM, XPM;. If not specified, the type is inferred from the file extension.
* @param $link (mixed) URL or identifier returned by AddLink().
* @param $align (string) Indicates the alignment of the pointer next to image insertion relative to image height. The value can be:
T: top-right for LTR or top-left for RTL
M: middle-right for LTR or middle-left for RTL
B: bottom-right for LTR or bottom-left for RTL
N: next line
* @param $resize (mixed) If true resize (reduce) the image to fit $w and $h (requires GD or ImageMagick library); if false do not resize; if 2 force resize in all cases (upscaling and downscaling).
* @param $dpi (int) dot-per-inch resolution used on resize
* @param $palign (string) Allows to center or align the image on the current line. Possible values are:
L : left align
C : center
R : right align
'' : empty string : left for LTR or right for RTL
* @param $ismask (boolean) true if this image is a mask, false otherwise
* @param $imgmask (mixed) image object returned by this function or false
* @param $border (mixed) Indicates if borders must be drawn around the cell. The value can be a number:
0: no border (default)
1: frame
or a string containing some or all of the following characters (in any order):
L: left
T: top
R: right
B: bottom
or an array of line styles for each border group - for example: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
* @param $fitbox (mixed) If not false scale image dimensions proportionally to fit within the ($w, $h) box. $fitbox can be true or a 2 characters string indicating the image alignment inside the box. The first character indicate the horizontal alignment (L = left, C = center, R = right) the second character indicate the vertical algnment (T = top, M = middle, B = bottom).
* @param $hidden (boolean) If true do not display the image.
* @param $fitonpage (boolean) If true the image is resized to not exceed page dimensions.
* @param $alt (boolean) If true the image will be added as alternative and not directly printed (the ID of the image will be returned).
* @param $altimgs (array) Array of alternate images IDs. Each alternative image must be an array with two values: an integer representing the image ID (the value returned by the Image method) and a boolean value to indicate if the image is the default for printing.
* @return image information
* @public
* @since 1.1
*/
public function Image($file, $x='', $y='', $w=0, $h=0, $type='', $link='', $align='', $resize=false, $dpi=300, $palign='', $ismask=false, $imgmask=false, $border=0, $fitbox=false, $hidden=false, $fitonpage=false, $alt=false, $altimgs=array()) {
if ($this->state != 2) {
return;
}
if ($x === '') {
$x = $this->x;
}
if ($y === '') {
$y = $this->y;
}
// check page for no-write regions and adapt page margins if necessary
list($x, $y) = $this->checkPageRegions($h, $x, $y);
$exurl = ''; // external streams
// check if we are passing an image as file or string
if ($file[0] === '@') {
// image from string
$imgdata = substr($file, 1);
} else { // image file
if ($file{0} === '*') {
// image as external stream
$file = substr($file, 1);
$exurl = $file;
}
// check if is local file
if (!@file_exists($file)) {
// encode spaces on filename (file is probably an URL)
$file = str_replace(' ', '%20', $file);
}
if (@file_exists($file)) {
// get image dimensions
$imsize = @getimagesize($file);
} else {
$imsize = FALSE;
}
if ($imsize === FALSE) {
if (function_exists('curl_init')) {
// try to get remote file data using cURL
$cs = curl_init(); // curl session
curl_setopt($cs, CURLOPT_URL, $file);
curl_setopt($cs, CURLOPT_BINARYTRANSFER, true);
curl_setopt($cs, CURLOPT_FAILONERROR, true);
curl_setopt($cs, CURLOPT_RETURNTRANSFER, true);
if ((ini_get('open_basedir') == '') AND (!ini_get('safe_mode'))) {
curl_setopt($cs, CURLOPT_FOLLOWLOCATION, true);
}
curl_setopt($cs, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($cs, CURLOPT_TIMEOUT, 30);
curl_setopt($cs, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($cs, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($cs, CURLOPT_USERAGENT, 'TCPDF');
$imgdata = curl_exec($cs);
curl_close($cs);
} else {
$imgdata = @file_get_contents($file);
}
}
}
if (isset($imgdata) AND ($imgdata !== FALSE)) {
// copy image to cache
$file = TCPDF_STATIC::getObjFilename('img');
$fp = fopen($file, 'w');
fwrite($fp, $imgdata);
fclose($fp);
unset($imgdata);
$imsize = @getimagesize($file);
if ($imsize === FALSE) {
unlink($file);
} else {
$this->cached_files[] = $file;
}
}
if ($imsize === FALSE) {
if (($w > 0) AND ($h > 0)) {
// get measures from specified data
$pw = $this->getHTMLUnitToUnits($w, 0, $this->pdfunit, true) * $this->imgscale * $this->k;
$ph = $this->getHTMLUnitToUnits($h, 0, $this->pdfunit, true) * $this->imgscale * $this->k;
$imsize = array($pw, $ph);
} else {
$this->Error('[Image] Unable to get image: '.$file);
}
}
// file hash
$filehash = md5($this->file_id.$file);
// get original image width and height in pixels
list($pixw, $pixh) = $imsize;
// calculate image width and height on document
if (($w <= 0) AND ($h <= 0)) {
// convert image size to document unit
$w = $this->pixelsToUnits($pixw);
$h = $this->pixelsToUnits($pixh);
} elseif ($w <= 0) {
$w = $h * $pixw / $pixh;
} elseif ($h <= 0) {
$h = $w * $pixh / $pixw;
} elseif (($fitbox !== false) AND ($w > 0) AND ($h > 0)) {
if (strlen($fitbox) !== 2) {
// set default alignment
$fitbox = '--';
}
// scale image dimensions proportionally to fit within the ($w, $h) box
if ((($w * $pixh) / ($h * $pixw)) < 1) {
// store current height
$oldh = $h;
// calculate new height
$h = $w * $pixh / $pixw;
// height difference
$hdiff = ($oldh - $h);
// vertical alignment
switch (strtoupper($fitbox{1})) {
case 'T': {
break;
}
case 'M': {
$y += ($hdiff / 2);
break;
}
case 'B': {
$y += $hdiff;
break;
}
}
} else {
// store current width
$oldw = $w;
// calculate new width
$w = $h * $pixw / $pixh;
// width difference
$wdiff = ($oldw - $w);
// horizontal alignment
switch (strtoupper($fitbox{0})) {
case 'L': {
if ($this->rtl) {
$x -= $wdiff;
}
break;
}
case 'C': {
if ($this->rtl) {
$x -= ($wdiff / 2);
} else {
$x += ($wdiff / 2);
}
break;
}
case 'R': {
if (!$this->rtl) {
$x += $wdiff;
}
break;
}
}
}
}
// fit the image on available space
list($w, $h, $x, $y) = $this->fitBlock($w, $h, $x, $y, $fitonpage);
// calculate new minimum dimensions in pixels
$neww = round($w * $this->k * $dpi / $this->dpi);
$newh = round($h * $this->k * $dpi / $this->dpi);
// check if resize is necessary (resize is used only to reduce the image)
$newsize = ($neww * $newh);
$pixsize = ($pixw * $pixh);
if (intval($resize) == 2) {
$resize = true;
} elseif ($newsize >= $pixsize) {
$resize = false;
}
// check if image has been already added on document
$newimage = true;
if (in_array($file, $this->imagekeys)) {
$newimage = false;
// get existing image data
$info = $this->getImageBuffer($file);
if (substr($file, 0, -34) != K_PATH_CACHE.'msk') {
// check if the newer image is larger
$oldsize = ($info['w'] * $info['h']);
if ((($oldsize < $newsize) AND ($resize)) OR (($oldsize < $pixsize) AND (!$resize))) {
$newimage = true;
}
}
} elseif (substr($file, 0, -34) != K_PATH_CACHE.'msk') {
// check for cached images with alpha channel
$tempfile_plain = K_PATH_CACHE.'mskp_'.$filehash;
$tempfile_alpha = K_PATH_CACHE.'mska_'.$filehash;
if (in_array($tempfile_plain, $this->imagekeys)) {
// get existing image data
$info = $this->getImageBuffer($tempfile_plain);
// check if the newer image is larger
$oldsize = ($info['w'] * $info['h']);
if ((($oldsize < $newsize) AND ($resize)) OR (($oldsize < $pixsize) AND (!$resize))) {
$newimage = true;
} else {
$newimage = false;
// embed mask image
$imgmask = $this->Image($tempfile_alpha, $x, $y, $w, $h, 'PNG', '', '', $resize, $dpi, '', true, false);
// embed image, masked with previously embedded mask
return $this->Image($tempfile_plain, $x, $y, $w, $h, $type, $link, $align, $resize, $dpi, $palign, false, $imgmask);
}
}
}
if ($newimage) {
//First use of image, get info
$type = strtolower($type);
if ($type == '') {
$type = TCPDF_IMAGES::getImageFileType($file, $imsize);
} elseif ($type == 'jpg') {
$type = 'jpeg';
}
$mqr = TCPDF_STATIC::get_mqr();
TCPDF_STATIC::set_mqr(false);
// Specific image handlers (defined on TCPDF_IMAGES CLASS)
$mtd = '_parse'.$type;
// GD image handler function
$gdfunction = 'imagecreatefrom'.$type;
$info = false;
if ((method_exists('TCPDF_IMAGES', $mtd)) AND (!($resize AND (function_exists($gdfunction) OR extension_loaded('imagick'))))) {
// TCPDF image functions
$info = TCPDF_IMAGES::$mtd($file);
if ($info == 'pngalpha') {
return $this->ImagePngAlpha($file, $x, $y, $pixw, $pixh, $w, $h, 'PNG', $link, $align, $resize, $dpi, $palign, $filehash);
}
}
if (!$info) {
if (function_exists($gdfunction)) {
// GD library
$img = $gdfunction($file);
if ($resize) {
$imgr = imagecreatetruecolor($neww, $newh);
if (($type == 'gif') OR ($type == 'png')) {
$imgr = TCPDF_IMAGES::setGDImageTransparency($imgr, $img);
}
imagecopyresampled($imgr, $img, 0, 0, 0, 0, $neww, $newh, $pixw, $pixh);
if (($type == 'gif') OR ($type == 'png')) {
$info = TCPDF_IMAGES::_toPNG($imgr);
} else {
$info = TCPDF_IMAGES::_toJPEG($imgr, $this->jpeg_quality);
}
} else {
if (($type == 'gif') OR ($type == 'png')) {
$info = TCPDF_IMAGES::_toPNG($img);
} else {
$info = TCPDF_IMAGES::_toJPEG($img, $this->jpeg_quality);
}
}
} elseif (extension_loaded('imagick')) {
// ImageMagick library
$img = new Imagick();
if ($type == 'SVG') {
// get SVG file content
$svgimg = file_get_contents($file);
// get width and height
$regs = array();
if (preg_match('/_textstring($title, $oid);
$out .= ' /Parent '.($n + $o['parent']).' 0 R';
if (isset($o['prev'])) {
$out .= ' /Prev '.($n + $o['prev']).' 0 R';
}
if (isset($o['next'])) {
$out .= ' /Next '.($n + $o['next']).' 0 R';
}
if (isset($o['first'])) {
$out .= ' /First '.($n + $o['first']).' 0 R';
}
if (isset($o['last'])) {
$out .= ' /Last '.($n + $o['last']).' 0 R';
}
if (isset($o['u']) AND !empty($o['u'])) {
// link
if (is_string($o['u'])) {
if ($o['u'][0] == '#') {
// internal destination
$out .= ' /Dest /'.TCPDF_STATIC::encodeNameObject(substr($o['u'], 1));
} elseif ($o['u'][0] == '%') {
// embedded PDF file
$filename = basename(substr($o['u'], 1));
$out .= ' /A <embeddedfiles[$filename]['a'].' >> >>';
} elseif ($o['u'][0] == '*') {
// embedded generic file
$filename = basename(substr($o['u'], 1));
$jsa = 'var D=event.target.doc;var MyData=D.dataObjects;for (var i in MyData) if (MyData[i].path=="'.$filename.'") D.exportDataObject( { cName : MyData[i].name, nLaunch : 2});';
$out .= ' /A <_textstring($jsa, $oid).'>>';
} else {
// external URI link
$out .= ' /A <_datastring($this->unhtmlentities($o['u']), $oid).'>>';
}
} elseif (isset($this->links[$o['u']])) {
// internal link ID
$l = $this->links[$o['u']];
if (isset($this->page_obj_id[($l[0])])) {
$out .= sprintf(' /Dest [%u 0 R /XYZ 0 %F null]', $this->page_obj_id[($l[0])], ($this->pagedim[$l[0]]['h'] - ($l[1] * $this->k)));
}
}
} elseif (isset($this->page_obj_id[($o['p'])])) {
// link to a page
$out .= ' '.sprintf('/Dest [%u 0 R /XYZ %F %F null]', $this->page_obj_id[($o['p'])], ($o['x'] * $this->k), ($this->pagedim[$o['p']]['h'] - ($o['y'] * $this->k)));
}
// set font style
$style = 0;
if (!empty($o['s'])) {
// bold
if (strpos($o['s'], 'B') !== false) {
$style |= 2;
}
// oblique
if (strpos($o['s'], 'I') !== false) {
$style |= 1;
}
}
$out .= sprintf(' /F %d', $style);
// set bookmark color
if (isset($o['c']) AND is_array($o['c']) AND (count($o['c']) == 3)) {
$color = array_values($o['c']);
$out .= sprintf(' /C [%F %F %F]', ($color[0] / 255), ($color[1] / 255), ($color[2] / 255));
} else {
// black
$out .= ' /C [0.0 0.0 0.0]';
}
$out .= ' /Count 0'; // normally closed item
$out .= ' >>';
$out .= "\n".'endobj';
$this->_out($out);
}
//Outline root
$this->OutlineRoot = $this->_newobj();
$this->_out('<< /Type /Outlines /First '.$n.' 0 R /Last '.($n + $lru[0]).' 0 R >>'."\n".'endobj');
}
// --- JAVASCRIPT ------------------------------------------------------
/**
* Adds a javascript
* @param $script (string) Javascript code
* @public
* @author Johannes Gntert, Nicola Asuni
* @since 2.1.002 (2008-02-12)
*/
public function IncludeJS($script) {
$this->javascript .= $script;
}
/**
* Adds a javascript object and return object ID
* @param $script (string) Javascript code
* @param $onload (boolean) if true executes this object when opening the document
* @return int internal object ID
* @public
* @author Nicola Asuni
* @since 4.8.000 (2009-09-07)
*/
public function addJavascriptObject($script, $onload=false) {
if ($this->pdfa_mode) {
// javascript is not allowed in PDF/A mode
return false;
}
++$this->n;
$this->js_objects[$this->n] = array('n' => $this->n, 'js' => $script, 'onload' => $onload);
return $this->n;
}
/**
* Create a javascript PDF string.
* @protected
* @author Johannes Gntert, Nicola Asuni
* @since 2.1.002 (2008-02-12)
*/
protected function _putjavascript() {
if ($this->pdfa_mode OR (empty($this->javascript) AND empty($this->js_objects))) {
return;
}
if (strpos($this->javascript, 'this.addField') > 0) {
if (!$this->ur['enabled']) {
//$this->setUserRights();
}
// the following two lines are used to avoid form fields duplication after saving
// The addField method only works when releasing user rights (UR3)
$jsa = sprintf("ftcpdfdocsaved=this.addField('%s','%s',%d,[%F,%F,%F,%F]);", 'tcpdfdocsaved', 'text', 0, 0, 1, 0, 1);
$jsb = "getField('tcpdfdocsaved').value='saved';";
$this->javascript = $jsa."\n".$this->javascript."\n".$jsb;
}
// name tree for javascript
$this->n_js = '<< /Names [';
if (!empty($this->javascript)) {
$this->n_js .= ' (EmbeddedJS) '.($this->n + 1).' 0 R';
}
if (!empty($this->js_objects)) {
foreach ($this->js_objects as $key => $val) {
if ($val['onload']) {
$this->n_js .= ' (JS'.$key.') '.$key.' 0 R';
}
}
}
$this->n_js .= ' ] >>';
// default Javascript object
if (!empty($this->javascript)) {
$obj_id = $this->_newobj();
$out = '<< /S /JavaScript';
$out .= ' /JS '.$this->_textstring($this->javascript, $obj_id);
$out .= ' >>';
$out .= "\n".'endobj';
$this->_out($out);
}
// additional Javascript objects
if (!empty($this->js_objects)) {
foreach ($this->js_objects as $key => $val) {
$out = $this->_getobj($key)."\n".' << /S /JavaScript /JS '.$this->_textstring($val['js'], $key).' >>'."\n".'endobj';
$this->_out($out);
}
}
}
/**
* Adds a javascript form field.
* @param $type (string) field type
* @param $name (string) field name
* @param $x (int) horizontal position
* @param $y (int) vertical position
* @param $w (int) width
* @param $h (int) height
* @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
* @protected
* @author Denis Van Nuffelen, Nicola Asuni
* @since 2.1.002 (2008-02-12)
*/
protected function _addfield($type, $name, $x, $y, $w, $h, $prop) {
if ($this->rtl) {
$x = $x - $w;
}
// the followind avoid fields duplication after saving the document
$this->javascript .= "if (getField('tcpdfdocsaved').value != 'saved') {";
$k = $this->k;
$this->javascript .= sprintf("f".$name."=this.addField('%s','%s',%u,[%F,%F,%F,%F]);", $name, $type, $this->PageNo()-1, $x*$k, ($this->h-$y)*$k+1, ($x+$w)*$k, ($this->h-$y-$h)*$k+1)."\n";
$this->javascript .= 'f'.$name.'.textSize='.$this->FontSizePt.";\n";
while (list($key, $val) = each($prop)) {
if (strcmp(substr($key, -5), 'Color') == 0) {
$val = TCPDF_COLORS::_JScolor($val);
} else {
$val = "'".$val."'";
}
$this->javascript .= 'f'.$name.'.'.$key.'='.$val.";\n";
}
if ($this->rtl) {
$this->x -= $w;
} else {
$this->x += $w;
}
$this->javascript .= '}';
}
// --- FORM FIELDS -----------------------------------------------------
/**
* Set default properties for form fields.
* @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
* @public
* @author Nicola Asuni
* @since 4.8.000 (2009-09-06)
*/
public function setFormDefaultProp($prop=array()) {
$this->default_form_prop = $prop;
}
/**
* Return the default properties for form fields.
* @return array $prop javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
* @public
* @author Nicola Asuni
* @since 4.8.000 (2009-09-06)
*/
public function getFormDefaultProp() {
return $this->default_form_prop;
}
/**
* Creates a text field
* @param $name (string) field name
* @param $w (float) Width of the rectangle
* @param $h (float) Height of the rectangle
* @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
* @param $opt (array) annotation parameters. Possible values are described on official PDF32000_2008 reference.
* @param $x (float) Abscissa of the upper-left corner of the rectangle
* @param $y (float) Ordinate of the upper-left corner of the rectangle
* @param $js (boolean) if true put the field using JavaScript (requires Acrobat Writer to be rendered).
* @public
* @author Nicola Asuni
* @since 4.8.000 (2009-09-07)
*/
public function TextField($name, $w, $h, $prop=array(), $opt=array(), $x='', $y='', $js=false) {
if ($x === '') {
$x = $this->x;
}
if ($y === '') {
$y = $this->y;
}
// check page for no-write regions and adapt page margins if necessary
list($x, $y) = $this->checkPageRegions($h, $x, $y);
if ($js) {
$this->_addfield('text', $name, $x, $y, $w, $h, $prop);
return;
}
// get default style
$prop = array_merge($this->getFormDefaultProp(), $prop);
// get annotation data
$popt = TCPDF_STATIC::getAnnotOptFromJSProp($prop, $this->spot_colors, $this->rtl);
// set default appearance stream
$this->annotation_fonts[$this->CurrentFont['fontkey']] = $this->CurrentFont['i'];
$fontstyle = sprintf('/F%d %F Tf %s', $this->CurrentFont['i'], $this->FontSizePt, $this->TextColor);
$popt['da'] = $fontstyle;
// build appearance stream
$popt['ap'] = array();
$popt['ap']['n'] = '/Tx BMC q '.$fontstyle.' ';
$text = '';
if (isset($prop['value']) AND !empty($prop['value'])) {
$text = $prop['value'];
} elseif (isset($opt['v']) AND !empty($opt['v'])) {
$text = $opt['v'];
}
$tmpid = $this->startTemplate($w, $h, false);
$align = '';
if (isset($popt['q'])) {
switch ($popt['q']) {
case 0: {
$align = 'L';
break;
}
case 1: {
$align = 'C';
break;
}
case 2: {
$align = 'R';
break;
}
default: {
$align = '';
break;
}
}
}
$this->MultiCell($w, $h, $text, 0, $align, false, 0, 0, 0, true, 0, false, true, 0, 'T', false);
$this->endTemplate();
--$this->n;
$popt['ap']['n'] .= $this->xobjects[$tmpid]['outdata'];
unset($this->xobjects[$tmpid]);
$popt['ap']['n'] .= 'Q EMC';
// merge options
$opt = array_merge($popt, $opt);
// remove some conflicting options
unset($opt['bs']);
// set remaining annotation data
$opt['Subtype'] = 'Widget';
$opt['ft'] = 'Tx';
$opt['t'] = $name;
// Additional annotation's parameters (check _putannotsobj() method):
//$opt['f']
//$opt['as']
//$opt['bs']
//$opt['be']
//$opt['c']
//$opt['border']
//$opt['h']
//$opt['mk'];
//$opt['mk']['r']
//$opt['mk']['bc'];
//$opt['mk']['bg'];
unset($opt['mk']['ca']);
unset($opt['mk']['rc']);
unset($opt['mk']['ac']);
unset($opt['mk']['i']);
unset($opt['mk']['ri']);
unset($opt['mk']['ix']);
unset($opt['mk']['if']);
//$opt['mk']['if']['sw'];
//$opt['mk']['if']['s'];
//$opt['mk']['if']['a'];
//$opt['mk']['if']['fb'];
unset($opt['mk']['tp']);
//$opt['tu']
//$opt['tm']
//$opt['ff']
//$opt['v']
//$opt['dv']
//$opt['a']
//$opt['aa']
//$opt['q']
$this->Annotation($x, $y, $w, $h, $name, $opt, 0);
if ($this->rtl) {
$this->x -= $w;
} else {
$this->x += $w;
}
}
/**
* Creates a RadioButton field.
* @param $name (string) Field name.
* @param $w (int) Width of the radio button.
* @param $prop (array) Javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
* @param $opt (array) Annotation parameters. Possible values are described on official PDF32000_2008 reference.
* @param $onvalue (string) Value to be returned if selected.
* @param $checked (boolean) Define the initial state.
* @param $x (float) Abscissa of the upper-left corner of the rectangle
* @param $y (float) Ordinate of the upper-left corner of the rectangle
* @param $js (boolean) If true put the field using JavaScript (requires Acrobat Writer to be rendered).
* @public
* @author Nicola Asuni
* @since 4.8.000 (2009-09-07)
*/
public function RadioButton($name, $w, $prop=array(), $opt=array(), $onvalue='On', $checked=false, $x='', $y='', $js=false) {
if ($x === '') {
$x = $this->x;
}
if ($y === '') {
$y = $this->y;
}
// check page for no-write regions and adapt page margins if necessary
list($x, $y) = $this->checkPageRegions($w, $x, $y);
if ($js) {
$this->_addfield('radiobutton', $name, $x, $y, $w, $w, $prop);
return;
}
if (TCPDF_STATIC::empty_string($onvalue)) {
$onvalue = 'On';
}
if ($checked) {
$defval = $onvalue;
} else {
$defval = 'Off';
}
// set font
$font = 'zapfdingbats';
if ($this->pdfa_mode) {
// all fonts must be embedded
$font = 'pdfa'.$font;
}
$this->AddFont($font);
$tmpfont = $this->getFontBuffer($font);
// set data for parent group
if (!isset($this->radiobutton_groups[$this->page])) {
$this->radiobutton_groups[$this->page] = array();
}
if (!isset($this->radiobutton_groups[$this->page][$name])) {
$this->radiobutton_groups[$this->page][$name] = array();
++$this->n;
$this->radiobutton_groups[$this->page][$name]['n'] = $this->n;
$this->radio_groups[] = $this->n;
}
$kid = ($this->n + 1);
// save object ID to be added on Kids entry on parent object
$this->radiobutton_groups[$this->page][$name][] = array('kid' => $kid, 'def' => $defval);
// get default style
$prop = array_merge($this->getFormDefaultProp(), $prop);
$prop['NoToggleToOff'] = 'true';
$prop['Radio'] = 'true';
$prop['borderStyle'] = 'inset';
// get annotation data
$popt = TCPDF_STATIC::getAnnotOptFromJSProp($prop, $this->spot_colors, $this->rtl);
// set additional default options
$this->annotation_fonts[$tmpfont['fontkey']] = $tmpfont['i'];
$fontstyle = sprintf('/F%d %F Tf %s', $tmpfont['i'], $this->FontSizePt, $this->TextColor);
$popt['da'] = $fontstyle;
// build appearance stream
$popt['ap'] = array();
$popt['ap']['n'] = array();
$fx = ((($w - $this->getAbsFontMeasure($tmpfont['cw'][108])) / 2) * $this->k);
$fy = (($w - ((($tmpfont['desc']['Ascent'] - $tmpfont['desc']['Descent']) * $this->FontSizePt / 1000) / $this->k)) * $this->k);
$popt['ap']['n'][$onvalue] = sprintf('q %s BT /F%d %F Tf %F %F Td ('.chr(108).') Tj ET Q', $this->TextColor, $tmpfont['i'], $this->FontSizePt, $fx, $fy);
$popt['ap']['n']['Off'] = sprintf('q %s BT /F%d %F Tf %F %F Td ('.chr(109).') Tj ET Q', $this->TextColor, $tmpfont['i'], $this->FontSizePt, $fx, $fy);
if (!isset($popt['mk'])) {
$popt['mk'] = array();
}
$popt['mk']['ca'] = '(l)';
// merge options
$opt = array_merge($popt, $opt);
// set remaining annotation data
$opt['Subtype'] = 'Widget';
$opt['ft'] = 'Btn';
if ($checked) {
$opt['v'] = array('/'.$onvalue);
$opt['as'] = $onvalue;
} else {
$opt['as'] = 'Off';
}
// store readonly flag
if (!isset($this->radiobutton_groups[$this->page][$name]['#readonly#'])) {
$this->radiobutton_groups[$this->page][$name]['#readonly#'] = false;
}
$this->radiobutton_groups[$this->page][$name]['#readonly#'] |= ($opt['f'] & 64);
$this->Annotation($x, $y, $w, $w, $name, $opt, 0);
if ($this->rtl) {
$this->x -= $w;
} else {
$this->x += $w;
}
}
/**
* Creates a List-box field
* @param $name (string) field name
* @param $w (int) width
* @param $h (int) height
* @param $values (array) array containing the list of values.
* @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
* @param $opt (array) annotation parameters. Possible values are described on official PDF32000_2008 reference.
* @param $x (float) Abscissa of the upper-left corner of the rectangle
* @param $y (float) Ordinate of the upper-left corner of the rectangle
* @param $js (boolean) if true put the field using JavaScript (requires Acrobat Writer to be rendered).
* @public
* @author Nicola Asuni
* @since 4.8.000 (2009-09-07)
*/
public function ListBox($name, $w, $h, $values, $prop=array(), $opt=array(), $x='', $y='', $js=false) {
if ($x === '') {
$x = $this->x;
}
if ($y === '') {
$y = $this->y;
}
// check page for no-write regions and adapt page margins if necessary
list($x, $y) = $this->checkPageRegions($h, $x, $y);
if ($js) {
$this->_addfield('listbox', $name, $x, $y, $w, $h, $prop);
$s = '';
foreach ($values as $value) {
if (is_array($value)) {
$s .= ',[\''.addslashes($value[1]).'\',\''.addslashes($value[0]).'\']';
} else {
$s .= ',[\''.addslashes($value).'\',\''.addslashes($value).'\']';
}
}
$this->javascript .= 'f'.$name.'.setItems('.substr($s, 1).');'."\n";
return;
}
// get default style
$prop = array_merge($this->getFormDefaultProp(), $prop);
// get annotation data
$popt = TCPDF_STATIC::getAnnotOptFromJSProp($prop, $this->spot_colors, $this->rtl);
// set additional default values
$this->annotation_fonts[$this->CurrentFont['fontkey']] = $this->CurrentFont['i'];
$fontstyle = sprintf('/F%d %F Tf %s', $this->CurrentFont['i'], $this->FontSizePt, $this->TextColor);
$popt['da'] = $fontstyle;
// build appearance stream
$popt['ap'] = array();
$popt['ap']['n'] = '/Tx BMC q '.$fontstyle.' ';
$text = '';
foreach($values as $item) {
if (is_array($item)) {
$text .= $item[1]."\n";
} else {
$text .= $item."\n";
}
}
$tmpid = $this->startTemplate($w, $h, false);
$this->MultiCell($w, $h, $text, 0, '', false, 0, 0, 0, true, 0, false, true, 0, 'T', false);
$this->endTemplate();
--$this->n;
$popt['ap']['n'] .= $this->xobjects[$tmpid]['outdata'];
unset($this->xobjects[$tmpid]);
$popt['ap']['n'] .= 'Q EMC';
// merge options
$opt = array_merge($popt, $opt);
// set remaining annotation data
$opt['Subtype'] = 'Widget';
$opt['ft'] = 'Ch';
$opt['t'] = $name;
$opt['opt'] = $values;
unset($opt['mk']['ca']);
unset($opt['mk']['rc']);
unset($opt['mk']['ac']);
unset($opt['mk']['i']);
unset($opt['mk']['ri']);
unset($opt['mk']['ix']);
unset($opt['mk']['if']);
unset($opt['mk']['tp']);
$this->Annotation($x, $y, $w, $h, $name, $opt, 0);
if ($this->rtl) {
$this->x -= $w;
} else {
$this->x += $w;
}
}
/**
* Creates a Combo-box field
* @param $name (string) field name
* @param $w (int) width
* @param $h (int) height
* @param $values (array) array containing the list of values.
* @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
* @param $opt (array) annotation parameters. Possible values are described on official PDF32000_2008 reference.
* @param $x (float) Abscissa of the upper-left corner of the rectangle
* @param $y (float) Ordinate of the upper-left corner of the rectangle
* @param $js (boolean) if true put the field using JavaScript (requires Acrobat Writer to be rendered).
* @public
* @author Nicola Asuni
* @since 4.8.000 (2009-09-07)
*/
public function ComboBox($name, $w, $h, $values, $prop=array(), $opt=array(), $x='', $y='', $js=false) {
if ($x === '') {
$x = $this->x;
}
if ($y === '') {
$y = $this->y;
}
// check page for no-write regions and adapt page margins if necessary
list($x, $y) = $this->checkPageRegions($h, $x, $y);
if ($js) {
$this->_addfield('combobox', $name, $x, $y, $w, $h, $prop);
$s = '';
foreach ($values as $value) {
if (is_array($value)) {
$s .= ',[\''.addslashes($value[1]).'\',\''.addslashes($value[0]).'\']';
} else {
$s .= ',[\''.addslashes($value).'\',\''.addslashes($value).'\']';
}
}
$this->javascript .= 'f'.$name.'.setItems('.substr($s, 1).');'."\n";
return;
}
// get default style
$prop = array_merge($this->getFormDefaultProp(), $prop);
$prop['Combo'] = true;
// get annotation data
$popt = TCPDF_STATIC::getAnnotOptFromJSProp($prop, $this->spot_colors, $this->rtl);
// set additional default options
$this->annotation_fonts[$this->CurrentFont['fontkey']] = $this->CurrentFont['i'];
$fontstyle = sprintf('/F%d %F Tf %s', $this->CurrentFont['i'], $this->FontSizePt, $this->TextColor);
$popt['da'] = $fontstyle;
// build appearance stream
$popt['ap'] = array();
$popt['ap']['n'] = '/Tx BMC q '.$fontstyle.' ';
$text = '';
foreach($values as $item) {
if (is_array($item)) {
$text .= $item[1]."\n";
} else {
$text .= $item."\n";
}
}
$tmpid = $this->startTemplate($w, $h, false);
$this->MultiCell($w, $h, $text, 0, '', false, 0, 0, 0, true, 0, false, true, 0, 'T', false);
$this->endTemplate();
--$this->n;
$popt['ap']['n'] .= $this->xobjects[$tmpid]['outdata'];
unset($this->xobjects[$tmpid]);
$popt['ap']['n'] .= 'Q EMC';
// merge options
$opt = array_merge($popt, $opt);
// set remaining annotation data
$opt['Subtype'] = 'Widget';
$opt['ft'] = 'Ch';
$opt['t'] = $name;
$opt['opt'] = $values;
unset($opt['mk']['ca']);
unset($opt['mk']['rc']);
unset($opt['mk']['ac']);
unset($opt['mk']['i']);
unset($opt['mk']['ri']);
unset($opt['mk']['ix']);
unset($opt['mk']['if']);
unset($opt['mk']['tp']);
$this->Annotation($x, $y, $w, $h, $name, $opt, 0);
if ($this->rtl) {
$this->x -= $w;
} else {
$this->x += $w;
}
}
/**
* Creates a CheckBox field
* @param $name (string) field name
* @param $w (int) width
* @param $checked (boolean) define the initial state.
* @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
* @param $opt (array) annotation parameters. Possible values are described on official PDF32000_2008 reference.
* @param $onvalue (string) value to be returned if selected.
* @param $x (float) Abscissa of the upper-left corner of the rectangle
* @param $y (float) Ordinate of the upper-left corner of the rectangle
* @param $js (boolean) if true put the field using JavaScript (requires Acrobat Writer to be rendered).
* @public
* @author Nicola Asuni
* @since 4.8.000 (2009-09-07)
*/
public function CheckBox($name, $w, $checked=false, $prop=array(), $opt=array(), $onvalue='Yes', $x='', $y='', $js=false) {
if ($x === '') {
$x = $this->x;
}
if ($y === '') {
$y = $this->y;
}
// check page for no-write regions and adapt page margins if necessary
list($x, $y) = $this->checkPageRegions($w, $x, $y);
if ($js) {
$this->_addfield('checkbox', $name, $x, $y, $w, $w, $prop);
return;
}
if (!isset($prop['value'])) {
$prop['value'] = array('Yes');
}
// get default style
$prop = array_merge($this->getFormDefaultProp(), $prop);
$prop['borderStyle'] = 'inset';
// get annotation data
$popt = TCPDF_STATIC::getAnnotOptFromJSProp($prop, $this->spot_colors, $this->rtl);
// set additional default options
$font = 'zapfdingbats';
if ($this->pdfa_mode) {
// all fonts must be embedded
$font = 'pdfa'.$font;
}
$this->AddFont($font);
$tmpfont = $this->getFontBuffer($font);
$this->annotation_fonts[$tmpfont['fontkey']] = $tmpfont['i'];
$fontstyle = sprintf('/F%d %F Tf %s', $tmpfont['i'], $this->FontSizePt, $this->TextColor);
$popt['da'] = $fontstyle;
// build appearance stream
$popt['ap'] = array();
$popt['ap']['n'] = array();
$fx = ((($w - $this->getAbsFontMeasure($tmpfont['cw'][110])) / 2) * $this->k);
$fy = (($w - ((($tmpfont['desc']['Ascent'] - $tmpfont['desc']['Descent']) * $this->FontSizePt / 1000) / $this->k)) * $this->k);
$popt['ap']['n']['Yes'] = sprintf('q %s BT /F%d %F Tf %F %F Td ('.chr(110).') Tj ET Q', $this->TextColor, $tmpfont['i'], $this->FontSizePt, $fx, $fy);
$popt['ap']['n']['Off'] = sprintf('q %s BT /F%d %F Tf %F %F Td ('.chr(111).') Tj ET Q', $this->TextColor, $tmpfont['i'], $this->FontSizePt, $fx, $fy);
// merge options
$opt = array_merge($popt, $opt);
// set remaining annotation data
$opt['Subtype'] = 'Widget';
$opt['ft'] = 'Btn';
$opt['t'] = $name;
if (TCPDF_STATIC::empty_string($onvalue)) {
$onvalue = 'Yes';
}
$opt['opt'] = array($onvalue);
if ($checked) {
$opt['v'] = array('/Yes');
$opt['as'] = 'Yes';
} else {
$opt['v'] = array('/Off');
$opt['as'] = 'Off';
}
$this->Annotation($x, $y, $w, $w, $name, $opt, 0);
if ($this->rtl) {
$this->x -= $w;
} else {
$this->x += $w;
}
}
/**
* Creates a button field
* @param $name (string) field name
* @param $w (int) width
* @param $h (int) height
* @param $caption (string) caption.
* @param $action (mixed) action triggered by pressing the button. Use a string to specify a javascript action. Use an array to specify a form action options as on section 12.7.5 of PDF32000_2008.
* @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
* @param $opt (array) annotation parameters. Possible values are described on official PDF32000_2008 reference.
* @param $x (float) Abscissa of the upper-left corner of the rectangle
* @param $y (float) Ordinate of the upper-left corner of the rectangle
* @param $js (boolean) if true put the field using JavaScript (requires Acrobat Writer to be rendered).
* @public
* @author Nicola Asuni
* @since 4.8.000 (2009-09-07)
*/
public function Button($name, $w, $h, $caption, $action, $prop=array(), $opt=array(), $x='', $y='', $js=false) {
if ($x === '') {
$x = $this->x;
}
if ($y === '') {
$y = $this->y;
}
// check page for no-write regions and adapt page margins if necessary
list($x, $y) = $this->checkPageRegions($h, $x, $y);
if ($js) {
$this->_addfield('button', $name, $this->x, $this->y, $w, $h, $prop);
$this->javascript .= 'f'.$name.".buttonSetCaption('".addslashes($caption)."');\n";
$this->javascript .= 'f'.$name.".setAction('MouseUp','".addslashes($action)."');\n";
$this->javascript .= 'f'.$name.".highlight='push';\n";
$this->javascript .= 'f'.$name.".print=false;\n";
return;
}
// get default style
$prop = array_merge($this->getFormDefaultProp(), $prop);
$prop['Pushbutton'] = 'true';
$prop['highlight'] = 'push';
$prop['display'] = 'display.noPrint';
// get annotation data
$popt = TCPDF_STATIC::getAnnotOptFromJSProp($prop, $this->spot_colors, $this->rtl);
$this->annotation_fonts[$this->CurrentFont['fontkey']] = $this->CurrentFont['i'];
$fontstyle = sprintf('/F%d %F Tf %s', $this->CurrentFont['i'], $this->FontSizePt, $this->TextColor);
$popt['da'] = $fontstyle;
// build appearance stream
$popt['ap'] = array();
$popt['ap']['n'] = '/Tx BMC q '.$fontstyle.' ';
$tmpid = $this->startTemplate($w, $h, false);
$bw = (2 / $this->k); // border width
$border = array(
'L' => array('width' => $bw, 'cap' => 'square', 'join' => 'miter', 'dash' => 0, 'color' => array(231)),
'R' => array('width' => $bw, 'cap' => 'square', 'join' => 'miter', 'dash' => 0, 'color' => array(51)),
'T' => array('width' => $bw, 'cap' => 'square', 'join' => 'miter', 'dash' => 0, 'color' => array(231)),
'B' => array('width' => $bw, 'cap' => 'square', 'join' => 'miter', 'dash' => 0, 'color' => array(51)));
$this->SetFillColor(204);
$this->Cell($w, $h, $caption, $border, 0, 'C', true, '', 1, false, 'T', 'M');
$this->endTemplate();
--$this->n;
$popt['ap']['n'] .= $this->xobjects[$tmpid]['outdata'];
unset($this->xobjects[$tmpid]);
$popt['ap']['n'] .= 'Q EMC';
// set additional default options
if (!isset($popt['mk'])) {
$popt['mk'] = array();
}
$ann_obj_id = ($this->n + 1);
if (!empty($action) AND !is_array($action)) {
$ann_obj_id = ($this->n + 2);
}
$popt['mk']['ca'] = $this->_textstring($caption, $ann_obj_id);
$popt['mk']['rc'] = $this->_textstring($caption, $ann_obj_id);
$popt['mk']['ac'] = $this->_textstring($caption, $ann_obj_id);
// merge options
$opt = array_merge($popt, $opt);
// set remaining annotation data
$opt['Subtype'] = 'Widget';
$opt['ft'] = 'Btn';
$opt['t'] = $caption;
$opt['v'] = $name;
if (!empty($action)) {
if (is_array($action)) {
// form action options as on section 12.7.5 of PDF32000_2008.
$opt['aa'] = '/D <<';
$bmode = array('SubmitForm', 'ResetForm', 'ImportData');
foreach ($action AS $key => $val) {
if (($key == 'S') AND in_array($val, $bmode)) {
$opt['aa'] .= ' /S /'.$val;
} elseif (($key == 'F') AND (!empty($val))) {
$opt['aa'] .= ' /F '.$this->_datastring($val, $ann_obj_id);
} elseif (($key == 'Fields') AND is_array($val) AND !empty($val)) {
$opt['aa'] .= ' /Fields [';
foreach ($val AS $field) {
$opt['aa'] .= ' '.$this->_textstring($field, $ann_obj_id);
}
$opt['aa'] .= ']';
} elseif (($key == 'Flags')) {
$ff = 0;
if (is_array($val)) {
foreach ($val AS $flag) {
switch ($flag) {
case 'Include/Exclude': {
$ff += 1 << 0;
break;
}
case 'IncludeNoValueFields': {
$ff += 1 << 1;
break;
}
case 'ExportFormat': {
$ff += 1 << 2;
break;
}
case 'GetMethod': {
$ff += 1 << 3;
break;
}
case 'SubmitCoordinates': {
$ff += 1 << 4;
break;
}
case 'XFDF': {
$ff += 1 << 5;
break;
}
case 'IncludeAppendSaves': {
$ff += 1 << 6;
break;
}
case 'IncludeAnnotations': {
$ff += 1 << 7;
break;
}
case 'SubmitPDF': {
$ff += 1 << 8;
break;
}
case 'CanonicalFormat': {
$ff += 1 << 9;
break;
}
case 'ExclNonUserAnnots': {
$ff += 1 << 10;
break;
}
case 'ExclFKey': {
$ff += 1 << 11;
break;
}
case 'EmbedForm': {
$ff += 1 << 13;
break;
}
}
}
} else {
$ff = intval($val);
}
$opt['aa'] .= ' /Flags '.$ff;
}
}
$opt['aa'] .= ' >>';
} else {
// Javascript action or raw action command
$js_obj_id = $this->addJavascriptObject($action);
$opt['aa'] = '/D '.$js_obj_id.' 0 R';
}
}
$this->Annotation($x, $y, $w, $h, $name, $opt, 0);
if ($this->rtl) {
$this->x -= $w;
} else {
$this->x += $w;
}
}
// --- END FORMS FIELDS ------------------------------------------------
/**
* Add certification signature (DocMDP or UR3)
* You can set only one signature type
* @protected
* @author Nicola Asuni
* @since 4.6.008 (2009-05-07)
*/
protected function _putsignature() {
if ((!$this->sign) OR (!isset($this->signature_data['cert_type']))) {
return;
}
$sigobjid = ($this->sig_obj_id + 1);
$out = $this->_getobj($sigobjid)."\n";
$out .= '<< /Type /Sig';
$out .= ' /Filter /Adobe.PPKLite';
$out .= ' /SubFilter /adbe.pkcs7.detached';
$out .= ' '.TCPDF_STATIC::$byterange_string;
$out .= ' /Contents<'.str_repeat('0', $this->signature_max_length).'>';
$out .= ' /Reference ['; // array of signature reference dictionaries
$out .= ' << /Type /SigRef';
if ($this->signature_data['cert_type'] > 0) {
$out .= ' /TransformMethod /DocMDP';
$out .= ' /TransformParams <<';
$out .= ' /Type /TransformParams';
$out .= ' /P '.$this->signature_data['cert_type'];
$out .= ' /V /1.2';
} else {
$out .= ' /TransformMethod /UR3';
$out .= ' /TransformParams <<';
$out .= ' /Type /TransformParams';
$out .= ' /V /2.2';
if (!TCPDF_STATIC::empty_string($this->ur['document'])) {
$out .= ' /Document['.$this->ur['document'].']';
}
if (!TCPDF_STATIC::empty_string($this->ur['form'])) {
$out .= ' /Form['.$this->ur['form'].']';
}
if (!TCPDF_STATIC::empty_string($this->ur['signature'])) {
$out .= ' /Signature['.$this->ur['signature'].']';
}
if (!TCPDF_STATIC::empty_string($this->ur['annots'])) {
$out .= ' /Annots['.$this->ur['annots'].']';
}
if (!TCPDF_STATIC::empty_string($this->ur['ef'])) {
$out .= ' /EF['.$this->ur['ef'].']';
}
if (!TCPDF_STATIC::empty_string($this->ur['formex'])) {
$out .= ' /FormEX['.$this->ur['formex'].']';
}
}
$out .= ' >>'; // close TransformParams
// optional digest data (values must be calculated and replaced later)
//$out .= ' /Data ********** 0 R';
//$out .= ' /DigestMethod/MD5';
//$out .= ' /DigestLocation[********** 34]';
//$out .= ' /DigestValue<********************************>';
$out .= ' >>';
$out .= ' ]'; // end of reference
if (isset($this->signature_data['info']['Name']) AND !TCPDF_STATIC::empty_string($this->signature_data['info']['Name'])) {
$out .= ' /Name '.$this->_textstring($this->signature_data['info']['Name'], $sigobjid);
}
if (isset($this->signature_data['info']['Location']) AND !TCPDF_STATIC::empty_string($this->signature_data['info']['Location'])) {
$out .= ' /Location '.$this->_textstring($this->signature_data['info']['Location'], $sigobjid);
}
if (isset($this->signature_data['info']['Reason']) AND !TCPDF_STATIC::empty_string($this->signature_data['info']['Reason'])) {
$out .= ' /Reason '.$this->_textstring($this->signature_data['info']['Reason'], $sigobjid);
}
if (isset($this->signature_data['info']['ContactInfo']) AND !TCPDF_STATIC::empty_string($this->signature_data['info']['ContactInfo'])) {
$out .= ' /ContactInfo '.$this->_textstring($this->signature_data['info']['ContactInfo'], $sigobjid);
}
$out .= ' /M '.$this->_datestring($sigobjid, $this->doc_modification_timestamp);
$out .= ' >>';
$out .= "\n".'endobj';
$this->_out($out);
}
/**
* Set User's Rights for PDF Reader
* WARNING: This is experimental and currently do not work.
* Check the PDF Reference 8.7.1 Transform Methods,
* Table 8.105 Entries in the UR transform parameters dictionary
* @param $enable (boolean) if true enable user's rights on PDF reader
* @param $document (string) Names specifying additional document-wide usage rights for the document. The only defined value is "/FullSave", which permits a user to save the document along with modified form and/or annotation data.
* @param $annots (string) Names specifying additional annotation-related usage rights for the document. Valid names in PDF 1.5 and later are /Create/Delete/Modify/Copy/Import/Export, which permit the user to perform the named operation on annotations.
* @param $form (string) Names specifying additional form-field-related usage rights for the document. Valid names are: /Add/Delete/FillIn/Import/Export/SubmitStandalone/SpawnTemplate
* @param $signature (string) Names specifying additional signature-related usage rights for the document. The only defined value is /Modify, which permits a user to apply a digital signature to an existing signature form field or clear a signed signature form field.
* @param $ef (string) Names specifying additional usage rights for named embedded files in the document. Valid names are /Create/Delete/Modify/Import, which permit the user to perform the named operation on named embedded files
Names specifying additional embedded-files-related usage rights for the document.
* @param $formex (string) Names specifying additional form-field-related usage rights. The only valid name is BarcodePlaintext, which permits text form field data to be encoded as a plaintext two-dimensional barcode.
* @public
* @author Nicola Asuni
* @since 2.9.000 (2008-03-26)
*/
public function setUserRights(
$enable=true,
$document='/FullSave',
$annots='/Create/Delete/Modify/Copy/Import/Export',
$form='/Add/Delete/FillIn/Import/Export/SubmitStandalone/SpawnTemplate',
$signature='/Modify',
$ef='/Create/Delete/Modify/Import',
$formex='') {
$this->ur['enabled'] = $enable;
$this->ur['document'] = $document;
$this->ur['annots'] = $annots;
$this->ur['form'] = $form;
$this->ur['signature'] = $signature;
$this->ur['ef'] = $ef;
$this->ur['formex'] = $formex;
if (!$this->sign) {
$this->setSignature('', '', '', '', 0, array());
}
}
/**
* Enable document signature (requires the OpenSSL Library).
* The digital signature improve document authenticity and integrity and allows o enable extra features on Acrobat Reader.
* To create self-signed signature: openssl req -x509 -nodes -days 365000 -newkey rsa:1024 -keyout tcpdf.crt -out tcpdf.crt
* To export crt to p12: openssl pkcs12 -export -in tcpdf.crt -out tcpdf.p12
* To convert pfx certificate to pem: openssl pkcs12 -in tcpdf.pfx -out tcpdf.crt -nodes
* @param $signing_cert (mixed) signing certificate (string or filename prefixed with 'file://')
* @param $private_key (mixed) private key (string or filename prefixed with 'file://')
* @param $private_key_password (string) password
* @param $extracerts (string) specifies the name of a file containing a bunch of extra certificates to include in the signature which can for example be used to help the recipient to verify the certificate that you used.
* @param $cert_type (int) The access permissions granted for this document. Valid values shall be: 1 = No changes to the document shall be permitted; any change to the document shall invalidate the signature; 2 = Permitted changes shall be filling in forms, instantiating page templates, and signing; other changes shall invalidate the signature; 3 = Permitted changes shall be the same as for 2, as well as annotation creation, deletion, and modification; other changes shall invalidate the signature.
* @param $info (array) array of option information: Name, Location, Reason, ContactInfo.
* @public
* @author Nicola Asuni
* @since 4.6.005 (2009-04-24)
*/
public function setSignature($signing_cert='', $private_key='', $private_key_password='', $extracerts='', $cert_type=2, $info=array()) {
// to create self-signed signature: openssl req -x509 -nodes -days 365000 -newkey rsa:1024 -keyout tcpdf.crt -out tcpdf.crt
// to export crt to p12: openssl pkcs12 -export -in tcpdf.crt -out tcpdf.p12
// to convert pfx certificate to pem: openssl
// OpenSSL> pkcs12 -in -out -nodes
$this->sign = true;
++$this->n;
$this->sig_obj_id = $this->n; // signature widget
++$this->n; // signature object ($this->sig_obj_id + 1)
$this->signature_data = array();
if (strlen($signing_cert) == 0) {
$signing_cert = 'file://'.dirname(__FILE__).'/config/cert/tcpdf.crt';
$private_key_password = 'tcpdfdemo';
}
if (strlen($private_key) == 0) {
$private_key = $signing_cert;
}
$this->signature_data['signcert'] = $signing_cert;
$this->signature_data['privkey'] = $private_key;
$this->signature_data['password'] = $private_key_password;
$this->signature_data['extracerts'] = $extracerts;
$this->signature_data['cert_type'] = $cert_type;
$this->signature_data['info'] = $info;
}
/**
* Set the digital signature appearance (a cliccable rectangle area to get signature properties)
* @param $x (float) Abscissa of the upper-left corner.
* @param $y (float) Ordinate of the upper-left corner.
* @param $w (float) Width of the signature area.
* @param $h (float) Height of the signature area.
* @param $page (int) option page number (if < 0 the current page is used).
* @param $name (string) Name of the signature.
* @public
* @author Nicola Asuni
* @since 5.3.011 (2010-06-17)
*/
public function setSignatureAppearance($x=0, $y=0, $w=0, $h=0, $page=-1, $name='') {
$this->signature_appearance = $this->getSignatureAppearanceArray($x, $y, $w, $h, $page, $name);
}
/**
* Add an empty digital signature appearance (a cliccable rectangle area to get signature properties)
* @param $x (float) Abscissa of the upper-left corner.
* @param $y (float) Ordinate of the upper-left corner.
* @param $w (float) Width of the signature area.
* @param $h (float) Height of the signature area.
* @param $page (int) option page number (if < 0 the current page is used).
* @param $name (string) Name of the signature.
* @public
* @author Nicola Asuni
* @since 5.9.101 (2011-07-06)
*/
public function addEmptySignatureAppearance($x=0, $y=0, $w=0, $h=0, $page=-1, $name='') {
++$this->n;
$this->empty_signature_appearance[] = array('objid' => $this->n) + $this->getSignatureAppearanceArray($x, $y, $w, $h, $page, $name);
}
/**
* Get the array that defines the signature appearance (page and rectangle coordinates).
* @param $x (float) Abscissa of the upper-left corner.
* @param $y (float) Ordinate of the upper-left corner.
* @param $w (float) Width of the signature area.
* @param $h (float) Height of the signature area.
* @param $page (int) option page number (if < 0 the current page is used).
* @param $name (string) Name of the signature.
* @return (array) Array defining page and rectangle coordinates of signature appearance.
* @protected
* @author Nicola Asuni
* @since 5.9.101 (2011-07-06)
*/
protected function getSignatureAppearanceArray($x=0, $y=0, $w=0, $h=0, $page=-1, $name='') {
$sigapp = array();
if (($page < 1) OR ($page > $this->numpages)) {
$sigapp['page'] = $this->page;
} else {
$sigapp['page'] = intval($page);
}
if (empty($name)) {
$sigapp['name'] = 'Signature';
} else {
$sigapp['name'] = $name;
}
$a = $x * $this->k;
$b = $this->pagedim[($sigapp['page'])]['h'] - (($y + $h) * $this->k);
$c = $w * $this->k;
$d = $h * $this->k;
$sigapp['rect'] = sprintf('%F %F %F %F', $a, $b, ($a + $c), ($b + $d));
return $sigapp;
}
/**
* Create a new page group.
* NOTE: call this function before calling AddPage()
* @param $page (int) starting group page (leave empty for next page).
* @public
* @since 3.0.000 (2008-03-27)
*/
public function startPageGroup($page='') {
if (empty($page)) {
$page = $this->page + 1;
}
$this->newpagegroup[$page] = sizeof($this->newpagegroup) + 1;
}
/**
* Set the starting page number.
* @param $num (int) Starting page number.
* @since 5.9.093 (2011-06-16)
* @public
*/
public function setStartingPageNumber($num=1) {
$this->starting_page_number = max(0, intval($num));
}
/**
* Returns the string alias used right align page numbers.
* If the current font is unicode type, the returned string wil contain an additional open curly brace.
* @return string
* @since 5.9.099 (2011-06-27)
* @public
*/
public function getAliasRightShift() {
// calculate aproximatively the ratio between widths of aliases and replacements.
$ref = '{'.TCPDF_STATIC::$alias_right_shift.'}{'.TCPDF_STATIC::$alias_tot_pages.'}{'.TCPDF_STATIC::$alias_num_page.'}';
$rep = str_repeat(' ', $this->GetNumChars($ref));
$wdiff = max(1, ($this->GetStringWidth($ref) / $this->GetStringWidth($rep)));
$sdiff = sprintf('%F', $wdiff);
$alias = TCPDF_STATIC::$alias_right_shift.$sdiff.'}';
if ($this->isUnicodeFont()) {
$alias = '{'.$alias;
}
return $alias;
}
/**
* Returns the string alias used for the total number of pages.
* If the current font is unicode type, the returned string is surrounded by additional curly braces.
* This alias will be replaced by the total number of pages in the document.
* @return string
* @since 4.0.018 (2008-08-08)
* @public
*/
public function getAliasNbPages() {
if ($this->isUnicodeFont()) {
return '{'.TCPDF_STATIC::$alias_tot_pages.'}';
}
return TCPDF_STATIC::$alias_tot_pages;
}
/**
* Returns the string alias used for the page number.
* If the current font is unicode type, the returned string is surrounded by additional curly braces.
* This alias will be replaced by the page number.
* @return string
* @since 4.5.000 (2009-01-02)
* @public
*/
public function getAliasNumPage() {
if ($this->isUnicodeFont()) {
return '{'.TCPDF_STATIC::$alias_num_page.'}';
}
return TCPDF_STATIC::$alias_num_page;
}
/**
* Return the alias for the total number of pages in the current page group.
* If the current font is unicode type, the returned string is surrounded by additional curly braces.
* This alias will be replaced by the total number of pages in this group.
* @return alias of the current page group
* @public
* @since 3.0.000 (2008-03-27)
*/
public function getPageGroupAlias() {
if ($this->isUnicodeFont()) {
return '{'.TCPDF_STATIC::$alias_group_tot_pages.'}';
}
return TCPDF_STATIC::$alias_group_tot_pages;
}
/**
* Return the alias for the page number on the current page group.
* If the current font is unicode type, the returned string is surrounded by additional curly braces.
* This alias will be replaced by the page number (relative to the belonging group).
* @return alias of the current page group
* @public
* @since 4.5.000 (2009-01-02)
*/
public function getPageNumGroupAlias() {
if ($this->isUnicodeFont()) {
return '{'.TCPDF_STATIC::$alias_group_num_page.'}';
}
return TCPDF_STATIC::$alias_group_num_page;
}
/**
* Return the current page in the group.
* @return current page in the group
* @public
* @since 3.0.000 (2008-03-27)
*/
public function getGroupPageNo() {
return $this->pagegroups[$this->currpagegroup];
}
/**
* Returns the current group page number formatted as a string.
* @public
* @since 4.3.003 (2008-11-18)
* @see PaneNo(), formatPageNumber()
*/
public function getGroupPageNoFormatted() {
return TCPDF_STATIC::formatPageNumber($this->getGroupPageNo());
}
/**
* Returns the current page number formatted as a string.
* @public
* @since 4.2.005 (2008-11-06)
* @see PaneNo(), formatPageNumber()
*/
public function PageNoFormatted() {
return TCPDF_STATIC::formatPageNumber($this->PageNo());
}
/**
* Put pdf layers.
* @protected
* @since 3.0.000 (2008-03-27)
*/
protected function _putocg() {
if (empty($this->pdflayers)) {
return;
}
foreach ($this->pdflayers as $key => $layer) {
$this->pdflayers[$key]['objid'] = $this->_newobj();
$out = '<< /Type /OCG';
$out .= ' /Name '.$this->_textstring($layer['name'], $this->pdflayers[$key]['objid']);
$out .= ' /Usage <<';
$out .= ' /Print <>';
$out .= ' /View <>';
$out .= ' >> >>';
$out .= "\n".'endobj';
$this->_out($out);
}
}
/**
* Start a new pdf layer.
* @param $name (string) Layer name (only a-z letters and numbers). Leave empty for automatic name.
* @param $print (boolean) Set to true to print this layer.
* @param $view (boolean) Set to true to view this layer.
* @public
* @since 5.9.102 (2011-07-13)
*/
public function startLayer($name='', $print=true, $view=true) {
if ($this->state != 2) {
return;
}
$layer = sprintf('LYR%03d', (count($this->pdflayers) + 1));
if (empty($name)) {
$name = $layer;
} else {
$name = preg_replace('/[^a-zA-Z0-9_\-]/', '', $name);
}
$this->pdflayers[] = array('layer' => $layer, 'name' => $name, 'print' => $print, 'view' => $view);
$this->openMarkedContent = true;
$this->_out('/OC /'.$layer.' BDC');
}
/**
* End the current PDF layer.
* @public
* @since 5.9.102 (2011-07-13)
*/
public function endLayer() {
if ($this->state != 2) {
return;
}
if ($this->openMarkedContent) {
// close existing open marked-content layer
$this->_out('EMC');
$this->openMarkedContent = false;
}
}
/**
* Set the visibility of the successive elements.
* This can be useful, for instance, to put a background
* image or color that will show on screen but won't print.
* @param $v (string) visibility mode. Legal values are: all, print, screen or view.
* @public
* @since 3.0.000 (2008-03-27)
*/
public function setVisibility($v) {
if ($this->state != 2) {
return;
}
$this->endLayer();
switch($v) {
case 'print': {
$this->startLayer('Print', true, false);
break;
}
case 'view':
case 'screen': {
$this->startLayer('View', false, true);
break;
}
case 'all': {
$this->_out('');
break;
}
default: {
$this->Error('Incorrect visibility: '.$v);
break;
}
}
}
/**
* Add transparency parameters to the current extgstate
* @param $parms (array) parameters
* @return the number of extgstates
* @protected
* @since 3.0.000 (2008-03-27)
*/
protected function addExtGState($parms) {
if ($this->pdfa_mode) {
// transparencies are not allowed in PDF/A mode
return;
}
// check if this ExtGState already exist
foreach ($this->extgstates as $i => $ext) {
if ($ext['parms'] == $parms) {
if ($this->inxobj) {
// we are inside an XObject template
$this->xobjects[$this->xobjid]['extgstates'][$i] = $ext;
}
// return reference to existing ExtGState
return $i;
}
}
$n = (count($this->extgstates) + 1);
$this->extgstates[$n] = array('parms' => $parms);
if ($this->inxobj) {
// we are inside an XObject template
$this->xobjects[$this->xobjid]['extgstates'][$n] = $this->extgstates[$n];
}
return $n;
}
/**
* Add an extgstate
* @param $gs (array) extgstate
* @protected
* @since 3.0.000 (2008-03-27)
*/
protected function setExtGState($gs) {
if ($this->pdfa_mode OR ($this->state != 2)) {
// transparency is not allowed in PDF/A mode
return;
}
$this->_out(sprintf('/GS%d gs', $gs));
}
/**
* Put extgstates for object transparency
* @protected
* @since 3.0.000 (2008-03-27)
*/
protected function _putextgstates() {
foreach ($this->extgstates as $i => $ext) {
$this->extgstates[$i]['n'] = $this->_newobj();
$out = '<< /Type /ExtGState';
foreach ($ext['parms'] as $k => $v) {
if (is_float($v)) {
$v = sprintf('%F', $v);
} elseif ($v === true) {
$v = 'true';
} elseif ($v === false) {
$v = 'false';
}
$out .= ' /'.$k.' '.$v;
}
$out .= ' >>';
$out .= "\n".'endobj';
$this->_out($out);
}
}
/**
* Set overprint mode for stroking (OP) and non-stroking (op) painting operations.
* (Check the "Entries in a Graphics State Parameter Dictionary" on PDF 32000-1:2008).
* @param $stroking (boolean) If true apply overprint for stroking operations.
* @param $nonstroking (boolean) If true apply overprint for painting operations other than stroking.
* @param $mode (integer) Overprint mode: (0 = each source colour component value replaces the value previously painted for the corresponding device colorant; 1 = a tint value of 0.0 for a source colour component shall leave the corresponding component of the previously painted colour unchanged).
* @public
* @since 5.9.152 (2012-03-23)
*/
public function setOverprint($stroking=true, $nonstroking='', $mode=0) {
if ($this->state != 2) {
return;
}
$stroking = $stroking ? true : false;
if (TCPDF_STATIC::empty_string($nonstroking)) {
// default value if not set
$nonstroking = $stroking;
} else {
$nonstroking = $nonstroking ? true : false;
}
if (($mode != 0) AND ($mode != 1)) {
$mode = 0;
}
$this->overprint = array('OP' => $stroking, 'op' => $nonstroking, 'OPM' => $mode);
$gs = $this->addExtGState($this->overprint);
$this->setExtGState($gs);
}
/**
* Get the overprint mode array (OP, op, OPM).
* (Check the "Entries in a Graphics State Parameter Dictionary" on PDF 32000-1:2008).
* @return array.
* @public
* @since 5.9.152 (2012-03-23)
*/
public function getOverprint() {
return $this->overprint;
}
/**
* Set alpha for stroking (CA) and non-stroking (ca) operations.
* @param $stroking (float) Alpha value for stroking operations: real value from 0 (transparent) to 1 (opaque).
* @param $bm (string) blend mode, one of the following: Normal, Multiply, Screen, Overlay, Darken, Lighten, ColorDodge, ColorBurn, HardLight, SoftLight, Difference, Exclusion, Hue, Saturation, Color, Luminosity
* @param $nonstroking (float) Alpha value for non-stroking operations: real value from 0 (transparent) to 1 (opaque).
* @param $ais (boolean)
* @public
* @since 3.0.000 (2008-03-27)
*/
public function setAlpha($stroking=1, $bm='Normal', $nonstroking='', $ais=false) {
if ($this->pdfa_mode) {
// transparency is not allowed in PDF/A mode
return;
}
$stroking = floatval($stroking);
if (TCPDF_STATIC::empty_string($nonstroking)) {
// default value if not set
$nonstroking = $stroking;
} else {
$nonstroking = floatval($nonstroking);
}
if ($bm[0] == '/') {
// remove trailing slash
$bm = substr($bm, 1);
}
if (!in_array($bm, array('Normal', 'Multiply', 'Screen', 'Overlay', 'Darken', 'Lighten', 'ColorDodge', 'ColorBurn', 'HardLight', 'SoftLight', 'Difference', 'Exclusion', 'Hue', 'Saturation', 'Color', 'Luminosity'))) {
$bm = 'Normal';
}
$ais = $ais ? true : false;
$this->alpha = array('CA' => $stroking, 'ca' => $nonstroking, 'BM' => '/'.$bm, 'AIS' => $ais);
$gs = $this->addExtGState($this->alpha);
$this->setExtGState($gs);
}
/**
* Get the alpha mode array (CA, ca, BM, AIS).
* (Check the "Entries in a Graphics State Parameter Dictionary" on PDF 32000-1:2008).
* @return array.
* @public
* @since 5.9.152 (2012-03-23)
*/
public function getAlpha() {
return $this->alpha;
}
/**
* Set the default JPEG compression quality (1-100)
* @param $quality (int) JPEG quality, integer between 1 and 100
* @public
* @since 3.0.000 (2008-03-27)
*/
public function setJPEGQuality($quality) {
if (($quality < 1) OR ($quality > 100)) {
$quality = 75;
}
$this->jpeg_quality = intval($quality);
}
/**
* Set the default number of columns in a row for HTML tables.
* @param $cols (int) number of columns
* @public
* @since 3.0.014 (2008-06-04)
*/
public function setDefaultTableColumns($cols=4) {
$this->default_table_columns = intval($cols);
}
/**
* Set the height of the cell (line height) respect the font height.
* @param $h (int) cell proportion respect font height (typical value = 1.25).
* @public
* @since 3.0.014 (2008-06-04)
*/
public function setCellHeightRatio($h) {
$this->cell_height_ratio = $h;
}
/**
* return the height of cell repect font height.
* @public
* @since 4.0.012 (2008-07-24)
*/
public function getCellHeightRatio() {
return $this->cell_height_ratio;
}
/**
* Set the PDF version (check PDF reference for valid values).
* @param $version (string) PDF document version.
* @public
* @since 3.1.000 (2008-06-09)
*/
public function setPDFVersion($version='1.7') {
if ($this->pdfa_mode) {
// PDF/A mode
$this->PDFVersion = '1.4';
} else {
$this->PDFVersion = $version;
}
}
/**
* Set the viewer preferences dictionary controlling the way the document is to be presented on the screen or in print.
* (see Section 8.1 of PDF reference, "Viewer Preferences").
*
HideToolbar boolean (Optional) A flag specifying whether to hide the viewer application's tool bars when the document is active. Default value: false.
HideMenubar boolean (Optional) A flag specifying whether to hide the viewer application's menu bar when the document is active. Default value: false.
HideWindowUI boolean (Optional) A flag specifying whether to hide user interface elements in the document's window (such as scroll bars and navigation controls), leaving only the document's contents displayed. Default value: false.
FitWindow boolean (Optional) A flag specifying whether to resize the document's window to fit the size of the first displayed page. Default value: false.
CenterWindow boolean (Optional) A flag specifying whether to position the document's window in the center of the screen. Default value: false.
DisplayDocTitle boolean (Optional; PDF 1.4) A flag specifying whether the window's title bar should display the document title taken from the Title entry of the document information dictionary (see Section 10.2.1, "Document Information Dictionary"). If false, the title bar should instead display the name of the PDF file containing the document. Default value: false.
NonFullScreenPageMode name (Optional) The document's page mode, specifying how to display the document on exiting full-screen mode:
UseNone Neither document outline nor thumbnail images visible
UseOutlines Document outline visible
UseThumbs Thumbnail images visible
UseOC Optional content group panel visible
This entry is meaningful only if the value of the PageMode entry in the catalog dictionary (see Section 3.6.1, "Document Catalog") is FullScreen; it is ignored otherwise. Default value: UseNone.
ViewArea name (Optional; PDF 1.4) The name of the page boundary representing the area of a page to be displayed when viewing the document on the screen. Valid values are (see Section 10.10.1, "Page Boundaries").:
MediaBox
CropBox (default)
BleedBox
TrimBox
ArtBox
ViewClip name (Optional; PDF 1.4) The name of the page boundary to which the contents of a page are to be clipped when viewing the document on the screen. Valid values are (see Section 10.10.1, "Page Boundaries").:
MediaBox
CropBox (default)
BleedBox
TrimBox
ArtBox
PrintArea name (Optional; PDF 1.4) The name of the page boundary representing the area of a page to be rendered when printing the document. Valid values are (see Section 10.10.1, "Page Boundaries").:
MediaBox
CropBox (default)
BleedBox
TrimBox
ArtBox
PrintClip name (Optional; PDF 1.4) The name of the page boundary to which the contents of a page are to be clipped when printing the document. Valid values are (see Section 10.10.1, "Page Boundaries").:
MediaBox
CropBox (default)
BleedBox
TrimBox
ArtBox
PrintScaling name (Optional; PDF 1.6) The page scaling option to be selected when a print dialog is displayed for this document. Valid values are:
None, which indicates that the print dialog should reflect no page scaling
AppDefault (default), which indicates that applications should use the current print scaling
Duplex name (Optional; PDF 1.7) The paper handling option to use when printing the file from the print dialog. The following values are valid:
Simplex - Print single-sided
DuplexFlipShortEdge - Duplex and flip on the short edge of the sheet
DuplexFlipLongEdge - Duplex and flip on the long edge of the sheet
Default value: none
PickTrayByPDFSize boolean (Optional; PDF 1.7) A flag specifying whether the PDF page size is used to select the input paper tray. This setting influences only the preset values used to populate the print dialog presented by a PDF viewer application. If PickTrayByPDFSize is true, the check box in the print dialog associated with input paper tray is checked. Note: This setting has no effect on Mac OS systems, which do not provide the ability to pick the input tray by size.
PrintPageRange array (Optional; PDF 1.7) The page numbers used to initialize the print dialog box when the file is printed. The first page of the PDF file is denoted by 1. Each pair consists of the first and last pages in the sub-range. An odd number of integers causes this entry to be ignored. Negative numbers cause the entire array to be ignored. Default value: as defined by PDF viewer application
NumCopies integer (Optional; PDF 1.7) The number of copies to be printed when the print dialog is opened for this file. Supported values are the integers 2 through 5. Values outside this range are ignored. Default value: as defined by PDF viewer application, but typically 1
* @param $preferences (array) array of options.
* @author Nicola Asuni
* @public
* @since 3.1.000 (2008-06-09)
*/
public function setViewerPreferences($preferences) {
$this->viewer_preferences = $preferences;
}
/**
* Paints color transition registration bars
* @param $x (float) abscissa of the top left corner of the rectangle.
* @param $y (float) ordinate of the top left corner of the rectangle.
* @param $w (float) width of the rectangle.
* @param $h (float) height of the rectangle.
* @param $transition (boolean) if true prints tcolor transitions to white.
* @param $vertical (boolean) if true prints bar vertically.
* @param $colors (string) colors to print, one letter per color separated by comma (for example 'A,W,R,G,B,C,M,Y,K'): A=black, W=white, R=red, G=green, B=blue, C=cyan, M=magenta, Y=yellow, K=black.
* @author Nicola Asuni
* @since 4.9.000 (2010-03-26)
* @public
*/
public function colorRegistrationBar($x, $y, $w, $h, $transition=true, $vertical=false, $colors='A,R,G,B,C,M,Y,K') {
$bars = explode(',', $colors);
$numbars = count($bars); // number of bars to print
// set bar measures
if ($vertical) {
$coords = array(0, 0, 0, 1);
$wb = $w / $numbars; // bar width
$hb = $h; // bar height
$xd = $wb; // delta x
$yd = 0; // delta y
} else {
$coords = array(1, 0, 0, 0);
$wb = $w; // bar width
$hb = $h / $numbars; // bar height
$xd = 0; // delta x
$yd = $hb; // delta y
}
$xb = $x;
$yb = $y;
foreach ($bars as $col) {
switch ($col) {
// set transition colors
case 'A': { // BLACK
$col_a = array(255);
$col_b = array(0);
break;
}
case 'W': { // WHITE
$col_a = array(0);
$col_b = array(255);
break;
}
case 'R': { // R
$col_a = array(255,255,255);
$col_b = array(255,0,0);
break;
}
case 'G': { // G
$col_a = array(255,255,255);
$col_b = array(0,255,0);
break;
}
case 'B': { // B
$col_a = array(255,255,255);
$col_b = array(0,0,255);
break;
}
case 'C': { // C
$col_a = array(0,0,0,0);
$col_b = array(100,0,0,0);
break;
}
case 'M': { // M
$col_a = array(0,0,0,0);
$col_b = array(0,100,0,0);
break;
}
case 'Y': { // Y
$col_a = array(0,0,0,0);
$col_b = array(0,0,100,0);
break;
}
case 'K': { // K
$col_a = array(0,0,0,0);
$col_b = array(0,0,0,100);
break;
}
default: { // GRAY
$col_a = array(255);
$col_b = array(0);
break;
}
}
if ($transition) {
// color gradient
$this->LinearGradient($xb, $yb, $wb, $hb, $col_a, $col_b, $coords);
} else {
// color rectangle
$this->SetFillColorArray($col_b);
$this->Rect($xb, $yb, $wb, $hb, 'F', array());
}
$xb += $xd;
$yb += $yd;
}
}
/**
* Paints crop marks.
* @param $x (float) abscissa of the crop mark center.
* @param $y (float) ordinate of the crop mark center.
* @param $w (float) width of the crop mark.
* @param $h (float) height of the crop mark.
* @param $type (string) type of crop mark, one symbol per type separated by comma: T = TOP, F = BOTTOM, L = LEFT, R = RIGHT, TL = A = TOP-LEFT, TR = B = TOP-RIGHT, BL = C = BOTTOM-LEFT, BR = D = BOTTOM-RIGHT.
* @param $color (array) crop mark color (default black).
* @author Nicola Asuni
* @since 4.9.000 (2010-03-26)
* @public
*/
public function cropMark($x, $y, $w, $h, $type='T,R,B,L', $color=array(0,0,0)) {
$this->SetLineStyle(array('width' => (0.5 / $this->k), 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => $color));
$type = strtoupper($type);
$type = preg_replace('/[^A-Z\-\,]*/', '', $type);
// split type in single components
$type = str_replace('-', ',', $type);
$type = str_replace('TL', 'T,L', $type);
$type = str_replace('TR', 'T,R', $type);
$type = str_replace('BL', 'F,L', $type);
$type = str_replace('BR', 'F,R', $type);
$type = str_replace('A', 'T,L', $type);
$type = str_replace('B', 'T,R', $type);
$type = str_replace('T,RO', 'BO', $type);
$type = str_replace('C', 'F,L', $type);
$type = str_replace('D', 'F,R', $type);
$crops = explode(',', strtoupper($type));
// remove duplicates
$crops = array_unique($crops);
$dw = ($w / 4); // horizontal space to leave before the intersection point
$dh = ($h / 4); // vertical space to leave before the intersection point
foreach ($crops as $crop) {
switch ($crop) {
case 'T':
case 'TOP': {
$x1 = $x;
$y1 = ($y - $h);
$x2 = $x;
$y2 = ($y - $dh);
break;
}
case 'F':
case 'BOTTOM': {
$x1 = $x;
$y1 = ($y + $dh);
$x2 = $x;
$y2 = ($y + $h);
break;
}
case 'L':
case 'LEFT': {
$x1 = ($x - $w);
$y1 = $y;
$x2 = ($x - $dw);
$y2 = $y;
break;
}
case 'R':
case 'RIGHT': {
$x1 = ($x + $dw);
$y1 = $y;
$x2 = ($x + $w);
$y2 = $y;
break;
}
}
$this->Line($x1, $y1, $x2, $y2);
}
}
/**
* Paints a registration mark
* @param $x (float) abscissa of the registration mark center.
* @param $y (float) ordinate of the registration mark center.
* @param $r (float) radius of the crop mark.
* @param $double (boolean) if true print two concentric crop marks.
* @param $cola (array) crop mark color (default black).
* @param $colb (array) second crop mark color.
* @author Nicola Asuni
* @since 4.9.000 (2010-03-26)
* @public
*/
public function registrationMark($x, $y, $r, $double=false, $cola=array(0,0,0), $colb=array(255,255,255)) {
$line_style = array('width' => (0.5 / $this->k), 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => $cola);
$this->SetFillColorArray($cola);
$this->PieSector($x, $y, $r, 90, 180, 'F');
$this->PieSector($x, $y, $r, 270, 360, 'F');
$this->Circle($x, $y, $r, 0, 360, 'C', $line_style, array(), 8);
if ($double) {
$r2 = $r * 0.5;
$this->SetFillColorArray($colb);
$this->PieSector($x, $y, $r2, 90, 180, 'F');
$this->PieSector($x, $y, $r2, 270, 360, 'F');
$this->SetFillColorArray($cola);
$this->PieSector($x, $y, $r2, 0, 90, 'F');
$this->PieSector($x, $y, $r2, 180, 270, 'F');
$this->Circle($x, $y, $r2, 0, 360, 'C', $line_style, array(), 8);
}
}
/**
* Paints a linear colour gradient.
* @param $x (float) abscissa of the top left corner of the rectangle.
* @param $y (float) ordinate of the top left corner of the rectangle.
* @param $w (float) width of the rectangle.
* @param $h (float) height of the rectangle.
* @param $col1 (array) first color (Grayscale, RGB or CMYK components).
* @param $col2 (array) second color (Grayscale, RGB or CMYK components).
* @param $coords (array) array of the form (x1, y1, x2, y2) which defines the gradient vector (see linear_gradient_coords.jpg). The default value is from left to right (x1=0, y1=0, x2=1, y2=0).
* @author Andreas Wrmser, Nicola Asuni
* @since 3.1.000 (2008-06-09)
* @public
*/
public function LinearGradient($x, $y, $w, $h, $col1=array(), $col2=array(), $coords=array(0,0,1,0)) {
$this->Clip($x, $y, $w, $h);
$this->Gradient(2, $coords, array(array('color' => $col1, 'offset' => 0, 'exponent' => 1), array('color' => $col2, 'offset' => 1, 'exponent' => 1)), array(), false);
}
/**
* Paints a radial colour gradient.
* @param $x (float) abscissa of the top left corner of the rectangle.
* @param $y (float) ordinate of the top left corner of the rectangle.
* @param $w (float) width of the rectangle.
* @param $h (float) height of the rectangle.
* @param $col1 (array) first color (Grayscale, RGB or CMYK components).
* @param $col2 (array) second color (Grayscale, RGB or CMYK components).
* @param $coords (array) array of the form (fx, fy, cx, cy, r) where (fx, fy) is the starting point of the gradient with color1, (cx, cy) is the center of the circle with color2, and r is the radius of the circle (see radial_gradient_coords.jpg). (fx, fy) should be inside the circle, otherwise some areas will not be defined.
* @author Andreas Wrmser, Nicola Asuni
* @since 3.1.000 (2008-06-09)
* @public
*/
public function RadialGradient($x, $y, $w, $h, $col1=array(), $col2=array(), $coords=array(0.5,0.5,0.5,0.5,1)) {
$this->Clip($x, $y, $w, $h);
$this->Gradient(3, $coords, array(array('color' => $col1, 'offset' => 0, 'exponent' => 1), array('color' => $col2, 'offset' => 1, 'exponent' => 1)), array(), false);
}
/**
* Paints a coons patch mesh.
* @param $x (float) abscissa of the top left corner of the rectangle.
* @param $y (float) ordinate of the top left corner of the rectangle.
* @param $w (float) width of the rectangle.
* @param $h (float) height of the rectangle.
* @param $col1 (array) first color (lower left corner) (RGB components).
* @param $col2 (array) second color (lower right corner) (RGB components).
* @param $col3 (array) third color (upper right corner) (RGB components).
* @param $col4 (array) fourth color (upper left corner) (RGB components).
* @param $coords (array)
for one patch mesh: array(float x1, float y1, .... float x12, float y12): 12 pairs of coordinates (normally from 0 to 1) which specify the Bezier control points that define the patch. First pair is the lower left edge point, next is its right control point (control point 2). Then the other points are defined in the order: control point 1, edge point, control point 2 going counter-clockwise around the patch. Last (x12, y12) is the first edge point's left control point (control point 1).
for two or more patch meshes: array[number of patches]: arrays with the following keys for each patch: f: where to put that patch (0 = first patch, 1, 2, 3 = right, top and left of precedent patch - I didn't figure this out completely - just try and error ;-) points: 12 pairs of coordinates of the Bezier control points as above for the first patch, 8 pairs of coordinates for the following patches, ignoring the coordinates already defined by the precedent patch (I also didn't figure out the order of these - also: try and see what's happening) colors: must be 4 colors for the first patch, 2 colors for the following patches
* @param $coords_min (array) minimum value used by the coordinates. If a coordinate's value is smaller than this it will be cut to coords_min. default: 0
* @param $coords_max (array) maximum value used by the coordinates. If a coordinate's value is greater than this it will be cut to coords_max. default: 1
* @param $antialias (boolean) A flag indicating whether to filter the shading function to prevent aliasing artifacts.
* @author Andreas Wrmser, Nicola Asuni
* @since 3.1.000 (2008-06-09)
* @public
*/
public function CoonsPatchMesh($x, $y, $w, $h, $col1=array(), $col2=array(), $col3=array(), $col4=array(), $coords=array(0.00,0.0,0.33,0.00,0.67,0.00,1.00,0.00,1.00,0.33,1.00,0.67,1.00,1.00,0.67,1.00,0.33,1.00,0.00,1.00,0.00,0.67,0.00,0.33), $coords_min=0, $coords_max=1, $antialias=false) {
if ($this->pdfa_mode OR ($this->state != 2)) {
return;
}
$this->Clip($x, $y, $w, $h);
$n = count($this->gradients) + 1;
$this->gradients[$n] = array();
$this->gradients[$n]['type'] = 6; //coons patch mesh
$this->gradients[$n]['coords'] = array();
$this->gradients[$n]['antialias'] = $antialias;
$this->gradients[$n]['colors'] = array();
$this->gradients[$n]['transparency'] = false;
//check the coords array if it is the simple array or the multi patch array
if (!isset($coords[0]['f'])) {
//simple array -> convert to multi patch array
if (!isset($col1[1])) {
$col1[1] = $col1[2] = $col1[0];
}
if (!isset($col2[1])) {
$col2[1] = $col2[2] = $col2[0];
}
if (!isset($col3[1])) {
$col3[1] = $col3[2] = $col3[0];
}
if (!isset($col4[1])) {
$col4[1] = $col4[2] = $col4[0];
}
$patch_array[0]['f'] = 0;
$patch_array[0]['points'] = $coords;
$patch_array[0]['colors'][0]['r'] = $col1[0];
$patch_array[0]['colors'][0]['g'] = $col1[1];
$patch_array[0]['colors'][0]['b'] = $col1[2];
$patch_array[0]['colors'][1]['r'] = $col2[0];
$patch_array[0]['colors'][1]['g'] = $col2[1];
$patch_array[0]['colors'][1]['b'] = $col2[2];
$patch_array[0]['colors'][2]['r'] = $col3[0];
$patch_array[0]['colors'][2]['g'] = $col3[1];
$patch_array[0]['colors'][2]['b'] = $col3[2];
$patch_array[0]['colors'][3]['r'] = $col4[0];
$patch_array[0]['colors'][3]['g'] = $col4[1];
$patch_array[0]['colors'][3]['b'] = $col4[2];
} else {
//multi patch array
$patch_array = $coords;
}
$bpcd = 65535; //16 bits per coordinate
//build the data stream
$this->gradients[$n]['stream'] = '';
$count_patch = count($patch_array);
for ($i=0; $i < $count_patch; ++$i) {
$this->gradients[$n]['stream'] .= chr($patch_array[$i]['f']); //start with the edge flag as 8 bit
$count_points = count($patch_array[$i]['points']);
for ($j=0; $j < $count_points; ++$j) {
//each point as 16 bit
$patch_array[$i]['points'][$j] = (($patch_array[$i]['points'][$j] - $coords_min) / ($coords_max - $coords_min)) * $bpcd;
if ($patch_array[$i]['points'][$j] < 0) {
$patch_array[$i]['points'][$j] = 0;
}
if ($patch_array[$i]['points'][$j] > $bpcd) {
$patch_array[$i]['points'][$j] = $bpcd;
}
$this->gradients[$n]['stream'] .= chr(floor($patch_array[$i]['points'][$j] / 256));
$this->gradients[$n]['stream'] .= chr(floor($patch_array[$i]['points'][$j] % 256));
}
$count_cols = count($patch_array[$i]['colors']);
for ($j=0; $j < $count_cols; ++$j) {
//each color component as 8 bit
$this->gradients[$n]['stream'] .= chr($patch_array[$i]['colors'][$j]['r']);
$this->gradients[$n]['stream'] .= chr($patch_array[$i]['colors'][$j]['g']);
$this->gradients[$n]['stream'] .= chr($patch_array[$i]['colors'][$j]['b']);
}
}
//paint the gradient
$this->_out('/Sh'.$n.' sh');
//restore previous Graphic State
$this->_out('Q');
if ($this->inxobj) {
// we are inside an XObject template
$this->xobjects[$this->xobjid]['gradients'][$n] = $this->gradients[$n];
}
}
/**
* Set a rectangular clipping area.
* @param $x (float) abscissa of the top left corner of the rectangle (or top right corner for RTL mode).
* @param $y (float) ordinate of the top left corner of the rectangle.
* @param $w (float) width of the rectangle.
* @param $h (float) height of the rectangle.
* @author Andreas Wrmser, Nicola Asuni
* @since 3.1.000 (2008-06-09)
* @protected
*/
protected function Clip($x, $y, $w, $h) {
if ($this->state != 2) {
return;
}
if ($this->rtl) {
$x = $this->w - $x - $w;
}
//save current Graphic State
$s = 'q';
//set clipping area
$s .= sprintf(' %F %F %F %F re W n', $x*$this->k, ($this->h-$y)*$this->k, $w*$this->k, -$h*$this->k);
//set up transformation matrix for gradient
$s .= sprintf(' %F 0 0 %F %F %F cm', $w*$this->k, $h*$this->k, $x*$this->k, ($this->h-($y+$h))*$this->k);
$this->_out($s);
}
/**
* Output gradient.
* @param $type (int) type of gradient (1 Function-based shading; 2 Axial shading; 3 Radial shading; 4 Free-form Gouraud-shaded triangle mesh; 5 Lattice-form Gouraud-shaded triangle mesh; 6 Coons patch mesh; 7 Tensor-product patch mesh). (Not all types are currently supported)
* @param $coords (array) array of coordinates.
* @param $stops (array) array gradient color components: color = array of GRAY, RGB or CMYK color components; offset = (0 to 1) represents a location along the gradient vector; exponent = exponent of the exponential interpolation function (default = 1).
* @param $background (array) An array of colour components appropriate to the colour space, specifying a single background colour value.
* @param $antialias (boolean) A flag indicating whether to filter the shading function to prevent aliasing artifacts.
* @author Nicola Asuni
* @since 3.1.000 (2008-06-09)
* @public
*/
public function Gradient($type, $coords, $stops, $background=array(), $antialias=false) {
if ($this->pdfa_mode OR ($this->state != 2)) {
return;
}
$n = count($this->gradients) + 1;
$this->gradients[$n] = array();
$this->gradients[$n]['type'] = $type;
$this->gradients[$n]['coords'] = $coords;
$this->gradients[$n]['antialias'] = $antialias;
$this->gradients[$n]['colors'] = array();
$this->gradients[$n]['transparency'] = false;
// color space
$numcolspace = count($stops[0]['color']);
$bcolor = array_values($background);
switch($numcolspace) {
case 4: { // CMYK
$this->gradients[$n]['colspace'] = 'DeviceCMYK';
if (!empty($background)) {
$this->gradients[$n]['background'] = sprintf('%F %F %F %F', $bcolor[0]/100, $bcolor[1]/100, $bcolor[2]/100, $bcolor[3]/100);
}
break;
}
case 3: { // RGB
$this->gradients[$n]['colspace'] = 'DeviceRGB';
if (!empty($background)) {
$this->gradients[$n]['background'] = sprintf('%F %F %F', $bcolor[0]/255, $bcolor[1]/255, $bcolor[2]/255);
}
break;
}
case 1: { // Gray scale
$this->gradients[$n]['colspace'] = 'DeviceGray';
if (!empty($background)) {
$this->gradients[$n]['background'] = sprintf('%F', $bcolor[0]/255);
}
break;
}
}
$num_stops = count($stops);
$last_stop_id = $num_stops - 1;
foreach ($stops as $key => $stop) {
$this->gradients[$n]['colors'][$key] = array();
// offset represents a location along the gradient vector
if (isset($stop['offset'])) {
$this->gradients[$n]['colors'][$key]['offset'] = $stop['offset'];
} else {
if ($key == 0) {
$this->gradients[$n]['colors'][$key]['offset'] = 0;
} elseif ($key == $last_stop_id) {
$this->gradients[$n]['colors'][$key]['offset'] = 1;
} else {
$offsetstep = (1 - $this->gradients[$n]['colors'][($key - 1)]['offset']) / ($num_stops - $key);
$this->gradients[$n]['colors'][$key]['offset'] = $this->gradients[$n]['colors'][($key - 1)]['offset'] + $offsetstep;
}
}
if (isset($stop['opacity'])) {
$this->gradients[$n]['colors'][$key]['opacity'] = $stop['opacity'];
if ((!$this->pdfa_mode) AND ($stop['opacity'] < 1)) {
$this->gradients[$n]['transparency'] = true;
}
} else {
$this->gradients[$n]['colors'][$key]['opacity'] = 1;
}
// exponent for the exponential interpolation function
if (isset($stop['exponent'])) {
$this->gradients[$n]['colors'][$key]['exponent'] = $stop['exponent'];
} else {
$this->gradients[$n]['colors'][$key]['exponent'] = 1;
}
// set colors
$color = array_values($stop['color']);
switch($numcolspace) {
case 4: { // CMYK
$this->gradients[$n]['colors'][$key]['color'] = sprintf('%F %F %F %F', $color[0]/100, $color[1]/100, $color[2]/100, $color[3]/100);
break;
}
case 3: { // RGB
$this->gradients[$n]['colors'][$key]['color'] = sprintf('%F %F %F', $color[0]/255, $color[1]/255, $color[2]/255);
break;
}
case 1: { // Gray scale
$this->gradients[$n]['colors'][$key]['color'] = sprintf('%F', $color[0]/255);
break;
}
}
}
if ($this->gradients[$n]['transparency']) {
// paint luminosity gradient
$this->_out('/TGS'.$n.' gs');
}
//paint the gradient
$this->_out('/Sh'.$n.' sh');
//restore previous Graphic State
$this->_out('Q');
if ($this->inxobj) {
// we are inside an XObject template
$this->xobjects[$this->xobjid]['gradients'][$n] = $this->gradients[$n];
}
}
/**
* Output gradient shaders.
* @author Nicola Asuni
* @since 3.1.000 (2008-06-09)
* @protected
*/
function _putshaders() {
if ($this->pdfa_mode) {
return;
}
$idt = count($this->gradients); //index for transparency gradients
foreach ($this->gradients as $id => $grad) {
if (($grad['type'] == 2) OR ($grad['type'] == 3)) {
$fc = $this->_newobj();
$out = '<<';
$out .= ' /FunctionType 3';
$out .= ' /Domain [0 1]';
$functions = '';
$bounds = '';
$encode = '';
$i = 1;
$num_cols = count($grad['colors']);
$lastcols = $num_cols - 1;
for ($i = 1; $i < $num_cols; ++$i) {
$functions .= ($fc + $i).' 0 R ';
if ($i < $lastcols) {
$bounds .= sprintf('%F ', $grad['colors'][$i]['offset']);
}
$encode .= '0 1 ';
}
$out .= ' /Functions ['.trim($functions).']';
$out .= ' /Bounds ['.trim($bounds).']';
$out .= ' /Encode ['.trim($encode).']';
$out .= ' >>';
$out .= "\n".'endobj';
$this->_out($out);
for ($i = 1; $i < $num_cols; ++$i) {
$this->_newobj();
$out = '<<';
$out .= ' /FunctionType 2';
$out .= ' /Domain [0 1]';
$out .= ' /C0 ['.$grad['colors'][($i - 1)]['color'].']';
$out .= ' /C1 ['.$grad['colors'][$i]['color'].']';
$out .= ' /N '.$grad['colors'][$i]['exponent'];
$out .= ' >>';
$out .= "\n".'endobj';
$this->_out($out);
}
// set transparency fuctions
if ($grad['transparency']) {
$ft = $this->_newobj();
$out = '<<';
$out .= ' /FunctionType 3';
$out .= ' /Domain [0 1]';
$functions = '';
$i = 1;
$num_cols = count($grad['colors']);
for ($i = 1; $i < $num_cols; ++$i) {
$functions .= ($ft + $i).' 0 R ';
}
$out .= ' /Functions ['.trim($functions).']';
$out .= ' /Bounds ['.trim($bounds).']';
$out .= ' /Encode ['.trim($encode).']';
$out .= ' >>';
$out .= "\n".'endobj';
$this->_out($out);
for ($i = 1; $i < $num_cols; ++$i) {
$this->_newobj();
$out = '<<';
$out .= ' /FunctionType 2';
$out .= ' /Domain [0 1]';
$out .= ' /C0 ['.$grad['colors'][($i - 1)]['opacity'].']';
$out .= ' /C1 ['.$grad['colors'][$i]['opacity'].']';
$out .= ' /N '.$grad['colors'][$i]['exponent'];
$out .= ' >>';
$out .= "\n".'endobj';
$this->_out($out);
}
}
}
// set shading object
$this->_newobj();
$out = '<< /ShadingType '.$grad['type'];
if (isset($grad['colspace'])) {
$out .= ' /ColorSpace /'.$grad['colspace'];
} else {
$out .= ' /ColorSpace /DeviceRGB';
}
if (isset($grad['background']) AND !empty($grad['background'])) {
$out .= ' /Background ['.$grad['background'].']';
}
if (isset($grad['antialias']) AND ($grad['antialias'] === true)) {
$out .= ' /AntiAlias true';
}
if ($grad['type'] == 2) {
$out .= ' '.sprintf('/Coords [%F %F %F %F]', $grad['coords'][0], $grad['coords'][1], $grad['coords'][2], $grad['coords'][3]);
$out .= ' /Domain [0 1]';
$out .= ' /Function '.$fc.' 0 R';
$out .= ' /Extend [true true]';
$out .= ' >>';
} elseif ($grad['type'] == 3) {
//x0, y0, r0, x1, y1, r1
//at this this time radius of inner circle is 0
$out .= ' '.sprintf('/Coords [%F %F 0 %F %F %F]', $grad['coords'][0], $grad['coords'][1], $grad['coords'][2], $grad['coords'][3], $grad['coords'][4]);
$out .= ' /Domain [0 1]';
$out .= ' /Function '.$fc.' 0 R';
$out .= ' /Extend [true true]';
$out .= ' >>';
} elseif ($grad['type'] == 6) {
$out .= ' /BitsPerCoordinate 16';
$out .= ' /BitsPerComponent 8';
$out .= ' /Decode[0 1 0 1 0 1 0 1 0 1]';
$out .= ' /BitsPerFlag 8';
$stream = $this->_getrawstream($grad['stream']);
$out .= ' /Length '.strlen($stream);
$out .= ' >>';
$out .= ' stream'."\n".$stream."\n".'endstream';
}
$out .= "\n".'endobj';
$this->_out($out);
if ($grad['transparency']) {
$shading_transparency = preg_replace('/\/ColorSpace \/[^\s]+/si', '/ColorSpace /DeviceGray', $out);
$shading_transparency = preg_replace('/\/Function [0-9]+ /si', '/Function '.$ft.' ', $shading_transparency);
}
$this->gradients[$id]['id'] = $this->n;
// set pattern object
$this->_newobj();
$out = '<< /Type /Pattern /PatternType 2';
$out .= ' /Shading '.$this->gradients[$id]['id'].' 0 R';
$out .= ' >>';
$out .= "\n".'endobj';
$this->_out($out);
$this->gradients[$id]['pattern'] = $this->n;
// set shading and pattern for transparency mask
if ($grad['transparency']) {
// luminosity pattern
$idgs = $id + $idt;
$this->_newobj();
$this->_out($shading_transparency);
$this->gradients[$idgs]['id'] = $this->n;
$this->_newobj();
$out = '<< /Type /Pattern /PatternType 2';
$out .= ' /Shading '.$this->gradients[$idgs]['id'].' 0 R';
$out .= ' >>';
$out .= "\n".'endobj';
$this->_out($out);
$this->gradients[$idgs]['pattern'] = $this->n;
// luminosity XObject
$oid = $this->_newobj();
$this->xobjects['LX'.$oid] = array('n' => $oid);
$filter = '';
$stream = 'q /a0 gs /Pattern cs /p'.$idgs.' scn 0 0 '.$this->wPt.' '.$this->hPt.' re f Q';
if ($this->compress) {
$filter = ' /Filter /FlateDecode';
$stream = gzcompress($stream);
}
$stream = $this->_getrawstream($stream);
$out = '<< /Type /XObject /Subtype /Form /FormType 1'.$filter;
$out .= ' /Length '.strlen($stream);
$rect = sprintf('%F %F', $this->wPt, $this->hPt);
$out .= ' /BBox [0 0 '.$rect.']';
$out .= ' /Group << /Type /Group /S /Transparency /CS /DeviceGray >>';
$out .= ' /Resources <<';
$out .= ' /ExtGState << /a0 << /ca 1 /CA 1 >> >>';
$out .= ' /Pattern << /p'.$idgs.' '.$this->gradients[$idgs]['pattern'].' 0 R >>';
$out .= ' >>';
$out .= ' >> ';
$out .= ' stream'."\n".$stream."\n".'endstream';
$out .= "\n".'endobj';
$this->_out($out);
// SMask
$this->_newobj();
$out = '<< /Type /Mask /S /Luminosity /G '.($this->n - 1).' 0 R >>'."\n".'endobj';
$this->_out($out);
// ExtGState
$this->_newobj();
$out = '<< /Type /ExtGState /SMask '.($this->n - 1).' 0 R /AIS false >>'."\n".'endobj';
$this->_out($out);
$this->extgstates[] = array('n' => $this->n, 'name' => 'TGS'.$id);
}
}
}
/**
* Draw the sector of a circle.
* It can be used for instance to render pie charts.
* @param $xc (float) abscissa of the center.
* @param $yc (float) ordinate of the center.
* @param $r (float) radius.
* @param $a (float) start angle (in degrees).
* @param $b (float) end angle (in degrees).
* @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
* @param $cw: (float) indicates whether to go clockwise (default: true).
* @param $o: (float) origin of angles (0 for 3 o'clock, 90 for noon, 180 for 9 o'clock, 270 for 6 o'clock). Default: 90.
* @author Maxime Delorme, Nicola Asuni
* @since 3.1.000 (2008-06-09)
* @public
*/
public function PieSector($xc, $yc, $r, $a, $b, $style='FD', $cw=true, $o=90) {
$this->PieSectorXY($xc, $yc, $r, $r, $a, $b, $style, $cw, $o);
}
/**
* Draw the sector of an ellipse.
* It can be used for instance to render pie charts.
* @param $xc (float) abscissa of the center.
* @param $yc (float) ordinate of the center.
* @param $rx (float) the x-axis radius.
* @param $ry (float) the y-axis radius.
* @param $a (float) start angle (in degrees).
* @param $b (float) end angle (in degrees).
* @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
* @param $cw: (float) indicates whether to go clockwise.
* @param $o: (float) origin of angles (0 for 3 o'clock, 90 for noon, 180 for 9 o'clock, 270 for 6 o'clock).
* @param $nc (integer) Number of curves used to draw a 90 degrees portion of arc.
* @author Maxime Delorme, Nicola Asuni
* @since 3.1.000 (2008-06-09)
* @public
*/
public function PieSectorXY($xc, $yc, $rx, $ry, $a, $b, $style='FD', $cw=false, $o=0, $nc=2) {
if ($this->state != 2) {
return;
}
if ($this->rtl) {
$xc = ($this->w - $xc);
}
$op = TCPDF_STATIC::getPathPaintOperator($style);
if ($op == 'f') {
$line_style = array();
}
if ($cw) {
$d = $b;
$b = (360 - $a + $o);
$a = (360 - $d + $o);
} else {
$b += $o;
$a += $o;
}
$this->_outellipticalarc($xc, $yc, $rx, $ry, 0, $a, $b, true, $nc);
$this->_out($op);
}
/**
* Embed vector-based Adobe Illustrator (AI) or AI-compatible EPS files.
* NOTE: EPS is not yet fully implemented, use the setRasterizeVectorImages() method to enable/disable rasterization of vector images using ImageMagick library.
* Only vector drawing is supported, not text or bitmap.
* Although the script was successfully tested with various AI format versions, best results are probably achieved with files that were exported in the AI3 format (tested with Illustrator CS2, Freehand MX and Photoshop CS2).
* @param $file (string) Name of the file containing the image or a '@' character followed by the EPS/AI data string.
* @param $x (float) Abscissa of the upper-left corner.
* @param $y (float) Ordinate of the upper-left corner.
* @param $w (float) Width of the image in the page. If not specified or equal to zero, it is automatically calculated.
* @param $h (float) Height of the image in the page. If not specified or equal to zero, it is automatically calculated.
* @param $link (mixed) URL or identifier returned by AddLink().
* @param $useBoundingBox (boolean) specifies whether to position the bounding box (true) or the complete canvas (false) at location (x,y). Default value is true.
* @param $align (string) Indicates the alignment of the pointer next to image insertion relative to image height. The value can be:
T: top-right for LTR or top-left for RTL
M: middle-right for LTR or middle-left for RTL
B: bottom-right for LTR or bottom-left for RTL
N: next line
* @param $palign (string) Allows to center or align the image on the current line. Possible values are:
L : left align
C : center
R : right align
'' : empty string : left for LTR or right for RTL
* @param $border (mixed) Indicates if borders must be drawn around the cell. The value can be a number:
0: no border (default)
1: frame
or a string containing some or all of the following characters (in any order):
L: left
T: top
R: right
B: bottom
or an array of line styles for each border group - for example: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
* @param $fitonpage (boolean) if true the image is resized to not exceed page dimensions.
* @param $fixoutvals (boolean) if true remove values outside the bounding box.
* @author Valentin Schmidt, Nicola Asuni
* @since 3.1.000 (2008-06-09)
* @public
*/
public function ImageEps($file, $x='', $y='', $w=0, $h=0, $link='', $useBoundingBox=true, $align='', $palign='', $border=0, $fitonpage=false, $fixoutvals=false) {
if ($this->state != 2) {
return;
}
if ($this->rasterize_vector_images AND ($w > 0) AND ($h > 0)) {
// convert EPS to raster image using GD or ImageMagick libraries
return $this->Image($file, $x, $y, $w, $h, 'EPS', $link, $align, true, 300, $palign, false, false, $border, false, false, $fitonpage);
}
if ($x === '') {
$x = $this->x;
}
if ($y === '') {
$y = $this->y;
}
// check page for no-write regions and adapt page margins if necessary
list($x, $y) = $this->checkPageRegions($h, $x, $y);
$k = $this->k;
if ($file{0} === '@') { // image from string
$data = substr($file, 1);
} else { // EPS/AI file
$data = file_get_contents($file);
}
if ($data === false) {
$this->Error('EPS file not found: '.$file);
}
$regs = array();
// EPS/AI compatibility check (only checks files created by Adobe Illustrator!)
preg_match("/%%Creator:([^\r\n]+)/", $data, $regs); # find Creator
if (count($regs) > 1) {
$version_str = trim($regs[1]); # e.g. "Adobe Illustrator(R) 8.0"
if (strpos($version_str, 'Adobe Illustrator') !== false) {
$versexp = explode(' ', $version_str);
$version = (float)array_pop($versexp);
if ($version >= 9) {
$this->Error('This version of Adobe Illustrator file is not supported: '.$file);
}
}
}
// strip binary bytes in front of PS-header
$start = strpos($data, '%!PS-Adobe');
if ($start > 0) {
$data = substr($data, $start);
}
// find BoundingBox params
preg_match("/%%BoundingBox:([^\r\n]+)/", $data, $regs);
if (count($regs) > 1) {
list($x1, $y1, $x2, $y2) = explode(' ', trim($regs[1]));
} else {
$this->Error('No BoundingBox found in EPS/AI file: '.$file);
}
$start = strpos($data, '%%EndSetup');
if ($start === false) {
$start = strpos($data, '%%EndProlog');
}
if ($start === false) {
$start = strpos($data, '%%BoundingBox');
}
$data = substr($data, $start);
$end = strpos($data, '%%PageTrailer');
if ($end===false) {
$end = strpos($data, 'showpage');
}
if ($end) {
$data = substr($data, 0, $end);
}
// calculate image width and height on document
if (($w <= 0) AND ($h <= 0)) {
$w = ($x2 - $x1) / $k;
$h = ($y2 - $y1) / $k;
} elseif ($w <= 0) {
$w = ($x2-$x1) / $k * ($h / (($y2 - $y1) / $k));
} elseif ($h <= 0) {
$h = ($y2 - $y1) / $k * ($w / (($x2 - $x1) / $k));
}
// fit the image on available space
list($w, $h, $x, $y) = $this->fitBlock($w, $h, $x, $y, $fitonpage);
if ($this->rasterize_vector_images) {
// convert EPS to raster image using GD or ImageMagick libraries
return $this->Image($file, $x, $y, $w, $h, 'EPS', $link, $align, true, 300, $palign, false, false, $border, false, false, $fitonpage);
}
// set scaling factors
$scale_x = $w / (($x2 - $x1) / $k);
$scale_y = $h / (($y2 - $y1) / $k);
// set alignment
$this->img_rb_y = $y + $h;
// set alignment
if ($this->rtl) {
if ($palign == 'L') {
$ximg = $this->lMargin;
} elseif ($palign == 'C') {
$ximg = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
} elseif ($palign == 'R') {
$ximg = $this->w - $this->rMargin - $w;
} else {
$ximg = $x - $w;
}
$this->img_rb_x = $ximg;
} else {
if ($palign == 'L') {
$ximg = $this->lMargin;
} elseif ($palign == 'C') {
$ximg = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
} elseif ($palign == 'R') {
$ximg = $this->w - $this->rMargin - $w;
} else {
$ximg = $x;
}
$this->img_rb_x = $ximg + $w;
}
if ($useBoundingBox) {
$dx = $ximg * $k - $x1;
$dy = $y * $k - $y1;
} else {
$dx = $ximg * $k;
$dy = $y * $k;
}
// save the current graphic state
$this->_out('q'.$this->epsmarker);
// translate
$this->_out(sprintf('%F %F %F %F %F %F cm', 1, 0, 0, 1, $dx, $dy + ($this->hPt - (2 * $y * $k) - ($y2 - $y1))));
// scale
if (isset($scale_x)) {
$this->_out(sprintf('%F %F %F %F %F %F cm', $scale_x, 0, 0, $scale_y, $x1 * (1 - $scale_x), $y2 * (1 - $scale_y)));
}
// handle pc/unix/mac line endings
$lines = preg_split('/[\r\n]+/si', $data, -1, PREG_SPLIT_NO_EMPTY);
$u=0;
$cnt = count($lines);
for ($i=0; $i < $cnt; ++$i) {
$line = $lines[$i];
if (($line == '') OR ($line{0} == '%')) {
continue;
}
$len = strlen($line);
// check for spot color names
$color_name = '';
if (strcasecmp('x', substr(trim($line), -1)) == 0) {
if (preg_match('/\([^\)]*\)/', $line, $matches) > 0) {
// extract spot color name
$color_name = $matches[0];
// remove color name from string
$line = str_replace(' '.$color_name, '', $line);
// remove pharentesis from color name
$color_name = substr($color_name, 1, -1);
}
}
$chunks = explode(' ', $line);
$cmd = trim(array_pop($chunks));
// RGB
if (($cmd == 'Xa') OR ($cmd == 'XA')) {
$b = array_pop($chunks);
$g = array_pop($chunks);
$r = array_pop($chunks);
$this->_out(''.$r.' '.$g.' '.$b.' '.($cmd=='Xa'?'rg':'RG')); //substr($line, 0, -2).'rg' -> in EPS (AI8): c m y k r g b rg!
continue;
}
$skip = false;
if ($fixoutvals) {
// check for values outside the bounding box
switch ($cmd) {
case 'm':
case 'l':
case 'L': {
// skip values outside bounding box
foreach ($chunks as $key => $val) {
if ((($key % 2) == 0) AND (($val < $x1) OR ($val > $x2))) {
$skip = true;
} elseif ((($key % 2) != 0) AND (($val < $y1) OR ($val > $y2))) {
$skip = true;
}
}
}
}
}
switch ($cmd) {
case 'm':
case 'l':
case 'v':
case 'y':
case 'c':
case 'k':
case 'K':
case 'g':
case 'G':
case 's':
case 'S':
case 'J':
case 'j':
case 'w':
case 'M':
case 'd':
case 'n': {
if ($skip) {
break;
}
$this->_out($line);
break;
}
case 'x': {// custom fill color
if (empty($color_name)) {
// CMYK color
list($col_c, $col_m, $col_y, $col_k) = $chunks;
$this->_out(''.$col_c.' '.$col_m.' '.$col_y.' '.$col_k.' k');
} else {
// Spot Color (CMYK + tint)
list($col_c, $col_m, $col_y, $col_k, $col_t) = $chunks;
$this->AddSpotColor($color_name, ($col_c * 100), ($col_m * 100), ($col_y * 100), ($col_k * 100));
$color_cmd = sprintf('/CS%d cs %F scn', $this->spot_colors[$color_name]['i'], (1 - $col_t));
$this->_out($color_cmd);
}
break;
}
case 'X': { // custom stroke color
if (empty($color_name)) {
// CMYK color
list($col_c, $col_m, $col_y, $col_k) = $chunks;
$this->_out(''.$col_c.' '.$col_m.' '.$col_y.' '.$col_k.' K');
} else {
// Spot Color (CMYK + tint)
list($col_c, $col_m, $col_y, $col_k, $col_t) = $chunks;
$this->AddSpotColor($color_name, ($col_c * 100), ($col_m * 100), ($col_y * 100), ($col_k * 100));
$color_cmd = sprintf('/CS%d CS %F SCN', $this->spot_colors[$color_name]['i'], (1 - $col_t));
$this->_out($color_cmd);
}
break;
}
case 'Y':
case 'N':
case 'V':
case 'L':
case 'C': {
if ($skip) {
break;
}
$line[($len - 1)] = strtolower($cmd);
$this->_out($line);
break;
}
case 'b':
case 'B': {
$this->_out($cmd . '*');
break;
}
case 'f':
case 'F': {
if ($u > 0) {
$isU = false;
$max = min(($i + 5), $cnt);
for ($j = ($i + 1); $j < $max; ++$j) {
$isU = ($isU OR (($lines[$j] == 'U') OR ($lines[$j] == '*U')));
}
if ($isU) {
$this->_out('f*');
}
} else {
$this->_out('f*');
}
break;
}
case '*u': {
++$u;
break;
}
case '*U': {
--$u;
break;
}
}
}
// restore previous graphic state
$this->_out($this->epsmarker.'Q');
if (!empty($border)) {
$bx = $this->x;
$by = $this->y;
$this->x = $ximg;
if ($this->rtl) {
$this->x += $w;
}
$this->y = $y;
$this->Cell($w, $h, '', $border, 0, '', 0, '', 0, true);
$this->x = $bx;
$this->y = $by;
}
if ($link) {
$this->Link($ximg, $y, $w, $h, $link, 0);
}
// set pointer to align the next text/objects
switch($align) {
case 'T':{
$this->y = $y;
$this->x = $this->img_rb_x;
break;
}
case 'M':{
$this->y = $y + round($h/2);
$this->x = $this->img_rb_x;
break;
}
case 'B':{
$this->y = $this->img_rb_y;
$this->x = $this->img_rb_x;
break;
}
case 'N':{
$this->SetY($this->img_rb_y);
break;
}
default:{
break;
}
}
$this->endlinex = $this->img_rb_x;
}
/**
* Set document barcode.
* @param $bc (string) barcode
* @public
*/
public function setBarcode($bc='') {
$this->barcode = $bc;
}
/**
* Get current barcode.
* @return string
* @public
* @since 4.0.012 (2008-07-24)
*/
public function getBarcode() {
return $this->barcode;
}
/**
* Print a Linear Barcode.
* @param $code (string) code to print
* @param $type (string) type of barcode (see tcpdf_barcodes_1d.php for supported formats).
* @param $x (int) x position in user units (empty string = current x position)
* @param $y (int) y position in user units (empty string = current y position)
* @param $w (int) width in user units (empty string = remaining page width)
* @param $h (int) height in user units (empty string = remaining page height)
* @param $xres (float) width of the smallest bar in user units (empty string = default value = 0.4mm)
* @param $style (array) array of options:
*
boolean $style['border'] if true prints a border
*
int $style['padding'] padding to leave around the barcode in user units (set to 'auto' for automatic padding)
*
int $style['hpadding'] horizontal padding in user units (set to 'auto' for automatic padding)
*
int $style['vpadding'] vertical padding in user units (set to 'auto' for automatic padding)
*
array $style['fgcolor'] color array for bars and text
*
mixed $style['bgcolor'] color array for background (set to false for transparent)
*
boolean $style['text'] if true prints text below the barcode
*
string $style['label'] override default label
*
string $style['font'] font name for text
int $style['fontsize'] font size for text
*
int $style['stretchtext']: 0 = disabled; 1 = horizontal scaling only if necessary; 2 = forced horizontal scaling; 3 = character spacing only if necessary; 4 = forced character spacing.
*
string $style['position'] horizontal position of the containing barcode cell on the page: L = left margin; C = center; R = right margin.
*
string $style['align'] horizontal position of the barcode on the containing rectangle: L = left; C = center; R = right.
*
string $style['stretch'] if true stretch the barcode to best fit the available width, otherwise uses $xres resolution for a single bar.
*
string $style['fitwidth'] if true reduce the width to fit the barcode width + padding. When this option is enabled the 'stretch' option is automatically disabled.
*
string $style['cellfitalign'] this option works only when 'fitwidth' is true and 'position' is unset or empty. Set the horizontal position of the containing barcode cell inside the specified rectangle: L = left; C = center; R = right.
* @param $align (string) Indicates the alignment of the pointer next to barcode insertion relative to barcode height. The value can be:
T: top-right for LTR or top-left for RTL
M: middle-right for LTR or middle-left for RTL
B: bottom-right for LTR or bottom-left for RTL
N: next line
* @author Nicola Asuni
* @since 3.1.000 (2008-06-09)
* @public
*/
public function write1DBarcode($code, $type, $x='', $y='', $w='', $h='', $xres='', $style='', $align='') {
if (TCPDF_STATIC::empty_string(trim($code))) {
return;
}
require_once(dirname(__FILE__).'/tcpdf_barcodes_1d.php');
// save current graphic settings
$gvars = $this->getGraphicVars();
// create new barcode object
$barcodeobj = new TCPDFBarcode($code, $type);
$arrcode = $barcodeobj->getBarcodeArray();
if (($arrcode === false) OR empty($arrcode) OR ($arrcode['maxw'] == 0)) {
$this->Error('Error in 1D barcode string');
}
// set default values
if (!isset($style['position'])) {
$style['position'] = '';
} elseif ($style['position'] == 'S') {
// keep this for backward compatibility
$style['position'] = '';
$style['stretch'] = true;
}
if (!isset($style['fitwidth'])) {
if (!isset($style['stretch'])) {
$style['fitwidth'] = true;
} else {
$style['fitwidth'] = false;
}
}
if ($style['fitwidth']) {
// disable stretch
$style['stretch'] = false;
}
if (!isset($style['stretch'])) {
if (($w === '') OR ($w <= 0)) {
$style['stretch'] = false;
} else {
$style['stretch'] = true;
}
}
if (!isset($style['fgcolor'])) {
$style['fgcolor'] = array(0,0,0); // default black
}
if (!isset($style['bgcolor'])) {
$style['bgcolor'] = false; // default transparent
}
if (!isset($style['border'])) {
$style['border'] = false;
}
$fontsize = 0;
if (!isset($style['text'])) {
$style['text'] = false;
}
if ($style['text'] AND isset($style['font'])) {
if (isset($style['fontsize'])) {
$fontsize = $style['fontsize'];
}
$this->SetFont($style['font'], '', $fontsize);
}
if (!isset($style['stretchtext'])) {
$style['stretchtext'] = 4;
}
if ($x === '') {
$x = $this->x;
}
if ($y === '') {
$y = $this->y;
}
// check page for no-write regions and adapt page margins if necessary
list($x, $y) = $this->checkPageRegions($h, $x, $y);
if (($w === '') OR ($w <= 0)) {
if ($this->rtl) {
$w = $x - $this->lMargin;
} else {
$w = $this->w - $this->rMargin - $x;
}
}
// padding
if (!isset($style['padding'])) {
$padding = 0;
} elseif ($style['padding'] === 'auto') {
$padding = 10 * ($w / ($arrcode['maxw'] + 20));
} else {
$padding = floatval($style['padding']);
}
// horizontal padding
if (!isset($style['hpadding'])) {
$hpadding = $padding;
} elseif ($style['hpadding'] === 'auto') {
$hpadding = 10 * ($w / ($arrcode['maxw'] + 20));
} else {
$hpadding = floatval($style['hpadding']);
}
// vertical padding
if (!isset($style['vpadding'])) {
$vpadding = $padding;
} elseif ($style['vpadding'] === 'auto') {
$vpadding = ($hpadding / 2);
} else {
$vpadding = floatval($style['vpadding']);
}
// calculate xres (single bar width)
$max_xres = ($w - (2 * $hpadding)) / $arrcode['maxw'];
if ($style['stretch']) {
$xres = $max_xres;
} else {
if (TCPDF_STATIC::empty_string($xres)) {
$xres = (0.141 * $this->k); // default bar width = 0.4 mm
}
if ($xres > $max_xres) {
// correct xres to fit on $w
$xres = $max_xres;
}
if ((isset($style['padding']) AND ($style['padding'] === 'auto'))
OR (isset($style['hpadding']) AND ($style['hpadding'] === 'auto'))) {
$hpadding = 10 * $xres;
if (isset($style['vpadding']) AND ($style['vpadding'] === 'auto')) {
$vpadding = ($hpadding / 2);
}
}
}
if ($style['fitwidth']) {
$wold = $w;
$w = (($arrcode['maxw'] * $xres) + (2 * $hpadding));
if (isset($style['cellfitalign'])) {
switch ($style['cellfitalign']) {
case 'L': {
if ($this->rtl) {
$x -= ($wold - $w);
}
break;
}
case 'R': {
if (!$this->rtl) {
$x += ($wold - $w);
}
break;
}
case 'C': {
if ($this->rtl) {
$x -= (($wold - $w) / 2);
} else {
$x += (($wold - $w) / 2);
}
break;
}
default : {
break;
}
}
}
}
$text_height = ($this->cell_height_ratio * $fontsize / $this->k);
// height
if (($h === '') OR ($h <= 0)) {
// set default height
$h = (($arrcode['maxw'] * $xres) / 3) + (2 * $vpadding) + $text_height;
}
$barh = $h - $text_height - (2 * $vpadding);
if ($barh <=0) {
// try to reduce font or padding to fit barcode on available height
if ($text_height > $h) {
$fontsize = (($h * $this->k) / (4 * $this->cell_height_ratio));
$text_height = ($this->cell_height_ratio * $fontsize / $this->k);
$this->SetFont($style['font'], '', $fontsize);
}
if ($vpadding > 0) {
$vpadding = (($h - $text_height) / 4);
}
$barh = $h - $text_height - (2 * $vpadding);
}
// fit the barcode on available space
list($w, $h, $x, $y) = $this->fitBlock($w, $h, $x, $y, false);
// set alignment
$this->img_rb_y = $y + $h;
// set alignment
if ($this->rtl) {
if ($style['position'] == 'L') {
$xpos = $this->lMargin;
} elseif ($style['position'] == 'C') {
$xpos = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
} elseif ($style['position'] == 'R') {
$xpos = $this->w - $this->rMargin - $w;
} else {
$xpos = $x - $w;
}
$this->img_rb_x = $xpos;
} else {
if ($style['position'] == 'L') {
$xpos = $this->lMargin;
} elseif ($style['position'] == 'C') {
$xpos = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
} elseif ($style['position'] == 'R') {
$xpos = $this->w - $this->rMargin - $w;
} else {
$xpos = $x;
}
$this->img_rb_x = $xpos + $w;
}
$xpos_rect = $xpos;
if (!isset($style['align'])) {
$style['align'] = 'C';
}
switch ($style['align']) {
case 'L': {
$xpos = $xpos_rect + $hpadding;
break;
}
case 'R': {
$xpos = $xpos_rect + ($w - ($arrcode['maxw'] * $xres)) - $hpadding;
break;
}
case 'C':
default : {
$xpos = $xpos_rect + (($w - ($arrcode['maxw'] * $xres)) / 2);
break;
}
}
$xpos_text = $xpos;
// barcode is always printed in LTR direction
$tempRTL = $this->rtl;
$this->rtl = false;
// print background color
if ($style['bgcolor']) {
$this->Rect($xpos_rect, $y, $w, $h, $style['border'] ? 'DF' : 'F', '', $style['bgcolor']);
} elseif ($style['border']) {
$this->Rect($xpos_rect, $y, $w, $h, 'D');
}
// set foreground color
$this->SetDrawColorArray($style['fgcolor']);
$this->SetTextColorArray($style['fgcolor']);
// print bars
foreach ($arrcode['bcode'] as $k => $v) {
$bw = ($v['w'] * $xres);
if ($v['t']) {
// draw a vertical bar
$ypos = $y + $vpadding + ($v['p'] * $barh / $arrcode['maxh']);
$this->Rect($xpos, $ypos, $bw, ($v['h'] * $barh / $arrcode['maxh']), 'F', array(), $style['fgcolor']);
}
$xpos += $bw;
}
// print text
if ($style['text']) {
if (isset($style['label']) AND !TCPDF_STATIC::empty_string($style['label'])) {
$label = $style['label'];
} else {
$label = $code;
}
$txtwidth = ($arrcode['maxw'] * $xres);
if ($this->GetStringWidth($label) > $txtwidth) {
$style['stretchtext'] = 2;
}
// print text
$this->x = $xpos_text;
$this->y = $y + $vpadding + $barh;
$cellpadding = $this->cell_padding;
$this->SetCellPadding(0);
$this->Cell($txtwidth, '', $label, 0, 0, 'C', false, '', $style['stretchtext'], false, 'T', 'T');
$this->cell_padding = $cellpadding;
}
// restore original direction
$this->rtl = $tempRTL;
// restore previous settings
$this->setGraphicVars($gvars);
// set pointer to align the next text/objects
switch($align) {
case 'T':{
$this->y = $y;
$this->x = $this->img_rb_x;
break;
}
case 'M':{
$this->y = $y + round($h / 2);
$this->x = $this->img_rb_x;
break;
}
case 'B':{
$this->y = $this->img_rb_y;
$this->x = $this->img_rb_x;
break;
}
case 'N':{
$this->SetY($this->img_rb_y);
break;
}
default:{
break;
}
}
$this->endlinex = $this->img_rb_x;
}
/**
* Print 2D Barcode.
* @param $code (string) code to print
* @param $type (string) type of barcode (see tcpdf_barcodes_2d.php for supported formats).
* @param $x (int) x position in user units
* @param $y (int) y position in user units
* @param $w (int) width in user units
* @param $h (int) height in user units
* @param $style (array) array of options:
*
boolean $style['border'] if true prints a border around the barcode
*
int $style['padding'] padding to leave around the barcode in barcode units (set to 'auto' for automatic padding)
*
int $style['hpadding'] horizontal padding in barcode units (set to 'auto' for automatic padding)
*
int $style['vpadding'] vertical padding in barcode units (set to 'auto' for automatic padding)
*
int $style['module_width'] width of a single module in points
*
int $style['module_height'] height of a single module in points
*
array $style['fgcolor'] color array for bars and text
*
mixed $style['bgcolor'] color array for background or false for transparent
*
string $style['position'] barcode position on the page: L = left margin; C = center; R = right margin; S = stretch
$style['module_width'] width of a single module in points
*
$style['module_height'] height of a single module in points
* @param $align (string) Indicates the alignment of the pointer next to barcode insertion relative to barcode height. The value can be:
T: top-right for LTR or top-left for RTL
M: middle-right for LTR or middle-left for RTL
B: bottom-right for LTR or bottom-left for RTL
N: next line
* @param $distort (boolean) if true distort the barcode to fit width and height, otherwise preserve aspect ratio
* @author Nicola Asuni
* @since 4.5.037 (2009-04-07)
* @public
*/
public function write2DBarcode($code, $type, $x='', $y='', $w='', $h='', $style='', $align='', $distort=false) {
if (TCPDF_STATIC::empty_string(trim($code))) {
return;
}
require_once(dirname(__FILE__).'/tcpdf_barcodes_2d.php');
// save current graphic settings
$gvars = $this->getGraphicVars();
// create new barcode object
$barcodeobj = new TCPDF2DBarcode($code, $type);
$arrcode = $barcodeobj->getBarcodeArray();
if (($arrcode === false) OR empty($arrcode) OR !isset($arrcode['num_rows']) OR ($arrcode['num_rows'] == 0) OR !isset($arrcode['num_cols']) OR ($arrcode['num_cols'] == 0)) {
$this->Error('Error in 2D barcode string');
}
// set default values
if (!isset($style['position'])) {
$style['position'] = '';
}
if (!isset($style['fgcolor'])) {
$style['fgcolor'] = array(0,0,0); // default black
}
if (!isset($style['bgcolor'])) {
$style['bgcolor'] = false; // default transparent
}
if (!isset($style['border'])) {
$style['border'] = false;
}
// padding
if (!isset($style['padding'])) {
$style['padding'] = 0;
} elseif ($style['padding'] === 'auto') {
$style['padding'] = 4;
}
if (!isset($style['hpadding'])) {
$style['hpadding'] = $style['padding'];
} elseif ($style['hpadding'] === 'auto') {
$style['hpadding'] = 4;
}
if (!isset($style['vpadding'])) {
$style['vpadding'] = $style['padding'];
} elseif ($style['vpadding'] === 'auto') {
$style['vpadding'] = 4;
}
$hpad = (2 * $style['hpadding']);
$vpad = (2 * $style['vpadding']);
// cell (module) dimension
if (!isset($style['module_width'])) {
$style['module_width'] = 1; // width of a single module in points
}
if (!isset($style['module_height'])) {
$style['module_height'] = 1; // height of a single module in points
}
if ($x === '') {
$x = $this->x;
}
if ($y === '') {
$y = $this->y;
}
// check page for no-write regions and adapt page margins if necessary
list($x, $y) = $this->checkPageRegions($h, $x, $y);
// number of barcode columns and rows
$rows = $arrcode['num_rows'];
$cols = $arrcode['num_cols'];
// module width and height
$mw = $style['module_width'];
$mh = $style['module_height'];
if (($mw == 0) OR ($mh == 0)) {
$this->Error('Error in 2D barcode string');
}
// get max dimensions
if ($this->rtl) {
$maxw = $x - $this->lMargin;
} else {
$maxw = $this->w - $this->rMargin - $x;
}
$maxh = ($this->h - $this->tMargin - $this->bMargin);
$ratioHW = ((($rows * $mh) + $hpad) / (($cols * $mw) + $vpad));
$ratioWH = ((($cols * $mw) + $vpad) / (($rows * $mh) + $hpad));
if (!$distort) {
if (($maxw * $ratioHW) > $maxh) {
$maxw = $maxh * $ratioWH;
}
if (($maxh * $ratioWH) > $maxw) {
$maxh = $maxw * $ratioHW;
}
}
// set maximum dimesions
if ($w > $maxw) {
$w = $maxw;
}
if ($h > $maxh) {
$h = $maxh;
}
// set dimensions
if ((($w === '') OR ($w <= 0)) AND (($h === '') OR ($h <= 0))) {
$w = ($cols + $hpad) * ($mw / $this->k);
$h = ($rows + $vpad) * ($mh / $this->k);
} elseif (($w === '') OR ($w <= 0)) {
$w = $h * $ratioWH;
} elseif (($h === '') OR ($h <= 0)) {
$h = $w * $ratioHW;
}
// barcode size (excluding padding)
$bw = ($w * $cols) / ($cols + $hpad);
$bh = ($h * $rows) / ($rows + $vpad);
// dimension of single barcode cell unit
$cw = $bw / $cols;
$ch = $bh / $rows;
if (!$distort) {
if (($cw / $ch) > ($mw / $mh)) {
// correct horizontal distortion
$cw = $ch * $mw / $mh;
$bw = $cw * $cols;
$style['hpadding'] = ($w - $bw) / (2 * $cw);
} else {
// correct vertical distortion
$ch = $cw * $mh / $mw;
$bh = $ch * $rows;
$style['vpadding'] = ($h - $bh) / (2 * $ch);
}
}
// fit the barcode on available space
list($w, $h, $x, $y) = $this->fitBlock($w, $h, $x, $y, false);
// set alignment
$this->img_rb_y = $y + $h;
// set alignment
if ($this->rtl) {
if ($style['position'] == 'L') {
$xpos = $this->lMargin;
} elseif ($style['position'] == 'C') {
$xpos = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
} elseif ($style['position'] == 'R') {
$xpos = $this->w - $this->rMargin - $w;
} else {
$xpos = $x - $w;
}
$this->img_rb_x = $xpos;
} else {
if ($style['position'] == 'L') {
$xpos = $this->lMargin;
} elseif ($style['position'] == 'C') {
$xpos = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
} elseif ($style['position'] == 'R') {
$xpos = $this->w - $this->rMargin - $w;
} else {
$xpos = $x;
}
$this->img_rb_x = $xpos + $w;
}
$xstart = $xpos + ($style['hpadding'] * $cw);
$ystart = $y + ($style['vpadding'] * $ch);
// barcode is always printed in LTR direction
$tempRTL = $this->rtl;
$this->rtl = false;
// print background color
if ($style['bgcolor']) {
$this->Rect($xpos, $y, $w, $h, $style['border'] ? 'DF' : 'F', '', $style['bgcolor']);
} elseif ($style['border']) {
$this->Rect($xpos, $y, $w, $h, 'D');
}
// set foreground color
$this->SetDrawColorArray($style['fgcolor']);
// print barcode cells
// for each row
for ($r = 0; $r < $rows; ++$r) {
$xr = $xstart;
// for each column
for ($c = 0; $c < $cols; ++$c) {
if ($arrcode['bcode'][$r][$c] == 1) {
// draw a single barcode cell
$this->Rect($xr, $ystart, $cw, $ch, 'F', array(), $style['fgcolor']);
}
$xr += $cw;
}
$ystart += $ch;
}
// restore original direction
$this->rtl = $tempRTL;
// restore previous settings
$this->setGraphicVars($gvars);
// set pointer to align the next text/objects
switch($align) {
case 'T':{
$this->y = $y;
$this->x = $this->img_rb_x;
break;
}
case 'M':{
$this->y = $y + round($h/2);
$this->x = $this->img_rb_x;
break;
}
case 'B':{
$this->y = $this->img_rb_y;
$this->x = $this->img_rb_x;
break;
}
case 'N':{
$this->SetY($this->img_rb_y);
break;
}
default:{
break;
}
}
$this->endlinex = $this->img_rb_x;
}
/**
* Returns an array containing current margins:
*