51Degrees.mobi and place the unzipped contents in '. dirname(__FILE__).'.'); } /** * Retrieves the licence key if available. * * @param string $dir * Directory to search for licence file. * @return array * Array of licence keys. Empty if no licence. */ function fiftyone_degrees_get_licenses($dir) { $licenses = array(); foreach (glob($dir . '/*.lic') as $file) { $licenses[] = file_get_contents($file); } return $licenses; } /** * Adds php hash codes to working list. * * @param string $dir * Update working directory. * @param string &$php_current * List of hash codes to be added to. */ function fiftyone_degrees_get_php_file_arrays($dir, &$php_current) { $update_hashes_file = $dir . '/UpdateHash.txt'; $update_lengths_file = $dir . '/UpdateLengths.txt'; $current_hash = ""; if (fiftyone_degrees_write_update_hash_file($dir, $current_hash)) { $php_current .= $current_hash; return true; } else return false; } /** * Records current hash codes in working file. * * @param string $dir * Update working directory. * @param string &$current_hash * Current hashcode string */ function fiftyone_degrees_write_update_hash_file($dir, &$current_hash) { $dirlengths = ""; $filename = $dir . '/UpdateHash.txt'; fiftyone_degrees_php_file_arrays_processor($dir, $current_hash, $dirlengths); $fh = fopen($filename, 'w'); fwrite($fh, $current_hash); fclose($fh); $dirlengths = fiftyone_degrees_shorten_lengths_var($dirlengths); $fh = @fopen($dir . '/UpdateLengths.txt', 'w+'); fwrite($fh, $dirlengths); fclose($fh); if (!file_exists($filename) || !file_exists($dir . '/UpdateLengths.txt')) { fiftyone_degrees_WriteMessage('You do not have permissions set to update these files. Please change your permissions and try again.'); return false; } return true; } /** * Adds hashcodes for files in the target directory. * * @param string $dir * Update working directory. * @param string &$current_hash * Current hashcode string * @param int &$dirlengths * * @param bool $is_subdir * */ function fiftyone_degrees_php_file_arrays_processor($dir, &$current_hash, &$dirlengths) { foreach (glob($dir . '/*') as $file) { if (is_dir($file)) { fiftyone_degrees_php_file_arrays_processor($file, $current_hash, $dirlengths); } else { $file_no_dir = str_replace(dirname(__FILE__), "", $file); // check that no excluded files are submitted if( ends_with($file_no_dir, ".zip") == false && ends_with($file_no_dir, ".lic") == false && ends_with($file_no_dir, ".oldphp") == false && $file_no_dir != "UpdateHash.txt" && $file_no_dir != "UpdateLengths.txt" && $file_no_dir != "51DUpdate.php") { $this_hash = $file_no_dir . md5_file($file); $dirlengths .= (strlen($this_hash) - 32) . '-'; $current_hash .= $this_hash; } } } } function ends_with($string, $test) { $strlen = strlen($string); $testlen = strlen($test); if ($testlen > $strlen) return false; return substr_compare($string, $test, -$testlen) === 0; } /** * Unpacks the length of new files. * * @param string $dirlengths * File lengths, hyphen seperated. * @return array * Pipe seperated list of lengths. */ function fiftyone_degrees_shorten_lengths_var($dirlengths) { $ctr = 0; $vals = array(); $prev = 0; foreach (explode('-', $dirlengths) as $len) { if ($prev != 0 && $len != $prev) { $vals[] = $prev . '*' . $ctr; $ctr = 1; $prev = $len; } elseif ($prev != 0) { $ctr++; } else { $prev = $len; $ctr++; } } return implode('|', $vals); } /** * Downloads the update file content. Returns a string containing update data if successful, false if there was a failure and "NoData" if there is no * new data to add. * * @param string $url * URL to download update from. * @param string $data * Current file hashcodes. * @param string $dir * Working directory. * @param $optional_headers * Optional HTTP headers. * @return string * New file content. */ function fiftyone_degrees_download($url, $data, $dir, $optional_headers = NULL) { $params = array( 'http' => array( 'method' => 'POST', 'header' => "Content-type: application/x-www-form-urlencoded\r\n" ."Content-Length: ".strlen($data)."\r\n" ."Hash-Length: " . file_get_contents($dir . "/UpdateLengths.txt") . "\r\n", 'content' => $data ) ); if ($optional_headers != NULL) { $params['http']['header'] = $optional_headers; } $ctx = stream_context_create($params); stream_context_set_params($ctx, array("notification" => "fiftyone_degrees_Stream_Notification")); try { ini_set('user_agent', $_SERVER['HTTP_USER_AGENT']); $fp = @fopen($url, 'rb', FALSE, $ctx); $response = @stream_get_contents($fp); } catch (Exception $e) { fiftyone_degrees_WriteMessage('There was a problem trying to run the update. Exception: ' . $e->getMessage()); return false; } fiftyone_degrees_WriteMessage('Download complete.'); if($response == "") return "NoData"; return $response; } /** * Provides notification messages during update download. */ function fiftyone_degrees_Stream_Notification($notification_code, $severity, $message, $message_code, $bytes_transferred, $bytes_max) { global $_51d_download_percentage; switch($notification_code) { case STREAM_NOTIFY_RESOLVE: case STREAM_NOTIFY_AUTH_REQUIRED: case STREAM_NOTIFY_FAILURE: case STREAM_NOTIFY_AUTH_RESULT: case STREAM_NOTIFY_REDIRECTED: case STREAM_NOTIFY_MIME_TYPE_IS: case STREAM_NOTIFY_COMPLETED: break; case STREAM_NOTIFY_CONNECT: fiftyone_degrees_WriteMessage('Connecting to upgrade server...'); $_51d_download_percentage = 0; break; case STREAM_NOTIFY_FILE_SIZE_IS: // get MB if($bytes_max == 0) $mbytes = 0.00; else $mbytes = number_format($bytes_max / 1000000, 2); fiftyone_degrees_WriteMessage("Downloading $mbytes MB."); break; case STREAM_NOTIFY_PROGRESS: $percentage = ($bytes_transferred / $bytes_max) * 100; if($percentage > $_51d_download_percentage + 10) { $_51d_download_percentage += 10; fiftyone_degrees_WriteMessage("Download ".$_51d_download_percentage."% complete."); } break; } } /** * Initiates the update process. * * @param $php_current * Current hashcode of current files. * @param &$update_file * The update file content. * @param $url * URL to download the update file from. * @param $dir * The working directory. */ function fiftyone_degrees_do_update($php_current, &$update_file, $url, $dir) { fiftyone_degrees_WriteMessage('Updating files...'); //fiftyone_degrees_double_check($php_current, $update_file, $url, $dir); $linevars = explode('!]', $update_file); array_pop($linevars); $update_length = count($linevars); $counter = 0; $percent_complete = 0; $delete_files = array(); foreach ($linevars as $value) { $vals = explode('|', $value); if (trim($vals[0]) != "[!DELETE") { $text = array(); for ($i = 2; $i < count($vals); $i++) { $text[] = $vals[$i]; } $res = fiftyone_degrees_create_update_file($dir . '/' . $vals[1], implode('|', $text)); if ($res != "") { fiftyone_degrees_rollback_update($dir); fiftyone_degrees_WriteMessage('Problem occured while updating - ' . $res); fiftyone_degrees_WriteMessage('Rolling back data.'); } } else { $delete_files[] = $vals[1]; } $counter++; $percent = number_format(($counter / $update_length) * 100, 0); if($percent > $percent_complete + 10) { $percent_complete += 10; fiftyone_degrees_WriteMessage("File updating ".$percent_complete."% complete."); } } fiftyone_degrees_WriteMessage('File update complete.'); fiftyone_degrees_WriteMessage('Removing old files...'); try { fiftyone_degrees_delete_marked($delete_files, $dir); } catch (Exception $e) { fiftyone_degrees_WriteMessage('Problem occured while updating - ' . $e->getMessage()); } fiftyone_degrees_remove_old_files($dir); fiftyone_degrees_WriteMessage('File removal complete.'); $current_hash = ""; fiftyone_degrees_write_update_hash_file($dir, $current_hash); } /** * Ensures the latest update file will be used. * * @param $php_current * Current file hash codes. * @param &$update_file * Update file content. * @param $url * URL to download update from * @param $dir * Directory to store update file */ function fiftyone_degrees_double_check($php_current, &$update_file, $url, $dir) { $checking = fiftyone_degrees_download($url, $php_current, $dir); fiftyone_degrees_WriteMesssage("Finished checking."); if ($checking != $update_file) { $update_file = $checking; fiftyone_degrees_double_check($php_current, $update_file, $url, $dir); } } /** * Creates a new file containing updated content. * * @param string $file_directory * Full path of file. * @param string $file_contents * Contents to update. */ function fiftyone_degrees_create_update_file($file_directory, $file_contents) { try { $directory = explode('\\', $file_directory); $checking_dir = ""; for ($i = 0; $i < count($directory) - 1; $i++) { $checking_dir .= $directory[$i] . '/'; if (!is_dir($checking_dir)) { mkdir($checking_dir); } } if (file_exists($file_directory)) { $fh = @fopen($file_directory . 'upd', 'w+', FALSE) or die("Problem creating file: your permissions will not allow writing."); fwrite($fh, $file_contents); fclose($fh); if (file_exists($file_directory)) { rename($file_directory, $file_directory . 'old'); } rename($file_directory . 'upd', $file_directory); } else { $fh = @fopen($file_directory, 'w+', FALSE) or die("Problem creating file: your permissions will not allow writing."); fwrite($fh, $file_contents); fclose($fh); } return ""; } catch (Exception $e) { return $e->getMessage(); } } /** * Removes files during update. * * @param array $delete_files * Array of files to remove. */ function fiftyone_degrees_delete_marked($delete_files, $dir) { foreach ($delete_files as $to_delete) { if (file_exists($dir . '/' . $to_delete)) { unlink($dir . '/' . $to_delete); } } } /** * Rolls back to previous version. * * @param string $dir * Target directory. */ function fiftyone_degrees_rollback_update($dir) { foreach (glob($dir . '/*') as $file) { if (is_dir($file)) { fiftyone_degrees_rollback_update($file); } else { if (substr($file, strlen($file) - 3, strlen($file)) == "old") { try { rename($file, substr($file, 0, strlen($file) - 3)); } catch (Exception $e) { } } } } } /** * Removes old files after update. * * @param string $dir * Target directory. */ function fiftyone_degrees_remove_old_files($dir) { foreach (glob($dir . '/*') as $file) { if (is_dir($file)) { fiftyone_degrees_remove_old_files($file); } else { if (substr($file, strlen($file) - 3, strlen($file)) == "old") { try { unlink($file); } catch (Exception $e) { } } } } } /** * Writes a status message to the output stream. * * $message string * The message to the written. */ function fiftyone_degrees_WriteMessage($message) { if((isset($_51d_suppress_update_output) && $_51d_suppress_update_output == true) == false) { echo $message . "\r\n"; @flush(); } }