From 11af2bc93f5966c4851d6752ded1da5de8450626 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Mon, 25 Apr 2011 22:06:44 -0700 Subject: Update test golden files. --- modules/gallery/tests/controller_auth_data.txt | 1 + modules/gallery/tests/xss_data.txt | 131 ++++++++++++++----------- 2 files changed, 73 insertions(+), 59 deletions(-) (limited to 'modules/gallery/tests') diff --git a/modules/gallery/tests/controller_auth_data.txt b/modules/gallery/tests/controller_auth_data.txt index f1192071..e35708c0 100644 --- a/modules/gallery/tests/controller_auth_data.txt +++ b/modules/gallery/tests/controller_auth_data.txt @@ -1,6 +1,7 @@ modules/comment/controllers/admin_manage_comments.php queue DIRTY_CSRF modules/comment/helpers/comment_rss.php feed DIRTY_AUTH modules/digibug/controllers/digibug.php print_proxy DIRTY_CSRF|DIRTY_AUTH +modules/g2_import/controllers/admin_g2_import.php autocomplete DIRTY_CSRF modules/g2_import/controllers/g2.php map DIRTY_CSRF modules/gallery/controllers/admin.php __call DIRTY_AUTH modules/gallery/controllers/albums.php index DIRTY_AUTH diff --git a/modules/gallery/tests/xss_data.txt b/modules/gallery/tests/xss_data.txt index 0c812fb4..7da79b23 100644 --- a/modules/gallery/tests/xss_data.txt +++ b/modules/gallery/tests/xss_data.txt @@ -42,7 +42,8 @@ modules/digibug/views/digibug_form.html.php 4 DIRTY form:: modules/digibug/views/digibug_form.html.php 6 DIRTY form::hidden($key,$value) modules/exif/views/exif_dialog.html.php 14 DIRTY $details[$i]["caption"] modules/exif/views/exif_dialog.html.php 21 DIRTY $details[$i]["caption"] -modules/g2_import/views/admin_g2_import.html.php 9 DIRTY $form +modules/g2_import/views/admin_g2_import.html.php 7 DIRTY_JS url::site("__ARGS__") +modules/g2_import/views/admin_g2_import.html.php 52 DIRTY $form modules/gallery/views/admin_advanced_settings.html.php 21 DIRTY_ATTR text::alternate("g-odd","g-even") modules/gallery/views/admin_advanced_settings.html.php 22 DIRTY $var->module_name modules/gallery/views/admin_block_log_entries.html.php 4 DIRTY_ATTR log::severity_class($entry->severity) @@ -77,30 +78,35 @@ modules/gallery/views/admin_languages.html.php 62 DIRTY form:: modules/gallery/views/admin_languages.html.php 63 DIRTY $display_name modules/gallery/views/admin_languages.html.php 65 DIRTY form::radio("default_locale",$code,($default_locale==$code),((isset($installed_locales[$code]))?'':'disabled="disabled"')) modules/gallery/views/admin_languages.html.php 113 DIRTY $share_translations_form -modules/gallery/views/admin_maintenance.html.php 40 DIRTY_ATTR text::alternate("g-odd","g-even") -modules/gallery/views/admin_maintenance.html.php 40 DIRTY_ATTR log::severity_class($task->severity) -modules/gallery/views/admin_maintenance.html.php 41 DIRTY_ATTR log::severity_class($task->severity) -modules/gallery/views/admin_maintenance.html.php 42 DIRTY $task->name -modules/gallery/views/admin_maintenance.html.php 45 DIRTY $task->description -modules/gallery/views/admin_maintenance.html.php 86 DIRTY_ATTR text::alternate("g-odd","g-even") -modules/gallery/views/admin_maintenance.html.php 86 DIRTY_ATTR $task->state=="stalled"?"g-warning":"" -modules/gallery/views/admin_maintenance.html.php 87 DIRTY_ATTR $task->state=="stalled"?"g-warning":"" -modules/gallery/views/admin_maintenance.html.php 88 DIRTY gallery::date_time($task->updated) -modules/gallery/views/admin_maintenance.html.php 91 DIRTY $task->name -modules/gallery/views/admin_maintenance.html.php 106 DIRTY $task->status -modules/gallery/views/admin_maintenance.html.php 162 DIRTY_ATTR text::alternate("g-odd","g-even") -modules/gallery/views/admin_maintenance.html.php 162 DIRTY_ATTR $task->state=="success"?"g-success":"g-error" -modules/gallery/views/admin_maintenance.html.php 163 DIRTY_ATTR $task->state=="success"?"g-success":"g-error" -modules/gallery/views/admin_maintenance.html.php 164 DIRTY gallery::date_time($task->updated) -modules/gallery/views/admin_maintenance.html.php 167 DIRTY $task->name -modules/gallery/views/admin_maintenance.html.php 179 DIRTY $task->status +modules/gallery/views/admin_maintenance.html.php 42 DIRTY_ATTR text::alternate("g-odd","g-even") +modules/gallery/views/admin_maintenance.html.php 42 DIRTY_ATTR log::severity_class($task->severity) +modules/gallery/views/admin_maintenance.html.php 43 DIRTY_ATTR log::severity_class($task->severity) +modules/gallery/views/admin_maintenance.html.php 44 DIRTY $task->name +modules/gallery/views/admin_maintenance.html.php 47 DIRTY $task->description +modules/gallery/views/admin_maintenance.html.php 88 DIRTY_ATTR text::alternate("g-odd","g-even") +modules/gallery/views/admin_maintenance.html.php 88 DIRTY_ATTR $task->state=="stalled"?"g-warning":"" +modules/gallery/views/admin_maintenance.html.php 89 DIRTY_ATTR $task->state=="stalled"?"g-warning":"" +modules/gallery/views/admin_maintenance.html.php 90 DIRTY gallery::date_time($task->updated) +modules/gallery/views/admin_maintenance.html.php 93 DIRTY $task->name +modules/gallery/views/admin_maintenance.html.php 108 DIRTY $task->status +modules/gallery/views/admin_maintenance.html.php 164 DIRTY_ATTR text::alternate("g-odd","g-even") +modules/gallery/views/admin_maintenance.html.php 164 DIRTY_ATTR $task->state=="success"?"g-success":"g-error" +modules/gallery/views/admin_maintenance.html.php 165 DIRTY_ATTR $task->state=="success"?"g-success":"g-error" +modules/gallery/views/admin_maintenance.html.php 166 DIRTY gallery::date_time($task->updated) +modules/gallery/views/admin_maintenance.html.php 169 DIRTY $task->name +modules/gallery/views/admin_maintenance.html.php 181 DIRTY $task->status modules/gallery/views/admin_maintenance_show_log.html.php 8 DIRTY_JS url::site("admin/maintenance/save_log/$task->id?csrf=$csrf") modules/gallery/views/admin_maintenance_show_log.html.php 13 DIRTY $task->name modules/gallery/views/admin_maintenance_task.html.php 75 DIRTY $task->name modules/gallery/views/admin_modules.html.php 51 DIRTY access::csrf_form_field() -modules/gallery/views/admin_modules.html.php 60 DIRTY_ATTR text::alternate("g-odd","g-even") -modules/gallery/views/admin_modules.html.php 63 DIRTY form::checkbox($data,'1',module::is_active($module_name)) -modules/gallery/views/admin_modules.html.php 65 DIRTY $module_info->version +modules/gallery/views/admin_modules.html.php 61 DIRTY_ATTR text::alternate("g-odd","g-even") +modules/gallery/views/admin_modules.html.php 64 DIRTY form::checkbox($data,'1',module::is_active($module_name)) +modules/gallery/views/admin_modules.html.php 66 DIRTY $module_info->version +modules/gallery/views/admin_modules.html.php 74 DIRTY_JS $module_info->author_url +modules/gallery/views/admin_modules.html.php 81 DIRTY_ATTR $module_info->author_name +modules/gallery/views/admin_modules.html.php 85 DIRTY $module_info->author_name +modules/gallery/views/admin_modules.html.php 93 DIRTY_JS $module_info->info_url +modules/gallery/views/admin_modules.html.php 106 DIRTY_JS $module_info->discuss_url modules/gallery/views/admin_modules_confirm.html.php 11 DIRTY_ATTR $css_class modules/gallery/views/admin_modules_confirm.html.php 11 DIRTY $message modules/gallery/views/admin_modules_confirm.html.php 16 DIRTY access::csrf_form_field() @@ -114,12 +120,17 @@ modules/gallery/views/admin_themes.html.php 3 DIRTY_JS url::s modules/gallery/views/admin_themes.html.php 5 DIRTY_JS $csrf modules/gallery/views/admin_themes.html.php 22 DIRTY $themes[$site]->name modules/gallery/views/admin_themes.html.php 24 DIRTY $themes[$site]->description -modules/gallery/views/admin_themes.html.php 38 DIRTY $info->name -modules/gallery/views/admin_themes.html.php 40 DIRTY $info->description -modules/gallery/views/admin_themes.html.php 60 DIRTY $themes[$admin]->name -modules/gallery/views/admin_themes.html.php 62 DIRTY $themes[$admin]->description -modules/gallery/views/admin_themes.html.php 76 DIRTY $info->name -modules/gallery/views/admin_themes.html.php 78 DIRTY $info->description +modules/gallery/views/admin_themes.html.php 39 DIRTY $info->name +modules/gallery/views/admin_themes.html.php 41 DIRTY $info->description +modules/gallery/views/admin_themes.html.php 62 DIRTY $themes[$admin]->name +modules/gallery/views/admin_themes.html.php 64 DIRTY $themes[$admin]->description +modules/gallery/views/admin_themes.html.php 79 DIRTY $info->name +modules/gallery/views/admin_themes.html.php 81 DIRTY $info->description +modules/gallery/views/admin_themes_buttonset.html.php 7 DIRTY_JS $info['author_url'] +modules/gallery/views/admin_themes_buttonset.html.php 14 DIRTY_ATTR $info['author_name'] +modules/gallery/views/admin_themes_buttonset.html.php 18 DIRTY $info['author_name'] +modules/gallery/views/admin_themes_buttonset.html.php 26 DIRTY_JS $info['info_url'] +modules/gallery/views/admin_themes_buttonset.html.php 39 DIRTY_JS $info['discuss_url'] modules/gallery/views/admin_themes_preview.html.php 8 DIRTY_ATTR $url modules/gallery/views/error_404.html.php 14 DIRTY $login_form modules/gallery/views/error_admin.html.php 178 DIRTY @gallery_block::get("platform_info") @@ -174,7 +185,8 @@ modules/gallery/views/form_uploadify.html.php 28 DIRTY_JS url::f modules/gallery/views/form_uploadify.html.php 29 DIRTY_JS url::site("uploader/add_photo/{$album->id}") modules/gallery/views/form_uploadify.html.php 33 DIRTY_JS url::file("lib/uploadify/cancel.png") modules/gallery/views/form_uploadify.html.php 34 DIRTY_JS $simultaneous_upload_limit -modules/gallery/views/form_uploadify.html.php 160 DIRTY_ATTR request::protocol() +modules/gallery/views/form_uploadify.html.php 35 DIRTY_JS $size_limit_bytes +modules/gallery/views/form_uploadify.html.php 162 DIRTY_ATTR request::protocol() modules/gallery/views/in_place_edit.html.php 2 DIRTY form::open($action,array("method"=>"post","id"=>"g-in-place-edit-form","class"=>"g-short-form")) modules/gallery/views/in_place_edit.html.php 3 DIRTY access::csrf_form_field() modules/gallery/views/in_place_edit.html.php 6 DIRTY form::input("input",$form["input"]," class=\"textbox\"") @@ -248,7 +260,7 @@ modules/gallery/views/permissions_form.html.php 80 DIRTY_JS $permi modules/gallery/views/permissions_form.html.php 80 DIRTY_JS $item->id modules/gallery/views/quick_delete_confirm.html.php 11 DIRTY $form modules/gallery/views/reauthenticate.html.php 9 DIRTY $form -modules/gallery/views/upgrade_checker_block.html.php 17 DIRTY $new_version +modules/gallery/views/upgrade_checker_block.html.php 19 DIRTY $new_version modules/gallery/views/upgrader.html.php 76 DIRTY_ATTR $done?"muted":"" modules/gallery/views/upgrader.html.php 94 DIRTY_ATTR $done?"muted":"" modules/gallery/views/upgrader.html.php 102 DIRTY_ATTR $module->version==$module->code_version?"current":"upgradeable" @@ -330,8 +342,9 @@ modules/search/views/search.html.php 27 DIRTY_ATTR $ite modules/search/views/search.html.php 28 DIRTY_JS $item->url() modules/search/views/search.html.php 29 DIRTY $item->thumb_img() modules/search/views/search.html.php 40 DIRTY $theme->paginator() -modules/server_add/views/admin_server_add.html.php 5 DIRTY $form -modules/server_add/views/admin_server_add.html.php 15 DIRTY_ATTR $id +modules/server_add/views/admin_server_add.html.php 8 DIRTY_JS url::site("__ARGS__") +modules/server_add/views/admin_server_add.html.php 19 DIRTY $form +modules/server_add/views/admin_server_add.html.php 30 DIRTY_ATTR $id modules/server_add/views/server_add_tree.html.php 20 DIRTY_ATTR is_dir($file)?"ui-icon-folder-collapsed":"ui-icon-document" modules/server_add/views/server_add_tree.html.php 21 DIRTY_ATTR is_dir($file)?"g-directory":"g-file" modules/server_add/views/server_add_tree_dialog.html.php 3 DIRTY_JS url::site("server_add/children?path=__PATH__") @@ -365,21 +378,21 @@ modules/watermark/views/admin_watermarks.html.php 20 DIRTY_ATTR $wid modules/watermark/views/admin_watermarks.html.php 20 DIRTY_ATTR $height modules/watermark/views/admin_watermarks.html.php 20 DIRTY_ATTR $url themes/admin_wind/views/admin.html.php 4 DIRTY $theme->html_attributes() -themes/admin_wind/views/admin.html.php 31 DIRTY $theme->admin_head() -themes/admin_wind/views/admin.html.php 40 DIRTY_JS $theme->url() -themes/admin_wind/views/admin.html.php 45 DIRTY $theme->get_combined("script") -themes/admin_wind/views/admin.html.php 48 DIRTY $theme->get_combined("css") -themes/admin_wind/views/admin.html.php 52 DIRTY $theme->admin_page_top() -themes/admin_wind/views/admin.html.php 60 DIRTY $theme->admin_header_top() -themes/admin_wind/views/admin.html.php 61 DIRTY_JS item::root()->url() -themes/admin_wind/views/admin.html.php 64 DIRTY $theme->user_menu() -themes/admin_wind/views/admin.html.php 67 DIRTY $theme->admin_menu() -themes/admin_wind/views/admin.html.php 70 DIRTY $theme->admin_header_bottom() -themes/admin_wind/views/admin.html.php 77 DIRTY $content -themes/admin_wind/views/admin.html.php 83 DIRTY $sidebar -themes/admin_wind/views/admin.html.php 88 DIRTY $theme->admin_footer() -themes/admin_wind/views/admin.html.php 91 DIRTY $theme->admin_credits() -themes/admin_wind/views/admin.html.php 96 DIRTY $theme->admin_page_bottom() +themes/admin_wind/views/admin.html.php 33 DIRTY $theme->admin_head() +themes/admin_wind/views/admin.html.php 42 DIRTY_JS $theme->url() +themes/admin_wind/views/admin.html.php 47 DIRTY $theme->get_combined("css") +themes/admin_wind/views/admin.html.php 50 DIRTY $theme->get_combined("script") +themes/admin_wind/views/admin.html.php 54 DIRTY $theme->admin_page_top() +themes/admin_wind/views/admin.html.php 62 DIRTY $theme->admin_header_top() +themes/admin_wind/views/admin.html.php 63 DIRTY_JS item::root()->url() +themes/admin_wind/views/admin.html.php 66 DIRTY $theme->user_menu() +themes/admin_wind/views/admin.html.php 69 DIRTY $theme->admin_menu() +themes/admin_wind/views/admin.html.php 72 DIRTY $theme->admin_header_bottom() +themes/admin_wind/views/admin.html.php 79 DIRTY $content +themes/admin_wind/views/admin.html.php 85 DIRTY $sidebar +themes/admin_wind/views/admin.html.php 90 DIRTY $theme->admin_footer() +themes/admin_wind/views/admin.html.php 93 DIRTY $theme->admin_credits() +themes/admin_wind/views/admin.html.php 98 DIRTY $theme->admin_page_bottom() themes/admin_wind/views/block.html.php 3 DIRTY_ATTR $anchor themes/admin_wind/views/block.html.php 5 DIRTY $id themes/admin_wind/views/block.html.php 5 DIRTY_ATTR $css_id @@ -414,19 +427,19 @@ themes/wind/views/page.html.php 4 DIRTY $theme themes/wind/views/page.html.php 10 DIRTY $page_title themes/wind/views/page.html.php 13 DIRTY $theme->item()->title themes/wind/views/page.html.php 17 DIRTY item::root()->title -themes/wind/views/page.html.php 31 DIRTY $new_width -themes/wind/views/page.html.php 32 DIRTY $new_height -themes/wind/views/page.html.php 33 DIRTY $thumb_proportion -themes/wind/views/page.html.php 70 DIRTY_JS $theme->url() -themes/wind/views/page.html.php 75 DIRTY $theme->get_combined("script") -themes/wind/views/page.html.php 78 DIRTY $theme->get_combined("css") -themes/wind/views/page.html.php 88 DIRTY $header_text -themes/wind/views/page.html.php 90 DIRTY_JS item::root()->url() -themes/wind/views/page.html.php 94 DIRTY $theme->user_menu() -themes/wind/views/page.html.php 115 DIRTY_JS $parent->url($parent->id==$theme->item()->parent_id?"show={$theme->item()->id}":null) -themes/wind/views/page.html.php 136 DIRTY $content -themes/wind/views/page.html.php 142 DIRTY newView("sidebar.html") -themes/wind/views/page.html.php 149 DIRTY $footer_text +themes/wind/views/page.html.php 32 DIRTY $new_width +themes/wind/views/page.html.php 33 DIRTY $new_height +themes/wind/views/page.html.php 34 DIRTY $thumb_proportion +themes/wind/views/page.html.php 71 DIRTY_JS $theme->url() +themes/wind/views/page.html.php 76 DIRTY $theme->get_combined("css") +themes/wind/views/page.html.php 79 DIRTY $theme->get_combined("script") +themes/wind/views/page.html.php 89 DIRTY $header_text +themes/wind/views/page.html.php 91 DIRTY_JS item::root()->url() +themes/wind/views/page.html.php 95 DIRTY $theme->user_menu() +themes/wind/views/page.html.php 116 DIRTY_JS $parent->url($parent->id==$theme->item()->parent_id?"show={$theme->item()->id}":null) +themes/wind/views/page.html.php 137 DIRTY $content +themes/wind/views/page.html.php 143 DIRTY newView("sidebar.html") +themes/wind/views/page.html.php 150 DIRTY $footer_text themes/wind/views/paginator.html.php 33 DIRTY_JS $first_page_url themes/wind/views/paginator.html.php 42 DIRTY_JS $previous_page_url themes/wind/views/paginator.html.php 70 DIRTY_JS $next_page_url -- cgit v1.2.3 From cbd04e1ae02491ab4c213fe03d62d12ca1546251 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Mon, 25 Apr 2011 23:26:43 -0700 Subject: Move random::string() to be test::random_string() and have it return a random string of any length. Also introduce test::lorem_ipsum() and use that in places where we want strings of words. Fixes #1713. --- modules/gallery/helpers/random.php | 10 +- modules/gallery/tests/Albums_Controller_Test.php | 2 +- modules/gallery/tests/File_Structure_Test.php | 1 + modules/gallery/tests/Gallery_Filters.php | 1 + modules/gallery_unit_test/helpers/test.php | 30 +- .../gallery_unit_test/vendor/LoremIpsum.class.php | 461 +++++++++++++++++++++ 6 files changed, 489 insertions(+), 16 deletions(-) create mode 100644 modules/gallery_unit_test/vendor/LoremIpsum.class.php (limited to 'modules/gallery/tests') diff --git a/modules/gallery/helpers/random.php b/modules/gallery/helpers/random.php index 6016df7b..06542e8c 100644 --- a/modules/gallery/helpers/random.php +++ b/modules/gallery/helpers/random.php @@ -19,21 +19,13 @@ */ class random_Core { /** - * Return a random 32 bit hash value. + * Return a random 32 byte hash value. * @param string extra entropy data */ static function hash($entropy="") { return md5($entropy . uniqid(mt_rand(), true)); } - /** - * Return a random hexadecimal string of the given length. - * @param int the desired length of the string - */ - static function string($length) { - return substr(random::hash(), 0, $length); - } - /** * Return a random floating point number between 0 and 1 */ diff --git a/modules/gallery/tests/Albums_Controller_Test.php b/modules/gallery/tests/Albums_Controller_Test.php index d9983cc2..2ff017d7 100644 --- a/modules/gallery/tests/Albums_Controller_Test.php +++ b/modules/gallery/tests/Albums_Controller_Test.php @@ -31,7 +31,7 @@ class Albums_Controller_Test extends Gallery_Unit_Test_Case { $album = test::random_album(); // Randomize to avoid conflicts. - $new_name = "new_name_" . random::string(6); + $new_name = "new_name_" . test::random_string(6); $_POST["name"] = $new_name; $_POST["title"] = "new title"; diff --git a/modules/gallery/tests/File_Structure_Test.php b/modules/gallery/tests/File_Structure_Test.php index 69c4bbf9..1d1ff5ce 100644 --- a/modules/gallery/tests/File_Structure_Test.php +++ b/modules/gallery/tests/File_Structure_Test.php @@ -101,6 +101,7 @@ class File_Structure_Test extends Gallery_Unit_Test_Case { $expected_4 = array("type = "album"; @@ -34,7 +34,7 @@ class test_Core { } static function random_photo_unsaved($parent=null) { - $rand = random::string(6); + $rand = test::random_string(6); $photo = ORM::factory("item"); $photo->type = "photo"; $photo->parent_id = $parent ? $parent->id : 1; @@ -49,16 +49,16 @@ class test_Core { } static function random_user($password="password") { - $rand = "name_" . random::string(6); + $rand = "name_" . test::random_string(6); return identity::create_user($rand, $rand, $password, "$rand@rand.com"); } static function random_group() { - return identity::create_group(random::string(6)); + return identity::create_group(test::random_string(6)); } static function random_name($item=null) { - $rand = "name_" . random::string(6); + $rand = "name_" . test::random_string(6); if ($item && $item->is_photo()) { $rand .= ".jpg"; } @@ -77,7 +77,7 @@ class test_Core { static function random_tag() { $tag = ORM::factory("tag"); - $tag->name = random::string(6); + $tag->name = test::lorem_ipsum(rand(2, 4)); // Reload so that ORM coerces all fields into strings. return $tag->save()->reload(); @@ -88,4 +88,22 @@ class test_Core { fwrite(fopen($b_name = tempnam("/tmp", "test"), "w"), $b); return `diff $a_name $b_name`; } + + static function random_string($length) { + $buf = ""; + do { + $buf .= random::hash(); + } while (strlen($buf) < $length); + return substr($buf, 0, $length); + } + + static function lorem_ipsum($num) { + static $lorem_ipsum = null; + if (!$lorem_ipsum) { + require_once(MODPATH . "gallery_unit_test/vendor/LoremIpsum.class.php"); + $lorem_ipsum = new LoremIpsumGenerator(); + } + // skip past initial 'lorem ipsum' + return substr($lorem_ipsum->getContent($num + 2, "txt"), 13); + } } diff --git a/modules/gallery_unit_test/vendor/LoremIpsum.class.php b/modules/gallery_unit_test/vendor/LoremIpsum.class.php new file mode 100644 index 00000000..07050fe5 --- /dev/null +++ b/modules/gallery_unit_test/vendor/LoremIpsum.class.php @@ -0,0 +1,461 @@ + BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + + private $words, $wordsPerParagraph, $wordsPerSentence; + + function __construct($wordsPer = 100) + { + $this->wordsPerParagraph = $wordsPer; + $this->wordsPerSentence = 24.460; + $this->words = array( + 'lorem', + 'ipsum', + 'dolor', + 'sit', + 'amet', + 'consectetur', + 'adipiscing', + 'elit', + 'curabitur', + 'vel', + 'hendrerit', + 'libero', + 'eleifend', + 'blandit', + 'nunc', + 'ornare', + 'odio', + 'ut', + 'orci', + 'gravida', + 'imperdiet', + 'nullam', + 'purus', + 'lacinia', + 'a', + 'pretium', + 'quis', + 'congue', + 'praesent', + 'sagittis', + 'laoreet', + 'auctor', + 'mauris', + 'non', + 'velit', + 'eros', + 'dictum', + 'proin', + 'accumsan', + 'sapien', + 'nec', + 'massa', + 'volutpat', + 'venenatis', + 'sed', + 'eu', + 'molestie', + 'lacus', + 'quisque', + 'porttitor', + 'ligula', + 'dui', + 'mollis', + 'tempus', + 'at', + 'magna', + 'vestibulum', + 'turpis', + 'ac', + 'diam', + 'tincidunt', + 'id', + 'condimentum', + 'enim', + 'sodales', + 'in', + 'hac', + 'habitasse', + 'platea', + 'dictumst', + 'aenean', + 'neque', + 'fusce', + 'augue', + 'leo', + 'eget', + 'semper', + 'mattis', + 'tortor', + 'scelerisque', + 'nulla', + 'interdum', + 'tellus', + 'malesuada', + 'rhoncus', + 'porta', + 'sem', + 'aliquet', + 'et', + 'nam', + 'suspendisse', + 'potenti', + 'vivamus', + 'luctus', + 'fringilla', + 'erat', + 'donec', + 'justo', + 'vehicula', + 'ultricies', + 'varius', + 'ante', + 'primis', + 'faucibus', + 'ultrices', + 'posuere', + 'cubilia', + 'curae', + 'etiam', + 'cursus', + 'aliquam', + 'quam', + 'dapibus', + 'nisl', + 'feugiat', + 'egestas', + 'class', + 'aptent', + 'taciti', + 'sociosqu', + 'ad', + 'litora', + 'torquent', + 'per', + 'conubia', + 'nostra', + 'inceptos', + 'himenaeos', + 'phasellus', + 'nibh', + 'pulvinar', + 'vitae', + 'urna', + 'iaculis', + 'lobortis', + 'nisi', + 'viverra', + 'arcu', + 'morbi', + 'pellentesque', + 'metus', + 'commodo', + 'ut', + 'facilisis', + 'felis', + 'tristique', + 'ullamcorper', + 'placerat', + 'aenean', + 'convallis', + 'sollicitudin', + 'integer', + 'rutrum', + 'duis', + 'est', + 'etiam', + 'bibendum', + 'donec', + 'pharetra', + 'vulputate', + 'maecenas', + 'mi', + 'fermentum', + 'consequat', + 'suscipit', + 'aliquam', + 'habitant', + 'senectus', + 'netus', + 'fames', + 'quisque', + 'euismod', + 'curabitur', + 'lectus', + 'elementum', + 'tempor', + 'risus', + 'cras' ); + } + + function getContent($count, $format = 'html', $loremipsum = true) + { + $format = strtolower($format); + + if($count <= 0) + return ''; + + switch($format) + { + case 'txt': + return $this->getText($count, $loremipsum); + case 'plain': + return $this->getPlain($count, $loremipsum); + default: + return $this->getHTML($count, $loremipsum); + } + } + + private function getWords(&$arr, $count, $loremipsum) + { + $i = 0; + if($loremipsum) + { + $i = 2; + $arr[0] = 'lorem'; + $arr[1] = 'ipsum'; + } + + for($i; $i < $count; $i++) + { + $index = array_rand($this->words); + $word = $this->words[$index]; + //echo $index . '=>' . $word . '
'; + + if($i > 0 && $arr[$i - 1] == $word) + $i--; + else + $arr[$i] = $word; + } + } + + private function getPlain($count, $loremipsum, $returnStr = true) + { + $words = array(); + $this->getWords($words, $count, $loremipsum); + //print_r($words); + + $delta = $count; + $curr = 0; + $sentences = array(); + while($delta > 0) + { + $senSize = $this->gaussianSentence(); + //echo $curr . '
'; + if(($delta - $senSize) < 4) + $senSize = $delta; + + $delta -= $senSize; + + $sentence = array(); + for($i = $curr; $i < ($curr + $senSize); $i++) + $sentence[] = $words[$i]; + + $this->punctuate($sentence); + $curr = $curr + $senSize; + $sentences[] = $sentence; + } + + if($returnStr) + { + $output = ''; + foreach($sentences as $s) + foreach($s as $w) + $output .= $w . ' '; + + return $output; + } + else + return $sentences; + } + + private function getText($count, $loremipsum) + { + $sentences = $this->getPlain($count, $loremipsum, false); + $paragraphs = $this->getParagraphArr($sentences); + + $paragraphStr = array(); + foreach($paragraphs as $p) + { + $paragraphStr[] = $this->paragraphToString($p); + } + + $paragraphStr[0] = "\t" . $paragraphStr[0]; + return implode("\n\n\t", $paragraphStr); + } + + private function getParagraphArr($sentences) + { + $wordsPer = $this->wordsPerParagraph; + $sentenceAvg = $this->wordsPerSentence; + $total = count($sentences); + + $paragraphs = array(); + $pCount = 0; + $currCount = 0; + $curr = array(); + + for($i = 0; $i < $total; $i++) + { + $s = $sentences[$i]; + $currCount += count($s); + $curr[] = $s; + if($currCount >= ($wordsPer - round($sentenceAvg / 2.00)) || $i == $total - 1) + { + $currCount = 0; + $paragraphs[] = $curr; + $curr = array(); + //print_r($paragraphs); + } + //print_r($paragraphs); + } + + return $paragraphs; + } + + private function getHTML($count, $loremipsum) + { + $sentences = $this->getPlain($count, $loremipsum, false); + $paragraphs = $this->getParagraphArr($sentences); + //print_r($paragraphs); + + $paragraphStr = array(); + foreach($paragraphs as $p) + { + $paragraphStr[] = "

\n" . $this->paragraphToString($p, true) . '

'; + } + + //add new lines for the sake of clean code + return implode("\n", $paragraphStr); + } + + private function paragraphToString($paragraph, $htmlCleanCode = false) + { + $paragraphStr = ''; + foreach($paragraph as $sentence) + { + foreach($sentence as $word) + $paragraphStr .= $word . ' '; + + if($htmlCleanCode) + $paragraphStr .= "\n"; + } + return $paragraphStr; + } + + /* + * Inserts commas and periods in the given + * word array. + */ + private function punctuate(& $sentence) + { + $count = count($sentence); + $sentence[$count - 1] = $sentence[$count - 1] . '.'; + + if($count < 4) + return $sentence; + + $commas = $this->numberOfCommas($count); + + for($i = 1; $i <= $commas; $i++) + { + $index = (int) round($i * $count / ($commas + 1)); + + if($index < ($count - 1) && $index > 0) + { + $sentence[$index] = $sentence[$index] . ','; + } + } + } + + /* + * Determines the number of commas for a + * sentence of the given length. Average and + * standard deviation are determined superficially + */ + private function numberOfCommas($len) + { + $avg = (float) log($len, 6); + $stdDev = (float) $avg / 6.000; + + return (int) round($this->gauss_ms($avg, $stdDev)); + } + + /* + * Returns a number on a gaussian distribution + * based on the average word length of an english + * sentence. + * Statistics Source: + * http://hearle.nahoo.net/Academic/Maths/Sentence.html + * Average: 24.46 + * Standard Deviation: 5.08 + */ + private function gaussianSentence() + { + $avg = (float) 24.460; + $stdDev = (float) 5.080; + + return (int) round($this->gauss_ms($avg, $stdDev)); + } + + /* + * The following three functions are used to + * compute numbers with a guassian distrobution + * Source: + * http://us.php.net/manual/en/function.rand.php#53784 + */ + private function gauss() + { // N(0,1) + // returns random number with normal distribution: + // mean=0 + // std dev=1 + + // auxilary vars + $x=$this->random_0_1(); + $y=$this->random_0_1(); + + // two independent variables with normal distribution N(0,1) + $u=sqrt(-2*log($x))*cos(2*pi()*$y); + $v=sqrt(-2*log($x))*sin(2*pi()*$y); + + // i will return only one, couse only one needed + return $u; + } + + private function gauss_ms($m=0.0,$s=1.0) + { + return $this->gauss()*$s+$m; + } + + private function random_0_1() + { + return (float)rand()/(float)getrandmax(); + } + +} \ No newline at end of file -- cgit v1.2.3